blob: 77ad08bf183db4549c16431636f621cb97381614 [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 Moolenaar071d4272004-06-13 20:20:40 +000053struct builtin_term
54{
55 int bt_entry;
56 char *bt_string;
57};
58
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010059// start of keys that are not directly used by Vim but can be mapped
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#define BT_EXTRA_KEYS 0x101
61
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010063static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010065static void req_codes_from_term(void);
66static void req_more_codes_from_term(void);
67static void got_code_from_term(char_u *code, int len);
68static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010070static void del_termcode_idx(int idx);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020071static int find_term_bykeys(char_u *src);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010072static int term_is_builtin(char_u *name);
73static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074
75#ifdef HAVE_TGETENT
Bram Moolenaar3efd65c2022-06-19 17:45:28 +010076static char *invoke_tgetent(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077
78/*
79 * Here is our own prototype for tgetstr(), any prototypes from the include
80 * files have been disabled by the define at the start of this file.
81 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010082char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
84# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010085 // Change this to "if 1" to debug what happens with termresponse.
Bram Moolenaar2951b772013-07-03 12:45:31 +020086# if 0
87# define DEBUG_TERMRESPONSE
Bram Moolenaar952d9d82021-08-02 18:07:18 +020088static void log_tr(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Bram Moolenaarb255b902018-04-24 21:40:10 +020089# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +020090# else
Bram Moolenaarb255b902018-04-24 21:40:10 +020091# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +020092# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +020093
Bram Moolenaarafd78262019-05-10 23:10:31 +020094typedef enum {
95 STATUS_GET, // send request when switching to RAW mode
96 STATUS_SENT, // did send request, checking for response
97 STATUS_GOT, // received response
98 STATUS_FAIL // timed out
99} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200100
Bram Moolenaarafd78262019-05-10 23:10:31 +0200101typedef struct {
102 request_progress_T tr_progress;
103 time_t tr_start; // when request was sent, -1 for never
104} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200105
Bram Moolenaarafd78262019-05-10 23:10:31 +0200106# define TERMREQUEST_INIT {STATUS_GET, -1}
107
108// Request Terminal Version status:
109static termrequest_T crv_status = TERMREQUEST_INIT;
110
111// Request Cursor position report:
112static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200113
Bram Moolenaara45551a2020-06-09 15:57:37 +0200114// Request xterm compatibility check:
115static termrequest_T xcc_status = TERMREQUEST_INIT;
116
Bram Moolenaar9377df32017-10-15 13:22:01 +0200117# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200118// Request foreground color report:
119static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200120static int fg_r = 0;
121static int fg_g = 0;
122static int fg_b = 0;
123static int bg_r = 255;
124static int bg_g = 255;
125static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200126# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100128// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200129static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200130
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100131// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200132static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200133
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100134// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200135static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100136
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100137// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200138static termrequest_T winpos_status = TERMREQUEST_INIT;
139
140static termrequest_T *all_termrequests[] = {
141 &crv_status,
142 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200143 &xcc_status,
Bram Moolenaarafd78262019-05-10 23:10:31 +0200144# ifdef FEAT_TERMINAL
145 &rfg_status,
146# endif
147 &rbg_status,
148 &rbm_status,
149 &rcs_status,
150 &winpos_status,
151 NULL
152};
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100153
154// The t_8u code may default to a value but get reset when the term response is
155// received. To avoid redrawing too often, only redraw when t_8u is not reset
Bram Moolenaarb3932752022-10-01 21:22:17 +0100156// and it was supposed to be written. Unless t_8u was set explicitly.
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100157// FALSE -> don't output t_8u yet
158// MAYBE -> tried outputing t_8u while FALSE
159// OK -> can write t_8u
160int write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161# endif
162
163/*
164 * Don't declare these variables if termcap.h contains them.
165 * Autoconf checks if these variables should be declared extern (not all
166 * systems have them).
167 * Some versions define ospeed to be speed_t, but that is incompatible with
168 * BSD, where ospeed is short and speed_t is long.
169 */
170# ifndef HAVE_OSPEED
171# ifdef OSPEED_EXTERN
172extern short ospeed;
173# else
174short ospeed;
175# endif
176# endif
177# ifndef HAVE_UP_BC_PC
178# ifdef UP_BC_PC_EXTERN
179extern char *UP, *BC, PC;
180# else
181char *UP, *BC, PC;
182# endif
183# endif
184
185# define TGETSTR(s, p) vim_tgetstr((s), (p))
186# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100187static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100188#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100190static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100192#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000193static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100194#endif
195
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200196#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100197// When the cursor shape was detected these values are used:
198// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200199static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200200
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100201// The blink flag from the style response may be inverted from the actual
202// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200203static int initial_cursor_shape_blink = FALSE;
204
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100205// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200206static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200207#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200208
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100209/*
210 * Here are the builtin termcap entries. They are not stored as complete
211 * structures with all entries to save space.
212 *
213 * The entries are also included even when HAVE_TGETENT is defined, the systerm
214 * termcap may be incomplee. When HAVE_TGETENT is defined, the builtin entries
215 * can be accessed with "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
216 *
217 * Each termcap is a list of builtin_term structures. It always starts with
218 * KS_NAME, which separates the entries. See parse_builtin_tcap() for all
219 * details.
220 * bt_entry is either a KS_xxx code (>= 0), or a K_xxx code.
221 *
222 * Entries marked with "guessed" may be wrong.
223 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000224static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#if defined(FEAT_GUI)
227/*
228 * GUI pseudo term-cap.
229 */
230 {(int)KS_NAME, "gui"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000231 {(int)KS_CE, "\033|$"},
232 {(int)KS_AL, "\033|i"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000234 {(int)KS_CAL, "\033|%p1%dI"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000236 {(int)KS_CAL, "\033|%dI"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000238 {(int)KS_DL, "\033|d"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000240 {(int)KS_CDL, "\033|%p1%dD"},
241 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
242 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000244 {(int)KS_CDL, "\033|%dD"},
245 {(int)KS_CS, "\033|%d;%dR"},
246 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000248 {(int)KS_CL, "\033|C"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100249 // attributes switched on with 'h', off with * 'H'
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000250 {(int)KS_ME, "\033|31H"}, // HL_ALL
251 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
252 {(int)KS_MD, "\033|2h"}, // HL_BOLD
253 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
254 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
255 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
256 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
257 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
258 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
259 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
260 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
261 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
262 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
263 {(int)KS_VB, "\033|f"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 {(int)KS_MS, "y"},
265 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100266 {(int)KS_XN, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100267 {(int)KS_LE, "\b"}, // cursor-left = BS
268 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000270 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000272 {(int)KS_CM, "\033|%d;%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100274 // there are no key sequences here, the GUI sequences are recognized
275 // in check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276#endif
277
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278/*
279 * Amiga console window, default for Amiga
280 */
281 {(int)KS_NAME, "amiga"},
282 {(int)KS_CE, "\033[K"},
283 {(int)KS_CD, "\033[J"},
284 {(int)KS_AL, "\033[L"},
285# ifdef TERMINFO
286 {(int)KS_CAL, "\033[%p1%dL"},
287# else
288 {(int)KS_CAL, "\033[%dL"},
289# endif
290 {(int)KS_DL, "\033[M"},
291# ifdef TERMINFO
292 {(int)KS_CDL, "\033[%p1%dM"},
293# else
294 {(int)KS_CDL, "\033[%dM"},
295# endif
296 {(int)KS_CL, "\014"},
297 {(int)KS_VI, "\033[0 p"},
298 {(int)KS_VE, "\033[1 p"},
299 {(int)KS_ME, "\033[0m"},
300 {(int)KS_MR, "\033[7m"},
301 {(int)KS_MD, "\033[1m"},
302 {(int)KS_SE, "\033[0m"},
303 {(int)KS_SO, "\033[33m"},
304 {(int)KS_US, "\033[4m"},
305 {(int)KS_UE, "\033[0m"},
306 {(int)KS_CZH, "\033[3m"},
307 {(int)KS_CZR, "\033[0m"},
Bram Moolenaar2d718262020-11-21 12:44:56 +0100308#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100309 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100311 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
312 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100314 {(int)KS_CAB, "\033[4%dm"}, // set background color
315 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100317 {(int)KS_OP, "\033[m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#endif
319 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100320 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 {(int)KS_LE, "\b"},
322# ifdef TERMINFO
323 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
324# else
325 {(int)KS_CM, "\033[%i%d;%dH"},
326# endif
327#if defined(__MORPHOS__)
328 {(int)KS_SR, "\033M"},
329#endif
330# ifdef TERMINFO
331 {(int)KS_CRI, "\033[%p1%dC"},
332# else
333 {(int)KS_CRI, "\033[%dC"},
334# endif
335 {K_UP, "\233A"},
336 {K_DOWN, "\233B"},
337 {K_LEFT, "\233D"},
338 {K_RIGHT, "\233C"},
339 {K_S_UP, "\233T"},
340 {K_S_DOWN, "\233S"},
341 {K_S_LEFT, "\233 A"},
342 {K_S_RIGHT, "\233 @"},
343 {K_S_TAB, "\233Z"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100344 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {K_F2, "\233\061~"},
346 {K_F3, "\233\062~"},
347 {K_F4, "\233\063~"},
348 {K_F5, "\233\064~"},
349 {K_F6, "\233\065~"},
350 {K_F7, "\233\066~"},
351 {K_F8, "\233\067~"},
352 {K_F9, "\233\070~"},
353 {K_F10, "\233\071~"},
354 {K_S_F1, "\233\061\060~"},
355 {K_S_F2, "\233\061\061~"},
356 {K_S_F3, "\233\061\062~"},
357 {K_S_F4, "\233\061\063~"},
358 {K_S_F5, "\233\061\064~"},
359 {K_S_F6, "\233\061\065~"},
360 {K_S_F7, "\233\061\066~"},
361 {K_S_F8, "\233\061\067~"},
362 {K_S_F9, "\233\061\070~"},
363 {K_S_F10, "\233\061\071~"},
364 {K_HELP, "\233?~"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100365 {K_INS, "\233\064\060~"}, // 101 key keyboard
366 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
367 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
368 {K_HOME, "\233\064\064~"}, // 101 key keyboard
369 {K_END, "\233\064\065~"}, // 101 key keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370
371 {BT_EXTRA_KEYS, ""},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100372 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
373 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
374 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376/*
377 * standard ANSI terminal, default for unix
378 */
379 {(int)KS_NAME, "ansi"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000380 {(int)KS_CE, "\033[K"},
381 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000383 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000385 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000387 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000389 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000391 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000393 {(int)KS_CL, "\033[H\033[2J"},
394 {(int)KS_ME, "\033[0m"},
395 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100397 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {(int)KS_LE, "\b"},
399# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000400 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000402 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403# endif
404# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000405 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000407 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410/*
411 * These codes are valid when nansi.sys or equivalent has been installed.
412 * Function keys on a PC are preceded with a NUL. These are converted into
413 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
414 * CTRL-arrow is used instead of SHIFT-arrow.
415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 {(int)KS_NAME, "pcansi"},
417 {(int)KS_DL, "\033[M"},
418 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {(int)KS_CE, "\033[K"},
420 {(int)KS_CL, "\033[2J"},
421 {(int)KS_ME, "\033[0m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100422 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
423 {(int)KS_MD, "\033[1m"}, // bold: white text
424 {(int)KS_SE, "\033[0m"}, // standout end
425 {(int)KS_SO, "\033[31m"}, // standout: white on blue
426 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
427 {(int)KS_CZR, "\033[0m"}, // italic mode end
428 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
429 {(int)KS_UE, "\033[0m"}, // underscore mode end
430 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100432 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
433 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100435 {(int)KS_CAB, "\033[4%dm"}, // set background color
436 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100438 {(int)KS_OP, "\033[0m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100440 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 {(int)KS_LE, "\b"},
442# ifdef TERMINFO
443 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
444# else
445 {(int)KS_CM, "\033[%i%d;%dH"},
446# endif
447# ifdef TERMINFO
448 {(int)KS_CRI, "\033[%p1%dC"},
449# else
450 {(int)KS_CRI, "\033[%dC"},
451# endif
452 {K_UP, "\316H"},
453 {K_DOWN, "\316P"},
454 {K_LEFT, "\316K"},
455 {K_RIGHT, "\316M"},
456 {K_S_LEFT, "\316s"},
457 {K_S_RIGHT, "\316t"},
458 {K_F1, "\316;"},
459 {K_F2, "\316<"},
460 {K_F3, "\316="},
461 {K_F4, "\316>"},
462 {K_F5, "\316?"},
463 {K_F6, "\316@"},
464 {K_F7, "\316A"},
465 {K_F8, "\316B"},
466 {K_F9, "\316C"},
467 {K_F10, "\316D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100468 {K_F11, "\316\205"}, // guessed
469 {K_F12, "\316\206"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 {K_S_F1, "\316T"},
471 {K_S_F2, "\316U"},
472 {K_S_F3, "\316V"},
473 {K_S_F4, "\316W"},
474 {K_S_F5, "\316X"},
475 {K_S_F6, "\316Y"},
476 {K_S_F7, "\316Z"},
477 {K_S_F8, "\316["},
478 {K_S_F9, "\316\\"},
479 {K_S_F10, "\316]"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100480 {K_S_F11, "\316\207"}, // guessed
481 {K_S_F12, "\316\210"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {K_INS, "\316R"},
483 {K_DEL, "\316S"},
484 {K_HOME, "\316G"},
485 {K_END, "\316O"},
486 {K_PAGEDOWN, "\316Q"},
487 {K_PAGEUP, "\316I"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489/*
490 * These codes are valid for the Win32 Console . The entries that start with
491 * ESC | are translated into console calls in os_win32.c. The function keys
492 * are also translated in os_win32.c.
493 */
494 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100495 {(int)KS_CE, "\033|K"}, // clear to end of line
496 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100498 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100500 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100502 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100504 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
505 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100507 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
508 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100510 {(int)KS_CL, "\033|J"}, // clear screen
511 {(int)KS_CD, "\033|j"}, // clear to end of display
512 {(int)KS_VI, "\033|v"}, // cursor invisible
513 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514
Bram Moolenaar6982f422019-02-16 16:48:01 +0100515 {(int)KS_ME, "\033|0m"}, // normal
516 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
517 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100519 {(int)KS_SO, "\033|31m"}, // standout: white on blue
520 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100522 {(int)KS_SO, "\033|F"}, // standout: high intensity
523 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100525 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
526 {(int)KS_CZR, "\033|0m"}, // italic end
527 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
528 {(int)KS_UE, "\033|0m"}, // underscore end
529 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100531 {(int)KS_CAB, "\033|%p1%db"}, // set background color
532 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100534 {(int)KS_CAB, "\033|%db"}, // set background color
535 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536# endif
537
Bram Moolenaar6982f422019-02-16 16:48:01 +0100538 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100540 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 {(int)KS_LE, "\b"},
542# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100543 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100545 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100547 {(int)KS_VB, "\033|B"}, // visual bell
548 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
549 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100551 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100553 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100555# ifdef FEAT_TERMGUICOLORS
556 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
557 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
558# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
560 {K_UP, "\316H"},
561 {K_DOWN, "\316P"},
562 {K_LEFT, "\316K"},
563 {K_RIGHT, "\316M"},
564 {K_S_UP, "\316\304"},
565 {K_S_DOWN, "\316\317"},
566 {K_S_LEFT, "\316\311"},
567 {K_C_LEFT, "\316s"},
568 {K_S_RIGHT, "\316\313"},
569 {K_C_RIGHT, "\316t"},
570 {K_S_TAB, "\316\017"},
571 {K_F1, "\316;"},
572 {K_F2, "\316<"},
573 {K_F3, "\316="},
574 {K_F4, "\316>"},
575 {K_F5, "\316?"},
576 {K_F6, "\316@"},
577 {K_F7, "\316A"},
578 {K_F8, "\316B"},
579 {K_F9, "\316C"},
580 {K_F10, "\316D"},
581 {K_F11, "\316\205"},
582 {K_F12, "\316\206"},
583 {K_S_F1, "\316T"},
584 {K_S_F2, "\316U"},
585 {K_S_F3, "\316V"},
586 {K_S_F4, "\316W"},
587 {K_S_F5, "\316X"},
588 {K_S_F6, "\316Y"},
589 {K_S_F7, "\316Z"},
590 {K_S_F8, "\316["},
591 {K_S_F9, "\316\\"},
592 {K_S_F10, "\316]"},
593 {K_S_F11, "\316\207"},
594 {K_S_F12, "\316\210"},
595 {K_INS, "\316R"},
596 {K_DEL, "\316S"},
597 {K_HOME, "\316G"},
598 {K_S_HOME, "\316\302"},
599 {K_C_HOME, "\316w"},
600 {K_END, "\316O"},
601 {K_S_END, "\316\315"},
602 {K_C_END, "\316u"},
603 {K_PAGEDOWN, "\316Q"},
604 {K_PAGEUP, "\316I"},
605 {K_KPLUS, "\316N"},
606 {K_KMINUS, "\316J"},
607 {K_KMULTIPLY, "\316\067"},
608 {K_K0, "\316\332"},
609 {K_K1, "\316\336"},
610 {K_K2, "\316\342"},
611 {K_K3, "\316\346"},
612 {K_K4, "\316\352"},
613 {K_K5, "\316\356"},
614 {K_K6, "\316\362"},
615 {K_K7, "\316\366"},
616 {K_K8, "\316\372"},
617 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100618 {K_BS, "\316x"},
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100619 {K_S_BS, "\316y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621/*
622 * VT320 is working as an ANSI terminal compatible DEC terminal.
623 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 * TODO:- rewrite ESC[ codes to CSI
625 * - keyboard languages (CSI ? 26 n)
626 */
627 {(int)KS_NAME, "vt320"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000628 {(int)KS_CE, "\033[K"},
629 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000631 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000633 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000635 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000637 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000639 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000641 {(int)KS_CL, "\033[H\033[2J"},
642 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100643 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000644 {(int)KS_ME, "\033[0m"},
645 {(int)KS_MR, "\033[7m"},
646 {(int)KS_MD, "\033[1m"}, // bold mode
647 {(int)KS_SE, "\033[22m"},// normal mode
648 {(int)KS_UE, "\033[24m"},// exit underscore mode
649 {(int)KS_US, "\033[4m"}, // underscore mode
650 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
651 {(int)KS_CZR, "\033[0m"}, // italic mode end
652 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
653 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
654 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
655 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {(int)KS_MS, "y"},
657 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100658 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {(int)KS_LE, "\b"},
660# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000661 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000663 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664# endif
665# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000666 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000668 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000670 {K_UP, "\033[A"},
671 {K_DOWN, "\033[B"},
672 {K_RIGHT, "\033[C"},
673 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200674 // Note: cursor key sequences for application cursor mode are omitted,
675 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000676 {K_F1, "\033[11~"},
677 {K_F2, "\033[12~"},
678 {K_F3, "\033[13~"},
679 {K_F4, "\033[14~"},
680 {K_F5, "\033[15~"},
681 {K_F6, "\033[17~"},
682 {K_F7, "\033[18~"},
683 {K_F8, "\033[19~"},
684 {K_F9, "\033[20~"},
685 {K_F10, "\033[21~"},
686 {K_F11, "\033[23~"},
687 {K_F12, "\033[24~"},
688 {K_F13, "\033[25~"},
689 {K_F14, "\033[26~"},
690 {K_F15, "\033[28~"}, // Help
691 {K_F16, "\033[29~"}, // Select
692 {K_F17, "\033[31~"},
693 {K_F18, "\033[32~"},
694 {K_F19, "\033[33~"},
695 {K_F20, "\033[34~"},
696 {K_INS, "\033[2~"},
697 {K_DEL, "\033[3~"},
698 {K_HOME, "\033[1~"},
699 {K_END, "\033[4~"},
700 {K_PAGEUP, "\033[5~"},
701 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200702 // These sequences starting with <Esc> O may interfere with what the user
703 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000704 {K_KPLUS, "\033Ok"}, // keypad plus
705 {K_KMINUS, "\033Om"}, // keypad minus
706 {K_KDIVIDE, "\033Oo"}, // keypad /
707 {K_KMULTIPLY, "\033Oj"}, // keypad *
708 {K_KENTER, "\033OM"}, // keypad Enter
709 {K_K0, "\033Op"}, // keypad 0
710 {K_K1, "\033Oq"}, // keypad 1
711 {K_K2, "\033Or"}, // keypad 2
712 {K_K3, "\033Os"}, // keypad 3
713 {K_K4, "\033Ot"}, // keypad 4
714 {K_K5, "\033Ou"}, // keypad 5
715 {K_K6, "\033Ov"}, // keypad 6
716 {K_K7, "\033Ow"}, // keypad 7
717 {K_K8, "\033Ox"}, // keypad 8
718 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100719 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721/*
722 * Ordinary vt52
723 */
724 {(int)KS_NAME, "vt52"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000725 {(int)KS_CE, "\033K"},
726 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100727# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000728 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100729# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000730 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100731# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000733 {(int)KS_SR, "\033I"},
734 {(int)KS_AL, "\033L"},
735 {(int)KS_DL, "\033M"},
736 {K_UP, "\033A"},
737 {K_DOWN, "\033B"},
738 {K_LEFT, "\033D"},
739 {K_RIGHT, "\033C"},
740 {K_F1, "\033P"},
741 {K_F2, "\033Q"},
742 {K_F3, "\033R"},
743 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200746 {(int)KS_NAME, "xterm"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000747 {(int)KS_CE, "\033[K"},
748 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000750 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000752 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000754 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000756 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000758 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759# endif
760# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000761 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000763 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000765 {(int)KS_CL, "\033[H\033[2J"},
766 {(int)KS_CD, "\033[J"},
767 {(int)KS_ME, "\033[m"},
768 {(int)KS_MR, "\033[7m"},
769 {(int)KS_MD, "\033[1m"},
770 {(int)KS_UE, "\033[m"},
771 {(int)KS_US, "\033[4m"},
772 {(int)KS_STE, "\033[29m"},
773 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 {(int)KS_MS, "y"},
775 {(int)KS_UT, "y"},
776 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000777 {(int)KS_VI, "\033[?25l"},
778 {(int)KS_VE, "\033[?25h"},
779 {(int)KS_VS, "\033[?12h"},
780 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200781# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000782 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200783# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000784 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200785# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000786 {(int)KS_CRC, "\033[?12$p"},
787 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000789 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000791 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000793 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000795 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000797 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000799 {(int)KS_KS, "\033[?1h\033="},
800 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000802 {(int)KS_TI, "\0337\033[?47h"},
803 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000805 {(int)KS_CTI, "\033[>4;2m"},
806 {(int)KS_CTE, "\033[>4;m"},
807 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000809 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000811 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200812 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000814 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
815 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
816 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000818 {(int)KS_CWS, "\033[8;%d;%dt"},
819 {(int)KS_CWP, "\033[3;%d;%dt"},
820 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000822 {(int)KS_CRV, "\033[>c"},
823 {(int)KS_RFG, "\033]10;?\007"},
824 {(int)KS_RBG, "\033]11;?\007"},
825 {(int)KS_U7, "\033[6n"},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200826# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100827 // These are printf strings, not terminal codes.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000828 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
829 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
830 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200831# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000832 {(int)KS_CAU, "\033[58;5;%dm"},
833 {(int)KS_CBE, "\033[?2004h"},
834 {(int)KS_CBD, "\033[?2004l"},
835 {(int)KS_CST, "\033[22;2t"},
836 {(int)KS_CRT, "\033[23;2t"},
837 {(int)KS_SSI, "\033[22;1t"},
838 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100839# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000840 {(int)KS_FD, "\033[?1004l"},
841 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100842# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000843
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000844 {K_UP, "\033O*A"},
845 {K_DOWN, "\033O*B"},
846 {K_RIGHT, "\033O*C"},
847 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100848 // An extra set of cursor keys for vt100 mode
Trygve Aabergea3532822022-10-18 19:22:25 +0100849 {K_XUP, "\033[@;*A"}, // Esc [ A or Esc [ 1 ; A
850 {K_XDOWN, "\033[@;*B"}, // Esc [ B or Esc [ 1 ; B
851 {K_XRIGHT, "\033[@;*C"}, // Esc [ C or Esc [ 1 ; C
852 {K_XLEFT, "\033[@;*D"}, // Esc [ D or Esc [ 1 ; D
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100853 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000854 {K_XF1, "\033O*P"},
855 {K_XF2, "\033O*Q"},
856 {K_XF3, "\033O*R"},
857 {K_XF4, "\033O*S"},
858 {K_F1, "\033[11;*~"},
859 {K_F2, "\033[12;*~"},
860 {K_F3, "\033[13;*~"},
861 {K_F4, "\033[14;*~"},
862 {K_F5, "\033[15;*~"},
863 {K_F6, "\033[17;*~"},
864 {K_F7, "\033[18;*~"},
865 {K_F8, "\033[19;*~"},
866 {K_F9, "\033[20;*~"},
867 {K_F10, "\033[21;*~"},
868 {K_F11, "\033[23;*~"},
869 {K_F12, "\033[24;*~"},
870 {K_S_TAB, "\033[Z"},
871 {K_HELP, "\033[28;*~"},
872 {K_UNDO, "\033[26;*~"},
873 {K_INS, "\033[2;*~"},
Trygve Aabergea3532822022-10-18 19:22:25 +0100874 {K_HOME, "\033[@;*H"}, // Esc [ H or Esc 1 ; H
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000875 // {K_S_HOME, "\033O2H"},
876 // {K_C_HOME, "\033O5H"},
877 {K_KHOME, "\033[1;*~"},
878 {K_XHOME, "\033O*H"}, // other Home
879 {K_ZHOME, "\033[7;*~"}, // other Home
Trygve Aabergea3532822022-10-18 19:22:25 +0100880 {K_END, "\033[@;*F"}, // Esc [ F or Esc 1 ; F
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000881 // {K_S_END, "\033O2F"},
882 // {K_C_END, "\033O5F"},
883 {K_KEND, "\033[4;*~"},
884 {K_XEND, "\033O*F"}, // other End
885 {K_ZEND, "\033[8;*~"},
886 {K_PAGEUP, "\033[5;*~"},
887 {K_PAGEDOWN, "\033[6;*~"},
888 {K_KPLUS, "\033O*k"}, // keypad plus
889 {K_KMINUS, "\033O*m"}, // keypad minus
890 {K_KDIVIDE, "\033O*o"}, // keypad /
891 {K_KMULTIPLY, "\033O*j"}, // keypad *
892 {K_KENTER, "\033O*M"}, // keypad Enter
893 {K_KPOINT, "\033O*n"}, // keypad .
894 {K_K0, "\033O*p"}, // keypad 0
895 {K_K1, "\033O*q"}, // keypad 1
896 {K_K2, "\033O*r"}, // keypad 2
897 {K_K3, "\033O*s"}, // keypad 3
898 {K_K4, "\033O*t"}, // keypad 4
899 {K_K5, "\033O*u"}, // keypad 5
900 {K_K6, "\033O*v"}, // keypad 6
901 {K_K7, "\033O*w"}, // keypad 7
902 {K_K8, "\033O*x"}, // keypad 8
903 {K_K9, "\033O*y"}, // keypad 9
904 {K_KDEL, "\033[3;*~"}, // keypad Del
905 {K_PS, "\033[200~"}, // paste start
906 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
908 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000909 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
910 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100911 // F14 and F15 are missing, because they send the same codes as the undo
912 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000913 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
914 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
915 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
916 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
917 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000918
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000919 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
920 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
921 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
922 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
923 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
924 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
925 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
926 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
927 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
928 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000929
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000930 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
931 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
932 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
933 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
934 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
935 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
936 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938/*
939 * iris-ansi for Silicon Graphics machines.
940 */
941 {(int)KS_NAME, "iris-ansi"},
942 {(int)KS_CE, "\033[K"},
943 {(int)KS_CD, "\033[J"},
944 {(int)KS_AL, "\033[L"},
945# ifdef TERMINFO
946 {(int)KS_CAL, "\033[%p1%dL"},
947# else
948 {(int)KS_CAL, "\033[%dL"},
949# endif
950 {(int)KS_DL, "\033[M"},
951# ifdef TERMINFO
952 {(int)KS_CDL, "\033[%p1%dM"},
953# else
954 {(int)KS_CDL, "\033[%dM"},
955# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100956#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957# ifdef TERMINFO
958 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
959# else
960 {(int)KS_CS, "\033[%i%d;%dr"},
961# endif
962#endif
963 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100964 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
965 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {(int)KS_TI, "\033[=6h"},
967 {(int)KS_TE, "\033[=6l"},
968 {(int)KS_SE, "\033[21;27m"},
969 {(int)KS_SO, "\033[1;7m"},
970 {(int)KS_ME, "\033[m"},
971 {(int)KS_MR, "\033[7m"},
972 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100973 {(int)KS_CCO, "8"}, // allow 8 colors
974 {(int)KS_CZH, "\033[3m"}, // italic mode on
975 {(int)KS_CZR, "\033[23m"}, // italic mode off
976 {(int)KS_US, "\033[4m"}, // underline on
977 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100979 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
980 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
981 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
982 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100984 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
985 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
986 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
987 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100989 {(int)KS_MS, "y"}, // guessed
990 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {(int)KS_LE, "\b"},
992# ifdef TERMINFO
993 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
994# else
995 {(int)KS_CM, "\033[%i%d;%dH"},
996# endif
997 {(int)KS_SR, "\033M"},
998# ifdef TERMINFO
999 {(int)KS_CRI, "\033[%p1%dC"},
1000# else
1001 {(int)KS_CRI, "\033[%dC"},
1002# endif
1003 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001004 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001006 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007# ifdef TERMINFO
1008 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1009 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1010# else
1011 {(int)KS_CWS, "\033[203;%d;%d/y"},
1012 {(int)KS_CWP, "\033[205;%d;%d/y"},
1013# endif
1014 {K_UP, "\033[A"},
1015 {K_DOWN, "\033[B"},
1016 {K_LEFT, "\033[D"},
1017 {K_RIGHT, "\033[C"},
1018 {K_S_UP, "\033[161q"},
1019 {K_S_DOWN, "\033[164q"},
1020 {K_S_LEFT, "\033[158q"},
1021 {K_S_RIGHT, "\033[167q"},
1022 {K_F1, "\033[001q"},
1023 {K_F2, "\033[002q"},
1024 {K_F3, "\033[003q"},
1025 {K_F4, "\033[004q"},
1026 {K_F5, "\033[005q"},
1027 {K_F6, "\033[006q"},
1028 {K_F7, "\033[007q"},
1029 {K_F8, "\033[008q"},
1030 {K_F9, "\033[009q"},
1031 {K_F10, "\033[010q"},
1032 {K_F11, "\033[011q"},
1033 {K_F12, "\033[012q"},
1034 {K_S_F1, "\033[013q"},
1035 {K_S_F2, "\033[014q"},
1036 {K_S_F3, "\033[015q"},
1037 {K_S_F4, "\033[016q"},
1038 {K_S_F5, "\033[017q"},
1039 {K_S_F6, "\033[018q"},
1040 {K_S_F7, "\033[019q"},
1041 {K_S_F8, "\033[020q"},
1042 {K_S_F9, "\033[021q"},
1043 {K_S_F10, "\033[022q"},
1044 {K_S_F11, "\033[023q"},
1045 {K_S_F12, "\033[024q"},
1046 {K_INS, "\033[139q"},
1047 {K_HOME, "\033[H"},
1048 {K_END, "\033[146q"},
1049 {K_PAGEUP, "\033[150q"},
1050 {K_PAGEDOWN, "\033[154q"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052/*
1053 * for debugging
1054 */
1055 {(int)KS_NAME, "debug"},
1056 {(int)KS_CE, "[CE]"},
1057 {(int)KS_CD, "[CD]"},
1058 {(int)KS_AL, "[AL]"},
1059# ifdef TERMINFO
1060 {(int)KS_CAL, "[CAL%p1%d]"},
1061# else
1062 {(int)KS_CAL, "[CAL%d]"},
1063# endif
1064 {(int)KS_DL, "[DL]"},
1065# ifdef TERMINFO
1066 {(int)KS_CDL, "[CDL%p1%d]"},
1067# else
1068 {(int)KS_CDL, "[CDL%d]"},
1069# endif
1070# ifdef TERMINFO
1071 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1072# else
1073 {(int)KS_CS, "[%dCS%d]"},
1074# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001075# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001077# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079# endif
1080# ifdef TERMINFO
1081 {(int)KS_CAB, "[CAB%p1%d]"},
1082 {(int)KS_CAF, "[CAF%p1%d]"},
1083 {(int)KS_CSB, "[CSB%p1%d]"},
1084 {(int)KS_CSF, "[CSF%p1%d]"},
1085# else
1086 {(int)KS_CAB, "[CAB%d]"},
1087 {(int)KS_CAF, "[CAF%d]"},
1088 {(int)KS_CSB, "[CSB%d]"},
1089 {(int)KS_CSF, "[CSF%d]"},
1090# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001091 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {(int)KS_OP, "[OP]"},
1093 {(int)KS_LE, "[LE]"},
1094 {(int)KS_CL, "[CL]"},
1095 {(int)KS_VI, "[VI]"},
1096 {(int)KS_VE, "[VE]"},
1097 {(int)KS_VS, "[VS]"},
1098 {(int)KS_ME, "[ME]"},
1099 {(int)KS_MR, "[MR]"},
1100 {(int)KS_MB, "[MB]"},
1101 {(int)KS_MD, "[MD]"},
1102 {(int)KS_SE, "[SE]"},
1103 {(int)KS_SO, "[SO]"},
1104 {(int)KS_UE, "[UE]"},
1105 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001106 {(int)KS_UCE, "[UCE]"},
1107 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001108 {(int)KS_USS, "[USS]"},
1109 {(int)KS_DS, "[DS]"},
1110 {(int)KS_CDS, "[CDS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001111 {(int)KS_STE, "[STE]"},
1112 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 {(int)KS_MS, "[MS]"},
1114 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001115 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116# ifdef TERMINFO
1117 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1118# else
1119 {(int)KS_CM, "[%dCM%d]"},
1120# endif
1121 {(int)KS_SR, "[SR]"},
1122# ifdef TERMINFO
1123 {(int)KS_CRI, "[CRI%p1%d]"},
1124# else
1125 {(int)KS_CRI, "[CRI%d]"},
1126# endif
1127 {(int)KS_VB, "[VB]"},
1128 {(int)KS_KS, "[KS]"},
1129 {(int)KS_KE, "[KE]"},
1130 {(int)KS_TI, "[TI]"},
1131 {(int)KS_TE, "[TE]"},
1132 {(int)KS_CIS, "[CIS]"},
1133 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001134 {(int)KS_CSC, "[CSC]"},
1135 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 {(int)KS_TS, "[TS]"},
1137 {(int)KS_FS, "[FS]"},
1138# ifdef TERMINFO
1139 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1140 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1141# else
1142 {(int)KS_CWS, "[%dCWS%d]"},
1143 {(int)KS_CWP, "[%dCWP%d]"},
1144# endif
1145 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001146 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001147 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001148 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 {K_UP, "[KU]"},
1150 {K_DOWN, "[KD]"},
1151 {K_LEFT, "[KL]"},
1152 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001153 {K_XUP, "[xKU]"},
1154 {K_XDOWN, "[xKD]"},
1155 {K_XLEFT, "[xKL]"},
1156 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {K_S_UP, "[S-KU]"},
1158 {K_S_DOWN, "[S-KD]"},
1159 {K_S_LEFT, "[S-KL]"},
1160 {K_C_LEFT, "[C-KL]"},
1161 {K_S_RIGHT, "[S-KR]"},
1162 {K_C_RIGHT, "[C-KR]"},
1163 {K_F1, "[F1]"},
1164 {K_XF1, "[xF1]"},
1165 {K_F2, "[F2]"},
1166 {K_XF2, "[xF2]"},
1167 {K_F3, "[F3]"},
1168 {K_XF3, "[xF3]"},
1169 {K_F4, "[F4]"},
1170 {K_XF4, "[xF4]"},
1171 {K_F5, "[F5]"},
1172 {K_F6, "[F6]"},
1173 {K_F7, "[F7]"},
1174 {K_F8, "[F8]"},
1175 {K_F9, "[F9]"},
1176 {K_F10, "[F10]"},
1177 {K_F11, "[F11]"},
1178 {K_F12, "[F12]"},
1179 {K_S_F1, "[S-F1]"},
1180 {K_S_XF1, "[S-xF1]"},
1181 {K_S_F2, "[S-F2]"},
1182 {K_S_XF2, "[S-xF2]"},
1183 {K_S_F3, "[S-F3]"},
1184 {K_S_XF3, "[S-xF3]"},
1185 {K_S_F4, "[S-F4]"},
1186 {K_S_XF4, "[S-xF4]"},
1187 {K_S_F5, "[S-F5]"},
1188 {K_S_F6, "[S-F6]"},
1189 {K_S_F7, "[S-F7]"},
1190 {K_S_F8, "[S-F8]"},
1191 {K_S_F9, "[S-F9]"},
1192 {K_S_F10, "[S-F10]"},
1193 {K_S_F11, "[S-F11]"},
1194 {K_S_F12, "[S-F12]"},
1195 {K_HELP, "[HELP]"},
1196 {K_UNDO, "[UNDO]"},
1197 {K_BS, "[BS]"},
1198 {K_INS, "[INS]"},
1199 {K_KINS, "[KINS]"},
1200 {K_DEL, "[DEL]"},
1201 {K_KDEL, "[KDEL]"},
1202 {K_HOME, "[HOME]"},
1203 {K_S_HOME, "[C-HOME]"},
1204 {K_C_HOME, "[C-HOME]"},
1205 {K_KHOME, "[KHOME]"},
1206 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001207 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {K_END, "[END]"},
1209 {K_S_END, "[C-END]"},
1210 {K_C_END, "[C-END]"},
1211 {K_KEND, "[KEND]"},
1212 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001213 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {K_PAGEUP, "[PAGEUP]"},
1215 {K_PAGEDOWN, "[PAGEDOWN]"},
1216 {K_KPAGEUP, "[KPAGEUP]"},
1217 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1218 {K_MOUSE, "[MOUSE]"},
1219 {K_KPLUS, "[KPLUS]"},
1220 {K_KMINUS, "[KMINUS]"},
1221 {K_KDIVIDE, "[KDIVIDE]"},
1222 {K_KMULTIPLY, "[KMULTIPLY]"},
1223 {K_KENTER, "[KENTER]"},
1224 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001225 {K_PS, "[PASTE-START]"},
1226 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {K_K0, "[K0]"},
1228 {K_K1, "[K1]"},
1229 {K_K2, "[K2]"},
1230 {K_K3, "[K3]"},
1231 {K_K4, "[K4]"},
1232 {K_K5, "[K5]"},
1233 {K_K6, "[K6]"},
1234 {K_K7, "[K7]"},
1235 {K_K8, "[K8]"},
1236 {K_K9, "[K9]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
1238/*
1239 * The most minimal terminal: only clear screen and cursor positioning
1240 * Always included.
1241 */
1242 {(int)KS_NAME, "dumb"},
1243 {(int)KS_CL, "\014"},
1244#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001245 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001247 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248#endif
1249
1250/*
1251 * end marker
1252 */
1253 {(int)KS_NAME, NULL}
1254
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001255}; // end of builtin_termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001257#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001258 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001259termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001260{
Bram Moolenaarab302212016-04-26 20:59:29 +02001261 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001262}
1263
1264 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001265termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001266{
1267 guicolor_T t;
1268
1269 if (*name == NUL)
1270 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001271 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001272
1273 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001274 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001275 return t;
1276}
1277
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001278 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001279termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001280{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001281 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001282}
1283#endif
1284
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285/*
1286 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#ifdef AMIGA
1289# define DEFAULT_TERM (char_u *)"amiga"
1290#endif
1291
1292#ifdef MSWIN
1293# define DEFAULT_TERM (char_u *)"win32"
1294#endif
1295
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001296#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297# define DEFAULT_TERM (char_u *)"ansi"
1298#endif
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#ifdef VMS
1301# define DEFAULT_TERM (char_u *)"vt320"
1302#endif
1303
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001304#ifdef __HAIKU__
1305# undef DEFAULT_TERM
1306# define DEFAULT_TERM (char_u *)"xterm"
1307#endif
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309#ifndef DEFAULT_TERM
1310# define DEFAULT_TERM (char_u *)"dumb"
1311#endif
1312
1313/*
1314 * Term_strings contains currently used terminal output strings.
1315 * It is initialized with the default values by parse_builtin_tcap().
1316 * The values can be changed by setting the option with the same name.
1317 */
1318char_u *(term_strings[(int)KS_LAST + 1]);
1319
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001320static int need_gather = FALSE; // need to fill termleader[]
1321static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001323static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001324
1325/*
1326 * Structure and table to store terminal features that can be detected by
1327 * querying the terminal. Either by inspecting the termresponse or a more
1328 * specific request. Besides this there are:
1329 * t_colors - number of colors supported
1330 */
1331typedef struct {
1332 char *tpr_name;
1333 int tpr_set_by_termresponse;
1334 int tpr_status;
1335} termprop_T;
1336
1337// Values for tpr_status.
1338#define TPR_UNKNOWN 'u'
1339#define TPR_YES 'y'
1340#define TPR_NO 'n'
1341#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1342#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1343#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1344
1345// can request the cursor style without messing up the display
1346#define TPR_CURSOR_STYLE 0
1347// can request the cursor blink mode without messing up the display
1348#define TPR_CURSOR_BLINK 1
1349// can set the underline color with t_8u without resetting other colors
1350#define TPR_UNDERLINE_RGB 2
1351// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1352#define TPR_MOUSE 3
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001353// term response indicates kitty
1354#define TPR_KITTY 4
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001355// table size
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001356#define TPR_COUNT 5
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001357
1358static termprop_T term_props[TPR_COUNT];
1359
1360/*
1361 * Initialize the term_props table.
1362 * When "all" is FALSE only set those that are detected from the version
1363 * response.
1364 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001365 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001366init_term_props(int all)
1367{
1368 int i;
1369
1370 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1371 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1372 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1373 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1374 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1375 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1376 term_props[TPR_MOUSE].tpr_name = "mouse";
1377 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001378 term_props[TPR_KITTY].tpr_name = "kitty";
1379 term_props[TPR_KITTY].tpr_set_by_termresponse = FALSE;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001380
1381 for (i = 0; i < TPR_COUNT; ++i)
1382 if (all || term_props[i].tpr_set_by_termresponse)
1383 term_props[i].tpr_status = TPR_UNKNOWN;
1384}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385#endif
1386
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001387#if defined(FEAT_EVAL) || defined(PROTO)
1388 void
1389f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1390{
1391# ifdef FEAT_TERMRESPONSE
1392 int i;
1393# endif
1394
Bram Moolenaar93a10962022-06-16 11:42:09 +01001395 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001396 return;
1397# ifdef FEAT_TERMRESPONSE
1398 for (i = 0; i < TPR_COUNT; ++i)
1399 {
1400 char_u value[2];
1401
1402 value[0] = term_props[i].tpr_status;
1403 value[1] = NUL;
1404 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1405 }
1406# endif
1407}
1408#endif
1409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001411find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
1413 struct builtin_term *p;
1414
1415 p = builtin_termcaps;
1416 while (p->bt_string != NULL)
1417 {
1418 if (p->bt_entry == (int)KS_NAME)
1419 {
1420#ifdef UNIX
1421 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1422 return p;
1423 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1424 return p;
1425 else
1426#endif
1427#ifdef VMS
1428 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1429 return p;
1430 else
1431#endif
1432 if (STRCMP(term, p->bt_string) == 0)
1433 return p;
1434 }
1435 ++p;
1436 }
1437 return p;
1438}
1439
1440/*
1441 * Parsing of the builtin termcap entries.
1442 * Caller should check if 'name' is a valid builtin term.
1443 * The terminal's name is not set, as this is already done in termcapinit().
1444 */
1445 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001446parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447{
1448 struct builtin_term *p;
1449 char_u name[2];
1450 int term_8bit;
1451
1452 p = find_builtin_term(term);
1453 term_8bit = term_is_8bit(term);
1454
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001455 // Do not parse if builtin term not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 if (p->bt_string == NULL)
1457 return;
1458
1459 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1460 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001461 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001463 // Only set the value if it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (term_strings[p->bt_entry] == NULL
1465 || term_strings[p->bt_entry] == empty_option)
1466 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001467#ifdef FEAT_EVAL
1468 int opt_idx = -1;
1469#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001470 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1472 {
1473 char_u *s, *t;
1474
1475 s = vim_strsave((char_u *)p->bt_string);
1476 if (s != NULL)
1477 {
1478 for (t = s; *t; ++t)
1479 if (term_7to8bit(t))
1480 {
1481 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001482 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
1484 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001485#ifdef FEAT_EVAL
1486 opt_idx =
1487#endif
1488 set_term_option_alloced(
1489 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491 }
1492 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001495#ifdef FEAT_EVAL
1496 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1497#endif
1498 }
1499#ifdef FEAT_EVAL
1500 set_term_option_sctx_idx(NULL, opt_idx);
1501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 }
1503 }
1504 else
1505 {
1506 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1507 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1508 if (find_termcode(name) == NULL)
1509 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1510 }
1511 }
1512}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514/*
1515 * Set number of colors.
1516 * Store it as a number in t_colors.
1517 * Store it as a string in T_CCO (using nr_colors[]).
1518 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001519 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001520set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001522 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
1524 t_colors = nr;
1525 if (t_colors > 1)
1526 sprintf((char *)nr_colors, "%d", t_colors);
1527 else
1528 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001529 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001531
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001532#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001533/*
1534 * Set the color count to "val" and redraw if it changed.
1535 */
1536 static void
1537may_adjust_color_count(int val)
1538{
1539 if (val != t_colors)
1540 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001541 // Nr of colors changed, initialize highlighting and
1542 // redraw everything. This causes a redraw, which usually
1543 // clears the message. Try keeping the message if it
1544 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001545 set_keep_msg_from_hist();
1546 set_color_count(val);
1547 init_highlight(TRUE, FALSE);
1548# ifdef DEBUG_TERMRESPONSE
1549 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001550 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001551
Bram Moolenaarb255b902018-04-24 21:40:10 +02001552 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001553 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001554# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001555 redraw_asap(UPD_CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001556# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001557 }
1558}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559#endif
1560
1561#ifdef HAVE_TGETENT
1562static char *(key_names[]) =
1563{
Bram Moolenaar87202262020-05-24 17:23:45 +02001564# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001565 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 "#2", "#4", "%i", "*7",
1570 "k1", "k2", "k3", "k4", "k5", "k6",
1571 "k7", "k8", "k9", "k;", "F1", "F2",
1572 "%1", "&8", "kb", "kI", "kD", "kh",
1573 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1574 NULL
1575};
1576#endif
1577
Bram Moolenaar9289df52018-05-10 14:40:57 +02001578#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001579 static void
1580get_term_entries(int *height, int *width)
1581{
1582 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001583 enum SpecialKey dest; // index in term_strings[]
1584 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001585 } string_names[] =
1586 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1587 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1588 {KS_CL, "cl"}, {KS_CD, "cd"},
1589 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1590 {KS_ME, "me"}, {KS_MR, "mr"},
1591 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1592 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1593 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001594 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001595 {KS_STE,"Te"}, {KS_STS,"Ts"},
1596 {KS_CM, "cm"}, {KS_SR, "sr"},
1597 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1598 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001599 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001600 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001601 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1602 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001603 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1604 {KS_VS, "vs"}, {KS_CVS, "VS"},
1605 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1606 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1607 {KS_TS, "ts"}, {KS_FS, "fs"},
1608 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1609 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1610 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001611 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001612 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1613 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001614 {KS_CST, "ST"}, {KS_CRT, "RT"},
1615 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001616 {(enum SpecialKey)0, NULL}
1617 };
1618 int i;
1619 char_u *p;
1620 static char_u tstrbuf[TBUFSZ];
1621 char_u *tp = tstrbuf;
1622
1623 /*
1624 * get output strings
1625 */
1626 for (i = 0; string_names[i].name != NULL; ++i)
1627 {
1628 if (TERM_STR(string_names[i].dest) == NULL
1629 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001630 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001631 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001632#ifdef FEAT_EVAL
1633 set_term_option_sctx_idx(string_names[i].name, -1);
1634#endif
1635 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001636 }
1637
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001638 // tgetflag() returns 1 if the flag is present, 0 if not and
1639 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001640 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1641 T_MS = (char_u *)"y";
1642 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1643 T_XS = (char_u *)"y";
1644 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1645 T_XN = (char_u *)"y";
1646 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1647 T_DB = (char_u *)"y";
1648 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1649 T_DA = (char_u *)"y";
1650 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1651 T_UT = (char_u *)"y";
1652
1653 /*
1654 * get key codes
1655 */
1656 for (i = 0; key_names[i] != NULL; ++i)
1657 if (find_termcode((char_u *)key_names[i]) == NULL)
1658 {
1659 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001660 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001661 if (p != NULL
1662 && (*p != Ctrl_H
1663 || key_names[i][0] != 'k'
1664 || key_names[i][1] != 'l'))
1665 add_termcode((char_u *)key_names[i], p, FALSE);
1666 }
1667
1668 if (*height == 0)
1669 *height = tgetnum("li");
1670 if (*width == 0)
1671 *width = tgetnum("co");
1672
1673 /*
1674 * Get number of colors (if not done already).
1675 */
1676 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001677 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001678 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001679#ifdef FEAT_EVAL
1680 set_term_option_sctx_idx("Co", -1);
1681#endif
1682 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001683
1684# ifndef hpux
1685 BC = (char *)TGETSTR("bc", &tp);
1686 UP = (char *)TGETSTR("up", &tp);
1687 p = TGETSTR("pc", &tp);
1688 if (p)
1689 PC = *p;
1690# endif
1691}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001692#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001693
1694 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001695report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001696{
1697 struct builtin_term *termp;
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001698 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001699
1700 mch_errmsg("\r\n");
1701 if (error_msg != NULL)
1702 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001703 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001704 mch_errmsg("\r\n");
1705 }
1706 mch_errmsg("'");
1707 mch_errmsg((char *)term);
1708 mch_errmsg(_("' not known. Available builtin terminals are:"));
1709 mch_errmsg("\r\n");
1710 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1711 {
Bram Moolenaar0f5575d2021-07-30 21:18:03 +02001712 if (termp->bt_entry == (int)KS_NAME
1713 && STRCMP(termp->bt_string, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001714 {
1715#ifdef HAVE_TGETENT
1716 mch_errmsg(" builtin_");
1717#else
1718 mch_errmsg(" ");
1719#endif
1720 mch_errmsg(termp->bt_string);
1721 mch_errmsg("\r\n");
1722 }
1723 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001724 // Output extra 'cmdheight' line breaks to avoid that the following error
1725 // message overwrites the last terminal name.
1726 for (i = 1; i < p_ch; ++i)
1727 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001728}
1729
1730 static void
1731report_default_term(char_u *term)
1732{
1733 mch_errmsg(_("defaulting to '"));
1734 mch_errmsg((char *)term);
1735 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001736 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001737 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001738 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001739 out_flush();
1740 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001741 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001742 }
1743}
1744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745/*
1746 * Set terminal options for terminal "term".
1747 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1748 *
1749 * While doing this, until ttest(), some options may be NULL, be careful.
1750 */
1751 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001752set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 struct builtin_term *termp;
1755#ifdef HAVE_TGETENT
1756 int builtin_first = p_tbi;
1757 int try;
1758 int termcap_cleared = FALSE;
1759#endif
1760 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001761 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 char_u *bs_p, *del_p;
1763
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001764 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001765 if (silent_mode)
1766 return OK;
1767
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001768 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
1770 if (term_is_builtin(term))
1771 {
1772 term += 8;
1773#ifdef HAVE_TGETENT
1774 builtin_first = 1;
1775#endif
1776 }
1777
1778/*
1779 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1780 * If builtin_first is TRUE:
1781 * 0. try builtin termcap
1782 * 1. try external termcap
1783 * 2. if both fail default to a builtin terminal
1784 * If builtin_first is FALSE:
1785 * 1. try external termcap
1786 * 2. try builtin termcap, if both fail default to a builtin terminal
1787 */
1788#ifdef HAVE_TGETENT
1789 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1790 {
1791 /*
1792 * Use external termcap
1793 */
1794 if (try == 1)
1795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 /*
1799 * If the external termcap does not have a matching entry, try the
1800 * builtin ones.
1801 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01001802 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (!termcap_cleared)
1805 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001806 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 termcap_cleared = TRUE;
1808 }
1809
Bram Moolenaar69e05692018-05-10 14:11:52 +02001810 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
1812 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001813 else // try == 0 || try == 2
1814#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 /*
1816 * Use builtin termcap
1817 */
1818 {
1819#ifdef HAVE_TGETENT
1820 /*
1821 * If builtin termcap was already used, there is no need to search
1822 * for the builtin termcap again, quit now.
1823 */
1824 if (try == 2 && builtin_first && termcap_cleared)
1825 break;
1826#endif
1827 /*
1828 * search for 'term' in builtin_termcaps[]
1829 */
1830 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001831 if (termp->bt_string == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 {
1833#ifdef HAVE_TGETENT
1834 /*
1835 * If try == 0, first try the external termcap. If that is not
1836 * found we'll get back here with try == 2.
1837 * If termcap_cleared is set we used the external termcap,
1838 * don't complain about not finding the term in the builtin
1839 * termcap.
1840 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001841 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001843 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 break;
1845#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001846 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001848 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if (starting != NO_SCREEN)
1850 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001851 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 wait_return(TRUE);
1853 return FAIL;
1854 }
1855 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001856 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001857 set_string_option_direct((char_u *)"term", -1, term,
1858 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 display_errors();
1860 }
1861 out_flush();
1862#ifdef HAVE_TGETENT
1863 if (!termcap_cleared)
1864 {
1865#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001866 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867#ifdef HAVE_TGETENT
1868 termcap_cleared = TRUE;
1869 }
1870#endif
1871 parse_builtin_tcap(term);
1872#ifdef FEAT_GUI
1873 if (term_is_gui(term))
1874 {
1875 out_flush();
1876 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001877 // If starting the GUI failed, don't do any of the other
1878 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (!gui.in_use)
1880 return FAIL;
1881#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001882 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883#endif
1884 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001885#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 }
1887#ifdef HAVE_TGETENT
1888 }
1889#endif
1890
1891/*
1892 * special: There is no info in the termcap about whether the cursor
1893 * positioning is relative to the start of the screen or to the start of the
1894 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1895 * relative.
1896 */
1897 if (STRCMP(term, "pcterm") == 0)
1898 T_CCS = (char_u *)"yes";
1899 else
1900 T_CCS = empty_option;
1901
1902#ifdef UNIX
1903/*
1904 * Any "stty" settings override the default for t_kb from the termcap.
1905 * This is in os_unix.c, because it depends a lot on the version of unix that
1906 * is being used.
1907 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1908 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001909# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001911# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 get_stty();
1913#endif
1914
1915/*
1916 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1917 * didn't work, use the default CTRL-H
1918 * The default for t_kD is DEL, unless t_kb is DEL.
1919 * The vim_strsave'd strings are probably lost forever, well it's only two
1920 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1921 * directly.
1922 */
1923#ifdef FEAT_GUI
1924 if (!gui.in_use)
1925#endif
1926 {
1927 bs_p = find_termcode((char_u *)"kb");
1928 del_p = find_termcode((char_u *)"kD");
1929 if (bs_p == NULL || *bs_p == NUL)
1930 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1931 if ((del_p == NULL || *del_p == NUL) &&
1932 (bs_p == NULL || *bs_p != DEL))
1933 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1934 }
1935
1936#if defined(UNIX) || defined(VMS)
1937 term_is_xterm = vim_is_xterm(term);
1938#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02001939#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001940 // Reset terminal properties that are set based on the termresponse, which
1941 // will be sent out soon.
1942 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02001943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001945#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 /*
1947 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1948 * The termcode for the mouse is added as a side effect in option.c.
1949 */
1950 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001951 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001953# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001954 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001957 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 else
1959 p = (char_u *)"xterm";
1960 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001963 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001964 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001965 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1966 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001967 reset_option_was_set((char_u *)"ttym");
1968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001970# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001974 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001976#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
Bram Moolenaarbf789742021-01-16 11:21:40 +01001980#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00001981 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001982 if (use_xterm_like_mouse(term))
1983 {
1984 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001985
1986 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01001987 name[0] = KS_EXTRA;
1988 name[1] = KE_FOCUSGAINED;
1989 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001990 add_termcode(name, (char_u *)"\033[I", FALSE);
1991
1992 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01001993 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001994 add_termcode(name, (char_u *)"\033[O", FALSE);
1995
Bram Moolenaar51b477f2021-03-03 15:24:28 +01001996 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001997 }
1998#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00001999#if (defined(UNIX) || defined(VMS))
2000 // First time after setting 'term' a focus event is always reported.
2001 focus_state = MAYBE;
2002#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002003
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002005 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 if (STRCMP(term, DEFAULT_TERM) != 0)
2007 term_console = FALSE;
2008 else
2009 {
2010 term_console = TRUE;
2011# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002012 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013# endif
2014 }
2015#endif
2016
2017#if defined(UNIX) || defined(VMS)
2018 /*
2019 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2020 */
2021 if (vim_is_fastterm(term))
2022 p_tf = TRUE;
2023#endif
2024#ifdef USE_TERM_CONSOLE
2025 /*
2026 * 'ttyfast' is default on consoles
2027 */
2028 if (term_console)
2029 p_tf = TRUE;
2030#endif
2031
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002032 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002034 full_screen = TRUE; // we can use termcap codes from now on
2035 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002037 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002038 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002039 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040#endif
2041
2042 /*
2043 * Initialize the terminal with the appropriate termcap codes.
2044 * Set the mouse and window title if possible.
2045 * Don't do this when starting, need to parse the .vimrc first, because it
2046 * may redefine t_TI etc.
2047 */
2048 if (starting != NO_SCREEN)
2049 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002050 starttermcap(); // may change terminal mode
2051 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002052 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
2054
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002055 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if (width <= 0 || height <= 0)
2057 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002058 // termcap failed to report size
2059 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002061#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002062 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002064 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065#endif
2066 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002067 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 if (starting != NO_SCREEN)
2069 {
2070 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002071 scroll_region_reset(); // In case Rows changed
2072 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002075 buf_T *buf;
2076 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 /*
2079 * Execute the TermChanged autocommands for each buffer that is
2080 * loaded.
2081 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002082 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
2084 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002085 {
2086 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2088 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002089 // restore curwin/curbuf and a few other things
2090 aucmd_restbuf(&aco);
2091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
2095
2096#ifdef FEAT_TERMRESPONSE
2097 may_req_termresponse();
2098#endif
2099
2100 return OK;
2101}
2102
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002103#if defined(EXITFREE) || defined(PROTO)
2104
2105# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002106# include <term.h> // declares cur_term
2107# endif
2108
2109/*
2110 * If supported, delete "cur_term", which caches terminal related entries.
2111 * Avoids that valgrind reports possibly lost memory.
2112 */
2113 void
2114free_cur_term()
2115{
2116# ifdef HAVE_DEL_CURTERM
2117 if (cur_term)
2118 del_curterm(cur_term);
2119# endif
2120}
2121
2122#endif
2123
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124#ifdef HAVE_TGETENT
2125/*
2126 * Call tgetent()
2127 * Return error message if it fails, NULL if it's OK.
2128 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002129 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002130invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131{
2132 int i;
2133
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002134 // Note: Valgrind may report a leak here, because the library keeps one
2135 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002137 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002139 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140# endif
2141 )
2142 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002143 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2144 // tgetent() with the always existing "dumb" entry to avoid a crash or
2145 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 (void)TGETENT(tbuf, "dumb");
2147
2148 if (i < 0)
2149# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002150 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 if (i == 0)
2152# endif
2153#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002154 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002156 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157#endif
2158 }
2159 return NULL;
2160}
2161
2162/*
2163 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2164 * Fix that here.
2165 */
2166 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002167vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168{
2169 char *p;
2170
2171 p = tgetstr(s, (char **)pp);
2172 if (p == (char *)-1)
2173 p = NULL;
2174 return (char_u *)p;
2175}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002176#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002178#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179/*
2180 * Get Columns and Rows from the termcap. Used after a window signal if the
2181 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2182 * and "li" entries never change. But on some systems this works.
2183 * Errors while getting the entries are ignored.
2184 */
2185 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002186getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002187 long *cp, // pointer to columns
2188 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189{
2190 char_u tbuf[TBUFSZ];
2191
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002192 if (T_NAME != NULL && *T_NAME != NUL && invoke_tgetent(tbuf, T_NAME) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
2194 if (*cp == 0)
2195 *cp = tgetnum("co");
2196 if (*rp == 0)
2197 *rp = tgetnum("li");
2198 }
2199}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002200#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
2202/*
2203 * Get a string entry from the termcap and add it to the list of termcodes.
2204 * Used for <t_xx> special keys.
2205 * Give an error message for failure when not sourcing.
2206 * If force given, replace an existing entry.
2207 * Return FAIL if the entry was not found, OK if the entry was added.
2208 */
2209 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002210add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211{
2212 char_u *term;
2213 int key;
2214 struct builtin_term *termp;
2215#ifdef HAVE_TGETENT
2216 char_u *string;
2217 int i;
2218 int builtin_first;
2219 char_u tbuf[TBUFSZ];
2220 char_u tstrbuf[TBUFSZ];
2221 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002222 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223#endif
2224
2225/*
2226 * If the GUI is running or will start in a moment, we only support the keys
2227 * that the GUI can produce.
2228 */
2229#ifdef FEAT_GUI
2230 if (gui.in_use || gui.starting)
2231 return gui_mch_haskey(name);
2232#endif
2233
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002234 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 return OK;
2236
2237 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002238 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 return FAIL;
2240
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002241 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
2243 term += 8;
2244#ifdef HAVE_TGETENT
2245 builtin_first = TRUE;
2246#endif
2247 }
2248#ifdef HAVE_TGETENT
2249 else
2250 builtin_first = p_tbi;
2251#endif
2252
2253#ifdef HAVE_TGETENT
2254/*
2255 * We can get the entry from the builtin termcap and from the external one.
2256 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2257 * builtin termcap first.
2258 * If 'ttybuiltin' is off, try external termcap first.
2259 */
2260 for (i = 0; i < 2; ++i)
2261 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002262 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263#endif
2264 /*
2265 * Search in builtin termcap
2266 */
2267 {
2268 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002269 if (termp->bt_string != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
2271 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002272 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 while (termp->bt_entry != (int)KS_NAME)
2274 {
2275 if ((int)termp->bt_entry == key)
2276 {
2277 add_termcode(name, (char_u *)termp->bt_string,
2278 term_is_8bit(term));
2279 return OK;
2280 }
2281 ++termp;
2282 }
2283 }
2284 }
2285#ifdef HAVE_TGETENT
2286 else
2287 /*
2288 * Search in external termcap
2289 */
2290 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002291 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (error_msg == NULL)
2293 {
2294 string = TGETSTR((char *)name, &tp);
2295 if (string != NULL && *string != NUL)
2296 {
2297 add_termcode(name, string, FALSE);
2298 return OK;
2299 }
2300 }
2301 }
2302 }
2303#endif
2304
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002305 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
2307#ifdef HAVE_TGETENT
2308 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002309 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 else
2311#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002312 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
2314 return FAIL;
2315}
2316
2317 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002318term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
2320 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2321}
2322
2323/*
2324 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2325 * Assume that the terminal is using 8-bit controls when the name contains
2326 * "8bit", like in "xterm-8bit".
2327 */
2328 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002329term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330{
2331 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2332}
2333
2334/*
2335 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002336 * <Esc>[ -> CSI <M_C_[>
2337 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 * <Esc>O -> <M-C-O>
2339 */
2340 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002341term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
2343 if (*p == ESC)
2344 {
2345 if (p[1] == '[')
2346 return CSI;
2347 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002348 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 if (p[1] == 'O')
2350 return 0x8f;
2351 }
2352 return 0;
2353}
2354
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002355#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002357term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
2359 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2360}
2361#endif
2362
2363#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2364
2365 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002366tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
2368 static char_u buf[16];
2369 char_u *p;
2370
2371 p = buf + 15;
2372 *p = '\0';
2373 do
2374 {
2375 --p;
2376 *p = (char_u) (i % 10 + '0');
2377 i /= 10;
2378 }
2379 while (i > 0 && p > buf);
2380 return p;
2381}
2382#endif
2383
2384#ifndef HAVE_TGETENT
2385
2386/*
2387 * minimal tgoto() implementation.
2388 * no padding and we only parse for %i %d and %+char
2389 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002390 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002391tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392{
2393 static char buf[30];
2394 char *p, *s, *e;
2395
2396 if (!cm)
2397 return "OOPS";
2398 e = buf + 29;
2399 for (s = buf; s < e && *cm; cm++)
2400 {
2401 if (*cm != '%')
2402 {
2403 *s++ = *cm;
2404 continue;
2405 }
2406 switch (*++cm)
2407 {
2408 case 'd':
2409 p = (char *)tltoa((unsigned long)y);
2410 y = x;
2411 while (*p)
2412 *s++ = *p++;
2413 break;
2414 case 'i':
2415 x++;
2416 y++;
2417 break;
2418 case '+':
2419 *s++ = (char)(*++cm + y);
2420 y = x;
2421 break;
2422 case '%':
2423 *s++ = *cm;
2424 break;
2425 default:
2426 return "OOPS";
2427 }
2428 }
2429 *s = '\0';
2430 return buf;
2431}
2432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002433#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435/*
2436 * Set the terminal name and initialize the terminal options.
2437 * If "name" is NULL or empty, get the terminal name from the environment.
2438 * If that fails, use the default terminal name.
2439 */
2440 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002441termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442{
2443 char_u *term;
2444
2445 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002446 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 term = name;
2448
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449#ifndef MSWIN
2450 if (term == NULL)
2451 term = mch_getenv((char_u *)"TERM");
2452#endif
2453 if (term == NULL || *term == NUL)
2454 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002455 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002457 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 set_string_default("term", term);
2459 set_string_default("ttytype", term);
2460
2461 /*
2462 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2463 */
2464 set_termname(T_NAME != NULL ? T_NAME : term);
2465}
2466
2467/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002468 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002470#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002471
2472// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002474
2475static int out_pos = 0; // number of chars in out_buf
2476
2477// Since the maximum number of SGR parameters shown as a normal value range is
2478// 16, the escape sequence length can be 4 * 16 + lead + tail.
2479#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481/*
2482 * out_flush(): flush the output buffer
2483 */
2484 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002485out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486{
2487 int len;
2488
2489 if (out_pos != 0)
2490 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002491 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 len = out_pos;
2493 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002494 ui_write(out_buf, len, FALSE);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002495#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002496 if (ch_log_output != FALSE)
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002497 {
2498 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002499 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002500# ifdef FEAT_GUI
2501 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2502# endif
2503 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002504 out_buf);
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002505 if (ch_log_output == TRUE)
2506 ch_log_output = FALSE; // only log once
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002507 }
2508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 }
2510}
2511
Bram Moolenaara338adc2018-01-31 20:51:47 +01002512/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002513 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2514 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002515 */
2516 void
2517out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002518 int force UNUSED, // when TRUE, update cursor even when not moved
2519 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002520{
2521 mch_disable_flush();
2522 out_flush();
2523 mch_enable_flush();
2524#ifdef FEAT_GUI
2525 if (gui.in_use)
2526 {
2527 gui_update_cursor(force, clear_selection);
2528 gui_may_flush();
2529 }
2530#endif
2531}
2532
2533
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534/*
2535 * Sometimes a byte out of a multi-byte character is written with out_char().
2536 * To avoid flushing half of the character, call this function first.
2537 */
2538 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002539out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2542 out_flush();
2543}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
2545#ifdef FEAT_GUI
2546/*
2547 * out_trash(): Throw away the contents of the output buffer
2548 */
2549 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002550out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 out_pos = 0;
2553}
2554#endif
2555
2556/*
2557 * out_char(c): put a byte into the output buffer.
2558 * Flush it if it becomes full.
2559 * This should not be used for outputting text on the screen (use functions
2560 * like msg_puts() and screen_putchar() for that).
2561 */
2562 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002563out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
Bram Moolenaard0573012017-10-28 21:11:06 +02002565#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002566 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 out_char('\r');
2568#endif
2569
2570 out_buf[out_pos++] = c;
2571
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002572 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (out_pos >= OUT_SIZE || p_wd)
2574 out_flush();
2575}
2576
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002578 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002580 static int
2581out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002583 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585 if (out_pos >= OUT_SIZE)
2586 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002587 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588}
2589
2590/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002591 * A never-padding out_str().
2592 * Use this whenever you don't want to run the string through tputs().
2593 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 * is likely to strip off leading digits, that it mistakes for padding
2595 * information, and "%i", "%d", etc.
2596 * This should only be used for writing terminal codes, not for outputting
2597 * normal text (use functions like msg_puts() and screen_putchar() for that).
2598 */
2599 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002600out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002602 // avoid terminal strings being split up
2603 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 while (*s)
2607 out_char_nf(*s++);
2608
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002609 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 if (p_wd)
2611 out_flush();
2612}
2613
2614/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002615 * A conditional-flushing out_str, mainly for visualbell.
2616 * Handles a delay internally, because termlib may not respect the delay or do
2617 * it at the wrong time.
2618 * Note: Only for terminal strings.
2619 */
2620 void
2621out_str_cf(char_u *s)
2622{
2623 if (s != NULL && *s)
2624 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002625#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002626 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002627#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002628
2629#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002630 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002631 if (gui.in_use)
2632 {
2633 out_str_nf(s);
2634 return;
2635 }
2636#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002637 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002638 out_flush();
2639#ifdef HAVE_TGETENT
2640 for (p = s; *s; ++s)
2641 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002642 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002643 if (*s == '$' && *(s + 1) == '<')
2644 {
2645 char_u save_c = *s;
2646 int duration = atoi((char *)s + 2);
2647
2648 *s = NUL;
2649 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2650 *s = save_c;
2651 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002652# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002653 // Only sleep here if we can limit this happening in
2654 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002655 p = vim_strchr(s, '>');
2656 if (p == NULL || duration <= 0)
2657 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002658 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002659 p = s;
2660 }
2661 else
2662 {
2663 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002664 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002665 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002666# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002667 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002668 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002669# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002670 break;
2671 }
2672 }
2673 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2674#else
2675 while (*s)
2676 out_char_nf(*s++);
2677#endif
2678
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002679 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002680 if (p_wd)
2681 out_flush();
2682 }
2683}
2684
2685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002687 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 * This should only be used for writing terminal codes, not for outputting
2689 * normal text (use functions like msg_puts() and screen_putchar() for that).
2690 */
2691 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002692out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 if (s != NULL && *s)
2695 {
2696#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002697 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 if (gui.in_use)
2699 {
2700 out_str_nf(s);
2701 return;
2702 }
2703#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002704 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002705 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 out_flush();
2707#ifdef HAVE_TGETENT
2708 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2709#else
2710 while (*s)
2711 out_char_nf(*s++);
2712#endif
2713
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002714 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 if (p_wd)
2716 out_flush();
2717 }
2718}
2719
2720/*
2721 * cursor positioning using termcap parser. (jw)
2722 */
2723 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002724term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
2726 OUT_STR(tgoto((char *)T_CM, col, row));
2727}
2728
2729 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002730term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
2732 OUT_STR(tgoto((char *)T_CRI, 0, i));
2733}
2734
2735 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002736term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737{
2738 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2739}
2740
2741 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002742term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743{
2744 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2745}
2746
2747#if defined(HAVE_TGETENT) || defined(PROTO)
2748 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002749term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002751 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 if (x < 0)
2753 x = 0;
2754 if (y < 0)
2755 y = 0;
2756 OUT_STR(tgoto((char *)T_CWP, y, x));
2757}
2758
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002759# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2760/*
2761 * Return TRUE if we can request the terminal for a response.
2762 */
2763 static int
2764can_get_termresponse()
2765{
2766 return cur_tmode == TMODE_RAW
2767 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002768# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002769 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002770# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002771 && p_ek;
2772}
2773
Bram Moolenaarafd78262019-05-10 23:10:31 +02002774/*
2775 * Set "status" to STATUS_SENT.
2776 */
2777 static void
2778termrequest_sent(termrequest_T *status)
2779{
2780 status->tr_progress = STATUS_SENT;
2781 status->tr_start = time(NULL);
2782}
2783
2784/*
2785 * Return TRUE if any of the requests are in STATUS_SENT.
2786 */
2787 static int
2788termrequest_any_pending()
2789{
2790 int i;
2791 time_t now = time(NULL);
2792
2793 for (i = 0; all_termrequests[i] != NULL; ++i)
2794 {
2795 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2796 {
2797 if (all_termrequests[i]->tr_start > 0 && now > 0
2798 && all_termrequests[i]->tr_start + 2 < now)
2799 // Sent the request more than 2 seconds ago and didn't get a
2800 // response, assume it failed.
2801 all_termrequests[i]->tr_progress = STATUS_FAIL;
2802 else
2803 return TRUE;
2804 }
2805 }
2806 return FALSE;
2807}
2808
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002809static int winpos_x = -1;
2810static int winpos_y = -1;
2811static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002812
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002813# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002814/*
2815 * Try getting the Vim window position from the terminal.
2816 * Returns OK or FAIL.
2817 */
2818 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002819term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002820{
2821 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002822 int prev_winpos_x = winpos_x;
2823 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002824
2825 if (*T_CGP == NUL || !can_get_termresponse())
2826 return FAIL;
2827 winpos_x = -1;
2828 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002829 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002830 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002831 OUT_STR(T_CGP);
2832 out_flush();
2833
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002834 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002835 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002836 {
2837 (void)vpeekc_nomap();
2838 if (winpos_x >= 0 && winpos_y >= 0)
2839 {
2840 *x = winpos_x;
2841 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002842 return OK;
2843 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002844 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002845 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002846 // Do not reset "did_request_winpos", if we timed out the response might
2847 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002848
2849 winpos_x = prev_winpos_x;
2850 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002851 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002852 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002853 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002854 *x = winpos_x;
2855 *y = winpos_y;
2856 return OK;
2857 }
2858
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002859 return FALSE;
2860}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002861# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002862# endif
2863
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002865term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002867 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868}
2869#endif
2870
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002872term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
2874 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002875 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002876 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002878 // Special handling of 16 colors, because termcap can't handle it
2879 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
2880 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002882 && ((s[0] == ESC && s[1] == '[')
2883#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2884 || (s[0] == ESC && s[1] == '|')
2885#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02002886 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 && s[i] != NUL
2888 && (STRCMP(s + i + 1, "%p1%dm") == 0
2889 || STRCMP(s + i + 1, "%dm") == 0)
2890 && (s[i] == '3' || s[i] == '4'))
2891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002893 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002895 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002897 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002898#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002899 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002900#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002901 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02002902 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2903 : (n >= 16 ? "48;5;" : "10");
2904
2905 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2907 }
2908 else
2909 OUT_STR(tgoto((char *)s, 0, n));
2910}
2911
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002912 void
2913term_fg_color(int n)
2914{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002915 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002916 if (*T_CAF)
2917 term_color(T_CAF, n);
2918 else if (*T_CSF)
2919 term_color(T_CSF, n);
2920}
2921
2922 void
2923term_bg_color(int n)
2924{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002925 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002926 if (*T_CAB)
2927 term_color(T_CAB, n);
2928 else if (*T_CSB)
2929 term_color(T_CSB, n);
2930}
2931
Bram Moolenaare023e882020-05-31 16:42:30 +02002932 void
2933term_ul_color(int n)
2934{
2935 if (*T_CAU)
2936 term_color(T_CAU, n);
2937}
2938
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002939/*
2940 * Return "dark" or "light" depending on the kind of terminal.
2941 * This is just guessing! Recognized are:
2942 * "linux" Linux console
2943 * "screen.linux" Linux console with screen
2944 * "cygwin.*" Cygwin shell
2945 * "putty.*" Putty program
2946 * We also check the COLORFGBG environment variable, which is set by
2947 * rxvt and derivatives. This variable contains either two or three
2948 * values separated by semicolons; we want the last value in either
2949 * case. If this value is 0-6 or 8, our background is dark.
2950 */
2951 char_u *
2952term_bg_default(void)
2953{
2954#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002955 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002956 return (char_u *)"dark";
2957#else
2958 char_u *p;
2959
2960 if (STRCMP(T_NAME, "linux") == 0
2961 || STRCMP(T_NAME, "screen.linux") == 0
2962 || STRNCMP(T_NAME, "cygwin", 6) == 0
2963 || STRNCMP(T_NAME, "putty", 5) == 0
2964 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
2965 && (p = vim_strrchr(p, ';')) != NULL
2966 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
2967 && p[2] == NUL))
2968 return (char_u *)"dark";
2969 return (char_u *)"light";
2970#endif
2971}
2972
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002973#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002974
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002975#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2976#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2977#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002978
2979 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002980term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002981{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002982#define MAX_COLOR_STR_LEN 100
2983 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002984
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002985 if (*s == NUL)
2986 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002987 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002988 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02002989#ifdef FEAT_VTP
2990 if (use_wt())
2991 {
2992 out_flush();
2993 buf[1] = '[';
2994 vtp_printf(buf);
2995 }
2996 else
2997#endif
2998 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002999}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003000
3001 void
3002term_fg_rgb_color(guicolor_T rgb)
3003{
3004 term_rgb_color(T_8F, rgb);
3005}
3006
3007 void
3008term_bg_rgb_color(guicolor_T rgb)
3009{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003010 if (rgb != INVALCOLOR)
3011 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003012}
Bram Moolenaare023e882020-05-31 16:42:30 +02003013
3014 void
3015term_ul_rgb_color(guicolor_T rgb)
3016{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003017# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003018 // If the user explicitly sets t_8u then use it. Otherwise wait for
3019 // termresponse to be received, which is when t_8u would be set and a
3020 // redraw is needed if it was used.
3021 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003022 write_t_8u_state = MAYBE;
3023 else
3024# endif
3025 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003026}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003027#endif
3028
Bram Moolenaar651fca82021-11-29 20:39:38 +00003029#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030/*
3031 * Generic function to set window title, using t_ts and t_fs.
3032 */
3033 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003034term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003036 MAY_WANT_TO_LOG_THIS;
3037
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003038 // t_ts takes one argument: column in status line
3039 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003041 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 out_flush();
3043}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003044
3045/*
3046 * Tell the terminal to push (save) the title and/or icon, so that it can be
3047 * popped (restored) later.
3048 */
3049 void
3050term_push_title(int which)
3051{
Bram Moolenaar27821262019-05-08 16:41:09 +02003052 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003053 {
3054 OUT_STR(T_CST);
3055 out_flush();
3056 }
3057
Bram Moolenaar27821262019-05-08 16:41:09 +02003058 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003059 {
3060 OUT_STR(T_SSI);
3061 out_flush();
3062 }
3063}
3064
3065/*
3066 * Tell the terminal to pop the title and/or icon.
3067 */
3068 void
3069term_pop_title(int which)
3070{
Bram Moolenaar27821262019-05-08 16:41:09 +02003071 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003072 {
3073 OUT_STR(T_CRT);
3074 out_flush();
3075 }
3076
Bram Moolenaar27821262019-05-08 16:41:09 +02003077 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003078 {
3079 OUT_STR(T_SRI);
3080 out_flush();
3081 }
3082}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083#endif
3084
3085/*
3086 * Make sure we have a valid set or terminal options.
3087 * Replace all entries that are NULL by empty_option
3088 */
3089 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003090ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003092 char_u *env_colors;
3093
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003094 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
3096 /*
3097 * MUST have "cm": cursor motion.
3098 */
3099 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003100 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101
3102 /*
3103 * if "cs" defined, use a scroll region, it's faster.
3104 */
3105 if (*T_CS != NUL)
3106 scroll_region = TRUE;
3107 else
3108 scroll_region = FALSE;
3109
3110 if (pairs)
3111 {
3112 /*
3113 * optional pairs
3114 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003115 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 if (*T_ME == NUL)
3117 T_ME = T_MR = T_MD = T_MB = empty_option;
3118 if (*T_SO == NUL || *T_SE == NUL)
3119 T_SO = T_SE = empty_option;
3120 if (*T_US == NUL || *T_UE == NUL)
3121 T_US = T_UE = empty_option;
3122 if (*T_CZH == NUL || *T_CZR == NUL)
3123 T_CZH = T_CZR = empty_option;
3124
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003125 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 if (*T_VE == NUL)
3127 T_VI = empty_option;
3128
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003129 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 if (*T_ME == NUL)
3131 {
3132 T_ME = T_SE;
3133 T_MR = T_SO;
3134 T_MD = T_SO;
3135 }
3136
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003137 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 if (*T_SO == NUL)
3139 {
3140 T_SE = T_ME;
3141 if (*T_MR == NUL)
3142 T_SO = T_MD;
3143 else
3144 T_SO = T_MR;
3145 }
3146
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003147 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 if (*T_CZH == NUL)
3149 {
3150 T_CZR = T_ME;
3151 if (*T_MR == NUL)
3152 T_CZH = T_MD;
3153 else
3154 T_CZH = T_MR;
3155 }
3156
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003157 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 if (*T_CSB == NUL || *T_CSF == NUL)
3159 {
3160 T_CSB = empty_option;
3161 T_CSF = empty_option;
3162 }
3163
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003164 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 if (*T_CAB == NUL || *T_CAF == NUL)
3166 {
3167 T_CAB = empty_option;
3168 T_CAF = empty_option;
3169 }
3170
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003171 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003173 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003175 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 p_wiv = (*T_XS != NUL);
3177 }
3178 need_gather = TRUE;
3179
Bram Moolenaar759d8152020-04-26 16:52:49 +02003180 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3181 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003183#ifdef FEAT_GUI
3184 if (!gui.in_use)
3185#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003186 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003187 env_colors = mch_getenv((char_u *)"COLORS");
3188 if (env_colors != NULL && isdigit(*env_colors))
3189 {
3190 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003191
Bram Moolenaar759d8152020-04-26 16:52:49 +02003192 if (colors != t_colors)
3193 set_color_count(colors);
3194 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196}
3197
3198#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3199 || defined(PROTO)
3200/*
3201 * Represent the given long_u as individual bytes, with the most significant
3202 * byte first, and store them in dst.
3203 */
3204 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003205add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206{
3207 int i;
3208 int shift;
3209
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003210 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
3212 shift = 8 * (sizeof(long_u) - i);
3213 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3214 }
3215}
3216
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217/*
3218 * Interpret the next string of bytes in buf as a long integer, with the most
3219 * significant byte first. Note that it is assumed that buf has been through
3220 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3221 * Puts result in val, and returns the number of bytes read from buf
3222 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3223 * were present.
3224 */
3225 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003226get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227{
3228 int len;
3229 char_u bytes[sizeof(long_u)];
3230 int i;
3231 int shift;
3232
3233 *val = 0;
3234 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3235 if (len != -1)
3236 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003237 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
3239 shift = 8 * (sizeof(long_u) - 1 - i);
3240 *val += (long_u)bytes[i] << shift;
3241 }
3242 }
3243 return len;
3244}
3245#endif
3246
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247/*
3248 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3249 * that buf has been through inchar(). Returns the actual number of bytes used
3250 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3251 * available.
3252 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003253 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003254get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
3256 int len = 0;
3257 int i;
3258 char_u c;
3259
3260 for (i = 0; i < num_bytes; i++)
3261 {
3262 if ((c = buf[len++]) == NUL)
3263 return -1;
3264 if (c == K_SPECIAL)
3265 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003266 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 return -1;
3268 if (buf[len++] == (int)KS_ZERO)
3269 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003270 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3271 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003272 if (buf[len++] == (int)KE_CSI)
3273 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003275 else if (c == CSI && buf[len] == KS_EXTRA
3276 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003277 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3278 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003279 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 bytes[i] = c;
3281 }
3282 return len;
3283}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284
3285/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003286 * Check if the new shell size is valid, correct it if it's too small or way
3287 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 */
3289 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003290check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003292 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003294 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003295
3296 // make sure these values are not invalid
3297 if (cmdline_row >= Rows)
3298 cmdline_row = Rows - 1;
3299 if (msg_row >= Rows)
3300 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003301}
3302
3303/*
3304 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3305 */
3306 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003307limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003308{
3309 if (Columns < MIN_COLUMNS)
3310 Columns = MIN_COLUMNS;
3311 else if (Columns > 10000)
3312 Columns = 10000;
3313 if (Rows > 1000)
3314 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315}
3316
Bram Moolenaar86b68352004-12-27 21:59:20 +00003317/*
3318 * Invoked just before the screen structures are going to be (re)allocated.
3319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003321win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
3323 static int old_Rows = 0;
3324 static int old_Columns = 0;
3325
3326 if (old_Rows != Rows || old_Columns != Columns)
3327 ui_new_shellsize();
3328 if (old_Rows != Rows)
3329 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003330 // If 'window' uses the whole screen, keep it using that.
3331 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003332 if (p_window == old_Rows - 1
3333 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003334 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003336 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
3338 if (old_Columns != Columns)
3339 {
3340 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003341 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 }
3343}
3344
3345/*
3346 * Call this function when the Vim shell has been resized in any way.
3347 * Will obtain the current size and redraw (also when size didn't change).
3348 */
3349 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003350shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
3352 set_shellsize(0, 0, FALSE);
3353}
3354
3355/*
3356 * Check if the shell size changed. Handle a resize.
3357 * When the size didn't change, nothing happens.
3358 */
3359 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003360shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
3362 int old_Rows = Rows;
3363 int old_Columns = Columns;
3364
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003365 if (!exiting
3366#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003367 // Do not get the size when executing a shell command during
3368 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003369 && !gui.starting
3370#endif
3371 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003372 {
3373 (void)ui_get_shellsize();
3374 check_shellsize();
3375 if (old_Rows != Rows || old_Columns != Columns)
3376 shell_resized();
3377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378}
3379
3380/*
3381 * Set size of the Vim shell.
3382 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3383 * window size (this is used for the :win command).
3384 * If 'mustset' is FALSE, we may try to get the real window size and if
3385 * it fails use 'width' and 'height'.
3386 */
3387 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003388set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389{
3390 static int busy = FALSE;
3391
3392 /*
3393 * Avoid recursiveness, can happen when setting the window size causes
3394 * another window-changed signal.
3395 */
3396 if (busy)
3397 return;
3398
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003399 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 return;
3401
Bram Moolenaar24959102022-05-07 20:01:16 +01003402 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003404 // postpone the resizing
Bram Moolenaar24959102022-05-07 20:01:16 +01003405 State = MODE_SETWSIZE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 return;
3407 }
3408
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003409 if (updating_screen)
3410 // resizing while in update_screen() may cause a crash
3411 return;
3412
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003413 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003414 // buffer (or window) has already been closed and removing a scrollbar
3415 // causes a resize event. Don't resize then, it will happen after entering
3416 // another buffer.
3417 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003418 return;
3419
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 ++busy;
3421
3422#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003423 out_flush(); // must do this before mch_get_shellsize() for
3424 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425#endif
3426
3427 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3428 {
3429 Rows = height;
3430 Columns = width;
3431 check_shellsize();
3432 ui_set_shellsize(mustset);
3433 }
3434 else
3435 check_shellsize();
3436
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003437 // The window layout used to be adjusted here, but it now happens in
3438 // screenalloc() (also invoked from screenclear()). That is because the
3439 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
Bram Moolenaar24959102022-05-07 20:01:16 +01003441 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3442 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 screenclear();
3444 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003445 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 if (starting != NO_SCREEN)
3448 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003450
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 changed_line_abv_curs();
3452 invalidate_botline();
3453
3454 /*
3455 * We only redraw when it's needed:
3456 * - While at the more prompt or executing an external command, don't
3457 * redraw, but position the cursor.
3458 * - While editing the command line, only redraw that.
3459 * - in Ex mode, don't redraw anything.
3460 * - Otherwise, redraw right now, and position the cursor.
3461 * Always need to call update_screen() or screenalloc(), to make
3462 * sure Rows/Columns and the size of ScreenLines[] is correct!
3463 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003464 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3465 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
3467 screenalloc(FALSE);
3468 repeat_message();
3469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 else
3471 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003472 if (curwin->w_p_scb)
3473 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003474 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003475 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003476 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003477 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003478 }
3479 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003480 {
3481 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003482 if (pum_visible())
3483 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003484 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003485 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003486 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003487 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003488 if (redrawing())
3489 setcursor();
3490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003492 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494 out_flush();
3495 --busy;
3496}
3497
3498/*
3499 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3500 * commands and Ex mode).
3501 */
3502 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003503settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
3505#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003506 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 if (gui.in_use)
3508 return;
3509#endif
3510
3511 if (full_screen)
3512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003514 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3515 * set the terminal to raw mode, even though we think it already is,
3516 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 * When we think the terminal is normal, don't try to set it to
3518 * normal again, because that causes problems (logout!) on some
3519 * machines.
3520 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003521 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
3523#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003524# ifdef FEAT_GUI
3525 if (!gui.in_use && !gui.starting)
3526# endif
3527 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003528 // May need to check for T_CRV response and termcodes, it
3529 // doesn't work in Cooked mode, an external program may get
3530 // them.
3531 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003532 (void)vpeekc_nomap();
3533 check_for_codes_from_term();
3534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003537 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003538
3539 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3540 // Avoid doing this too often, on some terminals the codes are not
3541 // handled properly.
3542 if (termcap_active && tmode != TMODE_SLEEP
3543 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003544 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003545 MAY_WANT_TO_LOG_THIS;
3546
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003547 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003548 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003549 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003550 out_str(T_CTE); // possibly disables modifyOtherKeys
3551 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003552 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003553 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003554 out_str(T_BE); // enable bracketed paste mode (should
3555 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003556 out_str(T_CTI); // possibly enables modifyOtherKeys
3557 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003560 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003563 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 out_flush();
3565 }
3566#ifdef FEAT_TERMRESPONSE
3567 may_req_termresponse();
3568#endif
3569 }
3570}
3571
3572 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003573starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
3575 if (full_screen && !termcap_active)
3576 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003577 MAY_WANT_TO_LOG_THIS;
3578
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003579 out_str(T_TI); // start termcap mode
3580 out_str(T_CTI); // start "raw" mode
3581 out_str(T_KS); // start "keypad transmit" mode
3582 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003583
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003584#if defined(UNIX) || defined(VMS)
3585 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003586 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003587 out_str(T_FE);
3588#endif
3589
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 out_flush();
3591 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003592 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003594# ifdef FEAT_GUI
3595 if (!gui.in_use && !gui.starting)
3596# endif
3597 {
3598 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003599 // Immediately check for a response. If t_Co changes, we don't
3600 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003601 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003602 check_for_codes_from_term();
3603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604#endif
3605 }
3606}
3607
3608 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003609stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
3611 screen_stop_highlight();
3612 reset_cterm_colors();
3613 if (termcap_active)
3614 {
3615#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003616# ifdef FEAT_GUI
3617 if (!gui.in_use && !gui.starting)
3618# endif
3619 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003620 // May need to discard T_CRV, T_U7 or T_RBG response.
3621 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003622 {
3623# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003624 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003625 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003626# endif
3627# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003628 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003629 if (exiting)
3630 tcflush(fileno(stdin), TCIFLUSH);
3631# endif
3632 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003633 // Check for termcodes first, otherwise an external program may
3634 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003635 check_for_codes_from_term();
3636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003638 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003639
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003640#if defined(UNIX) || defined(VMS)
3641 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003642 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003643 out_str(T_FD);
3644#endif
3645
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003646 out_str(T_BD); // disable bracketed paste mode
3647 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 out_flush();
3649 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003650 cursor_on(); // just in case it is still off
3651 out_str(T_CTE); // stop "raw" mode
3652 out_str(T_TE); // stop termcap mode
3653 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 out_flush();
3655 }
3656}
3657
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003658#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659/*
3660 * Request version string (for xterm) when needed.
3661 * Only do this after switching to raw mode, otherwise the result will be
3662 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003663 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003664 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 * Only do this after termcap mode has been started, otherwise the codes for
3666 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003667 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3668 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003669 * On Unix only do it when both output and input are a tty (avoid writing
3670 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 * The result is caught in check_termcode().
3672 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003673 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003674may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003676 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003677 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003678 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 && *T_CRV != NUL)
3680 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003681 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003682 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003684 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003685 // check for the characters now, otherwise they might be eaten by
3686 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 out_flush();
3688 (void)vpeekc_nomap();
3689 }
3690}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003691
Bram Moolenaar9584b312013-03-13 19:29:28 +01003692/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003693 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3694 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003695 * Note that this goes out before T_CRV, so that the result can be used when
3696 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003697 */
3698 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003699check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003700{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003701 int did_send = FALSE;
3702
3703 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3704 return;
3705
Bram Moolenaarafd78262019-05-10 23:10:31 +02003706 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003707 && !option_was_set((char_u *)"ambiwidth"))
3708 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003709 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003710
Bram Moolenaara45551a2020-06-09 15:57:37 +02003711 // Ambiguous width check.
3712 // Check how the terminal treats ambiguous character width (UAX #11).
3713 // First, we move the cursor to (1, 0) and print a test ambiguous
3714 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3715 // the current cursor position. If the terminal treats \u25bd as
3716 // single width, the position is (1, 1), or if it is treated as double
3717 // width, that will be (1, 2). This function has the side effect that
3718 // changes cursor position, so it must be called immediately after
3719 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003720 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003721 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003722 // Do this in the second row. In the first row the returned sequence
3723 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003724 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003725 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003726 out_str(buf);
3727 out_str(T_U7);
3728 termrequest_sent(&u7_status);
3729 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003730 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003731
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003732 // This overwrites a few characters on the screen, a redraw is needed
3733 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003734 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003735 term_windgoto(1, 0);
3736 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003737 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003738 }
3739
Bram Moolenaard1f34e62022-01-07 19:24:20 +00003740 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02003741 {
3742 // 2. Check compatibility with xterm.
3743 // We move the cursor to (2, 0), print a test sequence and then query
3744 // the current cursor position. If the terminal properly handles
3745 // unknown DCS string and CSI sequence with intermediate byte, the test
3746 // sequence is ignored and the cursor does not move. If the terminal
3747 // handles test sequence incorrectly, a garbage string is displayed and
3748 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003749 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003750 LOG_TR(("Sending xterm compatibility test sequence."));
3751 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003752 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003753 term_windgoto(2, 0);
3754 // send the test DCS string.
3755 out_str((char_u *)"\033Pzz\033\\");
3756 // send the test CSI sequence with intermediate byte.
3757 out_str((char_u *)"\033[0%m");
3758 out_str(T_U7);
3759 termrequest_sent(&xcc_status);
3760 out_flush();
3761 did_send = TRUE;
3762
3763 // If the terminal handles test sequence incorrectly, garbage text is
3764 // displayed. Clear them out for now.
3765 screen_stop_highlight();
3766 term_windgoto(2, 0);
3767 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003768 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003769 }
3770
3771 if (did_send)
3772 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003773 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003774
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003775 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003776 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003777
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003778 // check for the characters now, otherwise they might be eaten by
3779 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003780 out_flush();
3781 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003782 }
3783}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003784
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003785/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003786 * Similar to requesting the version string: Request the terminal background
3787 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003788 */
3789 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003790may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003791{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003792 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003793 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003794 int didit = FALSE;
3795
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003796# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003797 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003798 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003799 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003800 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003801 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003802 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003803 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003804 didit = TRUE;
3805 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003806# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003807
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003808 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003809 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003810 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003811 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003812 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003813 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003814 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003815 didit = TRUE;
3816 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003817
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003818 if (didit)
3819 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003820 // check for the characters now, otherwise they might be eaten by
3821 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003822 out_flush();
3823 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003824 }
3825 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003826}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003827
Bram Moolenaar2951b772013-07-03 12:45:31 +02003828# ifdef DEBUG_TERMRESPONSE
3829 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003830log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003831{
3832 static FILE *fd_tr = NULL;
3833 static proftime_T start;
3834 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003835 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003836
3837 if (fd_tr == NULL)
3838 {
3839 fd_tr = fopen("termresponse.log", "w");
3840 profile_start(&start);
3841 }
3842 now = start;
3843 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003844 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003845 must_redraw == UPD_NOT_VALID ? "NV"
3846 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02003847 va_start(ap, fmt);
3848 vfprintf(fd_tr, fmt, ap);
3849 va_end(ap);
3850 fputc('\n', fd_tr);
3851 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003852}
3853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854#endif
3855
3856/*
3857 * Return TRUE when saving and restoring the screen.
3858 */
3859 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003860swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861{
3862 return (full_screen && *T_TI != NUL);
3863}
3864
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865/*
3866 * By outputting the 'cursor very visible' termcap code, for some windowed
3867 * terminals this makes the screen scrolled to the correct position.
3868 * Used when starting Vim or returning from a shell.
3869 */
3870 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003871scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003873 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003875 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003877 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003878 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 }
3880}
3881
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003882// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883static int cursor_is_off = FALSE;
3884
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003885// True if cursor is not visible due to an ongoing cursor-less sleep
3886static int cursor_is_asleep = FALSE;
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003889 * Enable the cursor without checking if it's already enabled.
3890 */
3891 void
3892cursor_on_force(void)
3893{
3894 out_str(T_VE);
3895 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003896 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02003897}
3898
3899/*
3900 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 */
3902 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003903cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003905 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003906 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907}
3908
3909/*
3910 * Disable the cursor.
3911 */
3912 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003913cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003915 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003917 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 cursor_is_off = TRUE;
3919 }
3920}
3921
Dominique Pelle748b3082022-01-08 12:41:16 +00003922#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003923/*
3924 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
3925 */
3926 int
3927cursor_is_sleeping(void)
3928{
3929 return cursor_is_asleep;
3930}
Dominique Pelle748b3082022-01-08 12:41:16 +00003931#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003932
3933/*
3934 * Disable the cursor and mark it disabled by cursor-less sleep
3935 */
3936 void
3937cursor_sleep(void)
3938{
3939 cursor_is_asleep = TRUE;
3940 cursor_off();
3941}
3942
3943/*
3944 * Enable the cursor and mark it not disabled by cursor-less sleep
3945 */
3946 void
3947cursor_unsleep(void)
3948{
3949 cursor_is_asleep = FALSE;
3950 cursor_on();
3951}
3952
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003953#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003955 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003956 */
3957 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003958term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003959{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003960 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003961 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003962
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003963 // Only do something when redrawing the screen and we can restore the
3964 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003965 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003966 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003967# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003968 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003969 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003970 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003971# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003972 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003973 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003974
Bram Moolenaar24959102022-05-07 20:01:16 +01003975 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003976 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003977 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003978 {
3979 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003980 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003981 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003982 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003983 if (*p != NUL)
3984 {
3985 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01003986 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003987 }
3988 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003989 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003990 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003991 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003992 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003993 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003994 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01003995 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003996 }
3997 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003998 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003999 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004000 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01004001 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004002 }
4003}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004004
4005# if defined(FEAT_TERMINAL) || defined(PROTO)
4006 void
4007term_cursor_color(char_u *color)
4008{
4009 if (*T_CSC != NUL)
4010 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004011 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004012 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004013 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004014 out_flush();
4015 }
4016}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004017# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004018
Bram Moolenaar4db25542017-08-28 22:43:05 +02004019 int
4020blink_state_is_inverted()
4021{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004022#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004023 return rbm_status.tr_progress == STATUS_GOT
4024 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004025 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004026#else
4027 return FALSE;
4028#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004029}
4030
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004031/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004032 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004033 */
4034 void
4035term_cursor_shape(int shape, int blink)
4036{
4037 if (*T_CSH != NUL)
4038 {
4039 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4040 out_flush();
4041 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004042 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004043 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004044 int do_blink = blink;
4045
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004046 // t_SH is empty: try setting just the blink state.
4047 // The blink flags are XORed together, if the initial blinking from
4048 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004049 if (blink_state_is_inverted())
4050 do_blink = !blink;
4051
4052 if (do_blink && *T_VS != NUL)
4053 {
4054 out_str(T_VS);
4055 out_flush();
4056 }
4057 else if (!do_blink && *T_CVS != NUL)
4058 {
4059 out_str(T_CVS);
4060 out_flush();
4061 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004062 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004063}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004064#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004065
4066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 * Set scrolling region for window 'wp'.
4068 * The region starts 'off' lines from the start of the window.
4069 * Also set the vertical scroll region for a vertically split window. Always
4070 * the full width of the window, excluding the vertical separator.
4071 */
4072 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004073scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074{
4075 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4076 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004078 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4079 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004080 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081}
4082
4083/*
4084 * Reset scrolling region to the whole screen.
4085 */
4086 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004087scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088{
4089 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 if (*T_CSV != NUL)
4091 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004092 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093}
4094
4095
4096/*
4097 * List of terminal codes that are currently recognized.
4098 */
4099
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004100static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004102 char_u name[2]; // termcap name of entry
4103 char_u *code; // terminal code (in allocated memory)
4104 int len; // STRLEN(code)
4105 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106} *termcodes = NULL;
4107
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004108static int tc_max_len = 0; // number of entries that termcodes[] can hold
4109static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004111static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004112
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004114clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 while (tc_len > 0)
4117 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004118 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 tc_max_len = 0;
4120
4121#ifdef HAVE_TGETENT
4122 BC = (char *)empty_option;
4123 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004124 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 ospeed = 0;
4126#endif
4127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004128 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129}
4130
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004131#define ATC_FROM_TERM 55
4132
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133/*
4134 * Add a new entry to the list of terminal codes.
4135 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004136 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4137 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 */
4139 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004140add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141{
4142 struct termcode *new_tc;
4143 int i, j;
4144 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004145 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146
4147 if (string == NULL || *string == NUL)
4148 {
4149 del_termcode(name);
4150 return;
4151 }
4152
Bram Moolenaar4f974752019-02-17 17:44:42 +01004153#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004154 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004155#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004156# ifdef VIMDLL
4157 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004158 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004159 else
4160# endif
4161 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 if (s == NULL)
4164 return;
4165
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004166 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004167 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004169 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 s[0] = term_7to8bit(string);
4171 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004172
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004173#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4174# ifdef VIMDLL
4175 if (!gui.in_use)
4176# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004177 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004178 if (s[0] == K_NUL)
4179 {
4180 STRMOVE(s + 1, s);
4181 s[1] = 3;
4182 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004183 }
4184#endif
4185
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004186 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004188 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189
4190 /*
4191 * need to make space for more entries
4192 */
4193 if (tc_len == tc_max_len)
4194 {
4195 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004196 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 if (new_tc == NULL)
4198 {
4199 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004200 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return;
4202 }
4203 for (i = 0; i < tc_len; ++i)
4204 new_tc[i] = termcodes[i];
4205 vim_free(termcodes);
4206 termcodes = new_tc;
4207 }
4208
4209 /*
4210 * Look for existing entry with the same name, it is replaced.
4211 * Look for an existing entry that is alphabetical higher, the new entry
4212 * is inserted in front of it.
4213 */
4214 for (i = 0; i < tc_len; ++i)
4215 {
4216 if (termcodes[i].name[0] < name[0])
4217 continue;
4218 if (termcodes[i].name[0] == name[0])
4219 {
4220 if (termcodes[i].name[1] < name[1])
4221 continue;
4222 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004223 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 */
4225 if (termcodes[i].name[1] == name[1])
4226 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004227 if (flags == ATC_FROM_TERM && (j = termcode_star(
4228 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004229 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004230 // Don't replace ESC[123;*X or ESC O*X with another when
4231 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004232 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004233 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004234 && s[len - 1]
4235 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004236 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004237 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004238 vim_free(s);
4239 return;
4240 }
4241 }
4242 else
4243 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004244 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004245 vim_free(termcodes[i].code);
4246 --tc_len;
4247 break;
4248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250 }
4251 /*
4252 * Found alphabetical larger entry, move rest to insert new entry
4253 */
4254 for (j = tc_len; j > i; --j)
4255 termcodes[j] = termcodes[j - 1];
4256 break;
4257 }
4258
4259 termcodes[i].name[0] = name[0];
4260 termcodes[i].name[1] = name[1];
4261 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004262 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004263
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004264 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4265 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004266 termcodes[i].modlen = 0;
4267 j = termcode_star(s, len);
4268 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004269 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004270 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004271 // For "CSI[@;X" the "@" is not included in "modlen".
4272 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4273 --termcodes[i].modlen;
4274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 ++tc_len;
4276}
4277
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004278/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004279 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004280 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004281 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004282 */
4283 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004284termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004285{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004286 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004287 if (len >= 3 && code[len - 2] == '*')
4288 {
4289 if (len >= 5 && code[len - 3] == ';')
4290 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004291 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004292 return 1;
4293 }
4294 return 0;
4295}
4296
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004298find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299{
4300 int i;
4301
4302 for (i = 0; i < tc_len; ++i)
4303 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4304 return termcodes[i].code;
4305 return NULL;
4306}
4307
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004309get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310{
4311 if (i >= tc_len)
4312 return NULL;
4313 return &termcodes[i].name[0];
4314}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004316/*
4317 * Returns the length of the terminal code at index 'idx'.
4318 */
4319 int
4320get_termcode_len(int idx)
4321{
4322 return termcodes[idx].len;
4323}
4324
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004325 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004326del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327{
4328 int i;
4329
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004330 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 return;
4332
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004333 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
4335 for (i = 0; i < tc_len; ++i)
4336 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4337 {
4338 del_termcode_idx(i);
4339 return;
4340 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004341 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342}
4343
4344 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004345del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346{
4347 int i;
4348
4349 vim_free(termcodes[idx].code);
4350 --tc_len;
4351 for (i = idx; i < tc_len; ++i)
4352 termcodes[i] = termcodes[i + 1];
4353}
4354
4355#ifdef FEAT_TERMRESPONSE
4356/*
4357 * Called when detected that the terminal sends 8-bit codes.
4358 * Convert all 7-bit codes to their 8-bit equivalent.
4359 */
4360 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004361switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362{
4363 int i;
4364 int c;
4365
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004366 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 if (!term_is_8bit(T_NAME))
4368 {
4369 for (i = 0; i < tc_len; ++i)
4370 {
4371 c = term_7to8bit(termcodes[i].code);
4372 if (c != 0)
4373 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004374 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 termcodes[i].code[0] = c;
4376 }
4377 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004378 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004381 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382}
4383#endif
4384
4385#ifdef CHECK_DOUBLE_CLICK
4386static linenr_T orig_topline = 0;
4387# ifdef FEAT_DIFF
4388static int orig_topfill = 0;
4389# endif
4390#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004391#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004393 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 * "orig_topline" is used to avoid detecting a double-click when the window
4395 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4396 */
4397/*
4398 * Set orig_topline. Used when jumping to another window, so that a double
4399 * click still works.
4400 */
4401 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004402set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403{
4404 orig_topline = wp->w_topline;
4405# ifdef FEAT_DIFF
4406 orig_topfill = wp->w_topfill;
4407# endif
4408}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004409
4410/*
4411 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4412 * topline and topfill.
4413 */
4414 int
4415is_mouse_topline(win_T *wp)
4416{
4417 return orig_topline == wp->w_topline
4418#ifdef FEAT_DIFF
4419 && orig_topfill == wp->w_topfill
4420#endif
4421 ;
4422}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423#endif
4424
4425/*
zeertzjqfc78a032022-04-26 22:11:38 +01004426 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4427 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4428 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004429 * Remove "slen" bytes.
4430 * Returns FAIL for error.
4431 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004432 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004433put_string_in_typebuf(
4434 int offset,
4435 int slen,
4436 char_u *string,
4437 int new_slen,
4438 char_u *buf,
4439 int bufsize,
4440 int *buflen)
4441{
4442 int extra = new_slen - slen;
4443
4444 string[new_slen] = NUL;
4445 if (buf == NULL)
4446 {
4447 if (extra < 0)
4448 // remove matched chars, taking care of noremap
4449 del_typebuf(-extra, offset);
4450 else if (extra > 0)
4451 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004452 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4453 == FAIL)
4454 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004455
4456 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4457 // typebuf.tb_buf[]!
4458 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4459 (size_t)new_slen);
4460 }
4461 else
4462 {
4463 if (extra < 0)
4464 // remove matched characters
4465 mch_memmove(buf + offset, buf + offset - extra,
4466 (size_t)(*buflen + offset + extra));
4467 else if (extra > 0)
4468 {
4469 // Insert the extra space we need. If there is insufficient
4470 // space return -1.
4471 if (*buflen + extra + new_slen >= bufsize)
4472 return FAIL;
4473 mch_memmove(buf + offset + extra, buf + offset,
4474 (size_t)(*buflen - offset));
4475 }
4476 mch_memmove(buf + offset, string, (size_t)new_slen);
4477 *buflen = *buflen + extra + new_slen;
4478 }
4479 return OK;
4480}
4481
4482/*
4483 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4484 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004485 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004486decode_modifiers(int n)
4487{
4488 int code = n - 1;
4489 int modifiers = 0;
4490
4491 if (code & 1)
4492 modifiers |= MOD_MASK_SHIFT;
4493 if (code & 2)
4494 modifiers |= MOD_MASK_ALT;
4495 if (code & 4)
4496 modifiers |= MOD_MASK_CTRL;
4497 if (code & 8)
4498 modifiers |= MOD_MASK_META;
4499 return modifiers;
4500}
4501
4502 static int
4503modifiers2keycode(int modifiers, int *key, char_u *string)
4504{
4505 int new_slen = 0;
4506
4507 if (modifiers != 0)
4508 {
4509 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004510 // make mappings work. This may result in a special key, such as
4511 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004512 *key = simplify_key(*key, &modifiers);
4513 if (modifiers != 0)
4514 {
4515 string[new_slen++] = K_SPECIAL;
4516 string[new_slen++] = (int)KS_MODIFIER;
4517 string[new_slen++] = modifiers;
4518 }
4519 }
4520 return new_slen;
4521}
4522
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004523#ifdef FEAT_TERMRESPONSE
4524/*
4525 * Handle a cursor position report.
4526 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004527 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004528handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004529{
4530 if (arg[0] == 2 && arg[1] >= 2)
4531 {
4532 char *aw = NULL;
4533
4534 LOG_TR(("Received U7 status: %s", tp));
4535 u7_status.tr_progress = STATUS_GOT;
4536 did_cursorhold = TRUE;
4537 if (arg[1] == 2)
4538 aw = "single";
4539 else if (arg[1] == 3)
4540 aw = "double";
4541 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4542 {
4543 // Setting the option causes a screen redraw. Do
4544 // that right away if possible, keeping any
4545 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004546 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004547# ifdef DEBUG_TERMRESPONSE
4548 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004549 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004550
4551 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4552 }
4553# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004554 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004555# endif
4556# ifdef FEAT_EVAL
4557 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4558# endif
4559 }
4560 }
4561 else if (arg[0] == 3)
4562 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004563 int value;
4564
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004565 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004566 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004567
4568 // Third row: xterm compatibility test.
4569 // If the cursor is on the first column then the terminal can handle
4570 // the request for cursor style and blinking.
4571 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4572 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4573 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004574 }
4575}
4576
4577/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004578 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004579 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004580 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004581 */
4582 static void
4583handle_version_response(int first, int *arg, int argc, char_u *tp)
4584{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004585 // The xterm version. It is set to zero when it can't be an actual xterm
4586 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004587 int version = arg[1];
4588
4589 LOG_TR(("Received CRV response: %s", tp));
4590 crv_status.tr_progress = STATUS_GOT;
4591 did_cursorhold = TRUE;
4592
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004593 // Reset terminal properties that are set based on the termresponse.
4594 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004595 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004596 init_term_props(
4597#ifdef FEAT_EVAL
4598 reset_term_props_on_termresponse
4599#else
4600 FALSE
4601#endif
4602 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004603
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004604 // If this code starts with CSI, you can bet that the
4605 // terminal uses 8-bit codes.
4606 if (tp[0] == CSI)
4607 switch_to_8bit();
4608
4609 // Screen sends 40500.
4610 // rxvt sends its version number: "20703" is 2.7.3.
4611 // Ignore it for when the user has set 'term' to xterm,
4612 // even though it's an rxvt.
4613 if (version > 20000)
4614 version = 0;
4615
zeertzjqfc78a032022-04-26 22:11:38 +01004616 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004617 if (first == '>' && argc == 3)
4618 {
4619 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004620
4621 // mintty 2.9.5 sends 77;20905;0c.
4622 // (77 is ASCII 'M' for mintty.)
4623 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004624 {
4625 // mintty can do SGR mouse reporting
4626 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4627 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004628
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004629 // If xterm version >= 141 try to get termcap codes. For other
4630 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004631 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004632 {
4633 LOG_TR(("Enable checking for XT codes"));
4634 check_for_codes = TRUE;
4635 need_gather = TRUE;
4636 req_codes_from_term();
4637 }
4638
4639 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01004640 // Konsole sends 0;115;0 and works the same way
4641 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004642 {
4643 // If run from Vim $COLORS is set to the number of
4644 // colors the terminal supports. Otherwise assume
4645 // 256, libvterm supports even more.
4646 if (mch_getenv((char_u *)"COLORS") == NULL)
4647 may_adjust_color_count(256);
4648 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004649 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004650 }
4651
4652 if (version == 95)
4653 {
4654 // Mac Terminal.app sends 1;95;0
4655 if (arg[0] == 1 && arg[2] == 0)
4656 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004657 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4658 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004659 }
4660 // iTerm2 sends 0;95;0
4661 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004662 {
4663 // iTerm2 can do SGR mouse reporting
4664 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4665 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004666 // old iTerm2 sends 0;95;
4667 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004668 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004669 }
4670
4671 // screen sends 83;40500;0 83 is 'S' in ASCII.
4672 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004673 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004674 // screen supports SGR mouse codes since 4.7.0
4675 if (arg[1] >= 40700)
4676 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4677 else
4678 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4679 }
4680
4681 // If no recognized terminal has set mouse behavior, assume xterm.
4682 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4683 {
4684 // Xterm version 277 supports SGR.
4685 // Xterm version >= 95 supports mouse dragging.
4686 if (version >= 277)
4687 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004688 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004689 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004690 }
4691
4692 // Detect terminals that set $TERM to something like
4693 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004694 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004695 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004696 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004697 // xfce4-terminal sends 1;2802;0.
4698 // screen sends 83;40500;0
4699 // Assuming any version number over 2500 is not an
4700 // xterm (without the limit for rxvt and screen).
4701 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004702 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004703
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004704 else if (version == 136 && arg[2] == 0)
4705 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004706 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004707
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004708 // PuTTY sends 0;136;0
4709 if (arg[0] == 0)
4710 {
4711 // supports sgr-like mouse reporting.
4712 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4713 }
4714 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004715 }
4716
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004717 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
4718 // commented out.
4719 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
4720 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004721
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01004722 // Kitty sends 1;400{version};{secondary-version}
4723 if (arg[0] == 1 && arg[1] >= 4000 && arg[1] <= 4009)
4724 {
4725 term_props[TPR_KITTY].tpr_status = TPR_YES;
4726 term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
4727 }
4728
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004729 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004730 // 30600/40500 is a version number of GNU screen. DA2 support is added
4731 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4732 // compatibility checking does not detect GNU screen.
4733 if (arg[0] == 83 && arg[1] >= 30600)
4734 {
4735 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4736 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4737 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004738
4739 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004740 // 95, so assume anything below 95 is not xterm and hopefully supports
4741 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004742 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004743 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004744
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004745 // Getting the cursor style is only supported properly by xterm since
4746 // version 279 (otherwise it returns 0x18).
4747 if (version < 279)
4748 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4749
4750 /*
4751 * Take action on the detected properties.
4752 */
4753
4754 // Unless the underline RGB color is expected to work, disable "t_8u".
4755 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004756 // This may cause some flicker. Alternative would be to set "t_8u"
4757 // here if the terminal is expected to support it, but that might
4758 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01004759 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
4760 && *T_8U != NUL
4761 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004762 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004763 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4764 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004765 }
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01004766 if (*T_8U != NUL && write_t_8u_state == MAYBE)
4767 // Did skip writing t_8u, a complete redraw is needed.
4768 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01004769 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004770
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004771 // Only set 'ttymouse' automatically if it was not set
4772 // by the user already.
4773 if (!option_was_set((char_u *)"ttym")
4774 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4775 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4776 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004777 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004778 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4779 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4780 }
4781
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004782 // Only request the cursor style if t_SH and t_RS are
4783 // set. Only supported properly by xterm since version
4784 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004785 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004786 // Not for Terminal.app, it can't handle t_RS, it
4787 // echoes the characters to the screen.
4788 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004789 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004790 && *T_CSH != NUL
4791 && *T_CRS != NUL)
4792 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004793 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004794 LOG_TR(("Sending cursor style request"));
4795 out_str(T_CRS);
4796 termrequest_sent(&rcs_status);
4797 need_flush = TRUE;
4798 }
4799
4800 // Only request the cursor blink mode if t_RC set. Not
4801 // for Gnome terminal, it can't handle t_RC, it
4802 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004803 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004804 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004805 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004806 && *T_CRC != NUL)
4807 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004808 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004809 LOG_TR(("Sending cursor blink mode request"));
4810 out_str(T_CRC);
4811 termrequest_sent(&rbm_status);
4812 need_flush = TRUE;
4813 }
4814
4815 if (need_flush)
4816 out_flush();
4817 }
4818}
4819
4820/*
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01004821 * Add "key" to "buf" and return the number of bytes used.
4822 * Handles special keys and multi-byte characters.
4823 */
4824 static int
4825add_key_to_buf(int key, char_u *buf)
4826{
4827 int idx = 0;
4828
4829 if (IS_SPECIAL(key))
4830 {
4831 buf[idx++] = K_SPECIAL;
4832 buf[idx++] = KEY2TERMCAP0(key);
4833 buf[idx++] = KEY2TERMCAP1(key);
4834 }
4835 else if (has_mbyte)
4836 idx += (*mb_char2bytes)(key, buf + idx);
4837 else
4838 buf[idx++] = key;
4839 return idx;
4840}
4841
4842/*
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004843 * Handle a sequence with key and modifier, one of:
4844 * {lead}27;{modifier};{key}~
4845 * {lead}{key};{modifier}u
4846 * Returns the difference in length.
4847 */
4848 static int
4849handle_key_with_modifier(
4850 int *arg,
4851 int trail,
4852 int csi_len,
4853 int offset,
4854 char_u *buf,
4855 int bufsize,
4856 int *buflen)
4857{
4858 int key;
4859 int modifiers;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004860 char_u string[MAX_KEY_CODE_LEN + 1];
4861
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01004862 // Do not set seenModifyOtherKeys for kitty, it does send some sequences
4863 // like this but does not have the modifyOtherKeys feature.
4864 if (term_props[TPR_KITTY].tpr_status != TPR_YES)
4865 seenModifyOtherKeys = TRUE;
4866
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004867 if (trail == 'u')
4868 key = arg[0];
4869 else
4870 key = arg[2];
4871
4872 modifiers = decode_modifiers(arg[1]);
4873
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02004874 // Some keys need adjustment when the Ctrl modifier is used.
4875 key = may_adjust_key_for_ctrl(modifiers, key);
4876
Bram Moolenaaref6746f2020-06-20 14:43:23 +02004877 // May remove the shift modifier if it's already included in the key.
4878 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004879
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004880 // insert modifiers with KS_MODIFIER
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01004881 int new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004882
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01004883 // add the bytes for the key
4884 new_slen += add_key_to_buf(key, string + new_slen);
4885
4886 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4887 buf, bufsize, buflen) == FAIL)
4888 return -1;
4889 return new_slen - csi_len + offset;
4890}
4891
4892/*
4893 * Handle a sequence with key without a modifier:
4894 * {lead}{key}u
4895 * Returns the difference in length.
4896 */
4897 static int
4898handle_key_without_modifier(
4899 int *arg,
4900 int csi_len,
4901 int offset,
4902 char_u *buf,
4903 int bufsize,
4904 int *buflen)
4905{
4906 char_u string[MAX_KEY_CODE_LEN + 1];
4907 int new_slen = add_key_to_buf(arg[0], string);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004908
4909 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4910 buf, bufsize, buflen) == FAIL)
4911 return -1;
4912 return new_slen - csi_len + offset;
4913}
4914
4915/*
4916 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004917 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004918 *
4919 * - Cursor position report: {lead}{row};{col}R
4920 * The final byte must be 'R'. It is used for checking the
4921 * ambiguous-width character state.
4922 *
4923 * - window position reply: {lead}3;{x};{y}t
4924 *
4925 * - key with modifiers when modifyOtherKeys is enabled:
4926 * {lead}27;{modifier};{key}~
4927 * {lead}{key};{modifier}u
4928 * Return 0 for no match, -1 for partial match, > 0 for full match.
4929 */
4930 static int
4931handle_csi(
4932 char_u *tp,
4933 int len,
4934 char_u *argp,
4935 int offset,
4936 char_u *buf,
4937 int bufsize,
4938 int *buflen,
4939 char_u *key_name,
4940 int *slen)
4941{
4942 int first = -1; // optional char right after {lead}
4943 int trail; // char that ends CSI sequence
4944 int arg[3] = {-1, -1, -1}; // argument numbers
4945 int argc; // number of arguments
4946 char_u *ap = argp;
4947 int csi_len;
4948
4949 // Check for non-digit after CSI.
4950 if (!VIM_ISDIGIT(*ap))
4951 first = *ap++;
4952
4953 // Find up to three argument numbers.
4954 for (argc = 0; argc < 3; )
4955 {
4956 if (ap >= tp + len)
4957 return -1;
4958 if (*ap == ';')
4959 arg[argc++] = -1; // omitted number
4960 else if (VIM_ISDIGIT(*ap))
4961 {
4962 arg[argc] = 0;
4963 for (;;)
4964 {
4965 if (ap >= tp + len)
4966 return -1;
4967 if (!VIM_ISDIGIT(*ap))
4968 break;
4969 arg[argc] = arg[argc] * 10 + (*ap - '0');
4970 ++ap;
4971 }
4972 ++argc;
4973 }
4974 if (*ap == ';')
4975 ++ap;
4976 else
4977 break;
4978 }
4979
4980 // mrxvt has been reported to have "+" in the version. Assume
4981 // the escape sequence ends with a letter or one of "{|}~".
4982 while (ap < tp + len
4983 && !(*ap >= '{' && *ap <= '~')
4984 && !ASCII_ISALPHA(*ap))
4985 ++ap;
4986 if (ap >= tp + len)
4987 return -1;
4988 trail = *ap;
4989 csi_len = (int)(ap - tp) + 1;
4990
4991 // Cursor position report: Eat it when there are 2 arguments
4992 // and it ends in 'R'. Also when u7_status is not "sent", it
4993 // may be from a previous Vim that just exited. But not for
4994 // <S-F3>, it sends something similar, check for row and column
4995 // to make sense.
4996 if (first == -1 && argc == 2 && trail == 'R')
4997 {
4998 handle_u7_response(arg, tp, csi_len);
4999
5000 key_name[0] = (int)KS_EXTRA;
5001 key_name[1] = (int)KE_IGNORE;
5002 *slen = csi_len;
5003 }
5004
5005 // Version string: Eat it when there is at least one digit and
5006 // it ends in 'c'
5007 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5008 {
5009 handle_version_response(first, arg, argc, tp);
5010
5011 *slen = csi_len;
5012# ifdef FEAT_EVAL
5013 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
5014# endif
5015 apply_autocmds(EVENT_TERMRESPONSE,
5016 NULL, NULL, FALSE, curbuf);
5017 key_name[0] = (int)KS_EXTRA;
5018 key_name[1] = (int)KE_IGNORE;
5019 }
5020
5021 // Check blinking cursor from xterm:
5022 // {lead}?12;1$y set
5023 // {lead}?12;2$y not set
5024 //
5025 // {lead} can be <Esc>[ or CSI
5026 else if (rbm_status.tr_progress == STATUS_SENT
5027 && first == '?'
5028 && ap == argp + 6
5029 && arg[0] == 12
5030 && ap[-1] == '$'
5031 && trail == 'y')
5032 {
5033 initial_cursor_blink = (arg[1] == '1');
5034 rbm_status.tr_progress = STATUS_GOT;
5035 LOG_TR(("Received cursor blinking mode response: %s", tp));
5036 key_name[0] = (int)KS_EXTRA;
5037 key_name[1] = (int)KE_IGNORE;
5038 *slen = csi_len;
5039# ifdef FEAT_EVAL
5040 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
5041# endif
5042 }
5043
5044 // Check for a window position response from the terminal:
5045 // {lead}3;{x};{y}t
5046 else if (did_request_winpos && argc == 3 && arg[0] == 3
5047 && trail == 't')
5048 {
5049 winpos_x = arg[1];
5050 winpos_y = arg[2];
5051 // got finished code: consume it
5052 key_name[0] = (int)KS_EXTRA;
5053 key_name[1] = (int)KE_IGNORE;
5054 *slen = csi_len;
5055
5056 if (--did_request_winpos <= 0)
5057 winpos_status.tr_progress = STATUS_GOT;
5058 }
5059
5060 // Key with modifier:
5061 // {lead}27;{modifier};{key}~
5062 // {lead}{key};{modifier}u
Bram Moolenaar7609c882022-10-19 20:07:09 +01005063 // Only handles four modifiers, this won't work if the modifier value is
5064 // more than 16.
5065 else if (((arg[0] == 27 && argc == 3 && trail == '~')
5066 || (argc == 2 && trail == 'u'))
5067 && arg[1] <= 16)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005068 {
5069 return len + handle_key_with_modifier(arg, trail,
5070 csi_len, offset, buf, bufsize, buflen);
5071 }
5072
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01005073 // Key without modifier (bad Kitty may send this):
5074 // {lead}{key}u
5075 else if (argc == 1 && trail == 'u')
5076 {
5077 return len + handle_key_without_modifier(arg,
5078 csi_len, offset, buf, bufsize, buflen);
5079 }
5080
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005081 // else: Unknown CSI sequence. We could drop it, but then the
5082 // user can't create a map for it.
5083 return 0;
5084}
5085
5086/*
5087 * Handle an OSC sequence, fore/background color response from the terminal:
5088 *
5089 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5090 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5091 *
5092 * {code} is 10 for foreground, 11 for background
5093 * {lead} can be <Esc>] or OSC
5094 * {tail} can be '\007', <Esc>\ or STERM.
5095 *
5096 * Consume any code that starts with "{lead}11;", it's also
5097 * possible that "rgba" is following.
5098 */
5099 static int
5100handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5101{
5102 int i, j;
5103
5104 j = 1 + (tp[0] == ESC);
5105 if (len >= j + 3 && (argp[0] != '1'
5106 || (argp[1] != '1' && argp[1] != '0')
5107 || argp[2] != ';'))
5108 i = 0; // no match
5109 else
5110 for (i = j; i < len; ++i)
5111 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5112 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5113 {
5114 int is_bg = argp[1] == '1';
5115 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5116 && tp[j + 16] == '/';
5117
5118 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5119 && (is_4digit
5120 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5121 {
5122 char_u *tp_r = tp + j + 7;
5123 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5124 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
5125# ifdef FEAT_TERMINAL
5126 int rval, gval, bval;
5127
5128 rval = hexhex2nr(tp_r);
5129 gval = hexhex2nr(tp_b);
5130 bval = hexhex2nr(tp_g);
5131# endif
5132 if (is_bg)
5133 {
5134 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5135 *tp_b) ? "light" : "dark";
5136
5137 LOG_TR(("Received RBG response: %s", tp));
5138 rbg_status.tr_progress = STATUS_GOT;
5139# ifdef FEAT_TERMINAL
5140 bg_r = rval;
5141 bg_g = gval;
5142 bg_b = bval;
5143# endif
5144 if (!option_was_set((char_u *)"bg")
5145 && STRCMP(p_bg, new_bg_val) != 0)
5146 {
5147 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005148 set_option_value_give_err((char_u *)"bg",
5149 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005150 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005151 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005152 }
5153 }
5154# ifdef FEAT_TERMINAL
5155 else
5156 {
5157 LOG_TR(("Received RFG response: %s", tp));
5158 rfg_status.tr_progress = STATUS_GOT;
5159 fg_r = rval;
5160 fg_g = gval;
5161 fg_b = bval;
5162 }
5163# endif
5164 }
5165
5166 // got finished code: consume it
5167 key_name[0] = (int)KS_EXTRA;
5168 key_name[1] = (int)KE_IGNORE;
5169 *slen = i + 1 + (tp[i] == ESC);
5170# ifdef FEAT_EVAL
5171 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5172 : VV_TERMRFGRESP, tp, *slen);
5173# endif
5174 break;
5175 }
5176 if (i == len)
5177 {
5178 LOG_TR(("not enough characters for RB"));
5179 return FAIL;
5180 }
5181 return OK;
5182}
5183
5184/*
5185 * Check for key code response from xterm:
5186 * {lead}{flag}+r<hex bytes><{tail}
5187 *
5188 * {lead} can be <Esc>P or DCS
5189 * {flag} can be '0' or '1'
5190 * {tail} can be Esc>\ or STERM
5191 *
5192 * Check for cursor shape response from xterm:
5193 * {lead}1$r<digit> q{tail}
5194 *
5195 * {lead} can be <Esc>P or DCS
5196 * {tail} can be <Esc>\ or STERM
5197 *
5198 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5199 */
5200 static int
5201handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5202{
5203 int i, j;
5204
5205 j = 1 + (tp[0] == ESC);
5206 if (len < j + 3)
5207 i = len; // need more chars
5208 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
5209 i = 0; // no match
5210 else if (argp[1] == '+')
5211 // key code response
5212 for (i = j; i < len; ++i)
5213 {
5214 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5215 || tp[i] == STERM)
5216 {
5217 if (i - j >= 3)
5218 got_code_from_term(tp + j, i);
5219 key_name[0] = (int)KS_EXTRA;
5220 key_name[1] = (int)KE_IGNORE;
5221 *slen = i + 1 + (tp[i] == ESC);
5222 break;
5223 }
5224 }
5225 else
5226 {
5227 // Probably the cursor shape response. Make sure that "i"
5228 // is equal to "len" when there are not sufficient
5229 // characters.
5230 for (i = j + 3; i < len; ++i)
5231 {
5232 if (i - j == 3 && !isdigit(tp[i]))
5233 break;
5234 if (i - j == 4 && tp[i] != ' ')
5235 break;
5236 if (i - j == 5 && tp[i] != 'q')
5237 break;
5238 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5239 break;
5240 if ((i - j == 6 && tp[i] == STERM)
5241 || (i - j == 7 && tp[i] == '\\'))
5242 {
5243 int number = argp[3] - '0';
5244
5245 // 0, 1 = block blink, 2 = block
5246 // 3 = underline blink, 4 = underline
5247 // 5 = vertical bar blink, 6 = vertical bar
5248 number = number == 0 ? 1 : number;
5249 initial_cursor_shape = (number + 1) / 2;
5250 // The blink flag is actually inverted, compared to
5251 // the value set with T_SH.
5252 initial_cursor_shape_blink =
5253 (number & 1) ? FALSE : TRUE;
5254 rcs_status.tr_progress = STATUS_GOT;
5255 LOG_TR(("Received cursor shape response: %s", tp));
5256
5257 key_name[0] = (int)KS_EXTRA;
5258 key_name[1] = (int)KE_IGNORE;
5259 *slen = i + 1;
5260# ifdef FEAT_EVAL
5261 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5262# endif
5263 break;
5264 }
5265 }
5266 }
5267
5268 if (i == len)
5269 {
5270 // These codes arrive many together, each code can be
5271 // truncated at any point.
5272 LOG_TR(("not enough characters for XT"));
5273 return FAIL;
5274 }
5275 return OK;
5276}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005277#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005278
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 * Check if typebuf.tb_buf[] contains a terminal key code.
5281 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005282 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005284 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 * With a match, the match is removed, the replacement code is inserted in
5286 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5287 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005288 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5289 * "buflen" is then the length of the string in buf[] and is updated for
5290 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 */
5292 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005293check_termcode(
5294 int max_offset,
5295 char_u *buf,
5296 int bufsize,
5297 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298{
5299 char_u *tp;
5300 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005301 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005302 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005304 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 int offset;
5306 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005307 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005308 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005309 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005310 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 char_u string[MAX_KEY_CODE_LEN + 1];
5312 int i, j;
5313 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
5316 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5317
5318 /*
5319 * Speed up the checks for terminal codes by gathering all first bytes
5320 * used in termleader[]. Often this is just a single <Esc>.
5321 */
5322 if (need_gather)
5323 gather_termleader();
5324
5325 /*
5326 * Check at several positions in typebuf.tb_buf[], to catch something like
5327 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5328 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005329 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 * This is used often, KEEP IT FAST!
5331 */
5332 for (offset = 0; offset < max_offset; ++offset)
5333 {
5334 if (buf == NULL)
5335 {
5336 if (offset >= typebuf.tb_len)
5337 break;
5338 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005339 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
5341 else
5342 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005343 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 break;
5345 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005346 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
5348
5349 /*
5350 * Don't check characters after K_SPECIAL, those are already
5351 * translated terminal chars (avoid translating ~@^Hx).
5352 */
5353 if (*tp == K_SPECIAL)
5354 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005355 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 continue;
5357 }
5358
5359 /*
5360 * Skip this position if the character does not appear as the first
5361 * character in term_strings. This speeds up a lot, since most
5362 * termcodes start with the same character (ESC or CSI).
5363 */
5364 i = *tp;
5365 for (p = termleader; *p && *p != i; ++p)
5366 ;
5367 if (*p == NUL)
5368 continue;
5369
5370 /*
5371 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5372 * are in Insert mode.
5373 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005374 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 continue;
5376
Bram Moolenaar27efc622022-07-01 16:35:45 +01005377 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005378 key_name[0] = NUL; // no key name found yet
5379 key_name[1] = NUL; // no key name found yet
5380 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381
5382#ifdef FEAT_GUI
5383 if (gui.in_use)
5384 {
5385 /*
5386 * GUI special key codes are all of the form [CSI xx].
5387 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005388 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
5390 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005391 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 slen = 3;
5393 key_name[0] = tp[1];
5394 key_name[1] = tp[2];
5395 }
5396 }
5397 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005398#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005400 int mouse_index_found = -1;
5401
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 for (idx = 0; idx < tc_len; ++idx)
5403 {
5404 /*
5405 * Ignore the entry if we are not at the start of
5406 * typebuf.tb_buf[]
5407 * and there are not enough characters to make a match.
5408 * But only when the 'K' flag is in 'cpoptions'.
5409 */
5410 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005411 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 if (cpo_koffset && offset && len < slen)
5413 continue;
5414 if (STRNCMP(termcodes[idx].code, tp,
5415 (size_t)(slen > len ? len : slen)) == 0)
5416 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005417 int looks_like_mouse_start = FALSE;
5418
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005419 if (len < slen) // got a partial sequence
5420 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421
5422 /*
5423 * When found a keypad key, check if there is another key
5424 * that matches and use that one. This makes <Home> to be
5425 * found instead of <kHome> when they produce the same
5426 * key code.
5427 */
5428 if (termcodes[idx].name[0] == 'K'
5429 && VIM_ISDIGIT(termcodes[idx].name[1]))
5430 {
5431 for (j = idx + 1; j < tc_len; ++j)
5432 if (termcodes[j].len == slen &&
5433 STRNCMP(termcodes[idx].code,
5434 termcodes[j].code, slen) == 0)
5435 {
5436 idx = j;
5437 break;
5438 }
5439 }
5440
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005441 if (slen == 2 && len > 2
5442 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005443 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005444 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005445 // The mouse termcode "ESC [" is also the prefix of
5446 // "ESC [ I" (focus gained) and other keys. Check some
5447 // more bytes to find out.
5448 if (!isdigit(tp[2]))
5449 {
5450 // ESC [ without number following: Only use it when
5451 // there is no other match.
5452 looks_like_mouse_start = TRUE;
5453 }
5454 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5455 {
5456 char_u *nr = tp + 2;
5457 int count = 0;
5458
5459 // If a digit is following it could be a key with
5460 // modifier, e.g., ESC [ 1;2P. Can be confused
5461 // with DEC_MOUSE, which requires four numbers
5462 // following. If not then it can't be a DEC_MOUSE
5463 // code.
5464 for (;;)
5465 {
5466 ++count;
5467 (void)getdigits(&nr);
5468 if (nr >= tp + len)
5469 return -1; // partial sequence
5470 if (*nr != ';')
5471 break;
5472 ++nr;
5473 if (nr >= tp + len)
5474 return -1; // partial sequence
5475 }
5476 if (count < 4)
5477 continue; // no match
5478 }
5479 }
5480 if (looks_like_mouse_start)
5481 {
5482 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005483 if (mouse_index_found < 0)
5484 mouse_index_found = idx;
5485 }
5486 else
5487 {
5488 key_name[0] = termcodes[idx].name[0];
5489 key_name[1] = termcodes[idx].name[1];
5490 break;
5491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005493
5494 /*
5495 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005496 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005497 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005498 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5499 * When there is a modifier the * matches a number.
5500 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005501 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005502 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005503 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005504 modslen = termcodes[idx].modlen;
5505 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005506 continue;
5507 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005508 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005509 {
5510 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005511
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005512 if (len <= modslen) // got a partial sequence
5513 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005514
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005515 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005516 // no modifiers
5517 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005518 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005519 // no match for "code;*X" with "code;"
5520 continue;
Trygve Aabergea3532822022-10-18 19:22:25 +01005521 else if (termcodes[idx].code[modslen] == '@'
5522 && tp[modslen] != '1')
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005523 // no match for "<Esc>[@" with "<Esc>[1"
5524 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005525 else
5526 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005527 // Skip over the digits, the final char must
5528 // follow. URXVT can use a negative value, thus
5529 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005530 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005531 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005532 ;
5533 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005534 if (len < j) // got a partial sequence
5535 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005536 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005537 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005538
Bram Moolenaara529ce02017-06-22 22:37:57 +02005539 modifiers_start = tp + slen - 2;
5540
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005541 // Match! Convert modifier bits.
5542 n = atoi((char *)modifiers_start);
5543 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005544
5545 slen = j;
5546 }
5547 key_name[0] = termcodes[idx].name[0];
5548 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005549 break;
5550 }
5551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005553 if (idx == tc_len && mouse_index_found >= 0)
5554 {
5555 key_name[0] = termcodes[mouse_index_found].name[0];
5556 key_name[1] = termcodes[mouse_index_found].name[1];
5557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 }
5559
5560#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005561 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005562 // Mouse codes of DEC and pterm start with <ESC>[. When
5563 // detecting the start of these mouse codes they might as well be
5564 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005565# ifdef FEAT_MOUSE_DEC
5566 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005567# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005568# ifdef FEAT_MOUSE_PTERM
5569 || key_name[0] == KS_PTERM_MOUSE
5570# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005571 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005573 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5574
5575 /*
5576 * Check for responses from the terminal starting with {lead}:
5577 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005578 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005579 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005580 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005581 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005582 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005583 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005584 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005585 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005586 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005587 * - window position reply: {lead}3;{x};{y}t
5588 *
5589 * - key with modifiers when modifyOtherKeys is enabled:
5590 * {lead}27;{modifier};{key}~
5591 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005592 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005593 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005594 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005595 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005597 int resp = handle_csi(tp, len, argp, offset, buf,
5598 bufsize, buflen, key_name, &slen);
5599 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005600 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005601# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005602 if (resp == -1)
5603 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005604# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005605 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005606 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005607 }
5608
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005609 // Check for fore/background color response from the terminal,
5610 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005611 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005612 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5613 || tp[0] == OSC))
5614 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005615 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005616 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 }
5618
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005619 // Check for key code response from xterm,
5620 // starting with <Esc>P or DCS
Bram Moolenaarafd78262019-05-10 23:10:31 +02005621 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005622 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 || tp[0] == DCS))
5624 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005625 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005626 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 }
5628 }
5629#endif
5630
5631 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005632 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005634 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005636#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 /*
5638 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5639 * so that we know which window to scroll later.
5640 */
5641 if (gui.in_use
5642 && key_name[0] == (int)KS_EXTRA
5643 && (key_name[1] == (int)KE_X1MOUSE
5644 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005645 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005646 || key_name[1] == (int)KE_MOUSELEFT
5647 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 || key_name[1] == (int)KE_MOUSEDOWN
5649 || key_name[1] == (int)KE_MOUSEUP))
5650 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005651 char_u bytes[6];
5652 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5653
5654 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 return -1;
5656 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5657 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5658 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005659 // equal to K_MOUSEMOVE
5660 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
5661 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
5663 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 /*
5666 * If it is a mouse click, get the coordinates.
5667 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005668 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005669#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005670 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005671#endif
5672#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005673 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005674#endif
5675#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005676 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005677#endif
5678#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005679 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005680#endif
5681#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005682 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005683#endif
5684#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005685 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005686#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005687 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005688 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005690 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5691 &modifiers) == -1)
5692 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694
5695#ifdef FEAT_GUI
5696 /*
5697 * If using the GUI, then we get menu and scrollbar events.
5698 *
5699 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5700 * four bytes which are to be taken as a pointer to the vimmenu_T
5701 * structure.
5702 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005703 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005704 * is one byte with the tab index.
5705 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5707 * by one byte representing the scrollbar number, and then four bytes
5708 * representing a long_u which is the new value of the scrollbar.
5709 *
5710 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5711 * KE_FILLER followed by four bytes representing a long_u which is the
5712 * new value of the scrollbar.
5713 */
5714# ifdef FEAT_MENU
5715 else if (key_name[0] == (int)KS_MENU)
5716 {
5717 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005718 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 if (num_bytes == -1)
5721 return -1;
5722 current_menu = (vimmenu_T *)val;
5723 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005724
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005725 // The menu may have been deleted right after it was used, check
5726 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005727 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5728 {
5729 key_name[0] = KS_EXTRA;
5730 key_name[1] = (int)KE_IGNORE;
5731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
5733# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005734# ifdef FEAT_GUI_TABLINE
5735 else if (key_name[0] == (int)KS_TABLINE)
5736 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005737 // Selecting tabline tab or using its menu.
5738 char_u bytes[6];
5739 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5740
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005741 if (num_bytes == -1)
5742 return -1;
5743 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005744 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005745 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005746 slen += num_bytes;
5747 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005748 else if (key_name[0] == (int)KS_TABMENU)
5749 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005750 // Selecting tabline tab or using its menu.
5751 char_u bytes[6];
5752 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5753
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005754 if (num_bytes == -1)
5755 return -1;
5756 current_tab = (int)bytes[0];
5757 current_tabmenu = (int)bytes[1];
5758 slen += num_bytes;
5759 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005760# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761# ifndef USE_ON_FLY_SCROLL
5762 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5763 {
5764 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005765 char_u bytes[6];
5766 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005768 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 j = 0;
5770 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5771 && tp[j + 2] != NUL; ++i)
5772 {
5773 j += 3;
5774 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5775 if (num_bytes == -1)
5776 break;
5777 if (i == 0)
5778 current_scrollbar = (int)bytes[0];
5779 else if (current_scrollbar != (int)bytes[0])
5780 break;
5781 j += num_bytes;
5782 num_bytes = get_long_from_buf(tp + j, &val);
5783 if (num_bytes == -1)
5784 break;
5785 scrollbar_value = val;
5786 j += num_bytes;
5787 slen = j;
5788 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005789 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 return -1;
5791 }
5792 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5793 {
5794 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005795 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005797 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 j = 0;
5799 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5800 && tp[j + 2] != NUL; ++i)
5801 {
5802 j += 3;
5803 num_bytes = get_long_from_buf(tp + j, &val);
5804 if (num_bytes == -1)
5805 break;
5806 scrollbar_value = val;
5807 j += num_bytes;
5808 slen = j;
5809 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005810 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 return -1;
5812 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005813# endif // !USE_ON_FLY_SCROLL
5814#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005816#if (defined(UNIX) || defined(VMS))
5817 /*
5818 * Handle FocusIn/FocusOut event sequences reported by XTerm.
5819 * (CSI I/CSI O)
5820 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00005821 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005822# ifdef FEAT_GUI
5823 && !gui.in_use
5824# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005825 )
5826 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005827 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005828 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005829 if (!focus_state)
5830 {
5831 ui_focus_change(TRUE);
5832 did_cursorhold = TRUE;
5833 focus_state = TRUE;
5834 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005835 key_name[1] = (int)KE_IGNORE;
5836 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01005837 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005838 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005839 if (focus_state)
5840 {
5841 ui_focus_change(FALSE);
5842 did_cursorhold = TRUE;
5843 focus_state = FALSE;
5844 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005845 key_name[1] = (int)KE_IGNORE;
5846 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005847 }
5848#endif
5849
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005850 /*
5851 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5852 */
5853 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5854
5855 /*
5856 * Add any modifier codes to our string.
5857 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005858 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005859
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005860 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005861 key_name[0] = KEY2TERMCAP0(key);
5862 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005864 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005865 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005866 if (has_mbyte)
5867 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5868 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005869 string[new_slen++] = key_name[1];
5870 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005871 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5872 && key_name[1] == KE_IGNORE)
5873 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005874 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5875 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005876 retval = KEYLEN_REMOVED;
5877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 else
5879 {
5880 string[new_slen++] = K_SPECIAL;
5881 string[new_slen++] = key_name[0];
5882 string[new_slen++] = key_name[1];
5883 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005884 if (put_string_in_typebuf(offset, slen, string, new_slen,
5885 buf, bufsize, buflen) == FAIL)
5886 return -1;
5887 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 }
5889
Bram Moolenaar2951b772013-07-03 12:45:31 +02005890#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005891 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005892#endif
5893
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005894 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895}
5896
Bram Moolenaar9377df32017-10-15 13:22:01 +02005897#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005898/*
5899 * Get the text foreground color, if known.
5900 */
5901 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005902term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005903{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005904 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005905 {
5906 *r = fg_r;
5907 *g = fg_g;
5908 *b = fg_b;
5909 }
5910}
5911
5912/*
5913 * Get the text background color, if known.
5914 */
5915 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005916term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005917{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005918 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005919 {
5920 *r = bg_r;
5921 *g = bg_g;
5922 *b = bg_b;
5923 }
5924}
5925#endif
5926
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927/*
5928 * Replace any terminal code strings in from[] with the equivalent internal
5929 * vim representation. This is used for the "from" and "to" part of a
5930 * mapping, and the "to" part of a menu command.
5931 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5932 * '<'.
5933 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5934 *
5935 * The replacement is done in result[] and finally copied into allocated
5936 * memory. If this all works well *bufp is set to the allocated memory and a
5937 * pointer to it is returned. If something fails *bufp is set to NULL and from
5938 * is returned.
5939 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005940 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5941 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5942 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5943 * used instead of a CTRL-V.
5944 *
5945 * Flags:
5946 * REPTERM_FROM_PART see above
5947 * REPTERM_DO_LT also translate <lt>
5948 * REPTERM_SPECIAL always accept <key> notation
5949 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5950 *
5951 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5952 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005954 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005955replace_termcodes(
5956 char_u *from,
5957 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005958 int flags,
5959 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960{
5961 int i;
5962 int slen;
5963 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01005964 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005966 int do_backslash; // backslash is a special character
5967 int do_special; // recognize <> key codes
5968 int do_key_code; // recognize raw key codes
5969 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01005970 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971
5972 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02005973 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
5974 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01005976 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
5978 /*
5979 * Allocate space for the translation. Worst case a single character is
5980 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01005981 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01005983 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01005984 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 {
5986 *bufp = NULL;
5987 return from;
5988 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01005989 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990
5991 /*
5992 * Check for #n at start only: function key n
5993 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005994 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 {
5996 result[dlen++] = K_SPECIAL;
5997 result[dlen++] = 'k';
5998 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005999 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006001 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 src += 2;
6003 }
6004
6005 /*
6006 * Copy each byte from *from to result[dlen]
6007 */
6008 while (*src != NUL)
6009 {
6010 /*
6011 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006012 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006014 if (do_special && ((flags & REPTERM_DO_LT)
6015 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 {
6017#ifdef FEAT_EVAL
6018 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006019 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6020 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006022 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6023 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 */
6025 if (STRNICMP(src, "<SID>", 5) == 0)
6026 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006027 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006028 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 else
6030 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006031 char_u *dot;
6032 long sid = current_sctx.sc_sid;
6033
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006035 if (in_vim9script()
6036 && (dot = vim_strchr(src, '.')) != NULL)
6037 {
6038 imported_T *imp = find_imported(src, dot - src, FALSE);
6039
6040 if (imp != NULL)
6041 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006042 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6043 size_t len;
6044
Bram Moolenaar89445512022-04-14 12:58:23 +01006045 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006046 if (si->sn_autoload_prefix != NULL)
6047 {
6048 // Turn "<SID>name.Func"
6049 // into "scriptname#Func".
6050 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006051 if (ga_grow(&ga,
6052 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006053 {
6054 ga_clear(&ga);
6055 *bufp = NULL;
6056 return from;
6057 }
6058 result = ga.ga_data;
6059 STRCPY(result + dlen, si->sn_autoload_prefix);
6060 dlen += len;
6061 continue;
6062 }
6063 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006064 }
6065 }
6066
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 result[dlen++] = K_SPECIAL;
6068 result[dlen++] = (int)KS_EXTRA;
6069 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006070 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006071 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 result[dlen++] = '_';
6073 continue;
6074 }
6075 }
6076#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006077 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6078 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006079 TRUE, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 if (slen)
6081 {
6082 dlen += slen;
6083 continue;
6084 }
6085 }
6086
6087 /*
6088 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6089 * Note that this is also checked after replacing the <> form.
6090 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6091 * it could be a character in the file.
6092 */
6093 if (do_key_code)
6094 {
6095 i = find_term_bykeys(src);
6096 if (i >= 0)
6097 {
6098 result[dlen++] = K_SPECIAL;
6099 result[dlen++] = termcodes[i].name[0];
6100 result[dlen++] = termcodes[i].name[1];
6101 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006102 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 continue;
6104 }
6105 }
6106
6107#ifdef FEAT_EVAL
6108 if (do_special)
6109 {
6110 char_u *p, *s, len;
6111
6112 /*
6113 * Replace <Leader> by the value of "mapleader".
6114 * Replace <LocalLeader> by the value of "maplocalleader".
6115 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6116 */
6117 if (STRNICMP(src, "<Leader>", 8) == 0)
6118 {
6119 len = 8;
6120 p = get_var_value((char_u *)"g:mapleader");
6121 }
6122 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6123 {
6124 len = 13;
6125 p = get_var_value((char_u *)"g:maplocalleader");
6126 }
6127 else
6128 {
6129 len = 0;
6130 p = NULL;
6131 }
6132 if (len != 0)
6133 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006134 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6136 s = (char_u *)"\\";
6137 else
6138 s = p;
6139 while (*s != NUL)
6140 result[dlen++] = *s++;
6141 src += len;
6142 continue;
6143 }
6144 }
6145#endif
6146
6147 /*
6148 * Remove CTRL-V and ignore the next character.
6149 * For "from" side the CTRL-V at the end is included, for the "to"
6150 * part it is removed.
6151 * If 'cpoptions' does not contain 'B', also accept a backslash.
6152 */
6153 key = *src;
6154 if (key == Ctrl_V || (do_backslash && key == '\\'))
6155 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006156 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 if (*src == NUL)
6158 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006159 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 result[dlen++] = key;
6161 break;
6162 }
6163 }
6164
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006165 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006166 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 {
6168 /*
6169 * If the character is K_SPECIAL, replace it with K_SPECIAL
6170 * KS_SPECIAL KE_FILLER.
6171 * If compiled with the GUI replace CSI with K_CSI.
6172 */
6173 if (*src == K_SPECIAL)
6174 {
6175 result[dlen++] = K_SPECIAL;
6176 result[dlen++] = KS_SPECIAL;
6177 result[dlen++] = KE_FILLER;
6178 }
6179# ifdef FEAT_GUI
6180 else if (*src == CSI)
6181 {
6182 result[dlen++] = K_SPECIAL;
6183 result[dlen++] = KS_EXTRA;
6184 result[dlen++] = (int)KE_CSI;
6185 }
6186# endif
6187 else
6188 result[dlen++] = *src;
6189 ++src;
6190 }
6191 }
6192 result[dlen] = NUL;
6193
6194 /*
6195 * Copy the new string to allocated memory.
6196 * If this fails, just return from.
6197 */
6198 if ((*bufp = vim_strsave(result)) != NULL)
6199 from = *bufp;
6200 vim_free(result);
6201 return from;
6202}
6203
6204/*
6205 * Find a termcode with keys 'src' (must be NUL terminated).
6206 * Return the index in termcodes[], or -1 if not found.
6207 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006208 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006209find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210{
6211 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006212 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213
6214 for (i = 0; i < tc_len; ++i)
6215 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006216 if (slen == termcodes[i].len
6217 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 return i;
6219 }
6220 return -1;
6221}
6222
6223/*
6224 * Gather the first characters in the terminal key codes into a string.
6225 * Used to speed up check_termcode().
6226 */
6227 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006228gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229{
6230 int i;
6231 int len = 0;
6232
6233#ifdef FEAT_GUI
6234 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006235 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236#endif
6237#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006238 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006239 termleader[len++] = DCS; // the termcode response starts with DCS
6240 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241#endif
6242 termleader[len] = NUL;
6243
6244 for (i = 0; i < tc_len; ++i)
6245 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6246 {
6247 termleader[len++] = termcodes[i].code[0];
6248 termleader[len] = NUL;
6249 }
6250
6251 need_gather = FALSE;
6252}
6253
6254/*
6255 * Show all termcodes (for ":set termcap")
6256 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006257 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 */
6259 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006260show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 int col;
6263 int *items;
6264 int item_count;
6265 int run;
6266 int row, rows;
6267 int cols;
6268 int i;
6269 int len;
6270
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006271#define INC3 27 // try to make three columns
6272#define INC2 40 // try to make two columns
6273#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006275 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006277 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 if (items == NULL)
6279 return;
6280
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006281 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006282 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283
6284 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006285 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006287 * 2. display the medium items (medium length strings)
6288 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006289 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006291 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 {
6293 /*
6294 * collect the items in items[]
6295 */
6296 item_count = 0;
6297 for (i = 0; i < tc_len; i++)
6298 {
6299 len = show_one_termcode(termcodes[i].name,
6300 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006301 if ((flags & OPT_ONECOLUMN) ||
6302 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006303 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006304 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 items[item_count++] = i;
6306 }
6307
6308 /*
6309 * display the items
6310 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006311 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006313 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 if (cols == 0)
6315 cols = 1;
6316 rows = (item_count + cols - 1) / cols;
6317 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006318 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 rows = item_count;
6320 for (row = 0; row < rows && !got_int; ++row)
6321 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006322 msg_putchar('\n'); // go to next line
6323 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 break;
6325 col = 0;
6326 for (i = row; i < item_count; i += rows)
6327 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006328 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 show_one_termcode(termcodes[items[i]].name,
6330 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006331 if (run == 2)
6332 col += INC2;
6333 else
6334 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 }
6336 out_flush();
6337 ui_breakcheck();
6338 }
6339 }
6340 vim_free(items);
6341}
6342
6343/*
6344 * Show one termcode entry.
6345 * Output goes into IObuff[]
6346 */
6347 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006348show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349{
6350 char_u *p;
6351 int len;
6352
6353 if (name[0] > '~')
6354 {
6355 IObuff[0] = ' ';
6356 IObuff[1] = ' ';
6357 IObuff[2] = ' ';
6358 IObuff[3] = ' ';
6359 }
6360 else
6361 {
6362 IObuff[0] = 't';
6363 IObuff[1] = '_';
6364 IObuff[2] = name[0];
6365 IObuff[3] = name[1];
6366 }
6367 IObuff[4] = ' ';
6368
6369 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6370 if (p[1] != 't')
6371 STRCPY(IObuff + 5, p);
6372 else
6373 IObuff[5] = NUL;
6374 len = (int)STRLEN(IObuff);
6375 do
6376 IObuff[len++] = ' ';
6377 while (len < 17);
6378 IObuff[len] = NUL;
6379 if (code == NULL)
6380 len += 4;
6381 else
6382 len += vim_strsize(code);
6383
6384 if (printit)
6385 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006386 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006388 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 else
6390 msg_outtrans(code);
6391 }
6392 return len;
6393}
6394
6395#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6396/*
6397 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6398 * termcap codes from the terminal itself.
6399 * We get them one by one to avoid a very long response string.
6400 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006401static int xt_index_in = 0;
6402static int xt_index_out = 0;
6403
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006405req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
6407 xt_index_out = 0;
6408 xt_index_in = 0;
6409 req_more_codes_from_term();
6410}
6411
6412 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006413req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006415 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 int old_idx = xt_index_out;
6417
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006418 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 if (exiting)
6420 return;
6421
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006422 // Send up to 10 more requests out than we received. Avoid sending too
6423 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6425 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006426 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006427
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006428 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006429 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6430 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 out_str_nf((char_u *)buf);
6432 ++xt_index_out;
6433 }
6434
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006435 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 if (xt_index_out != old_idx)
6437 out_flush();
6438}
6439
6440/*
6441 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6442 * A "0" instead of the "1" indicates a code that isn't supported.
6443 * Both <name> and <string> are encoded in hex.
6444 * "code" points to the "0" or "1".
6445 */
6446 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006447got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449#define XT_LEN 100
6450 char_u name[3];
6451 char_u str[XT_LEN];
6452 int i;
6453 int j = 0;
6454 int c;
6455
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006456 // A '1' means the code is supported, a '0' means it isn't.
6457 // When half the length is > XT_LEN we can't use it.
6458 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6460 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006461 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 name[0] = hexhex2nr(code + 3);
6463 name[1] = hexhex2nr(code + 5);
6464 name[2] = NUL;
6465 for (i = 0; key_names[i] != NULL; ++i)
6466 {
6467 if (STRCMP(key_names[i], name) == 0)
6468 {
6469 xt_index_in = i;
6470 break;
6471 }
6472 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006473
Bram Moolenaarb255b902018-04-24 21:40:10 +02006474 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6475
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 if (key_names[i] != NULL)
6477 {
6478 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6479 str[j++] = c;
6480 str[j] = NUL;
6481 if (name[0] == 'C' && name[1] == 'o')
6482 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006483 // Color count is not a key code.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00006484 may_adjust_color_count(atoi((char *)str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 }
6486 else
6487 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006488 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 i = find_term_bykeys(str);
6490 if (i >= 0)
6491 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006492 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 }
6494 }
6495 }
6496
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006497 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 ++xt_index_in;
6499 req_more_codes_from_term();
6500}
6501
6502/*
6503 * Check if there are any unanswered requests and deal with them.
6504 * This is called before starting an external program or getting direct
6505 * keyboard input. We don't want responses to be send to that program or
6506 * handled as typed text.
6507 */
6508 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006509check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
6511 int c;
6512
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006513 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6515 return;
6516
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006517 // Vgetc() will check for and handle any response.
6518 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 ++no_mapping;
6520 ++allow_keys;
6521 for (;;)
6522 {
6523 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006524 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 break;
6526
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006527 // If a response is recognized it's replaced with K_IGNORE, must read
6528 // it from the input stream. If there is no K_IGNORE we can't do
6529 // anything, break here (there might be some responses further on, but
6530 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 if (c != K_SPECIAL && c != K_IGNORE)
6532 break;
6533 c = vgetc();
6534 if (c != K_IGNORE)
6535 {
6536 vungetc(c);
6537 break;
6538 }
6539 }
6540 --no_mapping;
6541 --allow_keys;
6542}
6543#endif
6544
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006545#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546static char ksme_str[20];
6547static char ksmr_str[20];
6548static char ksmd_str[20];
6549
6550/*
6551 * For Win32 console: update termcap codes for existing console attributes.
6552 */
6553 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006554update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
6556 struct builtin_term *p;
6557
6558 p = find_builtin_term(DEFAULT_TERM);
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006559 sprintf(ksme_str, "\033|%dm", attr);
6560 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
6561 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562
6563 while (p->bt_string != NULL)
6564 {
6565 if (p->bt_entry == (int)KS_ME)
6566 p->bt_string = &ksme_str[0];
6567 else if (p->bt_entry == (int)KS_MR)
6568 p->bt_string = &ksmr_str[0];
6569 else if (p->bt_entry == (int)KS_MD)
6570 p->bt_string = &ksmd_str[0];
6571 ++p;
6572 }
6573}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006574
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006575# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006576# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006577
6578typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006579{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006580 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
6581 CMODE_RGB, // Use 24bit RGB colors using VTP.
6582 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
6583 CMODE_LAST,
6584} cmode_T;
6585
6586struct ks_tbl_S
6587{
6588 int code; // value of KS_
6589 char *vtp; // code in RGB mode
6590 char *vtp2; // code in 256color mode
6591 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006592};
6593
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006594static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006595{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006596 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
6597 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
6598 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
6599 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
6600 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
6601 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
6602 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
6603 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
6604 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006605# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006606 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
6607 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
6608 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
6609 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006610# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006611 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
6612 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
6613 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
6614 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006615# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006616 {(int)KS_CCO, "256", "256", {""}}, // colors
6617 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006618};
6619
6620 static struct builtin_term *
6621find_first_tcap(
6622 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006623 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006624{
6625 struct builtin_term *p;
6626
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006627 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006628 if (p->bt_entry == code)
6629 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006630 return NULL;
6631}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006632# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006633
6634/*
6635 * For Win32 console: replace the sequence immediately after termguicolors.
6636 */
6637 void
6638swap_tcap(void)
6639{
6640# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006641 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006642 static cmode_T curr_mode;
6643 struct ks_tbl_S *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006644 struct builtin_term *bt;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006645 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006646
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006647 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006648 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006649 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006650 {
6651 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006652 if (bt != NULL)
6653 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006654 // Preserve the original value.
6655 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
6656 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
6657 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006658
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006659 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006660 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006661 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006662 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006663 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006664 }
6665
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006666 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006667 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006668 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006669 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006670 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006671 mode = CMODE_INDEXED;
6672
6673 if (mode == curr_mode)
6674 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006675
6676 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006677 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006678 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6679 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006680 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006681 }
6682
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006683 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006684# endif
6685}
6686
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006688
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006689
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006690#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006691 || defined(PROTO)
6692static int cube_value[] = {
6693 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6694};
6695
6696static int grey_ramp[] = {
6697 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6698 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6699};
6700
LemonBoyb2b3acb2022-05-20 10:10:34 +01006701static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01006702// R G B
6703 { 0, 0, 0}, // black
6704 {224, 0, 0}, // dark red
6705 { 0, 224, 0}, // dark green
6706 {224, 224, 0}, // dark yellow / brown
6707 { 0, 0, 224}, // dark blue
6708 {224, 0, 224}, // dark magenta
6709 { 0, 224, 224}, // dark cyan
6710 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006711
LemonBoyd2a46622022-05-01 17:43:33 +01006712 {128, 128, 128}, // dark grey
6713 {255, 64, 64}, // light red
6714 { 64, 255, 64}, // light green
6715 {255, 255, 64}, // yellow
6716 { 64, 64, 255}, // light blue
6717 {255, 64, 255}, // light magenta
6718 { 64, 255, 255}, // light cyan
6719 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006720};
6721
LemonBoyd2a46622022-05-01 17:43:33 +01006722#if defined(MSWIN)
6723// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
6724static const char_u cterm_ansi_idx[] = {
6725 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
6726};
6727#endif
6728
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006729#define ANSI_INDEX_NONE 0
6730
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006731 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01006732ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
6733{
6734 if (nr < 16)
6735 {
6736 *r = ansi_table[nr][0];
6737 *g = ansi_table[nr][1];
6738 *b = ansi_table[nr][2];
6739 *ansi_idx = nr;
6740 }
6741 else
6742 {
6743 *r = 0;
6744 *g = 0;
6745 *b = 0;
6746 *ansi_idx = ANSI_INDEX_NONE;
6747 }
6748}
6749
6750 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006751cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006752{
6753 int idx;
6754
6755 if (nr < 16)
6756 {
LemonBoyd2a46622022-05-01 17:43:33 +01006757#if defined(MSWIN)
6758 idx = cterm_ansi_idx[nr];
6759#else
6760 idx = nr;
6761#endif
6762 *r = ansi_table[idx][0];
6763 *g = ansi_table[idx][1];
6764 *b = ansi_table[idx][2];
6765 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006766 }
6767 else if (nr < 232)
6768 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006769 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006770 idx = nr - 16;
6771 *r = cube_value[idx / 36 % 6];
6772 *g = cube_value[idx / 6 % 6];
6773 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006774 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006775 }
6776 else if (nr < 256)
6777 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006778 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006779 idx = nr - 232;
6780 *r = grey_ramp[idx];
6781 *g = grey_ramp[idx];
6782 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006783 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006784 }
6785 else
6786 {
6787 *r = 0;
6788 *g = 0;
6789 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006790 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006791 }
6792}
6793#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006794
Bram Moolenaarf4140482020-02-15 23:06:45 +01006795/*
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006796 * Replace K_BS by <BS> and K_DEL by <DEL>.
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01006797 * Include any modifiers into the key and drop them.
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006798 * Returns "len" adjusted for replaced codes.
Bram Moolenaarf4140482020-02-15 23:06:45 +01006799 */
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006800 int
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01006801term_replace_keycodes(char_u *ta_buf, int ta_len, int len_arg)
Bram Moolenaarf4140482020-02-15 23:06:45 +01006802{
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006803 int len = len_arg;
Bram Moolenaarf4140482020-02-15 23:06:45 +01006804 int i;
6805 int c;
6806
6807 for (i = ta_len; i < ta_len + len; ++i)
6808 {
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01006809 if (ta_buf[i] == CSI && len - i > 3 && ta_buf[i + 1] == KS_MODIFIER)
6810 {
6811 int modifiers = ta_buf[i + 2];
6812 int key = ta_buf[i + 3];
6813
6814 // Try to use the modifier to modify the key. In any case drop the
6815 // modifier.
6816 mch_memmove(ta_buf + i + 1, ta_buf + i + 4, (size_t)(len - i - 3));
6817 len -= 3;
6818 if (key < 0x80)
6819 key = merge_modifyOtherKeys(key, &modifiers);
6820 ta_buf[i] = key;
6821 }
6822 else if (ta_buf[i] == CSI && len - i > 2)
Bram Moolenaarf4140482020-02-15 23:06:45 +01006823 {
6824 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6825 if (c == K_DEL || c == K_KDEL || c == K_BS)
6826 {
6827 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
Bram Moolenaar2f7e1b82022-10-04 13:17:31 +01006828 (size_t)(len - i - 2));
Bram Moolenaarf4140482020-02-15 23:06:45 +01006829 if (c == K_DEL || c == K_KDEL)
6830 ta_buf[i] = DEL;
6831 else
6832 ta_buf[i] = Ctrl_H;
6833 len -= 2;
6834 }
6835 }
6836 else if (ta_buf[i] == '\r')
6837 ta_buf[i] = '\n';
6838 if (has_mbyte)
6839 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6840 }
Bram Moolenaar01c34e72022-10-03 20:24:39 +01006841 return len;
Bram Moolenaarf4140482020-02-15 23:06:45 +01006842}