blob: 7a70af450ca37d4fbcbef5d27c39c9507ce30b31 [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"},
476 {(int)KS_RFG, "\033]10;?\007"},
477 {(int)KS_RBG, "\033]11;?\007"},
478 {(int)KS_U7, "\033[6n"},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200479# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100480 // These are printf strings, not terminal codes.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000481 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
482 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
483 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200484# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000485 {(int)KS_CAU, "\033[58;5;%dm"},
486 {(int)KS_CBE, "\033[?2004h"},
487 {(int)KS_CBD, "\033[?2004l"},
488 {(int)KS_CST, "\033[22;2t"},
489 {(int)KS_CRT, "\033[23;2t"},
490 {(int)KS_SSI, "\033[22;1t"},
491 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100492# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000493 {(int)KS_FD, "\033[?1004l"},
494 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100495# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000496
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000497 {K_UP, "\033O*A"},
498 {K_DOWN, "\033O*B"},
499 {K_RIGHT, "\033O*C"},
500 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100501 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100502 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
503 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
504 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
505 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100506 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000507 {K_XF1, "\033O*P"},
508 {K_XF2, "\033O*Q"},
509 {K_XF3, "\033O*R"},
510 {K_XF4, "\033O*S"},
511 {K_F1, "\033[11;*~"},
512 {K_F2, "\033[12;*~"},
513 {K_F3, "\033[13;*~"},
514 {K_F4, "\033[14;*~"},
515 {K_F5, "\033[15;*~"},
516 {K_F6, "\033[17;*~"},
517 {K_F7, "\033[18;*~"},
518 {K_F8, "\033[19;*~"},
519 {K_F9, "\033[20;*~"},
520 {K_F10, "\033[21;*~"},
521 {K_F11, "\033[23;*~"},
522 {K_F12, "\033[24;*~"},
523 {K_S_TAB, "\033[Z"},
524 {K_HELP, "\033[28;*~"},
525 {K_UNDO, "\033[26;*~"},
526 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100527 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000528 // {K_S_HOME, "\033O2H"},
529 // {K_C_HOME, "\033O5H"},
530 {K_KHOME, "\033[1;*~"},
531 {K_XHOME, "\033O*H"}, // other Home
532 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100533 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000534 // {K_S_END, "\033O2F"},
535 // {K_C_END, "\033O5F"},
536 {K_KEND, "\033[4;*~"},
537 {K_XEND, "\033O*F"}, // other End
538 {K_ZEND, "\033[8;*~"},
539 {K_PAGEUP, "\033[5;*~"},
540 {K_PAGEDOWN, "\033[6;*~"},
541 {K_KPLUS, "\033O*k"}, // keypad plus
542 {K_KMINUS, "\033O*m"}, // keypad minus
543 {K_KDIVIDE, "\033O*o"}, // keypad /
544 {K_KMULTIPLY, "\033O*j"}, // keypad *
545 {K_KENTER, "\033O*M"}, // keypad Enter
546 {K_KPOINT, "\033O*n"}, // keypad .
547 {K_K0, "\033O*p"}, // keypad 0
548 {K_K1, "\033O*q"}, // keypad 1
549 {K_K2, "\033O*r"}, // keypad 2
550 {K_K3, "\033O*s"}, // keypad 3
551 {K_K4, "\033O*t"}, // keypad 4
552 {K_K5, "\033O*u"}, // keypad 5
553 {K_K6, "\033O*v"}, // keypad 6
554 {K_K7, "\033O*w"}, // keypad 7
555 {K_K8, "\033O*x"}, // keypad 8
556 {K_K9, "\033O*y"}, // keypad 9
557 {K_KDEL, "\033[3;*~"}, // keypad Del
558 {K_PS, "\033[200~"}, // paste start
559 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
561 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000562 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
563 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100564 // F14 and F15 are missing, because they send the same codes as the undo
565 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000566 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
567 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
568 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
569 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
570 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000571
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000572 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
573 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
574 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
575 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
576 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
577 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
578 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
579 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
580 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
581 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000582
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000583 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
584 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
585 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
586 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
587 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
588 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
589 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
Bram Moolenaar4654d632022-11-17 22:05:12 +0000591 {(int)KS_NAME, NULL} // end marker
592};
593
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000595 * Additions for using modifyOtherKeys level 2. Same as what is used for
596 * xterm.
597 */
598static tcap_entry_T builtin_mok2[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000599 // t_TI enables modifyOtherKeys level 2
600 {(int)KS_CTI, "\033[>4;2m"},
601
Bram Moolenaarc255b782022-11-26 19:16:48 +0000602 // XTQMODKEYS was added in xterm version 377: "CSI ? 4 m" which should
603 // return "{lead} > 4 ; Pv m". Before version 377 we expect it to have no
604 // effect.
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000605 {(int)KS_CRK, "\033[?4m"},
606
607 // t_TE disables modifyOtherKeys
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000608 {(int)KS_CTE, "\033[>4;m"},
609
610 {(int)KS_NAME, NULL} // end marker
611};
612
613/*
614 * Additions for using the Kitty keyboard protocol.
615 */
616static tcap_entry_T builtin_kitty[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000617 // t_TI enables the kitty keyboard protocol.
618 {(int)KS_CTI, "\033[=1;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000619
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000620 // t_RK requests the kitty keyboard protocol state
621 {(int)KS_CRK, "\033[?u"},
622
623 // t_TE also disables modifyOtherKeys, because t_TI from xterm may already
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000624 // have been used.
Bram Moolenaara87749e2022-11-30 10:23:17 +0000625 {(int)KS_CTE, "\033[>4;m\033[=0;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000626
627 {(int)KS_NAME, NULL} // end marker
628};
629
630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 * iris-ansi for Silicon Graphics machines.
632 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000633static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 {(int)KS_CE, "\033[K"},
635 {(int)KS_CD, "\033[J"},
636 {(int)KS_AL, "\033[L"},
637# ifdef TERMINFO
638 {(int)KS_CAL, "\033[%p1%dL"},
639# else
640 {(int)KS_CAL, "\033[%dL"},
641# endif
642 {(int)KS_DL, "\033[M"},
643# ifdef TERMINFO
644 {(int)KS_CDL, "\033[%p1%dM"},
645# else
646 {(int)KS_CDL, "\033[%dM"},
647# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100648#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649# ifdef TERMINFO
650 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
651# else
652 {(int)KS_CS, "\033[%i%d;%dr"},
653# endif
654#endif
655 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100656 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
657 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 {(int)KS_TI, "\033[=6h"},
659 {(int)KS_TE, "\033[=6l"},
660 {(int)KS_SE, "\033[21;27m"},
661 {(int)KS_SO, "\033[1;7m"},
662 {(int)KS_ME, "\033[m"},
663 {(int)KS_MR, "\033[7m"},
664 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100665 {(int)KS_CCO, "8"}, // allow 8 colors
666 {(int)KS_CZH, "\033[3m"}, // italic mode on
667 {(int)KS_CZR, "\033[23m"}, // italic mode off
668 {(int)KS_US, "\033[4m"}, // underline on
669 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100671 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
672 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
673 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
674 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100676 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
677 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
678 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
679 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100681 {(int)KS_MS, "y"}, // guessed
682 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {(int)KS_LE, "\b"},
684# ifdef TERMINFO
685 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
686# else
687 {(int)KS_CM, "\033[%i%d;%dH"},
688# endif
689 {(int)KS_SR, "\033M"},
690# ifdef TERMINFO
691 {(int)KS_CRI, "\033[%p1%dC"},
692# else
693 {(int)KS_CRI, "\033[%dC"},
694# endif
695 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100696 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100698 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699# ifdef TERMINFO
700 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
701 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
702# else
703 {(int)KS_CWS, "\033[203;%d;%d/y"},
704 {(int)KS_CWP, "\033[205;%d;%d/y"},
705# endif
706 {K_UP, "\033[A"},
707 {K_DOWN, "\033[B"},
708 {K_LEFT, "\033[D"},
709 {K_RIGHT, "\033[C"},
710 {K_S_UP, "\033[161q"},
711 {K_S_DOWN, "\033[164q"},
712 {K_S_LEFT, "\033[158q"},
713 {K_S_RIGHT, "\033[167q"},
714 {K_F1, "\033[001q"},
715 {K_F2, "\033[002q"},
716 {K_F3, "\033[003q"},
717 {K_F4, "\033[004q"},
718 {K_F5, "\033[005q"},
719 {K_F6, "\033[006q"},
720 {K_F7, "\033[007q"},
721 {K_F8, "\033[008q"},
722 {K_F9, "\033[009q"},
723 {K_F10, "\033[010q"},
724 {K_F11, "\033[011q"},
725 {K_F12, "\033[012q"},
726 {K_S_F1, "\033[013q"},
727 {K_S_F2, "\033[014q"},
728 {K_S_F3, "\033[015q"},
729 {K_S_F4, "\033[016q"},
730 {K_S_F5, "\033[017q"},
731 {K_S_F6, "\033[018q"},
732 {K_S_F7, "\033[019q"},
733 {K_S_F8, "\033[020q"},
734 {K_S_F9, "\033[021q"},
735 {K_S_F10, "\033[022q"},
736 {K_S_F11, "\033[023q"},
737 {K_S_F12, "\033[024q"},
738 {K_INS, "\033[139q"},
739 {K_HOME, "\033[H"},
740 {K_END, "\033[146q"},
741 {K_PAGEUP, "\033[150q"},
742 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
Bram Moolenaar4654d632022-11-17 22:05:12 +0000744 {(int)KS_NAME, NULL} // end marker
745};
746
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000748 * These codes are valid when nansi.sys or equivalent has been installed.
749 * Function keys on a PC are preceded with a NUL. These are converted into
750 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
751 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000753static tcap_entry_T builtin_pcansi[] = {
754 {(int)KS_DL, "\033[M"},
755 {(int)KS_AL, "\033[L"},
756 {(int)KS_CE, "\033[K"},
757 {(int)KS_CL, "\033[2J"},
758 {(int)KS_ME, "\033[0m"},
759 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
760 {(int)KS_MD, "\033[1m"}, // bold: white text
761 {(int)KS_SE, "\033[0m"}, // standout end
762 {(int)KS_SO, "\033[31m"}, // standout: white on blue
763 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
764 {(int)KS_CZR, "\033[0m"}, // italic mode end
765 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
766 {(int)KS_UE, "\033[0m"}, // underscore mode end
767 {(int)KS_CCO, "8"}, // allow 8 colors
768# ifdef TERMINFO
769 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
770 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
771# else
772 {(int)KS_CAB, "\033[4%dm"}, // set background color
773 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
774# endif
775 {(int)KS_OP, "\033[0m"}, // reset colors
776 {(int)KS_MS, "y"},
777 {(int)KS_UT, "y"}, // guessed
778 {(int)KS_LE, "\b"},
779# ifdef TERMINFO
780 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
781# else
782 {(int)KS_CM, "\033[%i%d;%dH"},
783# endif
784# ifdef TERMINFO
785 {(int)KS_CRI, "\033[%p1%dC"},
786# else
787 {(int)KS_CRI, "\033[%dC"},
788# endif
789 {K_UP, "\316H"},
790 {K_DOWN, "\316P"},
791 {K_LEFT, "\316K"},
792 {K_RIGHT, "\316M"},
793 {K_S_LEFT, "\316s"},
794 {K_S_RIGHT, "\316t"},
795 {K_F1, "\316;"},
796 {K_F2, "\316<"},
797 {K_F3, "\316="},
798 {K_F4, "\316>"},
799 {K_F5, "\316?"},
800 {K_F6, "\316@"},
801 {K_F7, "\316A"},
802 {K_F8, "\316B"},
803 {K_F9, "\316C"},
804 {K_F10, "\316D"},
805 {K_F11, "\316\205"}, // guessed
806 {K_F12, "\316\206"}, // guessed
807 {K_S_F1, "\316T"},
808 {K_S_F2, "\316U"},
809 {K_S_F3, "\316V"},
810 {K_S_F4, "\316W"},
811 {K_S_F5, "\316X"},
812 {K_S_F6, "\316Y"},
813 {K_S_F7, "\316Z"},
814 {K_S_F8, "\316["},
815 {K_S_F9, "\316\\"},
816 {K_S_F10, "\316]"},
817 {K_S_F11, "\316\207"}, // guessed
818 {K_S_F12, "\316\210"}, // guessed
819 {K_INS, "\316R"},
820 {K_DEL, "\316S"},
821 {K_HOME, "\316G"},
822 {K_END, "\316O"},
823 {K_PAGEDOWN, "\316Q"},
824 {K_PAGEUP, "\316I"},
825
826 {(int)KS_NAME, NULL} // end marker
827};
828
829/*
830 * These codes are valid for the Win32 Console . The entries that start with
831 * ESC | are translated into console calls in os_win32.c. The function keys
832 * are also translated in os_win32.c.
833 */
834static tcap_entry_T builtin_win32[] = {
835 {(int)KS_CE, "\033|K"}, // clear to end of line
836 {(int)KS_AL, "\033|L"}, // add new blank line
837# ifdef TERMINFO
838 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
839# else
840 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
841# endif
842 {(int)KS_DL, "\033|M"}, // delete line
843# ifdef TERMINFO
844 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
845 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
846# else
847 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
848 {(int)KS_CSV, "\033|%d;%dV"},
849# endif
850 {(int)KS_CL, "\033|J"}, // clear screen
851 {(int)KS_CD, "\033|j"}, // clear to end of display
852 {(int)KS_VI, "\033|v"}, // cursor invisible
853 {(int)KS_VE, "\033|V"}, // cursor visible
854
855 {(int)KS_ME, "\033|0m"}, // normal
856 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
857 {(int)KS_MD, "\033|15m"}, // bold: white on black
858#if 1
859 {(int)KS_SO, "\033|31m"}, // standout: white on blue
860 {(int)KS_SE, "\033|0m"}, // standout end
861#else
862 {(int)KS_SO, "\033|F"}, // standout: high intensity
863 {(int)KS_SE, "\033|f"}, // standout end
864#endif
865 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
866 {(int)KS_CZR, "\033|0m"}, // italic end
867 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
868 {(int)KS_UE, "\033|0m"}, // underscore end
869 {(int)KS_CCO, "16"}, // allow 16 colors
870# ifdef TERMINFO
871 {(int)KS_CAB, "\033|%p1%db"}, // set background color
872 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
873# else
874 {(int)KS_CAB, "\033|%db"}, // set background color
875 {(int)KS_CAF, "\033|%df"}, // set foreground color
876# endif
877
878 {(int)KS_MS, "y"}, // save to move cur in reverse mode
879 {(int)KS_UT, "y"},
880 {(int)KS_XN, "y"},
881 {(int)KS_LE, "\b"},
882# ifdef TERMINFO
883 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
884# else
885 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
886# endif
887 {(int)KS_VB, "\033|B"}, // visual bell
888 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
889 {(int)KS_TE, "\033|E"}, // out of termcap mode
890# ifdef TERMINFO
891 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
892# else
893 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
894# endif
895# ifdef FEAT_TERMGUICOLORS
896 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
897 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
898# endif
899
900 {K_UP, "\316H"},
901 {K_DOWN, "\316P"},
902 {K_LEFT, "\316K"},
903 {K_RIGHT, "\316M"},
904 {K_S_UP, "\316\304"},
905 {K_S_DOWN, "\316\317"},
906 {K_S_LEFT, "\316\311"},
907 {K_C_LEFT, "\316s"},
908 {K_S_RIGHT, "\316\313"},
909 {K_C_RIGHT, "\316t"},
910 {K_S_TAB, "\316\017"},
911 {K_F1, "\316;"},
912 {K_F2, "\316<"},
913 {K_F3, "\316="},
914 {K_F4, "\316>"},
915 {K_F5, "\316?"},
916 {K_F6, "\316@"},
917 {K_F7, "\316A"},
918 {K_F8, "\316B"},
919 {K_F9, "\316C"},
920 {K_F10, "\316D"},
921 {K_F11, "\316\205"},
922 {K_F12, "\316\206"},
923 {K_S_F1, "\316T"},
924 {K_S_F2, "\316U"},
925 {K_S_F3, "\316V"},
926 {K_S_F4, "\316W"},
927 {K_S_F5, "\316X"},
928 {K_S_F6, "\316Y"},
929 {K_S_F7, "\316Z"},
930 {K_S_F8, "\316["},
931 {K_S_F9, "\316\\"},
932 {K_S_F10, "\316]"},
933 {K_S_F11, "\316\207"},
934 {K_S_F12, "\316\210"},
935 {K_INS, "\316R"},
936 {K_DEL, "\316S"},
937 {K_HOME, "\316G"},
938 {K_S_HOME, "\316\302"},
939 {K_C_HOME, "\316w"},
940 {K_END, "\316O"},
941 {K_S_END, "\316\315"},
942 {K_C_END, "\316u"},
943 {K_PAGEDOWN, "\316Q"},
944 {K_PAGEUP, "\316I"},
945 {K_KPLUS, "\316N"},
946 {K_KMINUS, "\316J"},
947 {K_KMULTIPLY, "\316\067"},
948 {K_K0, "\316\332"},
949 {K_K1, "\316\336"},
950 {K_K2, "\316\342"},
951 {K_K3, "\316\346"},
952 {K_K4, "\316\352"},
953 {K_K5, "\316\356"},
954 {K_K6, "\316\362"},
955 {K_K7, "\316\366"},
956 {K_K8, "\316\372"},
957 {K_K9, "\316\376"},
958 {K_BS, "\316x"},
959 {K_S_BS, "\316y"},
960
961 {(int)KS_NAME, NULL} // end marker
962};
963
964#if defined(FEAT_GUI)
965/*
966 * GUI uses made-up codes, only used inside Vim.
967 */
968static tcap_entry_T builtin_gui[] = {
969 {(int)KS_CE, "\033|$"},
970 {(int)KS_AL, "\033|i"},
971# ifdef TERMINFO
972 {(int)KS_CAL, "\033|%p1%dI"},
973# else
974 {(int)KS_CAL, "\033|%dI"},
975# endif
976 {(int)KS_DL, "\033|d"},
977# ifdef TERMINFO
978 {(int)KS_CDL, "\033|%p1%dD"},
979 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
980 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
981# else
982 {(int)KS_CDL, "\033|%dD"},
983 {(int)KS_CS, "\033|%d;%dR"},
984 {(int)KS_CSV, "\033|%d;%dV"},
985# endif
986 {(int)KS_CL, "\033|C"},
987 // attributes switched on with 'h', off with * 'H'
988 {(int)KS_ME, "\033|31H"}, // HL_ALL
989 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
990 {(int)KS_MD, "\033|2h"}, // HL_BOLD
991 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
992 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
993 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
994 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
995 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
996 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
997 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
998 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
999 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
1000 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
1001 {(int)KS_VB, "\033|f"},
1002 {(int)KS_MS, "y"},
1003 {(int)KS_UT, "y"},
1004 {(int)KS_XN, "y"},
1005 {(int)KS_LE, "\b"}, // cursor-left = BS
1006 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
1007# ifdef TERMINFO
1008 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
1009# else
1010 {(int)KS_CM, "\033|%d;%dM"},
1011# endif
1012 // there are no key sequences here, the GUI sequences are recognized
1013 // in check_termcode()
1014
1015 {(int)KS_NAME, NULL} // end marker
1016};
1017#endif
1018
1019/*
1020 * Amiga console window, default for Amiga.
1021 */
1022static tcap_entry_T builtin_amiga[] = {
1023 {(int)KS_CE, "\033[K"},
1024 {(int)KS_CD, "\033[J"},
1025 {(int)KS_AL, "\033[L"},
1026# ifdef TERMINFO
1027 {(int)KS_CAL, "\033[%p1%dL"},
1028# else
1029 {(int)KS_CAL, "\033[%dL"},
1030# endif
1031 {(int)KS_DL, "\033[M"},
1032# ifdef TERMINFO
1033 {(int)KS_CDL, "\033[%p1%dM"},
1034# else
1035 {(int)KS_CDL, "\033[%dM"},
1036# endif
1037 {(int)KS_CL, "\014"},
1038 {(int)KS_VI, "\033[0 p"},
1039 {(int)KS_VE, "\033[1 p"},
1040 {(int)KS_ME, "\033[0m"},
1041 {(int)KS_MR, "\033[7m"},
1042 {(int)KS_MD, "\033[1m"},
1043 {(int)KS_SE, "\033[0m"},
1044 {(int)KS_SO, "\033[33m"},
1045 {(int)KS_US, "\033[4m"},
1046 {(int)KS_UE, "\033[0m"},
1047 {(int)KS_CZH, "\033[3m"},
1048 {(int)KS_CZR, "\033[0m"},
1049#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1050 {(int)KS_CCO, "8"}, // allow 8 colors
1051# ifdef TERMINFO
1052 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1053 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
1054# else
1055 {(int)KS_CAB, "\033[4%dm"}, // set background color
1056 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
1057# endif
1058 {(int)KS_OP, "\033[m"}, // reset colors
1059#endif
1060 {(int)KS_MS, "y"},
1061 {(int)KS_UT, "y"}, // guessed
1062 {(int)KS_LE, "\b"},
1063# ifdef TERMINFO
1064 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1065# else
1066 {(int)KS_CM, "\033[%i%d;%dH"},
1067# endif
1068#if defined(__MORPHOS__)
1069 {(int)KS_SR, "\033M"},
1070#endif
1071# ifdef TERMINFO
1072 {(int)KS_CRI, "\033[%p1%dC"},
1073# else
1074 {(int)KS_CRI, "\033[%dC"},
1075# endif
1076 {K_UP, "\233A"},
1077 {K_DOWN, "\233B"},
1078 {K_LEFT, "\233D"},
1079 {K_RIGHT, "\233C"},
1080 {K_S_UP, "\233T"},
1081 {K_S_DOWN, "\233S"},
1082 {K_S_LEFT, "\233 A"},
1083 {K_S_RIGHT, "\233 @"},
1084 {K_S_TAB, "\233Z"},
1085 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1086 {K_F2, "\233\061~"},
1087 {K_F3, "\233\062~"},
1088 {K_F4, "\233\063~"},
1089 {K_F5, "\233\064~"},
1090 {K_F6, "\233\065~"},
1091 {K_F7, "\233\066~"},
1092 {K_F8, "\233\067~"},
1093 {K_F9, "\233\070~"},
1094 {K_F10, "\233\071~"},
1095 {K_S_F1, "\233\061\060~"},
1096 {K_S_F2, "\233\061\061~"},
1097 {K_S_F3, "\233\061\062~"},
1098 {K_S_F4, "\233\061\063~"},
1099 {K_S_F5, "\233\061\064~"},
1100 {K_S_F6, "\233\061\065~"},
1101 {K_S_F7, "\233\061\066~"},
1102 {K_S_F8, "\233\061\067~"},
1103 {K_S_F9, "\233\061\070~"},
1104 {K_S_F10, "\233\061\071~"},
1105 {K_HELP, "\233?~"},
1106 {K_INS, "\233\064\060~"}, // 101 key keyboard
1107 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1108 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1109 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1110 {K_END, "\233\064\065~"}, // 101 key keyboard
1111
1112 {BT_EXTRA_KEYS, ""},
1113 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1114 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1115 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1116
1117 {(int)KS_NAME, NULL} // end marker
1118};
1119
1120/*
1121 * The most minimal terminal: only clear screen and cursor positioning.
1122 */
1123static tcap_entry_T builtin_dumb[] = {
1124 {(int)KS_CL, "\014"},
1125#ifdef TERMINFO
1126 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1127#else
1128 {(int)KS_CM, "\033[%i%d;%dH"},
1129#endif
1130
1131 {(int)KS_NAME, NULL} // end marker
1132};
1133
1134/*
1135 * Terminal used for debugging.
1136 */
1137static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 {(int)KS_CE, "[CE]"},
1139 {(int)KS_CD, "[CD]"},
1140 {(int)KS_AL, "[AL]"},
1141# ifdef TERMINFO
1142 {(int)KS_CAL, "[CAL%p1%d]"},
1143# else
1144 {(int)KS_CAL, "[CAL%d]"},
1145# endif
1146 {(int)KS_DL, "[DL]"},
1147# ifdef TERMINFO
1148 {(int)KS_CDL, "[CDL%p1%d]"},
1149# else
1150 {(int)KS_CDL, "[CDL%d]"},
1151# endif
1152# ifdef TERMINFO
1153 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1154# else
1155 {(int)KS_CS, "[%dCS%d]"},
1156# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001157# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001159# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161# endif
1162# ifdef TERMINFO
1163 {(int)KS_CAB, "[CAB%p1%d]"},
1164 {(int)KS_CAF, "[CAF%p1%d]"},
1165 {(int)KS_CSB, "[CSB%p1%d]"},
1166 {(int)KS_CSF, "[CSF%p1%d]"},
1167# else
1168 {(int)KS_CAB, "[CAB%d]"},
1169 {(int)KS_CAF, "[CAF%d]"},
1170 {(int)KS_CSB, "[CSB%d]"},
1171 {(int)KS_CSF, "[CSF%d]"},
1172# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001173 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {(int)KS_OP, "[OP]"},
1175 {(int)KS_LE, "[LE]"},
1176 {(int)KS_CL, "[CL]"},
1177 {(int)KS_VI, "[VI]"},
1178 {(int)KS_VE, "[VE]"},
1179 {(int)KS_VS, "[VS]"},
1180 {(int)KS_ME, "[ME]"},
1181 {(int)KS_MR, "[MR]"},
1182 {(int)KS_MB, "[MB]"},
1183 {(int)KS_MD, "[MD]"},
1184 {(int)KS_SE, "[SE]"},
1185 {(int)KS_SO, "[SO]"},
1186 {(int)KS_UE, "[UE]"},
1187 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001188 {(int)KS_UCE, "[UCE]"},
1189 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001190 {(int)KS_USS, "[USS]"},
1191 {(int)KS_DS, "[DS]"},
1192 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001193 {(int)KS_STE, "[STE]"},
1194 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {(int)KS_MS, "[MS]"},
1196 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001197 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198# ifdef TERMINFO
1199 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1200# else
1201 {(int)KS_CM, "[%dCM%d]"},
1202# endif
1203 {(int)KS_SR, "[SR]"},
1204# ifdef TERMINFO
1205 {(int)KS_CRI, "[CRI%p1%d]"},
1206# else
1207 {(int)KS_CRI, "[CRI%d]"},
1208# endif
1209 {(int)KS_VB, "[VB]"},
1210 {(int)KS_KS, "[KS]"},
1211 {(int)KS_KE, "[KE]"},
1212 {(int)KS_TI, "[TI]"},
1213 {(int)KS_TE, "[TE]"},
1214 {(int)KS_CIS, "[CIS]"},
1215 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001216 {(int)KS_CSC, "[CSC]"},
1217 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {(int)KS_TS, "[TS]"},
1219 {(int)KS_FS, "[FS]"},
1220# ifdef TERMINFO
1221 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1222 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1223# else
1224 {(int)KS_CWS, "[%dCWS%d]"},
1225 {(int)KS_CWP, "[%dCWP%d]"},
1226# endif
1227 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001228 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001229 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001230 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {K_UP, "[KU]"},
1232 {K_DOWN, "[KD]"},
1233 {K_LEFT, "[KL]"},
1234 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001235 {K_XUP, "[xKU]"},
1236 {K_XDOWN, "[xKD]"},
1237 {K_XLEFT, "[xKL]"},
1238 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {K_S_UP, "[S-KU]"},
1240 {K_S_DOWN, "[S-KD]"},
1241 {K_S_LEFT, "[S-KL]"},
1242 {K_C_LEFT, "[C-KL]"},
1243 {K_S_RIGHT, "[S-KR]"},
1244 {K_C_RIGHT, "[C-KR]"},
1245 {K_F1, "[F1]"},
1246 {K_XF1, "[xF1]"},
1247 {K_F2, "[F2]"},
1248 {K_XF2, "[xF2]"},
1249 {K_F3, "[F3]"},
1250 {K_XF3, "[xF3]"},
1251 {K_F4, "[F4]"},
1252 {K_XF4, "[xF4]"},
1253 {K_F5, "[F5]"},
1254 {K_F6, "[F6]"},
1255 {K_F7, "[F7]"},
1256 {K_F8, "[F8]"},
1257 {K_F9, "[F9]"},
1258 {K_F10, "[F10]"},
1259 {K_F11, "[F11]"},
1260 {K_F12, "[F12]"},
1261 {K_S_F1, "[S-F1]"},
1262 {K_S_XF1, "[S-xF1]"},
1263 {K_S_F2, "[S-F2]"},
1264 {K_S_XF2, "[S-xF2]"},
1265 {K_S_F3, "[S-F3]"},
1266 {K_S_XF3, "[S-xF3]"},
1267 {K_S_F4, "[S-F4]"},
1268 {K_S_XF4, "[S-xF4]"},
1269 {K_S_F5, "[S-F5]"},
1270 {K_S_F6, "[S-F6]"},
1271 {K_S_F7, "[S-F7]"},
1272 {K_S_F8, "[S-F8]"},
1273 {K_S_F9, "[S-F9]"},
1274 {K_S_F10, "[S-F10]"},
1275 {K_S_F11, "[S-F11]"},
1276 {K_S_F12, "[S-F12]"},
1277 {K_HELP, "[HELP]"},
1278 {K_UNDO, "[UNDO]"},
1279 {K_BS, "[BS]"},
1280 {K_INS, "[INS]"},
1281 {K_KINS, "[KINS]"},
1282 {K_DEL, "[DEL]"},
1283 {K_KDEL, "[KDEL]"},
1284 {K_HOME, "[HOME]"},
1285 {K_S_HOME, "[C-HOME]"},
1286 {K_C_HOME, "[C-HOME]"},
1287 {K_KHOME, "[KHOME]"},
1288 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001289 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {K_END, "[END]"},
1291 {K_S_END, "[C-END]"},
1292 {K_C_END, "[C-END]"},
1293 {K_KEND, "[KEND]"},
1294 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001295 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {K_PAGEUP, "[PAGEUP]"},
1297 {K_PAGEDOWN, "[PAGEDOWN]"},
1298 {K_KPAGEUP, "[KPAGEUP]"},
1299 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1300 {K_MOUSE, "[MOUSE]"},
1301 {K_KPLUS, "[KPLUS]"},
1302 {K_KMINUS, "[KMINUS]"},
1303 {K_KDIVIDE, "[KDIVIDE]"},
1304 {K_KMULTIPLY, "[KMULTIPLY]"},
1305 {K_KENTER, "[KENTER]"},
1306 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001307 {K_PS, "[PASTE-START]"},
1308 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {K_K0, "[K0]"},
1310 {K_K1, "[K1]"},
1311 {K_K2, "[K2]"},
1312 {K_K3, "[K3]"},
1313 {K_K4, "[K4]"},
1314 {K_K5, "[K5]"},
1315 {K_K6, "[K6]"},
1316 {K_K7, "[K7]"},
1317 {K_K8, "[K8]"},
1318 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319
Bram Moolenaar4654d632022-11-17 22:05:12 +00001320 {(int)KS_NAME, NULL} // end marker
1321};
1322
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001324 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001326typedef struct {
1327 char *bitc_name; // name, such as "xterm"
1328 tcap_entry_T *bitc_table; // table with entries for bitc_name
1329} builtin_tcap_T;
1330
1331builtin_tcap_T builtin_terminals[] = {
1332 // Unix and Generic
1333 {"ansi", builtin_ansi},
1334 {"vt320", builtin_vt320},
1335 {"vt52", builtin_vt52},
1336 {"xterm", builtin_xterm},
1337 {"iris-ansi", builtin_iris_ansi},
1338
1339 // MS-Windows
1340 {"pcansi", builtin_pcansi},
1341 {"win32", builtin_win32},
1342
1343 // Other systems
1344#if defined(FEAT_GUI)
1345 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001347 {"amiga", builtin_amiga},
1348 {"dumb", builtin_dumb},
1349 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
Bram Moolenaar4654d632022-11-17 22:05:12 +00001351 {NULL, NULL}, // end marker
1352};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001354#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001355 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001356termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001357{
Bram Moolenaarab302212016-04-26 20:59:29 +02001358 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001359}
1360
1361 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001362termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001363{
1364 guicolor_T t;
1365
1366 if (*name == NUL)
1367 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001368 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001369
1370 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001371 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001372 return t;
1373}
1374
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001375 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001376termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001377{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001378 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001379}
1380#endif
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382/*
1383 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385#ifdef AMIGA
1386# define DEFAULT_TERM (char_u *)"amiga"
1387#endif
1388
1389#ifdef MSWIN
1390# define DEFAULT_TERM (char_u *)"win32"
1391#endif
1392
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001393#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394# define DEFAULT_TERM (char_u *)"ansi"
1395#endif
1396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397#ifdef VMS
1398# define DEFAULT_TERM (char_u *)"vt320"
1399#endif
1400
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001401#ifdef __HAIKU__
1402# undef DEFAULT_TERM
1403# define DEFAULT_TERM (char_u *)"xterm"
1404#endif
1405
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406#ifndef DEFAULT_TERM
1407# define DEFAULT_TERM (char_u *)"dumb"
1408#endif
1409
1410/*
1411 * Term_strings contains currently used terminal output strings.
1412 * It is initialized with the default values by parse_builtin_tcap().
1413 * The values can be changed by setting the option with the same name.
1414 */
1415char_u *(term_strings[(int)KS_LAST + 1]);
1416
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001417static int need_gather = FALSE; // need to fill termleader[]
1418static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001420static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001421#endif
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001422
1423/*
1424 * Structure and table to store terminal features that can be detected by
1425 * querying the terminal. Either by inspecting the termresponse or a more
1426 * specific request. Besides this there are:
1427 * t_colors - number of colors supported
1428 */
1429typedef struct {
1430 char *tpr_name;
1431 int tpr_set_by_termresponse;
1432 int tpr_status;
1433} termprop_T;
1434
1435// Values for tpr_status.
1436#define TPR_UNKNOWN 'u'
1437#define TPR_YES 'y'
1438#define TPR_NO 'n'
1439#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1440#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1441#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1442
1443// can request the cursor style without messing up the display
1444#define TPR_CURSOR_STYLE 0
1445// can request the cursor blink mode without messing up the display
1446#define TPR_CURSOR_BLINK 1
1447// can set the underline color with t_8u without resetting other colors
1448#define TPR_UNDERLINE_RGB 2
1449// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1450#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001451// term response indicates kitty
1452#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001453// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001454#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001455
1456static termprop_T term_props[TPR_COUNT];
1457
1458/*
1459 * Initialize the term_props table.
1460 * When "all" is FALSE only set those that are detected from the version
1461 * response.
1462 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001463 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001464init_term_props(int all)
1465{
1466 int i;
1467
1468 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1469 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1470 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1471 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1472 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1473 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1474 term_props[TPR_MOUSE].tpr_name = "mouse";
1475 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001476 term_props[TPR_KITTY].tpr_name = "kitty";
1477 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001478
1479 for (i = 0; i < TPR_COUNT; ++i)
1480 if (all || term_props[i].tpr_set_by_termresponse)
1481 term_props[i].tpr_status = TPR_UNKNOWN;
1482}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001484#if defined(FEAT_EVAL) || defined(PROTO)
1485 void
1486f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1487{
1488# ifdef FEAT_TERMRESPONSE
1489 int i;
1490# endif
1491
Bram Moolenaar93a10962022-06-16 11:42:09 +01001492 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001493 return;
1494# ifdef FEAT_TERMRESPONSE
1495 for (i = 0; i < TPR_COUNT; ++i)
1496 {
1497 char_u value[2];
1498
1499 value[0] = term_props[i].tpr_status;
1500 value[1] = NUL;
1501 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1502 }
1503# endif
1504}
1505#endif
1506
Bram Moolenaar4654d632022-11-17 22:05:12 +00001507/*
1508 * Find the builtin termcap entries for "term".
1509 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1510 * entry.
1511 * Returns NULL when "term" is not found.
1512 */
1513 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001514find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001516 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001518 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1519 if (name == NULL) // end marker
1520 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001522 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1523 return builtin_terminals[i].bitc_table;
1524 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1525 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526#endif
1527#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001528 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1529 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001531 if (STRCMP(term, name) == 0)
1532 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001534 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535}
1536
1537/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001538 * Apply entries from a builtin termcap.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 */
1540 static void
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001541apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001543 int term_8bit = term_is_8bit(term);
1544
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001545 for (tcap_entry_T *p = entries;
1546 p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001548 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001550 // Only set the value if it wasn't set yet or "overwrite" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 if (term_strings[p->bt_entry] == NULL
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001552 || term_strings[p->bt_entry] == empty_option
1553 || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001555#ifdef FEAT_EVAL
1556 int opt_idx = -1;
1557#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001558 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1560 {
1561 char_u *s, *t;
1562
1563 s = vim_strsave((char_u *)p->bt_string);
1564 if (s != NULL)
1565 {
1566 for (t = s; *t; ++t)
1567 if (term_7to8bit(t))
1568 {
1569 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001570 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 }
1572 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001573#ifdef FEAT_EVAL
1574 opt_idx =
1575#endif
1576 set_term_option_alloced(
1577 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 }
1579 }
1580 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001583#ifdef FEAT_EVAL
1584 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1585#endif
1586 }
1587#ifdef FEAT_EVAL
1588 set_term_option_sctx_idx(NULL, opt_idx);
1589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 }
1591 }
1592 else
1593 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001594 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1596 name[1] = KEY2TERMCAP1((int)p->bt_entry);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001597 if (find_termcode(name) == NULL || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1599 }
1600 }
1601}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001602
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001604 * Parsing of the builtin termcap entries.
1605 * Caller should check if "term" is a valid builtin terminal name.
1606 * The terminal's name is not set, as this is already done in termcapinit().
1607 */
1608 static void
1609parse_builtin_tcap(char_u *term)
1610{
1611 tcap_entry_T *entries = find_builtin_term(term);
1612 if (entries != NULL)
1613 apply_builtin_tcap(term, entries, FALSE);
1614}
1615
1616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 * Set number of colors.
1618 * Store it as a number in t_colors.
1619 * Store it as a string in T_CCO (using nr_colors[]).
1620 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001621 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001622set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001624 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
1626 t_colors = nr;
1627 if (t_colors > 1)
1628 sprintf((char *)nr_colors, "%d", t_colors);
1629 else
1630 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001631 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001633
1634/*
1635 * Set the color count to "val" and redraw if it changed.
1636 */
1637 static void
1638may_adjust_color_count(int val)
1639{
1640 if (val != t_colors)
1641 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001642 // Nr of colors changed, initialize highlighting and redraw everything.
1643 // This causes a redraw, which usually clears the message. Try keeping
1644 // the message if it might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001645 set_keep_msg_from_hist();
1646 set_color_count(val);
1647 init_highlight(TRUE, FALSE);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001648#ifdef DEBUG_TERMRESPONSE
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001649 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001650 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001651
Bram Moolenaarb255b902018-04-24 21:40:10 +02001652 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001653 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001654#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001655 redraw_asap(UPD_CLEAR);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001656#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001657 }
1658}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
1660#ifdef HAVE_TGETENT
1661static char *(key_names[]) =
1662{
Bram Moolenaar87202262020-05-24 17:23:45 +02001663# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001664 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001666# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 "#2", "#4", "%i", "*7",
1669 "k1", "k2", "k3", "k4", "k5", "k6",
1670 "k7", "k8", "k9", "k;", "F1", "F2",
1671 "%1", "&8", "kb", "kI", "kD", "kh",
1672 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1673 NULL
1674};
1675#endif
1676
Bram Moolenaar9289df52018-05-10 14:40:57 +02001677#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001678 static void
1679get_term_entries(int *height, int *width)
1680{
1681 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001682 enum SpecialKey dest; // index in term_strings[]
1683 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001684 } string_names[] =
1685 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1686 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1687 {KS_CL, "cl"}, {KS_CD, "cd"},
1688 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1689 {KS_ME, "me"}, {KS_MR, "mr"},
1690 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1691 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1692 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001693 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001694 {KS_STE,"Te"}, {KS_STS,"Ts"},
1695 {KS_CM, "cm"}, {KS_SR, "sr"},
1696 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1697 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001698 {KS_CTI, "TI"}, {KS_CRK, "RK"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001699 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001700 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1701 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001702 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1703 {KS_VS, "vs"}, {KS_CVS, "VS"},
1704 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1705 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1706 {KS_TS, "ts"}, {KS_FS, "fs"},
1707 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1708 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1709 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001710 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001711 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1712 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001713 {KS_CST, "ST"}, {KS_CRT, "RT"},
1714 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001715 {(enum SpecialKey)0, NULL}
1716 };
1717 int i;
1718 char_u *p;
1719 static char_u tstrbuf[TBUFSZ];
1720 char_u *tp = tstrbuf;
1721
1722 /*
1723 * get output strings
1724 */
1725 for (i = 0; string_names[i].name != NULL; ++i)
1726 {
1727 if (TERM_STR(string_names[i].dest) == NULL
1728 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001729 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001730 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001731#ifdef FEAT_EVAL
1732 set_term_option_sctx_idx(string_names[i].name, -1);
1733#endif
1734 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001735 }
1736
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001737 // tgetflag() returns 1 if the flag is present, 0 if not and
1738 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001739 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1740 T_MS = (char_u *)"y";
1741 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1742 T_XS = (char_u *)"y";
1743 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1744 T_XN = (char_u *)"y";
1745 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1746 T_DB = (char_u *)"y";
1747 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1748 T_DA = (char_u *)"y";
1749 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1750 T_UT = (char_u *)"y";
1751
1752 /*
1753 * get key codes
1754 */
1755 for (i = 0; key_names[i] != NULL; ++i)
1756 if (find_termcode((char_u *)key_names[i]) == NULL)
1757 {
1758 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001759 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001760 if (p != NULL
1761 && (*p != Ctrl_H
1762 || key_names[i][0] != 'k'
1763 || key_names[i][1] != 'l'))
1764 add_termcode((char_u *)key_names[i], p, FALSE);
1765 }
1766
1767 if (*height == 0)
1768 *height = tgetnum("li");
1769 if (*width == 0)
1770 *width = tgetnum("co");
1771
1772 /*
1773 * Get number of colors (if not done already).
1774 */
1775 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001776 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001777 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001778#ifdef FEAT_EVAL
1779 set_term_option_sctx_idx("Co", -1);
1780#endif
1781 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001782
1783# ifndef hpux
1784 BC = (char *)TGETSTR("bc", &tp);
1785 UP = (char *)TGETSTR("up", &tp);
1786 p = TGETSTR("pc", &tp);
1787 if (p)
1788 PC = *p;
1789# endif
1790}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001791#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001792
Bram Moolenaar4654d632022-11-17 22:05:12 +00001793/*
1794 * Report "term" is not found and list the ones we do know about.
1795 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001796 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001797report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001798{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001799 mch_errmsg("\r\n");
1800 if (error_msg != NULL)
1801 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001802 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001803 mch_errmsg("\r\n");
1804 }
1805 mch_errmsg("'");
1806 mch_errmsg((char *)term);
1807 mch_errmsg(_("' not known. Available builtin terminals are:"));
1808 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001809
1810 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001811 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001812 char *name = builtin_terminals[i].bitc_name;
1813 if (name == NULL) // end marker
1814 break;
1815 // Do not mention the "gui" entry, the user won't need to type it.
1816 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001817 {
1818#ifdef HAVE_TGETENT
1819 mch_errmsg(" builtin_");
1820#else
1821 mch_errmsg(" ");
1822#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001823 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001824 mch_errmsg("\r\n");
1825 }
1826 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001827 // Output extra 'cmdheight' line breaks to avoid that the following error
1828 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001829 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001830 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001831}
1832
1833 static void
1834report_default_term(char_u *term)
1835{
1836 mch_errmsg(_("defaulting to '"));
1837 mch_errmsg((char *)term);
1838 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001839 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001840 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001841 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001842 out_flush();
1843 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001844 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001845 }
1846}
1847
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001849 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1850 * for the first matching entry.
1851 * When "term" is NULL then compile all patterns to check for any errors.
1852 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1853 * Returns KEYPROTOCOL_NONE if there is no match.
1854 */
1855 keyprot_T
1856match_keyprotocol(char_u *term)
1857{
1858 int len = (int)STRLEN(p_kpc) + 1;
1859 char_u *buf = alloc(len);
1860 if (buf == NULL)
1861 return KEYPROTOCOL_FAIL;
1862
1863 keyprot_T ret = KEYPROTOCOL_FAIL;
1864 char_u *p = p_kpc;
1865 while (*p != NUL)
1866 {
1867 // Isolate one comma separated item.
1868 (void)copy_option_part(&p, buf, len, ",");
1869 char_u *colon = vim_strchr(buf, ':');
1870 if (colon == NULL || colon == buf || colon[1] == NUL)
1871 goto theend;
1872 *colon = NUL;
1873
1874 keyprot_T prot;
1875 if (STRCMP(colon + 1, "none") == 0)
1876 prot = KEYPROTOCOL_NONE;
1877 else if (STRCMP(colon + 1, "mok2") == 0)
1878 prot = KEYPROTOCOL_MOK2;
1879 else if (STRCMP(colon + 1, "kitty") == 0)
1880 prot = KEYPROTOCOL_KITTY;
1881 else
1882 goto theend;
1883
1884 regmatch_T regmatch;
1885 CLEAR_FIELD(regmatch);
1886 regmatch.rm_ic = TRUE;
1887 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1888 if (regmatch.regprog == NULL)
1889 goto theend;
1890
1891 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1892 vim_regfree(regmatch.regprog);
1893 if (match)
1894 {
1895 ret = prot;
1896 goto theend;
1897 }
1898
1899 }
1900
1901 // No match found, use "none".
1902 ret = KEYPROTOCOL_NONE;
1903
1904theend:
1905 vim_free(buf);
1906 return ret;
1907}
1908
1909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 * Set terminal options for terminal "term".
1911 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1912 *
1913 * While doing this, until ttest(), some options may be NULL, be careful.
1914 */
1915 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001916set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918#ifdef HAVE_TGETENT
1919 int builtin_first = p_tbi;
1920 int try;
1921 int termcap_cleared = FALSE;
1922#endif
1923 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001924 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 char_u *bs_p, *del_p;
1926
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001927 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001928 if (silent_mode)
1929 return OK;
1930
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001931 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932
1933 if (term_is_builtin(term))
1934 {
1935 term += 8;
1936#ifdef HAVE_TGETENT
1937 builtin_first = 1;
1938#endif
1939 }
1940
1941/*
1942 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1943 * If builtin_first is TRUE:
1944 * 0. try builtin termcap
1945 * 1. try external termcap
1946 * 2. if both fail default to a builtin terminal
1947 * If builtin_first is FALSE:
1948 * 1. try external termcap
1949 * 2. try builtin termcap, if both fail default to a builtin terminal
1950 */
1951#ifdef HAVE_TGETENT
1952 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1953 {
1954 /*
1955 * Use external termcap
1956 */
1957 if (try == 1)
1958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960
1961 /*
1962 * If the external termcap does not have a matching entry, try the
1963 * builtin ones.
1964 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01001965 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 if (!termcap_cleared)
1968 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001969 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 termcap_cleared = TRUE;
1971 }
1972
Bram Moolenaar69e05692018-05-10 14:11:52 +02001973 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 }
1975 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001976 else // try == 0 || try == 2
1977#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 /*
1979 * Use builtin termcap
1980 */
1981 {
1982#ifdef HAVE_TGETENT
1983 /*
1984 * If builtin termcap was already used, there is no need to search
1985 * for the builtin termcap again, quit now.
1986 */
1987 if (try == 2 && builtin_first && termcap_cleared)
1988 break;
1989#endif
1990 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001991 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001993 tcap_entry_T *termp = find_builtin_term(term);
1994 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 {
1996#ifdef HAVE_TGETENT
1997 /*
1998 * If try == 0, first try the external termcap. If that is not
1999 * found we'll get back here with try == 2.
2000 * If termcap_cleared is set we used the external termcap,
2001 * don't complain about not finding the term in the builtin
2002 * termcap.
2003 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002004 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002006 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 break;
2008#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02002009 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002011 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if (starting != NO_SCREEN)
2013 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002014 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 wait_return(TRUE);
2016 return FAIL;
2017 }
2018 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002019 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002020 set_string_option_direct((char_u *)"term", -1, term,
2021 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 display_errors();
2023 }
2024 out_flush();
2025#ifdef HAVE_TGETENT
2026 if (!termcap_cleared)
2027 {
2028#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002029 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030#ifdef HAVE_TGETENT
2031 termcap_cleared = TRUE;
2032 }
2033#endif
2034 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002035
2036 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
2037 // termcap entries if there is an entry maching "term".
2038 keyprot_T kpc = match_keyprotocol(term);
2039 if (kpc == KEYPROTOCOL_KITTY)
2040 apply_builtin_tcap(term, builtin_kitty, TRUE);
2041 else if (kpc == KEYPROTOCOL_MOK2)
2042 apply_builtin_tcap(term, builtin_mok2, TRUE);
2043
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044#ifdef FEAT_GUI
2045 if (term_is_gui(term))
2046 {
2047 out_flush();
2048 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002049 // If starting the GUI failed, don't do any of the other
2050 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 if (!gui.in_use)
2052 return FAIL;
Bram Moolenaar1a173402022-12-02 12:28:47 +00002053# ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002054 break; // don't try using external termcap
Bram Moolenaar1a173402022-12-02 12:28:47 +00002055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002057#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
2059#ifdef HAVE_TGETENT
2060 }
2061#endif
2062
2063/*
2064 * special: There is no info in the termcap about whether the cursor
2065 * positioning is relative to the start of the screen or to the start of the
2066 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2067 * relative.
2068 */
2069 if (STRCMP(term, "pcterm") == 0)
2070 T_CCS = (char_u *)"yes";
2071 else
2072 T_CCS = empty_option;
2073
Bram Moolenaar731d0072022-12-18 17:47:18 +00002074 // Special case: "kitty" does not normally have a "RV" entry in terminfo,
2075 // but we need to request the version for several other things to work.
2076 if (strstr((char *)term, "kitty") != NULL
2077 && (T_CRV == NULL || *T_CRV == NUL))
2078 T_CRV = (char_u *)"\033[>c";
2079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080#ifdef UNIX
2081/*
2082 * Any "stty" settings override the default for t_kb from the termcap.
2083 * This is in os_unix.c, because it depends a lot on the version of unix that
2084 * is being used.
2085 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2086 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002087# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002089# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 get_stty();
2091#endif
2092
2093/*
2094 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2095 * didn't work, use the default CTRL-H
2096 * The default for t_kD is DEL, unless t_kb is DEL.
2097 * The vim_strsave'd strings are probably lost forever, well it's only two
2098 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2099 * directly.
2100 */
2101#ifdef FEAT_GUI
2102 if (!gui.in_use)
2103#endif
2104 {
2105 bs_p = find_termcode((char_u *)"kb");
2106 del_p = find_termcode((char_u *)"kD");
2107 if (bs_p == NULL || *bs_p == NUL)
2108 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2109 if ((del_p == NULL || *del_p == NUL) &&
2110 (bs_p == NULL || *bs_p != DEL))
2111 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2112 }
2113
2114#if defined(UNIX) || defined(VMS)
2115 term_is_xterm = vim_is_xterm(term);
2116#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002117#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002118 // Reset terminal properties that are set based on the termresponse, which
2119 // will be sent out soon.
2120 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002123#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 /*
2125 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2126 * The termcode for the mouse is added as a side effect in option.c.
2127 */
2128 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002129 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002131# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002132 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
2134 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002135 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 else
2137 p = (char_u *)"xterm";
2138 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002139# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002141 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002142 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002143 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2144 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002145 reset_option_was_set((char_u *)"ttym");
2146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002148# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002150# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002152 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002154#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
Bram Moolenaarbf789742021-01-16 11:21:40 +01002158#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002159 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002160 if (use_xterm_like_mouse(term))
2161 {
2162 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002163
2164 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002165 name[0] = KS_EXTRA;
2166 name[1] = KE_FOCUSGAINED;
2167 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002168 add_termcode(name, (char_u *)"\033[I", FALSE);
2169
2170 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002171 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002172 add_termcode(name, (char_u *)"\033[O", FALSE);
2173
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002174 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002175 }
2176#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002177#if (defined(UNIX) || defined(VMS))
2178 // First time after setting 'term' a focus event is always reported.
2179 focus_state = MAYBE;
2180#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002181
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002183 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 if (STRCMP(term, DEFAULT_TERM) != 0)
2185 term_console = FALSE;
2186 else
2187 {
2188 term_console = TRUE;
2189# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002190 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191# endif
2192 }
2193#endif
2194
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002195 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002197 full_screen = TRUE; // we can use termcap codes from now on
2198 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002200 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002201 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002202 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203#endif
2204
2205 /*
2206 * Initialize the terminal with the appropriate termcap codes.
2207 * Set the mouse and window title if possible.
2208 * Don't do this when starting, need to parse the .vimrc first, because it
2209 * may redefine t_TI etc.
2210 */
2211 if (starting != NO_SCREEN)
2212 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002213 starttermcap(); // may change terminal mode
2214 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002215 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
2217
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002218 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 if (width <= 0 || height <= 0)
2220 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002221 // termcap failed to report size
2222 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002224#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002225 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002227 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228#endif
2229 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002230 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 if (starting != NO_SCREEN)
2232 {
2233 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002234 scroll_region_reset(); // In case Rows changed
2235 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002238 buf_T *buf;
2239 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240
2241 /*
2242 * Execute the TermChanged autocommands for each buffer that is
2243 * loaded.
2244 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002245 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
2247 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002248 {
2249 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002250 if (curbuf == buf)
2251 {
2252 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 curbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002254 // restore curwin/curbuf and a few other things
2255 aucmd_restbuf(&aco);
2256 }
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 }
2261
2262#ifdef FEAT_TERMRESPONSE
2263 may_req_termresponse();
2264#endif
2265
2266 return OK;
2267}
2268
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002269#if defined(EXITFREE) || defined(PROTO)
2270
2271# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002272# include <term.h> // declares cur_term
2273# endif
2274
2275/*
2276 * If supported, delete "cur_term", which caches terminal related entries.
2277 * Avoids that valgrind reports possibly lost memory.
2278 */
2279 void
2280free_cur_term()
2281{
2282# ifdef HAVE_DEL_CURTERM
2283 if (cur_term)
2284 del_curterm(cur_term);
2285# endif
2286}
2287
2288#endif
2289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290#ifdef HAVE_TGETENT
2291/*
2292 * Call tgetent()
2293 * Return error message if it fails, NULL if it's OK.
2294 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002295 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002296invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297{
2298 int i;
2299
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002300 // Note: Valgrind may report a leak here, because the library keeps one
2301 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002303 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002305 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306# endif
2307 )
2308 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002309 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2310 // tgetent() with the always existing "dumb" entry to avoid a crash or
2311 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 (void)TGETENT(tbuf, "dumb");
2313
2314 if (i < 0)
2315# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002316 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 if (i == 0)
2318# endif
2319#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002320 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002322 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323#endif
2324 }
2325 return NULL;
2326}
2327
2328/*
2329 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2330 * Fix that here.
2331 */
2332 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002333vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
2335 char *p;
2336
2337 p = tgetstr(s, (char **)pp);
2338 if (p == (char *)-1)
2339 p = NULL;
2340 return (char_u *)p;
2341}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002342#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002344#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345/*
2346 * Get Columns and Rows from the termcap. Used after a window signal if the
2347 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2348 * and "li" entries never change. But on some systems this works.
2349 * Errors while getting the entries are ignored.
2350 */
2351 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002352getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002353 long *cp, // pointer to columns
2354 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355{
2356 char_u tbuf[TBUFSZ];
2357
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002358 if (T_NAME != NULL && *T_NAME != NUL && invoke_tgetent(tbuf, T_NAME) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
2360 if (*cp == 0)
2361 *cp = tgetnum("co");
2362 if (*rp == 0)
2363 *rp = tgetnum("li");
2364 }
2365}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002366#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
2368/*
2369 * Get a string entry from the termcap and add it to the list of termcodes.
2370 * Used for <t_xx> special keys.
2371 * Give an error message for failure when not sourcing.
2372 * If force given, replace an existing entry.
2373 * Return FAIL if the entry was not found, OK if the entry was added.
2374 */
2375 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002376add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
2378 char_u *term;
2379 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380#ifdef HAVE_TGETENT
2381 char_u *string;
2382 int i;
2383 int builtin_first;
2384 char_u tbuf[TBUFSZ];
2385 char_u tstrbuf[TBUFSZ];
2386 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002387 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388#endif
2389
2390/*
2391 * If the GUI is running or will start in a moment, we only support the keys
2392 * that the GUI can produce.
2393 */
2394#ifdef FEAT_GUI
2395 if (gui.in_use || gui.starting)
2396 return gui_mch_haskey(name);
2397#endif
2398
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002399 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 return OK;
2401
2402 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002403 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 return FAIL;
2405
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002406 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
2408 term += 8;
2409#ifdef HAVE_TGETENT
2410 builtin_first = TRUE;
2411#endif
2412 }
2413#ifdef HAVE_TGETENT
2414 else
2415 builtin_first = p_tbi;
2416#endif
2417
2418#ifdef HAVE_TGETENT
2419/*
2420 * We can get the entry from the builtin termcap and from the external one.
2421 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2422 * builtin termcap first.
2423 * If 'ttybuiltin' is off, try external termcap first.
2424 */
2425 for (i = 0; i < 2; ++i)
2426 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002427 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428#endif
2429 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002430 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 */
2432 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002433 tcap_entry_T *termp = find_builtin_term(term);
2434 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
2436 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002437 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 while (termp->bt_entry != (int)KS_NAME)
2439 {
2440 if ((int)termp->bt_entry == key)
2441 {
2442 add_termcode(name, (char_u *)termp->bt_string,
2443 term_is_8bit(term));
2444 return OK;
2445 }
2446 ++termp;
2447 }
2448 }
2449 }
2450#ifdef HAVE_TGETENT
2451 else
2452 /*
2453 * Search in external termcap
2454 */
2455 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002456 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 if (error_msg == NULL)
2458 {
2459 string = TGETSTR((char *)name, &tp);
2460 if (string != NULL && *string != NUL)
2461 {
2462 add_termcode(name, string, FALSE);
2463 return OK;
2464 }
2465 }
2466 }
2467 }
2468#endif
2469
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002470 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {
2472#ifdef HAVE_TGETENT
2473 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002474 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 else
2476#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002477 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 }
2479 return FAIL;
2480}
2481
2482 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002483term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484{
2485 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2486}
2487
2488/*
2489 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2490 * Assume that the terminal is using 8-bit controls when the name contains
2491 * "8bit", like in "xterm-8bit".
2492 */
2493 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002494term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495{
2496 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2497}
2498
2499/*
2500 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002501 * <Esc>[ -> CSI <M_C_[>
2502 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * <Esc>O -> <M-C-O>
2504 */
2505 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002506term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
2508 if (*p == ESC)
2509 {
2510 if (p[1] == '[')
2511 return CSI;
2512 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002513 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 if (p[1] == 'O')
2515 return 0x8f;
2516 }
2517 return 0;
2518}
2519
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002520#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002522term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2525}
2526#endif
2527
2528#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2529
2530 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002531tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
2533 static char_u buf[16];
2534 char_u *p;
2535
2536 p = buf + 15;
2537 *p = '\0';
2538 do
2539 {
2540 --p;
2541 *p = (char_u) (i % 10 + '0');
2542 i /= 10;
2543 }
2544 while (i > 0 && p > buf);
2545 return p;
2546}
2547#endif
2548
2549#ifndef HAVE_TGETENT
2550
2551/*
2552 * minimal tgoto() implementation.
2553 * no padding and we only parse for %i %d and %+char
2554 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002555 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002556tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
2558 static char buf[30];
2559 char *p, *s, *e;
2560
2561 if (!cm)
2562 return "OOPS";
2563 e = buf + 29;
2564 for (s = buf; s < e && *cm; cm++)
2565 {
2566 if (*cm != '%')
2567 {
2568 *s++ = *cm;
2569 continue;
2570 }
2571 switch (*++cm)
2572 {
2573 case 'd':
2574 p = (char *)tltoa((unsigned long)y);
2575 y = x;
2576 while (*p)
2577 *s++ = *p++;
2578 break;
2579 case 'i':
2580 x++;
2581 y++;
2582 break;
2583 case '+':
2584 *s++ = (char)(*++cm + y);
2585 y = x;
2586 break;
2587 case '%':
2588 *s++ = *cm;
2589 break;
2590 default:
2591 return "OOPS";
2592 }
2593 }
2594 *s = '\0';
2595 return buf;
2596}
2597
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002598#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600/*
2601 * Set the terminal name and initialize the terminal options.
2602 * If "name" is NULL or empty, get the terminal name from the environment.
2603 * If that fails, use the default terminal name.
2604 */
2605 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002606termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
Bram Moolenaar731d0072022-12-18 17:47:18 +00002608 char_u *term = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
Bram Moolenaar731d0072022-12-18 17:47:18 +00002610 if (term != NULL && *term == NUL)
2611 term = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613#ifndef MSWIN
Bram Moolenaar731d0072022-12-18 17:47:18 +00002614 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (term == NULL)
Bram Moolenaar731d0072022-12-18 17:47:18 +00002616 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 term = mch_getenv((char_u *)"TERM");
Bram Moolenaar731d0072022-12-18 17:47:18 +00002618
2619 // "xterm-kitty" is used for Kitty, but it is not actually compatible
2620 // with xterm. Remove the "xterm-" part to avoid trouble.
2621 if (term != NULL && STRNCMP(term, "xterm-kitty", 11) == 0)
2622 {
2623#ifdef FEAT_EVAL
2624 ch_log(NULL, "Removing xterm- prefix from terminal name %s", term);
2625#endif
2626 if (p_verbose > 0)
2627 {
2628 verbose_enter();
2629 smsg(_("Removing xterm- prefix from terminal name %s"), term);
2630 verbose_leave();
2631 }
2632 term = vim_strsave(term + 6);
2633 tofree = term;
2634 }
2635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#endif
2637 if (term == NULL || *term == NUL)
2638 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002639 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002641 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 set_string_default("term", term);
2643 set_string_default("ttytype", term);
2644
Bram Moolenaar731d0072022-12-18 17:47:18 +00002645 // Avoid using "term" here, because the next mch_getenv() may overwrite it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 set_termname(T_NAME != NULL ? T_NAME : term);
Bram Moolenaar731d0072022-12-18 17:47:18 +00002647
2648#ifndef MSWIN
2649 vim_free(tofree);
2650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651}
2652
2653/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002654 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002656#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002657
2658// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002660
2661static int out_pos = 0; // number of chars in out_buf
2662
2663// Since the maximum number of SGR parameters shown as a normal value range is
2664// 16, the escape sequence length can be 4 * 16 + lead + tail.
2665#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
2667/*
2668 * out_flush(): flush the output buffer
2669 */
2670 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002671out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672{
2673 int len;
2674
2675 if (out_pos != 0)
2676 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002677 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 len = out_pos;
2679 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002680 ui_write(out_buf, len, FALSE);
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00002681#ifdef FEAT_EVAL
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002682 if (ch_log_output != FALSE)
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002683 {
2684 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002685 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002686# ifdef FEAT_GUI
2687 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2688# endif
2689 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002690 out_buf);
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002691 if (ch_log_output == TRUE)
2692 ch_log_output = FALSE; // only log once
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002693 }
2694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
2696}
2697
Bram Moolenaara338adc2018-01-31 20:51:47 +01002698/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002699 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2700 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002701 */
2702 void
2703out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002704 int force UNUSED, // when TRUE, update cursor even when not moved
2705 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002706{
2707 mch_disable_flush();
2708 out_flush();
2709 mch_enable_flush();
2710#ifdef FEAT_GUI
2711 if (gui.in_use)
2712 {
2713 gui_update_cursor(force, clear_selection);
2714 gui_may_flush();
2715 }
2716#endif
2717}
2718
2719
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720/*
2721 * Sometimes a byte out of a multi-byte character is written with out_char().
2722 * To avoid flushing half of the character, call this function first.
2723 */
2724 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002725out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
2727 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2728 out_flush();
2729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
2731#ifdef FEAT_GUI
2732/*
2733 * out_trash(): Throw away the contents of the output buffer
2734 */
2735 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002736out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737{
2738 out_pos = 0;
2739}
2740#endif
2741
2742/*
2743 * out_char(c): put a byte into the output buffer.
2744 * Flush it if it becomes full.
2745 * This should not be used for outputting text on the screen (use functions
2746 * like msg_puts() and screen_putchar() for that).
2747 */
2748 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002749out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750{
Bram Moolenaard0573012017-10-28 21:11:06 +02002751#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002752 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 out_char('\r');
2754#endif
2755
2756 out_buf[out_pos++] = c;
2757
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002758 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 if (out_pos >= OUT_SIZE || p_wd)
2760 out_flush();
2761}
2762
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002764 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002766 static int
2767out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002769 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770
2771 if (out_pos >= OUT_SIZE)
2772 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002773 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774}
2775
2776/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002777 * A never-padding out_str().
2778 * Use this whenever you don't want to run the string through tputs().
2779 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 * is likely to strip off leading digits, that it mistakes for padding
2781 * information, and "%i", "%d", etc.
2782 * This should only be used for writing terminal codes, not for outputting
2783 * normal text (use functions like msg_puts() and screen_putchar() for that).
2784 */
2785 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002786out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002788 // avoid terminal strings being split up
2789 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002791
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 while (*s)
2793 out_char_nf(*s++);
2794
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002795 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (p_wd)
2797 out_flush();
2798}
2799
2800/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002801 * A conditional-flushing out_str, mainly for visualbell.
2802 * Handles a delay internally, because termlib may not respect the delay or do
2803 * it at the wrong time.
2804 * Note: Only for terminal strings.
2805 */
2806 void
2807out_str_cf(char_u *s)
2808{
2809 if (s != NULL && *s)
2810 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002811#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002812 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002813#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002814
2815#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002816 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002817 if (gui.in_use)
2818 {
2819 out_str_nf(s);
2820 return;
2821 }
2822#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002823 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002824 out_flush();
2825#ifdef HAVE_TGETENT
2826 for (p = s; *s; ++s)
2827 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002828 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002829 if (*s == '$' && *(s + 1) == '<')
2830 {
2831 char_u save_c = *s;
2832 int duration = atoi((char *)s + 2);
2833
2834 *s = NUL;
2835 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2836 *s = save_c;
2837 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002838# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002839 // Only sleep here if we can limit this happening in
2840 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002841 p = vim_strchr(s, '>');
2842 if (p == NULL || duration <= 0)
2843 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002844 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002845 p = s;
2846 }
2847 else
2848 {
2849 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002850 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002851 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002852# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002853 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002854 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002855# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002856 break;
2857 }
2858 }
2859 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2860#else
2861 while (*s)
2862 out_char_nf(*s++);
2863#endif
2864
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002865 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002866 if (p_wd)
2867 out_flush();
2868 }
2869}
2870
2871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002873 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 * This should only be used for writing terminal codes, not for outputting
2875 * normal text (use functions like msg_puts() and screen_putchar() for that).
2876 */
2877 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002878out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
2880 if (s != NULL && *s)
2881 {
2882#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002883 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 if (gui.in_use)
2885 {
2886 out_str_nf(s);
2887 return;
2888 }
2889#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002890 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002891 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 out_flush();
2893#ifdef HAVE_TGETENT
2894 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2895#else
2896 while (*s)
2897 out_char_nf(*s++);
2898#endif
2899
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002900 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (p_wd)
2902 out_flush();
2903 }
2904}
2905
2906/*
2907 * cursor positioning using termcap parser. (jw)
2908 */
2909 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002910term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911{
2912 OUT_STR(tgoto((char *)T_CM, col, row));
2913}
2914
2915 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002916term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917{
2918 OUT_STR(tgoto((char *)T_CRI, 0, i));
2919}
2920
2921 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002922term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
2924 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2925}
2926
2927 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002928term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929{
2930 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2931}
2932
2933#if defined(HAVE_TGETENT) || defined(PROTO)
2934 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002935term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002937 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 if (x < 0)
2939 x = 0;
2940 if (y < 0)
2941 y = 0;
2942 OUT_STR(tgoto((char *)T_CWP, y, x));
2943}
2944
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002945# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2946/*
2947 * Return TRUE if we can request the terminal for a response.
2948 */
2949 static int
2950can_get_termresponse()
2951{
2952 return cur_tmode == TMODE_RAW
2953 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002954# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002955 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002956# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002957 && p_ek;
2958}
2959
Bram Moolenaarafd78262019-05-10 23:10:31 +02002960/*
2961 * Set "status" to STATUS_SENT.
2962 */
2963 static void
2964termrequest_sent(termrequest_T *status)
2965{
2966 status->tr_progress = STATUS_SENT;
2967 status->tr_start = time(NULL);
2968}
2969
2970/*
2971 * Return TRUE if any of the requests are in STATUS_SENT.
2972 */
2973 static int
2974termrequest_any_pending()
2975{
2976 int i;
2977 time_t now = time(NULL);
2978
2979 for (i = 0; all_termrequests[i] != NULL; ++i)
2980 {
2981 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2982 {
2983 if (all_termrequests[i]->tr_start > 0 && now > 0
2984 && all_termrequests[i]->tr_start + 2 < now)
2985 // Sent the request more than 2 seconds ago and didn't get a
2986 // response, assume it failed.
2987 all_termrequests[i]->tr_progress = STATUS_FAIL;
2988 else
2989 return TRUE;
2990 }
2991 }
2992 return FALSE;
2993}
2994
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002995static int winpos_x = -1;
2996static int winpos_y = -1;
2997static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002998
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002999# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003000/*
3001 * Try getting the Vim window position from the terminal.
3002 * Returns OK or FAIL.
3003 */
3004 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01003005term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003006{
3007 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003008 int prev_winpos_x = winpos_x;
3009 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003010
3011 if (*T_CGP == NUL || !can_get_termresponse())
3012 return FAIL;
3013 winpos_x = -1;
3014 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003015 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003016 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003017 OUT_STR(T_CGP);
3018 out_flush();
3019
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003020 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003021 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003022 {
3023 (void)vpeekc_nomap();
3024 if (winpos_x >= 0 && winpos_y >= 0)
3025 {
3026 *x = winpos_x;
3027 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003028 return OK;
3029 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01003030 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003031 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003032 // Do not reset "did_request_winpos", if we timed out the response might
3033 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003034
3035 winpos_x = prev_winpos_x;
3036 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02003037 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003038 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003039 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003040 *x = winpos_x;
3041 *y = winpos_y;
3042 return OK;
3043 }
3044
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003045 return FALSE;
3046}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003047# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003048# endif
3049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003051term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003053 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054}
3055#endif
3056
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003058term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059{
3060 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003061 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003062 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003064 // Special handling of 16 colors, because termcap can't handle it
3065 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3066 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003068 && ((s[0] == ESC && s[1] == '[')
3069#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3070 || (s[0] == ESC && s[1] == '|')
3071#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003072 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 && s[i] != NUL
3074 && (STRCMP(s + i + 1, "%p1%dm") == 0
3075 || STRCMP(s + i + 1, "%dm") == 0)
3076 && (s[i] == '3' || s[i] == '4'))
3077 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003079 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003081 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003083 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003084#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003085 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003086#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003087 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003088 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3089 : (n >= 16 ? "48;5;" : "10");
3090
3091 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3093 }
3094 else
3095 OUT_STR(tgoto((char *)s, 0, n));
3096}
3097
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003098 void
3099term_fg_color(int n)
3100{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003101 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003102 if (*T_CAF)
3103 term_color(T_CAF, n);
3104 else if (*T_CSF)
3105 term_color(T_CSF, n);
3106}
3107
3108 void
3109term_bg_color(int n)
3110{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003111 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003112 if (*T_CAB)
3113 term_color(T_CAB, n);
3114 else if (*T_CSB)
3115 term_color(T_CSB, n);
3116}
3117
Bram Moolenaare023e882020-05-31 16:42:30 +02003118 void
3119term_ul_color(int n)
3120{
3121 if (*T_CAU)
3122 term_color(T_CAU, n);
3123}
3124
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003125/*
3126 * Return "dark" or "light" depending on the kind of terminal.
3127 * This is just guessing! Recognized are:
3128 * "linux" Linux console
3129 * "screen.linux" Linux console with screen
3130 * "cygwin.*" Cygwin shell
3131 * "putty.*" Putty program
3132 * We also check the COLORFGBG environment variable, which is set by
3133 * rxvt and derivatives. This variable contains either two or three
3134 * values separated by semicolons; we want the last value in either
3135 * case. If this value is 0-6 or 8, our background is dark.
3136 */
3137 char_u *
3138term_bg_default(void)
3139{
3140#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003141 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003142 return (char_u *)"dark";
3143#else
3144 char_u *p;
3145
3146 if (STRCMP(T_NAME, "linux") == 0
3147 || STRCMP(T_NAME, "screen.linux") == 0
3148 || STRNCMP(T_NAME, "cygwin", 6) == 0
3149 || STRNCMP(T_NAME, "putty", 5) == 0
3150 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3151 && (p = vim_strrchr(p, ';')) != NULL
3152 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3153 && p[2] == NUL))
3154 return (char_u *)"dark";
3155 return (char_u *)"light";
3156#endif
3157}
3158
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003159#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003160
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003161#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3162#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3163#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003164
3165 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003166term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003167{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003168#define MAX_COLOR_STR_LEN 100
3169 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003170
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003171 if (*s == NUL)
3172 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003173 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003174 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003175#ifdef FEAT_VTP
Christopher Plewright38804d62022-11-09 23:55:52 +00003176 if (has_vtp_working())
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003177 {
3178 out_flush();
3179 buf[1] = '[';
3180 vtp_printf(buf);
3181 }
3182 else
3183#endif
3184 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003185}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003186
3187 void
3188term_fg_rgb_color(guicolor_T rgb)
3189{
Christopher Plewright38804d62022-11-09 23:55:52 +00003190 if (rgb != INVALCOLOR)
3191 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003192}
3193
3194 void
3195term_bg_rgb_color(guicolor_T rgb)
3196{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003197 if (rgb != INVALCOLOR)
3198 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003199}
Bram Moolenaare023e882020-05-31 16:42:30 +02003200
3201 void
3202term_ul_rgb_color(guicolor_T rgb)
3203{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003204# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003205 // If the user explicitly sets t_8u then use it. Otherwise wait for
3206 // termresponse to be received, which is when t_8u would be set and a
3207 // redraw is needed if it was used.
3208 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003209 write_t_8u_state = MAYBE;
3210 else
3211# endif
3212 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003213}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003214#endif
3215
Bram Moolenaar651fca82021-11-29 20:39:38 +00003216#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217/*
3218 * Generic function to set window title, using t_ts and t_fs.
3219 */
3220 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003221term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003223 MAY_WANT_TO_LOG_THIS;
3224
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003225 // t_ts takes one argument: column in status line
3226 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003228 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 out_flush();
3230}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003231
3232/*
3233 * Tell the terminal to push (save) the title and/or icon, so that it can be
3234 * popped (restored) later.
3235 */
3236 void
3237term_push_title(int which)
3238{
Bram Moolenaar27821262019-05-08 16:41:09 +02003239 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003240 {
3241 OUT_STR(T_CST);
3242 out_flush();
3243 }
3244
Bram Moolenaar27821262019-05-08 16:41:09 +02003245 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003246 {
3247 OUT_STR(T_SSI);
3248 out_flush();
3249 }
3250}
3251
3252/*
3253 * Tell the terminal to pop the title and/or icon.
3254 */
3255 void
3256term_pop_title(int which)
3257{
Bram Moolenaar27821262019-05-08 16:41:09 +02003258 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003259 {
3260 OUT_STR(T_CRT);
3261 out_flush();
3262 }
3263
Bram Moolenaar27821262019-05-08 16:41:09 +02003264 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003265 {
3266 OUT_STR(T_SRI);
3267 out_flush();
3268 }
3269}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270#endif
3271
3272/*
3273 * Make sure we have a valid set or terminal options.
3274 * Replace all entries that are NULL by empty_option
3275 */
3276 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003277ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003279 char_u *env_colors;
3280
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003281 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
3283 /*
3284 * MUST have "cm": cursor motion.
3285 */
3286 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003287 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288
3289 /*
3290 * if "cs" defined, use a scroll region, it's faster.
3291 */
3292 if (*T_CS != NUL)
3293 scroll_region = TRUE;
3294 else
3295 scroll_region = FALSE;
3296
3297 if (pairs)
3298 {
3299 /*
3300 * optional pairs
3301 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003302 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 if (*T_ME == NUL)
3304 T_ME = T_MR = T_MD = T_MB = empty_option;
3305 if (*T_SO == NUL || *T_SE == NUL)
3306 T_SO = T_SE = empty_option;
3307 if (*T_US == NUL || *T_UE == NUL)
3308 T_US = T_UE = empty_option;
3309 if (*T_CZH == NUL || *T_CZR == NUL)
3310 T_CZH = T_CZR = empty_option;
3311
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003312 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 if (*T_VE == NUL)
3314 T_VI = empty_option;
3315
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003316 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (*T_ME == NUL)
3318 {
3319 T_ME = T_SE;
3320 T_MR = T_SO;
3321 T_MD = T_SO;
3322 }
3323
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003324 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 if (*T_SO == NUL)
3326 {
3327 T_SE = T_ME;
3328 if (*T_MR == NUL)
3329 T_SO = T_MD;
3330 else
3331 T_SO = T_MR;
3332 }
3333
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003334 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 if (*T_CZH == NUL)
3336 {
3337 T_CZR = T_ME;
3338 if (*T_MR == NUL)
3339 T_CZH = T_MD;
3340 else
3341 T_CZH = T_MR;
3342 }
3343
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003344 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (*T_CSB == NUL || *T_CSF == NUL)
3346 {
3347 T_CSB = empty_option;
3348 T_CSF = empty_option;
3349 }
3350
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003351 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 if (*T_CAB == NUL || *T_CAF == NUL)
3353 {
3354 T_CAB = empty_option;
3355 T_CAF = empty_option;
3356 }
3357
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003358 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003360 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003362 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 p_wiv = (*T_XS != NUL);
3364 }
3365 need_gather = TRUE;
3366
Bram Moolenaar759d8152020-04-26 16:52:49 +02003367 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3368 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003370#ifdef FEAT_GUI
3371 if (!gui.in_use)
3372#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003373 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003374 env_colors = mch_getenv((char_u *)"COLORS");
3375 if (env_colors != NULL && isdigit(*env_colors))
3376 {
3377 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003378
Bram Moolenaar759d8152020-04-26 16:52:49 +02003379 if (colors != t_colors)
3380 set_color_count(colors);
3381 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383}
3384
3385#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3386 || defined(PROTO)
3387/*
3388 * Represent the given long_u as individual bytes, with the most significant
3389 * byte first, and store them in dst.
3390 */
3391 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003392add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
3394 int i;
3395 int shift;
3396
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003397 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 {
3399 shift = 8 * (sizeof(long_u) - i);
3400 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3401 }
3402}
3403
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404/*
3405 * Interpret the next string of bytes in buf as a long integer, with the most
3406 * significant byte first. Note that it is assumed that buf has been through
3407 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3408 * Puts result in val, and returns the number of bytes read from buf
3409 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3410 * were present.
3411 */
3412 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003413get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414{
3415 int len;
3416 char_u bytes[sizeof(long_u)];
3417 int i;
3418 int shift;
3419
3420 *val = 0;
3421 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3422 if (len != -1)
3423 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003424 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 {
3426 shift = 8 * (sizeof(long_u) - 1 - i);
3427 *val += (long_u)bytes[i] << shift;
3428 }
3429 }
3430 return len;
3431}
3432#endif
3433
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434/*
3435 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3436 * that buf has been through inchar(). Returns the actual number of bytes used
3437 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3438 * available.
3439 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003440 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003441get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 int len = 0;
3444 int i;
3445 char_u c;
3446
3447 for (i = 0; i < num_bytes; i++)
3448 {
3449 if ((c = buf[len++]) == NUL)
3450 return -1;
3451 if (c == K_SPECIAL)
3452 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003453 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 return -1;
3455 if (buf[len++] == (int)KS_ZERO)
3456 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003457 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3458 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003459 if (buf[len++] == (int)KE_CSI)
3460 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003462 else if (c == CSI && buf[len] == KS_EXTRA
3463 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003464 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3465 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003466 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 bytes[i] = c;
3468 }
3469 return len;
3470}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
3472/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003473 * Check if the new shell size is valid, correct it if it's too small or way
3474 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 */
3476 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003477check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003479 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003481 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003482
3483 // make sure these values are not invalid
3484 if (cmdline_row >= Rows)
3485 cmdline_row = Rows - 1;
3486 if (msg_row >= Rows)
3487 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003488}
3489
3490/*
3491 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3492 */
3493 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003494limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003495{
3496 if (Columns < MIN_COLUMNS)
3497 Columns = MIN_COLUMNS;
3498 else if (Columns > 10000)
3499 Columns = 10000;
3500 if (Rows > 1000)
3501 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502}
3503
Bram Moolenaar86b68352004-12-27 21:59:20 +00003504/*
3505 * Invoked just before the screen structures are going to be (re)allocated.
3506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003508win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509{
3510 static int old_Rows = 0;
3511 static int old_Columns = 0;
3512
3513 if (old_Rows != Rows || old_Columns != Columns)
3514 ui_new_shellsize();
3515 if (old_Rows != Rows)
3516 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003517 // If 'window' uses the whole screen, keep it using that.
3518 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003519 if (p_window == old_Rows - 1
3520 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003521 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003523 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
3525 if (old_Columns != Columns)
3526 {
3527 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003528 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
3530}
3531
3532/*
3533 * Call this function when the Vim shell has been resized in any way.
3534 * Will obtain the current size and redraw (also when size didn't change).
3535 */
3536 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003537shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538{
3539 set_shellsize(0, 0, FALSE);
3540}
3541
3542/*
3543 * Check if the shell size changed. Handle a resize.
3544 * When the size didn't change, nothing happens.
3545 */
3546 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003547shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548{
3549 int old_Rows = Rows;
3550 int old_Columns = Columns;
3551
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003552 if (!exiting
3553#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003554 // Do not get the size when executing a shell command during
3555 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003556 && !gui.starting
3557#endif
3558 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003559 {
3560 (void)ui_get_shellsize();
3561 check_shellsize();
3562 if (old_Rows != Rows || old_Columns != Columns)
3563 shell_resized();
3564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565}
3566
3567/*
3568 * Set size of the Vim shell.
3569 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3570 * window size (this is used for the :win command).
3571 * If 'mustset' is FALSE, we may try to get the real window size and if
3572 * it fails use 'width' and 'height'.
3573 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003574 static void
3575set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003577 if (updating_screen)
3578 // resizing while in update_screen() may cause a crash
3579 return;
3580
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003581 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003582 // buffer (or window) has already been closed and removing a scrollbar
3583 // causes a resize event. Don't resize then, it will happen after entering
3584 // another buffer.
3585 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003586 return;
3587
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003589 out_flush(); // must do this before mch_get_shellsize() for
3590 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591#endif
3592
3593 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3594 {
3595 Rows = height;
3596 Columns = width;
3597 check_shellsize();
3598 ui_set_shellsize(mustset);
3599 }
3600 else
3601 check_shellsize();
3602
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003603 // The window layout used to be adjusted here, but it now happens in
3604 // screenalloc() (also invoked from screenclear()). That is because the
3605 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
Bram Moolenaar24959102022-05-07 20:01:16 +01003607 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3608 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 screenclear();
3610 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003611 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
3613 if (starting != NO_SCREEN)
3614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003616
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 changed_line_abv_curs();
3618 invalidate_botline();
3619
3620 /*
3621 * We only redraw when it's needed:
3622 * - While at the more prompt or executing an external command, don't
3623 * redraw, but position the cursor.
3624 * - While editing the command line, only redraw that.
3625 * - in Ex mode, don't redraw anything.
3626 * - Otherwise, redraw right now, and position the cursor.
3627 * Always need to call update_screen() or screenalloc(), to make
3628 * sure Rows/Columns and the size of ScreenLines[] is correct!
3629 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003630 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3631 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 {
3633 screenalloc(FALSE);
3634 repeat_message();
3635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 else
3637 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003638 if (curwin->w_p_scb)
3639 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003640 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003641 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003642 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003643 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003644 }
3645 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003646 {
3647 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003648 if (pum_visible())
3649 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003650 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003651 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003652 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003653 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003654 if (redrawing())
3655 setcursor();
3656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003658 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
3660 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003661}
3662
3663 void
3664set_shellsize(int width, int height, int mustset)
3665{
3666 static int busy = FALSE;
3667 static int do_run = FALSE;
3668
3669 if (width < 0 || height < 0) // just checking...
3670 return;
3671
3672 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3673 {
3674 // postpone the resizing
3675 State = MODE_SETWSIZE;
3676 return;
3677 }
3678
3679 // Avoid recursiveness. This can happen when setting the window size
3680 // causes another window-changed signal or when two SIGWINCH signals come
3681 // very close together. There needs to be another run then after the
3682 // current one is done to pick up the latest size.
3683 do_run = TRUE;
3684 if (busy)
3685 return;
3686
3687 while (do_run)
3688 {
3689 do_run = FALSE;
3690 busy = TRUE;
3691 set_shellsize_inner(width, height, mustset);
3692 busy = FALSE;
3693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694}
3695
3696/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003697 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3698 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3699 */
3700 void
3701out_str_t_TE(void)
3702{
3703 out_str(T_CTE);
3704
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003705 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
Bram Moolenaarc255b782022-11-26 19:16:48 +00003706 // disable modifyOtherKeys, but until Xterm version 377 there is no way to
3707 // detect it's enabled again after the following t_TI. We assume that when
3708 // seenModifyOtherKeys was set before it will still be valid.
3709
3710 // When the modifyOtherKeys level is detected to be 2 we expect t_TE to
3711 // disable it. Remembering that it was detected to be enabled is useful in
3712 // some situations.
3713 // The following t_TI is expected to request the state and then
3714 // modify_otherkeys_state will be set again.
3715 if (modify_otherkeys_state == MOKS_ENABLED
3716 || modify_otherkeys_state == MOKS_DISABLED)
3717 modify_otherkeys_state = MOKS_DISABLED;
3718 else if (modify_otherkeys_state != MOKS_INITIAL)
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003719 modify_otherkeys_state = MOKS_AFTER_T_TE;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003720
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003721 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3722 // it. Remembering that it was detected to be enabled is useful in some
3723 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003724 // The following t_TI is expected to request the state and then
3725 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003726 if (kitty_protocol_state == KKPS_ENABLED
3727 || kitty_protocol_state == KKPS_DISABLED)
3728 kitty_protocol_state = KKPS_DISABLED;
3729 else
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003730 kitty_protocol_state = KKPS_AFTER_T_TE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003731}
3732
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003733static int send_t_RK = FALSE;
3734
3735/*
3736 * Output T_TI and setup for what follows.
3737 */
3738 void
3739out_str_t_TI(void)
3740{
3741 out_str(T_CTI);
3742
3743 // Send t_RK when there is no more work to do.
3744 send_t_RK = TRUE;
3745}
3746
3747/*
3748 * If t_TI was recently sent and there is no typeahead or work to do, now send
3749 * t_RK. This is postponed to avoid the response arriving in a shell command
3750 * or after Vim exits.
3751 */
3752 void
3753may_send_t_RK(void)
3754{
3755 if (send_t_RK
3756 && !work_pending()
3757 && !ex_normal_busy
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003758#ifdef FEAT_EVAL
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003759 && !in_feedkeys
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003760#endif
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003761 && !exiting)
3762 {
3763 send_t_RK = FALSE;
3764 out_str(T_CRK);
3765 out_flush();
3766 }
3767}
3768
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3771 * commands and Ex mode).
3772 */
3773 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003774settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775{
3776#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003777 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 if (gui.in_use)
3779 return;
3780#endif
3781
3782 if (full_screen)
3783 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003785 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3786 * set the terminal to raw mode, even though we think it already is,
3787 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 * When we think the terminal is normal, don't try to set it to
3789 * normal again, because that causes problems (logout!) on some
3790 * machines.
3791 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003792 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 {
3794#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003795# ifdef FEAT_GUI
3796 if (!gui.in_use && !gui.starting)
3797# endif
3798 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003799 // May need to check for T_CRV response and termcodes, it
3800 // doesn't work in Cooked mode, an external program may get
3801 // them.
3802 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003803 (void)vpeekc_nomap();
3804 check_for_codes_from_term();
3805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003808 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003809
3810 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3811 // Avoid doing this too often, on some terminals the codes are not
3812 // handled properly.
3813 if (termcap_active && tmode != TMODE_SLEEP
3814 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003815 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003816 MAY_WANT_TO_LOG_THIS;
3817
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003818 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003819 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003820 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003821 out_str_t_TE(); // possibly disables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003822 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003823 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003824 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003825 out_str(T_BE); // enable bracketed paste mode (should
3826 // be before mch_settmode().
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003827 out_str_t_TI(); // possibly enables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003828 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003831 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003834 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 out_flush();
3836 }
3837#ifdef FEAT_TERMRESPONSE
3838 may_req_termresponse();
3839#endif
3840 }
3841}
3842
3843 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003844starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845{
3846 if (full_screen && !termcap_active)
3847 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003848 MAY_WANT_TO_LOG_THIS;
3849
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003850 out_str(T_TI); // start termcap mode
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003851 out_str_t_TI(); // start "raw" mode
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003852 out_str(T_KS); // start "keypad transmit" mode
3853 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003854
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003855#if defined(UNIX) || defined(VMS)
3856 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003857 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003858 out_str(T_FE);
3859#endif
3860
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 out_flush();
3862 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003863 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003865# ifdef FEAT_GUI
3866 if (!gui.in_use && !gui.starting)
3867# endif
3868 {
3869 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003870 // Immediately check for a response. If t_Co changes, we don't
3871 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003872 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003873 check_for_codes_from_term();
3874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875#endif
3876 }
3877}
3878
3879 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003880stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881{
3882 screen_stop_highlight();
3883 reset_cterm_colors();
3884 if (termcap_active)
3885 {
3886#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003887# ifdef FEAT_GUI
3888 if (!gui.in_use && !gui.starting)
3889# endif
3890 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003891 // May need to discard T_CRV, T_U7 or T_RBG response.
3892 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003893 {
3894# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003895 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003896 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003897# endif
3898# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003899 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003900 if (exiting)
3901 tcflush(fileno(stdin), TCIFLUSH);
3902# endif
3903 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003904 // Check for termcodes first, otherwise an external program may
3905 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003906 check_for_codes_from_term();
3907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003909 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003910
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003911#if defined(UNIX) || defined(VMS)
3912 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003913 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003914 out_str(T_FD);
3915#endif
3916
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003917 out_str(T_BD); // disable bracketed paste mode
3918 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 out_flush();
3920 termcap_active = FALSE;
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00003921
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003922 // Output t_te before t_TE, t_te may switch between main and alternate
3923 // screen and following codes may work on the active screen only.
3924 //
3925 // When using the Kitty keyboard protocol the main and alternate screen
3926 // use a separate state. If we are (or were) using the Kitty keyboard
3927 // protocol and t_te is not empty (possibly switching screens) then
3928 // output t_TE both before and after outputting t_te.
3929 if (*T_TE != NUL && (kitty_protocol_state == KKPS_ENABLED
3930 || kitty_protocol_state == KKPS_DISABLED))
3931 out_str_t_TE(); // probably disables the kitty keyboard
3932 // protocol
3933
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00003934 out_str(T_TE); // stop termcap mode
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003935 cursor_on(); // just in case it is still off
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003936 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
3937 // Kitty keyboard protocol
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003938 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 out_flush();
3940 }
3941}
3942
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003943#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944/*
3945 * Request version string (for xterm) when needed.
3946 * Only do this after switching to raw mode, otherwise the result will be
3947 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003948 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003949 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 * Only do this after termcap mode has been started, otherwise the codes for
3951 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003952 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3953 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003954 * On Unix only do it when both output and input are a tty (avoid writing
3955 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 * The result is caught in check_termcode().
3957 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003958 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003959may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003961 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003962 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003963 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 && *T_CRV != NUL)
3965 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003966 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003967 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003969 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003970 // check for the characters now, otherwise they might be eaten by
3971 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 out_flush();
3973 (void)vpeekc_nomap();
3974 }
3975}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003976
Bram Moolenaar9584b312013-03-13 19:29:28 +01003977/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003978 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3979 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003980 * Note that this goes out before T_CRV, so that the result can be used when
3981 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003982 */
3983 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003984check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003985{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003986 int did_send = FALSE;
3987
3988 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3989 return;
3990
Bram Moolenaarafd78262019-05-10 23:10:31 +02003991 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003992 && !option_was_set((char_u *)"ambiwidth"))
3993 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003994 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003995
Bram Moolenaara45551a2020-06-09 15:57:37 +02003996 // Ambiguous width check.
3997 // Check how the terminal treats ambiguous character width (UAX #11).
3998 // First, we move the cursor to (1, 0) and print a test ambiguous
3999 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
4000 // the current cursor position. If the terminal treats \u25bd as
4001 // single width, the position is (1, 1), or if it is treated as double
4002 // width, that will be (1, 2). This function has the side effect that
4003 // changes cursor position, so it must be called immediately after
4004 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004005 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004006 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004007 // Do this in the second row. In the first row the returned sequence
4008 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004009 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004010 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02004011 out_str(buf);
4012 out_str(T_U7);
4013 termrequest_sent(&u7_status);
4014 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02004015 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02004016
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004017 // This overwrites a few characters on the screen, a redraw is needed
4018 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02004019 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02004020 term_windgoto(1, 0);
4021 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004022 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004023 }
4024
Bram Moolenaard1f34e62022-01-07 19:24:20 +00004025 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02004026 {
4027 // 2. Check compatibility with xterm.
4028 // We move the cursor to (2, 0), print a test sequence and then query
4029 // the current cursor position. If the terminal properly handles
4030 // unknown DCS string and CSI sequence with intermediate byte, the test
4031 // sequence is ignored and the cursor does not move. If the terminal
4032 // handles test sequence incorrectly, a garbage string is displayed and
4033 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004034 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004035 LOG_TR(("Sending xterm compatibility test sequence."));
4036 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004037 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02004038 term_windgoto(2, 0);
4039 // send the test DCS string.
4040 out_str((char_u *)"\033Pzz\033\\");
4041 // send the test CSI sequence with intermediate byte.
4042 out_str((char_u *)"\033[0%m");
4043 out_str(T_U7);
4044 termrequest_sent(&xcc_status);
4045 out_flush();
4046 did_send = TRUE;
4047
4048 // If the terminal handles test sequence incorrectly, garbage text is
4049 // displayed. Clear them out for now.
4050 screen_stop_highlight();
4051 term_windgoto(2, 0);
4052 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004053 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004054 }
4055
4056 if (did_send)
4057 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004058 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02004059
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004060 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004061 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01004062
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004063 // check for the characters now, otherwise they might be eaten by
4064 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02004065 out_flush();
4066 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01004067 }
4068}
Bram Moolenaar2951b772013-07-03 12:45:31 +02004069
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004070/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02004071 * Similar to requesting the version string: Request the terminal background
4072 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004073 */
4074 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004075may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004076{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004077 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004078 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004079 int didit = FALSE;
4080
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004081# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004082 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004083 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004084 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004085 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004086 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004087 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004088 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004089 didit = TRUE;
4090 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004091# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004092
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004093 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004094 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004095 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004096 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004097 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004098 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004099 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004100 didit = TRUE;
4101 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004102
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004103 if (didit)
4104 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004105 // check for the characters now, otherwise they might be eaten by
4106 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004107 out_flush();
4108 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004109 }
4110 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004111}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004112
Bram Moolenaar2951b772013-07-03 12:45:31 +02004113# ifdef DEBUG_TERMRESPONSE
4114 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02004115log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004116{
4117 static FILE *fd_tr = NULL;
4118 static proftime_T start;
4119 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004120 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004121
4122 if (fd_tr == NULL)
4123 {
4124 fd_tr = fopen("termresponse.log", "w");
4125 profile_start(&start);
4126 }
4127 now = start;
4128 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02004129 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004130 must_redraw == UPD_NOT_VALID ? "NV"
4131 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02004132 va_start(ap, fmt);
4133 vfprintf(fd_tr, fmt, ap);
4134 va_end(ap);
4135 fputc('\n', fd_tr);
4136 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02004137}
4138# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139#endif
4140
4141/*
4142 * Return TRUE when saving and restoring the screen.
4143 */
4144 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004145swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146{
4147 return (full_screen && *T_TI != NUL);
4148}
4149
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150/*
4151 * By outputting the 'cursor very visible' termcap code, for some windowed
4152 * terminals this makes the screen scrolled to the correct position.
4153 * Used when starting Vim or returning from a shell.
4154 */
4155 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004156scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004158 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004160 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004162 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004163 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
4165}
4166
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004167// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168static int cursor_is_off = FALSE;
4169
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004170// True if cursor is not visible due to an ongoing cursor-less sleep
4171static int cursor_is_asleep = FALSE;
4172
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004174 * Enable the cursor without checking if it's already enabled.
4175 */
4176 void
4177cursor_on_force(void)
4178{
4179 out_str(T_VE);
4180 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004181 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004182}
4183
4184/*
4185 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 */
4187 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004188cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004190 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004191 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192}
4193
4194/*
4195 * Disable the cursor.
4196 */
4197 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004198cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004200 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004202 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 cursor_is_off = TRUE;
4204 }
4205}
4206
Dominique Pelle748b3082022-01-08 12:41:16 +00004207#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004208/*
4209 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4210 */
4211 int
4212cursor_is_sleeping(void)
4213{
4214 return cursor_is_asleep;
4215}
Dominique Pelle748b3082022-01-08 12:41:16 +00004216#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004217
4218/*
4219 * Disable the cursor and mark it disabled by cursor-less sleep
4220 */
4221 void
4222cursor_sleep(void)
4223{
4224 cursor_is_asleep = TRUE;
4225 cursor_off();
4226}
4227
4228/*
4229 * Enable the cursor and mark it not disabled by cursor-less sleep
4230 */
4231 void
4232cursor_unsleep(void)
4233{
4234 cursor_is_asleep = FALSE;
4235 cursor_on();
4236}
4237
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004238#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004240 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004241 */
4242 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004243term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004244{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004245 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004246 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004247
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004248 // Only do something when redrawing the screen and we can restore the
4249 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004250 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004251 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004252# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004253 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004254 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004255 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004256# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004257 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004258 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004259
Bram Moolenaar24959102022-05-07 20:01:16 +01004260 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004261 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004262 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004263 {
4264 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004265 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004266 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004267 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004268 if (*p != NUL)
4269 {
4270 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004271 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004272 }
4273 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004274 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004275 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004276 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004277 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004278 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004279 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004280 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004281 }
4282 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004283 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004284 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004285 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004286 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004287 }
4288}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004289
4290# if defined(FEAT_TERMINAL) || defined(PROTO)
4291 void
4292term_cursor_color(char_u *color)
4293{
4294 if (*T_CSC != NUL)
4295 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004296 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004297 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004298 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004299 out_flush();
4300 }
4301}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004302# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004303
Bram Moolenaar4db25542017-08-28 22:43:05 +02004304 int
4305blink_state_is_inverted()
4306{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004307#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004308 return rbm_status.tr_progress == STATUS_GOT
4309 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004310 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004311#else
4312 return FALSE;
4313#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004314}
4315
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004316/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004317 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004318 */
4319 void
4320term_cursor_shape(int shape, int blink)
4321{
4322 if (*T_CSH != NUL)
4323 {
4324 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4325 out_flush();
4326 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004327 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004328 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004329 int do_blink = blink;
4330
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004331 // t_SH is empty: try setting just the blink state.
4332 // The blink flags are XORed together, if the initial blinking from
4333 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004334 if (blink_state_is_inverted())
4335 do_blink = !blink;
4336
4337 if (do_blink && *T_VS != NUL)
4338 {
4339 out_str(T_VS);
4340 out_flush();
4341 }
4342 else if (!do_blink && *T_CVS != NUL)
4343 {
4344 out_str(T_CVS);
4345 out_flush();
4346 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004347 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004348}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004349#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004350
4351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 * Set scrolling region for window 'wp'.
4353 * The region starts 'off' lines from the start of the window.
4354 * Also set the vertical scroll region for a vertically split window. Always
4355 * the full width of the window, excluding the vertical separator.
4356 */
4357 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004358scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359{
4360 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4361 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004363 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4364 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004365 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366}
4367
4368/*
4369 * Reset scrolling region to the whole screen.
4370 */
4371 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004372scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373{
4374 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 if (*T_CSV != NUL)
4376 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004377 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378}
4379
4380
4381/*
4382 * List of terminal codes that are currently recognized.
4383 */
4384
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004385static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004387 char_u name[2]; // termcap name of entry
4388 char_u *code; // terminal code (in allocated memory)
4389 int len; // STRLEN(code)
4390 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391} *termcodes = NULL;
4392
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004393static int tc_max_len = 0; // number of entries that termcodes[] can hold
4394static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004396static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004397
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004399clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400{
4401 while (tc_len > 0)
4402 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004403 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 tc_max_len = 0;
4405
4406#ifdef HAVE_TGETENT
4407 BC = (char *)empty_option;
4408 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004409 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 ospeed = 0;
4411#endif
4412
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004413 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414}
4415
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004416#define ATC_FROM_TERM 55
4417
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418/*
Bram Moolenaarb2646172022-12-17 15:35:43 +00004419 * Add a new entry for "name[2]" to the list of terminal codes.
4420 * Note that "name" may not have a terminating NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004422 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4423 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 */
4425 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004426add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427{
4428 struct termcode *new_tc;
4429 int i, j;
4430 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004431 int len;
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004432#ifdef FEAT_EVAL
4433 char *action = "Setting";
4434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
4436 if (string == NULL || *string == NUL)
4437 {
4438 del_termcode(name);
4439 return;
4440 }
4441
Bram Moolenaar4f974752019-02-17 17:44:42 +01004442#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004443 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004444#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004445# ifdef VIMDLL
4446 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004447 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004448 else
4449# endif
4450 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 if (s == NULL)
4453 return;
4454
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004455 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004456 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004458 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 s[0] = term_7to8bit(string);
4460 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004461
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004462#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4463# ifdef VIMDLL
4464 if (!gui.in_use)
4465# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004466 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004467 if (s[0] == K_NUL)
4468 {
4469 STRMOVE(s + 1, s);
4470 s[1] = 3;
4471 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004472 }
4473#endif
4474
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004475 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004477 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
4479 /*
4480 * need to make space for more entries
4481 */
4482 if (tc_len == tc_max_len)
4483 {
4484 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004485 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 if (new_tc == NULL)
4487 {
4488 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004489 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 return;
4491 }
4492 for (i = 0; i < tc_len; ++i)
4493 new_tc[i] = termcodes[i];
4494 vim_free(termcodes);
4495 termcodes = new_tc;
4496 }
4497
4498 /*
4499 * Look for existing entry with the same name, it is replaced.
4500 * Look for an existing entry that is alphabetical higher, the new entry
4501 * is inserted in front of it.
4502 */
4503 for (i = 0; i < tc_len; ++i)
4504 {
4505 if (termcodes[i].name[0] < name[0])
4506 continue;
4507 if (termcodes[i].name[0] == name[0])
4508 {
4509 if (termcodes[i].name[1] < name[1])
4510 continue;
4511 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004512 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 */
4514 if (termcodes[i].name[1] == name[1])
4515 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004516 if (flags == ATC_FROM_TERM && (j = termcode_star(
4517 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004518 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004519 // Don't replace ESC[123;*X or ESC O*X with another when
4520 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004521 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004522 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004523 && s[len - 1]
4524 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004525 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004526 // They are equal but for the ";*": don't add it.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004527#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004528 ch_log(NULL, "Termcap entry %c%c did not change",
4529 name[0], name[1]);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004530#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004531 vim_free(s);
4532 return;
4533 }
4534 }
4535 else
4536 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004537 // Replace old code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004538#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004539 ch_log(NULL, "Termcap entry %c%c was: %s",
4540 name[0], name[1], termcodes[i].code);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004541#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004542 vim_free(termcodes[i].code);
4543 --tc_len;
4544 break;
4545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 }
4547 }
4548 /*
4549 * Found alphabetical larger entry, move rest to insert new entry
4550 */
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004551#ifdef FEAT_EVAL
4552 action = "Adding";
4553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 for (j = tc_len; j > i; --j)
4555 termcodes[j] = termcodes[j - 1];
4556 break;
4557 }
4558
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004559#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004560 ch_log(NULL, "%s termcap entry %c%c to %s", action, name[0], name[1], s);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 termcodes[i].name[0] = name[0];
4563 termcodes[i].name[1] = name[1];
4564 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004565 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004566
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004567 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4568 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004569 termcodes[i].modlen = 0;
4570 j = termcode_star(s, len);
4571 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004572 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004573 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004574 // For "CSI[@;X" the "@" is not included in "modlen".
4575 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4576 --termcodes[i].modlen;
4577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 ++tc_len;
4579}
4580
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004581/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004582 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004583 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004584 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004585 */
4586 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004587termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004588{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004589 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004590 if (len >= 3 && code[len - 2] == '*')
4591 {
4592 if (len >= 5 && code[len - 3] == ';')
4593 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004594 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004595 return 1;
4596 }
4597 return 0;
4598}
4599
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004601find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602{
4603 int i;
4604
4605 for (i = 0; i < tc_len; ++i)
4606 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4607 return termcodes[i].code;
4608 return NULL;
4609}
4610
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004612get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613{
4614 if (i >= tc_len)
4615 return NULL;
4616 return &termcodes[i].name[0];
4617}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004619/*
4620 * Returns the length of the terminal code at index 'idx'.
4621 */
4622 int
4623get_termcode_len(int idx)
4624{
4625 return termcodes[idx].len;
4626}
4627
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004628 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004629del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
4631 int i;
4632
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004633 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 return;
4635
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004636 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637
4638 for (i = 0; i < tc_len; ++i)
4639 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4640 {
4641 del_termcode_idx(i);
4642 return;
4643 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004644 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645}
4646
4647 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004648del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649{
4650 int i;
4651
4652 vim_free(termcodes[idx].code);
4653 --tc_len;
4654 for (i = idx; i < tc_len; ++i)
4655 termcodes[i] = termcodes[i + 1];
4656}
4657
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658/*
4659 * Called when detected that the terminal sends 8-bit codes.
4660 * Convert all 7-bit codes to their 8-bit equivalent.
4661 */
4662 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004663switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664{
4665 int i;
4666 int c;
4667
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004668 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (!term_is_8bit(T_NAME))
4670 {
4671 for (i = 0; i < tc_len; ++i)
4672 {
4673 c = term_7to8bit(termcodes[i].code);
4674 if (c != 0)
4675 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004676 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 termcodes[i].code[0] = c;
4678 }
4679 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004680 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
4682 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004683 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685
4686#ifdef CHECK_DOUBLE_CLICK
4687static linenr_T orig_topline = 0;
4688# ifdef FEAT_DIFF
4689static int orig_topfill = 0;
4690# endif
4691#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004692#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004694 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 * "orig_topline" is used to avoid detecting a double-click when the window
4696 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4697 */
4698/*
4699 * Set orig_topline. Used when jumping to another window, so that a double
4700 * click still works.
4701 */
4702 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004703set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
4705 orig_topline = wp->w_topline;
4706# ifdef FEAT_DIFF
4707 orig_topfill = wp->w_topfill;
4708# endif
4709}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004710
4711/*
4712 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4713 * topline and topfill.
4714 */
4715 int
4716is_mouse_topline(win_T *wp)
4717{
4718 return orig_topline == wp->w_topline
4719#ifdef FEAT_DIFF
4720 && orig_topfill == wp->w_topfill
4721#endif
4722 ;
4723}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724#endif
4725
4726/*
zeertzjqfc78a032022-04-26 22:11:38 +01004727 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4728 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4729 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004730 * Remove "slen" bytes.
4731 * Returns FAIL for error.
4732 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004733 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004734put_string_in_typebuf(
4735 int offset,
4736 int slen,
4737 char_u *string,
4738 int new_slen,
4739 char_u *buf,
4740 int bufsize,
4741 int *buflen)
4742{
4743 int extra = new_slen - slen;
4744
4745 string[new_slen] = NUL;
4746 if (buf == NULL)
4747 {
4748 if (extra < 0)
4749 // remove matched chars, taking care of noremap
4750 del_typebuf(-extra, offset);
4751 else if (extra > 0)
4752 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004753 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4754 == FAIL)
4755 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004756
4757 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4758 // typebuf.tb_buf[]!
4759 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4760 (size_t)new_slen);
4761 }
4762 else
4763 {
4764 if (extra < 0)
4765 // remove matched characters
4766 mch_memmove(buf + offset, buf + offset - extra,
4767 (size_t)(*buflen + offset + extra));
4768 else if (extra > 0)
4769 {
4770 // Insert the extra space we need. If there is insufficient
4771 // space return -1.
4772 if (*buflen + extra + new_slen >= bufsize)
4773 return FAIL;
4774 mch_memmove(buf + offset + extra, buf + offset,
4775 (size_t)(*buflen - offset));
4776 }
4777 mch_memmove(buf + offset, string, (size_t)new_slen);
4778 *buflen = *buflen + extra + new_slen;
4779 }
4780 return OK;
4781}
4782
4783/*
4784 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4785 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004786 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004787decode_modifiers(int n)
4788{
4789 int code = n - 1;
4790 int modifiers = 0;
4791
4792 if (code & 1)
4793 modifiers |= MOD_MASK_SHIFT;
4794 if (code & 2)
4795 modifiers |= MOD_MASK_ALT;
4796 if (code & 4)
4797 modifiers |= MOD_MASK_CTRL;
4798 if (code & 8)
4799 modifiers |= MOD_MASK_META;
Bram Moolenaar064fd672022-11-29 18:32:32 +00004800 // Any further modifiers are silently dropped.
4801
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004802 return modifiers;
4803}
4804
4805 static int
4806modifiers2keycode(int modifiers, int *key, char_u *string)
4807{
4808 int new_slen = 0;
4809
4810 if (modifiers != 0)
4811 {
4812 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004813 // make mappings work. This may result in a special key, such as
4814 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004815 *key = simplify_key(*key, &modifiers);
4816 if (modifiers != 0)
4817 {
4818 string[new_slen++] = K_SPECIAL;
4819 string[new_slen++] = (int)KS_MODIFIER;
4820 string[new_slen++] = modifiers;
4821 }
4822 }
4823 return new_slen;
4824}
4825
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004826/*
4827 * Handle a cursor position report.
4828 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004829 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004830handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004831{
4832 if (arg[0] == 2 && arg[1] >= 2)
4833 {
4834 char *aw = NULL;
4835
4836 LOG_TR(("Received U7 status: %s", tp));
4837 u7_status.tr_progress = STATUS_GOT;
4838 did_cursorhold = TRUE;
4839 if (arg[1] == 2)
4840 aw = "single";
4841 else if (arg[1] == 3)
4842 aw = "double";
4843 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4844 {
4845 // Setting the option causes a screen redraw. Do
4846 // that right away if possible, keeping any
4847 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004848 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004849#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004850 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004851 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004852
4853 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4854 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004855#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004856 redraw_asap(UPD_CLEAR);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004857#endif
4858#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004859 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004860#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004861 }
4862 }
4863 else if (arg[0] == 3)
4864 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004865 int value;
4866
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004867 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004868 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004869
4870 // Third row: xterm compatibility test.
4871 // If the cursor is on the first column then the terminal can handle
4872 // the request for cursor style and blinking.
4873 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4874 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4875 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004876 }
4877}
4878
4879/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004880 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004881 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004882 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004883 */
4884 static void
4885handle_version_response(int first, int *arg, int argc, char_u *tp)
4886{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004887 // The xterm version. It is set to zero when it can't be an actual xterm
4888 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004889 int version = arg[1];
4890
4891 LOG_TR(("Received CRV response: %s", tp));
4892 crv_status.tr_progress = STATUS_GOT;
4893 did_cursorhold = TRUE;
4894
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004895 // Reset terminal properties that are set based on the termresponse.
4896 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004897 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004898 init_term_props(
4899#ifdef FEAT_EVAL
4900 reset_term_props_on_termresponse
4901#else
4902 FALSE
4903#endif
4904 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004905
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004906 // If this code starts with CSI, you can bet that the
4907 // terminal uses 8-bit codes.
4908 if (tp[0] == CSI)
4909 switch_to_8bit();
4910
4911 // Screen sends 40500.
4912 // rxvt sends its version number: "20703" is 2.7.3.
4913 // Ignore it for when the user has set 'term' to xterm,
4914 // even though it's an rxvt.
4915 if (version > 20000)
4916 version = 0;
4917
zeertzjqfc78a032022-04-26 22:11:38 +01004918 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004919 if (first == '>' && argc == 3)
4920 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004921 // mintty 2.9.5 sends 77;20905;0c.
4922 // (77 is ASCII 'M' for mintty.)
4923 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004924 {
4925 // mintty can do SGR mouse reporting
4926 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4927 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004928
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004929#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004930 // If xterm version >= 141 try to get termcap codes. For other
4931 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004932 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004933 {
4934 LOG_TR(("Enable checking for XT codes"));
4935 check_for_codes = TRUE;
4936 need_gather = TRUE;
4937 req_codes_from_term();
4938 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004939#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004940
4941 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01004942 // Konsole sends 0;115;0 and works the same way
4943 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004944 {
4945 // If run from Vim $COLORS is set to the number of
4946 // colors the terminal supports. Otherwise assume
4947 // 256, libvterm supports even more.
4948 if (mch_getenv((char_u *)"COLORS") == NULL)
4949 may_adjust_color_count(256);
4950 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004951 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004952 }
4953
4954 if (version == 95)
4955 {
4956 // Mac Terminal.app sends 1;95;0
4957 if (arg[0] == 1 && arg[2] == 0)
4958 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004959 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4960 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004961 }
4962 // iTerm2 sends 0;95;0
4963 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004964 {
4965 // iTerm2 can do SGR mouse reporting
4966 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4967 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004968 // old iTerm2 sends 0;95;
4969 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004970 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004971 }
4972
4973 // screen sends 83;40500;0 83 is 'S' in ASCII.
4974 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004975 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004976 // screen supports SGR mouse codes since 4.7.0
4977 if (arg[1] >= 40700)
4978 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4979 else
4980 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4981 }
4982
4983 // If no recognized terminal has set mouse behavior, assume xterm.
4984 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4985 {
4986 // Xterm version 277 supports SGR.
4987 // Xterm version >= 95 supports mouse dragging.
4988 if (version >= 277)
4989 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004990 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004991 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004992 }
4993
4994 // Detect terminals that set $TERM to something like
4995 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004996 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004997 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004998 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004999 // xfce4-terminal sends 1;2802;0.
5000 // screen sends 83;40500;0
5001 // Assuming any version number over 2500 is not an
5002 // xterm (without the limit for rxvt and screen).
5003 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005004 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005005
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005006 else if (version == 136 && arg[2] == 0)
5007 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005008 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005009
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005010 // PuTTY sends 0;136;0
5011 if (arg[0] == 0)
5012 {
5013 // supports sgr-like mouse reporting.
5014 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5015 }
5016 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005017 }
5018
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005019 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
5020 // commented out.
5021 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
5022 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005023
Bram Moolenaar1573e732022-11-16 20:33:21 +00005024 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005025 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
5026 {
5027 term_props[TPR_KITTY].tpr_status = TPR_YES;
5028 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
Bram Moolenaar731d0072022-12-18 17:47:18 +00005029
5030 // Kitty can handle SGR mouse reporting.
5031 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005032 }
5033
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005034 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005035 // 30600/40500 is a version number of GNU screen. DA2 support is added
5036 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
5037 // compatibility checking does not detect GNU screen.
5038 if (arg[0] == 83 && arg[1] >= 30600)
5039 {
5040 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5041 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
5042 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005043
5044 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005045 // 95, so assume anything below 95 is not xterm and hopefully supports
5046 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005047 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005048 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005049
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005050 // Getting the cursor style is only supported properly by xterm since
5051 // version 279 (otherwise it returns 0x18).
5052 if (version < 279)
5053 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5054
5055 /*
5056 * Take action on the detected properties.
5057 */
5058
5059 // Unless the underline RGB color is expected to work, disable "t_8u".
5060 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005061 // This may cause some flicker. Alternative would be to set "t_8u"
5062 // here if the terminal is expected to support it, but that might
5063 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01005064 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
5065 && *T_8U != NUL
5066 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005067 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02005068 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
5069 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005070 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005071#ifdef FEAT_TERMRESPONSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01005072 if (*T_8U != NUL && write_t_8u_state == MAYBE)
5073 // Did skip writing t_8u, a complete redraw is needed.
5074 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01005075 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005076#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005077
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005078 // Only set 'ttymouse' automatically if it was not set
5079 // by the user already.
5080 if (!option_was_set((char_u *)"ttym")
5081 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
5082 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
5083 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005084 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005085 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
5086 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
5087 }
5088
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005089#ifdef FEAT_TERMRESPONSE
5090 int need_flush = FALSE;
5091
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005092 // Only request the cursor style if t_SH and t_RS are
5093 // set. Only supported properly by xterm since version
5094 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005095 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005096 // Not for Terminal.app, it can't handle t_RS, it
5097 // echoes the characters to the screen.
5098 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005099 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005100 && *T_CSH != NUL
5101 && *T_CRS != NUL)
5102 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005103 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005104 LOG_TR(("Sending cursor style request"));
5105 out_str(T_CRS);
5106 termrequest_sent(&rcs_status);
5107 need_flush = TRUE;
5108 }
5109
5110 // Only request the cursor blink mode if t_RC set. Not
5111 // for Gnome terminal, it can't handle t_RC, it
5112 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005113 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005114 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005115 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005116 && *T_CRC != NUL)
5117 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005118 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005119 LOG_TR(("Sending cursor blink mode request"));
5120 out_str(T_CRC);
5121 termrequest_sent(&rbm_status);
5122 need_flush = TRUE;
5123 }
5124
5125 if (need_flush)
5126 out_flush();
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005127#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005128 }
5129}
5130
5131/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005132 * Add "key" to "buf" and return the number of bytes used.
5133 * Handles special keys and multi-byte characters.
5134 */
5135 static int
5136add_key_to_buf(int key, char_u *buf)
5137{
5138 int idx = 0;
5139
5140 if (IS_SPECIAL(key))
5141 {
5142 buf[idx++] = K_SPECIAL;
5143 buf[idx++] = KEY2TERMCAP0(key);
5144 buf[idx++] = KEY2TERMCAP1(key);
5145 }
5146 else if (has_mbyte)
5147 idx += (*mb_char2bytes)(key, buf + idx);
5148 else
5149 buf[idx++] = key;
5150 return idx;
5151}
5152
5153/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005154 * Shared between handle_key_with_modifier() and handle_csi_function_key().
5155 */
5156 static int
5157put_key_modifiers_in_typebuf(
5158 int key_arg,
5159 int modifiers_arg,
5160 int csi_len,
5161 int offset,
5162 char_u *buf,
5163 int bufsize,
5164 int *buflen)
5165{
5166 int key = key_arg;
5167 int modifiers = modifiers_arg;
5168
5169 // Some keys need adjustment when the Ctrl modifier is used.
5170 key = may_adjust_key_for_ctrl(modifiers, key);
5171
5172 // May remove the shift modifier if it's already included in the key.
5173 modifiers = may_remove_shift_modifier(modifiers, key);
5174
5175 // Produce modifiers with K_SPECIAL KS_MODIFIER {mod}
5176 char_u string[MAX_KEY_CODE_LEN + 1];
5177 int new_slen = modifiers2keycode(modifiers, &key, string);
5178
5179 // Add the bytes for the key.
5180 new_slen += add_key_to_buf(key, string + new_slen);
5181
5182 string[new_slen] = NUL;
5183 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5184 buf, bufsize, buflen) == FAIL)
5185 return -1;
5186 return new_slen - csi_len + offset;
5187}
5188
5189/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005190 * Handle a sequence with key and modifier, one of:
5191 * {lead}27;{modifier};{key}~
5192 * {lead}{key};{modifier}u
5193 * Returns the difference in length.
5194 */
5195 static int
5196handle_key_with_modifier(
5197 int *arg,
5198 int trail,
5199 int csi_len,
5200 int offset,
5201 char_u *buf,
5202 int bufsize,
5203 int *buflen)
5204{
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005205 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5206 // it for terminals using the kitty keyboard protocol. Xterm sends
5207 // the form ending in "u" when the formatOtherKeys resource is set. We do
5208 // not support this.
5209 //
5210 // Do not set seenModifyOtherKeys if there was a positive response at any
5211 // time from requesting the kitty keyboard protocol state, these are not
5212 // expected to support modifyOtherKeys level 2.
5213 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005214 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5215 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005216 if (trail != 'u'
5217 && (kitty_protocol_state == KKPS_INITIAL
5218 || kitty_protocol_state == KKPS_OFF
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00005219 || kitty_protocol_state == KKPS_AFTER_T_TE)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005220 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar1a173402022-12-02 12:28:47 +00005221 {
Bram Moolenaar5390c052022-12-02 13:10:03 +00005222#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005223 ch_log(NULL, "setting seenModifyOtherKeys to TRUE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005224#endif
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005225 seenModifyOtherKeys = TRUE;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005226 }
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005227
Bram Moolenaar1a173402022-12-02 12:28:47 +00005228 int key = trail == 'u' ? arg[0] : arg[2];
5229 int modifiers = decode_modifiers(arg[1]);
5230 return put_key_modifiers_in_typebuf(key, modifiers,
5231 csi_len, offset, buf, bufsize, buflen);
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005232}
5233
5234/*
5235 * Handle a sequence with key without a modifier:
5236 * {lead}{key}u
5237 * Returns the difference in length.
5238 */
5239 static int
5240handle_key_without_modifier(
5241 int *arg,
5242 int csi_len,
5243 int offset,
5244 char_u *buf,
5245 int bufsize,
5246 int *buflen)
5247{
5248 char_u string[MAX_KEY_CODE_LEN + 1];
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00005249 int new_slen;
5250
5251 if (arg[0] == ESC)
5252 {
5253 // Putting Esc in the buffer creates ambiguity, it can be the start of
5254 // an escape sequence. Use K_ESC to avoid that.
5255 string[0] = K_SPECIAL;
5256 string[1] = KS_EXTRA;
5257 string[2] = KE_ESC;
5258 new_slen = 3;
5259 }
5260 else
5261 new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005262
5263 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5264 buf, bufsize, buflen) == FAIL)
5265 return -1;
5266 return new_slen - csi_len + offset;
5267}
5268
5269/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005270 * CSI function key without or with modifiers:
5271 * {lead}[ABCDEFHPQRS]
5272 * {lead}1;{modifier}[ABCDEFHPQRS]
5273 * Returns zero when nog recognized, a positive number when recognized.
5274 */
5275 static int
5276handle_csi_function_key(
5277 int argc,
5278 int *arg,
5279 int trail,
5280 int csi_len,
5281 char_u *key_name,
5282 int offset,
5283 char_u *buf,
5284 int bufsize,
5285 int *buflen)
5286{
5287 key_name[0] = 'k';
5288 switch (trail)
5289 {
5290 case 'A': key_name[1] = 'u'; break; // K_UP
5291 case 'B': key_name[1] = 'd'; break; // K_DOWN
5292 case 'C': key_name[1] = 'r'; break; // K_RIGHT
5293 case 'D': key_name[1] = 'l'; break; // K_LEFT
5294
5295 // case 'E': keypad BEGIN - not supported
5296 case 'F': key_name[0] = '@'; key_name[1] = '7'; break; // K_END
5297 case 'H': key_name[1] = 'h'; break; // K_HOME
5298
5299 case 'P': key_name[1] = '1'; break; // K_F1
5300 case 'Q': key_name[1] = '2'; break; // K_F2
5301 case 'R': key_name[1] = '3'; break; // K_F3
5302 case 'S': key_name[1] = '4'; break; // K_F4
5303
5304 default: return 0; // not recognized
5305 }
5306
5307 int key = TERMCAP2KEY(key_name[0], key_name[1]);
5308 int modifiers = argc == 2 ? decode_modifiers(arg[1]) : 0;
5309 put_key_modifiers_in_typebuf(key, modifiers,
5310 csi_len, offset, buf, bufsize, buflen);
5311 return csi_len;
5312}
5313
5314/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005315 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005316 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005317 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005318 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5319 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005320 * - Cursor position report: {lead}{row};{col}R
5321 * The final byte must be 'R'. It is used for checking the
5322 * ambiguous-width character state.
5323 *
5324 * - window position reply: {lead}3;{x};{y}t
5325 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005326 * - key with modifiers when modifyOtherKeys is enabled or the Kitty keyboard
5327 * protocol is used:
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005328 * {lead}27;{modifier};{key}~
5329 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005330 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005331 * - function key with or without modifiers:
5332 * {lead}[ABCDEFHPQRS]
5333 * {lead}1;{modifier}[ABCDEFHPQRS]
5334 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005335 * Return 0 for no match, -1 for partial match, > 0 for full match.
5336 */
5337 static int
5338handle_csi(
5339 char_u *tp,
5340 int len,
5341 char_u *argp,
5342 int offset,
5343 char_u *buf,
5344 int bufsize,
5345 int *buflen,
5346 char_u *key_name,
5347 int *slen)
5348{
5349 int first = -1; // optional char right after {lead}
5350 int trail; // char that ends CSI sequence
5351 int arg[3] = {-1, -1, -1}; // argument numbers
Bram Moolenaar1a173402022-12-02 12:28:47 +00005352 int argc = 0; // number of arguments
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005353 char_u *ap = argp;
5354 int csi_len;
5355
5356 // Check for non-digit after CSI.
5357 if (!VIM_ISDIGIT(*ap))
5358 first = *ap++;
5359
Bram Moolenaar8ffb7e02022-12-03 10:13:30 +00005360 if (first >= 'A' && first <= 'Z')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005361 {
Bram Moolenaar1a173402022-12-02 12:28:47 +00005362 // If "first" is in [ABCDEFHPQRS] then it is actually the "trail" and
5363 // no argument follows.
5364 trail = first;
5365 first = -1;
5366 --ap;
5367 }
5368 else
5369 {
5370 // Find up to three argument numbers.
5371 for (argc = 0; argc < 3; )
5372 {
5373 if (ap >= tp + len)
5374 return -1;
5375 if (*ap == ';')
5376 arg[argc++] = -1; // omitted number
5377 else if (VIM_ISDIGIT(*ap))
5378 {
5379 arg[argc] = 0;
5380 for (;;)
5381 {
5382 if (ap >= tp + len)
5383 return -1;
5384 if (!VIM_ISDIGIT(*ap))
5385 break;
5386 arg[argc] = arg[argc] * 10 + (*ap - '0');
5387 ++ap;
5388 }
5389 ++argc;
5390 }
5391 if (*ap == ';')
5392 ++ap;
5393 else
5394 break;
5395 }
5396
5397 // mrxvt has been reported to have "+" in the version. Assume
5398 // the escape sequence ends with a letter or one of "{|}~".
5399 while (ap < tp + len
5400 && !(*ap >= '{' && *ap <= '~')
5401 && !ASCII_ISALPHA(*ap))
5402 ++ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005403 if (ap >= tp + len)
5404 return -1;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005405 trail = *ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005406 }
5407
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005408 csi_len = (int)(ap - tp) + 1;
5409
Bram Moolenaarc255b782022-11-26 19:16:48 +00005410 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5411 // modifyOtherKeys level. Drop similar responses.
5412 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5413 {
5414 if (arg[0] == 4 && argc == 2)
5415 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5416
5417 key_name[0] = (int)KS_EXTRA;
5418 key_name[1] = (int)KE_IGNORE;
5419 *slen = csi_len;
5420 }
5421
Bram Moolenaar1a173402022-12-02 12:28:47 +00005422 // Function key starting with CSI:
5423 // {lead}[ABCDEFHPQRS]
5424 // {lead}1;{modifier}[ABCDEFHPQRS]
5425 else if (first == -1 && ASCII_ISUPPER(trail)
5426 && (argc == 0 || (argc == 2 && arg[0] == 1)))
5427 {
5428 int res = handle_csi_function_key(argc, arg, trail,
5429 csi_len, key_name, offset, buf, bufsize, buflen);
5430 return res <= 0 ? res : len + res;
5431 }
5432
5433 // Cursor position report: {lead}{row};{col}R
5434 // Eat it when there are 2 arguments and it ends in 'R'.
5435 // Also when u7_status is not "sent", it may be from a previous Vim that
5436 // just exited. But not for <S-F3>, it sends something similar, check for
5437 // row and column to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005438 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005439 {
5440 handle_u7_response(arg, tp, csi_len);
5441
5442 key_name[0] = (int)KS_EXTRA;
5443 key_name[1] = (int)KE_IGNORE;
5444 *slen = csi_len;
5445 }
5446
5447 // Version string: Eat it when there is at least one digit and
5448 // it ends in 'c'
5449 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5450 {
5451 handle_version_response(first, arg, argc, tp);
5452
5453 *slen = csi_len;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005454#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005455 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005456#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005457 apply_autocmds(EVENT_TERMRESPONSE,
5458 NULL, NULL, FALSE, curbuf);
5459 key_name[0] = (int)KS_EXTRA;
5460 key_name[1] = (int)KE_IGNORE;
5461 }
5462
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005463#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005464 // Check blinking cursor from xterm:
5465 // {lead}?12;1$y set
5466 // {lead}?12;2$y not set
5467 //
5468 // {lead} can be <Esc>[ or CSI
5469 else if (rbm_status.tr_progress == STATUS_SENT
5470 && first == '?'
5471 && ap == argp + 6
5472 && arg[0] == 12
5473 && ap[-1] == '$'
5474 && trail == 'y')
5475 {
5476 initial_cursor_blink = (arg[1] == '1');
5477 rbm_status.tr_progress = STATUS_GOT;
5478 LOG_TR(("Received cursor blinking mode response: %s", tp));
5479 key_name[0] = (int)KS_EXTRA;
5480 key_name[1] = (int)KE_IGNORE;
5481 *slen = csi_len;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005482# ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005483 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005484# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005485 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005486#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005487
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005488 // Kitty keyboard protocol status response: CSI ? flags u
5489 else if (first == '?' && argc == 1 && trail == 'u')
5490 {
5491 // The protocol has various "progressive enhancement flags" values, but
5492 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005493 if (arg[0] == '0')
5494 {
5495 kitty_protocol_state = KKPS_OFF;
5496 }
5497 else
5498 {
5499 kitty_protocol_state = KKPS_ENABLED;
5500
5501 // Reset seenModifyOtherKeys just in case some key combination has
5502 // been seen that set it before we get the status response.
Bram Moolenaar5390c052022-12-02 13:10:03 +00005503#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005504 ch_log(NULL, "setting seenModifyOtherKeys to FALSE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005505#endif
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005506 seenModifyOtherKeys = FALSE;
5507 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005508
5509 key_name[0] = (int)KS_EXTRA;
5510 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005511 *slen = csi_len;
5512 }
5513
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005514#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005515 // Check for a window position response from the terminal:
5516 // {lead}3;{x};{y}t
5517 else if (did_request_winpos && argc == 3 && arg[0] == 3
5518 && trail == 't')
5519 {
5520 winpos_x = arg[1];
5521 winpos_y = arg[2];
5522 // got finished code: consume it
5523 key_name[0] = (int)KS_EXTRA;
5524 key_name[1] = (int)KE_IGNORE;
5525 *slen = csi_len;
5526
5527 if (--did_request_winpos <= 0)
5528 winpos_status.tr_progress = STATUS_GOT;
5529 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005530#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005531
5532 // Key with modifier:
5533 // {lead}27;{modifier};{key}~
5534 // {lead}{key};{modifier}u
Bram Moolenaar064fd672022-11-29 18:32:32 +00005535 // Even though we only handle four modifiers and the {modifier} value
5536 // should be 16 or lower, we accept all modifier values to avoid the raw
5537 // sequence to be passed through.
5538 else if ((arg[0] == 27 && argc == 3 && trail == '~')
Bram Moolenaar7609c882022-10-19 20:07:09 +01005539 || (argc == 2 && trail == 'u'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005540 {
5541 return len + handle_key_with_modifier(arg, trail,
Bram Moolenaar064fd672022-11-29 18:32:32 +00005542 csi_len, offset, buf, bufsize, buflen);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005543 }
5544
Christopher Plewright03193062022-11-22 12:58:27 +00005545 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005546 // {lead}{key}u
5547 else if (argc == 1 && trail == 'u')
5548 {
5549 return len + handle_key_without_modifier(arg,
5550 csi_len, offset, buf, bufsize, buflen);
5551 }
5552
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005553 // else: Unknown CSI sequence. We could drop it, but then the
5554 // user can't create a map for it.
5555 return 0;
5556}
5557
5558/*
5559 * Handle an OSC sequence, fore/background color response from the terminal:
5560 *
5561 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5562 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5563 *
5564 * {code} is 10 for foreground, 11 for background
5565 * {lead} can be <Esc>] or OSC
5566 * {tail} can be '\007', <Esc>\ or STERM.
5567 *
5568 * Consume any code that starts with "{lead}11;", it's also
5569 * possible that "rgba" is following.
5570 */
5571 static int
5572handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5573{
5574 int i, j;
5575
5576 j = 1 + (tp[0] == ESC);
5577 if (len >= j + 3 && (argp[0] != '1'
5578 || (argp[1] != '1' && argp[1] != '0')
5579 || argp[2] != ';'))
5580 i = 0; // no match
5581 else
5582 for (i = j; i < len; ++i)
5583 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5584 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5585 {
5586 int is_bg = argp[1] == '1';
5587 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5588 && tp[j + 16] == '/';
5589
5590 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5591 && (is_4digit
5592 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5593 {
5594 char_u *tp_r = tp + j + 7;
5595 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5596 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005597#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005598 int rval, gval, bval;
5599
5600 rval = hexhex2nr(tp_r);
5601 gval = hexhex2nr(tp_b);
5602 bval = hexhex2nr(tp_g);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005603#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005604 if (is_bg)
5605 {
5606 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5607 *tp_b) ? "light" : "dark";
5608
5609 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005610#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005611 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005612# ifdef FEAT_TERMINAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005613 bg_r = rval;
5614 bg_g = gval;
5615 bg_b = bval;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005616# endif
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005617#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005618 if (!option_was_set((char_u *)"bg")
5619 && STRCMP(p_bg, new_bg_val) != 0)
5620 {
5621 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005622 set_option_value_give_err((char_u *)"bg",
5623 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005624 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005625 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005626 }
5627 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005628#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005629 else
5630 {
5631 LOG_TR(("Received RFG response: %s", tp));
5632 rfg_status.tr_progress = STATUS_GOT;
5633 fg_r = rval;
5634 fg_g = gval;
5635 fg_b = bval;
5636 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005637#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005638 }
5639
5640 // got finished code: consume it
5641 key_name[0] = (int)KS_EXTRA;
5642 key_name[1] = (int)KE_IGNORE;
5643 *slen = i + 1 + (tp[i] == ESC);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005644#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005645 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5646 : VV_TERMRFGRESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005647#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005648 break;
5649 }
5650 if (i == len)
5651 {
5652 LOG_TR(("not enough characters for RB"));
5653 return FAIL;
5654 }
5655 return OK;
5656}
5657
5658/*
5659 * Check for key code response from xterm:
5660 * {lead}{flag}+r<hex bytes><{tail}
5661 *
5662 * {lead} can be <Esc>P or DCS
5663 * {flag} can be '0' or '1'
5664 * {tail} can be Esc>\ or STERM
5665 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005666 * Check for resource response from xterm (and drop it):
5667 * {lead}{flag}+R<hex bytes>=<value>{tail}
5668 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005669 * Check for cursor shape response from xterm:
5670 * {lead}1$r<digit> q{tail}
5671 *
5672 * {lead} can be <Esc>P or DCS
5673 * {tail} can be <Esc>\ or STERM
5674 *
5675 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5676 */
5677 static int
5678handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5679{
5680 int i, j;
5681
5682 j = 1 + (tp[0] == ESC);
5683 if (len < j + 3)
5684 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005685 else if ((argp[1] != '+' && argp[1] != '$')
5686 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005687 i = 0; // no match
5688 else if (argp[1] == '+')
5689 // key code response
5690 for (i = j; i < len; ++i)
5691 {
5692 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5693 || tp[i] == STERM)
5694 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005695#ifdef FEAT_TERMRESPONSE
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005696 // handle a key code response, drop a resource response
5697 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005698 got_code_from_term(tp + j, i);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005699#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005700 key_name[0] = (int)KS_EXTRA;
5701 key_name[1] = (int)KE_IGNORE;
5702 *slen = i + 1 + (tp[i] == ESC);
5703 break;
5704 }
5705 }
5706 else
5707 {
5708 // Probably the cursor shape response. Make sure that "i"
5709 // is equal to "len" when there are not sufficient
5710 // characters.
5711 for (i = j + 3; i < len; ++i)
5712 {
5713 if (i - j == 3 && !isdigit(tp[i]))
5714 break;
5715 if (i - j == 4 && tp[i] != ' ')
5716 break;
5717 if (i - j == 5 && tp[i] != 'q')
5718 break;
5719 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5720 break;
5721 if ((i - j == 6 && tp[i] == STERM)
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005722 || (i - j == 7 && tp[i] == '\\'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005723 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005724#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005725 int number = argp[3] - '0';
5726
5727 // 0, 1 = block blink, 2 = block
5728 // 3 = underline blink, 4 = underline
5729 // 5 = vertical bar blink, 6 = vertical bar
5730 number = number == 0 ? 1 : number;
5731 initial_cursor_shape = (number + 1) / 2;
5732 // The blink flag is actually inverted, compared to
5733 // the value set with T_SH.
5734 initial_cursor_shape_blink =
5735 (number & 1) ? FALSE : TRUE;
5736 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005737#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005738 LOG_TR(("Received cursor shape response: %s", tp));
5739
5740 key_name[0] = (int)KS_EXTRA;
5741 key_name[1] = (int)KE_IGNORE;
5742 *slen = i + 1;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005743#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005744 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005745#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005746 break;
5747 }
5748 }
5749 }
5750
5751 if (i == len)
5752 {
5753 // These codes arrive many together, each code can be
5754 // truncated at any point.
5755 LOG_TR(("not enough characters for XT"));
5756 return FAIL;
5757 }
5758 return OK;
5759}
5760
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 * Check if typebuf.tb_buf[] contains a terminal key code.
5763 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005764 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005766 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 * With a match, the match is removed, the replacement code is inserted in
5768 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5769 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005770 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5771 * "buflen" is then the length of the string in buf[] and is updated for
5772 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 */
5774 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005775check_termcode(
5776 int max_offset,
5777 char_u *buf,
5778 int bufsize,
5779 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
5781 char_u *tp;
5782 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005783 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005784 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005786 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 int offset;
5788 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005789 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005790 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005791 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005792 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 char_u string[MAX_KEY_CODE_LEN + 1];
5794 int i, j;
5795 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797
5798 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5799
5800 /*
5801 * Speed up the checks for terminal codes by gathering all first bytes
5802 * used in termleader[]. Often this is just a single <Esc>.
5803 */
5804 if (need_gather)
5805 gather_termleader();
5806
5807 /*
5808 * Check at several positions in typebuf.tb_buf[], to catch something like
5809 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5810 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005811 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 * This is used often, KEEP IT FAST!
5813 */
5814 for (offset = 0; offset < max_offset; ++offset)
5815 {
5816 if (buf == NULL)
5817 {
5818 if (offset >= typebuf.tb_len)
5819 break;
5820 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005821 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 }
5823 else
5824 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005825 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 break;
5827 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005828 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
5830
5831 /*
5832 * Don't check characters after K_SPECIAL, those are already
5833 * translated terminal chars (avoid translating ~@^Hx).
5834 */
5835 if (*tp == K_SPECIAL)
5836 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005837 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 continue;
5839 }
5840
5841 /*
5842 * Skip this position if the character does not appear as the first
5843 * character in term_strings. This speeds up a lot, since most
5844 * termcodes start with the same character (ESC or CSI).
5845 */
5846 i = *tp;
5847 for (p = termleader; *p && *p != i; ++p)
5848 ;
5849 if (*p == NUL)
5850 continue;
5851
5852 /*
5853 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5854 * are in Insert mode.
5855 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005856 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 continue;
5858
Bram Moolenaar27efc622022-07-01 16:35:45 +01005859 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005860 key_name[0] = NUL; // no key name found yet
5861 key_name[1] = NUL; // no key name found yet
5862 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863
5864#ifdef FEAT_GUI
5865 if (gui.in_use)
5866 {
5867 /*
5868 * GUI special key codes are all of the form [CSI xx].
5869 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005870 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 {
5872 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005873 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 slen = 3;
5875 key_name[0] = tp[1];
5876 key_name[1] = tp[2];
5877 }
5878 }
5879 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005880#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00005881#ifdef MSWIN
5882 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
5883 && (tp[2] == KE_MOUSEUP
5884 || tp[2] == KE_MOUSEDOWN
5885 || tp[2] == KE_MOUSELEFT
5886 || tp[2] == KE_MOUSERIGHT))
5887 {
5888 // MS-Windows console sends mouse scroll events encoded:
5889 // - CSI
5890 // - KS_EXTRA
5891 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
5892 slen = 3;
5893 key_name[0] = tp[1];
5894 key_name[1] = tp[2];
5895 }
5896 else
5897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005899 int mouse_index_found = -1;
5900
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 for (idx = 0; idx < tc_len; ++idx)
5902 {
5903 /*
5904 * Ignore the entry if we are not at the start of
5905 * typebuf.tb_buf[]
5906 * and there are not enough characters to make a match.
5907 * But only when the 'K' flag is in 'cpoptions'.
5908 */
5909 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005910 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 if (cpo_koffset && offset && len < slen)
5912 continue;
5913 if (STRNCMP(termcodes[idx].code, tp,
5914 (size_t)(slen > len ? len : slen)) == 0)
5915 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005916 int looks_like_mouse_start = FALSE;
5917
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005918 if (len < slen) // got a partial sequence
5919 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
5921 /*
5922 * When found a keypad key, check if there is another key
5923 * that matches and use that one. This makes <Home> to be
5924 * found instead of <kHome> when they produce the same
5925 * key code.
5926 */
5927 if (termcodes[idx].name[0] == 'K'
5928 && VIM_ISDIGIT(termcodes[idx].name[1]))
5929 {
5930 for (j = idx + 1; j < tc_len; ++j)
5931 if (termcodes[j].len == slen &&
5932 STRNCMP(termcodes[idx].code,
5933 termcodes[j].code, slen) == 0)
5934 {
5935 idx = j;
5936 break;
5937 }
5938 }
5939
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005940 if (slen == 2 && len > 2
5941 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005942 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005943 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005944 // The mouse termcode "ESC [" is also the prefix of
5945 // "ESC [ I" (focus gained) and other keys. Check some
5946 // more bytes to find out.
5947 if (!isdigit(tp[2]))
5948 {
5949 // ESC [ without number following: Only use it when
5950 // there is no other match.
5951 looks_like_mouse_start = TRUE;
5952 }
5953 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5954 {
5955 char_u *nr = tp + 2;
5956 int count = 0;
5957
5958 // If a digit is following it could be a key with
5959 // modifier, e.g., ESC [ 1;2P. Can be confused
5960 // with DEC_MOUSE, which requires four numbers
5961 // following. If not then it can't be a DEC_MOUSE
5962 // code.
5963 for (;;)
5964 {
5965 ++count;
5966 (void)getdigits(&nr);
5967 if (nr >= tp + len)
5968 return -1; // partial sequence
5969 if (*nr != ';')
5970 break;
5971 ++nr;
5972 if (nr >= tp + len)
5973 return -1; // partial sequence
5974 }
5975 if (count < 4)
5976 continue; // no match
5977 }
5978 }
5979 if (looks_like_mouse_start)
5980 {
5981 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005982 if (mouse_index_found < 0)
5983 mouse_index_found = idx;
5984 }
5985 else
5986 {
5987 key_name[0] = termcodes[idx].name[0];
5988 key_name[1] = termcodes[idx].name[1];
5989 break;
5990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005992
5993 /*
5994 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005995 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005996 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005997 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5998 * When there is a modifier the * matches a number.
5999 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006000 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006001 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006002 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006003 modslen = termcodes[idx].modlen;
6004 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006005 continue;
6006 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006007 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006008 {
6009 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006010
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006011 if (len <= modslen) // got a partial sequence
6012 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006013
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006014 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006015 // no modifiers
6016 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006017 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006018 // no match for "code;*X" with "code;"
6019 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01006020 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00006021 && (tp[modslen] != '1'
6022 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00006023 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006024 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006025 else
6026 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006027 // Skip over the digits, the final char must
6028 // follow. URXVT can use a negative value, thus
6029 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006030 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006031 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006032 ;
6033 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006034 if (len < j) // got a partial sequence
6035 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006036 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006037 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006038
Bram Moolenaara529ce02017-06-22 22:37:57 +02006039 modifiers_start = tp + slen - 2;
6040
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006041 // Match! Convert modifier bits.
6042 n = atoi((char *)modifiers_start);
6043 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006044
6045 slen = j;
6046 }
6047 key_name[0] = termcodes[idx].name[0];
6048 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006049 break;
6050 }
6051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006053 if (idx == tc_len && mouse_index_found >= 0)
6054 {
6055 key_name[0] = termcodes[mouse_index_found].name[0];
6056 key_name[1] = termcodes[mouse_index_found].name[1];
6057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 }
6059
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02006060 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006061 // Mouse codes of DEC and pterm start with <ESC>[. When
6062 // detecting the start of these mouse codes they might as well be
6063 // another key code or terminal response.
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006064#ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006065 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006066#endif
6067#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006068 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006069#endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006070 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006072 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
6073
6074 /*
6075 * Check for responses from the terminal starting with {lead}:
Bram Moolenaar1a173402022-12-02 12:28:47 +00006076 * "<Esc>[" or CSI followed by [0-9>?].
6077 * Also for function keys without a modifier:
6078 * "<Esc>[" or CSI followed by [ABCDEFHPQRS].
Bram Moolenaar9584b312013-03-13 19:29:28 +01006079 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006080 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01006081 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006082 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01006083 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00006084 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
6085 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006086 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006087 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01006088 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006089 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006090 * - window position reply: {lead}3;{x};{y}t
6091 *
6092 * - key with modifiers when modifyOtherKeys is enabled:
6093 * {lead}27;{modifier};{key}~
6094 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01006095 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006096 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar1a173402022-12-02 12:28:47 +00006097 || (tp[0] == CSI && len >= 2))
6098 && vim_strchr((char_u *)"0123456789>?ABCDEFHPQRS",
6099 *argp) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006101 int resp = handle_csi(tp, len, argp, offset, buf,
6102 bufsize, buflen, key_name, &slen);
6103 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006104 {
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006105#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006106 if (resp == -1)
6107 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006108#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006109 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01006110 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006111 }
6112
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006113 // Check for fore/background color response from the terminal,
6114 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006115 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006116 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
6117 || tp[0] == OSC))
6118 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006119 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006120 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 }
6122
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006123 // Check for key code response from xterm,
6124 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00006125 // It would only be needed with this condition:
6126 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
6127 // Now this is always done so that DCS codes don't mess up things.
6128 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006130 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006131 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 }
6133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134
6135 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006136 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006138 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139
Christopher Plewright36446bb2022-11-23 22:28:08 +00006140#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00006142 * For scroll events from the GUI or MS-Windows console, fetch the
6143 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00006145 if (TRUE
6146# if defined(FEAT_GUI) && !defined(MSWIN)
6147 && gui.in_use
6148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 && key_name[0] == (int)KS_EXTRA
6150 && (key_name[1] == (int)KE_X1MOUSE
6151 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006152 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006153 || key_name[1] == (int)KE_MOUSELEFT
6154 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 || key_name[1] == (int)KE_MOUSEDOWN
6156 || key_name[1] == (int)KE_MOUSEUP))
6157 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006158 char_u bytes[6];
6159 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
6160
6161 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 return -1;
6163 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
6164 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
6165 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006166 // equal to K_MOUSEMOVE
6167 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
6168 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 }
6170 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 /*
6173 * If it is a mouse click, get the coordinates.
6174 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006175 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006176#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02006177 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006178#endif
6179#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006180 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006181#endif
6182#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006183 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006184#endif
6185#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006186 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006187#endif
6188#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006189 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006190#endif
6191#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006192 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006193#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006194 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006195 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006197 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
6198 &modifiers) == -1)
6199 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201
6202#ifdef FEAT_GUI
6203 /*
6204 * If using the GUI, then we get menu and scrollbar events.
6205 *
6206 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
6207 * four bytes which are to be taken as a pointer to the vimmenu_T
6208 * structure.
6209 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00006210 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006211 * is one byte with the tab index.
6212 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
6214 * by one byte representing the scrollbar number, and then four bytes
6215 * representing a long_u which is the new value of the scrollbar.
6216 *
6217 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
6218 * KE_FILLER followed by four bytes representing a long_u which is the
6219 * new value of the scrollbar.
6220 */
6221# ifdef FEAT_MENU
6222 else if (key_name[0] == (int)KS_MENU)
6223 {
6224 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006225 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 if (num_bytes == -1)
6228 return -1;
6229 current_menu = (vimmenu_T *)val;
6230 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006231
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006232 // The menu may have been deleted right after it was used, check
6233 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006234 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6235 {
6236 key_name[0] = KS_EXTRA;
6237 key_name[1] = (int)KE_IGNORE;
6238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 }
6240# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006241# ifdef FEAT_GUI_TABLINE
6242 else if (key_name[0] == (int)KS_TABLINE)
6243 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006244 // Selecting tabline tab or using its menu.
6245 char_u bytes[6];
6246 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6247
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006248 if (num_bytes == -1)
6249 return -1;
6250 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006251 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006252 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006253 slen += num_bytes;
6254 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006255 else if (key_name[0] == (int)KS_TABMENU)
6256 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006257 // Selecting tabline tab or using its menu.
6258 char_u bytes[6];
6259 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6260
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006261 if (num_bytes == -1)
6262 return -1;
6263 current_tab = (int)bytes[0];
6264 current_tabmenu = (int)bytes[1];
6265 slen += num_bytes;
6266 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006267# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268# ifndef USE_ON_FLY_SCROLL
6269 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6270 {
6271 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006272 char_u bytes[6];
6273 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006275 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 j = 0;
6277 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6278 && tp[j + 2] != NUL; ++i)
6279 {
6280 j += 3;
6281 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6282 if (num_bytes == -1)
6283 break;
6284 if (i == 0)
6285 current_scrollbar = (int)bytes[0];
6286 else if (current_scrollbar != (int)bytes[0])
6287 break;
6288 j += num_bytes;
6289 num_bytes = get_long_from_buf(tp + j, &val);
6290 if (num_bytes == -1)
6291 break;
6292 scrollbar_value = val;
6293 j += num_bytes;
6294 slen = j;
6295 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006296 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 return -1;
6298 }
6299 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6300 {
6301 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006302 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006304 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 j = 0;
6306 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6307 && tp[j + 2] != NUL; ++i)
6308 {
6309 j += 3;
6310 num_bytes = get_long_from_buf(tp + j, &val);
6311 if (num_bytes == -1)
6312 break;
6313 scrollbar_value = val;
6314 j += num_bytes;
6315 slen = j;
6316 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006317 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 return -1;
6319 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006320# endif // !USE_ON_FLY_SCROLL
6321#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006323#if (defined(UNIX) || defined(VMS))
6324 /*
6325 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6326 * (CSI I/CSI O)
6327 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006328 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006329# ifdef FEAT_GUI
6330 && !gui.in_use
6331# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006332 )
6333 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006334 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006335 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006336 if (!focus_state)
6337 {
6338 ui_focus_change(TRUE);
6339 did_cursorhold = TRUE;
6340 focus_state = TRUE;
6341 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006342 key_name[1] = (int)KE_IGNORE;
6343 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006344 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006345 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006346 if (focus_state)
6347 {
6348 ui_focus_change(FALSE);
6349 did_cursorhold = TRUE;
6350 focus_state = FALSE;
6351 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006352 key_name[1] = (int)KE_IGNORE;
6353 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006354 }
6355#endif
6356
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006357 /*
6358 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6359 */
6360 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6361
6362 /*
6363 * Add any modifier codes to our string.
6364 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006365 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006366
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006367 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006368 key_name[0] = KEY2TERMCAP0(key);
6369 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006371 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006372 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006373 if (has_mbyte)
6374 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6375 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006376 string[new_slen++] = key_name[1];
6377 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006378 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6379 && key_name[1] == KE_IGNORE)
6380 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006381 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6382 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006383 retval = KEYLEN_REMOVED;
6384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 else
6386 {
6387 string[new_slen++] = K_SPECIAL;
6388 string[new_slen++] = key_name[0];
6389 string[new_slen++] = key_name[1];
6390 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006391 if (put_string_in_typebuf(offset, slen, string, new_slen,
6392 buf, bufsize, buflen) == FAIL)
6393 return -1;
6394 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 }
6396
Bram Moolenaar2951b772013-07-03 12:45:31 +02006397#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006398 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006399#endif
6400
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006401 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402}
6403
Bram Moolenaar9377df32017-10-15 13:22:01 +02006404#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006405/*
6406 * Get the text foreground color, if known.
6407 */
6408 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006409term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006410{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006411 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006412 {
6413 *r = fg_r;
6414 *g = fg_g;
6415 *b = fg_b;
6416 }
6417}
6418
6419/*
6420 * Get the text background color, if known.
6421 */
6422 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006423term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006424{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006425 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006426 {
6427 *r = bg_r;
6428 *g = bg_g;
6429 *b = bg_b;
6430 }
6431}
6432#endif
6433
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434/*
6435 * Replace any terminal code strings in from[] with the equivalent internal
6436 * vim representation. This is used for the "from" and "to" part of a
6437 * mapping, and the "to" part of a menu command.
6438 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6439 * '<'.
6440 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6441 *
6442 * The replacement is done in result[] and finally copied into allocated
6443 * memory. If this all works well *bufp is set to the allocated memory and a
6444 * pointer to it is returned. If something fails *bufp is set to NULL and from
6445 * is returned.
6446 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006447 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6448 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6449 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6450 * used instead of a CTRL-V.
6451 *
6452 * Flags:
6453 * REPTERM_FROM_PART see above
6454 * REPTERM_DO_LT also translate <lt>
6455 * REPTERM_SPECIAL always accept <key> notation
6456 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6457 *
6458 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6459 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006461 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006462replace_termcodes(
6463 char_u *from,
6464 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02006465 int flags,
6466 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467{
6468 int i;
6469 int slen;
6470 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006471 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006473 int do_backslash; // backslash is a special character
6474 int do_special; // recognize <> key codes
6475 int do_key_code; // recognize raw key codes
6476 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006477 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478
6479 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006480 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6481 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006483 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
6485 /*
6486 * Allocate space for the translation. Worst case a single character is
6487 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006488 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006490 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006491 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 {
6493 *bufp = NULL;
6494 return from;
6495 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006496 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497
6498 /*
6499 * Check for #n at start only: function key n
6500 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006501 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 {
6503 result[dlen++] = K_SPECIAL;
6504 result[dlen++] = 'k';
6505 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006506 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006508 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 src += 2;
6510 }
6511
6512 /*
6513 * Copy each byte from *from to result[dlen]
6514 */
6515 while (*src != NUL)
6516 {
6517 /*
6518 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006519 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006521 if (do_special && ((flags & REPTERM_DO_LT)
6522 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 {
6524#ifdef FEAT_EVAL
6525 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006526 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6527 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006529 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6530 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 */
6532 if (STRNICMP(src, "<SID>", 5) == 0)
6533 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006534 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006535 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 else
6537 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006538 char_u *dot;
6539 long sid = current_sctx.sc_sid;
6540
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006542 if (in_vim9script()
6543 && (dot = vim_strchr(src, '.')) != NULL)
6544 {
6545 imported_T *imp = find_imported(src, dot - src, FALSE);
6546
6547 if (imp != NULL)
6548 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006549 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6550 size_t len;
6551
Bram Moolenaar89445512022-04-14 12:58:23 +01006552 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006553 if (si->sn_autoload_prefix != NULL)
6554 {
6555 // Turn "<SID>name.Func"
6556 // into "scriptname#Func".
6557 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006558 if (ga_grow(&ga,
6559 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006560 {
6561 ga_clear(&ga);
6562 *bufp = NULL;
6563 return from;
6564 }
6565 result = ga.ga_data;
6566 STRCPY(result + dlen, si->sn_autoload_prefix);
6567 dlen += len;
6568 continue;
6569 }
6570 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006571 }
6572 }
6573
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 result[dlen++] = K_SPECIAL;
6575 result[dlen++] = (int)KS_EXTRA;
6576 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006577 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006578 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 result[dlen++] = '_';
6580 continue;
6581 }
6582 }
6583#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006584 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6585 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006586 TRUE, did_simplify);
Bram Moolenaar1a173402022-12-02 12:28:47 +00006587 if (slen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 {
6589 dlen += slen;
6590 continue;
6591 }
6592 }
6593
6594 /*
6595 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6596 * Note that this is also checked after replacing the <> form.
6597 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6598 * it could be a character in the file.
6599 */
6600 if (do_key_code)
6601 {
6602 i = find_term_bykeys(src);
6603 if (i >= 0)
6604 {
6605 result[dlen++] = K_SPECIAL;
6606 result[dlen++] = termcodes[i].name[0];
6607 result[dlen++] = termcodes[i].name[1];
6608 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006609 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 continue;
6611 }
6612 }
6613
6614#ifdef FEAT_EVAL
6615 if (do_special)
6616 {
6617 char_u *p, *s, len;
6618
6619 /*
6620 * Replace <Leader> by the value of "mapleader".
6621 * Replace <LocalLeader> by the value of "maplocalleader".
6622 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6623 */
6624 if (STRNICMP(src, "<Leader>", 8) == 0)
6625 {
6626 len = 8;
6627 p = get_var_value((char_u *)"g:mapleader");
6628 }
6629 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6630 {
6631 len = 13;
6632 p = get_var_value((char_u *)"g:maplocalleader");
6633 }
6634 else
6635 {
6636 len = 0;
6637 p = NULL;
6638 }
6639 if (len != 0)
6640 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006641 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6643 s = (char_u *)"\\";
6644 else
6645 s = p;
6646 while (*s != NUL)
6647 result[dlen++] = *s++;
6648 src += len;
6649 continue;
6650 }
6651 }
6652#endif
6653
6654 /*
6655 * Remove CTRL-V and ignore the next character.
6656 * For "from" side the CTRL-V at the end is included, for the "to"
6657 * part it is removed.
6658 * If 'cpoptions' does not contain 'B', also accept a backslash.
6659 */
6660 key = *src;
6661 if (key == Ctrl_V || (do_backslash && key == '\\'))
6662 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006663 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 if (*src == NUL)
6665 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006666 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 result[dlen++] = key;
6668 break;
6669 }
6670 }
6671
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006672 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006673 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 {
6675 /*
6676 * If the character is K_SPECIAL, replace it with K_SPECIAL
6677 * KS_SPECIAL KE_FILLER.
6678 * If compiled with the GUI replace CSI with K_CSI.
6679 */
6680 if (*src == K_SPECIAL)
6681 {
6682 result[dlen++] = K_SPECIAL;
6683 result[dlen++] = KS_SPECIAL;
6684 result[dlen++] = KE_FILLER;
6685 }
6686# ifdef FEAT_GUI
6687 else if (*src == CSI)
6688 {
6689 result[dlen++] = K_SPECIAL;
6690 result[dlen++] = KS_EXTRA;
6691 result[dlen++] = (int)KE_CSI;
6692 }
6693# endif
6694 else
6695 result[dlen++] = *src;
6696 ++src;
6697 }
6698 }
6699 result[dlen] = NUL;
6700
6701 /*
6702 * Copy the new string to allocated memory.
6703 * If this fails, just return from.
6704 */
6705 if ((*bufp = vim_strsave(result)) != NULL)
6706 from = *bufp;
6707 vim_free(result);
6708 return from;
6709}
6710
6711/*
6712 * Find a termcode with keys 'src' (must be NUL terminated).
6713 * Return the index in termcodes[], or -1 if not found.
6714 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006715 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006716find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717{
6718 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006719 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720
6721 for (i = 0; i < tc_len; ++i)
6722 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006723 if (slen == termcodes[i].len
6724 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 return i;
6726 }
6727 return -1;
6728}
6729
6730/*
6731 * Gather the first characters in the terminal key codes into a string.
6732 * Used to speed up check_termcode().
6733 */
6734 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006735gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736{
6737 int i;
6738 int len = 0;
6739
6740#ifdef FEAT_GUI
6741 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006742 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743#endif
6744#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006745 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006746 termleader[len++] = DCS; // the termcode response starts with DCS
6747 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748#endif
6749 termleader[len] = NUL;
6750
6751 for (i = 0; i < tc_len; ++i)
6752 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6753 {
6754 termleader[len++] = termcodes[i].code[0];
6755 termleader[len] = NUL;
6756 }
6757
6758 need_gather = FALSE;
6759}
6760
6761/*
6762 * Show all termcodes (for ":set termcap")
6763 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006764 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 */
6766 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006767show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
6769 int col;
6770 int *items;
6771 int item_count;
6772 int run;
6773 int row, rows;
6774 int cols;
6775 int i;
6776 int len;
6777
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006778#define INC3 27 // try to make three columns
6779#define INC2 40 // try to make two columns
6780#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006782 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006784 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 if (items == NULL)
6786 return;
6787
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006788 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006789 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790
6791 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006792 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006794 * 2. display the medium items (medium length strings)
6795 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006796 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006798 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {
6800 /*
6801 * collect the items in items[]
6802 */
6803 item_count = 0;
6804 for (i = 0; i < tc_len; i++)
6805 {
6806 len = show_one_termcode(termcodes[i].name,
6807 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006808 if ((flags & OPT_ONECOLUMN) ||
6809 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006810 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006811 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 items[item_count++] = i;
6813 }
6814
6815 /*
6816 * display the items
6817 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006818 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006820 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 if (cols == 0)
6822 cols = 1;
6823 rows = (item_count + cols - 1) / cols;
6824 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006825 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 rows = item_count;
6827 for (row = 0; row < rows && !got_int; ++row)
6828 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006829 msg_putchar('\n'); // go to next line
6830 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 break;
6832 col = 0;
6833 for (i = row; i < item_count; i += rows)
6834 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006835 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 show_one_termcode(termcodes[items[i]].name,
6837 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006838 if (run == 2)
6839 col += INC2;
6840 else
6841 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 }
6843 out_flush();
6844 ui_breakcheck();
6845 }
6846 }
6847 vim_free(items);
6848}
6849
6850/*
6851 * Show one termcode entry.
6852 * Output goes into IObuff[]
6853 */
6854 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006855show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856{
6857 char_u *p;
6858 int len;
6859
6860 if (name[0] > '~')
6861 {
6862 IObuff[0] = ' ';
6863 IObuff[1] = ' ';
6864 IObuff[2] = ' ';
6865 IObuff[3] = ' ';
6866 }
6867 else
6868 {
6869 IObuff[0] = 't';
6870 IObuff[1] = '_';
6871 IObuff[2] = name[0];
6872 IObuff[3] = name[1];
6873 }
6874 IObuff[4] = ' ';
6875
6876 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6877 if (p[1] != 't')
6878 STRCPY(IObuff + 5, p);
6879 else
6880 IObuff[5] = NUL;
6881 len = (int)STRLEN(IObuff);
6882 do
6883 IObuff[len++] = ' ';
6884 while (len < 17);
6885 IObuff[len] = NUL;
6886 if (code == NULL)
6887 len += 4;
6888 else
6889 len += vim_strsize(code);
6890
6891 if (printit)
6892 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006893 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006895 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 else
6897 msg_outtrans(code);
6898 }
6899 return len;
6900}
6901
6902#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6903/*
6904 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6905 * termcap codes from the terminal itself.
6906 * We get them one by one to avoid a very long response string.
6907 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006908static int xt_index_in = 0;
6909static int xt_index_out = 0;
6910
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006912req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
6914 xt_index_out = 0;
6915 xt_index_in = 0;
6916 req_more_codes_from_term();
6917}
6918
6919 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006920req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006922 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 int old_idx = xt_index_out;
6924
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006925 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 if (exiting)
6927 return;
6928
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006929 // Send up to 10 more requests out than we received. Avoid sending too
6930 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6932 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006933 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006934
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006935 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006936 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6937 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 out_str_nf((char_u *)buf);
6939 ++xt_index_out;
6940 }
6941
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006942 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 if (xt_index_out != old_idx)
6944 out_flush();
6945}
6946
6947/*
6948 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6949 * A "0" instead of the "1" indicates a code that isn't supported.
6950 * Both <name> and <string> are encoded in hex.
6951 * "code" points to the "0" or "1".
6952 */
6953 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006954got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955{
6956#define XT_LEN 100
6957 char_u name[3];
6958 char_u str[XT_LEN];
6959 int i;
6960 int j = 0;
6961 int c;
6962
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006963 // A '1' means the code is supported, a '0' means it isn't.
6964 // When half the length is > XT_LEN we can't use it.
6965 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6967 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006968 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 name[0] = hexhex2nr(code + 3);
6970 name[1] = hexhex2nr(code + 5);
6971 name[2] = NUL;
6972 for (i = 0; key_names[i] != NULL; ++i)
6973 {
6974 if (STRCMP(key_names[i], name) == 0)
6975 {
6976 xt_index_in = i;
6977 break;
6978 }
6979 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006980
Bram Moolenaarb255b902018-04-24 21:40:10 +02006981 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6982
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (key_names[i] != NULL)
6984 {
6985 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6986 str[j++] = c;
6987 str[j] = NUL;
6988 if (name[0] == 'C' && name[1] == 'o')
6989 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006990 // Color count is not a key code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00006991 int val = atoi((char *)str);
6992#if defined(FEAT_EVAL)
6993 if (val == t_colors)
6994 ch_log(NULL, "got_code_from_term(Co): no change (%d)", val);
6995 else
6996 ch_log(NULL,
6997 "got_code_from_term(Co): changed from %d to %d",
6998 t_colors, val);
6999#endif
7000 may_adjust_color_count(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 }
7002 else
7003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 i = find_term_bykeys(str);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007005 if (i >= 0 && name[0] == termcodes[i].name[0]
7006 && name[1] == termcodes[i].name[1])
7007 {
7008 // Existing entry with the same name and code - skip.
7009#ifdef FEAT_EVAL
7010 ch_log(NULL, "got_code_from_term(): Entry %c%c did not change",
7011 name[0], name[1]);
7012#endif
7013 }
7014 else
7015 {
7016 if (i >= 0)
7017 {
7018 // Delete an existing entry using the same code.
7019#ifdef FEAT_EVAL
7020 ch_log(NULL, "got_code_from_term(): Deleting entry %c%c with matching keys %s",
7021 termcodes[i].name[0], termcodes[i].name[1], str);
7022#endif
7023 del_termcode_idx(i);
7024 }
7025#ifdef FEAT_EVAL
7026 else
7027 ch_log(NULL, "got_code_from_term(): Adding entry %c%c with keys %s",
7028 name[0], name[1], str);
7029#endif
7030 add_termcode(name, str, ATC_FROM_TERM);
7031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 }
7033 }
7034 }
7035
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007036 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 ++xt_index_in;
7038 req_more_codes_from_term();
7039}
7040
7041/*
7042 * Check if there are any unanswered requests and deal with them.
7043 * This is called before starting an external program or getting direct
7044 * keyboard input. We don't want responses to be send to that program or
7045 * handled as typed text.
7046 */
7047 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007048check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049{
7050 int c;
7051
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007052 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 if (xt_index_out == 0 || xt_index_out == xt_index_in)
7054 return;
7055
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007056 // Vgetc() will check for and handle any response.
7057 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 ++no_mapping;
7059 ++allow_keys;
7060 for (;;)
7061 {
7062 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007063 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 break;
7065
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007066 // If a response is recognized it's replaced with K_IGNORE, must read
7067 // it from the input stream. If there is no K_IGNORE we can't do
7068 // anything, break here (there might be some responses further on, but
7069 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 if (c != K_SPECIAL && c != K_IGNORE)
7071 break;
7072 c = vgetc();
7073 if (c != K_IGNORE)
7074 {
7075 vungetc(c);
7076 break;
7077 }
7078 }
7079 --no_mapping;
7080 --allow_keys;
7081}
7082#endif
7083
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007084#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085static char ksme_str[20];
7086static char ksmr_str[20];
7087static char ksmd_str[20];
7088
7089/*
7090 * For Win32 console: update termcap codes for existing console attributes.
7091 */
7092 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007093update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00007095 sprintf(ksme_str, "\033|%dm", attr);
7096 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
7097 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098
Bram Moolenaar4654d632022-11-17 22:05:12 +00007099 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
7100 if (p == NULL) // did not find it
7101 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 while (p->bt_string != NULL)
7103 {
7104 if (p->bt_entry == (int)KS_ME)
7105 p->bt_string = &ksme_str[0];
7106 else if (p->bt_entry == (int)KS_MR)
7107 p->bt_string = &ksmr_str[0];
7108 else if (p->bt_entry == (int)KS_MD)
7109 p->bt_string = &ksmd_str[0];
7110 ++p;
7111 }
7112}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007113
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007114# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007115# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007116
7117typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007118{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007119 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
7120 CMODE_RGB, // Use 24bit RGB colors using VTP.
7121 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
7122 CMODE_LAST,
7123} cmode_T;
7124
7125struct ks_tbl_S
7126{
7127 int code; // value of KS_
7128 char *vtp; // code in RGB mode
7129 char *vtp2; // code in 256color mode
7130 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007131};
7132
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007133static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007134{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007135 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
7136 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
7137 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
7138 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
7139 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
7140 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
7141 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
7142 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
7143 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007144# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007145 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
7146 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
7147 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
7148 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007149# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007150 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
7151 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
7152 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
7153 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007154# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007155 {(int)KS_CCO, "256", "256", {""}}, // colors
7156 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007157};
7158
Bram Moolenaar4654d632022-11-17 22:05:12 +00007159/*
7160 * Find the first entry for "code" in the builtin termcap for "name".
7161 * Returns NULL when not found.
7162 */
7163 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007164find_first_tcap(
7165 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007166 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007167{
Bram Moolenaar4654d632022-11-17 22:05:12 +00007168 tcap_entry_T *p = find_builtin_term(name);
7169 if (p != NULL)
7170 {
7171 while (p->bt_string != NULL)
7172 {
7173 if (p->bt_entry == code)
7174 return p;
7175 ++p;
7176 }
7177 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007178 return NULL;
7179}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007180# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007181
7182/*
7183 * For Win32 console: replace the sequence immediately after termguicolors.
7184 */
7185 void
7186swap_tcap(void)
7187{
7188# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007189 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007190 static cmode_T curr_mode;
7191 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007192 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007193
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007194 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007195 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007196 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007197 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007198 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007199 if (bt != NULL)
7200 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007201 // Preserve the original value.
7202 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
7203 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
7204 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007205
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007206 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007207 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007208 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007209 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007210 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007211 }
7212
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007213 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007214 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007215 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007216 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007217 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007218 mode = CMODE_INDEXED;
7219
7220 if (mode == curr_mode)
7221 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007222
7223 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007224 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007225 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007226 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007227 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007228 }
7229
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007230 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007231# endif
7232}
7233
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02007235
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007236
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007237#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007238 || defined(PROTO)
7239static int cube_value[] = {
7240 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7241};
7242
7243static int grey_ramp[] = {
7244 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7245 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7246};
7247
LemonBoyb2b3acb2022-05-20 10:10:34 +01007248static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01007249// R G B
7250 { 0, 0, 0}, // black
7251 {224, 0, 0}, // dark red
7252 { 0, 224, 0}, // dark green
7253 {224, 224, 0}, // dark yellow / brown
7254 { 0, 0, 224}, // dark blue
7255 {224, 0, 224}, // dark magenta
7256 { 0, 224, 224}, // dark cyan
7257 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007258
LemonBoyd2a46622022-05-01 17:43:33 +01007259 {128, 128, 128}, // dark grey
7260 {255, 64, 64}, // light red
7261 { 64, 255, 64}, // light green
7262 {255, 255, 64}, // yellow
7263 { 64, 64, 255}, // light blue
7264 {255, 64, 255}, // light magenta
7265 { 64, 255, 255}, // light cyan
7266 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007267};
7268
LemonBoyd2a46622022-05-01 17:43:33 +01007269#if defined(MSWIN)
7270// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7271static const char_u cterm_ansi_idx[] = {
7272 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7273};
7274#endif
7275
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007276#define ANSI_INDEX_NONE 0
7277
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007278 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007279ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7280{
7281 if (nr < 16)
7282 {
7283 *r = ansi_table[nr][0];
7284 *g = ansi_table[nr][1];
7285 *b = ansi_table[nr][2];
7286 *ansi_idx = nr;
7287 }
7288 else
7289 {
7290 *r = 0;
7291 *g = 0;
7292 *b = 0;
7293 *ansi_idx = ANSI_INDEX_NONE;
7294 }
7295}
7296
7297 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007298cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007299{
7300 int idx;
7301
7302 if (nr < 16)
7303 {
LemonBoyd2a46622022-05-01 17:43:33 +01007304#if defined(MSWIN)
7305 idx = cterm_ansi_idx[nr];
7306#else
7307 idx = nr;
7308#endif
7309 *r = ansi_table[idx][0];
7310 *g = ansi_table[idx][1];
7311 *b = ansi_table[idx][2];
7312 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007313 }
7314 else if (nr < 232)
7315 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007316 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007317 idx = nr - 16;
7318 *r = cube_value[idx / 36 % 6];
7319 *g = cube_value[idx / 6 % 6];
7320 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007321 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007322 }
7323 else if (nr < 256)
7324 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007325 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007326 idx = nr - 232;
7327 *r = grey_ramp[idx];
7328 *g = grey_ramp[idx];
7329 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007330 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007331 }
7332 else
7333 {
7334 *r = 0;
7335 *g = 0;
7336 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007337 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007338 }
7339}
7340#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007341
Bram Moolenaarf4140482020-02-15 23:06:45 +01007342/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007343 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007344 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007345 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007346 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007347 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007348term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007349{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007350 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007351 int i;
7352 int c;
7353
7354 for (i = ta_len; i < ta_len + len; ++i)
7355 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007356 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7357 {
7358 int modifiers = ta_buf[i + 2];
7359 int key = ta_buf[i + 3];
7360
7361 // Try to use the modifier to modify the key. In any case drop the
7362 // modifier.
7363 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7364 len -= 3;
7365 if (key < 0x80)
7366 key = merge_modifyOtherKeys(key, &modifiers);
7367 ta_buf[i] = key;
7368 }
7369 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007370 {
7371 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7372 if (c == K_DEL || c == K_KDEL || c == K_BS)
7373 {
7374 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007375 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007376 if (c == K_DEL || c == K_KDEL)
7377 ta_buf[i] = DEL;
7378 else
7379 ta_buf[i] = Ctrl_H;
7380 len -= 2;
7381 }
7382 }
7383 else if (ta_buf[i] == '\r')
7384 ta_buf[i] = '\n';
7385 if (has_mbyte)
7386 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7387 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007388 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007389}