blob: 3b2c3c063fc08b62373d17547a05130540556a19 [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
69#ifdef HAVE_TGETENT
Bram Moolenaar3efd65c2022-06-19 17:45:28 +010070static char *invoke_tgetent(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/*
73 * Here is our own prototype for tgetstr(), any prototypes from the include
74 * files have been disabled by the define at the start of this file.
75 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010076char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077
78# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010079 // Change this to "if 1" to debug what happens with termresponse.
Bram Moolenaar2951b772013-07-03 12:45:31 +020080# if 0
81# define DEBUG_TERMRESPONSE
Bram Moolenaar952d9d82021-08-02 18:07:18 +020082static void log_tr(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Bram Moolenaarb255b902018-04-24 21:40:10 +020083# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +020084# else
Bram Moolenaarb255b902018-04-24 21:40:10 +020085# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +020086# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020087
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 Moolenaarafd78262019-05-10 23:10:31 +0200100# define TERMREQUEST_INIT {STATUS_GET, -1}
101
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 Moolenaar9377df32017-10-15 13:22:01 +0200111# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200112// Request foreground color report:
113static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200114static int fg_r = 0;
115static int fg_g = 0;
116static int fg_b = 0;
117static int bg_r = 255;
118static int bg_g = 255;
119static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200120# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200121
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100122// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200123static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200124
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100125// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200126static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100128// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200129static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100130
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100131// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200132static termrequest_T winpos_status = TERMREQUEST_INIT;
133
134static termrequest_T *all_termrequests[] = {
135 &crv_status,
136 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200137 &xcc_status,
Bram Moolenaarafd78262019-05-10 23:10:31 +0200138# ifdef FEAT_TERMINAL
139 &rfg_status,
140# endif
141 &rbg_status,
142 &rbm_status,
143 &rcs_status,
144 &winpos_status,
145 NULL
146};
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100147
148// The t_8u code may default to a value but get reset when the term response is
149// received. To avoid redrawing too often, only redraw when t_8u is not reset
Bram Moolenaarb3932752022-10-01 21:22:17 +0100150// and it was supposed to be written. Unless t_8u was set explicitly.
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100151// FALSE -> don't output t_8u yet
dundargocc57b5bc2022-11-02 13:30:51 +0000152// MAYBE -> tried outputting t_8u while FALSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100153// OK -> can write t_8u
154int write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155# endif
156
157/*
158 * Don't declare these variables if termcap.h contains them.
159 * Autoconf checks if these variables should be declared extern (not all
160 * systems have them).
161 * Some versions define ospeed to be speed_t, but that is incompatible with
162 * BSD, where ospeed is short and speed_t is long.
163 */
164# ifndef HAVE_OSPEED
165# ifdef OSPEED_EXTERN
166extern short ospeed;
167# else
168short ospeed;
169# endif
170# endif
171# ifndef HAVE_UP_BC_PC
172# ifdef UP_BC_PC_EXTERN
173extern char *UP, *BC, PC;
174# else
175char *UP, *BC, PC;
176# endif
177# endif
178
179# define TGETSTR(s, p) vim_tgetstr((s), (p))
180# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100181static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100182#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100184static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100186#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000187static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100188#endif
189
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200190#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100191// When the cursor shape was detected these values are used:
192// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200193static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200194
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100195// The blink flag from the style response may be inverted from the actual
196// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200197static int initial_cursor_shape_blink = FALSE;
198
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100199// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200200static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200201#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200202
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100203/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000204 * The builtin termcap entries.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100205 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000206 * The entries are also included when HAVE_TGETENT is defined, the system
207 * termcap may be incomplete and a few Vim-specific entries are added.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100208 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000209 * When HAVE_TGETENT is defined, the builtin entries can be accessed with
210 * "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
211 *
212 * Each termcap is a list of tcap_entry_T. See parse_builtin_tcap() for all
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100213 * details.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100214 *
215 * Entries marked with "guessed" may be wrong.
216 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000217typedef struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218{
Bram Moolenaar4654d632022-11-17 22:05:12 +0000219 int bt_entry; // either a KS_xxx code (>= 0), or a K_xxx code.
220 char *bt_string; // value
221} tcap_entry_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000224 * Standard ANSI terminal, default for Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000226static tcap_entry_T builtin_ansi[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000227 {(int)KS_CE, "\033[K"},
228 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000230 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000232 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000234 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000236 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000238 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000240 {(int)KS_CL, "\033[H\033[2J"},
241 {(int)KS_ME, "\033[0m"},
242 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100244 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {(int)KS_LE, "\b"},
246# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000247 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000249 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250# endif
251# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000252 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000254 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256
Bram Moolenaar4654d632022-11-17 22:05:12 +0000257 {(int)KS_NAME, NULL} // end marker
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260/*
261 * VT320 is working as an ANSI terminal compatible DEC terminal.
262 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 * TODO:- rewrite ESC[ codes to CSI
264 * - keyboard languages (CSI ? 26 n)
265 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000266static tcap_entry_T builtin_vt320[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000267 {(int)KS_CE, "\033[K"},
268 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000270 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000272 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000274 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000276 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000278 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000280 {(int)KS_CL, "\033[H\033[2J"},
281 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100282 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000283 {(int)KS_ME, "\033[0m"},
284 {(int)KS_MR, "\033[7m"},
285 {(int)KS_MD, "\033[1m"}, // bold mode
286 {(int)KS_SE, "\033[22m"},// normal mode
287 {(int)KS_UE, "\033[24m"},// exit underscore mode
288 {(int)KS_US, "\033[4m"}, // underscore mode
289 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
290 {(int)KS_CZR, "\033[0m"}, // italic mode end
291 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
292 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
293 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
294 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 {(int)KS_MS, "y"},
296 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100297 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 {(int)KS_LE, "\b"},
299# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000300 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000302 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# endif
304# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000305 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000307 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000309 {K_UP, "\033[A"},
310 {K_DOWN, "\033[B"},
311 {K_RIGHT, "\033[C"},
312 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200313 // Note: cursor key sequences for application cursor mode are omitted,
314 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000315 {K_F1, "\033[11~"},
316 {K_F2, "\033[12~"},
317 {K_F3, "\033[13~"},
318 {K_F4, "\033[14~"},
319 {K_F5, "\033[15~"},
320 {K_F6, "\033[17~"},
321 {K_F7, "\033[18~"},
322 {K_F8, "\033[19~"},
323 {K_F9, "\033[20~"},
324 {K_F10, "\033[21~"},
325 {K_F11, "\033[23~"},
326 {K_F12, "\033[24~"},
327 {K_F13, "\033[25~"},
328 {K_F14, "\033[26~"},
329 {K_F15, "\033[28~"}, // Help
330 {K_F16, "\033[29~"}, // Select
331 {K_F17, "\033[31~"},
332 {K_F18, "\033[32~"},
333 {K_F19, "\033[33~"},
334 {K_F20, "\033[34~"},
335 {K_INS, "\033[2~"},
336 {K_DEL, "\033[3~"},
337 {K_HOME, "\033[1~"},
338 {K_END, "\033[4~"},
339 {K_PAGEUP, "\033[5~"},
340 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200341 // These sequences starting with <Esc> O may interfere with what the user
342 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000343 {K_KPLUS, "\033Ok"}, // keypad plus
344 {K_KMINUS, "\033Om"}, // keypad minus
345 {K_KDIVIDE, "\033Oo"}, // keypad /
346 {K_KMULTIPLY, "\033Oj"}, // keypad *
347 {K_KENTER, "\033OM"}, // keypad Enter
348 {K_K0, "\033Op"}, // keypad 0
349 {K_K1, "\033Oq"}, // keypad 1
350 {K_K2, "\033Or"}, // keypad 2
351 {K_K3, "\033Os"}, // keypad 3
352 {K_K4, "\033Ot"}, // keypad 4
353 {K_K5, "\033Ou"}, // keypad 5
354 {K_K6, "\033Ov"}, // keypad 6
355 {K_K7, "\033Ow"}, // keypad 7
356 {K_K8, "\033Ox"}, // keypad 8
357 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100358 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
Bram Moolenaar4654d632022-11-17 22:05:12 +0000360 {(int)KS_NAME, NULL} // end marker
361};
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363/*
364 * Ordinary vt52
365 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000366static tcap_entry_T builtin_vt52[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000367 {(int)KS_CE, "\033K"},
368 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100369# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000370 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100371# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000372 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100373# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000375 {(int)KS_SR, "\033I"},
376 {(int)KS_AL, "\033L"},
377 {(int)KS_DL, "\033M"},
378 {K_UP, "\033A"},
379 {K_DOWN, "\033B"},
380 {K_LEFT, "\033D"},
381 {K_RIGHT, "\033C"},
382 {K_F1, "\033P"},
383 {K_F2, "\033Q"},
384 {K_F3, "\033R"},
385 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387
Bram Moolenaar4654d632022-11-17 22:05:12 +0000388 {(int)KS_NAME, NULL} // end marker
389};
390
391/*
392 * Builtin xterm with Vim-specific entries.
393 */
394static tcap_entry_T builtin_xterm[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000395 {(int)KS_CE, "\033[K"},
396 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000398 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000400 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000402 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000404 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000406 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407# endif
408# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000409 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000411 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000413 {(int)KS_CL, "\033[H\033[2J"},
414 {(int)KS_CD, "\033[J"},
415 {(int)KS_ME, "\033[m"},
416 {(int)KS_MR, "\033[7m"},
417 {(int)KS_MD, "\033[1m"},
418 {(int)KS_UE, "\033[m"},
419 {(int)KS_US, "\033[4m"},
420 {(int)KS_STE, "\033[29m"},
421 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 {(int)KS_MS, "y"},
423 {(int)KS_UT, "y"},
424 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000425 {(int)KS_VI, "\033[?25l"},
426 {(int)KS_VE, "\033[?25h"},
427 {(int)KS_VS, "\033[?12h"},
428 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200429# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000430 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200431# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000432 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200433# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000434 {(int)KS_CRC, "\033[?12$p"},
435 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000437 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000439 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000441 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000443 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000445 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000447 {(int)KS_KS, "\033[?1h\033="},
448 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000450 {(int)KS_TI, "\0337\033[?47h"},
451 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000453 {(int)KS_CTI, "\033[>4;2m"},
454 {(int)KS_CTE, "\033[>4;m"},
455 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000457 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000459 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200460 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000462 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
463 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
464 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000466 {(int)KS_CWS, "\033[8;%d;%dt"},
467 {(int)KS_CWP, "\033[3;%d;%dt"},
468 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000470 {(int)KS_CRV, "\033[>c"},
471 {(int)KS_RFG, "\033]10;?\007"},
472 {(int)KS_RBG, "\033]11;?\007"},
473 {(int)KS_U7, "\033[6n"},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200474# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100475 // These are printf strings, not terminal codes.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000476 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
477 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
478 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200479# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000480 {(int)KS_CAU, "\033[58;5;%dm"},
481 {(int)KS_CBE, "\033[?2004h"},
482 {(int)KS_CBD, "\033[?2004l"},
483 {(int)KS_CST, "\033[22;2t"},
484 {(int)KS_CRT, "\033[23;2t"},
485 {(int)KS_SSI, "\033[22;1t"},
486 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100487# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000488 {(int)KS_FD, "\033[?1004l"},
489 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100490# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000491
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000492 {K_UP, "\033O*A"},
493 {K_DOWN, "\033O*B"},
494 {K_RIGHT, "\033O*C"},
495 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100496 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100497 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
498 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
499 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
500 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100501 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000502 {K_XF1, "\033O*P"},
503 {K_XF2, "\033O*Q"},
504 {K_XF3, "\033O*R"},
505 {K_XF4, "\033O*S"},
506 {K_F1, "\033[11;*~"},
507 {K_F2, "\033[12;*~"},
508 {K_F3, "\033[13;*~"},
509 {K_F4, "\033[14;*~"},
510 {K_F5, "\033[15;*~"},
511 {K_F6, "\033[17;*~"},
512 {K_F7, "\033[18;*~"},
513 {K_F8, "\033[19;*~"},
514 {K_F9, "\033[20;*~"},
515 {K_F10, "\033[21;*~"},
516 {K_F11, "\033[23;*~"},
517 {K_F12, "\033[24;*~"},
518 {K_S_TAB, "\033[Z"},
519 {K_HELP, "\033[28;*~"},
520 {K_UNDO, "\033[26;*~"},
521 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100522 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000523 // {K_S_HOME, "\033O2H"},
524 // {K_C_HOME, "\033O5H"},
525 {K_KHOME, "\033[1;*~"},
526 {K_XHOME, "\033O*H"}, // other Home
527 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100528 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000529 // {K_S_END, "\033O2F"},
530 // {K_C_END, "\033O5F"},
531 {K_KEND, "\033[4;*~"},
532 {K_XEND, "\033O*F"}, // other End
533 {K_ZEND, "\033[8;*~"},
534 {K_PAGEUP, "\033[5;*~"},
535 {K_PAGEDOWN, "\033[6;*~"},
536 {K_KPLUS, "\033O*k"}, // keypad plus
537 {K_KMINUS, "\033O*m"}, // keypad minus
538 {K_KDIVIDE, "\033O*o"}, // keypad /
539 {K_KMULTIPLY, "\033O*j"}, // keypad *
540 {K_KENTER, "\033O*M"}, // keypad Enter
541 {K_KPOINT, "\033O*n"}, // keypad .
542 {K_K0, "\033O*p"}, // keypad 0
543 {K_K1, "\033O*q"}, // keypad 1
544 {K_K2, "\033O*r"}, // keypad 2
545 {K_K3, "\033O*s"}, // keypad 3
546 {K_K4, "\033O*t"}, // keypad 4
547 {K_K5, "\033O*u"}, // keypad 5
548 {K_K6, "\033O*v"}, // keypad 6
549 {K_K7, "\033O*w"}, // keypad 7
550 {K_K8, "\033O*x"}, // keypad 8
551 {K_K9, "\033O*y"}, // keypad 9
552 {K_KDEL, "\033[3;*~"}, // keypad Del
553 {K_PS, "\033[200~"}, // paste start
554 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
556 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000557 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
558 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100559 // F14 and F15 are missing, because they send the same codes as the undo
560 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000561 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
562 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
563 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
564 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
565 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000566
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000567 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
568 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
569 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
570 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
571 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
572 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
573 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
574 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
575 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
576 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000577
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000578 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
579 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
580 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
581 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
582 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
583 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
584 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
Bram Moolenaar4654d632022-11-17 22:05:12 +0000586 {(int)KS_NAME, NULL} // end marker
587};
588
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589/*
590 * iris-ansi for Silicon Graphics machines.
591 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000592static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 {(int)KS_CE, "\033[K"},
594 {(int)KS_CD, "\033[J"},
595 {(int)KS_AL, "\033[L"},
596# ifdef TERMINFO
597 {(int)KS_CAL, "\033[%p1%dL"},
598# else
599 {(int)KS_CAL, "\033[%dL"},
600# endif
601 {(int)KS_DL, "\033[M"},
602# ifdef TERMINFO
603 {(int)KS_CDL, "\033[%p1%dM"},
604# else
605 {(int)KS_CDL, "\033[%dM"},
606# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100607#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608# ifdef TERMINFO
609 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
610# else
611 {(int)KS_CS, "\033[%i%d;%dr"},
612# endif
613#endif
614 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100615 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
616 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 {(int)KS_TI, "\033[=6h"},
618 {(int)KS_TE, "\033[=6l"},
619 {(int)KS_SE, "\033[21;27m"},
620 {(int)KS_SO, "\033[1;7m"},
621 {(int)KS_ME, "\033[m"},
622 {(int)KS_MR, "\033[7m"},
623 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100624 {(int)KS_CCO, "8"}, // allow 8 colors
625 {(int)KS_CZH, "\033[3m"}, // italic mode on
626 {(int)KS_CZR, "\033[23m"}, // italic mode off
627 {(int)KS_US, "\033[4m"}, // underline on
628 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100630 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
631 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
632 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
633 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100635 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
636 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
637 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
638 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100640 {(int)KS_MS, "y"}, // guessed
641 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 {(int)KS_LE, "\b"},
643# ifdef TERMINFO
644 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
645# else
646 {(int)KS_CM, "\033[%i%d;%dH"},
647# endif
648 {(int)KS_SR, "\033M"},
649# ifdef TERMINFO
650 {(int)KS_CRI, "\033[%p1%dC"},
651# else
652 {(int)KS_CRI, "\033[%dC"},
653# endif
654 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100655 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100657 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658# ifdef TERMINFO
659 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
660 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
661# else
662 {(int)KS_CWS, "\033[203;%d;%d/y"},
663 {(int)KS_CWP, "\033[205;%d;%d/y"},
664# endif
665 {K_UP, "\033[A"},
666 {K_DOWN, "\033[B"},
667 {K_LEFT, "\033[D"},
668 {K_RIGHT, "\033[C"},
669 {K_S_UP, "\033[161q"},
670 {K_S_DOWN, "\033[164q"},
671 {K_S_LEFT, "\033[158q"},
672 {K_S_RIGHT, "\033[167q"},
673 {K_F1, "\033[001q"},
674 {K_F2, "\033[002q"},
675 {K_F3, "\033[003q"},
676 {K_F4, "\033[004q"},
677 {K_F5, "\033[005q"},
678 {K_F6, "\033[006q"},
679 {K_F7, "\033[007q"},
680 {K_F8, "\033[008q"},
681 {K_F9, "\033[009q"},
682 {K_F10, "\033[010q"},
683 {K_F11, "\033[011q"},
684 {K_F12, "\033[012q"},
685 {K_S_F1, "\033[013q"},
686 {K_S_F2, "\033[014q"},
687 {K_S_F3, "\033[015q"},
688 {K_S_F4, "\033[016q"},
689 {K_S_F5, "\033[017q"},
690 {K_S_F6, "\033[018q"},
691 {K_S_F7, "\033[019q"},
692 {K_S_F8, "\033[020q"},
693 {K_S_F9, "\033[021q"},
694 {K_S_F10, "\033[022q"},
695 {K_S_F11, "\033[023q"},
696 {K_S_F12, "\033[024q"},
697 {K_INS, "\033[139q"},
698 {K_HOME, "\033[H"},
699 {K_END, "\033[146q"},
700 {K_PAGEUP, "\033[150q"},
701 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702
Bram Moolenaar4654d632022-11-17 22:05:12 +0000703 {(int)KS_NAME, NULL} // end marker
704};
705
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000707 * These codes are valid when nansi.sys or equivalent has been installed.
708 * Function keys on a PC are preceded with a NUL. These are converted into
709 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
710 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000712static tcap_entry_T builtin_pcansi[] = {
713 {(int)KS_DL, "\033[M"},
714 {(int)KS_AL, "\033[L"},
715 {(int)KS_CE, "\033[K"},
716 {(int)KS_CL, "\033[2J"},
717 {(int)KS_ME, "\033[0m"},
718 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
719 {(int)KS_MD, "\033[1m"}, // bold: white text
720 {(int)KS_SE, "\033[0m"}, // standout end
721 {(int)KS_SO, "\033[31m"}, // standout: white on blue
722 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
723 {(int)KS_CZR, "\033[0m"}, // italic mode end
724 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
725 {(int)KS_UE, "\033[0m"}, // underscore mode end
726 {(int)KS_CCO, "8"}, // allow 8 colors
727# ifdef TERMINFO
728 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
729 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
730# else
731 {(int)KS_CAB, "\033[4%dm"}, // set background color
732 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
733# endif
734 {(int)KS_OP, "\033[0m"}, // reset colors
735 {(int)KS_MS, "y"},
736 {(int)KS_UT, "y"}, // guessed
737 {(int)KS_LE, "\b"},
738# ifdef TERMINFO
739 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
740# else
741 {(int)KS_CM, "\033[%i%d;%dH"},
742# endif
743# ifdef TERMINFO
744 {(int)KS_CRI, "\033[%p1%dC"},
745# else
746 {(int)KS_CRI, "\033[%dC"},
747# endif
748 {K_UP, "\316H"},
749 {K_DOWN, "\316P"},
750 {K_LEFT, "\316K"},
751 {K_RIGHT, "\316M"},
752 {K_S_LEFT, "\316s"},
753 {K_S_RIGHT, "\316t"},
754 {K_F1, "\316;"},
755 {K_F2, "\316<"},
756 {K_F3, "\316="},
757 {K_F4, "\316>"},
758 {K_F5, "\316?"},
759 {K_F6, "\316@"},
760 {K_F7, "\316A"},
761 {K_F8, "\316B"},
762 {K_F9, "\316C"},
763 {K_F10, "\316D"},
764 {K_F11, "\316\205"}, // guessed
765 {K_F12, "\316\206"}, // guessed
766 {K_S_F1, "\316T"},
767 {K_S_F2, "\316U"},
768 {K_S_F3, "\316V"},
769 {K_S_F4, "\316W"},
770 {K_S_F5, "\316X"},
771 {K_S_F6, "\316Y"},
772 {K_S_F7, "\316Z"},
773 {K_S_F8, "\316["},
774 {K_S_F9, "\316\\"},
775 {K_S_F10, "\316]"},
776 {K_S_F11, "\316\207"}, // guessed
777 {K_S_F12, "\316\210"}, // guessed
778 {K_INS, "\316R"},
779 {K_DEL, "\316S"},
780 {K_HOME, "\316G"},
781 {K_END, "\316O"},
782 {K_PAGEDOWN, "\316Q"},
783 {K_PAGEUP, "\316I"},
784
785 {(int)KS_NAME, NULL} // end marker
786};
787
788/*
789 * These codes are valid for the Win32 Console . The entries that start with
790 * ESC | are translated into console calls in os_win32.c. The function keys
791 * are also translated in os_win32.c.
792 */
793static tcap_entry_T builtin_win32[] = {
794 {(int)KS_CE, "\033|K"}, // clear to end of line
795 {(int)KS_AL, "\033|L"}, // add new blank line
796# ifdef TERMINFO
797 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
798# else
799 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
800# endif
801 {(int)KS_DL, "\033|M"}, // delete line
802# ifdef TERMINFO
803 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
804 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
805# else
806 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
807 {(int)KS_CSV, "\033|%d;%dV"},
808# endif
809 {(int)KS_CL, "\033|J"}, // clear screen
810 {(int)KS_CD, "\033|j"}, // clear to end of display
811 {(int)KS_VI, "\033|v"}, // cursor invisible
812 {(int)KS_VE, "\033|V"}, // cursor visible
813
814 {(int)KS_ME, "\033|0m"}, // normal
815 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
816 {(int)KS_MD, "\033|15m"}, // bold: white on black
817#if 1
818 {(int)KS_SO, "\033|31m"}, // standout: white on blue
819 {(int)KS_SE, "\033|0m"}, // standout end
820#else
821 {(int)KS_SO, "\033|F"}, // standout: high intensity
822 {(int)KS_SE, "\033|f"}, // standout end
823#endif
824 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
825 {(int)KS_CZR, "\033|0m"}, // italic end
826 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
827 {(int)KS_UE, "\033|0m"}, // underscore end
828 {(int)KS_CCO, "16"}, // allow 16 colors
829# ifdef TERMINFO
830 {(int)KS_CAB, "\033|%p1%db"}, // set background color
831 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
832# else
833 {(int)KS_CAB, "\033|%db"}, // set background color
834 {(int)KS_CAF, "\033|%df"}, // set foreground color
835# endif
836
837 {(int)KS_MS, "y"}, // save to move cur in reverse mode
838 {(int)KS_UT, "y"},
839 {(int)KS_XN, "y"},
840 {(int)KS_LE, "\b"},
841# ifdef TERMINFO
842 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
843# else
844 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
845# endif
846 {(int)KS_VB, "\033|B"}, // visual bell
847 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
848 {(int)KS_TE, "\033|E"}, // out of termcap mode
849# ifdef TERMINFO
850 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
851# else
852 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
853# endif
854# ifdef FEAT_TERMGUICOLORS
855 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
856 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
857# endif
858
859 {K_UP, "\316H"},
860 {K_DOWN, "\316P"},
861 {K_LEFT, "\316K"},
862 {K_RIGHT, "\316M"},
863 {K_S_UP, "\316\304"},
864 {K_S_DOWN, "\316\317"},
865 {K_S_LEFT, "\316\311"},
866 {K_C_LEFT, "\316s"},
867 {K_S_RIGHT, "\316\313"},
868 {K_C_RIGHT, "\316t"},
869 {K_S_TAB, "\316\017"},
870 {K_F1, "\316;"},
871 {K_F2, "\316<"},
872 {K_F3, "\316="},
873 {K_F4, "\316>"},
874 {K_F5, "\316?"},
875 {K_F6, "\316@"},
876 {K_F7, "\316A"},
877 {K_F8, "\316B"},
878 {K_F9, "\316C"},
879 {K_F10, "\316D"},
880 {K_F11, "\316\205"},
881 {K_F12, "\316\206"},
882 {K_S_F1, "\316T"},
883 {K_S_F2, "\316U"},
884 {K_S_F3, "\316V"},
885 {K_S_F4, "\316W"},
886 {K_S_F5, "\316X"},
887 {K_S_F6, "\316Y"},
888 {K_S_F7, "\316Z"},
889 {K_S_F8, "\316["},
890 {K_S_F9, "\316\\"},
891 {K_S_F10, "\316]"},
892 {K_S_F11, "\316\207"},
893 {K_S_F12, "\316\210"},
894 {K_INS, "\316R"},
895 {K_DEL, "\316S"},
896 {K_HOME, "\316G"},
897 {K_S_HOME, "\316\302"},
898 {K_C_HOME, "\316w"},
899 {K_END, "\316O"},
900 {K_S_END, "\316\315"},
901 {K_C_END, "\316u"},
902 {K_PAGEDOWN, "\316Q"},
903 {K_PAGEUP, "\316I"},
904 {K_KPLUS, "\316N"},
905 {K_KMINUS, "\316J"},
906 {K_KMULTIPLY, "\316\067"},
907 {K_K0, "\316\332"},
908 {K_K1, "\316\336"},
909 {K_K2, "\316\342"},
910 {K_K3, "\316\346"},
911 {K_K4, "\316\352"},
912 {K_K5, "\316\356"},
913 {K_K6, "\316\362"},
914 {K_K7, "\316\366"},
915 {K_K8, "\316\372"},
916 {K_K9, "\316\376"},
917 {K_BS, "\316x"},
918 {K_S_BS, "\316y"},
919
920 {(int)KS_NAME, NULL} // end marker
921};
922
923#if defined(FEAT_GUI)
924/*
925 * GUI uses made-up codes, only used inside Vim.
926 */
927static tcap_entry_T builtin_gui[] = {
928 {(int)KS_CE, "\033|$"},
929 {(int)KS_AL, "\033|i"},
930# ifdef TERMINFO
931 {(int)KS_CAL, "\033|%p1%dI"},
932# else
933 {(int)KS_CAL, "\033|%dI"},
934# endif
935 {(int)KS_DL, "\033|d"},
936# ifdef TERMINFO
937 {(int)KS_CDL, "\033|%p1%dD"},
938 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
939 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
940# else
941 {(int)KS_CDL, "\033|%dD"},
942 {(int)KS_CS, "\033|%d;%dR"},
943 {(int)KS_CSV, "\033|%d;%dV"},
944# endif
945 {(int)KS_CL, "\033|C"},
946 // attributes switched on with 'h', off with * 'H'
947 {(int)KS_ME, "\033|31H"}, // HL_ALL
948 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
949 {(int)KS_MD, "\033|2h"}, // HL_BOLD
950 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
951 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
952 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
953 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
954 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
955 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
956 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
957 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
958 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
959 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
960 {(int)KS_VB, "\033|f"},
961 {(int)KS_MS, "y"},
962 {(int)KS_UT, "y"},
963 {(int)KS_XN, "y"},
964 {(int)KS_LE, "\b"}, // cursor-left = BS
965 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
966# ifdef TERMINFO
967 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
968# else
969 {(int)KS_CM, "\033|%d;%dM"},
970# endif
971 // there are no key sequences here, the GUI sequences are recognized
972 // in check_termcode()
973
974 {(int)KS_NAME, NULL} // end marker
975};
976#endif
977
978/*
979 * Amiga console window, default for Amiga.
980 */
981static tcap_entry_T builtin_amiga[] = {
982 {(int)KS_CE, "\033[K"},
983 {(int)KS_CD, "\033[J"},
984 {(int)KS_AL, "\033[L"},
985# ifdef TERMINFO
986 {(int)KS_CAL, "\033[%p1%dL"},
987# else
988 {(int)KS_CAL, "\033[%dL"},
989# endif
990 {(int)KS_DL, "\033[M"},
991# ifdef TERMINFO
992 {(int)KS_CDL, "\033[%p1%dM"},
993# else
994 {(int)KS_CDL, "\033[%dM"},
995# endif
996 {(int)KS_CL, "\014"},
997 {(int)KS_VI, "\033[0 p"},
998 {(int)KS_VE, "\033[1 p"},
999 {(int)KS_ME, "\033[0m"},
1000 {(int)KS_MR, "\033[7m"},
1001 {(int)KS_MD, "\033[1m"},
1002 {(int)KS_SE, "\033[0m"},
1003 {(int)KS_SO, "\033[33m"},
1004 {(int)KS_US, "\033[4m"},
1005 {(int)KS_UE, "\033[0m"},
1006 {(int)KS_CZH, "\033[3m"},
1007 {(int)KS_CZR, "\033[0m"},
1008#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1009 {(int)KS_CCO, "8"}, // allow 8 colors
1010# ifdef TERMINFO
1011 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1012 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
1013# else
1014 {(int)KS_CAB, "\033[4%dm"}, // set background color
1015 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
1016# endif
1017 {(int)KS_OP, "\033[m"}, // reset colors
1018#endif
1019 {(int)KS_MS, "y"},
1020 {(int)KS_UT, "y"}, // guessed
1021 {(int)KS_LE, "\b"},
1022# ifdef TERMINFO
1023 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1024# else
1025 {(int)KS_CM, "\033[%i%d;%dH"},
1026# endif
1027#if defined(__MORPHOS__)
1028 {(int)KS_SR, "\033M"},
1029#endif
1030# ifdef TERMINFO
1031 {(int)KS_CRI, "\033[%p1%dC"},
1032# else
1033 {(int)KS_CRI, "\033[%dC"},
1034# endif
1035 {K_UP, "\233A"},
1036 {K_DOWN, "\233B"},
1037 {K_LEFT, "\233D"},
1038 {K_RIGHT, "\233C"},
1039 {K_S_UP, "\233T"},
1040 {K_S_DOWN, "\233S"},
1041 {K_S_LEFT, "\233 A"},
1042 {K_S_RIGHT, "\233 @"},
1043 {K_S_TAB, "\233Z"},
1044 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1045 {K_F2, "\233\061~"},
1046 {K_F3, "\233\062~"},
1047 {K_F4, "\233\063~"},
1048 {K_F5, "\233\064~"},
1049 {K_F6, "\233\065~"},
1050 {K_F7, "\233\066~"},
1051 {K_F8, "\233\067~"},
1052 {K_F9, "\233\070~"},
1053 {K_F10, "\233\071~"},
1054 {K_S_F1, "\233\061\060~"},
1055 {K_S_F2, "\233\061\061~"},
1056 {K_S_F3, "\233\061\062~"},
1057 {K_S_F4, "\233\061\063~"},
1058 {K_S_F5, "\233\061\064~"},
1059 {K_S_F6, "\233\061\065~"},
1060 {K_S_F7, "\233\061\066~"},
1061 {K_S_F8, "\233\061\067~"},
1062 {K_S_F9, "\233\061\070~"},
1063 {K_S_F10, "\233\061\071~"},
1064 {K_HELP, "\233?~"},
1065 {K_INS, "\233\064\060~"}, // 101 key keyboard
1066 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1067 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1068 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1069 {K_END, "\233\064\065~"}, // 101 key keyboard
1070
1071 {BT_EXTRA_KEYS, ""},
1072 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1073 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1074 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1075
1076 {(int)KS_NAME, NULL} // end marker
1077};
1078
1079/*
1080 * The most minimal terminal: only clear screen and cursor positioning.
1081 */
1082static tcap_entry_T builtin_dumb[] = {
1083 {(int)KS_CL, "\014"},
1084#ifdef TERMINFO
1085 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1086#else
1087 {(int)KS_CM, "\033[%i%d;%dH"},
1088#endif
1089
1090 {(int)KS_NAME, NULL} // end marker
1091};
1092
1093/*
1094 * Terminal used for debugging.
1095 */
1096static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 {(int)KS_CE, "[CE]"},
1098 {(int)KS_CD, "[CD]"},
1099 {(int)KS_AL, "[AL]"},
1100# ifdef TERMINFO
1101 {(int)KS_CAL, "[CAL%p1%d]"},
1102# else
1103 {(int)KS_CAL, "[CAL%d]"},
1104# endif
1105 {(int)KS_DL, "[DL]"},
1106# ifdef TERMINFO
1107 {(int)KS_CDL, "[CDL%p1%d]"},
1108# else
1109 {(int)KS_CDL, "[CDL%d]"},
1110# endif
1111# ifdef TERMINFO
1112 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1113# else
1114 {(int)KS_CS, "[%dCS%d]"},
1115# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001116# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001118# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120# endif
1121# ifdef TERMINFO
1122 {(int)KS_CAB, "[CAB%p1%d]"},
1123 {(int)KS_CAF, "[CAF%p1%d]"},
1124 {(int)KS_CSB, "[CSB%p1%d]"},
1125 {(int)KS_CSF, "[CSF%p1%d]"},
1126# else
1127 {(int)KS_CAB, "[CAB%d]"},
1128 {(int)KS_CAF, "[CAF%d]"},
1129 {(int)KS_CSB, "[CSB%d]"},
1130 {(int)KS_CSF, "[CSF%d]"},
1131# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001132 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 {(int)KS_OP, "[OP]"},
1134 {(int)KS_LE, "[LE]"},
1135 {(int)KS_CL, "[CL]"},
1136 {(int)KS_VI, "[VI]"},
1137 {(int)KS_VE, "[VE]"},
1138 {(int)KS_VS, "[VS]"},
1139 {(int)KS_ME, "[ME]"},
1140 {(int)KS_MR, "[MR]"},
1141 {(int)KS_MB, "[MB]"},
1142 {(int)KS_MD, "[MD]"},
1143 {(int)KS_SE, "[SE]"},
1144 {(int)KS_SO, "[SO]"},
1145 {(int)KS_UE, "[UE]"},
1146 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001147 {(int)KS_UCE, "[UCE]"},
1148 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001149 {(int)KS_USS, "[USS]"},
1150 {(int)KS_DS, "[DS]"},
1151 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001152 {(int)KS_STE, "[STE]"},
1153 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {(int)KS_MS, "[MS]"},
1155 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001156 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157# ifdef TERMINFO
1158 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1159# else
1160 {(int)KS_CM, "[%dCM%d]"},
1161# endif
1162 {(int)KS_SR, "[SR]"},
1163# ifdef TERMINFO
1164 {(int)KS_CRI, "[CRI%p1%d]"},
1165# else
1166 {(int)KS_CRI, "[CRI%d]"},
1167# endif
1168 {(int)KS_VB, "[VB]"},
1169 {(int)KS_KS, "[KS]"},
1170 {(int)KS_KE, "[KE]"},
1171 {(int)KS_TI, "[TI]"},
1172 {(int)KS_TE, "[TE]"},
1173 {(int)KS_CIS, "[CIS]"},
1174 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001175 {(int)KS_CSC, "[CSC]"},
1176 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 {(int)KS_TS, "[TS]"},
1178 {(int)KS_FS, "[FS]"},
1179# ifdef TERMINFO
1180 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1181 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1182# else
1183 {(int)KS_CWS, "[%dCWS%d]"},
1184 {(int)KS_CWP, "[%dCWP%d]"},
1185# endif
1186 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001187 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001188 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001189 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {K_UP, "[KU]"},
1191 {K_DOWN, "[KD]"},
1192 {K_LEFT, "[KL]"},
1193 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001194 {K_XUP, "[xKU]"},
1195 {K_XDOWN, "[xKD]"},
1196 {K_XLEFT, "[xKL]"},
1197 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {K_S_UP, "[S-KU]"},
1199 {K_S_DOWN, "[S-KD]"},
1200 {K_S_LEFT, "[S-KL]"},
1201 {K_C_LEFT, "[C-KL]"},
1202 {K_S_RIGHT, "[S-KR]"},
1203 {K_C_RIGHT, "[C-KR]"},
1204 {K_F1, "[F1]"},
1205 {K_XF1, "[xF1]"},
1206 {K_F2, "[F2]"},
1207 {K_XF2, "[xF2]"},
1208 {K_F3, "[F3]"},
1209 {K_XF3, "[xF3]"},
1210 {K_F4, "[F4]"},
1211 {K_XF4, "[xF4]"},
1212 {K_F5, "[F5]"},
1213 {K_F6, "[F6]"},
1214 {K_F7, "[F7]"},
1215 {K_F8, "[F8]"},
1216 {K_F9, "[F9]"},
1217 {K_F10, "[F10]"},
1218 {K_F11, "[F11]"},
1219 {K_F12, "[F12]"},
1220 {K_S_F1, "[S-F1]"},
1221 {K_S_XF1, "[S-xF1]"},
1222 {K_S_F2, "[S-F2]"},
1223 {K_S_XF2, "[S-xF2]"},
1224 {K_S_F3, "[S-F3]"},
1225 {K_S_XF3, "[S-xF3]"},
1226 {K_S_F4, "[S-F4]"},
1227 {K_S_XF4, "[S-xF4]"},
1228 {K_S_F5, "[S-F5]"},
1229 {K_S_F6, "[S-F6]"},
1230 {K_S_F7, "[S-F7]"},
1231 {K_S_F8, "[S-F8]"},
1232 {K_S_F9, "[S-F9]"},
1233 {K_S_F10, "[S-F10]"},
1234 {K_S_F11, "[S-F11]"},
1235 {K_S_F12, "[S-F12]"},
1236 {K_HELP, "[HELP]"},
1237 {K_UNDO, "[UNDO]"},
1238 {K_BS, "[BS]"},
1239 {K_INS, "[INS]"},
1240 {K_KINS, "[KINS]"},
1241 {K_DEL, "[DEL]"},
1242 {K_KDEL, "[KDEL]"},
1243 {K_HOME, "[HOME]"},
1244 {K_S_HOME, "[C-HOME]"},
1245 {K_C_HOME, "[C-HOME]"},
1246 {K_KHOME, "[KHOME]"},
1247 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001248 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 {K_END, "[END]"},
1250 {K_S_END, "[C-END]"},
1251 {K_C_END, "[C-END]"},
1252 {K_KEND, "[KEND]"},
1253 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001254 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 {K_PAGEUP, "[PAGEUP]"},
1256 {K_PAGEDOWN, "[PAGEDOWN]"},
1257 {K_KPAGEUP, "[KPAGEUP]"},
1258 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1259 {K_MOUSE, "[MOUSE]"},
1260 {K_KPLUS, "[KPLUS]"},
1261 {K_KMINUS, "[KMINUS]"},
1262 {K_KDIVIDE, "[KDIVIDE]"},
1263 {K_KMULTIPLY, "[KMULTIPLY]"},
1264 {K_KENTER, "[KENTER]"},
1265 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001266 {K_PS, "[PASTE-START]"},
1267 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {K_K0, "[K0]"},
1269 {K_K1, "[K1]"},
1270 {K_K2, "[K2]"},
1271 {K_K3, "[K3]"},
1272 {K_K4, "[K4]"},
1273 {K_K5, "[K5]"},
1274 {K_K6, "[K6]"},
1275 {K_K7, "[K7]"},
1276 {K_K8, "[K8]"},
1277 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278
Bram Moolenaar4654d632022-11-17 22:05:12 +00001279 {(int)KS_NAME, NULL} // end marker
1280};
1281
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001283 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001285typedef struct {
1286 char *bitc_name; // name, such as "xterm"
1287 tcap_entry_T *bitc_table; // table with entries for bitc_name
1288} builtin_tcap_T;
1289
1290builtin_tcap_T builtin_terminals[] = {
1291 // Unix and Generic
1292 {"ansi", builtin_ansi},
1293 {"vt320", builtin_vt320},
1294 {"vt52", builtin_vt52},
1295 {"xterm", builtin_xterm},
1296 {"iris-ansi", builtin_iris_ansi},
1297
1298 // MS-Windows
1299 {"pcansi", builtin_pcansi},
1300 {"win32", builtin_win32},
1301
1302 // Other systems
1303#if defined(FEAT_GUI)
1304 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001306 {"amiga", builtin_amiga},
1307 {"dumb", builtin_dumb},
1308 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309
Bram Moolenaar4654d632022-11-17 22:05:12 +00001310 {NULL, NULL}, // end marker
1311};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001313#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001314 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001315termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001316{
Bram Moolenaarab302212016-04-26 20:59:29 +02001317 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001318}
1319
1320 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001321termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001322{
1323 guicolor_T t;
1324
1325 if (*name == NUL)
1326 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001327 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001328
1329 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001330 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001331 return t;
1332}
1333
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001334 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001335termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001336{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001337 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001338}
1339#endif
1340
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341/*
1342 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344#ifdef AMIGA
1345# define DEFAULT_TERM (char_u *)"amiga"
1346#endif
1347
1348#ifdef MSWIN
1349# define DEFAULT_TERM (char_u *)"win32"
1350#endif
1351
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001352#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353# define DEFAULT_TERM (char_u *)"ansi"
1354#endif
1355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356#ifdef VMS
1357# define DEFAULT_TERM (char_u *)"vt320"
1358#endif
1359
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001360#ifdef __HAIKU__
1361# undef DEFAULT_TERM
1362# define DEFAULT_TERM (char_u *)"xterm"
1363#endif
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365#ifndef DEFAULT_TERM
1366# define DEFAULT_TERM (char_u *)"dumb"
1367#endif
1368
1369/*
1370 * Term_strings contains currently used terminal output strings.
1371 * It is initialized with the default values by parse_builtin_tcap().
1372 * The values can be changed by setting the option with the same name.
1373 */
1374char_u *(term_strings[(int)KS_LAST + 1]);
1375
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001376static int need_gather = FALSE; // need to fill termleader[]
1377static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001379static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001380
1381/*
1382 * Structure and table to store terminal features that can be detected by
1383 * querying the terminal. Either by inspecting the termresponse or a more
1384 * specific request. Besides this there are:
1385 * t_colors - number of colors supported
1386 */
1387typedef struct {
1388 char *tpr_name;
1389 int tpr_set_by_termresponse;
1390 int tpr_status;
1391} termprop_T;
1392
1393// Values for tpr_status.
1394#define TPR_UNKNOWN 'u'
1395#define TPR_YES 'y'
1396#define TPR_NO 'n'
1397#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1398#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1399#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1400
1401// can request the cursor style without messing up the display
1402#define TPR_CURSOR_STYLE 0
1403// can request the cursor blink mode without messing up the display
1404#define TPR_CURSOR_BLINK 1
1405// can set the underline color with t_8u without resetting other colors
1406#define TPR_UNDERLINE_RGB 2
1407// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1408#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001409// term response indicates kitty
1410#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001411// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001412#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001413
1414static termprop_T term_props[TPR_COUNT];
1415
1416/*
1417 * Initialize the term_props table.
1418 * When "all" is FALSE only set those that are detected from the version
1419 * response.
1420 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001421 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001422init_term_props(int all)
1423{
1424 int i;
1425
1426 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1427 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1428 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1429 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1430 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1431 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1432 term_props[TPR_MOUSE].tpr_name = "mouse";
1433 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001434 term_props[TPR_KITTY].tpr_name = "kitty";
1435 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001436
1437 for (i = 0; i < TPR_COUNT; ++i)
1438 if (all || term_props[i].tpr_set_by_termresponse)
1439 term_props[i].tpr_status = TPR_UNKNOWN;
1440}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441#endif
1442
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001443#if defined(FEAT_EVAL) || defined(PROTO)
1444 void
1445f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1446{
1447# ifdef FEAT_TERMRESPONSE
1448 int i;
1449# endif
1450
Bram Moolenaar93a10962022-06-16 11:42:09 +01001451 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001452 return;
1453# ifdef FEAT_TERMRESPONSE
1454 for (i = 0; i < TPR_COUNT; ++i)
1455 {
1456 char_u value[2];
1457
1458 value[0] = term_props[i].tpr_status;
1459 value[1] = NUL;
1460 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1461 }
1462# endif
1463}
1464#endif
1465
Bram Moolenaar4654d632022-11-17 22:05:12 +00001466/*
1467 * Find the builtin termcap entries for "term".
1468 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1469 * entry.
1470 * Returns NULL when "term" is not found.
1471 */
1472 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001473find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001475 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001477 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1478 if (name == NULL) // end marker
1479 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001481 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1482 return builtin_terminals[i].bitc_table;
1483 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1484 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485#endif
1486#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001487 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1488 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001490 if (STRCMP(term, name) == 0)
1491 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001493 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494}
1495
1496/*
1497 * Parsing of the builtin termcap entries.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001498 * Caller should check if "term" is a valid builtin terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 * The terminal's name is not set, as this is already done in termcapinit().
1500 */
1501 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001502parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001504 tcap_entry_T *p = find_builtin_term(term);
1505 if (p == NULL) // builtin term not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 return;
1507
Bram Moolenaar4654d632022-11-17 22:05:12 +00001508 int term_8bit = term_is_8bit(term);
1509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1511 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001512 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001514 // Only set the value if it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 if (term_strings[p->bt_entry] == NULL
1516 || term_strings[p->bt_entry] == empty_option)
1517 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001518#ifdef FEAT_EVAL
1519 int opt_idx = -1;
1520#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001521 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1523 {
1524 char_u *s, *t;
1525
1526 s = vim_strsave((char_u *)p->bt_string);
1527 if (s != NULL)
1528 {
1529 for (t = s; *t; ++t)
1530 if (term_7to8bit(t))
1531 {
1532 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001533 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 }
1535 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001536#ifdef FEAT_EVAL
1537 opt_idx =
1538#endif
1539 set_term_option_alloced(
1540 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 }
1542 }
1543 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001544 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001546#ifdef FEAT_EVAL
1547 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1548#endif
1549 }
1550#ifdef FEAT_EVAL
1551 set_term_option_sctx_idx(NULL, opt_idx);
1552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 }
1554 }
1555 else
1556 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001557 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1559 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1560 if (find_termcode(name) == NULL)
1561 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1562 }
1563 }
1564}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001565
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566/*
1567 * Set number of colors.
1568 * Store it as a number in t_colors.
1569 * Store it as a string in T_CCO (using nr_colors[]).
1570 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001571 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001572set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001574 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576 t_colors = nr;
1577 if (t_colors > 1)
1578 sprintf((char *)nr_colors, "%d", t_colors);
1579 else
1580 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001581 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001583
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001584#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001585/*
1586 * Set the color count to "val" and redraw if it changed.
1587 */
1588 static void
1589may_adjust_color_count(int val)
1590{
1591 if (val != t_colors)
1592 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001593 // Nr of colors changed, initialize highlighting and
1594 // redraw everything. This causes a redraw, which usually
1595 // clears the message. Try keeping the message if it
1596 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001597 set_keep_msg_from_hist();
1598 set_color_count(val);
1599 init_highlight(TRUE, FALSE);
1600# ifdef DEBUG_TERMRESPONSE
1601 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001602 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001603
Bram Moolenaarb255b902018-04-24 21:40:10 +02001604 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001605 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001606# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001607 redraw_asap(UPD_CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001608# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001609 }
1610}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611#endif
1612
1613#ifdef HAVE_TGETENT
1614static char *(key_names[]) =
1615{
Bram Moolenaar87202262020-05-24 17:23:45 +02001616# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001617 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 "#2", "#4", "%i", "*7",
1622 "k1", "k2", "k3", "k4", "k5", "k6",
1623 "k7", "k8", "k9", "k;", "F1", "F2",
1624 "%1", "&8", "kb", "kI", "kD", "kh",
1625 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1626 NULL
1627};
1628#endif
1629
Bram Moolenaar9289df52018-05-10 14:40:57 +02001630#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001631 static void
1632get_term_entries(int *height, int *width)
1633{
1634 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001635 enum SpecialKey dest; // index in term_strings[]
1636 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001637 } string_names[] =
1638 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1639 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1640 {KS_CL, "cl"}, {KS_CD, "cd"},
1641 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1642 {KS_ME, "me"}, {KS_MR, "mr"},
1643 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1644 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1645 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001646 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001647 {KS_STE,"Te"}, {KS_STS,"Ts"},
1648 {KS_CM, "cm"}, {KS_SR, "sr"},
1649 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1650 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001651 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001652 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001653 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1654 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001655 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1656 {KS_VS, "vs"}, {KS_CVS, "VS"},
1657 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1658 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1659 {KS_TS, "ts"}, {KS_FS, "fs"},
1660 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1661 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1662 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001663 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001664 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1665 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001666 {KS_CST, "ST"}, {KS_CRT, "RT"},
1667 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001668 {(enum SpecialKey)0, NULL}
1669 };
1670 int i;
1671 char_u *p;
1672 static char_u tstrbuf[TBUFSZ];
1673 char_u *tp = tstrbuf;
1674
1675 /*
1676 * get output strings
1677 */
1678 for (i = 0; string_names[i].name != NULL; ++i)
1679 {
1680 if (TERM_STR(string_names[i].dest) == NULL
1681 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001682 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001683 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001684#ifdef FEAT_EVAL
1685 set_term_option_sctx_idx(string_names[i].name, -1);
1686#endif
1687 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001688 }
1689
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001690 // tgetflag() returns 1 if the flag is present, 0 if not and
1691 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001692 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1693 T_MS = (char_u *)"y";
1694 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1695 T_XS = (char_u *)"y";
1696 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1697 T_XN = (char_u *)"y";
1698 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1699 T_DB = (char_u *)"y";
1700 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1701 T_DA = (char_u *)"y";
1702 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1703 T_UT = (char_u *)"y";
1704
1705 /*
1706 * get key codes
1707 */
1708 for (i = 0; key_names[i] != NULL; ++i)
1709 if (find_termcode((char_u *)key_names[i]) == NULL)
1710 {
1711 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001712 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001713 if (p != NULL
1714 && (*p != Ctrl_H
1715 || key_names[i][0] != 'k'
1716 || key_names[i][1] != 'l'))
1717 add_termcode((char_u *)key_names[i], p, FALSE);
1718 }
1719
1720 if (*height == 0)
1721 *height = tgetnum("li");
1722 if (*width == 0)
1723 *width = tgetnum("co");
1724
1725 /*
1726 * Get number of colors (if not done already).
1727 */
1728 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001729 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001730 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001731#ifdef FEAT_EVAL
1732 set_term_option_sctx_idx("Co", -1);
1733#endif
1734 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001735
1736# ifndef hpux
1737 BC = (char *)TGETSTR("bc", &tp);
1738 UP = (char *)TGETSTR("up", &tp);
1739 p = TGETSTR("pc", &tp);
1740 if (p)
1741 PC = *p;
1742# endif
1743}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001744#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001745
Bram Moolenaar4654d632022-11-17 22:05:12 +00001746/*
1747 * Report "term" is not found and list the ones we do know about.
1748 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001749 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001750report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001751{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001752 mch_errmsg("\r\n");
1753 if (error_msg != NULL)
1754 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001755 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001756 mch_errmsg("\r\n");
1757 }
1758 mch_errmsg("'");
1759 mch_errmsg((char *)term);
1760 mch_errmsg(_("' not known. Available builtin terminals are:"));
1761 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001762
1763 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001764 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001765 char *name = builtin_terminals[i].bitc_name;
1766 if (name == NULL) // end marker
1767 break;
1768 // Do not mention the "gui" entry, the user won't need to type it.
1769 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001770 {
1771#ifdef HAVE_TGETENT
1772 mch_errmsg(" builtin_");
1773#else
1774 mch_errmsg(" ");
1775#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001776 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001777 mch_errmsg("\r\n");
1778 }
1779 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001780 // Output extra 'cmdheight' line breaks to avoid that the following error
1781 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001782 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001783 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001784}
1785
1786 static void
1787report_default_term(char_u *term)
1788{
1789 mch_errmsg(_("defaulting to '"));
1790 mch_errmsg((char *)term);
1791 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001792 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001793 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001794 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001795 out_flush();
1796 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001797 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001798 }
1799}
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801/*
1802 * Set terminal options for terminal "term".
1803 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1804 *
1805 * While doing this, until ttest(), some options may be NULL, be careful.
1806 */
1807 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001808set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810#ifdef HAVE_TGETENT
1811 int builtin_first = p_tbi;
1812 int try;
1813 int termcap_cleared = FALSE;
1814#endif
1815 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001816 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 char_u *bs_p, *del_p;
1818
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001819 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001820 if (silent_mode)
1821 return OK;
1822
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001823 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825 if (term_is_builtin(term))
1826 {
1827 term += 8;
1828#ifdef HAVE_TGETENT
1829 builtin_first = 1;
1830#endif
1831 }
1832
1833/*
1834 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1835 * If builtin_first is TRUE:
1836 * 0. try builtin termcap
1837 * 1. try external termcap
1838 * 2. if both fail default to a builtin terminal
1839 * If builtin_first is FALSE:
1840 * 1. try external termcap
1841 * 2. try builtin termcap, if both fail default to a builtin terminal
1842 */
1843#ifdef HAVE_TGETENT
1844 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1845 {
1846 /*
1847 * Use external termcap
1848 */
1849 if (try == 1)
1850 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
1853 /*
1854 * If the external termcap does not have a matching entry, try the
1855 * builtin ones.
1856 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01001857 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (!termcap_cleared)
1860 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001861 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 termcap_cleared = TRUE;
1863 }
1864
Bram Moolenaar69e05692018-05-10 14:11:52 +02001865 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 }
1867 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001868 else // try == 0 || try == 2
1869#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 /*
1871 * Use builtin termcap
1872 */
1873 {
1874#ifdef HAVE_TGETENT
1875 /*
1876 * If builtin termcap was already used, there is no need to search
1877 * for the builtin termcap again, quit now.
1878 */
1879 if (try == 2 && builtin_first && termcap_cleared)
1880 break;
1881#endif
1882 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001883 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001885 tcap_entry_T *termp = find_builtin_term(term);
1886 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
1888#ifdef HAVE_TGETENT
1889 /*
1890 * If try == 0, first try the external termcap. If that is not
1891 * found we'll get back here with try == 2.
1892 * If termcap_cleared is set we used the external termcap,
1893 * don't complain about not finding the term in the builtin
1894 * termcap.
1895 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001896 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001898 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 break;
1900#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001901 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001903 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (starting != NO_SCREEN)
1905 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001906 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 wait_return(TRUE);
1908 return FAIL;
1909 }
1910 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001911 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001912 set_string_option_direct((char_u *)"term", -1, term,
1913 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 display_errors();
1915 }
1916 out_flush();
1917#ifdef HAVE_TGETENT
1918 if (!termcap_cleared)
1919 {
1920#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001921 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922#ifdef HAVE_TGETENT
1923 termcap_cleared = TRUE;
1924 }
1925#endif
1926 parse_builtin_tcap(term);
1927#ifdef FEAT_GUI
1928 if (term_is_gui(term))
1929 {
1930 out_flush();
1931 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001932 // If starting the GUI failed, don't do any of the other
1933 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 if (!gui.in_use)
1935 return FAIL;
1936#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001937 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938#endif
1939 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001940#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
1942#ifdef HAVE_TGETENT
1943 }
1944#endif
1945
1946/*
1947 * special: There is no info in the termcap about whether the cursor
1948 * positioning is relative to the start of the screen or to the start of the
1949 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1950 * relative.
1951 */
1952 if (STRCMP(term, "pcterm") == 0)
1953 T_CCS = (char_u *)"yes";
1954 else
1955 T_CCS = empty_option;
1956
1957#ifdef UNIX
1958/*
1959 * Any "stty" settings override the default for t_kb from the termcap.
1960 * This is in os_unix.c, because it depends a lot on the version of unix that
1961 * is being used.
1962 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1963 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001964# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 get_stty();
1968#endif
1969
1970/*
1971 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1972 * didn't work, use the default CTRL-H
1973 * The default for t_kD is DEL, unless t_kb is DEL.
1974 * The vim_strsave'd strings are probably lost forever, well it's only two
1975 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1976 * directly.
1977 */
1978#ifdef FEAT_GUI
1979 if (!gui.in_use)
1980#endif
1981 {
1982 bs_p = find_termcode((char_u *)"kb");
1983 del_p = find_termcode((char_u *)"kD");
1984 if (bs_p == NULL || *bs_p == NUL)
1985 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1986 if ((del_p == NULL || *del_p == NUL) &&
1987 (bs_p == NULL || *bs_p != DEL))
1988 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1989 }
1990
1991#if defined(UNIX) || defined(VMS)
1992 term_is_xterm = vim_is_xterm(term);
1993#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02001994#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001995 // Reset terminal properties that are set based on the termresponse, which
1996 // will be sent out soon.
1997 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02001998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002000#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 /*
2002 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2003 * The termcode for the mouse is added as a side effect in option.c.
2004 */
2005 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002006 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002008# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002009 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
2011 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002012 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 else
2014 p = (char_u *)"xterm";
2015 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002016# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002018 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002019 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002020 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2021 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002022 reset_option_was_set((char_u *)"ttym");
2023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002025# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002029 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002031#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034
Bram Moolenaarbf789742021-01-16 11:21:40 +01002035#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002036 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002037 if (use_xterm_like_mouse(term))
2038 {
2039 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002040
2041 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002042 name[0] = KS_EXTRA;
2043 name[1] = KE_FOCUSGAINED;
2044 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002045 add_termcode(name, (char_u *)"\033[I", FALSE);
2046
2047 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002048 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002049 add_termcode(name, (char_u *)"\033[O", FALSE);
2050
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002051 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002052 }
2053#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002054#if (defined(UNIX) || defined(VMS))
2055 // First time after setting 'term' a focus event is always reported.
2056 focus_state = MAYBE;
2057#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002058
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002060 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 if (STRCMP(term, DEFAULT_TERM) != 0)
2062 term_console = FALSE;
2063 else
2064 {
2065 term_console = TRUE;
2066# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002067 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068# endif
2069 }
2070#endif
2071
2072#if defined(UNIX) || defined(VMS)
2073 /*
2074 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2075 */
2076 if (vim_is_fastterm(term))
2077 p_tf = TRUE;
2078#endif
2079#ifdef USE_TERM_CONSOLE
2080 /*
2081 * 'ttyfast' is default on consoles
2082 */
2083 if (term_console)
2084 p_tf = TRUE;
2085#endif
2086
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002087 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002089 full_screen = TRUE; // we can use termcap codes from now on
2090 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002092 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002093 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002094 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095#endif
2096
2097 /*
2098 * Initialize the terminal with the appropriate termcap codes.
2099 * Set the mouse and window title if possible.
2100 * Don't do this when starting, need to parse the .vimrc first, because it
2101 * may redefine t_TI etc.
2102 */
2103 if (starting != NO_SCREEN)
2104 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002105 starttermcap(); // may change terminal mode
2106 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002107 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 }
2109
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002110 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 if (width <= 0 || height <= 0)
2112 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002113 // termcap failed to report size
2114 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002116#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002117 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002119 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120#endif
2121 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002122 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 if (starting != NO_SCREEN)
2124 {
2125 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002126 scroll_region_reset(); // In case Rows changed
2127 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002130 buf_T *buf;
2131 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
2133 /*
2134 * Execute the TermChanged autocommands for each buffer that is
2135 * loaded.
2136 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002137 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
2139 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002140 {
2141 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2143 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002144 // restore curwin/curbuf and a few other things
2145 aucmd_restbuf(&aco);
2146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 }
2150
2151#ifdef FEAT_TERMRESPONSE
2152 may_req_termresponse();
2153#endif
2154
2155 return OK;
2156}
2157
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002158#if defined(EXITFREE) || defined(PROTO)
2159
2160# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002161# include <term.h> // declares cur_term
2162# endif
2163
2164/*
2165 * If supported, delete "cur_term", which caches terminal related entries.
2166 * Avoids that valgrind reports possibly lost memory.
2167 */
2168 void
2169free_cur_term()
2170{
2171# ifdef HAVE_DEL_CURTERM
2172 if (cur_term)
2173 del_curterm(cur_term);
2174# endif
2175}
2176
2177#endif
2178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179#ifdef HAVE_TGETENT
2180/*
2181 * Call tgetent()
2182 * Return error message if it fails, NULL if it's OK.
2183 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002184 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002185invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186{
2187 int i;
2188
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002189 // Note: Valgrind may report a leak here, because the library keeps one
2190 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002192 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002194 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195# endif
2196 )
2197 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002198 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2199 // tgetent() with the always existing "dumb" entry to avoid a crash or
2200 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 (void)TGETENT(tbuf, "dumb");
2202
2203 if (i < 0)
2204# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002205 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 if (i == 0)
2207# endif
2208#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002209 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002211 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212#endif
2213 }
2214 return NULL;
2215}
2216
2217/*
2218 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2219 * Fix that here.
2220 */
2221 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002222vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223{
2224 char *p;
2225
2226 p = tgetstr(s, (char **)pp);
2227 if (p == (char *)-1)
2228 p = NULL;
2229 return (char_u *)p;
2230}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002231#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002233#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234/*
2235 * Get Columns and Rows from the termcap. Used after a window signal if the
2236 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2237 * and "li" entries never change. But on some systems this works.
2238 * Errors while getting the entries are ignored.
2239 */
2240 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002241getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002242 long *cp, // pointer to columns
2243 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244{
2245 char_u tbuf[TBUFSZ];
2246
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002247 if (T_NAME != NULL && *T_NAME != NUL && invoke_tgetent(tbuf, T_NAME) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
2249 if (*cp == 0)
2250 *cp = tgetnum("co");
2251 if (*rp == 0)
2252 *rp = tgetnum("li");
2253 }
2254}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002255#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256
2257/*
2258 * Get a string entry from the termcap and add it to the list of termcodes.
2259 * Used for <t_xx> special keys.
2260 * Give an error message for failure when not sourcing.
2261 * If force given, replace an existing entry.
2262 * Return FAIL if the entry was not found, OK if the entry was added.
2263 */
2264 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002265add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 char_u *term;
2268 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269#ifdef HAVE_TGETENT
2270 char_u *string;
2271 int i;
2272 int builtin_first;
2273 char_u tbuf[TBUFSZ];
2274 char_u tstrbuf[TBUFSZ];
2275 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002276 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277#endif
2278
2279/*
2280 * If the GUI is running or will start in a moment, we only support the keys
2281 * that the GUI can produce.
2282 */
2283#ifdef FEAT_GUI
2284 if (gui.in_use || gui.starting)
2285 return gui_mch_haskey(name);
2286#endif
2287
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002288 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 return OK;
2290
2291 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002292 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 return FAIL;
2294
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002295 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 term += 8;
2298#ifdef HAVE_TGETENT
2299 builtin_first = TRUE;
2300#endif
2301 }
2302#ifdef HAVE_TGETENT
2303 else
2304 builtin_first = p_tbi;
2305#endif
2306
2307#ifdef HAVE_TGETENT
2308/*
2309 * We can get the entry from the builtin termcap and from the external one.
2310 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2311 * builtin termcap first.
2312 * If 'ttybuiltin' is off, try external termcap first.
2313 */
2314 for (i = 0; i < 2; ++i)
2315 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002316 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317#endif
2318 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002319 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 */
2321 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002322 tcap_entry_T *termp = find_builtin_term(term);
2323 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
2325 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002326 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 while (termp->bt_entry != (int)KS_NAME)
2328 {
2329 if ((int)termp->bt_entry == key)
2330 {
2331 add_termcode(name, (char_u *)termp->bt_string,
2332 term_is_8bit(term));
2333 return OK;
2334 }
2335 ++termp;
2336 }
2337 }
2338 }
2339#ifdef HAVE_TGETENT
2340 else
2341 /*
2342 * Search in external termcap
2343 */
2344 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002345 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 if (error_msg == NULL)
2347 {
2348 string = TGETSTR((char *)name, &tp);
2349 if (string != NULL && *string != NUL)
2350 {
2351 add_termcode(name, string, FALSE);
2352 return OK;
2353 }
2354 }
2355 }
2356 }
2357#endif
2358
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002359 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
2361#ifdef HAVE_TGETENT
2362 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002363 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 else
2365#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002366 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368 return FAIL;
2369}
2370
2371 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002372term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373{
2374 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2375}
2376
2377/*
2378 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2379 * Assume that the terminal is using 8-bit controls when the name contains
2380 * "8bit", like in "xterm-8bit".
2381 */
2382 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002383term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
2385 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2386}
2387
2388/*
2389 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002390 * <Esc>[ -> CSI <M_C_[>
2391 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 * <Esc>O -> <M-C-O>
2393 */
2394 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002395term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
2397 if (*p == ESC)
2398 {
2399 if (p[1] == '[')
2400 return CSI;
2401 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002402 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 if (p[1] == 'O')
2404 return 0x8f;
2405 }
2406 return 0;
2407}
2408
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002409#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002411term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412{
2413 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2414}
2415#endif
2416
2417#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2418
2419 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002420tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
2422 static char_u buf[16];
2423 char_u *p;
2424
2425 p = buf + 15;
2426 *p = '\0';
2427 do
2428 {
2429 --p;
2430 *p = (char_u) (i % 10 + '0');
2431 i /= 10;
2432 }
2433 while (i > 0 && p > buf);
2434 return p;
2435}
2436#endif
2437
2438#ifndef HAVE_TGETENT
2439
2440/*
2441 * minimal tgoto() implementation.
2442 * no padding and we only parse for %i %d and %+char
2443 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002444 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002445tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446{
2447 static char buf[30];
2448 char *p, *s, *e;
2449
2450 if (!cm)
2451 return "OOPS";
2452 e = buf + 29;
2453 for (s = buf; s < e && *cm; cm++)
2454 {
2455 if (*cm != '%')
2456 {
2457 *s++ = *cm;
2458 continue;
2459 }
2460 switch (*++cm)
2461 {
2462 case 'd':
2463 p = (char *)tltoa((unsigned long)y);
2464 y = x;
2465 while (*p)
2466 *s++ = *p++;
2467 break;
2468 case 'i':
2469 x++;
2470 y++;
2471 break;
2472 case '+':
2473 *s++ = (char)(*++cm + y);
2474 y = x;
2475 break;
2476 case '%':
2477 *s++ = *cm;
2478 break;
2479 default:
2480 return "OOPS";
2481 }
2482 }
2483 *s = '\0';
2484 return buf;
2485}
2486
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002487#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
2489/*
2490 * Set the terminal name and initialize the terminal options.
2491 * If "name" is NULL or empty, get the terminal name from the environment.
2492 * If that fails, use the default terminal name.
2493 */
2494 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002495termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
2497 char_u *term;
2498
2499 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002500 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 term = name;
2502
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503#ifndef MSWIN
2504 if (term == NULL)
2505 term = mch_getenv((char_u *)"TERM");
2506#endif
2507 if (term == NULL || *term == NUL)
2508 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002509 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002511 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 set_string_default("term", term);
2513 set_string_default("ttytype", term);
2514
2515 /*
2516 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2517 */
2518 set_termname(T_NAME != NULL ? T_NAME : term);
2519}
2520
2521/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002522 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002524#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002525
2526// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002528
2529static int out_pos = 0; // number of chars in out_buf
2530
2531// Since the maximum number of SGR parameters shown as a normal value range is
2532// 16, the escape sequence length can be 4 * 16 + lead + tail.
2533#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
2535/*
2536 * out_flush(): flush the output buffer
2537 */
2538 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002539out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 int len;
2542
2543 if (out_pos != 0)
2544 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002545 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 len = out_pos;
2547 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002548 ui_write(out_buf, len, FALSE);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002549#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002550 if (ch_log_output != FALSE)
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002551 {
2552 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002553 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002554# ifdef FEAT_GUI
2555 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2556# endif
2557 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002558 out_buf);
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002559 if (ch_log_output == TRUE)
2560 ch_log_output = FALSE; // only log once
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002561 }
2562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 }
2564}
2565
Bram Moolenaara338adc2018-01-31 20:51:47 +01002566/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002567 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2568 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002569 */
2570 void
2571out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002572 int force UNUSED, // when TRUE, update cursor even when not moved
2573 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002574{
2575 mch_disable_flush();
2576 out_flush();
2577 mch_enable_flush();
2578#ifdef FEAT_GUI
2579 if (gui.in_use)
2580 {
2581 gui_update_cursor(force, clear_selection);
2582 gui_may_flush();
2583 }
2584#endif
2585}
2586
2587
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588/*
2589 * Sometimes a byte out of a multi-byte character is written with out_char().
2590 * To avoid flushing half of the character, call this function first.
2591 */
2592 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002593out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
2595 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2596 out_flush();
2597}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
2599#ifdef FEAT_GUI
2600/*
2601 * out_trash(): Throw away the contents of the output buffer
2602 */
2603 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002604out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
2606 out_pos = 0;
2607}
2608#endif
2609
2610/*
2611 * out_char(c): put a byte into the output buffer.
2612 * Flush it if it becomes full.
2613 * This should not be used for outputting text on the screen (use functions
2614 * like msg_puts() and screen_putchar() for that).
2615 */
2616 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002617out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
Bram Moolenaard0573012017-10-28 21:11:06 +02002619#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002620 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 out_char('\r');
2622#endif
2623
2624 out_buf[out_pos++] = c;
2625
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002626 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 if (out_pos >= OUT_SIZE || p_wd)
2628 out_flush();
2629}
2630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002632 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002634 static int
2635out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002637 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
2639 if (out_pos >= OUT_SIZE)
2640 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002641 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642}
2643
2644/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002645 * A never-padding out_str().
2646 * Use this whenever you don't want to run the string through tputs().
2647 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 * is likely to strip off leading digits, that it mistakes for padding
2649 * information, and "%i", "%d", etc.
2650 * This should only be used for writing terminal codes, not for outputting
2651 * normal text (use functions like msg_puts() and screen_putchar() for that).
2652 */
2653 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002654out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002656 // avoid terminal strings being split up
2657 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002659
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 while (*s)
2661 out_char_nf(*s++);
2662
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002663 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (p_wd)
2665 out_flush();
2666}
2667
2668/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002669 * A conditional-flushing out_str, mainly for visualbell.
2670 * Handles a delay internally, because termlib may not respect the delay or do
2671 * it at the wrong time.
2672 * Note: Only for terminal strings.
2673 */
2674 void
2675out_str_cf(char_u *s)
2676{
2677 if (s != NULL && *s)
2678 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002679#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002680 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002681#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002682
2683#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002684 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002685 if (gui.in_use)
2686 {
2687 out_str_nf(s);
2688 return;
2689 }
2690#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002691 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002692 out_flush();
2693#ifdef HAVE_TGETENT
2694 for (p = s; *s; ++s)
2695 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002696 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002697 if (*s == '$' && *(s + 1) == '<')
2698 {
2699 char_u save_c = *s;
2700 int duration = atoi((char *)s + 2);
2701
2702 *s = NUL;
2703 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2704 *s = save_c;
2705 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002706# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002707 // Only sleep here if we can limit this happening in
2708 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002709 p = vim_strchr(s, '>');
2710 if (p == NULL || duration <= 0)
2711 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002712 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002713 p = s;
2714 }
2715 else
2716 {
2717 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002718 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002719 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002720# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002721 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002722 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002723# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002724 break;
2725 }
2726 }
2727 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2728#else
2729 while (*s)
2730 out_char_nf(*s++);
2731#endif
2732
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002733 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002734 if (p_wd)
2735 out_flush();
2736 }
2737}
2738
2739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002741 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 * This should only be used for writing terminal codes, not for outputting
2743 * normal text (use functions like msg_puts() and screen_putchar() for that).
2744 */
2745 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002746out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747{
2748 if (s != NULL && *s)
2749 {
2750#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002751 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 if (gui.in_use)
2753 {
2754 out_str_nf(s);
2755 return;
2756 }
2757#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002758 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002759 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 out_flush();
2761#ifdef HAVE_TGETENT
2762 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2763#else
2764 while (*s)
2765 out_char_nf(*s++);
2766#endif
2767
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002768 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (p_wd)
2770 out_flush();
2771 }
2772}
2773
2774/*
2775 * cursor positioning using termcap parser. (jw)
2776 */
2777 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002778term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779{
2780 OUT_STR(tgoto((char *)T_CM, col, row));
2781}
2782
2783 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002784term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785{
2786 OUT_STR(tgoto((char *)T_CRI, 0, i));
2787}
2788
2789 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002790term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791{
2792 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2793}
2794
2795 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002796term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
2798 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2799}
2800
2801#if defined(HAVE_TGETENT) || defined(PROTO)
2802 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002803term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002805 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 if (x < 0)
2807 x = 0;
2808 if (y < 0)
2809 y = 0;
2810 OUT_STR(tgoto((char *)T_CWP, y, x));
2811}
2812
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002813# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2814/*
2815 * Return TRUE if we can request the terminal for a response.
2816 */
2817 static int
2818can_get_termresponse()
2819{
2820 return cur_tmode == TMODE_RAW
2821 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002822# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002823 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002824# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002825 && p_ek;
2826}
2827
Bram Moolenaarafd78262019-05-10 23:10:31 +02002828/*
2829 * Set "status" to STATUS_SENT.
2830 */
2831 static void
2832termrequest_sent(termrequest_T *status)
2833{
2834 status->tr_progress = STATUS_SENT;
2835 status->tr_start = time(NULL);
2836}
2837
2838/*
2839 * Return TRUE if any of the requests are in STATUS_SENT.
2840 */
2841 static int
2842termrequest_any_pending()
2843{
2844 int i;
2845 time_t now = time(NULL);
2846
2847 for (i = 0; all_termrequests[i] != NULL; ++i)
2848 {
2849 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2850 {
2851 if (all_termrequests[i]->tr_start > 0 && now > 0
2852 && all_termrequests[i]->tr_start + 2 < now)
2853 // Sent the request more than 2 seconds ago and didn't get a
2854 // response, assume it failed.
2855 all_termrequests[i]->tr_progress = STATUS_FAIL;
2856 else
2857 return TRUE;
2858 }
2859 }
2860 return FALSE;
2861}
2862
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002863static int winpos_x = -1;
2864static int winpos_y = -1;
2865static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002866
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002867# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002868/*
2869 * Try getting the Vim window position from the terminal.
2870 * Returns OK or FAIL.
2871 */
2872 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002873term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002874{
2875 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002876 int prev_winpos_x = winpos_x;
2877 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002878
2879 if (*T_CGP == NUL || !can_get_termresponse())
2880 return FAIL;
2881 winpos_x = -1;
2882 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002883 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002884 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002885 OUT_STR(T_CGP);
2886 out_flush();
2887
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002888 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002889 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002890 {
2891 (void)vpeekc_nomap();
2892 if (winpos_x >= 0 && winpos_y >= 0)
2893 {
2894 *x = winpos_x;
2895 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002896 return OK;
2897 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002898 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002899 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002900 // Do not reset "did_request_winpos", if we timed out the response might
2901 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002902
2903 winpos_x = prev_winpos_x;
2904 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002905 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002906 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002907 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002908 *x = winpos_x;
2909 *y = winpos_y;
2910 return OK;
2911 }
2912
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002913 return FALSE;
2914}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002915# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002916# endif
2917
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002919term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002921 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922}
2923#endif
2924
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002926term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
2928 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002929 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002930 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002932 // Special handling of 16 colors, because termcap can't handle it
2933 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
2934 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002936 && ((s[0] == ESC && s[1] == '[')
2937#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2938 || (s[0] == ESC && s[1] == '|')
2939#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02002940 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 && s[i] != NUL
2942 && (STRCMP(s + i + 1, "%p1%dm") == 0
2943 || STRCMP(s + i + 1, "%dm") == 0)
2944 && (s[i] == '3' || s[i] == '4'))
2945 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002947 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002949 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002951 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002952#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002953 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002954#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002955 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02002956 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2957 : (n >= 16 ? "48;5;" : "10");
2958
2959 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2961 }
2962 else
2963 OUT_STR(tgoto((char *)s, 0, n));
2964}
2965
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002966 void
2967term_fg_color(int n)
2968{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002969 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002970 if (*T_CAF)
2971 term_color(T_CAF, n);
2972 else if (*T_CSF)
2973 term_color(T_CSF, n);
2974}
2975
2976 void
2977term_bg_color(int n)
2978{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002979 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002980 if (*T_CAB)
2981 term_color(T_CAB, n);
2982 else if (*T_CSB)
2983 term_color(T_CSB, n);
2984}
2985
Bram Moolenaare023e882020-05-31 16:42:30 +02002986 void
2987term_ul_color(int n)
2988{
2989 if (*T_CAU)
2990 term_color(T_CAU, n);
2991}
2992
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002993/*
2994 * Return "dark" or "light" depending on the kind of terminal.
2995 * This is just guessing! Recognized are:
2996 * "linux" Linux console
2997 * "screen.linux" Linux console with screen
2998 * "cygwin.*" Cygwin shell
2999 * "putty.*" Putty program
3000 * We also check the COLORFGBG environment variable, which is set by
3001 * rxvt and derivatives. This variable contains either two or three
3002 * values separated by semicolons; we want the last value in either
3003 * case. If this value is 0-6 or 8, our background is dark.
3004 */
3005 char_u *
3006term_bg_default(void)
3007{
3008#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003009 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003010 return (char_u *)"dark";
3011#else
3012 char_u *p;
3013
3014 if (STRCMP(T_NAME, "linux") == 0
3015 || STRCMP(T_NAME, "screen.linux") == 0
3016 || STRNCMP(T_NAME, "cygwin", 6) == 0
3017 || STRNCMP(T_NAME, "putty", 5) == 0
3018 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3019 && (p = vim_strrchr(p, ';')) != NULL
3020 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3021 && p[2] == NUL))
3022 return (char_u *)"dark";
3023 return (char_u *)"light";
3024#endif
3025}
3026
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003027#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003028
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003029#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3030#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3031#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003032
3033 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003034term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003035{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003036#define MAX_COLOR_STR_LEN 100
3037 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003038
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003039 if (*s == NUL)
3040 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003041 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003042 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003043#ifdef FEAT_VTP
Christopher Plewright38804d62022-11-09 23:55:52 +00003044 if (has_vtp_working())
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003045 {
3046 out_flush();
3047 buf[1] = '[';
3048 vtp_printf(buf);
3049 }
3050 else
3051#endif
3052 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003053}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003054
3055 void
3056term_fg_rgb_color(guicolor_T rgb)
3057{
Christopher Plewright38804d62022-11-09 23:55:52 +00003058 if (rgb != INVALCOLOR)
3059 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003060}
3061
3062 void
3063term_bg_rgb_color(guicolor_T rgb)
3064{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003065 if (rgb != INVALCOLOR)
3066 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003067}
Bram Moolenaare023e882020-05-31 16:42:30 +02003068
3069 void
3070term_ul_rgb_color(guicolor_T rgb)
3071{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003072# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003073 // If the user explicitly sets t_8u then use it. Otherwise wait for
3074 // termresponse to be received, which is when t_8u would be set and a
3075 // redraw is needed if it was used.
3076 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003077 write_t_8u_state = MAYBE;
3078 else
3079# endif
3080 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003081}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003082#endif
3083
Bram Moolenaar651fca82021-11-29 20:39:38 +00003084#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085/*
3086 * Generic function to set window title, using t_ts and t_fs.
3087 */
3088 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003089term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003091 MAY_WANT_TO_LOG_THIS;
3092
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003093 // t_ts takes one argument: column in status line
3094 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003096 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 out_flush();
3098}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003099
3100/*
3101 * Tell the terminal to push (save) the title and/or icon, so that it can be
3102 * popped (restored) later.
3103 */
3104 void
3105term_push_title(int which)
3106{
Bram Moolenaar27821262019-05-08 16:41:09 +02003107 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003108 {
3109 OUT_STR(T_CST);
3110 out_flush();
3111 }
3112
Bram Moolenaar27821262019-05-08 16:41:09 +02003113 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003114 {
3115 OUT_STR(T_SSI);
3116 out_flush();
3117 }
3118}
3119
3120/*
3121 * Tell the terminal to pop the title and/or icon.
3122 */
3123 void
3124term_pop_title(int which)
3125{
Bram Moolenaar27821262019-05-08 16:41:09 +02003126 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003127 {
3128 OUT_STR(T_CRT);
3129 out_flush();
3130 }
3131
Bram Moolenaar27821262019-05-08 16:41:09 +02003132 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003133 {
3134 OUT_STR(T_SRI);
3135 out_flush();
3136 }
3137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#endif
3139
3140/*
3141 * Make sure we have a valid set or terminal options.
3142 * Replace all entries that are NULL by empty_option
3143 */
3144 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003145ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003147 char_u *env_colors;
3148
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003149 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151 /*
3152 * MUST have "cm": cursor motion.
3153 */
3154 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003155 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
3157 /*
3158 * if "cs" defined, use a scroll region, it's faster.
3159 */
3160 if (*T_CS != NUL)
3161 scroll_region = TRUE;
3162 else
3163 scroll_region = FALSE;
3164
3165 if (pairs)
3166 {
3167 /*
3168 * optional pairs
3169 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003170 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (*T_ME == NUL)
3172 T_ME = T_MR = T_MD = T_MB = empty_option;
3173 if (*T_SO == NUL || *T_SE == NUL)
3174 T_SO = T_SE = empty_option;
3175 if (*T_US == NUL || *T_UE == NUL)
3176 T_US = T_UE = empty_option;
3177 if (*T_CZH == NUL || *T_CZR == NUL)
3178 T_CZH = T_CZR = empty_option;
3179
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003180 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (*T_VE == NUL)
3182 T_VI = empty_option;
3183
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003184 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 if (*T_ME == NUL)
3186 {
3187 T_ME = T_SE;
3188 T_MR = T_SO;
3189 T_MD = T_SO;
3190 }
3191
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003192 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 if (*T_SO == NUL)
3194 {
3195 T_SE = T_ME;
3196 if (*T_MR == NUL)
3197 T_SO = T_MD;
3198 else
3199 T_SO = T_MR;
3200 }
3201
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003202 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 if (*T_CZH == NUL)
3204 {
3205 T_CZR = T_ME;
3206 if (*T_MR == NUL)
3207 T_CZH = T_MD;
3208 else
3209 T_CZH = T_MR;
3210 }
3211
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003212 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 if (*T_CSB == NUL || *T_CSF == NUL)
3214 {
3215 T_CSB = empty_option;
3216 T_CSF = empty_option;
3217 }
3218
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003219 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 if (*T_CAB == NUL || *T_CAF == NUL)
3221 {
3222 T_CAB = empty_option;
3223 T_CAF = empty_option;
3224 }
3225
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003226 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003228 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003230 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 p_wiv = (*T_XS != NUL);
3232 }
3233 need_gather = TRUE;
3234
Bram Moolenaar759d8152020-04-26 16:52:49 +02003235 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3236 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003238#ifdef FEAT_GUI
3239 if (!gui.in_use)
3240#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003241 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003242 env_colors = mch_getenv((char_u *)"COLORS");
3243 if (env_colors != NULL && isdigit(*env_colors))
3244 {
3245 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003246
Bram Moolenaar759d8152020-04-26 16:52:49 +02003247 if (colors != t_colors)
3248 set_color_count(colors);
3249 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251}
3252
3253#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3254 || defined(PROTO)
3255/*
3256 * Represent the given long_u as individual bytes, with the most significant
3257 * byte first, and store them in dst.
3258 */
3259 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003260add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261{
3262 int i;
3263 int shift;
3264
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003265 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
3267 shift = 8 * (sizeof(long_u) - i);
3268 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3269 }
3270}
3271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272/*
3273 * Interpret the next string of bytes in buf as a long integer, with the most
3274 * significant byte first. Note that it is assumed that buf has been through
3275 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3276 * Puts result in val, and returns the number of bytes read from buf
3277 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3278 * were present.
3279 */
3280 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003281get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283 int len;
3284 char_u bytes[sizeof(long_u)];
3285 int i;
3286 int shift;
3287
3288 *val = 0;
3289 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3290 if (len != -1)
3291 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003292 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
3294 shift = 8 * (sizeof(long_u) - 1 - i);
3295 *val += (long_u)bytes[i] << shift;
3296 }
3297 }
3298 return len;
3299}
3300#endif
3301
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302/*
3303 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3304 * that buf has been through inchar(). Returns the actual number of bytes used
3305 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3306 * available.
3307 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003308 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003309get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
3311 int len = 0;
3312 int i;
3313 char_u c;
3314
3315 for (i = 0; i < num_bytes; i++)
3316 {
3317 if ((c = buf[len++]) == NUL)
3318 return -1;
3319 if (c == K_SPECIAL)
3320 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003321 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 return -1;
3323 if (buf[len++] == (int)KS_ZERO)
3324 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003325 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3326 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003327 if (buf[len++] == (int)KE_CSI)
3328 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003330 else if (c == CSI && buf[len] == KS_EXTRA
3331 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003332 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3333 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003334 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 bytes[i] = c;
3336 }
3337 return len;
3338}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339
3340/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003341 * Check if the new shell size is valid, correct it if it's too small or way
3342 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 */
3344 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003345check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003347 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003349 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003350
3351 // make sure these values are not invalid
3352 if (cmdline_row >= Rows)
3353 cmdline_row = Rows - 1;
3354 if (msg_row >= Rows)
3355 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003356}
3357
3358/*
3359 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3360 */
3361 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003362limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003363{
3364 if (Columns < MIN_COLUMNS)
3365 Columns = MIN_COLUMNS;
3366 else if (Columns > 10000)
3367 Columns = 10000;
3368 if (Rows > 1000)
3369 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370}
3371
Bram Moolenaar86b68352004-12-27 21:59:20 +00003372/*
3373 * Invoked just before the screen structures are going to be (re)allocated.
3374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003376win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
3378 static int old_Rows = 0;
3379 static int old_Columns = 0;
3380
3381 if (old_Rows != Rows || old_Columns != Columns)
3382 ui_new_shellsize();
3383 if (old_Rows != Rows)
3384 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003385 // If 'window' uses the whole screen, keep it using that.
3386 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003387 if (p_window == old_Rows - 1
3388 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003389 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003391 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 }
3393 if (old_Columns != Columns)
3394 {
3395 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003396 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398}
3399
3400/*
3401 * Call this function when the Vim shell has been resized in any way.
3402 * Will obtain the current size and redraw (also when size didn't change).
3403 */
3404 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003405shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
3407 set_shellsize(0, 0, FALSE);
3408}
3409
3410/*
3411 * Check if the shell size changed. Handle a resize.
3412 * When the size didn't change, nothing happens.
3413 */
3414 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003415shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416{
3417 int old_Rows = Rows;
3418 int old_Columns = Columns;
3419
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003420 if (!exiting
3421#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003422 // Do not get the size when executing a shell command during
3423 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003424 && !gui.starting
3425#endif
3426 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003427 {
3428 (void)ui_get_shellsize();
3429 check_shellsize();
3430 if (old_Rows != Rows || old_Columns != Columns)
3431 shell_resized();
3432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433}
3434
3435/*
3436 * Set size of the Vim shell.
3437 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3438 * window size (this is used for the :win command).
3439 * If 'mustset' is FALSE, we may try to get the real window size and if
3440 * it fails use 'width' and 'height'.
3441 */
3442 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003443set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
3445 static int busy = FALSE;
3446
3447 /*
3448 * Avoid recursiveness, can happen when setting the window size causes
3449 * another window-changed signal.
3450 */
3451 if (busy)
3452 return;
3453
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003454 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 return;
3456
Bram Moolenaar24959102022-05-07 20:01:16 +01003457 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003459 // postpone the resizing
Bram Moolenaar24959102022-05-07 20:01:16 +01003460 State = MODE_SETWSIZE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 return;
3462 }
3463
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003464 if (updating_screen)
3465 // resizing while in update_screen() may cause a crash
3466 return;
3467
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003468 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003469 // buffer (or window) has already been closed and removing a scrollbar
3470 // causes a resize event. Don't resize then, it will happen after entering
3471 // another buffer.
3472 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003473 return;
3474
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 ++busy;
3476
3477#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003478 out_flush(); // must do this before mch_get_shellsize() for
3479 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480#endif
3481
3482 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3483 {
3484 Rows = height;
3485 Columns = width;
3486 check_shellsize();
3487 ui_set_shellsize(mustset);
3488 }
3489 else
3490 check_shellsize();
3491
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003492 // The window layout used to be adjusted here, but it now happens in
3493 // screenalloc() (also invoked from screenclear()). That is because the
3494 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
Bram Moolenaar24959102022-05-07 20:01:16 +01003496 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3497 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 screenclear();
3499 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003500 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501
3502 if (starting != NO_SCREEN)
3503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 changed_line_abv_curs();
3507 invalidate_botline();
3508
3509 /*
3510 * We only redraw when it's needed:
3511 * - While at the more prompt or executing an external command, don't
3512 * redraw, but position the cursor.
3513 * - While editing the command line, only redraw that.
3514 * - in Ex mode, don't redraw anything.
3515 * - Otherwise, redraw right now, and position the cursor.
3516 * Always need to call update_screen() or screenalloc(), to make
3517 * sure Rows/Columns and the size of ScreenLines[] is correct!
3518 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003519 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3520 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
3522 screenalloc(FALSE);
3523 repeat_message();
3524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 else
3526 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003527 if (curwin->w_p_scb)
3528 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003529 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003530 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003531 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003532 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003533 }
3534 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003535 {
3536 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003537 if (pum_visible())
3538 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003539 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003540 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003541 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003542 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003543 if (redrawing())
3544 setcursor();
3545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003547 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
3549 out_flush();
3550 --busy;
3551}
3552
3553/*
3554 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3555 * commands and Ex mode).
3556 */
3557 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003558settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559{
3560#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003561 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 if (gui.in_use)
3563 return;
3564#endif
3565
3566 if (full_screen)
3567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003569 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3570 * set the terminal to raw mode, even though we think it already is,
3571 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 * When we think the terminal is normal, don't try to set it to
3573 * normal again, because that causes problems (logout!) on some
3574 * machines.
3575 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003576 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 {
3578#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003579# ifdef FEAT_GUI
3580 if (!gui.in_use && !gui.starting)
3581# endif
3582 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003583 // May need to check for T_CRV response and termcodes, it
3584 // doesn't work in Cooked mode, an external program may get
3585 // them.
3586 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003587 (void)vpeekc_nomap();
3588 check_for_codes_from_term();
3589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003592 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003593
3594 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3595 // Avoid doing this too often, on some terminals the codes are not
3596 // handled properly.
3597 if (termcap_active && tmode != TMODE_SLEEP
3598 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003599 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003600 MAY_WANT_TO_LOG_THIS;
3601
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003602 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003603 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003604 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003605 out_str(T_CTE); // possibly disables modifyOtherKeys
3606 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003607 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003608 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003609 out_str(T_BE); // enable bracketed paste mode (should
3610 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003611 out_str(T_CTI); // possibly enables modifyOtherKeys
3612 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003615 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003618 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 out_flush();
3620 }
3621#ifdef FEAT_TERMRESPONSE
3622 may_req_termresponse();
3623#endif
3624 }
3625}
3626
3627 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003628starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629{
3630 if (full_screen && !termcap_active)
3631 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003632 MAY_WANT_TO_LOG_THIS;
3633
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003634 out_str(T_TI); // start termcap mode
3635 out_str(T_CTI); // start "raw" mode
3636 out_str(T_KS); // start "keypad transmit" mode
3637 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003638
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003639#if defined(UNIX) || defined(VMS)
3640 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003641 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003642 out_str(T_FE);
3643#endif
3644
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 out_flush();
3646 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003647 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003649# ifdef FEAT_GUI
3650 if (!gui.in_use && !gui.starting)
3651# endif
3652 {
3653 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003654 // Immediately check for a response. If t_Co changes, we don't
3655 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003656 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003657 check_for_codes_from_term();
3658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659#endif
3660 }
3661}
3662
3663 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003664stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665{
3666 screen_stop_highlight();
3667 reset_cterm_colors();
3668 if (termcap_active)
3669 {
3670#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003671# ifdef FEAT_GUI
3672 if (!gui.in_use && !gui.starting)
3673# endif
3674 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003675 // May need to discard T_CRV, T_U7 or T_RBG response.
3676 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003677 {
3678# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003679 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003680 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003681# endif
3682# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003683 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003684 if (exiting)
3685 tcflush(fileno(stdin), TCIFLUSH);
3686# endif
3687 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003688 // Check for termcodes first, otherwise an external program may
3689 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003690 check_for_codes_from_term();
3691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003693 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003694
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003695#if defined(UNIX) || defined(VMS)
3696 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003697 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003698 out_str(T_FD);
3699#endif
3700
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003701 out_str(T_BD); // disable bracketed paste mode
3702 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 out_flush();
3704 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003705 cursor_on(); // just in case it is still off
3706 out_str(T_CTE); // stop "raw" mode
3707 out_str(T_TE); // stop termcap mode
3708 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 out_flush();
3710 }
3711}
3712
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003713#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714/*
3715 * Request version string (for xterm) when needed.
3716 * Only do this after switching to raw mode, otherwise the result will be
3717 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003718 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003719 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 * Only do this after termcap mode has been started, otherwise the codes for
3721 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003722 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3723 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003724 * On Unix only do it when both output and input are a tty (avoid writing
3725 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 * The result is caught in check_termcode().
3727 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003728 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003729may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003731 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003732 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003733 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 && *T_CRV != NUL)
3735 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003736 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003737 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003739 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003740 // check for the characters now, otherwise they might be eaten by
3741 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 out_flush();
3743 (void)vpeekc_nomap();
3744 }
3745}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003746
Bram Moolenaar9584b312013-03-13 19:29:28 +01003747/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003748 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3749 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003750 * Note that this goes out before T_CRV, so that the result can be used when
3751 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003752 */
3753 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003754check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003755{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003756 int did_send = FALSE;
3757
3758 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3759 return;
3760
Bram Moolenaarafd78262019-05-10 23:10:31 +02003761 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003762 && !option_was_set((char_u *)"ambiwidth"))
3763 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003764 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003765
Bram Moolenaara45551a2020-06-09 15:57:37 +02003766 // Ambiguous width check.
3767 // Check how the terminal treats ambiguous character width (UAX #11).
3768 // First, we move the cursor to (1, 0) and print a test ambiguous
3769 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3770 // the current cursor position. If the terminal treats \u25bd as
3771 // single width, the position is (1, 1), or if it is treated as double
3772 // width, that will be (1, 2). This function has the side effect that
3773 // changes cursor position, so it must be called immediately after
3774 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003775 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003776 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003777 // Do this in the second row. In the first row the returned sequence
3778 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003779 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003780 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003781 out_str(buf);
3782 out_str(T_U7);
3783 termrequest_sent(&u7_status);
3784 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003785 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003786
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003787 // This overwrites a few characters on the screen, a redraw is needed
3788 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003789 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003790 term_windgoto(1, 0);
3791 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003792 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003793 }
3794
Bram Moolenaard1f34e62022-01-07 19:24:20 +00003795 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02003796 {
3797 // 2. Check compatibility with xterm.
3798 // We move the cursor to (2, 0), print a test sequence and then query
3799 // the current cursor position. If the terminal properly handles
3800 // unknown DCS string and CSI sequence with intermediate byte, the test
3801 // sequence is ignored and the cursor does not move. If the terminal
3802 // handles test sequence incorrectly, a garbage string is displayed and
3803 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003804 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003805 LOG_TR(("Sending xterm compatibility test sequence."));
3806 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003807 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003808 term_windgoto(2, 0);
3809 // send the test DCS string.
3810 out_str((char_u *)"\033Pzz\033\\");
3811 // send the test CSI sequence with intermediate byte.
3812 out_str((char_u *)"\033[0%m");
3813 out_str(T_U7);
3814 termrequest_sent(&xcc_status);
3815 out_flush();
3816 did_send = TRUE;
3817
3818 // If the terminal handles test sequence incorrectly, garbage text is
3819 // displayed. Clear them out for now.
3820 screen_stop_highlight();
3821 term_windgoto(2, 0);
3822 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003823 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003824 }
3825
3826 if (did_send)
3827 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003828 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003830 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003831 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003832
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003833 // check for the characters now, otherwise they might be eaten by
3834 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003835 out_flush();
3836 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003837 }
3838}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003839
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003840/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003841 * Similar to requesting the version string: Request the terminal background
3842 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003843 */
3844 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003845may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003846{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003847 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003848 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003849 int didit = FALSE;
3850
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003851# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003852 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003853 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003854 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003855 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003856 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003857 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003858 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003859 didit = TRUE;
3860 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003861# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003862
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003863 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003864 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003865 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003866 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003867 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003868 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003869 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003870 didit = TRUE;
3871 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003872
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003873 if (didit)
3874 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003875 // check for the characters now, otherwise they might be eaten by
3876 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003877 out_flush();
3878 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003879 }
3880 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003881}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003882
Bram Moolenaar2951b772013-07-03 12:45:31 +02003883# ifdef DEBUG_TERMRESPONSE
3884 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003885log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003886{
3887 static FILE *fd_tr = NULL;
3888 static proftime_T start;
3889 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003890 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003891
3892 if (fd_tr == NULL)
3893 {
3894 fd_tr = fopen("termresponse.log", "w");
3895 profile_start(&start);
3896 }
3897 now = start;
3898 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003899 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003900 must_redraw == UPD_NOT_VALID ? "NV"
3901 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02003902 va_start(ap, fmt);
3903 vfprintf(fd_tr, fmt, ap);
3904 va_end(ap);
3905 fputc('\n', fd_tr);
3906 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003907}
3908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909#endif
3910
3911/*
3912 * Return TRUE when saving and restoring the screen.
3913 */
3914 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003915swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916{
3917 return (full_screen && *T_TI != NUL);
3918}
3919
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920/*
3921 * By outputting the 'cursor very visible' termcap code, for some windowed
3922 * terminals this makes the screen scrolled to the correct position.
3923 * Used when starting Vim or returning from a shell.
3924 */
3925 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003926scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003928 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003930 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003932 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003933 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
3935}
3936
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003937// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938static int cursor_is_off = FALSE;
3939
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003940// True if cursor is not visible due to an ongoing cursor-less sleep
3941static int cursor_is_asleep = FALSE;
3942
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003944 * Enable the cursor without checking if it's already enabled.
3945 */
3946 void
3947cursor_on_force(void)
3948{
3949 out_str(T_VE);
3950 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003951 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02003952}
3953
3954/*
3955 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 */
3957 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003958cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003960 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003961 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962}
3963
3964/*
3965 * Disable the cursor.
3966 */
3967 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003968cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003970 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003972 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 cursor_is_off = TRUE;
3974 }
3975}
3976
Dominique Pelle748b3082022-01-08 12:41:16 +00003977#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003978/*
3979 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
3980 */
3981 int
3982cursor_is_sleeping(void)
3983{
3984 return cursor_is_asleep;
3985}
Dominique Pelle748b3082022-01-08 12:41:16 +00003986#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003987
3988/*
3989 * Disable the cursor and mark it disabled by cursor-less sleep
3990 */
3991 void
3992cursor_sleep(void)
3993{
3994 cursor_is_asleep = TRUE;
3995 cursor_off();
3996}
3997
3998/*
3999 * Enable the cursor and mark it not disabled by cursor-less sleep
4000 */
4001 void
4002cursor_unsleep(void)
4003{
4004 cursor_is_asleep = FALSE;
4005 cursor_on();
4006}
4007
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004008#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004010 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004011 */
4012 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004013term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004014{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004015 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004016 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004017
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004018 // Only do something when redrawing the screen and we can restore the
4019 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004020 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004021 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004022# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004023 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004024 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004025 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004026# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004027 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004028 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004029
Bram Moolenaar24959102022-05-07 20:01:16 +01004030 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004031 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004032 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004033 {
4034 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004035 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004036 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004037 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004038 if (*p != NUL)
4039 {
4040 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004041 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004042 }
4043 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004044 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004045 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004046 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004047 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004048 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004049 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004050 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004051 }
4052 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004053 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004054 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004055 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004056 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004057 }
4058}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004059
4060# if defined(FEAT_TERMINAL) || defined(PROTO)
4061 void
4062term_cursor_color(char_u *color)
4063{
4064 if (*T_CSC != NUL)
4065 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004066 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004067 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004068 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004069 out_flush();
4070 }
4071}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004072# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004073
Bram Moolenaar4db25542017-08-28 22:43:05 +02004074 int
4075blink_state_is_inverted()
4076{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004077#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004078 return rbm_status.tr_progress == STATUS_GOT
4079 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004080 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004081#else
4082 return FALSE;
4083#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004084}
4085
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004086/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004087 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004088 */
4089 void
4090term_cursor_shape(int shape, int blink)
4091{
4092 if (*T_CSH != NUL)
4093 {
4094 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4095 out_flush();
4096 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004097 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004098 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004099 int do_blink = blink;
4100
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004101 // t_SH is empty: try setting just the blink state.
4102 // The blink flags are XORed together, if the initial blinking from
4103 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004104 if (blink_state_is_inverted())
4105 do_blink = !blink;
4106
4107 if (do_blink && *T_VS != NUL)
4108 {
4109 out_str(T_VS);
4110 out_flush();
4111 }
4112 else if (!do_blink && *T_CVS != NUL)
4113 {
4114 out_str(T_CVS);
4115 out_flush();
4116 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004117 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004118}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004119#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004120
4121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 * Set scrolling region for window 'wp'.
4123 * The region starts 'off' lines from the start of the window.
4124 * Also set the vertical scroll region for a vertically split window. Always
4125 * the full width of the window, excluding the vertical separator.
4126 */
4127 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004128scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
4130 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4131 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004133 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4134 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004135 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136}
4137
4138/*
4139 * Reset scrolling region to the whole screen.
4140 */
4141 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004142scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
4144 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 if (*T_CSV != NUL)
4146 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004147 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148}
4149
4150
4151/*
4152 * List of terminal codes that are currently recognized.
4153 */
4154
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004155static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004157 char_u name[2]; // termcap name of entry
4158 char_u *code; // terminal code (in allocated memory)
4159 int len; // STRLEN(code)
4160 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161} *termcodes = NULL;
4162
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004163static int tc_max_len = 0; // number of entries that termcodes[] can hold
4164static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004166static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004167
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004169clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
4171 while (tc_len > 0)
4172 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004173 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 tc_max_len = 0;
4175
4176#ifdef HAVE_TGETENT
4177 BC = (char *)empty_option;
4178 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004179 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 ospeed = 0;
4181#endif
4182
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004183 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184}
4185
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004186#define ATC_FROM_TERM 55
4187
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188/*
4189 * Add a new entry to the list of terminal codes.
4190 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004191 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4192 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 */
4194 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004195add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196{
4197 struct termcode *new_tc;
4198 int i, j;
4199 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004200 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
4202 if (string == NULL || *string == NUL)
4203 {
4204 del_termcode(name);
4205 return;
4206 }
4207
Bram Moolenaar4f974752019-02-17 17:44:42 +01004208#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004209 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004210#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004211# ifdef VIMDLL
4212 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004213 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004214 else
4215# endif
4216 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 if (s == NULL)
4219 return;
4220
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004221 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004222 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004224 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 s[0] = term_7to8bit(string);
4226 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004227
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004228#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4229# ifdef VIMDLL
4230 if (!gui.in_use)
4231# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004232 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004233 if (s[0] == K_NUL)
4234 {
4235 STRMOVE(s + 1, s);
4236 s[1] = 3;
4237 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004238 }
4239#endif
4240
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004241 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004243 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245 /*
4246 * need to make space for more entries
4247 */
4248 if (tc_len == tc_max_len)
4249 {
4250 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004251 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 if (new_tc == NULL)
4253 {
4254 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004255 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 return;
4257 }
4258 for (i = 0; i < tc_len; ++i)
4259 new_tc[i] = termcodes[i];
4260 vim_free(termcodes);
4261 termcodes = new_tc;
4262 }
4263
4264 /*
4265 * Look for existing entry with the same name, it is replaced.
4266 * Look for an existing entry that is alphabetical higher, the new entry
4267 * is inserted in front of it.
4268 */
4269 for (i = 0; i < tc_len; ++i)
4270 {
4271 if (termcodes[i].name[0] < name[0])
4272 continue;
4273 if (termcodes[i].name[0] == name[0])
4274 {
4275 if (termcodes[i].name[1] < name[1])
4276 continue;
4277 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004278 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 */
4280 if (termcodes[i].name[1] == name[1])
4281 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004282 if (flags == ATC_FROM_TERM && (j = termcode_star(
4283 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004284 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004285 // Don't replace ESC[123;*X or ESC O*X with another when
4286 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004287 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004288 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004289 && s[len - 1]
4290 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004291 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004292 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004293 vim_free(s);
4294 return;
4295 }
4296 }
4297 else
4298 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004299 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004300 vim_free(termcodes[i].code);
4301 --tc_len;
4302 break;
4303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305 }
4306 /*
4307 * Found alphabetical larger entry, move rest to insert new entry
4308 */
4309 for (j = tc_len; j > i; --j)
4310 termcodes[j] = termcodes[j - 1];
4311 break;
4312 }
4313
4314 termcodes[i].name[0] = name[0];
4315 termcodes[i].name[1] = name[1];
4316 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004317 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004318
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004319 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4320 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004321 termcodes[i].modlen = 0;
4322 j = termcode_star(s, len);
4323 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004324 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004325 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004326 // For "CSI[@;X" the "@" is not included in "modlen".
4327 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4328 --termcodes[i].modlen;
4329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 ++tc_len;
4331}
4332
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004333/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004334 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004335 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004336 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004337 */
4338 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004339termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004340{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004341 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004342 if (len >= 3 && code[len - 2] == '*')
4343 {
4344 if (len >= 5 && code[len - 3] == ';')
4345 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004346 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004347 return 1;
4348 }
4349 return 0;
4350}
4351
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004353find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354{
4355 int i;
4356
4357 for (i = 0; i < tc_len; ++i)
4358 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4359 return termcodes[i].code;
4360 return NULL;
4361}
4362
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004364get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365{
4366 if (i >= tc_len)
4367 return NULL;
4368 return &termcodes[i].name[0];
4369}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004371/*
4372 * Returns the length of the terminal code at index 'idx'.
4373 */
4374 int
4375get_termcode_len(int idx)
4376{
4377 return termcodes[idx].len;
4378}
4379
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004380 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004381del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
4383 int i;
4384
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004385 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 return;
4387
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004388 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
4390 for (i = 0; i < tc_len; ++i)
4391 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4392 {
4393 del_termcode_idx(i);
4394 return;
4395 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004396 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397}
4398
4399 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004400del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401{
4402 int i;
4403
4404 vim_free(termcodes[idx].code);
4405 --tc_len;
4406 for (i = idx; i < tc_len; ++i)
4407 termcodes[i] = termcodes[i + 1];
4408}
4409
4410#ifdef FEAT_TERMRESPONSE
4411/*
4412 * Called when detected that the terminal sends 8-bit codes.
4413 * Convert all 7-bit codes to their 8-bit equivalent.
4414 */
4415 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004416switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417{
4418 int i;
4419 int c;
4420
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004421 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 if (!term_is_8bit(T_NAME))
4423 {
4424 for (i = 0; i < tc_len; ++i)
4425 {
4426 c = term_7to8bit(termcodes[i].code);
4427 if (c != 0)
4428 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004429 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 termcodes[i].code[0] = c;
4431 }
4432 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004433 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
4435 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004436 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437}
4438#endif
4439
4440#ifdef CHECK_DOUBLE_CLICK
4441static linenr_T orig_topline = 0;
4442# ifdef FEAT_DIFF
4443static int orig_topfill = 0;
4444# endif
4445#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004446#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004448 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 * "orig_topline" is used to avoid detecting a double-click when the window
4450 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4451 */
4452/*
4453 * Set orig_topline. Used when jumping to another window, so that a double
4454 * click still works.
4455 */
4456 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004457set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458{
4459 orig_topline = wp->w_topline;
4460# ifdef FEAT_DIFF
4461 orig_topfill = wp->w_topfill;
4462# endif
4463}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004464
4465/*
4466 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4467 * topline and topfill.
4468 */
4469 int
4470is_mouse_topline(win_T *wp)
4471{
4472 return orig_topline == wp->w_topline
4473#ifdef FEAT_DIFF
4474 && orig_topfill == wp->w_topfill
4475#endif
4476 ;
4477}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478#endif
4479
4480/*
zeertzjqfc78a032022-04-26 22:11:38 +01004481 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4482 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4483 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004484 * Remove "slen" bytes.
4485 * Returns FAIL for error.
4486 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004487 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004488put_string_in_typebuf(
4489 int offset,
4490 int slen,
4491 char_u *string,
4492 int new_slen,
4493 char_u *buf,
4494 int bufsize,
4495 int *buflen)
4496{
4497 int extra = new_slen - slen;
4498
4499 string[new_slen] = NUL;
4500 if (buf == NULL)
4501 {
4502 if (extra < 0)
4503 // remove matched chars, taking care of noremap
4504 del_typebuf(-extra, offset);
4505 else if (extra > 0)
4506 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004507 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4508 == FAIL)
4509 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004510
4511 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4512 // typebuf.tb_buf[]!
4513 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4514 (size_t)new_slen);
4515 }
4516 else
4517 {
4518 if (extra < 0)
4519 // remove matched characters
4520 mch_memmove(buf + offset, buf + offset - extra,
4521 (size_t)(*buflen + offset + extra));
4522 else if (extra > 0)
4523 {
4524 // Insert the extra space we need. If there is insufficient
4525 // space return -1.
4526 if (*buflen + extra + new_slen >= bufsize)
4527 return FAIL;
4528 mch_memmove(buf + offset + extra, buf + offset,
4529 (size_t)(*buflen - offset));
4530 }
4531 mch_memmove(buf + offset, string, (size_t)new_slen);
4532 *buflen = *buflen + extra + new_slen;
4533 }
4534 return OK;
4535}
4536
4537/*
4538 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4539 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004540 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004541decode_modifiers(int n)
4542{
4543 int code = n - 1;
4544 int modifiers = 0;
4545
4546 if (code & 1)
4547 modifiers |= MOD_MASK_SHIFT;
4548 if (code & 2)
4549 modifiers |= MOD_MASK_ALT;
4550 if (code & 4)
4551 modifiers |= MOD_MASK_CTRL;
4552 if (code & 8)
4553 modifiers |= MOD_MASK_META;
4554 return modifiers;
4555}
4556
4557 static int
4558modifiers2keycode(int modifiers, int *key, char_u *string)
4559{
4560 int new_slen = 0;
4561
4562 if (modifiers != 0)
4563 {
4564 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004565 // make mappings work. This may result in a special key, such as
4566 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004567 *key = simplify_key(*key, &modifiers);
4568 if (modifiers != 0)
4569 {
4570 string[new_slen++] = K_SPECIAL;
4571 string[new_slen++] = (int)KS_MODIFIER;
4572 string[new_slen++] = modifiers;
4573 }
4574 }
4575 return new_slen;
4576}
4577
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004578#ifdef FEAT_TERMRESPONSE
4579/*
4580 * Handle a cursor position report.
4581 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004582 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004583handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004584{
4585 if (arg[0] == 2 && arg[1] >= 2)
4586 {
4587 char *aw = NULL;
4588
4589 LOG_TR(("Received U7 status: %s", tp));
4590 u7_status.tr_progress = STATUS_GOT;
4591 did_cursorhold = TRUE;
4592 if (arg[1] == 2)
4593 aw = "single";
4594 else if (arg[1] == 3)
4595 aw = "double";
4596 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4597 {
4598 // Setting the option causes a screen redraw. Do
4599 // that right away if possible, keeping any
4600 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004601 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004602# ifdef DEBUG_TERMRESPONSE
4603 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004604 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004605
4606 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4607 }
4608# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004609 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004610# endif
4611# ifdef FEAT_EVAL
4612 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4613# endif
4614 }
4615 }
4616 else if (arg[0] == 3)
4617 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004618 int value;
4619
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004620 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004621 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004622
4623 // Third row: xterm compatibility test.
4624 // If the cursor is on the first column then the terminal can handle
4625 // the request for cursor style and blinking.
4626 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4627 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4628 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004629 }
4630}
4631
4632/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004633 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004634 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004635 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004636 */
4637 static void
4638handle_version_response(int first, int *arg, int argc, char_u *tp)
4639{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004640 // The xterm version. It is set to zero when it can't be an actual xterm
4641 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004642 int version = arg[1];
4643
4644 LOG_TR(("Received CRV response: %s", tp));
4645 crv_status.tr_progress = STATUS_GOT;
4646 did_cursorhold = TRUE;
4647
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004648 // Reset terminal properties that are set based on the termresponse.
4649 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004650 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004651 init_term_props(
4652#ifdef FEAT_EVAL
4653 reset_term_props_on_termresponse
4654#else
4655 FALSE
4656#endif
4657 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004658
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004659 // If this code starts with CSI, you can bet that the
4660 // terminal uses 8-bit codes.
4661 if (tp[0] == CSI)
4662 switch_to_8bit();
4663
4664 // Screen sends 40500.
4665 // rxvt sends its version number: "20703" is 2.7.3.
4666 // Ignore it for when the user has set 'term' to xterm,
4667 // even though it's an rxvt.
4668 if (version > 20000)
4669 version = 0;
4670
zeertzjqfc78a032022-04-26 22:11:38 +01004671 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004672 if (first == '>' && argc == 3)
4673 {
4674 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004675
4676 // mintty 2.9.5 sends 77;20905;0c.
4677 // (77 is ASCII 'M' for mintty.)
4678 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004679 {
4680 // mintty can do SGR mouse reporting
4681 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4682 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004683
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004684 // If xterm version >= 141 try to get termcap codes. For other
4685 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004686 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004687 {
4688 LOG_TR(("Enable checking for XT codes"));
4689 check_for_codes = TRUE;
4690 need_gather = TRUE;
4691 req_codes_from_term();
4692 }
4693
4694 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01004695 // Konsole sends 0;115;0 and works the same way
4696 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004697 {
4698 // If run from Vim $COLORS is set to the number of
4699 // colors the terminal supports. Otherwise assume
4700 // 256, libvterm supports even more.
4701 if (mch_getenv((char_u *)"COLORS") == NULL)
4702 may_adjust_color_count(256);
4703 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004704 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004705 }
4706
4707 if (version == 95)
4708 {
4709 // Mac Terminal.app sends 1;95;0
4710 if (arg[0] == 1 && arg[2] == 0)
4711 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004712 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4713 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004714 }
4715 // iTerm2 sends 0;95;0
4716 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004717 {
4718 // iTerm2 can do SGR mouse reporting
4719 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4720 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004721 // old iTerm2 sends 0;95;
4722 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004723 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004724 }
4725
4726 // screen sends 83;40500;0 83 is 'S' in ASCII.
4727 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004728 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004729 // screen supports SGR mouse codes since 4.7.0
4730 if (arg[1] >= 40700)
4731 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4732 else
4733 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4734 }
4735
4736 // If no recognized terminal has set mouse behavior, assume xterm.
4737 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4738 {
4739 // Xterm version 277 supports SGR.
4740 // Xterm version >= 95 supports mouse dragging.
4741 if (version >= 277)
4742 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004743 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004744 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004745 }
4746
4747 // Detect terminals that set $TERM to something like
4748 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004749 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004750 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004751 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004752 // xfce4-terminal sends 1;2802;0.
4753 // screen sends 83;40500;0
4754 // Assuming any version number over 2500 is not an
4755 // xterm (without the limit for rxvt and screen).
4756 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004757 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004758
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004759 else if (version == 136 && arg[2] == 0)
4760 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004761 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004762
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004763 // PuTTY sends 0;136;0
4764 if (arg[0] == 0)
4765 {
4766 // supports sgr-like mouse reporting.
4767 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4768 }
4769 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004770 }
4771
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004772 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
4773 // commented out.
4774 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
4775 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004776
Bram Moolenaar1573e732022-11-16 20:33:21 +00004777 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01004778 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
4779 {
4780 term_props[TPR_KITTY].tpr_status = TPR_YES;
4781 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
4782 }
4783
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004784 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004785 // 30600/40500 is a version number of GNU screen. DA2 support is added
4786 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4787 // compatibility checking does not detect GNU screen.
4788 if (arg[0] == 83 && arg[1] >= 30600)
4789 {
4790 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4791 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4792 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004793
4794 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004795 // 95, so assume anything below 95 is not xterm and hopefully supports
4796 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004797 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004798 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004799
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004800 // Getting the cursor style is only supported properly by xterm since
4801 // version 279 (otherwise it returns 0x18).
4802 if (version < 279)
4803 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4804
4805 /*
4806 * Take action on the detected properties.
4807 */
4808
4809 // Unless the underline RGB color is expected to work, disable "t_8u".
4810 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004811 // This may cause some flicker. Alternative would be to set "t_8u"
4812 // here if the terminal is expected to support it, but that might
4813 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01004814 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
4815 && *T_8U != NUL
4816 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004817 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004818 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4819 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004820 }
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01004821 if (*T_8U != NUL && write_t_8u_state == MAYBE)
4822 // Did skip writing t_8u, a complete redraw is needed.
4823 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01004824 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004825
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004826 // Only set 'ttymouse' automatically if it was not set
4827 // by the user already.
4828 if (!option_was_set((char_u *)"ttym")
4829 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4830 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4831 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004832 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004833 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4834 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4835 }
4836
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004837 // Only request the cursor style if t_SH and t_RS are
4838 // set. Only supported properly by xterm since version
4839 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004840 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004841 // Not for Terminal.app, it can't handle t_RS, it
4842 // echoes the characters to the screen.
4843 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004844 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004845 && *T_CSH != NUL
4846 && *T_CRS != NUL)
4847 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004848 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004849 LOG_TR(("Sending cursor style request"));
4850 out_str(T_CRS);
4851 termrequest_sent(&rcs_status);
4852 need_flush = TRUE;
4853 }
4854
4855 // Only request the cursor blink mode if t_RC set. Not
4856 // for Gnome terminal, it can't handle t_RC, it
4857 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004858 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004859 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004860 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004861 && *T_CRC != NUL)
4862 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004863 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004864 LOG_TR(("Sending cursor blink mode request"));
4865 out_str(T_CRC);
4866 termrequest_sent(&rbm_status);
4867 need_flush = TRUE;
4868 }
4869
4870 if (need_flush)
4871 out_flush();
4872 }
4873}
4874
4875/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01004876 * Add "key" to "buf" and return the number of bytes used.
4877 * Handles special keys and multi-byte characters.
4878 */
4879 static int
4880add_key_to_buf(int key, char_u *buf)
4881{
4882 int idx = 0;
4883
4884 if (IS_SPECIAL(key))
4885 {
4886 buf[idx++] = K_SPECIAL;
4887 buf[idx++] = KEY2TERMCAP0(key);
4888 buf[idx++] = KEY2TERMCAP1(key);
4889 }
4890 else if (has_mbyte)
4891 idx += (*mb_char2bytes)(key, buf + idx);
4892 else
4893 buf[idx++] = key;
4894 return idx;
4895}
4896
4897/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004898 * Handle a sequence with key and modifier, one of:
4899 * {lead}27;{modifier};{key}~
4900 * {lead}{key};{modifier}u
4901 * Returns the difference in length.
4902 */
4903 static int
4904handle_key_with_modifier(
4905 int *arg,
4906 int trail,
4907 int csi_len,
4908 int offset,
4909 char_u *buf,
4910 int bufsize,
4911 int *buflen)
4912{
4913 int key;
4914 int modifiers;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004915 char_u string[MAX_KEY_CODE_LEN + 1];
4916
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01004917 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
4918 // like this but does not have the modifyOtherKeys feature.
4919 if (term_props[TPR_KITTY].tpr_status != TPR_YES)
4920 seenModifyOtherKeys = TRUE;
4921
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004922 if (trail == 'u')
4923 key = arg[0];
4924 else
4925 key = arg[2];
4926
4927 modifiers = decode_modifiers(arg[1]);
4928
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02004929 // Some keys need adjustment when the Ctrl modifier is used.
4930 key = may_adjust_key_for_ctrl(modifiers, key);
4931
Bram Moolenaaref6746f2020-06-20 14:43:23 +02004932 // May remove the shift modifier if it's already included in the key.
4933 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004934
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004935 // insert modifiers with KS_MODIFIER
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01004936 int new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004937
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01004938 // add the bytes for the key
4939 new_slen += add_key_to_buf(key, string + new_slen);
4940
4941 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4942 buf, bufsize, buflen) == FAIL)
4943 return -1;
4944 return new_slen - csi_len + offset;
4945}
4946
4947/*
4948 * Handle a sequence with key without a modifier:
4949 * {lead}{key}u
4950 * Returns the difference in length.
4951 */
4952 static int
4953handle_key_without_modifier(
4954 int *arg,
4955 int csi_len,
4956 int offset,
4957 char_u *buf,
4958 int bufsize,
4959 int *buflen)
4960{
4961 char_u string[MAX_KEY_CODE_LEN + 1];
4962 int new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004963
4964 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4965 buf, bufsize, buflen) == FAIL)
4966 return -1;
4967 return new_slen - csi_len + offset;
4968}
4969
4970/*
4971 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004972 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004973 *
4974 * - Cursor position report: {lead}{row};{col}R
4975 * The final byte must be 'R'. It is used for checking the
4976 * ambiguous-width character state.
4977 *
4978 * - window position reply: {lead}3;{x};{y}t
4979 *
4980 * - key with modifiers when modifyOtherKeys is enabled:
4981 * {lead}27;{modifier};{key}~
4982 * {lead}{key};{modifier}u
4983 * Return 0 for no match, -1 for partial match, > 0 for full match.
4984 */
4985 static int
4986handle_csi(
4987 char_u *tp,
4988 int len,
4989 char_u *argp,
4990 int offset,
4991 char_u *buf,
4992 int bufsize,
4993 int *buflen,
4994 char_u *key_name,
4995 int *slen)
4996{
4997 int first = -1; // optional char right after {lead}
4998 int trail; // char that ends CSI sequence
4999 int arg[3] = {-1, -1, -1}; // argument numbers
5000 int argc; // number of arguments
5001 char_u *ap = argp;
5002 int csi_len;
5003
5004 // Check for non-digit after CSI.
5005 if (!VIM_ISDIGIT(*ap))
5006 first = *ap++;
5007
5008 // Find up to three argument numbers.
5009 for (argc = 0; argc < 3; )
5010 {
5011 if (ap >= tp + len)
5012 return -1;
5013 if (*ap == ';')
5014 arg[argc++] = -1; // omitted number
5015 else if (VIM_ISDIGIT(*ap))
5016 {
5017 arg[argc] = 0;
5018 for (;;)
5019 {
5020 if (ap >= tp + len)
5021 return -1;
5022 if (!VIM_ISDIGIT(*ap))
5023 break;
5024 arg[argc] = arg[argc] * 10 + (*ap - '0');
5025 ++ap;
5026 }
5027 ++argc;
5028 }
5029 if (*ap == ';')
5030 ++ap;
5031 else
5032 break;
5033 }
5034
5035 // mrxvt has been reported to have "+" in the version. Assume
5036 // the escape sequence ends with a letter or one of "{|}~".
5037 while (ap < tp + len
5038 && !(*ap >= '{' && *ap <= '~')
5039 && !ASCII_ISALPHA(*ap))
5040 ++ap;
5041 if (ap >= tp + len)
5042 return -1;
5043 trail = *ap;
5044 csi_len = (int)(ap - tp) + 1;
5045
5046 // Cursor position report: Eat it when there are 2 arguments
5047 // and it ends in 'R'. Also when u7_status is not "sent", it
5048 // may be from a previous Vim that just exited. But not for
5049 // <S-F3>, it sends something similar, check for row and column
5050 // to make sense.
5051 if (first == -1 && argc == 2 && trail == 'R')
5052 {
5053 handle_u7_response(arg, tp, csi_len);
5054
5055 key_name[0] = (int)KS_EXTRA;
5056 key_name[1] = (int)KE_IGNORE;
5057 *slen = csi_len;
5058 }
5059
5060 // Version string: Eat it when there is at least one digit and
5061 // it ends in 'c'
5062 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5063 {
5064 handle_version_response(first, arg, argc, tp);
5065
5066 *slen = csi_len;
5067# ifdef FEAT_EVAL
5068 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
5069# endif
5070 apply_autocmds(EVENT_TERMRESPONSE,
5071 NULL, NULL, FALSE, curbuf);
5072 key_name[0] = (int)KS_EXTRA;
5073 key_name[1] = (int)KE_IGNORE;
5074 }
5075
5076 // Check blinking cursor from xterm:
5077 // {lead}?12;1$y set
5078 // {lead}?12;2$y not set
5079 //
5080 // {lead} can be <Esc>[ or CSI
5081 else if (rbm_status.tr_progress == STATUS_SENT
5082 && first == '?'
5083 && ap == argp + 6
5084 && arg[0] == 12
5085 && ap[-1] == '$'
5086 && trail == 'y')
5087 {
5088 initial_cursor_blink = (arg[1] == '1');
5089 rbm_status.tr_progress = STATUS_GOT;
5090 LOG_TR(("Received cursor blinking mode response: %s", tp));
5091 key_name[0] = (int)KS_EXTRA;
5092 key_name[1] = (int)KE_IGNORE;
5093 *slen = csi_len;
5094# ifdef FEAT_EVAL
5095 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
5096# endif
5097 }
5098
5099 // Check for a window position response from the terminal:
5100 // {lead}3;{x};{y}t
5101 else if (did_request_winpos && argc == 3 && arg[0] == 3
5102 && trail == 't')
5103 {
5104 winpos_x = arg[1];
5105 winpos_y = arg[2];
5106 // got finished code: consume it
5107 key_name[0] = (int)KS_EXTRA;
5108 key_name[1] = (int)KE_IGNORE;
5109 *slen = csi_len;
5110
5111 if (--did_request_winpos <= 0)
5112 winpos_status.tr_progress = STATUS_GOT;
5113 }
5114
5115 // Key with modifier:
5116 // {lead}27;{modifier};{key}~
5117 // {lead}{key};{modifier}u
Bram Moolenaar7609c882022-10-19 20:07:09 +01005118 // Only handles four modifiers, this won't work if the modifier value is
5119 // more than 16.
5120 else if (((arg[0] == 27 && argc == 3 && trail == '~')
5121 || (argc == 2 && trail == 'u'))
5122 && arg[1] <= 16)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005123 {
5124 return len + handle_key_with_modifier(arg, trail,
5125 csi_len, offset, buf, bufsize, buflen);
5126 }
5127
Christopher Plewright03193062022-11-22 12:58:27 +00005128 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005129 // {lead}{key}u
5130 else if (argc == 1 && trail == 'u')
5131 {
5132 return len + handle_key_without_modifier(arg,
5133 csi_len, offset, buf, bufsize, buflen);
5134 }
5135
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005136 // else: Unknown CSI sequence. We could drop it, but then the
5137 // user can't create a map for it.
5138 return 0;
5139}
5140
5141/*
5142 * Handle an OSC sequence, fore/background color response from the terminal:
5143 *
5144 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5145 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5146 *
5147 * {code} is 10 for foreground, 11 for background
5148 * {lead} can be <Esc>] or OSC
5149 * {tail} can be '\007', <Esc>\ or STERM.
5150 *
5151 * Consume any code that starts with "{lead}11;", it's also
5152 * possible that "rgba" is following.
5153 */
5154 static int
5155handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5156{
5157 int i, j;
5158
5159 j = 1 + (tp[0] == ESC);
5160 if (len >= j + 3 && (argp[0] != '1'
5161 || (argp[1] != '1' && argp[1] != '0')
5162 || argp[2] != ';'))
5163 i = 0; // no match
5164 else
5165 for (i = j; i < len; ++i)
5166 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5167 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5168 {
5169 int is_bg = argp[1] == '1';
5170 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5171 && tp[j + 16] == '/';
5172
5173 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5174 && (is_4digit
5175 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5176 {
5177 char_u *tp_r = tp + j + 7;
5178 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5179 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
5180# ifdef FEAT_TERMINAL
5181 int rval, gval, bval;
5182
5183 rval = hexhex2nr(tp_r);
5184 gval = hexhex2nr(tp_b);
5185 bval = hexhex2nr(tp_g);
5186# endif
5187 if (is_bg)
5188 {
5189 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5190 *tp_b) ? "light" : "dark";
5191
5192 LOG_TR(("Received RBG response: %s", tp));
5193 rbg_status.tr_progress = STATUS_GOT;
5194# ifdef FEAT_TERMINAL
5195 bg_r = rval;
5196 bg_g = gval;
5197 bg_b = bval;
5198# endif
5199 if (!option_was_set((char_u *)"bg")
5200 && STRCMP(p_bg, new_bg_val) != 0)
5201 {
5202 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005203 set_option_value_give_err((char_u *)"bg",
5204 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005205 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005206 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005207 }
5208 }
5209# ifdef FEAT_TERMINAL
5210 else
5211 {
5212 LOG_TR(("Received RFG response: %s", tp));
5213 rfg_status.tr_progress = STATUS_GOT;
5214 fg_r = rval;
5215 fg_g = gval;
5216 fg_b = bval;
5217 }
5218# endif
5219 }
5220
5221 // got finished code: consume it
5222 key_name[0] = (int)KS_EXTRA;
5223 key_name[1] = (int)KE_IGNORE;
5224 *slen = i + 1 + (tp[i] == ESC);
5225# ifdef FEAT_EVAL
5226 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5227 : VV_TERMRFGRESP, tp, *slen);
5228# endif
5229 break;
5230 }
5231 if (i == len)
5232 {
5233 LOG_TR(("not enough characters for RB"));
5234 return FAIL;
5235 }
5236 return OK;
5237}
5238
5239/*
5240 * Check for key code response from xterm:
5241 * {lead}{flag}+r<hex bytes><{tail}
5242 *
5243 * {lead} can be <Esc>P or DCS
5244 * {flag} can be '0' or '1'
5245 * {tail} can be Esc>\ or STERM
5246 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005247 * Check for resource response from xterm (and drop it):
5248 * {lead}{flag}+R<hex bytes>=<value>{tail}
5249 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005250 * Check for cursor shape response from xterm:
5251 * {lead}1$r<digit> q{tail}
5252 *
5253 * {lead} can be <Esc>P or DCS
5254 * {tail} can be <Esc>\ or STERM
5255 *
5256 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5257 */
5258 static int
5259handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5260{
5261 int i, j;
5262
5263 j = 1 + (tp[0] == ESC);
5264 if (len < j + 3)
5265 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005266 else if ((argp[1] != '+' && argp[1] != '$')
5267 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005268 i = 0; // no match
5269 else if (argp[1] == '+')
5270 // key code response
5271 for (i = j; i < len; ++i)
5272 {
5273 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5274 || tp[i] == STERM)
5275 {
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005276 // handle a key code response, drop a resource response
5277 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005278 got_code_from_term(tp + j, i);
5279 key_name[0] = (int)KS_EXTRA;
5280 key_name[1] = (int)KE_IGNORE;
5281 *slen = i + 1 + (tp[i] == ESC);
5282 break;
5283 }
5284 }
5285 else
5286 {
5287 // Probably the cursor shape response. Make sure that "i"
5288 // is equal to "len" when there are not sufficient
5289 // characters.
5290 for (i = j + 3; i < len; ++i)
5291 {
5292 if (i - j == 3 && !isdigit(tp[i]))
5293 break;
5294 if (i - j == 4 && tp[i] != ' ')
5295 break;
5296 if (i - j == 5 && tp[i] != 'q')
5297 break;
5298 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5299 break;
5300 if ((i - j == 6 && tp[i] == STERM)
5301 || (i - j == 7 && tp[i] == '\\'))
5302 {
5303 int number = argp[3] - '0';
5304
5305 // 0, 1 = block blink, 2 = block
5306 // 3 = underline blink, 4 = underline
5307 // 5 = vertical bar blink, 6 = vertical bar
5308 number = number == 0 ? 1 : number;
5309 initial_cursor_shape = (number + 1) / 2;
5310 // The blink flag is actually inverted, compared to
5311 // the value set with T_SH.
5312 initial_cursor_shape_blink =
5313 (number & 1) ? FALSE : TRUE;
5314 rcs_status.tr_progress = STATUS_GOT;
5315 LOG_TR(("Received cursor shape response: %s", tp));
5316
5317 key_name[0] = (int)KS_EXTRA;
5318 key_name[1] = (int)KE_IGNORE;
5319 *slen = i + 1;
5320# ifdef FEAT_EVAL
5321 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5322# endif
5323 break;
5324 }
5325 }
5326 }
5327
5328 if (i == len)
5329 {
5330 // These codes arrive many together, each code can be
5331 // truncated at any point.
5332 LOG_TR(("not enough characters for XT"));
5333 return FAIL;
5334 }
5335 return OK;
5336}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005337#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005338
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 * Check if typebuf.tb_buf[] contains a terminal key code.
5341 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005342 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005344 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 * With a match, the match is removed, the replacement code is inserted in
5346 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5347 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005348 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5349 * "buflen" is then the length of the string in buf[] and is updated for
5350 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 */
5352 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005353check_termcode(
5354 int max_offset,
5355 char_u *buf,
5356 int bufsize,
5357 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358{
5359 char_u *tp;
5360 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005361 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005362 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005364 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 int offset;
5366 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005367 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005368 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005369 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005370 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 char_u string[MAX_KEY_CODE_LEN + 1];
5372 int i, j;
5373 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
5376 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5377
5378 /*
5379 * Speed up the checks for terminal codes by gathering all first bytes
5380 * used in termleader[]. Often this is just a single <Esc>.
5381 */
5382 if (need_gather)
5383 gather_termleader();
5384
5385 /*
5386 * Check at several positions in typebuf.tb_buf[], to catch something like
5387 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5388 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005389 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 * This is used often, KEEP IT FAST!
5391 */
5392 for (offset = 0; offset < max_offset; ++offset)
5393 {
5394 if (buf == NULL)
5395 {
5396 if (offset >= typebuf.tb_len)
5397 break;
5398 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005399 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 }
5401 else
5402 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005403 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 break;
5405 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005406 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 }
5408
5409 /*
5410 * Don't check characters after K_SPECIAL, those are already
5411 * translated terminal chars (avoid translating ~@^Hx).
5412 */
5413 if (*tp == K_SPECIAL)
5414 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005415 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 continue;
5417 }
5418
5419 /*
5420 * Skip this position if the character does not appear as the first
5421 * character in term_strings. This speeds up a lot, since most
5422 * termcodes start with the same character (ESC or CSI).
5423 */
5424 i = *tp;
5425 for (p = termleader; *p && *p != i; ++p)
5426 ;
5427 if (*p == NUL)
5428 continue;
5429
5430 /*
5431 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5432 * are in Insert mode.
5433 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005434 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 continue;
5436
Bram Moolenaar27efc622022-07-01 16:35:45 +01005437 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005438 key_name[0] = NUL; // no key name found yet
5439 key_name[1] = NUL; // no key name found yet
5440 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
5442#ifdef FEAT_GUI
5443 if (gui.in_use)
5444 {
5445 /*
5446 * GUI special key codes are all of the form [CSI xx].
5447 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005448 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 {
5450 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005451 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 slen = 3;
5453 key_name[0] = tp[1];
5454 key_name[1] = tp[2];
5455 }
5456 }
5457 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005458#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00005459#ifdef MSWIN
5460 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
5461 && (tp[2] == KE_MOUSEUP
5462 || tp[2] == KE_MOUSEDOWN
5463 || tp[2] == KE_MOUSELEFT
5464 || tp[2] == KE_MOUSERIGHT))
5465 {
5466 // MS-Windows console sends mouse scroll events encoded:
5467 // - CSI
5468 // - KS_EXTRA
5469 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
5470 slen = 3;
5471 key_name[0] = tp[1];
5472 key_name[1] = tp[2];
5473 }
5474 else
5475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005477 int mouse_index_found = -1;
5478
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 for (idx = 0; idx < tc_len; ++idx)
5480 {
5481 /*
5482 * Ignore the entry if we are not at the start of
5483 * typebuf.tb_buf[]
5484 * and there are not enough characters to make a match.
5485 * But only when the 'K' flag is in 'cpoptions'.
5486 */
5487 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005488 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 if (cpo_koffset && offset && len < slen)
5490 continue;
5491 if (STRNCMP(termcodes[idx].code, tp,
5492 (size_t)(slen > len ? len : slen)) == 0)
5493 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005494 int looks_like_mouse_start = FALSE;
5495
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005496 if (len < slen) // got a partial sequence
5497 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498
5499 /*
5500 * When found a keypad key, check if there is another key
5501 * that matches and use that one. This makes <Home> to be
5502 * found instead of <kHome> when they produce the same
5503 * key code.
5504 */
5505 if (termcodes[idx].name[0] == 'K'
5506 && VIM_ISDIGIT(termcodes[idx].name[1]))
5507 {
5508 for (j = idx + 1; j < tc_len; ++j)
5509 if (termcodes[j].len == slen &&
5510 STRNCMP(termcodes[idx].code,
5511 termcodes[j].code, slen) == 0)
5512 {
5513 idx = j;
5514 break;
5515 }
5516 }
5517
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005518 if (slen == 2 && len > 2
5519 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005520 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005521 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005522 // The mouse termcode "ESC [" is also the prefix of
5523 // "ESC [ I" (focus gained) and other keys. Check some
5524 // more bytes to find out.
5525 if (!isdigit(tp[2]))
5526 {
5527 // ESC [ without number following: Only use it when
5528 // there is no other match.
5529 looks_like_mouse_start = TRUE;
5530 }
5531 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5532 {
5533 char_u *nr = tp + 2;
5534 int count = 0;
5535
5536 // If a digit is following it could be a key with
5537 // modifier, e.g., ESC [ 1;2P. Can be confused
5538 // with DEC_MOUSE, which requires four numbers
5539 // following. If not then it can't be a DEC_MOUSE
5540 // code.
5541 for (;;)
5542 {
5543 ++count;
5544 (void)getdigits(&nr);
5545 if (nr >= tp + len)
5546 return -1; // partial sequence
5547 if (*nr != ';')
5548 break;
5549 ++nr;
5550 if (nr >= tp + len)
5551 return -1; // partial sequence
5552 }
5553 if (count < 4)
5554 continue; // no match
5555 }
5556 }
5557 if (looks_like_mouse_start)
5558 {
5559 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005560 if (mouse_index_found < 0)
5561 mouse_index_found = idx;
5562 }
5563 else
5564 {
5565 key_name[0] = termcodes[idx].name[0];
5566 key_name[1] = termcodes[idx].name[1];
5567 break;
5568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005570
5571 /*
5572 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005573 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005574 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005575 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5576 * When there is a modifier the * matches a number.
5577 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005578 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005579 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005580 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005581 modslen = termcodes[idx].modlen;
5582 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005583 continue;
5584 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005585 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005586 {
5587 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005588
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005589 if (len <= modslen) // got a partial sequence
5590 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005591
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005592 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005593 // no modifiers
5594 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005595 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005596 // no match for "code;*X" with "code;"
5597 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01005598 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00005599 && (tp[modslen] != '1'
5600 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00005601 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005602 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005603 else
5604 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005605 // Skip over the digits, the final char must
5606 // follow. URXVT can use a negative value, thus
5607 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005608 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005609 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005610 ;
5611 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005612 if (len < j) // got a partial sequence
5613 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005614 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005615 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005616
Bram Moolenaara529ce02017-06-22 22:37:57 +02005617 modifiers_start = tp + slen - 2;
5618
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005619 // Match! Convert modifier bits.
5620 n = atoi((char *)modifiers_start);
5621 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005622
5623 slen = j;
5624 }
5625 key_name[0] = termcodes[idx].name[0];
5626 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005627 break;
5628 }
5629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005631 if (idx == tc_len && mouse_index_found >= 0)
5632 {
5633 key_name[0] = termcodes[mouse_index_found].name[0];
5634 key_name[1] = termcodes[mouse_index_found].name[1];
5635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
5637
5638#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005639 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005640 // Mouse codes of DEC and pterm start with <ESC>[. When
5641 // detecting the start of these mouse codes they might as well be
5642 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005643# ifdef FEAT_MOUSE_DEC
5644 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005645# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005646# ifdef FEAT_MOUSE_PTERM
5647 || key_name[0] == KS_PTERM_MOUSE
5648# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005649 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005651 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5652
5653 /*
5654 * Check for responses from the terminal starting with {lead}:
5655 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005656 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005657 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005658 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005659 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005660 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005661 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005662 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005663 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005664 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005665 * - window position reply: {lead}3;{x};{y}t
5666 *
5667 * - key with modifiers when modifyOtherKeys is enabled:
5668 * {lead}27;{modifier};{key}~
5669 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005670 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005671 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005672 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005673 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005675 int resp = handle_csi(tp, len, argp, offset, buf,
5676 bufsize, buflen, key_name, &slen);
5677 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005678 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005679# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005680 if (resp == -1)
5681 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005682# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005683 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005684 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005685 }
5686
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005687 // Check for fore/background color response from the terminal,
5688 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005689 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005690 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5691 || tp[0] == OSC))
5692 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005693 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005694 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
5696
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005697 // Check for key code response from xterm,
5698 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005699 // It would only be needed with this condition:
5700 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
5701 // Now this is always done so that DCS codes don't mess up things.
5702 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005704 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005705 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
5707 }
5708#endif
5709
5710 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005711 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005713 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005715#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 /*
5717 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5718 * so that we know which window to scroll later.
5719 */
5720 if (gui.in_use
5721 && key_name[0] == (int)KS_EXTRA
5722 && (key_name[1] == (int)KE_X1MOUSE
5723 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005724 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005725 || key_name[1] == (int)KE_MOUSELEFT
5726 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 || key_name[1] == (int)KE_MOUSEDOWN
5728 || key_name[1] == (int)KE_MOUSEUP))
5729 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005730 char_u bytes[6];
5731 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5732
5733 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 return -1;
5735 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5736 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5737 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005738 // equal to K_MOUSEMOVE
5739 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
5740 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 }
5742 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 /*
5745 * If it is a mouse click, get the coordinates.
5746 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005747 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005748#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005749 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005750#endif
5751#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005752 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005753#endif
5754#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005755 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005756#endif
5757#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005758 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005759#endif
5760#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005761 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005762#endif
5763#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005764 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005765#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005766 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005767 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005769 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5770 &modifiers) == -1)
5771 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
5774#ifdef FEAT_GUI
5775 /*
5776 * If using the GUI, then we get menu and scrollbar events.
5777 *
5778 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5779 * four bytes which are to be taken as a pointer to the vimmenu_T
5780 * structure.
5781 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005782 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005783 * is one byte with the tab index.
5784 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5786 * by one byte representing the scrollbar number, and then four bytes
5787 * representing a long_u which is the new value of the scrollbar.
5788 *
5789 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5790 * KE_FILLER followed by four bytes representing a long_u which is the
5791 * new value of the scrollbar.
5792 */
5793# ifdef FEAT_MENU
5794 else if (key_name[0] == (int)KS_MENU)
5795 {
5796 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005797 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 if (num_bytes == -1)
5800 return -1;
5801 current_menu = (vimmenu_T *)val;
5802 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005803
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005804 // The menu may have been deleted right after it was used, check
5805 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005806 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5807 {
5808 key_name[0] = KS_EXTRA;
5809 key_name[1] = (int)KE_IGNORE;
5810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 }
5812# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005813# ifdef FEAT_GUI_TABLINE
5814 else if (key_name[0] == (int)KS_TABLINE)
5815 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005816 // Selecting tabline tab or using its menu.
5817 char_u bytes[6];
5818 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5819
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005820 if (num_bytes == -1)
5821 return -1;
5822 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005823 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005824 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005825 slen += num_bytes;
5826 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005827 else if (key_name[0] == (int)KS_TABMENU)
5828 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005829 // Selecting tabline tab or using its menu.
5830 char_u bytes[6];
5831 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5832
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005833 if (num_bytes == -1)
5834 return -1;
5835 current_tab = (int)bytes[0];
5836 current_tabmenu = (int)bytes[1];
5837 slen += num_bytes;
5838 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005839# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840# ifndef USE_ON_FLY_SCROLL
5841 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5842 {
5843 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005844 char_u bytes[6];
5845 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005847 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 j = 0;
5849 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5850 && tp[j + 2] != NUL; ++i)
5851 {
5852 j += 3;
5853 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5854 if (num_bytes == -1)
5855 break;
5856 if (i == 0)
5857 current_scrollbar = (int)bytes[0];
5858 else if (current_scrollbar != (int)bytes[0])
5859 break;
5860 j += num_bytes;
5861 num_bytes = get_long_from_buf(tp + j, &val);
5862 if (num_bytes == -1)
5863 break;
5864 scrollbar_value = val;
5865 j += num_bytes;
5866 slen = j;
5867 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005868 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 return -1;
5870 }
5871 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5872 {
5873 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005874 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005876 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 j = 0;
5878 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5879 && tp[j + 2] != NUL; ++i)
5880 {
5881 j += 3;
5882 num_bytes = get_long_from_buf(tp + j, &val);
5883 if (num_bytes == -1)
5884 break;
5885 scrollbar_value = val;
5886 j += num_bytes;
5887 slen = j;
5888 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005889 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 return -1;
5891 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005892# endif // !USE_ON_FLY_SCROLL
5893#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005895#if (defined(UNIX) || defined(VMS))
5896 /*
5897 * Handle FocusIn/FocusOut event sequences reported by XTerm.
5898 * (CSI I/CSI O)
5899 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00005900 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005901# ifdef FEAT_GUI
5902 && !gui.in_use
5903# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005904 )
5905 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005906 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005907 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005908 if (!focus_state)
5909 {
5910 ui_focus_change(TRUE);
5911 did_cursorhold = TRUE;
5912 focus_state = TRUE;
5913 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005914 key_name[1] = (int)KE_IGNORE;
5915 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01005916 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005917 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005918 if (focus_state)
5919 {
5920 ui_focus_change(FALSE);
5921 did_cursorhold = TRUE;
5922 focus_state = FALSE;
5923 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005924 key_name[1] = (int)KE_IGNORE;
5925 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005926 }
5927#endif
5928
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005929 /*
5930 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5931 */
5932 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5933
5934 /*
5935 * Add any modifier codes to our string.
5936 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005937 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005938
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005939 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005940 key_name[0] = KEY2TERMCAP0(key);
5941 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005943 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005944 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005945 if (has_mbyte)
5946 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5947 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005948 string[new_slen++] = key_name[1];
5949 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005950 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5951 && key_name[1] == KE_IGNORE)
5952 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005953 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5954 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005955 retval = KEYLEN_REMOVED;
5956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 else
5958 {
5959 string[new_slen++] = K_SPECIAL;
5960 string[new_slen++] = key_name[0];
5961 string[new_slen++] = key_name[1];
5962 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005963 if (put_string_in_typebuf(offset, slen, string, new_slen,
5964 buf, bufsize, buflen) == FAIL)
5965 return -1;
5966 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 }
5968
Bram Moolenaar2951b772013-07-03 12:45:31 +02005969#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005970 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005971#endif
5972
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005973 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974}
5975
Bram Moolenaar9377df32017-10-15 13:22:01 +02005976#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005977/*
5978 * Get the text foreground color, if known.
5979 */
5980 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005981term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005982{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005983 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005984 {
5985 *r = fg_r;
5986 *g = fg_g;
5987 *b = fg_b;
5988 }
5989}
5990
5991/*
5992 * Get the text background color, if known.
5993 */
5994 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005995term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005996{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005997 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005998 {
5999 *r = bg_r;
6000 *g = bg_g;
6001 *b = bg_b;
6002 }
6003}
6004#endif
6005
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006/*
6007 * Replace any terminal code strings in from[] with the equivalent internal
6008 * vim representation. This is used for the "from" and "to" part of a
6009 * mapping, and the "to" part of a menu command.
6010 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6011 * '<'.
6012 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6013 *
6014 * The replacement is done in result[] and finally copied into allocated
6015 * memory. If this all works well *bufp is set to the allocated memory and a
6016 * pointer to it is returned. If something fails *bufp is set to NULL and from
6017 * is returned.
6018 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006019 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6020 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6021 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6022 * used instead of a CTRL-V.
6023 *
6024 * Flags:
6025 * REPTERM_FROM_PART see above
6026 * REPTERM_DO_LT also translate <lt>
6027 * REPTERM_SPECIAL always accept <key> notation
6028 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6029 *
6030 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6031 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006033 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006034replace_termcodes(
6035 char_u *from,
6036 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02006037 int flags,
6038 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039{
6040 int i;
6041 int slen;
6042 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006043 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006045 int do_backslash; // backslash is a special character
6046 int do_special; // recognize <> key codes
6047 int do_key_code; // recognize raw key codes
6048 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006049 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050
6051 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006052 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6053 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006055 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
6057 /*
6058 * Allocate space for the translation. Worst case a single character is
6059 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006060 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006062 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006063 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 {
6065 *bufp = NULL;
6066 return from;
6067 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006068 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069
6070 /*
6071 * Check for #n at start only: function key n
6072 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006073 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 {
6075 result[dlen++] = K_SPECIAL;
6076 result[dlen++] = 'k';
6077 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006078 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006080 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 src += 2;
6082 }
6083
6084 /*
6085 * Copy each byte from *from to result[dlen]
6086 */
6087 while (*src != NUL)
6088 {
6089 /*
6090 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006091 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006093 if (do_special && ((flags & REPTERM_DO_LT)
6094 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 {
6096#ifdef FEAT_EVAL
6097 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006098 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6099 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006101 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6102 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 */
6104 if (STRNICMP(src, "<SID>", 5) == 0)
6105 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006106 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006107 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 else
6109 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006110 char_u *dot;
6111 long sid = current_sctx.sc_sid;
6112
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006114 if (in_vim9script()
6115 && (dot = vim_strchr(src, '.')) != NULL)
6116 {
6117 imported_T *imp = find_imported(src, dot - src, FALSE);
6118
6119 if (imp != NULL)
6120 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006121 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6122 size_t len;
6123
Bram Moolenaar89445512022-04-14 12:58:23 +01006124 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006125 if (si->sn_autoload_prefix != NULL)
6126 {
6127 // Turn "<SID>name.Func"
6128 // into "scriptname#Func".
6129 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006130 if (ga_grow(&ga,
6131 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006132 {
6133 ga_clear(&ga);
6134 *bufp = NULL;
6135 return from;
6136 }
6137 result = ga.ga_data;
6138 STRCPY(result + dlen, si->sn_autoload_prefix);
6139 dlen += len;
6140 continue;
6141 }
6142 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006143 }
6144 }
6145
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 result[dlen++] = K_SPECIAL;
6147 result[dlen++] = (int)KS_EXTRA;
6148 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006149 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006150 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 result[dlen++] = '_';
6152 continue;
6153 }
6154 }
6155#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006156 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6157 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006158 TRUE, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 if (slen)
6160 {
6161 dlen += slen;
6162 continue;
6163 }
6164 }
6165
6166 /*
6167 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6168 * Note that this is also checked after replacing the <> form.
6169 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6170 * it could be a character in the file.
6171 */
6172 if (do_key_code)
6173 {
6174 i = find_term_bykeys(src);
6175 if (i >= 0)
6176 {
6177 result[dlen++] = K_SPECIAL;
6178 result[dlen++] = termcodes[i].name[0];
6179 result[dlen++] = termcodes[i].name[1];
6180 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006181 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 continue;
6183 }
6184 }
6185
6186#ifdef FEAT_EVAL
6187 if (do_special)
6188 {
6189 char_u *p, *s, len;
6190
6191 /*
6192 * Replace <Leader> by the value of "mapleader".
6193 * Replace <LocalLeader> by the value of "maplocalleader".
6194 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6195 */
6196 if (STRNICMP(src, "<Leader>", 8) == 0)
6197 {
6198 len = 8;
6199 p = get_var_value((char_u *)"g:mapleader");
6200 }
6201 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6202 {
6203 len = 13;
6204 p = get_var_value((char_u *)"g:maplocalleader");
6205 }
6206 else
6207 {
6208 len = 0;
6209 p = NULL;
6210 }
6211 if (len != 0)
6212 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006213 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6215 s = (char_u *)"\\";
6216 else
6217 s = p;
6218 while (*s != NUL)
6219 result[dlen++] = *s++;
6220 src += len;
6221 continue;
6222 }
6223 }
6224#endif
6225
6226 /*
6227 * Remove CTRL-V and ignore the next character.
6228 * For "from" side the CTRL-V at the end is included, for the "to"
6229 * part it is removed.
6230 * If 'cpoptions' does not contain 'B', also accept a backslash.
6231 */
6232 key = *src;
6233 if (key == Ctrl_V || (do_backslash && key == '\\'))
6234 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006235 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 if (*src == NUL)
6237 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006238 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 result[dlen++] = key;
6240 break;
6241 }
6242 }
6243
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006244 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006245 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 {
6247 /*
6248 * If the character is K_SPECIAL, replace it with K_SPECIAL
6249 * KS_SPECIAL KE_FILLER.
6250 * If compiled with the GUI replace CSI with K_CSI.
6251 */
6252 if (*src == K_SPECIAL)
6253 {
6254 result[dlen++] = K_SPECIAL;
6255 result[dlen++] = KS_SPECIAL;
6256 result[dlen++] = KE_FILLER;
6257 }
6258# ifdef FEAT_GUI
6259 else if (*src == CSI)
6260 {
6261 result[dlen++] = K_SPECIAL;
6262 result[dlen++] = KS_EXTRA;
6263 result[dlen++] = (int)KE_CSI;
6264 }
6265# endif
6266 else
6267 result[dlen++] = *src;
6268 ++src;
6269 }
6270 }
6271 result[dlen] = NUL;
6272
6273 /*
6274 * Copy the new string to allocated memory.
6275 * If this fails, just return from.
6276 */
6277 if ((*bufp = vim_strsave(result)) != NULL)
6278 from = *bufp;
6279 vim_free(result);
6280 return from;
6281}
6282
6283/*
6284 * Find a termcode with keys 'src' (must be NUL terminated).
6285 * Return the index in termcodes[], or -1 if not found.
6286 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006287 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006288find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
6290 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006291 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292
6293 for (i = 0; i < tc_len; ++i)
6294 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006295 if (slen == termcodes[i].len
6296 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 return i;
6298 }
6299 return -1;
6300}
6301
6302/*
6303 * Gather the first characters in the terminal key codes into a string.
6304 * Used to speed up check_termcode().
6305 */
6306 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006307gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308{
6309 int i;
6310 int len = 0;
6311
6312#ifdef FEAT_GUI
6313 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006314 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315#endif
6316#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006317 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006318 termleader[len++] = DCS; // the termcode response starts with DCS
6319 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320#endif
6321 termleader[len] = NUL;
6322
6323 for (i = 0; i < tc_len; ++i)
6324 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6325 {
6326 termleader[len++] = termcodes[i].code[0];
6327 termleader[len] = NUL;
6328 }
6329
6330 need_gather = FALSE;
6331}
6332
6333/*
6334 * Show all termcodes (for ":set termcap")
6335 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006336 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 */
6338 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006339show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340{
6341 int col;
6342 int *items;
6343 int item_count;
6344 int run;
6345 int row, rows;
6346 int cols;
6347 int i;
6348 int len;
6349
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006350#define INC3 27 // try to make three columns
6351#define INC2 40 // try to make two columns
6352#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006354 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006356 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 if (items == NULL)
6358 return;
6359
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006360 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006361 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362
6363 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006364 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006366 * 2. display the medium items (medium length strings)
6367 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006368 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006370 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 {
6372 /*
6373 * collect the items in items[]
6374 */
6375 item_count = 0;
6376 for (i = 0; i < tc_len; i++)
6377 {
6378 len = show_one_termcode(termcodes[i].name,
6379 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006380 if ((flags & OPT_ONECOLUMN) ||
6381 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006382 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006383 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 items[item_count++] = i;
6385 }
6386
6387 /*
6388 * display the items
6389 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006390 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006392 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 if (cols == 0)
6394 cols = 1;
6395 rows = (item_count + cols - 1) / cols;
6396 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006397 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 rows = item_count;
6399 for (row = 0; row < rows && !got_int; ++row)
6400 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006401 msg_putchar('\n'); // go to next line
6402 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 break;
6404 col = 0;
6405 for (i = row; i < item_count; i += rows)
6406 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006407 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 show_one_termcode(termcodes[items[i]].name,
6409 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006410 if (run == 2)
6411 col += INC2;
6412 else
6413 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 }
6415 out_flush();
6416 ui_breakcheck();
6417 }
6418 }
6419 vim_free(items);
6420}
6421
6422/*
6423 * Show one termcode entry.
6424 * Output goes into IObuff[]
6425 */
6426 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006427show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428{
6429 char_u *p;
6430 int len;
6431
6432 if (name[0] > '~')
6433 {
6434 IObuff[0] = ' ';
6435 IObuff[1] = ' ';
6436 IObuff[2] = ' ';
6437 IObuff[3] = ' ';
6438 }
6439 else
6440 {
6441 IObuff[0] = 't';
6442 IObuff[1] = '_';
6443 IObuff[2] = name[0];
6444 IObuff[3] = name[1];
6445 }
6446 IObuff[4] = ' ';
6447
6448 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6449 if (p[1] != 't')
6450 STRCPY(IObuff + 5, p);
6451 else
6452 IObuff[5] = NUL;
6453 len = (int)STRLEN(IObuff);
6454 do
6455 IObuff[len++] = ' ';
6456 while (len < 17);
6457 IObuff[len] = NUL;
6458 if (code == NULL)
6459 len += 4;
6460 else
6461 len += vim_strsize(code);
6462
6463 if (printit)
6464 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006465 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006467 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 else
6469 msg_outtrans(code);
6470 }
6471 return len;
6472}
6473
6474#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6475/*
6476 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6477 * termcap codes from the terminal itself.
6478 * We get them one by one to avoid a very long response string.
6479 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006480static int xt_index_in = 0;
6481static int xt_index_out = 0;
6482
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006484req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485{
6486 xt_index_out = 0;
6487 xt_index_in = 0;
6488 req_more_codes_from_term();
6489}
6490
6491 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006492req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006494 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 int old_idx = xt_index_out;
6496
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006497 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 if (exiting)
6499 return;
6500
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006501 // Send up to 10 more requests out than we received. Avoid sending too
6502 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6504 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006505 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006506
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006507 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006508 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6509 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 out_str_nf((char_u *)buf);
6511 ++xt_index_out;
6512 }
6513
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006514 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 if (xt_index_out != old_idx)
6516 out_flush();
6517}
6518
6519/*
6520 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6521 * A "0" instead of the "1" indicates a code that isn't supported.
6522 * Both <name> and <string> are encoded in hex.
6523 * "code" points to the "0" or "1".
6524 */
6525 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006526got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527{
6528#define XT_LEN 100
6529 char_u name[3];
6530 char_u str[XT_LEN];
6531 int i;
6532 int j = 0;
6533 int c;
6534
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006535 // A '1' means the code is supported, a '0' means it isn't.
6536 // When half the length is > XT_LEN we can't use it.
6537 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6539 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006540 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 name[0] = hexhex2nr(code + 3);
6542 name[1] = hexhex2nr(code + 5);
6543 name[2] = NUL;
6544 for (i = 0; key_names[i] != NULL; ++i)
6545 {
6546 if (STRCMP(key_names[i], name) == 0)
6547 {
6548 xt_index_in = i;
6549 break;
6550 }
6551 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006552
Bram Moolenaarb255b902018-04-24 21:40:10 +02006553 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6554
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 if (key_names[i] != NULL)
6556 {
6557 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6558 str[j++] = c;
6559 str[j] = NUL;
6560 if (name[0] == 'C' && name[1] == 'o')
6561 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006562 // Color count is not a key code.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00006563 may_adjust_color_count(atoi((char *)str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 }
6565 else
6566 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006567 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 i = find_term_bykeys(str);
6569 if (i >= 0)
6570 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006571 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 }
6573 }
6574 }
6575
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006576 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 ++xt_index_in;
6578 req_more_codes_from_term();
6579}
6580
6581/*
6582 * Check if there are any unanswered requests and deal with them.
6583 * This is called before starting an external program or getting direct
6584 * keyboard input. We don't want responses to be send to that program or
6585 * handled as typed text.
6586 */
6587 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006588check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589{
6590 int c;
6591
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006592 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6594 return;
6595
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006596 // Vgetc() will check for and handle any response.
6597 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 ++no_mapping;
6599 ++allow_keys;
6600 for (;;)
6601 {
6602 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006603 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 break;
6605
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006606 // If a response is recognized it's replaced with K_IGNORE, must read
6607 // it from the input stream. If there is no K_IGNORE we can't do
6608 // anything, break here (there might be some responses further on, but
6609 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 if (c != K_SPECIAL && c != K_IGNORE)
6611 break;
6612 c = vgetc();
6613 if (c != K_IGNORE)
6614 {
6615 vungetc(c);
6616 break;
6617 }
6618 }
6619 --no_mapping;
6620 --allow_keys;
6621}
6622#endif
6623
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006624#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625static char ksme_str[20];
6626static char ksmr_str[20];
6627static char ksmd_str[20];
6628
6629/*
6630 * For Win32 console: update termcap codes for existing console attributes.
6631 */
6632 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006633update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006635 sprintf(ksme_str, "\033|%dm", attr);
6636 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
6637 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638
Bram Moolenaar4654d632022-11-17 22:05:12 +00006639 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
6640 if (p == NULL) // did not find it
6641 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 while (p->bt_string != NULL)
6643 {
6644 if (p->bt_entry == (int)KS_ME)
6645 p->bt_string = &ksme_str[0];
6646 else if (p->bt_entry == (int)KS_MR)
6647 p->bt_string = &ksmr_str[0];
6648 else if (p->bt_entry == (int)KS_MD)
6649 p->bt_string = &ksmd_str[0];
6650 ++p;
6651 }
6652}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006653
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006654# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006655# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006656
6657typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006658{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006659 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
6660 CMODE_RGB, // Use 24bit RGB colors using VTP.
6661 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
6662 CMODE_LAST,
6663} cmode_T;
6664
6665struct ks_tbl_S
6666{
6667 int code; // value of KS_
6668 char *vtp; // code in RGB mode
6669 char *vtp2; // code in 256color mode
6670 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006671};
6672
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006673static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006674{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006675 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
6676 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
6677 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
6678 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
6679 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
6680 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
6681 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
6682 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
6683 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006684# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006685 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
6686 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
6687 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
6688 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006689# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006690 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
6691 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
6692 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
6693 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006694# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006695 {(int)KS_CCO, "256", "256", {""}}, // colors
6696 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006697};
6698
Bram Moolenaar4654d632022-11-17 22:05:12 +00006699/*
6700 * Find the first entry for "code" in the builtin termcap for "name".
6701 * Returns NULL when not found.
6702 */
6703 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006704find_first_tcap(
6705 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006706 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006707{
Bram Moolenaar4654d632022-11-17 22:05:12 +00006708 tcap_entry_T *p = find_builtin_term(name);
6709 if (p != NULL)
6710 {
6711 while (p->bt_string != NULL)
6712 {
6713 if (p->bt_entry == code)
6714 return p;
6715 ++p;
6716 }
6717 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006718 return NULL;
6719}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006720# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006721
6722/*
6723 * For Win32 console: replace the sequence immediately after termguicolors.
6724 */
6725 void
6726swap_tcap(void)
6727{
6728# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006729 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006730 static cmode_T curr_mode;
6731 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006732 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006733
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006734 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006735 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006736 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006737 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00006738 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006739 if (bt != NULL)
6740 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006741 // Preserve the original value.
6742 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
6743 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
6744 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006745
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006746 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006747 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006748 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006749 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006750 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006751 }
6752
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006753 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006754 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006755 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006756 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006757 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006758 mode = CMODE_INDEXED;
6759
6760 if (mode == curr_mode)
6761 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006762
6763 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006764 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00006765 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006766 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006767 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006768 }
6769
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006770 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006771# endif
6772}
6773
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006775
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006776
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006777#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006778 || defined(PROTO)
6779static int cube_value[] = {
6780 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6781};
6782
6783static int grey_ramp[] = {
6784 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6785 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6786};
6787
LemonBoyb2b3acb2022-05-20 10:10:34 +01006788static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01006789// R G B
6790 { 0, 0, 0}, // black
6791 {224, 0, 0}, // dark red
6792 { 0, 224, 0}, // dark green
6793 {224, 224, 0}, // dark yellow / brown
6794 { 0, 0, 224}, // dark blue
6795 {224, 0, 224}, // dark magenta
6796 { 0, 224, 224}, // dark cyan
6797 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006798
LemonBoyd2a46622022-05-01 17:43:33 +01006799 {128, 128, 128}, // dark grey
6800 {255, 64, 64}, // light red
6801 { 64, 255, 64}, // light green
6802 {255, 255, 64}, // yellow
6803 { 64, 64, 255}, // light blue
6804 {255, 64, 255}, // light magenta
6805 { 64, 255, 255}, // light cyan
6806 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006807};
6808
LemonBoyd2a46622022-05-01 17:43:33 +01006809#if defined(MSWIN)
6810// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
6811static const char_u cterm_ansi_idx[] = {
6812 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
6813};
6814#endif
6815
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006816#define ANSI_INDEX_NONE 0
6817
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006818 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01006819ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
6820{
6821 if (nr < 16)
6822 {
6823 *r = ansi_table[nr][0];
6824 *g = ansi_table[nr][1];
6825 *b = ansi_table[nr][2];
6826 *ansi_idx = nr;
6827 }
6828 else
6829 {
6830 *r = 0;
6831 *g = 0;
6832 *b = 0;
6833 *ansi_idx = ANSI_INDEX_NONE;
6834 }
6835}
6836
6837 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006838cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006839{
6840 int idx;
6841
6842 if (nr < 16)
6843 {
LemonBoyd2a46622022-05-01 17:43:33 +01006844#if defined(MSWIN)
6845 idx = cterm_ansi_idx[nr];
6846#else
6847 idx = nr;
6848#endif
6849 *r = ansi_table[idx][0];
6850 *g = ansi_table[idx][1];
6851 *b = ansi_table[idx][2];
6852 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006853 }
6854 else if (nr < 232)
6855 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006856 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006857 idx = nr - 16;
6858 *r = cube_value[idx / 36 % 6];
6859 *g = cube_value[idx / 6 % 6];
6860 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006861 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006862 }
6863 else if (nr < 256)
6864 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006865 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006866 idx = nr - 232;
6867 *r = grey_ramp[idx];
6868 *g = grey_ramp[idx];
6869 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006870 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006871 }
6872 else
6873 {
6874 *r = 0;
6875 *g = 0;
6876 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006877 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006878 }
6879}
6880#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006881
Bram Moolenaarf4140482020-02-15 23:06:45 +01006882/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006883 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01006884 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006885 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01006886 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006887 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01006888term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01006889{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006890 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01006891 int i;
6892 int c;
6893
6894 for (i = ta_len; i < ta_len + len; ++i)
6895 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01006896 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
6897 {
6898 int modifiers = ta_buf[i + 2];
6899 int key = ta_buf[i + 3];
6900
6901 // Try to use the modifier to modify the key. In any case drop the
6902 // modifier.
6903 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
6904 len -= 3;
6905 if (key < 0x80)
6906 key = merge_modifyOtherKeys(key, &modifiers);
6907 ta_buf[i] = key;
6908 }
6909 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01006910 {
6911 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6912 if (c == K_DEL || c == K_KDEL || c == K_BS)
6913 {
6914 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01006915 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01006916 if (c == K_DEL || c == K_KDEL)
6917 ta_buf[i] = DEL;
6918 else
6919 ta_buf[i] = Ctrl_H;
6920 len -= 2;
6921 }
6922 }
6923 else if (ta_buf[i] == '\r')
6924 ta_buf[i] = '\n';
6925 if (has_mbyte)
6926 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6927 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006928 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01006929}