blob: 0bb6d71904a32a33cb867d363fd6e91890b09279 [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",
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001677 "PS", "PE",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 NULL
1679};
1680#endif
1681
Bram Moolenaar77071372022-12-30 19:54:53 +00001682#if defined(HAVE_TGETENT) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001683/*
1684 * Return TRUE if "term_strings[idx]" was not set.
1685 */
1686 static int
1687term_strings_not_set(enum SpecialKey idx)
1688{
1689 return TERM_STR(idx) == NULL || TERM_STR(idx) == empty_option;
1690}
Bram Moolenaar77071372022-12-30 19:54:53 +00001691#endif
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001692
Bram Moolenaar9289df52018-05-10 14:40:57 +02001693#ifdef HAVE_TGETENT
Bram Moolenaarafa3f1c2022-12-19 18:56:48 +00001694/*
1695 * Get the termcap entries we need with tgetstr(), tgetflag() and tgetnum().
1696 * "invoke_tgetent()" must have been called before.
1697 * If "*height" or "*width" are not zero then use the "li" and "col" entries to
1698 * get their value.
1699 */
Bram Moolenaar69e05692018-05-10 14:11:52 +02001700 static void
1701get_term_entries(int *height, int *width)
1702{
1703 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001704 enum SpecialKey dest; // index in term_strings[]
1705 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001706 } string_names[] =
1707 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1708 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1709 {KS_CL, "cl"}, {KS_CD, "cd"},
1710 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1711 {KS_ME, "me"}, {KS_MR, "mr"},
1712 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1713 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1714 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001715 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001716 {KS_STE,"Te"}, {KS_STS,"Ts"},
1717 {KS_CM, "cm"}, {KS_SR, "sr"},
1718 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1719 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001720 {KS_CTI, "TI"}, {KS_CRK, "RK"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001721 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001722 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1723 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001724 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1725 {KS_VS, "vs"}, {KS_CVS, "VS"},
1726 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1727 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1728 {KS_TS, "ts"}, {KS_FS, "fs"},
1729 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1730 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1731 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001732 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001733 {KS_CBE, "BE"}, {KS_CBD, "BD"},
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;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001739 static char_u tstrbuf[TBUFSZ];
1740 char_u *tp = tstrbuf;
1741
1742 /*
1743 * get output strings
1744 */
1745 for (i = 0; string_names[i].name != NULL; ++i)
1746 {
Bram Moolenaar96dd34e2022-12-30 11:16:00 +00001747 if (term_strings_not_set(string_names[i].dest))
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001748 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001749 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001750#ifdef FEAT_EVAL
1751 set_term_option_sctx_idx(string_names[i].name, -1);
1752#endif
1753 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001754 }
1755
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001756 // tgetflag() returns 1 if the flag is present, 0 if not and
1757 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001758 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1759 T_MS = (char_u *)"y";
1760 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1761 T_XS = (char_u *)"y";
1762 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1763 T_XN = (char_u *)"y";
1764 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1765 T_DB = (char_u *)"y";
1766 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1767 T_DA = (char_u *)"y";
1768 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1769 T_UT = (char_u *)"y";
1770
1771 /*
1772 * get key codes
1773 */
1774 for (i = 0; key_names[i] != NULL; ++i)
1775 if (find_termcode((char_u *)key_names[i]) == NULL)
1776 {
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001777 char_u *p = TGETSTR(key_names[i], &tp);
1778
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);
Bram Moolenaar7b8db112022-12-30 21:10:25 +00001806 char_u *p = TGETSTR("pc", &tp);
1807 if (p != NULL)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001808 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/*
Bram Moolenaarfc966c12023-01-01 18:04:33 +00003760 * Output T_BE, but only when t_PS and t_PE are set.
3761 */
3762 void
3763out_str_t_BE(void)
3764{
3765 char_u *p;
3766
3767 if (T_BE == NULL || *T_BE == NUL
3768 || (p = find_termcode((char_u *)"PS")) == NULL || *p == NUL
3769 || (p = find_termcode((char_u *)"PE")) == NULL || *p == NUL)
3770 return;
3771 out_str(T_BE);
3772}
3773
3774/*
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003775 * If t_TI was recently sent and there is no typeahead or work to do, now send
3776 * t_RK. This is postponed to avoid the response arriving in a shell command
3777 * or after Vim exits.
3778 */
3779 void
3780may_send_t_RK(void)
3781{
3782 if (send_t_RK
3783 && !work_pending()
3784 && !ex_normal_busy
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003785#ifdef FEAT_EVAL
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003786 && !in_feedkeys
Bram Moolenaarc3f18812022-12-01 12:29:43 +00003787#endif
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003788 && !exiting)
3789 {
3790 send_t_RK = FALSE;
3791 out_str(T_CRK);
3792 out_flush();
3793 }
3794}
3795
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3798 * commands and Ex mode).
3799 */
3800 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003801settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802{
3803#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003804 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 if (gui.in_use)
3806 return;
3807#endif
3808
3809 if (full_screen)
3810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003812 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3813 * set the terminal to raw mode, even though we think it already is,
3814 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 * When we think the terminal is normal, don't try to set it to
3816 * normal again, because that causes problems (logout!) on some
3817 * machines.
3818 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003819 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 {
3821#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003822# ifdef FEAT_GUI
3823 if (!gui.in_use && !gui.starting)
3824# endif
3825 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003826 // May need to check for T_CRV response and termcodes, it
3827 // doesn't work in Cooked mode, an external program may get
3828 // them.
3829 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003830 (void)vpeekc_nomap();
3831 check_for_codes_from_term();
3832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003835 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003836
3837 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3838 // Avoid doing this too often, on some terminals the codes are not
3839 // handled properly.
3840 if (termcap_active && tmode != TMODE_SLEEP
3841 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003842 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003843 MAY_WANT_TO_LOG_THIS;
3844
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003845 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003846 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003847 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003848 out_str_t_TE(); // possibly disables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003849 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003850 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003851 {
Bram Moolenaarfc966c12023-01-01 18:04:33 +00003852 out_str_t_BE(); // enable bracketed paste mode (should
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003853 // be before mch_settmode().
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003854 out_str_t_TI(); // possibly enables modifyOtherKeys
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003855 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003858 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003861 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 out_flush();
3863 }
3864#ifdef FEAT_TERMRESPONSE
3865 may_req_termresponse();
3866#endif
3867 }
3868}
3869
3870 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003871starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872{
3873 if (full_screen && !termcap_active)
3874 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003875 MAY_WANT_TO_LOG_THIS;
3876
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003877 out_str(T_TI); // start termcap mode
Bram Moolenaar733a69b2022-12-01 12:03:47 +00003878 out_str_t_TI(); // start "raw" mode
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003879 out_str(T_KS); // start "keypad transmit" mode
Bram Moolenaarfc966c12023-01-01 18:04:33 +00003880 out_str_t_BE(); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003881
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003882#if defined(UNIX) || defined(VMS)
3883 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003884 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003885 out_str(T_FE);
3886#endif
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 out_flush();
3889 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003890 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003892# ifdef FEAT_GUI
3893 if (!gui.in_use && !gui.starting)
3894# endif
3895 {
3896 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003897 // Immediately check for a response. If t_Co changes, we don't
3898 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003899 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003900 check_for_codes_from_term();
3901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902#endif
3903 }
3904}
3905
3906 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003907stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908{
3909 screen_stop_highlight();
3910 reset_cterm_colors();
3911 if (termcap_active)
3912 {
3913#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003914# ifdef FEAT_GUI
3915 if (!gui.in_use && !gui.starting)
3916# endif
3917 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003918 // May need to discard T_CRV, T_U7 or T_RBG response.
3919 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003920 {
3921# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003922 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003923 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003924# endif
3925# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003926 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003927 if (exiting)
3928 tcflush(fileno(stdin), TCIFLUSH);
3929# endif
3930 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003931 // Check for termcodes first, otherwise an external program may
3932 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003933 check_for_codes_from_term();
3934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003936 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003937
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003938#if defined(UNIX) || defined(VMS)
3939 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003940 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003941 out_str(T_FD);
3942#endif
3943
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003944 out_str(T_BD); // disable bracketed paste mode
3945 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 out_flush();
3947 termcap_active = FALSE;
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00003948
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00003949 // Output t_te before t_TE, t_te may switch between main and alternate
3950 // screen and following codes may work on the active screen only.
3951 //
3952 // When using the Kitty keyboard protocol the main and alternate screen
3953 // use a separate state. If we are (or were) using the Kitty keyboard
3954 // protocol and t_te is not empty (possibly switching screens) then
3955 // output t_TE both before and after outputting t_te.
3956 if (*T_TE != NUL && (kitty_protocol_state == KKPS_ENABLED
3957 || kitty_protocol_state == KKPS_DISABLED))
3958 out_str_t_TE(); // probably disables the kitty keyboard
3959 // protocol
3960
Bram Moolenaar4ab1f4a2022-12-16 13:08:36 +00003961 out_str(T_TE); // stop termcap mode
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003962 cursor_on(); // just in case it is still off
Bram Moolenaar63a2e362022-11-23 20:20:18 +00003963 out_str_t_TE(); // stop "raw" mode, modifyOtherKeys and
3964 // Kitty keyboard protocol
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003965 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 out_flush();
3967 }
3968}
3969
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003970#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971/*
3972 * Request version string (for xterm) when needed.
3973 * Only do this after switching to raw mode, otherwise the result will be
3974 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003975 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003976 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 * Only do this after termcap mode has been started, otherwise the codes for
3978 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003979 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3980 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003981 * On Unix only do it when both output and input are a tty (avoid writing
3982 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 * The result is caught in check_termcode().
3984 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003985 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003986may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003988 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003989 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003990 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 && *T_CRV != NUL)
3992 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003993 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003994 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003996 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003997 // check for the characters now, otherwise they might be eaten by
3998 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 out_flush();
4000 (void)vpeekc_nomap();
4001 }
4002}
Bram Moolenaar9584b312013-03-13 19:29:28 +01004003
Bram Moolenaar9584b312013-03-13 19:29:28 +01004004/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02004005 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
4006 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004007 * Note that this goes out before T_CRV, so that the result can be used when
4008 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01004009 */
4010 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02004011check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01004012{
Bram Moolenaara45551a2020-06-09 15:57:37 +02004013 int did_send = FALSE;
4014
4015 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
4016 return;
4017
Bram Moolenaarafd78262019-05-10 23:10:31 +02004018 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01004019 && !option_was_set((char_u *)"ambiwidth"))
4020 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004021 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01004022
Bram Moolenaara45551a2020-06-09 15:57:37 +02004023 // Ambiguous width check.
4024 // Check how the terminal treats ambiguous character width (UAX #11).
4025 // First, we move the cursor to (1, 0) and print a test ambiguous
4026 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
4027 // the current cursor position. If the terminal treats \u25bd as
4028 // single width, the position is (1, 1), or if it is treated as double
4029 // width, that will be (1, 2). This function has the side effect that
4030 // changes cursor position, so it must be called immediately after
4031 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004032 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004033 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004034 // Do this in the second row. In the first row the returned sequence
4035 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004036 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004037 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02004038 out_str(buf);
4039 out_str(T_U7);
4040 termrequest_sent(&u7_status);
4041 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02004042 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02004043
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004044 // This overwrites a few characters on the screen, a redraw is needed
4045 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02004046 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02004047 term_windgoto(1, 0);
4048 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004049 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004050 }
4051
Bram Moolenaard1f34e62022-01-07 19:24:20 +00004052 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02004053 {
4054 // 2. Check compatibility with xterm.
4055 // We move the cursor to (2, 0), print a test sequence and then query
4056 // the current cursor position. If the terminal properly handles
4057 // unknown DCS string and CSI sequence with intermediate byte, the test
4058 // sequence is ignored and the cursor does not move. If the terminal
4059 // handles test sequence incorrectly, a garbage string is displayed and
4060 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004061 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02004062 LOG_TR(("Sending xterm compatibility test sequence."));
4063 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004064 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02004065 term_windgoto(2, 0);
4066 // send the test DCS string.
4067 out_str((char_u *)"\033Pzz\033\\");
4068 // send the test CSI sequence with intermediate byte.
4069 out_str((char_u *)"\033[0%m");
4070 out_str(T_U7);
4071 termrequest_sent(&xcc_status);
4072 out_flush();
4073 did_send = TRUE;
4074
4075 // If the terminal handles test sequence incorrectly, garbage text is
4076 // displayed. Clear them out for now.
4077 screen_stop_highlight();
4078 term_windgoto(2, 0);
4079 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02004080 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02004081 }
4082
4083 if (did_send)
4084 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02004085 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02004086
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004087 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004088 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01004089
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004090 // check for the characters now, otherwise they might be eaten by
4091 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02004092 out_flush();
4093 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01004094 }
4095}
Bram Moolenaar2951b772013-07-03 12:45:31 +02004096
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004097/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02004098 * Similar to requesting the version string: Request the terminal background
4099 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004100 */
4101 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004102may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004103{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004104 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004105 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004106 int didit = FALSE;
4107
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004108# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004109 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004110 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004111 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004112 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004113 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004114 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004115 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004116 didit = TRUE;
4117 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02004118# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004119
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004120 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004121 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004122 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004123 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004124 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004125 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004126 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004127 didit = TRUE;
4128 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004129
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004130 if (didit)
4131 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004132 // check for the characters now, otherwise they might be eaten by
4133 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004134 out_flush();
4135 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004136 }
4137 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004138}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004139
Bram Moolenaar2951b772013-07-03 12:45:31 +02004140# ifdef DEBUG_TERMRESPONSE
4141 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02004142log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004143{
4144 static FILE *fd_tr = NULL;
4145 static proftime_T start;
4146 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004147 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004148
4149 if (fd_tr == NULL)
4150 {
4151 fd_tr = fopen("termresponse.log", "w");
4152 profile_start(&start);
4153 }
4154 now = start;
4155 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02004156 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004157 must_redraw == UPD_NOT_VALID ? "NV"
4158 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02004159 va_start(ap, fmt);
4160 vfprintf(fd_tr, fmt, ap);
4161 va_end(ap);
4162 fputc('\n', fd_tr);
4163 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02004164}
4165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166#endif
4167
4168/*
4169 * Return TRUE when saving and restoring the screen.
4170 */
4171 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004172swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173{
4174 return (full_screen && *T_TI != NUL);
4175}
4176
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177/*
4178 * By outputting the 'cursor very visible' termcap code, for some windowed
4179 * terminals this makes the screen scrolled to the correct position.
4180 * Used when starting Vim or returning from a shell.
4181 */
4182 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004183scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004185 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004187 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004189 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004190 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192}
4193
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004194// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195static int cursor_is_off = FALSE;
4196
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004197// True if cursor is not visible due to an ongoing cursor-less sleep
4198static int cursor_is_asleep = FALSE;
4199
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02004201 * Enable the cursor without checking if it's already enabled.
4202 */
4203 void
4204cursor_on_force(void)
4205{
4206 out_str(T_VE);
4207 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004208 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02004209}
4210
4211/*
4212 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 */
4214 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004215cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004217 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02004218 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219}
4220
4221/*
4222 * Disable the cursor.
4223 */
4224 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004225cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004227 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004229 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 cursor_is_off = TRUE;
4231 }
4232}
4233
Dominique Pelle748b3082022-01-08 12:41:16 +00004234#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004235/*
4236 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4237 */
4238 int
4239cursor_is_sleeping(void)
4240{
4241 return cursor_is_asleep;
4242}
Dominique Pelle748b3082022-01-08 12:41:16 +00004243#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004244
4245/*
4246 * Disable the cursor and mark it disabled by cursor-less sleep
4247 */
4248 void
4249cursor_sleep(void)
4250{
4251 cursor_is_asleep = TRUE;
4252 cursor_off();
4253}
4254
4255/*
4256 * Enable the cursor and mark it not disabled by cursor-less sleep
4257 */
4258 void
4259cursor_unsleep(void)
4260{
4261 cursor_is_asleep = FALSE;
4262 cursor_on();
4263}
4264
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004265#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004267 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004268 */
4269 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004270term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004271{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004272 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004273 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004274
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004275 // Only do something when redrawing the screen and we can restore the
4276 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004277 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004278 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004279# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004280 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004281 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004282 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004283# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004284 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004285 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004286
Bram Moolenaar24959102022-05-07 20:01:16 +01004287 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004288 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004289 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004290 {
4291 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004292 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004293 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004294 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004295 if (*p != NUL)
4296 {
4297 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01004298 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004299 }
4300 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004301 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004302 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004303 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004304 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004305 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004306 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004307 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004308 }
4309 }
Bram Moolenaar24959102022-05-07 20:01:16 +01004310 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004311 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004312 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004313 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004314 }
4315}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004316
4317# if defined(FEAT_TERMINAL) || defined(PROTO)
4318 void
4319term_cursor_color(char_u *color)
4320{
4321 if (*T_CSC != NUL)
4322 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004323 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004324 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004325 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004326 out_flush();
4327 }
4328}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004329# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004330
Bram Moolenaar4db25542017-08-28 22:43:05 +02004331 int
4332blink_state_is_inverted()
4333{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004334#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004335 return rbm_status.tr_progress == STATUS_GOT
4336 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004337 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004338#else
4339 return FALSE;
4340#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004341}
4342
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004343/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004344 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004345 */
4346 void
4347term_cursor_shape(int shape, int blink)
4348{
4349 if (*T_CSH != NUL)
4350 {
4351 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4352 out_flush();
4353 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004354 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004355 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004356 int do_blink = blink;
4357
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004358 // t_SH is empty: try setting just the blink state.
4359 // The blink flags are XORed together, if the initial blinking from
4360 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004361 if (blink_state_is_inverted())
4362 do_blink = !blink;
4363
4364 if (do_blink && *T_VS != NUL)
4365 {
4366 out_str(T_VS);
4367 out_flush();
4368 }
4369 else if (!do_blink && *T_CVS != NUL)
4370 {
4371 out_str(T_CVS);
4372 out_flush();
4373 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004374 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004375}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004376#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004377
4378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 * Set scrolling region for window 'wp'.
4380 * The region starts 'off' lines from the start of the window.
4381 * Also set the vertical scroll region for a vertically split window. Always
4382 * the full width of the window, excluding the vertical separator.
4383 */
4384 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004385scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386{
4387 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4388 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004390 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4391 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004392 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393}
4394
4395/*
4396 * Reset scrolling region to the whole screen.
4397 */
4398 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004399scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400{
4401 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 if (*T_CSV != NUL)
4403 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004404 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405}
4406
4407
4408/*
4409 * List of terminal codes that are currently recognized.
4410 */
4411
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004412static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004414 char_u name[2]; // termcap name of entry
4415 char_u *code; // terminal code (in allocated memory)
4416 int len; // STRLEN(code)
4417 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418} *termcodes = NULL;
4419
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004420static int tc_max_len = 0; // number of entries that termcodes[] can hold
4421static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004423static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004424
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004426clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427{
4428 while (tc_len > 0)
4429 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004430 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 tc_max_len = 0;
4432
4433#ifdef HAVE_TGETENT
4434 BC = (char *)empty_option;
4435 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004436 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 ospeed = 0;
4438#endif
4439
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004440 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441}
4442
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004443#define ATC_FROM_TERM 55
4444
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445/*
Bram Moolenaarb2646172022-12-17 15:35:43 +00004446 * Add a new entry for "name[2]" to the list of terminal codes.
4447 * Note that "name" may not have a terminating NUL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004449 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4450 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 */
4452 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004453add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454{
4455 struct termcode *new_tc;
4456 int i, j;
4457 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004458 int len;
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004459#ifdef FEAT_EVAL
4460 char *action = "Setting";
4461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
4463 if (string == NULL || *string == NUL)
4464 {
4465 del_termcode(name);
4466 return;
4467 }
4468
Bram Moolenaar4f974752019-02-17 17:44:42 +01004469#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004470 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004471#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004472# ifdef VIMDLL
4473 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004474 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004475 else
4476# endif
4477 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 if (s == NULL)
4480 return;
4481
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004482 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004483 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004485 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 s[0] = term_7to8bit(string);
4487 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004488
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004489#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4490# ifdef VIMDLL
4491 if (!gui.in_use)
4492# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004493 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004494 if (s[0] == K_NUL)
4495 {
4496 STRMOVE(s + 1, s);
4497 s[1] = 3;
4498 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004499 }
4500#endif
4501
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004502 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004504 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
4506 /*
4507 * need to make space for more entries
4508 */
4509 if (tc_len == tc_max_len)
4510 {
4511 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004512 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 if (new_tc == NULL)
4514 {
4515 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004516 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 return;
4518 }
4519 for (i = 0; i < tc_len; ++i)
4520 new_tc[i] = termcodes[i];
4521 vim_free(termcodes);
4522 termcodes = new_tc;
4523 }
4524
4525 /*
4526 * Look for existing entry with the same name, it is replaced.
4527 * Look for an existing entry that is alphabetical higher, the new entry
4528 * is inserted in front of it.
4529 */
4530 for (i = 0; i < tc_len; ++i)
4531 {
4532 if (termcodes[i].name[0] < name[0])
4533 continue;
4534 if (termcodes[i].name[0] == name[0])
4535 {
4536 if (termcodes[i].name[1] < name[1])
4537 continue;
4538 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004539 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 */
4541 if (termcodes[i].name[1] == name[1])
4542 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004543 if (flags == ATC_FROM_TERM && (j = termcode_star(
4544 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004545 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004546 // Don't replace ESC[123;*X or ESC O*X with another when
4547 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004548 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004549 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004550 && s[len - 1]
4551 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004552 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004553 // They are equal but for the ";*": don't add it.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004554#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004555 ch_log(NULL, "Termcap entry %c%c did not change",
4556 name[0], name[1]);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004557#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004558 vim_free(s);
4559 return;
4560 }
4561 }
4562 else
4563 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004564 // Replace old code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004565#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004566 ch_log(NULL, "Termcap entry %c%c was: %s",
4567 name[0], name[1], termcodes[i].code);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004568#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004569 vim_free(termcodes[i].code);
4570 --tc_len;
4571 break;
4572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 }
4574 }
4575 /*
4576 * Found alphabetical larger entry, move rest to insert new entry
4577 */
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004578#ifdef FEAT_EVAL
4579 action = "Adding";
4580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 for (j = tc_len; j > i; --j)
4582 termcodes[j] = termcodes[j - 1];
4583 break;
4584 }
4585
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004586#ifdef FEAT_EVAL
Bram Moolenaarb2646172022-12-17 15:35:43 +00004587 ch_log(NULL, "%s termcap entry %c%c to %s", action, name[0], name[1], s);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00004588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 termcodes[i].name[0] = name[0];
4590 termcodes[i].name[1] = name[1];
4591 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004592 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004593
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004594 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4595 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004596 termcodes[i].modlen = 0;
4597 j = termcode_star(s, len);
4598 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004599 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004600 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004601 // For "CSI[@;X" the "@" is not included in "modlen".
4602 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4603 --termcodes[i].modlen;
4604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 ++tc_len;
4606}
4607
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004608/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004609 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004610 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004611 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004612 */
4613 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004614termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004615{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004616 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004617 if (len >= 3 && code[len - 2] == '*')
4618 {
4619 if (len >= 5 && code[len - 3] == ';')
4620 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004621 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004622 return 1;
4623 }
4624 return 0;
4625}
4626
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004628find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629{
4630 int i;
4631
4632 for (i = 0; i < tc_len; ++i)
4633 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4634 return termcodes[i].code;
4635 return NULL;
4636}
4637
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004639get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 if (i >= tc_len)
4642 return NULL;
4643 return &termcodes[i].name[0];
4644}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004646/*
4647 * Returns the length of the terminal code at index 'idx'.
4648 */
4649 int
4650get_termcode_len(int idx)
4651{
4652 return termcodes[idx].len;
4653}
4654
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004655 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004656del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
4658 int i;
4659
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004660 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 return;
4662
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004663 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
4665 for (i = 0; i < tc_len; ++i)
4666 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4667 {
4668 del_termcode_idx(i);
4669 return;
4670 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004671 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672}
4673
4674 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004675del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676{
4677 int i;
4678
4679 vim_free(termcodes[idx].code);
4680 --tc_len;
4681 for (i = idx; i < tc_len; ++i)
4682 termcodes[i] = termcodes[i + 1];
4683}
4684
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685/*
4686 * Called when detected that the terminal sends 8-bit codes.
4687 * Convert all 7-bit codes to their 8-bit equivalent.
4688 */
4689 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004690switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
4692 int i;
4693 int c;
4694
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004695 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 if (!term_is_8bit(T_NAME))
4697 {
4698 for (i = 0; i < tc_len; ++i)
4699 {
4700 c = term_7to8bit(termcodes[i].code);
4701 if (c != 0)
4702 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004703 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 termcodes[i].code[0] = c;
4705 }
4706 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004707 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004710 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
4713#ifdef CHECK_DOUBLE_CLICK
4714static linenr_T orig_topline = 0;
4715# ifdef FEAT_DIFF
4716static int orig_topfill = 0;
4717# endif
4718#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004719#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004721 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 * "orig_topline" is used to avoid detecting a double-click when the window
4723 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4724 */
4725/*
4726 * Set orig_topline. Used when jumping to another window, so that a double
4727 * click still works.
4728 */
4729 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004730set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
4732 orig_topline = wp->w_topline;
4733# ifdef FEAT_DIFF
4734 orig_topfill = wp->w_topfill;
4735# endif
4736}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004737
4738/*
4739 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4740 * topline and topfill.
4741 */
4742 int
4743is_mouse_topline(win_T *wp)
4744{
4745 return orig_topline == wp->w_topline
4746#ifdef FEAT_DIFF
4747 && orig_topfill == wp->w_topfill
4748#endif
4749 ;
4750}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751#endif
4752
4753/*
zeertzjqfc78a032022-04-26 22:11:38 +01004754 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4755 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4756 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004757 * Remove "slen" bytes.
4758 * Returns FAIL for error.
4759 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004760 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004761put_string_in_typebuf(
4762 int offset,
4763 int slen,
4764 char_u *string,
4765 int new_slen,
4766 char_u *buf,
4767 int bufsize,
4768 int *buflen)
4769{
4770 int extra = new_slen - slen;
4771
4772 string[new_slen] = NUL;
4773 if (buf == NULL)
4774 {
4775 if (extra < 0)
4776 // remove matched chars, taking care of noremap
4777 del_typebuf(-extra, offset);
4778 else if (extra > 0)
4779 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004780 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4781 == FAIL)
4782 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004783
4784 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4785 // typebuf.tb_buf[]!
4786 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4787 (size_t)new_slen);
4788 }
4789 else
4790 {
4791 if (extra < 0)
4792 // remove matched characters
4793 mch_memmove(buf + offset, buf + offset - extra,
4794 (size_t)(*buflen + offset + extra));
4795 else if (extra > 0)
4796 {
4797 // Insert the extra space we need. If there is insufficient
4798 // space return -1.
4799 if (*buflen + extra + new_slen >= bufsize)
4800 return FAIL;
4801 mch_memmove(buf + offset + extra, buf + offset,
4802 (size_t)(*buflen - offset));
4803 }
4804 mch_memmove(buf + offset, string, (size_t)new_slen);
4805 *buflen = *buflen + extra + new_slen;
4806 }
4807 return OK;
4808}
4809
4810/*
4811 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4812 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004813 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004814decode_modifiers(int n)
4815{
4816 int code = n - 1;
4817 int modifiers = 0;
4818
4819 if (code & 1)
4820 modifiers |= MOD_MASK_SHIFT;
4821 if (code & 2)
4822 modifiers |= MOD_MASK_ALT;
4823 if (code & 4)
4824 modifiers |= MOD_MASK_CTRL;
4825 if (code & 8)
4826 modifiers |= MOD_MASK_META;
Bram Moolenaar064fd672022-11-29 18:32:32 +00004827 // Any further modifiers are silently dropped.
4828
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004829 return modifiers;
4830}
4831
4832 static int
4833modifiers2keycode(int modifiers, int *key, char_u *string)
4834{
4835 int new_slen = 0;
4836
4837 if (modifiers != 0)
4838 {
4839 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004840 // make mappings work. This may result in a special key, such as
4841 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004842 *key = simplify_key(*key, &modifiers);
4843 if (modifiers != 0)
4844 {
4845 string[new_slen++] = K_SPECIAL;
4846 string[new_slen++] = (int)KS_MODIFIER;
4847 string[new_slen++] = modifiers;
4848 }
4849 }
4850 return new_slen;
4851}
4852
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004853/*
4854 * Handle a cursor position report.
4855 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004856 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004857handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004858{
4859 if (arg[0] == 2 && arg[1] >= 2)
4860 {
4861 char *aw = NULL;
4862
4863 LOG_TR(("Received U7 status: %s", tp));
4864 u7_status.tr_progress = STATUS_GOT;
4865 did_cursorhold = TRUE;
4866 if (arg[1] == 2)
4867 aw = "single";
4868 else if (arg[1] == 3)
4869 aw = "double";
4870 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4871 {
4872 // Setting the option causes a screen redraw. Do
4873 // that right away if possible, keeping any
4874 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004875 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004876#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004877 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004878 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004879
4880 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4881 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004882#else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004883 redraw_asap(UPD_CLEAR);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004884#endif
4885#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004886 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00004887#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004888 }
4889 }
4890 else if (arg[0] == 3)
4891 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004892 int value;
4893
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004894 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004895 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004896
4897 // Third row: xterm compatibility test.
4898 // If the cursor is on the first column then the terminal can handle
4899 // the request for cursor style and blinking.
4900 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4901 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4902 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004903 }
4904}
4905
4906/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004907 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004908 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004909 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004910 */
4911 static void
4912handle_version_response(int first, int *arg, int argc, char_u *tp)
4913{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004914 // The xterm version. It is set to zero when it can't be an actual xterm
4915 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004916 int version = arg[1];
4917
4918 LOG_TR(("Received CRV response: %s", tp));
4919 crv_status.tr_progress = STATUS_GOT;
4920 did_cursorhold = TRUE;
4921
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004922 // Reset terminal properties that are set based on the termresponse.
4923 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004924 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004925 init_term_props(
4926#ifdef FEAT_EVAL
4927 reset_term_props_on_termresponse
4928#else
4929 FALSE
4930#endif
4931 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004932
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004933 // If this code starts with CSI, you can bet that the
4934 // terminal uses 8-bit codes.
4935 if (tp[0] == CSI)
4936 switch_to_8bit();
4937
4938 // Screen sends 40500.
4939 // rxvt sends its version number: "20703" is 2.7.3.
4940 // Ignore it for when the user has set 'term' to xterm,
4941 // even though it's an rxvt.
4942 if (version > 20000)
4943 version = 0;
4944
zeertzjqfc78a032022-04-26 22:11:38 +01004945 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004946 if (first == '>' && argc == 3)
4947 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004948 // mintty 2.9.5 sends 77;20905;0c.
4949 // (77 is ASCII 'M' for mintty.)
4950 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004951 {
4952 // mintty can do SGR mouse reporting
4953 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4954 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004955
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004956#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004957 // If xterm version >= 141 try to get termcap codes. For other
4958 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004959 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004960 {
4961 LOG_TR(("Enable checking for XT codes"));
4962 check_for_codes = TRUE;
4963 need_gather = TRUE;
4964 req_codes_from_term();
4965 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00004966#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004967
4968 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01004969 // Konsole sends 0;115;0 and works the same way
4970 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004971 {
4972 // If run from Vim $COLORS is set to the number of
4973 // colors the terminal supports. Otherwise assume
4974 // 256, libvterm supports even more.
4975 if (mch_getenv((char_u *)"COLORS") == NULL)
4976 may_adjust_color_count(256);
4977 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004978 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004979 }
4980
4981 if (version == 95)
4982 {
4983 // Mac Terminal.app sends 1;95;0
4984 if (arg[0] == 1 && arg[2] == 0)
4985 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004986 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4987 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004988 }
4989 // iTerm2 sends 0;95;0
4990 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004991 {
4992 // iTerm2 can do SGR mouse reporting
4993 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4994 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004995 // old iTerm2 sends 0;95;
4996 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004997 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004998 }
4999
5000 // screen sends 83;40500;0 83 is 'S' in ASCII.
5001 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005002 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005003 // screen supports SGR mouse codes since 4.7.0
5004 if (arg[1] >= 40700)
5005 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5006 else
5007 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
5008 }
5009
5010 // If no recognized terminal has set mouse behavior, assume xterm.
5011 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
5012 {
5013 // Xterm version 277 supports SGR.
5014 // Xterm version >= 95 supports mouse dragging.
5015 if (version >= 277)
5016 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005017 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005018 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005019 }
5020
5021 // Detect terminals that set $TERM to something like
5022 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005023 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005024 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02005025 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005026 // xfce4-terminal sends 1;2802;0.
5027 // screen sends 83;40500;0
5028 // Assuming any version number over 2500 is not an
5029 // xterm (without the limit for rxvt and screen).
5030 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005031 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005032
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005033 else if (version == 136 && arg[2] == 0)
5034 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005035 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005036
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005037 // PuTTY sends 0;136;0
5038 if (arg[0] == 0)
5039 {
5040 // supports sgr-like mouse reporting.
5041 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
5042 }
5043 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005044 }
5045
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005046 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
5047 // commented out.
5048 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
5049 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005050
Bram Moolenaar1573e732022-11-16 20:33:21 +00005051 // Kitty up to 9.x sends 1;400{version};{secondary-version}
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005052 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
5053 {
5054 term_props[TPR_KITTY].tpr_status = TPR_YES;
5055 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
Bram Moolenaar731d0072022-12-18 17:47:18 +00005056
5057 // Kitty can handle SGR mouse reporting.
5058 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005059 }
5060
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005061 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005062 // 30600/40500 is a version number of GNU screen. DA2 support is added
5063 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
5064 // compatibility checking does not detect GNU screen.
5065 if (arg[0] == 83 && arg[1] >= 30600)
5066 {
5067 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5068 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
5069 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005070
5071 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005072 // 95, so assume anything below 95 is not xterm and hopefully supports
5073 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005074 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005075 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005076
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005077 // Getting the cursor style is only supported properly by xterm since
5078 // version 279 (otherwise it returns 0x18).
5079 if (version < 279)
5080 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
5081
5082 /*
5083 * Take action on the detected properties.
5084 */
5085
5086 // Unless the underline RGB color is expected to work, disable "t_8u".
5087 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005088 // This may cause some flicker. Alternative would be to set "t_8u"
5089 // here if the terminal is expected to support it, but that might
5090 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01005091 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
5092 && *T_8U != NUL
5093 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005094 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02005095 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
5096 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01005097 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005098#ifdef FEAT_TERMRESPONSE
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01005099 if (*T_8U != NUL && write_t_8u_state == MAYBE)
5100 // Did skip writing t_8u, a complete redraw is needed.
5101 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01005102 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005103#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005104
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005105 // Only set 'ttymouse' automatically if it was not set
5106 // by the user already.
5107 if (!option_was_set((char_u *)"ttym")
5108 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
5109 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
5110 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005111 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005112 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
5113 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
5114 }
5115
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005116#ifdef FEAT_TERMRESPONSE
5117 int need_flush = FALSE;
5118
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005119 // Only request the cursor style if t_SH and t_RS are
5120 // set. Only supported properly by xterm since version
5121 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005122 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005123 // Not for Terminal.app, it can't handle t_RS, it
5124 // echoes the characters to the screen.
5125 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005126 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005127 && *T_CSH != NUL
5128 && *T_CRS != 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 style request"));
5132 out_str(T_CRS);
5133 termrequest_sent(&rcs_status);
5134 need_flush = TRUE;
5135 }
5136
5137 // Only request the cursor blink mode if t_RC set. Not
5138 // for Gnome terminal, it can't handle t_RC, it
5139 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005140 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005141 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005142 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005143 && *T_CRC != NUL)
5144 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01005145 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005146 LOG_TR(("Sending cursor blink mode request"));
5147 out_str(T_CRC);
5148 termrequest_sent(&rbm_status);
5149 need_flush = TRUE;
5150 }
5151
5152 if (need_flush)
5153 out_flush();
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005154#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005155 }
5156}
5157
5158/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005159 * Add "key" to "buf" and return the number of bytes used.
5160 * Handles special keys and multi-byte characters.
5161 */
5162 static int
5163add_key_to_buf(int key, char_u *buf)
5164{
5165 int idx = 0;
5166
5167 if (IS_SPECIAL(key))
5168 {
5169 buf[idx++] = K_SPECIAL;
5170 buf[idx++] = KEY2TERMCAP0(key);
5171 buf[idx++] = KEY2TERMCAP1(key);
5172 }
5173 else if (has_mbyte)
5174 idx += (*mb_char2bytes)(key, buf + idx);
5175 else
5176 buf[idx++] = key;
5177 return idx;
5178}
5179
5180/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005181 * Shared between handle_key_with_modifier() and handle_csi_function_key().
5182 */
5183 static int
5184put_key_modifiers_in_typebuf(
5185 int key_arg,
5186 int modifiers_arg,
5187 int csi_len,
5188 int offset,
5189 char_u *buf,
5190 int bufsize,
5191 int *buflen)
5192{
5193 int key = key_arg;
5194 int modifiers = modifiers_arg;
5195
5196 // Some keys need adjustment when the Ctrl modifier is used.
5197 key = may_adjust_key_for_ctrl(modifiers, key);
5198
5199 // May remove the shift modifier if it's already included in the key.
5200 modifiers = may_remove_shift_modifier(modifiers, key);
5201
5202 // Produce modifiers with K_SPECIAL KS_MODIFIER {mod}
5203 char_u string[MAX_KEY_CODE_LEN + 1];
5204 int new_slen = modifiers2keycode(modifiers, &key, string);
5205
5206 // Add the bytes for the key.
5207 new_slen += add_key_to_buf(key, string + new_slen);
5208
5209 string[new_slen] = NUL;
5210 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5211 buf, bufsize, buflen) == FAIL)
5212 return -1;
5213 return new_slen - csi_len + offset;
5214}
5215
5216/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005217 * Handle a sequence with key and modifier, one of:
5218 * {lead}27;{modifier};{key}~
5219 * {lead}{key};{modifier}u
5220 * Returns the difference in length.
5221 */
5222 static int
5223handle_key_with_modifier(
5224 int *arg,
5225 int trail,
5226 int csi_len,
5227 int offset,
5228 char_u *buf,
5229 int bufsize,
5230 int *buflen)
5231{
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005232 // Only set seenModifyOtherKeys for the "{lead}27;" code to avoid setting
5233 // it for terminals using the kitty keyboard protocol. Xterm sends
5234 // the form ending in "u" when the formatOtherKeys resource is set. We do
5235 // not support this.
5236 //
5237 // Do not set seenModifyOtherKeys if there was a positive response at any
5238 // time from requesting the kitty keyboard protocol state, these are not
5239 // expected to support modifyOtherKeys level 2.
5240 //
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005241 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
5242 // like this but does not have the modifyOtherKeys feature.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005243 if (trail != 'u'
5244 && (kitty_protocol_state == KKPS_INITIAL
5245 || kitty_protocol_state == KKPS_OFF
Bram Moolenaar9d1184c2022-12-16 18:33:20 +00005246 || kitty_protocol_state == KKPS_AFTER_T_TE)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005247 && term_props[TPR_KITTY].tpr_status != TPR_YES)
Bram Moolenaar1a173402022-12-02 12:28:47 +00005248 {
Bram Moolenaar5390c052022-12-02 13:10:03 +00005249#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005250 ch_log(NULL, "setting seenModifyOtherKeys to TRUE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005251#endif
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005252 seenModifyOtherKeys = TRUE;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005253 }
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01005254
Bram Moolenaar1a173402022-12-02 12:28:47 +00005255 int key = trail == 'u' ? arg[0] : arg[2];
5256 int modifiers = decode_modifiers(arg[1]);
5257 return put_key_modifiers_in_typebuf(key, modifiers,
5258 csi_len, offset, buf, bufsize, buflen);
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005259}
5260
5261/*
5262 * Handle a sequence with key without a modifier:
5263 * {lead}{key}u
5264 * Returns the difference in length.
5265 */
5266 static int
5267handle_key_without_modifier(
5268 int *arg,
5269 int csi_len,
5270 int offset,
5271 char_u *buf,
5272 int bufsize,
5273 int *buflen)
5274{
5275 char_u string[MAX_KEY_CODE_LEN + 1];
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00005276 int new_slen;
5277
5278 if (arg[0] == ESC)
5279 {
5280 // Putting Esc in the buffer creates ambiguity, it can be the start of
5281 // an escape sequence. Use K_ESC to avoid that.
5282 string[0] = K_SPECIAL;
5283 string[1] = KS_EXTRA;
5284 string[2] = KE_ESC;
5285 new_slen = 3;
5286 }
5287 else
5288 new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005289
5290 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
5291 buf, bufsize, buflen) == FAIL)
5292 return -1;
5293 return new_slen - csi_len + offset;
5294}
5295
5296/*
Bram Moolenaar1a173402022-12-02 12:28:47 +00005297 * CSI function key without or with modifiers:
5298 * {lead}[ABCDEFHPQRS]
5299 * {lead}1;{modifier}[ABCDEFHPQRS]
5300 * Returns zero when nog recognized, a positive number when recognized.
5301 */
5302 static int
5303handle_csi_function_key(
5304 int argc,
5305 int *arg,
5306 int trail,
5307 int csi_len,
5308 char_u *key_name,
5309 int offset,
5310 char_u *buf,
5311 int bufsize,
5312 int *buflen)
5313{
5314 key_name[0] = 'k';
5315 switch (trail)
5316 {
5317 case 'A': key_name[1] = 'u'; break; // K_UP
5318 case 'B': key_name[1] = 'd'; break; // K_DOWN
5319 case 'C': key_name[1] = 'r'; break; // K_RIGHT
5320 case 'D': key_name[1] = 'l'; break; // K_LEFT
5321
5322 // case 'E': keypad BEGIN - not supported
5323 case 'F': key_name[0] = '@'; key_name[1] = '7'; break; // K_END
5324 case 'H': key_name[1] = 'h'; break; // K_HOME
5325
5326 case 'P': key_name[1] = '1'; break; // K_F1
5327 case 'Q': key_name[1] = '2'; break; // K_F2
5328 case 'R': key_name[1] = '3'; break; // K_F3
5329 case 'S': key_name[1] = '4'; break; // K_F4
5330
5331 default: return 0; // not recognized
5332 }
5333
5334 int key = TERMCAP2KEY(key_name[0], key_name[1]);
5335 int modifiers = argc == 2 ? decode_modifiers(arg[1]) : 0;
5336 put_key_modifiers_in_typebuf(key, modifiers,
5337 csi_len, offset, buf, bufsize, buflen);
5338 return csi_len;
5339}
5340
5341/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005342 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02005343 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005344 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00005345 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
5346 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005347 * - Cursor position report: {lead}{row};{col}R
5348 * The final byte must be 'R'. It is used for checking the
5349 * ambiguous-width character state.
5350 *
5351 * - window position reply: {lead}3;{x};{y}t
5352 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005353 * - key with modifiers when modifyOtherKeys is enabled or the Kitty keyboard
5354 * protocol is used:
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005355 * {lead}27;{modifier};{key}~
5356 * {lead}{key};{modifier}u
Bram Moolenaarc255b782022-11-26 19:16:48 +00005357 *
Bram Moolenaar1a173402022-12-02 12:28:47 +00005358 * - function key with or without modifiers:
5359 * {lead}[ABCDEFHPQRS]
5360 * {lead}1;{modifier}[ABCDEFHPQRS]
5361 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005362 * Return 0 for no match, -1 for partial match, > 0 for full match.
5363 */
5364 static int
5365handle_csi(
5366 char_u *tp,
5367 int len,
5368 char_u *argp,
5369 int offset,
5370 char_u *buf,
5371 int bufsize,
5372 int *buflen,
5373 char_u *key_name,
5374 int *slen)
5375{
5376 int first = -1; // optional char right after {lead}
5377 int trail; // char that ends CSI sequence
5378 int arg[3] = {-1, -1, -1}; // argument numbers
Bram Moolenaar1a173402022-12-02 12:28:47 +00005379 int argc = 0; // number of arguments
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005380 char_u *ap = argp;
5381 int csi_len;
5382
5383 // Check for non-digit after CSI.
5384 if (!VIM_ISDIGIT(*ap))
5385 first = *ap++;
5386
Bram Moolenaar8ffb7e02022-12-03 10:13:30 +00005387 if (first >= 'A' && first <= 'Z')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005388 {
Bram Moolenaar1a173402022-12-02 12:28:47 +00005389 // If "first" is in [ABCDEFHPQRS] then it is actually the "trail" and
5390 // no argument follows.
5391 trail = first;
5392 first = -1;
5393 --ap;
5394 }
5395 else
5396 {
5397 // Find up to three argument numbers.
5398 for (argc = 0; argc < 3; )
5399 {
5400 if (ap >= tp + len)
5401 return -1;
5402 if (*ap == ';')
5403 arg[argc++] = -1; // omitted number
5404 else if (VIM_ISDIGIT(*ap))
5405 {
5406 arg[argc] = 0;
5407 for (;;)
5408 {
5409 if (ap >= tp + len)
5410 return -1;
5411 if (!VIM_ISDIGIT(*ap))
5412 break;
5413 arg[argc] = arg[argc] * 10 + (*ap - '0');
5414 ++ap;
5415 }
5416 ++argc;
5417 }
5418 if (*ap == ';')
5419 ++ap;
5420 else
5421 break;
5422 }
5423
5424 // mrxvt has been reported to have "+" in the version. Assume
5425 // the escape sequence ends with a letter or one of "{|}~".
5426 while (ap < tp + len
5427 && !(*ap >= '{' && *ap <= '~')
5428 && !ASCII_ISALPHA(*ap))
5429 ++ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005430 if (ap >= tp + len)
5431 return -1;
Bram Moolenaar1a173402022-12-02 12:28:47 +00005432 trail = *ap;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005433 }
5434
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005435 csi_len = (int)(ap - tp) + 1;
5436
Bram Moolenaarc255b782022-11-26 19:16:48 +00005437 // Response to XTQMODKEYS: "CSI > 4 ; Pv m" where Pv indicates the
5438 // modifyOtherKeys level. Drop similar responses.
5439 if (first == '>' && (argc == 1 || argc == 2) && trail == 'm')
5440 {
5441 if (arg[0] == 4 && argc == 2)
5442 modify_otherkeys_state = arg[1] == 2 ? MOKS_ENABLED : MOKS_OFF;
5443
5444 key_name[0] = (int)KS_EXTRA;
5445 key_name[1] = (int)KE_IGNORE;
5446 *slen = csi_len;
5447 }
5448
Bram Moolenaar1a173402022-12-02 12:28:47 +00005449 // Function key starting with CSI:
5450 // {lead}[ABCDEFHPQRS]
5451 // {lead}1;{modifier}[ABCDEFHPQRS]
5452 else if (first == -1 && ASCII_ISUPPER(trail)
5453 && (argc == 0 || (argc == 2 && arg[0] == 1)))
5454 {
5455 int res = handle_csi_function_key(argc, arg, trail,
5456 csi_len, key_name, offset, buf, bufsize, buflen);
5457 return res <= 0 ? res : len + res;
5458 }
5459
5460 // Cursor position report: {lead}{row};{col}R
5461 // Eat it when there are 2 arguments and it ends in 'R'.
5462 // Also when u7_status is not "sent", it may be from a previous Vim that
5463 // just exited. But not for <S-F3>, it sends something similar, check for
5464 // row and column to make sense.
Bram Moolenaarc255b782022-11-26 19:16:48 +00005465 else if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005466 {
5467 handle_u7_response(arg, tp, csi_len);
5468
5469 key_name[0] = (int)KS_EXTRA;
5470 key_name[1] = (int)KE_IGNORE;
5471 *slen = csi_len;
5472 }
5473
5474 // Version string: Eat it when there is at least one digit and
5475 // it ends in 'c'
5476 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5477 {
5478 handle_version_response(first, arg, argc, tp);
5479
5480 *slen = csi_len;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005481#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005482 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005483#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005484 apply_autocmds(EVENT_TERMRESPONSE,
5485 NULL, NULL, FALSE, curbuf);
5486 key_name[0] = (int)KS_EXTRA;
5487 key_name[1] = (int)KE_IGNORE;
5488 }
5489
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005490#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005491 // Check blinking cursor from xterm:
5492 // {lead}?12;1$y set
5493 // {lead}?12;2$y not set
5494 //
5495 // {lead} can be <Esc>[ or CSI
5496 else if (rbm_status.tr_progress == STATUS_SENT
5497 && first == '?'
5498 && ap == argp + 6
5499 && arg[0] == 12
5500 && ap[-1] == '$'
5501 && trail == 'y')
5502 {
5503 initial_cursor_blink = (arg[1] == '1');
5504 rbm_status.tr_progress = STATUS_GOT;
5505 LOG_TR(("Received cursor blinking mode response: %s", tp));
5506 key_name[0] = (int)KS_EXTRA;
5507 key_name[1] = (int)KE_IGNORE;
5508 *slen = csi_len;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005509# ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005510 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005511# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005512 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005513#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005514
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005515 // Kitty keyboard protocol status response: CSI ? flags u
5516 else if (first == '?' && argc == 1 && trail == 'u')
5517 {
5518 // The protocol has various "progressive enhancement flags" values, but
5519 // we only check for zero and non-zero here.
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005520 if (arg[0] == '0')
5521 {
5522 kitty_protocol_state = KKPS_OFF;
5523 }
5524 else
5525 {
5526 kitty_protocol_state = KKPS_ENABLED;
5527
5528 // Reset seenModifyOtherKeys just in case some key combination has
5529 // been seen that set it before we get the status response.
Bram Moolenaar5390c052022-12-02 13:10:03 +00005530#ifdef FEAT_EVAL
Bram Moolenaar1a173402022-12-02 12:28:47 +00005531 ch_log(NULL, "setting seenModifyOtherKeys to FALSE");
Bram Moolenaar5390c052022-12-02 13:10:03 +00005532#endif
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00005533 seenModifyOtherKeys = FALSE;
5534 }
Bram Moolenaar43300f62022-11-23 23:30:58 +00005535
5536 key_name[0] = (int)KS_EXTRA;
5537 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar63a2e362022-11-23 20:20:18 +00005538 *slen = csi_len;
5539 }
5540
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005541#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005542 // Check for a window position response from the terminal:
5543 // {lead}3;{x};{y}t
5544 else if (did_request_winpos && argc == 3 && arg[0] == 3
5545 && trail == 't')
5546 {
5547 winpos_x = arg[1];
5548 winpos_y = arg[2];
5549 // got finished code: consume it
5550 key_name[0] = (int)KS_EXTRA;
5551 key_name[1] = (int)KE_IGNORE;
5552 *slen = csi_len;
5553
5554 if (--did_request_winpos <= 0)
5555 winpos_status.tr_progress = STATUS_GOT;
5556 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005557#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005558
5559 // Key with modifier:
5560 // {lead}27;{modifier};{key}~
5561 // {lead}{key};{modifier}u
Bram Moolenaar064fd672022-11-29 18:32:32 +00005562 // Even though we only handle four modifiers and the {modifier} value
5563 // should be 16 or lower, we accept all modifier values to avoid the raw
5564 // sequence to be passed through.
5565 else if ((arg[0] == 27 && argc == 3 && trail == '~')
Bram Moolenaar7609c882022-10-19 20:07:09 +01005566 || (argc == 2 && trail == 'u'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005567 {
5568 return len + handle_key_with_modifier(arg, trail,
Bram Moolenaar064fd672022-11-29 18:32:32 +00005569 csi_len, offset, buf, bufsize, buflen);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005570 }
5571
Christopher Plewright03193062022-11-22 12:58:27 +00005572 // Key without modifier (Kitty sends this for Esc):
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005573 // {lead}{key}u
5574 else if (argc == 1 && trail == 'u')
5575 {
5576 return len + handle_key_without_modifier(arg,
5577 csi_len, offset, buf, bufsize, buflen);
5578 }
5579
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005580 // else: Unknown CSI sequence. We could drop it, but then the
5581 // user can't create a map for it.
5582 return 0;
5583}
5584
5585/*
5586 * Handle an OSC sequence, fore/background color response from the terminal:
5587 *
5588 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5589 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5590 *
5591 * {code} is 10 for foreground, 11 for background
5592 * {lead} can be <Esc>] or OSC
5593 * {tail} can be '\007', <Esc>\ or STERM.
5594 *
5595 * Consume any code that starts with "{lead}11;", it's also
5596 * possible that "rgba" is following.
5597 */
5598 static int
5599handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5600{
5601 int i, j;
5602
5603 j = 1 + (tp[0] == ESC);
5604 if (len >= j + 3 && (argp[0] != '1'
5605 || (argp[1] != '1' && argp[1] != '0')
5606 || argp[2] != ';'))
5607 i = 0; // no match
5608 else
5609 for (i = j; i < len; ++i)
5610 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5611 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5612 {
5613 int is_bg = argp[1] == '1';
5614 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5615 && tp[j + 16] == '/';
5616
5617 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5618 && (is_4digit
5619 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5620 {
5621 char_u *tp_r = tp + j + 7;
5622 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5623 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005624#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005625 int rval, gval, bval;
5626
5627 rval = hexhex2nr(tp_r);
5628 gval = hexhex2nr(tp_b);
5629 bval = hexhex2nr(tp_g);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005630#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005631 if (is_bg)
5632 {
5633 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5634 *tp_b) ? "light" : "dark";
5635
5636 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005637#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005638 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005639# ifdef FEAT_TERMINAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005640 bg_r = rval;
5641 bg_g = gval;
5642 bg_b = bval;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005643# endif
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005644#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005645 if (!option_was_set((char_u *)"bg")
5646 && STRCMP(p_bg, new_bg_val) != 0)
5647 {
5648 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005649 set_option_value_give_err((char_u *)"bg",
5650 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005651 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005652 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005653 }
5654 }
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005655#if defined(FEAT_TERMRESPONSE) && defined(FEAT_TERMINAL)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005656 else
5657 {
5658 LOG_TR(("Received RFG response: %s", tp));
5659 rfg_status.tr_progress = STATUS_GOT;
5660 fg_r = rval;
5661 fg_g = gval;
5662 fg_b = bval;
5663 }
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005664#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005665 }
5666
5667 // got finished code: consume it
5668 key_name[0] = (int)KS_EXTRA;
5669 key_name[1] = (int)KE_IGNORE;
5670 *slen = i + 1 + (tp[i] == ESC);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005671#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005672 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5673 : VV_TERMRFGRESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005674#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005675 break;
5676 }
5677 if (i == len)
5678 {
5679 LOG_TR(("not enough characters for RB"));
5680 return FAIL;
5681 }
5682 return OK;
5683}
5684
5685/*
5686 * Check for key code response from xterm:
5687 * {lead}{flag}+r<hex bytes><{tail}
5688 *
5689 * {lead} can be <Esc>P or DCS
5690 * {flag} can be '0' or '1'
5691 * {tail} can be Esc>\ or STERM
5692 *
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005693 * Check for resource response from xterm (and drop it):
5694 * {lead}{flag}+R<hex bytes>=<value>{tail}
5695 *
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005696 * Check for cursor shape response from xterm:
5697 * {lead}1$r<digit> q{tail}
5698 *
5699 * {lead} can be <Esc>P or DCS
5700 * {tail} can be <Esc>\ or STERM
5701 *
5702 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5703 */
5704 static int
5705handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5706{
5707 int i, j;
5708
5709 j = 1 + (tp[0] == ESC);
5710 if (len < j + 3)
5711 i = len; // need more chars
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005712 else if ((argp[1] != '+' && argp[1] != '$')
5713 || (argp[2] != 'r' && argp[2] != 'R'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005714 i = 0; // no match
5715 else if (argp[1] == '+')
5716 // key code response
5717 for (i = j; i < len; ++i)
5718 {
5719 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5720 || tp[i] == STERM)
5721 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005722#ifdef FEAT_TERMRESPONSE
Bram Moolenaar236dffa2022-11-18 21:20:25 +00005723 // handle a key code response, drop a resource response
5724 if (i - j >= 3 && argp[2] == 'r')
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005725 got_code_from_term(tp + j, i);
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005726#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005727 key_name[0] = (int)KS_EXTRA;
5728 key_name[1] = (int)KE_IGNORE;
5729 *slen = i + 1 + (tp[i] == ESC);
5730 break;
5731 }
5732 }
5733 else
5734 {
5735 // Probably the cursor shape response. Make sure that "i"
5736 // is equal to "len" when there are not sufficient
5737 // characters.
5738 for (i = j + 3; i < len; ++i)
5739 {
5740 if (i - j == 3 && !isdigit(tp[i]))
5741 break;
5742 if (i - j == 4 && tp[i] != ' ')
5743 break;
5744 if (i - j == 5 && tp[i] != 'q')
5745 break;
5746 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5747 break;
5748 if ((i - j == 6 && tp[i] == STERM)
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005749 || (i - j == 7 && tp[i] == '\\'))
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005750 {
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005751#ifdef FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005752 int number = argp[3] - '0';
5753
5754 // 0, 1 = block blink, 2 = block
5755 // 3 = underline blink, 4 = underline
5756 // 5 = vertical bar blink, 6 = vertical bar
5757 number = number == 0 ? 1 : number;
5758 initial_cursor_shape = (number + 1) / 2;
5759 // The blink flag is actually inverted, compared to
5760 // the value set with T_SH.
5761 initial_cursor_shape_blink =
5762 (number & 1) ? FALSE : TRUE;
5763 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e6072b2022-11-29 16:09:18 +00005764#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005765 LOG_TR(("Received cursor shape response: %s", tp));
5766
5767 key_name[0] = (int)KS_EXTRA;
5768 key_name[1] = (int)KE_IGNORE;
5769 *slen = i + 1;
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005770#ifdef FEAT_EVAL
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005771 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00005772#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005773 break;
5774 }
5775 }
5776 }
5777
5778 if (i == len)
5779 {
5780 // These codes arrive many together, each code can be
5781 // truncated at any point.
5782 LOG_TR(("not enough characters for XT"));
5783 return FAIL;
5784 }
5785 return OK;
5786}
5787
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 * Check if typebuf.tb_buf[] contains a terminal key code.
5790 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005791 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005793 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 * With a match, the match is removed, the replacement code is inserted in
5795 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5796 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005797 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5798 * "buflen" is then the length of the string in buf[] and is updated for
5799 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 */
5801 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005802check_termcode(
5803 int max_offset,
5804 char_u *buf,
5805 int bufsize,
5806 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807{
5808 char_u *tp;
5809 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005810 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005811 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005813 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 int offset;
5815 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005816 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005817 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005818 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005819 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 char_u string[MAX_KEY_CODE_LEN + 1];
5821 int i, j;
5822 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
5825 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5826
5827 /*
5828 * Speed up the checks for terminal codes by gathering all first bytes
5829 * used in termleader[]. Often this is just a single <Esc>.
5830 */
5831 if (need_gather)
5832 gather_termleader();
5833
5834 /*
5835 * Check at several positions in typebuf.tb_buf[], to catch something like
5836 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5837 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005838 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 * This is used often, KEEP IT FAST!
5840 */
5841 for (offset = 0; offset < max_offset; ++offset)
5842 {
5843 if (buf == NULL)
5844 {
5845 if (offset >= typebuf.tb_len)
5846 break;
5847 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005848 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 }
5850 else
5851 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005852 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 break;
5854 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005855 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 }
5857
5858 /*
5859 * Don't check characters after K_SPECIAL, those are already
5860 * translated terminal chars (avoid translating ~@^Hx).
5861 */
5862 if (*tp == K_SPECIAL)
5863 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005864 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 continue;
5866 }
5867
5868 /*
5869 * Skip this position if the character does not appear as the first
5870 * character in term_strings. This speeds up a lot, since most
5871 * termcodes start with the same character (ESC or CSI).
5872 */
5873 i = *tp;
5874 for (p = termleader; *p && *p != i; ++p)
5875 ;
5876 if (*p == NUL)
5877 continue;
5878
5879 /*
5880 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5881 * are in Insert mode.
5882 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005883 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 continue;
5885
Bram Moolenaar27efc622022-07-01 16:35:45 +01005886 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005887 key_name[0] = NUL; // no key name found yet
5888 key_name[1] = NUL; // no key name found yet
5889 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
5891#ifdef FEAT_GUI
5892 if (gui.in_use)
5893 {
5894 /*
5895 * GUI special key codes are all of the form [CSI xx].
5896 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005897 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 {
5899 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005900 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 slen = 3;
5902 key_name[0] = tp[1];
5903 key_name[1] = tp[2];
5904 }
5905 }
5906 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005907#endif // FEAT_GUI
Christopher Plewright03193062022-11-22 12:58:27 +00005908#ifdef MSWIN
5909 if (len >= 3 && tp[0] == CSI && tp[1] == KS_EXTRA
5910 && (tp[2] == KE_MOUSEUP
5911 || tp[2] == KE_MOUSEDOWN
5912 || tp[2] == KE_MOUSELEFT
5913 || tp[2] == KE_MOUSERIGHT))
5914 {
5915 // MS-Windows console sends mouse scroll events encoded:
5916 // - CSI
5917 // - KS_EXTRA
5918 // - {KE_MOUSE[UP|DOWN|LEFT|RIGHT]}
5919 slen = 3;
5920 key_name[0] = tp[1];
5921 key_name[1] = tp[2];
5922 }
5923 else
5924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005926 int mouse_index_found = -1;
5927
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 for (idx = 0; idx < tc_len; ++idx)
5929 {
5930 /*
5931 * Ignore the entry if we are not at the start of
5932 * typebuf.tb_buf[]
5933 * and there are not enough characters to make a match.
5934 * But only when the 'K' flag is in 'cpoptions'.
5935 */
5936 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005937 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 if (cpo_koffset && offset && len < slen)
5939 continue;
5940 if (STRNCMP(termcodes[idx].code, tp,
5941 (size_t)(slen > len ? len : slen)) == 0)
5942 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005943 int looks_like_mouse_start = FALSE;
5944
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005945 if (len < slen) // got a partial sequence
5946 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
5948 /*
5949 * When found a keypad key, check if there is another key
5950 * that matches and use that one. This makes <Home> to be
5951 * found instead of <kHome> when they produce the same
5952 * key code.
5953 */
5954 if (termcodes[idx].name[0] == 'K'
5955 && VIM_ISDIGIT(termcodes[idx].name[1]))
5956 {
5957 for (j = idx + 1; j < tc_len; ++j)
5958 if (termcodes[j].len == slen &&
5959 STRNCMP(termcodes[idx].code,
5960 termcodes[j].code, slen) == 0)
5961 {
5962 idx = j;
5963 break;
5964 }
5965 }
5966
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005967 if (slen == 2 && len > 2
5968 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005969 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005970 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005971 // The mouse termcode "ESC [" is also the prefix of
5972 // "ESC [ I" (focus gained) and other keys. Check some
5973 // more bytes to find out.
5974 if (!isdigit(tp[2]))
5975 {
5976 // ESC [ without number following: Only use it when
5977 // there is no other match.
5978 looks_like_mouse_start = TRUE;
5979 }
5980 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5981 {
5982 char_u *nr = tp + 2;
5983 int count = 0;
5984
5985 // If a digit is following it could be a key with
5986 // modifier, e.g., ESC [ 1;2P. Can be confused
5987 // with DEC_MOUSE, which requires four numbers
5988 // following. If not then it can't be a DEC_MOUSE
5989 // code.
5990 for (;;)
5991 {
5992 ++count;
5993 (void)getdigits(&nr);
5994 if (nr >= tp + len)
5995 return -1; // partial sequence
5996 if (*nr != ';')
5997 break;
5998 ++nr;
5999 if (nr >= tp + len)
6000 return -1; // partial sequence
6001 }
6002 if (count < 4)
6003 continue; // no match
6004 }
6005 }
6006 if (looks_like_mouse_start)
6007 {
6008 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006009 if (mouse_index_found < 0)
6010 mouse_index_found = idx;
6011 }
6012 else
6013 {
6014 key_name[0] = termcodes[idx].name[0];
6015 key_name[1] = termcodes[idx].name[1];
6016 break;
6017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006019
6020 /*
6021 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006022 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006023 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006024 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
6025 * When there is a modifier the * matches a number.
6026 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006027 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006028 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006029 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006030 modslen = termcodes[idx].modlen;
6031 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006032 continue;
6033 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006034 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006035 {
6036 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006037
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006038 if (len <= modslen) // got a partial sequence
6039 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006040
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006041 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006042 // no modifiers
6043 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006044 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006045 // no match for "code;*X" with "code;"
6046 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01006047 else if (termcodes[idx].code[modslen] == '@'
Bram Moolenaar1573e732022-11-16 20:33:21 +00006048 && (tp[modslen] != '1'
6049 || tp[modslen + 1] != ';'))
Bram Moolenaar1410d182022-11-01 22:04:40 +00006050 // no match for "<Esc>[@" with "<Esc>[1;"
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01006051 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006052 else
6053 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006054 // Skip over the digits, the final char must
6055 // follow. URXVT can use a negative value, thus
6056 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006057 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02006058 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006059 ;
6060 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006061 if (len < j) // got a partial sequence
6062 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006063 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006064 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006065
Bram Moolenaara529ce02017-06-22 22:37:57 +02006066 modifiers_start = tp + slen - 2;
6067
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006068 // Match! Convert modifier bits.
6069 n = atoi((char *)modifiers_start);
6070 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006071
6072 slen = j;
6073 }
6074 key_name[0] = termcodes[idx].name[0];
6075 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006076 break;
6077 }
6078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01006080 if (idx == tc_len && mouse_index_found >= 0)
6081 {
6082 key_name[0] = termcodes[mouse_index_found].name[0];
6083 key_name[1] = termcodes[mouse_index_found].name[1];
6084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 }
6086
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02006087 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006088 // Mouse codes of DEC and pterm start with <ESC>[. When
6089 // detecting the start of these mouse codes they might as well be
6090 // another key code or terminal response.
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006091#ifdef FEAT_MOUSE_DEC
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006092 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006093#endif
6094#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006095 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006096#endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006097 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006099 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
6100
6101 /*
6102 * Check for responses from the terminal starting with {lead}:
Bram Moolenaar1a173402022-12-02 12:28:47 +00006103 * "<Esc>[" or CSI followed by [0-9>?].
6104 * Also for function keys without a modifier:
6105 * "<Esc>[" or CSI followed by [ABCDEFHPQRS].
Bram Moolenaar9584b312013-03-13 19:29:28 +01006106 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006107 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01006108 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006109 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01006110 *
Bram Moolenaarc255b782022-11-26 19:16:48 +00006111 * - Response to XTQMODKEYS: "{lead} > 4 ; Pv m".
6112 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006113 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006114 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01006115 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006116 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006117 * - window position reply: {lead}3;{x};{y}t
6118 *
6119 * - key with modifiers when modifyOtherKeys is enabled:
6120 * {lead}27;{modifier};{key}~
6121 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01006122 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006123 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar1a173402022-12-02 12:28:47 +00006124 || (tp[0] == CSI && len >= 2))
6125 && vim_strchr((char_u *)"0123456789>?ABCDEFHPQRS",
6126 *argp) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006128 int resp = handle_csi(tp, len, argp, offset, buf,
6129 bufsize, buflen, key_name, &slen);
6130 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02006131 {
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006132#ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006133 if (resp == -1)
6134 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar6f2a2272022-11-29 13:59:13 +00006135#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006136 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01006137 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006138 }
6139
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006140 // Check for fore/background color response from the terminal,
6141 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006142 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006143 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
6144 || tp[0] == OSC))
6145 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006146 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006147 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 }
6149
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006150 // Check for key code response from xterm,
6151 // starting with <Esc>P or DCS
Bram Moolenaar236dffa2022-11-18 21:20:25 +00006152 // It would only be needed with this condition:
6153 // (check_for_codes || rcs_status.tr_progress == STATUS_SENT)
6154 // Now this is always done so that DCS codes don't mess up things.
6155 else if ((tp[0] == ESC && len >= 2 && tp[1] == 'P') || tp[0] == DCS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02006157 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02006158 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 }
6160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161
6162 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006163 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006165 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166
Christopher Plewright36446bb2022-11-23 22:28:08 +00006167#if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 /*
Christopher Plewright36446bb2022-11-23 22:28:08 +00006169 * For scroll events from the GUI or MS-Windows console, fetch the
6170 * pointer coordinates so that we know which window to scroll later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 */
Christopher Plewright36446bb2022-11-23 22:28:08 +00006172 if (TRUE
6173# if defined(FEAT_GUI) && !defined(MSWIN)
6174 && gui.in_use
6175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 && key_name[0] == (int)KS_EXTRA
6177 && (key_name[1] == (int)KE_X1MOUSE
6178 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006179 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006180 || key_name[1] == (int)KE_MOUSELEFT
6181 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 || key_name[1] == (int)KE_MOUSEDOWN
6183 || key_name[1] == (int)KE_MOUSEUP))
6184 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006185 char_u bytes[6];
6186 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
6187
6188 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 return -1;
6190 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
6191 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
6192 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02006193 // equal to K_MOUSEMOVE
6194 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
6195 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 }
6197 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 /*
6200 * If it is a mouse click, get the coordinates.
6201 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006202 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006203#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02006204 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006205#endif
6206#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006207 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006208#endif
6209#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006210 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006211#endif
6212#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006213 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006214#endif
6215#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006216 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006217#endif
6218#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006219 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02006220#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02006221 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006222 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006224 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
6225 &modifiers) == -1)
6226 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228
6229#ifdef FEAT_GUI
6230 /*
6231 * If using the GUI, then we get menu and scrollbar events.
6232 *
6233 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
6234 * four bytes which are to be taken as a pointer to the vimmenu_T
6235 * structure.
6236 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00006237 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006238 * is one byte with the tab index.
6239 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
6241 * by one byte representing the scrollbar number, and then four bytes
6242 * representing a long_u which is the new value of the scrollbar.
6243 *
6244 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
6245 * KE_FILLER followed by four bytes representing a long_u which is the
6246 * new value of the scrollbar.
6247 */
6248# ifdef FEAT_MENU
6249 else if (key_name[0] == (int)KS_MENU)
6250 {
6251 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006252 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 if (num_bytes == -1)
6255 return -1;
6256 current_menu = (vimmenu_T *)val;
6257 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006258
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006259 // The menu may have been deleted right after it was used, check
6260 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00006261 if (check_menu_pointer(root_menu, current_menu) == FAIL)
6262 {
6263 key_name[0] = KS_EXTRA;
6264 key_name[1] = (int)KE_IGNORE;
6265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 }
6267# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006268# ifdef FEAT_GUI_TABLINE
6269 else if (key_name[0] == (int)KS_TABLINE)
6270 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006271 // Selecting tabline tab or using its menu.
6272 char_u bytes[6];
6273 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
6274
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006275 if (num_bytes == -1)
6276 return -1;
6277 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006278 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00006279 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006280 slen += num_bytes;
6281 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006282 else if (key_name[0] == (int)KS_TABMENU)
6283 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006284 // Selecting tabline tab or using its menu.
6285 char_u bytes[6];
6286 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
6287
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006288 if (num_bytes == -1)
6289 return -1;
6290 current_tab = (int)bytes[0];
6291 current_tabmenu = (int)bytes[1];
6292 slen += num_bytes;
6293 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006294# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295# ifndef USE_ON_FLY_SCROLL
6296 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
6297 {
6298 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006299 char_u bytes[6];
6300 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006302 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 j = 0;
6304 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
6305 && tp[j + 2] != NUL; ++i)
6306 {
6307 j += 3;
6308 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
6309 if (num_bytes == -1)
6310 break;
6311 if (i == 0)
6312 current_scrollbar = (int)bytes[0];
6313 else if (current_scrollbar != (int)bytes[0])
6314 break;
6315 j += num_bytes;
6316 num_bytes = get_long_from_buf(tp + j, &val);
6317 if (num_bytes == -1)
6318 break;
6319 scrollbar_value = val;
6320 j += num_bytes;
6321 slen = j;
6322 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006323 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 return -1;
6325 }
6326 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
6327 {
6328 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02006329 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006331 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 j = 0;
6333 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
6334 && tp[j + 2] != NUL; ++i)
6335 {
6336 j += 3;
6337 num_bytes = get_long_from_buf(tp + j, &val);
6338 if (num_bytes == -1)
6339 break;
6340 scrollbar_value = val;
6341 j += num_bytes;
6342 slen = j;
6343 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006344 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 return -1;
6346 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006347# endif // !USE_ON_FLY_SCROLL
6348#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006350#if (defined(UNIX) || defined(VMS))
6351 /*
6352 * Handle FocusIn/FocusOut event sequences reported by XTerm.
6353 * (CSI I/CSI O)
6354 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00006355 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006356# ifdef FEAT_GUI
6357 && !gui.in_use
6358# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006359 )
6360 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006361 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006362 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006363 if (!focus_state)
6364 {
6365 ui_focus_change(TRUE);
6366 did_cursorhold = TRUE;
6367 focus_state = TRUE;
6368 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006369 key_name[1] = (int)KE_IGNORE;
6370 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01006371 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006372 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01006373 if (focus_state)
6374 {
6375 ui_focus_change(FALSE);
6376 did_cursorhold = TRUE;
6377 focus_state = FALSE;
6378 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006379 key_name[1] = (int)KE_IGNORE;
6380 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01006381 }
6382#endif
6383
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006384 /*
6385 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
6386 */
6387 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
6388
6389 /*
6390 * Add any modifier codes to our string.
6391 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006392 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006393
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006394 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006395 key_name[0] = KEY2TERMCAP0(key);
6396 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00006398 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006399 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00006400 if (has_mbyte)
6401 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
6402 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00006403 string[new_slen++] = key_name[1];
6404 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006405 else if (new_slen == 0 && key_name[0] == KS_EXTRA
6406 && key_name[1] == KE_IGNORE)
6407 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006408 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
6409 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01006410 retval = KEYLEN_REMOVED;
6411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 else
6413 {
6414 string[new_slen++] = K_SPECIAL;
6415 string[new_slen++] = key_name[0];
6416 string[new_slen++] = key_name[1];
6417 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02006418 if (put_string_in_typebuf(offset, slen, string, new_slen,
6419 buf, bufsize, buflen) == FAIL)
6420 return -1;
6421 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 }
6423
Bram Moolenaar2951b772013-07-03 12:45:31 +02006424#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02006425 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02006426#endif
6427
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006428 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429}
6430
Bram Moolenaar9377df32017-10-15 13:22:01 +02006431#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006432/*
6433 * Get the text foreground color, if known.
6434 */
6435 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006436term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006437{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006438 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006439 {
6440 *r = fg_r;
6441 *g = fg_g;
6442 *b = fg_b;
6443 }
6444}
6445
6446/*
6447 * Get the text background color, if known.
6448 */
6449 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02006450term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006451{
Bram Moolenaarafd78262019-05-10 23:10:31 +02006452 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02006453 {
6454 *r = bg_r;
6455 *g = bg_g;
6456 *b = bg_b;
6457 }
6458}
6459#endif
6460
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461/*
6462 * Replace any terminal code strings in from[] with the equivalent internal
6463 * vim representation. This is used for the "from" and "to" part of a
6464 * mapping, and the "to" part of a menu command.
6465 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
6466 * '<'.
6467 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
6468 *
6469 * The replacement is done in result[] and finally copied into allocated
6470 * memory. If this all works well *bufp is set to the allocated memory and a
6471 * pointer to it is returned. If something fails *bufp is set to NULL and from
6472 * is returned.
6473 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02006474 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
6475 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
6476 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
6477 * used instead of a CTRL-V.
6478 *
6479 * Flags:
6480 * REPTERM_FROM_PART see above
6481 * REPTERM_DO_LT also translate <lt>
6482 * REPTERM_SPECIAL always accept <key> notation
6483 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
6484 *
6485 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
6486 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00006488 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006489replace_termcodes(
6490 char_u *from,
6491 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02006492 int flags,
6493 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494{
6495 int i;
6496 int slen;
6497 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01006498 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006500 int do_backslash; // backslash is a special character
6501 int do_special; // recognize <> key codes
6502 int do_key_code; // recognize raw key codes
6503 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01006504 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
6506 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006507 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6508 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006510 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
6512 /*
6513 * Allocate space for the translation. Worst case a single character is
6514 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006515 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006517 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006518 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
6520 *bufp = NULL;
6521 return from;
6522 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006523 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524
6525 /*
6526 * Check for #n at start only: function key n
6527 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006528 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 {
6530 result[dlen++] = K_SPECIAL;
6531 result[dlen++] = 'k';
6532 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006533 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006535 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 src += 2;
6537 }
6538
6539 /*
6540 * Copy each byte from *from to result[dlen]
6541 */
6542 while (*src != NUL)
6543 {
6544 /*
6545 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006546 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006548 if (do_special && ((flags & REPTERM_DO_LT)
6549 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 {
6551#ifdef FEAT_EVAL
6552 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006553 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6554 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006556 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6557 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 */
6559 if (STRNICMP(src, "<SID>", 5) == 0)
6560 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006561 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006562 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 else
6564 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006565 char_u *dot;
6566 long sid = current_sctx.sc_sid;
6567
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006569 if (in_vim9script()
6570 && (dot = vim_strchr(src, '.')) != NULL)
6571 {
6572 imported_T *imp = find_imported(src, dot - src, FALSE);
6573
6574 if (imp != NULL)
6575 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006576 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6577 size_t len;
6578
Bram Moolenaar89445512022-04-14 12:58:23 +01006579 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006580 if (si->sn_autoload_prefix != NULL)
6581 {
6582 // Turn "<SID>name.Func"
6583 // into "scriptname#Func".
6584 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006585 if (ga_grow(&ga,
6586 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006587 {
6588 ga_clear(&ga);
6589 *bufp = NULL;
6590 return from;
6591 }
6592 result = ga.ga_data;
6593 STRCPY(result + dlen, si->sn_autoload_prefix);
6594 dlen += len;
6595 continue;
6596 }
6597 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006598 }
6599 }
6600
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 result[dlen++] = K_SPECIAL;
6602 result[dlen++] = (int)KS_EXTRA;
6603 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006604 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006605 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 result[dlen++] = '_';
6607 continue;
6608 }
6609 }
6610#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006611 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6612 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006613 TRUE, did_simplify);
Bram Moolenaar1a173402022-12-02 12:28:47 +00006614 if (slen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 {
6616 dlen += slen;
6617 continue;
6618 }
6619 }
6620
6621 /*
6622 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6623 * Note that this is also checked after replacing the <> form.
6624 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6625 * it could be a character in the file.
6626 */
6627 if (do_key_code)
6628 {
6629 i = find_term_bykeys(src);
6630 if (i >= 0)
6631 {
6632 result[dlen++] = K_SPECIAL;
6633 result[dlen++] = termcodes[i].name[0];
6634 result[dlen++] = termcodes[i].name[1];
6635 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006636 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 continue;
6638 }
6639 }
6640
6641#ifdef FEAT_EVAL
6642 if (do_special)
6643 {
6644 char_u *p, *s, len;
6645
6646 /*
6647 * Replace <Leader> by the value of "mapleader".
6648 * Replace <LocalLeader> by the value of "maplocalleader".
6649 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6650 */
6651 if (STRNICMP(src, "<Leader>", 8) == 0)
6652 {
6653 len = 8;
6654 p = get_var_value((char_u *)"g:mapleader");
6655 }
6656 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6657 {
6658 len = 13;
6659 p = get_var_value((char_u *)"g:maplocalleader");
6660 }
6661 else
6662 {
6663 len = 0;
6664 p = NULL;
6665 }
6666 if (len != 0)
6667 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006668 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6670 s = (char_u *)"\\";
6671 else
6672 s = p;
6673 while (*s != NUL)
6674 result[dlen++] = *s++;
6675 src += len;
6676 continue;
6677 }
6678 }
6679#endif
6680
6681 /*
6682 * Remove CTRL-V and ignore the next character.
6683 * For "from" side the CTRL-V at the end is included, for the "to"
6684 * part it is removed.
6685 * If 'cpoptions' does not contain 'B', also accept a backslash.
6686 */
6687 key = *src;
6688 if (key == Ctrl_V || (do_backslash && key == '\\'))
6689 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006690 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 if (*src == NUL)
6692 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006693 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 result[dlen++] = key;
6695 break;
6696 }
6697 }
6698
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006699 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006700 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 {
6702 /*
6703 * If the character is K_SPECIAL, replace it with K_SPECIAL
6704 * KS_SPECIAL KE_FILLER.
6705 * If compiled with the GUI replace CSI with K_CSI.
6706 */
6707 if (*src == K_SPECIAL)
6708 {
6709 result[dlen++] = K_SPECIAL;
6710 result[dlen++] = KS_SPECIAL;
6711 result[dlen++] = KE_FILLER;
6712 }
6713# ifdef FEAT_GUI
6714 else if (*src == CSI)
6715 {
6716 result[dlen++] = K_SPECIAL;
6717 result[dlen++] = KS_EXTRA;
6718 result[dlen++] = (int)KE_CSI;
6719 }
6720# endif
6721 else
6722 result[dlen++] = *src;
6723 ++src;
6724 }
6725 }
6726 result[dlen] = NUL;
6727
6728 /*
6729 * Copy the new string to allocated memory.
6730 * If this fails, just return from.
6731 */
6732 if ((*bufp = vim_strsave(result)) != NULL)
6733 from = *bufp;
6734 vim_free(result);
6735 return from;
6736}
6737
6738/*
6739 * Find a termcode with keys 'src' (must be NUL terminated).
6740 * Return the index in termcodes[], or -1 if not found.
6741 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006742 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006743find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
6745 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006746 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747
6748 for (i = 0; i < tc_len; ++i)
6749 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006750 if (slen == termcodes[i].len
6751 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 return i;
6753 }
6754 return -1;
6755}
6756
6757/*
6758 * Gather the first characters in the terminal key codes into a string.
6759 * Used to speed up check_termcode().
6760 */
6761 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006762gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763{
6764 int i;
6765 int len = 0;
6766
6767#ifdef FEAT_GUI
6768 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006769 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770#endif
6771#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006772 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006773 termleader[len++] = DCS; // the termcode response starts with DCS
6774 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775#endif
6776 termleader[len] = NUL;
6777
6778 for (i = 0; i < tc_len; ++i)
6779 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6780 {
6781 termleader[len++] = termcodes[i].code[0];
6782 termleader[len] = NUL;
6783 }
6784
6785 need_gather = FALSE;
6786}
6787
6788/*
6789 * Show all termcodes (for ":set termcap")
6790 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006791 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 */
6793 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006794show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795{
6796 int col;
6797 int *items;
6798 int item_count;
6799 int run;
6800 int row, rows;
6801 int cols;
6802 int i;
6803 int len;
6804
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006805#define INC3 27 // try to make three columns
6806#define INC2 40 // try to make two columns
6807#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006809 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006811 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (items == NULL)
6813 return;
6814
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006815 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006816 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
6818 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006819 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006821 * 2. display the medium items (medium length strings)
6822 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006823 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006825 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 {
6827 /*
6828 * collect the items in items[]
6829 */
6830 item_count = 0;
6831 for (i = 0; i < tc_len; i++)
6832 {
6833 len = show_one_termcode(termcodes[i].name,
6834 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006835 if ((flags & OPT_ONECOLUMN) ||
6836 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006837 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006838 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 items[item_count++] = i;
6840 }
6841
6842 /*
6843 * display the items
6844 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006845 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006847 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 if (cols == 0)
6849 cols = 1;
6850 rows = (item_count + cols - 1) / cols;
6851 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006852 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 rows = item_count;
6854 for (row = 0; row < rows && !got_int; ++row)
6855 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006856 msg_putchar('\n'); // go to next line
6857 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 break;
6859 col = 0;
6860 for (i = row; i < item_count; i += rows)
6861 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006862 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 show_one_termcode(termcodes[items[i]].name,
6864 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006865 if (run == 2)
6866 col += INC2;
6867 else
6868 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 }
6870 out_flush();
6871 ui_breakcheck();
6872 }
6873 }
6874 vim_free(items);
6875}
6876
6877/*
6878 * Show one termcode entry.
6879 * Output goes into IObuff[]
6880 */
6881 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006882show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883{
6884 char_u *p;
6885 int len;
6886
6887 if (name[0] > '~')
6888 {
6889 IObuff[0] = ' ';
6890 IObuff[1] = ' ';
6891 IObuff[2] = ' ';
6892 IObuff[3] = ' ';
6893 }
6894 else
6895 {
6896 IObuff[0] = 't';
6897 IObuff[1] = '_';
6898 IObuff[2] = name[0];
6899 IObuff[3] = name[1];
6900 }
6901 IObuff[4] = ' ';
6902
6903 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6904 if (p[1] != 't')
6905 STRCPY(IObuff + 5, p);
6906 else
6907 IObuff[5] = NUL;
6908 len = (int)STRLEN(IObuff);
6909 do
6910 IObuff[len++] = ' ';
6911 while (len < 17);
6912 IObuff[len] = NUL;
6913 if (code == NULL)
6914 len += 4;
6915 else
6916 len += vim_strsize(code);
6917
6918 if (printit)
6919 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006920 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006922 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 else
6924 msg_outtrans(code);
6925 }
6926 return len;
6927}
6928
6929#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6930/*
6931 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6932 * termcap codes from the terminal itself.
6933 * We get them one by one to avoid a very long response string.
6934 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006935static int xt_index_in = 0;
6936static int xt_index_out = 0;
6937
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006939req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940{
6941 xt_index_out = 0;
6942 xt_index_in = 0;
6943 req_more_codes_from_term();
6944}
6945
6946 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006947req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006949 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 int old_idx = xt_index_out;
6951
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006952 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 if (exiting)
6954 return;
6955
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006956 // Send up to 10 more requests out than we received. Avoid sending too
6957 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6959 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006960 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006961
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006962 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006963 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6964 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 out_str_nf((char_u *)buf);
6966 ++xt_index_out;
6967 }
6968
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006969 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 if (xt_index_out != old_idx)
6971 out_flush();
6972}
6973
6974/*
6975 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6976 * A "0" instead of the "1" indicates a code that isn't supported.
6977 * Both <name> and <string> are encoded in hex.
6978 * "code" points to the "0" or "1".
6979 */
6980 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006981got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982{
6983#define XT_LEN 100
6984 char_u name[3];
6985 char_u str[XT_LEN];
6986 int i;
6987 int j = 0;
6988 int c;
6989
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006990 // A '1' means the code is supported, a '0' means it isn't.
6991 // When half the length is > XT_LEN we can't use it.
6992 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6994 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006995 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 name[0] = hexhex2nr(code + 3);
6997 name[1] = hexhex2nr(code + 5);
6998 name[2] = NUL;
6999 for (i = 0; key_names[i] != NULL; ++i)
7000 {
7001 if (STRCMP(key_names[i], name) == 0)
7002 {
7003 xt_index_in = i;
7004 break;
7005 }
7006 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02007007
Bram Moolenaarb255b902018-04-24 21:40:10 +02007008 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
7009
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 if (key_names[i] != NULL)
7011 {
7012 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
7013 str[j++] = c;
7014 str[j] = NUL;
7015 if (name[0] == 'C' && name[1] == 'o')
7016 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007017 // Color count is not a key code.
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007018 int val = atoi((char *)str);
7019#if defined(FEAT_EVAL)
7020 if (val == t_colors)
7021 ch_log(NULL, "got_code_from_term(Co): no change (%d)", val);
7022 else
7023 ch_log(NULL,
7024 "got_code_from_term(Co): changed from %d to %d",
7025 t_colors, val);
7026#endif
7027 may_adjust_color_count(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 }
7029 else
7030 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 i = find_term_bykeys(str);
Bram Moolenaar8d754fa2022-12-17 13:49:16 +00007032 if (i >= 0 && name[0] == termcodes[i].name[0]
7033 && name[1] == termcodes[i].name[1])
7034 {
7035 // Existing entry with the same name and code - skip.
7036#ifdef FEAT_EVAL
7037 ch_log(NULL, "got_code_from_term(): Entry %c%c did not change",
7038 name[0], name[1]);
7039#endif
7040 }
7041 else
7042 {
7043 if (i >= 0)
7044 {
7045 // Delete an existing entry using the same code.
7046#ifdef FEAT_EVAL
7047 ch_log(NULL, "got_code_from_term(): Deleting entry %c%c with matching keys %s",
7048 termcodes[i].name[0], termcodes[i].name[1], str);
7049#endif
7050 del_termcode_idx(i);
7051 }
7052#ifdef FEAT_EVAL
7053 else
7054 ch_log(NULL, "got_code_from_term(): Adding entry %c%c with keys %s",
7055 name[0], name[1], str);
7056#endif
7057 add_termcode(name, str, ATC_FROM_TERM);
7058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 }
7060 }
7061 }
7062
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007063 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 ++xt_index_in;
7065 req_more_codes_from_term();
7066}
7067
7068/*
7069 * Check if there are any unanswered requests and deal with them.
7070 * This is called before starting an external program or getting direct
7071 * keyboard input. We don't want responses to be send to that program or
7072 * handled as typed text.
7073 */
7074 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007075check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
7077 int c;
7078
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007079 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 if (xt_index_out == 0 || xt_index_out == xt_index_in)
7081 return;
7082
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007083 // Vgetc() will check for and handle any response.
7084 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 ++no_mapping;
7086 ++allow_keys;
7087 for (;;)
7088 {
7089 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007090 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 break;
7092
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007093 // If a response is recognized it's replaced with K_IGNORE, must read
7094 // it from the input stream. If there is no K_IGNORE we can't do
7095 // anything, break here (there might be some responses further on, but
7096 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 if (c != K_SPECIAL && c != K_IGNORE)
7098 break;
7099 c = vgetc();
7100 if (c != K_IGNORE)
7101 {
7102 vungetc(c);
7103 break;
7104 }
7105 }
7106 --no_mapping;
7107 --allow_keys;
7108}
7109#endif
7110
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007111#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112static char ksme_str[20];
7113static char ksmr_str[20];
7114static char ksmd_str[20];
7115
7116/*
7117 * For Win32 console: update termcap codes for existing console attributes.
7118 */
7119 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007120update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
Bram Moolenaar424bcae2022-01-31 14:59:41 +00007122 sprintf(ksme_str, "\033|%dm", attr);
7123 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
7124 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
Bram Moolenaar4654d632022-11-17 22:05:12 +00007126 tcap_entry_T *p = find_builtin_term(DEFAULT_TERM);
7127 if (p == NULL) // did not find it
7128 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 while (p->bt_string != NULL)
7130 {
7131 if (p->bt_entry == (int)KS_ME)
7132 p->bt_string = &ksme_str[0];
7133 else if (p->bt_entry == (int)KS_MR)
7134 p->bt_string = &ksmr_str[0];
7135 else if (p->bt_entry == (int)KS_MD)
7136 p->bt_string = &ksmd_str[0];
7137 ++p;
7138 }
7139}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007140
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007141# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007142# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007143
7144typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007145{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007146 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
7147 CMODE_RGB, // Use 24bit RGB colors using VTP.
7148 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
7149 CMODE_LAST,
7150} cmode_T;
7151
7152struct ks_tbl_S
7153{
7154 int code; // value of KS_
7155 char *vtp; // code in RGB mode
7156 char *vtp2; // code in 256color mode
7157 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007158};
7159
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007160static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007161{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007162 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
7163 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
7164 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
7165 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
7166 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
7167 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
7168 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
7169 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
7170 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007171# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007172 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
7173 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
7174 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
7175 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007176# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007177 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
7178 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
7179 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
7180 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007181# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007182 {(int)KS_CCO, "256", "256", {""}}, // colors
7183 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007184};
7185
Bram Moolenaar4654d632022-11-17 22:05:12 +00007186/*
7187 * Find the first entry for "code" in the builtin termcap for "name".
7188 * Returns NULL when not found.
7189 */
7190 static tcap_entry_T *
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007191find_first_tcap(
7192 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007193 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007194{
Bram Moolenaar4654d632022-11-17 22:05:12 +00007195 tcap_entry_T *p = find_builtin_term(name);
7196 if (p != NULL)
7197 {
7198 while (p->bt_string != NULL)
7199 {
7200 if (p->bt_entry == code)
7201 return p;
7202 ++p;
7203 }
7204 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007205 return NULL;
7206}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007207# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007208
7209/*
7210 * For Win32 console: replace the sequence immediately after termguicolors.
7211 */
7212 void
7213swap_tcap(void)
7214{
7215# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007216 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007217 static cmode_T curr_mode;
7218 struct ks_tbl_S *ks;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007219 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007220
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007221 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007222 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007223 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007224 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007225 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007226 if (bt != NULL)
7227 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007228 // Preserve the original value.
7229 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
7230 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
7231 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007232
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007233 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007234 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007235 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01007236 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007237 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007238 }
7239
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007240 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007241 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007242 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007243 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007244 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007245 mode = CMODE_INDEXED;
7246
7247 if (mode == curr_mode)
7248 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007249
7250 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007251 {
Bram Moolenaar4654d632022-11-17 22:05:12 +00007252 tcap_entry_T *bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007253 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007254 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007255 }
7256
LemonBoy5a7b6dc2022-05-06 18:38:41 +01007257 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007258# endif
7259}
7260
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02007262
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007263
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007264#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007265 || defined(PROTO)
7266static int cube_value[] = {
7267 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
7268};
7269
7270static int grey_ramp[] = {
7271 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
7272 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
7273};
7274
LemonBoyb2b3acb2022-05-20 10:10:34 +01007275static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01007276// R G B
7277 { 0, 0, 0}, // black
7278 {224, 0, 0}, // dark red
7279 { 0, 224, 0}, // dark green
7280 {224, 224, 0}, // dark yellow / brown
7281 { 0, 0, 224}, // dark blue
7282 {224, 0, 224}, // dark magenta
7283 { 0, 224, 224}, // dark cyan
7284 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007285
LemonBoyd2a46622022-05-01 17:43:33 +01007286 {128, 128, 128}, // dark grey
7287 {255, 64, 64}, // light red
7288 { 64, 255, 64}, // light green
7289 {255, 255, 64}, // yellow
7290 { 64, 64, 255}, // light blue
7291 {255, 64, 255}, // light magenta
7292 { 64, 255, 255}, // light cyan
7293 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007294};
7295
LemonBoyd2a46622022-05-01 17:43:33 +01007296#if defined(MSWIN)
7297// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
7298static const char_u cterm_ansi_idx[] = {
7299 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
7300};
7301#endif
7302
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007303#define ANSI_INDEX_NONE 0
7304
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007305 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01007306ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
7307{
7308 if (nr < 16)
7309 {
7310 *r = ansi_table[nr][0];
7311 *g = ansi_table[nr][1];
7312 *b = ansi_table[nr][2];
7313 *ansi_idx = nr;
7314 }
7315 else
7316 {
7317 *r = 0;
7318 *g = 0;
7319 *b = 0;
7320 *ansi_idx = ANSI_INDEX_NONE;
7321 }
7322}
7323
7324 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02007325cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007326{
7327 int idx;
7328
7329 if (nr < 16)
7330 {
LemonBoyd2a46622022-05-01 17:43:33 +01007331#if defined(MSWIN)
7332 idx = cterm_ansi_idx[nr];
7333#else
7334 idx = nr;
7335#endif
7336 *r = ansi_table[idx][0];
7337 *g = ansi_table[idx][1];
7338 *b = ansi_table[idx][2];
7339 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007340 }
7341 else if (nr < 232)
7342 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007343 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007344 idx = nr - 16;
7345 *r = cube_value[idx / 36 % 6];
7346 *g = cube_value[idx / 6 % 6];
7347 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007348 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007349 }
7350 else if (nr < 256)
7351 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007352 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007353 idx = nr - 232;
7354 *r = grey_ramp[idx];
7355 *g = grey_ramp[idx];
7356 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007357 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007358 }
7359 else
7360 {
7361 *r = 0;
7362 *g = 0;
7363 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02007364 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02007365 }
7366}
7367#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007368
Bram Moolenaarf4140482020-02-15 23:06:45 +01007369/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007370 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007371 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007372 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01007373 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007374 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007375term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007376{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007377 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007378 int i;
7379 int c;
7380
7381 for (i = ta_len; i < ta_len + len; ++i)
7382 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007383 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
7384 {
7385 int modifiers = ta_buf[i + 2];
7386 int key = ta_buf[i + 3];
7387
7388 // Try to use the modifier to modify the key. In any case drop the
7389 // modifier.
7390 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
7391 len -= 3;
7392 if (key < 0x80)
7393 key = merge_modifyOtherKeys(key, &modifiers);
7394 ta_buf[i] = key;
7395 }
7396 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01007397 {
7398 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
7399 if (c == K_DEL || c == K_KDEL || c == K_BS)
7400 {
7401 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01007402 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01007403 if (c == K_DEL || c == K_KDEL)
7404 ta_buf[i] = DEL;
7405 else
7406 ta_buf[i] = Ctrl_H;
7407 len -= 2;
7408 }
7409 }
7410 else if (ta_buf[i] == '\r')
7411 ta_buf[i] = '\n';
7412 if (has_mbyte)
7413 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
7414 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01007415 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01007416}