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