blob: f00aac9674b29ab0c1b05ddcf1e296776cfb1b27 [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 Moolenaar733a69b2022-12-01 12:03:47 +0000455 {(int)KS_CTI, "\033[>4;2m"},
456 {(int)KS_CRK, "\033[?4m"}, // see "builtin_mok2"
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000457 {(int)KS_CTE, "\033[>4;m"},
458 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000460 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000462 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200463 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000465 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
466 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
467 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000469 {(int)KS_CWS, "\033[8;%d;%dt"},
470 {(int)KS_CWP, "\033[3;%d;%dt"},
471 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000473 {(int)KS_CRV, "\033[>c"},
474 {(int)KS_RFG, "\033]10;?\007"},
475 {(int)KS_RBG, "\033]11;?\007"},
476 {(int)KS_U7, "\033[6n"},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200477# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100478 // These are printf strings, not terminal codes.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000479 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
480 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
481 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200482# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000483 {(int)KS_CAU, "\033[58;5;%dm"},
484 {(int)KS_CBE, "\033[?2004h"},
485 {(int)KS_CBD, "\033[?2004l"},
486 {(int)KS_CST, "\033[22;2t"},
487 {(int)KS_CRT, "\033[23;2t"},
488 {(int)KS_SSI, "\033[22;1t"},
489 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100490# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000491 {(int)KS_FD, "\033[?1004l"},
492 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100493# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000494
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000495 {K_UP, "\033O*A"},
496 {K_DOWN, "\033O*B"},
497 {K_RIGHT, "\033O*C"},
498 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100499 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100500 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
501 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
502 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
503 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100504 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000505 {K_XF1, "\033O*P"},
506 {K_XF2, "\033O*Q"},
507 {K_XF3, "\033O*R"},
508 {K_XF4, "\033O*S"},
509 {K_F1, "\033[11;*~"},
510 {K_F2, "\033[12;*~"},
511 {K_F3, "\033[13;*~"},
512 {K_F4, "\033[14;*~"},
513 {K_F5, "\033[15;*~"},
514 {K_F6, "\033[17;*~"},
515 {K_F7, "\033[18;*~"},
516 {K_F8, "\033[19;*~"},
517 {K_F9, "\033[20;*~"},
518 {K_F10, "\033[21;*~"},
519 {K_F11, "\033[23;*~"},
520 {K_F12, "\033[24;*~"},
521 {K_S_TAB, "\033[Z"},
522 {K_HELP, "\033[28;*~"},
523 {K_UNDO, "\033[26;*~"},
524 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100525 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000526 // {K_S_HOME, "\033O2H"},
527 // {K_C_HOME, "\033O5H"},
528 {K_KHOME, "\033[1;*~"},
529 {K_XHOME, "\033O*H"}, // other Home
530 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100531 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000532 // {K_S_END, "\033O2F"},
533 // {K_C_END, "\033O5F"},
534 {K_KEND, "\033[4;*~"},
535 {K_XEND, "\033O*F"}, // other End
536 {K_ZEND, "\033[8;*~"},
537 {K_PAGEUP, "\033[5;*~"},
538 {K_PAGEDOWN, "\033[6;*~"},
539 {K_KPLUS, "\033O*k"}, // keypad plus
540 {K_KMINUS, "\033O*m"}, // keypad minus
541 {K_KDIVIDE, "\033O*o"}, // keypad /
542 {K_KMULTIPLY, "\033O*j"}, // keypad *
543 {K_KENTER, "\033O*M"}, // keypad Enter
544 {K_KPOINT, "\033O*n"}, // keypad .
545 {K_K0, "\033O*p"}, // keypad 0
546 {K_K1, "\033O*q"}, // keypad 1
547 {K_K2, "\033O*r"}, // keypad 2
548 {K_K3, "\033O*s"}, // keypad 3
549 {K_K4, "\033O*t"}, // keypad 4
550 {K_K5, "\033O*u"}, // keypad 5
551 {K_K6, "\033O*v"}, // keypad 6
552 {K_K7, "\033O*w"}, // keypad 7
553 {K_K8, "\033O*x"}, // keypad 8
554 {K_K9, "\033O*y"}, // keypad 9
555 {K_KDEL, "\033[3;*~"}, // keypad Del
556 {K_PS, "\033[200~"}, // paste start
557 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
559 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000560 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
561 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100562 // F14 and F15 are missing, because they send the same codes as the undo
563 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000564 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
565 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
566 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
567 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
568 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000569
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000570 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
571 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
572 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
573 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
574 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
575 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
576 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
577 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
578 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
579 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000580
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000581 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
582 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
583 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
584 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
585 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
586 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
587 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588
Bram Moolenaar4654d632022-11-17 22:05:12 +0000589 {(int)KS_NAME, NULL} // end marker
590};
591
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000593 * Additions for using modifyOtherKeys level 2. Same as what is used for
594 * xterm.
595 */
596static tcap_entry_T builtin_mok2[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000597 // t_TI enables modifyOtherKeys level 2
598 {(int)KS_CTI, "\033[>4;2m"},
599
Bram Moolenaarc255b782022-11-26 19:16:48 +0000600 // XTQMODKEYS was added in xterm version 377: "CSI ? 4 m" which should
601 // return "{lead} > 4 ; Pv m". Before version 377 we expect it to have no
602 // effect.
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000603 {(int)KS_CRK, "\033[?4m"},
604
605 // t_TE disables modifyOtherKeys
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000606 {(int)KS_CTE, "\033[>4;m"},
607
608 {(int)KS_NAME, NULL} // end marker
609};
610
611/*
612 * Additions for using the Kitty keyboard protocol.
613 */
614static tcap_entry_T builtin_kitty[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000615 // t_TI enables the kitty keyboard protocol.
616 {(int)KS_CTI, "\033[=1;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000617
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000618 // t_RK requests the kitty keyboard protocol state
619 {(int)KS_CRK, "\033[?u"},
620
621 // t_TE also disables modifyOtherKeys, because t_TI from xterm may already
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000622 // have been used.
Bram Moolenaara87749e2022-11-30 10:23:17 +0000623 {(int)KS_CTE, "\033[>4;m\033[=0;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000624
625 {(int)KS_NAME, NULL} // end marker
626};
627
628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 * iris-ansi for Silicon Graphics machines.
630 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000631static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 {(int)KS_CE, "\033[K"},
633 {(int)KS_CD, "\033[J"},
634 {(int)KS_AL, "\033[L"},
635# ifdef TERMINFO
636 {(int)KS_CAL, "\033[%p1%dL"},
637# else
638 {(int)KS_CAL, "\033[%dL"},
639# endif
640 {(int)KS_DL, "\033[M"},
641# ifdef TERMINFO
642 {(int)KS_CDL, "\033[%p1%dM"},
643# else
644 {(int)KS_CDL, "\033[%dM"},
645# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100646#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647# ifdef TERMINFO
648 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
649# else
650 {(int)KS_CS, "\033[%i%d;%dr"},
651# endif
652#endif
653 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100654 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
655 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {(int)KS_TI, "\033[=6h"},
657 {(int)KS_TE, "\033[=6l"},
658 {(int)KS_SE, "\033[21;27m"},
659 {(int)KS_SO, "\033[1;7m"},
660 {(int)KS_ME, "\033[m"},
661 {(int)KS_MR, "\033[7m"},
662 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100663 {(int)KS_CCO, "8"}, // allow 8 colors
664 {(int)KS_CZH, "\033[3m"}, // italic mode on
665 {(int)KS_CZR, "\033[23m"}, // italic mode off
666 {(int)KS_US, "\033[4m"}, // underline on
667 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100669 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
670 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
671 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
672 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100674 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
675 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
676 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
677 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100679 {(int)KS_MS, "y"}, // guessed
680 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {(int)KS_LE, "\b"},
682# ifdef TERMINFO
683 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
684# else
685 {(int)KS_CM, "\033[%i%d;%dH"},
686# endif
687 {(int)KS_SR, "\033M"},
688# ifdef TERMINFO
689 {(int)KS_CRI, "\033[%p1%dC"},
690# else
691 {(int)KS_CRI, "\033[%dC"},
692# endif
693 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100694 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100696 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697# ifdef TERMINFO
698 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
699 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
700# else
701 {(int)KS_CWS, "\033[203;%d;%d/y"},
702 {(int)KS_CWP, "\033[205;%d;%d/y"},
703# endif
704 {K_UP, "\033[A"},
705 {K_DOWN, "\033[B"},
706 {K_LEFT, "\033[D"},
707 {K_RIGHT, "\033[C"},
708 {K_S_UP, "\033[161q"},
709 {K_S_DOWN, "\033[164q"},
710 {K_S_LEFT, "\033[158q"},
711 {K_S_RIGHT, "\033[167q"},
712 {K_F1, "\033[001q"},
713 {K_F2, "\033[002q"},
714 {K_F3, "\033[003q"},
715 {K_F4, "\033[004q"},
716 {K_F5, "\033[005q"},
717 {K_F6, "\033[006q"},
718 {K_F7, "\033[007q"},
719 {K_F8, "\033[008q"},
720 {K_F9, "\033[009q"},
721 {K_F10, "\033[010q"},
722 {K_F11, "\033[011q"},
723 {K_F12, "\033[012q"},
724 {K_S_F1, "\033[013q"},
725 {K_S_F2, "\033[014q"},
726 {K_S_F3, "\033[015q"},
727 {K_S_F4, "\033[016q"},
728 {K_S_F5, "\033[017q"},
729 {K_S_F6, "\033[018q"},
730 {K_S_F7, "\033[019q"},
731 {K_S_F8, "\033[020q"},
732 {K_S_F9, "\033[021q"},
733 {K_S_F10, "\033[022q"},
734 {K_S_F11, "\033[023q"},
735 {K_S_F12, "\033[024q"},
736 {K_INS, "\033[139q"},
737 {K_HOME, "\033[H"},
738 {K_END, "\033[146q"},
739 {K_PAGEUP, "\033[150q"},
740 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741
Bram Moolenaar4654d632022-11-17 22:05:12 +0000742 {(int)KS_NAME, NULL} // end marker
743};
744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000746 * These codes are valid when nansi.sys or equivalent has been installed.
747 * Function keys on a PC are preceded with a NUL. These are converted into
748 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
749 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000751static tcap_entry_T builtin_pcansi[] = {
752 {(int)KS_DL, "\033[M"},
753 {(int)KS_AL, "\033[L"},
754 {(int)KS_CE, "\033[K"},
755 {(int)KS_CL, "\033[2J"},
756 {(int)KS_ME, "\033[0m"},
757 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
758 {(int)KS_MD, "\033[1m"}, // bold: white text
759 {(int)KS_SE, "\033[0m"}, // standout end
760 {(int)KS_SO, "\033[31m"}, // standout: white on blue
761 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
762 {(int)KS_CZR, "\033[0m"}, // italic mode end
763 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
764 {(int)KS_UE, "\033[0m"}, // underscore mode end
765 {(int)KS_CCO, "8"}, // allow 8 colors
766# ifdef TERMINFO
767 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
768 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
769# else
770 {(int)KS_CAB, "\033[4%dm"}, // set background color
771 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
772# endif
773 {(int)KS_OP, "\033[0m"}, // reset colors
774 {(int)KS_MS, "y"},
775 {(int)KS_UT, "y"}, // guessed
776 {(int)KS_LE, "\b"},
777# ifdef TERMINFO
778 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
779# else
780 {(int)KS_CM, "\033[%i%d;%dH"},
781# endif
782# ifdef TERMINFO
783 {(int)KS_CRI, "\033[%p1%dC"},
784# else
785 {(int)KS_CRI, "\033[%dC"},
786# endif
787 {K_UP, "\316H"},
788 {K_DOWN, "\316P"},
789 {K_LEFT, "\316K"},
790 {K_RIGHT, "\316M"},
791 {K_S_LEFT, "\316s"},
792 {K_S_RIGHT, "\316t"},
793 {K_F1, "\316;"},
794 {K_F2, "\316<"},
795 {K_F3, "\316="},
796 {K_F4, "\316>"},
797 {K_F5, "\316?"},
798 {K_F6, "\316@"},
799 {K_F7, "\316A"},
800 {K_F8, "\316B"},
801 {K_F9, "\316C"},
802 {K_F10, "\316D"},
803 {K_F11, "\316\205"}, // guessed
804 {K_F12, "\316\206"}, // guessed
805 {K_S_F1, "\316T"},
806 {K_S_F2, "\316U"},
807 {K_S_F3, "\316V"},
808 {K_S_F4, "\316W"},
809 {K_S_F5, "\316X"},
810 {K_S_F6, "\316Y"},
811 {K_S_F7, "\316Z"},
812 {K_S_F8, "\316["},
813 {K_S_F9, "\316\\"},
814 {K_S_F10, "\316]"},
815 {K_S_F11, "\316\207"}, // guessed
816 {K_S_F12, "\316\210"}, // guessed
817 {K_INS, "\316R"},
818 {K_DEL, "\316S"},
819 {K_HOME, "\316G"},
820 {K_END, "\316O"},
821 {K_PAGEDOWN, "\316Q"},
822 {K_PAGEUP, "\316I"},
823
824 {(int)KS_NAME, NULL} // end marker
825};
826
827/*
828 * These codes are valid for the Win32 Console . The entries that start with
829 * ESC | are translated into console calls in os_win32.c. The function keys
830 * are also translated in os_win32.c.
831 */
832static tcap_entry_T builtin_win32[] = {
833 {(int)KS_CE, "\033|K"}, // clear to end of line
834 {(int)KS_AL, "\033|L"}, // add new blank line
835# ifdef TERMINFO
836 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
837# else
838 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
839# endif
840 {(int)KS_DL, "\033|M"}, // delete line
841# ifdef TERMINFO
842 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
843 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
844# else
845 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
846 {(int)KS_CSV, "\033|%d;%dV"},
847# endif
848 {(int)KS_CL, "\033|J"}, // clear screen
849 {(int)KS_CD, "\033|j"}, // clear to end of display
850 {(int)KS_VI, "\033|v"}, // cursor invisible
851 {(int)KS_VE, "\033|V"}, // cursor visible
852
853 {(int)KS_ME, "\033|0m"}, // normal
854 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
855 {(int)KS_MD, "\033|15m"}, // bold: white on black
856#if 1
857 {(int)KS_SO, "\033|31m"}, // standout: white on blue
858 {(int)KS_SE, "\033|0m"}, // standout end
859#else
860 {(int)KS_SO, "\033|F"}, // standout: high intensity
861 {(int)KS_SE, "\033|f"}, // standout end
862#endif
863 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
864 {(int)KS_CZR, "\033|0m"}, // italic end
865 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
866 {(int)KS_UE, "\033|0m"}, // underscore end
867 {(int)KS_CCO, "16"}, // allow 16 colors
868# ifdef TERMINFO
869 {(int)KS_CAB, "\033|%p1%db"}, // set background color
870 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
871# else
872 {(int)KS_CAB, "\033|%db"}, // set background color
873 {(int)KS_CAF, "\033|%df"}, // set foreground color
874# endif
875
876 {(int)KS_MS, "y"}, // save to move cur in reverse mode
877 {(int)KS_UT, "y"},
878 {(int)KS_XN, "y"},
879 {(int)KS_LE, "\b"},
880# ifdef TERMINFO
881 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
882# else
883 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
884# endif
885 {(int)KS_VB, "\033|B"}, // visual bell
886 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
887 {(int)KS_TE, "\033|E"}, // out of termcap mode
888# ifdef TERMINFO
889 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
890# else
891 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
892# endif
893# ifdef FEAT_TERMGUICOLORS
894 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
895 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
896# endif
897
898 {K_UP, "\316H"},
899 {K_DOWN, "\316P"},
900 {K_LEFT, "\316K"},
901 {K_RIGHT, "\316M"},
902 {K_S_UP, "\316\304"},
903 {K_S_DOWN, "\316\317"},
904 {K_S_LEFT, "\316\311"},
905 {K_C_LEFT, "\316s"},
906 {K_S_RIGHT, "\316\313"},
907 {K_C_RIGHT, "\316t"},
908 {K_S_TAB, "\316\017"},
909 {K_F1, "\316;"},
910 {K_F2, "\316<"},
911 {K_F3, "\316="},
912 {K_F4, "\316>"},
913 {K_F5, "\316?"},
914 {K_F6, "\316@"},
915 {K_F7, "\316A"},
916 {K_F8, "\316B"},
917 {K_F9, "\316C"},
918 {K_F10, "\316D"},
919 {K_F11, "\316\205"},
920 {K_F12, "\316\206"},
921 {K_S_F1, "\316T"},
922 {K_S_F2, "\316U"},
923 {K_S_F3, "\316V"},
924 {K_S_F4, "\316W"},
925 {K_S_F5, "\316X"},
926 {K_S_F6, "\316Y"},
927 {K_S_F7, "\316Z"},
928 {K_S_F8, "\316["},
929 {K_S_F9, "\316\\"},
930 {K_S_F10, "\316]"},
931 {K_S_F11, "\316\207"},
932 {K_S_F12, "\316\210"},
933 {K_INS, "\316R"},
934 {K_DEL, "\316S"},
935 {K_HOME, "\316G"},
936 {K_S_HOME, "\316\302"},
937 {K_C_HOME, "\316w"},
938 {K_END, "\316O"},
939 {K_S_END, "\316\315"},
940 {K_C_END, "\316u"},
941 {K_PAGEDOWN, "\316Q"},
942 {K_PAGEUP, "\316I"},
943 {K_KPLUS, "\316N"},
944 {K_KMINUS, "\316J"},
945 {K_KMULTIPLY, "\316\067"},
946 {K_K0, "\316\332"},
947 {K_K1, "\316\336"},
948 {K_K2, "\316\342"},
949 {K_K3, "\316\346"},
950 {K_K4, "\316\352"},
951 {K_K5, "\316\356"},
952 {K_K6, "\316\362"},
953 {K_K7, "\316\366"},
954 {K_K8, "\316\372"},
955 {K_K9, "\316\376"},
956 {K_BS, "\316x"},
957 {K_S_BS, "\316y"},
958
959 {(int)KS_NAME, NULL} // end marker
960};
961
962#if defined(FEAT_GUI)
963/*
964 * GUI uses made-up codes, only used inside Vim.
965 */
966static tcap_entry_T builtin_gui[] = {
967 {(int)KS_CE, "\033|$"},
968 {(int)KS_AL, "\033|i"},
969# ifdef TERMINFO
970 {(int)KS_CAL, "\033|%p1%dI"},
971# else
972 {(int)KS_CAL, "\033|%dI"},
973# endif
974 {(int)KS_DL, "\033|d"},
975# ifdef TERMINFO
976 {(int)KS_CDL, "\033|%p1%dD"},
977 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
978 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
979# else
980 {(int)KS_CDL, "\033|%dD"},
981 {(int)KS_CS, "\033|%d;%dR"},
982 {(int)KS_CSV, "\033|%d;%dV"},
983# endif
984 {(int)KS_CL, "\033|C"},
985 // attributes switched on with 'h', off with * 'H'
986 {(int)KS_ME, "\033|31H"}, // HL_ALL
987 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
988 {(int)KS_MD, "\033|2h"}, // HL_BOLD
989 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
990 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
991 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
992 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
993 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
994 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
995 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
996 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
997 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
998 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
999 {(int)KS_VB, "\033|f"},
1000 {(int)KS_MS, "y"},
1001 {(int)KS_UT, "y"},
1002 {(int)KS_XN, "y"},
1003 {(int)KS_LE, "\b"}, // cursor-left = BS
1004 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
1005# ifdef TERMINFO
1006 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
1007# else
1008 {(int)KS_CM, "\033|%d;%dM"},
1009# endif
1010 // there are no key sequences here, the GUI sequences are recognized
1011 // in check_termcode()
1012
1013 {(int)KS_NAME, NULL} // end marker
1014};
1015#endif
1016
1017/*
1018 * Amiga console window, default for Amiga.
1019 */
1020static tcap_entry_T builtin_amiga[] = {
1021 {(int)KS_CE, "\033[K"},
1022 {(int)KS_CD, "\033[J"},
1023 {(int)KS_AL, "\033[L"},
1024# ifdef TERMINFO
1025 {(int)KS_CAL, "\033[%p1%dL"},
1026# else
1027 {(int)KS_CAL, "\033[%dL"},
1028# endif
1029 {(int)KS_DL, "\033[M"},
1030# ifdef TERMINFO
1031 {(int)KS_CDL, "\033[%p1%dM"},
1032# else
1033 {(int)KS_CDL, "\033[%dM"},
1034# endif
1035 {(int)KS_CL, "\014"},
1036 {(int)KS_VI, "\033[0 p"},
1037 {(int)KS_VE, "\033[1 p"},
1038 {(int)KS_ME, "\033[0m"},
1039 {(int)KS_MR, "\033[7m"},
1040 {(int)KS_MD, "\033[1m"},
1041 {(int)KS_SE, "\033[0m"},
1042 {(int)KS_SO, "\033[33m"},
1043 {(int)KS_US, "\033[4m"},
1044 {(int)KS_UE, "\033[0m"},
1045 {(int)KS_CZH, "\033[3m"},
1046 {(int)KS_CZR, "\033[0m"},
1047#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1048 {(int)KS_CCO, "8"}, // allow 8 colors
1049# ifdef TERMINFO
1050 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1051 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
1052# else
1053 {(int)KS_CAB, "\033[4%dm"}, // set background color
1054 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
1055# endif
1056 {(int)KS_OP, "\033[m"}, // reset colors
1057#endif
1058 {(int)KS_MS, "y"},
1059 {(int)KS_UT, "y"}, // guessed
1060 {(int)KS_LE, "\b"},
1061# ifdef TERMINFO
1062 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1063# else
1064 {(int)KS_CM, "\033[%i%d;%dH"},
1065# endif
1066#if defined(__MORPHOS__)
1067 {(int)KS_SR, "\033M"},
1068#endif
1069# ifdef TERMINFO
1070 {(int)KS_CRI, "\033[%p1%dC"},
1071# else
1072 {(int)KS_CRI, "\033[%dC"},
1073# endif
1074 {K_UP, "\233A"},
1075 {K_DOWN, "\233B"},
1076 {K_LEFT, "\233D"},
1077 {K_RIGHT, "\233C"},
1078 {K_S_UP, "\233T"},
1079 {K_S_DOWN, "\233S"},
1080 {K_S_LEFT, "\233 A"},
1081 {K_S_RIGHT, "\233 @"},
1082 {K_S_TAB, "\233Z"},
1083 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1084 {K_F2, "\233\061~"},
1085 {K_F3, "\233\062~"},
1086 {K_F4, "\233\063~"},
1087 {K_F5, "\233\064~"},
1088 {K_F6, "\233\065~"},
1089 {K_F7, "\233\066~"},
1090 {K_F8, "\233\067~"},
1091 {K_F9, "\233\070~"},
1092 {K_F10, "\233\071~"},
1093 {K_S_F1, "\233\061\060~"},
1094 {K_S_F2, "\233\061\061~"},
1095 {K_S_F3, "\233\061\062~"},
1096 {K_S_F4, "\233\061\063~"},
1097 {K_S_F5, "\233\061\064~"},
1098 {K_S_F6, "\233\061\065~"},
1099 {K_S_F7, "\233\061\066~"},
1100 {K_S_F8, "\233\061\067~"},
1101 {K_S_F9, "\233\061\070~"},
1102 {K_S_F10, "\233\061\071~"},
1103 {K_HELP, "\233?~"},
1104 {K_INS, "\233\064\060~"}, // 101 key keyboard
1105 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1106 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1107 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1108 {K_END, "\233\064\065~"}, // 101 key keyboard
1109
1110 {BT_EXTRA_KEYS, ""},
1111 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1112 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1113 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1114
1115 {(int)KS_NAME, NULL} // end marker
1116};
1117
1118/*
1119 * The most minimal terminal: only clear screen and cursor positioning.
1120 */
1121static tcap_entry_T builtin_dumb[] = {
1122 {(int)KS_CL, "\014"},
1123#ifdef TERMINFO
1124 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1125#else
1126 {(int)KS_CM, "\033[%i%d;%dH"},
1127#endif
1128
1129 {(int)KS_NAME, NULL} // end marker
1130};
1131
1132/*
1133 * Terminal used for debugging.
1134 */
1135static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 {(int)KS_CE, "[CE]"},
1137 {(int)KS_CD, "[CD]"},
1138 {(int)KS_AL, "[AL]"},
1139# ifdef TERMINFO
1140 {(int)KS_CAL, "[CAL%p1%d]"},
1141# else
1142 {(int)KS_CAL, "[CAL%d]"},
1143# endif
1144 {(int)KS_DL, "[DL]"},
1145# ifdef TERMINFO
1146 {(int)KS_CDL, "[CDL%p1%d]"},
1147# else
1148 {(int)KS_CDL, "[CDL%d]"},
1149# endif
1150# ifdef TERMINFO
1151 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1152# else
1153 {(int)KS_CS, "[%dCS%d]"},
1154# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001155# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001157# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159# endif
1160# ifdef TERMINFO
1161 {(int)KS_CAB, "[CAB%p1%d]"},
1162 {(int)KS_CAF, "[CAF%p1%d]"},
1163 {(int)KS_CSB, "[CSB%p1%d]"},
1164 {(int)KS_CSF, "[CSF%p1%d]"},
1165# else
1166 {(int)KS_CAB, "[CAB%d]"},
1167 {(int)KS_CAF, "[CAF%d]"},
1168 {(int)KS_CSB, "[CSB%d]"},
1169 {(int)KS_CSF, "[CSF%d]"},
1170# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001171 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {(int)KS_OP, "[OP]"},
1173 {(int)KS_LE, "[LE]"},
1174 {(int)KS_CL, "[CL]"},
1175 {(int)KS_VI, "[VI]"},
1176 {(int)KS_VE, "[VE]"},
1177 {(int)KS_VS, "[VS]"},
1178 {(int)KS_ME, "[ME]"},
1179 {(int)KS_MR, "[MR]"},
1180 {(int)KS_MB, "[MB]"},
1181 {(int)KS_MD, "[MD]"},
1182 {(int)KS_SE, "[SE]"},
1183 {(int)KS_SO, "[SO]"},
1184 {(int)KS_UE, "[UE]"},
1185 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001186 {(int)KS_UCE, "[UCE]"},
1187 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001188 {(int)KS_USS, "[USS]"},
1189 {(int)KS_DS, "[DS]"},
1190 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001191 {(int)KS_STE, "[STE]"},
1192 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 {(int)KS_MS, "[MS]"},
1194 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001195 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196# ifdef TERMINFO
1197 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1198# else
1199 {(int)KS_CM, "[%dCM%d]"},
1200# endif
1201 {(int)KS_SR, "[SR]"},
1202# ifdef TERMINFO
1203 {(int)KS_CRI, "[CRI%p1%d]"},
1204# else
1205 {(int)KS_CRI, "[CRI%d]"},
1206# endif
1207 {(int)KS_VB, "[VB]"},
1208 {(int)KS_KS, "[KS]"},
1209 {(int)KS_KE, "[KE]"},
1210 {(int)KS_TI, "[TI]"},
1211 {(int)KS_TE, "[TE]"},
1212 {(int)KS_CIS, "[CIS]"},
1213 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001214 {(int)KS_CSC, "[CSC]"},
1215 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {(int)KS_TS, "[TS]"},
1217 {(int)KS_FS, "[FS]"},
1218# ifdef TERMINFO
1219 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1220 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1221# else
1222 {(int)KS_CWS, "[%dCWS%d]"},
1223 {(int)KS_CWP, "[%dCWP%d]"},
1224# endif
1225 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001226 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001227 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001228 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {K_UP, "[KU]"},
1230 {K_DOWN, "[KD]"},
1231 {K_LEFT, "[KL]"},
1232 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001233 {K_XUP, "[xKU]"},
1234 {K_XDOWN, "[xKD]"},
1235 {K_XLEFT, "[xKL]"},
1236 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 {K_S_UP, "[S-KU]"},
1238 {K_S_DOWN, "[S-KD]"},
1239 {K_S_LEFT, "[S-KL]"},
1240 {K_C_LEFT, "[C-KL]"},
1241 {K_S_RIGHT, "[S-KR]"},
1242 {K_C_RIGHT, "[C-KR]"},
1243 {K_F1, "[F1]"},
1244 {K_XF1, "[xF1]"},
1245 {K_F2, "[F2]"},
1246 {K_XF2, "[xF2]"},
1247 {K_F3, "[F3]"},
1248 {K_XF3, "[xF3]"},
1249 {K_F4, "[F4]"},
1250 {K_XF4, "[xF4]"},
1251 {K_F5, "[F5]"},
1252 {K_F6, "[F6]"},
1253 {K_F7, "[F7]"},
1254 {K_F8, "[F8]"},
1255 {K_F9, "[F9]"},
1256 {K_F10, "[F10]"},
1257 {K_F11, "[F11]"},
1258 {K_F12, "[F12]"},
1259 {K_S_F1, "[S-F1]"},
1260 {K_S_XF1, "[S-xF1]"},
1261 {K_S_F2, "[S-F2]"},
1262 {K_S_XF2, "[S-xF2]"},
1263 {K_S_F3, "[S-F3]"},
1264 {K_S_XF3, "[S-xF3]"},
1265 {K_S_F4, "[S-F4]"},
1266 {K_S_XF4, "[S-xF4]"},
1267 {K_S_F5, "[S-F5]"},
1268 {K_S_F6, "[S-F6]"},
1269 {K_S_F7, "[S-F7]"},
1270 {K_S_F8, "[S-F8]"},
1271 {K_S_F9, "[S-F9]"},
1272 {K_S_F10, "[S-F10]"},
1273 {K_S_F11, "[S-F11]"},
1274 {K_S_F12, "[S-F12]"},
1275 {K_HELP, "[HELP]"},
1276 {K_UNDO, "[UNDO]"},
1277 {K_BS, "[BS]"},
1278 {K_INS, "[INS]"},
1279 {K_KINS, "[KINS]"},
1280 {K_DEL, "[DEL]"},
1281 {K_KDEL, "[KDEL]"},
1282 {K_HOME, "[HOME]"},
1283 {K_S_HOME, "[C-HOME]"},
1284 {K_C_HOME, "[C-HOME]"},
1285 {K_KHOME, "[KHOME]"},
1286 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001287 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 {K_END, "[END]"},
1289 {K_S_END, "[C-END]"},
1290 {K_C_END, "[C-END]"},
1291 {K_KEND, "[KEND]"},
1292 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001293 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 {K_PAGEUP, "[PAGEUP]"},
1295 {K_PAGEDOWN, "[PAGEDOWN]"},
1296 {K_KPAGEUP, "[KPAGEUP]"},
1297 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1298 {K_MOUSE, "[MOUSE]"},
1299 {K_KPLUS, "[KPLUS]"},
1300 {K_KMINUS, "[KMINUS]"},
1301 {K_KDIVIDE, "[KDIVIDE]"},
1302 {K_KMULTIPLY, "[KMULTIPLY]"},
1303 {K_KENTER, "[KENTER]"},
1304 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001305 {K_PS, "[PASTE-START]"},
1306 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {K_K0, "[K0]"},
1308 {K_K1, "[K1]"},
1309 {K_K2, "[K2]"},
1310 {K_K3, "[K3]"},
1311 {K_K4, "[K4]"},
1312 {K_K5, "[K5]"},
1313 {K_K6, "[K6]"},
1314 {K_K7, "[K7]"},
1315 {K_K8, "[K8]"},
1316 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317
Bram Moolenaar4654d632022-11-17 22:05:12 +00001318 {(int)KS_NAME, NULL} // end marker
1319};
1320
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001322 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001324typedef struct {
1325 char *bitc_name; // name, such as "xterm"
1326 tcap_entry_T *bitc_table; // table with entries for bitc_name
1327} builtin_tcap_T;
1328
1329builtin_tcap_T builtin_terminals[] = {
1330 // Unix and Generic
1331 {"ansi", builtin_ansi},
1332 {"vt320", builtin_vt320},
1333 {"vt52", builtin_vt52},
1334 {"xterm", builtin_xterm},
1335 {"iris-ansi", builtin_iris_ansi},
1336
1337 // MS-Windows
1338 {"pcansi", builtin_pcansi},
1339 {"win32", builtin_win32},
1340
1341 // Other systems
1342#if defined(FEAT_GUI)
1343 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001345 {"amiga", builtin_amiga},
1346 {"dumb", builtin_dumb},
1347 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
Bram Moolenaar4654d632022-11-17 22:05:12 +00001349 {NULL, NULL}, // end marker
1350};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001352#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001353 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001354termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001355{
Bram Moolenaarab302212016-04-26 20:59:29 +02001356 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001357}
1358
1359 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001360termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001361{
1362 guicolor_T t;
1363
1364 if (*name == NUL)
1365 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001366 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001367
1368 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001369 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001370 return t;
1371}
1372
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001373 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001374termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001375{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001376 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001377}
1378#endif
1379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380/*
1381 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383#ifdef AMIGA
1384# define DEFAULT_TERM (char_u *)"amiga"
1385#endif
1386
1387#ifdef MSWIN
1388# define DEFAULT_TERM (char_u *)"win32"
1389#endif
1390
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001391#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392# define DEFAULT_TERM (char_u *)"ansi"
1393#endif
1394
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395#ifdef VMS
1396# define DEFAULT_TERM (char_u *)"vt320"
1397#endif
1398
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001399#ifdef __HAIKU__
1400# undef DEFAULT_TERM
1401# define DEFAULT_TERM (char_u *)"xterm"
1402#endif
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#ifndef DEFAULT_TERM
1405# define DEFAULT_TERM (char_u *)"dumb"
1406#endif
1407
1408/*
1409 * Term_strings contains currently used terminal output strings.
1410 * It is initialized with the default values by parse_builtin_tcap().
1411 * The values can be changed by setting the option with the same name.
1412 */
1413char_u *(term_strings[(int)KS_LAST + 1]);
1414
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001415static int need_gather = FALSE; // need to fill termleader[]
1416static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001418static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001419#endif
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001420
1421/*
1422 * Structure and table to store terminal features that can be detected by
1423 * querying the terminal. Either by inspecting the termresponse or a more
1424 * specific request. Besides this there are:
1425 * t_colors - number of colors supported
1426 */
1427typedef struct {
1428 char *tpr_name;
1429 int tpr_set_by_termresponse;
1430 int tpr_status;
1431} termprop_T;
1432
1433// Values for tpr_status.
1434#define TPR_UNKNOWN 'u'
1435#define TPR_YES 'y'
1436#define TPR_NO 'n'
1437#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1438#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1439#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1440
1441// can request the cursor style without messing up the display
1442#define TPR_CURSOR_STYLE 0
1443// can request the cursor blink mode without messing up the display
1444#define TPR_CURSOR_BLINK 1
1445// can set the underline color with t_8u without resetting other colors
1446#define TPR_UNDERLINE_RGB 2
1447// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1448#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001449// term response indicates kitty
1450#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001451// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001452#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001453
1454static termprop_T term_props[TPR_COUNT];
1455
1456/*
1457 * Initialize the term_props table.
1458 * When "all" is FALSE only set those that are detected from the version
1459 * response.
1460 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001461 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001462init_term_props(int all)
1463{
1464 int i;
1465
1466 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1467 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1468 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1469 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1470 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1471 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1472 term_props[TPR_MOUSE].tpr_name = "mouse";
1473 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001474 term_props[TPR_KITTY].tpr_name = "kitty";
1475 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001476
1477 for (i = 0; i < TPR_COUNT; ++i)
1478 if (all || term_props[i].tpr_set_by_termresponse)
1479 term_props[i].tpr_status = TPR_UNKNOWN;
1480}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001482#if defined(FEAT_EVAL) || defined(PROTO)
1483 void
1484f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1485{
1486# ifdef FEAT_TERMRESPONSE
1487 int i;
1488# endif
1489
Bram Moolenaar93a10962022-06-16 11:42:09 +01001490 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001491 return;
1492# ifdef FEAT_TERMRESPONSE
1493 for (i = 0; i < TPR_COUNT; ++i)
1494 {
1495 char_u value[2];
1496
1497 value[0] = term_props[i].tpr_status;
1498 value[1] = NUL;
1499 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1500 }
1501# endif
1502}
1503#endif
1504
Bram Moolenaar4654d632022-11-17 22:05:12 +00001505/*
1506 * Find the builtin termcap entries for "term".
1507 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1508 * entry.
1509 * Returns NULL when "term" is not found.
1510 */
1511 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001512find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001514 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001516 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1517 if (name == NULL) // end marker
1518 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001520 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1521 return builtin_terminals[i].bitc_table;
1522 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1523 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524#endif
1525#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001526 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1527 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001529 if (STRCMP(term, name) == 0)
1530 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001532 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533}
1534
1535/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001536 * Apply entries from a builtin termcap.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 */
1538 static void
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001539apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001541 int term_8bit = term_is_8bit(term);
1542
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001543 for (tcap_entry_T *p = entries;
1544 p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001546 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001548 // Only set the value if it wasn't set yet or "overwrite" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (term_strings[p->bt_entry] == NULL
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001550 || term_strings[p->bt_entry] == empty_option
1551 || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001553#ifdef FEAT_EVAL
1554 int opt_idx = -1;
1555#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001556 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1558 {
1559 char_u *s, *t;
1560
1561 s = vim_strsave((char_u *)p->bt_string);
1562 if (s != NULL)
1563 {
1564 for (t = s; *t; ++t)
1565 if (term_7to8bit(t))
1566 {
1567 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001568 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 }
1570 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001571#ifdef FEAT_EVAL
1572 opt_idx =
1573#endif
1574 set_term_option_alloced(
1575 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 }
1577 }
1578 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001579 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001581#ifdef FEAT_EVAL
1582 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1583#endif
1584 }
1585#ifdef FEAT_EVAL
1586 set_term_option_sctx_idx(NULL, opt_idx);
1587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 }
1589 }
1590 else
1591 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001592 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1594 name[1] = KEY2TERMCAP1((int)p->bt_entry);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001595 if (find_termcode(name) == NULL || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1597 }
1598 }
1599}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001602 * Parsing of the builtin termcap entries.
1603 * Caller should check if "term" is a valid builtin terminal name.
1604 * The terminal's name is not set, as this is already done in termcapinit().
1605 */
1606 static void
1607parse_builtin_tcap(char_u *term)
1608{
1609 tcap_entry_T *entries = find_builtin_term(term);
1610 if (entries != NULL)
1611 apply_builtin_tcap(term, entries, FALSE);
1612}
1613
1614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 * Set number of colors.
1616 * Store it as a number in t_colors.
1617 * Store it as a string in T_CCO (using nr_colors[]).
1618 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001619 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001620set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001622 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
1624 t_colors = nr;
1625 if (t_colors > 1)
1626 sprintf((char *)nr_colors, "%d", t_colors);
1627 else
1628 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001629 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001631
1632/*
1633 * Set the color count to "val" and redraw if it changed.
1634 */
1635 static void
1636may_adjust_color_count(int val)
1637{
1638 if (val != t_colors)
1639 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001640 // Nr of colors changed, initialize highlighting and redraw everything.
1641 // This causes a redraw, which usually clears the message. Try keeping
1642 // the message if it might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001643 set_keep_msg_from_hist();
1644 set_color_count(val);
1645 init_highlight(TRUE, FALSE);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001646#ifdef DEBUG_TERMRESPONSE
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001647 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001648 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001649
Bram Moolenaarb255b902018-04-24 21:40:10 +02001650 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001651 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001652#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001653 redraw_asap(UPD_CLEAR);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001654#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001655 }
1656}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
1658#ifdef HAVE_TGETENT
1659static char *(key_names[]) =
1660{
Bram Moolenaar87202262020-05-24 17:23:45 +02001661# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001662 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 "#2", "#4", "%i", "*7",
1667 "k1", "k2", "k3", "k4", "k5", "k6",
1668 "k7", "k8", "k9", "k;", "F1", "F2",
1669 "%1", "&8", "kb", "kI", "kD", "kh",
1670 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1671 NULL
1672};
1673#endif
1674
Bram Moolenaar9289df52018-05-10 14:40:57 +02001675#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001676 static void
1677get_term_entries(int *height, int *width)
1678{
1679 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001680 enum SpecialKey dest; // index in term_strings[]
1681 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001682 } string_names[] =
1683 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1684 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1685 {KS_CL, "cl"}, {KS_CD, "cd"},
1686 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1687 {KS_ME, "me"}, {KS_MR, "mr"},
1688 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1689 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1690 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001691 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001692 {KS_STE,"Te"}, {KS_STS,"Ts"},
1693 {KS_CM, "cm"}, {KS_SR, "sr"},
1694 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1695 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001696 {KS_CTI, "TI"}, {KS_CRK, "RK"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001697 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001698 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1699 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001700 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1701 {KS_VS, "vs"}, {KS_CVS, "VS"},
1702 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1703 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1704 {KS_TS, "ts"}, {KS_FS, "fs"},
1705 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1706 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1707 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001708 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001709 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1710 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001711 {KS_CST, "ST"}, {KS_CRT, "RT"},
1712 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001713 {(enum SpecialKey)0, NULL}
1714 };
1715 int i;
1716 char_u *p;
1717 static char_u tstrbuf[TBUFSZ];
1718 char_u *tp = tstrbuf;
1719
1720 /*
1721 * get output strings
1722 */
1723 for (i = 0; string_names[i].name != NULL; ++i)
1724 {
1725 if (TERM_STR(string_names[i].dest) == NULL
1726 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001727 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001728 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001729#ifdef FEAT_EVAL
1730 set_term_option_sctx_idx(string_names[i].name, -1);
1731#endif
1732 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001733 }
1734
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001735 // tgetflag() returns 1 if the flag is present, 0 if not and
1736 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001737 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1738 T_MS = (char_u *)"y";
1739 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1740 T_XS = (char_u *)"y";
1741 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1742 T_XN = (char_u *)"y";
1743 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1744 T_DB = (char_u *)"y";
1745 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1746 T_DA = (char_u *)"y";
1747 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1748 T_UT = (char_u *)"y";
1749
1750 /*
1751 * get key codes
1752 */
1753 for (i = 0; key_names[i] != NULL; ++i)
1754 if (find_termcode((char_u *)key_names[i]) == NULL)
1755 {
1756 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001757 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001758 if (p != NULL
1759 && (*p != Ctrl_H
1760 || key_names[i][0] != 'k'
1761 || key_names[i][1] != 'l'))
1762 add_termcode((char_u *)key_names[i], p, FALSE);
1763 }
1764
1765 if (*height == 0)
1766 *height = tgetnum("li");
1767 if (*width == 0)
1768 *width = tgetnum("co");
1769
1770 /*
1771 * Get number of colors (if not done already).
1772 */
1773 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001774 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001775 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001776#ifdef FEAT_EVAL
1777 set_term_option_sctx_idx("Co", -1);
1778#endif
1779 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001780
1781# ifndef hpux
1782 BC = (char *)TGETSTR("bc", &tp);
1783 UP = (char *)TGETSTR("up", &tp);
1784 p = TGETSTR("pc", &tp);
1785 if (p)
1786 PC = *p;
1787# endif
1788}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001789#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001790
Bram Moolenaar4654d632022-11-17 22:05:12 +00001791/*
1792 * Report "term" is not found and list the ones we do know about.
1793 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001794 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001795report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001796{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001797 mch_errmsg("\r\n");
1798 if (error_msg != NULL)
1799 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001800 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001801 mch_errmsg("\r\n");
1802 }
1803 mch_errmsg("'");
1804 mch_errmsg((char *)term);
1805 mch_errmsg(_("' not known. Available builtin terminals are:"));
1806 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001807
1808 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001809 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001810 char *name = builtin_terminals[i].bitc_name;
1811 if (name == NULL) // end marker
1812 break;
1813 // Do not mention the "gui" entry, the user won't need to type it.
1814 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001815 {
1816#ifdef HAVE_TGETENT
1817 mch_errmsg(" builtin_");
1818#else
1819 mch_errmsg(" ");
1820#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001821 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001822 mch_errmsg("\r\n");
1823 }
1824 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001825 // Output extra 'cmdheight' line breaks to avoid that the following error
1826 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001827 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001828 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001829}
1830
1831 static void
1832report_default_term(char_u *term)
1833{
1834 mch_errmsg(_("defaulting to '"));
1835 mch_errmsg((char *)term);
1836 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001837 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001838 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001839 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001840 out_flush();
1841 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001842 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001843 }
1844}
1845
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001847 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1848 * for the first matching entry.
1849 * When "term" is NULL then compile all patterns to check for any errors.
1850 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1851 * Returns KEYPROTOCOL_NONE if there is no match.
1852 */
1853 keyprot_T
1854match_keyprotocol(char_u *term)
1855{
1856 int len = (int)STRLEN(p_kpc) + 1;
1857 char_u *buf = alloc(len);
1858 if (buf == NULL)
1859 return KEYPROTOCOL_FAIL;
1860
1861 keyprot_T ret = KEYPROTOCOL_FAIL;
1862 char_u *p = p_kpc;
1863 while (*p != NUL)
1864 {
1865 // Isolate one comma separated item.
1866 (void)copy_option_part(&p, buf, len, ",");
1867 char_u *colon = vim_strchr(buf, ':');
1868 if (colon == NULL || colon == buf || colon[1] == NUL)
1869 goto theend;
1870 *colon = NUL;
1871
1872 keyprot_T prot;
1873 if (STRCMP(colon + 1, "none") == 0)
1874 prot = KEYPROTOCOL_NONE;
1875 else if (STRCMP(colon + 1, "mok2") == 0)
1876 prot = KEYPROTOCOL_MOK2;
1877 else if (STRCMP(colon + 1, "kitty") == 0)
1878 prot = KEYPROTOCOL_KITTY;
1879 else
1880 goto theend;
1881
1882 regmatch_T regmatch;
1883 CLEAR_FIELD(regmatch);
1884 regmatch.rm_ic = TRUE;
1885 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1886 if (regmatch.regprog == NULL)
1887 goto theend;
1888
1889 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1890 vim_regfree(regmatch.regprog);
1891 if (match)
1892 {
1893 ret = prot;
1894 goto theend;
1895 }
1896
1897 }
1898
1899 // No match found, use "none".
1900 ret = KEYPROTOCOL_NONE;
1901
1902theend:
1903 vim_free(buf);
1904 return ret;
1905}
1906
1907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 * Set terminal options for terminal "term".
1909 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1910 *
1911 * While doing this, until ttest(), some options may be NULL, be careful.
1912 */
1913 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001914set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916#ifdef HAVE_TGETENT
1917 int builtin_first = p_tbi;
1918 int try;
1919 int termcap_cleared = FALSE;
1920#endif
1921 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001922 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 char_u *bs_p, *del_p;
1924
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001925 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001926 if (silent_mode)
1927 return OK;
1928
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001929 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
1931 if (term_is_builtin(term))
1932 {
1933 term += 8;
1934#ifdef HAVE_TGETENT
1935 builtin_first = 1;
1936#endif
1937 }
1938
1939/*
1940 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1941 * If builtin_first is TRUE:
1942 * 0. try builtin termcap
1943 * 1. try external termcap
1944 * 2. if both fail default to a builtin terminal
1945 * If builtin_first is FALSE:
1946 * 1. try external termcap
1947 * 2. try builtin termcap, if both fail default to a builtin terminal
1948 */
1949#ifdef HAVE_TGETENT
1950 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1951 {
1952 /*
1953 * Use external termcap
1954 */
1955 if (try == 1)
1956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958
1959 /*
1960 * If the external termcap does not have a matching entry, try the
1961 * builtin ones.
1962 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01001963 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 if (!termcap_cleared)
1966 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001967 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 termcap_cleared = TRUE;
1969 }
1970
Bram Moolenaar69e05692018-05-10 14:11:52 +02001971 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 }
1973 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001974 else // try == 0 || try == 2
1975#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 /*
1977 * Use builtin termcap
1978 */
1979 {
1980#ifdef HAVE_TGETENT
1981 /*
1982 * If builtin termcap was already used, there is no need to search
1983 * for the builtin termcap again, quit now.
1984 */
1985 if (try == 2 && builtin_first && termcap_cleared)
1986 break;
1987#endif
1988 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001989 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001991 tcap_entry_T *termp = find_builtin_term(term);
1992 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
1994#ifdef HAVE_TGETENT
1995 /*
1996 * If try == 0, first try the external termcap. If that is not
1997 * found we'll get back here with try == 2.
1998 * If termcap_cleared is set we used the external termcap,
1999 * don't complain about not finding the term in the builtin
2000 * termcap.
2001 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002002 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002004 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 break;
2006#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02002007 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002009 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 if (starting != NO_SCREEN)
2011 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002012 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 wait_return(TRUE);
2014 return FAIL;
2015 }
2016 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002017 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002018 set_string_option_direct((char_u *)"term", -1, term,
2019 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 display_errors();
2021 }
2022 out_flush();
2023#ifdef HAVE_TGETENT
2024 if (!termcap_cleared)
2025 {
2026#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002027 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028#ifdef HAVE_TGETENT
2029 termcap_cleared = TRUE;
2030 }
2031#endif
2032 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002033
2034 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
2035 // termcap entries if there is an entry maching "term".
2036 keyprot_T kpc = match_keyprotocol(term);
2037 if (kpc == KEYPROTOCOL_KITTY)
2038 apply_builtin_tcap(term, builtin_kitty, TRUE);
2039 else if (kpc == KEYPROTOCOL_MOK2)
2040 apply_builtin_tcap(term, builtin_mok2, TRUE);
2041
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042#ifdef FEAT_GUI
2043 if (term_is_gui(term))
2044 {
2045 out_flush();
2046 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002047 // If starting the GUI failed, don't do any of the other
2048 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 if (!gui.in_use)
2050 return FAIL;
2051#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002052 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053#endif
2054 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002055#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 }
2057#ifdef HAVE_TGETENT
2058 }
2059#endif
2060
2061/*
2062 * special: There is no info in the termcap about whether the cursor
2063 * positioning is relative to the start of the screen or to the start of the
2064 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2065 * relative.
2066 */
2067 if (STRCMP(term, "pcterm") == 0)
2068 T_CCS = (char_u *)"yes";
2069 else
2070 T_CCS = empty_option;
2071
2072#ifdef UNIX
2073/*
2074 * Any "stty" settings override the default for t_kb from the termcap.
2075 * This is in os_unix.c, because it depends a lot on the version of unix that
2076 * is being used.
2077 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2078 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002079# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002081# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 get_stty();
2083#endif
2084
2085/*
2086 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2087 * didn't work, use the default CTRL-H
2088 * The default for t_kD is DEL, unless t_kb is DEL.
2089 * The vim_strsave'd strings are probably lost forever, well it's only two
2090 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2091 * directly.
2092 */
2093#ifdef FEAT_GUI
2094 if (!gui.in_use)
2095#endif
2096 {
2097 bs_p = find_termcode((char_u *)"kb");
2098 del_p = find_termcode((char_u *)"kD");
2099 if (bs_p == NULL || *bs_p == NUL)
2100 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2101 if ((del_p == NULL || *del_p == NUL) &&
2102 (bs_p == NULL || *bs_p != DEL))
2103 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2104 }
2105
2106#if defined(UNIX) || defined(VMS)
2107 term_is_xterm = vim_is_xterm(term);
2108#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002109#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002110 // Reset terminal properties that are set based on the termresponse, which
2111 // will be sent out soon.
2112 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002115#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 /*
2117 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2118 * The termcode for the mouse is added as a side effect in option.c.
2119 */
2120 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002121 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002123# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002124 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
2126 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002127 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 else
2129 p = (char_u *)"xterm";
2130 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002133 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002134 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002135 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2136 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002137 reset_option_was_set((char_u *)"ttym");
2138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002140# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002142# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002144 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002146#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
Bram Moolenaarbf789742021-01-16 11:21:40 +01002150#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002151 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002152 if (use_xterm_like_mouse(term))
2153 {
2154 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002155
2156 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002157 name[0] = KS_EXTRA;
2158 name[1] = KE_FOCUSGAINED;
2159 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002160 add_termcode(name, (char_u *)"\033[I", FALSE);
2161
2162 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002163 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002164 add_termcode(name, (char_u *)"\033[O", FALSE);
2165
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002166 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002167 }
2168#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002169#if (defined(UNIX) || defined(VMS))
2170 // First time after setting 'term' a focus event is always reported.
2171 focus_state = MAYBE;
2172#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002173
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002175 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (STRCMP(term, DEFAULT_TERM) != 0)
2177 term_console = FALSE;
2178 else
2179 {
2180 term_console = TRUE;
2181# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002182 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183# endif
2184 }
2185#endif
2186
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002187 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002189 full_screen = TRUE; // we can use termcap codes from now on
2190 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002192 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002193 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002194 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195#endif
2196
2197 /*
2198 * Initialize the terminal with the appropriate termcap codes.
2199 * Set the mouse and window title if possible.
2200 * Don't do this when starting, need to parse the .vimrc first, because it
2201 * may redefine t_TI etc.
2202 */
2203 if (starting != NO_SCREEN)
2204 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002205 starttermcap(); // may change terminal mode
2206 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002207 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002210 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 if (width <= 0 || height <= 0)
2212 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002213 // termcap failed to report size
2214 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002216#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002217 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002219 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220#endif
2221 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002222 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 if (starting != NO_SCREEN)
2224 {
2225 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002226 scroll_region_reset(); // In case Rows changed
2227 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002230 buf_T *buf;
2231 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
2233 /*
2234 * Execute the TermChanged autocommands for each buffer that is
2235 * loaded.
2236 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002237 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
2239 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002240 {
2241 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002242 if (curbuf == buf)
2243 {
2244 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 curbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002246 // restore curwin/curbuf and a few other things
2247 aucmd_restbuf(&aco);
2248 }
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 }
2253
2254#ifdef FEAT_TERMRESPONSE
2255 may_req_termresponse();
2256#endif
2257
2258 return OK;
2259}
2260
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002261#if defined(EXITFREE) || defined(PROTO)
2262
2263# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002264# include <term.h> // declares cur_term
2265# endif
2266
2267/*
2268 * If supported, delete "cur_term", which caches terminal related entries.
2269 * Avoids that valgrind reports possibly lost memory.
2270 */
2271 void
2272free_cur_term()
2273{
2274# ifdef HAVE_DEL_CURTERM
2275 if (cur_term)
2276 del_curterm(cur_term);
2277# endif
2278}
2279
2280#endif
2281
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282#ifdef HAVE_TGETENT
2283/*
2284 * Call tgetent()
2285 * Return error message if it fails, NULL if it's OK.
2286 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002287 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002288invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
2290 int i;
2291
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002292 // Note: Valgrind may report a leak here, because the library keeps one
2293 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002295 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002297 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298# endif
2299 )
2300 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002301 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2302 // tgetent() with the always existing "dumb" entry to avoid a crash or
2303 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 (void)TGETENT(tbuf, "dumb");
2305
2306 if (i < 0)
2307# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002308 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (i == 0)
2310# endif
2311#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002312 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002314 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#endif
2316 }
2317 return NULL;
2318}
2319
2320/*
2321 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2322 * Fix that here.
2323 */
2324 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002325vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326{
2327 char *p;
2328
2329 p = tgetstr(s, (char **)pp);
2330 if (p == (char *)-1)
2331 p = NULL;
2332 return (char_u *)p;
2333}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002334#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002336#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337/*
2338 * Get Columns and Rows from the termcap. Used after a window signal if the
2339 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2340 * and "li" entries never change. But on some systems this works.
2341 * Errors while getting the entries are ignored.
2342 */
2343 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002344getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002345 long *cp, // pointer to columns
2346 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347{
2348 char_u tbuf[TBUFSZ];
2349
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002350 if (T_NAME != NULL && *T_NAME != NUL && invoke_tgetent(tbuf, T_NAME) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
2352 if (*cp == 0)
2353 *cp = tgetnum("co");
2354 if (*rp == 0)
2355 *rp = tgetnum("li");
2356 }
2357}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002358#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
2360/*
2361 * Get a string entry from the termcap and add it to the list of termcodes.
2362 * Used for <t_xx> special keys.
2363 * Give an error message for failure when not sourcing.
2364 * If force given, replace an existing entry.
2365 * Return FAIL if the entry was not found, OK if the entry was added.
2366 */
2367 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002368add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370 char_u *term;
2371 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372#ifdef HAVE_TGETENT
2373 char_u *string;
2374 int i;
2375 int builtin_first;
2376 char_u tbuf[TBUFSZ];
2377 char_u tstrbuf[TBUFSZ];
2378 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002379 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380#endif
2381
2382/*
2383 * If the GUI is running or will start in a moment, we only support the keys
2384 * that the GUI can produce.
2385 */
2386#ifdef FEAT_GUI
2387 if (gui.in_use || gui.starting)
2388 return gui_mch_haskey(name);
2389#endif
2390
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002391 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 return OK;
2393
2394 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002395 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 return FAIL;
2397
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002398 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
2400 term += 8;
2401#ifdef HAVE_TGETENT
2402 builtin_first = TRUE;
2403#endif
2404 }
2405#ifdef HAVE_TGETENT
2406 else
2407 builtin_first = p_tbi;
2408#endif
2409
2410#ifdef HAVE_TGETENT
2411/*
2412 * We can get the entry from the builtin termcap and from the external one.
2413 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2414 * builtin termcap first.
2415 * If 'ttybuiltin' is off, try external termcap first.
2416 */
2417 for (i = 0; i < 2; ++i)
2418 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002419 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420#endif
2421 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002422 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 */
2424 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002425 tcap_entry_T *termp = find_builtin_term(term);
2426 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
2428 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002429 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 while (termp->bt_entry != (int)KS_NAME)
2431 {
2432 if ((int)termp->bt_entry == key)
2433 {
2434 add_termcode(name, (char_u *)termp->bt_string,
2435 term_is_8bit(term));
2436 return OK;
2437 }
2438 ++termp;
2439 }
2440 }
2441 }
2442#ifdef HAVE_TGETENT
2443 else
2444 /*
2445 * Search in external termcap
2446 */
2447 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002448 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 if (error_msg == NULL)
2450 {
2451 string = TGETSTR((char *)name, &tp);
2452 if (string != NULL && *string != NUL)
2453 {
2454 add_termcode(name, string, FALSE);
2455 return OK;
2456 }
2457 }
2458 }
2459 }
2460#endif
2461
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002462 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
2464#ifdef HAVE_TGETENT
2465 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002466 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 else
2468#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002469 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 }
2471 return FAIL;
2472}
2473
2474 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002475term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476{
2477 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2478}
2479
2480/*
2481 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2482 * Assume that the terminal is using 8-bit controls when the name contains
2483 * "8bit", like in "xterm-8bit".
2484 */
2485 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002486term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
2488 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2489}
2490
2491/*
2492 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002493 * <Esc>[ -> CSI <M_C_[>
2494 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 * <Esc>O -> <M-C-O>
2496 */
2497 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002498term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
2500 if (*p == ESC)
2501 {
2502 if (p[1] == '[')
2503 return CSI;
2504 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002505 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 if (p[1] == 'O')
2507 return 0x8f;
2508 }
2509 return 0;
2510}
2511
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002512#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002514term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2517}
2518#endif
2519
2520#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2521
2522 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002523tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525 static char_u buf[16];
2526 char_u *p;
2527
2528 p = buf + 15;
2529 *p = '\0';
2530 do
2531 {
2532 --p;
2533 *p = (char_u) (i % 10 + '0');
2534 i /= 10;
2535 }
2536 while (i > 0 && p > buf);
2537 return p;
2538}
2539#endif
2540
2541#ifndef HAVE_TGETENT
2542
2543/*
2544 * minimal tgoto() implementation.
2545 * no padding and we only parse for %i %d and %+char
2546 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002547 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002548tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549{
2550 static char buf[30];
2551 char *p, *s, *e;
2552
2553 if (!cm)
2554 return "OOPS";
2555 e = buf + 29;
2556 for (s = buf; s < e && *cm; cm++)
2557 {
2558 if (*cm != '%')
2559 {
2560 *s++ = *cm;
2561 continue;
2562 }
2563 switch (*++cm)
2564 {
2565 case 'd':
2566 p = (char *)tltoa((unsigned long)y);
2567 y = x;
2568 while (*p)
2569 *s++ = *p++;
2570 break;
2571 case 'i':
2572 x++;
2573 y++;
2574 break;
2575 case '+':
2576 *s++ = (char)(*++cm + y);
2577 y = x;
2578 break;
2579 case '%':
2580 *s++ = *cm;
2581 break;
2582 default:
2583 return "OOPS";
2584 }
2585 }
2586 *s = '\0';
2587 return buf;
2588}
2589
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002590#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
2592/*
2593 * Set the terminal name and initialize the terminal options.
2594 * If "name" is NULL or empty, get the terminal name from the environment.
2595 * If that fails, use the default terminal name.
2596 */
2597 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002598termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
2600 char_u *term;
2601
2602 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002603 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 term = name;
2605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606#ifndef MSWIN
2607 if (term == NULL)
2608 term = mch_getenv((char_u *)"TERM");
2609#endif
2610 if (term == NULL || *term == NUL)
2611 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002612 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002614 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 set_string_default("term", term);
2616 set_string_default("ttytype", term);
2617
2618 /*
2619 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2620 */
2621 set_termname(T_NAME != NULL ? T_NAME : term);
2622}
2623
2624/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002625 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002627#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002628
2629// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002631
2632static int out_pos = 0; // number of chars in out_buf
2633
2634// Since the maximum number of SGR parameters shown as a normal value range is
2635// 16, the escape sequence length can be 4 * 16 + lead + tail.
2636#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
2638/*
2639 * out_flush(): flush the output buffer
2640 */
2641 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002642out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
2644 int len;
2645
2646 if (out_pos != 0)
2647 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002648 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 len = out_pos;
2650 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002651 ui_write(out_buf, len, FALSE);
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00002652#ifdef FEAT_EVAL
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002653 if (ch_log_output != FALSE)
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002654 {
2655 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002656 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002657# ifdef FEAT_GUI
2658 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2659# endif
2660 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002661 out_buf);
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002662 if (ch_log_output == TRUE)
2663 ch_log_output = FALSE; // only log once
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002664 }
2665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667}
2668
Bram Moolenaara338adc2018-01-31 20:51:47 +01002669/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002670 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2671 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002672 */
2673 void
2674out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002675 int force UNUSED, // when TRUE, update cursor even when not moved
2676 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002677{
2678 mch_disable_flush();
2679 out_flush();
2680 mch_enable_flush();
2681#ifdef FEAT_GUI
2682 if (gui.in_use)
2683 {
2684 gui_update_cursor(force, clear_selection);
2685 gui_may_flush();
2686 }
2687#endif
2688}
2689
2690
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691/*
2692 * Sometimes a byte out of a multi-byte character is written with out_char().
2693 * To avoid flushing half of the character, call this function first.
2694 */
2695 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002696out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
2698 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2699 out_flush();
2700}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
2702#ifdef FEAT_GUI
2703/*
2704 * out_trash(): Throw away the contents of the output buffer
2705 */
2706 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002707out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 out_pos = 0;
2710}
2711#endif
2712
2713/*
2714 * out_char(c): put a byte into the output buffer.
2715 * Flush it if it becomes full.
2716 * This should not be used for outputting text on the screen (use functions
2717 * like msg_puts() and screen_putchar() for that).
2718 */
2719 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002720out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721{
Bram Moolenaard0573012017-10-28 21:11:06 +02002722#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002723 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 out_char('\r');
2725#endif
2726
2727 out_buf[out_pos++] = c;
2728
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002729 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 if (out_pos >= OUT_SIZE || p_wd)
2731 out_flush();
2732}
2733
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002735 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002737 static int
2738out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002740 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742 if (out_pos >= OUT_SIZE)
2743 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002744 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745}
2746
2747/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002748 * A never-padding out_str().
2749 * Use this whenever you don't want to run the string through tputs().
2750 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 * is likely to strip off leading digits, that it mistakes for padding
2752 * information, and "%i", "%d", etc.
2753 * This should only be used for writing terminal codes, not for outputting
2754 * normal text (use functions like msg_puts() and screen_putchar() for that).
2755 */
2756 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002757out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002759 // avoid terminal strings being split up
2760 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002762
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 while (*s)
2764 out_char_nf(*s++);
2765
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002766 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 if (p_wd)
2768 out_flush();
2769}
2770
2771/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002772 * A conditional-flushing out_str, mainly for visualbell.
2773 * Handles a delay internally, because termlib may not respect the delay or do
2774 * it at the wrong time.
2775 * Note: Only for terminal strings.
2776 */
2777 void
2778out_str_cf(char_u *s)
2779{
2780 if (s != NULL && *s)
2781 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002782#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002783 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002784#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002785
2786#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002787 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002788 if (gui.in_use)
2789 {
2790 out_str_nf(s);
2791 return;
2792 }
2793#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002794 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002795 out_flush();
2796#ifdef HAVE_TGETENT
2797 for (p = s; *s; ++s)
2798 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002799 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002800 if (*s == '$' && *(s + 1) == '<')
2801 {
2802 char_u save_c = *s;
2803 int duration = atoi((char *)s + 2);
2804
2805 *s = NUL;
2806 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2807 *s = save_c;
2808 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002809# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002810 // Only sleep here if we can limit this happening in
2811 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002812 p = vim_strchr(s, '>');
2813 if (p == NULL || duration <= 0)
2814 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002815 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002816 p = s;
2817 }
2818 else
2819 {
2820 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002821 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002822 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002823# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002824 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002825 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002826# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002827 break;
2828 }
2829 }
2830 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2831#else
2832 while (*s)
2833 out_char_nf(*s++);
2834#endif
2835
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002836 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002837 if (p_wd)
2838 out_flush();
2839 }
2840}
2841
2842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002844 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 * This should only be used for writing terminal codes, not for outputting
2846 * normal text (use functions like msg_puts() and screen_putchar() for that).
2847 */
2848 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002849out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
2851 if (s != NULL && *s)
2852 {
2853#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002854 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 if (gui.in_use)
2856 {
2857 out_str_nf(s);
2858 return;
2859 }
2860#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002861 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002862 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 out_flush();
2864#ifdef HAVE_TGETENT
2865 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2866#else
2867 while (*s)
2868 out_char_nf(*s++);
2869#endif
2870
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002871 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 if (p_wd)
2873 out_flush();
2874 }
2875}
2876
2877/*
2878 * cursor positioning using termcap parser. (jw)
2879 */
2880 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002881term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
2883 OUT_STR(tgoto((char *)T_CM, col, row));
2884}
2885
2886 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002887term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888{
2889 OUT_STR(tgoto((char *)T_CRI, 0, i));
2890}
2891
2892 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002893term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
2895 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2896}
2897
2898 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002899term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
2901 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2902}
2903
2904#if defined(HAVE_TGETENT) || defined(PROTO)
2905 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002906term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002908 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 if (x < 0)
2910 x = 0;
2911 if (y < 0)
2912 y = 0;
2913 OUT_STR(tgoto((char *)T_CWP, y, x));
2914}
2915
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002916# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2917/*
2918 * Return TRUE if we can request the terminal for a response.
2919 */
2920 static int
2921can_get_termresponse()
2922{
2923 return cur_tmode == TMODE_RAW
2924 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002925# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002926 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002927# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002928 && p_ek;
2929}
2930
Bram Moolenaarafd78262019-05-10 23:10:31 +02002931/*
2932 * Set "status" to STATUS_SENT.
2933 */
2934 static void
2935termrequest_sent(termrequest_T *status)
2936{
2937 status->tr_progress = STATUS_SENT;
2938 status->tr_start = time(NULL);
2939}
2940
2941/*
2942 * Return TRUE if any of the requests are in STATUS_SENT.
2943 */
2944 static int
2945termrequest_any_pending()
2946{
2947 int i;
2948 time_t now = time(NULL);
2949
2950 for (i = 0; all_termrequests[i] != NULL; ++i)
2951 {
2952 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2953 {
2954 if (all_termrequests[i]->tr_start > 0 && now > 0
2955 && all_termrequests[i]->tr_start + 2 < now)
2956 // Sent the request more than 2 seconds ago and didn't get a
2957 // response, assume it failed.
2958 all_termrequests[i]->tr_progress = STATUS_FAIL;
2959 else
2960 return TRUE;
2961 }
2962 }
2963 return FALSE;
2964}
2965
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002966static int winpos_x = -1;
2967static int winpos_y = -1;
2968static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002969
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002970# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002971/*
2972 * Try getting the Vim window position from the terminal.
2973 * Returns OK or FAIL.
2974 */
2975 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002976term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002977{
2978 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002979 int prev_winpos_x = winpos_x;
2980 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002981
2982 if (*T_CGP == NUL || !can_get_termresponse())
2983 return FAIL;
2984 winpos_x = -1;
2985 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002986 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002987 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002988 OUT_STR(T_CGP);
2989 out_flush();
2990
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002991 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002992 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002993 {
2994 (void)vpeekc_nomap();
2995 if (winpos_x >= 0 && winpos_y >= 0)
2996 {
2997 *x = winpos_x;
2998 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002999 return OK;
3000 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01003001 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003002 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003003 // Do not reset "did_request_winpos", if we timed out the response might
3004 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003005
3006 winpos_x = prev_winpos_x;
3007 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02003008 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003009 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003010 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003011 *x = winpos_x;
3012 *y = winpos_y;
3013 return OK;
3014 }
3015
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003016 return FALSE;
3017}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003018# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003019# endif
3020
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003022term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003024 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025}
3026#endif
3027
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003029term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
3031 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003032 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003033 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003035 // Special handling of 16 colors, because termcap can't handle it
3036 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3037 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003039 && ((s[0] == ESC && s[1] == '[')
3040#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3041 || (s[0] == ESC && s[1] == '|')
3042#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003043 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 && s[i] != NUL
3045 && (STRCMP(s + i + 1, "%p1%dm") == 0
3046 || STRCMP(s + i + 1, "%dm") == 0)
3047 && (s[i] == '3' || s[i] == '4'))
3048 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003050 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003052 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003054 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003055#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003056 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003057#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003058 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003059 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3060 : (n >= 16 ? "48;5;" : "10");
3061
3062 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3064 }
3065 else
3066 OUT_STR(tgoto((char *)s, 0, n));
3067}
3068
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003069 void
3070term_fg_color(int n)
3071{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003072 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003073 if (*T_CAF)
3074 term_color(T_CAF, n);
3075 else if (*T_CSF)
3076 term_color(T_CSF, n);
3077}
3078
3079 void
3080term_bg_color(int n)
3081{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003082 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003083 if (*T_CAB)
3084 term_color(T_CAB, n);
3085 else if (*T_CSB)
3086 term_color(T_CSB, n);
3087}
3088
Bram Moolenaare023e882020-05-31 16:42:30 +02003089 void
3090term_ul_color(int n)
3091{
3092 if (*T_CAU)
3093 term_color(T_CAU, n);
3094}
3095
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003096/*
3097 * Return "dark" or "light" depending on the kind of terminal.
3098 * This is just guessing! Recognized are:
3099 * "linux" Linux console
3100 * "screen.linux" Linux console with screen
3101 * "cygwin.*" Cygwin shell
3102 * "putty.*" Putty program
3103 * We also check the COLORFGBG environment variable, which is set by
3104 * rxvt and derivatives. This variable contains either two or three
3105 * values separated by semicolons; we want the last value in either
3106 * case. If this value is 0-6 or 8, our background is dark.
3107 */
3108 char_u *
3109term_bg_default(void)
3110{
3111#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003112 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003113 return (char_u *)"dark";
3114#else
3115 char_u *p;
3116
3117 if (STRCMP(T_NAME, "linux") == 0
3118 || STRCMP(T_NAME, "screen.linux") == 0
3119 || STRNCMP(T_NAME, "cygwin", 6) == 0
3120 || STRNCMP(T_NAME, "putty", 5) == 0
3121 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3122 && (p = vim_strrchr(p, ';')) != NULL
3123 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3124 && p[2] == NUL))
3125 return (char_u *)"dark";
3126 return (char_u *)"light";
3127#endif
3128}
3129
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003130#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003131
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003132#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3133#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3134#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003135
3136 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003137term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003138{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003139#define MAX_COLOR_STR_LEN 100
3140 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003141
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003142 if (*s == NUL)
3143 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003144 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003145 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003146#ifdef FEAT_VTP
Christopher Plewright38804d62022-11-09 23:55:52 +00003147 if (has_vtp_working())
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003148 {
3149 out_flush();
3150 buf[1] = '[';
3151 vtp_printf(buf);
3152 }
3153 else
3154#endif
3155 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003156}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003157
3158 void
3159term_fg_rgb_color(guicolor_T rgb)
3160{
Christopher Plewright38804d62022-11-09 23:55:52 +00003161 if (rgb != INVALCOLOR)
3162 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003163}
3164
3165 void
3166term_bg_rgb_color(guicolor_T rgb)
3167{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003168 if (rgb != INVALCOLOR)
3169 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003170}
Bram Moolenaare023e882020-05-31 16:42:30 +02003171
3172 void
3173term_ul_rgb_color(guicolor_T rgb)
3174{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003175# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003176 // If the user explicitly sets t_8u then use it. Otherwise wait for
3177 // termresponse to be received, which is when t_8u would be set and a
3178 // redraw is needed if it was used.
3179 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003180 write_t_8u_state = MAYBE;
3181 else
3182# endif
3183 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003184}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003185#endif
3186
Bram Moolenaar651fca82021-11-29 20:39:38 +00003187#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188/*
3189 * Generic function to set window title, using t_ts and t_fs.
3190 */
3191 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003192term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003194 MAY_WANT_TO_LOG_THIS;
3195
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003196 // t_ts takes one argument: column in status line
3197 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003199 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 out_flush();
3201}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003202
3203/*
3204 * Tell the terminal to push (save) the title and/or icon, so that it can be
3205 * popped (restored) later.
3206 */
3207 void
3208term_push_title(int which)
3209{
Bram Moolenaar27821262019-05-08 16:41:09 +02003210 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003211 {
3212 OUT_STR(T_CST);
3213 out_flush();
3214 }
3215
Bram Moolenaar27821262019-05-08 16:41:09 +02003216 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003217 {
3218 OUT_STR(T_SSI);
3219 out_flush();
3220 }
3221}
3222
3223/*
3224 * Tell the terminal to pop the title and/or icon.
3225 */
3226 void
3227term_pop_title(int which)
3228{
Bram Moolenaar27821262019-05-08 16:41:09 +02003229 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003230 {
3231 OUT_STR(T_CRT);
3232 out_flush();
3233 }
3234
Bram Moolenaar27821262019-05-08 16:41:09 +02003235 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003236 {
3237 OUT_STR(T_SRI);
3238 out_flush();
3239 }
3240}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241#endif
3242
3243/*
3244 * Make sure we have a valid set or terminal options.
3245 * Replace all entries that are NULL by empty_option
3246 */
3247 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003248ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003250 char_u *env_colors;
3251
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003252 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
3254 /*
3255 * MUST have "cm": cursor motion.
3256 */
3257 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003258 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
3260 /*
3261 * if "cs" defined, use a scroll region, it's faster.
3262 */
3263 if (*T_CS != NUL)
3264 scroll_region = TRUE;
3265 else
3266 scroll_region = FALSE;
3267
3268 if (pairs)
3269 {
3270 /*
3271 * optional pairs
3272 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003273 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 if (*T_ME == NUL)
3275 T_ME = T_MR = T_MD = T_MB = empty_option;
3276 if (*T_SO == NUL || *T_SE == NUL)
3277 T_SO = T_SE = empty_option;
3278 if (*T_US == NUL || *T_UE == NUL)
3279 T_US = T_UE = empty_option;
3280 if (*T_CZH == NUL || *T_CZR == NUL)
3281 T_CZH = T_CZR = empty_option;
3282
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003283 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 if (*T_VE == NUL)
3285 T_VI = empty_option;
3286
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003287 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 if (*T_ME == NUL)
3289 {
3290 T_ME = T_SE;
3291 T_MR = T_SO;
3292 T_MD = T_SO;
3293 }
3294
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003295 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 if (*T_SO == NUL)
3297 {
3298 T_SE = T_ME;
3299 if (*T_MR == NUL)
3300 T_SO = T_MD;
3301 else
3302 T_SO = T_MR;
3303 }
3304
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003305 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 if (*T_CZH == NUL)
3307 {
3308 T_CZR = T_ME;
3309 if (*T_MR == NUL)
3310 T_CZH = T_MD;
3311 else
3312 T_CZH = T_MR;
3313 }
3314
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003315 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 if (*T_CSB == NUL || *T_CSF == NUL)
3317 {
3318 T_CSB = empty_option;
3319 T_CSF = empty_option;
3320 }
3321
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003322 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 if (*T_CAB == NUL || *T_CAF == NUL)
3324 {
3325 T_CAB = empty_option;
3326 T_CAF = empty_option;
3327 }
3328
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003329 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003331 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003333 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 p_wiv = (*T_XS != NUL);
3335 }
3336 need_gather = TRUE;
3337
Bram Moolenaar759d8152020-04-26 16:52:49 +02003338 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3339 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003341#ifdef FEAT_GUI
3342 if (!gui.in_use)
3343#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003344 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003345 env_colors = mch_getenv((char_u *)"COLORS");
3346 if (env_colors != NULL && isdigit(*env_colors))
3347 {
3348 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003349
Bram Moolenaar759d8152020-04-26 16:52:49 +02003350 if (colors != t_colors)
3351 set_color_count(colors);
3352 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354}
3355
3356#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3357 || defined(PROTO)
3358/*
3359 * Represent the given long_u as individual bytes, with the most significant
3360 * byte first, and store them in dst.
3361 */
3362 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003363add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 int i;
3366 int shift;
3367
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003368 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 {
3370 shift = 8 * (sizeof(long_u) - i);
3371 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3372 }
3373}
3374
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375/*
3376 * Interpret the next string of bytes in buf as a long integer, with the most
3377 * significant byte first. Note that it is assumed that buf has been through
3378 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3379 * Puts result in val, and returns the number of bytes read from buf
3380 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3381 * were present.
3382 */
3383 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003384get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
3386 int len;
3387 char_u bytes[sizeof(long_u)];
3388 int i;
3389 int shift;
3390
3391 *val = 0;
3392 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3393 if (len != -1)
3394 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003395 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
3397 shift = 8 * (sizeof(long_u) - 1 - i);
3398 *val += (long_u)bytes[i] << shift;
3399 }
3400 }
3401 return len;
3402}
3403#endif
3404
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405/*
3406 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3407 * that buf has been through inchar(). Returns the actual number of bytes used
3408 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3409 * available.
3410 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003411 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003412get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
3414 int len = 0;
3415 int i;
3416 char_u c;
3417
3418 for (i = 0; i < num_bytes; i++)
3419 {
3420 if ((c = buf[len++]) == NUL)
3421 return -1;
3422 if (c == K_SPECIAL)
3423 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003424 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 return -1;
3426 if (buf[len++] == (int)KS_ZERO)
3427 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003428 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3429 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003430 if (buf[len++] == (int)KE_CSI)
3431 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003433 else if (c == CSI && buf[len] == KS_EXTRA
3434 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003435 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3436 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003437 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 bytes[i] = c;
3439 }
3440 return len;
3441}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
3443/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003444 * Check if the new shell size is valid, correct it if it's too small or way
3445 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 */
3447 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003448check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003450 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003452 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003453
3454 // make sure these values are not invalid
3455 if (cmdline_row >= Rows)
3456 cmdline_row = Rows - 1;
3457 if (msg_row >= Rows)
3458 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003459}
3460
3461/*
3462 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3463 */
3464 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003465limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003466{
3467 if (Columns < MIN_COLUMNS)
3468 Columns = MIN_COLUMNS;
3469 else if (Columns > 10000)
3470 Columns = 10000;
3471 if (Rows > 1000)
3472 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473}
3474
Bram Moolenaar86b68352004-12-27 21:59:20 +00003475/*
3476 * Invoked just before the screen structures are going to be (re)allocated.
3477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003479win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480{
3481 static int old_Rows = 0;
3482 static int old_Columns = 0;
3483
3484 if (old_Rows != Rows || old_Columns != Columns)
3485 ui_new_shellsize();
3486 if (old_Rows != Rows)
3487 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003488 // If 'window' uses the whole screen, keep it using that.
3489 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003490 if (p_window == old_Rows - 1
3491 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003492 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003494 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
3496 if (old_Columns != Columns)
3497 {
3498 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003499 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
3501}
3502
3503/*
3504 * Call this function when the Vim shell has been resized in any way.
3505 * Will obtain the current size and redraw (also when size didn't change).
3506 */
3507 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003508shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509{
3510 set_shellsize(0, 0, FALSE);
3511}
3512
3513/*
3514 * Check if the shell size changed. Handle a resize.
3515 * When the size didn't change, nothing happens.
3516 */
3517 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003518shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520 int old_Rows = Rows;
3521 int old_Columns = Columns;
3522
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003523 if (!exiting
3524#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003525 // Do not get the size when executing a shell command during
3526 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003527 && !gui.starting
3528#endif
3529 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003530 {
3531 (void)ui_get_shellsize();
3532 check_shellsize();
3533 if (old_Rows != Rows || old_Columns != Columns)
3534 shell_resized();
3535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536}
3537
3538/*
3539 * Set size of the Vim shell.
3540 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3541 * window size (this is used for the :win command).
3542 * If 'mustset' is FALSE, we may try to get the real window size and if
3543 * it fails use 'width' and 'height'.
3544 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003545 static void
3546set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003548 if (updating_screen)
3549 // resizing while in update_screen() may cause a crash
3550 return;
3551
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003552 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003553 // buffer (or window) has already been closed and removing a scrollbar
3554 // causes a resize event. Don't resize then, it will happen after entering
3555 // another buffer.
3556 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003557 return;
3558
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003560 out_flush(); // must do this before mch_get_shellsize() for
3561 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562#endif
3563
3564 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3565 {
3566 Rows = height;
3567 Columns = width;
3568 check_shellsize();
3569 ui_set_shellsize(mustset);
3570 }
3571 else
3572 check_shellsize();
3573
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003574 // The window layout used to be adjusted here, but it now happens in
3575 // screenalloc() (also invoked from screenclear()). That is because the
3576 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
Bram Moolenaar24959102022-05-07 20:01:16 +01003578 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3579 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 screenclear();
3581 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003582 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
3584 if (starting != NO_SCREEN)
3585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003587
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 changed_line_abv_curs();
3589 invalidate_botline();
3590
3591 /*
3592 * We only redraw when it's needed:
3593 * - While at the more prompt or executing an external command, don't
3594 * redraw, but position the cursor.
3595 * - While editing the command line, only redraw that.
3596 * - in Ex mode, don't redraw anything.
3597 * - Otherwise, redraw right now, and position the cursor.
3598 * Always need to call update_screen() or screenalloc(), to make
3599 * sure Rows/Columns and the size of ScreenLines[] is correct!
3600 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003601 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3602 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
3604 screenalloc(FALSE);
3605 repeat_message();
3606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 else
3608 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003609 if (curwin->w_p_scb)
3610 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003611 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003612 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003613 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003614 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003615 }
3616 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003617 {
3618 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003619 if (pum_visible())
3620 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003621 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003622 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003623 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003624 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003625 if (redrawing())
3626 setcursor();
3627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003629 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003632}
3633
3634 void
3635set_shellsize(int width, int height, int mustset)
3636{
3637 static int busy = FALSE;
3638 static int do_run = FALSE;
3639
3640 if (width < 0 || height < 0) // just checking...
3641 return;
3642
3643 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3644 {
3645 // postpone the resizing
3646 State = MODE_SETWSIZE;
3647 return;
3648 }
3649
3650 // Avoid recursiveness. This can happen when setting the window size
3651 // causes another window-changed signal or when two SIGWINCH signals come
3652 // very close together. There needs to be another run then after the
3653 // current one is done to pick up the latest size.
3654 do_run = TRUE;
3655 if (busy)
3656 return;
3657
3658 while (do_run)
3659 {
3660 do_run = FALSE;
3661 busy = TRUE;
3662 set_shellsize_inner(width, height, mustset);
3663 busy = FALSE;
3664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665}
3666
3667/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003668 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3669 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3670 */
3671 void
3672out_str_t_TE(void)
3673{
3674 out_str(T_CTE);
3675
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003676 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
Bram Moolenaarc255b782022-11-26 19:16:48 +00003677 // disable modifyOtherKeys, but until Xterm version 377 there is no way to
3678 // detect it's enabled again after the following t_TI. We assume that when
3679 // seenModifyOtherKeys was set before it will still be valid.
3680
3681 // When the modifyOtherKeys level is detected to be 2 we expect t_TE to
3682 // disable it. Remembering that it was detected to be enabled is useful in
3683 // some situations.
3684 // The following t_TI is expected to request the state and then
3685 // modify_otherkeys_state will be set again.
3686 if (modify_otherkeys_state == MOKS_ENABLED
3687 || modify_otherkeys_state == MOKS_DISABLED)
3688 modify_otherkeys_state = MOKS_DISABLED;
3689 else if (modify_otherkeys_state != MOKS_INITIAL)
3690 modify_otherkeys_state = MOKS_AFTER_T_KE;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003691
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003692 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3693 // it. Remembering that it was detected to be enabled is useful in some
3694 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003695 // The following t_TI is expected to request the state and then
3696 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003697 if (kitty_protocol_state == KKPS_ENABLED
3698 || kitty_protocol_state == KKPS_DISABLED)
3699 kitty_protocol_state = KKPS_DISABLED;
3700 else
3701 kitty_protocol_state = KKPS_AFTER_T_KE;
3702}
3703
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003704static int send_t_RK = FALSE;
3705
3706/*
3707 * Output T_TI and setup for what follows.
3708 */
3709 void
3710out_str_t_TI(void)
3711{
3712 out_str(T_CTI);
3713
3714 // Send t_RK when there is no more work to do.
3715 send_t_RK = TRUE;
3716}
3717
3718/*
3719 * If t_TI was recently sent and there is no typeahead or work to do, now send
3720 * t_RK. This is postponed to avoid the response arriving in a shell command
3721 * or after Vim exits.
3722 */
3723 void
3724may_send_t_RK(void)
3725{
3726 if (send_t_RK
3727 && !work_pending()
3728 && !ex_normal_busy
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003729#ifdef FEAT_EVAL
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003730 && !in_feedkeys
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003731#endif
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003732 && !exiting)
3733 {
3734 send_t_RK = FALSE;
3735 out_str(T_CRK);
3736 out_flush();
3737 }
3738}
3739
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3742 * commands and Ex mode).
3743 */
3744 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003745settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
3747#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003748 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (gui.in_use)
3750 return;
3751#endif
3752
3753 if (full_screen)
3754 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003756 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3757 * set the terminal to raw mode, even though we think it already is,
3758 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 * When we think the terminal is normal, don't try to set it to
3760 * normal again, because that causes problems (logout!) on some
3761 * machines.
3762 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003763 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 {
3765#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003766# ifdef FEAT_GUI
3767 if (!gui.in_use && !gui.starting)
3768# endif
3769 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003770 // May need to check for T_CRV response and termcodes, it
3771 // doesn't work in Cooked mode, an external program may get
3772 // them.
3773 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003774 (void)vpeekc_nomap();
3775 check_for_codes_from_term();
3776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003779 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003780
3781 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3782 // Avoid doing this too often, on some terminals the codes are not
3783 // handled properly.
3784 if (termcap_active && tmode != TMODE_SLEEP
3785 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003786 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003787 MAY_WANT_TO_LOG_THIS;
3788
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003789 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003790 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003791 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003792 out_str_t_TE(); // possibly disables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003793 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003794 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003795 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003796 out_str(T_BE); // enable bracketed paste mode (should
3797 // be before mch_settmode().
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003798 out_str_t_TI(); // possibly enables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003799 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003802 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003805 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 out_flush();
3807 }
3808#ifdef FEAT_TERMRESPONSE
3809 may_req_termresponse();
3810#endif
3811 }
3812}
3813
3814 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003815starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
3817 if (full_screen && !termcap_active)
3818 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003819 MAY_WANT_TO_LOG_THIS;
3820
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003821 out_str(T_TI); // start termcap mode
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003822 out_str_t_TI(); // start "raw" mode
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003823 out_str(T_KS); // start "keypad transmit" mode
3824 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003825
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003826#if defined(UNIX) || defined(VMS)
3827 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003828 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003829 out_str(T_FE);
3830#endif
3831
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 out_flush();
3833 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003834 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003836# ifdef FEAT_GUI
3837 if (!gui.in_use && !gui.starting)
3838# endif
3839 {
3840 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003841 // Immediately check for a response. If t_Co changes, we don't
3842 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003843 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003844 check_for_codes_from_term();
3845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846#endif
3847 }
3848}
3849
3850 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003851stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852{
3853 screen_stop_highlight();
3854 reset_cterm_colors();
3855 if (termcap_active)
3856 {
3857#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003858# ifdef FEAT_GUI
3859 if (!gui.in_use && !gui.starting)
3860# endif
3861 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003862 // May need to discard T_CRV, T_U7 or T_RBG response.
3863 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003864 {
3865# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003866 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003867 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003868# endif
3869# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003870 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003871 if (exiting)
3872 tcflush(fileno(stdin), TCIFLUSH);
3873# endif
3874 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003875 // Check for termcodes first, otherwise an external program may
3876 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003877 check_for_codes_from_term();
3878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003880 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003881
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003882#if defined(UNIX) || defined(VMS)
3883 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003884 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003885 out_str(T_FD);
3886#endif
3887
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003888 out_str(T_BD); // disable bracketed paste mode
3889 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 out_flush();
3891 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003892 cursor_on(); // just in case it is still off
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003893 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
3894 // Kitty keyboard protocol
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003895 out_str(T_TE); // stop termcap mode
3896 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 out_flush();
3898 }
3899}
3900
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003901#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902/*
3903 * Request version string (for xterm) when needed.
3904 * Only do this after switching to raw mode, otherwise the result will be
3905 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003906 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003907 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 * Only do this after termcap mode has been started, otherwise the codes for
3909 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003910 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3911 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003912 * On Unix only do it when both output and input are a tty (avoid writing
3913 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 * The result is caught in check_termcode().
3915 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003916 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003917may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003919 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003920 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003921 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 && *T_CRV != NUL)
3923 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003924 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003925 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003927 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003928 // check for the characters now, otherwise they might be eaten by
3929 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 out_flush();
3931 (void)vpeekc_nomap();
3932 }
3933}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003934
Bram Moolenaar9584b312013-03-13 19:29:28 +01003935/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003936 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3937 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003938 * Note that this goes out before T_CRV, so that the result can be used when
3939 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003940 */
3941 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003942check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003943{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003944 int did_send = FALSE;
3945
3946 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3947 return;
3948
Bram Moolenaarafd78262019-05-10 23:10:31 +02003949 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003950 && !option_was_set((char_u *)"ambiwidth"))
3951 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003952 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003953
Bram Moolenaara45551a2020-06-09 15:57:37 +02003954 // Ambiguous width check.
3955 // Check how the terminal treats ambiguous character width (UAX #11).
3956 // First, we move the cursor to (1, 0) and print a test ambiguous
3957 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3958 // the current cursor position. If the terminal treats \u25bd as
3959 // single width, the position is (1, 1), or if it is treated as double
3960 // width, that will be (1, 2). This function has the side effect that
3961 // changes cursor position, so it must be called immediately after
3962 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003963 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003964 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003965 // Do this in the second row. In the first row the returned sequence
3966 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003967 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003968 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003969 out_str(buf);
3970 out_str(T_U7);
3971 termrequest_sent(&u7_status);
3972 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003973 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003974
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003975 // This overwrites a few characters on the screen, a redraw is needed
3976 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003977 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003978 term_windgoto(1, 0);
3979 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003980 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003981 }
3982
Bram Moolenaard1f34e62022-01-07 19:24:20 +00003983 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02003984 {
3985 // 2. Check compatibility with xterm.
3986 // We move the cursor to (2, 0), print a test sequence and then query
3987 // the current cursor position. If the terminal properly handles
3988 // unknown DCS string and CSI sequence with intermediate byte, the test
3989 // sequence is ignored and the cursor does not move. If the terminal
3990 // handles test sequence incorrectly, a garbage string is displayed and
3991 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003992 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003993 LOG_TR(("Sending xterm compatibility test sequence."));
3994 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003995 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003996 term_windgoto(2, 0);
3997 // send the test DCS string.
3998 out_str((char_u *)"\033Pzz\033\\");
3999 // send the test CSI sequence with intermediate byte.
4000 out_str((char_u *)"\033[0%m");
4001 out_str(T_U7);
4002 termrequest_sent(&xcc_status);
4003 out_flush();
4004 did_send = TRUE;
4005
4006 // If the terminal handles test sequence incorrectly, garbage text is
4007 // displayed. Clear them out for now.
4008 screen_stop_highlight();
4009 term_windgoto(2, 0);
4010 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004011 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004012 }
4013
4014 if (did_send)
4015 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004016 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02004017
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004018 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004019 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01004020
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004021 // check for the characters now, otherwise they might be eaten by
4022 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02004023 out_flush();
4024 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01004025 }
4026}
Bram Moolenaar2951b772013-07-03 12:45:31 +02004027
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004028/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02004029 * Similar to requesting the version string: Request the terminal background
4030 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004031 */
4032 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004033may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004034{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004035 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004036 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004037 int didit = FALSE;
4038
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004039# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004040 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004041 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004042 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004043 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004044 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004045 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004046 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004047 didit = TRUE;
4048 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004049# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004050
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004051 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004052 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004053 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004054 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004055 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004056 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004057 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004058 didit = TRUE;
4059 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004060
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004061 if (didit)
4062 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004063 // check for the characters now, otherwise they might be eaten by
4064 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004065 out_flush();
4066 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004067 }
4068 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004069}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004070
Bram Moolenaar2951b772013-07-03 12:45:31 +02004071# ifdef DEBUG_TERMRESPONSE
4072 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02004073log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004074{
4075 static FILE *fd_tr = NULL;
4076 static proftime_T start;
4077 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004078 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004079
4080 if (fd_tr == NULL)
4081 {
4082 fd_tr = fopen("termresponse.log", "w");
4083 profile_start(&start);
4084 }
4085 now = start;
4086 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02004087 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004088 must_redraw == UPD_NOT_VALID ? "NV"
4089 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02004090 va_start(ap, fmt);
4091 vfprintf(fd_tr, fmt, ap);
4092 va_end(ap);
4093 fputc('\n', fd_tr);
4094 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02004095}
4096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097#endif
4098
4099/*
4100 * Return TRUE when saving and restoring the screen.
4101 */
4102 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004103swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
4105 return (full_screen && *T_TI != NUL);
4106}
4107
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108/*
4109 * By outputting the 'cursor very visible' termcap code, for some windowed
4110 * terminals this makes the screen scrolled to the correct position.
4111 * Used when starting Vim or returning from a shell.
4112 */
4113 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004114scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004116 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004118 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004120 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004121 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123}
4124
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004125// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126static int cursor_is_off = FALSE;
4127
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004128// True if cursor is not visible due to an ongoing cursor-less sleep
4129static int cursor_is_asleep = FALSE;
4130
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004132 * Enable the cursor without checking if it's already enabled.
4133 */
4134 void
4135cursor_on_force(void)
4136{
4137 out_str(T_VE);
4138 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004139 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004140}
4141
4142/*
4143 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 */
4145 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004146cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004148 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004149 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150}
4151
4152/*
4153 * Disable the cursor.
4154 */
4155 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004156cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004158 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004160 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 cursor_is_off = TRUE;
4162 }
4163}
4164
Dominique Pelle748b3082022-01-08 12:41:16 +00004165#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004166/*
4167 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4168 */
4169 int
4170cursor_is_sleeping(void)
4171{
4172 return cursor_is_asleep;
4173}
Dominique Pelle748b3082022-01-08 12:41:16 +00004174#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004175
4176/*
4177 * Disable the cursor and mark it disabled by cursor-less sleep
4178 */
4179 void
4180cursor_sleep(void)
4181{
4182 cursor_is_asleep = TRUE;
4183 cursor_off();
4184}
4185
4186/*
4187 * Enable the cursor and mark it not disabled by cursor-less sleep
4188 */
4189 void
4190cursor_unsleep(void)
4191{
4192 cursor_is_asleep = FALSE;
4193 cursor_on();
4194}
4195
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004196#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004198 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004199 */
4200 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004201term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004202{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004203 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004204 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004205
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004206 // Only do something when redrawing the screen and we can restore the
4207 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004208 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004209 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004210# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004211 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004212 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004213 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004214# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004215 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004216 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004217
Bram Moolenaar24959102022-05-07 20:01:16 +01004218 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004219 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004220 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004221 {
4222 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004223 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004224 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004225 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004226 if (*p != NUL)
4227 {
4228 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004229 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004230 }
4231 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004232 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004233 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004234 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004235 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004236 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004237 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004238 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004239 }
4240 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004241 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004242 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004243 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004244 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004245 }
4246}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004247
4248# if defined(FEAT_TERMINAL) || defined(PROTO)
4249 void
4250term_cursor_color(char_u *color)
4251{
4252 if (*T_CSC != NUL)
4253 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004254 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004255 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004256 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004257 out_flush();
4258 }
4259}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004260# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004261
Bram Moolenaar4db25542017-08-28 22:43:05 +02004262 int
4263blink_state_is_inverted()
4264{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004265#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004266 return rbm_status.tr_progress == STATUS_GOT
4267 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004268 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004269#else
4270 return FALSE;
4271#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004272}
4273
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004274/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004275 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004276 */
4277 void
4278term_cursor_shape(int shape, int blink)
4279{
4280 if (*T_CSH != NUL)
4281 {
4282 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4283 out_flush();
4284 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004285 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004286 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004287 int do_blink = blink;
4288
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004289 // t_SH is empty: try setting just the blink state.
4290 // The blink flags are XORed together, if the initial blinking from
4291 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004292 if (blink_state_is_inverted())
4293 do_blink = !blink;
4294
4295 if (do_blink && *T_VS != NUL)
4296 {
4297 out_str(T_VS);
4298 out_flush();
4299 }
4300 else if (!do_blink && *T_CVS != NUL)
4301 {
4302 out_str(T_CVS);
4303 out_flush();
4304 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004305 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004306}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004307#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004308
4309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 * Set scrolling region for window 'wp'.
4311 * The region starts 'off' lines from the start of the window.
4312 * Also set the vertical scroll region for a vertically split window. Always
4313 * the full width of the window, excluding the vertical separator.
4314 */
4315 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004316scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317{
4318 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4319 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004321 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4322 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004323 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324}
4325
4326/*
4327 * Reset scrolling region to the whole screen.
4328 */
4329 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004330scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331{
4332 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 if (*T_CSV != NUL)
4334 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004335 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336}
4337
4338
4339/*
4340 * List of terminal codes that are currently recognized.
4341 */
4342
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004343static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004345 char_u name[2]; // termcap name of entry
4346 char_u *code; // terminal code (in allocated memory)
4347 int len; // STRLEN(code)
4348 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349} *termcodes = NULL;
4350
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004351static int tc_max_len = 0; // number of entries that termcodes[] can hold
4352static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004354static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004355
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004357clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358{
4359 while (tc_len > 0)
4360 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004361 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 tc_max_len = 0;
4363
4364#ifdef HAVE_TGETENT
4365 BC = (char *)empty_option;
4366 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004367 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 ospeed = 0;
4369#endif
4370
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004371 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372}
4373
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004374#define ATC_FROM_TERM 55
4375
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376/*
4377 * Add a new entry to the list of terminal codes.
4378 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004379 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4380 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 */
4382 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004383add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
4385 struct termcode *new_tc;
4386 int i, j;
4387 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004388 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
4390 if (string == NULL || *string == NUL)
4391 {
4392 del_termcode(name);
4393 return;
4394 }
4395
Bram Moolenaar4f974752019-02-17 17:44:42 +01004396#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004397 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004398#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004399# ifdef VIMDLL
4400 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004401 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004402 else
4403# endif
4404 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 if (s == NULL)
4407 return;
4408
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004409 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004410 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004412 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 s[0] = term_7to8bit(string);
4414 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004415
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004416#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4417# ifdef VIMDLL
4418 if (!gui.in_use)
4419# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004420 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004421 if (s[0] == K_NUL)
4422 {
4423 STRMOVE(s + 1, s);
4424 s[1] = 3;
4425 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004426 }
4427#endif
4428
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004429 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004431 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
4433 /*
4434 * need to make space for more entries
4435 */
4436 if (tc_len == tc_max_len)
4437 {
4438 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004439 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 if (new_tc == NULL)
4441 {
4442 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004443 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 return;
4445 }
4446 for (i = 0; i < tc_len; ++i)
4447 new_tc[i] = termcodes[i];
4448 vim_free(termcodes);
4449 termcodes = new_tc;
4450 }
4451
4452 /*
4453 * Look for existing entry with the same name, it is replaced.
4454 * Look for an existing entry that is alphabetical higher, the new entry
4455 * is inserted in front of it.
4456 */
4457 for (i = 0; i < tc_len; ++i)
4458 {
4459 if (termcodes[i].name[0] < name[0])
4460 continue;
4461 if (termcodes[i].name[0] == name[0])
4462 {
4463 if (termcodes[i].name[1] < name[1])
4464 continue;
4465 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004466 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 */
4468 if (termcodes[i].name[1] == name[1])
4469 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004470 if (flags == ATC_FROM_TERM && (j = termcode_star(
4471 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004472 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004473 // Don't replace ESC[123;*X or ESC O*X with another when
4474 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004475 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004476 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004477 && s[len - 1]
4478 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004479 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004480 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004481 vim_free(s);
4482 return;
4483 }
4484 }
4485 else
4486 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004487 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004488 vim_free(termcodes[i].code);
4489 --tc_len;
4490 break;
4491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 }
4493 }
4494 /*
4495 * Found alphabetical larger entry, move rest to insert new entry
4496 */
4497 for (j = tc_len; j > i; --j)
4498 termcodes[j] = termcodes[j - 1];
4499 break;
4500 }
4501
4502 termcodes[i].name[0] = name[0];
4503 termcodes[i].name[1] = name[1];
4504 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004505 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004506
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004507 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4508 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004509 termcodes[i].modlen = 0;
4510 j = termcode_star(s, len);
4511 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004512 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004513 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004514 // For "CSI[@;X" the "@" is not included in "modlen".
4515 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4516 --termcodes[i].modlen;
4517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 ++tc_len;
4519}
4520
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004521/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004522 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004523 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004524 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004525 */
4526 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004527termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004528{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004529 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004530 if (len >= 3 && code[len - 2] == '*')
4531 {
4532 if (len >= 5 && code[len - 3] == ';')
4533 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004534 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004535 return 1;
4536 }
4537 return 0;
4538}
4539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004541find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542{
4543 int i;
4544
4545 for (i = 0; i < tc_len; ++i)
4546 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4547 return termcodes[i].code;
4548 return NULL;
4549}
4550
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004552get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553{
4554 if (i >= tc_len)
4555 return NULL;
4556 return &termcodes[i].name[0];
4557}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004559/*
4560 * Returns the length of the terminal code at index 'idx'.
4561 */
4562 int
4563get_termcode_len(int idx)
4564{
4565 return termcodes[idx].len;
4566}
4567
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004568 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004569del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570{
4571 int i;
4572
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004573 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 return;
4575
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004576 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
4578 for (i = 0; i < tc_len; ++i)
4579 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4580 {
4581 del_termcode_idx(i);
4582 return;
4583 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004584 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585}
4586
4587 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004588del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589{
4590 int i;
4591
4592 vim_free(termcodes[idx].code);
4593 --tc_len;
4594 for (i = idx; i < tc_len; ++i)
4595 termcodes[i] = termcodes[i + 1];
4596}
4597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598/*
4599 * Called when detected that the terminal sends 8-bit codes.
4600 * Convert all 7-bit codes to their 8-bit equivalent.
4601 */
4602 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004603switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604{
4605 int i;
4606 int c;
4607
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004608 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 if (!term_is_8bit(T_NAME))
4610 {
4611 for (i = 0; i < tc_len; ++i)
4612 {
4613 c = term_7to8bit(termcodes[i].code);
4614 if (c != 0)
4615 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004616 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 termcodes[i].code[0] = c;
4618 }
4619 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004620 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 }
4622 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004623 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
4626#ifdef CHECK_DOUBLE_CLICK
4627static linenr_T orig_topline = 0;
4628# ifdef FEAT_DIFF
4629static int orig_topfill = 0;
4630# endif
4631#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004632#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004634 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 * "orig_topline" is used to avoid detecting a double-click when the window
4636 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4637 */
4638/*
4639 * Set orig_topline. Used when jumping to another window, so that a double
4640 * click still works.
4641 */
4642 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004643set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
4645 orig_topline = wp->w_topline;
4646# ifdef FEAT_DIFF
4647 orig_topfill = wp->w_topfill;
4648# endif
4649}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004650
4651/*
4652 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4653 * topline and topfill.
4654 */
4655 int
4656is_mouse_topline(win_T *wp)
4657{
4658 return orig_topline == wp->w_topline
4659#ifdef FEAT_DIFF
4660 && orig_topfill == wp->w_topfill
4661#endif
4662 ;
4663}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664#endif
4665
4666/*
zeertzjqfc78a032022-04-26 22:11:38 +01004667 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4668 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4669 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004670 * Remove "slen" bytes.
4671 * Returns FAIL for error.
4672 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004673 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004674put_string_in_typebuf(
4675 int offset,
4676 int slen,
4677 char_u *string,
4678 int new_slen,
4679 char_u *buf,
4680 int bufsize,
4681 int *buflen)
4682{
4683 int extra = new_slen - slen;
4684
4685 string[new_slen] = NUL;
4686 if (buf == NULL)
4687 {
4688 if (extra < 0)
4689 // remove matched chars, taking care of noremap
4690 del_typebuf(-extra, offset);
4691 else if (extra > 0)
4692 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004693 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4694 == FAIL)
4695 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004696
4697 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4698 // typebuf.tb_buf[]!
4699 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4700 (size_t)new_slen);
4701 }
4702 else
4703 {
4704 if (extra < 0)
4705 // remove matched characters
4706 mch_memmove(buf + offset, buf + offset - extra,
4707 (size_t)(*buflen + offset + extra));
4708 else if (extra > 0)
4709 {
4710 // Insert the extra space we need. If there is insufficient
4711 // space return -1.
4712 if (*buflen + extra + new_slen >= bufsize)
4713 return FAIL;
4714 mch_memmove(buf + offset + extra, buf + offset,
4715 (size_t)(*buflen - offset));
4716 }
4717 mch_memmove(buf + offset, string, (size_t)new_slen);
4718 *buflen = *buflen + extra + new_slen;
4719 }
4720 return OK;
4721}
4722
4723/*
4724 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4725 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004726 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004727decode_modifiers(int n)
4728{
4729 int code = n - 1;
4730 int modifiers = 0;
4731
4732 if (code & 1)
4733 modifiers |= MOD_MASK_SHIFT;
4734 if (code & 2)
4735 modifiers |= MOD_MASK_ALT;
4736 if (code & 4)
4737 modifiers |= MOD_MASK_CTRL;
4738 if (code & 8)
4739 modifiers |= MOD_MASK_META;
Bram Moolenaar064fd672022-11-29 18:32:32 +00004740 // Any further modifiers are silently dropped.
4741
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004742 return modifiers;
4743}
4744
4745 static int
4746modifiers2keycode(int modifiers, int *key, char_u *string)
4747{
4748 int new_slen = 0;
4749
4750 if (modifiers != 0)
4751 {
4752 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004753 // make mappings work. This may result in a special key, such as
4754 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004755 *key = simplify_key(*key, &modifiers);
4756 if (modifiers != 0)
4757 {
4758 string[new_slen++] = K_SPECIAL;
4759 string[new_slen++] = (int)KS_MODIFIER;
4760 string[new_slen++] = modifiers;
4761 }
4762 }
4763 return new_slen;
4764}
4765
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004766/*
4767 * Handle a cursor position report.
4768 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004769 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004770handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004771{
4772 if (arg[0] == 2 && arg[1] >= 2)
4773 {
4774 char *aw = NULL;
4775
4776 LOG_TR(("Received U7 status: %s", tp));
4777 u7_status.tr_progress = STATUS_GOT;
4778 did_cursorhold = TRUE;
4779 if (arg[1] == 2)
4780 aw = "single";
4781 else if (arg[1] == 3)
4782 aw = "double";
4783 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4784 {
4785 // Setting the option causes a screen redraw. Do
4786 // that right away if possible, keeping any
4787 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004788 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004789#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004790 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004791 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004792
4793 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4794 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004795#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004796 redraw_asap(UPD_CLEAR);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004797#endif
4798#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004799 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004800#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004801 }
4802 }
4803 else if (arg[0] == 3)
4804 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004805 int value;
4806
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004807 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004808 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004809
4810 // Third row: xterm compatibility test.
4811 // If the cursor is on the first column then the terminal can handle
4812 // the request for cursor style and blinking.
4813 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4814 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4815 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004816 }
4817}
4818
4819/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004820 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004821 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004822 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004823 */
4824 static void
4825handle_version_response(int first, int *arg, int argc, char_u *tp)
4826{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004827 // The xterm version. It is set to zero when it can't be an actual xterm
4828 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004829 int version = arg[1];
4830
4831 LOG_TR(("Received CRV response: %s", tp));
4832 crv_status.tr_progress = STATUS_GOT;
4833 did_cursorhold = TRUE;
4834
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004835 // Reset terminal properties that are set based on the termresponse.
4836 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004837 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004838 init_term_props(
4839#ifdef FEAT_EVAL
4840 reset_term_props_on_termresponse
4841#else
4842 FALSE
4843#endif
4844 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004845
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004846 // If this code starts with CSI, you can bet that the
4847 // terminal uses 8-bit codes.
4848 if (tp[0] == CSI)
4849 switch_to_8bit();
4850
4851 // Screen sends 40500.
4852 // rxvt sends its version number: "20703" is 2.7.3.
4853 // Ignore it for when the user has set 'term' to xterm,
4854 // even though it's an rxvt.
4855 if (version > 20000)
4856 version = 0;
4857
zeertzjqfc78a032022-04-26 22:11:38 +01004858 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004859 if (first == '>' && argc == 3)
4860 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004861 // mintty 2.9.5 sends 77;20905;0c.
4862 // (77 is ASCII 'M' for mintty.)
4863 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004864 {
4865 // mintty can do SGR mouse reporting
4866 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4867 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004868
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004869#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004870 // If xterm version >= 141 try to get termcap codes. For other
4871 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004872 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004873 {
4874 LOG_TR(("Enable checking for XT codes"));
4875 check_for_codes = TRUE;
4876 need_gather = TRUE;
4877 req_codes_from_term();
4878 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004879#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004880
4881 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01004882 // Konsole sends 0;115;0 and works the same way
4883 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004884 {
4885 // If run from Vim $COLORS is set to the number of
4886 // colors the terminal supports. Otherwise assume
4887 // 256, libvterm supports even more.
4888 if (mch_getenv((char_u *)"COLORS") == NULL)
4889 may_adjust_color_count(256);
4890 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004891 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004892 }
4893
4894 if (version == 95)
4895 {
4896 // Mac Terminal.app sends 1;95;0
4897 if (arg[0] == 1 && arg[2] == 0)
4898 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004899 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4900 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004901 }
4902 // iTerm2 sends 0;95;0
4903 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004904 {
4905 // iTerm2 can do SGR mouse reporting
4906 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4907 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004908 // old iTerm2 sends 0;95;
4909 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004910 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004911 }
4912
4913 // screen sends 83;40500;0 83 is 'S' in ASCII.
4914 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004915 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004916 // screen supports SGR mouse codes since 4.7.0
4917 if (arg[1] >= 40700)
4918 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4919 else
4920 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4921 }
4922
4923 // If no recognized terminal has set mouse behavior, assume xterm.
4924 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4925 {
4926 // Xterm version 277 supports SGR.
4927 // Xterm version >= 95 supports mouse dragging.
4928 if (version >= 277)
4929 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004930 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004931 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004932 }
4933
4934 // Detect terminals that set $TERM to something like
4935 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004936 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004937 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004938 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004939 // xfce4-terminal sends 1;2802;0.
4940 // screen sends 83;40500;0
4941 // Assuming any version number over 2500 is not an
4942 // xterm (without the limit for rxvt and screen).
4943 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004944 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004945
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004946 else if (version == 136 && arg[2] == 0)
4947 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004948 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004949
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004950 // PuTTY sends 0;136;0
4951 if (arg[0] == 0)
4952 {
4953 // supports sgr-like mouse reporting.
4954 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4955 }
4956 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004957 }
4958
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004959 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
4960 // commented out.
4961 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
4962 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004963
Bram Moolenaar1573e732022-11-16 20:33:21 +00004964 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01004965 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
4966 {
4967 term_props[TPR_KITTY].tpr_status = TPR_YES;
4968 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
4969 }
4970
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004971 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004972 // 30600/40500 is a version number of GNU screen. DA2 support is added
4973 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4974 // compatibility checking does not detect GNU screen.
4975 if (arg[0] == 83 && arg[1] >= 30600)
4976 {
4977 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4978 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4979 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004980
4981 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004982 // 95, so assume anything below 95 is not xterm and hopefully supports
4983 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004984 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004985 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004986
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004987 // Getting the cursor style is only supported properly by xterm since
4988 // version 279 (otherwise it returns 0x18).
4989 if (version < 279)
4990 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4991
4992 /*
4993 * Take action on the detected properties.
4994 */
4995
4996 // Unless the underline RGB color is expected to work, disable "t_8u".
4997 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004998 // This may cause some flicker. Alternative would be to set "t_8u"
4999 // here if the terminal is expected to support it, but that might
5000 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01005001 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
5002 && *T_8U != NUL
5003 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005004 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02005005 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
5006 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005007 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005008#ifdef FEAT_TERMRESPONSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01005009 if (*T_8U != NUL && write_t_8u_state == MAYBE)
5010 // Did skip writing t_8u, a complete redraw is needed.
5011 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01005012 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005013#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005014
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005015 // Only set 'ttymouse' automatically if it was not set
5016 // by the user already.
5017 if (!option_was_set((char_u *)"ttym")
5018 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
5019 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
5020 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005021 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005022 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
5023 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
5024 }
5025
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005026#ifdef FEAT_TERMRESPONSE
5027 int need_flush = FALSE;
5028
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005029 // Only request the cursor style if t_SH and t_RS are
5030 // set. Only supported properly by xterm since version
5031 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005032 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005033 // Not for Terminal.app, it can't handle t_RS, it
5034 // echoes the characters to the screen.
5035 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005036 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005037 && *T_CSH != NUL
5038 && *T_CRS != NUL)
5039 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005040 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005041 LOG_TR(("Sending cursor style request"));
5042 out_str(T_CRS);
5043 termrequest_sent(&rcs_status);
5044 need_flush = TRUE;
5045 }
5046
5047 // Only request the cursor blink mode if t_RC set. Not
5048 // for Gnome terminal, it can't handle t_RC, it
5049 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005050 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005051 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005052 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005053 && *T_CRC != NUL)
5054 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005055 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005056 LOG_TR(("Sending cursor blink mode request"));
5057 out_str(T_CRC);
5058 termrequest_sent(&rbm_status);
5059 need_flush = TRUE;
5060 }
5061
5062 if (need_flush)
5063 out_flush();
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005064#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005065 }
5066}
5067
5068/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005069 * Add "key" to "buf" and return the number of bytes used.
5070 * Handles special keys and multi-byte characters.
5071 */
5072 static int
5073add_key_to_buf(int key, char_u *buf)
5074{
5075 int idx = 0;
5076
5077 if (IS_SPECIAL(key))
5078 {
5079 buf[idx++] = K_SPECIAL;
5080 buf[idx++] = KEY2TERMCAP0(key);
5081 buf[idx++] = KEY2TERMCAP1(key);
5082 }
5083 else if (has_mbyte)
5084 idx += (*mb_char2bytes)(key, buf + idx);
5085 else
5086 buf[idx++] = key;
5087 return idx;
5088}
5089
5090/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005091 * Handle a sequence with key and modifier, one of:
5092 * {lead}27;{modifier};{key}~
5093 * {lead}{key};{modifier}u
5094 * Returns the difference in length.
5095 */
5096 static int
5097handle_key_with_modifier(
5098 int *arg,
5099 int trail,
5100 int csi_len,
5101 int offset,
5102 char_u *buf,
5103 int bufsize,
5104 int *buflen)
5105{
5106 int key;
5107 int modifiers;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005108 char_u string[MAX_KEY_CODE_LEN + 1];
5109
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005110 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5111 // it for terminals using the kitty keyboard protocol. Xterm sends
5112 // the form ending in "u" when the formatOtherKeys resource is set. We do
5113 // not support this.
5114 //
5115 // Do not set seenModifyOtherKeys if there was a positive response at any
5116 // time from requesting the kitty keyboard protocol state, these are not
5117 // expected to support modifyOtherKeys level 2.
5118 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005119 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5120 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005121 if (trail != 'u'
5122 && (kitty_protocol_state == KKPS_INITIAL
5123 || kitty_protocol_state == KKPS_OFF
5124 || kitty_protocol_state == KKPS_AFTER_T_KE)
5125 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005126 seenModifyOtherKeys = TRUE;
5127
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005128 if (trail == 'u')
5129 key = arg[0];
5130 else
5131 key = arg[2];
5132
5133 modifiers = decode_modifiers(arg[1]);
5134
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02005135 // Some keys need adjustment when the Ctrl modifier is used.
5136 key = may_adjust_key_for_ctrl(modifiers, key);
5137
Bram Moolenaaref6746f2020-06-20 14:43:23 +02005138 // May remove the shift modifier if it's already included in the key.
5139 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005140
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005141 // insert modifiers with KS_MODIFIER
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005142 int new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005143
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005144 // add the bytes for the key
5145 new_slen += add_key_to_buf(key, string + new_slen);
5146
5147 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5148 buf, bufsize, buflen) == FAIL)
5149 return -1;
5150 return new_slen - csi_len + offset;
5151}
5152
5153/*
5154 * Handle a sequence with key without a modifier:
5155 * {lead}{key}u
5156 * Returns the difference in length.
5157 */
5158 static int
5159handle_key_without_modifier(
5160 int *arg,
5161 int csi_len,
5162 int offset,
5163 char_u *buf,
5164 int bufsize,
5165 int *buflen)
5166{
5167 char_u string[MAX_KEY_CODE_LEN + 1];
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00005168 int new_slen;
5169
5170 if (arg[0] == ESC)
5171 {
5172 // Putting Esc in the buffer creates ambiguity, it can be the start of
5173 // an escape sequence. Use K_ESC to avoid that.
5174 string[0] = K_SPECIAL;
5175 string[1] = KS_EXTRA;
5176 string[2] = KE_ESC;
5177 new_slen = 3;
5178 }
5179 else
5180 new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005181
5182 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5183 buf, bufsize, buflen) == FAIL)
5184 return -1;
5185 return new_slen - csi_len + offset;
5186}
5187
5188/*
5189 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005190 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005191 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005192 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5193 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005194 * - Cursor position report: {lead}{row};{col}R
5195 * The final byte must be 'R'. It is used for checking the
5196 * ambiguous-width character state.
5197 *
5198 * - window position reply: {lead}3;{x};{y}t
5199 *
5200 * - key with modifiers when modifyOtherKeys is enabled:
5201 * {lead}27;{modifier};{key}~
5202 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005203 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005204 * Return 0 for no match, -1 for partial match, > 0 for full match.
5205 */
5206 static int
5207handle_csi(
5208 char_u *tp,
5209 int len,
5210 char_u *argp,
5211 int offset,
5212 char_u *buf,
5213 int bufsize,
5214 int *buflen,
5215 char_u *key_name,
5216 int *slen)
5217{
5218 int first = -1; // optional char right after {lead}
5219 int trail; // char that ends CSI sequence
5220 int arg[3] = {-1, -1, -1}; // argument numbers
5221 int argc; // number of arguments
5222 char_u *ap = argp;
5223 int csi_len;
5224
5225 // Check for non-digit after CSI.
5226 if (!VIM_ISDIGIT(*ap))
5227 first = *ap++;
5228
5229 // Find up to three argument numbers.
5230 for (argc = 0; argc < 3; )
5231 {
5232 if (ap >= tp + len)
5233 return -1;
5234 if (*ap == ';')
5235 arg[argc++] = -1; // omitted number
5236 else if (VIM_ISDIGIT(*ap))
5237 {
5238 arg[argc] = 0;
5239 for (;;)
5240 {
5241 if (ap >= tp + len)
5242 return -1;
5243 if (!VIM_ISDIGIT(*ap))
5244 break;
5245 arg[argc] = arg[argc] * 10 + (*ap - '0');
5246 ++ap;
5247 }
5248 ++argc;
5249 }
5250 if (*ap == ';')
5251 ++ap;
5252 else
5253 break;
5254 }
5255
5256 // mrxvt has been reported to have "+" in the version. Assume
5257 // the escape sequence ends with a letter or one of "{|}~".
5258 while (ap < tp + len
5259 && !(*ap >= '{' && *ap <= '~')
5260 && !ASCII_ISALPHA(*ap))
5261 ++ap;
5262 if (ap >= tp + len)
5263 return -1;
5264 trail = *ap;
5265 csi_len = (int)(ap - tp) + 1;
5266
Bram Moolenaarc255b782022-11-26 19:16:48 +00005267 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5268 // modifyOtherKeys level. Drop similar responses.
5269 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5270 {
5271 if (arg[0] == 4 && argc == 2)
5272 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5273
5274 key_name[0] = (int)KS_EXTRA;
5275 key_name[1] = (int)KE_IGNORE;
5276 *slen = csi_len;
5277 }
5278
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005279 // Cursor position report: Eat it when there are 2 arguments
5280 // and it ends in 'R'. Also when u7_status is not "sent", it
5281 // may be from a previous Vim that just exited. But not for
5282 // <S-F3>, it sends something similar, check for row and column
5283 // to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005284 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005285 {
5286 handle_u7_response(arg, tp, csi_len);
5287
5288 key_name[0] = (int)KS_EXTRA;
5289 key_name[1] = (int)KE_IGNORE;
5290 *slen = csi_len;
5291 }
5292
5293 // Version string: Eat it when there is at least one digit and
5294 // it ends in 'c'
5295 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5296 {
5297 handle_version_response(first, arg, argc, tp);
5298
5299 *slen = csi_len;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005300#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005301 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005302#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005303 apply_autocmds(EVENT_TERMRESPONSE,
5304 NULL, NULL, FALSE, curbuf);
5305 key_name[0] = (int)KS_EXTRA;
5306 key_name[1] = (int)KE_IGNORE;
5307 }
5308
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005309#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005310 // Check blinking cursor from xterm:
5311 // {lead}?12;1$y set
5312 // {lead}?12;2$y not set
5313 //
5314 // {lead} can be <Esc>[ or CSI
5315 else if (rbm_status.tr_progress == STATUS_SENT
5316 && first == '?'
5317 && ap == argp + 6
5318 && arg[0] == 12
5319 && ap[-1] == '$'
5320 && trail == 'y')
5321 {
5322 initial_cursor_blink = (arg[1] == '1');
5323 rbm_status.tr_progress = STATUS_GOT;
5324 LOG_TR(("Received cursor blinking mode response: %s", tp));
5325 key_name[0] = (int)KS_EXTRA;
5326 key_name[1] = (int)KE_IGNORE;
5327 *slen = csi_len;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005328# ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005329 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005330# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005331 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005332#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005333
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005334 // Kitty keyboard protocol status response: CSI ? flags u
5335 else if (first == '?' && argc == 1 && trail == 'u')
5336 {
5337 // The protocol has various "progressive enhancement flags" values, but
5338 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005339 if (arg[0] == '0')
5340 {
5341 kitty_protocol_state = KKPS_OFF;
5342 }
5343 else
5344 {
5345 kitty_protocol_state = KKPS_ENABLED;
5346
5347 // Reset seenModifyOtherKeys just in case some key combination has
5348 // been seen that set it before we get the status response.
5349 seenModifyOtherKeys = FALSE;
5350 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005351
5352 key_name[0] = (int)KS_EXTRA;
5353 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005354 *slen = csi_len;
5355 }
5356
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005357#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005358 // Check for a window position response from the terminal:
5359 // {lead}3;{x};{y}t
5360 else if (did_request_winpos && argc == 3 && arg[0] == 3
5361 && trail == 't')
5362 {
5363 winpos_x = arg[1];
5364 winpos_y = arg[2];
5365 // got finished code: consume it
5366 key_name[0] = (int)KS_EXTRA;
5367 key_name[1] = (int)KE_IGNORE;
5368 *slen = csi_len;
5369
5370 if (--did_request_winpos <= 0)
5371 winpos_status.tr_progress = STATUS_GOT;
5372 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005373#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005374
5375 // Key with modifier:
5376 // {lead}27;{modifier};{key}~
5377 // {lead}{key};{modifier}u
Bram Moolenaar064fd672022-11-29 18:32:32 +00005378 // Even though we only handle four modifiers and the {modifier} value
5379 // should be 16 or lower, we accept all modifier values to avoid the raw
5380 // sequence to be passed through.
5381 else if ((arg[0] == 27 && argc == 3 && trail == '~')
Bram Moolenaar7609c882022-10-19 20:07:09 +01005382 || (argc == 2 && trail == 'u'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005383 {
5384 return len + handle_key_with_modifier(arg, trail,
Bram Moolenaar064fd672022-11-29 18:32:32 +00005385 csi_len, offset, buf, bufsize, buflen);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005386 }
5387
Christopher Plewright03193062022-11-22 12:58:27 +00005388 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005389 // {lead}{key}u
5390 else if (argc == 1 && trail == 'u')
5391 {
5392 return len + handle_key_without_modifier(arg,
5393 csi_len, offset, buf, bufsize, buflen);
5394 }
5395
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005396 // else: Unknown CSI sequence. We could drop it, but then the
5397 // user can't create a map for it.
5398 return 0;
5399}
5400
5401/*
5402 * Handle an OSC sequence, fore/background color response from the terminal:
5403 *
5404 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5405 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5406 *
5407 * {code} is 10 for foreground, 11 for background
5408 * {lead} can be <Esc>] or OSC
5409 * {tail} can be '\007', <Esc>\ or STERM.
5410 *
5411 * Consume any code that starts with "{lead}11;", it's also
5412 * possible that "rgba" is following.
5413 */
5414 static int
5415handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5416{
5417 int i, j;
5418
5419 j = 1 + (tp[0] == ESC);
5420 if (len >= j + 3 && (argp[0] != '1'
5421 || (argp[1] != '1' && argp[1] != '0')
5422 || argp[2] != ';'))
5423 i = 0; // no match
5424 else
5425 for (i = j; i < len; ++i)
5426 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5427 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5428 {
5429 int is_bg = argp[1] == '1';
5430 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5431 && tp[j + 16] == '/';
5432
5433 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5434 && (is_4digit
5435 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5436 {
5437 char_u *tp_r = tp + j + 7;
5438 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5439 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005440#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005441 int rval, gval, bval;
5442
5443 rval = hexhex2nr(tp_r);
5444 gval = hexhex2nr(tp_b);
5445 bval = hexhex2nr(tp_g);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005446#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005447 if (is_bg)
5448 {
5449 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5450 *tp_b) ? "light" : "dark";
5451
5452 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005453#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005454 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005455# ifdef FEAT_TERMINAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005456 bg_r = rval;
5457 bg_g = gval;
5458 bg_b = bval;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005459# endif
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005460#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005461 if (!option_was_set((char_u *)"bg")
5462 && STRCMP(p_bg, new_bg_val) != 0)
5463 {
5464 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005465 set_option_value_give_err((char_u *)"bg",
5466 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005467 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005468 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005469 }
5470 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005471#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005472 else
5473 {
5474 LOG_TR(("Received RFG response: %s", tp));
5475 rfg_status.tr_progress = STATUS_GOT;
5476 fg_r = rval;
5477 fg_g = gval;
5478 fg_b = bval;
5479 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005480#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005481 }
5482
5483 // got finished code: consume it
5484 key_name[0] = (int)KS_EXTRA;
5485 key_name[1] = (int)KE_IGNORE;
5486 *slen = i + 1 + (tp[i] == ESC);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005487#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005488 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5489 : VV_TERMRFGRESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005490#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005491 break;
5492 }
5493 if (i == len)
5494 {
5495 LOG_TR(("not enough characters for RB"));
5496 return FAIL;
5497 }
5498 return OK;
5499}
5500
5501/*
5502 * Check for key code response from xterm:
5503 * {lead}{flag}+r<hex bytes><{tail}
5504 *
5505 * {lead} can be <Esc>P or DCS
5506 * {flag} can be '0' or '1'
5507 * {tail} can be Esc>\ or STERM
5508 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005509 * Check for resource response from xterm (and drop it):
5510 * {lead}{flag}+R<hex bytes>=<value>{tail}
5511 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005512 * Check for cursor shape response from xterm:
5513 * {lead}1$r<digit> q{tail}
5514 *
5515 * {lead} can be <Esc>P or DCS
5516 * {tail} can be <Esc>\ or STERM
5517 *
5518 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5519 */
5520 static int
5521handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5522{
5523 int i, j;
5524
5525 j = 1 + (tp[0] == ESC);
5526 if (len < j + 3)
5527 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005528 else if ((argp[1] != '+' && argp[1] != '$')
5529 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005530 i = 0; // no match
5531 else if (argp[1] == '+')
5532 // key code response
5533 for (i = j; i < len; ++i)
5534 {
5535 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5536 || tp[i] == STERM)
5537 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005538#ifdef FEAT_TERMRESPONSE
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005539 // handle a key code response, drop a resource response
5540 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005541 got_code_from_term(tp + j, i);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005542#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005543 key_name[0] = (int)KS_EXTRA;
5544 key_name[1] = (int)KE_IGNORE;
5545 *slen = i + 1 + (tp[i] == ESC);
5546 break;
5547 }
5548 }
5549 else
5550 {
5551 // Probably the cursor shape response. Make sure that "i"
5552 // is equal to "len" when there are not sufficient
5553 // characters.
5554 for (i = j + 3; i < len; ++i)
5555 {
5556 if (i - j == 3 && !isdigit(tp[i]))
5557 break;
5558 if (i - j == 4 && tp[i] != ' ')
5559 break;
5560 if (i - j == 5 && tp[i] != 'q')
5561 break;
5562 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5563 break;
5564 if ((i - j == 6 && tp[i] == STERM)
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005565 || (i - j == 7 && tp[i] == '\\'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005566 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005567#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005568 int number = argp[3] - '0';
5569
5570 // 0, 1 = block blink, 2 = block
5571 // 3 = underline blink, 4 = underline
5572 // 5 = vertical bar blink, 6 = vertical bar
5573 number = number == 0 ? 1 : number;
5574 initial_cursor_shape = (number + 1) / 2;
5575 // The blink flag is actually inverted, compared to
5576 // the value set with T_SH.
5577 initial_cursor_shape_blink =
5578 (number & 1) ? FALSE : TRUE;
5579 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005580#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005581 LOG_TR(("Received cursor shape response: %s", tp));
5582
5583 key_name[0] = (int)KS_EXTRA;
5584 key_name[1] = (int)KE_IGNORE;
5585 *slen = i + 1;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005586#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005587 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005588#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005589 break;
5590 }
5591 }
5592 }
5593
5594 if (i == len)
5595 {
5596 // These codes arrive many together, each code can be
5597 // truncated at any point.
5598 LOG_TR(("not enough characters for XT"));
5599 return FAIL;
5600 }
5601 return OK;
5602}
5603
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 * Check if typebuf.tb_buf[] contains a terminal key code.
5606 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005607 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005609 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 * With a match, the match is removed, the replacement code is inserted in
5611 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5612 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005613 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5614 * "buflen" is then the length of the string in buf[] and is updated for
5615 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 */
5617 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005618check_termcode(
5619 int max_offset,
5620 char_u *buf,
5621 int bufsize,
5622 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
5624 char_u *tp;
5625 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005626 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005627 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005629 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 int offset;
5631 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005632 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005633 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005634 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005635 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 char_u string[MAX_KEY_CODE_LEN + 1];
5637 int i, j;
5638 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640
5641 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5642
5643 /*
5644 * Speed up the checks for terminal codes by gathering all first bytes
5645 * used in termleader[]. Often this is just a single <Esc>.
5646 */
5647 if (need_gather)
5648 gather_termleader();
5649
5650 /*
5651 * Check at several positions in typebuf.tb_buf[], to catch something like
5652 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5653 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005654 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 * This is used often, KEEP IT FAST!
5656 */
5657 for (offset = 0; offset < max_offset; ++offset)
5658 {
5659 if (buf == NULL)
5660 {
5661 if (offset >= typebuf.tb_len)
5662 break;
5663 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005664 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 }
5666 else
5667 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005668 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 break;
5670 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005671 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673
5674 /*
5675 * Don't check characters after K_SPECIAL, those are already
5676 * translated terminal chars (avoid translating ~@^Hx).
5677 */
5678 if (*tp == K_SPECIAL)
5679 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005680 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 continue;
5682 }
5683
5684 /*
5685 * Skip this position if the character does not appear as the first
5686 * character in term_strings. This speeds up a lot, since most
5687 * termcodes start with the same character (ESC or CSI).
5688 */
5689 i = *tp;
5690 for (p = termleader; *p && *p != i; ++p)
5691 ;
5692 if (*p == NUL)
5693 continue;
5694
5695 /*
5696 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5697 * are in Insert mode.
5698 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005699 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 continue;
5701
Bram Moolenaar27efc622022-07-01 16:35:45 +01005702 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005703 key_name[0] = NUL; // no key name found yet
5704 key_name[1] = NUL; // no key name found yet
5705 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706
5707#ifdef FEAT_GUI
5708 if (gui.in_use)
5709 {
5710 /*
5711 * GUI special key codes are all of the form [CSI xx].
5712 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005713 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 {
5715 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005716 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 slen = 3;
5718 key_name[0] = tp[1];
5719 key_name[1] = tp[2];
5720 }
5721 }
5722 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005723#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00005724#ifdef MSWIN
5725 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
5726 && (tp[2] == KE_MOUSEUP
5727 || tp[2] == KE_MOUSEDOWN
5728 || tp[2] == KE_MOUSELEFT
5729 || tp[2] == KE_MOUSERIGHT))
5730 {
5731 // MS-Windows console sends mouse scroll events encoded:
5732 // - CSI
5733 // - KS_EXTRA
5734 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
5735 slen = 3;
5736 key_name[0] = tp[1];
5737 key_name[1] = tp[2];
5738 }
5739 else
5740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005742 int mouse_index_found = -1;
5743
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 for (idx = 0; idx < tc_len; ++idx)
5745 {
5746 /*
5747 * Ignore the entry if we are not at the start of
5748 * typebuf.tb_buf[]
5749 * and there are not enough characters to make a match.
5750 * But only when the 'K' flag is in 'cpoptions'.
5751 */
5752 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005753 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 if (cpo_koffset && offset && len < slen)
5755 continue;
5756 if (STRNCMP(termcodes[idx].code, tp,
5757 (size_t)(slen > len ? len : slen)) == 0)
5758 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005759 int looks_like_mouse_start = FALSE;
5760
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005761 if (len < slen) // got a partial sequence
5762 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
5764 /*
5765 * When found a keypad key, check if there is another key
5766 * that matches and use that one. This makes <Home> to be
5767 * found instead of <kHome> when they produce the same
5768 * key code.
5769 */
5770 if (termcodes[idx].name[0] == 'K'
5771 && VIM_ISDIGIT(termcodes[idx].name[1]))
5772 {
5773 for (j = idx + 1; j < tc_len; ++j)
5774 if (termcodes[j].len == slen &&
5775 STRNCMP(termcodes[idx].code,
5776 termcodes[j].code, slen) == 0)
5777 {
5778 idx = j;
5779 break;
5780 }
5781 }
5782
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005783 if (slen == 2 && len > 2
5784 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005785 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005786 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005787 // The mouse termcode "ESC [" is also the prefix of
5788 // "ESC [ I" (focus gained) and other keys. Check some
5789 // more bytes to find out.
5790 if (!isdigit(tp[2]))
5791 {
5792 // ESC [ without number following: Only use it when
5793 // there is no other match.
5794 looks_like_mouse_start = TRUE;
5795 }
5796 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5797 {
5798 char_u *nr = tp + 2;
5799 int count = 0;
5800
5801 // If a digit is following it could be a key with
5802 // modifier, e.g., ESC [ 1;2P. Can be confused
5803 // with DEC_MOUSE, which requires four numbers
5804 // following. If not then it can't be a DEC_MOUSE
5805 // code.
5806 for (;;)
5807 {
5808 ++count;
5809 (void)getdigits(&nr);
5810 if (nr >= tp + len)
5811 return -1; // partial sequence
5812 if (*nr != ';')
5813 break;
5814 ++nr;
5815 if (nr >= tp + len)
5816 return -1; // partial sequence
5817 }
5818 if (count < 4)
5819 continue; // no match
5820 }
5821 }
5822 if (looks_like_mouse_start)
5823 {
5824 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005825 if (mouse_index_found < 0)
5826 mouse_index_found = idx;
5827 }
5828 else
5829 {
5830 key_name[0] = termcodes[idx].name[0];
5831 key_name[1] = termcodes[idx].name[1];
5832 break;
5833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005835
5836 /*
5837 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005838 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005839 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005840 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5841 * When there is a modifier the * matches a number.
5842 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005843 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005844 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005845 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005846 modslen = termcodes[idx].modlen;
5847 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005848 continue;
5849 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005850 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005851 {
5852 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005853
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005854 if (len <= modslen) // got a partial sequence
5855 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005856
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005857 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005858 // no modifiers
5859 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005860 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005861 // no match for "code;*X" with "code;"
5862 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01005863 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00005864 && (tp[modslen] != '1'
5865 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00005866 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005867 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005868 else
5869 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005870 // Skip over the digits, the final char must
5871 // follow. URXVT can use a negative value, thus
5872 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005873 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005874 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005875 ;
5876 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005877 if (len < j) // got a partial sequence
5878 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005879 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005880 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005881
Bram Moolenaara529ce02017-06-22 22:37:57 +02005882 modifiers_start = tp + slen - 2;
5883
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005884 // Match! Convert modifier bits.
5885 n = atoi((char *)modifiers_start);
5886 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005887
5888 slen = j;
5889 }
5890 key_name[0] = termcodes[idx].name[0];
5891 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005892 break;
5893 }
5894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005896 if (idx == tc_len && mouse_index_found >= 0)
5897 {
5898 key_name[0] = termcodes[mouse_index_found].name[0];
5899 key_name[1] = termcodes[mouse_index_found].name[1];
5900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 }
5902
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005903 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005904 // Mouse codes of DEC and pterm start with <ESC>[. When
5905 // detecting the start of these mouse codes they might as well be
5906 // another key code or terminal response.
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005907#ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005908 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005909#endif
5910#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005911 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005912#endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005913 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005915 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5916
5917 /*
5918 * Check for responses from the terminal starting with {lead}:
5919 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005920 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005921 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005922 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005923 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005924 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005925 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5926 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005927 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005928 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005929 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005930 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005931 * - window position reply: {lead}3;{x};{y}t
5932 *
5933 * - key with modifiers when modifyOtherKeys is enabled:
5934 * {lead}27;{modifier};{key}~
5935 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005936 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005937 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005938 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005939 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005941 int resp = handle_csi(tp, len, argp, offset, buf,
5942 bufsize, buflen, key_name, &slen);
5943 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005944 {
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005945#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005946 if (resp == -1)
5947 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005948#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005949 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005950 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005951 }
5952
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005953 // Check for fore/background color response from the terminal,
5954 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005955 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005956 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5957 || tp[0] == OSC))
5958 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005959 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005960 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 }
5962
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005963 // Check for key code response from xterm,
5964 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005965 // It would only be needed with this condition:
5966 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
5967 // Now this is always done so that DCS codes don't mess up things.
5968 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005970 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005971 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 }
5973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974
5975 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005976 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005978 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979
Christopher Plewright36446bb2022-11-23 22:28:08 +00005980#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00005982 * For scroll events from the GUI or MS-Windows console, fetch the
5983 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00005985 if (TRUE
5986# if defined(FEAT_GUI) && !defined(MSWIN)
5987 && gui.in_use
5988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 && key_name[0] == (int)KS_EXTRA
5990 && (key_name[1] == (int)KE_X1MOUSE
5991 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005992 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005993 || key_name[1] == (int)KE_MOUSELEFT
5994 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 || key_name[1] == (int)KE_MOUSEDOWN
5996 || key_name[1] == (int)KE_MOUSEUP))
5997 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005998 char_u bytes[6];
5999 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
6000
6001 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 return -1;
6003 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
6004 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
6005 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006006 // equal to K_MOUSEMOVE
6007 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
6008 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 }
6010 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 /*
6013 * If it is a mouse click, get the coordinates.
6014 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006015 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006016#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02006017 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006018#endif
6019#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006020 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006021#endif
6022#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006023 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006024#endif
6025#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006026 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006027#endif
6028#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006029 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006030#endif
6031#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006032 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006033#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006034 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006035 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006037 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
6038 &modifiers) == -1)
6039 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041
6042#ifdef FEAT_GUI
6043 /*
6044 * If using the GUI, then we get menu and scrollbar events.
6045 *
6046 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
6047 * four bytes which are to be taken as a pointer to the vimmenu_T
6048 * structure.
6049 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00006050 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006051 * is one byte with the tab index.
6052 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
6054 * by one byte representing the scrollbar number, and then four bytes
6055 * representing a long_u which is the new value of the scrollbar.
6056 *
6057 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
6058 * KE_FILLER followed by four bytes representing a long_u which is the
6059 * new value of the scrollbar.
6060 */
6061# ifdef FEAT_MENU
6062 else if (key_name[0] == (int)KS_MENU)
6063 {
6064 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006065 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 if (num_bytes == -1)
6068 return -1;
6069 current_menu = (vimmenu_T *)val;
6070 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006071
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006072 // The menu may have been deleted right after it was used, check
6073 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006074 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6075 {
6076 key_name[0] = KS_EXTRA;
6077 key_name[1] = (int)KE_IGNORE;
6078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 }
6080# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006081# ifdef FEAT_GUI_TABLINE
6082 else if (key_name[0] == (int)KS_TABLINE)
6083 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006084 // Selecting tabline tab or using its menu.
6085 char_u bytes[6];
6086 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6087
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006088 if (num_bytes == -1)
6089 return -1;
6090 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006091 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006092 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006093 slen += num_bytes;
6094 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006095 else if (key_name[0] == (int)KS_TABMENU)
6096 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006097 // Selecting tabline tab or using its menu.
6098 char_u bytes[6];
6099 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6100
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006101 if (num_bytes == -1)
6102 return -1;
6103 current_tab = (int)bytes[0];
6104 current_tabmenu = (int)bytes[1];
6105 slen += num_bytes;
6106 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108# ifndef USE_ON_FLY_SCROLL
6109 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6110 {
6111 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006112 char_u bytes[6];
6113 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006115 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 j = 0;
6117 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6118 && tp[j + 2] != NUL; ++i)
6119 {
6120 j += 3;
6121 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6122 if (num_bytes == -1)
6123 break;
6124 if (i == 0)
6125 current_scrollbar = (int)bytes[0];
6126 else if (current_scrollbar != (int)bytes[0])
6127 break;
6128 j += num_bytes;
6129 num_bytes = get_long_from_buf(tp + j, &val);
6130 if (num_bytes == -1)
6131 break;
6132 scrollbar_value = val;
6133 j += num_bytes;
6134 slen = j;
6135 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006136 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 return -1;
6138 }
6139 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6140 {
6141 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006142 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006144 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 j = 0;
6146 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6147 && tp[j + 2] != NUL; ++i)
6148 {
6149 j += 3;
6150 num_bytes = get_long_from_buf(tp + j, &val);
6151 if (num_bytes == -1)
6152 break;
6153 scrollbar_value = val;
6154 j += num_bytes;
6155 slen = j;
6156 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006157 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 return -1;
6159 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006160# endif // !USE_ON_FLY_SCROLL
6161#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006163#if (defined(UNIX) || defined(VMS))
6164 /*
6165 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6166 * (CSI I/CSI O)
6167 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006168 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006169# ifdef FEAT_GUI
6170 && !gui.in_use
6171# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006172 )
6173 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006174 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006175 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006176 if (!focus_state)
6177 {
6178 ui_focus_change(TRUE);
6179 did_cursorhold = TRUE;
6180 focus_state = TRUE;
6181 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006182 key_name[1] = (int)KE_IGNORE;
6183 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006184 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006185 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006186 if (focus_state)
6187 {
6188 ui_focus_change(FALSE);
6189 did_cursorhold = TRUE;
6190 focus_state = FALSE;
6191 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006192 key_name[1] = (int)KE_IGNORE;
6193 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006194 }
6195#endif
6196
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006197 /*
6198 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6199 */
6200 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6201
6202 /*
6203 * Add any modifier codes to our string.
6204 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006205 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006206
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006207 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006208 key_name[0] = KEY2TERMCAP0(key);
6209 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006211 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006212 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006213 if (has_mbyte)
6214 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6215 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006216 string[new_slen++] = key_name[1];
6217 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006218 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6219 && key_name[1] == KE_IGNORE)
6220 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006221 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6222 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006223 retval = KEYLEN_REMOVED;
6224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 else
6226 {
6227 string[new_slen++] = K_SPECIAL;
6228 string[new_slen++] = key_name[0];
6229 string[new_slen++] = key_name[1];
6230 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006231 if (put_string_in_typebuf(offset, slen, string, new_slen,
6232 buf, bufsize, buflen) == FAIL)
6233 return -1;
6234 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 }
6236
Bram Moolenaar2951b772013-07-03 12:45:31 +02006237#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006238 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006239#endif
6240
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006241 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242}
6243
Bram Moolenaar9377df32017-10-15 13:22:01 +02006244#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006245/*
6246 * Get the text foreground color, if known.
6247 */
6248 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006249term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006250{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006251 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006252 {
6253 *r = fg_r;
6254 *g = fg_g;
6255 *b = fg_b;
6256 }
6257}
6258
6259/*
6260 * Get the text background color, if known.
6261 */
6262 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006263term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006264{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006265 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006266 {
6267 *r = bg_r;
6268 *g = bg_g;
6269 *b = bg_b;
6270 }
6271}
6272#endif
6273
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274/*
6275 * Replace any terminal code strings in from[] with the equivalent internal
6276 * vim representation. This is used for the "from" and "to" part of a
6277 * mapping, and the "to" part of a menu command.
6278 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6279 * '<'.
6280 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6281 *
6282 * The replacement is done in result[] and finally copied into allocated
6283 * memory. If this all works well *bufp is set to the allocated memory and a
6284 * pointer to it is returned. If something fails *bufp is set to NULL and from
6285 * is returned.
6286 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006287 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6288 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6289 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6290 * used instead of a CTRL-V.
6291 *
6292 * Flags:
6293 * REPTERM_FROM_PART see above
6294 * REPTERM_DO_LT also translate <lt>
6295 * REPTERM_SPECIAL always accept <key> notation
6296 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6297 *
6298 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6299 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006301 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006302replace_termcodes(
6303 char_u *from,
6304 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02006305 int flags,
6306 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
6308 int i;
6309 int slen;
6310 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006311 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006313 int do_backslash; // backslash is a special character
6314 int do_special; // recognize <> key codes
6315 int do_key_code; // recognize raw key codes
6316 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006317 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318
6319 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006320 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6321 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006323 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324
6325 /*
6326 * Allocate space for the translation. Worst case a single character is
6327 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006328 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006330 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006331 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 {
6333 *bufp = NULL;
6334 return from;
6335 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006336 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337
6338 /*
6339 * Check for #n at start only: function key n
6340 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006341 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {
6343 result[dlen++] = K_SPECIAL;
6344 result[dlen++] = 'k';
6345 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006346 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006348 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 src += 2;
6350 }
6351
6352 /*
6353 * Copy each byte from *from to result[dlen]
6354 */
6355 while (*src != NUL)
6356 {
6357 /*
6358 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006359 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006361 if (do_special && ((flags & REPTERM_DO_LT)
6362 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 {
6364#ifdef FEAT_EVAL
6365 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006366 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6367 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006369 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6370 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 */
6372 if (STRNICMP(src, "<SID>", 5) == 0)
6373 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006374 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006375 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 else
6377 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006378 char_u *dot;
6379 long sid = current_sctx.sc_sid;
6380
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006382 if (in_vim9script()
6383 && (dot = vim_strchr(src, '.')) != NULL)
6384 {
6385 imported_T *imp = find_imported(src, dot - src, FALSE);
6386
6387 if (imp != NULL)
6388 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006389 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6390 size_t len;
6391
Bram Moolenaar89445512022-04-14 12:58:23 +01006392 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006393 if (si->sn_autoload_prefix != NULL)
6394 {
6395 // Turn "<SID>name.Func"
6396 // into "scriptname#Func".
6397 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006398 if (ga_grow(&ga,
6399 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006400 {
6401 ga_clear(&ga);
6402 *bufp = NULL;
6403 return from;
6404 }
6405 result = ga.ga_data;
6406 STRCPY(result + dlen, si->sn_autoload_prefix);
6407 dlen += len;
6408 continue;
6409 }
6410 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006411 }
6412 }
6413
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 result[dlen++] = K_SPECIAL;
6415 result[dlen++] = (int)KS_EXTRA;
6416 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006417 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006418 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 result[dlen++] = '_';
6420 continue;
6421 }
6422 }
6423#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006424 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6425 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006426 TRUE, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 if (slen)
6428 {
6429 dlen += slen;
6430 continue;
6431 }
6432 }
6433
6434 /*
6435 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6436 * Note that this is also checked after replacing the <> form.
6437 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6438 * it could be a character in the file.
6439 */
6440 if (do_key_code)
6441 {
6442 i = find_term_bykeys(src);
6443 if (i >= 0)
6444 {
6445 result[dlen++] = K_SPECIAL;
6446 result[dlen++] = termcodes[i].name[0];
6447 result[dlen++] = termcodes[i].name[1];
6448 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006449 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 continue;
6451 }
6452 }
6453
6454#ifdef FEAT_EVAL
6455 if (do_special)
6456 {
6457 char_u *p, *s, len;
6458
6459 /*
6460 * Replace <Leader> by the value of "mapleader".
6461 * Replace <LocalLeader> by the value of "maplocalleader".
6462 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6463 */
6464 if (STRNICMP(src, "<Leader>", 8) == 0)
6465 {
6466 len = 8;
6467 p = get_var_value((char_u *)"g:mapleader");
6468 }
6469 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6470 {
6471 len = 13;
6472 p = get_var_value((char_u *)"g:maplocalleader");
6473 }
6474 else
6475 {
6476 len = 0;
6477 p = NULL;
6478 }
6479 if (len != 0)
6480 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006481 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6483 s = (char_u *)"\\";
6484 else
6485 s = p;
6486 while (*s != NUL)
6487 result[dlen++] = *s++;
6488 src += len;
6489 continue;
6490 }
6491 }
6492#endif
6493
6494 /*
6495 * Remove CTRL-V and ignore the next character.
6496 * For "from" side the CTRL-V at the end is included, for the "to"
6497 * part it is removed.
6498 * If 'cpoptions' does not contain 'B', also accept a backslash.
6499 */
6500 key = *src;
6501 if (key == Ctrl_V || (do_backslash && key == '\\'))
6502 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006503 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 if (*src == NUL)
6505 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006506 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 result[dlen++] = key;
6508 break;
6509 }
6510 }
6511
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006512 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006513 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 {
6515 /*
6516 * If the character is K_SPECIAL, replace it with K_SPECIAL
6517 * KS_SPECIAL KE_FILLER.
6518 * If compiled with the GUI replace CSI with K_CSI.
6519 */
6520 if (*src == K_SPECIAL)
6521 {
6522 result[dlen++] = K_SPECIAL;
6523 result[dlen++] = KS_SPECIAL;
6524 result[dlen++] = KE_FILLER;
6525 }
6526# ifdef FEAT_GUI
6527 else if (*src == CSI)
6528 {
6529 result[dlen++] = K_SPECIAL;
6530 result[dlen++] = KS_EXTRA;
6531 result[dlen++] = (int)KE_CSI;
6532 }
6533# endif
6534 else
6535 result[dlen++] = *src;
6536 ++src;
6537 }
6538 }
6539 result[dlen] = NUL;
6540
6541 /*
6542 * Copy the new string to allocated memory.
6543 * If this fails, just return from.
6544 */
6545 if ((*bufp = vim_strsave(result)) != NULL)
6546 from = *bufp;
6547 vim_free(result);
6548 return from;
6549}
6550
6551/*
6552 * Find a termcode with keys 'src' (must be NUL terminated).
6553 * Return the index in termcodes[], or -1 if not found.
6554 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006555 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006556find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557{
6558 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006559 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
6561 for (i = 0; i < tc_len; ++i)
6562 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006563 if (slen == termcodes[i].len
6564 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 return i;
6566 }
6567 return -1;
6568}
6569
6570/*
6571 * Gather the first characters in the terminal key codes into a string.
6572 * Used to speed up check_termcode().
6573 */
6574 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006575gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576{
6577 int i;
6578 int len = 0;
6579
6580#ifdef FEAT_GUI
6581 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006582 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583#endif
6584#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006585 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006586 termleader[len++] = DCS; // the termcode response starts with DCS
6587 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588#endif
6589 termleader[len] = NUL;
6590
6591 for (i = 0; i < tc_len; ++i)
6592 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6593 {
6594 termleader[len++] = termcodes[i].code[0];
6595 termleader[len] = NUL;
6596 }
6597
6598 need_gather = FALSE;
6599}
6600
6601/*
6602 * Show all termcodes (for ":set termcap")
6603 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006604 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 */
6606 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006607show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608{
6609 int col;
6610 int *items;
6611 int item_count;
6612 int run;
6613 int row, rows;
6614 int cols;
6615 int i;
6616 int len;
6617
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006618#define INC3 27 // try to make three columns
6619#define INC2 40 // try to make two columns
6620#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006622 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006624 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 if (items == NULL)
6626 return;
6627
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006628 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006629 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630
6631 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006632 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006634 * 2. display the medium items (medium length strings)
6635 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006636 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006638 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 {
6640 /*
6641 * collect the items in items[]
6642 */
6643 item_count = 0;
6644 for (i = 0; i < tc_len; i++)
6645 {
6646 len = show_one_termcode(termcodes[i].name,
6647 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006648 if ((flags & OPT_ONECOLUMN) ||
6649 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006650 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006651 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 items[item_count++] = i;
6653 }
6654
6655 /*
6656 * display the items
6657 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006658 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006660 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 if (cols == 0)
6662 cols = 1;
6663 rows = (item_count + cols - 1) / cols;
6664 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006665 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 rows = item_count;
6667 for (row = 0; row < rows && !got_int; ++row)
6668 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006669 msg_putchar('\n'); // go to next line
6670 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 break;
6672 col = 0;
6673 for (i = row; i < item_count; i += rows)
6674 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006675 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 show_one_termcode(termcodes[items[i]].name,
6677 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006678 if (run == 2)
6679 col += INC2;
6680 else
6681 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 }
6683 out_flush();
6684 ui_breakcheck();
6685 }
6686 }
6687 vim_free(items);
6688}
6689
6690/*
6691 * Show one termcode entry.
6692 * Output goes into IObuff[]
6693 */
6694 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006695show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
6697 char_u *p;
6698 int len;
6699
6700 if (name[0] > '~')
6701 {
6702 IObuff[0] = ' ';
6703 IObuff[1] = ' ';
6704 IObuff[2] = ' ';
6705 IObuff[3] = ' ';
6706 }
6707 else
6708 {
6709 IObuff[0] = 't';
6710 IObuff[1] = '_';
6711 IObuff[2] = name[0];
6712 IObuff[3] = name[1];
6713 }
6714 IObuff[4] = ' ';
6715
6716 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6717 if (p[1] != 't')
6718 STRCPY(IObuff + 5, p);
6719 else
6720 IObuff[5] = NUL;
6721 len = (int)STRLEN(IObuff);
6722 do
6723 IObuff[len++] = ' ';
6724 while (len < 17);
6725 IObuff[len] = NUL;
6726 if (code == NULL)
6727 len += 4;
6728 else
6729 len += vim_strsize(code);
6730
6731 if (printit)
6732 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006733 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006735 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 else
6737 msg_outtrans(code);
6738 }
6739 return len;
6740}
6741
6742#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6743/*
6744 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6745 * termcap codes from the terminal itself.
6746 * We get them one by one to avoid a very long response string.
6747 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006748static int xt_index_in = 0;
6749static int xt_index_out = 0;
6750
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006752req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753{
6754 xt_index_out = 0;
6755 xt_index_in = 0;
6756 req_more_codes_from_term();
6757}
6758
6759 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006760req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006762 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 int old_idx = xt_index_out;
6764
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006765 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 if (exiting)
6767 return;
6768
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006769 // Send up to 10 more requests out than we received. Avoid sending too
6770 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6772 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006773 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006774
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006775 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006776 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6777 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 out_str_nf((char_u *)buf);
6779 ++xt_index_out;
6780 }
6781
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006782 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 if (xt_index_out != old_idx)
6784 out_flush();
6785}
6786
6787/*
6788 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6789 * A "0" instead of the "1" indicates a code that isn't supported.
6790 * Both <name> and <string> are encoded in hex.
6791 * "code" points to the "0" or "1".
6792 */
6793 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006794got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795{
6796#define XT_LEN 100
6797 char_u name[3];
6798 char_u str[XT_LEN];
6799 int i;
6800 int j = 0;
6801 int c;
6802
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006803 // A '1' means the code is supported, a '0' means it isn't.
6804 // When half the length is > XT_LEN we can't use it.
6805 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6807 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006808 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 name[0] = hexhex2nr(code + 3);
6810 name[1] = hexhex2nr(code + 5);
6811 name[2] = NUL;
6812 for (i = 0; key_names[i] != NULL; ++i)
6813 {
6814 if (STRCMP(key_names[i], name) == 0)
6815 {
6816 xt_index_in = i;
6817 break;
6818 }
6819 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006820
Bram Moolenaarb255b902018-04-24 21:40:10 +02006821 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6822
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 if (key_names[i] != NULL)
6824 {
6825 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6826 str[j++] = c;
6827 str[j] = NUL;
6828 if (name[0] == 'C' && name[1] == 'o')
6829 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006830 // Color count is not a key code.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00006831 may_adjust_color_count(atoi((char *)str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 }
6833 else
6834 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006835 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 i = find_term_bykeys(str);
6837 if (i >= 0)
6838 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006839 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 }
6841 }
6842 }
6843
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006844 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 ++xt_index_in;
6846 req_more_codes_from_term();
6847}
6848
6849/*
6850 * Check if there are any unanswered requests and deal with them.
6851 * This is called before starting an external program or getting direct
6852 * keyboard input. We don't want responses to be send to that program or
6853 * handled as typed text.
6854 */
6855 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006856check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
6858 int c;
6859
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006860 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6862 return;
6863
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006864 // Vgetc() will check for and handle any response.
6865 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 ++no_mapping;
6867 ++allow_keys;
6868 for (;;)
6869 {
6870 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006871 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 break;
6873
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006874 // If a response is recognized it's replaced with K_IGNORE, must read
6875 // it from the input stream. If there is no K_IGNORE we can't do
6876 // anything, break here (there might be some responses further on, but
6877 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 if (c != K_SPECIAL && c != K_IGNORE)
6879 break;
6880 c = vgetc();
6881 if (c != K_IGNORE)
6882 {
6883 vungetc(c);
6884 break;
6885 }
6886 }
6887 --no_mapping;
6888 --allow_keys;
6889}
6890#endif
6891
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006892#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893static char ksme_str[20];
6894static char ksmr_str[20];
6895static char ksmd_str[20];
6896
6897/*
6898 * For Win32 console: update termcap codes for existing console attributes.
6899 */
6900 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006901update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006903 sprintf(ksme_str, "\033|%dm", attr);
6904 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
6905 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906
Bram Moolenaar4654d632022-11-17 22:05:12 +00006907 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
6908 if (p == NULL) // did not find it
6909 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 while (p->bt_string != NULL)
6911 {
6912 if (p->bt_entry == (int)KS_ME)
6913 p->bt_string = &ksme_str[0];
6914 else if (p->bt_entry == (int)KS_MR)
6915 p->bt_string = &ksmr_str[0];
6916 else if (p->bt_entry == (int)KS_MD)
6917 p->bt_string = &ksmd_str[0];
6918 ++p;
6919 }
6920}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006921
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006922# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006923# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006924
6925typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006926{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006927 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
6928 CMODE_RGB, // Use 24bit RGB colors using VTP.
6929 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
6930 CMODE_LAST,
6931} cmode_T;
6932
6933struct ks_tbl_S
6934{
6935 int code; // value of KS_
6936 char *vtp; // code in RGB mode
6937 char *vtp2; // code in 256color mode
6938 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006939};
6940
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006941static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006942{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006943 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
6944 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
6945 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
6946 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
6947 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
6948 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
6949 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
6950 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
6951 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006952# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006953 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
6954 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
6955 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
6956 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006957# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006958 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
6959 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
6960 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
6961 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006962# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006963 {(int)KS_CCO, "256", "256", {""}}, // colors
6964 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006965};
6966
Bram Moolenaar4654d632022-11-17 22:05:12 +00006967/*
6968 * Find the first entry for "code" in the builtin termcap for "name".
6969 * Returns NULL when not found.
6970 */
6971 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006972find_first_tcap(
6973 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006974 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006975{
Bram Moolenaar4654d632022-11-17 22:05:12 +00006976 tcap_entry_T *p = find_builtin_term(name);
6977 if (p != NULL)
6978 {
6979 while (p->bt_string != NULL)
6980 {
6981 if (p->bt_entry == code)
6982 return p;
6983 ++p;
6984 }
6985 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006986 return NULL;
6987}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006988# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006989
6990/*
6991 * For Win32 console: replace the sequence immediately after termguicolors.
6992 */
6993 void
6994swap_tcap(void)
6995{
6996# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006997 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006998 static cmode_T curr_mode;
6999 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007000 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007001
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007002 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007003 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007004 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007005 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007006 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007007 if (bt != NULL)
7008 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007009 // Preserve the original value.
7010 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
7011 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
7012 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007013
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007014 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007015 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007016 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007017 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007018 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007019 }
7020
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007021 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007022 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007023 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007024 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007025 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007026 mode = CMODE_INDEXED;
7027
7028 if (mode == curr_mode)
7029 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007030
7031 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007032 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007033 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007034 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007035 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007036 }
7037
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007038 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007039# endif
7040}
7041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02007043
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007044
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007045#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007046 || defined(PROTO)
7047static int cube_value[] = {
7048 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7049};
7050
7051static int grey_ramp[] = {
7052 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7053 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7054};
7055
LemonBoyb2b3acb2022-05-20 10:10:34 +01007056static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01007057// R G B
7058 { 0, 0, 0}, // black
7059 {224, 0, 0}, // dark red
7060 { 0, 224, 0}, // dark green
7061 {224, 224, 0}, // dark yellow / brown
7062 { 0, 0, 224}, // dark blue
7063 {224, 0, 224}, // dark magenta
7064 { 0, 224, 224}, // dark cyan
7065 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007066
LemonBoyd2a46622022-05-01 17:43:33 +01007067 {128, 128, 128}, // dark grey
7068 {255, 64, 64}, // light red
7069 { 64, 255, 64}, // light green
7070 {255, 255, 64}, // yellow
7071 { 64, 64, 255}, // light blue
7072 {255, 64, 255}, // light magenta
7073 { 64, 255, 255}, // light cyan
7074 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007075};
7076
LemonBoyd2a46622022-05-01 17:43:33 +01007077#if defined(MSWIN)
7078// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7079static const char_u cterm_ansi_idx[] = {
7080 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7081};
7082#endif
7083
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007084#define ANSI_INDEX_NONE 0
7085
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007086 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007087ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7088{
7089 if (nr < 16)
7090 {
7091 *r = ansi_table[nr][0];
7092 *g = ansi_table[nr][1];
7093 *b = ansi_table[nr][2];
7094 *ansi_idx = nr;
7095 }
7096 else
7097 {
7098 *r = 0;
7099 *g = 0;
7100 *b = 0;
7101 *ansi_idx = ANSI_INDEX_NONE;
7102 }
7103}
7104
7105 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007106cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007107{
7108 int idx;
7109
7110 if (nr < 16)
7111 {
LemonBoyd2a46622022-05-01 17:43:33 +01007112#if defined(MSWIN)
7113 idx = cterm_ansi_idx[nr];
7114#else
7115 idx = nr;
7116#endif
7117 *r = ansi_table[idx][0];
7118 *g = ansi_table[idx][1];
7119 *b = ansi_table[idx][2];
7120 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007121 }
7122 else if (nr < 232)
7123 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007124 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007125 idx = nr - 16;
7126 *r = cube_value[idx / 36 % 6];
7127 *g = cube_value[idx / 6 % 6];
7128 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007129 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007130 }
7131 else if (nr < 256)
7132 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007133 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007134 idx = nr - 232;
7135 *r = grey_ramp[idx];
7136 *g = grey_ramp[idx];
7137 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007138 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007139 }
7140 else
7141 {
7142 *r = 0;
7143 *g = 0;
7144 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007145 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007146 }
7147}
7148#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007149
Bram Moolenaarf4140482020-02-15 23:06:45 +01007150/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007151 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007152 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007153 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007154 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007155 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007156term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007157{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007158 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007159 int i;
7160 int c;
7161
7162 for (i = ta_len; i < ta_len + len; ++i)
7163 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007164 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7165 {
7166 int modifiers = ta_buf[i + 2];
7167 int key = ta_buf[i + 3];
7168
7169 // Try to use the modifier to modify the key. In any case drop the
7170 // modifier.
7171 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7172 len -= 3;
7173 if (key < 0x80)
7174 key = merge_modifyOtherKeys(key, &modifiers);
7175 ta_buf[i] = key;
7176 }
7177 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007178 {
7179 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7180 if (c == K_DEL || c == K_KDEL || c == K_BS)
7181 {
7182 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007183 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007184 if (c == K_DEL || c == K_KDEL)
7185 ta_buf[i] = DEL;
7186 else
7187 ta_buf[i] = Ctrl_H;
7188 len -= 2;
7189 }
7190 }
7191 else if (ta_buf[i] == '\r')
7192 ta_buf[i] = '\n';
7193 if (has_mbyte)
7194 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7195 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007196 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007197}