blob: b7e3facdb1c1c8251330247b9bde2078fff22786 [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
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000849 {K_XUP, "\033[@;*A"},
850 {K_XDOWN, "\033[@;*B"},
851 {K_XRIGHT, "\033[@;*C"},
852 {K_XLEFT, "\033[@;*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;*~"},
874 {K_HOME, "\033[1;*H"},
875 // {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
880 {K_END, "\033[1;*F"},
881 // {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
1353// table size
1354#define TPR_COUNT 4
1355
1356static termprop_T term_props[TPR_COUNT];
1357
1358/*
1359 * Initialize the term_props table.
1360 * When "all" is FALSE only set those that are detected from the version
1361 * response.
1362 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001363 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001364init_term_props(int all)
1365{
1366 int i;
1367
1368 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1369 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1370 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1371 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1372 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1373 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1374 term_props[TPR_MOUSE].tpr_name = "mouse";
1375 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
1376
1377 for (i = 0; i < TPR_COUNT; ++i)
1378 if (all || term_props[i].tpr_set_by_termresponse)
1379 term_props[i].tpr_status = TPR_UNKNOWN;
1380}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381#endif
1382
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001383#if defined(FEAT_EVAL) || defined(PROTO)
1384 void
1385f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1386{
1387# ifdef FEAT_TERMRESPONSE
1388 int i;
1389# endif
1390
Bram Moolenaar93a10962022-06-16 11:42:09 +01001391 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001392 return;
1393# ifdef FEAT_TERMRESPONSE
1394 for (i = 0; i < TPR_COUNT; ++i)
1395 {
1396 char_u value[2];
1397
1398 value[0] = term_props[i].tpr_status;
1399 value[1] = NUL;
1400 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1401 }
1402# endif
1403}
1404#endif
1405
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001407find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 struct builtin_term *p;
1410
1411 p = builtin_termcaps;
1412 while (p->bt_string != NULL)
1413 {
1414 if (p->bt_entry == (int)KS_NAME)
1415 {
1416#ifdef UNIX
1417 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1418 return p;
1419 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1420 return p;
1421 else
1422#endif
1423#ifdef VMS
1424 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1425 return p;
1426 else
1427#endif
1428 if (STRCMP(term, p->bt_string) == 0)
1429 return p;
1430 }
1431 ++p;
1432 }
1433 return p;
1434}
1435
1436/*
1437 * Parsing of the builtin termcap entries.
1438 * Caller should check if 'name' is a valid builtin term.
1439 * The terminal's name is not set, as this is already done in termcapinit().
1440 */
1441 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001442parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443{
1444 struct builtin_term *p;
1445 char_u name[2];
1446 int term_8bit;
1447
1448 p = find_builtin_term(term);
1449 term_8bit = term_is_8bit(term);
1450
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001451 // Do not parse if builtin term not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if (p->bt_string == NULL)
1453 return;
1454
1455 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1456 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001457 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001459 // Only set the value if it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 if (term_strings[p->bt_entry] == NULL
1461 || term_strings[p->bt_entry] == empty_option)
1462 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001463#ifdef FEAT_EVAL
1464 int opt_idx = -1;
1465#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001466 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1468 {
1469 char_u *s, *t;
1470
1471 s = vim_strsave((char_u *)p->bt_string);
1472 if (s != NULL)
1473 {
1474 for (t = s; *t; ++t)
1475 if (term_7to8bit(t))
1476 {
1477 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001478 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 }
1480 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001481#ifdef FEAT_EVAL
1482 opt_idx =
1483#endif
1484 set_term_option_alloced(
1485 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 }
1487 }
1488 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001491#ifdef FEAT_EVAL
1492 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1493#endif
1494 }
1495#ifdef FEAT_EVAL
1496 set_term_option_sctx_idx(NULL, opt_idx);
1497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
1499 }
1500 else
1501 {
1502 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1503 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1504 if (find_termcode(name) == NULL)
1505 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1506 }
1507 }
1508}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510/*
1511 * Set number of colors.
1512 * Store it as a number in t_colors.
1513 * Store it as a string in T_CCO (using nr_colors[]).
1514 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001515 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001516set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001518 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
1520 t_colors = nr;
1521 if (t_colors > 1)
1522 sprintf((char *)nr_colors, "%d", t_colors);
1523 else
1524 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001525 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001527
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001528#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001529/*
1530 * Set the color count to "val" and redraw if it changed.
1531 */
1532 static void
1533may_adjust_color_count(int val)
1534{
1535 if (val != t_colors)
1536 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001537 // Nr of colors changed, initialize highlighting and
1538 // redraw everything. This causes a redraw, which usually
1539 // clears the message. Try keeping the message if it
1540 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001541 set_keep_msg_from_hist();
1542 set_color_count(val);
1543 init_highlight(TRUE, FALSE);
1544# ifdef DEBUG_TERMRESPONSE
1545 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001546 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001547
Bram Moolenaarb255b902018-04-24 21:40:10 +02001548 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001549 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001550# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001551 redraw_asap(UPD_CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001552# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001553 }
1554}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555#endif
1556
1557#ifdef HAVE_TGETENT
1558static char *(key_names[]) =
1559{
Bram Moolenaar87202262020-05-24 17:23:45 +02001560# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001561 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001563# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 "#2", "#4", "%i", "*7",
1566 "k1", "k2", "k3", "k4", "k5", "k6",
1567 "k7", "k8", "k9", "k;", "F1", "F2",
1568 "%1", "&8", "kb", "kI", "kD", "kh",
1569 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1570 NULL
1571};
1572#endif
1573
Bram Moolenaar9289df52018-05-10 14:40:57 +02001574#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001575 static void
1576get_term_entries(int *height, int *width)
1577{
1578 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001579 enum SpecialKey dest; // index in term_strings[]
1580 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001581 } string_names[] =
1582 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1583 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1584 {KS_CL, "cl"}, {KS_CD, "cd"},
1585 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1586 {KS_ME, "me"}, {KS_MR, "mr"},
1587 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1588 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1589 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
Bram Moolenaar84f54632022-06-29 18:39:11 +01001590 {KS_USS, "Us"}, {KS_DS, "ds"}, {KS_CDS, "Ds"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001591 {KS_STE,"Te"}, {KS_STS,"Ts"},
1592 {KS_CM, "cm"}, {KS_SR, "sr"},
1593 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1594 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001595 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001596 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001597 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1598 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001599 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1600 {KS_VS, "vs"}, {KS_CVS, "VS"},
1601 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1602 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1603 {KS_TS, "ts"}, {KS_FS, "fs"},
1604 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1605 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1606 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001607 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001608 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1609 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001610 {KS_CST, "ST"}, {KS_CRT, "RT"},
1611 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001612 {(enum SpecialKey)0, NULL}
1613 };
1614 int i;
1615 char_u *p;
1616 static char_u tstrbuf[TBUFSZ];
1617 char_u *tp = tstrbuf;
1618
1619 /*
1620 * get output strings
1621 */
1622 for (i = 0; string_names[i].name != NULL; ++i)
1623 {
1624 if (TERM_STR(string_names[i].dest) == NULL
1625 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001626 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001627 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001628#ifdef FEAT_EVAL
1629 set_term_option_sctx_idx(string_names[i].name, -1);
1630#endif
1631 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001632 }
1633
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001634 // tgetflag() returns 1 if the flag is present, 0 if not and
1635 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001636 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1637 T_MS = (char_u *)"y";
1638 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1639 T_XS = (char_u *)"y";
1640 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1641 T_XN = (char_u *)"y";
1642 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1643 T_DB = (char_u *)"y";
1644 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1645 T_DA = (char_u *)"y";
1646 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1647 T_UT = (char_u *)"y";
1648
1649 /*
1650 * get key codes
1651 */
1652 for (i = 0; key_names[i] != NULL; ++i)
1653 if (find_termcode((char_u *)key_names[i]) == NULL)
1654 {
1655 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001656 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001657 if (p != NULL
1658 && (*p != Ctrl_H
1659 || key_names[i][0] != 'k'
1660 || key_names[i][1] != 'l'))
1661 add_termcode((char_u *)key_names[i], p, FALSE);
1662 }
1663
1664 if (*height == 0)
1665 *height = tgetnum("li");
1666 if (*width == 0)
1667 *width = tgetnum("co");
1668
1669 /*
1670 * Get number of colors (if not done already).
1671 */
1672 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001673 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001674 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001675#ifdef FEAT_EVAL
1676 set_term_option_sctx_idx("Co", -1);
1677#endif
1678 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001679
1680# ifndef hpux
1681 BC = (char *)TGETSTR("bc", &tp);
1682 UP = (char *)TGETSTR("up", &tp);
1683 p = TGETSTR("pc", &tp);
1684 if (p)
1685 PC = *p;
1686# endif
1687}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001688#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001689
1690 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001691report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001692{
1693 struct builtin_term *termp;
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001694 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001695
1696 mch_errmsg("\r\n");
1697 if (error_msg != NULL)
1698 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001699 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001700 mch_errmsg("\r\n");
1701 }
1702 mch_errmsg("'");
1703 mch_errmsg((char *)term);
1704 mch_errmsg(_("' not known. Available builtin terminals are:"));
1705 mch_errmsg("\r\n");
1706 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1707 {
Bram Moolenaar0f5575d2021-07-30 21:18:03 +02001708 if (termp->bt_entry == (int)KS_NAME
1709 && STRCMP(termp->bt_string, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001710 {
1711#ifdef HAVE_TGETENT
1712 mch_errmsg(" builtin_");
1713#else
1714 mch_errmsg(" ");
1715#endif
1716 mch_errmsg(termp->bt_string);
1717 mch_errmsg("\r\n");
1718 }
1719 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001720 // Output extra 'cmdheight' line breaks to avoid that the following error
1721 // message overwrites the last terminal name.
1722 for (i = 1; i < p_ch; ++i)
1723 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001724}
1725
1726 static void
1727report_default_term(char_u *term)
1728{
1729 mch_errmsg(_("defaulting to '"));
1730 mch_errmsg((char *)term);
1731 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001732 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001733 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001734 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001735 out_flush();
1736 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001737 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001738 }
1739}
1740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741/*
1742 * Set terminal options for terminal "term".
1743 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1744 *
1745 * While doing this, until ttest(), some options may be NULL, be careful.
1746 */
1747 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001748set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 struct builtin_term *termp;
1751#ifdef HAVE_TGETENT
1752 int builtin_first = p_tbi;
1753 int try;
1754 int termcap_cleared = FALSE;
1755#endif
1756 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001757 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 char_u *bs_p, *del_p;
1759
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001760 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001761 if (silent_mode)
1762 return OK;
1763
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001764 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
1766 if (term_is_builtin(term))
1767 {
1768 term += 8;
1769#ifdef HAVE_TGETENT
1770 builtin_first = 1;
1771#endif
1772 }
1773
1774/*
1775 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1776 * If builtin_first is TRUE:
1777 * 0. try builtin termcap
1778 * 1. try external termcap
1779 * 2. if both fail default to a builtin terminal
1780 * If builtin_first is FALSE:
1781 * 1. try external termcap
1782 * 2. try builtin termcap, if both fail default to a builtin terminal
1783 */
1784#ifdef HAVE_TGETENT
1785 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1786 {
1787 /*
1788 * Use external termcap
1789 */
1790 if (try == 1)
1791 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794 /*
1795 * If the external termcap does not have a matching entry, try the
1796 * builtin ones.
1797 */
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01001798 if ((error_msg = invoke_tgetent(tbuf, term)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (!termcap_cleared)
1801 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001802 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 termcap_cleared = TRUE;
1804 }
1805
Bram Moolenaar69e05692018-05-10 14:11:52 +02001806 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
1808 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001809 else // try == 0 || try == 2
1810#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 /*
1812 * Use builtin termcap
1813 */
1814 {
1815#ifdef HAVE_TGETENT
1816 /*
1817 * If builtin termcap was already used, there is no need to search
1818 * for the builtin termcap again, quit now.
1819 */
1820 if (try == 2 && builtin_first && termcap_cleared)
1821 break;
1822#endif
1823 /*
1824 * search for 'term' in builtin_termcaps[]
1825 */
1826 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001827 if (termp->bt_string == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
1829#ifdef HAVE_TGETENT
1830 /*
1831 * If try == 0, first try the external termcap. If that is not
1832 * found we'll get back here with try == 2.
1833 * If termcap_cleared is set we used the external termcap,
1834 * don't complain about not finding the term in the builtin
1835 * termcap.
1836 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001837 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001839 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 break;
1841#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001842 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001844 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 if (starting != NO_SCREEN)
1846 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001847 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 wait_return(TRUE);
1849 return FAIL;
1850 }
1851 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001852 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001853 set_string_option_direct((char_u *)"term", -1, term,
1854 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 display_errors();
1856 }
1857 out_flush();
1858#ifdef HAVE_TGETENT
1859 if (!termcap_cleared)
1860 {
1861#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001862 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863#ifdef HAVE_TGETENT
1864 termcap_cleared = TRUE;
1865 }
1866#endif
1867 parse_builtin_tcap(term);
1868#ifdef FEAT_GUI
1869 if (term_is_gui(term))
1870 {
1871 out_flush();
1872 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001873 // If starting the GUI failed, don't do any of the other
1874 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (!gui.in_use)
1876 return FAIL;
1877#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001878 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879#endif
1880 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001881#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 }
1883#ifdef HAVE_TGETENT
1884 }
1885#endif
1886
1887/*
1888 * special: There is no info in the termcap about whether the cursor
1889 * positioning is relative to the start of the screen or to the start of the
1890 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1891 * relative.
1892 */
1893 if (STRCMP(term, "pcterm") == 0)
1894 T_CCS = (char_u *)"yes";
1895 else
1896 T_CCS = empty_option;
1897
1898#ifdef UNIX
1899/*
1900 * Any "stty" settings override the default for t_kb from the termcap.
1901 * This is in os_unix.c, because it depends a lot on the version of unix that
1902 * is being used.
1903 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1904 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001905# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001907# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 get_stty();
1909#endif
1910
1911/*
1912 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1913 * didn't work, use the default CTRL-H
1914 * The default for t_kD is DEL, unless t_kb is DEL.
1915 * The vim_strsave'd strings are probably lost forever, well it's only two
1916 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1917 * directly.
1918 */
1919#ifdef FEAT_GUI
1920 if (!gui.in_use)
1921#endif
1922 {
1923 bs_p = find_termcode((char_u *)"kb");
1924 del_p = find_termcode((char_u *)"kD");
1925 if (bs_p == NULL || *bs_p == NUL)
1926 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1927 if ((del_p == NULL || *del_p == NUL) &&
1928 (bs_p == NULL || *bs_p != DEL))
1929 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1930 }
1931
1932#if defined(UNIX) || defined(VMS)
1933 term_is_xterm = vim_is_xterm(term);
1934#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02001935#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001936 // Reset terminal properties that are set based on the termresponse, which
1937 // will be sent out soon.
1938 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02001939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001941#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 /*
1943 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1944 * The termcode for the mouse is added as a side effect in option.c.
1945 */
1946 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001947 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001949# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001950 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
1952 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001953 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 else
1955 p = (char_u *)"xterm";
1956 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001957# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001959 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001960 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001961 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1962 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001963 reset_option_was_set((char_u *)"ttym");
1964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001966# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001968# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001970 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001972#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
Bram Moolenaarbf789742021-01-16 11:21:40 +01001976#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00001977 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001978 if (use_xterm_like_mouse(term))
1979 {
1980 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001981
1982 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01001983 name[0] = KS_EXTRA;
1984 name[1] = KE_FOCUSGAINED;
1985 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001986 add_termcode(name, (char_u *)"\033[I", FALSE);
1987
1988 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01001989 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001990 add_termcode(name, (char_u *)"\033[O", FALSE);
1991
Bram Moolenaar51b477f2021-03-03 15:24:28 +01001992 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001993 }
1994#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00001995#if (defined(UNIX) || defined(VMS))
1996 // First time after setting 'term' a focus event is always reported.
1997 focus_state = MAYBE;
1998#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01001999
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002001 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 if (STRCMP(term, DEFAULT_TERM) != 0)
2003 term_console = FALSE;
2004 else
2005 {
2006 term_console = TRUE;
2007# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002008 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009# endif
2010 }
2011#endif
2012
2013#if defined(UNIX) || defined(VMS)
2014 /*
2015 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2016 */
2017 if (vim_is_fastterm(term))
2018 p_tf = TRUE;
2019#endif
2020#ifdef USE_TERM_CONSOLE
2021 /*
2022 * 'ttyfast' is default on consoles
2023 */
2024 if (term_console)
2025 p_tf = TRUE;
2026#endif
2027
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002028 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002030 full_screen = TRUE; // we can use termcap codes from now on
2031 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002033 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002034 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002035 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036#endif
2037
2038 /*
2039 * Initialize the terminal with the appropriate termcap codes.
2040 * Set the mouse and window title if possible.
2041 * Don't do this when starting, need to parse the .vimrc first, because it
2042 * may redefine t_TI etc.
2043 */
2044 if (starting != NO_SCREEN)
2045 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002046 starttermcap(); // may change terminal mode
2047 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002048 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
2050
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002051 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 if (width <= 0 || height <= 0)
2053 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002054 // termcap failed to report size
2055 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002057#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002058 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002060 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061#endif
2062 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002063 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 if (starting != NO_SCREEN)
2065 {
2066 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002067 scroll_region_reset(); // In case Rows changed
2068 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002071 buf_T *buf;
2072 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 /*
2075 * Execute the TermChanged autocommands for each buffer that is
2076 * loaded.
2077 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002078 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
2080 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002081 {
2082 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2084 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002085 // restore curwin/curbuf and a few other things
2086 aucmd_restbuf(&aco);
2087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 }
2091
2092#ifdef FEAT_TERMRESPONSE
2093 may_req_termresponse();
2094#endif
2095
2096 return OK;
2097}
2098
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002099#if defined(EXITFREE) || defined(PROTO)
2100
2101# ifdef HAVE_DEL_CURTERM
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002102# include <term.h> // declares cur_term
2103# endif
2104
2105/*
2106 * If supported, delete "cur_term", which caches terminal related entries.
2107 * Avoids that valgrind reports possibly lost memory.
2108 */
2109 void
2110free_cur_term()
2111{
2112# ifdef HAVE_DEL_CURTERM
2113 if (cur_term)
2114 del_curterm(cur_term);
2115# endif
2116}
2117
2118#endif
2119
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120#ifdef HAVE_TGETENT
2121/*
2122 * Call tgetent()
2123 * Return error message if it fails, NULL if it's OK.
2124 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002125 static char *
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002126invoke_tgetent(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
2128 int i;
2129
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002130 // Note: Valgrind may report a leak here, because the library keeps one
2131 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002133 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002135 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136# endif
2137 )
2138 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002139 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2140 // tgetent() with the always existing "dumb" entry to avoid a crash or
2141 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 (void)TGETENT(tbuf, "dumb");
2143
2144 if (i < 0)
2145# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002146 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (i == 0)
2148# endif
2149#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002150 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002152 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153#endif
2154 }
2155 return NULL;
2156}
2157
2158/*
2159 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2160 * Fix that here.
2161 */
2162 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002163vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164{
2165 char *p;
2166
2167 p = tgetstr(s, (char **)pp);
2168 if (p == (char *)-1)
2169 p = NULL;
2170 return (char_u *)p;
2171}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002172#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002174#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175/*
2176 * Get Columns and Rows from the termcap. Used after a window signal if the
2177 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2178 * and "li" entries never change. But on some systems this works.
2179 * Errors while getting the entries are ignored.
2180 */
2181 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002182getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002183 long *cp, // pointer to columns
2184 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
2186 char_u tbuf[TBUFSZ];
2187
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002188 if (T_NAME != NULL && *T_NAME != NUL && invoke_tgetent(tbuf, T_NAME) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
2190 if (*cp == 0)
2191 *cp = tgetnum("co");
2192 if (*rp == 0)
2193 *rp = tgetnum("li");
2194 }
2195}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002196#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
2198/*
2199 * Get a string entry from the termcap and add it to the list of termcodes.
2200 * Used for <t_xx> special keys.
2201 * Give an error message for failure when not sourcing.
2202 * If force given, replace an existing entry.
2203 * Return FAIL if the entry was not found, OK if the entry was added.
2204 */
2205 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002206add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207{
2208 char_u *term;
2209 int key;
2210 struct builtin_term *termp;
2211#ifdef HAVE_TGETENT
2212 char_u *string;
2213 int i;
2214 int builtin_first;
2215 char_u tbuf[TBUFSZ];
2216 char_u tstrbuf[TBUFSZ];
2217 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002218 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219#endif
2220
2221/*
2222 * If the GUI is running or will start in a moment, we only support the keys
2223 * that the GUI can produce.
2224 */
2225#ifdef FEAT_GUI
2226 if (gui.in_use || gui.starting)
2227 return gui_mch_haskey(name);
2228#endif
2229
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002230 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 return OK;
2232
2233 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002234 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 return FAIL;
2236
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002237 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
2239 term += 8;
2240#ifdef HAVE_TGETENT
2241 builtin_first = TRUE;
2242#endif
2243 }
2244#ifdef HAVE_TGETENT
2245 else
2246 builtin_first = p_tbi;
2247#endif
2248
2249#ifdef HAVE_TGETENT
2250/*
2251 * We can get the entry from the builtin termcap and from the external one.
2252 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2253 * builtin termcap first.
2254 * If 'ttybuiltin' is off, try external termcap first.
2255 */
2256 for (i = 0; i < 2; ++i)
2257 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002258 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259#endif
2260 /*
2261 * Search in builtin termcap
2262 */
2263 {
2264 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002265 if (termp->bt_string != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
2267 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002268 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 while (termp->bt_entry != (int)KS_NAME)
2270 {
2271 if ((int)termp->bt_entry == key)
2272 {
2273 add_termcode(name, (char_u *)termp->bt_string,
2274 term_is_8bit(term));
2275 return OK;
2276 }
2277 ++termp;
2278 }
2279 }
2280 }
2281#ifdef HAVE_TGETENT
2282 else
2283 /*
2284 * Search in external termcap
2285 */
2286 {
Bram Moolenaar3efd65c2022-06-19 17:45:28 +01002287 error_msg = invoke_tgetent(tbuf, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 if (error_msg == NULL)
2289 {
2290 string = TGETSTR((char *)name, &tp);
2291 if (string != NULL && *string != NUL)
2292 {
2293 add_termcode(name, string, FALSE);
2294 return OK;
2295 }
2296 }
2297 }
2298 }
2299#endif
2300
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002301 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303#ifdef HAVE_TGETENT
2304 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002305 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 else
2307#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002308 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
2310 return FAIL;
2311}
2312
2313 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002314term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315{
2316 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2317}
2318
2319/*
2320 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2321 * Assume that the terminal is using 8-bit controls when the name contains
2322 * "8bit", like in "xterm-8bit".
2323 */
2324 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002325term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326{
2327 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2328}
2329
2330/*
2331 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002332 * <Esc>[ -> CSI <M_C_[>
2333 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 * <Esc>O -> <M-C-O>
2335 */
2336 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002337term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338{
2339 if (*p == ESC)
2340 {
2341 if (p[1] == '[')
2342 return CSI;
2343 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002344 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (p[1] == 'O')
2346 return 0x8f;
2347 }
2348 return 0;
2349}
2350
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002351#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002353term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354{
2355 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2356}
2357#endif
2358
2359#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2360
2361 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002362tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
2364 static char_u buf[16];
2365 char_u *p;
2366
2367 p = buf + 15;
2368 *p = '\0';
2369 do
2370 {
2371 --p;
2372 *p = (char_u) (i % 10 + '0');
2373 i /= 10;
2374 }
2375 while (i > 0 && p > buf);
2376 return p;
2377}
2378#endif
2379
2380#ifndef HAVE_TGETENT
2381
2382/*
2383 * minimal tgoto() implementation.
2384 * no padding and we only parse for %i %d and %+char
2385 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002386 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002387tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388{
2389 static char buf[30];
2390 char *p, *s, *e;
2391
2392 if (!cm)
2393 return "OOPS";
2394 e = buf + 29;
2395 for (s = buf; s < e && *cm; cm++)
2396 {
2397 if (*cm != '%')
2398 {
2399 *s++ = *cm;
2400 continue;
2401 }
2402 switch (*++cm)
2403 {
2404 case 'd':
2405 p = (char *)tltoa((unsigned long)y);
2406 y = x;
2407 while (*p)
2408 *s++ = *p++;
2409 break;
2410 case 'i':
2411 x++;
2412 y++;
2413 break;
2414 case '+':
2415 *s++ = (char)(*++cm + y);
2416 y = x;
2417 break;
2418 case '%':
2419 *s++ = *cm;
2420 break;
2421 default:
2422 return "OOPS";
2423 }
2424 }
2425 *s = '\0';
2426 return buf;
2427}
2428
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002429#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431/*
2432 * Set the terminal name and initialize the terminal options.
2433 * If "name" is NULL or empty, get the terminal name from the environment.
2434 * If that fails, use the default terminal name.
2435 */
2436 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002437termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 char_u *term;
2440
2441 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002442 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 term = name;
2444
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445#ifndef MSWIN
2446 if (term == NULL)
2447 term = mch_getenv((char_u *)"TERM");
2448#endif
2449 if (term == NULL || *term == NUL)
2450 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002451 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002453 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 set_string_default("term", term);
2455 set_string_default("ttytype", term);
2456
2457 /*
2458 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2459 */
2460 set_termname(T_NAME != NULL ? T_NAME : term);
2461}
2462
2463/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002464 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002466#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002467
2468// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002470
2471static int out_pos = 0; // number of chars in out_buf
2472
2473// Since the maximum number of SGR parameters shown as a normal value range is
2474// 16, the escape sequence length can be 4 * 16 + lead + tail.
2475#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
2477/*
2478 * out_flush(): flush the output buffer
2479 */
2480 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002481out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483 int len;
2484
2485 if (out_pos != 0)
2486 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002487 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 len = out_pos;
2489 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002490 ui_write(out_buf, len, FALSE);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002491#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002492 if (ch_log_output != FALSE)
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002493 {
2494 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002495 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002496# ifdef FEAT_GUI
2497 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2498# endif
2499 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002500 out_buf);
Bram Moolenaar1d97db32022-06-04 22:15:54 +01002501 if (ch_log_output == TRUE)
2502 ch_log_output = FALSE; // only log once
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002503 }
2504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 }
2506}
2507
Bram Moolenaara338adc2018-01-31 20:51:47 +01002508/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002509 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2510 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002511 */
2512 void
2513out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002514 int force UNUSED, // when TRUE, update cursor even when not moved
2515 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002516{
2517 mch_disable_flush();
2518 out_flush();
2519 mch_enable_flush();
2520#ifdef FEAT_GUI
2521 if (gui.in_use)
2522 {
2523 gui_update_cursor(force, clear_selection);
2524 gui_may_flush();
2525 }
2526#endif
2527}
2528
2529
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530/*
2531 * Sometimes a byte out of a multi-byte character is written with out_char().
2532 * To avoid flushing half of the character, call this function first.
2533 */
2534 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002535out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
2537 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2538 out_flush();
2539}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
2541#ifdef FEAT_GUI
2542/*
2543 * out_trash(): Throw away the contents of the output buffer
2544 */
2545 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002546out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
2548 out_pos = 0;
2549}
2550#endif
2551
2552/*
2553 * out_char(c): put a byte into the output buffer.
2554 * Flush it if it becomes full.
2555 * This should not be used for outputting text on the screen (use functions
2556 * like msg_puts() and screen_putchar() for that).
2557 */
2558 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002559out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
Bram Moolenaard0573012017-10-28 21:11:06 +02002561#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002562 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 out_char('\r');
2564#endif
2565
2566 out_buf[out_pos++] = c;
2567
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002568 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 if (out_pos >= OUT_SIZE || p_wd)
2570 out_flush();
2571}
2572
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002574 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002576 static int
2577out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002579 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
2581 if (out_pos >= OUT_SIZE)
2582 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002583 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584}
2585
2586/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002587 * A never-padding out_str().
2588 * Use this whenever you don't want to run the string through tputs().
2589 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 * is likely to strip off leading digits, that it mistakes for padding
2591 * information, and "%i", "%d", etc.
2592 * This should only be used for writing terminal codes, not for outputting
2593 * normal text (use functions like msg_puts() and screen_putchar() for that).
2594 */
2595 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002596out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002598 // avoid terminal strings being split up
2599 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 while (*s)
2603 out_char_nf(*s++);
2604
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002605 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 if (p_wd)
2607 out_flush();
2608}
2609
2610/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002611 * A conditional-flushing out_str, mainly for visualbell.
2612 * Handles a delay internally, because termlib may not respect the delay or do
2613 * it at the wrong time.
2614 * Note: Only for terminal strings.
2615 */
2616 void
2617out_str_cf(char_u *s)
2618{
2619 if (s != NULL && *s)
2620 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002621#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002622 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002623#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002624
2625#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002626 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002627 if (gui.in_use)
2628 {
2629 out_str_nf(s);
2630 return;
2631 }
2632#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002633 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002634 out_flush();
2635#ifdef HAVE_TGETENT
2636 for (p = s; *s; ++s)
2637 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002638 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002639 if (*s == '$' && *(s + 1) == '<')
2640 {
2641 char_u save_c = *s;
2642 int duration = atoi((char *)s + 2);
2643
2644 *s = NUL;
2645 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2646 *s = save_c;
2647 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002648# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002649 // Only sleep here if we can limit this happening in
2650 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002651 p = vim_strchr(s, '>');
2652 if (p == NULL || duration <= 0)
2653 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002654 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002655 p = s;
2656 }
2657 else
2658 {
2659 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002660 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002661 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002662# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002663 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002664 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002665# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002666 break;
2667 }
2668 }
2669 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2670#else
2671 while (*s)
2672 out_char_nf(*s++);
2673#endif
2674
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002675 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002676 if (p_wd)
2677 out_flush();
2678 }
2679}
2680
2681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002683 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 * This should only be used for writing terminal codes, not for outputting
2685 * normal text (use functions like msg_puts() and screen_putchar() for that).
2686 */
2687 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002688out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
2690 if (s != NULL && *s)
2691 {
2692#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002693 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (gui.in_use)
2695 {
2696 out_str_nf(s);
2697 return;
2698 }
2699#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002700 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002701 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 out_flush();
2703#ifdef HAVE_TGETENT
2704 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2705#else
2706 while (*s)
2707 out_char_nf(*s++);
2708#endif
2709
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002710 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (p_wd)
2712 out_flush();
2713 }
2714}
2715
2716/*
2717 * cursor positioning using termcap parser. (jw)
2718 */
2719 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002720term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721{
2722 OUT_STR(tgoto((char *)T_CM, col, row));
2723}
2724
2725 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002726term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727{
2728 OUT_STR(tgoto((char *)T_CRI, 0, i));
2729}
2730
2731 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002732term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733{
2734 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2735}
2736
2737 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002738term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739{
2740 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2741}
2742
2743#if defined(HAVE_TGETENT) || defined(PROTO)
2744 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002745term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002747 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 if (x < 0)
2749 x = 0;
2750 if (y < 0)
2751 y = 0;
2752 OUT_STR(tgoto((char *)T_CWP, y, x));
2753}
2754
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002755# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2756/*
2757 * Return TRUE if we can request the terminal for a response.
2758 */
2759 static int
2760can_get_termresponse()
2761{
2762 return cur_tmode == TMODE_RAW
2763 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002764# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002765 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002766# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002767 && p_ek;
2768}
2769
Bram Moolenaarafd78262019-05-10 23:10:31 +02002770/*
2771 * Set "status" to STATUS_SENT.
2772 */
2773 static void
2774termrequest_sent(termrequest_T *status)
2775{
2776 status->tr_progress = STATUS_SENT;
2777 status->tr_start = time(NULL);
2778}
2779
2780/*
2781 * Return TRUE if any of the requests are in STATUS_SENT.
2782 */
2783 static int
2784termrequest_any_pending()
2785{
2786 int i;
2787 time_t now = time(NULL);
2788
2789 for (i = 0; all_termrequests[i] != NULL; ++i)
2790 {
2791 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2792 {
2793 if (all_termrequests[i]->tr_start > 0 && now > 0
2794 && all_termrequests[i]->tr_start + 2 < now)
2795 // Sent the request more than 2 seconds ago and didn't get a
2796 // response, assume it failed.
2797 all_termrequests[i]->tr_progress = STATUS_FAIL;
2798 else
2799 return TRUE;
2800 }
2801 }
2802 return FALSE;
2803}
2804
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002805static int winpos_x = -1;
2806static int winpos_y = -1;
2807static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002808
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002809# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002810/*
2811 * Try getting the Vim window position from the terminal.
2812 * Returns OK or FAIL.
2813 */
2814 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002815term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002816{
2817 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002818 int prev_winpos_x = winpos_x;
2819 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002820
2821 if (*T_CGP == NUL || !can_get_termresponse())
2822 return FAIL;
2823 winpos_x = -1;
2824 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002825 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002826 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002827 OUT_STR(T_CGP);
2828 out_flush();
2829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002830 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002831 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002832 {
2833 (void)vpeekc_nomap();
2834 if (winpos_x >= 0 && winpos_y >= 0)
2835 {
2836 *x = winpos_x;
2837 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002838 return OK;
2839 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002840 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002841 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002842 // Do not reset "did_request_winpos", if we timed out the response might
2843 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002844
2845 winpos_x = prev_winpos_x;
2846 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002847 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002848 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002849 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002850 *x = winpos_x;
2851 *y = winpos_y;
2852 return OK;
2853 }
2854
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002855 return FALSE;
2856}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002857# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002858# endif
2859
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002861term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002863 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864}
2865#endif
2866
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002868term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
2870 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002871 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002872 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002874 // Special handling of 16 colors, because termcap can't handle it
2875 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
2876 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002878 && ((s[0] == ESC && s[1] == '[')
2879#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2880 || (s[0] == ESC && s[1] == '|')
2881#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02002882 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 && s[i] != NUL
2884 && (STRCMP(s + i + 1, "%p1%dm") == 0
2885 || STRCMP(s + i + 1, "%dm") == 0)
2886 && (s[i] == '3' || s[i] == '4'))
2887 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002889 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002891 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002893 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002894#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002895 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002896#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002897 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02002898 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2899 : (n >= 16 ? "48;5;" : "10");
2900
2901 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2903 }
2904 else
2905 OUT_STR(tgoto((char *)s, 0, n));
2906}
2907
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002908 void
2909term_fg_color(int n)
2910{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002911 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002912 if (*T_CAF)
2913 term_color(T_CAF, n);
2914 else if (*T_CSF)
2915 term_color(T_CSF, n);
2916}
2917
2918 void
2919term_bg_color(int n)
2920{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002921 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002922 if (*T_CAB)
2923 term_color(T_CAB, n);
2924 else if (*T_CSB)
2925 term_color(T_CSB, n);
2926}
2927
Bram Moolenaare023e882020-05-31 16:42:30 +02002928 void
2929term_ul_color(int n)
2930{
2931 if (*T_CAU)
2932 term_color(T_CAU, n);
2933}
2934
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002935/*
2936 * Return "dark" or "light" depending on the kind of terminal.
2937 * This is just guessing! Recognized are:
2938 * "linux" Linux console
2939 * "screen.linux" Linux console with screen
2940 * "cygwin.*" Cygwin shell
2941 * "putty.*" Putty program
2942 * We also check the COLORFGBG environment variable, which is set by
2943 * rxvt and derivatives. This variable contains either two or three
2944 * values separated by semicolons; we want the last value in either
2945 * case. If this value is 0-6 or 8, our background is dark.
2946 */
2947 char_u *
2948term_bg_default(void)
2949{
2950#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002951 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002952 return (char_u *)"dark";
2953#else
2954 char_u *p;
2955
2956 if (STRCMP(T_NAME, "linux") == 0
2957 || STRCMP(T_NAME, "screen.linux") == 0
2958 || STRNCMP(T_NAME, "cygwin", 6) == 0
2959 || STRNCMP(T_NAME, "putty", 5) == 0
2960 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
2961 && (p = vim_strrchr(p, ';')) != NULL
2962 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
2963 && p[2] == NUL))
2964 return (char_u *)"dark";
2965 return (char_u *)"light";
2966#endif
2967}
2968
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002969#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002970
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002971#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2972#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2973#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002974
2975 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002976term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002977{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002978#define MAX_COLOR_STR_LEN 100
2979 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002980
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002981 if (*s == NUL)
2982 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002983 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002984 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02002985#ifdef FEAT_VTP
2986 if (use_wt())
2987 {
2988 out_flush();
2989 buf[1] = '[';
2990 vtp_printf(buf);
2991 }
2992 else
2993#endif
2994 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002995}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002996
2997 void
2998term_fg_rgb_color(guicolor_T rgb)
2999{
3000 term_rgb_color(T_8F, rgb);
3001}
3002
3003 void
3004term_bg_rgb_color(guicolor_T rgb)
3005{
Yasuhiro Matsumotoaa04e1b2022-05-07 14:09:19 +01003006 if (rgb != INVALCOLOR)
3007 term_rgb_color(T_8B, rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003008}
Bram Moolenaare023e882020-05-31 16:42:30 +02003009
3010 void
3011term_ul_rgb_color(guicolor_T rgb)
3012{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003013# ifdef FEAT_TERMRESPONSE
Bram Moolenaarb3932752022-10-01 21:22:17 +01003014 // If the user explicitly sets t_8u then use it. Otherwise wait for
3015 // termresponse to be received, which is when t_8u would be set and a
3016 // redraw is needed if it was used.
3017 if (!option_was_set((char_u *)"t_8u") && write_t_8u_state != OK)
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003018 write_t_8u_state = MAYBE;
3019 else
3020# endif
3021 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003022}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003023#endif
3024
Bram Moolenaar651fca82021-11-29 20:39:38 +00003025#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026/*
3027 * Generic function to set window title, using t_ts and t_fs.
3028 */
3029 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003030term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003032 MAY_WANT_TO_LOG_THIS;
3033
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003034 // t_ts takes one argument: column in status line
3035 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003037 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 out_flush();
3039}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003040
3041/*
3042 * Tell the terminal to push (save) the title and/or icon, so that it can be
3043 * popped (restored) later.
3044 */
3045 void
3046term_push_title(int which)
3047{
Bram Moolenaar27821262019-05-08 16:41:09 +02003048 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003049 {
3050 OUT_STR(T_CST);
3051 out_flush();
3052 }
3053
Bram Moolenaar27821262019-05-08 16:41:09 +02003054 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003055 {
3056 OUT_STR(T_SSI);
3057 out_flush();
3058 }
3059}
3060
3061/*
3062 * Tell the terminal to pop the title and/or icon.
3063 */
3064 void
3065term_pop_title(int which)
3066{
Bram Moolenaar27821262019-05-08 16:41:09 +02003067 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003068 {
3069 OUT_STR(T_CRT);
3070 out_flush();
3071 }
3072
Bram Moolenaar27821262019-05-08 16:41:09 +02003073 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003074 {
3075 OUT_STR(T_SRI);
3076 out_flush();
3077 }
3078}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079#endif
3080
3081/*
3082 * Make sure we have a valid set or terminal options.
3083 * Replace all entries that are NULL by empty_option
3084 */
3085 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003086ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003088 char_u *env_colors;
3089
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003090 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
3092 /*
3093 * MUST have "cm": cursor motion.
3094 */
3095 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003096 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
3098 /*
3099 * if "cs" defined, use a scroll region, it's faster.
3100 */
3101 if (*T_CS != NUL)
3102 scroll_region = TRUE;
3103 else
3104 scroll_region = FALSE;
3105
3106 if (pairs)
3107 {
3108 /*
3109 * optional pairs
3110 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003111 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 if (*T_ME == NUL)
3113 T_ME = T_MR = T_MD = T_MB = empty_option;
3114 if (*T_SO == NUL || *T_SE == NUL)
3115 T_SO = T_SE = empty_option;
3116 if (*T_US == NUL || *T_UE == NUL)
3117 T_US = T_UE = empty_option;
3118 if (*T_CZH == NUL || *T_CZR == NUL)
3119 T_CZH = T_CZR = empty_option;
3120
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003121 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 if (*T_VE == NUL)
3123 T_VI = empty_option;
3124
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003125 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 if (*T_ME == NUL)
3127 {
3128 T_ME = T_SE;
3129 T_MR = T_SO;
3130 T_MD = T_SO;
3131 }
3132
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003133 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 if (*T_SO == NUL)
3135 {
3136 T_SE = T_ME;
3137 if (*T_MR == NUL)
3138 T_SO = T_MD;
3139 else
3140 T_SO = T_MR;
3141 }
3142
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003143 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 if (*T_CZH == NUL)
3145 {
3146 T_CZR = T_ME;
3147 if (*T_MR == NUL)
3148 T_CZH = T_MD;
3149 else
3150 T_CZH = T_MR;
3151 }
3152
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003153 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 if (*T_CSB == NUL || *T_CSF == NUL)
3155 {
3156 T_CSB = empty_option;
3157 T_CSF = empty_option;
3158 }
3159
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003160 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (*T_CAB == NUL || *T_CAF == NUL)
3162 {
3163 T_CAB = empty_option;
3164 T_CAF = empty_option;
3165 }
3166
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003167 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003169 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003171 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 p_wiv = (*T_XS != NUL);
3173 }
3174 need_gather = TRUE;
3175
Bram Moolenaar759d8152020-04-26 16:52:49 +02003176 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3177 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003179#ifdef FEAT_GUI
3180 if (!gui.in_use)
3181#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003182 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003183 env_colors = mch_getenv((char_u *)"COLORS");
3184 if (env_colors != NULL && isdigit(*env_colors))
3185 {
3186 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003187
Bram Moolenaar759d8152020-04-26 16:52:49 +02003188 if (colors != t_colors)
3189 set_color_count(colors);
3190 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192}
3193
3194#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3195 || defined(PROTO)
3196/*
3197 * Represent the given long_u as individual bytes, with the most significant
3198 * byte first, and store them in dst.
3199 */
3200 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003201add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202{
3203 int i;
3204 int shift;
3205
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003206 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
3208 shift = 8 * (sizeof(long_u) - i);
3209 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3210 }
3211}
3212
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213/*
3214 * Interpret the next string of bytes in buf as a long integer, with the most
3215 * significant byte first. Note that it is assumed that buf has been through
3216 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3217 * Puts result in val, and returns the number of bytes read from buf
3218 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3219 * were present.
3220 */
3221 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003222get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 int len;
3225 char_u bytes[sizeof(long_u)];
3226 int i;
3227 int shift;
3228
3229 *val = 0;
3230 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3231 if (len != -1)
3232 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003233 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 {
3235 shift = 8 * (sizeof(long_u) - 1 - i);
3236 *val += (long_u)bytes[i] << shift;
3237 }
3238 }
3239 return len;
3240}
3241#endif
3242
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243/*
3244 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3245 * that buf has been through inchar(). Returns the actual number of bytes used
3246 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3247 * available.
3248 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003249 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003250get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251{
3252 int len = 0;
3253 int i;
3254 char_u c;
3255
3256 for (i = 0; i < num_bytes; i++)
3257 {
3258 if ((c = buf[len++]) == NUL)
3259 return -1;
3260 if (c == K_SPECIAL)
3261 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003262 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 return -1;
3264 if (buf[len++] == (int)KS_ZERO)
3265 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003266 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3267 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003268 if (buf[len++] == (int)KE_CSI)
3269 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003271 else if (c == CSI && buf[len] == KS_EXTRA
3272 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003273 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3274 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003275 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 bytes[i] = c;
3277 }
3278 return len;
3279}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
3281/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003282 * Check if the new shell size is valid, correct it if it's too small or way
3283 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 */
3285 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003286check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003288 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003290 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003291
3292 // make sure these values are not invalid
3293 if (cmdline_row >= Rows)
3294 cmdline_row = Rows - 1;
3295 if (msg_row >= Rows)
3296 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003297}
3298
3299/*
3300 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3301 */
3302 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003303limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003304{
3305 if (Columns < MIN_COLUMNS)
3306 Columns = MIN_COLUMNS;
3307 else if (Columns > 10000)
3308 Columns = 10000;
3309 if (Rows > 1000)
3310 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311}
3312
Bram Moolenaar86b68352004-12-27 21:59:20 +00003313/*
3314 * Invoked just before the screen structures are going to be (re)allocated.
3315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003317win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
3319 static int old_Rows = 0;
3320 static int old_Columns = 0;
3321
3322 if (old_Rows != Rows || old_Columns != Columns)
3323 ui_new_shellsize();
3324 if (old_Rows != Rows)
3325 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003326 // If 'window' uses the whole screen, keep it using that.
3327 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003328 if (p_window == old_Rows - 1
3329 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003330 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003332 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334 if (old_Columns != Columns)
3335 {
3336 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003337 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
3339}
3340
3341/*
3342 * Call this function when the Vim shell has been resized in any way.
3343 * Will obtain the current size and redraw (also when size didn't change).
3344 */
3345 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003346shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348 set_shellsize(0, 0, FALSE);
3349}
3350
3351/*
3352 * Check if the shell size changed. Handle a resize.
3353 * When the size didn't change, nothing happens.
3354 */
3355 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003356shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
3358 int old_Rows = Rows;
3359 int old_Columns = Columns;
3360
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003361 if (!exiting
3362#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003363 // Do not get the size when executing a shell command during
3364 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003365 && !gui.starting
3366#endif
3367 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003368 {
3369 (void)ui_get_shellsize();
3370 check_shellsize();
3371 if (old_Rows != Rows || old_Columns != Columns)
3372 shell_resized();
3373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374}
3375
3376/*
3377 * Set size of the Vim shell.
3378 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3379 * window size (this is used for the :win command).
3380 * If 'mustset' is FALSE, we may try to get the real window size and if
3381 * it fails use 'width' and 'height'.
3382 */
3383 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003384set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
3386 static int busy = FALSE;
3387
3388 /*
3389 * Avoid recursiveness, can happen when setting the window size causes
3390 * another window-changed signal.
3391 */
3392 if (busy)
3393 return;
3394
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003395 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 return;
3397
Bram Moolenaar24959102022-05-07 20:01:16 +01003398 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003400 // postpone the resizing
Bram Moolenaar24959102022-05-07 20:01:16 +01003401 State = MODE_SETWSIZE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 return;
3403 }
3404
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003405 if (updating_screen)
3406 // resizing while in update_screen() may cause a crash
3407 return;
3408
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003409 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003410 // buffer (or window) has already been closed and removing a scrollbar
3411 // causes a resize event. Don't resize then, it will happen after entering
3412 // another buffer.
3413 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003414 return;
3415
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 ++busy;
3417
3418#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003419 out_flush(); // must do this before mch_get_shellsize() for
3420 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421#endif
3422
3423 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3424 {
3425 Rows = height;
3426 Columns = width;
3427 check_shellsize();
3428 ui_set_shellsize(mustset);
3429 }
3430 else
3431 check_shellsize();
3432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003433 // The window layout used to be adjusted here, but it now happens in
3434 // screenalloc() (also invoked from screenclear()). That is because the
3435 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar24959102022-05-07 20:01:16 +01003437 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3438 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 screenclear();
3440 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003441 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
3443 if (starting != NO_SCREEN)
3444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003446
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 changed_line_abv_curs();
3448 invalidate_botline();
3449
3450 /*
3451 * We only redraw when it's needed:
3452 * - While at the more prompt or executing an external command, don't
3453 * redraw, but position the cursor.
3454 * - While editing the command line, only redraw that.
3455 * - in Ex mode, don't redraw anything.
3456 * - Otherwise, redraw right now, and position the cursor.
3457 * Always need to call update_screen() or screenalloc(), to make
3458 * sure Rows/Columns and the size of ScreenLines[] is correct!
3459 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003460 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3461 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
3463 screenalloc(FALSE);
3464 repeat_message();
3465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 else
3467 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003468 if (curwin->w_p_scb)
3469 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003470 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003471 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003472 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003473 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003474 }
3475 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003476 {
3477 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003478 if (pum_visible())
3479 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003480 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003481 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003482 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003483 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003484 if (redrawing())
3485 setcursor();
3486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003488 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 }
3490 out_flush();
3491 --busy;
3492}
3493
3494/*
3495 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3496 * commands and Ex mode).
3497 */
3498 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003499settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
3501#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003502 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 if (gui.in_use)
3504 return;
3505#endif
3506
3507 if (full_screen)
3508 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003510 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3511 * set the terminal to raw mode, even though we think it already is,
3512 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 * When we think the terminal is normal, don't try to set it to
3514 * normal again, because that causes problems (logout!) on some
3515 * machines.
3516 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003517 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
3519#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003520# ifdef FEAT_GUI
3521 if (!gui.in_use && !gui.starting)
3522# endif
3523 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003524 // May need to check for T_CRV response and termcodes, it
3525 // doesn't work in Cooked mode, an external program may get
3526 // them.
3527 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003528 (void)vpeekc_nomap();
3529 check_for_codes_from_term();
3530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003533 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003534
3535 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3536 // Avoid doing this too often, on some terminals the codes are not
3537 // handled properly.
3538 if (termcap_active && tmode != TMODE_SLEEP
3539 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003540 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003541 MAY_WANT_TO_LOG_THIS;
3542
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003543 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003544 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003545 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003546 out_str(T_CTE); // possibly disables modifyOtherKeys
3547 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003548 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003549 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003550 out_str(T_BE); // enable bracketed paste mode (should
3551 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003552 out_str(T_CTI); // possibly enables modifyOtherKeys
3553 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003556 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003559 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 out_flush();
3561 }
3562#ifdef FEAT_TERMRESPONSE
3563 may_req_termresponse();
3564#endif
3565 }
3566}
3567
3568 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003569starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
3571 if (full_screen && !termcap_active)
3572 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003573 MAY_WANT_TO_LOG_THIS;
3574
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003575 out_str(T_TI); // start termcap mode
3576 out_str(T_CTI); // start "raw" mode
3577 out_str(T_KS); // start "keypad transmit" mode
3578 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003579
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003580#if defined(UNIX) || defined(VMS)
3581 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003582 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003583 out_str(T_FE);
3584#endif
3585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 out_flush();
3587 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003588 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003590# ifdef FEAT_GUI
3591 if (!gui.in_use && !gui.starting)
3592# endif
3593 {
3594 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003595 // Immediately check for a response. If t_Co changes, we don't
3596 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003597 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003598 check_for_codes_from_term();
3599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600#endif
3601 }
3602}
3603
3604 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003605stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606{
3607 screen_stop_highlight();
3608 reset_cterm_colors();
3609 if (termcap_active)
3610 {
3611#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003612# ifdef FEAT_GUI
3613 if (!gui.in_use && !gui.starting)
3614# endif
3615 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003616 // May need to discard T_CRV, T_U7 or T_RBG response.
3617 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003618 {
3619# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003620 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003621 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003622# endif
3623# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003624 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003625 if (exiting)
3626 tcflush(fileno(stdin), TCIFLUSH);
3627# endif
3628 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003629 // Check for termcodes first, otherwise an external program may
3630 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003631 check_for_codes_from_term();
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003634 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003635
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003636#if defined(UNIX) || defined(VMS)
3637 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003638 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003639 out_str(T_FD);
3640#endif
3641
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003642 out_str(T_BD); // disable bracketed paste mode
3643 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 out_flush();
3645 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003646 cursor_on(); // just in case it is still off
3647 out_str(T_CTE); // stop "raw" mode
3648 out_str(T_TE); // stop termcap mode
3649 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 out_flush();
3651 }
3652}
3653
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003654#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655/*
3656 * Request version string (for xterm) when needed.
3657 * Only do this after switching to raw mode, otherwise the result will be
3658 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003659 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003660 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 * Only do this after termcap mode has been started, otherwise the codes for
3662 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003663 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3664 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003665 * On Unix only do it when both output and input are a tty (avoid writing
3666 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 * The result is caught in check_termcode().
3668 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003669 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003670may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003672 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003673 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003674 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 && *T_CRV != NUL)
3676 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003677 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003678 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003680 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003681 // check for the characters now, otherwise they might be eaten by
3682 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 out_flush();
3684 (void)vpeekc_nomap();
3685 }
3686}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003687
Bram Moolenaar9584b312013-03-13 19:29:28 +01003688/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003689 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3690 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003691 * Note that this goes out before T_CRV, so that the result can be used when
3692 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003693 */
3694 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003695check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003696{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003697 int did_send = FALSE;
3698
3699 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3700 return;
3701
Bram Moolenaarafd78262019-05-10 23:10:31 +02003702 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003703 && !option_was_set((char_u *)"ambiwidth"))
3704 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003705 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003706
Bram Moolenaara45551a2020-06-09 15:57:37 +02003707 // Ambiguous width check.
3708 // Check how the terminal treats ambiguous character width (UAX #11).
3709 // First, we move the cursor to (1, 0) and print a test ambiguous
3710 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3711 // the current cursor position. If the terminal treats \u25bd as
3712 // single width, the position is (1, 1), or if it is treated as double
3713 // width, that will be (1, 2). This function has the side effect that
3714 // changes cursor position, so it must be called immediately after
3715 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003716 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003717 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003718 // Do this in the second row. In the first row the returned sequence
3719 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003720 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003721 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003722 out_str(buf);
3723 out_str(T_U7);
3724 termrequest_sent(&u7_status);
3725 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003726 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003727
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003728 // This overwrites a few characters on the screen, a redraw is needed
3729 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003730 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003731 term_windgoto(1, 0);
3732 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003733 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003734 }
3735
Bram Moolenaard1f34e62022-01-07 19:24:20 +00003736 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02003737 {
3738 // 2. Check compatibility with xterm.
3739 // We move the cursor to (2, 0), print a test sequence and then query
3740 // the current cursor position. If the terminal properly handles
3741 // unknown DCS string and CSI sequence with intermediate byte, the test
3742 // sequence is ignored and the cursor does not move. If the terminal
3743 // handles test sequence incorrectly, a garbage string is displayed and
3744 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003745 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003746 LOG_TR(("Sending xterm compatibility test sequence."));
3747 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003748 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003749 term_windgoto(2, 0);
3750 // send the test DCS string.
3751 out_str((char_u *)"\033Pzz\033\\");
3752 // send the test CSI sequence with intermediate byte.
3753 out_str((char_u *)"\033[0%m");
3754 out_str(T_U7);
3755 termrequest_sent(&xcc_status);
3756 out_flush();
3757 did_send = TRUE;
3758
3759 // If the terminal handles test sequence incorrectly, garbage text is
3760 // displayed. Clear them out for now.
3761 screen_stop_highlight();
3762 term_windgoto(2, 0);
3763 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003764 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003765 }
3766
3767 if (did_send)
3768 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003769 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003770
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003771 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003772 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003773
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003774 // check for the characters now, otherwise they might be eaten by
3775 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003776 out_flush();
3777 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003778 }
3779}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003780
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003781/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003782 * Similar to requesting the version string: Request the terminal background
3783 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003784 */
3785 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003786may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003787{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003788 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003789 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003790 int didit = FALSE;
3791
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003792# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003793 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003794 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003795 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003796 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003797 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003798 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003799 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003800 didit = TRUE;
3801 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003802# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003803
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003804 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003805 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003806 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003807 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003808 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003809 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003810 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003811 didit = TRUE;
3812 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003813
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003814 if (didit)
3815 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003816 // check for the characters now, otherwise they might be eaten by
3817 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003818 out_flush();
3819 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003820 }
3821 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003822}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003823
Bram Moolenaar2951b772013-07-03 12:45:31 +02003824# ifdef DEBUG_TERMRESPONSE
3825 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003826log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003827{
3828 static FILE *fd_tr = NULL;
3829 static proftime_T start;
3830 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003831 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003832
3833 if (fd_tr == NULL)
3834 {
3835 fd_tr = fopen("termresponse.log", "w");
3836 profile_start(&start);
3837 }
3838 now = start;
3839 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003840 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003841 must_redraw == UPD_NOT_VALID ? "NV"
3842 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02003843 va_start(ap, fmt);
3844 vfprintf(fd_tr, fmt, ap);
3845 va_end(ap);
3846 fputc('\n', fd_tr);
3847 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003848}
3849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850#endif
3851
3852/*
3853 * Return TRUE when saving and restoring the screen.
3854 */
3855 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003856swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857{
3858 return (full_screen && *T_TI != NUL);
3859}
3860
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861/*
3862 * By outputting the 'cursor very visible' termcap code, for some windowed
3863 * terminals this makes the screen scrolled to the correct position.
3864 * Used when starting Vim or returning from a shell.
3865 */
3866 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003867scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003869 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003871 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003873 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003874 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
3876}
3877
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003878// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879static int cursor_is_off = FALSE;
3880
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003881// True if cursor is not visible due to an ongoing cursor-less sleep
3882static int cursor_is_asleep = FALSE;
3883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003885 * Enable the cursor without checking if it's already enabled.
3886 */
3887 void
3888cursor_on_force(void)
3889{
3890 out_str(T_VE);
3891 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003892 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02003893}
3894
3895/*
3896 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 */
3898 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003899cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003901 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003902 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903}
3904
3905/*
3906 * Disable the cursor.
3907 */
3908 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003909cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003911 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003913 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 cursor_is_off = TRUE;
3915 }
3916}
3917
Dominique Pelle748b3082022-01-08 12:41:16 +00003918#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003919/*
3920 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
3921 */
3922 int
3923cursor_is_sleeping(void)
3924{
3925 return cursor_is_asleep;
3926}
Dominique Pelle748b3082022-01-08 12:41:16 +00003927#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003928
3929/*
3930 * Disable the cursor and mark it disabled by cursor-less sleep
3931 */
3932 void
3933cursor_sleep(void)
3934{
3935 cursor_is_asleep = TRUE;
3936 cursor_off();
3937}
3938
3939/*
3940 * Enable the cursor and mark it not disabled by cursor-less sleep
3941 */
3942 void
3943cursor_unsleep(void)
3944{
3945 cursor_is_asleep = FALSE;
3946 cursor_on();
3947}
3948
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003949#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003951 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003952 */
3953 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003954term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003955{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003956 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003957 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003958
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003959 // Only do something when redrawing the screen and we can restore the
3960 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003961 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003962 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003963# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003964 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003965 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003966 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003967# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003968 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003969 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003970
Bram Moolenaar24959102022-05-07 20:01:16 +01003971 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003972 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003973 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003974 {
3975 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003976 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003977 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003978 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003979 if (*p != NUL)
3980 {
3981 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01003982 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003983 }
3984 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003985 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003986 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003987 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003988 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003989 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003990 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01003991 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003992 }
3993 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003994 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003995 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003996 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01003997 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003998 }
3999}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004000
4001# if defined(FEAT_TERMINAL) || defined(PROTO)
4002 void
4003term_cursor_color(char_u *color)
4004{
4005 if (*T_CSC != NUL)
4006 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004007 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004008 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004009 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004010 out_flush();
4011 }
4012}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004013# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004014
Bram Moolenaar4db25542017-08-28 22:43:05 +02004015 int
4016blink_state_is_inverted()
4017{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004018#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004019 return rbm_status.tr_progress == STATUS_GOT
4020 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004021 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004022#else
4023 return FALSE;
4024#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004025}
4026
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004027/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004028 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004029 */
4030 void
4031term_cursor_shape(int shape, int blink)
4032{
4033 if (*T_CSH != NUL)
4034 {
4035 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4036 out_flush();
4037 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004038 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004039 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004040 int do_blink = blink;
4041
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004042 // t_SH is empty: try setting just the blink state.
4043 // The blink flags are XORed together, if the initial blinking from
4044 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004045 if (blink_state_is_inverted())
4046 do_blink = !blink;
4047
4048 if (do_blink && *T_VS != NUL)
4049 {
4050 out_str(T_VS);
4051 out_flush();
4052 }
4053 else if (!do_blink && *T_CVS != NUL)
4054 {
4055 out_str(T_CVS);
4056 out_flush();
4057 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004058 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004059}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004060#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004061
4062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 * Set scrolling region for window 'wp'.
4064 * The region starts 'off' lines from the start of the window.
4065 * Also set the vertical scroll region for a vertically split window. Always
4066 * the full width of the window, excluding the vertical separator.
4067 */
4068 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004069scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070{
4071 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4072 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004074 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4075 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004076 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077}
4078
4079/*
4080 * Reset scrolling region to the whole screen.
4081 */
4082 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004083scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084{
4085 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 if (*T_CSV != NUL)
4087 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004088 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089}
4090
4091
4092/*
4093 * List of terminal codes that are currently recognized.
4094 */
4095
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004096static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004098 char_u name[2]; // termcap name of entry
4099 char_u *code; // terminal code (in allocated memory)
4100 int len; // STRLEN(code)
4101 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102} *termcodes = NULL;
4103
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004104static int tc_max_len = 0; // number of entries that termcodes[] can hold
4105static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004107static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004108
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004110clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111{
4112 while (tc_len > 0)
4113 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004114 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 tc_max_len = 0;
4116
4117#ifdef HAVE_TGETENT
4118 BC = (char *)empty_option;
4119 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004120 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 ospeed = 0;
4122#endif
4123
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004124 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125}
4126
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004127#define ATC_FROM_TERM 55
4128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129/*
4130 * Add a new entry to the list of terminal codes.
4131 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004132 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4133 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 */
4135 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004136add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137{
4138 struct termcode *new_tc;
4139 int i, j;
4140 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004141 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
4143 if (string == NULL || *string == NUL)
4144 {
4145 del_termcode(name);
4146 return;
4147 }
4148
Bram Moolenaar4f974752019-02-17 17:44:42 +01004149#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004150 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004151#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004152# ifdef VIMDLL
4153 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004154 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004155 else
4156# endif
4157 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 if (s == NULL)
4160 return;
4161
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004162 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004163 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004165 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 s[0] = term_7to8bit(string);
4167 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004168
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004169#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4170# ifdef VIMDLL
4171 if (!gui.in_use)
4172# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004173 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004174 if (s[0] == K_NUL)
4175 {
4176 STRMOVE(s + 1, s);
4177 s[1] = 3;
4178 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004179 }
4180#endif
4181
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004182 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004184 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185
4186 /*
4187 * need to make space for more entries
4188 */
4189 if (tc_len == tc_max_len)
4190 {
4191 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004192 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 if (new_tc == NULL)
4194 {
4195 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004196 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 return;
4198 }
4199 for (i = 0; i < tc_len; ++i)
4200 new_tc[i] = termcodes[i];
4201 vim_free(termcodes);
4202 termcodes = new_tc;
4203 }
4204
4205 /*
4206 * Look for existing entry with the same name, it is replaced.
4207 * Look for an existing entry that is alphabetical higher, the new entry
4208 * is inserted in front of it.
4209 */
4210 for (i = 0; i < tc_len; ++i)
4211 {
4212 if (termcodes[i].name[0] < name[0])
4213 continue;
4214 if (termcodes[i].name[0] == name[0])
4215 {
4216 if (termcodes[i].name[1] < name[1])
4217 continue;
4218 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004219 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 */
4221 if (termcodes[i].name[1] == name[1])
4222 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004223 if (flags == ATC_FROM_TERM && (j = termcode_star(
4224 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004225 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004226 // Don't replace ESC[123;*X or ESC O*X with another when
4227 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004228 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004229 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004230 && s[len - 1]
4231 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004232 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004233 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004234 vim_free(s);
4235 return;
4236 }
4237 }
4238 else
4239 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004240 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004241 vim_free(termcodes[i].code);
4242 --tc_len;
4243 break;
4244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
4246 }
4247 /*
4248 * Found alphabetical larger entry, move rest to insert new entry
4249 */
4250 for (j = tc_len; j > i; --j)
4251 termcodes[j] = termcodes[j - 1];
4252 break;
4253 }
4254
4255 termcodes[i].name[0] = name[0];
4256 termcodes[i].name[1] = name[1];
4257 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004258 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004259
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004260 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4261 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004262 termcodes[i].modlen = 0;
4263 j = termcode_star(s, len);
4264 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004265 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004266 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004267 // For "CSI[@;X" the "@" is not included in "modlen".
4268 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4269 --termcodes[i].modlen;
4270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 ++tc_len;
4272}
4273
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004274/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004275 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004276 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004277 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004278 */
4279 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004280termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004281{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004282 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004283 if (len >= 3 && code[len - 2] == '*')
4284 {
4285 if (len >= 5 && code[len - 3] == ';')
4286 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004287 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004288 return 1;
4289 }
4290 return 0;
4291}
4292
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004294find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295{
4296 int i;
4297
4298 for (i = 0; i < tc_len; ++i)
4299 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4300 return termcodes[i].code;
4301 return NULL;
4302}
4303
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004305get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306{
4307 if (i >= tc_len)
4308 return NULL;
4309 return &termcodes[i].name[0];
4310}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004312/*
4313 * Returns the length of the terminal code at index 'idx'.
4314 */
4315 int
4316get_termcode_len(int idx)
4317{
4318 return termcodes[idx].len;
4319}
4320
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004321 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004322del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323{
4324 int i;
4325
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004326 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return;
4328
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004329 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330
4331 for (i = 0; i < tc_len; ++i)
4332 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4333 {
4334 del_termcode_idx(i);
4335 return;
4336 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004337 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338}
4339
4340 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004341del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342{
4343 int i;
4344
4345 vim_free(termcodes[idx].code);
4346 --tc_len;
4347 for (i = idx; i < tc_len; ++i)
4348 termcodes[i] = termcodes[i + 1];
4349}
4350
4351#ifdef FEAT_TERMRESPONSE
4352/*
4353 * Called when detected that the terminal sends 8-bit codes.
4354 * Convert all 7-bit codes to their 8-bit equivalent.
4355 */
4356 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004357switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358{
4359 int i;
4360 int c;
4361
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004362 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 if (!term_is_8bit(T_NAME))
4364 {
4365 for (i = 0; i < tc_len; ++i)
4366 {
4367 c = term_7to8bit(termcodes[i].code);
4368 if (c != 0)
4369 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004370 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 termcodes[i].code[0] = c;
4372 }
4373 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004374 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004377 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378}
4379#endif
4380
4381#ifdef CHECK_DOUBLE_CLICK
4382static linenr_T orig_topline = 0;
4383# ifdef FEAT_DIFF
4384static int orig_topfill = 0;
4385# endif
4386#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004387#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004389 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 * "orig_topline" is used to avoid detecting a double-click when the window
4391 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4392 */
4393/*
4394 * Set orig_topline. Used when jumping to another window, so that a double
4395 * click still works.
4396 */
4397 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004398set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
4400 orig_topline = wp->w_topline;
4401# ifdef FEAT_DIFF
4402 orig_topfill = wp->w_topfill;
4403# endif
4404}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004405
4406/*
4407 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4408 * topline and topfill.
4409 */
4410 int
4411is_mouse_topline(win_T *wp)
4412{
4413 return orig_topline == wp->w_topline
4414#ifdef FEAT_DIFF
4415 && orig_topfill == wp->w_topfill
4416#endif
4417 ;
4418}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419#endif
4420
4421/*
zeertzjqfc78a032022-04-26 22:11:38 +01004422 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4423 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4424 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004425 * Remove "slen" bytes.
4426 * Returns FAIL for error.
4427 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004428 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004429put_string_in_typebuf(
4430 int offset,
4431 int slen,
4432 char_u *string,
4433 int new_slen,
4434 char_u *buf,
4435 int bufsize,
4436 int *buflen)
4437{
4438 int extra = new_slen - slen;
4439
4440 string[new_slen] = NUL;
4441 if (buf == NULL)
4442 {
4443 if (extra < 0)
4444 // remove matched chars, taking care of noremap
4445 del_typebuf(-extra, offset);
4446 else if (extra > 0)
4447 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004448 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4449 == FAIL)
4450 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004451
4452 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4453 // typebuf.tb_buf[]!
4454 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4455 (size_t)new_slen);
4456 }
4457 else
4458 {
4459 if (extra < 0)
4460 // remove matched characters
4461 mch_memmove(buf + offset, buf + offset - extra,
4462 (size_t)(*buflen + offset + extra));
4463 else if (extra > 0)
4464 {
4465 // Insert the extra space we need. If there is insufficient
4466 // space return -1.
4467 if (*buflen + extra + new_slen >= bufsize)
4468 return FAIL;
4469 mch_memmove(buf + offset + extra, buf + offset,
4470 (size_t)(*buflen - offset));
4471 }
4472 mch_memmove(buf + offset, string, (size_t)new_slen);
4473 *buflen = *buflen + extra + new_slen;
4474 }
4475 return OK;
4476}
4477
4478/*
4479 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4480 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004481 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004482decode_modifiers(int n)
4483{
4484 int code = n - 1;
4485 int modifiers = 0;
4486
4487 if (code & 1)
4488 modifiers |= MOD_MASK_SHIFT;
4489 if (code & 2)
4490 modifiers |= MOD_MASK_ALT;
4491 if (code & 4)
4492 modifiers |= MOD_MASK_CTRL;
4493 if (code & 8)
4494 modifiers |= MOD_MASK_META;
4495 return modifiers;
4496}
4497
4498 static int
4499modifiers2keycode(int modifiers, int *key, char_u *string)
4500{
4501 int new_slen = 0;
4502
4503 if (modifiers != 0)
4504 {
4505 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004506 // make mappings work. This may result in a special key, such as
4507 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004508 *key = simplify_key(*key, &modifiers);
4509 if (modifiers != 0)
4510 {
4511 string[new_slen++] = K_SPECIAL;
4512 string[new_slen++] = (int)KS_MODIFIER;
4513 string[new_slen++] = modifiers;
4514 }
4515 }
4516 return new_slen;
4517}
4518
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004519#ifdef FEAT_TERMRESPONSE
4520/*
4521 * Handle a cursor position report.
4522 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004523 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004524handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004525{
4526 if (arg[0] == 2 && arg[1] >= 2)
4527 {
4528 char *aw = NULL;
4529
4530 LOG_TR(("Received U7 status: %s", tp));
4531 u7_status.tr_progress = STATUS_GOT;
4532 did_cursorhold = TRUE;
4533 if (arg[1] == 2)
4534 aw = "single";
4535 else if (arg[1] == 3)
4536 aw = "double";
4537 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4538 {
4539 // Setting the option causes a screen redraw. Do
4540 // that right away if possible, keeping any
4541 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004542 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004543# ifdef DEBUG_TERMRESPONSE
4544 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004545 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004546
4547 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4548 }
4549# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004550 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004551# endif
4552# ifdef FEAT_EVAL
4553 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4554# endif
4555 }
4556 }
4557 else if (arg[0] == 3)
4558 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004559 int value;
4560
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004561 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004562 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004563
4564 // Third row: xterm compatibility test.
4565 // If the cursor is on the first column then the terminal can handle
4566 // the request for cursor style and blinking.
4567 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4568 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4569 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004570 }
4571}
4572
4573/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004574 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004575 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004576 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004577 */
4578 static void
4579handle_version_response(int first, int *arg, int argc, char_u *tp)
4580{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004581 // The xterm version. It is set to zero when it can't be an actual xterm
4582 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004583 int version = arg[1];
4584
4585 LOG_TR(("Received CRV response: %s", tp));
4586 crv_status.tr_progress = STATUS_GOT;
4587 did_cursorhold = TRUE;
4588
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004589 // Reset terminal properties that are set based on the termresponse.
4590 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004591 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004592 init_term_props(
4593#ifdef FEAT_EVAL
4594 reset_term_props_on_termresponse
4595#else
4596 FALSE
4597#endif
4598 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004599
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004600 // If this code starts with CSI, you can bet that the
4601 // terminal uses 8-bit codes.
4602 if (tp[0] == CSI)
4603 switch_to_8bit();
4604
4605 // Screen sends 40500.
4606 // rxvt sends its version number: "20703" is 2.7.3.
4607 // Ignore it for when the user has set 'term' to xterm,
4608 // even though it's an rxvt.
4609 if (version > 20000)
4610 version = 0;
4611
zeertzjqfc78a032022-04-26 22:11:38 +01004612 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004613 if (first == '>' && argc == 3)
4614 {
4615 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004616
4617 // mintty 2.9.5 sends 77;20905;0c.
4618 // (77 is ASCII 'M' for mintty.)
4619 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004620 {
4621 // mintty can do SGR mouse reporting
4622 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4623 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004624
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004625 // If xterm version >= 141 try to get termcap codes. For other
4626 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004627 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004628 {
4629 LOG_TR(("Enable checking for XT codes"));
4630 check_for_codes = TRUE;
4631 need_gather = TRUE;
4632 req_codes_from_term();
4633 }
4634
4635 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01004636 // Konsole sends 0;115;0 and works the same way
4637 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004638 {
4639 // If run from Vim $COLORS is set to the number of
4640 // colors the terminal supports. Otherwise assume
4641 // 256, libvterm supports even more.
4642 if (mch_getenv((char_u *)"COLORS") == NULL)
4643 may_adjust_color_count(256);
4644 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004645 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004646 }
4647
4648 if (version == 95)
4649 {
4650 // Mac Terminal.app sends 1;95;0
4651 if (arg[0] == 1 && arg[2] == 0)
4652 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004653 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4654 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004655 }
4656 // iTerm2 sends 0;95;0
4657 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004658 {
4659 // iTerm2 can do SGR mouse reporting
4660 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4661 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004662 // old iTerm2 sends 0;95;
4663 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004664 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004665 }
4666
4667 // screen sends 83;40500;0 83 is 'S' in ASCII.
4668 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004669 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004670 // screen supports SGR mouse codes since 4.7.0
4671 if (arg[1] >= 40700)
4672 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4673 else
4674 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4675 }
4676
4677 // If no recognized terminal has set mouse behavior, assume xterm.
4678 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4679 {
4680 // Xterm version 277 supports SGR.
4681 // Xterm version >= 95 supports mouse dragging.
4682 if (version >= 277)
4683 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004684 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004685 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004686 }
4687
4688 // Detect terminals that set $TERM to something like
4689 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004690 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004691 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004692 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004693 // xfce4-terminal sends 1;2802;0.
4694 // screen sends 83;40500;0
4695 // Assuming any version number over 2500 is not an
4696 // xterm (without the limit for rxvt and screen).
4697 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004698 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004699
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004700 else if (version == 136 && arg[2] == 0)
4701 {
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 Moolenaar517f00f2020-06-10 12:15:51 +02004704 // PuTTY sends 0;136;0
4705 if (arg[0] == 0)
4706 {
4707 // supports sgr-like mouse reporting.
4708 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4709 }
4710 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004711 }
4712
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004713 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
4714 // commented out.
4715 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
4716 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004717
4718 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004719 // 30600/40500 is a version number of GNU screen. DA2 support is added
4720 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4721 // compatibility checking does not detect GNU screen.
4722 if (arg[0] == 83 && arg[1] >= 30600)
4723 {
4724 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4725 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4726 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004727
4728 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004729 // 95, so assume anything below 95 is not xterm and hopefully supports
4730 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004731 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004732 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004733
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004734 // Getting the cursor style is only supported properly by xterm since
4735 // version 279 (otherwise it returns 0x18).
4736 if (version < 279)
4737 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4738
4739 /*
4740 * Take action on the detected properties.
4741 */
4742
4743 // Unless the underline RGB color is expected to work, disable "t_8u".
4744 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004745 // This may cause some flicker. Alternative would be to set "t_8u"
4746 // here if the terminal is expected to support it, but that might
4747 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01004748 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
4749 && *T_8U != NUL
4750 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004751 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004752 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4753 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004754 }
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01004755 if (*T_8U != NUL && write_t_8u_state == MAYBE)
4756 // Did skip writing t_8u, a complete redraw is needed.
4757 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01004758 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004759
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004760 // Only set 'ttymouse' automatically if it was not set
4761 // by the user already.
4762 if (!option_was_set((char_u *)"ttym")
4763 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4764 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4765 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004766 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004767 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4768 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4769 }
4770
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004771 // Only request the cursor style if t_SH and t_RS are
4772 // set. Only supported properly by xterm since version
4773 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004774 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004775 // Not for Terminal.app, it can't handle t_RS, it
4776 // echoes the characters to the screen.
4777 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004778 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004779 && *T_CSH != NUL
4780 && *T_CRS != NUL)
4781 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004782 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004783 LOG_TR(("Sending cursor style request"));
4784 out_str(T_CRS);
4785 termrequest_sent(&rcs_status);
4786 need_flush = TRUE;
4787 }
4788
4789 // Only request the cursor blink mode if t_RC set. Not
4790 // for Gnome terminal, it can't handle t_RC, it
4791 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004792 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004793 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004794 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004795 && *T_CRC != NUL)
4796 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004797 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004798 LOG_TR(("Sending cursor blink mode request"));
4799 out_str(T_CRC);
4800 termrequest_sent(&rbm_status);
4801 need_flush = TRUE;
4802 }
4803
4804 if (need_flush)
4805 out_flush();
4806 }
4807}
4808
4809/*
4810 * Handle a sequence with key and modifier, one of:
4811 * {lead}27;{modifier};{key}~
4812 * {lead}{key};{modifier}u
4813 * Returns the difference in length.
4814 */
4815 static int
4816handle_key_with_modifier(
4817 int *arg,
4818 int trail,
4819 int csi_len,
4820 int offset,
4821 char_u *buf,
4822 int bufsize,
4823 int *buflen)
4824{
4825 int key;
4826 int modifiers;
4827 int new_slen;
4828 char_u string[MAX_KEY_CODE_LEN + 1];
4829
4830 seenModifyOtherKeys = TRUE;
4831 if (trail == 'u')
4832 key = arg[0];
4833 else
4834 key = arg[2];
4835
4836 modifiers = decode_modifiers(arg[1]);
4837
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02004838 // Some keys need adjustment when the Ctrl modifier is used.
4839 key = may_adjust_key_for_ctrl(modifiers, key);
4840
Bram Moolenaaref6746f2020-06-20 14:43:23 +02004841 // May remove the shift modifier if it's already included in the key.
4842 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004843
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004844 // insert modifiers with KS_MODIFIER
4845 new_slen = modifiers2keycode(modifiers, &key, string);
4846
Bram Moolenaar749bc952020-10-31 16:33:47 +01004847 if (IS_SPECIAL(key))
4848 {
4849 string[new_slen++] = K_SPECIAL;
4850 string[new_slen++] = KEY2TERMCAP0(key);
4851 string[new_slen++] = KEY2TERMCAP1(key);
4852 }
4853 else if (has_mbyte)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004854 new_slen += (*mb_char2bytes)(key, string + new_slen);
4855 else
4856 string[new_slen++] = key;
4857
4858 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4859 buf, bufsize, buflen) == FAIL)
4860 return -1;
4861 return new_slen - csi_len + offset;
4862}
4863
4864/*
4865 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004866 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004867 *
4868 * - Cursor position report: {lead}{row};{col}R
4869 * The final byte must be 'R'. It is used for checking the
4870 * ambiguous-width character state.
4871 *
4872 * - window position reply: {lead}3;{x};{y}t
4873 *
4874 * - key with modifiers when modifyOtherKeys is enabled:
4875 * {lead}27;{modifier};{key}~
4876 * {lead}{key};{modifier}u
4877 * Return 0 for no match, -1 for partial match, > 0 for full match.
4878 */
4879 static int
4880handle_csi(
4881 char_u *tp,
4882 int len,
4883 char_u *argp,
4884 int offset,
4885 char_u *buf,
4886 int bufsize,
4887 int *buflen,
4888 char_u *key_name,
4889 int *slen)
4890{
4891 int first = -1; // optional char right after {lead}
4892 int trail; // char that ends CSI sequence
4893 int arg[3] = {-1, -1, -1}; // argument numbers
4894 int argc; // number of arguments
4895 char_u *ap = argp;
4896 int csi_len;
4897
4898 // Check for non-digit after CSI.
4899 if (!VIM_ISDIGIT(*ap))
4900 first = *ap++;
4901
4902 // Find up to three argument numbers.
4903 for (argc = 0; argc < 3; )
4904 {
4905 if (ap >= tp + len)
4906 return -1;
4907 if (*ap == ';')
4908 arg[argc++] = -1; // omitted number
4909 else if (VIM_ISDIGIT(*ap))
4910 {
4911 arg[argc] = 0;
4912 for (;;)
4913 {
4914 if (ap >= tp + len)
4915 return -1;
4916 if (!VIM_ISDIGIT(*ap))
4917 break;
4918 arg[argc] = arg[argc] * 10 + (*ap - '0');
4919 ++ap;
4920 }
4921 ++argc;
4922 }
4923 if (*ap == ';')
4924 ++ap;
4925 else
4926 break;
4927 }
4928
4929 // mrxvt has been reported to have "+" in the version. Assume
4930 // the escape sequence ends with a letter or one of "{|}~".
4931 while (ap < tp + len
4932 && !(*ap >= '{' && *ap <= '~')
4933 && !ASCII_ISALPHA(*ap))
4934 ++ap;
4935 if (ap >= tp + len)
4936 return -1;
4937 trail = *ap;
4938 csi_len = (int)(ap - tp) + 1;
4939
4940 // Cursor position report: Eat it when there are 2 arguments
4941 // and it ends in 'R'. Also when u7_status is not "sent", it
4942 // may be from a previous Vim that just exited. But not for
4943 // <S-F3>, it sends something similar, check for row and column
4944 // to make sense.
4945 if (first == -1 && argc == 2 && trail == 'R')
4946 {
4947 handle_u7_response(arg, tp, csi_len);
4948
4949 key_name[0] = (int)KS_EXTRA;
4950 key_name[1] = (int)KE_IGNORE;
4951 *slen = csi_len;
4952 }
4953
4954 // Version string: Eat it when there is at least one digit and
4955 // it ends in 'c'
4956 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
4957 {
4958 handle_version_response(first, arg, argc, tp);
4959
4960 *slen = csi_len;
4961# ifdef FEAT_EVAL
4962 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
4963# endif
4964 apply_autocmds(EVENT_TERMRESPONSE,
4965 NULL, NULL, FALSE, curbuf);
4966 key_name[0] = (int)KS_EXTRA;
4967 key_name[1] = (int)KE_IGNORE;
4968 }
4969
4970 // Check blinking cursor from xterm:
4971 // {lead}?12;1$y set
4972 // {lead}?12;2$y not set
4973 //
4974 // {lead} can be <Esc>[ or CSI
4975 else if (rbm_status.tr_progress == STATUS_SENT
4976 && first == '?'
4977 && ap == argp + 6
4978 && arg[0] == 12
4979 && ap[-1] == '$'
4980 && trail == 'y')
4981 {
4982 initial_cursor_blink = (arg[1] == '1');
4983 rbm_status.tr_progress = STATUS_GOT;
4984 LOG_TR(("Received cursor blinking mode response: %s", tp));
4985 key_name[0] = (int)KS_EXTRA;
4986 key_name[1] = (int)KE_IGNORE;
4987 *slen = csi_len;
4988# ifdef FEAT_EVAL
4989 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
4990# endif
4991 }
4992
4993 // Check for a window position response from the terminal:
4994 // {lead}3;{x};{y}t
4995 else if (did_request_winpos && argc == 3 && arg[0] == 3
4996 && trail == 't')
4997 {
4998 winpos_x = arg[1];
4999 winpos_y = arg[2];
5000 // got finished code: consume it
5001 key_name[0] = (int)KS_EXTRA;
5002 key_name[1] = (int)KE_IGNORE;
5003 *slen = csi_len;
5004
5005 if (--did_request_winpos <= 0)
5006 winpos_status.tr_progress = STATUS_GOT;
5007 }
5008
5009 // Key with modifier:
5010 // {lead}27;{modifier};{key}~
5011 // {lead}{key};{modifier}u
5012 else if ((arg[0] == 27 && argc == 3 && trail == '~')
5013 || (argc == 2 && trail == 'u'))
5014 {
5015 return len + handle_key_with_modifier(arg, trail,
5016 csi_len, offset, buf, bufsize, buflen);
5017 }
5018
5019 // else: Unknown CSI sequence. We could drop it, but then the
5020 // user can't create a map for it.
5021 return 0;
5022}
5023
5024/*
5025 * Handle an OSC sequence, fore/background color response from the terminal:
5026 *
5027 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5028 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5029 *
5030 * {code} is 10 for foreground, 11 for background
5031 * {lead} can be <Esc>] or OSC
5032 * {tail} can be '\007', <Esc>\ or STERM.
5033 *
5034 * Consume any code that starts with "{lead}11;", it's also
5035 * possible that "rgba" is following.
5036 */
5037 static int
5038handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5039{
5040 int i, j;
5041
5042 j = 1 + (tp[0] == ESC);
5043 if (len >= j + 3 && (argp[0] != '1'
5044 || (argp[1] != '1' && argp[1] != '0')
5045 || argp[2] != ';'))
5046 i = 0; // no match
5047 else
5048 for (i = j; i < len; ++i)
5049 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5050 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5051 {
5052 int is_bg = argp[1] == '1';
5053 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5054 && tp[j + 16] == '/';
5055
5056 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5057 && (is_4digit
5058 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5059 {
5060 char_u *tp_r = tp + j + 7;
5061 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5062 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
5063# ifdef FEAT_TERMINAL
5064 int rval, gval, bval;
5065
5066 rval = hexhex2nr(tp_r);
5067 gval = hexhex2nr(tp_b);
5068 bval = hexhex2nr(tp_g);
5069# endif
5070 if (is_bg)
5071 {
5072 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5073 *tp_b) ? "light" : "dark";
5074
5075 LOG_TR(("Received RBG response: %s", tp));
5076 rbg_status.tr_progress = STATUS_GOT;
5077# ifdef FEAT_TERMINAL
5078 bg_r = rval;
5079 bg_g = gval;
5080 bg_b = bval;
5081# endif
5082 if (!option_was_set((char_u *)"bg")
5083 && STRCMP(p_bg, new_bg_val) != 0)
5084 {
5085 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005086 set_option_value_give_err((char_u *)"bg",
5087 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005088 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005089 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005090 }
5091 }
5092# ifdef FEAT_TERMINAL
5093 else
5094 {
5095 LOG_TR(("Received RFG response: %s", tp));
5096 rfg_status.tr_progress = STATUS_GOT;
5097 fg_r = rval;
5098 fg_g = gval;
5099 fg_b = bval;
5100 }
5101# endif
5102 }
5103
5104 // got finished code: consume it
5105 key_name[0] = (int)KS_EXTRA;
5106 key_name[1] = (int)KE_IGNORE;
5107 *slen = i + 1 + (tp[i] == ESC);
5108# ifdef FEAT_EVAL
5109 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5110 : VV_TERMRFGRESP, tp, *slen);
5111# endif
5112 break;
5113 }
5114 if (i == len)
5115 {
5116 LOG_TR(("not enough characters for RB"));
5117 return FAIL;
5118 }
5119 return OK;
5120}
5121
5122/*
5123 * Check for key code response from xterm:
5124 * {lead}{flag}+r<hex bytes><{tail}
5125 *
5126 * {lead} can be <Esc>P or DCS
5127 * {flag} can be '0' or '1'
5128 * {tail} can be Esc>\ or STERM
5129 *
5130 * Check for cursor shape response from xterm:
5131 * {lead}1$r<digit> q{tail}
5132 *
5133 * {lead} can be <Esc>P or DCS
5134 * {tail} can be <Esc>\ or STERM
5135 *
5136 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5137 */
5138 static int
5139handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5140{
5141 int i, j;
5142
5143 j = 1 + (tp[0] == ESC);
5144 if (len < j + 3)
5145 i = len; // need more chars
5146 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
5147 i = 0; // no match
5148 else if (argp[1] == '+')
5149 // key code response
5150 for (i = j; i < len; ++i)
5151 {
5152 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5153 || tp[i] == STERM)
5154 {
5155 if (i - j >= 3)
5156 got_code_from_term(tp + j, i);
5157 key_name[0] = (int)KS_EXTRA;
5158 key_name[1] = (int)KE_IGNORE;
5159 *slen = i + 1 + (tp[i] == ESC);
5160 break;
5161 }
5162 }
5163 else
5164 {
5165 // Probably the cursor shape response. Make sure that "i"
5166 // is equal to "len" when there are not sufficient
5167 // characters.
5168 for (i = j + 3; i < len; ++i)
5169 {
5170 if (i - j == 3 && !isdigit(tp[i]))
5171 break;
5172 if (i - j == 4 && tp[i] != ' ')
5173 break;
5174 if (i - j == 5 && tp[i] != 'q')
5175 break;
5176 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5177 break;
5178 if ((i - j == 6 && tp[i] == STERM)
5179 || (i - j == 7 && tp[i] == '\\'))
5180 {
5181 int number = argp[3] - '0';
5182
5183 // 0, 1 = block blink, 2 = block
5184 // 3 = underline blink, 4 = underline
5185 // 5 = vertical bar blink, 6 = vertical bar
5186 number = number == 0 ? 1 : number;
5187 initial_cursor_shape = (number + 1) / 2;
5188 // The blink flag is actually inverted, compared to
5189 // the value set with T_SH.
5190 initial_cursor_shape_blink =
5191 (number & 1) ? FALSE : TRUE;
5192 rcs_status.tr_progress = STATUS_GOT;
5193 LOG_TR(("Received cursor shape response: %s", tp));
5194
5195 key_name[0] = (int)KS_EXTRA;
5196 key_name[1] = (int)KE_IGNORE;
5197 *slen = i + 1;
5198# ifdef FEAT_EVAL
5199 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5200# endif
5201 break;
5202 }
5203 }
5204 }
5205
5206 if (i == len)
5207 {
5208 // These codes arrive many together, each code can be
5209 // truncated at any point.
5210 LOG_TR(("not enough characters for XT"));
5211 return FAIL;
5212 }
5213 return OK;
5214}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005215#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005216
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 * Check if typebuf.tb_buf[] contains a terminal key code.
5219 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005220 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005222 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 * With a match, the match is removed, the replacement code is inserted in
5224 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5225 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005226 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5227 * "buflen" is then the length of the string in buf[] and is updated for
5228 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 */
5230 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005231check_termcode(
5232 int max_offset,
5233 char_u *buf,
5234 int bufsize,
5235 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236{
5237 char_u *tp;
5238 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005239 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005240 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005242 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 int offset;
5244 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005245 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005246 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005247 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005248 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 char_u string[MAX_KEY_CODE_LEN + 1];
5250 int i, j;
5251 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253
5254 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5255
5256 /*
5257 * Speed up the checks for terminal codes by gathering all first bytes
5258 * used in termleader[]. Often this is just a single <Esc>.
5259 */
5260 if (need_gather)
5261 gather_termleader();
5262
5263 /*
5264 * Check at several positions in typebuf.tb_buf[], to catch something like
5265 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5266 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005267 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 * This is used often, KEEP IT FAST!
5269 */
5270 for (offset = 0; offset < max_offset; ++offset)
5271 {
5272 if (buf == NULL)
5273 {
5274 if (offset >= typebuf.tb_len)
5275 break;
5276 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005277 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279 else
5280 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005281 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 break;
5283 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005284 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286
5287 /*
5288 * Don't check characters after K_SPECIAL, those are already
5289 * translated terminal chars (avoid translating ~@^Hx).
5290 */
5291 if (*tp == K_SPECIAL)
5292 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005293 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 continue;
5295 }
5296
5297 /*
5298 * Skip this position if the character does not appear as the first
5299 * character in term_strings. This speeds up a lot, since most
5300 * termcodes start with the same character (ESC or CSI).
5301 */
5302 i = *tp;
5303 for (p = termleader; *p && *p != i; ++p)
5304 ;
5305 if (*p == NUL)
5306 continue;
5307
5308 /*
5309 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5310 * are in Insert mode.
5311 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005312 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 continue;
5314
Bram Moolenaar27efc622022-07-01 16:35:45 +01005315 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005316 key_name[0] = NUL; // no key name found yet
5317 key_name[1] = NUL; // no key name found yet
5318 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
5320#ifdef FEAT_GUI
5321 if (gui.in_use)
5322 {
5323 /*
5324 * GUI special key codes are all of the form [CSI xx].
5325 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005326 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
5328 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005329 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 slen = 3;
5331 key_name[0] = tp[1];
5332 key_name[1] = tp[2];
5333 }
5334 }
5335 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005336#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005338 int mouse_index_found = -1;
5339
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 for (idx = 0; idx < tc_len; ++idx)
5341 {
5342 /*
5343 * Ignore the entry if we are not at the start of
5344 * typebuf.tb_buf[]
5345 * and there are not enough characters to make a match.
5346 * But only when the 'K' flag is in 'cpoptions'.
5347 */
5348 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005349 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 if (cpo_koffset && offset && len < slen)
5351 continue;
5352 if (STRNCMP(termcodes[idx].code, tp,
5353 (size_t)(slen > len ? len : slen)) == 0)
5354 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005355 int looks_like_mouse_start = FALSE;
5356
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005357 if (len < slen) // got a partial sequence
5358 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359
5360 /*
5361 * When found a keypad key, check if there is another key
5362 * that matches and use that one. This makes <Home> to be
5363 * found instead of <kHome> when they produce the same
5364 * key code.
5365 */
5366 if (termcodes[idx].name[0] == 'K'
5367 && VIM_ISDIGIT(termcodes[idx].name[1]))
5368 {
5369 for (j = idx + 1; j < tc_len; ++j)
5370 if (termcodes[j].len == slen &&
5371 STRNCMP(termcodes[idx].code,
5372 termcodes[j].code, slen) == 0)
5373 {
5374 idx = j;
5375 break;
5376 }
5377 }
5378
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005379 if (slen == 2 && len > 2
5380 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005381 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005382 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005383 // The mouse termcode "ESC [" is also the prefix of
5384 // "ESC [ I" (focus gained) and other keys. Check some
5385 // more bytes to find out.
5386 if (!isdigit(tp[2]))
5387 {
5388 // ESC [ without number following: Only use it when
5389 // there is no other match.
5390 looks_like_mouse_start = TRUE;
5391 }
5392 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5393 {
5394 char_u *nr = tp + 2;
5395 int count = 0;
5396
5397 // If a digit is following it could be a key with
5398 // modifier, e.g., ESC [ 1;2P. Can be confused
5399 // with DEC_MOUSE, which requires four numbers
5400 // following. If not then it can't be a DEC_MOUSE
5401 // code.
5402 for (;;)
5403 {
5404 ++count;
5405 (void)getdigits(&nr);
5406 if (nr >= tp + len)
5407 return -1; // partial sequence
5408 if (*nr != ';')
5409 break;
5410 ++nr;
5411 if (nr >= tp + len)
5412 return -1; // partial sequence
5413 }
5414 if (count < 4)
5415 continue; // no match
5416 }
5417 }
5418 if (looks_like_mouse_start)
5419 {
5420 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005421 if (mouse_index_found < 0)
5422 mouse_index_found = idx;
5423 }
5424 else
5425 {
5426 key_name[0] = termcodes[idx].name[0];
5427 key_name[1] = termcodes[idx].name[1];
5428 break;
5429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005431
5432 /*
5433 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005434 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005435 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005436 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5437 * When there is a modifier the * matches a number.
5438 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005439 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005440 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005441 {
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005442 int at_code;
5443
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005444 modslen = termcodes[idx].modlen;
5445 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005446 continue;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005447 at_code = termcodes[idx].code[modslen] == '@';
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005448 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005449 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005450 {
5451 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005452
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005453 if (len <= modslen) // got a partial sequence
5454 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005455
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005456 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005457 // no modifiers
5458 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005459 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005460 // no match for "code;*X" with "code;"
5461 continue;
5462 else if (at_code && tp[modslen] != '1')
5463 // no match for "<Esc>[@" with "<Esc>[1"
5464 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005465 else
5466 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005467 // Skip over the digits, the final char must
5468 // follow. URXVT can use a negative value, thus
5469 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005470 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005471 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005472 ;
5473 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005474 if (len < j) // got a partial sequence
5475 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005476 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005477 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005478
Bram Moolenaara529ce02017-06-22 22:37:57 +02005479 modifiers_start = tp + slen - 2;
5480
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005481 // Match! Convert modifier bits.
5482 n = atoi((char *)modifiers_start);
5483 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005484
5485 slen = j;
5486 }
5487 key_name[0] = termcodes[idx].name[0];
5488 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005489 break;
5490 }
5491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005493 if (idx == tc_len && mouse_index_found >= 0)
5494 {
5495 key_name[0] = termcodes[mouse_index_found].name[0];
5496 key_name[1] = termcodes[mouse_index_found].name[1];
5497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
5499
5500#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005501 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005502 // Mouse codes of DEC and pterm start with <ESC>[. When
5503 // detecting the start of these mouse codes they might as well be
5504 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005505# ifdef FEAT_MOUSE_DEC
5506 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005507# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005508# ifdef FEAT_MOUSE_PTERM
5509 || key_name[0] == KS_PTERM_MOUSE
5510# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005511 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005513 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5514
5515 /*
5516 * Check for responses from the terminal starting with {lead}:
5517 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005518 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005519 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005520 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005521 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005522 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005523 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005524 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005525 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005526 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005527 * - window position reply: {lead}3;{x};{y}t
5528 *
5529 * - key with modifiers when modifyOtherKeys is enabled:
5530 * {lead}27;{modifier};{key}~
5531 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005532 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005533 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005534 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005535 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005537 int resp = handle_csi(tp, len, argp, offset, buf,
5538 bufsize, buflen, key_name, &slen);
5539 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005540 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005541# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005542 if (resp == -1)
5543 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005544# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005545 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005546 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005547 }
5548
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005549 // Check for fore/background color response from the terminal,
5550 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005551 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005552 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5553 || tp[0] == OSC))
5554 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005555 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005556 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
5558
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005559 // Check for key code response from xterm,
5560 // starting with <Esc>P or DCS
Bram Moolenaarafd78262019-05-10 23:10:31 +02005561 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005562 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 || tp[0] == DCS))
5564 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005565 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005566 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 }
5568 }
5569#endif
5570
5571 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005572 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005574 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005576#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 /*
5578 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5579 * so that we know which window to scroll later.
5580 */
5581 if (gui.in_use
5582 && key_name[0] == (int)KS_EXTRA
5583 && (key_name[1] == (int)KE_X1MOUSE
5584 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005585 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005586 || key_name[1] == (int)KE_MOUSELEFT
5587 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 || key_name[1] == (int)KE_MOUSEDOWN
5589 || key_name[1] == (int)KE_MOUSEUP))
5590 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005591 char_u bytes[6];
5592 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5593
5594 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 return -1;
5596 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5597 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5598 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005599 // equal to K_MOUSEMOVE
5600 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
5601 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
5603 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 /*
5606 * If it is a mouse click, get the coordinates.
5607 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005608 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005609#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005610 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005611#endif
5612#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005613 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005614#endif
5615#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005616 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005617#endif
5618#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005619 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005620#endif
5621#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005622 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005623#endif
5624#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005625 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005626#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005627 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005628 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005630 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5631 &modifiers) == -1)
5632 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634
5635#ifdef FEAT_GUI
5636 /*
5637 * If using the GUI, then we get menu and scrollbar events.
5638 *
5639 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5640 * four bytes which are to be taken as a pointer to the vimmenu_T
5641 * structure.
5642 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005643 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005644 * is one byte with the tab index.
5645 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5647 * by one byte representing the scrollbar number, and then four bytes
5648 * representing a long_u which is the new value of the scrollbar.
5649 *
5650 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5651 * KE_FILLER followed by four bytes representing a long_u which is the
5652 * new value of the scrollbar.
5653 */
5654# ifdef FEAT_MENU
5655 else if (key_name[0] == (int)KS_MENU)
5656 {
5657 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005658 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 if (num_bytes == -1)
5661 return -1;
5662 current_menu = (vimmenu_T *)val;
5663 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005664
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005665 // The menu may have been deleted right after it was used, check
5666 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005667 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5668 {
5669 key_name[0] = KS_EXTRA;
5670 key_name[1] = (int)KE_IGNORE;
5671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005674# ifdef FEAT_GUI_TABLINE
5675 else if (key_name[0] == (int)KS_TABLINE)
5676 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005677 // Selecting tabline tab or using its menu.
5678 char_u bytes[6];
5679 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5680
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005681 if (num_bytes == -1)
5682 return -1;
5683 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005684 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005685 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005686 slen += num_bytes;
5687 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005688 else if (key_name[0] == (int)KS_TABMENU)
5689 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005690 // Selecting tabline tab or using its menu.
5691 char_u bytes[6];
5692 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5693
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005694 if (num_bytes == -1)
5695 return -1;
5696 current_tab = (int)bytes[0];
5697 current_tabmenu = (int)bytes[1];
5698 slen += num_bytes;
5699 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005700# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701# ifndef USE_ON_FLY_SCROLL
5702 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5703 {
5704 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005705 char_u bytes[6];
5706 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005708 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 j = 0;
5710 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5711 && tp[j + 2] != NUL; ++i)
5712 {
5713 j += 3;
5714 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5715 if (num_bytes == -1)
5716 break;
5717 if (i == 0)
5718 current_scrollbar = (int)bytes[0];
5719 else if (current_scrollbar != (int)bytes[0])
5720 break;
5721 j += num_bytes;
5722 num_bytes = get_long_from_buf(tp + j, &val);
5723 if (num_bytes == -1)
5724 break;
5725 scrollbar_value = val;
5726 j += num_bytes;
5727 slen = j;
5728 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005729 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 return -1;
5731 }
5732 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5733 {
5734 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005735 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005737 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 j = 0;
5739 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5740 && tp[j + 2] != NUL; ++i)
5741 {
5742 j += 3;
5743 num_bytes = get_long_from_buf(tp + j, &val);
5744 if (num_bytes == -1)
5745 break;
5746 scrollbar_value = val;
5747 j += num_bytes;
5748 slen = j;
5749 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005750 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 return -1;
5752 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005753# endif // !USE_ON_FLY_SCROLL
5754#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005756#if (defined(UNIX) || defined(VMS))
5757 /*
5758 * Handle FocusIn/FocusOut event sequences reported by XTerm.
5759 * (CSI I/CSI O)
5760 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00005761 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005762# ifdef FEAT_GUI
5763 && !gui.in_use
5764# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005765 )
5766 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005767 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005768 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005769 if (!focus_state)
5770 {
5771 ui_focus_change(TRUE);
5772 did_cursorhold = TRUE;
5773 focus_state = TRUE;
5774 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005775 key_name[1] = (int)KE_IGNORE;
5776 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01005777 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005778 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005779 if (focus_state)
5780 {
5781 ui_focus_change(FALSE);
5782 did_cursorhold = TRUE;
5783 focus_state = FALSE;
5784 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005785 key_name[1] = (int)KE_IGNORE;
5786 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005787 }
5788#endif
5789
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005790 /*
5791 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5792 */
5793 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5794
5795 /*
5796 * Add any modifier codes to our string.
5797 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005798 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005799
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005800 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005801 key_name[0] = KEY2TERMCAP0(key);
5802 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005804 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005805 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005806 if (has_mbyte)
5807 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5808 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005809 string[new_slen++] = key_name[1];
5810 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005811 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5812 && key_name[1] == KE_IGNORE)
5813 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005814 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5815 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005816 retval = KEYLEN_REMOVED;
5817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 else
5819 {
5820 string[new_slen++] = K_SPECIAL;
5821 string[new_slen++] = key_name[0];
5822 string[new_slen++] = key_name[1];
5823 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005824 if (put_string_in_typebuf(offset, slen, string, new_slen,
5825 buf, bufsize, buflen) == FAIL)
5826 return -1;
5827 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 }
5829
Bram Moolenaar2951b772013-07-03 12:45:31 +02005830#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005831 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005832#endif
5833
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005834 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835}
5836
Bram Moolenaar9377df32017-10-15 13:22:01 +02005837#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005838/*
5839 * Get the text foreground color, if known.
5840 */
5841 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005842term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005843{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005844 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005845 {
5846 *r = fg_r;
5847 *g = fg_g;
5848 *b = fg_b;
5849 }
5850}
5851
5852/*
5853 * Get the text background color, if known.
5854 */
5855 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005856term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005857{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005858 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005859 {
5860 *r = bg_r;
5861 *g = bg_g;
5862 *b = bg_b;
5863 }
5864}
5865#endif
5866
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867/*
5868 * Replace any terminal code strings in from[] with the equivalent internal
5869 * vim representation. This is used for the "from" and "to" part of a
5870 * mapping, and the "to" part of a menu command.
5871 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5872 * '<'.
5873 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5874 *
5875 * The replacement is done in result[] and finally copied into allocated
5876 * memory. If this all works well *bufp is set to the allocated memory and a
5877 * pointer to it is returned. If something fails *bufp is set to NULL and from
5878 * is returned.
5879 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005880 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5881 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5882 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5883 * used instead of a CTRL-V.
5884 *
5885 * Flags:
5886 * REPTERM_FROM_PART see above
5887 * REPTERM_DO_LT also translate <lt>
5888 * REPTERM_SPECIAL always accept <key> notation
5889 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5890 *
5891 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5892 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005894 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005895replace_termcodes(
5896 char_u *from,
5897 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005898 int flags,
5899 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 int i;
5902 int slen;
5903 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01005904 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005906 int do_backslash; // backslash is a special character
5907 int do_special; // recognize <> key codes
5908 int do_key_code; // recognize raw key codes
5909 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01005910 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911
5912 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02005913 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
5914 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01005916 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
5918 /*
5919 * Allocate space for the translation. Worst case a single character is
5920 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01005921 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01005923 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01005924 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
5926 *bufp = NULL;
5927 return from;
5928 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01005929 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930
5931 /*
5932 * Check for #n at start only: function key n
5933 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005934 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 {
5936 result[dlen++] = K_SPECIAL;
5937 result[dlen++] = 'k';
5938 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005939 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005941 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 src += 2;
5943 }
5944
5945 /*
5946 * Copy each byte from *from to result[dlen]
5947 */
5948 while (*src != NUL)
5949 {
5950 /*
5951 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005952 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005954 if (do_special && ((flags & REPTERM_DO_LT)
5955 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 {
5957#ifdef FEAT_EVAL
5958 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01005959 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
5960 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01005962 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
5963 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 */
5965 if (STRNICMP(src, "<SID>", 5) == 0)
5966 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005967 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00005968 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 else
5970 {
Bram Moolenaar89445512022-04-14 12:58:23 +01005971 char_u *dot;
5972 long sid = current_sctx.sc_sid;
5973
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01005975 if (in_vim9script()
5976 && (dot = vim_strchr(src, '.')) != NULL)
5977 {
5978 imported_T *imp = find_imported(src, dot - src, FALSE);
5979
5980 if (imp != NULL)
5981 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01005982 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
5983 size_t len;
5984
Bram Moolenaar89445512022-04-14 12:58:23 +01005985 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01005986 if (si->sn_autoload_prefix != NULL)
5987 {
5988 // Turn "<SID>name.Func"
5989 // into "scriptname#Func".
5990 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01005991 if (ga_grow(&ga,
5992 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01005993 {
5994 ga_clear(&ga);
5995 *bufp = NULL;
5996 return from;
5997 }
5998 result = ga.ga_data;
5999 STRCPY(result + dlen, si->sn_autoload_prefix);
6000 dlen += len;
6001 continue;
6002 }
6003 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006004 }
6005 }
6006
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 result[dlen++] = K_SPECIAL;
6008 result[dlen++] = (int)KS_EXTRA;
6009 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006010 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006011 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 result[dlen++] = '_';
6013 continue;
6014 }
6015 }
6016#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006017 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6018 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006019 TRUE, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 if (slen)
6021 {
6022 dlen += slen;
6023 continue;
6024 }
6025 }
6026
6027 /*
6028 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6029 * Note that this is also checked after replacing the <> form.
6030 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6031 * it could be a character in the file.
6032 */
6033 if (do_key_code)
6034 {
6035 i = find_term_bykeys(src);
6036 if (i >= 0)
6037 {
6038 result[dlen++] = K_SPECIAL;
6039 result[dlen++] = termcodes[i].name[0];
6040 result[dlen++] = termcodes[i].name[1];
6041 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006042 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 continue;
6044 }
6045 }
6046
6047#ifdef FEAT_EVAL
6048 if (do_special)
6049 {
6050 char_u *p, *s, len;
6051
6052 /*
6053 * Replace <Leader> by the value of "mapleader".
6054 * Replace <LocalLeader> by the value of "maplocalleader".
6055 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6056 */
6057 if (STRNICMP(src, "<Leader>", 8) == 0)
6058 {
6059 len = 8;
6060 p = get_var_value((char_u *)"g:mapleader");
6061 }
6062 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6063 {
6064 len = 13;
6065 p = get_var_value((char_u *)"g:maplocalleader");
6066 }
6067 else
6068 {
6069 len = 0;
6070 p = NULL;
6071 }
6072 if (len != 0)
6073 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006074 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6076 s = (char_u *)"\\";
6077 else
6078 s = p;
6079 while (*s != NUL)
6080 result[dlen++] = *s++;
6081 src += len;
6082 continue;
6083 }
6084 }
6085#endif
6086
6087 /*
6088 * Remove CTRL-V and ignore the next character.
6089 * For "from" side the CTRL-V at the end is included, for the "to"
6090 * part it is removed.
6091 * If 'cpoptions' does not contain 'B', also accept a backslash.
6092 */
6093 key = *src;
6094 if (key == Ctrl_V || (do_backslash && key == '\\'))
6095 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006096 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 if (*src == NUL)
6098 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006099 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 result[dlen++] = key;
6101 break;
6102 }
6103 }
6104
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006105 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006106 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 {
6108 /*
6109 * If the character is K_SPECIAL, replace it with K_SPECIAL
6110 * KS_SPECIAL KE_FILLER.
6111 * If compiled with the GUI replace CSI with K_CSI.
6112 */
6113 if (*src == K_SPECIAL)
6114 {
6115 result[dlen++] = K_SPECIAL;
6116 result[dlen++] = KS_SPECIAL;
6117 result[dlen++] = KE_FILLER;
6118 }
6119# ifdef FEAT_GUI
6120 else if (*src == CSI)
6121 {
6122 result[dlen++] = K_SPECIAL;
6123 result[dlen++] = KS_EXTRA;
6124 result[dlen++] = (int)KE_CSI;
6125 }
6126# endif
6127 else
6128 result[dlen++] = *src;
6129 ++src;
6130 }
6131 }
6132 result[dlen] = NUL;
6133
6134 /*
6135 * Copy the new string to allocated memory.
6136 * If this fails, just return from.
6137 */
6138 if ((*bufp = vim_strsave(result)) != NULL)
6139 from = *bufp;
6140 vim_free(result);
6141 return from;
6142}
6143
6144/*
6145 * Find a termcode with keys 'src' (must be NUL terminated).
6146 * Return the index in termcodes[], or -1 if not found.
6147 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006148 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006149find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150{
6151 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006152 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
6154 for (i = 0; i < tc_len; ++i)
6155 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006156 if (slen == termcodes[i].len
6157 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 return i;
6159 }
6160 return -1;
6161}
6162
6163/*
6164 * Gather the first characters in the terminal key codes into a string.
6165 * Used to speed up check_termcode().
6166 */
6167 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006168gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169{
6170 int i;
6171 int len = 0;
6172
6173#ifdef FEAT_GUI
6174 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006175 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176#endif
6177#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006178 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006179 termleader[len++] = DCS; // the termcode response starts with DCS
6180 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181#endif
6182 termleader[len] = NUL;
6183
6184 for (i = 0; i < tc_len; ++i)
6185 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6186 {
6187 termleader[len++] = termcodes[i].code[0];
6188 termleader[len] = NUL;
6189 }
6190
6191 need_gather = FALSE;
6192}
6193
6194/*
6195 * Show all termcodes (for ":set termcap")
6196 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006197 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 */
6199 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006200show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201{
6202 int col;
6203 int *items;
6204 int item_count;
6205 int run;
6206 int row, rows;
6207 int cols;
6208 int i;
6209 int len;
6210
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006211#define INC3 27 // try to make three columns
6212#define INC2 40 // try to make two columns
6213#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006215 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006217 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 if (items == NULL)
6219 return;
6220
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006221 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006222 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223
6224 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006225 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006227 * 2. display the medium items (medium length strings)
6228 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006229 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006231 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 {
6233 /*
6234 * collect the items in items[]
6235 */
6236 item_count = 0;
6237 for (i = 0; i < tc_len; i++)
6238 {
6239 len = show_one_termcode(termcodes[i].name,
6240 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006241 if ((flags & OPT_ONECOLUMN) ||
6242 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006243 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006244 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 items[item_count++] = i;
6246 }
6247
6248 /*
6249 * display the items
6250 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006251 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006253 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 if (cols == 0)
6255 cols = 1;
6256 rows = (item_count + cols - 1) / cols;
6257 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006258 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 rows = item_count;
6260 for (row = 0; row < rows && !got_int; ++row)
6261 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006262 msg_putchar('\n'); // go to next line
6263 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 break;
6265 col = 0;
6266 for (i = row; i < item_count; i += rows)
6267 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006268 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 show_one_termcode(termcodes[items[i]].name,
6270 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006271 if (run == 2)
6272 col += INC2;
6273 else
6274 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 }
6276 out_flush();
6277 ui_breakcheck();
6278 }
6279 }
6280 vim_free(items);
6281}
6282
6283/*
6284 * Show one termcode entry.
6285 * Output goes into IObuff[]
6286 */
6287 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006288show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
6290 char_u *p;
6291 int len;
6292
6293 if (name[0] > '~')
6294 {
6295 IObuff[0] = ' ';
6296 IObuff[1] = ' ';
6297 IObuff[2] = ' ';
6298 IObuff[3] = ' ';
6299 }
6300 else
6301 {
6302 IObuff[0] = 't';
6303 IObuff[1] = '_';
6304 IObuff[2] = name[0];
6305 IObuff[3] = name[1];
6306 }
6307 IObuff[4] = ' ';
6308
6309 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6310 if (p[1] != 't')
6311 STRCPY(IObuff + 5, p);
6312 else
6313 IObuff[5] = NUL;
6314 len = (int)STRLEN(IObuff);
6315 do
6316 IObuff[len++] = ' ';
6317 while (len < 17);
6318 IObuff[len] = NUL;
6319 if (code == NULL)
6320 len += 4;
6321 else
6322 len += vim_strsize(code);
6323
6324 if (printit)
6325 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006326 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006328 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 else
6330 msg_outtrans(code);
6331 }
6332 return len;
6333}
6334
6335#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6336/*
6337 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6338 * termcap codes from the terminal itself.
6339 * We get them one by one to avoid a very long response string.
6340 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006341static int xt_index_in = 0;
6342static int xt_index_out = 0;
6343
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006345req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346{
6347 xt_index_out = 0;
6348 xt_index_in = 0;
6349 req_more_codes_from_term();
6350}
6351
6352 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006353req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006355 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 int old_idx = xt_index_out;
6357
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006358 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 if (exiting)
6360 return;
6361
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006362 // Send up to 10 more requests out than we received. Avoid sending too
6363 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6365 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006366 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006367
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006368 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006369 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6370 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 out_str_nf((char_u *)buf);
6372 ++xt_index_out;
6373 }
6374
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006375 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 if (xt_index_out != old_idx)
6377 out_flush();
6378}
6379
6380/*
6381 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6382 * A "0" instead of the "1" indicates a code that isn't supported.
6383 * Both <name> and <string> are encoded in hex.
6384 * "code" points to the "0" or "1".
6385 */
6386 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006387got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389#define XT_LEN 100
6390 char_u name[3];
6391 char_u str[XT_LEN];
6392 int i;
6393 int j = 0;
6394 int c;
6395
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006396 // A '1' means the code is supported, a '0' means it isn't.
6397 // When half the length is > XT_LEN we can't use it.
6398 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6400 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006401 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 name[0] = hexhex2nr(code + 3);
6403 name[1] = hexhex2nr(code + 5);
6404 name[2] = NUL;
6405 for (i = 0; key_names[i] != NULL; ++i)
6406 {
6407 if (STRCMP(key_names[i], name) == 0)
6408 {
6409 xt_index_in = i;
6410 break;
6411 }
6412 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006413
Bram Moolenaarb255b902018-04-24 21:40:10 +02006414 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6415
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 if (key_names[i] != NULL)
6417 {
6418 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6419 str[j++] = c;
6420 str[j] = NUL;
6421 if (name[0] == 'C' && name[1] == 'o')
6422 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006423 // Color count is not a key code.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00006424 may_adjust_color_count(atoi((char *)str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 }
6426 else
6427 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006428 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 i = find_term_bykeys(str);
6430 if (i >= 0)
6431 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006432 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 }
6434 }
6435 }
6436
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006437 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 ++xt_index_in;
6439 req_more_codes_from_term();
6440}
6441
6442/*
6443 * Check if there are any unanswered requests and deal with them.
6444 * This is called before starting an external program or getting direct
6445 * keyboard input. We don't want responses to be send to that program or
6446 * handled as typed text.
6447 */
6448 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006449check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
6451 int c;
6452
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006453 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6455 return;
6456
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006457 // Vgetc() will check for and handle any response.
6458 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 ++no_mapping;
6460 ++allow_keys;
6461 for (;;)
6462 {
6463 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006464 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 break;
6466
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006467 // If a response is recognized it's replaced with K_IGNORE, must read
6468 // it from the input stream. If there is no K_IGNORE we can't do
6469 // anything, break here (there might be some responses further on, but
6470 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 if (c != K_SPECIAL && c != K_IGNORE)
6472 break;
6473 c = vgetc();
6474 if (c != K_IGNORE)
6475 {
6476 vungetc(c);
6477 break;
6478 }
6479 }
6480 --no_mapping;
6481 --allow_keys;
6482}
6483#endif
6484
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006485#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486static char ksme_str[20];
6487static char ksmr_str[20];
6488static char ksmd_str[20];
6489
6490/*
6491 * For Win32 console: update termcap codes for existing console attributes.
6492 */
6493 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006494update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495{
6496 struct builtin_term *p;
6497
6498 p = find_builtin_term(DEFAULT_TERM);
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006499 sprintf(ksme_str, "\033|%dm", attr);
6500 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
6501 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
6503 while (p->bt_string != NULL)
6504 {
6505 if (p->bt_entry == (int)KS_ME)
6506 p->bt_string = &ksme_str[0];
6507 else if (p->bt_entry == (int)KS_MR)
6508 p->bt_string = &ksmr_str[0];
6509 else if (p->bt_entry == (int)KS_MD)
6510 p->bt_string = &ksmd_str[0];
6511 ++p;
6512 }
6513}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006514
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006515# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006516# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006517
6518typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006519{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006520 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
6521 CMODE_RGB, // Use 24bit RGB colors using VTP.
6522 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
6523 CMODE_LAST,
6524} cmode_T;
6525
6526struct ks_tbl_S
6527{
6528 int code; // value of KS_
6529 char *vtp; // code in RGB mode
6530 char *vtp2; // code in 256color mode
6531 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006532};
6533
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006534static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006535{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006536 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
6537 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
6538 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
6539 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
6540 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
6541 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
6542 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
6543 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
6544 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006545# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006546 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
6547 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
6548 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
6549 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006550# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006551 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
6552 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
6553 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
6554 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006555# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006556 {(int)KS_CCO, "256", "256", {""}}, // colors
6557 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006558};
6559
6560 static struct builtin_term *
6561find_first_tcap(
6562 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006563 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006564{
6565 struct builtin_term *p;
6566
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006567 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006568 if (p->bt_entry == code)
6569 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006570 return NULL;
6571}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006572# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006573
6574/*
6575 * For Win32 console: replace the sequence immediately after termguicolors.
6576 */
6577 void
6578swap_tcap(void)
6579{
6580# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006581 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006582 static cmode_T curr_mode;
6583 struct ks_tbl_S *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006584 struct builtin_term *bt;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006585 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006586
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006587 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006588 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006589 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006590 {
6591 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006592 if (bt != NULL)
6593 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006594 // Preserve the original value.
6595 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
6596 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
6597 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006598
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006599 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006600 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006601 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006602 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006603 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006604 }
6605
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006606 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006607 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006608 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006609 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006610 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006611 mode = CMODE_INDEXED;
6612
6613 if (mode == curr_mode)
6614 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006615
6616 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006617 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006618 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6619 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006620 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006621 }
6622
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006623 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006624# endif
6625}
6626
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006628
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006629
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006630#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006631 || defined(PROTO)
6632static int cube_value[] = {
6633 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6634};
6635
6636static int grey_ramp[] = {
6637 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6638 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6639};
6640
LemonBoyb2b3acb2022-05-20 10:10:34 +01006641static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01006642// R G B
6643 { 0, 0, 0}, // black
6644 {224, 0, 0}, // dark red
6645 { 0, 224, 0}, // dark green
6646 {224, 224, 0}, // dark yellow / brown
6647 { 0, 0, 224}, // dark blue
6648 {224, 0, 224}, // dark magenta
6649 { 0, 224, 224}, // dark cyan
6650 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006651
LemonBoyd2a46622022-05-01 17:43:33 +01006652 {128, 128, 128}, // dark grey
6653 {255, 64, 64}, // light red
6654 { 64, 255, 64}, // light green
6655 {255, 255, 64}, // yellow
6656 { 64, 64, 255}, // light blue
6657 {255, 64, 255}, // light magenta
6658 { 64, 255, 255}, // light cyan
6659 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006660};
6661
LemonBoyd2a46622022-05-01 17:43:33 +01006662#if defined(MSWIN)
6663// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
6664static const char_u cterm_ansi_idx[] = {
6665 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
6666};
6667#endif
6668
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006669#define ANSI_INDEX_NONE 0
6670
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006671 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01006672ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
6673{
6674 if (nr < 16)
6675 {
6676 *r = ansi_table[nr][0];
6677 *g = ansi_table[nr][1];
6678 *b = ansi_table[nr][2];
6679 *ansi_idx = nr;
6680 }
6681 else
6682 {
6683 *r = 0;
6684 *g = 0;
6685 *b = 0;
6686 *ansi_idx = ANSI_INDEX_NONE;
6687 }
6688}
6689
6690 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006691cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006692{
6693 int idx;
6694
6695 if (nr < 16)
6696 {
LemonBoyd2a46622022-05-01 17:43:33 +01006697#if defined(MSWIN)
6698 idx = cterm_ansi_idx[nr];
6699#else
6700 idx = nr;
6701#endif
6702 *r = ansi_table[idx][0];
6703 *g = ansi_table[idx][1];
6704 *b = ansi_table[idx][2];
6705 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006706 }
6707 else if (nr < 232)
6708 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006709 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006710 idx = nr - 16;
6711 *r = cube_value[idx / 36 % 6];
6712 *g = cube_value[idx / 6 % 6];
6713 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006714 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006715 }
6716 else if (nr < 256)
6717 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006718 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006719 idx = nr - 232;
6720 *r = grey_ramp[idx];
6721 *g = grey_ramp[idx];
6722 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006723 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006724 }
6725 else
6726 {
6727 *r = 0;
6728 *g = 0;
6729 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006730 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006731 }
6732}
6733#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006734
Bram Moolenaarf4140482020-02-15 23:06:45 +01006735/*
6736 * Replace K_BS by <BS> and K_DEL by <DEL>
6737 */
6738 void
6739term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len)
6740{
6741 int i;
6742 int c;
6743
6744 for (i = ta_len; i < ta_len + len; ++i)
6745 {
6746 if (ta_buf[i] == CSI && len - i > 2)
6747 {
6748 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6749 if (c == K_DEL || c == K_KDEL || c == K_BS)
6750 {
6751 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
6752 (size_t)(len - i - 2));
6753 if (c == K_DEL || c == K_KDEL)
6754 ta_buf[i] = DEL;
6755 else
6756 ta_buf[i] = Ctrl_H;
6757 len -= 2;
6758 }
6759 }
6760 else if (ta_buf[i] == '\r')
6761 ta_buf[i] = '\n';
6762 if (has_mbyte)
6763 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6764 }
6765}