blob: e83c198bdf2e0c2995e6b51f3e2a3751bc39de8c [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 Moolenaar77071372022-12-30 19:54:53 +00001681#if defined(HAVE_TGETENT) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001682/*
1683 * Return TRUE if "term_strings[idx]" was not set.
1684 */
1685 static int
1686term_strings_not_set(enum SpecialKey idx)
1687{
1688 return TERM_STR(idx) == NULL || TERM_STR(idx) == empty_option;
1689}
Bram Moolenaar77071372022-12-30 19:54:53 +00001690#endif
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001691
Bram Moolenaar9289df52018-05-10 14:40:57 +02001692#ifdef HAVE_TGETENT
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00001693/*
1694 * Get the termcap entries we need with tgetstr(), tgetflag() and tgetnum().
1695 * "invoke_tgetent()" must have been called before.
1696 * If "*height" or "*width" are not zero then use the "li" and "col" entries to
1697 * get their value.
1698 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001699 static void
1700get_term_entries(int *height, int *width)
1701{
1702 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001703 enum SpecialKey dest; // index in term_strings[]
1704 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001705 } string_names[] =
1706 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1707 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1708 {KS_CL, "cl"}, {KS_CD, "cd"},
1709 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1710 {KS_ME, "me"}, {KS_MR, "mr"},
1711 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1712 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1713 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001714 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001715 {KS_STE,"Te"}, {KS_STS,"Ts"},
1716 {KS_CM, "cm"}, {KS_SR, "sr"},
1717 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1718 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001719 {KS_CTI, "TI"}, {KS_CRK, "RK"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001720 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001721 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1722 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001723 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1724 {KS_VS, "vs"}, {KS_CVS, "VS"},
1725 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1726 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1727 {KS_TS, "ts"}, {KS_FS, "fs"},
1728 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1729 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1730 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001731 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001732 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1733 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001734 {KS_CST, "ST"}, {KS_CRT, "RT"},
1735 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001736 {(enum SpecialKey)0, NULL}
1737 };
1738 int i;
1739 char_u *p;
1740 static char_u tstrbuf[TBUFSZ];
1741 char_u *tp = tstrbuf;
1742
1743 /*
1744 * get output strings
1745 */
1746 for (i = 0; string_names[i].name != NULL; ++i)
1747 {
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001748 if (term_strings_not_set(string_names[i].dest))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001749 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001750 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001751#ifdef FEAT_EVAL
1752 set_term_option_sctx_idx(string_names[i].name, -1);
1753#endif
1754 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001755 }
1756
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001757 // tgetflag() returns 1 if the flag is present, 0 if not and
1758 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001759 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1760 T_MS = (char_u *)"y";
1761 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1762 T_XS = (char_u *)"y";
1763 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1764 T_XN = (char_u *)"y";
1765 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1766 T_DB = (char_u *)"y";
1767 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1768 T_DA = (char_u *)"y";
1769 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1770 T_UT = (char_u *)"y";
1771
1772 /*
1773 * get key codes
1774 */
1775 for (i = 0; key_names[i] != NULL; ++i)
1776 if (find_termcode((char_u *)key_names[i]) == NULL)
1777 {
1778 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001779 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001780 if (p != NULL
1781 && (*p != Ctrl_H
1782 || key_names[i][0] != 'k'
1783 || key_names[i][1] != 'l'))
1784 add_termcode((char_u *)key_names[i], p, FALSE);
1785 }
1786
1787 if (*height == 0)
1788 *height = tgetnum("li");
1789 if (*width == 0)
1790 *width = tgetnum("co");
1791
1792 /*
1793 * Get number of colors (if not done already).
1794 */
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001795 if (term_strings_not_set(KS_CCO))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001796 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001797 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001798#ifdef FEAT_EVAL
1799 set_term_option_sctx_idx("Co", -1);
1800#endif
1801 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001802
1803# ifndef hpux
1804 BC = (char *)TGETSTR("bc", &tp);
1805 UP = (char *)TGETSTR("up", &tp);
1806 p = TGETSTR("pc", &tp);
1807 if (p)
1808 PC = *p;
1809# endif
1810}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001811#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001812
Bram Moolenaar4654d632022-11-17 22:05:12 +00001813/*
1814 * Report "term" is not found and list the ones we do know about.
1815 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001816 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001817report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001818{
Bram Moolenaar69e05692018-05-10 14:11:52 +02001819 mch_errmsg("\r\n");
1820 if (error_msg != NULL)
1821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001822 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001823 mch_errmsg("\r\n");
1824 }
1825 mch_errmsg("'");
1826 mch_errmsg((char *)term);
1827 mch_errmsg(_("' not known. Available builtin terminals are:"));
1828 mch_errmsg("\r\n");
Bram Moolenaar4654d632022-11-17 22:05:12 +00001829
1830 for (int i = 0; ; ++i)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001831 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00001832 char *name = builtin_terminals[i].bitc_name;
1833 if (name == NULL) // end marker
1834 break;
1835 // Do not mention the "gui" entry, the user won't need to type it.
1836 if (STRCMP(name, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001837 {
1838#ifdef HAVE_TGETENT
1839 mch_errmsg(" builtin_");
1840#else
1841 mch_errmsg(" ");
1842#endif
Bram Moolenaar4654d632022-11-17 22:05:12 +00001843 mch_errmsg(name);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001844 mch_errmsg("\r\n");
1845 }
1846 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001847 // Output extra 'cmdheight' line breaks to avoid that the following error
1848 // message overwrites the last terminal name.
Bram Moolenaar4654d632022-11-17 22:05:12 +00001849 for (int i = 1; i < p_ch; ++i)
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001850 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001851}
1852
1853 static void
1854report_default_term(char_u *term)
1855{
1856 mch_errmsg(_("defaulting to '"));
1857 mch_errmsg((char *)term);
1858 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001859 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001860 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001861 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001862 out_flush();
1863 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001864 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001865 }
1866}
1867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001869 * Parse the 'keyprotocol' option, match against "term" and return the protocol
1870 * for the first matching entry.
1871 * When "term" is NULL then compile all patterns to check for any errors.
1872 * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
1873 * Returns KEYPROTOCOL_NONE if there is no match.
1874 */
1875 keyprot_T
1876match_keyprotocol(char_u *term)
1877{
1878 int len = (int)STRLEN(p_kpc) + 1;
1879 char_u *buf = alloc(len);
1880 if (buf == NULL)
1881 return KEYPROTOCOL_FAIL;
1882
1883 keyprot_T ret = KEYPROTOCOL_FAIL;
1884 char_u *p = p_kpc;
1885 while (*p != NUL)
1886 {
1887 // Isolate one comma separated item.
1888 (void)copy_option_part(&p, buf, len, ",");
1889 char_u *colon = vim_strchr(buf, ':');
1890 if (colon == NULL || colon == buf || colon[1] == NUL)
1891 goto theend;
1892 *colon = NUL;
1893
1894 keyprot_T prot;
1895 if (STRCMP(colon + 1, "none") == 0)
1896 prot = KEYPROTOCOL_NONE;
1897 else if (STRCMP(colon + 1, "mok2") == 0)
1898 prot = KEYPROTOCOL_MOK2;
1899 else if (STRCMP(colon + 1, "kitty") == 0)
1900 prot = KEYPROTOCOL_KITTY;
1901 else
1902 goto theend;
1903
1904 regmatch_T regmatch;
1905 CLEAR_FIELD(regmatch);
1906 regmatch.rm_ic = TRUE;
1907 regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
1908 if (regmatch.regprog == NULL)
1909 goto theend;
1910
1911 int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
1912 vim_regfree(regmatch.regprog);
1913 if (match)
1914 {
1915 ret = prot;
1916 goto theend;
1917 }
1918
1919 }
1920
1921 // No match found, use "none".
1922 ret = KEYPROTOCOL_NONE;
1923
1924theend:
1925 vim_free(buf);
1926 return ret;
1927}
1928
1929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 * Set terminal options for terminal "term".
1931 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1932 *
1933 * While doing this, until ttest(), some options may be NULL, be careful.
1934 */
1935 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001936set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938#ifdef HAVE_TGETENT
1939 int builtin_first = p_tbi;
1940 int try;
1941 int termcap_cleared = FALSE;
1942#endif
1943 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001944 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 char_u *bs_p, *del_p;
1946
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001947 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001948 if (silent_mode)
1949 return OK;
1950
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001951 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
1953 if (term_is_builtin(term))
1954 {
1955 term += 8;
1956#ifdef HAVE_TGETENT
1957 builtin_first = 1;
1958#endif
1959 }
1960
1961/*
1962 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1963 * If builtin_first is TRUE:
1964 * 0. try builtin termcap
1965 * 1. try external termcap
1966 * 2. if both fail default to a builtin terminal
1967 * If builtin_first is FALSE:
1968 * 1. try external termcap
1969 * 2. try builtin termcap, if both fail default to a builtin terminal
1970 */
1971#ifdef HAVE_TGETENT
1972 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1973 {
1974 /*
1975 * Use external termcap
1976 */
1977 if (try == 1)
1978 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
1981 /*
1982 * If the external termcap does not have a matching entry, try the
1983 * builtin ones.
1984 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01001985 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (!termcap_cleared)
1988 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001989 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 termcap_cleared = TRUE;
1991 }
1992
Bram Moolenaar69e05692018-05-10 14:11:52 +02001993 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 }
1995 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001996 else // try == 0 || try == 2
1997#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 /*
1999 * Use builtin termcap
2000 */
2001 {
2002#ifdef HAVE_TGETENT
2003 /*
2004 * If builtin termcap was already used, there is no need to search
2005 * for the builtin termcap again, quit now.
2006 */
2007 if (try == 2 && builtin_first && termcap_cleared)
2008 break;
2009#endif
2010 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002011 * Search for 'term' in builtin_terminals[].
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 */
Bram Moolenaar4654d632022-11-17 22:05:12 +00002013 tcap_entry_T *termp = find_builtin_term(term);
2014 if (termp == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
2016#ifdef HAVE_TGETENT
2017 /*
2018 * If try == 0, first try the external termcap. If that is not
2019 * found we'll get back here with try == 2.
2020 * If termcap_cleared is set we used the external termcap,
2021 * don't complain about not finding the term in the builtin
2022 * termcap.
2023 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002024 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002026 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 break;
2028#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02002029 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002031 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 if (starting != NO_SCREEN)
2033 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002034 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 wait_return(TRUE);
2036 return FAIL;
2037 }
2038 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02002039 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002040 set_string_option_direct((char_u *)"term", -1, term,
2041 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 display_errors();
2043 }
2044 out_flush();
2045#ifdef HAVE_TGETENT
2046 if (!termcap_cleared)
2047 {
2048#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002049 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050#ifdef HAVE_TGETENT
2051 termcap_cleared = TRUE;
2052 }
2053#endif
2054 parse_builtin_tcap(term);
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002055
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056#ifdef FEAT_GUI
2057 if (term_is_gui(term))
2058 {
2059 out_flush();
2060 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002061 // If starting the GUI failed, don't do any of the other
2062 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (!gui.in_use)
2064 return FAIL;
Bram Moolenaar1a173402022-12-02 12:28:47 +00002065# ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002066 break; // don't try using external termcap
Bram Moolenaar1a173402022-12-02 12:28:47 +00002067# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002069#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071#ifdef HAVE_TGETENT
2072 }
2073#endif
2074
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002075#ifdef FEAT_GUI
2076 if (!gui.in_use)
2077#endif
2078 {
2079 // Use the 'keyprotocol' option to adjust the t_TE and t_TI
2080 // termcap entries if there is an entry maching "term".
2081 keyprot_T kpc = match_keyprotocol(term);
2082 if (kpc == KEYPROTOCOL_KITTY)
2083 apply_builtin_tcap(term, builtin_kitty, TRUE);
2084 else if (kpc == KEYPROTOCOL_MOK2)
2085 apply_builtin_tcap(term, builtin_mok2, TRUE);
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00002086
2087#ifdef FEAT_TERMGUICOLORS
2088 // There is no good way to detect that the terminal supports RGB
2089 // colors. Since these termcap entries are non-standard anyway and
2090 // only used when the user sets 'termguicolors' we might as well add
2091 // them. But not when one of them was alredy set.
2092 if (term_strings_not_set(KS_8F)
2093 && term_strings_not_set(KS_8B)
2094 && term_strings_not_set(KS_8U))
2095 apply_builtin_tcap(term, builtin_rgb, TRUE);
2096#endif
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00002097 }
2098
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099/*
2100 * special: There is no info in the termcap about whether the cursor
2101 * positioning is relative to the start of the screen or to the start of the
2102 * scrolling region. We just guess here. Only msdos pcterm is known to do it
2103 * relative.
2104 */
2105 if (STRCMP(term, "pcterm") == 0)
2106 T_CCS = (char_u *)"yes";
2107 else
2108 T_CCS = empty_option;
2109
Bram Moolenaar731d0072022-12-18 17:47:18 +00002110 // Special case: "kitty" does not normally have a "RV" entry in terminfo,
2111 // but we need to request the version for several other things to work.
2112 if (strstr((char *)term, "kitty") != NULL
2113 && (T_CRV == NULL || *T_CRV == NUL))
2114 T_CRV = (char_u *)"\033[>c";
2115
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116#ifdef UNIX
2117/*
2118 * Any "stty" settings override the default for t_kb from the termcap.
2119 * This is in os_unix.c, because it depends a lot on the version of unix that
2120 * is being used.
2121 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2122 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002123# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 get_stty();
2127#endif
2128
2129/*
2130 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2131 * didn't work, use the default CTRL-H
2132 * The default for t_kD is DEL, unless t_kb is DEL.
2133 * The vim_strsave'd strings are probably lost forever, well it's only two
2134 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2135 * directly.
2136 */
2137#ifdef FEAT_GUI
2138 if (!gui.in_use)
2139#endif
2140 {
2141 bs_p = find_termcode((char_u *)"kb");
2142 del_p = find_termcode((char_u *)"kD");
2143 if (bs_p == NULL || *bs_p == NUL)
2144 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2145 if ((del_p == NULL || *del_p == NUL) &&
2146 (bs_p == NULL || *bs_p != DEL))
2147 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2148 }
2149
2150#if defined(UNIX) || defined(VMS)
2151 term_is_xterm = vim_is_xterm(term);
2152#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002153#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002154 // Reset terminal properties that are set based on the termresponse, which
2155 // will be sent out soon.
2156 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002159#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 /*
2161 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2162 * The termcode for the mouse is added as a side effect in option.c.
2163 */
2164 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002165 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002167# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002168 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
2170 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002171 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 else
2173 p = (char_u *)"xterm";
2174 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002177 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002178 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002179 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2180 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002181 reset_option_was_set((char_u *)"ttym");
2182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002184# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002188 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002190#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
Bram Moolenaarbf789742021-01-16 11:21:40 +01002194#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002195 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002196 if (use_xterm_like_mouse(term))
2197 {
2198 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002199
2200 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002201 name[0] = KS_EXTRA;
2202 name[1] = KE_FOCUSGAINED;
2203 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002204 add_termcode(name, (char_u *)"\033[I", FALSE);
2205
2206 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002207 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002208 add_termcode(name, (char_u *)"\033[O", FALSE);
2209
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002210 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002211 }
2212#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002213#if (defined(UNIX) || defined(VMS))
2214 // First time after setting 'term' a focus event is always reported.
2215 focus_state = MAYBE;
2216#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002217
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002219 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 if (STRCMP(term, DEFAULT_TERM) != 0)
2221 term_console = FALSE;
2222 else
2223 {
2224 term_console = TRUE;
2225# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002226 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227# endif
2228 }
2229#endif
2230
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002231 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002233 full_screen = TRUE; // we can use termcap codes from now on
2234 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002236 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002237 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002238 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239#endif
2240
2241 /*
2242 * Initialize the terminal with the appropriate termcap codes.
2243 * Set the mouse and window title if possible.
2244 * Don't do this when starting, need to parse the .vimrc first, because it
2245 * may redefine t_TI etc.
2246 */
2247 if (starting != NO_SCREEN)
2248 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002249 starttermcap(); // may change terminal mode
2250 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002251 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 }
2253
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002254 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if (width <= 0 || height <= 0)
2256 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002257 // termcap failed to report size
2258 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002260#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002261 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002263 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264#endif
2265 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002266 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if (starting != NO_SCREEN)
2268 {
2269 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002270 scroll_region_reset(); // In case Rows changed
2271 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002274 buf_T *buf;
2275 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276
2277 /*
2278 * Execute the TermChanged autocommands for each buffer that is
2279 * loaded.
2280 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002281 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
2283 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002284 {
2285 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002286 if (curbuf == buf)
2287 {
2288 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 curbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00002290 // restore curwin/curbuf and a few other things
2291 aucmd_restbuf(&aco);
2292 }
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297
2298#ifdef FEAT_TERMRESPONSE
2299 may_req_termresponse();
2300#endif
2301
2302 return OK;
2303}
2304
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002305#if defined(EXITFREE) || defined(PROTO)
2306
2307# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002308# include <term.h> // declares cur_term
2309# endif
2310
2311/*
2312 * If supported, delete "cur_term", which caches terminal related entries.
2313 * Avoids that valgrind reports possibly lost memory.
2314 */
2315 void
2316free_cur_term()
2317{
2318# ifdef HAVE_DEL_CURTERM
2319 if (cur_term)
2320 del_curterm(cur_term);
2321# endif
2322}
2323
2324#endif
2325
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326#ifdef HAVE_TGETENT
2327/*
2328 * Call tgetent()
2329 * Return error message if it fails, NULL if it's OK.
2330 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002331 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002332invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334 int i;
2335
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002336 // Note: Valgrind may report a leak here, because the library keeps one
2337 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002339 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002341 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342# endif
2343 )
2344 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002345 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2346 // tgetent() with the always existing "dumb" entry to avoid a crash or
2347 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 (void)TGETENT(tbuf, "dumb");
2349
2350 if (i < 0)
2351# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002352 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 if (i == 0)
2354# endif
2355#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002356 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002358 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359#endif
2360 }
2361 return NULL;
2362}
2363
2364/*
2365 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2366 * Fix that here.
2367 */
2368 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002369vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370{
2371 char *p;
2372
2373 p = tgetstr(s, (char **)pp);
2374 if (p == (char *)-1)
2375 p = NULL;
2376 return (char_u *)p;
2377}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002378#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002380#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381/*
2382 * Get Columns and Rows from the termcap. Used after a window signal if the
2383 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2384 * and "li" entries never change. But on some systems this works.
2385 * Errors while getting the entries are ignored.
2386 */
2387 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002388getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002389 long *cp, // pointer to columns
2390 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391{
2392 char_u tbuf[TBUFSZ];
2393
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002394 if (T_NAME != NULL && *T_NAME != NUL && invoke_tgetent(tbuf, T_NAME) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
2396 if (*cp == 0)
2397 *cp = tgetnum("co");
2398 if (*rp == 0)
2399 *rp = tgetnum("li");
2400 }
2401}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002402#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404/*
2405 * Get a string entry from the termcap and add it to the list of termcodes.
2406 * Used for <t_xx> special keys.
2407 * Give an error message for failure when not sourcing.
2408 * If force given, replace an existing entry.
2409 * Return FAIL if the entry was not found, OK if the entry was added.
2410 */
2411 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002412add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
2414 char_u *term;
2415 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416#ifdef HAVE_TGETENT
2417 char_u *string;
2418 int i;
2419 int builtin_first;
2420 char_u tbuf[TBUFSZ];
2421 char_u tstrbuf[TBUFSZ];
2422 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002423 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424#endif
2425
2426/*
2427 * If the GUI is running or will start in a moment, we only support the keys
2428 * that the GUI can produce.
2429 */
2430#ifdef FEAT_GUI
2431 if (gui.in_use || gui.starting)
2432 return gui_mch_haskey(name);
2433#endif
2434
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002435 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 return OK;
2437
2438 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002439 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 return FAIL;
2441
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002442 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
2444 term += 8;
2445#ifdef HAVE_TGETENT
2446 builtin_first = TRUE;
2447#endif
2448 }
2449#ifdef HAVE_TGETENT
2450 else
2451 builtin_first = p_tbi;
2452#endif
2453
2454#ifdef HAVE_TGETENT
2455/*
2456 * We can get the entry from the builtin termcap and from the external one.
2457 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2458 * builtin termcap first.
2459 * If 'ttybuiltin' is off, try external termcap first.
2460 */
2461 for (i = 0; i < 2; ++i)
2462 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002463 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464#endif
2465 /*
Bram Moolenaar4654d632022-11-17 22:05:12 +00002466 * Search in builtin termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 */
2468 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00002469 tcap_entry_T *termp = find_builtin_term(term);
2470 if (termp != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {
2472 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002473 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 while (termp->bt_entry != (int)KS_NAME)
2475 {
2476 if ((int)termp->bt_entry == key)
2477 {
2478 add_termcode(name, (char_u *)termp->bt_string,
2479 term_is_8bit(term));
2480 return OK;
2481 }
2482 ++termp;
2483 }
2484 }
2485 }
2486#ifdef HAVE_TGETENT
2487 else
2488 /*
2489 * Search in external termcap
2490 */
2491 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002492 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 if (error_msg == NULL)
2494 {
2495 string = TGETSTR((char *)name, &tp);
2496 if (string != NULL && *string != NUL)
2497 {
2498 add_termcode(name, string, FALSE);
2499 return OK;
2500 }
2501 }
2502 }
2503 }
2504#endif
2505
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002506 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
2508#ifdef HAVE_TGETENT
2509 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002510 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 else
2512#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002513 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 }
2515 return FAIL;
2516}
2517
2518 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002519term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520{
2521 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2522}
2523
2524/*
2525 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2526 * Assume that the terminal is using 8-bit controls when the name contains
2527 * "8bit", like in "xterm-8bit".
2528 */
2529 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002530term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
2532 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2533}
2534
2535/*
2536 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002537 * <Esc>[ -> CSI <M_C_[>
2538 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 * <Esc>O -> <M-C-O>
2540 */
2541 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002542term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543{
2544 if (*p == ESC)
2545 {
2546 if (p[1] == '[')
2547 return CSI;
2548 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002549 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 if (p[1] == 'O')
2551 return 0x8f;
2552 }
2553 return 0;
2554}
2555
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002556#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002558term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559{
2560 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2561}
2562#endif
2563
2564#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2565
2566 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002567tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568{
2569 static char_u buf[16];
2570 char_u *p;
2571
2572 p = buf + 15;
2573 *p = '\0';
2574 do
2575 {
2576 --p;
2577 *p = (char_u) (i % 10 + '0');
2578 i /= 10;
2579 }
2580 while (i > 0 && p > buf);
2581 return p;
2582}
2583#endif
2584
2585#ifndef HAVE_TGETENT
2586
2587/*
2588 * minimal tgoto() implementation.
2589 * no padding and we only parse for %i %d and %+char
2590 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002591 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002592tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
2594 static char buf[30];
2595 char *p, *s, *e;
2596
2597 if (!cm)
2598 return "OOPS";
2599 e = buf + 29;
2600 for (s = buf; s < e && *cm; cm++)
2601 {
2602 if (*cm != '%')
2603 {
2604 *s++ = *cm;
2605 continue;
2606 }
2607 switch (*++cm)
2608 {
2609 case 'd':
2610 p = (char *)tltoa((unsigned long)y);
2611 y = x;
2612 while (*p)
2613 *s++ = *p++;
2614 break;
2615 case 'i':
2616 x++;
2617 y++;
2618 break;
2619 case '+':
2620 *s++ = (char)(*++cm + y);
2621 y = x;
2622 break;
2623 case '%':
2624 *s++ = *cm;
2625 break;
2626 default:
2627 return "OOPS";
2628 }
2629 }
2630 *s = '\0';
2631 return buf;
2632}
2633
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002634#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
2636/*
2637 * Set the terminal name and initialize the terminal options.
2638 * If "name" is NULL or empty, get the terminal name from the environment.
2639 * If that fails, use the default terminal name.
2640 */
2641 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002642termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
Bram Moolenaar731d0072022-12-18 17:47:18 +00002644 char_u *term = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
Bram Moolenaar731d0072022-12-18 17:47:18 +00002646 if (term != NULL && *term == NUL)
2647 term = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649#ifndef MSWIN
2650 if (term == NULL)
2651 term = mch_getenv((char_u *)"TERM");
2652#endif
2653 if (term == NULL || *term == NUL)
2654 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002655 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002657 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 set_string_default("term", term);
2659 set_string_default("ttytype", term);
2660
Bram Moolenaar731d0072022-12-18 17:47:18 +00002661 // Avoid using "term" here, because the next mch_getenv() may overwrite it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 set_termname(T_NAME != NULL ? T_NAME : term);
2663}
2664
2665/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002666 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002668#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002669
2670// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002672
2673static int out_pos = 0; // number of chars in out_buf
2674
2675// Since the maximum number of SGR parameters shown as a normal value range is
2676// 16, the escape sequence length can be 4 * 16 + lead + tail.
2677#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
2679/*
2680 * out_flush(): flush the output buffer
2681 */
2682 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002683out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684{
2685 int len;
2686
2687 if (out_pos != 0)
2688 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002689 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 len = out_pos;
2691 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002692 ui_write(out_buf, len, FALSE);
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00002693#ifdef FEAT_EVAL
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002694 if (ch_log_output != FALSE)
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002695 {
2696 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002697 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002698# ifdef FEAT_GUI
2699 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2700# endif
2701 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002702 out_buf);
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002703 if (ch_log_output == TRUE)
2704 ch_log_output = FALSE; // only log once
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002705 }
2706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
2708}
2709
Bram Moolenaara338adc2018-01-31 20:51:47 +01002710/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002711 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2712 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002713 */
2714 void
2715out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002716 int force UNUSED, // when TRUE, update cursor even when not moved
2717 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002718{
2719 mch_disable_flush();
2720 out_flush();
2721 mch_enable_flush();
2722#ifdef FEAT_GUI
2723 if (gui.in_use)
2724 {
2725 gui_update_cursor(force, clear_selection);
2726 gui_may_flush();
2727 }
2728#endif
2729}
2730
2731
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732/*
2733 * Sometimes a byte out of a multi-byte character is written with out_char().
2734 * To avoid flushing half of the character, call this function first.
2735 */
2736 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002737out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738{
2739 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2740 out_flush();
2741}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
2743#ifdef FEAT_GUI
2744/*
2745 * out_trash(): Throw away the contents of the output buffer
2746 */
2747 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002748out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749{
2750 out_pos = 0;
2751}
2752#endif
2753
2754/*
2755 * out_char(c): put a byte into the output buffer.
2756 * Flush it if it becomes full.
2757 * This should not be used for outputting text on the screen (use functions
2758 * like msg_puts() and screen_putchar() for that).
2759 */
2760 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002761out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762{
Bram Moolenaard0573012017-10-28 21:11:06 +02002763#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002764 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 out_char('\r');
2766#endif
2767
2768 out_buf[out_pos++] = c;
2769
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002770 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 if (out_pos >= OUT_SIZE || p_wd)
2772 out_flush();
2773}
2774
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002776 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002778 static int
2779out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002781 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783 if (out_pos >= OUT_SIZE)
2784 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002785 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786}
2787
2788/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002789 * A never-padding out_str().
2790 * Use this whenever you don't want to run the string through tputs().
2791 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 * is likely to strip off leading digits, that it mistakes for padding
2793 * information, and "%i", "%d", etc.
2794 * This should only be used for writing terminal codes, not for outputting
2795 * normal text (use functions like msg_puts() and screen_putchar() for that).
2796 */
2797 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002798out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002800 // avoid terminal strings being split up
2801 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002803
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 while (*s)
2805 out_char_nf(*s++);
2806
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002807 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 if (p_wd)
2809 out_flush();
2810}
2811
2812/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002813 * A conditional-flushing out_str, mainly for visualbell.
2814 * Handles a delay internally, because termlib may not respect the delay or do
2815 * it at the wrong time.
2816 * Note: Only for terminal strings.
2817 */
2818 void
2819out_str_cf(char_u *s)
2820{
2821 if (s != NULL && *s)
2822 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002823#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002824 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002825#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002826
2827#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002828 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002829 if (gui.in_use)
2830 {
2831 out_str_nf(s);
2832 return;
2833 }
2834#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002835 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002836 out_flush();
2837#ifdef HAVE_TGETENT
2838 for (p = s; *s; ++s)
2839 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002840 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002841 if (*s == '$' && *(s + 1) == '<')
2842 {
2843 char_u save_c = *s;
2844 int duration = atoi((char *)s + 2);
2845
2846 *s = NUL;
2847 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2848 *s = save_c;
2849 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002850# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002851 // Only sleep here if we can limit this happening in
2852 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002853 p = vim_strchr(s, '>');
2854 if (p == NULL || duration <= 0)
2855 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002856 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002857 p = s;
2858 }
2859 else
2860 {
2861 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002862 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002863 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002864# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002865 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002866 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002867# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002868 break;
2869 }
2870 }
2871 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2872#else
2873 while (*s)
2874 out_char_nf(*s++);
2875#endif
2876
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002877 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002878 if (p_wd)
2879 out_flush();
2880 }
2881}
2882
2883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002885 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 * This should only be used for writing terminal codes, not for outputting
2887 * normal text (use functions like msg_puts() and screen_putchar() for that).
2888 */
2889 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002890out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891{
2892 if (s != NULL && *s)
2893 {
2894#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002895 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 if (gui.in_use)
2897 {
2898 out_str_nf(s);
2899 return;
2900 }
2901#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002902 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002903 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 out_flush();
2905#ifdef HAVE_TGETENT
2906 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2907#else
2908 while (*s)
2909 out_char_nf(*s++);
2910#endif
2911
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002912 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 if (p_wd)
2914 out_flush();
2915 }
2916}
2917
2918/*
2919 * cursor positioning using termcap parser. (jw)
2920 */
2921 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002922term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
2924 OUT_STR(tgoto((char *)T_CM, col, row));
2925}
2926
2927 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002928term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929{
2930 OUT_STR(tgoto((char *)T_CRI, 0, i));
2931}
2932
2933 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002934term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935{
2936 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2937}
2938
2939 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002940term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941{
2942 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2943}
2944
2945#if defined(HAVE_TGETENT) || defined(PROTO)
2946 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002947term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002949 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 if (x < 0)
2951 x = 0;
2952 if (y < 0)
2953 y = 0;
2954 OUT_STR(tgoto((char *)T_CWP, y, x));
2955}
2956
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002957# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2958/*
2959 * Return TRUE if we can request the terminal for a response.
2960 */
2961 static int
2962can_get_termresponse()
2963{
2964 return cur_tmode == TMODE_RAW
2965 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002966# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002967 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002968# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002969 && p_ek;
2970}
2971
Bram Moolenaarafd78262019-05-10 23:10:31 +02002972/*
2973 * Set "status" to STATUS_SENT.
2974 */
2975 static void
2976termrequest_sent(termrequest_T *status)
2977{
2978 status->tr_progress = STATUS_SENT;
2979 status->tr_start = time(NULL);
2980}
2981
2982/*
2983 * Return TRUE if any of the requests are in STATUS_SENT.
2984 */
2985 static int
2986termrequest_any_pending()
2987{
2988 int i;
2989 time_t now = time(NULL);
2990
2991 for (i = 0; all_termrequests[i] != NULL; ++i)
2992 {
2993 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2994 {
2995 if (all_termrequests[i]->tr_start > 0 && now > 0
2996 && all_termrequests[i]->tr_start + 2 < now)
2997 // Sent the request more than 2 seconds ago and didn't get a
2998 // response, assume it failed.
2999 all_termrequests[i]->tr_progress = STATUS_FAIL;
3000 else
3001 return TRUE;
3002 }
3003 }
3004 return FALSE;
3005}
3006
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003007static int winpos_x = -1;
3008static int winpos_y = -1;
3009static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003010
Bram Moolenaar6bc93052019-04-06 20:00:19 +02003011# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003012/*
3013 * Try getting the Vim window position from the terminal.
3014 * Returns OK or FAIL.
3015 */
3016 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01003017term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003018{
3019 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003020 int prev_winpos_x = winpos_x;
3021 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003022
3023 if (*T_CGP == NUL || !can_get_termresponse())
3024 return FAIL;
3025 winpos_x = -1;
3026 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003027 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003028 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003029 OUT_STR(T_CGP);
3030 out_flush();
3031
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003032 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003033 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003034 {
3035 (void)vpeekc_nomap();
3036 if (winpos_x >= 0 && winpos_y >= 0)
3037 {
3038 *x = winpos_x;
3039 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003040 return OK;
3041 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01003042 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003043 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003044 // Do not reset "did_request_winpos", if we timed out the response might
3045 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003046
3047 winpos_x = prev_winpos_x;
3048 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02003049 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003050 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003051 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01003052 *x = winpos_x;
3053 *y = winpos_y;
3054 return OK;
3055 }
3056
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003057 return FALSE;
3058}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003059# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003060# endif
3061
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003063term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003065 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066}
3067#endif
3068
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003070term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003073 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003074 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003076 // Special handling of 16 colors, because termcap can't handle it
3077 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
3078 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003080 && ((s[0] == ESC && s[1] == '[')
3081#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3082 || (s[0] == ESC && s[1] == '|')
3083#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003084 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 && s[i] != NUL
3086 && (STRCMP(s + i + 1, "%p1%dm") == 0
3087 || STRCMP(s + i + 1, "%dm") == 0)
3088 && (s[i] == '3' || s[i] == '4'))
3089 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02003091 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02003093 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02003095 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003096#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003097 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003098#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003099 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02003100 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
3101 : (n >= 16 ? "48;5;" : "10");
3102
3103 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
3105 }
3106 else
3107 OUT_STR(tgoto((char *)s, 0, n));
3108}
3109
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003110 void
3111term_fg_color(int n)
3112{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003113 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003114 if (*T_CAF)
3115 term_color(T_CAF, n);
3116 else if (*T_CSF)
3117 term_color(T_CSF, n);
3118}
3119
3120 void
3121term_bg_color(int n)
3122{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003123 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003124 if (*T_CAB)
3125 term_color(T_CAB, n);
3126 else if (*T_CSB)
3127 term_color(T_CSB, n);
3128}
3129
Bram Moolenaare023e882020-05-31 16:42:30 +02003130 void
3131term_ul_color(int n)
3132{
3133 if (*T_CAU)
3134 term_color(T_CAU, n);
3135}
3136
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003137/*
3138 * Return "dark" or "light" depending on the kind of terminal.
3139 * This is just guessing! Recognized are:
3140 * "linux" Linux console
3141 * "screen.linux" Linux console with screen
3142 * "cygwin.*" Cygwin shell
3143 * "putty.*" Putty program
3144 * We also check the COLORFGBG environment variable, which is set by
3145 * rxvt and derivatives. This variable contains either two or three
3146 * values separated by semicolons; we want the last value in either
3147 * case. If this value is 0-6 or 8, our background is dark.
3148 */
3149 char_u *
3150term_bg_default(void)
3151{
3152#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003153 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003154 return (char_u *)"dark";
3155#else
3156 char_u *p;
3157
3158 if (STRCMP(T_NAME, "linux") == 0
3159 || STRCMP(T_NAME, "screen.linux") == 0
3160 || STRNCMP(T_NAME, "cygwin", 6) == 0
3161 || STRNCMP(T_NAME, "putty", 5) == 0
3162 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3163 && (p = vim_strrchr(p, ';')) != NULL
3164 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3165 && p[2] == NUL))
3166 return (char_u *)"dark";
3167 return (char_u *)"light";
3168#endif
3169}
3170
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003171#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003172
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003173#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3174#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3175#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003176
3177 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003178term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003179{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003180#define MAX_COLOR_STR_LEN 100
3181 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003182
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003183 if (*s == NUL)
3184 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003185 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003186 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003187#ifdef FEAT_VTP
Christopher Plewright38804d62022-11-09 23:55:52 +00003188 if (has_vtp_working())
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003189 {
3190 out_flush();
3191 buf[1] = '[';
3192 vtp_printf(buf);
3193 }
3194 else
3195#endif
3196 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003197}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003198
3199 void
3200term_fg_rgb_color(guicolor_T rgb)
3201{
Christopher Plewright38804d62022-11-09 23:55:52 +00003202 if (rgb != INVALCOLOR)
3203 term_rgb_color(T_8F, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003204}
3205
3206 void
3207term_bg_rgb_color(guicolor_T rgb)
3208{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003209 if (rgb != INVALCOLOR)
3210 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003211}
Bram Moolenaare023e882020-05-31 16:42:30 +02003212
3213 void
3214term_ul_rgb_color(guicolor_T rgb)
3215{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003216# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003217 // If the user explicitly sets t_8u then use it. Otherwise wait for
3218 // termresponse to be received, which is when t_8u would be set and a
3219 // redraw is needed if it was used.
3220 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003221 write_t_8u_state = MAYBE;
3222 else
3223# endif
3224 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003225}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003226#endif
3227
Bram Moolenaar651fca82021-11-29 20:39:38 +00003228#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229/*
3230 * Generic function to set window title, using t_ts and t_fs.
3231 */
3232 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003233term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003235 MAY_WANT_TO_LOG_THIS;
3236
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003237 // t_ts takes one argument: column in status line
3238 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003240 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 out_flush();
3242}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003243
3244/*
3245 * Tell the terminal to push (save) the title and/or icon, so that it can be
3246 * popped (restored) later.
3247 */
3248 void
3249term_push_title(int which)
3250{
Bram Moolenaar27821262019-05-08 16:41:09 +02003251 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003252 {
3253 OUT_STR(T_CST);
3254 out_flush();
3255 }
3256
Bram Moolenaar27821262019-05-08 16:41:09 +02003257 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003258 {
3259 OUT_STR(T_SSI);
3260 out_flush();
3261 }
3262}
3263
3264/*
3265 * Tell the terminal to pop the title and/or icon.
3266 */
3267 void
3268term_pop_title(int which)
3269{
Bram Moolenaar27821262019-05-08 16:41:09 +02003270 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003271 {
3272 OUT_STR(T_CRT);
3273 out_flush();
3274 }
3275
Bram Moolenaar27821262019-05-08 16:41:09 +02003276 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003277 {
3278 OUT_STR(T_SRI);
3279 out_flush();
3280 }
3281}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282#endif
3283
3284/*
3285 * Make sure we have a valid set or terminal options.
3286 * Replace all entries that are NULL by empty_option
3287 */
3288 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003289ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003291 char_u *env_colors;
3292
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003293 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294
3295 /*
3296 * MUST have "cm": cursor motion.
3297 */
3298 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003299 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300
3301 /*
3302 * if "cs" defined, use a scroll region, it's faster.
3303 */
3304 if (*T_CS != NUL)
3305 scroll_region = TRUE;
3306 else
3307 scroll_region = FALSE;
3308
3309 if (pairs)
3310 {
3311 /*
3312 * optional pairs
3313 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003314 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 if (*T_ME == NUL)
3316 T_ME = T_MR = T_MD = T_MB = empty_option;
3317 if (*T_SO == NUL || *T_SE == NUL)
3318 T_SO = T_SE = empty_option;
3319 if (*T_US == NUL || *T_UE == NUL)
3320 T_US = T_UE = empty_option;
3321 if (*T_CZH == NUL || *T_CZR == NUL)
3322 T_CZH = T_CZR = empty_option;
3323
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003324 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 if (*T_VE == NUL)
3326 T_VI = empty_option;
3327
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003328 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 if (*T_ME == NUL)
3330 {
3331 T_ME = T_SE;
3332 T_MR = T_SO;
3333 T_MD = T_SO;
3334 }
3335
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003336 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (*T_SO == NUL)
3338 {
3339 T_SE = T_ME;
3340 if (*T_MR == NUL)
3341 T_SO = T_MD;
3342 else
3343 T_SO = T_MR;
3344 }
3345
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003346 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 if (*T_CZH == NUL)
3348 {
3349 T_CZR = T_ME;
3350 if (*T_MR == NUL)
3351 T_CZH = T_MD;
3352 else
3353 T_CZH = T_MR;
3354 }
3355
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003356 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 if (*T_CSB == NUL || *T_CSF == NUL)
3358 {
3359 T_CSB = empty_option;
3360 T_CSF = empty_option;
3361 }
3362
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003363 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 if (*T_CAB == NUL || *T_CAF == NUL)
3365 {
3366 T_CAB = empty_option;
3367 T_CAF = empty_option;
3368 }
3369
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003370 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003372 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003374 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 p_wiv = (*T_XS != NUL);
3376 }
3377 need_gather = TRUE;
3378
Bram Moolenaar759d8152020-04-26 16:52:49 +02003379 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3380 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003382#ifdef FEAT_GUI
3383 if (!gui.in_use)
3384#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003385 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003386 env_colors = mch_getenv((char_u *)"COLORS");
3387 if (env_colors != NULL && isdigit(*env_colors))
3388 {
3389 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003390
Bram Moolenaar759d8152020-04-26 16:52:49 +02003391 if (colors != t_colors)
3392 set_color_count(colors);
3393 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395}
3396
3397#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3398 || defined(PROTO)
3399/*
3400 * Represent the given long_u as individual bytes, with the most significant
3401 * byte first, and store them in dst.
3402 */
3403 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003404add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
3406 int i;
3407 int shift;
3408
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003409 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 {
3411 shift = 8 * (sizeof(long_u) - i);
3412 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3413 }
3414}
3415
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416/*
3417 * Interpret the next string of bytes in buf as a long integer, with the most
3418 * significant byte first. Note that it is assumed that buf has been through
3419 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3420 * Puts result in val, and returns the number of bytes read from buf
3421 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3422 * were present.
3423 */
3424 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003425get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
3427 int len;
3428 char_u bytes[sizeof(long_u)];
3429 int i;
3430 int shift;
3431
3432 *val = 0;
3433 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3434 if (len != -1)
3435 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003436 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 {
3438 shift = 8 * (sizeof(long_u) - 1 - i);
3439 *val += (long_u)bytes[i] << shift;
3440 }
3441 }
3442 return len;
3443}
3444#endif
3445
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446/*
3447 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3448 * that buf has been through inchar(). Returns the actual number of bytes used
3449 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3450 * available.
3451 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003452 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003453get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
3455 int len = 0;
3456 int i;
3457 char_u c;
3458
3459 for (i = 0; i < num_bytes; i++)
3460 {
3461 if ((c = buf[len++]) == NUL)
3462 return -1;
3463 if (c == K_SPECIAL)
3464 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003465 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 return -1;
3467 if (buf[len++] == (int)KS_ZERO)
3468 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003469 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3470 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003471 if (buf[len++] == (int)KE_CSI)
3472 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003474 else if (c == CSI && buf[len] == KS_EXTRA
3475 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003476 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3477 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003478 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 bytes[i] = c;
3480 }
3481 return len;
3482}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
3484/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003485 * Check if the new shell size is valid, correct it if it's too small or way
3486 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 */
3488 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003489check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003491 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003493 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003494
3495 // make sure these values are not invalid
3496 if (cmdline_row >= Rows)
3497 cmdline_row = Rows - 1;
3498 if (msg_row >= Rows)
3499 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003500}
3501
3502/*
3503 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3504 */
3505 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003506limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003507{
3508 if (Columns < MIN_COLUMNS)
3509 Columns = MIN_COLUMNS;
3510 else if (Columns > 10000)
3511 Columns = 10000;
3512 if (Rows > 1000)
3513 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514}
3515
Bram Moolenaar86b68352004-12-27 21:59:20 +00003516/*
3517 * Invoked just before the screen structures are going to be (re)allocated.
3518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003520win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
3522 static int old_Rows = 0;
3523 static int old_Columns = 0;
3524
3525 if (old_Rows != Rows || old_Columns != Columns)
3526 ui_new_shellsize();
3527 if (old_Rows != Rows)
3528 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003529 // If 'window' uses the whole screen, keep it using that.
3530 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003531 if (p_window == old_Rows - 1
3532 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003533 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003535 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
3537 if (old_Columns != Columns)
3538 {
3539 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003540 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 }
3542}
3543
3544/*
3545 * Call this function when the Vim shell has been resized in any way.
3546 * Will obtain the current size and redraw (also when size didn't change).
3547 */
3548 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003549shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550{
3551 set_shellsize(0, 0, FALSE);
3552}
3553
3554/*
3555 * Check if the shell size changed. Handle a resize.
3556 * When the size didn't change, nothing happens.
3557 */
3558 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003559shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
3561 int old_Rows = Rows;
3562 int old_Columns = Columns;
3563
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003564 if (!exiting
3565#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003566 // Do not get the size when executing a shell command during
3567 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003568 && !gui.starting
3569#endif
3570 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003571 {
3572 (void)ui_get_shellsize();
3573 check_shellsize();
3574 if (old_Rows != Rows || old_Columns != Columns)
3575 shell_resized();
3576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577}
3578
3579/*
3580 * Set size of the Vim shell.
3581 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3582 * window size (this is used for the :win command).
3583 * If 'mustset' is FALSE, we may try to get the real window size and if
3584 * it fails use 'width' and 'height'.
3585 */
Bram Moolenaara787c242022-11-22 20:41:05 +00003586 static void
3587set_shellsize_inner(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588{
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003589 if (updating_screen)
3590 // resizing while in update_screen() may cause a crash
3591 return;
3592
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003593 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003594 // buffer (or window) has already been closed and removing a scrollbar
3595 // causes a resize event. Don't resize then, it will happen after entering
3596 // another buffer.
3597 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003598 return;
3599
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003601 out_flush(); // must do this before mch_get_shellsize() for
3602 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603#endif
3604
3605 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3606 {
3607 Rows = height;
3608 Columns = width;
3609 check_shellsize();
3610 ui_set_shellsize(mustset);
3611 }
3612 else
3613 check_shellsize();
3614
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003615 // The window layout used to be adjusted here, but it now happens in
3616 // screenalloc() (also invoked from screenclear()). That is because the
3617 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618
Bram Moolenaar24959102022-05-07 20:01:16 +01003619 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3620 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 screenclear();
3622 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003623 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
3625 if (starting != NO_SCREEN)
3626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 changed_line_abv_curs();
3630 invalidate_botline();
3631
3632 /*
3633 * We only redraw when it's needed:
3634 * - While at the more prompt or executing an external command, don't
3635 * redraw, but position the cursor.
3636 * - While editing the command line, only redraw that.
3637 * - in Ex mode, don't redraw anything.
3638 * - Otherwise, redraw right now, and position the cursor.
3639 * Always need to call update_screen() or screenalloc(), to make
3640 * sure Rows/Columns and the size of ScreenLines[] is correct!
3641 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003642 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3643 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
3645 screenalloc(FALSE);
3646 repeat_message();
3647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 else
3649 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003650 if (curwin->w_p_scb)
3651 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003652 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003653 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003654 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003655 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003656 }
3657 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003658 {
3659 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003660 if (pum_visible())
3661 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003662 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003663 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003664 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003665 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003666 if (redrawing())
3667 setcursor();
3668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003670 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
3672 out_flush();
Bram Moolenaara787c242022-11-22 20:41:05 +00003673}
3674
3675 void
3676set_shellsize(int width, int height, int mustset)
3677{
3678 static int busy = FALSE;
3679 static int do_run = FALSE;
3680
3681 if (width < 0 || height < 0) // just checking...
3682 return;
3683
3684 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
3685 {
3686 // postpone the resizing
3687 State = MODE_SETWSIZE;
3688 return;
3689 }
3690
3691 // Avoid recursiveness. This can happen when setting the window size
3692 // causes another window-changed signal or when two SIGWINCH signals come
3693 // very close together. There needs to be another run then after the
3694 // current one is done to pick up the latest size.
3695 do_run = TRUE;
3696 if (busy)
3697 return;
3698
3699 while (do_run)
3700 {
3701 do_run = FALSE;
3702 busy = TRUE;
3703 set_shellsize_inner(width, height, mustset);
3704 busy = FALSE;
3705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706}
3707
3708/*
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003709 * Output T_CTE, the t_TE termcap entry, and handle expected effects.
3710 * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
3711 */
3712 void
3713out_str_t_TE(void)
3714{
3715 out_str(T_CTE);
3716
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003717 // The seenModifyOtherKeys flag is not reset here. We do expect t_TE to
Bram Moolenaarc255b782022-11-26 19:16:48 +00003718 // disable modifyOtherKeys, but until Xterm version 377 there is no way to
3719 // detect it's enabled again after the following t_TI. We assume that when
3720 // seenModifyOtherKeys was set before it will still be valid.
3721
3722 // When the modifyOtherKeys level is detected to be 2 we expect t_TE to
3723 // disable it. Remembering that it was detected to be enabled is useful in
3724 // some situations.
3725 // The following t_TI is expected to request the state and then
3726 // modify_otherkeys_state will be set again.
3727 if (modify_otherkeys_state == MOKS_ENABLED
3728 || modify_otherkeys_state == MOKS_DISABLED)
3729 modify_otherkeys_state = MOKS_DISABLED;
3730 else if (modify_otherkeys_state != MOKS_INITIAL)
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003731 modify_otherkeys_state = MOKS_AFTER_T_TE;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003732
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003733 // When the kitty keyboard protocol is enabled we expect t_TE to disable
3734 // it. Remembering that it was detected to be enabled is useful in some
3735 // situations.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00003736 // The following t_TI is expected to request the state and then
3737 // kitty_protocol_state will be set again.
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003738 if (kitty_protocol_state == KKPS_ENABLED
3739 || kitty_protocol_state == KKPS_DISABLED)
3740 kitty_protocol_state = KKPS_DISABLED;
3741 else
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003742 kitty_protocol_state = KKPS_AFTER_T_TE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003743}
3744
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003745static int send_t_RK = FALSE;
3746
3747/*
3748 * Output T_TI and setup for what follows.
3749 */
3750 void
3751out_str_t_TI(void)
3752{
3753 out_str(T_CTI);
3754
3755 // Send t_RK when there is no more work to do.
3756 send_t_RK = TRUE;
3757}
3758
3759/*
3760 * If t_TI was recently sent and there is no typeahead or work to do, now send
3761 * t_RK. This is postponed to avoid the response arriving in a shell command
3762 * or after Vim exits.
3763 */
3764 void
3765may_send_t_RK(void)
3766{
3767 if (send_t_RK
3768 && !work_pending()
3769 && !ex_normal_busy
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003770#ifdef FEAT_EVAL
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003771 && !in_feedkeys
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003772#endif
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003773 && !exiting)
3774 {
3775 send_t_RK = FALSE;
3776 out_str(T_CRK);
3777 out_flush();
3778 }
3779}
3780
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3783 * commands and Ex mode).
3784 */
3785 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003786settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787{
3788#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003789 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 if (gui.in_use)
3791 return;
3792#endif
3793
3794 if (full_screen)
3795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003797 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3798 * set the terminal to raw mode, even though we think it already is,
3799 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 * When we think the terminal is normal, don't try to set it to
3801 * normal again, because that causes problems (logout!) on some
3802 * machines.
3803 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003804 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 {
3806#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003807# ifdef FEAT_GUI
3808 if (!gui.in_use && !gui.starting)
3809# endif
3810 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003811 // May need to check for T_CRV response and termcodes, it
3812 // doesn't work in Cooked mode, an external program may get
3813 // them.
3814 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003815 (void)vpeekc_nomap();
3816 check_for_codes_from_term();
3817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003820 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003821
3822 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3823 // Avoid doing this too often, on some terminals the codes are not
3824 // handled properly.
3825 if (termcap_active && tmode != TMODE_SLEEP
3826 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003827 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003828 MAY_WANT_TO_LOG_THIS;
3829
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003830 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003831 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003832 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003833 out_str_t_TE(); // possibly disables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003834 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003835 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003836 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003837 out_str(T_BE); // enable bracketed paste mode (should
3838 // be before mch_settmode().
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003839 out_str_t_TI(); // possibly enables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003840 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003843 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003846 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 out_flush();
3848 }
3849#ifdef FEAT_TERMRESPONSE
3850 may_req_termresponse();
3851#endif
3852 }
3853}
3854
3855 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003856starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857{
3858 if (full_screen && !termcap_active)
3859 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003860 MAY_WANT_TO_LOG_THIS;
3861
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003862 out_str(T_TI); // start termcap mode
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003863 out_str_t_TI(); // start "raw" mode
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003864 out_str(T_KS); // start "keypad transmit" mode
3865 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003866
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003867#if defined(UNIX) || defined(VMS)
3868 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003869 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003870 out_str(T_FE);
3871#endif
3872
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 out_flush();
3874 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003875 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003877# ifdef FEAT_GUI
3878 if (!gui.in_use && !gui.starting)
3879# endif
3880 {
3881 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003882 // Immediately check for a response. If t_Co changes, we don't
3883 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003884 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003885 check_for_codes_from_term();
3886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887#endif
3888 }
3889}
3890
3891 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003892stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893{
3894 screen_stop_highlight();
3895 reset_cterm_colors();
3896 if (termcap_active)
3897 {
3898#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003899# ifdef FEAT_GUI
3900 if (!gui.in_use && !gui.starting)
3901# endif
3902 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003903 // May need to discard T_CRV, T_U7 or T_RBG response.
3904 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003905 {
3906# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003907 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003908 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003909# endif
3910# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003911 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003912 if (exiting)
3913 tcflush(fileno(stdin), TCIFLUSH);
3914# endif
3915 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003916 // Check for termcodes first, otherwise an external program may
3917 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003918 check_for_codes_from_term();
3919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003921 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003922
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003923#if defined(UNIX) || defined(VMS)
3924 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003925 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003926 out_str(T_FD);
3927#endif
3928
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003929 out_str(T_BD); // disable bracketed paste mode
3930 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 out_flush();
3932 termcap_active = FALSE;
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00003933
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003934 // Output t_te before t_TE, t_te may switch between main and alternate
3935 // screen and following codes may work on the active screen only.
3936 //
3937 // When using the Kitty keyboard protocol the main and alternate screen
3938 // use a separate state. If we are (or were) using the Kitty keyboard
3939 // protocol and t_te is not empty (possibly switching screens) then
3940 // output t_TE both before and after outputting t_te.
3941 if (*T_TE != NUL && (kitty_protocol_state == KKPS_ENABLED
3942 || kitty_protocol_state == KKPS_DISABLED))
3943 out_str_t_TE(); // probably disables the kitty keyboard
3944 // protocol
3945
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00003946 out_str(T_TE); // stop termcap mode
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003947 cursor_on(); // just in case it is still off
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003948 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
3949 // Kitty keyboard protocol
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003950 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 out_flush();
3952 }
3953}
3954
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003955#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956/*
3957 * Request version string (for xterm) when needed.
3958 * Only do this after switching to raw mode, otherwise the result will be
3959 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003960 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003961 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 * Only do this after termcap mode has been started, otherwise the codes for
3963 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003964 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3965 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003966 * On Unix only do it when both output and input are a tty (avoid writing
3967 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 * The result is caught in check_termcode().
3969 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003970 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003971may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003973 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003974 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003975 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 && *T_CRV != NUL)
3977 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003978 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003979 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003981 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003982 // check for the characters now, otherwise they might be eaten by
3983 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 out_flush();
3985 (void)vpeekc_nomap();
3986 }
3987}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003988
Bram Moolenaar9584b312013-03-13 19:29:28 +01003989/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003990 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3991 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003992 * Note that this goes out before T_CRV, so that the result can be used when
3993 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003994 */
3995 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003996check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003997{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003998 int did_send = FALSE;
3999
4000 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
4001 return;
4002
Bram Moolenaarafd78262019-05-10 23:10:31 +02004003 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01004004 && !option_was_set((char_u *)"ambiwidth"))
4005 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004006 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01004007
Bram Moolenaara45551a2020-06-09 15:57:37 +02004008 // Ambiguous width check.
4009 // Check how the terminal treats ambiguous character width (UAX #11).
4010 // First, we move the cursor to (1, 0) and print a test ambiguous
4011 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
4012 // the current cursor position. If the terminal treats \u25bd as
4013 // single width, the position is (1, 1), or if it is treated as double
4014 // width, that will be (1, 2). This function has the side effect that
4015 // changes cursor position, so it must be called immediately after
4016 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004017 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004018 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004019 // Do this in the second row. In the first row the returned sequence
4020 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004021 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004022 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02004023 out_str(buf);
4024 out_str(T_U7);
4025 termrequest_sent(&u7_status);
4026 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02004027 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02004028
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004029 // This overwrites a few characters on the screen, a redraw is needed
4030 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02004031 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02004032 term_windgoto(1, 0);
4033 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004034 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004035 }
4036
Bram Moolenaard1f34e62022-01-07 19:24:20 +00004037 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02004038 {
4039 // 2. Check compatibility with xterm.
4040 // We move the cursor to (2, 0), print a test sequence and then query
4041 // the current cursor position. If the terminal properly handles
4042 // unknown DCS string and CSI sequence with intermediate byte, the test
4043 // sequence is ignored and the cursor does not move. If the terminal
4044 // handles test sequence incorrectly, a garbage string is displayed and
4045 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004046 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004047 LOG_TR(("Sending xterm compatibility test sequence."));
4048 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004049 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02004050 term_windgoto(2, 0);
4051 // send the test DCS string.
4052 out_str((char_u *)"\033Pzz\033\\");
4053 // send the test CSI sequence with intermediate byte.
4054 out_str((char_u *)"\033[0%m");
4055 out_str(T_U7);
4056 termrequest_sent(&xcc_status);
4057 out_flush();
4058 did_send = TRUE;
4059
4060 // If the terminal handles test sequence incorrectly, garbage text is
4061 // displayed. Clear them out for now.
4062 screen_stop_highlight();
4063 term_windgoto(2, 0);
4064 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004065 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004066 }
4067
4068 if (did_send)
4069 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004070 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02004071
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004072 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004073 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01004074
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004075 // check for the characters now, otherwise they might be eaten by
4076 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02004077 out_flush();
4078 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01004079 }
4080}
Bram Moolenaar2951b772013-07-03 12:45:31 +02004081
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004082/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02004083 * Similar to requesting the version string: Request the terminal background
4084 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004085 */
4086 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004087may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004088{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004089 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004090 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004091 int didit = FALSE;
4092
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004093# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004094 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004095 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004096 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004097 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004098 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004099 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004100 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004101 didit = TRUE;
4102 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004103# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004104
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004105 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004106 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004107 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004108 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004109 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004110 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004111 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004112 didit = TRUE;
4113 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004114
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004115 if (didit)
4116 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004117 // check for the characters now, otherwise they might be eaten by
4118 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004119 out_flush();
4120 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004121 }
4122 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004123}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004124
Bram Moolenaar2951b772013-07-03 12:45:31 +02004125# ifdef DEBUG_TERMRESPONSE
4126 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02004127log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004128{
4129 static FILE *fd_tr = NULL;
4130 static proftime_T start;
4131 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004132 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004133
4134 if (fd_tr == NULL)
4135 {
4136 fd_tr = fopen("termresponse.log", "w");
4137 profile_start(&start);
4138 }
4139 now = start;
4140 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02004141 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004142 must_redraw == UPD_NOT_VALID ? "NV"
4143 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02004144 va_start(ap, fmt);
4145 vfprintf(fd_tr, fmt, ap);
4146 va_end(ap);
4147 fputc('\n', fd_tr);
4148 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02004149}
4150# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151#endif
4152
4153/*
4154 * Return TRUE when saving and restoring the screen.
4155 */
4156 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004157swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158{
4159 return (full_screen && *T_TI != NUL);
4160}
4161
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162/*
4163 * By outputting the 'cursor very visible' termcap code, for some windowed
4164 * terminals this makes the screen scrolled to the correct position.
4165 * Used when starting Vim or returning from a shell.
4166 */
4167 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004168scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004170 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004172 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004174 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004175 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177}
4178
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004179// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180static int cursor_is_off = FALSE;
4181
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004182// True if cursor is not visible due to an ongoing cursor-less sleep
4183static int cursor_is_asleep = FALSE;
4184
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004186 * Enable the cursor without checking if it's already enabled.
4187 */
4188 void
4189cursor_on_force(void)
4190{
4191 out_str(T_VE);
4192 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004193 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004194}
4195
4196/*
4197 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 */
4199 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004200cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004202 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004203 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204}
4205
4206/*
4207 * Disable the cursor.
4208 */
4209 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004210cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004212 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004214 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 cursor_is_off = TRUE;
4216 }
4217}
4218
Dominique Pelle748b3082022-01-08 12:41:16 +00004219#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004220/*
4221 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4222 */
4223 int
4224cursor_is_sleeping(void)
4225{
4226 return cursor_is_asleep;
4227}
Dominique Pelle748b3082022-01-08 12:41:16 +00004228#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004229
4230/*
4231 * Disable the cursor and mark it disabled by cursor-less sleep
4232 */
4233 void
4234cursor_sleep(void)
4235{
4236 cursor_is_asleep = TRUE;
4237 cursor_off();
4238}
4239
4240/*
4241 * Enable the cursor and mark it not disabled by cursor-less sleep
4242 */
4243 void
4244cursor_unsleep(void)
4245{
4246 cursor_is_asleep = FALSE;
4247 cursor_on();
4248}
4249
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004250#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004252 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004253 */
4254 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004255term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004256{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004257 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004258 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004259
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004260 // Only do something when redrawing the screen and we can restore the
4261 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004262 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004263 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004264# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004265 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004266 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004267 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004268# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004269 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004270 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004271
Bram Moolenaar24959102022-05-07 20:01:16 +01004272 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004273 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004274 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004275 {
4276 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004277 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004278 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004279 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004280 if (*p != NUL)
4281 {
4282 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004283 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004284 }
4285 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004286 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004287 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004288 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004289 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004290 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004291 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004292 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004293 }
4294 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004295 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004296 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004297 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004298 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004299 }
4300}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004301
4302# if defined(FEAT_TERMINAL) || defined(PROTO)
4303 void
4304term_cursor_color(char_u *color)
4305{
4306 if (*T_CSC != NUL)
4307 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004308 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004309 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004310 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004311 out_flush();
4312 }
4313}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004314# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004315
Bram Moolenaar4db25542017-08-28 22:43:05 +02004316 int
4317blink_state_is_inverted()
4318{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004319#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004320 return rbm_status.tr_progress == STATUS_GOT
4321 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004322 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004323#else
4324 return FALSE;
4325#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004326}
4327
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004328/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004329 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004330 */
4331 void
4332term_cursor_shape(int shape, int blink)
4333{
4334 if (*T_CSH != NUL)
4335 {
4336 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4337 out_flush();
4338 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004339 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004340 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004341 int do_blink = blink;
4342
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004343 // t_SH is empty: try setting just the blink state.
4344 // The blink flags are XORed together, if the initial blinking from
4345 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004346 if (blink_state_is_inverted())
4347 do_blink = !blink;
4348
4349 if (do_blink && *T_VS != NUL)
4350 {
4351 out_str(T_VS);
4352 out_flush();
4353 }
4354 else if (!do_blink && *T_CVS != NUL)
4355 {
4356 out_str(T_CVS);
4357 out_flush();
4358 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004359 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004360}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004361#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004362
4363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 * Set scrolling region for window 'wp'.
4365 * The region starts 'off' lines from the start of the window.
4366 * Also set the vertical scroll region for a vertically split window. Always
4367 * the full width of the window, excluding the vertical separator.
4368 */
4369 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004370scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371{
4372 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4373 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004375 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4376 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004377 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378}
4379
4380/*
4381 * Reset scrolling region to the whole screen.
4382 */
4383 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004384scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385{
4386 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 if (*T_CSV != NUL)
4388 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004389 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390}
4391
4392
4393/*
4394 * List of terminal codes that are currently recognized.
4395 */
4396
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004397static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004399 char_u name[2]; // termcap name of entry
4400 char_u *code; // terminal code (in allocated memory)
4401 int len; // STRLEN(code)
4402 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403} *termcodes = NULL;
4404
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004405static int tc_max_len = 0; // number of entries that termcodes[] can hold
4406static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004408static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004409
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004411clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412{
4413 while (tc_len > 0)
4414 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004415 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 tc_max_len = 0;
4417
4418#ifdef HAVE_TGETENT
4419 BC = (char *)empty_option;
4420 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004421 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 ospeed = 0;
4423#endif
4424
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004425 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426}
4427
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004428#define ATC_FROM_TERM 55
4429
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430/*
Bram Moolenaarb2646172022-12-17 15:35:43 +00004431 * Add a new entry for "name[2]" to the list of terminal codes.
4432 * Note that "name" may not have a terminating NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004434 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4435 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 */
4437 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004438add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439{
4440 struct termcode *new_tc;
4441 int i, j;
4442 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004443 int len;
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004444#ifdef FEAT_EVAL
4445 char *action = "Setting";
4446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
4448 if (string == NULL || *string == NUL)
4449 {
4450 del_termcode(name);
4451 return;
4452 }
4453
Bram Moolenaar4f974752019-02-17 17:44:42 +01004454#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004455 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004456#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004457# ifdef VIMDLL
4458 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004459 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004460 else
4461# endif
4462 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 if (s == NULL)
4465 return;
4466
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004467 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004468 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004470 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 s[0] = term_7to8bit(string);
4472 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004473
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004474#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4475# ifdef VIMDLL
4476 if (!gui.in_use)
4477# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004478 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004479 if (s[0] == K_NUL)
4480 {
4481 STRMOVE(s + 1, s);
4482 s[1] = 3;
4483 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004484 }
4485#endif
4486
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004487 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004489 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490
4491 /*
4492 * need to make space for more entries
4493 */
4494 if (tc_len == tc_max_len)
4495 {
4496 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004497 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 if (new_tc == NULL)
4499 {
4500 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004501 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 return;
4503 }
4504 for (i = 0; i < tc_len; ++i)
4505 new_tc[i] = termcodes[i];
4506 vim_free(termcodes);
4507 termcodes = new_tc;
4508 }
4509
4510 /*
4511 * Look for existing entry with the same name, it is replaced.
4512 * Look for an existing entry that is alphabetical higher, the new entry
4513 * is inserted in front of it.
4514 */
4515 for (i = 0; i < tc_len; ++i)
4516 {
4517 if (termcodes[i].name[0] < name[0])
4518 continue;
4519 if (termcodes[i].name[0] == name[0])
4520 {
4521 if (termcodes[i].name[1] < name[1])
4522 continue;
4523 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004524 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 */
4526 if (termcodes[i].name[1] == name[1])
4527 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004528 if (flags == ATC_FROM_TERM && (j = termcode_star(
4529 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004530 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004531 // Don't replace ESC[123;*X or ESC O*X with another when
4532 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004533 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004534 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004535 && s[len - 1]
4536 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004537 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004538 // They are equal but for the ";*": don't add it.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004539#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004540 ch_log(NULL, "Termcap entry %c%c did not change",
4541 name[0], name[1]);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004542#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004543 vim_free(s);
4544 return;
4545 }
4546 }
4547 else
4548 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004549 // Replace old code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004550#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004551 ch_log(NULL, "Termcap entry %c%c was: %s",
4552 name[0], name[1], termcodes[i].code);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004553#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004554 vim_free(termcodes[i].code);
4555 --tc_len;
4556 break;
4557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 }
4559 }
4560 /*
4561 * Found alphabetical larger entry, move rest to insert new entry
4562 */
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004563#ifdef FEAT_EVAL
4564 action = "Adding";
4565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 for (j = tc_len; j > i; --j)
4567 termcodes[j] = termcodes[j - 1];
4568 break;
4569 }
4570
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004571#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004572 ch_log(NULL, "%s termcap entry %c%c to %s", action, name[0], name[1], s);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 termcodes[i].name[0] = name[0];
4575 termcodes[i].name[1] = name[1];
4576 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004577 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004578
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004579 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4580 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004581 termcodes[i].modlen = 0;
4582 j = termcode_star(s, len);
4583 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004584 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004585 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004586 // For "CSI[@;X" the "@" is not included in "modlen".
4587 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4588 --termcodes[i].modlen;
4589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 ++tc_len;
4591}
4592
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004593/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004594 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004595 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004596 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004597 */
4598 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004599termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004600{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004601 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004602 if (len >= 3 && code[len - 2] == '*')
4603 {
4604 if (len >= 5 && code[len - 3] == ';')
4605 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004606 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004607 return 1;
4608 }
4609 return 0;
4610}
4611
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004613find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614{
4615 int i;
4616
4617 for (i = 0; i < tc_len; ++i)
4618 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4619 return termcodes[i].code;
4620 return NULL;
4621}
4622
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004624get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625{
4626 if (i >= tc_len)
4627 return NULL;
4628 return &termcodes[i].name[0];
4629}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004631/*
4632 * Returns the length of the terminal code at index 'idx'.
4633 */
4634 int
4635get_termcode_len(int idx)
4636{
4637 return termcodes[idx].len;
4638}
4639
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004640 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004641del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642{
4643 int i;
4644
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004645 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 return;
4647
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004648 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
4650 for (i = 0; i < tc_len; ++i)
4651 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4652 {
4653 del_termcode_idx(i);
4654 return;
4655 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004656 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657}
4658
4659 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004660del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661{
4662 int i;
4663
4664 vim_free(termcodes[idx].code);
4665 --tc_len;
4666 for (i = idx; i < tc_len; ++i)
4667 termcodes[i] = termcodes[i + 1];
4668}
4669
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670/*
4671 * Called when detected that the terminal sends 8-bit codes.
4672 * Convert all 7-bit codes to their 8-bit equivalent.
4673 */
4674 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004675switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676{
4677 int i;
4678 int c;
4679
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004680 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 if (!term_is_8bit(T_NAME))
4682 {
4683 for (i = 0; i < tc_len; ++i)
4684 {
4685 c = term_7to8bit(termcodes[i].code);
4686 if (c != 0)
4687 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004688 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 termcodes[i].code[0] = c;
4690 }
4691 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004692 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004695 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697
4698#ifdef CHECK_DOUBLE_CLICK
4699static linenr_T orig_topline = 0;
4700# ifdef FEAT_DIFF
4701static int orig_topfill = 0;
4702# endif
4703#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004704#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004706 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 * "orig_topline" is used to avoid detecting a double-click when the window
4708 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4709 */
4710/*
4711 * Set orig_topline. Used when jumping to another window, so that a double
4712 * click still works.
4713 */
4714 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004715set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
4717 orig_topline = wp->w_topline;
4718# ifdef FEAT_DIFF
4719 orig_topfill = wp->w_topfill;
4720# endif
4721}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004722
4723/*
4724 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4725 * topline and topfill.
4726 */
4727 int
4728is_mouse_topline(win_T *wp)
4729{
4730 return orig_topline == wp->w_topline
4731#ifdef FEAT_DIFF
4732 && orig_topfill == wp->w_topfill
4733#endif
4734 ;
4735}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736#endif
4737
4738/*
zeertzjqfc78a032022-04-26 22:11:38 +01004739 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4740 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4741 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004742 * Remove "slen" bytes.
4743 * Returns FAIL for error.
4744 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004745 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004746put_string_in_typebuf(
4747 int offset,
4748 int slen,
4749 char_u *string,
4750 int new_slen,
4751 char_u *buf,
4752 int bufsize,
4753 int *buflen)
4754{
4755 int extra = new_slen - slen;
4756
4757 string[new_slen] = NUL;
4758 if (buf == NULL)
4759 {
4760 if (extra < 0)
4761 // remove matched chars, taking care of noremap
4762 del_typebuf(-extra, offset);
4763 else if (extra > 0)
4764 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004765 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4766 == FAIL)
4767 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004768
4769 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4770 // typebuf.tb_buf[]!
4771 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4772 (size_t)new_slen);
4773 }
4774 else
4775 {
4776 if (extra < 0)
4777 // remove matched characters
4778 mch_memmove(buf + offset, buf + offset - extra,
4779 (size_t)(*buflen + offset + extra));
4780 else if (extra > 0)
4781 {
4782 // Insert the extra space we need. If there is insufficient
4783 // space return -1.
4784 if (*buflen + extra + new_slen >= bufsize)
4785 return FAIL;
4786 mch_memmove(buf + offset + extra, buf + offset,
4787 (size_t)(*buflen - offset));
4788 }
4789 mch_memmove(buf + offset, string, (size_t)new_slen);
4790 *buflen = *buflen + extra + new_slen;
4791 }
4792 return OK;
4793}
4794
4795/*
4796 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4797 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004798 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004799decode_modifiers(int n)
4800{
4801 int code = n - 1;
4802 int modifiers = 0;
4803
4804 if (code & 1)
4805 modifiers |= MOD_MASK_SHIFT;
4806 if (code & 2)
4807 modifiers |= MOD_MASK_ALT;
4808 if (code & 4)
4809 modifiers |= MOD_MASK_CTRL;
4810 if (code & 8)
4811 modifiers |= MOD_MASK_META;
Bram Moolenaar064fd672022-11-29 18:32:32 +00004812 // Any further modifiers are silently dropped.
4813
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004814 return modifiers;
4815}
4816
4817 static int
4818modifiers2keycode(int modifiers, int *key, char_u *string)
4819{
4820 int new_slen = 0;
4821
4822 if (modifiers != 0)
4823 {
4824 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004825 // make mappings work. This may result in a special key, such as
4826 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004827 *key = simplify_key(*key, &modifiers);
4828 if (modifiers != 0)
4829 {
4830 string[new_slen++] = K_SPECIAL;
4831 string[new_slen++] = (int)KS_MODIFIER;
4832 string[new_slen++] = modifiers;
4833 }
4834 }
4835 return new_slen;
4836}
4837
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004838/*
4839 * Handle a cursor position report.
4840 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004841 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004842handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004843{
4844 if (arg[0] == 2 && arg[1] >= 2)
4845 {
4846 char *aw = NULL;
4847
4848 LOG_TR(("Received U7 status: %s", tp));
4849 u7_status.tr_progress = STATUS_GOT;
4850 did_cursorhold = TRUE;
4851 if (arg[1] == 2)
4852 aw = "single";
4853 else if (arg[1] == 3)
4854 aw = "double";
4855 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4856 {
4857 // Setting the option causes a screen redraw. Do
4858 // that right away if possible, keeping any
4859 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004860 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004861#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004862 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004863 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004864
4865 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4866 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004867#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004868 redraw_asap(UPD_CLEAR);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004869#endif
4870#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004871 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004872#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004873 }
4874 }
4875 else if (arg[0] == 3)
4876 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004877 int value;
4878
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004879 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004880 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004881
4882 // Third row: xterm compatibility test.
4883 // If the cursor is on the first column then the terminal can handle
4884 // the request for cursor style and blinking.
4885 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4886 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4887 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004888 }
4889}
4890
4891/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004892 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004893 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004894 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004895 */
4896 static void
4897handle_version_response(int first, int *arg, int argc, char_u *tp)
4898{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004899 // The xterm version. It is set to zero when it can't be an actual xterm
4900 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004901 int version = arg[1];
4902
4903 LOG_TR(("Received CRV response: %s", tp));
4904 crv_status.tr_progress = STATUS_GOT;
4905 did_cursorhold = TRUE;
4906
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004907 // Reset terminal properties that are set based on the termresponse.
4908 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004909 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004910 init_term_props(
4911#ifdef FEAT_EVAL
4912 reset_term_props_on_termresponse
4913#else
4914 FALSE
4915#endif
4916 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004917
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004918 // If this code starts with CSI, you can bet that the
4919 // terminal uses 8-bit codes.
4920 if (tp[0] == CSI)
4921 switch_to_8bit();
4922
4923 // Screen sends 40500.
4924 // rxvt sends its version number: "20703" is 2.7.3.
4925 // Ignore it for when the user has set 'term' to xterm,
4926 // even though it's an rxvt.
4927 if (version > 20000)
4928 version = 0;
4929
zeertzjqfc78a032022-04-26 22:11:38 +01004930 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004931 if (first == '>' && argc == 3)
4932 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004933 // mintty 2.9.5 sends 77;20905;0c.
4934 // (77 is ASCII 'M' for mintty.)
4935 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004936 {
4937 // mintty can do SGR mouse reporting
4938 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4939 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004940
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004941#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004942 // If xterm version >= 141 try to get termcap codes. For other
4943 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004944 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004945 {
4946 LOG_TR(("Enable checking for XT codes"));
4947 check_for_codes = TRUE;
4948 need_gather = TRUE;
4949 req_codes_from_term();
4950 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004951#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004952
4953 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01004954 // Konsole sends 0;115;0 and works the same way
4955 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004956 {
4957 // If run from Vim $COLORS is set to the number of
4958 // colors the terminal supports. Otherwise assume
4959 // 256, libvterm supports even more.
4960 if (mch_getenv((char_u *)"COLORS") == NULL)
4961 may_adjust_color_count(256);
4962 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004963 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004964 }
4965
4966 if (version == 95)
4967 {
4968 // Mac Terminal.app sends 1;95;0
4969 if (arg[0] == 1 && arg[2] == 0)
4970 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004971 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4972 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004973 }
4974 // iTerm2 sends 0;95;0
4975 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004976 {
4977 // iTerm2 can do SGR mouse reporting
4978 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4979 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004980 // old iTerm2 sends 0;95;
4981 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004982 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004983 }
4984
4985 // screen sends 83;40500;0 83 is 'S' in ASCII.
4986 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004987 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004988 // screen supports SGR mouse codes since 4.7.0
4989 if (arg[1] >= 40700)
4990 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4991 else
4992 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4993 }
4994
4995 // If no recognized terminal has set mouse behavior, assume xterm.
4996 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4997 {
4998 // Xterm version 277 supports SGR.
4999 // Xterm version >= 95 supports mouse dragging.
5000 if (version >= 277)
5001 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005002 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005003 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005004 }
5005
5006 // Detect terminals that set $TERM to something like
5007 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005008 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005009 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02005010 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005011 // xfce4-terminal sends 1;2802;0.
5012 // screen sends 83;40500;0
5013 // Assuming any version number over 2500 is not an
5014 // xterm (without the limit for rxvt and screen).
5015 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005016 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005017
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005018 else if (version == 136 && arg[2] == 0)
5019 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005020 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005021
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005022 // PuTTY sends 0;136;0
5023 if (arg[0] == 0)
5024 {
5025 // supports sgr-like mouse reporting.
5026 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5027 }
5028 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005029 }
5030
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005031 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
5032 // commented out.
5033 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
5034 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005035
Bram Moolenaar1573e732022-11-16 20:33:21 +00005036 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005037 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
5038 {
5039 term_props[TPR_KITTY].tpr_status = TPR_YES;
5040 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
Bram Moolenaar731d0072022-12-18 17:47:18 +00005041
5042 // Kitty can handle SGR mouse reporting.
5043 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005044 }
5045
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005046 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005047 // 30600/40500 is a version number of GNU screen. DA2 support is added
5048 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
5049 // compatibility checking does not detect GNU screen.
5050 if (arg[0] == 83 && arg[1] >= 30600)
5051 {
5052 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5053 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
5054 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005055
5056 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005057 // 95, so assume anything below 95 is not xterm and hopefully supports
5058 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005059 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005060 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005061
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005062 // Getting the cursor style is only supported properly by xterm since
5063 // version 279 (otherwise it returns 0x18).
5064 if (version < 279)
5065 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5066
5067 /*
5068 * Take action on the detected properties.
5069 */
5070
5071 // Unless the underline RGB color is expected to work, disable "t_8u".
5072 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005073 // This may cause some flicker. Alternative would be to set "t_8u"
5074 // here if the terminal is expected to support it, but that might
5075 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01005076 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
5077 && *T_8U != NUL
5078 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005079 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02005080 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
5081 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005082 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005083#ifdef FEAT_TERMRESPONSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01005084 if (*T_8U != NUL && write_t_8u_state == MAYBE)
5085 // Did skip writing t_8u, a complete redraw is needed.
5086 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01005087 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005088#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005089
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005090 // Only set 'ttymouse' automatically if it was not set
5091 // by the user already.
5092 if (!option_was_set((char_u *)"ttym")
5093 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
5094 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
5095 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005096 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005097 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
5098 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
5099 }
5100
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005101#ifdef FEAT_TERMRESPONSE
5102 int need_flush = FALSE;
5103
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005104 // Only request the cursor style if t_SH and t_RS are
5105 // set. Only supported properly by xterm since version
5106 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005107 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005108 // Not for Terminal.app, it can't handle t_RS, it
5109 // echoes the characters to the screen.
5110 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005111 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005112 && *T_CSH != NUL
5113 && *T_CRS != NUL)
5114 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005115 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005116 LOG_TR(("Sending cursor style request"));
5117 out_str(T_CRS);
5118 termrequest_sent(&rcs_status);
5119 need_flush = TRUE;
5120 }
5121
5122 // Only request the cursor blink mode if t_RC set. Not
5123 // for Gnome terminal, it can't handle t_RC, it
5124 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005125 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005126 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005127 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005128 && *T_CRC != NUL)
5129 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005130 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005131 LOG_TR(("Sending cursor blink mode request"));
5132 out_str(T_CRC);
5133 termrequest_sent(&rbm_status);
5134 need_flush = TRUE;
5135 }
5136
5137 if (need_flush)
5138 out_flush();
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005139#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005140 }
5141}
5142
5143/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005144 * Add "key" to "buf" and return the number of bytes used.
5145 * Handles special keys and multi-byte characters.
5146 */
5147 static int
5148add_key_to_buf(int key, char_u *buf)
5149{
5150 int idx = 0;
5151
5152 if (IS_SPECIAL(key))
5153 {
5154 buf[idx++] = K_SPECIAL;
5155 buf[idx++] = KEY2TERMCAP0(key);
5156 buf[idx++] = KEY2TERMCAP1(key);
5157 }
5158 else if (has_mbyte)
5159 idx += (*mb_char2bytes)(key, buf + idx);
5160 else
5161 buf[idx++] = key;
5162 return idx;
5163}
5164
5165/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005166 * Shared between handle_key_with_modifier() and handle_csi_function_key().
5167 */
5168 static int
5169put_key_modifiers_in_typebuf(
5170 int key_arg,
5171 int modifiers_arg,
5172 int csi_len,
5173 int offset,
5174 char_u *buf,
5175 int bufsize,
5176 int *buflen)
5177{
5178 int key = key_arg;
5179 int modifiers = modifiers_arg;
5180
5181 // Some keys need adjustment when the Ctrl modifier is used.
5182 key = may_adjust_key_for_ctrl(modifiers, key);
5183
5184 // May remove the shift modifier if it's already included in the key.
5185 modifiers = may_remove_shift_modifier(modifiers, key);
5186
5187 // Produce modifiers with K_SPECIAL KS_MODIFIER {mod}
5188 char_u string[MAX_KEY_CODE_LEN + 1];
5189 int new_slen = modifiers2keycode(modifiers, &key, string);
5190
5191 // Add the bytes for the key.
5192 new_slen += add_key_to_buf(key, string + new_slen);
5193
5194 string[new_slen] = NUL;
5195 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5196 buf, bufsize, buflen) == FAIL)
5197 return -1;
5198 return new_slen - csi_len + offset;
5199}
5200
5201/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005202 * Handle a sequence with key and modifier, one of:
5203 * {lead}27;{modifier};{key}~
5204 * {lead}{key};{modifier}u
5205 * Returns the difference in length.
5206 */
5207 static int
5208handle_key_with_modifier(
5209 int *arg,
5210 int trail,
5211 int csi_len,
5212 int offset,
5213 char_u *buf,
5214 int bufsize,
5215 int *buflen)
5216{
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005217 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5218 // it for terminals using the kitty keyboard protocol. Xterm sends
5219 // the form ending in "u" when the formatOtherKeys resource is set. We do
5220 // not support this.
5221 //
5222 // Do not set seenModifyOtherKeys if there was a positive response at any
5223 // time from requesting the kitty keyboard protocol state, these are not
5224 // expected to support modifyOtherKeys level 2.
5225 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005226 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5227 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005228 if (trail != 'u'
5229 && (kitty_protocol_state == KKPS_INITIAL
5230 || kitty_protocol_state == KKPS_OFF
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00005231 || kitty_protocol_state == KKPS_AFTER_T_TE)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005232 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar1a173402022-12-02 12:28:47 +00005233 {
Bram Moolenaar5390c052022-12-02 13:10:03 +00005234#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005235 ch_log(NULL, "setting seenModifyOtherKeys to TRUE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005236#endif
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005237 seenModifyOtherKeys = TRUE;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005238 }
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005239
Bram Moolenaar1a173402022-12-02 12:28:47 +00005240 int key = trail == 'u' ? arg[0] : arg[2];
5241 int modifiers = decode_modifiers(arg[1]);
5242 return put_key_modifiers_in_typebuf(key, modifiers,
5243 csi_len, offset, buf, bufsize, buflen);
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005244}
5245
5246/*
5247 * Handle a sequence with key without a modifier:
5248 * {lead}{key}u
5249 * Returns the difference in length.
5250 */
5251 static int
5252handle_key_without_modifier(
5253 int *arg,
5254 int csi_len,
5255 int offset,
5256 char_u *buf,
5257 int bufsize,
5258 int *buflen)
5259{
5260 char_u string[MAX_KEY_CODE_LEN + 1];
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00005261 int new_slen;
5262
5263 if (arg[0] == ESC)
5264 {
5265 // Putting Esc in the buffer creates ambiguity, it can be the start of
5266 // an escape sequence. Use K_ESC to avoid that.
5267 string[0] = K_SPECIAL;
5268 string[1] = KS_EXTRA;
5269 string[2] = KE_ESC;
5270 new_slen = 3;
5271 }
5272 else
5273 new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005274
5275 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5276 buf, bufsize, buflen) == FAIL)
5277 return -1;
5278 return new_slen - csi_len + offset;
5279}
5280
5281/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005282 * CSI function key without or with modifiers:
5283 * {lead}[ABCDEFHPQRS]
5284 * {lead}1;{modifier}[ABCDEFHPQRS]
5285 * Returns zero when nog recognized, a positive number when recognized.
5286 */
5287 static int
5288handle_csi_function_key(
5289 int argc,
5290 int *arg,
5291 int trail,
5292 int csi_len,
5293 char_u *key_name,
5294 int offset,
5295 char_u *buf,
5296 int bufsize,
5297 int *buflen)
5298{
5299 key_name[0] = 'k';
5300 switch (trail)
5301 {
5302 case 'A': key_name[1] = 'u'; break; // K_UP
5303 case 'B': key_name[1] = 'd'; break; // K_DOWN
5304 case 'C': key_name[1] = 'r'; break; // K_RIGHT
5305 case 'D': key_name[1] = 'l'; break; // K_LEFT
5306
5307 // case 'E': keypad BEGIN - not supported
5308 case 'F': key_name[0] = '@'; key_name[1] = '7'; break; // K_END
5309 case 'H': key_name[1] = 'h'; break; // K_HOME
5310
5311 case 'P': key_name[1] = '1'; break; // K_F1
5312 case 'Q': key_name[1] = '2'; break; // K_F2
5313 case 'R': key_name[1] = '3'; break; // K_F3
5314 case 'S': key_name[1] = '4'; break; // K_F4
5315
5316 default: return 0; // not recognized
5317 }
5318
5319 int key = TERMCAP2KEY(key_name[0], key_name[1]);
5320 int modifiers = argc == 2 ? decode_modifiers(arg[1]) : 0;
5321 put_key_modifiers_in_typebuf(key, modifiers,
5322 csi_len, offset, buf, bufsize, buflen);
5323 return csi_len;
5324}
5325
5326/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005327 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005328 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005329 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005330 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5331 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005332 * - Cursor position report: {lead}{row};{col}R
5333 * The final byte must be 'R'. It is used for checking the
5334 * ambiguous-width character state.
5335 *
5336 * - window position reply: {lead}3;{x};{y}t
5337 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005338 * - key with modifiers when modifyOtherKeys is enabled or the Kitty keyboard
5339 * protocol is used:
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005340 * {lead}27;{modifier};{key}~
5341 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005342 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005343 * - function key with or without modifiers:
5344 * {lead}[ABCDEFHPQRS]
5345 * {lead}1;{modifier}[ABCDEFHPQRS]
5346 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005347 * Return 0 for no match, -1 for partial match, > 0 for full match.
5348 */
5349 static int
5350handle_csi(
5351 char_u *tp,
5352 int len,
5353 char_u *argp,
5354 int offset,
5355 char_u *buf,
5356 int bufsize,
5357 int *buflen,
5358 char_u *key_name,
5359 int *slen)
5360{
5361 int first = -1; // optional char right after {lead}
5362 int trail; // char that ends CSI sequence
5363 int arg[3] = {-1, -1, -1}; // argument numbers
Bram Moolenaar1a173402022-12-02 12:28:47 +00005364 int argc = 0; // number of arguments
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005365 char_u *ap = argp;
5366 int csi_len;
5367
5368 // Check for non-digit after CSI.
5369 if (!VIM_ISDIGIT(*ap))
5370 first = *ap++;
5371
Bram Moolenaar8ffb7e02022-12-03 10:13:30 +00005372 if (first >= 'A' && first <= 'Z')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005373 {
Bram Moolenaar1a173402022-12-02 12:28:47 +00005374 // If "first" is in [ABCDEFHPQRS] then it is actually the "trail" and
5375 // no argument follows.
5376 trail = first;
5377 first = -1;
5378 --ap;
5379 }
5380 else
5381 {
5382 // Find up to three argument numbers.
5383 for (argc = 0; argc < 3; )
5384 {
5385 if (ap >= tp + len)
5386 return -1;
5387 if (*ap == ';')
5388 arg[argc++] = -1; // omitted number
5389 else if (VIM_ISDIGIT(*ap))
5390 {
5391 arg[argc] = 0;
5392 for (;;)
5393 {
5394 if (ap >= tp + len)
5395 return -1;
5396 if (!VIM_ISDIGIT(*ap))
5397 break;
5398 arg[argc] = arg[argc] * 10 + (*ap - '0');
5399 ++ap;
5400 }
5401 ++argc;
5402 }
5403 if (*ap == ';')
5404 ++ap;
5405 else
5406 break;
5407 }
5408
5409 // mrxvt has been reported to have "+" in the version. Assume
5410 // the escape sequence ends with a letter or one of "{|}~".
5411 while (ap < tp + len
5412 && !(*ap >= '{' && *ap <= '~')
5413 && !ASCII_ISALPHA(*ap))
5414 ++ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005415 if (ap >= tp + len)
5416 return -1;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005417 trail = *ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005418 }
5419
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005420 csi_len = (int)(ap - tp) + 1;
5421
Bram Moolenaarc255b782022-11-26 19:16:48 +00005422 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5423 // modifyOtherKeys level. Drop similar responses.
5424 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5425 {
5426 if (arg[0] == 4 && argc == 2)
5427 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5428
5429 key_name[0] = (int)KS_EXTRA;
5430 key_name[1] = (int)KE_IGNORE;
5431 *slen = csi_len;
5432 }
5433
Bram Moolenaar1a173402022-12-02 12:28:47 +00005434 // Function key starting with CSI:
5435 // {lead}[ABCDEFHPQRS]
5436 // {lead}1;{modifier}[ABCDEFHPQRS]
5437 else if (first == -1 && ASCII_ISUPPER(trail)
5438 && (argc == 0 || (argc == 2 && arg[0] == 1)))
5439 {
5440 int res = handle_csi_function_key(argc, arg, trail,
5441 csi_len, key_name, offset, buf, bufsize, buflen);
5442 return res <= 0 ? res : len + res;
5443 }
5444
5445 // Cursor position report: {lead}{row};{col}R
5446 // Eat it when there are 2 arguments and it ends in 'R'.
5447 // Also when u7_status is not "sent", it may be from a previous Vim that
5448 // just exited. But not for <S-F3>, it sends something similar, check for
5449 // row and column to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005450 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005451 {
5452 handle_u7_response(arg, tp, csi_len);
5453
5454 key_name[0] = (int)KS_EXTRA;
5455 key_name[1] = (int)KE_IGNORE;
5456 *slen = csi_len;
5457 }
5458
5459 // Version string: Eat it when there is at least one digit and
5460 // it ends in 'c'
5461 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5462 {
5463 handle_version_response(first, arg, argc, tp);
5464
5465 *slen = csi_len;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005466#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005467 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005468#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005469 apply_autocmds(EVENT_TERMRESPONSE,
5470 NULL, NULL, FALSE, curbuf);
5471 key_name[0] = (int)KS_EXTRA;
5472 key_name[1] = (int)KE_IGNORE;
5473 }
5474
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005475#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005476 // Check blinking cursor from xterm:
5477 // {lead}?12;1$y set
5478 // {lead}?12;2$y not set
5479 //
5480 // {lead} can be <Esc>[ or CSI
5481 else if (rbm_status.tr_progress == STATUS_SENT
5482 && first == '?'
5483 && ap == argp + 6
5484 && arg[0] == 12
5485 && ap[-1] == '$'
5486 && trail == 'y')
5487 {
5488 initial_cursor_blink = (arg[1] == '1');
5489 rbm_status.tr_progress = STATUS_GOT;
5490 LOG_TR(("Received cursor blinking mode response: %s", tp));
5491 key_name[0] = (int)KS_EXTRA;
5492 key_name[1] = (int)KE_IGNORE;
5493 *slen = csi_len;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005494# ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005495 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005496# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005497 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005498#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005499
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005500 // Kitty keyboard protocol status response: CSI ? flags u
5501 else if (first == '?' && argc == 1 && trail == 'u')
5502 {
5503 // The protocol has various "progressive enhancement flags" values, but
5504 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005505 if (arg[0] == '0')
5506 {
5507 kitty_protocol_state = KKPS_OFF;
5508 }
5509 else
5510 {
5511 kitty_protocol_state = KKPS_ENABLED;
5512
5513 // Reset seenModifyOtherKeys just in case some key combination has
5514 // been seen that set it before we get the status response.
Bram Moolenaar5390c052022-12-02 13:10:03 +00005515#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005516 ch_log(NULL, "setting seenModifyOtherKeys to FALSE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005517#endif
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005518 seenModifyOtherKeys = FALSE;
5519 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005520
5521 key_name[0] = (int)KS_EXTRA;
5522 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005523 *slen = csi_len;
5524 }
5525
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005526#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005527 // Check for a window position response from the terminal:
5528 // {lead}3;{x};{y}t
5529 else if (did_request_winpos && argc == 3 && arg[0] == 3
5530 && trail == 't')
5531 {
5532 winpos_x = arg[1];
5533 winpos_y = arg[2];
5534 // got finished code: consume it
5535 key_name[0] = (int)KS_EXTRA;
5536 key_name[1] = (int)KE_IGNORE;
5537 *slen = csi_len;
5538
5539 if (--did_request_winpos <= 0)
5540 winpos_status.tr_progress = STATUS_GOT;
5541 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005542#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005543
5544 // Key with modifier:
5545 // {lead}27;{modifier};{key}~
5546 // {lead}{key};{modifier}u
Bram Moolenaar064fd672022-11-29 18:32:32 +00005547 // Even though we only handle four modifiers and the {modifier} value
5548 // should be 16 or lower, we accept all modifier values to avoid the raw
5549 // sequence to be passed through.
5550 else if ((arg[0] == 27 && argc == 3 && trail == '~')
Bram Moolenaar7609c882022-10-19 20:07:09 +01005551 || (argc == 2 && trail == 'u'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005552 {
5553 return len + handle_key_with_modifier(arg, trail,
Bram Moolenaar064fd672022-11-29 18:32:32 +00005554 csi_len, offset, buf, bufsize, buflen);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005555 }
5556
Christopher Plewright03193062022-11-22 12:58:27 +00005557 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005558 // {lead}{key}u
5559 else if (argc == 1 && trail == 'u')
5560 {
5561 return len + handle_key_without_modifier(arg,
5562 csi_len, offset, buf, bufsize, buflen);
5563 }
5564
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005565 // else: Unknown CSI sequence. We could drop it, but then the
5566 // user can't create a map for it.
5567 return 0;
5568}
5569
5570/*
5571 * Handle an OSC sequence, fore/background color response from the terminal:
5572 *
5573 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5574 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5575 *
5576 * {code} is 10 for foreground, 11 for background
5577 * {lead} can be <Esc>] or OSC
5578 * {tail} can be '\007', <Esc>\ or STERM.
5579 *
5580 * Consume any code that starts with "{lead}11;", it's also
5581 * possible that "rgba" is following.
5582 */
5583 static int
5584handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5585{
5586 int i, j;
5587
5588 j = 1 + (tp[0] == ESC);
5589 if (len >= j + 3 && (argp[0] != '1'
5590 || (argp[1] != '1' && argp[1] != '0')
5591 || argp[2] != ';'))
5592 i = 0; // no match
5593 else
5594 for (i = j; i < len; ++i)
5595 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5596 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5597 {
5598 int is_bg = argp[1] == '1';
5599 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5600 && tp[j + 16] == '/';
5601
5602 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5603 && (is_4digit
5604 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5605 {
5606 char_u *tp_r = tp + j + 7;
5607 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5608 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005609#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005610 int rval, gval, bval;
5611
5612 rval = hexhex2nr(tp_r);
5613 gval = hexhex2nr(tp_b);
5614 bval = hexhex2nr(tp_g);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005615#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005616 if (is_bg)
5617 {
5618 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5619 *tp_b) ? "light" : "dark";
5620
5621 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005622#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005623 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005624# ifdef FEAT_TERMINAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005625 bg_r = rval;
5626 bg_g = gval;
5627 bg_b = bval;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005628# endif
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005629#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005630 if (!option_was_set((char_u *)"bg")
5631 && STRCMP(p_bg, new_bg_val) != 0)
5632 {
5633 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005634 set_option_value_give_err((char_u *)"bg",
5635 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005636 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005637 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005638 }
5639 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005640#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005641 else
5642 {
5643 LOG_TR(("Received RFG response: %s", tp));
5644 rfg_status.tr_progress = STATUS_GOT;
5645 fg_r = rval;
5646 fg_g = gval;
5647 fg_b = bval;
5648 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005649#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005650 }
5651
5652 // got finished code: consume it
5653 key_name[0] = (int)KS_EXTRA;
5654 key_name[1] = (int)KE_IGNORE;
5655 *slen = i + 1 + (tp[i] == ESC);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005656#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005657 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5658 : VV_TERMRFGRESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005659#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005660 break;
5661 }
5662 if (i == len)
5663 {
5664 LOG_TR(("not enough characters for RB"));
5665 return FAIL;
5666 }
5667 return OK;
5668}
5669
5670/*
5671 * Check for key code response from xterm:
5672 * {lead}{flag}+r<hex bytes><{tail}
5673 *
5674 * {lead} can be <Esc>P or DCS
5675 * {flag} can be '0' or '1'
5676 * {tail} can be Esc>\ or STERM
5677 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005678 * Check for resource response from xterm (and drop it):
5679 * {lead}{flag}+R<hex bytes>=<value>{tail}
5680 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005681 * Check for cursor shape response from xterm:
5682 * {lead}1$r<digit> q{tail}
5683 *
5684 * {lead} can be <Esc>P or DCS
5685 * {tail} can be <Esc>\ or STERM
5686 *
5687 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5688 */
5689 static int
5690handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5691{
5692 int i, j;
5693
5694 j = 1 + (tp[0] == ESC);
5695 if (len < j + 3)
5696 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005697 else if ((argp[1] != '+' && argp[1] != '$')
5698 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005699 i = 0; // no match
5700 else if (argp[1] == '+')
5701 // key code response
5702 for (i = j; i < len; ++i)
5703 {
5704 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5705 || tp[i] == STERM)
5706 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005707#ifdef FEAT_TERMRESPONSE
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005708 // handle a key code response, drop a resource response
5709 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005710 got_code_from_term(tp + j, i);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005711#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005712 key_name[0] = (int)KS_EXTRA;
5713 key_name[1] = (int)KE_IGNORE;
5714 *slen = i + 1 + (tp[i] == ESC);
5715 break;
5716 }
5717 }
5718 else
5719 {
5720 // Probably the cursor shape response. Make sure that "i"
5721 // is equal to "len" when there are not sufficient
5722 // characters.
5723 for (i = j + 3; i < len; ++i)
5724 {
5725 if (i - j == 3 && !isdigit(tp[i]))
5726 break;
5727 if (i - j == 4 && tp[i] != ' ')
5728 break;
5729 if (i - j == 5 && tp[i] != 'q')
5730 break;
5731 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5732 break;
5733 if ((i - j == 6 && tp[i] == STERM)
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005734 || (i - j == 7 && tp[i] == '\\'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005735 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005736#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005737 int number = argp[3] - '0';
5738
5739 // 0, 1 = block blink, 2 = block
5740 // 3 = underline blink, 4 = underline
5741 // 5 = vertical bar blink, 6 = vertical bar
5742 number = number == 0 ? 1 : number;
5743 initial_cursor_shape = (number + 1) / 2;
5744 // The blink flag is actually inverted, compared to
5745 // the value set with T_SH.
5746 initial_cursor_shape_blink =
5747 (number & 1) ? FALSE : TRUE;
5748 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005749#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005750 LOG_TR(("Received cursor shape response: %s", tp));
5751
5752 key_name[0] = (int)KS_EXTRA;
5753 key_name[1] = (int)KE_IGNORE;
5754 *slen = i + 1;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005755#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005756 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005757#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005758 break;
5759 }
5760 }
5761 }
5762
5763 if (i == len)
5764 {
5765 // These codes arrive many together, each code can be
5766 // truncated at any point.
5767 LOG_TR(("not enough characters for XT"));
5768 return FAIL;
5769 }
5770 return OK;
5771}
5772
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 * Check if typebuf.tb_buf[] contains a terminal key code.
5775 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005776 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005778 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 * With a match, the match is removed, the replacement code is inserted in
5780 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5781 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005782 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5783 * "buflen" is then the length of the string in buf[] and is updated for
5784 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 */
5786 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005787check_termcode(
5788 int max_offset,
5789 char_u *buf,
5790 int bufsize,
5791 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
5793 char_u *tp;
5794 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005795 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005796 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005798 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 int offset;
5800 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005801 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005802 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005803 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005804 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 char_u string[MAX_KEY_CODE_LEN + 1];
5806 int i, j;
5807 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809
5810 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5811
5812 /*
5813 * Speed up the checks for terminal codes by gathering all first bytes
5814 * used in termleader[]. Often this is just a single <Esc>.
5815 */
5816 if (need_gather)
5817 gather_termleader();
5818
5819 /*
5820 * Check at several positions in typebuf.tb_buf[], to catch something like
5821 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5822 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005823 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 * This is used often, KEEP IT FAST!
5825 */
5826 for (offset = 0; offset < max_offset; ++offset)
5827 {
5828 if (buf == NULL)
5829 {
5830 if (offset >= typebuf.tb_len)
5831 break;
5832 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005833 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 }
5835 else
5836 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005837 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 break;
5839 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005840 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
5842
5843 /*
5844 * Don't check characters after K_SPECIAL, those are already
5845 * translated terminal chars (avoid translating ~@^Hx).
5846 */
5847 if (*tp == K_SPECIAL)
5848 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005849 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 continue;
5851 }
5852
5853 /*
5854 * Skip this position if the character does not appear as the first
5855 * character in term_strings. This speeds up a lot, since most
5856 * termcodes start with the same character (ESC or CSI).
5857 */
5858 i = *tp;
5859 for (p = termleader; *p && *p != i; ++p)
5860 ;
5861 if (*p == NUL)
5862 continue;
5863
5864 /*
5865 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5866 * are in Insert mode.
5867 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005868 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 continue;
5870
Bram Moolenaar27efc622022-07-01 16:35:45 +01005871 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005872 key_name[0] = NUL; // no key name found yet
5873 key_name[1] = NUL; // no key name found yet
5874 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
5876#ifdef FEAT_GUI
5877 if (gui.in_use)
5878 {
5879 /*
5880 * GUI special key codes are all of the form [CSI xx].
5881 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005882 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {
5884 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005885 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 slen = 3;
5887 key_name[0] = tp[1];
5888 key_name[1] = tp[2];
5889 }
5890 }
5891 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005892#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00005893#ifdef MSWIN
5894 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
5895 && (tp[2] == KE_MOUSEUP
5896 || tp[2] == KE_MOUSEDOWN
5897 || tp[2] == KE_MOUSELEFT
5898 || tp[2] == KE_MOUSERIGHT))
5899 {
5900 // MS-Windows console sends mouse scroll events encoded:
5901 // - CSI
5902 // - KS_EXTRA
5903 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
5904 slen = 3;
5905 key_name[0] = tp[1];
5906 key_name[1] = tp[2];
5907 }
5908 else
5909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005911 int mouse_index_found = -1;
5912
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 for (idx = 0; idx < tc_len; ++idx)
5914 {
5915 /*
5916 * Ignore the entry if we are not at the start of
5917 * typebuf.tb_buf[]
5918 * and there are not enough characters to make a match.
5919 * But only when the 'K' flag is in 'cpoptions'.
5920 */
5921 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005922 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 if (cpo_koffset && offset && len < slen)
5924 continue;
5925 if (STRNCMP(termcodes[idx].code, tp,
5926 (size_t)(slen > len ? len : slen)) == 0)
5927 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005928 int looks_like_mouse_start = FALSE;
5929
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005930 if (len < slen) // got a partial sequence
5931 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
5933 /*
5934 * When found a keypad key, check if there is another key
5935 * that matches and use that one. This makes <Home> to be
5936 * found instead of <kHome> when they produce the same
5937 * key code.
5938 */
5939 if (termcodes[idx].name[0] == 'K'
5940 && VIM_ISDIGIT(termcodes[idx].name[1]))
5941 {
5942 for (j = idx + 1; j < tc_len; ++j)
5943 if (termcodes[j].len == slen &&
5944 STRNCMP(termcodes[idx].code,
5945 termcodes[j].code, slen) == 0)
5946 {
5947 idx = j;
5948 break;
5949 }
5950 }
5951
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005952 if (slen == 2 && len > 2
5953 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005954 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005955 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005956 // The mouse termcode "ESC [" is also the prefix of
5957 // "ESC [ I" (focus gained) and other keys. Check some
5958 // more bytes to find out.
5959 if (!isdigit(tp[2]))
5960 {
5961 // ESC [ without number following: Only use it when
5962 // there is no other match.
5963 looks_like_mouse_start = TRUE;
5964 }
5965 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5966 {
5967 char_u *nr = tp + 2;
5968 int count = 0;
5969
5970 // If a digit is following it could be a key with
5971 // modifier, e.g., ESC [ 1;2P. Can be confused
5972 // with DEC_MOUSE, which requires four numbers
5973 // following. If not then it can't be a DEC_MOUSE
5974 // code.
5975 for (;;)
5976 {
5977 ++count;
5978 (void)getdigits(&nr);
5979 if (nr >= tp + len)
5980 return -1; // partial sequence
5981 if (*nr != ';')
5982 break;
5983 ++nr;
5984 if (nr >= tp + len)
5985 return -1; // partial sequence
5986 }
5987 if (count < 4)
5988 continue; // no match
5989 }
5990 }
5991 if (looks_like_mouse_start)
5992 {
5993 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005994 if (mouse_index_found < 0)
5995 mouse_index_found = idx;
5996 }
5997 else
5998 {
5999 key_name[0] = termcodes[idx].name[0];
6000 key_name[1] = termcodes[idx].name[1];
6001 break;
6002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006004
6005 /*
6006 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006007 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006008 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006009 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
6010 * When there is a modifier the * matches a number.
6011 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006012 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006013 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006014 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006015 modslen = termcodes[idx].modlen;
6016 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006017 continue;
6018 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006019 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006020 {
6021 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006022
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006023 if (len <= modslen) // got a partial sequence
6024 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006025
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006026 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006027 // no modifiers
6028 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006029 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006030 // no match for "code;*X" with "code;"
6031 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01006032 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00006033 && (tp[modslen] != '1'
6034 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00006035 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006036 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006037 else
6038 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006039 // Skip over the digits, the final char must
6040 // follow. URXVT can use a negative value, thus
6041 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006042 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006043 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006044 ;
6045 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006046 if (len < j) // got a partial sequence
6047 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006048 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006049 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006050
Bram Moolenaara529ce02017-06-22 22:37:57 +02006051 modifiers_start = tp + slen - 2;
6052
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006053 // Match! Convert modifier bits.
6054 n = atoi((char *)modifiers_start);
6055 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006056
6057 slen = j;
6058 }
6059 key_name[0] = termcodes[idx].name[0];
6060 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006061 break;
6062 }
6063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006065 if (idx == tc_len && mouse_index_found >= 0)
6066 {
6067 key_name[0] = termcodes[mouse_index_found].name[0];
6068 key_name[1] = termcodes[mouse_index_found].name[1];
6069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 }
6071
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02006072 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006073 // Mouse codes of DEC and pterm start with <ESC>[. When
6074 // detecting the start of these mouse codes they might as well be
6075 // another key code or terminal response.
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006076#ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006077 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006078#endif
6079#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006080 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006081#endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006082 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006084 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
6085
6086 /*
6087 * Check for responses from the terminal starting with {lead}:
Bram Moolenaar1a173402022-12-02 12:28:47 +00006088 * "<Esc>[" or CSI followed by [0-9>?].
6089 * Also for function keys without a modifier:
6090 * "<Esc>[" or CSI followed by [ABCDEFHPQRS].
Bram Moolenaar9584b312013-03-13 19:29:28 +01006091 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006092 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01006093 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006094 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01006095 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00006096 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
6097 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006098 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006099 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01006100 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006101 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006102 * - window position reply: {lead}3;{x};{y}t
6103 *
6104 * - key with modifiers when modifyOtherKeys is enabled:
6105 * {lead}27;{modifier};{key}~
6106 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01006107 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006108 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar1a173402022-12-02 12:28:47 +00006109 || (tp[0] == CSI && len >= 2))
6110 && vim_strchr((char_u *)"0123456789>?ABCDEFHPQRS",
6111 *argp) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006113 int resp = handle_csi(tp, len, argp, offset, buf,
6114 bufsize, buflen, key_name, &slen);
6115 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006116 {
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006117#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006118 if (resp == -1)
6119 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006120#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006121 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01006122 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006123 }
6124
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006125 // Check for fore/background color response from the terminal,
6126 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006127 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006128 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
6129 || tp[0] == OSC))
6130 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006131 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006132 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 }
6134
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006135 // Check for key code response from xterm,
6136 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00006137 // It would only be needed with this condition:
6138 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
6139 // Now this is always done so that DCS codes don't mess up things.
6140 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006142 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006143 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 }
6145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
6147 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006148 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006150 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151
Christopher Plewright36446bb2022-11-23 22:28:08 +00006152#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00006154 * For scroll events from the GUI or MS-Windows console, fetch the
6155 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00006157 if (TRUE
6158# if defined(FEAT_GUI) && !defined(MSWIN)
6159 && gui.in_use
6160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 && key_name[0] == (int)KS_EXTRA
6162 && (key_name[1] == (int)KE_X1MOUSE
6163 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006164 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006165 || key_name[1] == (int)KE_MOUSELEFT
6166 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 || key_name[1] == (int)KE_MOUSEDOWN
6168 || key_name[1] == (int)KE_MOUSEUP))
6169 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006170 char_u bytes[6];
6171 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
6172
6173 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 return -1;
6175 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
6176 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
6177 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006178 // equal to K_MOUSEMOVE
6179 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
6180 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 }
6182 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 /*
6185 * If it is a mouse click, get the coordinates.
6186 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006187 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006188#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02006189 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006190#endif
6191#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006192 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006193#endif
6194#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006195 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006196#endif
6197#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006198 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006199#endif
6200#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006201 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006202#endif
6203#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006204 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006205#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006206 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006207 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006209 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
6210 &modifiers) == -1)
6211 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213
6214#ifdef FEAT_GUI
6215 /*
6216 * If using the GUI, then we get menu and scrollbar events.
6217 *
6218 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
6219 * four bytes which are to be taken as a pointer to the vimmenu_T
6220 * structure.
6221 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00006222 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006223 * is one byte with the tab index.
6224 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
6226 * by one byte representing the scrollbar number, and then four bytes
6227 * representing a long_u which is the new value of the scrollbar.
6228 *
6229 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
6230 * KE_FILLER followed by four bytes representing a long_u which is the
6231 * new value of the scrollbar.
6232 */
6233# ifdef FEAT_MENU
6234 else if (key_name[0] == (int)KS_MENU)
6235 {
6236 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006237 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 if (num_bytes == -1)
6240 return -1;
6241 current_menu = (vimmenu_T *)val;
6242 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006243
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006244 // The menu may have been deleted right after it was used, check
6245 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006246 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6247 {
6248 key_name[0] = KS_EXTRA;
6249 key_name[1] = (int)KE_IGNORE;
6250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 }
6252# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006253# ifdef FEAT_GUI_TABLINE
6254 else if (key_name[0] == (int)KS_TABLINE)
6255 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006256 // Selecting tabline tab or using its menu.
6257 char_u bytes[6];
6258 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6259
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006260 if (num_bytes == -1)
6261 return -1;
6262 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006263 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006264 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006265 slen += num_bytes;
6266 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006267 else if (key_name[0] == (int)KS_TABMENU)
6268 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006269 // Selecting tabline tab or using its menu.
6270 char_u bytes[6];
6271 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6272
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006273 if (num_bytes == -1)
6274 return -1;
6275 current_tab = (int)bytes[0];
6276 current_tabmenu = (int)bytes[1];
6277 slen += num_bytes;
6278 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280# ifndef USE_ON_FLY_SCROLL
6281 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6282 {
6283 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006284 char_u bytes[6];
6285 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006287 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 j = 0;
6289 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6290 && tp[j + 2] != NUL; ++i)
6291 {
6292 j += 3;
6293 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6294 if (num_bytes == -1)
6295 break;
6296 if (i == 0)
6297 current_scrollbar = (int)bytes[0];
6298 else if (current_scrollbar != (int)bytes[0])
6299 break;
6300 j += num_bytes;
6301 num_bytes = get_long_from_buf(tp + j, &val);
6302 if (num_bytes == -1)
6303 break;
6304 scrollbar_value = val;
6305 j += num_bytes;
6306 slen = j;
6307 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006308 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 return -1;
6310 }
6311 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6312 {
6313 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006314 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006316 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 j = 0;
6318 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6319 && tp[j + 2] != NUL; ++i)
6320 {
6321 j += 3;
6322 num_bytes = get_long_from_buf(tp + j, &val);
6323 if (num_bytes == -1)
6324 break;
6325 scrollbar_value = val;
6326 j += num_bytes;
6327 slen = j;
6328 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006329 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 return -1;
6331 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006332# endif // !USE_ON_FLY_SCROLL
6333#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006335#if (defined(UNIX) || defined(VMS))
6336 /*
6337 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6338 * (CSI I/CSI O)
6339 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006340 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006341# ifdef FEAT_GUI
6342 && !gui.in_use
6343# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006344 )
6345 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006346 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006347 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006348 if (!focus_state)
6349 {
6350 ui_focus_change(TRUE);
6351 did_cursorhold = TRUE;
6352 focus_state = TRUE;
6353 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006354 key_name[1] = (int)KE_IGNORE;
6355 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006356 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006357 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006358 if (focus_state)
6359 {
6360 ui_focus_change(FALSE);
6361 did_cursorhold = TRUE;
6362 focus_state = FALSE;
6363 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006364 key_name[1] = (int)KE_IGNORE;
6365 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006366 }
6367#endif
6368
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006369 /*
6370 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6371 */
6372 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6373
6374 /*
6375 * Add any modifier codes to our string.
6376 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006377 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006378
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006379 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006380 key_name[0] = KEY2TERMCAP0(key);
6381 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006383 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006384 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006385 if (has_mbyte)
6386 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6387 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006388 string[new_slen++] = key_name[1];
6389 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006390 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6391 && key_name[1] == KE_IGNORE)
6392 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006393 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6394 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006395 retval = KEYLEN_REMOVED;
6396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 else
6398 {
6399 string[new_slen++] = K_SPECIAL;
6400 string[new_slen++] = key_name[0];
6401 string[new_slen++] = key_name[1];
6402 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006403 if (put_string_in_typebuf(offset, slen, string, new_slen,
6404 buf, bufsize, buflen) == FAIL)
6405 return -1;
6406 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 }
6408
Bram Moolenaar2951b772013-07-03 12:45:31 +02006409#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006410 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006411#endif
6412
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006413 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414}
6415
Bram Moolenaar9377df32017-10-15 13:22:01 +02006416#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006417/*
6418 * Get the text foreground color, if known.
6419 */
6420 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006421term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006422{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006423 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006424 {
6425 *r = fg_r;
6426 *g = fg_g;
6427 *b = fg_b;
6428 }
6429}
6430
6431/*
6432 * Get the text background color, if known.
6433 */
6434 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006435term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006436{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006437 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006438 {
6439 *r = bg_r;
6440 *g = bg_g;
6441 *b = bg_b;
6442 }
6443}
6444#endif
6445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446/*
6447 * Replace any terminal code strings in from[] with the equivalent internal
6448 * vim representation. This is used for the "from" and "to" part of a
6449 * mapping, and the "to" part of a menu command.
6450 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6451 * '<'.
6452 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6453 *
6454 * The replacement is done in result[] and finally copied into allocated
6455 * memory. If this all works well *bufp is set to the allocated memory and a
6456 * pointer to it is returned. If something fails *bufp is set to NULL and from
6457 * is returned.
6458 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006459 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6460 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6461 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6462 * used instead of a CTRL-V.
6463 *
6464 * Flags:
6465 * REPTERM_FROM_PART see above
6466 * REPTERM_DO_LT also translate <lt>
6467 * REPTERM_SPECIAL always accept <key> notation
6468 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6469 *
6470 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6471 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006473 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006474replace_termcodes(
6475 char_u *from,
6476 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02006477 int flags,
6478 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
6480 int i;
6481 int slen;
6482 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006483 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006485 int do_backslash; // backslash is a special character
6486 int do_special; // recognize <> key codes
6487 int do_key_code; // recognize raw key codes
6488 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006489 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490
6491 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006492 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6493 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006495 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496
6497 /*
6498 * Allocate space for the translation. Worst case a single character is
6499 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006500 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006502 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006503 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 {
6505 *bufp = NULL;
6506 return from;
6507 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006508 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
6510 /*
6511 * Check for #n at start only: function key n
6512 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006513 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 {
6515 result[dlen++] = K_SPECIAL;
6516 result[dlen++] = 'k';
6517 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006518 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006520 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 src += 2;
6522 }
6523
6524 /*
6525 * Copy each byte from *from to result[dlen]
6526 */
6527 while (*src != NUL)
6528 {
6529 /*
6530 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006531 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006533 if (do_special && ((flags & REPTERM_DO_LT)
6534 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
6536#ifdef FEAT_EVAL
6537 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006538 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6539 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006541 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6542 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 */
6544 if (STRNICMP(src, "<SID>", 5) == 0)
6545 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006546 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006547 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 else
6549 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006550 char_u *dot;
6551 long sid = current_sctx.sc_sid;
6552
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006554 if (in_vim9script()
6555 && (dot = vim_strchr(src, '.')) != NULL)
6556 {
6557 imported_T *imp = find_imported(src, dot - src, FALSE);
6558
6559 if (imp != NULL)
6560 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006561 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6562 size_t len;
6563
Bram Moolenaar89445512022-04-14 12:58:23 +01006564 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006565 if (si->sn_autoload_prefix != NULL)
6566 {
6567 // Turn "<SID>name.Func"
6568 // into "scriptname#Func".
6569 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006570 if (ga_grow(&ga,
6571 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006572 {
6573 ga_clear(&ga);
6574 *bufp = NULL;
6575 return from;
6576 }
6577 result = ga.ga_data;
6578 STRCPY(result + dlen, si->sn_autoload_prefix);
6579 dlen += len;
6580 continue;
6581 }
6582 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006583 }
6584 }
6585
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 result[dlen++] = K_SPECIAL;
6587 result[dlen++] = (int)KS_EXTRA;
6588 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006589 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006590 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 result[dlen++] = '_';
6592 continue;
6593 }
6594 }
6595#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006596 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6597 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006598 TRUE, did_simplify);
Bram Moolenaar1a173402022-12-02 12:28:47 +00006599 if (slen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 {
6601 dlen += slen;
6602 continue;
6603 }
6604 }
6605
6606 /*
6607 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6608 * Note that this is also checked after replacing the <> form.
6609 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6610 * it could be a character in the file.
6611 */
6612 if (do_key_code)
6613 {
6614 i = find_term_bykeys(src);
6615 if (i >= 0)
6616 {
6617 result[dlen++] = K_SPECIAL;
6618 result[dlen++] = termcodes[i].name[0];
6619 result[dlen++] = termcodes[i].name[1];
6620 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006621 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 continue;
6623 }
6624 }
6625
6626#ifdef FEAT_EVAL
6627 if (do_special)
6628 {
6629 char_u *p, *s, len;
6630
6631 /*
6632 * Replace <Leader> by the value of "mapleader".
6633 * Replace <LocalLeader> by the value of "maplocalleader".
6634 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6635 */
6636 if (STRNICMP(src, "<Leader>", 8) == 0)
6637 {
6638 len = 8;
6639 p = get_var_value((char_u *)"g:mapleader");
6640 }
6641 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6642 {
6643 len = 13;
6644 p = get_var_value((char_u *)"g:maplocalleader");
6645 }
6646 else
6647 {
6648 len = 0;
6649 p = NULL;
6650 }
6651 if (len != 0)
6652 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006653 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6655 s = (char_u *)"\\";
6656 else
6657 s = p;
6658 while (*s != NUL)
6659 result[dlen++] = *s++;
6660 src += len;
6661 continue;
6662 }
6663 }
6664#endif
6665
6666 /*
6667 * Remove CTRL-V and ignore the next character.
6668 * For "from" side the CTRL-V at the end is included, for the "to"
6669 * part it is removed.
6670 * If 'cpoptions' does not contain 'B', also accept a backslash.
6671 */
6672 key = *src;
6673 if (key == Ctrl_V || (do_backslash && key == '\\'))
6674 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006675 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 if (*src == NUL)
6677 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006678 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 result[dlen++] = key;
6680 break;
6681 }
6682 }
6683
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006684 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006685 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
6687 /*
6688 * If the character is K_SPECIAL, replace it with K_SPECIAL
6689 * KS_SPECIAL KE_FILLER.
6690 * If compiled with the GUI replace CSI with K_CSI.
6691 */
6692 if (*src == K_SPECIAL)
6693 {
6694 result[dlen++] = K_SPECIAL;
6695 result[dlen++] = KS_SPECIAL;
6696 result[dlen++] = KE_FILLER;
6697 }
6698# ifdef FEAT_GUI
6699 else if (*src == CSI)
6700 {
6701 result[dlen++] = K_SPECIAL;
6702 result[dlen++] = KS_EXTRA;
6703 result[dlen++] = (int)KE_CSI;
6704 }
6705# endif
6706 else
6707 result[dlen++] = *src;
6708 ++src;
6709 }
6710 }
6711 result[dlen] = NUL;
6712
6713 /*
6714 * Copy the new string to allocated memory.
6715 * If this fails, just return from.
6716 */
6717 if ((*bufp = vim_strsave(result)) != NULL)
6718 from = *bufp;
6719 vim_free(result);
6720 return from;
6721}
6722
6723/*
6724 * Find a termcode with keys 'src' (must be NUL terminated).
6725 * Return the index in termcodes[], or -1 if not found.
6726 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006727 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006728find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729{
6730 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006731 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732
6733 for (i = 0; i < tc_len; ++i)
6734 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006735 if (slen == termcodes[i].len
6736 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 return i;
6738 }
6739 return -1;
6740}
6741
6742/*
6743 * Gather the first characters in the terminal key codes into a string.
6744 * Used to speed up check_termcode().
6745 */
6746 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006747gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748{
6749 int i;
6750 int len = 0;
6751
6752#ifdef FEAT_GUI
6753 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006754 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755#endif
6756#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006757 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006758 termleader[len++] = DCS; // the termcode response starts with DCS
6759 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760#endif
6761 termleader[len] = NUL;
6762
6763 for (i = 0; i < tc_len; ++i)
6764 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6765 {
6766 termleader[len++] = termcodes[i].code[0];
6767 termleader[len] = NUL;
6768 }
6769
6770 need_gather = FALSE;
6771}
6772
6773/*
6774 * Show all termcodes (for ":set termcap")
6775 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006776 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 */
6778 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006779show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780{
6781 int col;
6782 int *items;
6783 int item_count;
6784 int run;
6785 int row, rows;
6786 int cols;
6787 int i;
6788 int len;
6789
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006790#define INC3 27 // try to make three columns
6791#define INC2 40 // try to make two columns
6792#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006794 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006796 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if (items == NULL)
6798 return;
6799
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006800 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006801 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802
6803 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006804 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006806 * 2. display the medium items (medium length strings)
6807 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006808 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006810 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {
6812 /*
6813 * collect the items in items[]
6814 */
6815 item_count = 0;
6816 for (i = 0; i < tc_len; i++)
6817 {
6818 len = show_one_termcode(termcodes[i].name,
6819 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006820 if ((flags & OPT_ONECOLUMN) ||
6821 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006822 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006823 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 items[item_count++] = i;
6825 }
6826
6827 /*
6828 * display the items
6829 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006830 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006832 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 if (cols == 0)
6834 cols = 1;
6835 rows = (item_count + cols - 1) / cols;
6836 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006837 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 rows = item_count;
6839 for (row = 0; row < rows && !got_int; ++row)
6840 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006841 msg_putchar('\n'); // go to next line
6842 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 break;
6844 col = 0;
6845 for (i = row; i < item_count; i += rows)
6846 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006847 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 show_one_termcode(termcodes[items[i]].name,
6849 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006850 if (run == 2)
6851 col += INC2;
6852 else
6853 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 }
6855 out_flush();
6856 ui_breakcheck();
6857 }
6858 }
6859 vim_free(items);
6860}
6861
6862/*
6863 * Show one termcode entry.
6864 * Output goes into IObuff[]
6865 */
6866 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006867show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868{
6869 char_u *p;
6870 int len;
6871
6872 if (name[0] > '~')
6873 {
6874 IObuff[0] = ' ';
6875 IObuff[1] = ' ';
6876 IObuff[2] = ' ';
6877 IObuff[3] = ' ';
6878 }
6879 else
6880 {
6881 IObuff[0] = 't';
6882 IObuff[1] = '_';
6883 IObuff[2] = name[0];
6884 IObuff[3] = name[1];
6885 }
6886 IObuff[4] = ' ';
6887
6888 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6889 if (p[1] != 't')
6890 STRCPY(IObuff + 5, p);
6891 else
6892 IObuff[5] = NUL;
6893 len = (int)STRLEN(IObuff);
6894 do
6895 IObuff[len++] = ' ';
6896 while (len < 17);
6897 IObuff[len] = NUL;
6898 if (code == NULL)
6899 len += 4;
6900 else
6901 len += vim_strsize(code);
6902
6903 if (printit)
6904 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006905 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006907 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 else
6909 msg_outtrans(code);
6910 }
6911 return len;
6912}
6913
6914#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6915/*
6916 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6917 * termcap codes from the terminal itself.
6918 * We get them one by one to avoid a very long response string.
6919 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006920static int xt_index_in = 0;
6921static int xt_index_out = 0;
6922
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006924req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
6926 xt_index_out = 0;
6927 xt_index_in = 0;
6928 req_more_codes_from_term();
6929}
6930
6931 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006932req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006934 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 int old_idx = xt_index_out;
6936
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006937 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 if (exiting)
6939 return;
6940
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006941 // Send up to 10 more requests out than we received. Avoid sending too
6942 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6944 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006945 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006946
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006947 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006948 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6949 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 out_str_nf((char_u *)buf);
6951 ++xt_index_out;
6952 }
6953
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006954 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 if (xt_index_out != old_idx)
6956 out_flush();
6957}
6958
6959/*
6960 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6961 * A "0" instead of the "1" indicates a code that isn't supported.
6962 * Both <name> and <string> are encoded in hex.
6963 * "code" points to the "0" or "1".
6964 */
6965 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006966got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
6968#define XT_LEN 100
6969 char_u name[3];
6970 char_u str[XT_LEN];
6971 int i;
6972 int j = 0;
6973 int c;
6974
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006975 // A '1' means the code is supported, a '0' means it isn't.
6976 // When half the length is > XT_LEN we can't use it.
6977 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6979 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006980 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 name[0] = hexhex2nr(code + 3);
6982 name[1] = hexhex2nr(code + 5);
6983 name[2] = NUL;
6984 for (i = 0; key_names[i] != NULL; ++i)
6985 {
6986 if (STRCMP(key_names[i], name) == 0)
6987 {
6988 xt_index_in = i;
6989 break;
6990 }
6991 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006992
Bram Moolenaarb255b902018-04-24 21:40:10 +02006993 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6994
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 if (key_names[i] != NULL)
6996 {
6997 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6998 str[j++] = c;
6999 str[j] = NUL;
7000 if (name[0] == 'C' && name[1] == 'o')
7001 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007002 // Color count is not a key code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007003 int val = atoi((char *)str);
7004#if defined(FEAT_EVAL)
7005 if (val == t_colors)
7006 ch_log(NULL, "got_code_from_term(Co): no change (%d)", val);
7007 else
7008 ch_log(NULL,
7009 "got_code_from_term(Co): changed from %d to %d",
7010 t_colors, val);
7011#endif
7012 may_adjust_color_count(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 }
7014 else
7015 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 i = find_term_bykeys(str);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007017 if (i >= 0 && name[0] == termcodes[i].name[0]
7018 && name[1] == termcodes[i].name[1])
7019 {
7020 // Existing entry with the same name and code - skip.
7021#ifdef FEAT_EVAL
7022 ch_log(NULL, "got_code_from_term(): Entry %c%c did not change",
7023 name[0], name[1]);
7024#endif
7025 }
7026 else
7027 {
7028 if (i >= 0)
7029 {
7030 // Delete an existing entry using the same code.
7031#ifdef FEAT_EVAL
7032 ch_log(NULL, "got_code_from_term(): Deleting entry %c%c with matching keys %s",
7033 termcodes[i].name[0], termcodes[i].name[1], str);
7034#endif
7035 del_termcode_idx(i);
7036 }
7037#ifdef FEAT_EVAL
7038 else
7039 ch_log(NULL, "got_code_from_term(): Adding entry %c%c with keys %s",
7040 name[0], name[1], str);
7041#endif
7042 add_termcode(name, str, ATC_FROM_TERM);
7043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 }
7045 }
7046 }
7047
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007048 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 ++xt_index_in;
7050 req_more_codes_from_term();
7051}
7052
7053/*
7054 * Check if there are any unanswered requests and deal with them.
7055 * This is called before starting an external program or getting direct
7056 * keyboard input. We don't want responses to be send to that program or
7057 * handled as typed text.
7058 */
7059 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007060check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061{
7062 int c;
7063
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007064 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 if (xt_index_out == 0 || xt_index_out == xt_index_in)
7066 return;
7067
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007068 // Vgetc() will check for and handle any response.
7069 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 ++no_mapping;
7071 ++allow_keys;
7072 for (;;)
7073 {
7074 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007075 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 break;
7077
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007078 // If a response is recognized it's replaced with K_IGNORE, must read
7079 // it from the input stream. If there is no K_IGNORE we can't do
7080 // anything, break here (there might be some responses further on, but
7081 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 if (c != K_SPECIAL && c != K_IGNORE)
7083 break;
7084 c = vgetc();
7085 if (c != K_IGNORE)
7086 {
7087 vungetc(c);
7088 break;
7089 }
7090 }
7091 --no_mapping;
7092 --allow_keys;
7093}
7094#endif
7095
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007096#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097static char ksme_str[20];
7098static char ksmr_str[20];
7099static char ksmd_str[20];
7100
7101/*
7102 * For Win32 console: update termcap codes for existing console attributes.
7103 */
7104 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007105update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00007107 sprintf(ksme_str, "\033|%dm", attr);
7108 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
7109 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110
Bram Moolenaar4654d632022-11-17 22:05:12 +00007111 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
7112 if (p == NULL) // did not find it
7113 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 while (p->bt_string != NULL)
7115 {
7116 if (p->bt_entry == (int)KS_ME)
7117 p->bt_string = &ksme_str[0];
7118 else if (p->bt_entry == (int)KS_MR)
7119 p->bt_string = &ksmr_str[0];
7120 else if (p->bt_entry == (int)KS_MD)
7121 p->bt_string = &ksmd_str[0];
7122 ++p;
7123 }
7124}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007125
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007126# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007127# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007128
7129typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007130{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007131 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
7132 CMODE_RGB, // Use 24bit RGB colors using VTP.
7133 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
7134 CMODE_LAST,
7135} cmode_T;
7136
7137struct ks_tbl_S
7138{
7139 int code; // value of KS_
7140 char *vtp; // code in RGB mode
7141 char *vtp2; // code in 256color mode
7142 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007143};
7144
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007145static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007146{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007147 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
7148 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
7149 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
7150 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
7151 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
7152 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
7153 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
7154 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
7155 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007156# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007157 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
7158 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
7159 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
7160 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007161# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007162 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
7163 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
7164 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
7165 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007166# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007167 {(int)KS_CCO, "256", "256", {""}}, // colors
7168 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007169};
7170
Bram Moolenaar4654d632022-11-17 22:05:12 +00007171/*
7172 * Find the first entry for "code" in the builtin termcap for "name".
7173 * Returns NULL when not found.
7174 */
7175 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007176find_first_tcap(
7177 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007178 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007179{
Bram Moolenaar4654d632022-11-17 22:05:12 +00007180 tcap_entry_T *p = find_builtin_term(name);
7181 if (p != NULL)
7182 {
7183 while (p->bt_string != NULL)
7184 {
7185 if (p->bt_entry == code)
7186 return p;
7187 ++p;
7188 }
7189 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007190 return NULL;
7191}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007192# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007193
7194/*
7195 * For Win32 console: replace the sequence immediately after termguicolors.
7196 */
7197 void
7198swap_tcap(void)
7199{
7200# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007201 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007202 static cmode_T curr_mode;
7203 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007204 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007205
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007206 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007207 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007208 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007209 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007210 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007211 if (bt != NULL)
7212 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007213 // Preserve the original value.
7214 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
7215 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
7216 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007217
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007218 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007219 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007220 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007221 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007222 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007223 }
7224
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007225 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007226 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007227 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007228 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007229 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007230 mode = CMODE_INDEXED;
7231
7232 if (mode == curr_mode)
7233 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007234
7235 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007236 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007237 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007238 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007239 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007240 }
7241
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007242 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007243# endif
7244}
7245
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02007247
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007248
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007249#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007250 || defined(PROTO)
7251static int cube_value[] = {
7252 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7253};
7254
7255static int grey_ramp[] = {
7256 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7257 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7258};
7259
LemonBoyb2b3acb2022-05-20 10:10:34 +01007260static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01007261// R G B
7262 { 0, 0, 0}, // black
7263 {224, 0, 0}, // dark red
7264 { 0, 224, 0}, // dark green
7265 {224, 224, 0}, // dark yellow / brown
7266 { 0, 0, 224}, // dark blue
7267 {224, 0, 224}, // dark magenta
7268 { 0, 224, 224}, // dark cyan
7269 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007270
LemonBoyd2a46622022-05-01 17:43:33 +01007271 {128, 128, 128}, // dark grey
7272 {255, 64, 64}, // light red
7273 { 64, 255, 64}, // light green
7274 {255, 255, 64}, // yellow
7275 { 64, 64, 255}, // light blue
7276 {255, 64, 255}, // light magenta
7277 { 64, 255, 255}, // light cyan
7278 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007279};
7280
LemonBoyd2a46622022-05-01 17:43:33 +01007281#if defined(MSWIN)
7282// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7283static const char_u cterm_ansi_idx[] = {
7284 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7285};
7286#endif
7287
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007288#define ANSI_INDEX_NONE 0
7289
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007290 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007291ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7292{
7293 if (nr < 16)
7294 {
7295 *r = ansi_table[nr][0];
7296 *g = ansi_table[nr][1];
7297 *b = ansi_table[nr][2];
7298 *ansi_idx = nr;
7299 }
7300 else
7301 {
7302 *r = 0;
7303 *g = 0;
7304 *b = 0;
7305 *ansi_idx = ANSI_INDEX_NONE;
7306 }
7307}
7308
7309 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007310cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007311{
7312 int idx;
7313
7314 if (nr < 16)
7315 {
LemonBoyd2a46622022-05-01 17:43:33 +01007316#if defined(MSWIN)
7317 idx = cterm_ansi_idx[nr];
7318#else
7319 idx = nr;
7320#endif
7321 *r = ansi_table[idx][0];
7322 *g = ansi_table[idx][1];
7323 *b = ansi_table[idx][2];
7324 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007325 }
7326 else if (nr < 232)
7327 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007328 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007329 idx = nr - 16;
7330 *r = cube_value[idx / 36 % 6];
7331 *g = cube_value[idx / 6 % 6];
7332 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007333 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007334 }
7335 else if (nr < 256)
7336 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007337 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007338 idx = nr - 232;
7339 *r = grey_ramp[idx];
7340 *g = grey_ramp[idx];
7341 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007342 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007343 }
7344 else
7345 {
7346 *r = 0;
7347 *g = 0;
7348 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007349 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007350 }
7351}
7352#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007353
Bram Moolenaarf4140482020-02-15 23:06:45 +01007354/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007355 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007356 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007357 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007358 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007359 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007360term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007361{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007362 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007363 int i;
7364 int c;
7365
7366 for (i = ta_len; i < ta_len + len; ++i)
7367 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007368 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7369 {
7370 int modifiers = ta_buf[i + 2];
7371 int key = ta_buf[i + 3];
7372
7373 // Try to use the modifier to modify the key. In any case drop the
7374 // modifier.
7375 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7376 len -= 3;
7377 if (key < 0x80)
7378 key = merge_modifyOtherKeys(key, &modifiers);
7379 ta_buf[i] = key;
7380 }
7381 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007382 {
7383 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7384 if (c == K_DEL || c == K_KDEL || c == K_BS)
7385 {
7386 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007387 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007388 if (c == K_DEL || c == K_KDEL)
7389 ta_buf[i] = DEL;
7390 else
7391 ta_buf[i] = Ctrl_H;
7392 len -= 2;
7393 }
7394 }
7395 else if (ta_buf[i] == '\r')
7396 ta_buf[i] = '\n';
7397 if (has_mbyte)
7398 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7399 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007400 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007401}