blob: 9c287417f96e6ebf24f2d3801351ba952d1e343c [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
156// and it was supposed to be written.
157// 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
3014 if (write_t_8u_state != OK)
3015 write_t_8u_state = MAYBE;
3016 else
3017# endif
3018 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003019}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003020#endif
3021
Bram Moolenaar651fca82021-11-29 20:39:38 +00003022#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023/*
3024 * Generic function to set window title, using t_ts and t_fs.
3025 */
3026 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003027term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003029 MAY_WANT_TO_LOG_THIS;
3030
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003031 // t_ts takes one argument: column in status line
3032 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003034 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 out_flush();
3036}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003037
3038/*
3039 * Tell the terminal to push (save) the title and/or icon, so that it can be
3040 * popped (restored) later.
3041 */
3042 void
3043term_push_title(int which)
3044{
Bram Moolenaar27821262019-05-08 16:41:09 +02003045 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003046 {
3047 OUT_STR(T_CST);
3048 out_flush();
3049 }
3050
Bram Moolenaar27821262019-05-08 16:41:09 +02003051 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003052 {
3053 OUT_STR(T_SSI);
3054 out_flush();
3055 }
3056}
3057
3058/*
3059 * Tell the terminal to pop the title and/or icon.
3060 */
3061 void
3062term_pop_title(int which)
3063{
Bram Moolenaar27821262019-05-08 16:41:09 +02003064 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003065 {
3066 OUT_STR(T_CRT);
3067 out_flush();
3068 }
3069
Bram Moolenaar27821262019-05-08 16:41:09 +02003070 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003071 {
3072 OUT_STR(T_SRI);
3073 out_flush();
3074 }
3075}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076#endif
3077
3078/*
3079 * Make sure we have a valid set or terminal options.
3080 * Replace all entries that are NULL by empty_option
3081 */
3082 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003083ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003085 char_u *env_colors;
3086
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003087 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
3089 /*
3090 * MUST have "cm": cursor motion.
3091 */
3092 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003093 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
3095 /*
3096 * if "cs" defined, use a scroll region, it's faster.
3097 */
3098 if (*T_CS != NUL)
3099 scroll_region = TRUE;
3100 else
3101 scroll_region = FALSE;
3102
3103 if (pairs)
3104 {
3105 /*
3106 * optional pairs
3107 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003108 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 if (*T_ME == NUL)
3110 T_ME = T_MR = T_MD = T_MB = empty_option;
3111 if (*T_SO == NUL || *T_SE == NUL)
3112 T_SO = T_SE = empty_option;
3113 if (*T_US == NUL || *T_UE == NUL)
3114 T_US = T_UE = empty_option;
3115 if (*T_CZH == NUL || *T_CZR == NUL)
3116 T_CZH = T_CZR = empty_option;
3117
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003118 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 if (*T_VE == NUL)
3120 T_VI = empty_option;
3121
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003122 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 if (*T_ME == NUL)
3124 {
3125 T_ME = T_SE;
3126 T_MR = T_SO;
3127 T_MD = T_SO;
3128 }
3129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003130 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 if (*T_SO == NUL)
3132 {
3133 T_SE = T_ME;
3134 if (*T_MR == NUL)
3135 T_SO = T_MD;
3136 else
3137 T_SO = T_MR;
3138 }
3139
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003140 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (*T_CZH == NUL)
3142 {
3143 T_CZR = T_ME;
3144 if (*T_MR == NUL)
3145 T_CZH = T_MD;
3146 else
3147 T_CZH = T_MR;
3148 }
3149
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003150 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 if (*T_CSB == NUL || *T_CSF == NUL)
3152 {
3153 T_CSB = empty_option;
3154 T_CSF = empty_option;
3155 }
3156
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003157 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 if (*T_CAB == NUL || *T_CAF == NUL)
3159 {
3160 T_CAB = empty_option;
3161 T_CAF = empty_option;
3162 }
3163
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003164 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003166 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003168 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 p_wiv = (*T_XS != NUL);
3170 }
3171 need_gather = TRUE;
3172
Bram Moolenaar759d8152020-04-26 16:52:49 +02003173 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3174 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003176#ifdef FEAT_GUI
3177 if (!gui.in_use)
3178#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003179 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003180 env_colors = mch_getenv((char_u *)"COLORS");
3181 if (env_colors != NULL && isdigit(*env_colors))
3182 {
3183 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003184
Bram Moolenaar759d8152020-04-26 16:52:49 +02003185 if (colors != t_colors)
3186 set_color_count(colors);
3187 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189}
3190
3191#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3192 || defined(PROTO)
3193/*
3194 * Represent the given long_u as individual bytes, with the most significant
3195 * byte first, and store them in dst.
3196 */
3197 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003198add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199{
3200 int i;
3201 int shift;
3202
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003203 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
3205 shift = 8 * (sizeof(long_u) - i);
3206 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3207 }
3208}
3209
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210/*
3211 * Interpret the next string of bytes in buf as a long integer, with the most
3212 * significant byte first. Note that it is assumed that buf has been through
3213 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3214 * Puts result in val, and returns the number of bytes read from buf
3215 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3216 * were present.
3217 */
3218 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003219get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220{
3221 int len;
3222 char_u bytes[sizeof(long_u)];
3223 int i;
3224 int shift;
3225
3226 *val = 0;
3227 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3228 if (len != -1)
3229 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003230 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 {
3232 shift = 8 * (sizeof(long_u) - 1 - i);
3233 *val += (long_u)bytes[i] << shift;
3234 }
3235 }
3236 return len;
3237}
3238#endif
3239
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240/*
3241 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3242 * that buf has been through inchar(). Returns the actual number of bytes used
3243 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3244 * available.
3245 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003246 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003247get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248{
3249 int len = 0;
3250 int i;
3251 char_u c;
3252
3253 for (i = 0; i < num_bytes; i++)
3254 {
3255 if ((c = buf[len++]) == NUL)
3256 return -1;
3257 if (c == K_SPECIAL)
3258 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003259 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 return -1;
3261 if (buf[len++] == (int)KS_ZERO)
3262 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003263 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3264 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003265 if (buf[len++] == (int)KE_CSI)
3266 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003268 else if (c == CSI && buf[len] == KS_EXTRA
3269 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003270 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3271 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003272 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 bytes[i] = c;
3274 }
3275 return len;
3276}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277
3278/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003279 * Check if the new shell size is valid, correct it if it's too small or way
3280 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 */
3282 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003283check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003285 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003287 limit_screen_size();
Bram Moolenaare178af52022-06-25 19:54:09 +01003288
3289 // make sure these values are not invalid
3290 if (cmdline_row >= Rows)
3291 cmdline_row = Rows - 1;
3292 if (msg_row >= Rows)
3293 msg_row = Rows - 1;
Bram Moolenaare057d402013-06-30 17:51:51 +02003294}
3295
3296/*
3297 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3298 */
3299 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003300limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003301{
3302 if (Columns < MIN_COLUMNS)
3303 Columns = MIN_COLUMNS;
3304 else if (Columns > 10000)
3305 Columns = 10000;
3306 if (Rows > 1000)
3307 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308}
3309
Bram Moolenaar86b68352004-12-27 21:59:20 +00003310/*
3311 * Invoked just before the screen structures are going to be (re)allocated.
3312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003314win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
3316 static int old_Rows = 0;
3317 static int old_Columns = 0;
3318
3319 if (old_Rows != Rows || old_Columns != Columns)
3320 ui_new_shellsize();
3321 if (old_Rows != Rows)
3322 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003323 // If 'window' uses the whole screen, keep it using that.
3324 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003325 if (p_window == old_Rows - 1
3326 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003327 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003329 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 }
3331 if (old_Columns != Columns)
3332 {
3333 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003334 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
3336}
3337
3338/*
3339 * Call this function when the Vim shell has been resized in any way.
3340 * Will obtain the current size and redraw (also when size didn't change).
3341 */
3342 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003343shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 set_shellsize(0, 0, FALSE);
3346}
3347
3348/*
3349 * Check if the shell size changed. Handle a resize.
3350 * When the size didn't change, nothing happens.
3351 */
3352 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003353shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 int old_Rows = Rows;
3356 int old_Columns = Columns;
3357
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003358 if (!exiting
3359#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003360 // Do not get the size when executing a shell command during
3361 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003362 && !gui.starting
3363#endif
3364 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003365 {
3366 (void)ui_get_shellsize();
3367 check_shellsize();
3368 if (old_Rows != Rows || old_Columns != Columns)
3369 shell_resized();
3370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371}
3372
3373/*
3374 * Set size of the Vim shell.
3375 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3376 * window size (this is used for the :win command).
3377 * If 'mustset' is FALSE, we may try to get the real window size and if
3378 * it fails use 'width' and 'height'.
3379 */
3380 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003381set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 static int busy = FALSE;
3384
3385 /*
3386 * Avoid recursiveness, can happen when setting the window size causes
3387 * another window-changed signal.
3388 */
3389 if (busy)
3390 return;
3391
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003392 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 return;
3394
Bram Moolenaar24959102022-05-07 20:01:16 +01003395 if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003397 // postpone the resizing
Bram Moolenaar24959102022-05-07 20:01:16 +01003398 State = MODE_SETWSIZE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 return;
3400 }
3401
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003402 if (updating_screen)
3403 // resizing while in update_screen() may cause a crash
3404 return;
3405
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003406 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003407 // buffer (or window) has already been closed and removing a scrollbar
3408 // causes a resize event. Don't resize then, it will happen after entering
3409 // another buffer.
3410 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003411 return;
3412
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 ++busy;
3414
3415#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003416 out_flush(); // must do this before mch_get_shellsize() for
3417 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418#endif
3419
3420 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3421 {
3422 Rows = height;
3423 Columns = width;
3424 check_shellsize();
3425 ui_set_shellsize(mustset);
3426 }
3427 else
3428 check_shellsize();
3429
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003430 // The window layout used to be adjusted here, but it now happens in
3431 // screenalloc() (also invoked from screenclear()). That is because the
3432 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
Bram Moolenaar24959102022-05-07 20:01:16 +01003434 if (State != MODE_ASKMORE && State != MODE_EXTERNCMD
3435 && State != MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 screenclear();
3437 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003438 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 if (starting != NO_SCREEN)
3441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003443
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 changed_line_abv_curs();
3445 invalidate_botline();
3446
3447 /*
3448 * We only redraw when it's needed:
3449 * - While at the more prompt or executing an external command, don't
3450 * redraw, but position the cursor.
3451 * - While editing the command line, only redraw that.
3452 * - in Ex mode, don't redraw anything.
3453 * - Otherwise, redraw right now, and position the cursor.
3454 * Always need to call update_screen() or screenalloc(), to make
3455 * sure Rows/Columns and the size of ScreenLines[] is correct!
3456 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003457 if (State == MODE_ASKMORE || State == MODE_EXTERNCMD
3458 || State == MODE_CONFIRM || exmode_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
3460 screenalloc(FALSE);
3461 repeat_message();
3462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 else
3464 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003465 if (curwin->w_p_scb)
3466 do_check_scrollbind(TRUE);
Bram Moolenaar24959102022-05-07 20:01:16 +01003467 if (State & MODE_CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003468 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003469 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003470 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003471 }
3472 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003473 {
3474 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003475 if (pum_visible())
3476 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003477 redraw_later(UPD_NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003478 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003479 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003480 update_screen(UPD_NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003481 if (redrawing())
3482 setcursor();
3483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003485 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
3487 out_flush();
3488 --busy;
3489}
3490
3491/*
3492 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3493 * commands and Ex mode).
3494 */
3495 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003496settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
3498#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003499 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 if (gui.in_use)
3501 return;
3502#endif
3503
3504 if (full_screen)
3505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003507 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3508 * set the terminal to raw mode, even though we think it already is,
3509 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 * When we think the terminal is normal, don't try to set it to
3511 * normal again, because that causes problems (logout!) on some
3512 * machines.
3513 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003514 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
3516#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003517# ifdef FEAT_GUI
3518 if (!gui.in_use && !gui.starting)
3519# endif
3520 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003521 // May need to check for T_CRV response and termcodes, it
3522 // doesn't work in Cooked mode, an external program may get
3523 // them.
3524 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003525 (void)vpeekc_nomap();
3526 check_for_codes_from_term();
3527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003530 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003531
3532 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3533 // Avoid doing this too often, on some terminals the codes are not
3534 // handled properly.
3535 if (termcap_active && tmode != TMODE_SLEEP
3536 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003537 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003538 MAY_WANT_TO_LOG_THIS;
3539
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003540 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003541 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003542 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003543 out_str(T_CTE); // possibly disables modifyOtherKeys
3544 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003545 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003546 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003547 out_str(T_BE); // enable bracketed paste mode (should
3548 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003549 out_str(T_CTI); // possibly enables modifyOtherKeys
3550 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003553 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003556 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 out_flush();
3558 }
3559#ifdef FEAT_TERMRESPONSE
3560 may_req_termresponse();
3561#endif
3562 }
3563}
3564
3565 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003566starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567{
3568 if (full_screen && !termcap_active)
3569 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003570 MAY_WANT_TO_LOG_THIS;
3571
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003572 out_str(T_TI); // start termcap mode
3573 out_str(T_CTI); // start "raw" mode
3574 out_str(T_KS); // start "keypad transmit" mode
3575 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003576
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003577#if defined(UNIX) || defined(VMS)
3578 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003579 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003580 out_str(T_FE);
3581#endif
3582
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 out_flush();
3584 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003585 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003587# ifdef FEAT_GUI
3588 if (!gui.in_use && !gui.starting)
3589# endif
3590 {
3591 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003592 // Immediately check for a response. If t_Co changes, we don't
3593 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003594 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003595 check_for_codes_from_term();
3596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597#endif
3598 }
3599}
3600
3601 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003602stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603{
3604 screen_stop_highlight();
3605 reset_cterm_colors();
3606 if (termcap_active)
3607 {
3608#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003609# ifdef FEAT_GUI
3610 if (!gui.in_use && !gui.starting)
3611# endif
3612 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003613 // May need to discard T_CRV, T_U7 or T_RBG response.
3614 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003615 {
3616# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003617 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003618 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003619# endif
3620# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003621 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003622 if (exiting)
3623 tcflush(fileno(stdin), TCIFLUSH);
3624# endif
3625 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003626 // Check for termcodes first, otherwise an external program may
3627 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003628 check_for_codes_from_term();
3629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630#endif
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003631 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003632
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003633#if defined(UNIX) || defined(VMS)
3634 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003635 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003636 out_str(T_FD);
3637#endif
3638
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003639 out_str(T_BD); // disable bracketed paste mode
3640 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 out_flush();
3642 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003643 cursor_on(); // just in case it is still off
3644 out_str(T_CTE); // stop "raw" mode
3645 out_str(T_TE); // stop termcap mode
3646 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 out_flush();
3648 }
3649}
3650
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003651#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652/*
3653 * Request version string (for xterm) when needed.
3654 * Only do this after switching to raw mode, otherwise the result will be
3655 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003656 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003657 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 * Only do this after termcap mode has been started, otherwise the codes for
3659 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003660 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3661 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003662 * On Unix only do it when both output and input are a tty (avoid writing
3663 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 * The result is caught in check_termcode().
3665 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003666 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003667may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003669 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003670 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003671 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 && *T_CRV != NUL)
3673 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003674 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003675 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003677 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003678 // check for the characters now, otherwise they might be eaten by
3679 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 out_flush();
3681 (void)vpeekc_nomap();
3682 }
3683}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003684
Bram Moolenaar9584b312013-03-13 19:29:28 +01003685/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003686 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3687 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003688 * Note that this goes out before T_CRV, so that the result can be used when
3689 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003690 */
3691 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003692check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003693{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003694 int did_send = FALSE;
3695
3696 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3697 return;
3698
Bram Moolenaarafd78262019-05-10 23:10:31 +02003699 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003700 && !option_was_set((char_u *)"ambiwidth"))
3701 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003702 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003703
Bram Moolenaara45551a2020-06-09 15:57:37 +02003704 // Ambiguous width check.
3705 // Check how the terminal treats ambiguous character width (UAX #11).
3706 // First, we move the cursor to (1, 0) and print a test ambiguous
3707 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3708 // the current cursor position. If the terminal treats \u25bd as
3709 // single width, the position is (1, 1), or if it is treated as double
3710 // width, that will be (1, 2). This function has the side effect that
3711 // changes cursor position, so it must be called immediately after
3712 // entering termcap mode.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003713 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003714 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003715 // Do this in the second row. In the first row the returned sequence
3716 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003717 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003718 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003719 out_str(buf);
3720 out_str(T_U7);
3721 termrequest_sent(&u7_status);
3722 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003723 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003724
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003725 // This overwrites a few characters on the screen, a redraw is needed
3726 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003727 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003728 term_windgoto(1, 0);
3729 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003730 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003731 }
3732
Bram Moolenaard1f34e62022-01-07 19:24:20 +00003733 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02003734 {
3735 // 2. Check compatibility with xterm.
3736 // We move the cursor to (2, 0), print a test sequence and then query
3737 // the current cursor position. If the terminal properly handles
3738 // unknown DCS string and CSI sequence with intermediate byte, the test
3739 // sequence is ignored and the cursor does not move. If the terminal
3740 // handles test sequence incorrectly, a garbage string is displayed and
3741 // the cursor does move.
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003742 MAY_WANT_TO_LOG_THIS;
Bram Moolenaara45551a2020-06-09 15:57:37 +02003743 LOG_TR(("Sending xterm compatibility test sequence."));
3744 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003745 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003746 term_windgoto(2, 0);
3747 // send the test DCS string.
3748 out_str((char_u *)"\033Pzz\033\\");
3749 // send the test CSI sequence with intermediate byte.
3750 out_str((char_u *)"\033[0%m");
3751 out_str(T_U7);
3752 termrequest_sent(&xcc_status);
3753 out_flush();
3754 did_send = TRUE;
3755
3756 // If the terminal handles test sequence incorrectly, garbage text is
3757 // displayed. Clear them out for now.
3758 screen_stop_highlight();
3759 term_windgoto(2, 0);
3760 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003761 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003762 }
3763
3764 if (did_send)
3765 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003766 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003767
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003768 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003769 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003770
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003771 // check for the characters now, otherwise they might be eaten by
3772 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003773 out_flush();
3774 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003775 }
3776}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003777
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003778/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003779 * Similar to requesting the version string: Request the terminal background
3780 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003781 */
3782 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003783may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003784{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003785 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003786 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003787 int didit = FALSE;
3788
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003789# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003790 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003791 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003792 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003793 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003794 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003795 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003796 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003797 didit = TRUE;
3798 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003799# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003800
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003801 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003802 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003803 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003804 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003805 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003806 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003807 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003808 didit = TRUE;
3809 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003810
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003811 if (didit)
3812 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003813 // check for the characters now, otherwise they might be eaten by
3814 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003815 out_flush();
3816 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003817 }
3818 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003819}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003820
Bram Moolenaar2951b772013-07-03 12:45:31 +02003821# ifdef DEBUG_TERMRESPONSE
3822 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003823log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003824{
3825 static FILE *fd_tr = NULL;
3826 static proftime_T start;
3827 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003828 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003829
3830 if (fd_tr == NULL)
3831 {
3832 fd_tr = fopen("termresponse.log", "w");
3833 profile_start(&start);
3834 }
3835 now = start;
3836 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003837 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003838 must_redraw == UPD_NOT_VALID ? "NV"
3839 : must_redraw == UPD_CLEAR ? "CL" : " ");
Bram Moolenaarb255b902018-04-24 21:40:10 +02003840 va_start(ap, fmt);
3841 vfprintf(fd_tr, fmt, ap);
3842 va_end(ap);
3843 fputc('\n', fd_tr);
3844 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003845}
3846# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847#endif
3848
3849/*
3850 * Return TRUE when saving and restoring the screen.
3851 */
3852 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003853swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854{
3855 return (full_screen && *T_TI != NUL);
3856}
3857
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858/*
3859 * By outputting the 'cursor very visible' termcap code, for some windowed
3860 * terminals this makes the screen scrolled to the correct position.
3861 * Used when starting Vim or returning from a shell.
3862 */
3863 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003864scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003866 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01003868 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003870 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003871 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
3873}
3874
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003875// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876static int cursor_is_off = FALSE;
3877
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003878// True if cursor is not visible due to an ongoing cursor-less sleep
3879static int cursor_is_asleep = FALSE;
3880
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003882 * Enable the cursor without checking if it's already enabled.
3883 */
3884 void
3885cursor_on_force(void)
3886{
3887 out_str(T_VE);
3888 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003889 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02003890}
3891
3892/*
3893 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 */
3895 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003896cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003898 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003899 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900}
3901
3902/*
3903 * Disable the cursor.
3904 */
3905 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003906cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003908 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003910 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 cursor_is_off = TRUE;
3912 }
3913}
3914
Dominique Pelle748b3082022-01-08 12:41:16 +00003915#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003916/*
3917 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
3918 */
3919 int
3920cursor_is_sleeping(void)
3921{
3922 return cursor_is_asleep;
3923}
Dominique Pelle748b3082022-01-08 12:41:16 +00003924#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003925
3926/*
3927 * Disable the cursor and mark it disabled by cursor-less sleep
3928 */
3929 void
3930cursor_sleep(void)
3931{
3932 cursor_is_asleep = TRUE;
3933 cursor_off();
3934}
3935
3936/*
3937 * Enable the cursor and mark it not disabled by cursor-less sleep
3938 */
3939 void
3940cursor_unsleep(void)
3941{
3942 cursor_is_asleep = FALSE;
3943 cursor_on();
3944}
3945
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003946#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003948 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003949 */
3950 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003951term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003952{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003953 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003954 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003955
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003956 // Only do something when redrawing the screen and we can restore the
3957 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003958 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003959 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003960# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003961 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003962 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003963 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003964# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003965 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003966 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003967
Bram Moolenaar24959102022-05-07 20:01:16 +01003968 if ((State & MODE_REPLACE) == MODE_REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003969 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003970 if (forced || showing_mode != MODE_REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003971 {
3972 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003973 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003974 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003975 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003976 if (*p != NUL)
3977 {
3978 out_str(p);
Bram Moolenaar24959102022-05-07 20:01:16 +01003979 showing_mode = MODE_REPLACE;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003980 }
3981 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003982 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003983 else if (State & MODE_INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003984 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003985 if ((forced || showing_mode != MODE_INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003986 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003987 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01003988 showing_mode = MODE_INSERT;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003989 }
3990 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003991 else if (forced || showing_mode != MODE_NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003992 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003993 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar24959102022-05-07 20:01:16 +01003994 showing_mode = MODE_NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003995 }
3996}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003997
3998# if defined(FEAT_TERMINAL) || defined(PROTO)
3999 void
4000term_cursor_color(char_u *color)
4001{
4002 if (*T_CSC != NUL)
4003 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004004 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004005 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004006 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004007 out_flush();
4008 }
4009}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004010# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004011
Bram Moolenaar4db25542017-08-28 22:43:05 +02004012 int
4013blink_state_is_inverted()
4014{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004015#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004016 return rbm_status.tr_progress == STATUS_GOT
4017 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004018 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004019#else
4020 return FALSE;
4021#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004022}
4023
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004024/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004025 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004026 */
4027 void
4028term_cursor_shape(int shape, int blink)
4029{
4030 if (*T_CSH != NUL)
4031 {
4032 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4033 out_flush();
4034 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004035 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004036 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004037 int do_blink = blink;
4038
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004039 // t_SH is empty: try setting just the blink state.
4040 // The blink flags are XORed together, if the initial blinking from
4041 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004042 if (blink_state_is_inverted())
4043 do_blink = !blink;
4044
4045 if (do_blink && *T_VS != NUL)
4046 {
4047 out_str(T_VS);
4048 out_flush();
4049 }
4050 else if (!do_blink && *T_CVS != NUL)
4051 {
4052 out_str(T_CVS);
4053 out_flush();
4054 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004055 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004056}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004057#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004058
4059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 * Set scrolling region for window 'wp'.
4061 * The region starts 'off' lines from the start of the window.
4062 * Also set the vertical scroll region for a vertically split window. Always
4063 * the full width of the window, excluding the vertical separator.
4064 */
4065 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004066scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067{
4068 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4069 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004071 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4072 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004073 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074}
4075
4076/*
4077 * Reset scrolling region to the whole screen.
4078 */
4079 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004080scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081{
4082 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 if (*T_CSV != NUL)
4084 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004085 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086}
4087
4088
4089/*
4090 * List of terminal codes that are currently recognized.
4091 */
4092
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004093static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004095 char_u name[2]; // termcap name of entry
4096 char_u *code; // terminal code (in allocated memory)
4097 int len; // STRLEN(code)
4098 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099} *termcodes = NULL;
4100
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004101static int tc_max_len = 0; // number of entries that termcodes[] can hold
4102static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004104static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004105
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004107clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108{
4109 while (tc_len > 0)
4110 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004111 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 tc_max_len = 0;
4113
4114#ifdef HAVE_TGETENT
4115 BC = (char *)empty_option;
4116 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004117 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 ospeed = 0;
4119#endif
4120
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004121 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122}
4123
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004124#define ATC_FROM_TERM 55
4125
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126/*
4127 * Add a new entry to the list of terminal codes.
4128 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004129 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4130 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 */
4132 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004133add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
4135 struct termcode *new_tc;
4136 int i, j;
4137 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004138 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
4140 if (string == NULL || *string == NUL)
4141 {
4142 del_termcode(name);
4143 return;
4144 }
4145
Bram Moolenaar4f974752019-02-17 17:44:42 +01004146#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004147 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004148#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004149# ifdef VIMDLL
4150 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004151 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004152 else
4153# endif
4154 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 if (s == NULL)
4157 return;
4158
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004159 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004160 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004162 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 s[0] = term_7to8bit(string);
4164 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004165
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004166#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4167# ifdef VIMDLL
4168 if (!gui.in_use)
4169# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004170 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004171 if (s[0] == K_NUL)
4172 {
4173 STRMOVE(s + 1, s);
4174 s[1] = 3;
4175 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004176 }
4177#endif
4178
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004179 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004181 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
4183 /*
4184 * need to make space for more entries
4185 */
4186 if (tc_len == tc_max_len)
4187 {
4188 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004189 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 if (new_tc == NULL)
4191 {
4192 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004193 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 return;
4195 }
4196 for (i = 0; i < tc_len; ++i)
4197 new_tc[i] = termcodes[i];
4198 vim_free(termcodes);
4199 termcodes = new_tc;
4200 }
4201
4202 /*
4203 * Look for existing entry with the same name, it is replaced.
4204 * Look for an existing entry that is alphabetical higher, the new entry
4205 * is inserted in front of it.
4206 */
4207 for (i = 0; i < tc_len; ++i)
4208 {
4209 if (termcodes[i].name[0] < name[0])
4210 continue;
4211 if (termcodes[i].name[0] == name[0])
4212 {
4213 if (termcodes[i].name[1] < name[1])
4214 continue;
4215 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004216 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 */
4218 if (termcodes[i].name[1] == name[1])
4219 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004220 if (flags == ATC_FROM_TERM && (j = termcode_star(
4221 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004222 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004223 // Don't replace ESC[123;*X or ESC O*X with another when
4224 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004225 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004226 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004227 && s[len - 1]
4228 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004229 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004230 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004231 vim_free(s);
4232 return;
4233 }
4234 }
4235 else
4236 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004237 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004238 vim_free(termcodes[i].code);
4239 --tc_len;
4240 break;
4241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 }
4244 /*
4245 * Found alphabetical larger entry, move rest to insert new entry
4246 */
4247 for (j = tc_len; j > i; --j)
4248 termcodes[j] = termcodes[j - 1];
4249 break;
4250 }
4251
4252 termcodes[i].name[0] = name[0];
4253 termcodes[i].name[1] = name[1];
4254 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004255 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004256
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004257 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4258 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004259 termcodes[i].modlen = 0;
4260 j = termcode_star(s, len);
4261 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004262 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004263 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004264 // For "CSI[@;X" the "@" is not included in "modlen".
4265 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4266 --termcodes[i].modlen;
4267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 ++tc_len;
4269}
4270
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004271/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004272 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004273 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004274 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004275 */
4276 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004277termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004278{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004279 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004280 if (len >= 3 && code[len - 2] == '*')
4281 {
4282 if (len >= 5 && code[len - 3] == ';')
4283 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004284 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004285 return 1;
4286 }
4287 return 0;
4288}
4289
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004291find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292{
4293 int i;
4294
4295 for (i = 0; i < tc_len; ++i)
4296 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4297 return termcodes[i].code;
4298 return NULL;
4299}
4300
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004302get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303{
4304 if (i >= tc_len)
4305 return NULL;
4306 return &termcodes[i].name[0];
4307}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004309/*
4310 * Returns the length of the terminal code at index 'idx'.
4311 */
4312 int
4313get_termcode_len(int idx)
4314{
4315 return termcodes[idx].len;
4316}
4317
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004318 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004319del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320{
4321 int i;
4322
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004323 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 return;
4325
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004326 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
4328 for (i = 0; i < tc_len; ++i)
4329 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4330 {
4331 del_termcode_idx(i);
4332 return;
4333 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004334 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335}
4336
4337 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004338del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339{
4340 int i;
4341
4342 vim_free(termcodes[idx].code);
4343 --tc_len;
4344 for (i = idx; i < tc_len; ++i)
4345 termcodes[i] = termcodes[i + 1];
4346}
4347
4348#ifdef FEAT_TERMRESPONSE
4349/*
4350 * Called when detected that the terminal sends 8-bit codes.
4351 * Convert all 7-bit codes to their 8-bit equivalent.
4352 */
4353 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004354switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355{
4356 int i;
4357 int c;
4358
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004359 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 if (!term_is_8bit(T_NAME))
4361 {
4362 for (i = 0; i < tc_len; ++i)
4363 {
4364 c = term_7to8bit(termcodes[i].code);
4365 if (c != 0)
4366 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004367 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 termcodes[i].code[0] = c;
4369 }
4370 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004371 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
4373 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004374 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375}
4376#endif
4377
4378#ifdef CHECK_DOUBLE_CLICK
4379static linenr_T orig_topline = 0;
4380# ifdef FEAT_DIFF
4381static int orig_topfill = 0;
4382# endif
4383#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004384#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004386 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 * "orig_topline" is used to avoid detecting a double-click when the window
4388 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4389 */
4390/*
4391 * Set orig_topline. Used when jumping to another window, so that a double
4392 * click still works.
4393 */
4394 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004395set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396{
4397 orig_topline = wp->w_topline;
4398# ifdef FEAT_DIFF
4399 orig_topfill = wp->w_topfill;
4400# endif
4401}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004402
4403/*
4404 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4405 * topline and topfill.
4406 */
4407 int
4408is_mouse_topline(win_T *wp)
4409{
4410 return orig_topline == wp->w_topline
4411#ifdef FEAT_DIFF
4412 && orig_topfill == wp->w_topfill
4413#endif
4414 ;
4415}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416#endif
4417
4418/*
zeertzjqfc78a032022-04-26 22:11:38 +01004419 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4420 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4421 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004422 * Remove "slen" bytes.
4423 * Returns FAIL for error.
4424 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004425 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004426put_string_in_typebuf(
4427 int offset,
4428 int slen,
4429 char_u *string,
4430 int new_slen,
4431 char_u *buf,
4432 int bufsize,
4433 int *buflen)
4434{
4435 int extra = new_slen - slen;
4436
4437 string[new_slen] = NUL;
4438 if (buf == NULL)
4439 {
4440 if (extra < 0)
4441 // remove matched chars, taking care of noremap
4442 del_typebuf(-extra, offset);
4443 else if (extra > 0)
4444 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004445 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4446 == FAIL)
4447 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004448
4449 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4450 // typebuf.tb_buf[]!
4451 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4452 (size_t)new_slen);
4453 }
4454 else
4455 {
4456 if (extra < 0)
4457 // remove matched characters
4458 mch_memmove(buf + offset, buf + offset - extra,
4459 (size_t)(*buflen + offset + extra));
4460 else if (extra > 0)
4461 {
4462 // Insert the extra space we need. If there is insufficient
4463 // space return -1.
4464 if (*buflen + extra + new_slen >= bufsize)
4465 return FAIL;
4466 mch_memmove(buf + offset + extra, buf + offset,
4467 (size_t)(*buflen - offset));
4468 }
4469 mch_memmove(buf + offset, string, (size_t)new_slen);
4470 *buflen = *buflen + extra + new_slen;
4471 }
4472 return OK;
4473}
4474
4475/*
4476 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4477 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004478 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004479decode_modifiers(int n)
4480{
4481 int code = n - 1;
4482 int modifiers = 0;
4483
4484 if (code & 1)
4485 modifiers |= MOD_MASK_SHIFT;
4486 if (code & 2)
4487 modifiers |= MOD_MASK_ALT;
4488 if (code & 4)
4489 modifiers |= MOD_MASK_CTRL;
4490 if (code & 8)
4491 modifiers |= MOD_MASK_META;
4492 return modifiers;
4493}
4494
4495 static int
4496modifiers2keycode(int modifiers, int *key, char_u *string)
4497{
4498 int new_slen = 0;
4499
4500 if (modifiers != 0)
4501 {
4502 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004503 // make mappings work. This may result in a special key, such as
4504 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004505 *key = simplify_key(*key, &modifiers);
4506 if (modifiers != 0)
4507 {
4508 string[new_slen++] = K_SPECIAL;
4509 string[new_slen++] = (int)KS_MODIFIER;
4510 string[new_slen++] = modifiers;
4511 }
4512 }
4513 return new_slen;
4514}
4515
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004516#ifdef FEAT_TERMRESPONSE
4517/*
4518 * Handle a cursor position report.
4519 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004520 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004521handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004522{
4523 if (arg[0] == 2 && arg[1] >= 2)
4524 {
4525 char *aw = NULL;
4526
4527 LOG_TR(("Received U7 status: %s", tp));
4528 u7_status.tr_progress = STATUS_GOT;
4529 did_cursorhold = TRUE;
4530 if (arg[1] == 2)
4531 aw = "single";
4532 else if (arg[1] == 3)
4533 aw = "double";
4534 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4535 {
4536 // Setting the option causes a screen redraw. Do
4537 // that right away if possible, keeping any
4538 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004539 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004540# ifdef DEBUG_TERMRESPONSE
4541 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004542 int r = redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004543
4544 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4545 }
4546# else
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004547 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004548# endif
4549# ifdef FEAT_EVAL
4550 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4551# endif
4552 }
4553 }
4554 else if (arg[0] == 3)
4555 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004556 int value;
4557
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004558 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004559 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004560
4561 // Third row: xterm compatibility test.
4562 // If the cursor is on the first column then the terminal can handle
4563 // the request for cursor style and blinking.
4564 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4565 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4566 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004567 }
4568}
4569
4570/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004571 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004572 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004573 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004574 */
4575 static void
4576handle_version_response(int first, int *arg, int argc, char_u *tp)
4577{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004578 // The xterm version. It is set to zero when it can't be an actual xterm
4579 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004580 int version = arg[1];
4581
4582 LOG_TR(("Received CRV response: %s", tp));
4583 crv_status.tr_progress = STATUS_GOT;
4584 did_cursorhold = TRUE;
4585
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004586 // Reset terminal properties that are set based on the termresponse.
4587 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004588 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004589 init_term_props(
4590#ifdef FEAT_EVAL
4591 reset_term_props_on_termresponse
4592#else
4593 FALSE
4594#endif
4595 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004596
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004597 // If this code starts with CSI, you can bet that the
4598 // terminal uses 8-bit codes.
4599 if (tp[0] == CSI)
4600 switch_to_8bit();
4601
4602 // Screen sends 40500.
4603 // rxvt sends its version number: "20703" is 2.7.3.
4604 // Ignore it for when the user has set 'term' to xterm,
4605 // even though it's an rxvt.
4606 if (version > 20000)
4607 version = 0;
4608
zeertzjqfc78a032022-04-26 22:11:38 +01004609 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004610 if (first == '>' && argc == 3)
4611 {
4612 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004613
4614 // mintty 2.9.5 sends 77;20905;0c.
4615 // (77 is ASCII 'M' for mintty.)
4616 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004617 {
4618 // mintty can do SGR mouse reporting
4619 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4620 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004621
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004622 // If xterm version >= 141 try to get termcap codes. For other
4623 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004624 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004625 {
4626 LOG_TR(("Enable checking for XT codes"));
4627 check_for_codes = TRUE;
4628 need_gather = TRUE;
4629 req_codes_from_term();
4630 }
4631
4632 // libvterm sends 0;100;0
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01004633 // Konsole sends 0;115;0 and works the same way
4634 if ((version == 100 || version == 115) && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004635 {
4636 // If run from Vim $COLORS is set to the number of
4637 // colors the terminal supports. Otherwise assume
4638 // 256, libvterm supports even more.
4639 if (mch_getenv((char_u *)"COLORS") == NULL)
4640 may_adjust_color_count(256);
4641 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004642 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004643 }
4644
4645 if (version == 95)
4646 {
4647 // Mac Terminal.app sends 1;95;0
4648 if (arg[0] == 1 && arg[2] == 0)
4649 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004650 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4651 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004652 }
4653 // iTerm2 sends 0;95;0
4654 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004655 {
4656 // iTerm2 can do SGR mouse reporting
4657 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4658 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004659 // old iTerm2 sends 0;95;
4660 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004661 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004662 }
4663
4664 // screen sends 83;40500;0 83 is 'S' in ASCII.
4665 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004666 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004667 // screen supports SGR mouse codes since 4.7.0
4668 if (arg[1] >= 40700)
4669 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4670 else
4671 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4672 }
4673
4674 // If no recognized terminal has set mouse behavior, assume xterm.
4675 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4676 {
4677 // Xterm version 277 supports SGR.
4678 // Xterm version >= 95 supports mouse dragging.
4679 if (version >= 277)
4680 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004681 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004682 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004683 }
4684
4685 // Detect terminals that set $TERM to something like
4686 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004687 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004688 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004689 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004690 // xfce4-terminal sends 1;2802;0.
4691 // screen sends 83;40500;0
4692 // Assuming any version number over 2500 is not an
4693 // xterm (without the limit for rxvt and screen).
4694 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004695 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004696
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004697 else if (version == 136 && arg[2] == 0)
4698 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004699 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004700
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004701 // PuTTY sends 0;136;0
4702 if (arg[0] == 0)
4703 {
4704 // supports sgr-like mouse reporting.
4705 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4706 }
4707 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004708 }
4709
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004710 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
4711 // commented out.
4712 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
4713 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004714
4715 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004716 // 30600/40500 is a version number of GNU screen. DA2 support is added
4717 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4718 // compatibility checking does not detect GNU screen.
4719 if (arg[0] == 83 && arg[1] >= 30600)
4720 {
4721 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4722 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4723 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004724
4725 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004726 // 95, so assume anything below 95 is not xterm and hopefully supports
4727 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004728 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004729 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004730
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004731 // Getting the cursor style is only supported properly by xterm since
4732 // version 279 (otherwise it returns 0x18).
4733 if (version < 279)
4734 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4735
4736 /*
4737 * Take action on the detected properties.
4738 */
4739
4740 // Unless the underline RGB color is expected to work, disable "t_8u".
4741 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004742 // This may cause some flicker. Alternative would be to set "t_8u"
4743 // here if the terminal is expected to support it, but that might
4744 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01004745 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
4746 && *T_8U != NUL
4747 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004748 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004749 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4750 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004751 }
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01004752 if (*T_8U != NUL && write_t_8u_state == MAYBE)
4753 // Did skip writing t_8u, a complete redraw is needed.
4754 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01004755 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004756
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004757 // Only set 'ttymouse' automatically if it was not set
4758 // by the user already.
4759 if (!option_was_set((char_u *)"ttym")
4760 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4761 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4762 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004763 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004764 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4765 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4766 }
4767
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004768 // Only request the cursor style if t_SH and t_RS are
4769 // set. Only supported properly by xterm since version
4770 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004771 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004772 // Not for Terminal.app, it can't handle t_RS, it
4773 // echoes the characters to the screen.
4774 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004775 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004776 && *T_CSH != NUL
4777 && *T_CRS != NUL)
4778 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004779 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004780 LOG_TR(("Sending cursor style request"));
4781 out_str(T_CRS);
4782 termrequest_sent(&rcs_status);
4783 need_flush = TRUE;
4784 }
4785
4786 // Only request the cursor blink mode if t_RC set. Not
4787 // for Gnome terminal, it can't handle t_RC, it
4788 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004789 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004790 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004791 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004792 && *T_CRC != NUL)
4793 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01004794 MAY_WANT_TO_LOG_THIS;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004795 LOG_TR(("Sending cursor blink mode request"));
4796 out_str(T_CRC);
4797 termrequest_sent(&rbm_status);
4798 need_flush = TRUE;
4799 }
4800
4801 if (need_flush)
4802 out_flush();
4803 }
4804}
4805
4806/*
4807 * Handle a sequence with key and modifier, one of:
4808 * {lead}27;{modifier};{key}~
4809 * {lead}{key};{modifier}u
4810 * Returns the difference in length.
4811 */
4812 static int
4813handle_key_with_modifier(
4814 int *arg,
4815 int trail,
4816 int csi_len,
4817 int offset,
4818 char_u *buf,
4819 int bufsize,
4820 int *buflen)
4821{
4822 int key;
4823 int modifiers;
4824 int new_slen;
4825 char_u string[MAX_KEY_CODE_LEN + 1];
4826
4827 seenModifyOtherKeys = TRUE;
4828 if (trail == 'u')
4829 key = arg[0];
4830 else
4831 key = arg[2];
4832
4833 modifiers = decode_modifiers(arg[1]);
4834
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02004835 // Some keys need adjustment when the Ctrl modifier is used.
4836 key = may_adjust_key_for_ctrl(modifiers, key);
4837
Bram Moolenaaref6746f2020-06-20 14:43:23 +02004838 // May remove the shift modifier if it's already included in the key.
4839 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004840
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004841 // insert modifiers with KS_MODIFIER
4842 new_slen = modifiers2keycode(modifiers, &key, string);
4843
Bram Moolenaar749bc952020-10-31 16:33:47 +01004844 if (IS_SPECIAL(key))
4845 {
4846 string[new_slen++] = K_SPECIAL;
4847 string[new_slen++] = KEY2TERMCAP0(key);
4848 string[new_slen++] = KEY2TERMCAP1(key);
4849 }
4850 else if (has_mbyte)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004851 new_slen += (*mb_char2bytes)(key, string + new_slen);
4852 else
4853 string[new_slen++] = key;
4854
4855 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4856 buf, bufsize, buflen) == FAIL)
4857 return -1;
4858 return new_slen - csi_len + offset;
4859}
4860
4861/*
4862 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004863 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004864 *
4865 * - Cursor position report: {lead}{row};{col}R
4866 * The final byte must be 'R'. It is used for checking the
4867 * ambiguous-width character state.
4868 *
4869 * - window position reply: {lead}3;{x};{y}t
4870 *
4871 * - key with modifiers when modifyOtherKeys is enabled:
4872 * {lead}27;{modifier};{key}~
4873 * {lead}{key};{modifier}u
4874 * Return 0 for no match, -1 for partial match, > 0 for full match.
4875 */
4876 static int
4877handle_csi(
4878 char_u *tp,
4879 int len,
4880 char_u *argp,
4881 int offset,
4882 char_u *buf,
4883 int bufsize,
4884 int *buflen,
4885 char_u *key_name,
4886 int *slen)
4887{
4888 int first = -1; // optional char right after {lead}
4889 int trail; // char that ends CSI sequence
4890 int arg[3] = {-1, -1, -1}; // argument numbers
4891 int argc; // number of arguments
4892 char_u *ap = argp;
4893 int csi_len;
4894
4895 // Check for non-digit after CSI.
4896 if (!VIM_ISDIGIT(*ap))
4897 first = *ap++;
4898
4899 // Find up to three argument numbers.
4900 for (argc = 0; argc < 3; )
4901 {
4902 if (ap >= tp + len)
4903 return -1;
4904 if (*ap == ';')
4905 arg[argc++] = -1; // omitted number
4906 else if (VIM_ISDIGIT(*ap))
4907 {
4908 arg[argc] = 0;
4909 for (;;)
4910 {
4911 if (ap >= tp + len)
4912 return -1;
4913 if (!VIM_ISDIGIT(*ap))
4914 break;
4915 arg[argc] = arg[argc] * 10 + (*ap - '0');
4916 ++ap;
4917 }
4918 ++argc;
4919 }
4920 if (*ap == ';')
4921 ++ap;
4922 else
4923 break;
4924 }
4925
4926 // mrxvt has been reported to have "+" in the version. Assume
4927 // the escape sequence ends with a letter or one of "{|}~".
4928 while (ap < tp + len
4929 && !(*ap >= '{' && *ap <= '~')
4930 && !ASCII_ISALPHA(*ap))
4931 ++ap;
4932 if (ap >= tp + len)
4933 return -1;
4934 trail = *ap;
4935 csi_len = (int)(ap - tp) + 1;
4936
4937 // Cursor position report: Eat it when there are 2 arguments
4938 // and it ends in 'R'. Also when u7_status is not "sent", it
4939 // may be from a previous Vim that just exited. But not for
4940 // <S-F3>, it sends something similar, check for row and column
4941 // to make sense.
4942 if (first == -1 && argc == 2 && trail == 'R')
4943 {
4944 handle_u7_response(arg, tp, csi_len);
4945
4946 key_name[0] = (int)KS_EXTRA;
4947 key_name[1] = (int)KE_IGNORE;
4948 *slen = csi_len;
4949 }
4950
4951 // Version string: Eat it when there is at least one digit and
4952 // it ends in 'c'
4953 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
4954 {
4955 handle_version_response(first, arg, argc, tp);
4956
4957 *slen = csi_len;
4958# ifdef FEAT_EVAL
4959 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
4960# endif
4961 apply_autocmds(EVENT_TERMRESPONSE,
4962 NULL, NULL, FALSE, curbuf);
4963 key_name[0] = (int)KS_EXTRA;
4964 key_name[1] = (int)KE_IGNORE;
4965 }
4966
4967 // Check blinking cursor from xterm:
4968 // {lead}?12;1$y set
4969 // {lead}?12;2$y not set
4970 //
4971 // {lead} can be <Esc>[ or CSI
4972 else if (rbm_status.tr_progress == STATUS_SENT
4973 && first == '?'
4974 && ap == argp + 6
4975 && arg[0] == 12
4976 && ap[-1] == '$'
4977 && trail == 'y')
4978 {
4979 initial_cursor_blink = (arg[1] == '1');
4980 rbm_status.tr_progress = STATUS_GOT;
4981 LOG_TR(("Received cursor blinking mode response: %s", tp));
4982 key_name[0] = (int)KS_EXTRA;
4983 key_name[1] = (int)KE_IGNORE;
4984 *slen = csi_len;
4985# ifdef FEAT_EVAL
4986 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
4987# endif
4988 }
4989
4990 // Check for a window position response from the terminal:
4991 // {lead}3;{x};{y}t
4992 else if (did_request_winpos && argc == 3 && arg[0] == 3
4993 && trail == 't')
4994 {
4995 winpos_x = arg[1];
4996 winpos_y = arg[2];
4997 // got finished code: consume it
4998 key_name[0] = (int)KS_EXTRA;
4999 key_name[1] = (int)KE_IGNORE;
5000 *slen = csi_len;
5001
5002 if (--did_request_winpos <= 0)
5003 winpos_status.tr_progress = STATUS_GOT;
5004 }
5005
5006 // Key with modifier:
5007 // {lead}27;{modifier};{key}~
5008 // {lead}{key};{modifier}u
5009 else if ((arg[0] == 27 && argc == 3 && trail == '~')
5010 || (argc == 2 && trail == 'u'))
5011 {
5012 return len + handle_key_with_modifier(arg, trail,
5013 csi_len, offset, buf, bufsize, buflen);
5014 }
5015
5016 // else: Unknown CSI sequence. We could drop it, but then the
5017 // user can't create a map for it.
5018 return 0;
5019}
5020
5021/*
5022 * Handle an OSC sequence, fore/background color response from the terminal:
5023 *
5024 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5025 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5026 *
5027 * {code} is 10 for foreground, 11 for background
5028 * {lead} can be <Esc>] or OSC
5029 * {tail} can be '\007', <Esc>\ or STERM.
5030 *
5031 * Consume any code that starts with "{lead}11;", it's also
5032 * possible that "rgba" is following.
5033 */
5034 static int
5035handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5036{
5037 int i, j;
5038
5039 j = 1 + (tp[0] == ESC);
5040 if (len >= j + 3 && (argp[0] != '1'
5041 || (argp[1] != '1' && argp[1] != '0')
5042 || argp[2] != ';'))
5043 i = 0; // no match
5044 else
5045 for (i = j; i < len; ++i)
5046 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5047 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5048 {
5049 int is_bg = argp[1] == '1';
5050 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5051 && tp[j + 16] == '/';
5052
5053 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5054 && (is_4digit
5055 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5056 {
5057 char_u *tp_r = tp + j + 7;
5058 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5059 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
5060# ifdef FEAT_TERMINAL
5061 int rval, gval, bval;
5062
5063 rval = hexhex2nr(tp_r);
5064 gval = hexhex2nr(tp_b);
5065 bval = hexhex2nr(tp_g);
5066# endif
5067 if (is_bg)
5068 {
5069 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5070 *tp_b) ? "light" : "dark";
5071
5072 LOG_TR(("Received RBG response: %s", tp));
5073 rbg_status.tr_progress = STATUS_GOT;
5074# ifdef FEAT_TERMINAL
5075 bg_r = rval;
5076 bg_g = gval;
5077 bg_b = bval;
5078# endif
5079 if (!option_was_set((char_u *)"bg")
5080 && STRCMP(p_bg, new_bg_val) != 0)
5081 {
5082 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005083 set_option_value_give_err((char_u *)"bg",
5084 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005085 reset_option_was_set((char_u *)"bg");
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005086 redraw_asap(UPD_CLEAR);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005087 }
5088 }
5089# ifdef FEAT_TERMINAL
5090 else
5091 {
5092 LOG_TR(("Received RFG response: %s", tp));
5093 rfg_status.tr_progress = STATUS_GOT;
5094 fg_r = rval;
5095 fg_g = gval;
5096 fg_b = bval;
5097 }
5098# endif
5099 }
5100
5101 // got finished code: consume it
5102 key_name[0] = (int)KS_EXTRA;
5103 key_name[1] = (int)KE_IGNORE;
5104 *slen = i + 1 + (tp[i] == ESC);
5105# ifdef FEAT_EVAL
5106 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5107 : VV_TERMRFGRESP, tp, *slen);
5108# endif
5109 break;
5110 }
5111 if (i == len)
5112 {
5113 LOG_TR(("not enough characters for RB"));
5114 return FAIL;
5115 }
5116 return OK;
5117}
5118
5119/*
5120 * Check for key code response from xterm:
5121 * {lead}{flag}+r<hex bytes><{tail}
5122 *
5123 * {lead} can be <Esc>P or DCS
5124 * {flag} can be '0' or '1'
5125 * {tail} can be Esc>\ or STERM
5126 *
5127 * Check for cursor shape response from xterm:
5128 * {lead}1$r<digit> q{tail}
5129 *
5130 * {lead} can be <Esc>P or DCS
5131 * {tail} can be <Esc>\ or STERM
5132 *
5133 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5134 */
5135 static int
5136handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5137{
5138 int i, j;
5139
5140 j = 1 + (tp[0] == ESC);
5141 if (len < j + 3)
5142 i = len; // need more chars
5143 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
5144 i = 0; // no match
5145 else if (argp[1] == '+')
5146 // key code response
5147 for (i = j; i < len; ++i)
5148 {
5149 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5150 || tp[i] == STERM)
5151 {
5152 if (i - j >= 3)
5153 got_code_from_term(tp + j, i);
5154 key_name[0] = (int)KS_EXTRA;
5155 key_name[1] = (int)KE_IGNORE;
5156 *slen = i + 1 + (tp[i] == ESC);
5157 break;
5158 }
5159 }
5160 else
5161 {
5162 // Probably the cursor shape response. Make sure that "i"
5163 // is equal to "len" when there are not sufficient
5164 // characters.
5165 for (i = j + 3; i < len; ++i)
5166 {
5167 if (i - j == 3 && !isdigit(tp[i]))
5168 break;
5169 if (i - j == 4 && tp[i] != ' ')
5170 break;
5171 if (i - j == 5 && tp[i] != 'q')
5172 break;
5173 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5174 break;
5175 if ((i - j == 6 && tp[i] == STERM)
5176 || (i - j == 7 && tp[i] == '\\'))
5177 {
5178 int number = argp[3] - '0';
5179
5180 // 0, 1 = block blink, 2 = block
5181 // 3 = underline blink, 4 = underline
5182 // 5 = vertical bar blink, 6 = vertical bar
5183 number = number == 0 ? 1 : number;
5184 initial_cursor_shape = (number + 1) / 2;
5185 // The blink flag is actually inverted, compared to
5186 // the value set with T_SH.
5187 initial_cursor_shape_blink =
5188 (number & 1) ? FALSE : TRUE;
5189 rcs_status.tr_progress = STATUS_GOT;
5190 LOG_TR(("Received cursor shape response: %s", tp));
5191
5192 key_name[0] = (int)KS_EXTRA;
5193 key_name[1] = (int)KE_IGNORE;
5194 *slen = i + 1;
5195# ifdef FEAT_EVAL
5196 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5197# endif
5198 break;
5199 }
5200 }
5201 }
5202
5203 if (i == len)
5204 {
5205 // These codes arrive many together, each code can be
5206 // truncated at any point.
5207 LOG_TR(("not enough characters for XT"));
5208 return FAIL;
5209 }
5210 return OK;
5211}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005212#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005213
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 * Check if typebuf.tb_buf[] contains a terminal key code.
5216 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005217 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005219 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 * With a match, the match is removed, the replacement code is inserted in
5221 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5222 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005223 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5224 * "buflen" is then the length of the string in buf[] and is updated for
5225 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 */
5227 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005228check_termcode(
5229 int max_offset,
5230 char_u *buf,
5231 int bufsize,
5232 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233{
5234 char_u *tp;
5235 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005236 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005237 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005239 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 int offset;
5241 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005242 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005243 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005244 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005245 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 char_u string[MAX_KEY_CODE_LEN + 1];
5247 int i, j;
5248 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250
5251 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5252
5253 /*
5254 * Speed up the checks for terminal codes by gathering all first bytes
5255 * used in termleader[]. Often this is just a single <Esc>.
5256 */
5257 if (need_gather)
5258 gather_termleader();
5259
5260 /*
5261 * Check at several positions in typebuf.tb_buf[], to catch something like
5262 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5263 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005264 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 * This is used often, KEEP IT FAST!
5266 */
5267 for (offset = 0; offset < max_offset; ++offset)
5268 {
5269 if (buf == NULL)
5270 {
5271 if (offset >= typebuf.tb_len)
5272 break;
5273 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005274 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
5276 else
5277 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005278 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 break;
5280 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005281 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 }
5283
5284 /*
5285 * Don't check characters after K_SPECIAL, those are already
5286 * translated terminal chars (avoid translating ~@^Hx).
5287 */
5288 if (*tp == K_SPECIAL)
5289 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005290 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 continue;
5292 }
5293
5294 /*
5295 * Skip this position if the character does not appear as the first
5296 * character in term_strings. This speeds up a lot, since most
5297 * termcodes start with the same character (ESC or CSI).
5298 */
5299 i = *tp;
5300 for (p = termleader; *p && *p != i; ++p)
5301 ;
5302 if (*p == NUL)
5303 continue;
5304
5305 /*
5306 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5307 * are in Insert mode.
5308 */
Bram Moolenaar24959102022-05-07 20:01:16 +01005309 if (*tp == ESC && !p_ek && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 continue;
5311
Bram Moolenaar27efc622022-07-01 16:35:45 +01005312 tp[len] = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005313 key_name[0] = NUL; // no key name found yet
5314 key_name[1] = NUL; // no key name found yet
5315 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
5317#ifdef FEAT_GUI
5318 if (gui.in_use)
5319 {
5320 /*
5321 * GUI special key codes are all of the form [CSI xx].
5322 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005323 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
5325 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005326 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 slen = 3;
5328 key_name[0] = tp[1];
5329 key_name[1] = tp[2];
5330 }
5331 }
5332 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005333#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005335 int mouse_index_found = -1;
5336
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 for (idx = 0; idx < tc_len; ++idx)
5338 {
5339 /*
5340 * Ignore the entry if we are not at the start of
5341 * typebuf.tb_buf[]
5342 * and there are not enough characters to make a match.
5343 * But only when the 'K' flag is in 'cpoptions'.
5344 */
5345 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005346 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 if (cpo_koffset && offset && len < slen)
5348 continue;
5349 if (STRNCMP(termcodes[idx].code, tp,
5350 (size_t)(slen > len ? len : slen)) == 0)
5351 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005352 int looks_like_mouse_start = FALSE;
5353
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005354 if (len < slen) // got a partial sequence
5355 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
5357 /*
5358 * When found a keypad key, check if there is another key
5359 * that matches and use that one. This makes <Home> to be
5360 * found instead of <kHome> when they produce the same
5361 * key code.
5362 */
5363 if (termcodes[idx].name[0] == 'K'
5364 && VIM_ISDIGIT(termcodes[idx].name[1]))
5365 {
5366 for (j = idx + 1; j < tc_len; ++j)
5367 if (termcodes[j].len == slen &&
5368 STRNCMP(termcodes[idx].code,
5369 termcodes[j].code, slen) == 0)
5370 {
5371 idx = j;
5372 break;
5373 }
5374 }
5375
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005376 if (slen == 2 && len > 2
5377 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005378 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005379 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005380 // The mouse termcode "ESC [" is also the prefix of
5381 // "ESC [ I" (focus gained) and other keys. Check some
5382 // more bytes to find out.
5383 if (!isdigit(tp[2]))
5384 {
5385 // ESC [ without number following: Only use it when
5386 // there is no other match.
5387 looks_like_mouse_start = TRUE;
5388 }
5389 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5390 {
5391 char_u *nr = tp + 2;
5392 int count = 0;
5393
5394 // If a digit is following it could be a key with
5395 // modifier, e.g., ESC [ 1;2P. Can be confused
5396 // with DEC_MOUSE, which requires four numbers
5397 // following. If not then it can't be a DEC_MOUSE
5398 // code.
5399 for (;;)
5400 {
5401 ++count;
5402 (void)getdigits(&nr);
5403 if (nr >= tp + len)
5404 return -1; // partial sequence
5405 if (*nr != ';')
5406 break;
5407 ++nr;
5408 if (nr >= tp + len)
5409 return -1; // partial sequence
5410 }
5411 if (count < 4)
5412 continue; // no match
5413 }
5414 }
5415 if (looks_like_mouse_start)
5416 {
5417 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005418 if (mouse_index_found < 0)
5419 mouse_index_found = idx;
5420 }
5421 else
5422 {
5423 key_name[0] = termcodes[idx].name[0];
5424 key_name[1] = termcodes[idx].name[1];
5425 break;
5426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005428
5429 /*
5430 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005431 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005432 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005433 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5434 * When there is a modifier the * matches a number.
5435 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005436 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005437 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005438 {
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005439 int at_code;
5440
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005441 modslen = termcodes[idx].modlen;
5442 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005443 continue;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005444 at_code = termcodes[idx].code[modslen] == '@';
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005445 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005446 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005447 {
5448 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005449
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005450 if (len <= modslen) // got a partial sequence
5451 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005452
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005453 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005454 // no modifiers
5455 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005456 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005457 // no match for "code;*X" with "code;"
5458 continue;
5459 else if (at_code && tp[modslen] != '1')
5460 // no match for "<Esc>[@" with "<Esc>[1"
5461 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005462 else
5463 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005464 // Skip over the digits, the final char must
5465 // follow. URXVT can use a negative value, thus
5466 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005467 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005468 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005469 ;
5470 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005471 if (len < j) // got a partial sequence
5472 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005473 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005474 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005475
Bram Moolenaara529ce02017-06-22 22:37:57 +02005476 modifiers_start = tp + slen - 2;
5477
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005478 // Match! Convert modifier bits.
5479 n = atoi((char *)modifiers_start);
5480 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005481
5482 slen = j;
5483 }
5484 key_name[0] = termcodes[idx].name[0];
5485 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005486 break;
5487 }
5488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005490 if (idx == tc_len && mouse_index_found >= 0)
5491 {
5492 key_name[0] = termcodes[mouse_index_found].name[0];
5493 key_name[1] = termcodes[mouse_index_found].name[1];
5494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 }
5496
5497#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005498 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005499 // Mouse codes of DEC and pterm start with <ESC>[. When
5500 // detecting the start of these mouse codes they might as well be
5501 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005502# ifdef FEAT_MOUSE_DEC
5503 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005504# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005505# ifdef FEAT_MOUSE_PTERM
5506 || key_name[0] == KS_PTERM_MOUSE
5507# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005508 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005510 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5511
5512 /*
5513 * Check for responses from the terminal starting with {lead}:
5514 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005515 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005516 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005517 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005518 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005519 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005520 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005521 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005522 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005523 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005524 * - window position reply: {lead}3;{x};{y}t
5525 *
5526 * - key with modifiers when modifyOtherKeys is enabled:
5527 * {lead}27;{modifier};{key}~
5528 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005529 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005530 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005531 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005532 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005534 int resp = handle_csi(tp, len, argp, offset, buf,
5535 bufsize, buflen, key_name, &slen);
5536 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005537 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005538# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005539 if (resp == -1)
5540 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005541# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005542 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005543 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005544 }
5545
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005546 // Check for fore/background color response from the terminal,
5547 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005548 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005549 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5550 || tp[0] == OSC))
5551 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005552 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005553 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 }
5555
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005556 // Check for key code response from xterm,
5557 // starting with <Esc>P or DCS
Bram Moolenaarafd78262019-05-10 23:10:31 +02005558 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005559 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 || tp[0] == DCS))
5561 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005562 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005563 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 }
5565 }
5566#endif
5567
5568 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005569 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005571 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005573#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 /*
5575 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5576 * so that we know which window to scroll later.
5577 */
5578 if (gui.in_use
5579 && key_name[0] == (int)KS_EXTRA
5580 && (key_name[1] == (int)KE_X1MOUSE
5581 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005582 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005583 || key_name[1] == (int)KE_MOUSELEFT
5584 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 || key_name[1] == (int)KE_MOUSEDOWN
5586 || key_name[1] == (int)KE_MOUSEUP))
5587 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005588 char_u bytes[6];
5589 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5590
5591 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 return -1;
5593 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5594 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5595 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005596 // equal to K_MOUSEMOVE
5597 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
5598 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 }
5600 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 /*
5603 * If it is a mouse click, get the coordinates.
5604 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005605 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005606#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005607 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005608#endif
5609#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005610 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005611#endif
5612#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005613 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005614#endif
5615#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005616 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005617#endif
5618#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005619 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005620#endif
5621#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005622 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005623#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005624 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005625 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005627 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5628 &modifiers) == -1)
5629 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
5632#ifdef FEAT_GUI
5633 /*
5634 * If using the GUI, then we get menu and scrollbar events.
5635 *
5636 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5637 * four bytes which are to be taken as a pointer to the vimmenu_T
5638 * structure.
5639 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005640 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005641 * is one byte with the tab index.
5642 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5644 * by one byte representing the scrollbar number, and then four bytes
5645 * representing a long_u which is the new value of the scrollbar.
5646 *
5647 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5648 * KE_FILLER followed by four bytes representing a long_u which is the
5649 * new value of the scrollbar.
5650 */
5651# ifdef FEAT_MENU
5652 else if (key_name[0] == (int)KS_MENU)
5653 {
5654 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005655 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 if (num_bytes == -1)
5658 return -1;
5659 current_menu = (vimmenu_T *)val;
5660 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005661
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005662 // The menu may have been deleted right after it was used, check
5663 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005664 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5665 {
5666 key_name[0] = KS_EXTRA;
5667 key_name[1] = (int)KE_IGNORE;
5668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
5670# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005671# ifdef FEAT_GUI_TABLINE
5672 else if (key_name[0] == (int)KS_TABLINE)
5673 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005674 // Selecting tabline tab or using its menu.
5675 char_u bytes[6];
5676 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5677
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005678 if (num_bytes == -1)
5679 return -1;
5680 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005681 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005682 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005683 slen += num_bytes;
5684 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005685 else if (key_name[0] == (int)KS_TABMENU)
5686 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005687 // Selecting tabline tab or using its menu.
5688 char_u bytes[6];
5689 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5690
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005691 if (num_bytes == -1)
5692 return -1;
5693 current_tab = (int)bytes[0];
5694 current_tabmenu = (int)bytes[1];
5695 slen += num_bytes;
5696 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698# ifndef USE_ON_FLY_SCROLL
5699 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5700 {
5701 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005702 char_u bytes[6];
5703 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005705 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 j = 0;
5707 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5708 && tp[j + 2] != NUL; ++i)
5709 {
5710 j += 3;
5711 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5712 if (num_bytes == -1)
5713 break;
5714 if (i == 0)
5715 current_scrollbar = (int)bytes[0];
5716 else if (current_scrollbar != (int)bytes[0])
5717 break;
5718 j += num_bytes;
5719 num_bytes = get_long_from_buf(tp + j, &val);
5720 if (num_bytes == -1)
5721 break;
5722 scrollbar_value = val;
5723 j += num_bytes;
5724 slen = j;
5725 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005726 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 return -1;
5728 }
5729 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5730 {
5731 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005732 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005734 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 j = 0;
5736 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5737 && tp[j + 2] != NUL; ++i)
5738 {
5739 j += 3;
5740 num_bytes = get_long_from_buf(tp + j, &val);
5741 if (num_bytes == -1)
5742 break;
5743 scrollbar_value = val;
5744 j += num_bytes;
5745 slen = j;
5746 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005747 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 return -1;
5749 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005750# endif // !USE_ON_FLY_SCROLL
5751#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005753#if (defined(UNIX) || defined(VMS))
5754 /*
5755 * Handle FocusIn/FocusOut event sequences reported by XTerm.
5756 * (CSI I/CSI O)
5757 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00005758 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005759# ifdef FEAT_GUI
5760 && !gui.in_use
5761# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005762 )
5763 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005764 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005765 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005766 if (!focus_state)
5767 {
5768 ui_focus_change(TRUE);
5769 did_cursorhold = TRUE;
5770 focus_state = TRUE;
5771 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005772 key_name[1] = (int)KE_IGNORE;
5773 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01005774 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005775 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005776 if (focus_state)
5777 {
5778 ui_focus_change(FALSE);
5779 did_cursorhold = TRUE;
5780 focus_state = FALSE;
5781 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005782 key_name[1] = (int)KE_IGNORE;
5783 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005784 }
5785#endif
5786
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005787 /*
5788 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5789 */
5790 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5791
5792 /*
5793 * Add any modifier codes to our string.
5794 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005795 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005796
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005797 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005798 key_name[0] = KEY2TERMCAP0(key);
5799 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005801 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005802 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005803 if (has_mbyte)
5804 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5805 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005806 string[new_slen++] = key_name[1];
5807 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005808 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5809 && key_name[1] == KE_IGNORE)
5810 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005811 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5812 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005813 retval = KEYLEN_REMOVED;
5814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 else
5816 {
5817 string[new_slen++] = K_SPECIAL;
5818 string[new_slen++] = key_name[0];
5819 string[new_slen++] = key_name[1];
5820 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005821 if (put_string_in_typebuf(offset, slen, string, new_slen,
5822 buf, bufsize, buflen) == FAIL)
5823 return -1;
5824 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
5826
Bram Moolenaar2951b772013-07-03 12:45:31 +02005827#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005828 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005829#endif
5830
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005831 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832}
5833
Bram Moolenaar9377df32017-10-15 13:22:01 +02005834#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005835/*
5836 * Get the text foreground color, if known.
5837 */
5838 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005839term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005840{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005841 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005842 {
5843 *r = fg_r;
5844 *g = fg_g;
5845 *b = fg_b;
5846 }
5847}
5848
5849/*
5850 * Get the text background color, if known.
5851 */
5852 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005853term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005854{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005855 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005856 {
5857 *r = bg_r;
5858 *g = bg_g;
5859 *b = bg_b;
5860 }
5861}
5862#endif
5863
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864/*
5865 * Replace any terminal code strings in from[] with the equivalent internal
5866 * vim representation. This is used for the "from" and "to" part of a
5867 * mapping, and the "to" part of a menu command.
5868 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5869 * '<'.
5870 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5871 *
5872 * The replacement is done in result[] and finally copied into allocated
5873 * memory. If this all works well *bufp is set to the allocated memory and a
5874 * pointer to it is returned. If something fails *bufp is set to NULL and from
5875 * is returned.
5876 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005877 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5878 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5879 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5880 * used instead of a CTRL-V.
5881 *
5882 * Flags:
5883 * REPTERM_FROM_PART see above
5884 * REPTERM_DO_LT also translate <lt>
5885 * REPTERM_SPECIAL always accept <key> notation
5886 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5887 *
5888 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5889 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005891 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005892replace_termcodes(
5893 char_u *from,
5894 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005895 int flags,
5896 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
5898 int i;
5899 int slen;
5900 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01005901 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005903 int do_backslash; // backslash is a special character
5904 int do_special; // recognize <> key codes
5905 int do_key_code; // recognize raw key codes
5906 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01005907 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
5909 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02005910 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
5911 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01005913 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914
5915 /*
5916 * Allocate space for the translation. Worst case a single character is
5917 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01005918 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01005920 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01005921 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 {
5923 *bufp = NULL;
5924 return from;
5925 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01005926 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
5928 /*
5929 * Check for #n at start only: function key n
5930 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005931 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 {
5933 result[dlen++] = K_SPECIAL;
5934 result[dlen++] = 'k';
5935 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005936 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005938 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 src += 2;
5940 }
5941
5942 /*
5943 * Copy each byte from *from to result[dlen]
5944 */
5945 while (*src != NUL)
5946 {
5947 /*
5948 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005949 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005951 if (do_special && ((flags & REPTERM_DO_LT)
5952 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 {
5954#ifdef FEAT_EVAL
5955 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01005956 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
5957 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01005959 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
5960 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 */
5962 if (STRNICMP(src, "<SID>", 5) == 0)
5963 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005964 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00005965 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 else
5967 {
Bram Moolenaar89445512022-04-14 12:58:23 +01005968 char_u *dot;
5969 long sid = current_sctx.sc_sid;
5970
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01005972 if (in_vim9script()
5973 && (dot = vim_strchr(src, '.')) != NULL)
5974 {
5975 imported_T *imp = find_imported(src, dot - src, FALSE);
5976
5977 if (imp != NULL)
5978 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01005979 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
5980 size_t len;
5981
Bram Moolenaar89445512022-04-14 12:58:23 +01005982 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01005983 if (si->sn_autoload_prefix != NULL)
5984 {
5985 // Turn "<SID>name.Func"
5986 // into "scriptname#Func".
5987 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01005988 if (ga_grow(&ga,
5989 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01005990 {
5991 ga_clear(&ga);
5992 *bufp = NULL;
5993 return from;
5994 }
5995 result = ga.ga_data;
5996 STRCPY(result + dlen, si->sn_autoload_prefix);
5997 dlen += len;
5998 continue;
5999 }
6000 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006001 }
6002 }
6003
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 result[dlen++] = K_SPECIAL;
6005 result[dlen++] = (int)KS_EXTRA;
6006 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006007 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006008 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 result[dlen++] = '_';
6010 continue;
6011 }
6012 }
6013#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006014 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6015 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006016 TRUE, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 if (slen)
6018 {
6019 dlen += slen;
6020 continue;
6021 }
6022 }
6023
6024 /*
6025 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6026 * Note that this is also checked after replacing the <> form.
6027 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6028 * it could be a character in the file.
6029 */
6030 if (do_key_code)
6031 {
6032 i = find_term_bykeys(src);
6033 if (i >= 0)
6034 {
6035 result[dlen++] = K_SPECIAL;
6036 result[dlen++] = termcodes[i].name[0];
6037 result[dlen++] = termcodes[i].name[1];
6038 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006039 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 continue;
6041 }
6042 }
6043
6044#ifdef FEAT_EVAL
6045 if (do_special)
6046 {
6047 char_u *p, *s, len;
6048
6049 /*
6050 * Replace <Leader> by the value of "mapleader".
6051 * Replace <LocalLeader> by the value of "maplocalleader".
6052 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6053 */
6054 if (STRNICMP(src, "<Leader>", 8) == 0)
6055 {
6056 len = 8;
6057 p = get_var_value((char_u *)"g:mapleader");
6058 }
6059 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6060 {
6061 len = 13;
6062 p = get_var_value((char_u *)"g:maplocalleader");
6063 }
6064 else
6065 {
6066 len = 0;
6067 p = NULL;
6068 }
6069 if (len != 0)
6070 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006071 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6073 s = (char_u *)"\\";
6074 else
6075 s = p;
6076 while (*s != NUL)
6077 result[dlen++] = *s++;
6078 src += len;
6079 continue;
6080 }
6081 }
6082#endif
6083
6084 /*
6085 * Remove CTRL-V and ignore the next character.
6086 * For "from" side the CTRL-V at the end is included, for the "to"
6087 * part it is removed.
6088 * If 'cpoptions' does not contain 'B', also accept a backslash.
6089 */
6090 key = *src;
6091 if (key == Ctrl_V || (do_backslash && key == '\\'))
6092 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006093 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 if (*src == NUL)
6095 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006096 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 result[dlen++] = key;
6098 break;
6099 }
6100 }
6101
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006102 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006103 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 {
6105 /*
6106 * If the character is K_SPECIAL, replace it with K_SPECIAL
6107 * KS_SPECIAL KE_FILLER.
6108 * If compiled with the GUI replace CSI with K_CSI.
6109 */
6110 if (*src == K_SPECIAL)
6111 {
6112 result[dlen++] = K_SPECIAL;
6113 result[dlen++] = KS_SPECIAL;
6114 result[dlen++] = KE_FILLER;
6115 }
6116# ifdef FEAT_GUI
6117 else if (*src == CSI)
6118 {
6119 result[dlen++] = K_SPECIAL;
6120 result[dlen++] = KS_EXTRA;
6121 result[dlen++] = (int)KE_CSI;
6122 }
6123# endif
6124 else
6125 result[dlen++] = *src;
6126 ++src;
6127 }
6128 }
6129 result[dlen] = NUL;
6130
6131 /*
6132 * Copy the new string to allocated memory.
6133 * If this fails, just return from.
6134 */
6135 if ((*bufp = vim_strsave(result)) != NULL)
6136 from = *bufp;
6137 vim_free(result);
6138 return from;
6139}
6140
6141/*
6142 * Find a termcode with keys 'src' (must be NUL terminated).
6143 * Return the index in termcodes[], or -1 if not found.
6144 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006145 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006146find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
6148 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006149 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150
6151 for (i = 0; i < tc_len; ++i)
6152 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006153 if (slen == termcodes[i].len
6154 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 return i;
6156 }
6157 return -1;
6158}
6159
6160/*
6161 * Gather the first characters in the terminal key codes into a string.
6162 * Used to speed up check_termcode().
6163 */
6164 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006165gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166{
6167 int i;
6168 int len = 0;
6169
6170#ifdef FEAT_GUI
6171 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006172 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173#endif
6174#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006175 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006176 termleader[len++] = DCS; // the termcode response starts with DCS
6177 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178#endif
6179 termleader[len] = NUL;
6180
6181 for (i = 0; i < tc_len; ++i)
6182 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6183 {
6184 termleader[len++] = termcodes[i].code[0];
6185 termleader[len] = NUL;
6186 }
6187
6188 need_gather = FALSE;
6189}
6190
6191/*
6192 * Show all termcodes (for ":set termcap")
6193 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006194 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 */
6196 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006197show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198{
6199 int col;
6200 int *items;
6201 int item_count;
6202 int run;
6203 int row, rows;
6204 int cols;
6205 int i;
6206 int len;
6207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006208#define INC3 27 // try to make three columns
6209#define INC2 40 // try to make two columns
6210#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006212 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006214 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 if (items == NULL)
6216 return;
6217
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006218 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006219 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220
6221 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006222 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006224 * 2. display the medium items (medium length strings)
6225 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006226 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006228 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 {
6230 /*
6231 * collect the items in items[]
6232 */
6233 item_count = 0;
6234 for (i = 0; i < tc_len; i++)
6235 {
6236 len = show_one_termcode(termcodes[i].name,
6237 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006238 if ((flags & OPT_ONECOLUMN) ||
6239 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006240 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006241 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 items[item_count++] = i;
6243 }
6244
6245 /*
6246 * display the items
6247 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006248 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006250 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 if (cols == 0)
6252 cols = 1;
6253 rows = (item_count + cols - 1) / cols;
6254 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006255 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 rows = item_count;
6257 for (row = 0; row < rows && !got_int; ++row)
6258 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006259 msg_putchar('\n'); // go to next line
6260 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 break;
6262 col = 0;
6263 for (i = row; i < item_count; i += rows)
6264 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006265 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 show_one_termcode(termcodes[items[i]].name,
6267 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006268 if (run == 2)
6269 col += INC2;
6270 else
6271 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 }
6273 out_flush();
6274 ui_breakcheck();
6275 }
6276 }
6277 vim_free(items);
6278}
6279
6280/*
6281 * Show one termcode entry.
6282 * Output goes into IObuff[]
6283 */
6284 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006285show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286{
6287 char_u *p;
6288 int len;
6289
6290 if (name[0] > '~')
6291 {
6292 IObuff[0] = ' ';
6293 IObuff[1] = ' ';
6294 IObuff[2] = ' ';
6295 IObuff[3] = ' ';
6296 }
6297 else
6298 {
6299 IObuff[0] = 't';
6300 IObuff[1] = '_';
6301 IObuff[2] = name[0];
6302 IObuff[3] = name[1];
6303 }
6304 IObuff[4] = ' ';
6305
6306 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6307 if (p[1] != 't')
6308 STRCPY(IObuff + 5, p);
6309 else
6310 IObuff[5] = NUL;
6311 len = (int)STRLEN(IObuff);
6312 do
6313 IObuff[len++] = ' ';
6314 while (len < 17);
6315 IObuff[len] = NUL;
6316 if (code == NULL)
6317 len += 4;
6318 else
6319 len += vim_strsize(code);
6320
6321 if (printit)
6322 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006323 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006325 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 else
6327 msg_outtrans(code);
6328 }
6329 return len;
6330}
6331
6332#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6333/*
6334 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6335 * termcap codes from the terminal itself.
6336 * We get them one by one to avoid a very long response string.
6337 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006338static int xt_index_in = 0;
6339static int xt_index_out = 0;
6340
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006342req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
6344 xt_index_out = 0;
6345 xt_index_in = 0;
6346 req_more_codes_from_term();
6347}
6348
6349 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006350req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006352 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 int old_idx = xt_index_out;
6354
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006355 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 if (exiting)
6357 return;
6358
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006359 // Send up to 10 more requests out than we received. Avoid sending too
6360 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6362 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006363 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006364
Bram Moolenaar1d97db32022-06-04 22:15:54 +01006365 MAY_WANT_TO_LOG_THIS;
Bram Moolenaarb255b902018-04-24 21:40:10 +02006366 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6367 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 out_str_nf((char_u *)buf);
6369 ++xt_index_out;
6370 }
6371
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006372 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 if (xt_index_out != old_idx)
6374 out_flush();
6375}
6376
6377/*
6378 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6379 * A "0" instead of the "1" indicates a code that isn't supported.
6380 * Both <name> and <string> are encoded in hex.
6381 * "code" points to the "0" or "1".
6382 */
6383 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006384got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
6386#define XT_LEN 100
6387 char_u name[3];
6388 char_u str[XT_LEN];
6389 int i;
6390 int j = 0;
6391 int c;
6392
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006393 // A '1' means the code is supported, a '0' means it isn't.
6394 // When half the length is > XT_LEN we can't use it.
6395 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6397 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006398 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 name[0] = hexhex2nr(code + 3);
6400 name[1] = hexhex2nr(code + 5);
6401 name[2] = NUL;
6402 for (i = 0; key_names[i] != NULL; ++i)
6403 {
6404 if (STRCMP(key_names[i], name) == 0)
6405 {
6406 xt_index_in = i;
6407 break;
6408 }
6409 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006410
Bram Moolenaarb255b902018-04-24 21:40:10 +02006411 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6412
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 if (key_names[i] != NULL)
6414 {
6415 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6416 str[j++] = c;
6417 str[j] = NUL;
6418 if (name[0] == 'C' && name[1] == 'o')
6419 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006420 // Color count is not a key code.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00006421 may_adjust_color_count(atoi((char *)str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 }
6423 else
6424 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006425 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 i = find_term_bykeys(str);
6427 if (i >= 0)
6428 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006429 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 }
6431 }
6432 }
6433
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006434 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 ++xt_index_in;
6436 req_more_codes_from_term();
6437}
6438
6439/*
6440 * Check if there are any unanswered requests and deal with them.
6441 * This is called before starting an external program or getting direct
6442 * keyboard input. We don't want responses to be send to that program or
6443 * handled as typed text.
6444 */
6445 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006446check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447{
6448 int c;
6449
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006450 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6452 return;
6453
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006454 // Vgetc() will check for and handle any response.
6455 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 ++no_mapping;
6457 ++allow_keys;
6458 for (;;)
6459 {
6460 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006461 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 break;
6463
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006464 // If a response is recognized it's replaced with K_IGNORE, must read
6465 // it from the input stream. If there is no K_IGNORE we can't do
6466 // anything, break here (there might be some responses further on, but
6467 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 if (c != K_SPECIAL && c != K_IGNORE)
6469 break;
6470 c = vgetc();
6471 if (c != K_IGNORE)
6472 {
6473 vungetc(c);
6474 break;
6475 }
6476 }
6477 --no_mapping;
6478 --allow_keys;
6479}
6480#endif
6481
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006482#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483static char ksme_str[20];
6484static char ksmr_str[20];
6485static char ksmd_str[20];
6486
6487/*
6488 * For Win32 console: update termcap codes for existing console attributes.
6489 */
6490 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006491update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492{
6493 struct builtin_term *p;
6494
6495 p = find_builtin_term(DEFAULT_TERM);
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006496 sprintf(ksme_str, "\033|%dm", attr);
6497 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
6498 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
6500 while (p->bt_string != NULL)
6501 {
6502 if (p->bt_entry == (int)KS_ME)
6503 p->bt_string = &ksme_str[0];
6504 else if (p->bt_entry == (int)KS_MR)
6505 p->bt_string = &ksmr_str[0];
6506 else if (p->bt_entry == (int)KS_MD)
6507 p->bt_string = &ksmd_str[0];
6508 ++p;
6509 }
6510}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006511
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006512# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006513# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006514
6515typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006516{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006517 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
6518 CMODE_RGB, // Use 24bit RGB colors using VTP.
6519 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
6520 CMODE_LAST,
6521} cmode_T;
6522
6523struct ks_tbl_S
6524{
6525 int code; // value of KS_
6526 char *vtp; // code in RGB mode
6527 char *vtp2; // code in 256color mode
6528 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006529};
6530
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006531static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006532{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006533 {(int)KS_ME, "\033|0m", "\033|0m", {""}}, // normal
6534 {(int)KS_MR, "\033|7m", "\033|7m", {""}}, // reverse
6535 {(int)KS_MD, "\033|1m", "\033|1m", {""}}, // bold
6536 {(int)KS_SO, "\033|91m", "\033|91m", {""}}, // standout: bright red text
6537 {(int)KS_SE, "\033|39m", "\033|39m", {""}}, // standout end: default color
6538 {(int)KS_CZH, "\033|3m", "\033|3m", {""}}, // italic
6539 {(int)KS_CZR, "\033|0m", "\033|0m", {""}}, // italic end
6540 {(int)KS_US, "\033|4m", "\033|4m", {""}}, // underscore
6541 {(int)KS_UE, "\033|24m", "\033|24m", {""}}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006542# ifdef TERMINFO
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006543 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm", {""}}, // set background color
6544 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm", {""}}, // set foreground color
6545 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR", {""}},
6546 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006547# else
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006548 {(int)KS_CAB, "\033|%db", "\033|4%dm", {""}}, // set background color
6549 {(int)KS_CAF, "\033|%df", "\033|3%dm", {""}}, // set foreground color
6550 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR", {""}},
6551 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV", {""}},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006552# endif
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01006553 {(int)KS_CCO, "256", "256", {""}}, // colors
6554 {(int)KS_NAME, NULL, NULL, {""}} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006555};
6556
6557 static struct builtin_term *
6558find_first_tcap(
6559 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006560 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006561{
6562 struct builtin_term *p;
6563
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006564 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006565 if (p->bt_entry == code)
6566 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006567 return NULL;
6568}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006569# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006570
6571/*
6572 * For Win32 console: replace the sequence immediately after termguicolors.
6573 */
6574 void
6575swap_tcap(void)
6576{
6577# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006578 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006579 static cmode_T curr_mode;
6580 struct ks_tbl_S *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006581 struct builtin_term *bt;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006582 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006583
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006584 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006585 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006586 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006587 {
6588 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006589 if (bt != NULL)
6590 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006591 // Preserve the original value.
6592 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
6593 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
6594 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006595
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006596 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006597 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006598 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006599 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006600 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006601 }
6602
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006603 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006604 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006605 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006606 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006607 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006608 mode = CMODE_INDEXED;
6609
6610 if (mode == curr_mode)
6611 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006612
6613 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006614 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006615 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6616 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006617 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006618 }
6619
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006620 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006621# endif
6622}
6623
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006625
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006626
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006627#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006628 || defined(PROTO)
6629static int cube_value[] = {
6630 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6631};
6632
6633static int grey_ramp[] = {
6634 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6635 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6636};
6637
LemonBoyb2b3acb2022-05-20 10:10:34 +01006638static const char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01006639// R G B
6640 { 0, 0, 0}, // black
6641 {224, 0, 0}, // dark red
6642 { 0, 224, 0}, // dark green
6643 {224, 224, 0}, // dark yellow / brown
6644 { 0, 0, 224}, // dark blue
6645 {224, 0, 224}, // dark magenta
6646 { 0, 224, 224}, // dark cyan
6647 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006648
LemonBoyd2a46622022-05-01 17:43:33 +01006649 {128, 128, 128}, // dark grey
6650 {255, 64, 64}, // light red
6651 { 64, 255, 64}, // light green
6652 {255, 255, 64}, // yellow
6653 { 64, 64, 255}, // light blue
6654 {255, 64, 255}, // light magenta
6655 { 64, 255, 255}, // light cyan
6656 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006657};
6658
LemonBoyd2a46622022-05-01 17:43:33 +01006659#if defined(MSWIN)
6660// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
6661static const char_u cterm_ansi_idx[] = {
6662 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
6663};
6664#endif
6665
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006666#define ANSI_INDEX_NONE 0
6667
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006668 void
LemonBoyb2b3acb2022-05-20 10:10:34 +01006669ansi_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
6670{
6671 if (nr < 16)
6672 {
6673 *r = ansi_table[nr][0];
6674 *g = ansi_table[nr][1];
6675 *b = ansi_table[nr][2];
6676 *ansi_idx = nr;
6677 }
6678 else
6679 {
6680 *r = 0;
6681 *g = 0;
6682 *b = 0;
6683 *ansi_idx = ANSI_INDEX_NONE;
6684 }
6685}
6686
6687 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006688cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006689{
6690 int idx;
6691
6692 if (nr < 16)
6693 {
LemonBoyd2a46622022-05-01 17:43:33 +01006694#if defined(MSWIN)
6695 idx = cterm_ansi_idx[nr];
6696#else
6697 idx = nr;
6698#endif
6699 *r = ansi_table[idx][0];
6700 *g = ansi_table[idx][1];
6701 *b = ansi_table[idx][2];
6702 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006703 }
6704 else if (nr < 232)
6705 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006706 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006707 idx = nr - 16;
6708 *r = cube_value[idx / 36 % 6];
6709 *g = cube_value[idx / 6 % 6];
6710 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006711 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006712 }
6713 else if (nr < 256)
6714 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006715 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006716 idx = nr - 232;
6717 *r = grey_ramp[idx];
6718 *g = grey_ramp[idx];
6719 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006720 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006721 }
6722 else
6723 {
6724 *r = 0;
6725 *g = 0;
6726 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006727 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006728 }
6729}
6730#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006731
Bram Moolenaarf4140482020-02-15 23:06:45 +01006732/*
6733 * Replace K_BS by <BS> and K_DEL by <DEL>
6734 */
6735 void
6736term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len)
6737{
6738 int i;
6739 int c;
6740
6741 for (i = ta_len; i < ta_len + len; ++i)
6742 {
6743 if (ta_buf[i] == CSI && len - i > 2)
6744 {
6745 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6746 if (c == K_DEL || c == K_KDEL || c == K_BS)
6747 {
6748 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
6749 (size_t)(len - i - 2));
6750 if (c == K_DEL || c == K_KDEL)
6751 ta_buf[i] = DEL;
6752 else
6753 ta_buf[i] = Ctrl_H;
6754 len -= 2;
6755 }
6756 }
6757 else if (ta_buf[i] == '\r')
6758 ta_buf[i] = '\n';
6759 if (has_mbyte)
6760 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6761 }
6762}