blob: 6410a574e1b8aff45ecf613892d8a1de05e0f531 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 *
11 * term.c: functions for controlling the terminal
12 *
Bram Moolenaar48e330a2016-02-23 14:53:34 +010013 * primitive termcap support for Amiga and Win32 included
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * NOTE: padding and variable substitution is not performed,
16 * when compiling without HAVE_TGETENT, we use tputs() and tgoto() dummies.
17 */
18
19/*
20 * Some systems have a prototype for tgetstr() with (char *) instead of
21 * (char **). This define removes that prototype. We include our own prototype
22 * below.
23 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#define tgetstr tgetstr_defined_wrong
Bram Moolenaarad3ec762019-04-21 00:00:13 +020025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010030# include <termios.h> // seems to be required for some Linux
Bram Moolenaar071d4272004-06-13 20:20:40 +000031# endif
32# ifdef HAVE_TERMCAP_H
33# include <termcap.h>
34# endif
35
36/*
37 * A few linux systems define outfuntype in termcap.h to be used as the third
38 * argument for tputs().
39 */
ola.soder@axis.come1314962022-02-13 12:13:38 +000040# if defined(VMS) || defined(AMIGA)
Bram Moolenaar467676d2020-12-30 13:14:45 +010041# define TPUTSFUNCAST (void (*)(unsigned int))
Bram Moolenaar071d4272004-06-13 20:20:40 +000042# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
Bram Moolenaar86394aa2020-09-05 14:27:24 +020046# define TPUTSFUNCAST (int (*)(int))
Bram Moolenaar071d4272004-06-13 20:20:40 +000047# endif
48# endif
49#endif
50
51#undef tgetstr
52
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010053// start of keys that are not directly used by Vim but can be mapped
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#define BT_EXTRA_KEYS 0x101
55
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010057static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010059static void req_codes_from_term(void);
60static void req_more_codes_from_term(void);
61static void got_code_from_term(char_u *code, int len);
62static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000063#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static void del_termcode_idx(int idx);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020065static int find_term_bykeys(char_u *src);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010066static int term_is_builtin(char_u *name);
67static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010069 // Change this to "if 1" to debug what happens with termresponse.
Bram Moolenaar2951b772013-07-03 12:45:31 +020070# if 0
71# define DEBUG_TERMRESPONSE
Bram Moolenaar952d9d82021-08-02 18:07:18 +020072static void log_tr(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Bram Moolenaarb255b902018-04-24 21:40:10 +020073# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +020074# else
Bram Moolenaarb255b902018-04-24 21:40:10 +020075# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +020076# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020077
Bram Moolenaar4e6072b2022-11-29 16:09:18 +000078#ifdef HAVE_TGETENT
79static char *invoke_tgetent(char_u *, char_u *);
80
81/*
82 * Here is our own prototype for tgetstr(), any prototypes from the include
83 * files have been disabled by the define at the start of this file.
84 */
85char *tgetstr(char *, char **);
86#endif
87
Bram Moolenaarafd78262019-05-10 23:10:31 +020088typedef enum {
89 STATUS_GET, // send request when switching to RAW mode
90 STATUS_SENT, // did send request, checking for response
91 STATUS_GOT, // received response
92 STATUS_FAIL // timed out
93} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020094
Bram Moolenaarafd78262019-05-10 23:10:31 +020095typedef struct {
96 request_progress_T tr_progress;
97 time_t tr_start; // when request was sent, -1 for never
98} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020099
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000100# define TERMREQUEST_INIT {STATUS_GET, -1}
Bram Moolenaarafd78262019-05-10 23:10:31 +0200101
102// Request Terminal Version status:
103static termrequest_T crv_status = TERMREQUEST_INIT;
104
105// Request Cursor position report:
106static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200107
Bram Moolenaara45551a2020-06-09 15:57:37 +0200108// Request xterm compatibility check:
109static termrequest_T xcc_status = TERMREQUEST_INIT;
110
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000111#ifdef FEAT_TERMRESPONSE
112# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200113// Request foreground color report:
114static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200115static int fg_r = 0;
116static int fg_g = 0;
117static int fg_b = 0;
118static int bg_r = 255;
119static int bg_g = 255;
120static int bg_b = 255;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000121# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200122
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100123// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200124static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200125
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100126// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200127static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200128
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100129// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200130static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100131
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100132// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200133static termrequest_T winpos_status = TERMREQUEST_INIT;
134
135static termrequest_T *all_termrequests[] = {
136 &crv_status,
137 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200138 &xcc_status,
Bram Moolenaarafd78262019-05-10 23:10:31 +0200139# ifdef FEAT_TERMINAL
140 &rfg_status,
141# endif
142 &rbg_status,
143 &rbm_status,
144 &rcs_status,
145 &winpos_status,
146 NULL
147};
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100148
149// The t_8u code may default to a value but get reset when the term response is
150// received. To avoid redrawing too often, only redraw when t_8u is not reset
Bram Moolenaarb3932752022-10-01 21:22:17 +0100151// and it was supposed to be written. Unless t_8u was set explicitly.
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100152// FALSE -> don't output t_8u yet
dundargocc57b5bc2022-11-02 13:30:51 +0000153// MAYBE -> tried outputting t_8u while FALSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100154// OK -> can write t_8u
155int write_t_8u_state = FALSE;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
Bram Moolenaar4e6072b2022-11-29 16:09:18 +0000158#ifdef HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159/*
160 * Don't declare these variables if termcap.h contains them.
161 * Autoconf checks if these variables should be declared extern (not all
162 * systems have them).
163 * Some versions define ospeed to be speed_t, but that is incompatible with
164 * BSD, where ospeed is short and speed_t is long.
165 */
166# ifndef HAVE_OSPEED
167# ifdef OSPEED_EXTERN
168extern short ospeed;
169# else
170short ospeed;
171# endif
172# endif
173# ifndef HAVE_UP_BC_PC
174# ifdef UP_BC_PC_EXTERN
175extern char *UP, *BC, PC;
176# else
177char *UP, *BC, PC;
178# endif
179# endif
180
181# define TGETSTR(s, p) vim_tgetstr((s), (p))
182# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100183static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100184#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100186static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100188#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000189static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100190#endif
191
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200192#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100193// When the cursor shape was detected these values are used:
194// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200195static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200196
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100197// The blink flag from the style response may be inverted from the actual
198// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200199static int initial_cursor_shape_blink = FALSE;
200
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100201// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200202static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200203#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200204
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100205/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000206 * The builtin termcap entries.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100207 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000208 * The entries are also included when HAVE_TGETENT is defined, the system
209 * termcap may be incomplete and a few Vim-specific entries are added.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100210 *
Bram Moolenaar4654d632022-11-17 22:05:12 +0000211 * When HAVE_TGETENT is defined, the builtin entries can be accessed with
212 * "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
213 *
214 * Each termcap is a list of tcap_entry_T. See parse_builtin_tcap() for all
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100215 * details.
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100216 *
217 * Entries marked with "guessed" may be wrong.
218 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000219typedef struct
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
Bram Moolenaar4654d632022-11-17 22:05:12 +0000221 int bt_entry; // either a KS_xxx code (>= 0), or a K_xxx code.
222 char *bt_string; // value
223} tcap_entry_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000226 * Standard ANSI terminal, default for Unix.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000228static tcap_entry_T builtin_ansi[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000229 {(int)KS_CE, "\033[K"},
230 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000232 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000234 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000236 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000238 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000240 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000242 {(int)KS_CL, "\033[H\033[2J"},
243 {(int)KS_ME, "\033[0m"},
244 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100246 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {(int)KS_LE, "\b"},
248# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000249 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000251 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252# endif
253# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000254 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000256 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
Bram Moolenaar4654d632022-11-17 22:05:12 +0000259 {(int)KS_NAME, NULL} // end marker
260};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262/*
263 * VT320 is working as an ANSI terminal compatible DEC terminal.
264 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 * TODO:- rewrite ESC[ codes to CSI
266 * - keyboard languages (CSI ? 26 n)
267 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000268static tcap_entry_T builtin_vt320[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000269 {(int)KS_CE, "\033[K"},
270 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000272 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000274 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000276 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000278 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000280 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000282 {(int)KS_CL, "\033[H\033[2J"},
283 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100284 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000285 {(int)KS_ME, "\033[0m"},
286 {(int)KS_MR, "\033[7m"},
287 {(int)KS_MD, "\033[1m"}, // bold mode
288 {(int)KS_SE, "\033[22m"},// normal mode
289 {(int)KS_UE, "\033[24m"},// exit underscore mode
290 {(int)KS_US, "\033[4m"}, // underscore mode
291 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
292 {(int)KS_CZR, "\033[0m"}, // italic mode end
293 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
294 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
295 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
296 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 {(int)KS_MS, "y"},
298 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100299 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 {(int)KS_LE, "\b"},
301# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000302 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000304 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305# endif
306# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000307 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000309 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000311 {K_UP, "\033[A"},
312 {K_DOWN, "\033[B"},
313 {K_RIGHT, "\033[C"},
314 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200315 // Note: cursor key sequences for application cursor mode are omitted,
316 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000317 {K_F1, "\033[11~"},
318 {K_F2, "\033[12~"},
319 {K_F3, "\033[13~"},
320 {K_F4, "\033[14~"},
321 {K_F5, "\033[15~"},
322 {K_F6, "\033[17~"},
323 {K_F7, "\033[18~"},
324 {K_F8, "\033[19~"},
325 {K_F9, "\033[20~"},
326 {K_F10, "\033[21~"},
327 {K_F11, "\033[23~"},
328 {K_F12, "\033[24~"},
329 {K_F13, "\033[25~"},
330 {K_F14, "\033[26~"},
331 {K_F15, "\033[28~"}, // Help
332 {K_F16, "\033[29~"}, // Select
333 {K_F17, "\033[31~"},
334 {K_F18, "\033[32~"},
335 {K_F19, "\033[33~"},
336 {K_F20, "\033[34~"},
337 {K_INS, "\033[2~"},
338 {K_DEL, "\033[3~"},
339 {K_HOME, "\033[1~"},
340 {K_END, "\033[4~"},
341 {K_PAGEUP, "\033[5~"},
342 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200343 // These sequences starting with <Esc> O may interfere with what the user
344 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000345 {K_KPLUS, "\033Ok"}, // keypad plus
346 {K_KMINUS, "\033Om"}, // keypad minus
347 {K_KDIVIDE, "\033Oo"}, // keypad /
348 {K_KMULTIPLY, "\033Oj"}, // keypad *
349 {K_KENTER, "\033OM"}, // keypad Enter
350 {K_K0, "\033Op"}, // keypad 0
351 {K_K1, "\033Oq"}, // keypad 1
352 {K_K2, "\033Or"}, // keypad 2
353 {K_K3, "\033Os"}, // keypad 3
354 {K_K4, "\033Ot"}, // keypad 4
355 {K_K5, "\033Ou"}, // keypad 5
356 {K_K6, "\033Ov"}, // keypad 6
357 {K_K7, "\033Ow"}, // keypad 7
358 {K_K8, "\033Ox"}, // keypad 8
359 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100360 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaar4654d632022-11-17 22:05:12 +0000362 {(int)KS_NAME, NULL} // end marker
363};
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365/*
366 * Ordinary vt52
367 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000368static tcap_entry_T builtin_vt52[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000369 {(int)KS_CE, "\033K"},
370 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100371# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000372 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100373# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000374 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000377 {(int)KS_SR, "\033I"},
378 {(int)KS_AL, "\033L"},
379 {(int)KS_DL, "\033M"},
380 {K_UP, "\033A"},
381 {K_DOWN, "\033B"},
382 {K_LEFT, "\033D"},
383 {K_RIGHT, "\033C"},
384 {K_F1, "\033P"},
385 {K_F2, "\033Q"},
386 {K_F3, "\033R"},
387 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389
Bram Moolenaar4654d632022-11-17 22:05:12 +0000390 {(int)KS_NAME, NULL} // end marker
391};
392
393/*
394 * Builtin xterm with Vim-specific entries.
395 */
396static tcap_entry_T builtin_xterm[] = {
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000397 {(int)KS_CE, "\033[K"},
398 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000400 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000402 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000404 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000406 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000408 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409# endif
410# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000411 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000413 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000415 {(int)KS_CL, "\033[H\033[2J"},
416 {(int)KS_CD, "\033[J"},
417 {(int)KS_ME, "\033[m"},
418 {(int)KS_MR, "\033[7m"},
419 {(int)KS_MD, "\033[1m"},
420 {(int)KS_UE, "\033[m"},
421 {(int)KS_US, "\033[4m"},
422 {(int)KS_STE, "\033[29m"},
423 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 {(int)KS_MS, "y"},
425 {(int)KS_UT, "y"},
426 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000427 {(int)KS_VI, "\033[?25l"},
428 {(int)KS_VE, "\033[?25h"},
429 {(int)KS_VS, "\033[?12h"},
430 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200431# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000432 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200433# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000434 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200435# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000436 {(int)KS_CRC, "\033[?12$p"},
437 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000439 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000441 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000443 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000445 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000447 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000449 {(int)KS_KS, "\033[?1h\033="},
450 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000452 {(int)KS_TI, "\0337\033[?47h"},
453 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454# endif
Bram Moolenaaraf19ec02022-12-03 00:00:38 +0000455 // These are now under control of the 'keyprotocol' option, see
456 // "builtin_mok2".
457 // {(int)KS_CTI, "\033[>4;2m"},
458 // {(int)KS_CRK, "\033[?4m"},
459 // {(int)KS_CTE, "\033[>4;m"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000460 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000462 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000464 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200465 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000467 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
468 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
469 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000471 {(int)KS_CWS, "\033[8;%d;%dt"},
472 {(int)KS_CWP, "\033[3;%d;%dt"},
473 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000475 {(int)KS_CRV, "\033[>c"},
476 {(int)KS_RFG, "\033]10;?\007"},
477 {(int)KS_RBG, "\033]11;?\007"},
478 {(int)KS_U7, "\033[6n"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000479 {(int)KS_CAU, "\033[58;5;%dm"},
480 {(int)KS_CBE, "\033[?2004h"},
481 {(int)KS_CBD, "\033[?2004l"},
482 {(int)KS_CST, "\033[22;2t"},
483 {(int)KS_CRT, "\033[23;2t"},
484 {(int)KS_SSI, "\033[22;1t"},
485 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100486# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000487 {(int)KS_FD, "\033[?1004l"},
488 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100489# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000490
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000491 {K_UP, "\033O*A"},
492 {K_DOWN, "\033O*B"},
493 {K_RIGHT, "\033O*C"},
494 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100495 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100496 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
497 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
498 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
499 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100500 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000501 {K_XF1, "\033O*P"},
502 {K_XF2, "\033O*Q"},
503 {K_XF3, "\033O*R"},
504 {K_XF4, "\033O*S"},
505 {K_F1, "\033[11;*~"},
506 {K_F2, "\033[12;*~"},
507 {K_F3, "\033[13;*~"},
508 {K_F4, "\033[14;*~"},
509 {K_F5, "\033[15;*~"},
510 {K_F6, "\033[17;*~"},
511 {K_F7, "\033[18;*~"},
512 {K_F8, "\033[19;*~"},
513 {K_F9, "\033[20;*~"},
514 {K_F10, "\033[21;*~"},
515 {K_F11, "\033[23;*~"},
516 {K_F12, "\033[24;*~"},
517 {K_S_TAB, "\033[Z"},
518 {K_HELP, "\033[28;*~"},
519 {K_UNDO, "\033[26;*~"},
520 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100521 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000522 // {K_S_HOME, "\033O2H"},
523 // {K_C_HOME, "\033O5H"},
524 {K_KHOME, "\033[1;*~"},
525 {K_XHOME, "\033O*H"}, // other Home
526 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100527 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000528 // {K_S_END, "\033O2F"},
529 // {K_C_END, "\033O5F"},
530 {K_KEND, "\033[4;*~"},
531 {K_XEND, "\033O*F"}, // other End
532 {K_ZEND, "\033[8;*~"},
533 {K_PAGEUP, "\033[5;*~"},
534 {K_PAGEDOWN, "\033[6;*~"},
535 {K_KPLUS, "\033O*k"}, // keypad plus
536 {K_KMINUS, "\033O*m"}, // keypad minus
537 {K_KDIVIDE, "\033O*o"}, // keypad /
538 {K_KMULTIPLY, "\033O*j"}, // keypad *
539 {K_KENTER, "\033O*M"}, // keypad Enter
540 {K_KPOINT, "\033O*n"}, // keypad .
541 {K_K0, "\033O*p"}, // keypad 0
542 {K_K1, "\033O*q"}, // keypad 1
543 {K_K2, "\033O*r"}, // keypad 2
544 {K_K3, "\033O*s"}, // keypad 3
545 {K_K4, "\033O*t"}, // keypad 4
546 {K_K5, "\033O*u"}, // keypad 5
547 {K_K6, "\033O*v"}, // keypad 6
548 {K_K7, "\033O*w"}, // keypad 7
549 {K_K8, "\033O*x"}, // keypad 8
550 {K_K9, "\033O*y"}, // keypad 9
551 {K_KDEL, "\033[3;*~"}, // keypad Del
552 {K_PS, "\033[200~"}, // paste start
553 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
555 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000556 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
557 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100558 // F14 and F15 are missing, because they send the same codes as the undo
559 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000560 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
561 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
562 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
563 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
564 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000565
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000566 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
567 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
568 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
569 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
570 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
571 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
572 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
573 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
574 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
575 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000576
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000577 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
578 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
579 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
580 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
581 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
582 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
583 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
Bram Moolenaar4654d632022-11-17 22:05:12 +0000585 {(int)KS_NAME, NULL} // end marker
586};
587
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000589 * Additions for using modifyOtherKeys level 2. Same as what is used for
590 * xterm.
591 */
592static tcap_entry_T builtin_mok2[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000593 // t_TI enables modifyOtherKeys level 2
594 {(int)KS_CTI, "\033[>4;2m"},
595
Bram Moolenaarc255b782022-11-26 19:16:48 +0000596 // XTQMODKEYS was added in xterm version 377: "CSI ? 4 m" which should
597 // return "{lead} > 4 ; Pv m". Before version 377 we expect it to have no
598 // effect.
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000599 {(int)KS_CRK, "\033[?4m"},
600
601 // t_TE disables modifyOtherKeys
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000602 {(int)KS_CTE, "\033[>4;m"},
603
604 {(int)KS_NAME, NULL} // end marker
605};
606
607/*
608 * Additions for using the Kitty keyboard protocol.
609 */
610static tcap_entry_T builtin_kitty[] = {
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000611 // t_TI enables the kitty keyboard protocol.
612 {(int)KS_CTI, "\033[=1;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000613
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000614 // t_RK requests the kitty keyboard protocol state
615 {(int)KS_CRK, "\033[?u"},
616
617 // t_TE also disables modifyOtherKeys, because t_TI from xterm may already
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000618 // have been used.
Bram Moolenaara87749e2022-11-30 10:23:17 +0000619 {(int)KS_CTE, "\033[>4;m\033[=0;1u"},
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000620
621 {(int)KS_NAME, NULL} // end marker
622};
623
Bram Moolenaar96dd34e2022-12-30 11:16:00 +0000624#ifdef FEAT_TERMGUICOLORS
625/*
626 * Additions for using the RGB colors
627 */
628static tcap_entry_T builtin_rgb[] = {
629 // These are printf strings, not terminal codes.
630 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
631 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
632 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
633
634 {(int)KS_NAME, NULL} // end marker
635};
636#endif
637
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 * iris-ansi for Silicon Graphics machines.
640 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000641static tcap_entry_T builtin_iris_ansi[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 {(int)KS_CE, "\033[K"},
643 {(int)KS_CD, "\033[J"},
644 {(int)KS_AL, "\033[L"},
645# ifdef TERMINFO
646 {(int)KS_CAL, "\033[%p1%dL"},
647# else
648 {(int)KS_CAL, "\033[%dL"},
649# endif
650 {(int)KS_DL, "\033[M"},
651# ifdef TERMINFO
652 {(int)KS_CDL, "\033[%p1%dM"},
653# else
654 {(int)KS_CDL, "\033[%dM"},
655# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100656#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657# ifdef TERMINFO
658 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
659# else
660 {(int)KS_CS, "\033[%i%d;%dr"},
661# endif
662#endif
663 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100664 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
665 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 {(int)KS_TI, "\033[=6h"},
667 {(int)KS_TE, "\033[=6l"},
668 {(int)KS_SE, "\033[21;27m"},
669 {(int)KS_SO, "\033[1;7m"},
670 {(int)KS_ME, "\033[m"},
671 {(int)KS_MR, "\033[7m"},
672 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100673 {(int)KS_CCO, "8"}, // allow 8 colors
674 {(int)KS_CZH, "\033[3m"}, // italic mode on
675 {(int)KS_CZR, "\033[23m"}, // italic mode off
676 {(int)KS_US, "\033[4m"}, // underline on
677 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100679 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
680 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
681 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
682 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100684 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
685 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
686 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
687 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100689 {(int)KS_MS, "y"}, // guessed
690 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {(int)KS_LE, "\b"},
692# ifdef TERMINFO
693 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
694# else
695 {(int)KS_CM, "\033[%i%d;%dH"},
696# endif
697 {(int)KS_SR, "\033M"},
698# ifdef TERMINFO
699 {(int)KS_CRI, "\033[%p1%dC"},
700# else
701 {(int)KS_CRI, "\033[%dC"},
702# endif
703 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100704 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100706 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707# ifdef TERMINFO
708 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
709 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
710# else
711 {(int)KS_CWS, "\033[203;%d;%d/y"},
712 {(int)KS_CWP, "\033[205;%d;%d/y"},
713# endif
714 {K_UP, "\033[A"},
715 {K_DOWN, "\033[B"},
716 {K_LEFT, "\033[D"},
717 {K_RIGHT, "\033[C"},
718 {K_S_UP, "\033[161q"},
719 {K_S_DOWN, "\033[164q"},
720 {K_S_LEFT, "\033[158q"},
721 {K_S_RIGHT, "\033[167q"},
722 {K_F1, "\033[001q"},
723 {K_F2, "\033[002q"},
724 {K_F3, "\033[003q"},
725 {K_F4, "\033[004q"},
726 {K_F5, "\033[005q"},
727 {K_F6, "\033[006q"},
728 {K_F7, "\033[007q"},
729 {K_F8, "\033[008q"},
730 {K_F9, "\033[009q"},
731 {K_F10, "\033[010q"},
732 {K_F11, "\033[011q"},
733 {K_F12, "\033[012q"},
734 {K_S_F1, "\033[013q"},
735 {K_S_F2, "\033[014q"},
736 {K_S_F3, "\033[015q"},
737 {K_S_F4, "\033[016q"},
738 {K_S_F5, "\033[017q"},
739 {K_S_F6, "\033[018q"},
740 {K_S_F7, "\033[019q"},
741 {K_S_F8, "\033[020q"},
742 {K_S_F9, "\033[021q"},
743 {K_S_F10, "\033[022q"},
744 {K_S_F11, "\033[023q"},
745 {K_S_F12, "\033[024q"},
746 {K_INS, "\033[139q"},
747 {K_HOME, "\033[H"},
748 {K_END, "\033[146q"},
749 {K_PAGEUP, "\033[150q"},
750 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
Bram Moolenaar4654d632022-11-17 22:05:12 +0000752 {(int)KS_NAME, NULL} // end marker
753};
754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755/*
Bram Moolenaar4654d632022-11-17 22:05:12 +0000756 * These codes are valid when nansi.sys or equivalent has been installed.
757 * Function keys on a PC are preceded with a NUL. These are converted into
758 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
759 * CTRL-arrow is used instead of SHIFT-arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 */
Bram Moolenaar4654d632022-11-17 22:05:12 +0000761static tcap_entry_T builtin_pcansi[] = {
762 {(int)KS_DL, "\033[M"},
763 {(int)KS_AL, "\033[L"},
764 {(int)KS_CE, "\033[K"},
765 {(int)KS_CL, "\033[2J"},
766 {(int)KS_ME, "\033[0m"},
767 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
768 {(int)KS_MD, "\033[1m"}, // bold: white text
769 {(int)KS_SE, "\033[0m"}, // standout end
770 {(int)KS_SO, "\033[31m"}, // standout: white on blue
771 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
772 {(int)KS_CZR, "\033[0m"}, // italic mode end
773 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
774 {(int)KS_UE, "\033[0m"}, // underscore mode end
775 {(int)KS_CCO, "8"}, // allow 8 colors
776# ifdef TERMINFO
777 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
778 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
779# else
780 {(int)KS_CAB, "\033[4%dm"}, // set background color
781 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
782# endif
783 {(int)KS_OP, "\033[0m"}, // reset colors
784 {(int)KS_MS, "y"},
785 {(int)KS_UT, "y"}, // guessed
786 {(int)KS_LE, "\b"},
787# ifdef TERMINFO
788 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
789# else
790 {(int)KS_CM, "\033[%i%d;%dH"},
791# endif
792# ifdef TERMINFO
793 {(int)KS_CRI, "\033[%p1%dC"},
794# else
795 {(int)KS_CRI, "\033[%dC"},
796# endif
797 {K_UP, "\316H"},
798 {K_DOWN, "\316P"},
799 {K_LEFT, "\316K"},
800 {K_RIGHT, "\316M"},
801 {K_S_LEFT, "\316s"},
802 {K_S_RIGHT, "\316t"},
803 {K_F1, "\316;"},
804 {K_F2, "\316<"},
805 {K_F3, "\316="},
806 {K_F4, "\316>"},
807 {K_F5, "\316?"},
808 {K_F6, "\316@"},
809 {K_F7, "\316A"},
810 {K_F8, "\316B"},
811 {K_F9, "\316C"},
812 {K_F10, "\316D"},
813 {K_F11, "\316\205"}, // guessed
814 {K_F12, "\316\206"}, // guessed
815 {K_S_F1, "\316T"},
816 {K_S_F2, "\316U"},
817 {K_S_F3, "\316V"},
818 {K_S_F4, "\316W"},
819 {K_S_F5, "\316X"},
820 {K_S_F6, "\316Y"},
821 {K_S_F7, "\316Z"},
822 {K_S_F8, "\316["},
823 {K_S_F9, "\316\\"},
824 {K_S_F10, "\316]"},
825 {K_S_F11, "\316\207"}, // guessed
826 {K_S_F12, "\316\210"}, // guessed
827 {K_INS, "\316R"},
828 {K_DEL, "\316S"},
829 {K_HOME, "\316G"},
830 {K_END, "\316O"},
831 {K_PAGEDOWN, "\316Q"},
832 {K_PAGEUP, "\316I"},
833
834 {(int)KS_NAME, NULL} // end marker
835};
836
837/*
Christopher Plewright20b795e2022-12-20 20:01:58 +0000838 * These codes are valid for the Win32 Console. The entries that start with
Bram Moolenaar4654d632022-11-17 22:05:12 +0000839 * ESC | are translated into console calls in os_win32.c. The function keys
840 * are also translated in os_win32.c.
841 */
842static tcap_entry_T builtin_win32[] = {
843 {(int)KS_CE, "\033|K"}, // clear to end of line
844 {(int)KS_AL, "\033|L"}, // add new blank line
845# ifdef TERMINFO
846 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
847# else
848 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
849# endif
850 {(int)KS_DL, "\033|M"}, // delete line
851# ifdef TERMINFO
852 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
853 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
854# else
855 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
856 {(int)KS_CSV, "\033|%d;%dV"},
857# endif
858 {(int)KS_CL, "\033|J"}, // clear screen
859 {(int)KS_CD, "\033|j"}, // clear to end of display
860 {(int)KS_VI, "\033|v"}, // cursor invisible
861 {(int)KS_VE, "\033|V"}, // cursor visible
862
863 {(int)KS_ME, "\033|0m"}, // normal
864 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
865 {(int)KS_MD, "\033|15m"}, // bold: white on black
866#if 1
867 {(int)KS_SO, "\033|31m"}, // standout: white on blue
868 {(int)KS_SE, "\033|0m"}, // standout end
869#else
870 {(int)KS_SO, "\033|F"}, // standout: high intensity
871 {(int)KS_SE, "\033|f"}, // standout end
872#endif
873 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
874 {(int)KS_CZR, "\033|0m"}, // italic end
875 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
876 {(int)KS_UE, "\033|0m"}, // underscore end
877 {(int)KS_CCO, "16"}, // allow 16 colors
878# ifdef TERMINFO
879 {(int)KS_CAB, "\033|%p1%db"}, // set background color
880 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
881# else
882 {(int)KS_CAB, "\033|%db"}, // set background color
883 {(int)KS_CAF, "\033|%df"}, // set foreground color
884# endif
885
886 {(int)KS_MS, "y"}, // save to move cur in reverse mode
887 {(int)KS_UT, "y"},
888 {(int)KS_XN, "y"},
889 {(int)KS_LE, "\b"},
890# ifdef TERMINFO
891 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
892# else
893 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
894# endif
895 {(int)KS_VB, "\033|B"}, // visual bell
896 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
897 {(int)KS_TE, "\033|E"}, // out of termcap mode
898# ifdef TERMINFO
899 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
900# else
901 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
902# endif
Bram Moolenaar4654d632022-11-17 22:05:12 +0000903
904 {K_UP, "\316H"},
905 {K_DOWN, "\316P"},
906 {K_LEFT, "\316K"},
907 {K_RIGHT, "\316M"},
908 {K_S_UP, "\316\304"},
909 {K_S_DOWN, "\316\317"},
910 {K_S_LEFT, "\316\311"},
911 {K_C_LEFT, "\316s"},
912 {K_S_RIGHT, "\316\313"},
913 {K_C_RIGHT, "\316t"},
914 {K_S_TAB, "\316\017"},
915 {K_F1, "\316;"},
916 {K_F2, "\316<"},
917 {K_F3, "\316="},
918 {K_F4, "\316>"},
919 {K_F5, "\316?"},
920 {K_F6, "\316@"},
921 {K_F7, "\316A"},
922 {K_F8, "\316B"},
923 {K_F9, "\316C"},
924 {K_F10, "\316D"},
925 {K_F11, "\316\205"},
926 {K_F12, "\316\206"},
927 {K_S_F1, "\316T"},
928 {K_S_F2, "\316U"},
929 {K_S_F3, "\316V"},
930 {K_S_F4, "\316W"},
931 {K_S_F5, "\316X"},
932 {K_S_F6, "\316Y"},
933 {K_S_F7, "\316Z"},
934 {K_S_F8, "\316["},
935 {K_S_F9, "\316\\"},
936 {K_S_F10, "\316]"},
937 {K_S_F11, "\316\207"},
938 {K_S_F12, "\316\210"},
939 {K_INS, "\316R"},
940 {K_DEL, "\316S"},
941 {K_HOME, "\316G"},
942 {K_S_HOME, "\316\302"},
943 {K_C_HOME, "\316w"},
944 {K_END, "\316O"},
945 {K_S_END, "\316\315"},
946 {K_C_END, "\316u"},
947 {K_PAGEDOWN, "\316Q"},
948 {K_PAGEUP, "\316I"},
949 {K_KPLUS, "\316N"},
950 {K_KMINUS, "\316J"},
951 {K_KMULTIPLY, "\316\067"},
952 {K_K0, "\316\332"},
953 {K_K1, "\316\336"},
954 {K_K2, "\316\342"},
955 {K_K3, "\316\346"},
956 {K_K4, "\316\352"},
957 {K_K5, "\316\356"},
958 {K_K6, "\316\362"},
959 {K_K7, "\316\366"},
960 {K_K8, "\316\372"},
961 {K_K9, "\316\376"},
962 {K_BS, "\316x"},
963 {K_S_BS, "\316y"},
964
965 {(int)KS_NAME, NULL} // end marker
966};
967
968#if defined(FEAT_GUI)
969/*
970 * GUI uses made-up codes, only used inside Vim.
971 */
972static tcap_entry_T builtin_gui[] = {
973 {(int)KS_CE, "\033|$"},
974 {(int)KS_AL, "\033|i"},
975# ifdef TERMINFO
976 {(int)KS_CAL, "\033|%p1%dI"},
977# else
978 {(int)KS_CAL, "\033|%dI"},
979# endif
980 {(int)KS_DL, "\033|d"},
981# ifdef TERMINFO
982 {(int)KS_CDL, "\033|%p1%dD"},
983 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
984 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
985# else
986 {(int)KS_CDL, "\033|%dD"},
987 {(int)KS_CS, "\033|%d;%dR"},
988 {(int)KS_CSV, "\033|%d;%dV"},
989# endif
990 {(int)KS_CL, "\033|C"},
991 // attributes switched on with 'h', off with * 'H'
992 {(int)KS_ME, "\033|31H"}, // HL_ALL
993 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
994 {(int)KS_MD, "\033|2h"}, // HL_BOLD
995 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
996 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
997 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
998 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
999 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
1000 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
1001 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
1002 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
1003 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
1004 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
1005 {(int)KS_VB, "\033|f"},
1006 {(int)KS_MS, "y"},
1007 {(int)KS_UT, "y"},
1008 {(int)KS_XN, "y"},
1009 {(int)KS_LE, "\b"}, // cursor-left = BS
1010 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
1011# ifdef TERMINFO
1012 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
1013# else
1014 {(int)KS_CM, "\033|%d;%dM"},
1015# endif
1016 // there are no key sequences here, the GUI sequences are recognized
1017 // in check_termcode()
1018
1019 {(int)KS_NAME, NULL} // end marker
1020};
1021#endif
1022
1023/*
1024 * Amiga console window, default for Amiga.
1025 */
1026static tcap_entry_T builtin_amiga[] = {
1027 {(int)KS_CE, "\033[K"},
1028 {(int)KS_CD, "\033[J"},
1029 {(int)KS_AL, "\033[L"},
1030# ifdef TERMINFO
1031 {(int)KS_CAL, "\033[%p1%dL"},
1032# else
1033 {(int)KS_CAL, "\033[%dL"},
1034# endif
1035 {(int)KS_DL, "\033[M"},
1036# ifdef TERMINFO
1037 {(int)KS_CDL, "\033[%p1%dM"},
1038# else
1039 {(int)KS_CDL, "\033[%dM"},
1040# endif
1041 {(int)KS_CL, "\014"},
1042 {(int)KS_VI, "\033[0 p"},
1043 {(int)KS_VE, "\033[1 p"},
1044 {(int)KS_ME, "\033[0m"},
1045 {(int)KS_MR, "\033[7m"},
1046 {(int)KS_MD, "\033[1m"},
1047 {(int)KS_SE, "\033[0m"},
1048 {(int)KS_SO, "\033[33m"},
1049 {(int)KS_US, "\033[4m"},
1050 {(int)KS_UE, "\033[0m"},
1051 {(int)KS_CZH, "\033[3m"},
1052 {(int)KS_CZR, "\033[0m"},
1053#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
1054 {(int)KS_CCO, "8"}, // allow 8 colors
1055# ifdef TERMINFO
1056 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
1057 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
1058# else
1059 {(int)KS_CAB, "\033[4%dm"}, // set background color
1060 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
1061# endif
1062 {(int)KS_OP, "\033[m"}, // reset colors
1063#endif
1064 {(int)KS_MS, "y"},
1065 {(int)KS_UT, "y"}, // guessed
1066 {(int)KS_LE, "\b"},
1067# ifdef TERMINFO
1068 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1069# else
1070 {(int)KS_CM, "\033[%i%d;%dH"},
1071# endif
1072#if defined(__MORPHOS__)
1073 {(int)KS_SR, "\033M"},
1074#endif
1075# ifdef TERMINFO
1076 {(int)KS_CRI, "\033[%p1%dC"},
1077# else
1078 {(int)KS_CRI, "\033[%dC"},
1079# endif
1080 {K_UP, "\233A"},
1081 {K_DOWN, "\233B"},
1082 {K_LEFT, "\233D"},
1083 {K_RIGHT, "\233C"},
1084 {K_S_UP, "\233T"},
1085 {K_S_DOWN, "\233S"},
1086 {K_S_LEFT, "\233 A"},
1087 {K_S_RIGHT, "\233 @"},
1088 {K_S_TAB, "\233Z"},
1089 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
1090 {K_F2, "\233\061~"},
1091 {K_F3, "\233\062~"},
1092 {K_F4, "\233\063~"},
1093 {K_F5, "\233\064~"},
1094 {K_F6, "\233\065~"},
1095 {K_F7, "\233\066~"},
1096 {K_F8, "\233\067~"},
1097 {K_F9, "\233\070~"},
1098 {K_F10, "\233\071~"},
1099 {K_S_F1, "\233\061\060~"},
1100 {K_S_F2, "\233\061\061~"},
1101 {K_S_F3, "\233\061\062~"},
1102 {K_S_F4, "\233\061\063~"},
1103 {K_S_F5, "\233\061\064~"},
1104 {K_S_F6, "\233\061\065~"},
1105 {K_S_F7, "\233\061\066~"},
1106 {K_S_F8, "\233\061\067~"},
1107 {K_S_F9, "\233\061\070~"},
1108 {K_S_F10, "\233\061\071~"},
1109 {K_HELP, "\233?~"},
1110 {K_INS, "\233\064\060~"}, // 101 key keyboard
1111 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
1112 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
1113 {K_HOME, "\233\064\064~"}, // 101 key keyboard
1114 {K_END, "\233\064\065~"}, // 101 key keyboard
1115
1116 {BT_EXTRA_KEYS, ""},
1117 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
1118 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
1119 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
1120
1121 {(int)KS_NAME, NULL} // end marker
1122};
1123
1124/*
1125 * The most minimal terminal: only clear screen and cursor positioning.
1126 */
1127static tcap_entry_T builtin_dumb[] = {
1128 {(int)KS_CL, "\014"},
1129#ifdef TERMINFO
1130 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1131#else
1132 {(int)KS_CM, "\033[%i%d;%dH"},
1133#endif
1134
1135 {(int)KS_NAME, NULL} // end marker
1136};
1137
1138/*
1139 * Terminal used for debugging.
1140 */
1141static tcap_entry_T builtin_debug[] = {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 {(int)KS_CE, "[CE]"},
1143 {(int)KS_CD, "[CD]"},
1144 {(int)KS_AL, "[AL]"},
1145# ifdef TERMINFO
1146 {(int)KS_CAL, "[CAL%p1%d]"},
1147# else
1148 {(int)KS_CAL, "[CAL%d]"},
1149# endif
1150 {(int)KS_DL, "[DL]"},
1151# ifdef TERMINFO
1152 {(int)KS_CDL, "[CDL%p1%d]"},
1153# else
1154 {(int)KS_CDL, "[CDL%d]"},
1155# endif
1156# ifdef TERMINFO
1157 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1158# else
1159 {(int)KS_CS, "[%dCS%d]"},
1160# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001161# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001163# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165# endif
1166# ifdef TERMINFO
1167 {(int)KS_CAB, "[CAB%p1%d]"},
1168 {(int)KS_CAF, "[CAF%p1%d]"},
1169 {(int)KS_CSB, "[CSB%p1%d]"},
1170 {(int)KS_CSF, "[CSF%p1%d]"},
1171# else
1172 {(int)KS_CAB, "[CAB%d]"},
1173 {(int)KS_CAF, "[CAF%d]"},
1174 {(int)KS_CSB, "[CSB%d]"},
1175 {(int)KS_CSF, "[CSF%d]"},
1176# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001177 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {(int)KS_OP, "[OP]"},
1179 {(int)KS_LE, "[LE]"},
1180 {(int)KS_CL, "[CL]"},
1181 {(int)KS_VI, "[VI]"},
1182 {(int)KS_VE, "[VE]"},
1183 {(int)KS_VS, "[VS]"},
1184 {(int)KS_ME, "[ME]"},
1185 {(int)KS_MR, "[MR]"},
1186 {(int)KS_MB, "[MB]"},
1187 {(int)KS_MD, "[MD]"},
1188 {(int)KS_SE, "[SE]"},
1189 {(int)KS_SO, "[SO]"},
1190 {(int)KS_UE, "[UE]"},
1191 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001192 {(int)KS_UCE, "[UCE]"},
1193 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001194 {(int)KS_USS, "[USS]"},
1195 {(int)KS_DS, "[DS]"},
1196 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001197 {(int)KS_STE, "[STE]"},
1198 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {(int)KS_MS, "[MS]"},
1200 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001201 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202# ifdef TERMINFO
1203 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1204# else
1205 {(int)KS_CM, "[%dCM%d]"},
1206# endif
1207 {(int)KS_SR, "[SR]"},
1208# ifdef TERMINFO
1209 {(int)KS_CRI, "[CRI%p1%d]"},
1210# else
1211 {(int)KS_CRI, "[CRI%d]"},
1212# endif
1213 {(int)KS_VB, "[VB]"},
1214 {(int)KS_KS, "[KS]"},
1215 {(int)KS_KE, "[KE]"},
1216 {(int)KS_TI, "[TI]"},
1217 {(int)KS_TE, "[TE]"},
1218 {(int)KS_CIS, "[CIS]"},
1219 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001220 {(int)KS_CSC, "[CSC]"},
1221 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {(int)KS_TS, "[TS]"},
1223 {(int)KS_FS, "[FS]"},
1224# ifdef TERMINFO
1225 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1226 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1227# else
1228 {(int)KS_CWS, "[%dCWS%d]"},
1229 {(int)KS_CWP, "[%dCWP%d]"},
1230# endif
1231 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001232 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001233 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001234 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {K_UP, "[KU]"},
1236 {K_DOWN, "[KD]"},
1237 {K_LEFT, "[KL]"},
1238 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001239 {K_XUP, "[xKU]"},
1240 {K_XDOWN, "[xKD]"},
1241 {K_XLEFT, "[xKL]"},
1242 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {K_S_UP, "[S-KU]"},
1244 {K_S_DOWN, "[S-KD]"},
1245 {K_S_LEFT, "[S-KL]"},
1246 {K_C_LEFT, "[C-KL]"},
1247 {K_S_RIGHT, "[S-KR]"},
1248 {K_C_RIGHT, "[C-KR]"},
1249 {K_F1, "[F1]"},
1250 {K_XF1, "[xF1]"},
1251 {K_F2, "[F2]"},
1252 {K_XF2, "[xF2]"},
1253 {K_F3, "[F3]"},
1254 {K_XF3, "[xF3]"},
1255 {K_F4, "[F4]"},
1256 {K_XF4, "[xF4]"},
1257 {K_F5, "[F5]"},
1258 {K_F6, "[F6]"},
1259 {K_F7, "[F7]"},
1260 {K_F8, "[F8]"},
1261 {K_F9, "[F9]"},
1262 {K_F10, "[F10]"},
1263 {K_F11, "[F11]"},
1264 {K_F12, "[F12]"},
1265 {K_S_F1, "[S-F1]"},
1266 {K_S_XF1, "[S-xF1]"},
1267 {K_S_F2, "[S-F2]"},
1268 {K_S_XF2, "[S-xF2]"},
1269 {K_S_F3, "[S-F3]"},
1270 {K_S_XF3, "[S-xF3]"},
1271 {K_S_F4, "[S-F4]"},
1272 {K_S_XF4, "[S-xF4]"},
1273 {K_S_F5, "[S-F5]"},
1274 {K_S_F6, "[S-F6]"},
1275 {K_S_F7, "[S-F7]"},
1276 {K_S_F8, "[S-F8]"},
1277 {K_S_F9, "[S-F9]"},
1278 {K_S_F10, "[S-F10]"},
1279 {K_S_F11, "[S-F11]"},
1280 {K_S_F12, "[S-F12]"},
1281 {K_HELP, "[HELP]"},
1282 {K_UNDO, "[UNDO]"},
1283 {K_BS, "[BS]"},
1284 {K_INS, "[INS]"},
1285 {K_KINS, "[KINS]"},
1286 {K_DEL, "[DEL]"},
1287 {K_KDEL, "[KDEL]"},
1288 {K_HOME, "[HOME]"},
1289 {K_S_HOME, "[C-HOME]"},
1290 {K_C_HOME, "[C-HOME]"},
1291 {K_KHOME, "[KHOME]"},
1292 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001293 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 {K_END, "[END]"},
1295 {K_S_END, "[C-END]"},
1296 {K_C_END, "[C-END]"},
1297 {K_KEND, "[KEND]"},
1298 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001299 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 {K_PAGEUP, "[PAGEUP]"},
1301 {K_PAGEDOWN, "[PAGEDOWN]"},
1302 {K_KPAGEUP, "[KPAGEUP]"},
1303 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1304 {K_MOUSE, "[MOUSE]"},
1305 {K_KPLUS, "[KPLUS]"},
1306 {K_KMINUS, "[KMINUS]"},
1307 {K_KDIVIDE, "[KDIVIDE]"},
1308 {K_KMULTIPLY, "[KMULTIPLY]"},
1309 {K_KENTER, "[KENTER]"},
1310 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001311 {K_PS, "[PASTE-START]"},
1312 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 {K_K0, "[K0]"},
1314 {K_K1, "[K1]"},
1315 {K_K2, "[K2]"},
1316 {K_K3, "[K3]"},
1317 {K_K4, "[K4]"},
1318 {K_K5, "[K5]"},
1319 {K_K6, "[K6]"},
1320 {K_K7, "[K7]"},
1321 {K_K8, "[K8]"},
1322 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
Bram Moolenaar4654d632022-11-17 22:05:12 +00001324 {(int)KS_NAME, NULL} // end marker
1325};
1326
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327/*
Bram Moolenaar4654d632022-11-17 22:05:12 +00001328 * List of builtin terminals.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00001330typedef struct {
1331 char *bitc_name; // name, such as "xterm"
1332 tcap_entry_T *bitc_table; // table with entries for bitc_name
1333} builtin_tcap_T;
1334
1335builtin_tcap_T builtin_terminals[] = {
1336 // Unix and Generic
1337 {"ansi", builtin_ansi},
1338 {"vt320", builtin_vt320},
1339 {"vt52", builtin_vt52},
1340 {"xterm", builtin_xterm},
1341 {"iris-ansi", builtin_iris_ansi},
1342
1343 // MS-Windows
1344 {"pcansi", builtin_pcansi},
1345 {"win32", builtin_win32},
1346
1347 // Other systems
1348#if defined(FEAT_GUI)
1349 {"gui", builtin_gui},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001351 {"amiga", builtin_amiga},
1352 {"dumb", builtin_dumb},
1353 {"debug", builtin_debug},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
Bram Moolenaar4654d632022-11-17 22:05:12 +00001355 {NULL, NULL}, // end marker
1356};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001358#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001359 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001360termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001361{
Bram Moolenaarab302212016-04-26 20:59:29 +02001362 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001363}
1364
1365 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001366termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001367{
1368 guicolor_T t;
1369
1370 if (*name == NUL)
1371 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001372 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001373
1374 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001375 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001376 return t;
1377}
1378
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001379 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001380termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001381{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001382 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001383}
1384#endif
1385
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386/*
1387 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389#ifdef AMIGA
1390# define DEFAULT_TERM (char_u *)"amiga"
1391#endif
1392
1393#ifdef MSWIN
1394# define DEFAULT_TERM (char_u *)"win32"
1395#endif
1396
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001397#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398# define DEFAULT_TERM (char_u *)"ansi"
1399#endif
1400
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401#ifdef VMS
1402# define DEFAULT_TERM (char_u *)"vt320"
1403#endif
1404
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001405#ifdef __HAIKU__
1406# undef DEFAULT_TERM
1407# define DEFAULT_TERM (char_u *)"xterm"
1408#endif
1409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410#ifndef DEFAULT_TERM
1411# define DEFAULT_TERM (char_u *)"dumb"
1412#endif
1413
1414/*
1415 * Term_strings contains currently used terminal output strings.
1416 * It is initialized with the default values by parse_builtin_tcap().
1417 * The values can be changed by setting the option with the same name.
1418 */
1419char_u *(term_strings[(int)KS_LAST + 1]);
1420
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001421static int need_gather = FALSE; // need to fill termleader[]
1422static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001424static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001425#endif
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001426
1427/*
1428 * Structure and table to store terminal features that can be detected by
1429 * querying the terminal. Either by inspecting the termresponse or a more
1430 * specific request. Besides this there are:
1431 * t_colors - number of colors supported
1432 */
1433typedef struct {
1434 char *tpr_name;
1435 int tpr_set_by_termresponse;
1436 int tpr_status;
1437} termprop_T;
1438
1439// Values for tpr_status.
1440#define TPR_UNKNOWN 'u'
1441#define TPR_YES 'y'
1442#define TPR_NO 'n'
1443#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1444#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1445#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1446
1447// can request the cursor style without messing up the display
1448#define TPR_CURSOR_STYLE 0
1449// can request the cursor blink mode without messing up the display
1450#define TPR_CURSOR_BLINK 1
1451// can set the underline color with t_8u without resetting other colors
1452#define TPR_UNDERLINE_RGB 2
1453// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1454#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001455// term response indicates kitty
1456#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001457// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001458#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001459
1460static termprop_T term_props[TPR_COUNT];
1461
1462/*
1463 * Initialize the term_props table.
1464 * When "all" is FALSE only set those that are detected from the version
1465 * response.
1466 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001467 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001468init_term_props(int all)
1469{
1470 int i;
1471
1472 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1473 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1474 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1475 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1476 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1477 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1478 term_props[TPR_MOUSE].tpr_name = "mouse";
1479 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001480 term_props[TPR_KITTY].tpr_name = "kitty";
1481 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001482
1483 for (i = 0; i < TPR_COUNT; ++i)
1484 if (all || term_props[i].tpr_set_by_termresponse)
1485 term_props[i].tpr_status = TPR_UNKNOWN;
1486}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001488#if defined(FEAT_EVAL) || defined(PROTO)
1489 void
1490f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1491{
1492# ifdef FEAT_TERMRESPONSE
1493 int i;
1494# endif
1495
Bram Moolenaar93a10962022-06-16 11:42:09 +01001496 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001497 return;
1498# ifdef FEAT_TERMRESPONSE
1499 for (i = 0; i < TPR_COUNT; ++i)
1500 {
1501 char_u value[2];
1502
1503 value[0] = term_props[i].tpr_status;
1504 value[1] = NUL;
1505 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1506 }
1507# endif
1508}
1509#endif
1510
Bram Moolenaar4654d632022-11-17 22:05:12 +00001511/*
1512 * Find the builtin termcap entries for "term".
1513 * This also recognizes similar names. E.g. "xterm-256color" finds the "xterm"
1514 * entry.
1515 * Returns NULL when "term" is not found.
1516 */
1517 static tcap_entry_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001518find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001520 for (int i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001522 char_u *name = (char_u *)builtin_terminals[i].bitc_name;
1523 if (name == NULL) // end marker
1524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525#ifdef UNIX
Bram Moolenaar4654d632022-11-17 22:05:12 +00001526 if (STRCMP(name, "iris-ansi") == 0 && vim_is_iris(term))
1527 return builtin_terminals[i].bitc_table;
1528 if (STRCMP(name, "xterm") == 0 && vim_is_xterm(term))
1529 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530#endif
1531#ifdef VMS
Bram Moolenaar4654d632022-11-17 22:05:12 +00001532 if (STRCMP(name, "vt320") == 0 && vim_is_vt300(term))
1533 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001535 if (STRCMP(term, name) == 0)
1536 return builtin_terminals[i].bitc_table;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
Bram Moolenaar4654d632022-11-17 22:05:12 +00001538 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539}
1540
1541/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001542 * Apply entries from a builtin termcap.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 */
1544 static void
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001545apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
Bram Moolenaar4654d632022-11-17 22:05:12 +00001547 int term_8bit = term_is_8bit(term);
1548
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001549 for (tcap_entry_T *p = entries;
1550 p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001552 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001554 // Only set the value if it wasn't set yet or "overwrite" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 if (term_strings[p->bt_entry] == NULL
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001556 || term_strings[p->bt_entry] == empty_option
1557 || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001559#ifdef FEAT_EVAL
1560 int opt_idx = -1;
1561#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001562 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1564 {
1565 char_u *s, *t;
1566
1567 s = vim_strsave((char_u *)p->bt_string);
1568 if (s != NULL)
1569 {
1570 for (t = s; *t; ++t)
1571 if (term_7to8bit(t))
1572 {
1573 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001574 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 }
1576 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001577#ifdef FEAT_EVAL
1578 opt_idx =
1579#endif
1580 set_term_option_alloced(
1581 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 }
1583 }
1584 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001587#ifdef FEAT_EVAL
1588 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1589#endif
1590 }
1591#ifdef FEAT_EVAL
1592 set_term_option_sctx_idx(NULL, opt_idx);
1593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595 }
1596 else
1597 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001598 char_u name[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1600 name[1] = KEY2TERMCAP1((int)p->bt_entry);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001601 if (find_termcode(name) == NULL || overwrite)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1603 }
1604 }
1605}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001608 * Parsing of the builtin termcap entries.
1609 * Caller should check if "term" is a valid builtin terminal name.
1610 * The terminal's name is not set, as this is already done in termcapinit().
1611 */
1612 static void
1613parse_builtin_tcap(char_u *term)
1614{
1615 tcap_entry_T *entries = find_builtin_term(term);
1616 if (entries != NULL)
1617 apply_builtin_tcap(term, entries, FALSE);
1618}
1619
1620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 * Set number of colors.
1622 * Store it as a number in t_colors.
1623 * Store it as a string in T_CCO (using nr_colors[]).
1624 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001625 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001626set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001628 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
1630 t_colors = nr;
1631 if (t_colors > 1)
1632 sprintf((char *)nr_colors, "%d", t_colors);
1633 else
1634 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001635 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001637
1638/*
1639 * Set the color count to "val" and redraw if it changed.
1640 */
1641 static void
1642may_adjust_color_count(int val)
1643{
1644 if (val != t_colors)
1645 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001646 // Nr of colors changed, initialize highlighting and redraw everything.
1647 // This causes a redraw, which usually clears the message. Try keeping
1648 // the message if it might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001649 set_keep_msg_from_hist();
1650 set_color_count(val);
1651 init_highlight(TRUE, FALSE);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001652#ifdef DEBUG_TERMRESPONSE
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001653 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001654 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001655
Bram Moolenaarb255b902018-04-24 21:40:10 +02001656 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001657 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001658#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001659 redraw_asap(UPD_CLEAR);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00001660#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001661 }
1662}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
1664#ifdef HAVE_TGETENT
1665static char *(key_names[]) =
1666{
Bram Moolenaar87202262020-05-24 17:23:45 +02001667# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001668 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001670# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 "#2", "#4", "%i", "*7",
1673 "k1", "k2", "k3", "k4", "k5", "k6",
1674 "k7", "k8", "k9", "k;", "F1", "F2",
1675 "%1", "&8", "kb", "kI", "kD", "kh",
1676 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1677 NULL
1678};
1679#endif
1680
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001681/*
1682 * Return TRUE if "term_strings[idx]" was not set.
1683 */
1684 static int
1685term_strings_not_set(enum SpecialKey idx)
1686{
1687 return TERM_STR(idx) == NULL || TERM_STR(idx) == empty_option;
1688}
1689
Bram Moolenaar9289df52018-05-10 14:40:57 +02001690#ifdef HAVE_TGETENT
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00001691/*
1692 * Get the termcap entries we need with tgetstr(), tgetflag() and tgetnum().
1693 * "invoke_tgetent()" must have been called before.
1694 * If "*height" or "*width" are not zero then use the "li" and "col" entries to
1695 * get their value.
1696 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001697 static void
1698get_term_entries(int *height, int *width)
1699{
1700 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001701 enum SpecialKey dest; // index in term_strings[]
1702 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001703 } string_names[] =
1704 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1705 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1706 {KS_CL, "cl"}, {KS_CD, "cd"},
1707 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1708 {KS_ME, "me"}, {KS_MR, "mr"},
1709 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1710 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1711 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001712 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001713 {KS_STE,"Te"}, {KS_STS,"Ts"},
1714 {KS_CM, "cm"}, {KS_SR, "sr"},
1715 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1716 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001717 {KS_CTI, "TI"}, {KS_CRK, "RK"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001718 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001719 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1720 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001721 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1722 {KS_VS, "vs"}, {KS_CVS, "VS"},
1723 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1724 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1725 {KS_TS, "ts"}, {KS_FS, "fs"},
1726 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1727 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1728 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001729 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001730 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1731 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001732 {KS_CST, "ST"}, {KS_CRT, "RT"},
1733 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001734 {(enum SpecialKey)0, NULL}
1735 };
1736 int i;
1737 char_u *p;
1738 static char_u tstrbuf[TBUFSZ];
1739 char_u *tp = tstrbuf;
1740
1741 /*
1742 * get output strings
1743 */
1744 for (i = 0; string_names[i].name != NULL; ++i)
1745 {
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001746 if (term_strings_not_set(string_names[i].dest))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001747 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001748 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001749#ifdef FEAT_EVAL
1750 set_term_option_sctx_idx(string_names[i].name, -1);
1751#endif
1752 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001753 }
1754
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001755 // tgetflag() returns 1 if the flag is present, 0 if not and
1756 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001757 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1758 T_MS = (char_u *)"y";
1759 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1760 T_XS = (char_u *)"y";
1761 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1762 T_XN = (char_u *)"y";
1763 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1764 T_DB = (char_u *)"y";
1765 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1766 T_DA = (char_u *)"y";
1767 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1768 T_UT = (char_u *)"y";
1769
1770 /*
1771 * get key codes
1772 */
1773 for (i = 0; key_names[i] != NULL; ++i)
1774 if (find_termcode((char_u *)key_names[i]) == NULL)
1775 {
1776 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001777 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001778 if (p != NULL
1779 && (*p != Ctrl_H
1780 || key_names[i][0] != 'k'
1781 || key_names[i][1] != 'l'))
1782 add_termcode((char_u *)key_names[i], p, FALSE);
1783 }
1784
1785 if (*height == 0)
1786 *height = tgetnum("li");
1787 if (*width == 0)
1788 *width = tgetnum("co");
1789
1790 /*
1791 * Get number of colors (if not done already).
1792 */
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001793 if (term_strings_not_set(KS_CCO))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001794 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001795 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001796#ifdef FEAT_EVAL
1797 set_term_option_sctx_idx("Co", -1);
1798#endif
1799 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001800
1801# ifndef hpux
1802 BC = (char *)TGETSTR("bc", &tp);
1803 UP = (char *)TGETSTR("up", &tp);
1804 p = TGETSTR("pc", &tp);
1805 if (p)
1806 PC = *p;
1807# endif
1808}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001809#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001810
Bram Moolenaar4654d632022-11-17 22:05:12 +00001811/*
1812 * Report "term" is not found and list the ones we do know about.
1813 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001814 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001815report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001816{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001817 mch_errmsg("\r\n");
1818 if (error_msg != NULL)
1819 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001820 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001821 mch_errmsg("\r\n");
1822 }
1823 mch_errmsg("'");
1824 mch_errmsg((char *)term);
1825 mch_errmsg(_("' not known. Available builtin terminals are:"));
1826 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001827
1828 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001829 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001830 char *name = builtin_terminals[i].bitc_name;
1831 if (name == NULL) // end marker
1832 break;
1833 // Do not mention the "gui" entry, the user won't need to type it.
1834 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001835 {
1836#ifdef HAVE_TGETENT
1837 mch_errmsg(" builtin_");
1838#else
1839 mch_errmsg(" ");
1840#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001841 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001842 mch_errmsg("\r\n");
1843 }
1844 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001845 // Output extra 'cmdheight' line breaks to avoid that the following error
1846 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001847 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001848 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001849}
1850
1851 static void
1852report_default_term(char_u *term)
1853{
1854 mch_errmsg(_("defaulting to '"));
1855 mch_errmsg((char *)term);
1856 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001857 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001858 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001859 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001860 out_flush();
1861 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001862 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001863 }
1864}
1865
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001867 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1868 * for the first matching entry.
1869 * When "term" is NULL then compile all patterns to check for any errors.
1870 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1871 * Returns KEYPROTOCOL_NONE if there is no match.
1872 */
1873 keyprot_T
1874match_keyprotocol(char_u *term)
1875{
1876 int len = (int)STRLEN(p_kpc) + 1;
1877 char_u *buf = alloc(len);
1878 if (buf == NULL)
1879 return KEYPROTOCOL_FAIL;
1880
1881 keyprot_T ret = KEYPROTOCOL_FAIL;
1882 char_u *p = p_kpc;
1883 while (*p != NUL)
1884 {
1885 // Isolate one comma separated item.
1886 (void)copy_option_part(&p, buf, len, ",");
1887 char_u *colon = vim_strchr(buf, ':');
1888 if (colon == NULL || colon == buf || colon[1] == NUL)
1889 goto theend;
1890 *colon = NUL;
1891
1892 keyprot_T prot;
1893 if (STRCMP(colon + 1, "none") == 0)
1894 prot = KEYPROTOCOL_NONE;
1895 else if (STRCMP(colon + 1, "mok2") == 0)
1896 prot = KEYPROTOCOL_MOK2;
1897 else if (STRCMP(colon + 1, "kitty") == 0)
1898 prot = KEYPROTOCOL_KITTY;
1899 else
1900 goto theend;
1901
1902 regmatch_T regmatch;
1903 CLEAR_FIELD(regmatch);
1904 regmatch.rm_ic = TRUE;
1905 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1906 if (regmatch.regprog == NULL)
1907 goto theend;
1908
1909 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1910 vim_regfree(regmatch.regprog);
1911 if (match)
1912 {
1913 ret = prot;
1914 goto theend;
1915 }
1916
1917 }
1918
1919 // No match found, use "none".
1920 ret = KEYPROTOCOL_NONE;
1921
1922theend:
1923 vim_free(buf);
1924 return ret;
1925}
1926
1927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 * Set terminal options for terminal "term".
1929 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1930 *
1931 * While doing this, until ttest(), some options may be NULL, be careful.
1932 */
1933 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001934set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936#ifdef HAVE_TGETENT
1937 int builtin_first = p_tbi;
1938 int try;
1939 int termcap_cleared = FALSE;
1940#endif
1941 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001942 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 char_u *bs_p, *del_p;
1944
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001945 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001946 if (silent_mode)
1947 return OK;
1948
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001949 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950
1951 if (term_is_builtin(term))
1952 {
1953 term += 8;
1954#ifdef HAVE_TGETENT
1955 builtin_first = 1;
1956#endif
1957 }
1958
1959/*
1960 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1961 * If builtin_first is TRUE:
1962 * 0. try builtin termcap
1963 * 1. try external termcap
1964 * 2. if both fail default to a builtin terminal
1965 * If builtin_first is FALSE:
1966 * 1. try external termcap
1967 * 2. try builtin termcap, if both fail default to a builtin terminal
1968 */
1969#ifdef HAVE_TGETENT
1970 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1971 {
1972 /*
1973 * Use external termcap
1974 */
1975 if (try == 1)
1976 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
1979 /*
1980 * If the external termcap does not have a matching entry, try the
1981 * builtin ones.
1982 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01001983 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 if (!termcap_cleared)
1986 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001987 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 termcap_cleared = TRUE;
1989 }
1990
Bram Moolenaar69e05692018-05-10 14:11:52 +02001991 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 }
1993 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001994 else // try == 0 || try == 2
1995#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 /*
1997 * Use builtin termcap
1998 */
1999 {
2000#ifdef HAVE_TGETENT
2001 /*
2002 * If builtin termcap was already used, there is no need to search
2003 * for the builtin termcap again, quit now.
2004 */
2005 if (try == 2 && builtin_first && termcap_cleared)
2006 break;
2007#endif
2008 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002009 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00002011 tcap_entry_T *termp = find_builtin_term(term);
2012 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 {
2014#ifdef HAVE_TGETENT
2015 /*
2016 * If try == 0, first try the external termcap. If that is not
2017 * found we'll get back here with try == 2.
2018 * If termcap_cleared is set we used the external termcap,
2019 * don't complain about not finding the term in the builtin
2020 * termcap.
2021 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002022 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002024 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 break;
2026#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02002027 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002029 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (starting != NO_SCREEN)
2031 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002032 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 wait_return(TRUE);
2034 return FAIL;
2035 }
2036 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002037 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002038 set_string_option_direct((char_u *)"term", -1, term,
2039 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 display_errors();
2041 }
2042 out_flush();
2043#ifdef HAVE_TGETENT
2044 if (!termcap_cleared)
2045 {
2046#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002047 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048#ifdef HAVE_TGETENT
2049 termcap_cleared = TRUE;
2050 }
2051#endif
2052 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002053
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054#ifdef FEAT_GUI
2055 if (term_is_gui(term))
2056 {
2057 out_flush();
2058 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002059 // If starting the GUI failed, don't do any of the other
2060 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 if (!gui.in_use)
2062 return FAIL;
Bram Moolenaar1a173402022-12-02 12:28:47 +00002063# ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002064 break; // don't try using external termcap
Bram Moolenaar1a173402022-12-02 12:28:47 +00002065# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002067#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 }
2069#ifdef HAVE_TGETENT
2070 }
2071#endif
2072
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002073#ifdef FEAT_GUI
2074 if (!gui.in_use)
2075#endif
2076 {
2077 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
2078 // termcap entries if there is an entry maching "term".
2079 keyprot_T kpc = match_keyprotocol(term);
2080 if (kpc == KEYPROTOCOL_KITTY)
2081 apply_builtin_tcap(term, builtin_kitty, TRUE);
2082 else if (kpc == KEYPROTOCOL_MOK2)
2083 apply_builtin_tcap(term, builtin_mok2, TRUE);
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002084
2085#ifdef FEAT_TERMGUICOLORS
2086 // There is no good way to detect that the terminal supports RGB
2087 // colors. Since these termcap entries are non-standard anyway and
2088 // only used when the user sets 'termguicolors' we might as well add
2089 // them. But not when one of them was alredy set.
2090 if (term_strings_not_set(KS_8F)
2091 && term_strings_not_set(KS_8B)
2092 && term_strings_not_set(KS_8U))
2093 apply_builtin_tcap(term, builtin_rgb, TRUE);
2094#endif
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002095 }
2096
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097/*
2098 * special: There is no info in the termcap about whether the cursor
2099 * positioning is relative to the start of the screen or to the start of the
2100 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2101 * relative.
2102 */
2103 if (STRCMP(term, "pcterm") == 0)
2104 T_CCS = (char_u *)"yes";
2105 else
2106 T_CCS = empty_option;
2107
Bram Moolenaar731d0072022-12-18 17:47:18 +00002108 // Special case: "kitty" does not normally have a "RV" entry in terminfo,
2109 // but we need to request the version for several other things to work.
2110 if (strstr((char *)term, "kitty") != NULL
2111 && (T_CRV == NULL || *T_CRV == NUL))
2112 T_CRV = (char_u *)"\033[>c";
2113
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114#ifdef UNIX
2115/*
2116 * Any "stty" settings override the default for t_kb from the termcap.
2117 * This is in os_unix.c, because it depends a lot on the version of unix that
2118 * is being used.
2119 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2120 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002121# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 get_stty();
2125#endif
2126
2127/*
2128 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2129 * didn't work, use the default CTRL-H
2130 * The default for t_kD is DEL, unless t_kb is DEL.
2131 * The vim_strsave'd strings are probably lost forever, well it's only two
2132 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2133 * directly.
2134 */
2135#ifdef FEAT_GUI
2136 if (!gui.in_use)
2137#endif
2138 {
2139 bs_p = find_termcode((char_u *)"kb");
2140 del_p = find_termcode((char_u *)"kD");
2141 if (bs_p == NULL || *bs_p == NUL)
2142 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2143 if ((del_p == NULL || *del_p == NUL) &&
2144 (bs_p == NULL || *bs_p != DEL))
2145 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2146 }
2147
2148#if defined(UNIX) || defined(VMS)
2149 term_is_xterm = vim_is_xterm(term);
2150#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002151#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002152 // Reset terminal properties that are set based on the termresponse, which
2153 // will be sent out soon.
2154 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002157#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 /*
2159 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2160 * The termcode for the mouse is added as a side effect in option.c.
2161 */
2162 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002163 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002165# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002166 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
2168 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002169 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 else
2171 p = (char_u *)"xterm";
2172 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002175 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002176 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002177 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2178 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002179 reset_option_was_set((char_u *)"ttym");
2180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002182# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002186 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002188#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
Bram Moolenaarbf789742021-01-16 11:21:40 +01002192#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002193 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002194 if (use_xterm_like_mouse(term))
2195 {
2196 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002197
2198 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002199 name[0] = KS_EXTRA;
2200 name[1] = KE_FOCUSGAINED;
2201 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002202 add_termcode(name, (char_u *)"\033[I", FALSE);
2203
2204 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002205 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002206 add_termcode(name, (char_u *)"\033[O", FALSE);
2207
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002208 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002209 }
2210#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002211#if (defined(UNIX) || defined(VMS))
2212 // First time after setting 'term' a focus event is always reported.
2213 focus_state = MAYBE;
2214#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002215
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002217 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 if (STRCMP(term, DEFAULT_TERM) != 0)
2219 term_console = FALSE;
2220 else
2221 {
2222 term_console = TRUE;
2223# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002224 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225# endif
2226 }
2227#endif
2228
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002229 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002231 full_screen = TRUE; // we can use termcap codes from now on
2232 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002234 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002235 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002236 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237#endif
2238
2239 /*
2240 * Initialize the terminal with the appropriate termcap codes.
2241 * Set the mouse and window title if possible.
2242 * Don't do this when starting, need to parse the .vimrc first, because it
2243 * may redefine t_TI etc.
2244 */
2245 if (starting != NO_SCREEN)
2246 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002247 starttermcap(); // may change terminal mode
2248 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002249 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
2251
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002252 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 if (width <= 0 || height <= 0)
2254 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002255 // termcap failed to report size
2256 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002258#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002259 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002261 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262#endif
2263 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002264 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (starting != NO_SCREEN)
2266 {
2267 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002268 scroll_region_reset(); // In case Rows changed
2269 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002272 buf_T *buf;
2273 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274
2275 /*
2276 * Execute the TermChanged autocommands for each buffer that is
2277 * loaded.
2278 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002279 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 {
2281 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002282 {
2283 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002284 if (curbuf == buf)
2285 {
2286 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 curbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002288 // restore curwin/curbuf and a few other things
2289 aucmd_restbuf(&aco);
2290 }
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295
2296#ifdef FEAT_TERMRESPONSE
2297 may_req_termresponse();
2298#endif
2299
2300 return OK;
2301}
2302
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002303#if defined(EXITFREE) || defined(PROTO)
2304
2305# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002306# include <term.h> // declares cur_term
2307# endif
2308
2309/*
2310 * If supported, delete "cur_term", which caches terminal related entries.
2311 * Avoids that valgrind reports possibly lost memory.
2312 */
2313 void
2314free_cur_term()
2315{
2316# ifdef HAVE_DEL_CURTERM
2317 if (cur_term)
2318 del_curterm(cur_term);
2319# endif
2320}
2321
2322#endif
2323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324#ifdef HAVE_TGETENT
2325/*
2326 * Call tgetent()
2327 * Return error message if it fails, NULL if it's OK.
2328 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002329 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002330invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331{
2332 int i;
2333
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002334 // Note: Valgrind may report a leak here, because the library keeps one
2335 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002337 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002339 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340# endif
2341 )
2342 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002343 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2344 // tgetent() with the always existing "dumb" entry to avoid a crash or
2345 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 (void)TGETENT(tbuf, "dumb");
2347
2348 if (i < 0)
2349# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002350 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if (i == 0)
2352# endif
2353#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002354 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002356 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357#endif
2358 }
2359 return NULL;
2360}
2361
2362/*
2363 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2364 * Fix that here.
2365 */
2366 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002367vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368{
2369 char *p;
2370
2371 p = tgetstr(s, (char **)pp);
2372 if (p == (char *)-1)
2373 p = NULL;
2374 return (char_u *)p;
2375}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002376#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002378#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379/*
2380 * Get Columns and Rows from the termcap. Used after a window signal if the
2381 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2382 * and "li" entries never change. But on some systems this works.
2383 * Errors while getting the entries are ignored.
2384 */
2385 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002386getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002387 long *cp, // pointer to columns
2388 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389{
2390 char_u tbuf[TBUFSZ];
2391
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002392 if (T_NAME != NULL && *T_NAME != NUL && invoke_tgetent(tbuf, T_NAME) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 {
2394 if (*cp == 0)
2395 *cp = tgetnum("co");
2396 if (*rp == 0)
2397 *rp = tgetnum("li");
2398 }
2399}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002400#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
2402/*
2403 * Get a string entry from the termcap and add it to the list of termcodes.
2404 * Used for <t_xx> special keys.
2405 * Give an error message for failure when not sourcing.
2406 * If force given, replace an existing entry.
2407 * Return FAIL if the entry was not found, OK if the entry was added.
2408 */
2409 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002410add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
2412 char_u *term;
2413 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414#ifdef HAVE_TGETENT
2415 char_u *string;
2416 int i;
2417 int builtin_first;
2418 char_u tbuf[TBUFSZ];
2419 char_u tstrbuf[TBUFSZ];
2420 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002421 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422#endif
2423
2424/*
2425 * If the GUI is running or will start in a moment, we only support the keys
2426 * that the GUI can produce.
2427 */
2428#ifdef FEAT_GUI
2429 if (gui.in_use || gui.starting)
2430 return gui_mch_haskey(name);
2431#endif
2432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002433 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 return OK;
2435
2436 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002437 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 return FAIL;
2439
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002440 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
2442 term += 8;
2443#ifdef HAVE_TGETENT
2444 builtin_first = TRUE;
2445#endif
2446 }
2447#ifdef HAVE_TGETENT
2448 else
2449 builtin_first = p_tbi;
2450#endif
2451
2452#ifdef HAVE_TGETENT
2453/*
2454 * We can get the entry from the builtin termcap and from the external one.
2455 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2456 * builtin termcap first.
2457 * If 'ttybuiltin' is off, try external termcap first.
2458 */
2459 for (i = 0; i < 2; ++i)
2460 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002461 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462#endif
2463 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002464 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 */
2466 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002467 tcap_entry_T *termp = find_builtin_term(term);
2468 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
2470 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002471 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 while (termp->bt_entry != (int)KS_NAME)
2473 {
2474 if ((int)termp->bt_entry == key)
2475 {
2476 add_termcode(name, (char_u *)termp->bt_string,
2477 term_is_8bit(term));
2478 return OK;
2479 }
2480 ++termp;
2481 }
2482 }
2483 }
2484#ifdef HAVE_TGETENT
2485 else
2486 /*
2487 * Search in external termcap
2488 */
2489 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002490 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (error_msg == NULL)
2492 {
2493 string = TGETSTR((char *)name, &tp);
2494 if (string != NULL && *string != NUL)
2495 {
2496 add_termcode(name, string, FALSE);
2497 return OK;
2498 }
2499 }
2500 }
2501 }
2502#endif
2503
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002504 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 {
2506#ifdef HAVE_TGETENT
2507 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002508 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 else
2510#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002511 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 }
2513 return FAIL;
2514}
2515
2516 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002517term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
2519 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2520}
2521
2522/*
2523 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2524 * Assume that the terminal is using 8-bit controls when the name contains
2525 * "8bit", like in "xterm-8bit".
2526 */
2527 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002528term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
2530 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2531}
2532
2533/*
2534 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002535 * <Esc>[ -> CSI <M_C_[>
2536 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 * <Esc>O -> <M-C-O>
2538 */
2539 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002540term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541{
2542 if (*p == ESC)
2543 {
2544 if (p[1] == '[')
2545 return CSI;
2546 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002547 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 if (p[1] == 'O')
2549 return 0x8f;
2550 }
2551 return 0;
2552}
2553
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002554#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002556term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
2558 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2559}
2560#endif
2561
2562#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2563
2564 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002565tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566{
2567 static char_u buf[16];
2568 char_u *p;
2569
2570 p = buf + 15;
2571 *p = '\0';
2572 do
2573 {
2574 --p;
2575 *p = (char_u) (i % 10 + '0');
2576 i /= 10;
2577 }
2578 while (i > 0 && p > buf);
2579 return p;
2580}
2581#endif
2582
2583#ifndef HAVE_TGETENT
2584
2585/*
2586 * minimal tgoto() implementation.
2587 * no padding and we only parse for %i %d and %+char
2588 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002589 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002590tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
2592 static char buf[30];
2593 char *p, *s, *e;
2594
2595 if (!cm)
2596 return "OOPS";
2597 e = buf + 29;
2598 for (s = buf; s < e && *cm; cm++)
2599 {
2600 if (*cm != '%')
2601 {
2602 *s++ = *cm;
2603 continue;
2604 }
2605 switch (*++cm)
2606 {
2607 case 'd':
2608 p = (char *)tltoa((unsigned long)y);
2609 y = x;
2610 while (*p)
2611 *s++ = *p++;
2612 break;
2613 case 'i':
2614 x++;
2615 y++;
2616 break;
2617 case '+':
2618 *s++ = (char)(*++cm + y);
2619 y = x;
2620 break;
2621 case '%':
2622 *s++ = *cm;
2623 break;
2624 default:
2625 return "OOPS";
2626 }
2627 }
2628 *s = '\0';
2629 return buf;
2630}
2631
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002632#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
2634/*
2635 * Set the terminal name and initialize the terminal options.
2636 * If "name" is NULL or empty, get the terminal name from the environment.
2637 * If that fails, use the default terminal name.
2638 */
2639 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002640termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641{
Bram Moolenaar731d0072022-12-18 17:47:18 +00002642 char_u *term = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
Bram Moolenaar731d0072022-12-18 17:47:18 +00002644 if (term != NULL && *term == NUL)
2645 term = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647#ifndef MSWIN
2648 if (term == NULL)
2649 term = mch_getenv((char_u *)"TERM");
2650#endif
2651 if (term == NULL || *term == NUL)
2652 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002653 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002655 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 set_string_default("term", term);
2657 set_string_default("ttytype", term);
2658
Bram Moolenaar731d0072022-12-18 17:47:18 +00002659 // Avoid using "term" here, because the next mch_getenv() may overwrite it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 set_termname(T_NAME != NULL ? T_NAME : term);
2661}
2662
2663/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002664 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002666#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002667
2668// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002670
2671static int out_pos = 0; // number of chars in out_buf
2672
2673// Since the maximum number of SGR parameters shown as a normal value range is
2674// 16, the escape sequence length can be 4 * 16 + lead + tail.
2675#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
2677/*
2678 * out_flush(): flush the output buffer
2679 */
2680 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002681out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
2683 int len;
2684
2685 if (out_pos != 0)
2686 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002687 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 len = out_pos;
2689 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002690 ui_write(out_buf, len, FALSE);
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00002691#ifdef FEAT_EVAL
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002692 if (ch_log_output != FALSE)
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002693 {
2694 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002695 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002696# ifdef FEAT_GUI
2697 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2698# endif
2699 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002700 out_buf);
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002701 if (ch_log_output == TRUE)
2702 ch_log_output = FALSE; // only log once
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002703 }
2704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 }
2706}
2707
Bram Moolenaara338adc2018-01-31 20:51:47 +01002708/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002709 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2710 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002711 */
2712 void
2713out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002714 int force UNUSED, // when TRUE, update cursor even when not moved
2715 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002716{
2717 mch_disable_flush();
2718 out_flush();
2719 mch_enable_flush();
2720#ifdef FEAT_GUI
2721 if (gui.in_use)
2722 {
2723 gui_update_cursor(force, clear_selection);
2724 gui_may_flush();
2725 }
2726#endif
2727}
2728
2729
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730/*
2731 * Sometimes a byte out of a multi-byte character is written with out_char().
2732 * To avoid flushing half of the character, call this function first.
2733 */
2734 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002735out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736{
2737 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2738 out_flush();
2739}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
2741#ifdef FEAT_GUI
2742/*
2743 * out_trash(): Throw away the contents of the output buffer
2744 */
2745 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002746out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747{
2748 out_pos = 0;
2749}
2750#endif
2751
2752/*
2753 * out_char(c): put a byte into the output buffer.
2754 * Flush it if it becomes full.
2755 * This should not be used for outputting text on the screen (use functions
2756 * like msg_puts() and screen_putchar() for that).
2757 */
2758 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002759out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760{
Bram Moolenaard0573012017-10-28 21:11:06 +02002761#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002762 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 out_char('\r');
2764#endif
2765
2766 out_buf[out_pos++] = c;
2767
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002768 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (out_pos >= OUT_SIZE || p_wd)
2770 out_flush();
2771}
2772
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002774 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002776 static int
2777out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002779 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780
2781 if (out_pos >= OUT_SIZE)
2782 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002783 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784}
2785
2786/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002787 * A never-padding out_str().
2788 * Use this whenever you don't want to run the string through tputs().
2789 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 * is likely to strip off leading digits, that it mistakes for padding
2791 * information, and "%i", "%d", etc.
2792 * This should only be used for writing terminal codes, not for outputting
2793 * normal text (use functions like msg_puts() and screen_putchar() for that).
2794 */
2795 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002796out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002798 // avoid terminal strings being split up
2799 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002801
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 while (*s)
2803 out_char_nf(*s++);
2804
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002805 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 if (p_wd)
2807 out_flush();
2808}
2809
2810/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002811 * A conditional-flushing out_str, mainly for visualbell.
2812 * Handles a delay internally, because termlib may not respect the delay or do
2813 * it at the wrong time.
2814 * Note: Only for terminal strings.
2815 */
2816 void
2817out_str_cf(char_u *s)
2818{
2819 if (s != NULL && *s)
2820 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002821#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002822 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002823#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002824
2825#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002826 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002827 if (gui.in_use)
2828 {
2829 out_str_nf(s);
2830 return;
2831 }
2832#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002833 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002834 out_flush();
2835#ifdef HAVE_TGETENT
2836 for (p = s; *s; ++s)
2837 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002838 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002839 if (*s == '$' && *(s + 1) == '<')
2840 {
2841 char_u save_c = *s;
2842 int duration = atoi((char *)s + 2);
2843
2844 *s = NUL;
2845 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2846 *s = save_c;
2847 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002848# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002849 // Only sleep here if we can limit this happening in
2850 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002851 p = vim_strchr(s, '>');
2852 if (p == NULL || duration <= 0)
2853 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002854 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002855 p = s;
2856 }
2857 else
2858 {
2859 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002860 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002861 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002862# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002863 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002864 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002865# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002866 break;
2867 }
2868 }
2869 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2870#else
2871 while (*s)
2872 out_char_nf(*s++);
2873#endif
2874
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002875 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002876 if (p_wd)
2877 out_flush();
2878 }
2879}
2880
2881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002883 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 * This should only be used for writing terminal codes, not for outputting
2885 * normal text (use functions like msg_puts() and screen_putchar() for that).
2886 */
2887 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002888out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
2890 if (s != NULL && *s)
2891 {
2892#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002893 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 if (gui.in_use)
2895 {
2896 out_str_nf(s);
2897 return;
2898 }
2899#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002900 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002901 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 out_flush();
2903#ifdef HAVE_TGETENT
2904 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2905#else
2906 while (*s)
2907 out_char_nf(*s++);
2908#endif
2909
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002910 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 if (p_wd)
2912 out_flush();
2913 }
2914}
2915
2916/*
2917 * cursor positioning using termcap parser. (jw)
2918 */
2919 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002920term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921{
2922 OUT_STR(tgoto((char *)T_CM, col, row));
2923}
2924
2925 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002926term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
2928 OUT_STR(tgoto((char *)T_CRI, 0, i));
2929}
2930
2931 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002932term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
2934 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2935}
2936
2937 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002938term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
2940 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2941}
2942
2943#if defined(HAVE_TGETENT) || defined(PROTO)
2944 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002945term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002947 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 if (x < 0)
2949 x = 0;
2950 if (y < 0)
2951 y = 0;
2952 OUT_STR(tgoto((char *)T_CWP, y, x));
2953}
2954
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002955# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2956/*
2957 * Return TRUE if we can request the terminal for a response.
2958 */
2959 static int
2960can_get_termresponse()
2961{
2962 return cur_tmode == TMODE_RAW
2963 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002964# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002965 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002966# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002967 && p_ek;
2968}
2969
Bram Moolenaarafd78262019-05-10 23:10:31 +02002970/*
2971 * Set "status" to STATUS_SENT.
2972 */
2973 static void
2974termrequest_sent(termrequest_T *status)
2975{
2976 status->tr_progress = STATUS_SENT;
2977 status->tr_start = time(NULL);
2978}
2979
2980/*
2981 * Return TRUE if any of the requests are in STATUS_SENT.
2982 */
2983 static int
2984termrequest_any_pending()
2985{
2986 int i;
2987 time_t now = time(NULL);
2988
2989 for (i = 0; all_termrequests[i] != NULL; ++i)
2990 {
2991 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2992 {
2993 if (all_termrequests[i]->tr_start > 0 && now > 0
2994 && all_termrequests[i]->tr_start + 2 < now)
2995 // Sent the request more than 2 seconds ago and didn't get a
2996 // response, assume it failed.
2997 all_termrequests[i]->tr_progress = STATUS_FAIL;
2998 else
2999 return TRUE;
3000 }
3001 }
3002 return FALSE;
3003}
3004
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003005static int winpos_x = -1;
3006static int winpos_y = -1;
3007static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003008
Bram Moolenaar6bc93052019-04-06 20:00:19 +02003009# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003010/*
3011 * Try getting the Vim window position from the terminal.
3012 * Returns OK or FAIL.
3013 */
3014 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01003015term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003016{
3017 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003018 int prev_winpos_x = winpos_x;
3019 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003020
3021 if (*T_CGP == NUL || !can_get_termresponse())
3022 return FAIL;
3023 winpos_x = -1;
3024 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003025 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003026 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003027 OUT_STR(T_CGP);
3028 out_flush();
3029
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003030 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003031 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003032 {
3033 (void)vpeekc_nomap();
3034 if (winpos_x >= 0 && winpos_y >= 0)
3035 {
3036 *x = winpos_x;
3037 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003038 return OK;
3039 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01003040 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003041 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003042 // Do not reset "did_request_winpos", if we timed out the response might
3043 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003044
3045 winpos_x = prev_winpos_x;
3046 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02003047 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003048 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003049 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003050 *x = winpos_x;
3051 *y = winpos_y;
3052 return OK;
3053 }
3054
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003055 return FALSE;
3056}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003057# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003058# endif
3059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003061term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003063 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064}
3065#endif
3066
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003068term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
3070 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003071 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003072 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003074 // Special handling of 16 colors, because termcap can't handle it
3075 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3076 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003078 && ((s[0] == ESC && s[1] == '[')
3079#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3080 || (s[0] == ESC && s[1] == '|')
3081#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003082 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 && s[i] != NUL
3084 && (STRCMP(s + i + 1, "%p1%dm") == 0
3085 || STRCMP(s + i + 1, "%dm") == 0)
3086 && (s[i] == '3' || s[i] == '4'))
3087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003089 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003091 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003093 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003094#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003095 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003096#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003097 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003098 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3099 : (n >= 16 ? "48;5;" : "10");
3100
3101 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3103 }
3104 else
3105 OUT_STR(tgoto((char *)s, 0, n));
3106}
3107
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003108 void
3109term_fg_color(int n)
3110{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003111 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003112 if (*T_CAF)
3113 term_color(T_CAF, n);
3114 else if (*T_CSF)
3115 term_color(T_CSF, n);
3116}
3117
3118 void
3119term_bg_color(int n)
3120{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003121 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003122 if (*T_CAB)
3123 term_color(T_CAB, n);
3124 else if (*T_CSB)
3125 term_color(T_CSB, n);
3126}
3127
Bram Moolenaare023e882020-05-31 16:42:30 +02003128 void
3129term_ul_color(int n)
3130{
3131 if (*T_CAU)
3132 term_color(T_CAU, n);
3133}
3134
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003135/*
3136 * Return "dark" or "light" depending on the kind of terminal.
3137 * This is just guessing! Recognized are:
3138 * "linux" Linux console
3139 * "screen.linux" Linux console with screen
3140 * "cygwin.*" Cygwin shell
3141 * "putty.*" Putty program
3142 * We also check the COLORFGBG environment variable, which is set by
3143 * rxvt and derivatives. This variable contains either two or three
3144 * values separated by semicolons; we want the last value in either
3145 * case. If this value is 0-6 or 8, our background is dark.
3146 */
3147 char_u *
3148term_bg_default(void)
3149{
3150#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003151 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003152 return (char_u *)"dark";
3153#else
3154 char_u *p;
3155
3156 if (STRCMP(T_NAME, "linux") == 0
3157 || STRCMP(T_NAME, "screen.linux") == 0
3158 || STRNCMP(T_NAME, "cygwin", 6) == 0
3159 || STRNCMP(T_NAME, "putty", 5) == 0
3160 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3161 && (p = vim_strrchr(p, ';')) != NULL
3162 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3163 && p[2] == NUL))
3164 return (char_u *)"dark";
3165 return (char_u *)"light";
3166#endif
3167}
3168
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003169#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003170
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003171#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3172#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3173#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003174
3175 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003176term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003177{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003178#define MAX_COLOR_STR_LEN 100
3179 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003180
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003181 if (*s == NUL)
3182 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003183 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003184 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003185#ifdef FEAT_VTP
Christopher Plewright38804d62022-11-09 23:55:52 +00003186 if (has_vtp_working())
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003187 {
3188 out_flush();
3189 buf[1] = '[';
3190 vtp_printf(buf);
3191 }
3192 else
3193#endif
3194 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003195}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003196
3197 void
3198term_fg_rgb_color(guicolor_T rgb)
3199{
Christopher Plewright38804d62022-11-09 23:55:52 +00003200 if (rgb != INVALCOLOR)
3201 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003202}
3203
3204 void
3205term_bg_rgb_color(guicolor_T rgb)
3206{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003207 if (rgb != INVALCOLOR)
3208 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003209}
Bram Moolenaare023e882020-05-31 16:42:30 +02003210
3211 void
3212term_ul_rgb_color(guicolor_T rgb)
3213{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003214# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003215 // If the user explicitly sets t_8u then use it. Otherwise wait for
3216 // termresponse to be received, which is when t_8u would be set and a
3217 // redraw is needed if it was used.
3218 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003219 write_t_8u_state = MAYBE;
3220 else
3221# endif
3222 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003223}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003224#endif
3225
Bram Moolenaar651fca82021-11-29 20:39:38 +00003226#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227/*
3228 * Generic function to set window title, using t_ts and t_fs.
3229 */
3230 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003231term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003233 MAY_WANT_TO_LOG_THIS;
3234
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003235 // t_ts takes one argument: column in status line
3236 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003238 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 out_flush();
3240}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003241
3242/*
3243 * Tell the terminal to push (save) the title and/or icon, so that it can be
3244 * popped (restored) later.
3245 */
3246 void
3247term_push_title(int which)
3248{
Bram Moolenaar27821262019-05-08 16:41:09 +02003249 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003250 {
3251 OUT_STR(T_CST);
3252 out_flush();
3253 }
3254
Bram Moolenaar27821262019-05-08 16:41:09 +02003255 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003256 {
3257 OUT_STR(T_SSI);
3258 out_flush();
3259 }
3260}
3261
3262/*
3263 * Tell the terminal to pop the title and/or icon.
3264 */
3265 void
3266term_pop_title(int which)
3267{
Bram Moolenaar27821262019-05-08 16:41:09 +02003268 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003269 {
3270 OUT_STR(T_CRT);
3271 out_flush();
3272 }
3273
Bram Moolenaar27821262019-05-08 16:41:09 +02003274 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003275 {
3276 OUT_STR(T_SRI);
3277 out_flush();
3278 }
3279}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280#endif
3281
3282/*
3283 * Make sure we have a valid set or terminal options.
3284 * Replace all entries that are NULL by empty_option
3285 */
3286 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003287ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003289 char_u *env_colors;
3290
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003291 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292
3293 /*
3294 * MUST have "cm": cursor motion.
3295 */
3296 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003297 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298
3299 /*
3300 * if "cs" defined, use a scroll region, it's faster.
3301 */
3302 if (*T_CS != NUL)
3303 scroll_region = TRUE;
3304 else
3305 scroll_region = FALSE;
3306
3307 if (pairs)
3308 {
3309 /*
3310 * optional pairs
3311 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003312 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 if (*T_ME == NUL)
3314 T_ME = T_MR = T_MD = T_MB = empty_option;
3315 if (*T_SO == NUL || *T_SE == NUL)
3316 T_SO = T_SE = empty_option;
3317 if (*T_US == NUL || *T_UE == NUL)
3318 T_US = T_UE = empty_option;
3319 if (*T_CZH == NUL || *T_CZR == NUL)
3320 T_CZH = T_CZR = empty_option;
3321
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003322 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 if (*T_VE == NUL)
3324 T_VI = empty_option;
3325
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003326 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 if (*T_ME == NUL)
3328 {
3329 T_ME = T_SE;
3330 T_MR = T_SO;
3331 T_MD = T_SO;
3332 }
3333
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003334 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 if (*T_SO == NUL)
3336 {
3337 T_SE = T_ME;
3338 if (*T_MR == NUL)
3339 T_SO = T_MD;
3340 else
3341 T_SO = T_MR;
3342 }
3343
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003344 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (*T_CZH == NUL)
3346 {
3347 T_CZR = T_ME;
3348 if (*T_MR == NUL)
3349 T_CZH = T_MD;
3350 else
3351 T_CZH = T_MR;
3352 }
3353
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003354 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (*T_CSB == NUL || *T_CSF == NUL)
3356 {
3357 T_CSB = empty_option;
3358 T_CSF = empty_option;
3359 }
3360
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003361 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (*T_CAB == NUL || *T_CAF == NUL)
3363 {
3364 T_CAB = empty_option;
3365 T_CAF = empty_option;
3366 }
3367
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003368 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003370 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003372 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 p_wiv = (*T_XS != NUL);
3374 }
3375 need_gather = TRUE;
3376
Bram Moolenaar759d8152020-04-26 16:52:49 +02003377 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3378 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003380#ifdef FEAT_GUI
3381 if (!gui.in_use)
3382#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003383 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003384 env_colors = mch_getenv((char_u *)"COLORS");
3385 if (env_colors != NULL && isdigit(*env_colors))
3386 {
3387 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003388
Bram Moolenaar759d8152020-04-26 16:52:49 +02003389 if (colors != t_colors)
3390 set_color_count(colors);
3391 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393}
3394
3395#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3396 || defined(PROTO)
3397/*
3398 * Represent the given long_u as individual bytes, with the most significant
3399 * byte first, and store them in dst.
3400 */
3401 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003402add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403{
3404 int i;
3405 int shift;
3406
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003407 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 {
3409 shift = 8 * (sizeof(long_u) - i);
3410 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3411 }
3412}
3413
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414/*
3415 * Interpret the next string of bytes in buf as a long integer, with the most
3416 * significant byte first. Note that it is assumed that buf has been through
3417 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3418 * Puts result in val, and returns the number of bytes read from buf
3419 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3420 * were present.
3421 */
3422 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003423get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
3425 int len;
3426 char_u bytes[sizeof(long_u)];
3427 int i;
3428 int shift;
3429
3430 *val = 0;
3431 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3432 if (len != -1)
3433 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003434 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 {
3436 shift = 8 * (sizeof(long_u) - 1 - i);
3437 *val += (long_u)bytes[i] << shift;
3438 }
3439 }
3440 return len;
3441}
3442#endif
3443
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444/*
3445 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3446 * that buf has been through inchar(). Returns the actual number of bytes used
3447 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3448 * available.
3449 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003450 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003451get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452{
3453 int len = 0;
3454 int i;
3455 char_u c;
3456
3457 for (i = 0; i < num_bytes; i++)
3458 {
3459 if ((c = buf[len++]) == NUL)
3460 return -1;
3461 if (c == K_SPECIAL)
3462 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003463 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 return -1;
3465 if (buf[len++] == (int)KS_ZERO)
3466 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003467 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3468 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003469 if (buf[len++] == (int)KE_CSI)
3470 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003472 else if (c == CSI && buf[len] == KS_EXTRA
3473 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003474 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3475 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003476 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 bytes[i] = c;
3478 }
3479 return len;
3480}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
3482/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003483 * Check if the new shell size is valid, correct it if it's too small or way
3484 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 */
3486 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003487check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003489 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003491 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003492
3493 // make sure these values are not invalid
3494 if (cmdline_row >= Rows)
3495 cmdline_row = Rows - 1;
3496 if (msg_row >= Rows)
3497 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003498}
3499
3500/*
3501 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3502 */
3503 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003504limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003505{
3506 if (Columns < MIN_COLUMNS)
3507 Columns = MIN_COLUMNS;
3508 else if (Columns > 10000)
3509 Columns = 10000;
3510 if (Rows > 1000)
3511 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512}
3513
Bram Moolenaar86b68352004-12-27 21:59:20 +00003514/*
3515 * Invoked just before the screen structures are going to be (re)allocated.
3516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003518win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520 static int old_Rows = 0;
3521 static int old_Columns = 0;
3522
3523 if (old_Rows != Rows || old_Columns != Columns)
3524 ui_new_shellsize();
3525 if (old_Rows != Rows)
3526 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003527 // If 'window' uses the whole screen, keep it using that.
3528 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003529 if (p_window == old_Rows - 1
3530 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003531 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003533 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
3535 if (old_Columns != Columns)
3536 {
3537 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003538 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540}
3541
3542/*
3543 * Call this function when the Vim shell has been resized in any way.
3544 * Will obtain the current size and redraw (also when size didn't change).
3545 */
3546 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003547shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548{
3549 set_shellsize(0, 0, FALSE);
3550}
3551
3552/*
3553 * Check if the shell size changed. Handle a resize.
3554 * When the size didn't change, nothing happens.
3555 */
3556 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003557shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558{
3559 int old_Rows = Rows;
3560 int old_Columns = Columns;
3561
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003562 if (!exiting
3563#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003564 // Do not get the size when executing a shell command during
3565 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003566 && !gui.starting
3567#endif
3568 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003569 {
3570 (void)ui_get_shellsize();
3571 check_shellsize();
3572 if (old_Rows != Rows || old_Columns != Columns)
3573 shell_resized();
3574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575}
3576
3577/*
3578 * Set size of the Vim shell.
3579 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3580 * window size (this is used for the :win command).
3581 * If 'mustset' is FALSE, we may try to get the real window size and if
3582 * it fails use 'width' and 'height'.
3583 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003584 static void
3585set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003587 if (updating_screen)
3588 // resizing while in update_screen() may cause a crash
3589 return;
3590
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003591 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003592 // buffer (or window) has already been closed and removing a scrollbar
3593 // causes a resize event. Don't resize then, it will happen after entering
3594 // another buffer.
3595 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003596 return;
3597
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003599 out_flush(); // must do this before mch_get_shellsize() for
3600 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601#endif
3602
3603 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3604 {
3605 Rows = height;
3606 Columns = width;
3607 check_shellsize();
3608 ui_set_shellsize(mustset);
3609 }
3610 else
3611 check_shellsize();
3612
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003613 // The window layout used to be adjusted here, but it now happens in
3614 // screenalloc() (also invoked from screenclear()). That is because the
3615 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
Bram Moolenaar24959102022-05-07 20:01:16 +01003617 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3618 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 screenclear();
3620 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003621 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623 if (starting != NO_SCREEN)
3624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003626
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 changed_line_abv_curs();
3628 invalidate_botline();
3629
3630 /*
3631 * We only redraw when it's needed:
3632 * - While at the more prompt or executing an external command, don't
3633 * redraw, but position the cursor.
3634 * - While editing the command line, only redraw that.
3635 * - in Ex mode, don't redraw anything.
3636 * - Otherwise, redraw right now, and position the cursor.
3637 * Always need to call update_screen() or screenalloc(), to make
3638 * sure Rows/Columns and the size of ScreenLines[] is correct!
3639 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003640 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3641 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
3643 screenalloc(FALSE);
3644 repeat_message();
3645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 else
3647 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003648 if (curwin->w_p_scb)
3649 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003650 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003651 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003652 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003653 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003654 }
3655 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003656 {
3657 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003658 if (pum_visible())
3659 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003660 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003661 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003662 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003663 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003664 if (redrawing())
3665 setcursor();
3666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003668 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
3670 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003671}
3672
3673 void
3674set_shellsize(int width, int height, int mustset)
3675{
3676 static int busy = FALSE;
3677 static int do_run = FALSE;
3678
3679 if (width < 0 || height < 0) // just checking...
3680 return;
3681
3682 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3683 {
3684 // postpone the resizing
3685 State = MODE_SETWSIZE;
3686 return;
3687 }
3688
3689 // Avoid recursiveness. This can happen when setting the window size
3690 // causes another window-changed signal or when two SIGWINCH signals come
3691 // very close together. There needs to be another run then after the
3692 // current one is done to pick up the latest size.
3693 do_run = TRUE;
3694 if (busy)
3695 return;
3696
3697 while (do_run)
3698 {
3699 do_run = FALSE;
3700 busy = TRUE;
3701 set_shellsize_inner(width, height, mustset);
3702 busy = FALSE;
3703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704}
3705
3706/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003707 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3708 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3709 */
3710 void
3711out_str_t_TE(void)
3712{
3713 out_str(T_CTE);
3714
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003715 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
Bram Moolenaarc255b782022-11-26 19:16:48 +00003716 // disable modifyOtherKeys, but until Xterm version 377 there is no way to
3717 // detect it's enabled again after the following t_TI. We assume that when
3718 // seenModifyOtherKeys was set before it will still be valid.
3719
3720 // When the modifyOtherKeys level is detected to be 2 we expect t_TE to
3721 // disable it. Remembering that it was detected to be enabled is useful in
3722 // some situations.
3723 // The following t_TI is expected to request the state and then
3724 // modify_otherkeys_state will be set again.
3725 if (modify_otherkeys_state == MOKS_ENABLED
3726 || modify_otherkeys_state == MOKS_DISABLED)
3727 modify_otherkeys_state = MOKS_DISABLED;
3728 else if (modify_otherkeys_state != MOKS_INITIAL)
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003729 modify_otherkeys_state = MOKS_AFTER_T_TE;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003730
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003731 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3732 // it. Remembering that it was detected to be enabled is useful in some
3733 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003734 // The following t_TI is expected to request the state and then
3735 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003736 if (kitty_protocol_state == KKPS_ENABLED
3737 || kitty_protocol_state == KKPS_DISABLED)
3738 kitty_protocol_state = KKPS_DISABLED;
3739 else
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003740 kitty_protocol_state = KKPS_AFTER_T_TE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003741}
3742
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003743static int send_t_RK = FALSE;
3744
3745/*
3746 * Output T_TI and setup for what follows.
3747 */
3748 void
3749out_str_t_TI(void)
3750{
3751 out_str(T_CTI);
3752
3753 // Send t_RK when there is no more work to do.
3754 send_t_RK = TRUE;
3755}
3756
3757/*
3758 * If t_TI was recently sent and there is no typeahead or work to do, now send
3759 * t_RK. This is postponed to avoid the response arriving in a shell command
3760 * or after Vim exits.
3761 */
3762 void
3763may_send_t_RK(void)
3764{
3765 if (send_t_RK
3766 && !work_pending()
3767 && !ex_normal_busy
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003768#ifdef FEAT_EVAL
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003769 && !in_feedkeys
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003770#endif
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003771 && !exiting)
3772 {
3773 send_t_RK = FALSE;
3774 out_str(T_CRK);
3775 out_flush();
3776 }
3777}
3778
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3781 * commands and Ex mode).
3782 */
3783 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003784settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785{
3786#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003787 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (gui.in_use)
3789 return;
3790#endif
3791
3792 if (full_screen)
3793 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003795 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3796 * set the terminal to raw mode, even though we think it already is,
3797 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 * When we think the terminal is normal, don't try to set it to
3799 * normal again, because that causes problems (logout!) on some
3800 * machines.
3801 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003802 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 {
3804#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003805# ifdef FEAT_GUI
3806 if (!gui.in_use && !gui.starting)
3807# endif
3808 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003809 // May need to check for T_CRV response and termcodes, it
3810 // doesn't work in Cooked mode, an external program may get
3811 // them.
3812 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003813 (void)vpeekc_nomap();
3814 check_for_codes_from_term();
3815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003818 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003819
3820 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3821 // Avoid doing this too often, on some terminals the codes are not
3822 // handled properly.
3823 if (termcap_active && tmode != TMODE_SLEEP
3824 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003825 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003826 MAY_WANT_TO_LOG_THIS;
3827
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003828 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003829 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003830 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003831 out_str_t_TE(); // possibly disables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003832 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003833 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003834 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003835 out_str(T_BE); // enable bracketed paste mode (should
3836 // be before mch_settmode().
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003837 out_str_t_TI(); // possibly enables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003838 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003841 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003844 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 out_flush();
3846 }
3847#ifdef FEAT_TERMRESPONSE
3848 may_req_termresponse();
3849#endif
3850 }
3851}
3852
3853 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003854starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855{
3856 if (full_screen && !termcap_active)
3857 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003858 MAY_WANT_TO_LOG_THIS;
3859
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003860 out_str(T_TI); // start termcap mode
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003861 out_str_t_TI(); // start "raw" mode
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003862 out_str(T_KS); // start "keypad transmit" mode
3863 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003864
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003865#if defined(UNIX) || defined(VMS)
3866 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003867 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003868 out_str(T_FE);
3869#endif
3870
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 out_flush();
3872 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003873 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003875# ifdef FEAT_GUI
3876 if (!gui.in_use && !gui.starting)
3877# endif
3878 {
3879 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003880 // Immediately check for a response. If t_Co changes, we don't
3881 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003882 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003883 check_for_codes_from_term();
3884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885#endif
3886 }
3887}
3888
3889 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003890stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891{
3892 screen_stop_highlight();
3893 reset_cterm_colors();
3894 if (termcap_active)
3895 {
3896#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003897# ifdef FEAT_GUI
3898 if (!gui.in_use && !gui.starting)
3899# endif
3900 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003901 // May need to discard T_CRV, T_U7 or T_RBG response.
3902 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003903 {
3904# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003905 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003906 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003907# endif
3908# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003909 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003910 if (exiting)
3911 tcflush(fileno(stdin), TCIFLUSH);
3912# endif
3913 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003914 // Check for termcodes first, otherwise an external program may
3915 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003916 check_for_codes_from_term();
3917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003919 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003920
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003921#if defined(UNIX) || defined(VMS)
3922 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003923 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003924 out_str(T_FD);
3925#endif
3926
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003927 out_str(T_BD); // disable bracketed paste mode
3928 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 out_flush();
3930 termcap_active = FALSE;
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00003931
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003932 // Output t_te before t_TE, t_te may switch between main and alternate
3933 // screen and following codes may work on the active screen only.
3934 //
3935 // When using the Kitty keyboard protocol the main and alternate screen
3936 // use a separate state. If we are (or were) using the Kitty keyboard
3937 // protocol and t_te is not empty (possibly switching screens) then
3938 // output t_TE both before and after outputting t_te.
3939 if (*T_TE != NUL && (kitty_protocol_state == KKPS_ENABLED
3940 || kitty_protocol_state == KKPS_DISABLED))
3941 out_str_t_TE(); // probably disables the kitty keyboard
3942 // protocol
3943
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00003944 out_str(T_TE); // stop termcap mode
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003945 cursor_on(); // just in case it is still off
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003946 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
3947 // Kitty keyboard protocol
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003948 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 out_flush();
3950 }
3951}
3952
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003953#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954/*
3955 * Request version string (for xterm) when needed.
3956 * Only do this after switching to raw mode, otherwise the result will be
3957 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003958 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003959 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 * Only do this after termcap mode has been started, otherwise the codes for
3961 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003962 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3963 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003964 * On Unix only do it when both output and input are a tty (avoid writing
3965 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 * The result is caught in check_termcode().
3967 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003968 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003969may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003971 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003972 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003973 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 && *T_CRV != NUL)
3975 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003976 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003977 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003979 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003980 // check for the characters now, otherwise they might be eaten by
3981 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 out_flush();
3983 (void)vpeekc_nomap();
3984 }
3985}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003986
Bram Moolenaar9584b312013-03-13 19:29:28 +01003987/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003988 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3989 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003990 * Note that this goes out before T_CRV, so that the result can be used when
3991 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003992 */
3993 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003994check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003995{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003996 int did_send = FALSE;
3997
3998 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3999 return;
4000
Bram Moolenaarafd78262019-05-10 23:10:31 +02004001 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01004002 && !option_was_set((char_u *)"ambiwidth"))
4003 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004004 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01004005
Bram Moolenaara45551a2020-06-09 15:57:37 +02004006 // Ambiguous width check.
4007 // Check how the terminal treats ambiguous character width (UAX #11).
4008 // First, we move the cursor to (1, 0) and print a test ambiguous
4009 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
4010 // the current cursor position. If the terminal treats \u25bd as
4011 // single width, the position is (1, 1), or if it is treated as double
4012 // width, that will be (1, 2). This function has the side effect that
4013 // changes cursor position, so it must be called immediately after
4014 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004015 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004016 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004017 // Do this in the second row. In the first row the returned sequence
4018 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004019 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004020 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02004021 out_str(buf);
4022 out_str(T_U7);
4023 termrequest_sent(&u7_status);
4024 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02004025 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02004026
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004027 // This overwrites a few characters on the screen, a redraw is needed
4028 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02004029 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02004030 term_windgoto(1, 0);
4031 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004032 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004033 }
4034
Bram Moolenaard1f34e62022-01-07 19:24:20 +00004035 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02004036 {
4037 // 2. Check compatibility with xterm.
4038 // We move the cursor to (2, 0), print a test sequence and then query
4039 // the current cursor position. If the terminal properly handles
4040 // unknown DCS string and CSI sequence with intermediate byte, the test
4041 // sequence is ignored and the cursor does not move. If the terminal
4042 // handles test sequence incorrectly, a garbage string is displayed and
4043 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004044 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004045 LOG_TR(("Sending xterm compatibility test sequence."));
4046 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004047 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02004048 term_windgoto(2, 0);
4049 // send the test DCS string.
4050 out_str((char_u *)"\033Pzz\033\\");
4051 // send the test CSI sequence with intermediate byte.
4052 out_str((char_u *)"\033[0%m");
4053 out_str(T_U7);
4054 termrequest_sent(&xcc_status);
4055 out_flush();
4056 did_send = TRUE;
4057
4058 // If the terminal handles test sequence incorrectly, garbage text is
4059 // displayed. Clear them out for now.
4060 screen_stop_highlight();
4061 term_windgoto(2, 0);
4062 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004063 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004064 }
4065
4066 if (did_send)
4067 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004068 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02004069
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004070 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004071 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01004072
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004073 // check for the characters now, otherwise they might be eaten by
4074 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02004075 out_flush();
4076 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01004077 }
4078}
Bram Moolenaar2951b772013-07-03 12:45:31 +02004079
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004080/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02004081 * Similar to requesting the version string: Request the terminal background
4082 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004083 */
4084 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004085may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004086{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004087 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004088 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004089 int didit = FALSE;
4090
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004091# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004092 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004093 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004094 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004095 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004096 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004097 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004098 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004099 didit = TRUE;
4100 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004101# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004102
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004103 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004104 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004105 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004106 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004107 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004108 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004109 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004110 didit = TRUE;
4111 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004112
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004113 if (didit)
4114 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004115 // check for the characters now, otherwise they might be eaten by
4116 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004117 out_flush();
4118 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004119 }
4120 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004121}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004122
Bram Moolenaar2951b772013-07-03 12:45:31 +02004123# ifdef DEBUG_TERMRESPONSE
4124 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02004125log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004126{
4127 static FILE *fd_tr = NULL;
4128 static proftime_T start;
4129 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004130 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004131
4132 if (fd_tr == NULL)
4133 {
4134 fd_tr = fopen("termresponse.log", "w");
4135 profile_start(&start);
4136 }
4137 now = start;
4138 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02004139 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004140 must_redraw == UPD_NOT_VALID ? "NV"
4141 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02004142 va_start(ap, fmt);
4143 vfprintf(fd_tr, fmt, ap);
4144 va_end(ap);
4145 fputc('\n', fd_tr);
4146 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02004147}
4148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149#endif
4150
4151/*
4152 * Return TRUE when saving and restoring the screen.
4153 */
4154 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004155swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156{
4157 return (full_screen && *T_TI != NUL);
4158}
4159
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160/*
4161 * By outputting the 'cursor very visible' termcap code, for some windowed
4162 * terminals this makes the screen scrolled to the correct position.
4163 * Used when starting Vim or returning from a shell.
4164 */
4165 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004166scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004168 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004170 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004172 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004173 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 }
4175}
4176
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004177// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178static int cursor_is_off = FALSE;
4179
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004180// True if cursor is not visible due to an ongoing cursor-less sleep
4181static int cursor_is_asleep = FALSE;
4182
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004184 * Enable the cursor without checking if it's already enabled.
4185 */
4186 void
4187cursor_on_force(void)
4188{
4189 out_str(T_VE);
4190 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004191 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004192}
4193
4194/*
4195 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 */
4197 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004198cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004200 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004201 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202}
4203
4204/*
4205 * Disable the cursor.
4206 */
4207 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004208cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004210 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004212 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 cursor_is_off = TRUE;
4214 }
4215}
4216
Dominique Pelle748b3082022-01-08 12:41:16 +00004217#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004218/*
4219 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4220 */
4221 int
4222cursor_is_sleeping(void)
4223{
4224 return cursor_is_asleep;
4225}
Dominique Pelle748b3082022-01-08 12:41:16 +00004226#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004227
4228/*
4229 * Disable the cursor and mark it disabled by cursor-less sleep
4230 */
4231 void
4232cursor_sleep(void)
4233{
4234 cursor_is_asleep = TRUE;
4235 cursor_off();
4236}
4237
4238/*
4239 * Enable the cursor and mark it not disabled by cursor-less sleep
4240 */
4241 void
4242cursor_unsleep(void)
4243{
4244 cursor_is_asleep = FALSE;
4245 cursor_on();
4246}
4247
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004248#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004250 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004251 */
4252 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004253term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004254{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004255 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004256 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004257
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004258 // Only do something when redrawing the screen and we can restore the
4259 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004260 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004261 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004262# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004263 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004264 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004265 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004266# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004267 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004268 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004269
Bram Moolenaar24959102022-05-07 20:01:16 +01004270 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004271 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004272 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004273 {
4274 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004275 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004276 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004277 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004278 if (*p != NUL)
4279 {
4280 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004281 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004282 }
4283 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004284 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004285 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004286 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004287 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004288 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004289 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004290 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004291 }
4292 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004293 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004294 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004295 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004296 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004297 }
4298}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004299
4300# if defined(FEAT_TERMINAL) || defined(PROTO)
4301 void
4302term_cursor_color(char_u *color)
4303{
4304 if (*T_CSC != NUL)
4305 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004306 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004307 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004308 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004309 out_flush();
4310 }
4311}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004312# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004313
Bram Moolenaar4db25542017-08-28 22:43:05 +02004314 int
4315blink_state_is_inverted()
4316{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004317#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004318 return rbm_status.tr_progress == STATUS_GOT
4319 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004320 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004321#else
4322 return FALSE;
4323#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004324}
4325
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004326/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004327 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004328 */
4329 void
4330term_cursor_shape(int shape, int blink)
4331{
4332 if (*T_CSH != NUL)
4333 {
4334 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4335 out_flush();
4336 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004337 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004338 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004339 int do_blink = blink;
4340
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004341 // t_SH is empty: try setting just the blink state.
4342 // The blink flags are XORed together, if the initial blinking from
4343 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004344 if (blink_state_is_inverted())
4345 do_blink = !blink;
4346
4347 if (do_blink && *T_VS != NUL)
4348 {
4349 out_str(T_VS);
4350 out_flush();
4351 }
4352 else if (!do_blink && *T_CVS != NUL)
4353 {
4354 out_str(T_CVS);
4355 out_flush();
4356 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004357 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004358}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004359#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004360
4361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 * Set scrolling region for window 'wp'.
4363 * The region starts 'off' lines from the start of the window.
4364 * Also set the vertical scroll region for a vertically split window. Always
4365 * the full width of the window, excluding the vertical separator.
4366 */
4367 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004368scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369{
4370 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4371 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004373 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4374 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004375 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376}
4377
4378/*
4379 * Reset scrolling region to the whole screen.
4380 */
4381 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004382scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
4384 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 if (*T_CSV != NUL)
4386 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004387 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388}
4389
4390
4391/*
4392 * List of terminal codes that are currently recognized.
4393 */
4394
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004395static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004397 char_u name[2]; // termcap name of entry
4398 char_u *code; // terminal code (in allocated memory)
4399 int len; // STRLEN(code)
4400 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401} *termcodes = NULL;
4402
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004403static int tc_max_len = 0; // number of entries that termcodes[] can hold
4404static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004406static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004407
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004409clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410{
4411 while (tc_len > 0)
4412 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004413 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 tc_max_len = 0;
4415
4416#ifdef HAVE_TGETENT
4417 BC = (char *)empty_option;
4418 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004419 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 ospeed = 0;
4421#endif
4422
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004423 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424}
4425
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004426#define ATC_FROM_TERM 55
4427
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428/*
Bram Moolenaarb2646172022-12-17 15:35:43 +00004429 * Add a new entry for "name[2]" to the list of terminal codes.
4430 * Note that "name" may not have a terminating NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004432 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4433 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 */
4435 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004436add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437{
4438 struct termcode *new_tc;
4439 int i, j;
4440 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004441 int len;
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004442#ifdef FEAT_EVAL
4443 char *action = "Setting";
4444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
4446 if (string == NULL || *string == NUL)
4447 {
4448 del_termcode(name);
4449 return;
4450 }
4451
Bram Moolenaar4f974752019-02-17 17:44:42 +01004452#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004453 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004454#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004455# ifdef VIMDLL
4456 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004457 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004458 else
4459# endif
4460 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 if (s == NULL)
4463 return;
4464
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004465 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004466 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004468 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 s[0] = term_7to8bit(string);
4470 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004471
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004472#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4473# ifdef VIMDLL
4474 if (!gui.in_use)
4475# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004476 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004477 if (s[0] == K_NUL)
4478 {
4479 STRMOVE(s + 1, s);
4480 s[1] = 3;
4481 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004482 }
4483#endif
4484
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004485 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004487 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
4489 /*
4490 * need to make space for more entries
4491 */
4492 if (tc_len == tc_max_len)
4493 {
4494 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004495 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 if (new_tc == NULL)
4497 {
4498 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004499 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 return;
4501 }
4502 for (i = 0; i < tc_len; ++i)
4503 new_tc[i] = termcodes[i];
4504 vim_free(termcodes);
4505 termcodes = new_tc;
4506 }
4507
4508 /*
4509 * Look for existing entry with the same name, it is replaced.
4510 * Look for an existing entry that is alphabetical higher, the new entry
4511 * is inserted in front of it.
4512 */
4513 for (i = 0; i < tc_len; ++i)
4514 {
4515 if (termcodes[i].name[0] < name[0])
4516 continue;
4517 if (termcodes[i].name[0] == name[0])
4518 {
4519 if (termcodes[i].name[1] < name[1])
4520 continue;
4521 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004522 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 */
4524 if (termcodes[i].name[1] == name[1])
4525 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004526 if (flags == ATC_FROM_TERM && (j = termcode_star(
4527 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004528 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004529 // Don't replace ESC[123;*X or ESC O*X with another when
4530 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004531 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004532 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004533 && s[len - 1]
4534 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004535 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004536 // They are equal but for the ";*": don't add it.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004537#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004538 ch_log(NULL, "Termcap entry %c%c did not change",
4539 name[0], name[1]);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004540#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004541 vim_free(s);
4542 return;
4543 }
4544 }
4545 else
4546 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004547 // Replace old code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004548#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004549 ch_log(NULL, "Termcap entry %c%c was: %s",
4550 name[0], name[1], termcodes[i].code);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004551#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004552 vim_free(termcodes[i].code);
4553 --tc_len;
4554 break;
4555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 }
4557 }
4558 /*
4559 * Found alphabetical larger entry, move rest to insert new entry
4560 */
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004561#ifdef FEAT_EVAL
4562 action = "Adding";
4563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 for (j = tc_len; j > i; --j)
4565 termcodes[j] = termcodes[j - 1];
4566 break;
4567 }
4568
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004569#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004570 ch_log(NULL, "%s termcap entry %c%c to %s", action, name[0], name[1], s);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 termcodes[i].name[0] = name[0];
4573 termcodes[i].name[1] = name[1];
4574 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004575 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004576
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004577 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4578 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004579 termcodes[i].modlen = 0;
4580 j = termcode_star(s, len);
4581 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004582 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004583 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004584 // For "CSI[@;X" the "@" is not included in "modlen".
4585 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4586 --termcodes[i].modlen;
4587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 ++tc_len;
4589}
4590
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004591/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004592 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004593 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004594 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004595 */
4596 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004597termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004598{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004599 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004600 if (len >= 3 && code[len - 2] == '*')
4601 {
4602 if (len >= 5 && code[len - 3] == ';')
4603 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004604 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004605 return 1;
4606 }
4607 return 0;
4608}
4609
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004611find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612{
4613 int i;
4614
4615 for (i = 0; i < tc_len; ++i)
4616 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4617 return termcodes[i].code;
4618 return NULL;
4619}
4620
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004622get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623{
4624 if (i >= tc_len)
4625 return NULL;
4626 return &termcodes[i].name[0];
4627}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004629/*
4630 * Returns the length of the terminal code at index 'idx'.
4631 */
4632 int
4633get_termcode_len(int idx)
4634{
4635 return termcodes[idx].len;
4636}
4637
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004638 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004639del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 int i;
4642
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004643 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 return;
4645
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004646 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647
4648 for (i = 0; i < tc_len; ++i)
4649 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4650 {
4651 del_termcode_idx(i);
4652 return;
4653 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004654 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655}
4656
4657 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004658del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659{
4660 int i;
4661
4662 vim_free(termcodes[idx].code);
4663 --tc_len;
4664 for (i = idx; i < tc_len; ++i)
4665 termcodes[i] = termcodes[i + 1];
4666}
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668/*
4669 * Called when detected that the terminal sends 8-bit codes.
4670 * Convert all 7-bit codes to their 8-bit equivalent.
4671 */
4672 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004673switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674{
4675 int i;
4676 int c;
4677
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004678 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if (!term_is_8bit(T_NAME))
4680 {
4681 for (i = 0; i < tc_len; ++i)
4682 {
4683 c = term_7to8bit(termcodes[i].code);
4684 if (c != 0)
4685 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004686 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 termcodes[i].code[0] = c;
4688 }
4689 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004690 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 }
4692 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004693 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
4696#ifdef CHECK_DOUBLE_CLICK
4697static linenr_T orig_topline = 0;
4698# ifdef FEAT_DIFF
4699static int orig_topfill = 0;
4700# endif
4701#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004702#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004704 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 * "orig_topline" is used to avoid detecting a double-click when the window
4706 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4707 */
4708/*
4709 * Set orig_topline. Used when jumping to another window, so that a double
4710 * click still works.
4711 */
4712 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004713set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 orig_topline = wp->w_topline;
4716# ifdef FEAT_DIFF
4717 orig_topfill = wp->w_topfill;
4718# endif
4719}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004720
4721/*
4722 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4723 * topline and topfill.
4724 */
4725 int
4726is_mouse_topline(win_T *wp)
4727{
4728 return orig_topline == wp->w_topline
4729#ifdef FEAT_DIFF
4730 && orig_topfill == wp->w_topfill
4731#endif
4732 ;
4733}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734#endif
4735
4736/*
zeertzjqfc78a032022-04-26 22:11:38 +01004737 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4738 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4739 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004740 * Remove "slen" bytes.
4741 * Returns FAIL for error.
4742 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004743 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004744put_string_in_typebuf(
4745 int offset,
4746 int slen,
4747 char_u *string,
4748 int new_slen,
4749 char_u *buf,
4750 int bufsize,
4751 int *buflen)
4752{
4753 int extra = new_slen - slen;
4754
4755 string[new_slen] = NUL;
4756 if (buf == NULL)
4757 {
4758 if (extra < 0)
4759 // remove matched chars, taking care of noremap
4760 del_typebuf(-extra, offset);
4761 else if (extra > 0)
4762 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004763 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4764 == FAIL)
4765 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004766
4767 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4768 // typebuf.tb_buf[]!
4769 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4770 (size_t)new_slen);
4771 }
4772 else
4773 {
4774 if (extra < 0)
4775 // remove matched characters
4776 mch_memmove(buf + offset, buf + offset - extra,
4777 (size_t)(*buflen + offset + extra));
4778 else if (extra > 0)
4779 {
4780 // Insert the extra space we need. If there is insufficient
4781 // space return -1.
4782 if (*buflen + extra + new_slen >= bufsize)
4783 return FAIL;
4784 mch_memmove(buf + offset + extra, buf + offset,
4785 (size_t)(*buflen - offset));
4786 }
4787 mch_memmove(buf + offset, string, (size_t)new_slen);
4788 *buflen = *buflen + extra + new_slen;
4789 }
4790 return OK;
4791}
4792
4793/*
4794 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4795 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004796 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004797decode_modifiers(int n)
4798{
4799 int code = n - 1;
4800 int modifiers = 0;
4801
4802 if (code & 1)
4803 modifiers |= MOD_MASK_SHIFT;
4804 if (code & 2)
4805 modifiers |= MOD_MASK_ALT;
4806 if (code & 4)
4807 modifiers |= MOD_MASK_CTRL;
4808 if (code & 8)
4809 modifiers |= MOD_MASK_META;
Bram Moolenaar064fd672022-11-29 18:32:32 +00004810 // Any further modifiers are silently dropped.
4811
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004812 return modifiers;
4813}
4814
4815 static int
4816modifiers2keycode(int modifiers, int *key, char_u *string)
4817{
4818 int new_slen = 0;
4819
4820 if (modifiers != 0)
4821 {
4822 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004823 // make mappings work. This may result in a special key, such as
4824 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004825 *key = simplify_key(*key, &modifiers);
4826 if (modifiers != 0)
4827 {
4828 string[new_slen++] = K_SPECIAL;
4829 string[new_slen++] = (int)KS_MODIFIER;
4830 string[new_slen++] = modifiers;
4831 }
4832 }
4833 return new_slen;
4834}
4835
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004836/*
4837 * Handle a cursor position report.
4838 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004839 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004840handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004841{
4842 if (arg[0] == 2 && arg[1] >= 2)
4843 {
4844 char *aw = NULL;
4845
4846 LOG_TR(("Received U7 status: %s", tp));
4847 u7_status.tr_progress = STATUS_GOT;
4848 did_cursorhold = TRUE;
4849 if (arg[1] == 2)
4850 aw = "single";
4851 else if (arg[1] == 3)
4852 aw = "double";
4853 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4854 {
4855 // Setting the option causes a screen redraw. Do
4856 // that right away if possible, keeping any
4857 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004858 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004859#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004860 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004861 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004862
4863 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4864 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004865#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004866 redraw_asap(UPD_CLEAR);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004867#endif
4868#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004869 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004870#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004871 }
4872 }
4873 else if (arg[0] == 3)
4874 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004875 int value;
4876
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004877 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004878 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004879
4880 // Third row: xterm compatibility test.
4881 // If the cursor is on the first column then the terminal can handle
4882 // the request for cursor style and blinking.
4883 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4884 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4885 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004886 }
4887}
4888
4889/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004890 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004891 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004892 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004893 */
4894 static void
4895handle_version_response(int first, int *arg, int argc, char_u *tp)
4896{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004897 // The xterm version. It is set to zero when it can't be an actual xterm
4898 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004899 int version = arg[1];
4900
4901 LOG_TR(("Received CRV response: %s", tp));
4902 crv_status.tr_progress = STATUS_GOT;
4903 did_cursorhold = TRUE;
4904
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004905 // Reset terminal properties that are set based on the termresponse.
4906 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004907 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004908 init_term_props(
4909#ifdef FEAT_EVAL
4910 reset_term_props_on_termresponse
4911#else
4912 FALSE
4913#endif
4914 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004915
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004916 // If this code starts with CSI, you can bet that the
4917 // terminal uses 8-bit codes.
4918 if (tp[0] == CSI)
4919 switch_to_8bit();
4920
4921 // Screen sends 40500.
4922 // rxvt sends its version number: "20703" is 2.7.3.
4923 // Ignore it for when the user has set 'term' to xterm,
4924 // even though it's an rxvt.
4925 if (version > 20000)
4926 version = 0;
4927
zeertzjqfc78a032022-04-26 22:11:38 +01004928 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004929 if (first == '>' && argc == 3)
4930 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004931 // mintty 2.9.5 sends 77;20905;0c.
4932 // (77 is ASCII 'M' for mintty.)
4933 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004934 {
4935 // mintty can do SGR mouse reporting
4936 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4937 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004938
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004939#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004940 // If xterm version >= 141 try to get termcap codes. For other
4941 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004942 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004943 {
4944 LOG_TR(("Enable checking for XT codes"));
4945 check_for_codes = TRUE;
4946 need_gather = TRUE;
4947 req_codes_from_term();
4948 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004949#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004950
4951 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01004952 // Konsole sends 0;115;0 and works the same way
4953 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004954 {
4955 // If run from Vim $COLORS is set to the number of
4956 // colors the terminal supports. Otherwise assume
4957 // 256, libvterm supports even more.
4958 if (mch_getenv((char_u *)"COLORS") == NULL)
4959 may_adjust_color_count(256);
4960 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004961 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004962 }
4963
4964 if (version == 95)
4965 {
4966 // Mac Terminal.app sends 1;95;0
4967 if (arg[0] == 1 && arg[2] == 0)
4968 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004969 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4970 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004971 }
4972 // iTerm2 sends 0;95;0
4973 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004974 {
4975 // iTerm2 can do SGR mouse reporting
4976 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4977 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004978 // old iTerm2 sends 0;95;
4979 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004980 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004981 }
4982
4983 // screen sends 83;40500;0 83 is 'S' in ASCII.
4984 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004985 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004986 // screen supports SGR mouse codes since 4.7.0
4987 if (arg[1] >= 40700)
4988 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4989 else
4990 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4991 }
4992
4993 // If no recognized terminal has set mouse behavior, assume xterm.
4994 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4995 {
4996 // Xterm version 277 supports SGR.
4997 // Xterm version >= 95 supports mouse dragging.
4998 if (version >= 277)
4999 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005000 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005001 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005002 }
5003
5004 // Detect terminals that set $TERM to something like
5005 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005006 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005007 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02005008 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005009 // xfce4-terminal sends 1;2802;0.
5010 // screen sends 83;40500;0
5011 // Assuming any version number over 2500 is not an
5012 // xterm (without the limit for rxvt and screen).
5013 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005014 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005015
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005016 else if (version == 136 && arg[2] == 0)
5017 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005018 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005019
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005020 // PuTTY sends 0;136;0
5021 if (arg[0] == 0)
5022 {
5023 // supports sgr-like mouse reporting.
5024 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5025 }
5026 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005027 }
5028
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005029 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
5030 // commented out.
5031 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
5032 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005033
Bram Moolenaar1573e732022-11-16 20:33:21 +00005034 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005035 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
5036 {
5037 term_props[TPR_KITTY].tpr_status = TPR_YES;
5038 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
Bram Moolenaar731d0072022-12-18 17:47:18 +00005039
5040 // Kitty can handle SGR mouse reporting.
5041 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005042 }
5043
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005044 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005045 // 30600/40500 is a version number of GNU screen. DA2 support is added
5046 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
5047 // compatibility checking does not detect GNU screen.
5048 if (arg[0] == 83 && arg[1] >= 30600)
5049 {
5050 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5051 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
5052 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005053
5054 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005055 // 95, so assume anything below 95 is not xterm and hopefully supports
5056 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005057 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005058 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005059
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005060 // Getting the cursor style is only supported properly by xterm since
5061 // version 279 (otherwise it returns 0x18).
5062 if (version < 279)
5063 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5064
5065 /*
5066 * Take action on the detected properties.
5067 */
5068
5069 // Unless the underline RGB color is expected to work, disable "t_8u".
5070 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005071 // This may cause some flicker. Alternative would be to set "t_8u"
5072 // here if the terminal is expected to support it, but that might
5073 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01005074 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
5075 && *T_8U != NUL
5076 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005077 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02005078 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
5079 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005080 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005081#ifdef FEAT_TERMRESPONSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01005082 if (*T_8U != NUL && write_t_8u_state == MAYBE)
5083 // Did skip writing t_8u, a complete redraw is needed.
5084 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01005085 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005086#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005087
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005088 // Only set 'ttymouse' automatically if it was not set
5089 // by the user already.
5090 if (!option_was_set((char_u *)"ttym")
5091 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
5092 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
5093 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005094 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005095 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
5096 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
5097 }
5098
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005099#ifdef FEAT_TERMRESPONSE
5100 int need_flush = FALSE;
5101
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005102 // Only request the cursor style if t_SH and t_RS are
5103 // set. Only supported properly by xterm since version
5104 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005105 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005106 // Not for Terminal.app, it can't handle t_RS, it
5107 // echoes the characters to the screen.
5108 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005109 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005110 && *T_CSH != NUL
5111 && *T_CRS != NUL)
5112 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005113 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005114 LOG_TR(("Sending cursor style request"));
5115 out_str(T_CRS);
5116 termrequest_sent(&rcs_status);
5117 need_flush = TRUE;
5118 }
5119
5120 // Only request the cursor blink mode if t_RC set. Not
5121 // for Gnome terminal, it can't handle t_RC, it
5122 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005123 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005124 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005125 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005126 && *T_CRC != NUL)
5127 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005128 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005129 LOG_TR(("Sending cursor blink mode request"));
5130 out_str(T_CRC);
5131 termrequest_sent(&rbm_status);
5132 need_flush = TRUE;
5133 }
5134
5135 if (need_flush)
5136 out_flush();
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005137#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005138 }
5139}
5140
5141/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005142 * Add "key" to "buf" and return the number of bytes used.
5143 * Handles special keys and multi-byte characters.
5144 */
5145 static int
5146add_key_to_buf(int key, char_u *buf)
5147{
5148 int idx = 0;
5149
5150 if (IS_SPECIAL(key))
5151 {
5152 buf[idx++] = K_SPECIAL;
5153 buf[idx++] = KEY2TERMCAP0(key);
5154 buf[idx++] = KEY2TERMCAP1(key);
5155 }
5156 else if (has_mbyte)
5157 idx += (*mb_char2bytes)(key, buf + idx);
5158 else
5159 buf[idx++] = key;
5160 return idx;
5161}
5162
5163/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005164 * Shared between handle_key_with_modifier() and handle_csi_function_key().
5165 */
5166 static int
5167put_key_modifiers_in_typebuf(
5168 int key_arg,
5169 int modifiers_arg,
5170 int csi_len,
5171 int offset,
5172 char_u *buf,
5173 int bufsize,
5174 int *buflen)
5175{
5176 int key = key_arg;
5177 int modifiers = modifiers_arg;
5178
5179 // Some keys need adjustment when the Ctrl modifier is used.
5180 key = may_adjust_key_for_ctrl(modifiers, key);
5181
5182 // May remove the shift modifier if it's already included in the key.
5183 modifiers = may_remove_shift_modifier(modifiers, key);
5184
5185 // Produce modifiers with K_SPECIAL KS_MODIFIER {mod}
5186 char_u string[MAX_KEY_CODE_LEN + 1];
5187 int new_slen = modifiers2keycode(modifiers, &key, string);
5188
5189 // Add the bytes for the key.
5190 new_slen += add_key_to_buf(key, string + new_slen);
5191
5192 string[new_slen] = NUL;
5193 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5194 buf, bufsize, buflen) == FAIL)
5195 return -1;
5196 return new_slen - csi_len + offset;
5197}
5198
5199/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005200 * Handle a sequence with key and modifier, one of:
5201 * {lead}27;{modifier};{key}~
5202 * {lead}{key};{modifier}u
5203 * Returns the difference in length.
5204 */
5205 static int
5206handle_key_with_modifier(
5207 int *arg,
5208 int trail,
5209 int csi_len,
5210 int offset,
5211 char_u *buf,
5212 int bufsize,
5213 int *buflen)
5214{
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005215 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5216 // it for terminals using the kitty keyboard protocol. Xterm sends
5217 // the form ending in "u" when the formatOtherKeys resource is set. We do
5218 // not support this.
5219 //
5220 // Do not set seenModifyOtherKeys if there was a positive response at any
5221 // time from requesting the kitty keyboard protocol state, these are not
5222 // expected to support modifyOtherKeys level 2.
5223 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005224 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5225 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005226 if (trail != 'u'
5227 && (kitty_protocol_state == KKPS_INITIAL
5228 || kitty_protocol_state == KKPS_OFF
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00005229 || kitty_protocol_state == KKPS_AFTER_T_TE)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005230 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar1a173402022-12-02 12:28:47 +00005231 {
Bram Moolenaar5390c052022-12-02 13:10:03 +00005232#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005233 ch_log(NULL, "setting seenModifyOtherKeys to TRUE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005234#endif
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005235 seenModifyOtherKeys = TRUE;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005236 }
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005237
Bram Moolenaar1a173402022-12-02 12:28:47 +00005238 int key = trail == 'u' ? arg[0] : arg[2];
5239 int modifiers = decode_modifiers(arg[1]);
5240 return put_key_modifiers_in_typebuf(key, modifiers,
5241 csi_len, offset, buf, bufsize, buflen);
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005242}
5243
5244/*
5245 * Handle a sequence with key without a modifier:
5246 * {lead}{key}u
5247 * Returns the difference in length.
5248 */
5249 static int
5250handle_key_without_modifier(
5251 int *arg,
5252 int csi_len,
5253 int offset,
5254 char_u *buf,
5255 int bufsize,
5256 int *buflen)
5257{
5258 char_u string[MAX_KEY_CODE_LEN + 1];
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00005259 int new_slen;
5260
5261 if (arg[0] == ESC)
5262 {
5263 // Putting Esc in the buffer creates ambiguity, it can be the start of
5264 // an escape sequence. Use K_ESC to avoid that.
5265 string[0] = K_SPECIAL;
5266 string[1] = KS_EXTRA;
5267 string[2] = KE_ESC;
5268 new_slen = 3;
5269 }
5270 else
5271 new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005272
5273 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5274 buf, bufsize, buflen) == FAIL)
5275 return -1;
5276 return new_slen - csi_len + offset;
5277}
5278
5279/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005280 * CSI function key without or with modifiers:
5281 * {lead}[ABCDEFHPQRS]
5282 * {lead}1;{modifier}[ABCDEFHPQRS]
5283 * Returns zero when nog recognized, a positive number when recognized.
5284 */
5285 static int
5286handle_csi_function_key(
5287 int argc,
5288 int *arg,
5289 int trail,
5290 int csi_len,
5291 char_u *key_name,
5292 int offset,
5293 char_u *buf,
5294 int bufsize,
5295 int *buflen)
5296{
5297 key_name[0] = 'k';
5298 switch (trail)
5299 {
5300 case 'A': key_name[1] = 'u'; break; // K_UP
5301 case 'B': key_name[1] = 'd'; break; // K_DOWN
5302 case 'C': key_name[1] = 'r'; break; // K_RIGHT
5303 case 'D': key_name[1] = 'l'; break; // K_LEFT
5304
5305 // case 'E': keypad BEGIN - not supported
5306 case 'F': key_name[0] = '@'; key_name[1] = '7'; break; // K_END
5307 case 'H': key_name[1] = 'h'; break; // K_HOME
5308
5309 case 'P': key_name[1] = '1'; break; // K_F1
5310 case 'Q': key_name[1] = '2'; break; // K_F2
5311 case 'R': key_name[1] = '3'; break; // K_F3
5312 case 'S': key_name[1] = '4'; break; // K_F4
5313
5314 default: return 0; // not recognized
5315 }
5316
5317 int key = TERMCAP2KEY(key_name[0], key_name[1]);
5318 int modifiers = argc == 2 ? decode_modifiers(arg[1]) : 0;
5319 put_key_modifiers_in_typebuf(key, modifiers,
5320 csi_len, offset, buf, bufsize, buflen);
5321 return csi_len;
5322}
5323
5324/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005325 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005326 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005327 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005328 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5329 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005330 * - Cursor position report: {lead}{row};{col}R
5331 * The final byte must be 'R'. It is used for checking the
5332 * ambiguous-width character state.
5333 *
5334 * - window position reply: {lead}3;{x};{y}t
5335 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005336 * - key with modifiers when modifyOtherKeys is enabled or the Kitty keyboard
5337 * protocol is used:
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005338 * {lead}27;{modifier};{key}~
5339 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005340 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005341 * - function key with or without modifiers:
5342 * {lead}[ABCDEFHPQRS]
5343 * {lead}1;{modifier}[ABCDEFHPQRS]
5344 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005345 * Return 0 for no match, -1 for partial match, > 0 for full match.
5346 */
5347 static int
5348handle_csi(
5349 char_u *tp,
5350 int len,
5351 char_u *argp,
5352 int offset,
5353 char_u *buf,
5354 int bufsize,
5355 int *buflen,
5356 char_u *key_name,
5357 int *slen)
5358{
5359 int first = -1; // optional char right after {lead}
5360 int trail; // char that ends CSI sequence
5361 int arg[3] = {-1, -1, -1}; // argument numbers
Bram Moolenaar1a173402022-12-02 12:28:47 +00005362 int argc = 0; // number of arguments
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005363 char_u *ap = argp;
5364 int csi_len;
5365
5366 // Check for non-digit after CSI.
5367 if (!VIM_ISDIGIT(*ap))
5368 first = *ap++;
5369
Bram Moolenaar8ffb7e02022-12-03 10:13:30 +00005370 if (first >= 'A' && first <= 'Z')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005371 {
Bram Moolenaar1a173402022-12-02 12:28:47 +00005372 // If "first" is in [ABCDEFHPQRS] then it is actually the "trail" and
5373 // no argument follows.
5374 trail = first;
5375 first = -1;
5376 --ap;
5377 }
5378 else
5379 {
5380 // Find up to three argument numbers.
5381 for (argc = 0; argc < 3; )
5382 {
5383 if (ap >= tp + len)
5384 return -1;
5385 if (*ap == ';')
5386 arg[argc++] = -1; // omitted number
5387 else if (VIM_ISDIGIT(*ap))
5388 {
5389 arg[argc] = 0;
5390 for (;;)
5391 {
5392 if (ap >= tp + len)
5393 return -1;
5394 if (!VIM_ISDIGIT(*ap))
5395 break;
5396 arg[argc] = arg[argc] * 10 + (*ap - '0');
5397 ++ap;
5398 }
5399 ++argc;
5400 }
5401 if (*ap == ';')
5402 ++ap;
5403 else
5404 break;
5405 }
5406
5407 // mrxvt has been reported to have "+" in the version. Assume
5408 // the escape sequence ends with a letter or one of "{|}~".
5409 while (ap < tp + len
5410 && !(*ap >= '{' && *ap <= '~')
5411 && !ASCII_ISALPHA(*ap))
5412 ++ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005413 if (ap >= tp + len)
5414 return -1;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005415 trail = *ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005416 }
5417
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005418 csi_len = (int)(ap - tp) + 1;
5419
Bram Moolenaarc255b782022-11-26 19:16:48 +00005420 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5421 // modifyOtherKeys level. Drop similar responses.
5422 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5423 {
5424 if (arg[0] == 4 && argc == 2)
5425 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5426
5427 key_name[0] = (int)KS_EXTRA;
5428 key_name[1] = (int)KE_IGNORE;
5429 *slen = csi_len;
5430 }
5431
Bram Moolenaar1a173402022-12-02 12:28:47 +00005432 // Function key starting with CSI:
5433 // {lead}[ABCDEFHPQRS]
5434 // {lead}1;{modifier}[ABCDEFHPQRS]
5435 else if (first == -1 && ASCII_ISUPPER(trail)
5436 && (argc == 0 || (argc == 2 && arg[0] == 1)))
5437 {
5438 int res = handle_csi_function_key(argc, arg, trail,
5439 csi_len, key_name, offset, buf, bufsize, buflen);
5440 return res <= 0 ? res : len + res;
5441 }
5442
5443 // Cursor position report: {lead}{row};{col}R
5444 // Eat it when there are 2 arguments and it ends in 'R'.
5445 // Also when u7_status is not "sent", it may be from a previous Vim that
5446 // just exited. But not for <S-F3>, it sends something similar, check for
5447 // row and column to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005448 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005449 {
5450 handle_u7_response(arg, tp, csi_len);
5451
5452 key_name[0] = (int)KS_EXTRA;
5453 key_name[1] = (int)KE_IGNORE;
5454 *slen = csi_len;
5455 }
5456
5457 // Version string: Eat it when there is at least one digit and
5458 // it ends in 'c'
5459 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5460 {
5461 handle_version_response(first, arg, argc, tp);
5462
5463 *slen = csi_len;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005464#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005465 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005466#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005467 apply_autocmds(EVENT_TERMRESPONSE,
5468 NULL, NULL, FALSE, curbuf);
5469 key_name[0] = (int)KS_EXTRA;
5470 key_name[1] = (int)KE_IGNORE;
5471 }
5472
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005473#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005474 // Check blinking cursor from xterm:
5475 // {lead}?12;1$y set
5476 // {lead}?12;2$y not set
5477 //
5478 // {lead} can be <Esc>[ or CSI
5479 else if (rbm_status.tr_progress == STATUS_SENT
5480 && first == '?'
5481 && ap == argp + 6
5482 && arg[0] == 12
5483 && ap[-1] == '$'
5484 && trail == 'y')
5485 {
5486 initial_cursor_blink = (arg[1] == '1');
5487 rbm_status.tr_progress = STATUS_GOT;
5488 LOG_TR(("Received cursor blinking mode response: %s", tp));
5489 key_name[0] = (int)KS_EXTRA;
5490 key_name[1] = (int)KE_IGNORE;
5491 *slen = csi_len;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005492# ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005493 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005494# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005495 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005496#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005497
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005498 // Kitty keyboard protocol status response: CSI ? flags u
5499 else if (first == '?' && argc == 1 && trail == 'u')
5500 {
5501 // The protocol has various "progressive enhancement flags" values, but
5502 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005503 if (arg[0] == '0')
5504 {
5505 kitty_protocol_state = KKPS_OFF;
5506 }
5507 else
5508 {
5509 kitty_protocol_state = KKPS_ENABLED;
5510
5511 // Reset seenModifyOtherKeys just in case some key combination has
5512 // been seen that set it before we get the status response.
Bram Moolenaar5390c052022-12-02 13:10:03 +00005513#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005514 ch_log(NULL, "setting seenModifyOtherKeys to FALSE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005515#endif
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005516 seenModifyOtherKeys = FALSE;
5517 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005518
5519 key_name[0] = (int)KS_EXTRA;
5520 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005521 *slen = csi_len;
5522 }
5523
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005524#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005525 // Check for a window position response from the terminal:
5526 // {lead}3;{x};{y}t
5527 else if (did_request_winpos && argc == 3 && arg[0] == 3
5528 && trail == 't')
5529 {
5530 winpos_x = arg[1];
5531 winpos_y = arg[2];
5532 // got finished code: consume it
5533 key_name[0] = (int)KS_EXTRA;
5534 key_name[1] = (int)KE_IGNORE;
5535 *slen = csi_len;
5536
5537 if (--did_request_winpos <= 0)
5538 winpos_status.tr_progress = STATUS_GOT;
5539 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005540#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005541
5542 // Key with modifier:
5543 // {lead}27;{modifier};{key}~
5544 // {lead}{key};{modifier}u
Bram Moolenaar064fd672022-11-29 18:32:32 +00005545 // Even though we only handle four modifiers and the {modifier} value
5546 // should be 16 or lower, we accept all modifier values to avoid the raw
5547 // sequence to be passed through.
5548 else if ((arg[0] == 27 && argc == 3 && trail == '~')
Bram Moolenaar7609c882022-10-19 20:07:09 +01005549 || (argc == 2 && trail == 'u'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005550 {
5551 return len + handle_key_with_modifier(arg, trail,
Bram Moolenaar064fd672022-11-29 18:32:32 +00005552 csi_len, offset, buf, bufsize, buflen);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005553 }
5554
Christopher Plewright03193062022-11-22 12:58:27 +00005555 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005556 // {lead}{key}u
5557 else if (argc == 1 && trail == 'u')
5558 {
5559 return len + handle_key_without_modifier(arg,
5560 csi_len, offset, buf, bufsize, buflen);
5561 }
5562
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005563 // else: Unknown CSI sequence. We could drop it, but then the
5564 // user can't create a map for it.
5565 return 0;
5566}
5567
5568/*
5569 * Handle an OSC sequence, fore/background color response from the terminal:
5570 *
5571 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5572 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5573 *
5574 * {code} is 10 for foreground, 11 for background
5575 * {lead} can be <Esc>] or OSC
5576 * {tail} can be '\007', <Esc>\ or STERM.
5577 *
5578 * Consume any code that starts with "{lead}11;", it's also
5579 * possible that "rgba" is following.
5580 */
5581 static int
5582handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5583{
5584 int i, j;
5585
5586 j = 1 + (tp[0] == ESC);
5587 if (len >= j + 3 && (argp[0] != '1'
5588 || (argp[1] != '1' && argp[1] != '0')
5589 || argp[2] != ';'))
5590 i = 0; // no match
5591 else
5592 for (i = j; i < len; ++i)
5593 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5594 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5595 {
5596 int is_bg = argp[1] == '1';
5597 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5598 && tp[j + 16] == '/';
5599
5600 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5601 && (is_4digit
5602 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5603 {
5604 char_u *tp_r = tp + j + 7;
5605 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5606 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005607#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005608 int rval, gval, bval;
5609
5610 rval = hexhex2nr(tp_r);
5611 gval = hexhex2nr(tp_b);
5612 bval = hexhex2nr(tp_g);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005613#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005614 if (is_bg)
5615 {
5616 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5617 *tp_b) ? "light" : "dark";
5618
5619 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005620#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005621 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005622# ifdef FEAT_TERMINAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005623 bg_r = rval;
5624 bg_g = gval;
5625 bg_b = bval;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005626# endif
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005627#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005628 if (!option_was_set((char_u *)"bg")
5629 && STRCMP(p_bg, new_bg_val) != 0)
5630 {
5631 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005632 set_option_value_give_err((char_u *)"bg",
5633 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005634 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005635 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005636 }
5637 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005638#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005639 else
5640 {
5641 LOG_TR(("Received RFG response: %s", tp));
5642 rfg_status.tr_progress = STATUS_GOT;
5643 fg_r = rval;
5644 fg_g = gval;
5645 fg_b = bval;
5646 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005647#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005648 }
5649
5650 // got finished code: consume it
5651 key_name[0] = (int)KS_EXTRA;
5652 key_name[1] = (int)KE_IGNORE;
5653 *slen = i + 1 + (tp[i] == ESC);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005654#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005655 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5656 : VV_TERMRFGRESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005657#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005658 break;
5659 }
5660 if (i == len)
5661 {
5662 LOG_TR(("not enough characters for RB"));
5663 return FAIL;
5664 }
5665 return OK;
5666}
5667
5668/*
5669 * Check for key code response from xterm:
5670 * {lead}{flag}+r<hex bytes><{tail}
5671 *
5672 * {lead} can be <Esc>P or DCS
5673 * {flag} can be '0' or '1'
5674 * {tail} can be Esc>\ or STERM
5675 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005676 * Check for resource response from xterm (and drop it):
5677 * {lead}{flag}+R<hex bytes>=<value>{tail}
5678 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005679 * Check for cursor shape response from xterm:
5680 * {lead}1$r<digit> q{tail}
5681 *
5682 * {lead} can be <Esc>P or DCS
5683 * {tail} can be <Esc>\ or STERM
5684 *
5685 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5686 */
5687 static int
5688handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5689{
5690 int i, j;
5691
5692 j = 1 + (tp[0] == ESC);
5693 if (len < j + 3)
5694 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005695 else if ((argp[1] != '+' && argp[1] != '$')
5696 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005697 i = 0; // no match
5698 else if (argp[1] == '+')
5699 // key code response
5700 for (i = j; i < len; ++i)
5701 {
5702 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5703 || tp[i] == STERM)
5704 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005705#ifdef FEAT_TERMRESPONSE
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005706 // handle a key code response, drop a resource response
5707 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005708 got_code_from_term(tp + j, i);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005709#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005710 key_name[0] = (int)KS_EXTRA;
5711 key_name[1] = (int)KE_IGNORE;
5712 *slen = i + 1 + (tp[i] == ESC);
5713 break;
5714 }
5715 }
5716 else
5717 {
5718 // Probably the cursor shape response. Make sure that "i"
5719 // is equal to "len" when there are not sufficient
5720 // characters.
5721 for (i = j + 3; i < len; ++i)
5722 {
5723 if (i - j == 3 && !isdigit(tp[i]))
5724 break;
5725 if (i - j == 4 && tp[i] != ' ')
5726 break;
5727 if (i - j == 5 && tp[i] != 'q')
5728 break;
5729 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5730 break;
5731 if ((i - j == 6 && tp[i] == STERM)
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005732 || (i - j == 7 && tp[i] == '\\'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005733 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005734#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005735 int number = argp[3] - '0';
5736
5737 // 0, 1 = block blink, 2 = block
5738 // 3 = underline blink, 4 = underline
5739 // 5 = vertical bar blink, 6 = vertical bar
5740 number = number == 0 ? 1 : number;
5741 initial_cursor_shape = (number + 1) / 2;
5742 // The blink flag is actually inverted, compared to
5743 // the value set with T_SH.
5744 initial_cursor_shape_blink =
5745 (number & 1) ? FALSE : TRUE;
5746 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005747#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005748 LOG_TR(("Received cursor shape response: %s", tp));
5749
5750 key_name[0] = (int)KS_EXTRA;
5751 key_name[1] = (int)KE_IGNORE;
5752 *slen = i + 1;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005753#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005754 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005755#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005756 break;
5757 }
5758 }
5759 }
5760
5761 if (i == len)
5762 {
5763 // These codes arrive many together, each code can be
5764 // truncated at any point.
5765 LOG_TR(("not enough characters for XT"));
5766 return FAIL;
5767 }
5768 return OK;
5769}
5770
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 * Check if typebuf.tb_buf[] contains a terminal key code.
5773 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005774 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005776 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 * With a match, the match is removed, the replacement code is inserted in
5778 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5779 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005780 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5781 * "buflen" is then the length of the string in buf[] and is updated for
5782 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 */
5784 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005785check_termcode(
5786 int max_offset,
5787 char_u *buf,
5788 int bufsize,
5789 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
5791 char_u *tp;
5792 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005793 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005794 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005796 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 int offset;
5798 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005799 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005800 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005801 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005802 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 char_u string[MAX_KEY_CODE_LEN + 1];
5804 int i, j;
5805 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807
5808 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5809
5810 /*
5811 * Speed up the checks for terminal codes by gathering all first bytes
5812 * used in termleader[]. Often this is just a single <Esc>.
5813 */
5814 if (need_gather)
5815 gather_termleader();
5816
5817 /*
5818 * Check at several positions in typebuf.tb_buf[], to catch something like
5819 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5820 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005821 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 * This is used often, KEEP IT FAST!
5823 */
5824 for (offset = 0; offset < max_offset; ++offset)
5825 {
5826 if (buf == NULL)
5827 {
5828 if (offset >= typebuf.tb_len)
5829 break;
5830 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005831 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
5833 else
5834 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005835 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 break;
5837 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005838 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 }
5840
5841 /*
5842 * Don't check characters after K_SPECIAL, those are already
5843 * translated terminal chars (avoid translating ~@^Hx).
5844 */
5845 if (*tp == K_SPECIAL)
5846 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005847 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 continue;
5849 }
5850
5851 /*
5852 * Skip this position if the character does not appear as the first
5853 * character in term_strings. This speeds up a lot, since most
5854 * termcodes start with the same character (ESC or CSI).
5855 */
5856 i = *tp;
5857 for (p = termleader; *p && *p != i; ++p)
5858 ;
5859 if (*p == NUL)
5860 continue;
5861
5862 /*
5863 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5864 * are in Insert mode.
5865 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005866 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 continue;
5868
Bram Moolenaar27efc622022-07-01 16:35:45 +01005869 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005870 key_name[0] = NUL; // no key name found yet
5871 key_name[1] = NUL; // no key name found yet
5872 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873
5874#ifdef FEAT_GUI
5875 if (gui.in_use)
5876 {
5877 /*
5878 * GUI special key codes are all of the form [CSI xx].
5879 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005880 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 {
5882 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005883 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 slen = 3;
5885 key_name[0] = tp[1];
5886 key_name[1] = tp[2];
5887 }
5888 }
5889 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005890#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00005891#ifdef MSWIN
5892 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
5893 && (tp[2] == KE_MOUSEUP
5894 || tp[2] == KE_MOUSEDOWN
5895 || tp[2] == KE_MOUSELEFT
5896 || tp[2] == KE_MOUSERIGHT))
5897 {
5898 // MS-Windows console sends mouse scroll events encoded:
5899 // - CSI
5900 // - KS_EXTRA
5901 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
5902 slen = 3;
5903 key_name[0] = tp[1];
5904 key_name[1] = tp[2];
5905 }
5906 else
5907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005909 int mouse_index_found = -1;
5910
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 for (idx = 0; idx < tc_len; ++idx)
5912 {
5913 /*
5914 * Ignore the entry if we are not at the start of
5915 * typebuf.tb_buf[]
5916 * and there are not enough characters to make a match.
5917 * But only when the 'K' flag is in 'cpoptions'.
5918 */
5919 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005920 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 if (cpo_koffset && offset && len < slen)
5922 continue;
5923 if (STRNCMP(termcodes[idx].code, tp,
5924 (size_t)(slen > len ? len : slen)) == 0)
5925 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005926 int looks_like_mouse_start = FALSE;
5927
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005928 if (len < slen) // got a partial sequence
5929 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930
5931 /*
5932 * When found a keypad key, check if there is another key
5933 * that matches and use that one. This makes <Home> to be
5934 * found instead of <kHome> when they produce the same
5935 * key code.
5936 */
5937 if (termcodes[idx].name[0] == 'K'
5938 && VIM_ISDIGIT(termcodes[idx].name[1]))
5939 {
5940 for (j = idx + 1; j < tc_len; ++j)
5941 if (termcodes[j].len == slen &&
5942 STRNCMP(termcodes[idx].code,
5943 termcodes[j].code, slen) == 0)
5944 {
5945 idx = j;
5946 break;
5947 }
5948 }
5949
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005950 if (slen == 2 && len > 2
5951 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005952 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005953 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005954 // The mouse termcode "ESC [" is also the prefix of
5955 // "ESC [ I" (focus gained) and other keys. Check some
5956 // more bytes to find out.
5957 if (!isdigit(tp[2]))
5958 {
5959 // ESC [ without number following: Only use it when
5960 // there is no other match.
5961 looks_like_mouse_start = TRUE;
5962 }
5963 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5964 {
5965 char_u *nr = tp + 2;
5966 int count = 0;
5967
5968 // If a digit is following it could be a key with
5969 // modifier, e.g., ESC [ 1;2P. Can be confused
5970 // with DEC_MOUSE, which requires four numbers
5971 // following. If not then it can't be a DEC_MOUSE
5972 // code.
5973 for (;;)
5974 {
5975 ++count;
5976 (void)getdigits(&nr);
5977 if (nr >= tp + len)
5978 return -1; // partial sequence
5979 if (*nr != ';')
5980 break;
5981 ++nr;
5982 if (nr >= tp + len)
5983 return -1; // partial sequence
5984 }
5985 if (count < 4)
5986 continue; // no match
5987 }
5988 }
5989 if (looks_like_mouse_start)
5990 {
5991 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005992 if (mouse_index_found < 0)
5993 mouse_index_found = idx;
5994 }
5995 else
5996 {
5997 key_name[0] = termcodes[idx].name[0];
5998 key_name[1] = termcodes[idx].name[1];
5999 break;
6000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006002
6003 /*
6004 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006005 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006006 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006007 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
6008 * When there is a modifier the * matches a number.
6009 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006010 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006011 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006012 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006013 modslen = termcodes[idx].modlen;
6014 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006015 continue;
6016 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006017 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006018 {
6019 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006020
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006021 if (len <= modslen) // got a partial sequence
6022 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006023
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006024 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006025 // no modifiers
6026 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006027 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006028 // no match for "code;*X" with "code;"
6029 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01006030 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00006031 && (tp[modslen] != '1'
6032 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00006033 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006034 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006035 else
6036 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006037 // Skip over the digits, the final char must
6038 // follow. URXVT can use a negative value, thus
6039 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006040 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006041 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006042 ;
6043 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006044 if (len < j) // got a partial sequence
6045 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006046 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006047 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006048
Bram Moolenaara529ce02017-06-22 22:37:57 +02006049 modifiers_start = tp + slen - 2;
6050
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006051 // Match! Convert modifier bits.
6052 n = atoi((char *)modifiers_start);
6053 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006054
6055 slen = j;
6056 }
6057 key_name[0] = termcodes[idx].name[0];
6058 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006059 break;
6060 }
6061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006063 if (idx == tc_len && mouse_index_found >= 0)
6064 {
6065 key_name[0] = termcodes[mouse_index_found].name[0];
6066 key_name[1] = termcodes[mouse_index_found].name[1];
6067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 }
6069
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02006070 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006071 // Mouse codes of DEC and pterm start with <ESC>[. When
6072 // detecting the start of these mouse codes they might as well be
6073 // another key code or terminal response.
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006074#ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006075 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006076#endif
6077#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006078 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006079#endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006080 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006082 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
6083
6084 /*
6085 * Check for responses from the terminal starting with {lead}:
Bram Moolenaar1a173402022-12-02 12:28:47 +00006086 * "<Esc>[" or CSI followed by [0-9>?].
6087 * Also for function keys without a modifier:
6088 * "<Esc>[" or CSI followed by [ABCDEFHPQRS].
Bram Moolenaar9584b312013-03-13 19:29:28 +01006089 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006090 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01006091 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006092 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01006093 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00006094 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
6095 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006096 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006097 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01006098 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006099 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006100 * - window position reply: {lead}3;{x};{y}t
6101 *
6102 * - key with modifiers when modifyOtherKeys is enabled:
6103 * {lead}27;{modifier};{key}~
6104 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01006105 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006106 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar1a173402022-12-02 12:28:47 +00006107 || (tp[0] == CSI && len >= 2))
6108 && vim_strchr((char_u *)"0123456789>?ABCDEFHPQRS",
6109 *argp) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006111 int resp = handle_csi(tp, len, argp, offset, buf,
6112 bufsize, buflen, key_name, &slen);
6113 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006114 {
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006115#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006116 if (resp == -1)
6117 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006118#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006119 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01006120 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006121 }
6122
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006123 // Check for fore/background color response from the terminal,
6124 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006125 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006126 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
6127 || tp[0] == OSC))
6128 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006129 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006130 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 }
6132
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006133 // Check for key code response from xterm,
6134 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00006135 // It would only be needed with this condition:
6136 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
6137 // Now this is always done so that DCS codes don't mess up things.
6138 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006140 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006141 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 }
6143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144
6145 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006146 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006148 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149
Christopher Plewright36446bb2022-11-23 22:28:08 +00006150#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00006152 * For scroll events from the GUI or MS-Windows console, fetch the
6153 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00006155 if (TRUE
6156# if defined(FEAT_GUI) && !defined(MSWIN)
6157 && gui.in_use
6158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 && key_name[0] == (int)KS_EXTRA
6160 && (key_name[1] == (int)KE_X1MOUSE
6161 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006162 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006163 || key_name[1] == (int)KE_MOUSELEFT
6164 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 || key_name[1] == (int)KE_MOUSEDOWN
6166 || key_name[1] == (int)KE_MOUSEUP))
6167 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006168 char_u bytes[6];
6169 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
6170
6171 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 return -1;
6173 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
6174 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
6175 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006176 // equal to K_MOUSEMOVE
6177 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
6178 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 }
6180 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 /*
6183 * If it is a mouse click, get the coordinates.
6184 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006185 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006186#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02006187 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006188#endif
6189#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006190 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006191#endif
6192#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006193 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006194#endif
6195#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006196 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006197#endif
6198#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006199 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006200#endif
6201#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006202 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006203#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006204 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006205 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006207 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
6208 &modifiers) == -1)
6209 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
6212#ifdef FEAT_GUI
6213 /*
6214 * If using the GUI, then we get menu and scrollbar events.
6215 *
6216 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
6217 * four bytes which are to be taken as a pointer to the vimmenu_T
6218 * structure.
6219 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00006220 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006221 * is one byte with the tab index.
6222 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
6224 * by one byte representing the scrollbar number, and then four bytes
6225 * representing a long_u which is the new value of the scrollbar.
6226 *
6227 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
6228 * KE_FILLER followed by four bytes representing a long_u which is the
6229 * new value of the scrollbar.
6230 */
6231# ifdef FEAT_MENU
6232 else if (key_name[0] == (int)KS_MENU)
6233 {
6234 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006235 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 if (num_bytes == -1)
6238 return -1;
6239 current_menu = (vimmenu_T *)val;
6240 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006241
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006242 // The menu may have been deleted right after it was used, check
6243 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006244 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6245 {
6246 key_name[0] = KS_EXTRA;
6247 key_name[1] = (int)KE_IGNORE;
6248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 }
6250# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006251# ifdef FEAT_GUI_TABLINE
6252 else if (key_name[0] == (int)KS_TABLINE)
6253 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006254 // Selecting tabline tab or using its menu.
6255 char_u bytes[6];
6256 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6257
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006258 if (num_bytes == -1)
6259 return -1;
6260 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006261 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006262 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006263 slen += num_bytes;
6264 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006265 else if (key_name[0] == (int)KS_TABMENU)
6266 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006267 // Selecting tabline tab or using its menu.
6268 char_u bytes[6];
6269 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6270
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006271 if (num_bytes == -1)
6272 return -1;
6273 current_tab = (int)bytes[0];
6274 current_tabmenu = (int)bytes[1];
6275 slen += num_bytes;
6276 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278# ifndef USE_ON_FLY_SCROLL
6279 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6280 {
6281 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006282 char_u bytes[6];
6283 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006285 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 j = 0;
6287 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6288 && tp[j + 2] != NUL; ++i)
6289 {
6290 j += 3;
6291 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6292 if (num_bytes == -1)
6293 break;
6294 if (i == 0)
6295 current_scrollbar = (int)bytes[0];
6296 else if (current_scrollbar != (int)bytes[0])
6297 break;
6298 j += num_bytes;
6299 num_bytes = get_long_from_buf(tp + j, &val);
6300 if (num_bytes == -1)
6301 break;
6302 scrollbar_value = val;
6303 j += num_bytes;
6304 slen = j;
6305 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006306 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 return -1;
6308 }
6309 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6310 {
6311 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006312 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006314 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 j = 0;
6316 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6317 && tp[j + 2] != NUL; ++i)
6318 {
6319 j += 3;
6320 num_bytes = get_long_from_buf(tp + j, &val);
6321 if (num_bytes == -1)
6322 break;
6323 scrollbar_value = val;
6324 j += num_bytes;
6325 slen = j;
6326 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006327 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 return -1;
6329 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006330# endif // !USE_ON_FLY_SCROLL
6331#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006333#if (defined(UNIX) || defined(VMS))
6334 /*
6335 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6336 * (CSI I/CSI O)
6337 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006338 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006339# ifdef FEAT_GUI
6340 && !gui.in_use
6341# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006342 )
6343 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006344 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006345 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006346 if (!focus_state)
6347 {
6348 ui_focus_change(TRUE);
6349 did_cursorhold = TRUE;
6350 focus_state = TRUE;
6351 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006352 key_name[1] = (int)KE_IGNORE;
6353 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006354 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006355 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006356 if (focus_state)
6357 {
6358 ui_focus_change(FALSE);
6359 did_cursorhold = TRUE;
6360 focus_state = FALSE;
6361 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006362 key_name[1] = (int)KE_IGNORE;
6363 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006364 }
6365#endif
6366
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006367 /*
6368 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6369 */
6370 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6371
6372 /*
6373 * Add any modifier codes to our string.
6374 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006375 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006376
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006377 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006378 key_name[0] = KEY2TERMCAP0(key);
6379 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006381 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006382 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006383 if (has_mbyte)
6384 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6385 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006386 string[new_slen++] = key_name[1];
6387 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006388 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6389 && key_name[1] == KE_IGNORE)
6390 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006391 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6392 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006393 retval = KEYLEN_REMOVED;
6394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 else
6396 {
6397 string[new_slen++] = K_SPECIAL;
6398 string[new_slen++] = key_name[0];
6399 string[new_slen++] = key_name[1];
6400 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006401 if (put_string_in_typebuf(offset, slen, string, new_slen,
6402 buf, bufsize, buflen) == FAIL)
6403 return -1;
6404 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 }
6406
Bram Moolenaar2951b772013-07-03 12:45:31 +02006407#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006408 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006409#endif
6410
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006411 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412}
6413
Bram Moolenaar9377df32017-10-15 13:22:01 +02006414#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006415/*
6416 * Get the text foreground color, if known.
6417 */
6418 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006419term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006420{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006421 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006422 {
6423 *r = fg_r;
6424 *g = fg_g;
6425 *b = fg_b;
6426 }
6427}
6428
6429/*
6430 * Get the text background color, if known.
6431 */
6432 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006433term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006434{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006435 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006436 {
6437 *r = bg_r;
6438 *g = bg_g;
6439 *b = bg_b;
6440 }
6441}
6442#endif
6443
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444/*
6445 * Replace any terminal code strings in from[] with the equivalent internal
6446 * vim representation. This is used for the "from" and "to" part of a
6447 * mapping, and the "to" part of a menu command.
6448 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6449 * '<'.
6450 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6451 *
6452 * The replacement is done in result[] and finally copied into allocated
6453 * memory. If this all works well *bufp is set to the allocated memory and a
6454 * pointer to it is returned. If something fails *bufp is set to NULL and from
6455 * is returned.
6456 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006457 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6458 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6459 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6460 * used instead of a CTRL-V.
6461 *
6462 * Flags:
6463 * REPTERM_FROM_PART see above
6464 * REPTERM_DO_LT also translate <lt>
6465 * REPTERM_SPECIAL always accept <key> notation
6466 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6467 *
6468 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6469 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006471 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006472replace_termcodes(
6473 char_u *from,
6474 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02006475 int flags,
6476 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477{
6478 int i;
6479 int slen;
6480 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006481 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006483 int do_backslash; // backslash is a special character
6484 int do_special; // recognize <> key codes
6485 int do_key_code; // recognize raw key codes
6486 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006487 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488
6489 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006490 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6491 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006493 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494
6495 /*
6496 * Allocate space for the translation. Worst case a single character is
6497 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006498 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006500 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006501 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 {
6503 *bufp = NULL;
6504 return from;
6505 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006506 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
6508 /*
6509 * Check for #n at start only: function key n
6510 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006511 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 {
6513 result[dlen++] = K_SPECIAL;
6514 result[dlen++] = 'k';
6515 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006516 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006518 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 src += 2;
6520 }
6521
6522 /*
6523 * Copy each byte from *from to result[dlen]
6524 */
6525 while (*src != NUL)
6526 {
6527 /*
6528 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006529 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006531 if (do_special && ((flags & REPTERM_DO_LT)
6532 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
6534#ifdef FEAT_EVAL
6535 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006536 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6537 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006539 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6540 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 */
6542 if (STRNICMP(src, "<SID>", 5) == 0)
6543 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006544 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006545 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 else
6547 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006548 char_u *dot;
6549 long sid = current_sctx.sc_sid;
6550
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006552 if (in_vim9script()
6553 && (dot = vim_strchr(src, '.')) != NULL)
6554 {
6555 imported_T *imp = find_imported(src, dot - src, FALSE);
6556
6557 if (imp != NULL)
6558 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006559 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6560 size_t len;
6561
Bram Moolenaar89445512022-04-14 12:58:23 +01006562 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006563 if (si->sn_autoload_prefix != NULL)
6564 {
6565 // Turn "<SID>name.Func"
6566 // into "scriptname#Func".
6567 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006568 if (ga_grow(&ga,
6569 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006570 {
6571 ga_clear(&ga);
6572 *bufp = NULL;
6573 return from;
6574 }
6575 result = ga.ga_data;
6576 STRCPY(result + dlen, si->sn_autoload_prefix);
6577 dlen += len;
6578 continue;
6579 }
6580 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006581 }
6582 }
6583
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 result[dlen++] = K_SPECIAL;
6585 result[dlen++] = (int)KS_EXTRA;
6586 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006587 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006588 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 result[dlen++] = '_';
6590 continue;
6591 }
6592 }
6593#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006594 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6595 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006596 TRUE, did_simplify);
Bram Moolenaar1a173402022-12-02 12:28:47 +00006597 if (slen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 {
6599 dlen += slen;
6600 continue;
6601 }
6602 }
6603
6604 /*
6605 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6606 * Note that this is also checked after replacing the <> form.
6607 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6608 * it could be a character in the file.
6609 */
6610 if (do_key_code)
6611 {
6612 i = find_term_bykeys(src);
6613 if (i >= 0)
6614 {
6615 result[dlen++] = K_SPECIAL;
6616 result[dlen++] = termcodes[i].name[0];
6617 result[dlen++] = termcodes[i].name[1];
6618 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006619 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 continue;
6621 }
6622 }
6623
6624#ifdef FEAT_EVAL
6625 if (do_special)
6626 {
6627 char_u *p, *s, len;
6628
6629 /*
6630 * Replace <Leader> by the value of "mapleader".
6631 * Replace <LocalLeader> by the value of "maplocalleader".
6632 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6633 */
6634 if (STRNICMP(src, "<Leader>", 8) == 0)
6635 {
6636 len = 8;
6637 p = get_var_value((char_u *)"g:mapleader");
6638 }
6639 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6640 {
6641 len = 13;
6642 p = get_var_value((char_u *)"g:maplocalleader");
6643 }
6644 else
6645 {
6646 len = 0;
6647 p = NULL;
6648 }
6649 if (len != 0)
6650 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006651 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6653 s = (char_u *)"\\";
6654 else
6655 s = p;
6656 while (*s != NUL)
6657 result[dlen++] = *s++;
6658 src += len;
6659 continue;
6660 }
6661 }
6662#endif
6663
6664 /*
6665 * Remove CTRL-V and ignore the next character.
6666 * For "from" side the CTRL-V at the end is included, for the "to"
6667 * part it is removed.
6668 * If 'cpoptions' does not contain 'B', also accept a backslash.
6669 */
6670 key = *src;
6671 if (key == Ctrl_V || (do_backslash && key == '\\'))
6672 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006673 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 if (*src == NUL)
6675 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006676 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 result[dlen++] = key;
6678 break;
6679 }
6680 }
6681
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006682 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006683 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 {
6685 /*
6686 * If the character is K_SPECIAL, replace it with K_SPECIAL
6687 * KS_SPECIAL KE_FILLER.
6688 * If compiled with the GUI replace CSI with K_CSI.
6689 */
6690 if (*src == K_SPECIAL)
6691 {
6692 result[dlen++] = K_SPECIAL;
6693 result[dlen++] = KS_SPECIAL;
6694 result[dlen++] = KE_FILLER;
6695 }
6696# ifdef FEAT_GUI
6697 else if (*src == CSI)
6698 {
6699 result[dlen++] = K_SPECIAL;
6700 result[dlen++] = KS_EXTRA;
6701 result[dlen++] = (int)KE_CSI;
6702 }
6703# endif
6704 else
6705 result[dlen++] = *src;
6706 ++src;
6707 }
6708 }
6709 result[dlen] = NUL;
6710
6711 /*
6712 * Copy the new string to allocated memory.
6713 * If this fails, just return from.
6714 */
6715 if ((*bufp = vim_strsave(result)) != NULL)
6716 from = *bufp;
6717 vim_free(result);
6718 return from;
6719}
6720
6721/*
6722 * Find a termcode with keys 'src' (must be NUL terminated).
6723 * Return the index in termcodes[], or -1 if not found.
6724 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006725 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006726find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
6728 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006729 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730
6731 for (i = 0; i < tc_len; ++i)
6732 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006733 if (slen == termcodes[i].len
6734 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 return i;
6736 }
6737 return -1;
6738}
6739
6740/*
6741 * Gather the first characters in the terminal key codes into a string.
6742 * Used to speed up check_termcode().
6743 */
6744 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006745gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746{
6747 int i;
6748 int len = 0;
6749
6750#ifdef FEAT_GUI
6751 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006752 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753#endif
6754#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006755 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006756 termleader[len++] = DCS; // the termcode response starts with DCS
6757 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758#endif
6759 termleader[len] = NUL;
6760
6761 for (i = 0; i < tc_len; ++i)
6762 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6763 {
6764 termleader[len++] = termcodes[i].code[0];
6765 termleader[len] = NUL;
6766 }
6767
6768 need_gather = FALSE;
6769}
6770
6771/*
6772 * Show all termcodes (for ":set termcap")
6773 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006774 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 */
6776 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006777show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
6779 int col;
6780 int *items;
6781 int item_count;
6782 int run;
6783 int row, rows;
6784 int cols;
6785 int i;
6786 int len;
6787
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006788#define INC3 27 // try to make three columns
6789#define INC2 40 // try to make two columns
6790#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006792 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006794 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 if (items == NULL)
6796 return;
6797
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006798 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006799 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800
6801 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006802 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006804 * 2. display the medium items (medium length strings)
6805 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006806 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006808 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 {
6810 /*
6811 * collect the items in items[]
6812 */
6813 item_count = 0;
6814 for (i = 0; i < tc_len; i++)
6815 {
6816 len = show_one_termcode(termcodes[i].name,
6817 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006818 if ((flags & OPT_ONECOLUMN) ||
6819 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006820 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006821 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 items[item_count++] = i;
6823 }
6824
6825 /*
6826 * display the items
6827 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006828 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006830 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 if (cols == 0)
6832 cols = 1;
6833 rows = (item_count + cols - 1) / cols;
6834 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006835 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 rows = item_count;
6837 for (row = 0; row < rows && !got_int; ++row)
6838 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006839 msg_putchar('\n'); // go to next line
6840 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 break;
6842 col = 0;
6843 for (i = row; i < item_count; i += rows)
6844 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006845 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 show_one_termcode(termcodes[items[i]].name,
6847 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006848 if (run == 2)
6849 col += INC2;
6850 else
6851 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 }
6853 out_flush();
6854 ui_breakcheck();
6855 }
6856 }
6857 vim_free(items);
6858}
6859
6860/*
6861 * Show one termcode entry.
6862 * Output goes into IObuff[]
6863 */
6864 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006865show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866{
6867 char_u *p;
6868 int len;
6869
6870 if (name[0] > '~')
6871 {
6872 IObuff[0] = ' ';
6873 IObuff[1] = ' ';
6874 IObuff[2] = ' ';
6875 IObuff[3] = ' ';
6876 }
6877 else
6878 {
6879 IObuff[0] = 't';
6880 IObuff[1] = '_';
6881 IObuff[2] = name[0];
6882 IObuff[3] = name[1];
6883 }
6884 IObuff[4] = ' ';
6885
6886 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6887 if (p[1] != 't')
6888 STRCPY(IObuff + 5, p);
6889 else
6890 IObuff[5] = NUL;
6891 len = (int)STRLEN(IObuff);
6892 do
6893 IObuff[len++] = ' ';
6894 while (len < 17);
6895 IObuff[len] = NUL;
6896 if (code == NULL)
6897 len += 4;
6898 else
6899 len += vim_strsize(code);
6900
6901 if (printit)
6902 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006903 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006905 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 else
6907 msg_outtrans(code);
6908 }
6909 return len;
6910}
6911
6912#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6913/*
6914 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6915 * termcap codes from the terminal itself.
6916 * We get them one by one to avoid a very long response string.
6917 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006918static int xt_index_in = 0;
6919static int xt_index_out = 0;
6920
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006922req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923{
6924 xt_index_out = 0;
6925 xt_index_in = 0;
6926 req_more_codes_from_term();
6927}
6928
6929 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006930req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006932 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 int old_idx = xt_index_out;
6934
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006935 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 if (exiting)
6937 return;
6938
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006939 // Send up to 10 more requests out than we received. Avoid sending too
6940 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6942 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006943 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006944
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006945 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006946 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6947 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 out_str_nf((char_u *)buf);
6949 ++xt_index_out;
6950 }
6951
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006952 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 if (xt_index_out != old_idx)
6954 out_flush();
6955}
6956
6957/*
6958 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6959 * A "0" instead of the "1" indicates a code that isn't supported.
6960 * Both <name> and <string> are encoded in hex.
6961 * "code" points to the "0" or "1".
6962 */
6963 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006964got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965{
6966#define XT_LEN 100
6967 char_u name[3];
6968 char_u str[XT_LEN];
6969 int i;
6970 int j = 0;
6971 int c;
6972
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006973 // A '1' means the code is supported, a '0' means it isn't.
6974 // When half the length is > XT_LEN we can't use it.
6975 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6977 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006978 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 name[0] = hexhex2nr(code + 3);
6980 name[1] = hexhex2nr(code + 5);
6981 name[2] = NUL;
6982 for (i = 0; key_names[i] != NULL; ++i)
6983 {
6984 if (STRCMP(key_names[i], name) == 0)
6985 {
6986 xt_index_in = i;
6987 break;
6988 }
6989 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006990
Bram Moolenaarb255b902018-04-24 21:40:10 +02006991 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6992
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (key_names[i] != NULL)
6994 {
6995 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6996 str[j++] = c;
6997 str[j] = NUL;
6998 if (name[0] == 'C' && name[1] == 'o')
6999 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007000 // Color count is not a key code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007001 int val = atoi((char *)str);
7002#if defined(FEAT_EVAL)
7003 if (val == t_colors)
7004 ch_log(NULL, "got_code_from_term(Co): no change (%d)", val);
7005 else
7006 ch_log(NULL,
7007 "got_code_from_term(Co): changed from %d to %d",
7008 t_colors, val);
7009#endif
7010 may_adjust_color_count(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 }
7012 else
7013 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 i = find_term_bykeys(str);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007015 if (i >= 0 && name[0] == termcodes[i].name[0]
7016 && name[1] == termcodes[i].name[1])
7017 {
7018 // Existing entry with the same name and code - skip.
7019#ifdef FEAT_EVAL
7020 ch_log(NULL, "got_code_from_term(): Entry %c%c did not change",
7021 name[0], name[1]);
7022#endif
7023 }
7024 else
7025 {
7026 if (i >= 0)
7027 {
7028 // Delete an existing entry using the same code.
7029#ifdef FEAT_EVAL
7030 ch_log(NULL, "got_code_from_term(): Deleting entry %c%c with matching keys %s",
7031 termcodes[i].name[0], termcodes[i].name[1], str);
7032#endif
7033 del_termcode_idx(i);
7034 }
7035#ifdef FEAT_EVAL
7036 else
7037 ch_log(NULL, "got_code_from_term(): Adding entry %c%c with keys %s",
7038 name[0], name[1], str);
7039#endif
7040 add_termcode(name, str, ATC_FROM_TERM);
7041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 }
7043 }
7044 }
7045
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007046 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 ++xt_index_in;
7048 req_more_codes_from_term();
7049}
7050
7051/*
7052 * Check if there are any unanswered requests and deal with them.
7053 * This is called before starting an external program or getting direct
7054 * keyboard input. We don't want responses to be send to that program or
7055 * handled as typed text.
7056 */
7057 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007058check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059{
7060 int c;
7061
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007062 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 if (xt_index_out == 0 || xt_index_out == xt_index_in)
7064 return;
7065
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007066 // Vgetc() will check for and handle any response.
7067 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 ++no_mapping;
7069 ++allow_keys;
7070 for (;;)
7071 {
7072 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007073 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 break;
7075
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007076 // If a response is recognized it's replaced with K_IGNORE, must read
7077 // it from the input stream. If there is no K_IGNORE we can't do
7078 // anything, break here (there might be some responses further on, but
7079 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 if (c != K_SPECIAL && c != K_IGNORE)
7081 break;
7082 c = vgetc();
7083 if (c != K_IGNORE)
7084 {
7085 vungetc(c);
7086 break;
7087 }
7088 }
7089 --no_mapping;
7090 --allow_keys;
7091}
7092#endif
7093
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007094#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095static char ksme_str[20];
7096static char ksmr_str[20];
7097static char ksmd_str[20];
7098
7099/*
7100 * For Win32 console: update termcap codes for existing console attributes.
7101 */
7102 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007103update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00007105 sprintf(ksme_str, "\033|%dm", attr);
7106 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
7107 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108
Bram Moolenaar4654d632022-11-17 22:05:12 +00007109 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
7110 if (p == NULL) // did not find it
7111 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 while (p->bt_string != NULL)
7113 {
7114 if (p->bt_entry == (int)KS_ME)
7115 p->bt_string = &ksme_str[0];
7116 else if (p->bt_entry == (int)KS_MR)
7117 p->bt_string = &ksmr_str[0];
7118 else if (p->bt_entry == (int)KS_MD)
7119 p->bt_string = &ksmd_str[0];
7120 ++p;
7121 }
7122}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007123
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007124# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007125# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007126
7127typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007128{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007129 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
7130 CMODE_RGB, // Use 24bit RGB colors using VTP.
7131 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
7132 CMODE_LAST,
7133} cmode_T;
7134
7135struct ks_tbl_S
7136{
7137 int code; // value of KS_
7138 char *vtp; // code in RGB mode
7139 char *vtp2; // code in 256color mode
7140 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007141};
7142
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007143static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007144{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007145 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
7146 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
7147 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
7148 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
7149 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
7150 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
7151 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
7152 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
7153 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007154# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007155 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
7156 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
7157 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
7158 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007159# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007160 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
7161 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
7162 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
7163 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007164# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007165 {(int)KS_CCO, "256", "256", {""}}, // colors
7166 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007167};
7168
Bram Moolenaar4654d632022-11-17 22:05:12 +00007169/*
7170 * Find the first entry for "code" in the builtin termcap for "name".
7171 * Returns NULL when not found.
7172 */
7173 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007174find_first_tcap(
7175 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007176 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007177{
Bram Moolenaar4654d632022-11-17 22:05:12 +00007178 tcap_entry_T *p = find_builtin_term(name);
7179 if (p != NULL)
7180 {
7181 while (p->bt_string != NULL)
7182 {
7183 if (p->bt_entry == code)
7184 return p;
7185 ++p;
7186 }
7187 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007188 return NULL;
7189}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007190# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007191
7192/*
7193 * For Win32 console: replace the sequence immediately after termguicolors.
7194 */
7195 void
7196swap_tcap(void)
7197{
7198# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007199 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007200 static cmode_T curr_mode;
7201 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007202 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007203
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007204 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007205 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007206 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007207 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007208 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007209 if (bt != NULL)
7210 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007211 // Preserve the original value.
7212 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
7213 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
7214 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007215
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007216 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007217 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007218 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007219 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007220 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007221 }
7222
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007223 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007224 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007225 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007226 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007227 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007228 mode = CMODE_INDEXED;
7229
7230 if (mode == curr_mode)
7231 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007232
7233 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007234 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007235 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007236 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007237 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007238 }
7239
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007240 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007241# endif
7242}
7243
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02007245
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007246
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007247#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007248 || defined(PROTO)
7249static int cube_value[] = {
7250 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7251};
7252
7253static int grey_ramp[] = {
7254 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7255 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7256};
7257
LemonBoyb2b3acb2022-05-20 10:10:34 +01007258static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01007259// R G B
7260 { 0, 0, 0}, // black
7261 {224, 0, 0}, // dark red
7262 { 0, 224, 0}, // dark green
7263 {224, 224, 0}, // dark yellow / brown
7264 { 0, 0, 224}, // dark blue
7265 {224, 0, 224}, // dark magenta
7266 { 0, 224, 224}, // dark cyan
7267 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007268
LemonBoyd2a46622022-05-01 17:43:33 +01007269 {128, 128, 128}, // dark grey
7270 {255, 64, 64}, // light red
7271 { 64, 255, 64}, // light green
7272 {255, 255, 64}, // yellow
7273 { 64, 64, 255}, // light blue
7274 {255, 64, 255}, // light magenta
7275 { 64, 255, 255}, // light cyan
7276 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007277};
7278
LemonBoyd2a46622022-05-01 17:43:33 +01007279#if defined(MSWIN)
7280// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7281static const char_u cterm_ansi_idx[] = {
7282 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7283};
7284#endif
7285
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007286#define ANSI_INDEX_NONE 0
7287
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007288 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007289ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7290{
7291 if (nr < 16)
7292 {
7293 *r = ansi_table[nr][0];
7294 *g = ansi_table[nr][1];
7295 *b = ansi_table[nr][2];
7296 *ansi_idx = nr;
7297 }
7298 else
7299 {
7300 *r = 0;
7301 *g = 0;
7302 *b = 0;
7303 *ansi_idx = ANSI_INDEX_NONE;
7304 }
7305}
7306
7307 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007308cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007309{
7310 int idx;
7311
7312 if (nr < 16)
7313 {
LemonBoyd2a46622022-05-01 17:43:33 +01007314#if defined(MSWIN)
7315 idx = cterm_ansi_idx[nr];
7316#else
7317 idx = nr;
7318#endif
7319 *r = ansi_table[idx][0];
7320 *g = ansi_table[idx][1];
7321 *b = ansi_table[idx][2];
7322 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007323 }
7324 else if (nr < 232)
7325 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007326 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007327 idx = nr - 16;
7328 *r = cube_value[idx / 36 % 6];
7329 *g = cube_value[idx / 6 % 6];
7330 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007331 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007332 }
7333 else if (nr < 256)
7334 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007335 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007336 idx = nr - 232;
7337 *r = grey_ramp[idx];
7338 *g = grey_ramp[idx];
7339 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007340 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007341 }
7342 else
7343 {
7344 *r = 0;
7345 *g = 0;
7346 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007347 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007348 }
7349}
7350#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007351
Bram Moolenaarf4140482020-02-15 23:06:45 +01007352/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007353 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007354 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007355 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007356 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007357 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007358term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007359{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007360 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007361 int i;
7362 int c;
7363
7364 for (i = ta_len; i < ta_len + len; ++i)
7365 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007366 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7367 {
7368 int modifiers = ta_buf[i + 2];
7369 int key = ta_buf[i + 3];
7370
7371 // Try to use the modifier to modify the key. In any case drop the
7372 // modifier.
7373 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7374 len -= 3;
7375 if (key < 0x80)
7376 key = merge_modifyOtherKeys(key, &modifiers);
7377 ta_buf[i] = key;
7378 }
7379 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007380 {
7381 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7382 if (c == K_DEL || c == K_KDEL || c == K_BS)
7383 {
7384 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007385 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007386 if (c == K_DEL || c == K_KDEL)
7387 ta_buf[i] = DEL;
7388 else
7389 ta_buf[i] = Ctrl_H;
7390 len -= 2;
7391 }
7392 }
7393 else if (ta_buf[i] == '\r')
7394 ta_buf[i] = '\n';
7395 if (has_mbyte)
7396 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7397 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007398 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007399}