blob: 9cc480e6d02fbd3aaa49005ed77002af59db41d5 [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
53/*
54 * Here are the builtin termcap entries. They are not stored as complete
Bram Moolenaare60acc12011-05-10 16:41:25 +020055 * structures with all entries, as such a structure is too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 *
57 * The entries are compact, therefore they normally are included even when
58 * HAVE_TGETENT is defined. When HAVE_TGETENT is defined, the builtin entries
59 * can be accessed with "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
60 *
61 * Each termcap is a list of builtin_term structures. It always starts with
62 * KS_NAME, which separates the entries. See parse_builtin_tcap() for all
63 * details.
64 * bt_entry is either a KS_xxx code (>= 0), or a K_xxx code.
65 *
66 * Entries marked with "guessed" may be wrong.
67 */
68struct builtin_term
69{
70 int bt_entry;
71 char *bt_string;
72};
73
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010074// start of keys that are not directly used by Vim but can be mapped
Bram Moolenaar071d4272004-06-13 20:20:40 +000075#define BT_EXTRA_KEYS 0x101
76
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010077static void parse_builtin_tcap(char_u *s);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010078static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010080static void req_codes_from_term(void);
81static void req_more_codes_from_term(void);
82static void got_code_from_term(char_u *code, int len);
83static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010085static void del_termcode_idx(int idx);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020086static int find_term_bykeys(char_u *src);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010087static int term_is_builtin(char_u *name);
88static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
90#ifdef HAVE_TGETENT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010091static char *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
93/*
94 * Here is our own prototype for tgetstr(), any prototypes from the include
95 * files have been disabled by the define at the start of this file.
96 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010097char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
99# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100100 // Change this to "if 1" to debug what happens with termresponse.
Bram Moolenaar2951b772013-07-03 12:45:31 +0200101# if 0
102# define DEBUG_TERMRESPONSE
Bram Moolenaar952d9d82021-08-02 18:07:18 +0200103static void log_tr(const char *fmt, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
Bram Moolenaarb255b902018-04-24 21:40:10 +0200104# define LOG_TR(msg) log_tr msg
Bram Moolenaar2951b772013-07-03 12:45:31 +0200105# else
Bram Moolenaarb255b902018-04-24 21:40:10 +0200106# define LOG_TR(msg) do { /**/ } while (0)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200107# endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200108
Bram Moolenaarafd78262019-05-10 23:10:31 +0200109typedef enum {
110 STATUS_GET, // send request when switching to RAW mode
111 STATUS_SENT, // did send request, checking for response
112 STATUS_GOT, // received response
113 STATUS_FAIL // timed out
114} request_progress_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200115
Bram Moolenaarafd78262019-05-10 23:10:31 +0200116typedef struct {
117 request_progress_T tr_progress;
118 time_t tr_start; // when request was sent, -1 for never
119} termrequest_T;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200120
Bram Moolenaarafd78262019-05-10 23:10:31 +0200121# define TERMREQUEST_INIT {STATUS_GET, -1}
122
123// Request Terminal Version status:
124static termrequest_T crv_status = TERMREQUEST_INIT;
125
126// Request Cursor position report:
127static termrequest_T u7_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200128
Bram Moolenaara45551a2020-06-09 15:57:37 +0200129// Request xterm compatibility check:
130static termrequest_T xcc_status = TERMREQUEST_INIT;
131
Bram Moolenaar9377df32017-10-15 13:22:01 +0200132# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200133// Request foreground color report:
134static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200135static int fg_r = 0;
136static int fg_g = 0;
137static int fg_b = 0;
138static int bg_r = 255;
139static int bg_g = 255;
140static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200141# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200142
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100143// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200144static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200145
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100146// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200147static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200148
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100149// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200150static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100151
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100152// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200153static termrequest_T winpos_status = TERMREQUEST_INIT;
154
155static termrequest_T *all_termrequests[] = {
156 &crv_status,
157 &u7_status,
Bram Moolenaara45551a2020-06-09 15:57:37 +0200158 &xcc_status,
Bram Moolenaarafd78262019-05-10 23:10:31 +0200159# ifdef FEAT_TERMINAL
160 &rfg_status,
161# endif
162 &rbg_status,
163 &rbm_status,
164 &rcs_status,
165 &winpos_status,
166 NULL
167};
Bram Moolenaar366f0bd2022-04-17 19:20:33 +0100168
169// The t_8u code may default to a value but get reset when the term response is
170// received. To avoid redrawing too often, only redraw when t_8u is not reset
171// and it was supposed to be written.
172// FALSE -> don't output t_8u yet
173// MAYBE -> tried outputing t_8u while FALSE
174// OK -> can write t_8u
175int write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176# endif
177
178/*
179 * Don't declare these variables if termcap.h contains them.
180 * Autoconf checks if these variables should be declared extern (not all
181 * systems have them).
182 * Some versions define ospeed to be speed_t, but that is incompatible with
183 * BSD, where ospeed is short and speed_t is long.
184 */
185# ifndef HAVE_OSPEED
186# ifdef OSPEED_EXTERN
187extern short ospeed;
188# else
189short ospeed;
190# endif
191# endif
192# ifndef HAVE_UP_BC_PC
193# ifdef UP_BC_PC_EXTERN
194extern char *UP, *BC, PC;
195# else
196char *UP, *BC, PC;
197# endif
198# endif
199
200# define TGETSTR(s, p) vim_tgetstr((s), (p))
201# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100202static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100203#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100205static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100207#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000208static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100209#endif
210
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200211#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100212// When the cursor shape was detected these values are used:
213// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200214static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200215
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100216// The blink flag from the style response may be inverted from the actual
217// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200218static int initial_cursor_shape_blink = FALSE;
219
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100220// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200221static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200222#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200223
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000224static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225{
226
227#if defined(FEAT_GUI)
228/*
229 * GUI pseudo term-cap.
230 */
231 {(int)KS_NAME, "gui"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000232 {(int)KS_CE, "\033|$"},
233 {(int)KS_AL, "\033|i"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000235 {(int)KS_CAL, "\033|%p1%dI"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000237 {(int)KS_CAL, "\033|%dI"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000239 {(int)KS_DL, "\033|d"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000241 {(int)KS_CDL, "\033|%p1%dD"},
242 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
243 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000245 {(int)KS_CDL, "\033|%dD"},
246 {(int)KS_CS, "\033|%d;%dR"},
247 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000249 {(int)KS_CL, "\033|C"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100250 // attributes switched on with 'h', off with * 'H'
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000251 {(int)KS_ME, "\033|31H"}, // HL_ALL
252 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
253 {(int)KS_MD, "\033|2h"}, // HL_BOLD
254 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
255 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
256 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
257 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
258 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
259 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
260 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
261 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
262 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
263 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
264 {(int)KS_VB, "\033|f"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 {(int)KS_MS, "y"},
266 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100267 {(int)KS_XN, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100268 {(int)KS_LE, "\b"}, // cursor-left = BS
269 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000271 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000273 {(int)KS_CM, "\033|%d;%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100275 // there are no key sequences here, the GUI sequences are recognized
276 // in check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#endif
278
279#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
281# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
282/*
283 * Amiga console window, default for Amiga
284 */
285 {(int)KS_NAME, "amiga"},
286 {(int)KS_CE, "\033[K"},
287 {(int)KS_CD, "\033[J"},
288 {(int)KS_AL, "\033[L"},
289# ifdef TERMINFO
290 {(int)KS_CAL, "\033[%p1%dL"},
291# else
292 {(int)KS_CAL, "\033[%dL"},
293# endif
294 {(int)KS_DL, "\033[M"},
295# ifdef TERMINFO
296 {(int)KS_CDL, "\033[%p1%dM"},
297# else
298 {(int)KS_CDL, "\033[%dM"},
299# endif
300 {(int)KS_CL, "\014"},
301 {(int)KS_VI, "\033[0 p"},
302 {(int)KS_VE, "\033[1 p"},
303 {(int)KS_ME, "\033[0m"},
304 {(int)KS_MR, "\033[7m"},
305 {(int)KS_MD, "\033[1m"},
306 {(int)KS_SE, "\033[0m"},
307 {(int)KS_SO, "\033[33m"},
308 {(int)KS_US, "\033[4m"},
309 {(int)KS_UE, "\033[0m"},
310 {(int)KS_CZH, "\033[3m"},
311 {(int)KS_CZR, "\033[0m"},
Bram Moolenaar2d718262020-11-21 12:44:56 +0100312#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100313 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100315 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
316 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100318 {(int)KS_CAB, "\033[4%dm"}, // set background color
319 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100321 {(int)KS_OP, "\033[m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#endif
323 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100324 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 {(int)KS_LE, "\b"},
326# ifdef TERMINFO
327 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
328# else
329 {(int)KS_CM, "\033[%i%d;%dH"},
330# endif
331#if defined(__MORPHOS__)
332 {(int)KS_SR, "\033M"},
333#endif
334# ifdef TERMINFO
335 {(int)KS_CRI, "\033[%p1%dC"},
336# else
337 {(int)KS_CRI, "\033[%dC"},
338# endif
339 {K_UP, "\233A"},
340 {K_DOWN, "\233B"},
341 {K_LEFT, "\233D"},
342 {K_RIGHT, "\233C"},
343 {K_S_UP, "\233T"},
344 {K_S_DOWN, "\233S"},
345 {K_S_LEFT, "\233 A"},
346 {K_S_RIGHT, "\233 @"},
347 {K_S_TAB, "\233Z"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100348 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {K_F2, "\233\061~"},
350 {K_F3, "\233\062~"},
351 {K_F4, "\233\063~"},
352 {K_F5, "\233\064~"},
353 {K_F6, "\233\065~"},
354 {K_F7, "\233\066~"},
355 {K_F8, "\233\067~"},
356 {K_F9, "\233\070~"},
357 {K_F10, "\233\071~"},
358 {K_S_F1, "\233\061\060~"},
359 {K_S_F2, "\233\061\061~"},
360 {K_S_F3, "\233\061\062~"},
361 {K_S_F4, "\233\061\063~"},
362 {K_S_F5, "\233\061\064~"},
363 {K_S_F6, "\233\061\065~"},
364 {K_S_F7, "\233\061\066~"},
365 {K_S_F8, "\233\061\067~"},
366 {K_S_F9, "\233\061\070~"},
367 {K_S_F10, "\233\061\071~"},
368 {K_HELP, "\233?~"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100369 {K_INS, "\233\064\060~"}, // 101 key keyboard
370 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
371 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
372 {K_HOME, "\233\064\064~"}, // 101 key keyboard
373 {K_END, "\233\064\065~"}, // 101 key keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375 {BT_EXTRA_KEYS, ""},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100376 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
377 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
378 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379# endif
380
Bram Moolenaar041c7102020-05-30 18:14:57 +0200381# ifdef ALL_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382/*
Bram Moolenaar041c7102020-05-30 18:14:57 +0200383 * almost standard ANSI terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 {(int)KS_CE, "\033[K"},
386 {(int)KS_CD, "\033[J"},
387 {(int)KS_AL, "\033[L"},
388# ifdef TERMINFO
389 {(int)KS_CAL, "\033[%p1%dL"},
390# else
391 {(int)KS_CAL, "\033[%dL"},
392# endif
393 {(int)KS_DL, "\033[M"},
394# ifdef TERMINFO
395 {(int)KS_CDL, "\033[%p1%dM"},
396# else
397 {(int)KS_CDL, "\033[%dM"},
398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {(int)KS_CL, "\033[H\033[2J"},
400#ifdef notyet
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100401 {(int)KS_VI, "[VI]"}, // cursor invisible, VT320: CSI ? 25 l
402 {(int)KS_VE, "[VE]"}, // cursor visible, VT320: CSI ? 25 h
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100404 {(int)KS_ME, "\033[m"}, // normal mode
405 {(int)KS_MR, "\033[7m"}, // reverse
406 {(int)KS_MD, "\033[1m"}, // bold
407 {(int)KS_SO, "\033[31m"}, // standout mode: red
408 {(int)KS_SE, "\033[m"}, // standout end
409 {(int)KS_CZH, "\033[35m"}, // italic: purple
410 {(int)KS_CZR, "\033[m"}, // italic end
411 {(int)KS_US, "\033[4m"}, // underscore mode
412 {(int)KS_UE, "\033[m"}, // underscore end
413 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100415 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
416 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100418 {(int)KS_CAB, "\033[4%dm"}, // set background color
419 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100421 {(int)KS_OP, "\033[m"}, // reset colors
422 {(int)KS_MS, "y"}, // safe to move cur in reverse mode
423 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 {(int)KS_LE, "\b"},
425# ifdef TERMINFO
426 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
427# else
428 {(int)KS_CM, "\033[%i%d;%dH"},
429# endif
430 {(int)KS_SR, "\033M"},
431# ifdef TERMINFO
432 {(int)KS_CRI, "\033[%p1%dC"},
433# else
434 {(int)KS_CRI, "\033[%dC"},
435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436
437 {K_UP, "\033[A"},
438 {K_DOWN, "\033[B"},
439 {K_LEFT, "\033[D"},
440 {K_RIGHT, "\033[C"},
441# endif
442
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200443# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444/*
445 * standard ANSI terminal, default for unix
446 */
447 {(int)KS_NAME, "ansi"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000448 {(int)KS_CE, "\033[K"},
449 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000451 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000453 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000455 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000457 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000459 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000461 {(int)KS_CL, "\033[H\033[2J"},
462 {(int)KS_ME, "\033[0m"},
463 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100465 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 {(int)KS_LE, "\b"},
467# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000468 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000470 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471# endif
472# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000473 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000475 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476# endif
477# endif
478
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200479# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480/*
481 * These codes are valid when nansi.sys or equivalent has been installed.
482 * Function keys on a PC are preceded with a NUL. These are converted into
483 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
484 * CTRL-arrow is used instead of SHIFT-arrow.
485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 {(int)KS_NAME, "pcansi"},
487 {(int)KS_DL, "\033[M"},
488 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 {(int)KS_CE, "\033[K"},
490 {(int)KS_CL, "\033[2J"},
491 {(int)KS_ME, "\033[0m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100492 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
493 {(int)KS_MD, "\033[1m"}, // bold: white text
494 {(int)KS_SE, "\033[0m"}, // standout end
495 {(int)KS_SO, "\033[31m"}, // standout: white on blue
496 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
497 {(int)KS_CZR, "\033[0m"}, // italic mode end
498 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
499 {(int)KS_UE, "\033[0m"}, // underscore mode end
500 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100502 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
503 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100505 {(int)KS_CAB, "\033[4%dm"}, // set background color
506 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100508 {(int)KS_OP, "\033[0m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100510 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 {(int)KS_LE, "\b"},
512# ifdef TERMINFO
513 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
514# else
515 {(int)KS_CM, "\033[%i%d;%dH"},
516# endif
517# ifdef TERMINFO
518 {(int)KS_CRI, "\033[%p1%dC"},
519# else
520 {(int)KS_CRI, "\033[%dC"},
521# endif
522 {K_UP, "\316H"},
523 {K_DOWN, "\316P"},
524 {K_LEFT, "\316K"},
525 {K_RIGHT, "\316M"},
526 {K_S_LEFT, "\316s"},
527 {K_S_RIGHT, "\316t"},
528 {K_F1, "\316;"},
529 {K_F2, "\316<"},
530 {K_F3, "\316="},
531 {K_F4, "\316>"},
532 {K_F5, "\316?"},
533 {K_F6, "\316@"},
534 {K_F7, "\316A"},
535 {K_F8, "\316B"},
536 {K_F9, "\316C"},
537 {K_F10, "\316D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100538 {K_F11, "\316\205"}, // guessed
539 {K_F12, "\316\206"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {K_S_F1, "\316T"},
541 {K_S_F2, "\316U"},
542 {K_S_F3, "\316V"},
543 {K_S_F4, "\316W"},
544 {K_S_F5, "\316X"},
545 {K_S_F6, "\316Y"},
546 {K_S_F7, "\316Z"},
547 {K_S_F8, "\316["},
548 {K_S_F9, "\316\\"},
549 {K_S_F10, "\316]"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100550 {K_S_F11, "\316\207"}, // guessed
551 {K_S_F12, "\316\210"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 {K_INS, "\316R"},
553 {K_DEL, "\316S"},
554 {K_HOME, "\316G"},
555 {K_END, "\316O"},
556 {K_PAGEDOWN, "\316Q"},
557 {K_PAGEUP, "\316I"},
558# endif
559
Bram Moolenaar4f974752019-02-17 17:44:42 +0100560# if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561/*
562 * These codes are valid for the Win32 Console . The entries that start with
563 * ESC | are translated into console calls in os_win32.c. The function keys
564 * are also translated in os_win32.c.
565 */
566 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100567 {(int)KS_CE, "\033|K"}, // clear to end of line
568 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100570 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100572 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100574 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100576 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
577 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100579 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
580 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100582 {(int)KS_CL, "\033|J"}, // clear screen
583 {(int)KS_CD, "\033|j"}, // clear to end of display
584 {(int)KS_VI, "\033|v"}, // cursor invisible
585 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
Bram Moolenaar6982f422019-02-16 16:48:01 +0100587 {(int)KS_ME, "\033|0m"}, // normal
588 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
589 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100591 {(int)KS_SO, "\033|31m"}, // standout: white on blue
592 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100594 {(int)KS_SO, "\033|F"}, // standout: high intensity
595 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100597 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
598 {(int)KS_CZR, "\033|0m"}, // italic end
599 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
600 {(int)KS_UE, "\033|0m"}, // underscore end
601 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100603 {(int)KS_CAB, "\033|%p1%db"}, // set background color
604 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100606 {(int)KS_CAB, "\033|%db"}, // set background color
607 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608# endif
609
Bram Moolenaar6982f422019-02-16 16:48:01 +0100610 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100612 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 {(int)KS_LE, "\b"},
614# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100615 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100617 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100619 {(int)KS_VB, "\033|B"}, // visual bell
620 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
621 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100623 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100625 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100627# ifdef FEAT_TERMGUICOLORS
628 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
629 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632 {K_UP, "\316H"},
633 {K_DOWN, "\316P"},
634 {K_LEFT, "\316K"},
635 {K_RIGHT, "\316M"},
636 {K_S_UP, "\316\304"},
637 {K_S_DOWN, "\316\317"},
638 {K_S_LEFT, "\316\311"},
639 {K_C_LEFT, "\316s"},
640 {K_S_RIGHT, "\316\313"},
641 {K_C_RIGHT, "\316t"},
642 {K_S_TAB, "\316\017"},
643 {K_F1, "\316;"},
644 {K_F2, "\316<"},
645 {K_F3, "\316="},
646 {K_F4, "\316>"},
647 {K_F5, "\316?"},
648 {K_F6, "\316@"},
649 {K_F7, "\316A"},
650 {K_F8, "\316B"},
651 {K_F9, "\316C"},
652 {K_F10, "\316D"},
653 {K_F11, "\316\205"},
654 {K_F12, "\316\206"},
655 {K_S_F1, "\316T"},
656 {K_S_F2, "\316U"},
657 {K_S_F3, "\316V"},
658 {K_S_F4, "\316W"},
659 {K_S_F5, "\316X"},
660 {K_S_F6, "\316Y"},
661 {K_S_F7, "\316Z"},
662 {K_S_F8, "\316["},
663 {K_S_F9, "\316\\"},
664 {K_S_F10, "\316]"},
665 {K_S_F11, "\316\207"},
666 {K_S_F12, "\316\210"},
667 {K_INS, "\316R"},
668 {K_DEL, "\316S"},
669 {K_HOME, "\316G"},
670 {K_S_HOME, "\316\302"},
671 {K_C_HOME, "\316w"},
672 {K_END, "\316O"},
673 {K_S_END, "\316\315"},
674 {K_C_END, "\316u"},
675 {K_PAGEDOWN, "\316Q"},
676 {K_PAGEUP, "\316I"},
677 {K_KPLUS, "\316N"},
678 {K_KMINUS, "\316J"},
679 {K_KMULTIPLY, "\316\067"},
680 {K_K0, "\316\332"},
681 {K_K1, "\316\336"},
682 {K_K2, "\316\342"},
683 {K_K3, "\316\346"},
684 {K_K4, "\316\352"},
685 {K_K5, "\316\356"},
686 {K_K6, "\316\362"},
687 {K_K7, "\316\366"},
688 {K_K8, "\316\372"},
689 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100690 {K_BS, "\316x"},
Christian Brabandtdfbdadc2022-05-05 20:46:47 +0100691 {K_S_BS, "\316y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692# endif
693
694# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
695/*
696 * VT320 is working as an ANSI terminal compatible DEC terminal.
697 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 * TODO:- rewrite ESC[ codes to CSI
699 * - keyboard languages (CSI ? 26 n)
700 */
701 {(int)KS_NAME, "vt320"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000702 {(int)KS_CE, "\033[K"},
703 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000705 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000707 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000709 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000711 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000713 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000715 {(int)KS_CL, "\033[H\033[2J"},
716 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100717 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000718 {(int)KS_ME, "\033[0m"},
719 {(int)KS_MR, "\033[7m"},
720 {(int)KS_MD, "\033[1m"}, // bold mode
721 {(int)KS_SE, "\033[22m"},// normal mode
722 {(int)KS_UE, "\033[24m"},// exit underscore mode
723 {(int)KS_US, "\033[4m"}, // underscore mode
724 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
725 {(int)KS_CZR, "\033[0m"}, // italic mode end
726 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
727 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
728 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
729 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {(int)KS_MS, "y"},
731 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100732 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 {(int)KS_LE, "\b"},
734# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000735 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000737 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738# endif
739# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000740 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000742 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000744 {K_UP, "\033[A"},
745 {K_DOWN, "\033[B"},
746 {K_RIGHT, "\033[C"},
747 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200748 // Note: cursor key sequences for application cursor mode are omitted,
749 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000750 {K_F1, "\033[11~"},
751 {K_F2, "\033[12~"},
752 {K_F3, "\033[13~"},
753 {K_F4, "\033[14~"},
754 {K_F5, "\033[15~"},
755 {K_F6, "\033[17~"},
756 {K_F7, "\033[18~"},
757 {K_F8, "\033[19~"},
758 {K_F9, "\033[20~"},
759 {K_F10, "\033[21~"},
760 {K_F11, "\033[23~"},
761 {K_F12, "\033[24~"},
762 {K_F13, "\033[25~"},
763 {K_F14, "\033[26~"},
764 {K_F15, "\033[28~"}, // Help
765 {K_F16, "\033[29~"}, // Select
766 {K_F17, "\033[31~"},
767 {K_F18, "\033[32~"},
768 {K_F19, "\033[33~"},
769 {K_F20, "\033[34~"},
770 {K_INS, "\033[2~"},
771 {K_DEL, "\033[3~"},
772 {K_HOME, "\033[1~"},
773 {K_END, "\033[4~"},
774 {K_PAGEUP, "\033[5~"},
775 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200776 // These sequences starting with <Esc> O may interfere with what the user
777 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000778 {K_KPLUS, "\033Ok"}, // keypad plus
779 {K_KMINUS, "\033Om"}, // keypad minus
780 {K_KDIVIDE, "\033Oo"}, // keypad /
781 {K_KMULTIPLY, "\033Oj"}, // keypad *
782 {K_KENTER, "\033OM"}, // keypad Enter
783 {K_K0, "\033Op"}, // keypad 0
784 {K_K1, "\033Oq"}, // keypad 1
785 {K_K2, "\033Or"}, // keypad 2
786 {K_K3, "\033Os"}, // keypad 3
787 {K_K4, "\033Ot"}, // keypad 4
788 {K_K5, "\033Ou"}, // keypad 5
789 {K_K6, "\033Ov"}, // keypad 6
790 {K_K7, "\033Ow"}, // keypad 7
791 {K_K8, "\033Ox"}, // keypad 8
792 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100793 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794# endif
795
Bram Moolenaare3f915d2020-07-14 23:02:44 +0200796# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797/*
798 * Ordinary vt52
799 */
800 {(int)KS_NAME, "vt52"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000801 {(int)KS_CE, "\033K"},
802 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100803# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000804 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100805# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000806 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100807# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000809 {(int)KS_SR, "\033I"},
810 {(int)KS_AL, "\033L"},
811 {(int)KS_DL, "\033M"},
812 {K_UP, "\033A"},
813 {K_DOWN, "\033B"},
814 {K_LEFT, "\033D"},
815 {K_RIGHT, "\033C"},
816 {K_F1, "\033P"},
817 {K_F2, "\033Q"},
818 {K_F3, "\033R"},
819 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821# endif
822
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200823# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200824 {(int)KS_NAME, "xterm"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000825 {(int)KS_CE, "\033[K"},
826 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000828 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000830 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000832 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000834 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000836 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837# endif
838# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000839 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000841 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000843 {(int)KS_CL, "\033[H\033[2J"},
844 {(int)KS_CD, "\033[J"},
845 {(int)KS_ME, "\033[m"},
846 {(int)KS_MR, "\033[7m"},
847 {(int)KS_MD, "\033[1m"},
848 {(int)KS_UE, "\033[m"},
849 {(int)KS_US, "\033[4m"},
850 {(int)KS_STE, "\033[29m"},
851 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {(int)KS_MS, "y"},
853 {(int)KS_UT, "y"},
854 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000855 {(int)KS_VI, "\033[?25l"},
856 {(int)KS_VE, "\033[?25h"},
857 {(int)KS_VS, "\033[?12h"},
858 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200859# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000860 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200861# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000862 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200863# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000864 {(int)KS_CRC, "\033[?12$p"},
865 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000867 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000869 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000871 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000873 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000875 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000877 {(int)KS_KS, "\033[?1h\033="},
878 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000880 {(int)KS_TI, "\0337\033[?47h"},
881 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000883 {(int)KS_CTI, "\033[>4;2m"},
884 {(int)KS_CTE, "\033[>4;m"},
885 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000887 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000889 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200890 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000892 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
893 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
894 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000896 {(int)KS_CWS, "\033[8;%d;%dt"},
897 {(int)KS_CWP, "\033[3;%d;%dt"},
898 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000900 {(int)KS_CRV, "\033[>c"},
901 {(int)KS_RFG, "\033]10;?\007"},
902 {(int)KS_RBG, "\033]11;?\007"},
903 {(int)KS_U7, "\033[6n"},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200904# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100905 // These are printf strings, not terminal codes.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000906 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
907 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
908 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200909# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000910 {(int)KS_CAU, "\033[58;5;%dm"},
911 {(int)KS_CBE, "\033[?2004h"},
912 {(int)KS_CBD, "\033[?2004l"},
913 {(int)KS_CST, "\033[22;2t"},
914 {(int)KS_CRT, "\033[23;2t"},
915 {(int)KS_SSI, "\033[22;1t"},
916 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100917# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000918 {(int)KS_FD, "\033[?1004l"},
919 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100920# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000921
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000922 {K_UP, "\033O*A"},
923 {K_DOWN, "\033O*B"},
924 {K_RIGHT, "\033O*C"},
925 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100926 // An extra set of cursor keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000927 {K_XUP, "\033[@;*A"},
928 {K_XDOWN, "\033[@;*B"},
929 {K_XRIGHT, "\033[@;*C"},
930 {K_XLEFT, "\033[@;*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100931 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000932 {K_XF1, "\033O*P"},
933 {K_XF2, "\033O*Q"},
934 {K_XF3, "\033O*R"},
935 {K_XF4, "\033O*S"},
936 {K_F1, "\033[11;*~"},
937 {K_F2, "\033[12;*~"},
938 {K_F3, "\033[13;*~"},
939 {K_F4, "\033[14;*~"},
940 {K_F5, "\033[15;*~"},
941 {K_F6, "\033[17;*~"},
942 {K_F7, "\033[18;*~"},
943 {K_F8, "\033[19;*~"},
944 {K_F9, "\033[20;*~"},
945 {K_F10, "\033[21;*~"},
946 {K_F11, "\033[23;*~"},
947 {K_F12, "\033[24;*~"},
948 {K_S_TAB, "\033[Z"},
949 {K_HELP, "\033[28;*~"},
950 {K_UNDO, "\033[26;*~"},
951 {K_INS, "\033[2;*~"},
952 {K_HOME, "\033[1;*H"},
953 // {K_S_HOME, "\033O2H"},
954 // {K_C_HOME, "\033O5H"},
955 {K_KHOME, "\033[1;*~"},
956 {K_XHOME, "\033O*H"}, // other Home
957 {K_ZHOME, "\033[7;*~"}, // other Home
958 {K_END, "\033[1;*F"},
959 // {K_S_END, "\033O2F"},
960 // {K_C_END, "\033O5F"},
961 {K_KEND, "\033[4;*~"},
962 {K_XEND, "\033O*F"}, // other End
963 {K_ZEND, "\033[8;*~"},
964 {K_PAGEUP, "\033[5;*~"},
965 {K_PAGEDOWN, "\033[6;*~"},
966 {K_KPLUS, "\033O*k"}, // keypad plus
967 {K_KMINUS, "\033O*m"}, // keypad minus
968 {K_KDIVIDE, "\033O*o"}, // keypad /
969 {K_KMULTIPLY, "\033O*j"}, // keypad *
970 {K_KENTER, "\033O*M"}, // keypad Enter
971 {K_KPOINT, "\033O*n"}, // keypad .
972 {K_K0, "\033O*p"}, // keypad 0
973 {K_K1, "\033O*q"}, // keypad 1
974 {K_K2, "\033O*r"}, // keypad 2
975 {K_K3, "\033O*s"}, // keypad 3
976 {K_K4, "\033O*t"}, // keypad 4
977 {K_K5, "\033O*u"}, // keypad 5
978 {K_K6, "\033O*v"}, // keypad 6
979 {K_K7, "\033O*w"}, // keypad 7
980 {K_K8, "\033O*x"}, // keypad 8
981 {K_K9, "\033O*y"}, // keypad 9
982 {K_KDEL, "\033[3;*~"}, // keypad Del
983 {K_PS, "\033[200~"}, // paste start
984 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000987 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
988 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100989 // F14 and F15 are missing, because they send the same codes as the undo
990 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000991 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
992 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
993 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
994 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
995 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000996
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000997 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
998 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
999 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
1000 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
1001 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
1002 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
1003 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
1004 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
1005 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
1006 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001007
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001008 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
1009 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
1010 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
1011 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
1012 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
1013 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
1014 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015# endif
1016
1017# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1018/*
1019 * iris-ansi for Silicon Graphics machines.
1020 */
1021 {(int)KS_NAME, "iris-ansi"},
1022 {(int)KS_CE, "\033[K"},
1023 {(int)KS_CD, "\033[J"},
1024 {(int)KS_AL, "\033[L"},
1025# ifdef TERMINFO
1026 {(int)KS_CAL, "\033[%p1%dL"},
1027# else
1028 {(int)KS_CAL, "\033[%dL"},
1029# endif
1030 {(int)KS_DL, "\033[M"},
1031# ifdef TERMINFO
1032 {(int)KS_CDL, "\033[%p1%dM"},
1033# else
1034 {(int)KS_CDL, "\033[%dM"},
1035# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001036#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037# ifdef TERMINFO
1038 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1039# else
1040 {(int)KS_CS, "\033[%i%d;%dr"},
1041# endif
1042#endif
1043 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001044 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
1045 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {(int)KS_TI, "\033[=6h"},
1047 {(int)KS_TE, "\033[=6l"},
1048 {(int)KS_SE, "\033[21;27m"},
1049 {(int)KS_SO, "\033[1;7m"},
1050 {(int)KS_ME, "\033[m"},
1051 {(int)KS_MR, "\033[7m"},
1052 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001053 {(int)KS_CCO, "8"}, // allow 8 colors
1054 {(int)KS_CZH, "\033[3m"}, // italic mode on
1055 {(int)KS_CZR, "\033[23m"}, // italic mode off
1056 {(int)KS_US, "\033[4m"}, // underline on
1057 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001059 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
1060 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
1061 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
1062 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001064 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
1065 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
1066 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
1067 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001069 {(int)KS_MS, "y"}, // guessed
1070 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 {(int)KS_LE, "\b"},
1072# ifdef TERMINFO
1073 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1074# else
1075 {(int)KS_CM, "\033[%i%d;%dH"},
1076# endif
1077 {(int)KS_SR, "\033M"},
1078# ifdef TERMINFO
1079 {(int)KS_CRI, "\033[%p1%dC"},
1080# else
1081 {(int)KS_CRI, "\033[%dC"},
1082# endif
1083 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001084 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001086 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087# ifdef TERMINFO
1088 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1089 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1090# else
1091 {(int)KS_CWS, "\033[203;%d;%d/y"},
1092 {(int)KS_CWP, "\033[205;%d;%d/y"},
1093# endif
1094 {K_UP, "\033[A"},
1095 {K_DOWN, "\033[B"},
1096 {K_LEFT, "\033[D"},
1097 {K_RIGHT, "\033[C"},
1098 {K_S_UP, "\033[161q"},
1099 {K_S_DOWN, "\033[164q"},
1100 {K_S_LEFT, "\033[158q"},
1101 {K_S_RIGHT, "\033[167q"},
1102 {K_F1, "\033[001q"},
1103 {K_F2, "\033[002q"},
1104 {K_F3, "\033[003q"},
1105 {K_F4, "\033[004q"},
1106 {K_F5, "\033[005q"},
1107 {K_F6, "\033[006q"},
1108 {K_F7, "\033[007q"},
1109 {K_F8, "\033[008q"},
1110 {K_F9, "\033[009q"},
1111 {K_F10, "\033[010q"},
1112 {K_F11, "\033[011q"},
1113 {K_F12, "\033[012q"},
1114 {K_S_F1, "\033[013q"},
1115 {K_S_F2, "\033[014q"},
1116 {K_S_F3, "\033[015q"},
1117 {K_S_F4, "\033[016q"},
1118 {K_S_F5, "\033[017q"},
1119 {K_S_F6, "\033[018q"},
1120 {K_S_F7, "\033[019q"},
1121 {K_S_F8, "\033[020q"},
1122 {K_S_F9, "\033[021q"},
1123 {K_S_F10, "\033[022q"},
1124 {K_S_F11, "\033[023q"},
1125 {K_S_F12, "\033[024q"},
1126 {K_INS, "\033[139q"},
1127 {K_HOME, "\033[H"},
1128 {K_END, "\033[146q"},
1129 {K_PAGEUP, "\033[150q"},
1130 {K_PAGEDOWN, "\033[154q"},
1131# endif
1132
1133# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1134/*
1135 * for debugging
1136 */
1137 {(int)KS_NAME, "debug"},
1138 {(int)KS_CE, "[CE]"},
1139 {(int)KS_CD, "[CD]"},
1140 {(int)KS_AL, "[AL]"},
1141# ifdef TERMINFO
1142 {(int)KS_CAL, "[CAL%p1%d]"},
1143# else
1144 {(int)KS_CAL, "[CAL%d]"},
1145# endif
1146 {(int)KS_DL, "[DL]"},
1147# ifdef TERMINFO
1148 {(int)KS_CDL, "[CDL%p1%d]"},
1149# else
1150 {(int)KS_CDL, "[CDL%d]"},
1151# endif
1152# ifdef TERMINFO
1153 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1154# else
1155 {(int)KS_CS, "[%dCS%d]"},
1156# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001157# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001159# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161# endif
1162# ifdef TERMINFO
1163 {(int)KS_CAB, "[CAB%p1%d]"},
1164 {(int)KS_CAF, "[CAF%p1%d]"},
1165 {(int)KS_CSB, "[CSB%p1%d]"},
1166 {(int)KS_CSF, "[CSF%p1%d]"},
1167# else
1168 {(int)KS_CAB, "[CAB%d]"},
1169 {(int)KS_CAF, "[CAF%d]"},
1170 {(int)KS_CSB, "[CSB%d]"},
1171 {(int)KS_CSF, "[CSF%d]"},
1172# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001173 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {(int)KS_OP, "[OP]"},
1175 {(int)KS_LE, "[LE]"},
1176 {(int)KS_CL, "[CL]"},
1177 {(int)KS_VI, "[VI]"},
1178 {(int)KS_VE, "[VE]"},
1179 {(int)KS_VS, "[VS]"},
1180 {(int)KS_ME, "[ME]"},
1181 {(int)KS_MR, "[MR]"},
1182 {(int)KS_MB, "[MB]"},
1183 {(int)KS_MD, "[MD]"},
1184 {(int)KS_SE, "[SE]"},
1185 {(int)KS_SO, "[SO]"},
1186 {(int)KS_UE, "[UE]"},
1187 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001188 {(int)KS_UCE, "[UCE]"},
1189 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001190 {(int)KS_STE, "[STE]"},
1191 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {(int)KS_MS, "[MS]"},
1193 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001194 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195# ifdef TERMINFO
1196 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1197# else
1198 {(int)KS_CM, "[%dCM%d]"},
1199# endif
1200 {(int)KS_SR, "[SR]"},
1201# ifdef TERMINFO
1202 {(int)KS_CRI, "[CRI%p1%d]"},
1203# else
1204 {(int)KS_CRI, "[CRI%d]"},
1205# endif
1206 {(int)KS_VB, "[VB]"},
1207 {(int)KS_KS, "[KS]"},
1208 {(int)KS_KE, "[KE]"},
1209 {(int)KS_TI, "[TI]"},
1210 {(int)KS_TE, "[TE]"},
1211 {(int)KS_CIS, "[CIS]"},
1212 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001213 {(int)KS_CSC, "[CSC]"},
1214 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 {(int)KS_TS, "[TS]"},
1216 {(int)KS_FS, "[FS]"},
1217# ifdef TERMINFO
1218 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1219 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1220# else
1221 {(int)KS_CWS, "[%dCWS%d]"},
1222 {(int)KS_CWP, "[%dCWP%d]"},
1223# endif
1224 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001225 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001226 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001227 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {K_UP, "[KU]"},
1229 {K_DOWN, "[KD]"},
1230 {K_LEFT, "[KL]"},
1231 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001232 {K_XUP, "[xKU]"},
1233 {K_XDOWN, "[xKD]"},
1234 {K_XLEFT, "[xKL]"},
1235 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 {K_S_UP, "[S-KU]"},
1237 {K_S_DOWN, "[S-KD]"},
1238 {K_S_LEFT, "[S-KL]"},
1239 {K_C_LEFT, "[C-KL]"},
1240 {K_S_RIGHT, "[S-KR]"},
1241 {K_C_RIGHT, "[C-KR]"},
1242 {K_F1, "[F1]"},
1243 {K_XF1, "[xF1]"},
1244 {K_F2, "[F2]"},
1245 {K_XF2, "[xF2]"},
1246 {K_F3, "[F3]"},
1247 {K_XF3, "[xF3]"},
1248 {K_F4, "[F4]"},
1249 {K_XF4, "[xF4]"},
1250 {K_F5, "[F5]"},
1251 {K_F6, "[F6]"},
1252 {K_F7, "[F7]"},
1253 {K_F8, "[F8]"},
1254 {K_F9, "[F9]"},
1255 {K_F10, "[F10]"},
1256 {K_F11, "[F11]"},
1257 {K_F12, "[F12]"},
1258 {K_S_F1, "[S-F1]"},
1259 {K_S_XF1, "[S-xF1]"},
1260 {K_S_F2, "[S-F2]"},
1261 {K_S_XF2, "[S-xF2]"},
1262 {K_S_F3, "[S-F3]"},
1263 {K_S_XF3, "[S-xF3]"},
1264 {K_S_F4, "[S-F4]"},
1265 {K_S_XF4, "[S-xF4]"},
1266 {K_S_F5, "[S-F5]"},
1267 {K_S_F6, "[S-F6]"},
1268 {K_S_F7, "[S-F7]"},
1269 {K_S_F8, "[S-F8]"},
1270 {K_S_F9, "[S-F9]"},
1271 {K_S_F10, "[S-F10]"},
1272 {K_S_F11, "[S-F11]"},
1273 {K_S_F12, "[S-F12]"},
1274 {K_HELP, "[HELP]"},
1275 {K_UNDO, "[UNDO]"},
1276 {K_BS, "[BS]"},
1277 {K_INS, "[INS]"},
1278 {K_KINS, "[KINS]"},
1279 {K_DEL, "[DEL]"},
1280 {K_KDEL, "[KDEL]"},
1281 {K_HOME, "[HOME]"},
1282 {K_S_HOME, "[C-HOME]"},
1283 {K_C_HOME, "[C-HOME]"},
1284 {K_KHOME, "[KHOME]"},
1285 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001286 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 {K_END, "[END]"},
1288 {K_S_END, "[C-END]"},
1289 {K_C_END, "[C-END]"},
1290 {K_KEND, "[KEND]"},
1291 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001292 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 {K_PAGEUP, "[PAGEUP]"},
1294 {K_PAGEDOWN, "[PAGEDOWN]"},
1295 {K_KPAGEUP, "[KPAGEUP]"},
1296 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1297 {K_MOUSE, "[MOUSE]"},
1298 {K_KPLUS, "[KPLUS]"},
1299 {K_KMINUS, "[KMINUS]"},
1300 {K_KDIVIDE, "[KDIVIDE]"},
1301 {K_KMULTIPLY, "[KMULTIPLY]"},
1302 {K_KENTER, "[KENTER]"},
1303 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001304 {K_PS, "[PASTE-START]"},
1305 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {K_K0, "[K0]"},
1307 {K_K1, "[K1]"},
1308 {K_K2, "[K2]"},
1309 {K_K3, "[K3]"},
1310 {K_K4, "[K4]"},
1311 {K_K5, "[K5]"},
1312 {K_K6, "[K6]"},
1313 {K_K7, "[K7]"},
1314 {K_K8, "[K8]"},
1315 {K_K9, "[K9]"},
1316# endif
1317
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001318#endif // NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319
1320/*
1321 * The most minimal terminal: only clear screen and cursor positioning
1322 * Always included.
1323 */
1324 {(int)KS_NAME, "dumb"},
1325 {(int)KS_CL, "\014"},
1326#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001327 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001329 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330#endif
1331
1332/*
1333 * end marker
1334 */
1335 {(int)KS_NAME, NULL}
1336
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001337}; // end of builtin_termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001339#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001340 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001341termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001342{
Bram Moolenaarab302212016-04-26 20:59:29 +02001343 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001344}
1345
1346 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001347termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001348{
1349 guicolor_T t;
1350
1351 if (*name == NUL)
1352 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001353 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001354
1355 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001356 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001357 return t;
1358}
1359
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001360 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001361termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001362{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001363 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001364}
1365#endif
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367/*
1368 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370#ifdef AMIGA
1371# define DEFAULT_TERM (char_u *)"amiga"
1372#endif
1373
1374#ifdef MSWIN
1375# define DEFAULT_TERM (char_u *)"win32"
1376#endif
1377
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001378#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379# define DEFAULT_TERM (char_u *)"ansi"
1380#endif
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382#ifdef VMS
1383# define DEFAULT_TERM (char_u *)"vt320"
1384#endif
1385
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001386#ifdef __HAIKU__
1387# undef DEFAULT_TERM
1388# define DEFAULT_TERM (char_u *)"xterm"
1389#endif
1390
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391#ifndef DEFAULT_TERM
1392# define DEFAULT_TERM (char_u *)"dumb"
1393#endif
1394
1395/*
1396 * Term_strings contains currently used terminal output strings.
1397 * It is initialized with the default values by parse_builtin_tcap().
1398 * The values can be changed by setting the option with the same name.
1399 */
1400char_u *(term_strings[(int)KS_LAST + 1]);
1401
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001402static int need_gather = FALSE; // need to fill termleader[]
1403static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001405static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001406
1407/*
1408 * Structure and table to store terminal features that can be detected by
1409 * querying the terminal. Either by inspecting the termresponse or a more
1410 * specific request. Besides this there are:
1411 * t_colors - number of colors supported
1412 */
1413typedef struct {
1414 char *tpr_name;
1415 int tpr_set_by_termresponse;
1416 int tpr_status;
1417} termprop_T;
1418
1419// Values for tpr_status.
1420#define TPR_UNKNOWN 'u'
1421#define TPR_YES 'y'
1422#define TPR_NO 'n'
1423#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1424#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1425#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1426
1427// can request the cursor style without messing up the display
1428#define TPR_CURSOR_STYLE 0
1429// can request the cursor blink mode without messing up the display
1430#define TPR_CURSOR_BLINK 1
1431// can set the underline color with t_8u without resetting other colors
1432#define TPR_UNDERLINE_RGB 2
1433// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1434#define TPR_MOUSE 3
1435// table size
1436#define TPR_COUNT 4
1437
1438static termprop_T term_props[TPR_COUNT];
1439
1440/*
1441 * Initialize the term_props table.
1442 * When "all" is FALSE only set those that are detected from the version
1443 * response.
1444 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001445 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001446init_term_props(int all)
1447{
1448 int i;
1449
1450 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1451 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1452 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1453 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1454 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1455 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1456 term_props[TPR_MOUSE].tpr_name = "mouse";
1457 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
1458
1459 for (i = 0; i < TPR_COUNT; ++i)
1460 if (all || term_props[i].tpr_set_by_termresponse)
1461 term_props[i].tpr_status = TPR_UNKNOWN;
1462}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463#endif
1464
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001465#if defined(FEAT_EVAL) || defined(PROTO)
1466 void
1467f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1468{
1469# ifdef FEAT_TERMRESPONSE
1470 int i;
1471# endif
1472
1473 if (rettv_dict_alloc(rettv) != OK)
1474 return;
1475# ifdef FEAT_TERMRESPONSE
1476 for (i = 0; i < TPR_COUNT; ++i)
1477 {
1478 char_u value[2];
1479
1480 value[0] = term_props[i].tpr_status;
1481 value[1] = NUL;
1482 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1483 }
1484# endif
1485}
1486#endif
1487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001489find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 struct builtin_term *p;
1492
1493 p = builtin_termcaps;
1494 while (p->bt_string != NULL)
1495 {
1496 if (p->bt_entry == (int)KS_NAME)
1497 {
1498#ifdef UNIX
1499 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1500 return p;
1501 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1502 return p;
1503 else
1504#endif
1505#ifdef VMS
1506 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1507 return p;
1508 else
1509#endif
1510 if (STRCMP(term, p->bt_string) == 0)
1511 return p;
1512 }
1513 ++p;
1514 }
1515 return p;
1516}
1517
1518/*
1519 * Parsing of the builtin termcap entries.
1520 * Caller should check if 'name' is a valid builtin term.
1521 * The terminal's name is not set, as this is already done in termcapinit().
1522 */
1523 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001524parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 struct builtin_term *p;
1527 char_u name[2];
1528 int term_8bit;
1529
1530 p = find_builtin_term(term);
1531 term_8bit = term_is_8bit(term);
1532
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001533 // Do not parse if builtin term not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 if (p->bt_string == NULL)
1535 return;
1536
1537 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1538 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001539 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001541 // Only set the value if it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (term_strings[p->bt_entry] == NULL
1543 || term_strings[p->bt_entry] == empty_option)
1544 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001545#ifdef FEAT_EVAL
1546 int opt_idx = -1;
1547#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001548 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1550 {
1551 char_u *s, *t;
1552
1553 s = vim_strsave((char_u *)p->bt_string);
1554 if (s != NULL)
1555 {
1556 for (t = s; *t; ++t)
1557 if (term_7to8bit(t))
1558 {
1559 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001560 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 }
1562 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001563#ifdef FEAT_EVAL
1564 opt_idx =
1565#endif
1566 set_term_option_alloced(
1567 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 }
1569 }
1570 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001573#ifdef FEAT_EVAL
1574 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1575#endif
1576 }
1577#ifdef FEAT_EVAL
1578 set_term_option_sctx_idx(NULL, opt_idx);
1579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
1581 }
1582 else
1583 {
1584 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1585 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1586 if (find_termcode(name) == NULL)
1587 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1588 }
1589 }
1590}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592/*
1593 * Set number of colors.
1594 * Store it as a number in t_colors.
1595 * Store it as a string in T_CCO (using nr_colors[]).
1596 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001597 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001598set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001600 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
1602 t_colors = nr;
1603 if (t_colors > 1)
1604 sprintf((char *)nr_colors, "%d", t_colors);
1605 else
1606 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001607 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001609
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001610#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001611/*
1612 * Set the color count to "val" and redraw if it changed.
1613 */
1614 static void
1615may_adjust_color_count(int val)
1616{
1617 if (val != t_colors)
1618 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001619 // Nr of colors changed, initialize highlighting and
1620 // redraw everything. This causes a redraw, which usually
1621 // clears the message. Try keeping the message if it
1622 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001623 set_keep_msg_from_hist();
1624 set_color_count(val);
1625 init_highlight(TRUE, FALSE);
1626# ifdef DEBUG_TERMRESPONSE
1627 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001628 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001629
Bram Moolenaarb255b902018-04-24 21:40:10 +02001630 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001631 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001632# else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001633 redraw_asap(CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001634# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001635 }
1636}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637#endif
1638
1639#ifdef HAVE_TGETENT
1640static char *(key_names[]) =
1641{
Bram Moolenaar87202262020-05-24 17:23:45 +02001642# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001643 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 "#2", "#4", "%i", "*7",
1648 "k1", "k2", "k3", "k4", "k5", "k6",
1649 "k7", "k8", "k9", "k;", "F1", "F2",
1650 "%1", "&8", "kb", "kI", "kD", "kh",
1651 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1652 NULL
1653};
1654#endif
1655
Bram Moolenaar9289df52018-05-10 14:40:57 +02001656#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001657 static void
1658get_term_entries(int *height, int *width)
1659{
1660 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001661 enum SpecialKey dest; // index in term_strings[]
1662 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001663 } string_names[] =
1664 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1665 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1666 {KS_CL, "cl"}, {KS_CD, "cd"},
1667 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1668 {KS_ME, "me"}, {KS_MR, "mr"},
1669 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1670 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1671 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1672 {KS_STE,"Te"}, {KS_STS,"Ts"},
1673 {KS_CM, "cm"}, {KS_SR, "sr"},
1674 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1675 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001676 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001677 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001678 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1679 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001680 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1681 {KS_VS, "vs"}, {KS_CVS, "VS"},
1682 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1683 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1684 {KS_TS, "ts"}, {KS_FS, "fs"},
1685 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1686 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1687 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001688 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001689 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1690 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001691 {KS_CST, "ST"}, {KS_CRT, "RT"},
1692 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001693 {(enum SpecialKey)0, NULL}
1694 };
1695 int i;
1696 char_u *p;
1697 static char_u tstrbuf[TBUFSZ];
1698 char_u *tp = tstrbuf;
1699
1700 /*
1701 * get output strings
1702 */
1703 for (i = 0; string_names[i].name != NULL; ++i)
1704 {
1705 if (TERM_STR(string_names[i].dest) == NULL
1706 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001707 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001708 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001709#ifdef FEAT_EVAL
1710 set_term_option_sctx_idx(string_names[i].name, -1);
1711#endif
1712 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001713 }
1714
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001715 // tgetflag() returns 1 if the flag is present, 0 if not and
1716 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001717 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1718 T_MS = (char_u *)"y";
1719 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1720 T_XS = (char_u *)"y";
1721 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1722 T_XN = (char_u *)"y";
1723 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1724 T_DB = (char_u *)"y";
1725 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1726 T_DA = (char_u *)"y";
1727 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1728 T_UT = (char_u *)"y";
1729
1730 /*
1731 * get key codes
1732 */
1733 for (i = 0; key_names[i] != NULL; ++i)
1734 if (find_termcode((char_u *)key_names[i]) == NULL)
1735 {
1736 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001737 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001738 if (p != NULL
1739 && (*p != Ctrl_H
1740 || key_names[i][0] != 'k'
1741 || key_names[i][1] != 'l'))
1742 add_termcode((char_u *)key_names[i], p, FALSE);
1743 }
1744
1745 if (*height == 0)
1746 *height = tgetnum("li");
1747 if (*width == 0)
1748 *width = tgetnum("co");
1749
1750 /*
1751 * Get number of colors (if not done already).
1752 */
1753 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001754 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001755 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001756#ifdef FEAT_EVAL
1757 set_term_option_sctx_idx("Co", -1);
1758#endif
1759 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001760
1761# ifndef hpux
1762 BC = (char *)TGETSTR("bc", &tp);
1763 UP = (char *)TGETSTR("up", &tp);
1764 p = TGETSTR("pc", &tp);
1765 if (p)
1766 PC = *p;
1767# endif
1768}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001769#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001770
1771 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001772report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001773{
1774 struct builtin_term *termp;
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001775 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001776
1777 mch_errmsg("\r\n");
1778 if (error_msg != NULL)
1779 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001780 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001781 mch_errmsg("\r\n");
1782 }
1783 mch_errmsg("'");
1784 mch_errmsg((char *)term);
1785 mch_errmsg(_("' not known. Available builtin terminals are:"));
1786 mch_errmsg("\r\n");
1787 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1788 {
Bram Moolenaar0f5575d2021-07-30 21:18:03 +02001789 if (termp->bt_entry == (int)KS_NAME
1790 && STRCMP(termp->bt_string, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001791 {
1792#ifdef HAVE_TGETENT
1793 mch_errmsg(" builtin_");
1794#else
1795 mch_errmsg(" ");
1796#endif
1797 mch_errmsg(termp->bt_string);
1798 mch_errmsg("\r\n");
1799 }
1800 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001801 // Output extra 'cmdheight' line breaks to avoid that the following error
1802 // message overwrites the last terminal name.
1803 for (i = 1; i < p_ch; ++i)
1804 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001805}
1806
1807 static void
1808report_default_term(char_u *term)
1809{
1810 mch_errmsg(_("defaulting to '"));
1811 mch_errmsg((char *)term);
1812 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001813 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001814 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001815 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001816 out_flush();
1817 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001818 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001819 }
1820}
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
1823 * Set terminal options for terminal "term".
1824 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1825 *
1826 * While doing this, until ttest(), some options may be NULL, be careful.
1827 */
1828 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001829set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830{
1831 struct builtin_term *termp;
1832#ifdef HAVE_TGETENT
1833 int builtin_first = p_tbi;
1834 int try;
1835 int termcap_cleared = FALSE;
1836#endif
1837 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001838 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 char_u *bs_p, *del_p;
1840
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001841 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001842 if (silent_mode)
1843 return OK;
1844
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001845 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
1847 if (term_is_builtin(term))
1848 {
1849 term += 8;
1850#ifdef HAVE_TGETENT
1851 builtin_first = 1;
1852#endif
1853 }
1854
1855/*
1856 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1857 * If builtin_first is TRUE:
1858 * 0. try builtin termcap
1859 * 1. try external termcap
1860 * 2. if both fail default to a builtin terminal
1861 * If builtin_first is FALSE:
1862 * 1. try external termcap
1863 * 2. try builtin termcap, if both fail default to a builtin terminal
1864 */
1865#ifdef HAVE_TGETENT
1866 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1867 {
1868 /*
1869 * Use external termcap
1870 */
1871 if (try == 1)
1872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874
1875 /*
1876 * If the external termcap does not have a matching entry, try the
1877 * builtin ones.
1878 */
1879 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1880 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (!termcap_cleared)
1882 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001883 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 termcap_cleared = TRUE;
1885 }
1886
Bram Moolenaar69e05692018-05-10 14:11:52 +02001887 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 }
1889 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001890 else // try == 0 || try == 2
1891#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 /*
1893 * Use builtin termcap
1894 */
1895 {
1896#ifdef HAVE_TGETENT
1897 /*
1898 * If builtin termcap was already used, there is no need to search
1899 * for the builtin termcap again, quit now.
1900 */
1901 if (try == 2 && builtin_first && termcap_cleared)
1902 break;
1903#endif
1904 /*
1905 * search for 'term' in builtin_termcaps[]
1906 */
1907 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001908 if (termp->bt_string == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
1910#ifdef HAVE_TGETENT
1911 /*
1912 * If try == 0, first try the external termcap. If that is not
1913 * found we'll get back here with try == 2.
1914 * If termcap_cleared is set we used the external termcap,
1915 * don't complain about not finding the term in the builtin
1916 * termcap.
1917 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001918 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001920 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 break;
1922#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001923 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001925 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if (starting != NO_SCREEN)
1927 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001928 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 wait_return(TRUE);
1930 return FAIL;
1931 }
1932 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001933 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001934 set_string_option_direct((char_u *)"term", -1, term,
1935 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 display_errors();
1937 }
1938 out_flush();
1939#ifdef HAVE_TGETENT
1940 if (!termcap_cleared)
1941 {
1942#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001943 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944#ifdef HAVE_TGETENT
1945 termcap_cleared = TRUE;
1946 }
1947#endif
1948 parse_builtin_tcap(term);
1949#ifdef FEAT_GUI
1950 if (term_is_gui(term))
1951 {
1952 out_flush();
1953 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001954 // If starting the GUI failed, don't do any of the other
1955 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (!gui.in_use)
1957 return FAIL;
1958#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001959 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960#endif
1961 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001962#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 }
1964#ifdef HAVE_TGETENT
1965 }
1966#endif
1967
1968/*
1969 * special: There is no info in the termcap about whether the cursor
1970 * positioning is relative to the start of the screen or to the start of the
1971 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1972 * relative.
1973 */
1974 if (STRCMP(term, "pcterm") == 0)
1975 T_CCS = (char_u *)"yes";
1976 else
1977 T_CCS = empty_option;
1978
1979#ifdef UNIX
1980/*
1981 * Any "stty" settings override the default for t_kb from the termcap.
1982 * This is in os_unix.c, because it depends a lot on the version of unix that
1983 * is being used.
1984 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1985 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001986# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 get_stty();
1990#endif
1991
1992/*
1993 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1994 * didn't work, use the default CTRL-H
1995 * The default for t_kD is DEL, unless t_kb is DEL.
1996 * The vim_strsave'd strings are probably lost forever, well it's only two
1997 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1998 * directly.
1999 */
2000#ifdef FEAT_GUI
2001 if (!gui.in_use)
2002#endif
2003 {
2004 bs_p = find_termcode((char_u *)"kb");
2005 del_p = find_termcode((char_u *)"kD");
2006 if (bs_p == NULL || *bs_p == NUL)
2007 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2008 if ((del_p == NULL || *del_p == NUL) &&
2009 (bs_p == NULL || *bs_p != DEL))
2010 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2011 }
2012
2013#if defined(UNIX) || defined(VMS)
2014 term_is_xterm = vim_is_xterm(term);
2015#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002016#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002017 // Reset terminal properties that are set based on the termresponse, which
2018 // will be sent out soon.
2019 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002022#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 /*
2024 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2025 * The termcode for the mouse is added as a side effect in option.c.
2026 */
2027 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002028 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002030# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002031 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
2033 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002034 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 else
2036 p = (char_u *)"xterm";
2037 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002040 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002041 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002042 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2043 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002044 reset_option_was_set((char_u *)"ttym");
2045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002047# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002051 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002053#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
Bram Moolenaarbf789742021-01-16 11:21:40 +01002057#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002058 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002059 if (use_xterm_like_mouse(term))
2060 {
2061 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002062
2063 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002064 name[0] = KS_EXTRA;
2065 name[1] = KE_FOCUSGAINED;
2066 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002067 add_termcode(name, (char_u *)"\033[I", FALSE);
2068
2069 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002070 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002071 add_termcode(name, (char_u *)"\033[O", FALSE);
2072
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002073 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002074 }
2075#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002076#if (defined(UNIX) || defined(VMS))
2077 // First time after setting 'term' a focus event is always reported.
2078 focus_state = MAYBE;
2079#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002080
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002082 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 if (STRCMP(term, DEFAULT_TERM) != 0)
2084 term_console = FALSE;
2085 else
2086 {
2087 term_console = TRUE;
2088# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002089 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090# endif
2091 }
2092#endif
2093
2094#if defined(UNIX) || defined(VMS)
2095 /*
2096 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2097 */
2098 if (vim_is_fastterm(term))
2099 p_tf = TRUE;
2100#endif
2101#ifdef USE_TERM_CONSOLE
2102 /*
2103 * 'ttyfast' is default on consoles
2104 */
2105 if (term_console)
2106 p_tf = TRUE;
2107#endif
2108
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002109 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002111 full_screen = TRUE; // we can use termcap codes from now on
2112 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002114 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002115 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002116 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117#endif
2118
2119 /*
2120 * Initialize the terminal with the appropriate termcap codes.
2121 * Set the mouse and window title if possible.
2122 * Don't do this when starting, need to parse the .vimrc first, because it
2123 * may redefine t_TI etc.
2124 */
2125 if (starting != NO_SCREEN)
2126 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002127 starttermcap(); // may change terminal mode
2128 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002129 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
2131
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002132 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if (width <= 0 || height <= 0)
2134 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002135 // termcap failed to report size
2136 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002138#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002139 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002141 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142#endif
2143 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002144 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (starting != NO_SCREEN)
2146 {
2147 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002148 scroll_region_reset(); // In case Rows changed
2149 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002152 buf_T *buf;
2153 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
2155 /*
2156 * Execute the TermChanged autocommands for each buffer that is
2157 * loaded.
2158 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002159 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
2161 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002162 {
2163 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2165 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002166 // restore curwin/curbuf and a few other things
2167 aucmd_restbuf(&aco);
2168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
2172
2173#ifdef FEAT_TERMRESPONSE
2174 may_req_termresponse();
2175#endif
2176
2177 return OK;
2178}
2179
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002180#if defined(EXITFREE) || defined(PROTO)
2181
2182# ifdef HAVE_DEL_CURTERM
2183# undef TERMINAL // name clash in term.h
2184# include <term.h> // declares cur_term
2185# endif
2186
2187/*
2188 * If supported, delete "cur_term", which caches terminal related entries.
2189 * Avoids that valgrind reports possibly lost memory.
2190 */
2191 void
2192free_cur_term()
2193{
2194# ifdef HAVE_DEL_CURTERM
2195 if (cur_term)
2196 del_curterm(cur_term);
2197# endif
2198}
2199
2200#endif
2201
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202#ifdef HAVE_TGETENT
2203/*
2204 * Call tgetent()
2205 * Return error message if it fails, NULL if it's OK.
2206 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002207 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002208tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209{
2210 int i;
2211
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002212 // Note: Valgrind may report a leak here, because the library keeps one
2213 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002215 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002217 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218# endif
2219 )
2220 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002221 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2222 // tgetent() with the always existing "dumb" entry to avoid a crash or
2223 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 (void)TGETENT(tbuf, "dumb");
2225
2226 if (i < 0)
2227# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002228 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 if (i == 0)
2230# endif
2231#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002232 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002234 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235#endif
2236 }
2237 return NULL;
2238}
2239
2240/*
2241 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2242 * Fix that here.
2243 */
2244 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002245vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246{
2247 char *p;
2248
2249 p = tgetstr(s, (char **)pp);
2250 if (p == (char *)-1)
2251 p = NULL;
2252 return (char_u *)p;
2253}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002254#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002256#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257/*
2258 * Get Columns and Rows from the termcap. Used after a window signal if the
2259 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2260 * and "li" entries never change. But on some systems this works.
2261 * Errors while getting the entries are ignored.
2262 */
2263 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002264getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002265 long *cp, // pointer to columns
2266 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267{
2268 char_u tbuf[TBUFSZ];
2269
2270 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2271 {
2272 if (*cp == 0)
2273 *cp = tgetnum("co");
2274 if (*rp == 0)
2275 *rp = tgetnum("li");
2276 }
2277}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002278#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279
2280/*
2281 * Get a string entry from the termcap and add it to the list of termcodes.
2282 * Used for <t_xx> special keys.
2283 * Give an error message for failure when not sourcing.
2284 * If force given, replace an existing entry.
2285 * Return FAIL if the entry was not found, OK if the entry was added.
2286 */
2287 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002288add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
2290 char_u *term;
2291 int key;
2292 struct builtin_term *termp;
2293#ifdef HAVE_TGETENT
2294 char_u *string;
2295 int i;
2296 int builtin_first;
2297 char_u tbuf[TBUFSZ];
2298 char_u tstrbuf[TBUFSZ];
2299 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002300 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301#endif
2302
2303/*
2304 * If the GUI is running or will start in a moment, we only support the keys
2305 * that the GUI can produce.
2306 */
2307#ifdef FEAT_GUI
2308 if (gui.in_use || gui.starting)
2309 return gui_mch_haskey(name);
2310#endif
2311
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002312 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 return OK;
2314
2315 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002316 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 return FAIL;
2318
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002319 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
2321 term += 8;
2322#ifdef HAVE_TGETENT
2323 builtin_first = TRUE;
2324#endif
2325 }
2326#ifdef HAVE_TGETENT
2327 else
2328 builtin_first = p_tbi;
2329#endif
2330
2331#ifdef HAVE_TGETENT
2332/*
2333 * We can get the entry from the builtin termcap and from the external one.
2334 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2335 * builtin termcap first.
2336 * If 'ttybuiltin' is off, try external termcap first.
2337 */
2338 for (i = 0; i < 2; ++i)
2339 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002340 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341#endif
2342 /*
2343 * Search in builtin termcap
2344 */
2345 {
2346 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002347 if (termp->bt_string != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002350 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 while (termp->bt_entry != (int)KS_NAME)
2352 {
2353 if ((int)termp->bt_entry == key)
2354 {
2355 add_termcode(name, (char_u *)termp->bt_string,
2356 term_is_8bit(term));
2357 return OK;
2358 }
2359 ++termp;
2360 }
2361 }
2362 }
2363#ifdef HAVE_TGETENT
2364 else
2365 /*
2366 * Search in external termcap
2367 */
2368 {
2369 error_msg = tgetent_error(tbuf, term);
2370 if (error_msg == NULL)
2371 {
2372 string = TGETSTR((char *)name, &tp);
2373 if (string != NULL && *string != NUL)
2374 {
2375 add_termcode(name, string, FALSE);
2376 return OK;
2377 }
2378 }
2379 }
2380 }
2381#endif
2382
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002383 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 {
2385#ifdef HAVE_TGETENT
2386 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002387 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 else
2389#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002390 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392 return FAIL;
2393}
2394
2395 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002396term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
2398 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2399}
2400
2401/*
2402 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2403 * Assume that the terminal is using 8-bit controls when the name contains
2404 * "8bit", like in "xterm-8bit".
2405 */
2406 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002407term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408{
2409 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2410}
2411
2412/*
2413 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002414 * <Esc>[ -> CSI <M_C_[>
2415 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 * <Esc>O -> <M-C-O>
2417 */
2418 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002419term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421 if (*p == ESC)
2422 {
2423 if (p[1] == '[')
2424 return CSI;
2425 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002426 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 if (p[1] == 'O')
2428 return 0x8f;
2429 }
2430 return 0;
2431}
2432
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002433#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002435term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436{
2437 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2438}
2439#endif
2440
2441#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2442
2443 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002444tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445{
2446 static char_u buf[16];
2447 char_u *p;
2448
2449 p = buf + 15;
2450 *p = '\0';
2451 do
2452 {
2453 --p;
2454 *p = (char_u) (i % 10 + '0');
2455 i /= 10;
2456 }
2457 while (i > 0 && p > buf);
2458 return p;
2459}
2460#endif
2461
2462#ifndef HAVE_TGETENT
2463
2464/*
2465 * minimal tgoto() implementation.
2466 * no padding and we only parse for %i %d and %+char
2467 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002468 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002469tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470{
2471 static char buf[30];
2472 char *p, *s, *e;
2473
2474 if (!cm)
2475 return "OOPS";
2476 e = buf + 29;
2477 for (s = buf; s < e && *cm; cm++)
2478 {
2479 if (*cm != '%')
2480 {
2481 *s++ = *cm;
2482 continue;
2483 }
2484 switch (*++cm)
2485 {
2486 case 'd':
2487 p = (char *)tltoa((unsigned long)y);
2488 y = x;
2489 while (*p)
2490 *s++ = *p++;
2491 break;
2492 case 'i':
2493 x++;
2494 y++;
2495 break;
2496 case '+':
2497 *s++ = (char)(*++cm + y);
2498 y = x;
2499 break;
2500 case '%':
2501 *s++ = *cm;
2502 break;
2503 default:
2504 return "OOPS";
2505 }
2506 }
2507 *s = '\0';
2508 return buf;
2509}
2510
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002511#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513/*
2514 * Set the terminal name and initialize the terminal options.
2515 * If "name" is NULL or empty, get the terminal name from the environment.
2516 * If that fails, use the default terminal name.
2517 */
2518 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002519termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520{
2521 char_u *term;
2522
2523 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002524 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 term = name;
2526
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527#ifndef MSWIN
2528 if (term == NULL)
2529 term = mch_getenv((char_u *)"TERM");
2530#endif
2531 if (term == NULL || *term == NUL)
2532 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002533 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002535 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 set_string_default("term", term);
2537 set_string_default("ttytype", term);
2538
2539 /*
2540 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2541 */
2542 set_termname(T_NAME != NULL ? T_NAME : term);
2543}
2544
2545/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002546 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002548#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002549
2550// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002552
2553static int out_pos = 0; // number of chars in out_buf
2554
2555// Since the maximum number of SGR parameters shown as a normal value range is
2556// 16, the escape sequence length can be 4 * 16 + lead + tail.
2557#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
2559/*
2560 * out_flush(): flush the output buffer
2561 */
2562 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002563out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
2565 int len;
2566
2567 if (out_pos != 0)
2568 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002569 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 len = out_pos;
2571 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002572 ui_write(out_buf, len, FALSE);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002573#ifdef FEAT_JOB_CHANNEL
2574 if (ch_log_output)
2575 {
2576 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002577 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002578# ifdef FEAT_GUI
2579 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2580# endif
2581 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002582 out_buf);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002583 ch_log_output = FALSE;
2584 }
2585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 }
2587}
2588
Bram Moolenaara338adc2018-01-31 20:51:47 +01002589/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002590 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2591 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002592 */
2593 void
2594out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002595 int force UNUSED, // when TRUE, update cursor even when not moved
2596 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002597{
2598 mch_disable_flush();
2599 out_flush();
2600 mch_enable_flush();
2601#ifdef FEAT_GUI
2602 if (gui.in_use)
2603 {
2604 gui_update_cursor(force, clear_selection);
2605 gui_may_flush();
2606 }
2607#endif
2608}
2609
2610
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611/*
2612 * Sometimes a byte out of a multi-byte character is written with out_char().
2613 * To avoid flushing half of the character, call this function first.
2614 */
2615 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002616out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
2618 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2619 out_flush();
2620}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
2622#ifdef FEAT_GUI
2623/*
2624 * out_trash(): Throw away the contents of the output buffer
2625 */
2626 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002627out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
2629 out_pos = 0;
2630}
2631#endif
2632
2633/*
2634 * out_char(c): put a byte into the output buffer.
2635 * Flush it if it becomes full.
2636 * This should not be used for outputting text on the screen (use functions
2637 * like msg_puts() and screen_putchar() for that).
2638 */
2639 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002640out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641{
Bram Moolenaard0573012017-10-28 21:11:06 +02002642#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002643 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 out_char('\r');
2645#endif
2646
2647 out_buf[out_pos++] = c;
2648
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002649 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (out_pos >= OUT_SIZE || p_wd)
2651 out_flush();
2652}
2653
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002655 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002657 static int
2658out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002660 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
2662 if (out_pos >= OUT_SIZE)
2663 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002664 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665}
2666
2667/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002668 * A never-padding out_str().
2669 * Use this whenever you don't want to run the string through tputs().
2670 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 * is likely to strip off leading digits, that it mistakes for padding
2672 * information, and "%i", "%d", etc.
2673 * This should only be used for writing terminal codes, not for outputting
2674 * normal text (use functions like msg_puts() and screen_putchar() for that).
2675 */
2676 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002677out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002679 // avoid terminal strings being split up
2680 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 while (*s)
2684 out_char_nf(*s++);
2685
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002686 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (p_wd)
2688 out_flush();
2689}
2690
2691/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002692 * A conditional-flushing out_str, mainly for visualbell.
2693 * Handles a delay internally, because termlib may not respect the delay or do
2694 * it at the wrong time.
2695 * Note: Only for terminal strings.
2696 */
2697 void
2698out_str_cf(char_u *s)
2699{
2700 if (s != NULL && *s)
2701 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002702#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002703 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002704#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002705
2706#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002707 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002708 if (gui.in_use)
2709 {
2710 out_str_nf(s);
2711 return;
2712 }
2713#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002714 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002715 out_flush();
2716#ifdef HAVE_TGETENT
2717 for (p = s; *s; ++s)
2718 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002719 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002720 if (*s == '$' && *(s + 1) == '<')
2721 {
2722 char_u save_c = *s;
2723 int duration = atoi((char *)s + 2);
2724
2725 *s = NUL;
2726 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2727 *s = save_c;
2728 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002729# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002730 // Only sleep here if we can limit this happening in
2731 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002732 p = vim_strchr(s, '>');
2733 if (p == NULL || duration <= 0)
2734 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002735 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002736 p = s;
2737 }
2738 else
2739 {
2740 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002741 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002742 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002743# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002744 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002745 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002746# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002747 break;
2748 }
2749 }
2750 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2751#else
2752 while (*s)
2753 out_char_nf(*s++);
2754#endif
2755
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002756 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002757 if (p_wd)
2758 out_flush();
2759 }
2760}
2761
2762/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002764 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 * This should only be used for writing terminal codes, not for outputting
2766 * normal text (use functions like msg_puts() and screen_putchar() for that).
2767 */
2768 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002769out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
2771 if (s != NULL && *s)
2772 {
2773#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002774 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 if (gui.in_use)
2776 {
2777 out_str_nf(s);
2778 return;
2779 }
2780#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002781 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002782 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 out_flush();
2784#ifdef HAVE_TGETENT
2785 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2786#else
2787 while (*s)
2788 out_char_nf(*s++);
2789#endif
2790
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002791 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (p_wd)
2793 out_flush();
2794 }
2795}
2796
2797/*
2798 * cursor positioning using termcap parser. (jw)
2799 */
2800 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002801term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802{
2803 OUT_STR(tgoto((char *)T_CM, col, row));
2804}
2805
2806 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002807term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
2809 OUT_STR(tgoto((char *)T_CRI, 0, i));
2810}
2811
2812 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002813term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814{
2815 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2816}
2817
2818 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002819term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
2821 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2822}
2823
2824#if defined(HAVE_TGETENT) || defined(PROTO)
2825 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002826term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002828 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 if (x < 0)
2830 x = 0;
2831 if (y < 0)
2832 y = 0;
2833 OUT_STR(tgoto((char *)T_CWP, y, x));
2834}
2835
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002836# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2837/*
2838 * Return TRUE if we can request the terminal for a response.
2839 */
2840 static int
2841can_get_termresponse()
2842{
2843 return cur_tmode == TMODE_RAW
2844 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002845# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002846 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002847# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002848 && p_ek;
2849}
2850
Bram Moolenaarafd78262019-05-10 23:10:31 +02002851/*
2852 * Set "status" to STATUS_SENT.
2853 */
2854 static void
2855termrequest_sent(termrequest_T *status)
2856{
2857 status->tr_progress = STATUS_SENT;
2858 status->tr_start = time(NULL);
2859}
2860
2861/*
2862 * Return TRUE if any of the requests are in STATUS_SENT.
2863 */
2864 static int
2865termrequest_any_pending()
2866{
2867 int i;
2868 time_t now = time(NULL);
2869
2870 for (i = 0; all_termrequests[i] != NULL; ++i)
2871 {
2872 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2873 {
2874 if (all_termrequests[i]->tr_start > 0 && now > 0
2875 && all_termrequests[i]->tr_start + 2 < now)
2876 // Sent the request more than 2 seconds ago and didn't get a
2877 // response, assume it failed.
2878 all_termrequests[i]->tr_progress = STATUS_FAIL;
2879 else
2880 return TRUE;
2881 }
2882 }
2883 return FALSE;
2884}
2885
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002886static int winpos_x = -1;
2887static int winpos_y = -1;
2888static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002889
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002890# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002891/*
2892 * Try getting the Vim window position from the terminal.
2893 * Returns OK or FAIL.
2894 */
2895 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002896term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002897{
2898 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002899 int prev_winpos_x = winpos_x;
2900 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002901
2902 if (*T_CGP == NUL || !can_get_termresponse())
2903 return FAIL;
2904 winpos_x = -1;
2905 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002906 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002907 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002908 OUT_STR(T_CGP);
2909 out_flush();
2910
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002911 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002912 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002913 {
2914 (void)vpeekc_nomap();
2915 if (winpos_x >= 0 && winpos_y >= 0)
2916 {
2917 *x = winpos_x;
2918 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002919 return OK;
2920 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002921 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002922 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002923 // Do not reset "did_request_winpos", if we timed out the response might
2924 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002925
2926 winpos_x = prev_winpos_x;
2927 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002928 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002929 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002930 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002931 *x = winpos_x;
2932 *y = winpos_y;
2933 return OK;
2934 }
2935
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002936 return FALSE;
2937}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002938# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002939# endif
2940
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002942term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002944 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945}
2946#endif
2947
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002949term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002952 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002953 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002955 // Special handling of 16 colors, because termcap can't handle it
2956 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
2957 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002959 && ((s[0] == ESC && s[1] == '[')
2960#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2961 || (s[0] == ESC && s[1] == '|')
2962#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02002963 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 && s[i] != NUL
2965 && (STRCMP(s + i + 1, "%p1%dm") == 0
2966 || STRCMP(s + i + 1, "%dm") == 0)
2967 && (s[i] == '3' || s[i] == '4'))
2968 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002970 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002972 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002974 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002975#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002976 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002977#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002978 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02002979 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2980 : (n >= 16 ? "48;5;" : "10");
2981
2982 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2984 }
2985 else
2986 OUT_STR(tgoto((char *)s, 0, n));
2987}
2988
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002989 void
2990term_fg_color(int n)
2991{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002992 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002993 if (*T_CAF)
2994 term_color(T_CAF, n);
2995 else if (*T_CSF)
2996 term_color(T_CSF, n);
2997}
2998
2999 void
3000term_bg_color(int n)
3001{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003002 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003003 if (*T_CAB)
3004 term_color(T_CAB, n);
3005 else if (*T_CSB)
3006 term_color(T_CSB, n);
3007}
3008
Bram Moolenaare023e882020-05-31 16:42:30 +02003009 void
3010term_ul_color(int n)
3011{
3012 if (*T_CAU)
3013 term_color(T_CAU, n);
3014}
3015
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003016/*
3017 * Return "dark" or "light" depending on the kind of terminal.
3018 * This is just guessing! Recognized are:
3019 * "linux" Linux console
3020 * "screen.linux" Linux console with screen
3021 * "cygwin.*" Cygwin shell
3022 * "putty.*" Putty program
3023 * We also check the COLORFGBG environment variable, which is set by
3024 * rxvt and derivatives. This variable contains either two or three
3025 * values separated by semicolons; we want the last value in either
3026 * case. If this value is 0-6 or 8, our background is dark.
3027 */
3028 char_u *
3029term_bg_default(void)
3030{
3031#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003032 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003033 return (char_u *)"dark";
3034#else
3035 char_u *p;
3036
3037 if (STRCMP(T_NAME, "linux") == 0
3038 || STRCMP(T_NAME, "screen.linux") == 0
3039 || STRNCMP(T_NAME, "cygwin", 6) == 0
3040 || STRNCMP(T_NAME, "putty", 5) == 0
3041 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3042 && (p = vim_strrchr(p, ';')) != NULL
3043 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3044 && p[2] == NUL))
3045 return (char_u *)"dark";
3046 return (char_u *)"light";
3047#endif
3048}
3049
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003050#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003051
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003052#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3053#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3054#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003055
3056 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003057term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003058{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003059#define MAX_COLOR_STR_LEN 100
3060 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003061
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003062 if (*s == NUL)
3063 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003064 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003065 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003066#ifdef FEAT_VTP
3067 if (use_wt())
3068 {
3069 out_flush();
3070 buf[1] = '[';
3071 vtp_printf(buf);
3072 }
3073 else
3074#endif
3075 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003076}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003077
3078 void
3079term_fg_rgb_color(guicolor_T rgb)
3080{
3081 term_rgb_color(T_8F, rgb);
3082}
3083
3084 void
3085term_bg_rgb_color(guicolor_T rgb)
3086{
3087 term_rgb_color(T_8B, rgb);
3088}
Bram Moolenaare023e882020-05-31 16:42:30 +02003089
3090 void
3091term_ul_rgb_color(guicolor_T rgb)
3092{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003093# ifdef FEAT_TERMRESPONSE
3094 if (write_t_8u_state != OK)
3095 write_t_8u_state = MAYBE;
3096 else
3097# endif
3098 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003099}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003100#endif
3101
Bram Moolenaar651fca82021-11-29 20:39:38 +00003102#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103/*
3104 * Generic function to set window title, using t_ts and t_fs.
3105 */
3106 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003107term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003109#ifdef FEAT_JOB_CHANNEL
3110 ch_log_output = TRUE;
3111#endif
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003112 // t_ts takes one argument: column in status line
3113 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003115 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 out_flush();
3117}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003118
3119/*
3120 * Tell the terminal to push (save) the title and/or icon, so that it can be
3121 * popped (restored) later.
3122 */
3123 void
3124term_push_title(int which)
3125{
Bram Moolenaar27821262019-05-08 16:41:09 +02003126 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003127 {
3128 OUT_STR(T_CST);
3129 out_flush();
3130 }
3131
Bram Moolenaar27821262019-05-08 16:41:09 +02003132 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003133 {
3134 OUT_STR(T_SSI);
3135 out_flush();
3136 }
3137}
3138
3139/*
3140 * Tell the terminal to pop the title and/or icon.
3141 */
3142 void
3143term_pop_title(int which)
3144{
Bram Moolenaar27821262019-05-08 16:41:09 +02003145 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003146 {
3147 OUT_STR(T_CRT);
3148 out_flush();
3149 }
3150
Bram Moolenaar27821262019-05-08 16:41:09 +02003151 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003152 {
3153 OUT_STR(T_SRI);
3154 out_flush();
3155 }
3156}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#endif
3158
3159/*
3160 * Make sure we have a valid set or terminal options.
3161 * Replace all entries that are NULL by empty_option
3162 */
3163 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003164ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003166 char_u *env_colors;
3167
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003168 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 /*
3171 * MUST have "cm": cursor motion.
3172 */
3173 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003174 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 /*
3177 * if "cs" defined, use a scroll region, it's faster.
3178 */
3179 if (*T_CS != NUL)
3180 scroll_region = TRUE;
3181 else
3182 scroll_region = FALSE;
3183
3184 if (pairs)
3185 {
3186 /*
3187 * optional pairs
3188 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003189 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (*T_ME == NUL)
3191 T_ME = T_MR = T_MD = T_MB = empty_option;
3192 if (*T_SO == NUL || *T_SE == NUL)
3193 T_SO = T_SE = empty_option;
3194 if (*T_US == NUL || *T_UE == NUL)
3195 T_US = T_UE = empty_option;
3196 if (*T_CZH == NUL || *T_CZR == NUL)
3197 T_CZH = T_CZR = empty_option;
3198
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003199 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (*T_VE == NUL)
3201 T_VI = empty_option;
3202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003203 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 if (*T_ME == NUL)
3205 {
3206 T_ME = T_SE;
3207 T_MR = T_SO;
3208 T_MD = T_SO;
3209 }
3210
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003211 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (*T_SO == NUL)
3213 {
3214 T_SE = T_ME;
3215 if (*T_MR == NUL)
3216 T_SO = T_MD;
3217 else
3218 T_SO = T_MR;
3219 }
3220
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003221 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (*T_CZH == NUL)
3223 {
3224 T_CZR = T_ME;
3225 if (*T_MR == NUL)
3226 T_CZH = T_MD;
3227 else
3228 T_CZH = T_MR;
3229 }
3230
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003231 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 if (*T_CSB == NUL || *T_CSF == NUL)
3233 {
3234 T_CSB = empty_option;
3235 T_CSF = empty_option;
3236 }
3237
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003238 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (*T_CAB == NUL || *T_CAF == NUL)
3240 {
3241 T_CAB = empty_option;
3242 T_CAF = empty_option;
3243 }
3244
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003245 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003247 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003249 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 p_wiv = (*T_XS != NUL);
3251 }
3252 need_gather = TRUE;
3253
Bram Moolenaar759d8152020-04-26 16:52:49 +02003254 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3255 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003257#ifdef FEAT_GUI
3258 if (!gui.in_use)
3259#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003260 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003261 env_colors = mch_getenv((char_u *)"COLORS");
3262 if (env_colors != NULL && isdigit(*env_colors))
3263 {
3264 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003265
Bram Moolenaar759d8152020-04-26 16:52:49 +02003266 if (colors != t_colors)
3267 set_color_count(colors);
3268 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270}
3271
3272#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3273 || defined(PROTO)
3274/*
3275 * Represent the given long_u as individual bytes, with the most significant
3276 * byte first, and store them in dst.
3277 */
3278 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003279add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
3281 int i;
3282 int shift;
3283
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003284 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
3286 shift = 8 * (sizeof(long_u) - i);
3287 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3288 }
3289}
3290
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291/*
3292 * Interpret the next string of bytes in buf as a long integer, with the most
3293 * significant byte first. Note that it is assumed that buf has been through
3294 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3295 * Puts result in val, and returns the number of bytes read from buf
3296 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3297 * were present.
3298 */
3299 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003300get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302 int len;
3303 char_u bytes[sizeof(long_u)];
3304 int i;
3305 int shift;
3306
3307 *val = 0;
3308 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3309 if (len != -1)
3310 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003311 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 shift = 8 * (sizeof(long_u) - 1 - i);
3314 *val += (long_u)bytes[i] << shift;
3315 }
3316 }
3317 return len;
3318}
3319#endif
3320
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321/*
3322 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3323 * that buf has been through inchar(). Returns the actual number of bytes used
3324 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3325 * available.
3326 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003327 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003328get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329{
3330 int len = 0;
3331 int i;
3332 char_u c;
3333
3334 for (i = 0; i < num_bytes; i++)
3335 {
3336 if ((c = buf[len++]) == NUL)
3337 return -1;
3338 if (c == K_SPECIAL)
3339 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003340 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 return -1;
3342 if (buf[len++] == (int)KS_ZERO)
3343 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003344 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3345 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003346 if (buf[len++] == (int)KE_CSI)
3347 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003349 else if (c == CSI && buf[len] == KS_EXTRA
3350 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003351 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3352 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003353 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 bytes[i] = c;
3355 }
3356 return len;
3357}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
3359/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003360 * Check if the new shell size is valid, correct it if it's too small or way
3361 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 */
3363 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003364check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003366 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003368 limit_screen_size();
3369}
3370
3371/*
3372 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3373 */
3374 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003375limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003376{
3377 if (Columns < MIN_COLUMNS)
3378 Columns = MIN_COLUMNS;
3379 else if (Columns > 10000)
3380 Columns = 10000;
3381 if (Rows > 1000)
3382 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383}
3384
Bram Moolenaar86b68352004-12-27 21:59:20 +00003385/*
3386 * Invoked just before the screen structures are going to be (re)allocated.
3387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003389win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
3391 static int old_Rows = 0;
3392 static int old_Columns = 0;
3393
3394 if (old_Rows != Rows || old_Columns != Columns)
3395 ui_new_shellsize();
3396 if (old_Rows != Rows)
3397 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003398 // If 'window' uses the whole screen, keep it using that.
3399 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003400 if (p_window == old_Rows - 1
3401 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003402 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003404 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
3406 if (old_Columns != Columns)
3407 {
3408 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003409 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 }
3411}
3412
3413/*
3414 * Call this function when the Vim shell has been resized in any way.
3415 * Will obtain the current size and redraw (also when size didn't change).
3416 */
3417 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003418shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
3420 set_shellsize(0, 0, FALSE);
3421}
3422
3423/*
3424 * Check if the shell size changed. Handle a resize.
3425 * When the size didn't change, nothing happens.
3426 */
3427 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003428shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
3430 int old_Rows = Rows;
3431 int old_Columns = Columns;
3432
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003433 if (!exiting
3434#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003435 // Do not get the size when executing a shell command during
3436 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003437 && !gui.starting
3438#endif
3439 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003440 {
3441 (void)ui_get_shellsize();
3442 check_shellsize();
3443 if (old_Rows != Rows || old_Columns != Columns)
3444 shell_resized();
3445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446}
3447
3448/*
3449 * Set size of the Vim shell.
3450 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3451 * window size (this is used for the :win command).
3452 * If 'mustset' is FALSE, we may try to get the real window size and if
3453 * it fails use 'width' and 'height'.
3454 */
3455 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003456set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457{
3458 static int busy = FALSE;
3459
3460 /*
3461 * Avoid recursiveness, can happen when setting the window size causes
3462 * another window-changed signal.
3463 */
3464 if (busy)
3465 return;
3466
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003467 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 return;
3469
Bram Moolenaara971b822011-09-14 14:43:25 +02003470 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003472 // postpone the resizing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 State = SETWSIZE;
3474 return;
3475 }
3476
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003477 if (updating_screen)
3478 // resizing while in update_screen() may cause a crash
3479 return;
3480
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003481 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003482 // buffer (or window) has already been closed and removing a scrollbar
3483 // causes a resize event. Don't resize then, it will happen after entering
3484 // another buffer.
3485 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003486 return;
3487
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 ++busy;
3489
3490#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003491 out_flush(); // must do this before mch_get_shellsize() for
3492 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493#endif
3494
3495 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3496 {
3497 Rows = height;
3498 Columns = width;
3499 check_shellsize();
3500 ui_set_shellsize(mustset);
3501 }
3502 else
3503 check_shellsize();
3504
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003505 // The window layout used to be adjusted here, but it now happens in
3506 // screenalloc() (also invoked from screenclear()). That is because the
3507 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
3509 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3510 screenclear();
3511 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003512 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
3514 if (starting != NO_SCREEN)
3515 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003517
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 changed_line_abv_curs();
3519 invalidate_botline();
3520
3521 /*
3522 * We only redraw when it's needed:
3523 * - While at the more prompt or executing an external command, don't
3524 * redraw, but position the cursor.
3525 * - While editing the command line, only redraw that.
3526 * - in Ex mode, don't redraw anything.
3527 * - Otherwise, redraw right now, and position the cursor.
3528 * Always need to call update_screen() or screenalloc(), to make
3529 * sure Rows/Columns and the size of ScreenLines[] is correct!
3530 */
3531 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3532 || exmode_active)
3533 {
3534 screenalloc(FALSE);
3535 repeat_message();
3536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 else
3538 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003539 if (curwin->w_p_scb)
3540 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003541 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003542 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003543 update_screen(NOT_VALID);
3544 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003545 }
3546 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003547 {
3548 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003549 if (pum_visible())
3550 {
3551 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003552 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003553 }
Bram Moolenaara5e66212017-09-29 22:42:33 +02003554 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003555 if (redrawing())
3556 setcursor();
3557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003559 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
3561 out_flush();
3562 --busy;
3563}
3564
3565/*
3566 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3567 * commands and Ex mode).
3568 */
3569 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003570settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
3572#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003573 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 if (gui.in_use)
3575 return;
3576#endif
3577
3578 if (full_screen)
3579 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003581 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3582 * set the terminal to raw mode, even though we think it already is,
3583 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 * When we think the terminal is normal, don't try to set it to
3585 * normal again, because that causes problems (logout!) on some
3586 * machines.
3587 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003588 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
3590#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003591# ifdef FEAT_GUI
3592 if (!gui.in_use && !gui.starting)
3593# endif
3594 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003595 // May need to check for T_CRV response and termcodes, it
3596 // doesn't work in Cooked mode, an external program may get
3597 // them.
3598 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003599 (void)vpeekc_nomap();
3600 check_for_codes_from_term();
3601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003604 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003605
3606 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3607 // Avoid doing this too often, on some terminals the codes are not
3608 // handled properly.
3609 if (termcap_active && tmode != TMODE_SLEEP
3610 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003611 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003612#ifdef FEAT_JOB_CHANNEL
3613 ch_log_output = TRUE;
3614#endif
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003615 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003616 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003617 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003618 out_str(T_CTE); // possibly disables modifyOtherKeys
3619 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003620 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003621 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003622 out_str(T_BE); // enable bracketed paste mode (should
3623 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003624 out_str(T_CTI); // possibly enables modifyOtherKeys
3625 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003628 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003631 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 out_flush();
3633 }
3634#ifdef FEAT_TERMRESPONSE
3635 may_req_termresponse();
3636#endif
3637 }
3638}
3639
3640 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003641starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
3643 if (full_screen && !termcap_active)
3644 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003645#ifdef FEAT_JOB_CHANNEL
3646 ch_log_output = TRUE;
3647#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003648 out_str(T_TI); // start termcap mode
3649 out_str(T_CTI); // start "raw" mode
3650 out_str(T_KS); // start "keypad transmit" mode
3651 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003652
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003653#if defined(UNIX) || defined(VMS)
3654 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003655 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003656 out_str(T_FE);
3657#endif
3658
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 out_flush();
3660 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003661 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003663# ifdef FEAT_GUI
3664 if (!gui.in_use && !gui.starting)
3665# endif
3666 {
3667 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003668 // Immediately check for a response. If t_Co changes, we don't
3669 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003670 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003671 check_for_codes_from_term();
3672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673#endif
3674 }
3675}
3676
3677 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003678stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679{
3680 screen_stop_highlight();
3681 reset_cterm_colors();
3682 if (termcap_active)
3683 {
3684#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003685# ifdef FEAT_GUI
3686 if (!gui.in_use && !gui.starting)
3687# endif
3688 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003689 // May need to discard T_CRV, T_U7 or T_RBG response.
3690 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003691 {
3692# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003693 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003694 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003695# endif
3696# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003697 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003698 if (exiting)
3699 tcflush(fileno(stdin), TCIFLUSH);
3700# endif
3701 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003702 // Check for termcodes first, otherwise an external program may
3703 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003704 check_for_codes_from_term();
3705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706#endif
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003707#ifdef FEAT_JOB_CHANNEL
3708 ch_log_output = TRUE;
3709#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003710
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003711#if defined(UNIX) || defined(VMS)
3712 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003713 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003714 out_str(T_FD);
3715#endif
3716
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003717 out_str(T_BD); // disable bracketed paste mode
3718 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 out_flush();
3720 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003721 cursor_on(); // just in case it is still off
3722 out_str(T_CTE); // stop "raw" mode
3723 out_str(T_TE); // stop termcap mode
3724 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 out_flush();
3726 }
3727}
3728
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003729#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730/*
3731 * Request version string (for xterm) when needed.
3732 * Only do this after switching to raw mode, otherwise the result will be
3733 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003734 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003735 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 * Only do this after termcap mode has been started, otherwise the codes for
3737 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003738 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3739 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003740 * On Unix only do it when both output and input are a tty (avoid writing
3741 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 * The result is caught in check_termcode().
3743 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003744 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003745may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003747 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003748 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003749 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 && *T_CRV != NUL)
3751 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003752#ifdef FEAT_JOB_CHANNEL
3753 ch_log_output = TRUE;
3754#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003755 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003757 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003758 // check for the characters now, otherwise they might be eaten by
3759 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 out_flush();
3761 (void)vpeekc_nomap();
3762 }
3763}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003764
Bram Moolenaar9584b312013-03-13 19:29:28 +01003765/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003766 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3767 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003768 * Note that this goes out before T_CRV, so that the result can be used when
3769 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003770 */
3771 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003772check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003773{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003774 int did_send = FALSE;
3775
3776 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3777 return;
3778
Bram Moolenaarafd78262019-05-10 23:10:31 +02003779 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003780 && !option_was_set((char_u *)"ambiwidth"))
3781 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003782 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003783
Bram Moolenaara45551a2020-06-09 15:57:37 +02003784 // Ambiguous width check.
3785 // Check how the terminal treats ambiguous character width (UAX #11).
3786 // First, we move the cursor to (1, 0) and print a test ambiguous
3787 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3788 // the current cursor position. If the terminal treats \u25bd as
3789 // single width, the position is (1, 1), or if it is treated as double
3790 // width, that will be (1, 2). This function has the side effect that
3791 // changes cursor position, so it must be called immediately after
3792 // entering termcap mode.
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003793#ifdef FEAT_JOB_CHANNEL
3794 ch_log_output = TRUE;
3795#endif
Bram Moolenaara45551a2020-06-09 15:57:37 +02003796 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003797 // Do this in the second row. In the first row the returned sequence
3798 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003799 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003800 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003801 out_str(buf);
3802 out_str(T_U7);
3803 termrequest_sent(&u7_status);
3804 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003805 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003806
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003807 // This overwrites a few characters on the screen, a redraw is needed
3808 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003809 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003810 term_windgoto(1, 0);
3811 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003812 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003813 }
3814
Bram Moolenaard1f34e62022-01-07 19:24:20 +00003815 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02003816 {
3817 // 2. Check compatibility with xterm.
3818 // We move the cursor to (2, 0), print a test sequence and then query
3819 // the current cursor position. If the terminal properly handles
3820 // unknown DCS string and CSI sequence with intermediate byte, the test
3821 // sequence is ignored and the cursor does not move. If the terminal
3822 // handles test sequence incorrectly, a garbage string is displayed and
3823 // the cursor does move.
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003824#ifdef FEAT_JOB_CHANNEL
3825 ch_log_output = TRUE;
3826#endif
Bram Moolenaara45551a2020-06-09 15:57:37 +02003827 LOG_TR(("Sending xterm compatibility test sequence."));
3828 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003829 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003830 term_windgoto(2, 0);
3831 // send the test DCS string.
3832 out_str((char_u *)"\033Pzz\033\\");
3833 // send the test CSI sequence with intermediate byte.
3834 out_str((char_u *)"\033[0%m");
3835 out_str(T_U7);
3836 termrequest_sent(&xcc_status);
3837 out_flush();
3838 did_send = TRUE;
3839
3840 // If the terminal handles test sequence incorrectly, garbage text is
3841 // displayed. Clear them out for now.
3842 screen_stop_highlight();
3843 term_windgoto(2, 0);
3844 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003845 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003846 }
3847
3848 if (did_send)
3849 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003850 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003851
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003852 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003853 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003854
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003855 // check for the characters now, otherwise they might be eaten by
3856 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003857 out_flush();
3858 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003859 }
3860}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003861
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003862/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003863 * Similar to requesting the version string: Request the terminal background
3864 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003865 */
3866 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003867may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003868{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003869 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003870 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003871 int didit = FALSE;
3872
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003873# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003874 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003875 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003876 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003877#ifdef FEAT_JOB_CHANNEL
3878 ch_log_output = TRUE;
3879#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003880 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003881 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003882 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003883 didit = TRUE;
3884 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003885# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003886
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003887 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003888 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003889 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003890#ifdef FEAT_JOB_CHANNEL
3891 ch_log_output = TRUE;
3892#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003893 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003894 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003895 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003896 didit = TRUE;
3897 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003898
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003899 if (didit)
3900 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003901 // check for the characters now, otherwise they might be eaten by
3902 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003903 out_flush();
3904 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003905 }
3906 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003907}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003908
Bram Moolenaar2951b772013-07-03 12:45:31 +02003909# ifdef DEBUG_TERMRESPONSE
3910 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003911log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003912{
3913 static FILE *fd_tr = NULL;
3914 static proftime_T start;
3915 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003916 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003917
3918 if (fd_tr == NULL)
3919 {
3920 fd_tr = fopen("termresponse.log", "w");
3921 profile_start(&start);
3922 }
3923 now = start;
3924 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003925 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3926 must_redraw == NOT_VALID ? "NV"
3927 : must_redraw == CLEAR ? "CL" : " ");
3928 va_start(ap, fmt);
3929 vfprintf(fd_tr, fmt, ap);
3930 va_end(ap);
3931 fputc('\n', fd_tr);
3932 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003933}
3934# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935#endif
3936
3937/*
3938 * Return TRUE when saving and restoring the screen.
3939 */
3940 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003941swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
3943 return (full_screen && *T_TI != NUL);
3944}
3945
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946/*
3947 * By outputting the 'cursor very visible' termcap code, for some windowed
3948 * terminals this makes the screen scrolled to the correct position.
3949 * Used when starting Vim or returning from a shell.
3950 */
3951 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003952scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003954 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003956#ifdef FEAT_JOB_CHANNEL
3957 ch_log_output = TRUE;
3958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003960 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003961 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
3963}
3964
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003965// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966static int cursor_is_off = FALSE;
3967
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003968// True if cursor is not visible due to an ongoing cursor-less sleep
3969static int cursor_is_asleep = FALSE;
3970
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003972 * Enable the cursor without checking if it's already enabled.
3973 */
3974 void
3975cursor_on_force(void)
3976{
3977 out_str(T_VE);
3978 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003979 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02003980}
3981
3982/*
3983 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 */
3985 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003986cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003988 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003989 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990}
3991
3992/*
3993 * Disable the cursor.
3994 */
3995 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003996cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003998 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004000 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 cursor_is_off = TRUE;
4002 }
4003}
4004
Dominique Pelle748b3082022-01-08 12:41:16 +00004005#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004006/*
4007 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4008 */
4009 int
4010cursor_is_sleeping(void)
4011{
4012 return cursor_is_asleep;
4013}
Dominique Pelle748b3082022-01-08 12:41:16 +00004014#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004015
4016/*
4017 * Disable the cursor and mark it disabled by cursor-less sleep
4018 */
4019 void
4020cursor_sleep(void)
4021{
4022 cursor_is_asleep = TRUE;
4023 cursor_off();
4024}
4025
4026/*
4027 * Enable the cursor and mark it not disabled by cursor-less sleep
4028 */
4029 void
4030cursor_unsleep(void)
4031{
4032 cursor_is_asleep = FALSE;
4033 cursor_on();
4034}
4035
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004036#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004038 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004039 */
4040 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004041term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004042{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004043 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004044 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004045
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004046 // Only do something when redrawing the screen and we can restore the
4047 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004048 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004049 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004050# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004051 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004052 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004053 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004054# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004055 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004056 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004057
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004058 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004059 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004060 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004061 {
4062 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004063 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004064 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004065 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004066 if (*p != NUL)
4067 {
4068 out_str(p);
4069 showing_mode = REPLACE;
4070 }
4071 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004072 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004073 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004074 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004075 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004076 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004077 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004078 showing_mode = INSERT;
4079 }
4080 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004081 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004082 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004083 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004084 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004085 }
4086}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004087
4088# if defined(FEAT_TERMINAL) || defined(PROTO)
4089 void
4090term_cursor_color(char_u *color)
4091{
4092 if (*T_CSC != NUL)
4093 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004094 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004095 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004096 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004097 out_flush();
4098 }
4099}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004100# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004101
Bram Moolenaar4db25542017-08-28 22:43:05 +02004102 int
4103blink_state_is_inverted()
4104{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004105#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004106 return rbm_status.tr_progress == STATUS_GOT
4107 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004108 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004109#else
4110 return FALSE;
4111#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004112}
4113
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004114/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004115 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004116 */
4117 void
4118term_cursor_shape(int shape, int blink)
4119{
4120 if (*T_CSH != NUL)
4121 {
4122 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4123 out_flush();
4124 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004125 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004126 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004127 int do_blink = blink;
4128
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004129 // t_SH is empty: try setting just the blink state.
4130 // The blink flags are XORed together, if the initial blinking from
4131 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004132 if (blink_state_is_inverted())
4133 do_blink = !blink;
4134
4135 if (do_blink && *T_VS != NUL)
4136 {
4137 out_str(T_VS);
4138 out_flush();
4139 }
4140 else if (!do_blink && *T_CVS != NUL)
4141 {
4142 out_str(T_CVS);
4143 out_flush();
4144 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004145 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004146}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004147#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004148
4149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 * Set scrolling region for window 'wp'.
4151 * The region starts 'off' lines from the start of the window.
4152 * Also set the vertical scroll region for a vertically split window. Always
4153 * the full width of the window, excluding the vertical separator.
4154 */
4155 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004156scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157{
4158 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4159 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004161 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4162 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004163 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164}
4165
4166/*
4167 * Reset scrolling region to the whole screen.
4168 */
4169 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004170scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171{
4172 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 if (*T_CSV != NUL)
4174 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004175 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176}
4177
4178
4179/*
4180 * List of terminal codes that are currently recognized.
4181 */
4182
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004183static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004185 char_u name[2]; // termcap name of entry
4186 char_u *code; // terminal code (in allocated memory)
4187 int len; // STRLEN(code)
4188 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189} *termcodes = NULL;
4190
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004191static int tc_max_len = 0; // number of entries that termcodes[] can hold
4192static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004194static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004195
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004197clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198{
4199 while (tc_len > 0)
4200 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004201 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 tc_max_len = 0;
4203
4204#ifdef HAVE_TGETENT
4205 BC = (char *)empty_option;
4206 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004207 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 ospeed = 0;
4209#endif
4210
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004211 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212}
4213
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004214#define ATC_FROM_TERM 55
4215
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216/*
4217 * Add a new entry to the list of terminal codes.
4218 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004219 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4220 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 */
4222 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004223add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224{
4225 struct termcode *new_tc;
4226 int i, j;
4227 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004228 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
4230 if (string == NULL || *string == NUL)
4231 {
4232 del_termcode(name);
4233 return;
4234 }
4235
Bram Moolenaar4f974752019-02-17 17:44:42 +01004236#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004237 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004238#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004239# ifdef VIMDLL
4240 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004241 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004242 else
4243# endif
4244 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 if (s == NULL)
4247 return;
4248
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004249 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004250 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004252 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 s[0] = term_7to8bit(string);
4254 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004255
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004256#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4257# ifdef VIMDLL
4258 if (!gui.in_use)
4259# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004260 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004261 if (s[0] == K_NUL)
4262 {
4263 STRMOVE(s + 1, s);
4264 s[1] = 3;
4265 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004266 }
4267#endif
4268
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004269 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004271 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
4273 /*
4274 * need to make space for more entries
4275 */
4276 if (tc_len == tc_max_len)
4277 {
4278 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004279 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 if (new_tc == NULL)
4281 {
4282 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004283 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 return;
4285 }
4286 for (i = 0; i < tc_len; ++i)
4287 new_tc[i] = termcodes[i];
4288 vim_free(termcodes);
4289 termcodes = new_tc;
4290 }
4291
4292 /*
4293 * Look for existing entry with the same name, it is replaced.
4294 * Look for an existing entry that is alphabetical higher, the new entry
4295 * is inserted in front of it.
4296 */
4297 for (i = 0; i < tc_len; ++i)
4298 {
4299 if (termcodes[i].name[0] < name[0])
4300 continue;
4301 if (termcodes[i].name[0] == name[0])
4302 {
4303 if (termcodes[i].name[1] < name[1])
4304 continue;
4305 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004306 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 */
4308 if (termcodes[i].name[1] == name[1])
4309 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004310 if (flags == ATC_FROM_TERM && (j = termcode_star(
4311 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004312 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004313 // Don't replace ESC[123;*X or ESC O*X with another when
4314 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004315 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004316 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004317 && s[len - 1]
4318 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004319 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004320 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004321 vim_free(s);
4322 return;
4323 }
4324 }
4325 else
4326 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004327 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004328 vim_free(termcodes[i].code);
4329 --tc_len;
4330 break;
4331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 }
4333 }
4334 /*
4335 * Found alphabetical larger entry, move rest to insert new entry
4336 */
4337 for (j = tc_len; j > i; --j)
4338 termcodes[j] = termcodes[j - 1];
4339 break;
4340 }
4341
4342 termcodes[i].name[0] = name[0];
4343 termcodes[i].name[1] = name[1];
4344 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004345 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004346
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004347 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4348 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004349 termcodes[i].modlen = 0;
4350 j = termcode_star(s, len);
4351 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004352 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004353 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004354 // For "CSI[@;X" the "@" is not included in "modlen".
4355 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4356 --termcodes[i].modlen;
4357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 ++tc_len;
4359}
4360
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004361/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004362 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004363 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004364 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004365 */
4366 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004367termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004368{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004369 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004370 if (len >= 3 && code[len - 2] == '*')
4371 {
4372 if (len >= 5 && code[len - 3] == ';')
4373 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004374 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004375 return 1;
4376 }
4377 return 0;
4378}
4379
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004381find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
4383 int i;
4384
4385 for (i = 0; i < tc_len; ++i)
4386 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4387 return termcodes[i].code;
4388 return NULL;
4389}
4390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004392get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393{
4394 if (i >= tc_len)
4395 return NULL;
4396 return &termcodes[i].name[0];
4397}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004399/*
4400 * Returns the length of the terminal code at index 'idx'.
4401 */
4402 int
4403get_termcode_len(int idx)
4404{
4405 return termcodes[idx].len;
4406}
4407
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004408 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004409del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410{
4411 int i;
4412
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004413 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 return;
4415
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004416 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418 for (i = 0; i < tc_len; ++i)
4419 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4420 {
4421 del_termcode_idx(i);
4422 return;
4423 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004424 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425}
4426
4427 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004428del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429{
4430 int i;
4431
4432 vim_free(termcodes[idx].code);
4433 --tc_len;
4434 for (i = idx; i < tc_len; ++i)
4435 termcodes[i] = termcodes[i + 1];
4436}
4437
4438#ifdef FEAT_TERMRESPONSE
4439/*
4440 * Called when detected that the terminal sends 8-bit codes.
4441 * Convert all 7-bit codes to their 8-bit equivalent.
4442 */
4443 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004444switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445{
4446 int i;
4447 int c;
4448
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004449 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 if (!term_is_8bit(T_NAME))
4451 {
4452 for (i = 0; i < tc_len; ++i)
4453 {
4454 c = term_7to8bit(termcodes[i].code);
4455 if (c != 0)
4456 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004457 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 termcodes[i].code[0] = c;
4459 }
4460 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004461 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 }
4463 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004464 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465}
4466#endif
4467
4468#ifdef CHECK_DOUBLE_CLICK
4469static linenr_T orig_topline = 0;
4470# ifdef FEAT_DIFF
4471static int orig_topfill = 0;
4472# endif
4473#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004474#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004476 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 * "orig_topline" is used to avoid detecting a double-click when the window
4478 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4479 */
4480/*
4481 * Set orig_topline. Used when jumping to another window, so that a double
4482 * click still works.
4483 */
4484 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004485set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486{
4487 orig_topline = wp->w_topline;
4488# ifdef FEAT_DIFF
4489 orig_topfill = wp->w_topfill;
4490# endif
4491}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004492
4493/*
4494 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4495 * topline and topfill.
4496 */
4497 int
4498is_mouse_topline(win_T *wp)
4499{
4500 return orig_topline == wp->w_topline
4501#ifdef FEAT_DIFF
4502 && orig_topfill == wp->w_topfill
4503#endif
4504 ;
4505}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506#endif
4507
4508/*
zeertzjqfc78a032022-04-26 22:11:38 +01004509 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4510 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4511 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004512 * Remove "slen" bytes.
4513 * Returns FAIL for error.
4514 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004515 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004516put_string_in_typebuf(
4517 int offset,
4518 int slen,
4519 char_u *string,
4520 int new_slen,
4521 char_u *buf,
4522 int bufsize,
4523 int *buflen)
4524{
4525 int extra = new_slen - slen;
4526
4527 string[new_slen] = NUL;
4528 if (buf == NULL)
4529 {
4530 if (extra < 0)
4531 // remove matched chars, taking care of noremap
4532 del_typebuf(-extra, offset);
4533 else if (extra > 0)
4534 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004535 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4536 == FAIL)
4537 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004538
4539 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4540 // typebuf.tb_buf[]!
4541 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4542 (size_t)new_slen);
4543 }
4544 else
4545 {
4546 if (extra < 0)
4547 // remove matched characters
4548 mch_memmove(buf + offset, buf + offset - extra,
4549 (size_t)(*buflen + offset + extra));
4550 else if (extra > 0)
4551 {
4552 // Insert the extra space we need. If there is insufficient
4553 // space return -1.
4554 if (*buflen + extra + new_slen >= bufsize)
4555 return FAIL;
4556 mch_memmove(buf + offset + extra, buf + offset,
4557 (size_t)(*buflen - offset));
4558 }
4559 mch_memmove(buf + offset, string, (size_t)new_slen);
4560 *buflen = *buflen + extra + new_slen;
4561 }
4562 return OK;
4563}
4564
4565/*
4566 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4567 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004568 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004569decode_modifiers(int n)
4570{
4571 int code = n - 1;
4572 int modifiers = 0;
4573
4574 if (code & 1)
4575 modifiers |= MOD_MASK_SHIFT;
4576 if (code & 2)
4577 modifiers |= MOD_MASK_ALT;
4578 if (code & 4)
4579 modifiers |= MOD_MASK_CTRL;
4580 if (code & 8)
4581 modifiers |= MOD_MASK_META;
4582 return modifiers;
4583}
4584
4585 static int
4586modifiers2keycode(int modifiers, int *key, char_u *string)
4587{
4588 int new_slen = 0;
4589
4590 if (modifiers != 0)
4591 {
4592 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004593 // make mappings work. This may result in a special key, such as
4594 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004595 *key = simplify_key(*key, &modifiers);
4596 if (modifiers != 0)
4597 {
4598 string[new_slen++] = K_SPECIAL;
4599 string[new_slen++] = (int)KS_MODIFIER;
4600 string[new_slen++] = modifiers;
4601 }
4602 }
4603 return new_slen;
4604}
4605
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004606#ifdef FEAT_TERMRESPONSE
4607/*
4608 * Handle a cursor position report.
4609 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004610 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004611handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004612{
4613 if (arg[0] == 2 && arg[1] >= 2)
4614 {
4615 char *aw = NULL;
4616
4617 LOG_TR(("Received U7 status: %s", tp));
4618 u7_status.tr_progress = STATUS_GOT;
4619 did_cursorhold = TRUE;
4620 if (arg[1] == 2)
4621 aw = "single";
4622 else if (arg[1] == 3)
4623 aw = "double";
4624 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4625 {
4626 // Setting the option causes a screen redraw. Do
4627 // that right away if possible, keeping any
4628 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004629 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004630# ifdef DEBUG_TERMRESPONSE
4631 {
4632 int r = redraw_asap(CLEAR);
4633
4634 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4635 }
4636# else
4637 redraw_asap(CLEAR);
4638# endif
4639# ifdef FEAT_EVAL
4640 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4641# endif
4642 }
4643 }
4644 else if (arg[0] == 3)
4645 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004646 int value;
4647
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004648 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004649 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004650
4651 // Third row: xterm compatibility test.
4652 // If the cursor is on the first column then the terminal can handle
4653 // the request for cursor style and blinking.
4654 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4655 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4656 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004657 }
4658}
4659
4660/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004661 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004662 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004663 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004664 */
4665 static void
4666handle_version_response(int first, int *arg, int argc, char_u *tp)
4667{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004668 // The xterm version. It is set to zero when it can't be an actual xterm
4669 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004670 int version = arg[1];
4671
4672 LOG_TR(("Received CRV response: %s", tp));
4673 crv_status.tr_progress = STATUS_GOT;
4674 did_cursorhold = TRUE;
4675
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004676 // Reset terminal properties that are set based on the termresponse.
4677 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004678 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004679 init_term_props(
4680#ifdef FEAT_EVAL
4681 reset_term_props_on_termresponse
4682#else
4683 FALSE
4684#endif
4685 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004686
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004687 // If this code starts with CSI, you can bet that the
4688 // terminal uses 8-bit codes.
4689 if (tp[0] == CSI)
4690 switch_to_8bit();
4691
4692 // Screen sends 40500.
4693 // rxvt sends its version number: "20703" is 2.7.3.
4694 // Ignore it for when the user has set 'term' to xterm,
4695 // even though it's an rxvt.
4696 if (version > 20000)
4697 version = 0;
4698
zeertzjqfc78a032022-04-26 22:11:38 +01004699 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004700 if (first == '>' && argc == 3)
4701 {
4702 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004703
4704 // mintty 2.9.5 sends 77;20905;0c.
4705 // (77 is ASCII 'M' for mintty.)
4706 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004707 {
4708 // mintty can do SGR mouse reporting
4709 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4710 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004711
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004712 // If xterm version >= 141 try to get termcap codes. For other
4713 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004714 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004715 {
4716 LOG_TR(("Enable checking for XT codes"));
4717 check_for_codes = TRUE;
4718 need_gather = TRUE;
4719 req_codes_from_term();
4720 }
4721
4722 // libvterm sends 0;100;0
4723 if (version == 100 && arg[0] == 0 && arg[2] == 0)
4724 {
4725 // If run from Vim $COLORS is set to the number of
4726 // colors the terminal supports. Otherwise assume
4727 // 256, libvterm supports even more.
4728 if (mch_getenv((char_u *)"COLORS") == NULL)
4729 may_adjust_color_count(256);
4730 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004731 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004732 }
4733
4734 if (version == 95)
4735 {
4736 // Mac Terminal.app sends 1;95;0
4737 if (arg[0] == 1 && arg[2] == 0)
4738 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004739 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4740 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004741 }
4742 // iTerm2 sends 0;95;0
4743 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004744 {
4745 // iTerm2 can do SGR mouse reporting
4746 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4747 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004748 // old iTerm2 sends 0;95;
4749 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004750 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004751 }
4752
4753 // screen sends 83;40500;0 83 is 'S' in ASCII.
4754 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004755 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004756 // screen supports SGR mouse codes since 4.7.0
4757 if (arg[1] >= 40700)
4758 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4759 else
4760 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4761 }
4762
4763 // If no recognized terminal has set mouse behavior, assume xterm.
4764 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4765 {
4766 // Xterm version 277 supports SGR.
4767 // Xterm version >= 95 supports mouse dragging.
4768 if (version >= 277)
4769 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004770 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004771 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004772 }
4773
4774 // Detect terminals that set $TERM to something like
4775 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004776 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004777 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004778 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004779 // xfce4-terminal sends 1;2802;0.
4780 // screen sends 83;40500;0
4781 // Assuming any version number over 2500 is not an
4782 // xterm (without the limit for rxvt and screen).
4783 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004784 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004785
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004786 else if (version == 136 && arg[2] == 0)
4787 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004788 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004789
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004790 // PuTTY sends 0;136;0
4791 if (arg[0] == 0)
4792 {
4793 // supports sgr-like mouse reporting.
4794 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4795 }
4796 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004797 }
4798
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004799 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
4800 // commented out.
4801 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
4802 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004803
4804 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004805 // 30600/40500 is a version number of GNU screen. DA2 support is added
4806 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4807 // compatibility checking does not detect GNU screen.
4808 if (arg[0] == 83 && arg[1] >= 30600)
4809 {
4810 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4811 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4812 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004813
4814 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004815 // 95, so assume anything below 95 is not xterm and hopefully supports
4816 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004817 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004818 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004819
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004820 // Getting the cursor style is only supported properly by xterm since
4821 // version 279 (otherwise it returns 0x18).
4822 if (version < 279)
4823 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4824
4825 /*
4826 * Take action on the detected properties.
4827 */
4828
4829 // Unless the underline RGB color is expected to work, disable "t_8u".
4830 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004831 // This may cause some flicker. Alternative would be to set "t_8u"
4832 // here if the terminal is expected to support it, but that might
4833 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01004834 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
4835 && *T_8U != NUL
4836 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004837 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004838 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4839 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004840 }
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01004841 if (*T_8U != NUL && write_t_8u_state == MAYBE)
4842 // Did skip writing t_8u, a complete redraw is needed.
4843 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01004844 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004845
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004846 // Only set 'ttymouse' automatically if it was not set
4847 // by the user already.
4848 if (!option_was_set((char_u *)"ttym")
4849 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4850 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4851 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004852 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004853 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4854 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4855 }
4856
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004857 // Only request the cursor style if t_SH and t_RS are
4858 // set. Only supported properly by xterm since version
4859 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004860 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004861 // Not for Terminal.app, it can't handle t_RS, it
4862 // echoes the characters to the screen.
4863 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004864 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004865 && *T_CSH != NUL
4866 && *T_CRS != NUL)
4867 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02004868#ifdef FEAT_JOB_CHANNEL
4869 ch_log_output = TRUE;
4870#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004871 LOG_TR(("Sending cursor style request"));
4872 out_str(T_CRS);
4873 termrequest_sent(&rcs_status);
4874 need_flush = TRUE;
4875 }
4876
4877 // Only request the cursor blink mode if t_RC set. Not
4878 // for Gnome terminal, it can't handle t_RC, it
4879 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004880 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004881 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004882 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004883 && *T_CRC != NUL)
4884 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02004885#ifdef FEAT_JOB_CHANNEL
4886 ch_log_output = TRUE;
4887#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004888 LOG_TR(("Sending cursor blink mode request"));
4889 out_str(T_CRC);
4890 termrequest_sent(&rbm_status);
4891 need_flush = TRUE;
4892 }
4893
4894 if (need_flush)
4895 out_flush();
4896 }
4897}
4898
4899/*
4900 * Handle a sequence with key and modifier, one of:
4901 * {lead}27;{modifier};{key}~
4902 * {lead}{key};{modifier}u
4903 * Returns the difference in length.
4904 */
4905 static int
4906handle_key_with_modifier(
4907 int *arg,
4908 int trail,
4909 int csi_len,
4910 int offset,
4911 char_u *buf,
4912 int bufsize,
4913 int *buflen)
4914{
4915 int key;
4916 int modifiers;
4917 int new_slen;
4918 char_u string[MAX_KEY_CODE_LEN + 1];
4919
4920 seenModifyOtherKeys = TRUE;
4921 if (trail == 'u')
4922 key = arg[0];
4923 else
4924 key = arg[2];
4925
4926 modifiers = decode_modifiers(arg[1]);
4927
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02004928 // Some keys need adjustment when the Ctrl modifier is used.
4929 key = may_adjust_key_for_ctrl(modifiers, key);
4930
Bram Moolenaaref6746f2020-06-20 14:43:23 +02004931 // May remove the shift modifier if it's already included in the key.
4932 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004933
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004934 // insert modifiers with KS_MODIFIER
4935 new_slen = modifiers2keycode(modifiers, &key, string);
4936
Bram Moolenaar749bc952020-10-31 16:33:47 +01004937 if (IS_SPECIAL(key))
4938 {
4939 string[new_slen++] = K_SPECIAL;
4940 string[new_slen++] = KEY2TERMCAP0(key);
4941 string[new_slen++] = KEY2TERMCAP1(key);
4942 }
4943 else if (has_mbyte)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004944 new_slen += (*mb_char2bytes)(key, string + new_slen);
4945 else
4946 string[new_slen++] = key;
4947
4948 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4949 buf, bufsize, buflen) == FAIL)
4950 return -1;
4951 return new_slen - csi_len + offset;
4952}
4953
4954/*
4955 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004956 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004957 *
4958 * - Cursor position report: {lead}{row};{col}R
4959 * The final byte must be 'R'. It is used for checking the
4960 * ambiguous-width character state.
4961 *
4962 * - window position reply: {lead}3;{x};{y}t
4963 *
4964 * - key with modifiers when modifyOtherKeys is enabled:
4965 * {lead}27;{modifier};{key}~
4966 * {lead}{key};{modifier}u
4967 * Return 0 for no match, -1 for partial match, > 0 for full match.
4968 */
4969 static int
4970handle_csi(
4971 char_u *tp,
4972 int len,
4973 char_u *argp,
4974 int offset,
4975 char_u *buf,
4976 int bufsize,
4977 int *buflen,
4978 char_u *key_name,
4979 int *slen)
4980{
4981 int first = -1; // optional char right after {lead}
4982 int trail; // char that ends CSI sequence
4983 int arg[3] = {-1, -1, -1}; // argument numbers
4984 int argc; // number of arguments
4985 char_u *ap = argp;
4986 int csi_len;
4987
4988 // Check for non-digit after CSI.
4989 if (!VIM_ISDIGIT(*ap))
4990 first = *ap++;
4991
4992 // Find up to three argument numbers.
4993 for (argc = 0; argc < 3; )
4994 {
4995 if (ap >= tp + len)
4996 return -1;
4997 if (*ap == ';')
4998 arg[argc++] = -1; // omitted number
4999 else if (VIM_ISDIGIT(*ap))
5000 {
5001 arg[argc] = 0;
5002 for (;;)
5003 {
5004 if (ap >= tp + len)
5005 return -1;
5006 if (!VIM_ISDIGIT(*ap))
5007 break;
5008 arg[argc] = arg[argc] * 10 + (*ap - '0');
5009 ++ap;
5010 }
5011 ++argc;
5012 }
5013 if (*ap == ';')
5014 ++ap;
5015 else
5016 break;
5017 }
5018
5019 // mrxvt has been reported to have "+" in the version. Assume
5020 // the escape sequence ends with a letter or one of "{|}~".
5021 while (ap < tp + len
5022 && !(*ap >= '{' && *ap <= '~')
5023 && !ASCII_ISALPHA(*ap))
5024 ++ap;
5025 if (ap >= tp + len)
5026 return -1;
5027 trail = *ap;
5028 csi_len = (int)(ap - tp) + 1;
5029
5030 // Cursor position report: Eat it when there are 2 arguments
5031 // and it ends in 'R'. Also when u7_status is not "sent", it
5032 // may be from a previous Vim that just exited. But not for
5033 // <S-F3>, it sends something similar, check for row and column
5034 // to make sense.
5035 if (first == -1 && argc == 2 && trail == 'R')
5036 {
5037 handle_u7_response(arg, tp, csi_len);
5038
5039 key_name[0] = (int)KS_EXTRA;
5040 key_name[1] = (int)KE_IGNORE;
5041 *slen = csi_len;
5042 }
5043
5044 // Version string: Eat it when there is at least one digit and
5045 // it ends in 'c'
5046 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5047 {
5048 handle_version_response(first, arg, argc, tp);
5049
5050 *slen = csi_len;
5051# ifdef FEAT_EVAL
5052 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
5053# endif
5054 apply_autocmds(EVENT_TERMRESPONSE,
5055 NULL, NULL, FALSE, curbuf);
5056 key_name[0] = (int)KS_EXTRA;
5057 key_name[1] = (int)KE_IGNORE;
5058 }
5059
5060 // Check blinking cursor from xterm:
5061 // {lead}?12;1$y set
5062 // {lead}?12;2$y not set
5063 //
5064 // {lead} can be <Esc>[ or CSI
5065 else if (rbm_status.tr_progress == STATUS_SENT
5066 && first == '?'
5067 && ap == argp + 6
5068 && arg[0] == 12
5069 && ap[-1] == '$'
5070 && trail == 'y')
5071 {
5072 initial_cursor_blink = (arg[1] == '1');
5073 rbm_status.tr_progress = STATUS_GOT;
5074 LOG_TR(("Received cursor blinking mode response: %s", tp));
5075 key_name[0] = (int)KS_EXTRA;
5076 key_name[1] = (int)KE_IGNORE;
5077 *slen = csi_len;
5078# ifdef FEAT_EVAL
5079 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
5080# endif
5081 }
5082
5083 // Check for a window position response from the terminal:
5084 // {lead}3;{x};{y}t
5085 else if (did_request_winpos && argc == 3 && arg[0] == 3
5086 && trail == 't')
5087 {
5088 winpos_x = arg[1];
5089 winpos_y = arg[2];
5090 // got finished code: consume it
5091 key_name[0] = (int)KS_EXTRA;
5092 key_name[1] = (int)KE_IGNORE;
5093 *slen = csi_len;
5094
5095 if (--did_request_winpos <= 0)
5096 winpos_status.tr_progress = STATUS_GOT;
5097 }
5098
5099 // Key with modifier:
5100 // {lead}27;{modifier};{key}~
5101 // {lead}{key};{modifier}u
5102 else if ((arg[0] == 27 && argc == 3 && trail == '~')
5103 || (argc == 2 && trail == 'u'))
5104 {
5105 return len + handle_key_with_modifier(arg, trail,
5106 csi_len, offset, buf, bufsize, buflen);
5107 }
5108
5109 // else: Unknown CSI sequence. We could drop it, but then the
5110 // user can't create a map for it.
5111 return 0;
5112}
5113
5114/*
5115 * Handle an OSC sequence, fore/background color response from the terminal:
5116 *
5117 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5118 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5119 *
5120 * {code} is 10 for foreground, 11 for background
5121 * {lead} can be <Esc>] or OSC
5122 * {tail} can be '\007', <Esc>\ or STERM.
5123 *
5124 * Consume any code that starts with "{lead}11;", it's also
5125 * possible that "rgba" is following.
5126 */
5127 static int
5128handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5129{
5130 int i, j;
5131
5132 j = 1 + (tp[0] == ESC);
5133 if (len >= j + 3 && (argp[0] != '1'
5134 || (argp[1] != '1' && argp[1] != '0')
5135 || argp[2] != ';'))
5136 i = 0; // no match
5137 else
5138 for (i = j; i < len; ++i)
5139 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5140 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5141 {
5142 int is_bg = argp[1] == '1';
5143 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5144 && tp[j + 16] == '/';
5145
5146 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5147 && (is_4digit
5148 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5149 {
5150 char_u *tp_r = tp + j + 7;
5151 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5152 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
5153# ifdef FEAT_TERMINAL
5154 int rval, gval, bval;
5155
5156 rval = hexhex2nr(tp_r);
5157 gval = hexhex2nr(tp_b);
5158 bval = hexhex2nr(tp_g);
5159# endif
5160 if (is_bg)
5161 {
5162 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5163 *tp_b) ? "light" : "dark";
5164
5165 LOG_TR(("Received RBG response: %s", tp));
5166 rbg_status.tr_progress = STATUS_GOT;
5167# ifdef FEAT_TERMINAL
5168 bg_r = rval;
5169 bg_g = gval;
5170 bg_b = bval;
5171# endif
5172 if (!option_was_set((char_u *)"bg")
5173 && STRCMP(p_bg, new_bg_val) != 0)
5174 {
5175 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005176 set_option_value_give_err((char_u *)"bg",
5177 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005178 reset_option_was_set((char_u *)"bg");
5179 redraw_asap(CLEAR);
5180 }
5181 }
5182# ifdef FEAT_TERMINAL
5183 else
5184 {
5185 LOG_TR(("Received RFG response: %s", tp));
5186 rfg_status.tr_progress = STATUS_GOT;
5187 fg_r = rval;
5188 fg_g = gval;
5189 fg_b = bval;
5190 }
5191# endif
5192 }
5193
5194 // got finished code: consume it
5195 key_name[0] = (int)KS_EXTRA;
5196 key_name[1] = (int)KE_IGNORE;
5197 *slen = i + 1 + (tp[i] == ESC);
5198# ifdef FEAT_EVAL
5199 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5200 : VV_TERMRFGRESP, tp, *slen);
5201# endif
5202 break;
5203 }
5204 if (i == len)
5205 {
5206 LOG_TR(("not enough characters for RB"));
5207 return FAIL;
5208 }
5209 return OK;
5210}
5211
5212/*
5213 * Check for key code response from xterm:
5214 * {lead}{flag}+r<hex bytes><{tail}
5215 *
5216 * {lead} can be <Esc>P or DCS
5217 * {flag} can be '0' or '1'
5218 * {tail} can be Esc>\ or STERM
5219 *
5220 * Check for cursor shape response from xterm:
5221 * {lead}1$r<digit> q{tail}
5222 *
5223 * {lead} can be <Esc>P or DCS
5224 * {tail} can be <Esc>\ or STERM
5225 *
5226 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5227 */
5228 static int
5229handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5230{
5231 int i, j;
5232
5233 j = 1 + (tp[0] == ESC);
5234 if (len < j + 3)
5235 i = len; // need more chars
5236 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
5237 i = 0; // no match
5238 else if (argp[1] == '+')
5239 // key code response
5240 for (i = j; i < len; ++i)
5241 {
5242 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5243 || tp[i] == STERM)
5244 {
5245 if (i - j >= 3)
5246 got_code_from_term(tp + j, i);
5247 key_name[0] = (int)KS_EXTRA;
5248 key_name[1] = (int)KE_IGNORE;
5249 *slen = i + 1 + (tp[i] == ESC);
5250 break;
5251 }
5252 }
5253 else
5254 {
5255 // Probably the cursor shape response. Make sure that "i"
5256 // is equal to "len" when there are not sufficient
5257 // characters.
5258 for (i = j + 3; i < len; ++i)
5259 {
5260 if (i - j == 3 && !isdigit(tp[i]))
5261 break;
5262 if (i - j == 4 && tp[i] != ' ')
5263 break;
5264 if (i - j == 5 && tp[i] != 'q')
5265 break;
5266 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5267 break;
5268 if ((i - j == 6 && tp[i] == STERM)
5269 || (i - j == 7 && tp[i] == '\\'))
5270 {
5271 int number = argp[3] - '0';
5272
5273 // 0, 1 = block blink, 2 = block
5274 // 3 = underline blink, 4 = underline
5275 // 5 = vertical bar blink, 6 = vertical bar
5276 number = number == 0 ? 1 : number;
5277 initial_cursor_shape = (number + 1) / 2;
5278 // The blink flag is actually inverted, compared to
5279 // the value set with T_SH.
5280 initial_cursor_shape_blink =
5281 (number & 1) ? FALSE : TRUE;
5282 rcs_status.tr_progress = STATUS_GOT;
5283 LOG_TR(("Received cursor shape response: %s", tp));
5284
5285 key_name[0] = (int)KS_EXTRA;
5286 key_name[1] = (int)KE_IGNORE;
5287 *slen = i + 1;
5288# ifdef FEAT_EVAL
5289 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5290# endif
5291 break;
5292 }
5293 }
5294 }
5295
5296 if (i == len)
5297 {
5298 // These codes arrive many together, each code can be
5299 // truncated at any point.
5300 LOG_TR(("not enough characters for XT"));
5301 return FAIL;
5302 }
5303 return OK;
5304}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005305#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005306
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 * Check if typebuf.tb_buf[] contains a terminal key code.
5309 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005310 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005312 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 * With a match, the match is removed, the replacement code is inserted in
5314 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5315 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005316 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5317 * "buflen" is then the length of the string in buf[] and is updated for
5318 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 */
5320 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005321check_termcode(
5322 int max_offset,
5323 char_u *buf,
5324 int bufsize,
5325 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326{
5327 char_u *tp;
5328 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005329 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005330 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005332 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 int offset;
5334 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005335 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005336 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005337 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005338 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 char_u string[MAX_KEY_CODE_LEN + 1];
5340 int i, j;
5341 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
5344 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5345
5346 /*
5347 * Speed up the checks for terminal codes by gathering all first bytes
5348 * used in termleader[]. Often this is just a single <Esc>.
5349 */
5350 if (need_gather)
5351 gather_termleader();
5352
5353 /*
5354 * Check at several positions in typebuf.tb_buf[], to catch something like
5355 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5356 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005357 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 * This is used often, KEEP IT FAST!
5359 */
5360 for (offset = 0; offset < max_offset; ++offset)
5361 {
5362 if (buf == NULL)
5363 {
5364 if (offset >= typebuf.tb_len)
5365 break;
5366 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005367 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
5369 else
5370 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005371 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 break;
5373 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005374 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 }
5376
5377 /*
5378 * Don't check characters after K_SPECIAL, those are already
5379 * translated terminal chars (avoid translating ~@^Hx).
5380 */
5381 if (*tp == K_SPECIAL)
5382 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005383 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 continue;
5385 }
5386
5387 /*
5388 * Skip this position if the character does not appear as the first
5389 * character in term_strings. This speeds up a lot, since most
5390 * termcodes start with the same character (ESC or CSI).
5391 */
5392 i = *tp;
5393 for (p = termleader; *p && *p != i; ++p)
5394 ;
5395 if (*p == NUL)
5396 continue;
5397
5398 /*
5399 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5400 * are in Insert mode.
5401 */
5402 if (*tp == ESC && !p_ek && (State & INSERT))
5403 continue;
5404
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005405 key_name[0] = NUL; // no key name found yet
5406 key_name[1] = NUL; // no key name found yet
5407 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408
5409#ifdef FEAT_GUI
5410 if (gui.in_use)
5411 {
5412 /*
5413 * GUI special key codes are all of the form [CSI xx].
5414 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005415 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
5417 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005418 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 slen = 3;
5420 key_name[0] = tp[1];
5421 key_name[1] = tp[2];
5422 }
5423 }
5424 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005425#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005427 int mouse_index_found = -1;
5428
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 for (idx = 0; idx < tc_len; ++idx)
5430 {
5431 /*
5432 * Ignore the entry if we are not at the start of
5433 * typebuf.tb_buf[]
5434 * and there are not enough characters to make a match.
5435 * But only when the 'K' flag is in 'cpoptions'.
5436 */
5437 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005438 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 if (cpo_koffset && offset && len < slen)
5440 continue;
5441 if (STRNCMP(termcodes[idx].code, tp,
5442 (size_t)(slen > len ? len : slen)) == 0)
5443 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005444 int looks_like_mouse_start = FALSE;
5445
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005446 if (len < slen) // got a partial sequence
5447 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448
5449 /*
5450 * When found a keypad key, check if there is another key
5451 * that matches and use that one. This makes <Home> to be
5452 * found instead of <kHome> when they produce the same
5453 * key code.
5454 */
5455 if (termcodes[idx].name[0] == 'K'
5456 && VIM_ISDIGIT(termcodes[idx].name[1]))
5457 {
5458 for (j = idx + 1; j < tc_len; ++j)
5459 if (termcodes[j].len == slen &&
5460 STRNCMP(termcodes[idx].code,
5461 termcodes[j].code, slen) == 0)
5462 {
5463 idx = j;
5464 break;
5465 }
5466 }
5467
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005468 if (slen == 2 && len > 2
5469 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005470 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005471 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005472 // The mouse termcode "ESC [" is also the prefix of
5473 // "ESC [ I" (focus gained) and other keys. Check some
5474 // more bytes to find out.
5475 if (!isdigit(tp[2]))
5476 {
5477 // ESC [ without number following: Only use it when
5478 // there is no other match.
5479 looks_like_mouse_start = TRUE;
5480 }
5481 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5482 {
5483 char_u *nr = tp + 2;
5484 int count = 0;
5485
5486 // If a digit is following it could be a key with
5487 // modifier, e.g., ESC [ 1;2P. Can be confused
5488 // with DEC_MOUSE, which requires four numbers
5489 // following. If not then it can't be a DEC_MOUSE
5490 // code.
5491 for (;;)
5492 {
5493 ++count;
5494 (void)getdigits(&nr);
5495 if (nr >= tp + len)
5496 return -1; // partial sequence
5497 if (*nr != ';')
5498 break;
5499 ++nr;
5500 if (nr >= tp + len)
5501 return -1; // partial sequence
5502 }
5503 if (count < 4)
5504 continue; // no match
5505 }
5506 }
5507 if (looks_like_mouse_start)
5508 {
5509 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005510 if (mouse_index_found < 0)
5511 mouse_index_found = idx;
5512 }
5513 else
5514 {
5515 key_name[0] = termcodes[idx].name[0];
5516 key_name[1] = termcodes[idx].name[1];
5517 break;
5518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005520
5521 /*
5522 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005523 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005524 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005525 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5526 * When there is a modifier the * matches a number.
5527 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005528 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005529 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005530 {
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005531 int at_code;
5532
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005533 modslen = termcodes[idx].modlen;
5534 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005535 continue;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005536 at_code = termcodes[idx].code[modslen] == '@';
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005537 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005538 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005539 {
5540 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005541
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005542 if (len <= modslen) // got a partial sequence
5543 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005544
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005545 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005546 // no modifiers
5547 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005548 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005549 // no match for "code;*X" with "code;"
5550 continue;
5551 else if (at_code && tp[modslen] != '1')
5552 // no match for "<Esc>[@" with "<Esc>[1"
5553 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005554 else
5555 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005556 // Skip over the digits, the final char must
5557 // follow. URXVT can use a negative value, thus
5558 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005559 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005560 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005561 ;
5562 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005563 if (len < j) // got a partial sequence
5564 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005565 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005566 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005567
Bram Moolenaara529ce02017-06-22 22:37:57 +02005568 modifiers_start = tp + slen - 2;
5569
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005570 // Match! Convert modifier bits.
5571 n = atoi((char *)modifiers_start);
5572 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005573
5574 slen = j;
5575 }
5576 key_name[0] = termcodes[idx].name[0];
5577 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005578 break;
5579 }
5580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005582 if (idx == tc_len && mouse_index_found >= 0)
5583 {
5584 key_name[0] = termcodes[mouse_index_found].name[0];
5585 key_name[1] = termcodes[mouse_index_found].name[1];
5586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
5588
5589#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005590 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005591 // Mouse codes of DEC and pterm start with <ESC>[. When
5592 // detecting the start of these mouse codes they might as well be
5593 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005594# ifdef FEAT_MOUSE_DEC
5595 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005596# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005597# ifdef FEAT_MOUSE_PTERM
5598 || key_name[0] == KS_PTERM_MOUSE
5599# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005600 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005602 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5603
5604 /*
5605 * Check for responses from the terminal starting with {lead}:
5606 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005607 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005608 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005609 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005610 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005611 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005612 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005613 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005614 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005615 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005616 * - window position reply: {lead}3;{x};{y}t
5617 *
5618 * - key with modifiers when modifyOtherKeys is enabled:
5619 * {lead}27;{modifier};{key}~
5620 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005621 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005622 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005623 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005624 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005626 int resp = handle_csi(tp, len, argp, offset, buf,
5627 bufsize, buflen, key_name, &slen);
5628 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005629 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005630# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005631 if (resp == -1)
5632 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005633# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005634 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005635 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005636 }
5637
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005638 // Check for fore/background color response from the terminal,
5639 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005640 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005641 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5642 || tp[0] == OSC))
5643 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005644 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005645 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 }
5647
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005648 // Check for key code response from xterm,
5649 // starting with <Esc>P or DCS
Bram Moolenaarafd78262019-05-10 23:10:31 +02005650 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005651 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 || tp[0] == DCS))
5653 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005654 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005655 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 }
5657 }
5658#endif
5659
5660 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005661 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005663 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005665#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 /*
5667 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5668 * so that we know which window to scroll later.
5669 */
5670 if (gui.in_use
5671 && key_name[0] == (int)KS_EXTRA
5672 && (key_name[1] == (int)KE_X1MOUSE
5673 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005674 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005675 || key_name[1] == (int)KE_MOUSELEFT
5676 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 || key_name[1] == (int)KE_MOUSEDOWN
5678 || key_name[1] == (int)KE_MOUSEUP))
5679 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005680 char_u bytes[6];
5681 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5682
5683 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 return -1;
5685 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5686 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5687 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005688 // equal to K_MOUSEMOVE
5689 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
5690 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 /*
5695 * If it is a mouse click, get the coordinates.
5696 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005697 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005698#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005699 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005700#endif
5701#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005702 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005703#endif
5704#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005705 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005706#endif
5707#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005708 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005709#endif
5710#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005711 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005712#endif
5713#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005714 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005715#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005716 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005717 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005719 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5720 &modifiers) == -1)
5721 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
5724#ifdef FEAT_GUI
5725 /*
5726 * If using the GUI, then we get menu and scrollbar events.
5727 *
5728 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5729 * four bytes which are to be taken as a pointer to the vimmenu_T
5730 * structure.
5731 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005732 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005733 * is one byte with the tab index.
5734 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5736 * by one byte representing the scrollbar number, and then four bytes
5737 * representing a long_u which is the new value of the scrollbar.
5738 *
5739 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5740 * KE_FILLER followed by four bytes representing a long_u which is the
5741 * new value of the scrollbar.
5742 */
5743# ifdef FEAT_MENU
5744 else if (key_name[0] == (int)KS_MENU)
5745 {
5746 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005747 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 if (num_bytes == -1)
5750 return -1;
5751 current_menu = (vimmenu_T *)val;
5752 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005753
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005754 // The menu may have been deleted right after it was used, check
5755 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005756 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5757 {
5758 key_name[0] = KS_EXTRA;
5759 key_name[1] = (int)KE_IGNORE;
5760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
5762# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005763# ifdef FEAT_GUI_TABLINE
5764 else if (key_name[0] == (int)KS_TABLINE)
5765 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005766 // Selecting tabline tab or using its menu.
5767 char_u bytes[6];
5768 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5769
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005770 if (num_bytes == -1)
5771 return -1;
5772 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005773 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005774 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005775 slen += num_bytes;
5776 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005777 else if (key_name[0] == (int)KS_TABMENU)
5778 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005779 // Selecting tabline tab or using its menu.
5780 char_u bytes[6];
5781 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5782
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005783 if (num_bytes == -1)
5784 return -1;
5785 current_tab = (int)bytes[0];
5786 current_tabmenu = (int)bytes[1];
5787 slen += num_bytes;
5788 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005789# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790# ifndef USE_ON_FLY_SCROLL
5791 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5792 {
5793 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005794 char_u bytes[6];
5795 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005797 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 j = 0;
5799 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5800 && tp[j + 2] != NUL; ++i)
5801 {
5802 j += 3;
5803 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5804 if (num_bytes == -1)
5805 break;
5806 if (i == 0)
5807 current_scrollbar = (int)bytes[0];
5808 else if (current_scrollbar != (int)bytes[0])
5809 break;
5810 j += num_bytes;
5811 num_bytes = get_long_from_buf(tp + j, &val);
5812 if (num_bytes == -1)
5813 break;
5814 scrollbar_value = val;
5815 j += num_bytes;
5816 slen = j;
5817 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005818 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 return -1;
5820 }
5821 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5822 {
5823 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005824 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005826 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 j = 0;
5828 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5829 && tp[j + 2] != NUL; ++i)
5830 {
5831 j += 3;
5832 num_bytes = get_long_from_buf(tp + j, &val);
5833 if (num_bytes == -1)
5834 break;
5835 scrollbar_value = val;
5836 j += num_bytes;
5837 slen = j;
5838 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005839 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 return -1;
5841 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005842# endif // !USE_ON_FLY_SCROLL
5843#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005845#if (defined(UNIX) || defined(VMS))
5846 /*
5847 * Handle FocusIn/FocusOut event sequences reported by XTerm.
5848 * (CSI I/CSI O)
5849 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00005850 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005851# ifdef FEAT_GUI
5852 && !gui.in_use
5853# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005854 )
5855 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005856 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005857 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005858 if (!focus_state)
5859 {
5860 ui_focus_change(TRUE);
5861 did_cursorhold = TRUE;
5862 focus_state = TRUE;
5863 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005864 key_name[1] = (int)KE_IGNORE;
5865 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01005866 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005867 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005868 if (focus_state)
5869 {
5870 ui_focus_change(FALSE);
5871 did_cursorhold = TRUE;
5872 focus_state = FALSE;
5873 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005874 key_name[1] = (int)KE_IGNORE;
5875 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005876 }
5877#endif
5878
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005879 /*
5880 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5881 */
5882 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5883
5884 /*
5885 * Add any modifier codes to our string.
5886 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005887 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005888
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005889 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005890 key_name[0] = KEY2TERMCAP0(key);
5891 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005893 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005894 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005895 if (has_mbyte)
5896 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5897 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005898 string[new_slen++] = key_name[1];
5899 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005900 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5901 && key_name[1] == KE_IGNORE)
5902 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005903 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5904 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005905 retval = KEYLEN_REMOVED;
5906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 else
5908 {
5909 string[new_slen++] = K_SPECIAL;
5910 string[new_slen++] = key_name[0];
5911 string[new_slen++] = key_name[1];
5912 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005913 if (put_string_in_typebuf(offset, slen, string, new_slen,
5914 buf, bufsize, buflen) == FAIL)
5915 return -1;
5916 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 }
5918
Bram Moolenaar2951b772013-07-03 12:45:31 +02005919#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005920 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005921#endif
5922
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005923 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924}
5925
Bram Moolenaar9377df32017-10-15 13:22:01 +02005926#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005927/*
5928 * Get the text foreground color, if known.
5929 */
5930 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005931term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005932{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005933 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005934 {
5935 *r = fg_r;
5936 *g = fg_g;
5937 *b = fg_b;
5938 }
5939}
5940
5941/*
5942 * Get the text background color, if known.
5943 */
5944 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005945term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005946{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005947 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005948 {
5949 *r = bg_r;
5950 *g = bg_g;
5951 *b = bg_b;
5952 }
5953}
5954#endif
5955
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956/*
5957 * Replace any terminal code strings in from[] with the equivalent internal
5958 * vim representation. This is used for the "from" and "to" part of a
5959 * mapping, and the "to" part of a menu command.
5960 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5961 * '<'.
5962 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5963 *
5964 * The replacement is done in result[] and finally copied into allocated
5965 * memory. If this all works well *bufp is set to the allocated memory and a
5966 * pointer to it is returned. If something fails *bufp is set to NULL and from
5967 * is returned.
5968 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005969 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5970 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5971 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5972 * used instead of a CTRL-V.
5973 *
5974 * Flags:
5975 * REPTERM_FROM_PART see above
5976 * REPTERM_DO_LT also translate <lt>
5977 * REPTERM_SPECIAL always accept <key> notation
5978 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5979 *
5980 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5981 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005983 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005984replace_termcodes(
5985 char_u *from,
5986 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005987 int flags,
5988 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
5990 int i;
5991 int slen;
5992 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01005993 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005995 int do_backslash; // backslash is a special character
5996 int do_special; // recognize <> key codes
5997 int do_key_code; // recognize raw key codes
5998 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01005999 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000
6001 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006002 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6003 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006005 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006
6007 /*
6008 * Allocate space for the translation. Worst case a single character is
6009 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006010 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006012 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006013 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 {
6015 *bufp = NULL;
6016 return from;
6017 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006018 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019
6020 /*
6021 * Check for #n at start only: function key n
6022 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006023 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 {
6025 result[dlen++] = K_SPECIAL;
6026 result[dlen++] = 'k';
6027 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006028 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006030 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 src += 2;
6032 }
6033
6034 /*
6035 * Copy each byte from *from to result[dlen]
6036 */
6037 while (*src != NUL)
6038 {
6039 /*
6040 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006041 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006043 if (do_special && ((flags & REPTERM_DO_LT)
6044 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 {
6046#ifdef FEAT_EVAL
6047 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006048 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6049 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006051 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6052 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 */
6054 if (STRNICMP(src, "<SID>", 5) == 0)
6055 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006056 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006057 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 else
6059 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006060 char_u *dot;
6061 long sid = current_sctx.sc_sid;
6062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006064 if (in_vim9script()
6065 && (dot = vim_strchr(src, '.')) != NULL)
6066 {
6067 imported_T *imp = find_imported(src, dot - src, FALSE);
6068
6069 if (imp != NULL)
6070 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006071 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6072 size_t len;
6073
Bram Moolenaar89445512022-04-14 12:58:23 +01006074 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006075 if (si->sn_autoload_prefix != NULL)
6076 {
6077 // Turn "<SID>name.Func"
6078 // into "scriptname#Func".
6079 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006080 if (ga_grow(&ga,
6081 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006082 {
6083 ga_clear(&ga);
6084 *bufp = NULL;
6085 return from;
6086 }
6087 result = ga.ga_data;
6088 STRCPY(result + dlen, si->sn_autoload_prefix);
6089 dlen += len;
6090 continue;
6091 }
6092 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006093 }
6094 }
6095
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 result[dlen++] = K_SPECIAL;
6097 result[dlen++] = (int)KS_EXTRA;
6098 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006099 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006100 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 result[dlen++] = '_';
6102 continue;
6103 }
6104 }
6105#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006106 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6107 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
zeertzjqdb088872022-05-02 22:53:45 +01006108 TRUE, did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 if (slen)
6110 {
6111 dlen += slen;
6112 continue;
6113 }
6114 }
6115
6116 /*
6117 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6118 * Note that this is also checked after replacing the <> form.
6119 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6120 * it could be a character in the file.
6121 */
6122 if (do_key_code)
6123 {
6124 i = find_term_bykeys(src);
6125 if (i >= 0)
6126 {
6127 result[dlen++] = K_SPECIAL;
6128 result[dlen++] = termcodes[i].name[0];
6129 result[dlen++] = termcodes[i].name[1];
6130 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006131 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 continue;
6133 }
6134 }
6135
6136#ifdef FEAT_EVAL
6137 if (do_special)
6138 {
6139 char_u *p, *s, len;
6140
6141 /*
6142 * Replace <Leader> by the value of "mapleader".
6143 * Replace <LocalLeader> by the value of "maplocalleader".
6144 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6145 */
6146 if (STRNICMP(src, "<Leader>", 8) == 0)
6147 {
6148 len = 8;
6149 p = get_var_value((char_u *)"g:mapleader");
6150 }
6151 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6152 {
6153 len = 13;
6154 p = get_var_value((char_u *)"g:maplocalleader");
6155 }
6156 else
6157 {
6158 len = 0;
6159 p = NULL;
6160 }
6161 if (len != 0)
6162 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006163 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6165 s = (char_u *)"\\";
6166 else
6167 s = p;
6168 while (*s != NUL)
6169 result[dlen++] = *s++;
6170 src += len;
6171 continue;
6172 }
6173 }
6174#endif
6175
6176 /*
6177 * Remove CTRL-V and ignore the next character.
6178 * For "from" side the CTRL-V at the end is included, for the "to"
6179 * part it is removed.
6180 * If 'cpoptions' does not contain 'B', also accept a backslash.
6181 */
6182 key = *src;
6183 if (key == Ctrl_V || (do_backslash && key == '\\'))
6184 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006185 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 if (*src == NUL)
6187 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006188 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 result[dlen++] = key;
6190 break;
6191 }
6192 }
6193
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006194 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006195 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
6197 /*
6198 * If the character is K_SPECIAL, replace it with K_SPECIAL
6199 * KS_SPECIAL KE_FILLER.
6200 * If compiled with the GUI replace CSI with K_CSI.
6201 */
6202 if (*src == K_SPECIAL)
6203 {
6204 result[dlen++] = K_SPECIAL;
6205 result[dlen++] = KS_SPECIAL;
6206 result[dlen++] = KE_FILLER;
6207 }
6208# ifdef FEAT_GUI
6209 else if (*src == CSI)
6210 {
6211 result[dlen++] = K_SPECIAL;
6212 result[dlen++] = KS_EXTRA;
6213 result[dlen++] = (int)KE_CSI;
6214 }
6215# endif
6216 else
6217 result[dlen++] = *src;
6218 ++src;
6219 }
6220 }
6221 result[dlen] = NUL;
6222
6223 /*
6224 * Copy the new string to allocated memory.
6225 * If this fails, just return from.
6226 */
6227 if ((*bufp = vim_strsave(result)) != NULL)
6228 from = *bufp;
6229 vim_free(result);
6230 return from;
6231}
6232
6233/*
6234 * Find a termcode with keys 'src' (must be NUL terminated).
6235 * Return the index in termcodes[], or -1 if not found.
6236 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006237 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006238find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239{
6240 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006241 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242
6243 for (i = 0; i < tc_len; ++i)
6244 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006245 if (slen == termcodes[i].len
6246 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 return i;
6248 }
6249 return -1;
6250}
6251
6252/*
6253 * Gather the first characters in the terminal key codes into a string.
6254 * Used to speed up check_termcode().
6255 */
6256 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006257gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258{
6259 int i;
6260 int len = 0;
6261
6262#ifdef FEAT_GUI
6263 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006264 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265#endif
6266#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006267 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006268 termleader[len++] = DCS; // the termcode response starts with DCS
6269 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270#endif
6271 termleader[len] = NUL;
6272
6273 for (i = 0; i < tc_len; ++i)
6274 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6275 {
6276 termleader[len++] = termcodes[i].code[0];
6277 termleader[len] = NUL;
6278 }
6279
6280 need_gather = FALSE;
6281}
6282
6283/*
6284 * Show all termcodes (for ":set termcap")
6285 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006286 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 */
6288 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006289show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 int col;
6292 int *items;
6293 int item_count;
6294 int run;
6295 int row, rows;
6296 int cols;
6297 int i;
6298 int len;
6299
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006300#define INC3 27 // try to make three columns
6301#define INC2 40 // try to make two columns
6302#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006304 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006306 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 if (items == NULL)
6308 return;
6309
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006310 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006311 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
6313 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006314 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006316 * 2. display the medium items (medium length strings)
6317 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006318 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006320 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 {
6322 /*
6323 * collect the items in items[]
6324 */
6325 item_count = 0;
6326 for (i = 0; i < tc_len; i++)
6327 {
6328 len = show_one_termcode(termcodes[i].name,
6329 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006330 if ((flags & OPT_ONECOLUMN) ||
6331 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006332 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006333 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 items[item_count++] = i;
6335 }
6336
6337 /*
6338 * display the items
6339 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006340 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006342 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 if (cols == 0)
6344 cols = 1;
6345 rows = (item_count + cols - 1) / cols;
6346 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006347 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 rows = item_count;
6349 for (row = 0; row < rows && !got_int; ++row)
6350 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006351 msg_putchar('\n'); // go to next line
6352 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 break;
6354 col = 0;
6355 for (i = row; i < item_count; i += rows)
6356 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006357 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 show_one_termcode(termcodes[items[i]].name,
6359 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006360 if (run == 2)
6361 col += INC2;
6362 else
6363 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 }
6365 out_flush();
6366 ui_breakcheck();
6367 }
6368 }
6369 vim_free(items);
6370}
6371
6372/*
6373 * Show one termcode entry.
6374 * Output goes into IObuff[]
6375 */
6376 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006377show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
6379 char_u *p;
6380 int len;
6381
6382 if (name[0] > '~')
6383 {
6384 IObuff[0] = ' ';
6385 IObuff[1] = ' ';
6386 IObuff[2] = ' ';
6387 IObuff[3] = ' ';
6388 }
6389 else
6390 {
6391 IObuff[0] = 't';
6392 IObuff[1] = '_';
6393 IObuff[2] = name[0];
6394 IObuff[3] = name[1];
6395 }
6396 IObuff[4] = ' ';
6397
6398 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6399 if (p[1] != 't')
6400 STRCPY(IObuff + 5, p);
6401 else
6402 IObuff[5] = NUL;
6403 len = (int)STRLEN(IObuff);
6404 do
6405 IObuff[len++] = ' ';
6406 while (len < 17);
6407 IObuff[len] = NUL;
6408 if (code == NULL)
6409 len += 4;
6410 else
6411 len += vim_strsize(code);
6412
6413 if (printit)
6414 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006415 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006417 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 else
6419 msg_outtrans(code);
6420 }
6421 return len;
6422}
6423
6424#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6425/*
6426 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6427 * termcap codes from the terminal itself.
6428 * We get them one by one to avoid a very long response string.
6429 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006430static int xt_index_in = 0;
6431static int xt_index_out = 0;
6432
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006434req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435{
6436 xt_index_out = 0;
6437 xt_index_in = 0;
6438 req_more_codes_from_term();
6439}
6440
6441 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006442req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006444 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 int old_idx = xt_index_out;
6446
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006447 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 if (exiting)
6449 return;
6450
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006451 // Send up to 10 more requests out than we received. Avoid sending too
6452 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6454 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006455 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006456
Bram Moolenaar86394aa2020-09-05 14:27:24 +02006457#ifdef FEAT_JOB_CHANNEL
6458 ch_log_output = TRUE;
6459#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02006460 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6461 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 out_str_nf((char_u *)buf);
6463 ++xt_index_out;
6464 }
6465
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006466 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 if (xt_index_out != old_idx)
6468 out_flush();
6469}
6470
6471/*
6472 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6473 * A "0" instead of the "1" indicates a code that isn't supported.
6474 * Both <name> and <string> are encoded in hex.
6475 * "code" points to the "0" or "1".
6476 */
6477 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006478got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
6480#define XT_LEN 100
6481 char_u name[3];
6482 char_u str[XT_LEN];
6483 int i;
6484 int j = 0;
6485 int c;
6486
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006487 // A '1' means the code is supported, a '0' means it isn't.
6488 // When half the length is > XT_LEN we can't use it.
6489 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6491 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006492 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 name[0] = hexhex2nr(code + 3);
6494 name[1] = hexhex2nr(code + 5);
6495 name[2] = NUL;
6496 for (i = 0; key_names[i] != NULL; ++i)
6497 {
6498 if (STRCMP(key_names[i], name) == 0)
6499 {
6500 xt_index_in = i;
6501 break;
6502 }
6503 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006504
Bram Moolenaarb255b902018-04-24 21:40:10 +02006505 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6506
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 if (key_names[i] != NULL)
6508 {
6509 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6510 str[j++] = c;
6511 str[j] = NUL;
6512 if (name[0] == 'C' && name[1] == 'o')
6513 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006514 // Color count is not a key code.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00006515 may_adjust_color_count(atoi((char *)str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 }
6517 else
6518 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006519 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 i = find_term_bykeys(str);
6521 if (i >= 0)
6522 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006523 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 }
6525 }
6526 }
6527
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006528 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 ++xt_index_in;
6530 req_more_codes_from_term();
6531}
6532
6533/*
6534 * Check if there are any unanswered requests and deal with them.
6535 * This is called before starting an external program or getting direct
6536 * keyboard input. We don't want responses to be send to that program or
6537 * handled as typed text.
6538 */
6539 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006540check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541{
6542 int c;
6543
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006544 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6546 return;
6547
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006548 // Vgetc() will check for and handle any response.
6549 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 ++no_mapping;
6551 ++allow_keys;
6552 for (;;)
6553 {
6554 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006555 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 break;
6557
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006558 // If a response is recognized it's replaced with K_IGNORE, must read
6559 // it from the input stream. If there is no K_IGNORE we can't do
6560 // anything, break here (there might be some responses further on, but
6561 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 if (c != K_SPECIAL && c != K_IGNORE)
6563 break;
6564 c = vgetc();
6565 if (c != K_IGNORE)
6566 {
6567 vungetc(c);
6568 break;
6569 }
6570 }
6571 --no_mapping;
6572 --allow_keys;
6573}
6574#endif
6575
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006576#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577static char ksme_str[20];
6578static char ksmr_str[20];
6579static char ksmd_str[20];
6580
6581/*
6582 * For Win32 console: update termcap codes for existing console attributes.
6583 */
6584 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006585update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586{
6587 struct builtin_term *p;
6588
6589 p = find_builtin_term(DEFAULT_TERM);
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006590 sprintf(ksme_str, "\033|%dm", attr);
6591 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
6592 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593
6594 while (p->bt_string != NULL)
6595 {
6596 if (p->bt_entry == (int)KS_ME)
6597 p->bt_string = &ksme_str[0];
6598 else if (p->bt_entry == (int)KS_MR)
6599 p->bt_string = &ksmr_str[0];
6600 else if (p->bt_entry == (int)KS_MD)
6601 p->bt_string = &ksmd_str[0];
6602 ++p;
6603 }
6604}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006605
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006606# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006607# define KSSIZE 20
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006608
6609typedef enum
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006610{
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006611 CMODE_INDEXED = 0, // Use cmd.exe 4bit palette.
6612 CMODE_RGB, // Use 24bit RGB colors using VTP.
6613 CMODE_256COL, // Emulate xterm's 256-color palette using VTP.
6614 CMODE_LAST,
6615} cmode_T;
6616
6617struct ks_tbl_S
6618{
6619 int code; // value of KS_
6620 char *vtp; // code in RGB mode
6621 char *vtp2; // code in 256color mode
6622 char buf[CMODE_LAST][KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006623};
6624
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006625static struct ks_tbl_S ks_tbl[] =
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006626{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006627 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6628 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6629 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6630 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6631 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006632 {(int)KS_CZH, "\033|3m", "\033|3m"}, // italic
Bram Moolenaard4f73432018-09-21 12:24:12 +02006633 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6634 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6635 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006636# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006637 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6638 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006639 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6640 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006641# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006642 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6643 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006644 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6645 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006646# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006647 {(int)KS_CCO, "256", "256"}, // colors
6648 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006649};
6650
6651 static struct builtin_term *
6652find_first_tcap(
6653 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006654 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006655{
6656 struct builtin_term *p;
6657
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006658 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006659 if (p->bt_entry == code)
6660 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006661 return NULL;
6662}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006663# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006664
6665/*
6666 * For Win32 console: replace the sequence immediately after termguicolors.
6667 */
6668 void
6669swap_tcap(void)
6670{
6671# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006672 static int init_done = FALSE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006673 static cmode_T curr_mode;
6674 struct ks_tbl_S *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006675 struct builtin_term *bt;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006676 cmode_T mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006677
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006678 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006679 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006680 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006681 {
6682 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006683 if (bt != NULL)
6684 {
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006685 // Preserve the original value.
6686 STRNCPY(ks->buf[CMODE_INDEXED], bt->bt_string, KSSIZE);
6687 STRNCPY(ks->buf[CMODE_RGB], ks->vtp, KSSIZE);
6688 STRNCPY(ks->buf[CMODE_256COL], ks->vtp2, KSSIZE);
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006689
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006690 bt->bt_string = ks->buf[CMODE_INDEXED];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006691 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006692 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006693 init_done = TRUE;
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006694 curr_mode = CMODE_INDEXED;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006695 }
6696
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006697 if (p_tgc)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006698 mode = CMODE_RGB;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006699 else if (t_colors >= 256)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006700 mode = CMODE_256COL;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006701 else
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006702 mode = CMODE_INDEXED;
6703
6704 if (mode == curr_mode)
6705 return;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006706
6707 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006708 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006709 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6710 if (bt != NULL)
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006711 bt->bt_string = ks->buf[mode];
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006712 }
6713
LemonBoy5a7b6dc2022-05-06 18:38:41 +01006714 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006715# endif
6716}
6717
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006719
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006720
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006721#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006722 || defined(PROTO)
6723static int cube_value[] = {
6724 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6725};
6726
6727static int grey_ramp[] = {
6728 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6729 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6730};
6731
Christian Brabandt2f7e00a2022-05-02 00:06:51 +01006732static char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01006733// R G B
6734 { 0, 0, 0}, // black
6735 {224, 0, 0}, // dark red
6736 { 0, 224, 0}, // dark green
6737 {224, 224, 0}, // dark yellow / brown
6738 { 0, 0, 224}, // dark blue
6739 {224, 0, 224}, // dark magenta
6740 { 0, 224, 224}, // dark cyan
6741 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006742
LemonBoyd2a46622022-05-01 17:43:33 +01006743 {128, 128, 128}, // dark grey
6744 {255, 64, 64}, // light red
6745 { 64, 255, 64}, // light green
6746 {255, 255, 64}, // yellow
6747 { 64, 64, 255}, // light blue
6748 {255, 64, 255}, // light magenta
6749 { 64, 255, 255}, // light cyan
6750 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006751};
6752
LemonBoyd2a46622022-05-01 17:43:33 +01006753#if defined(MSWIN)
6754// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
6755static const char_u cterm_ansi_idx[] = {
6756 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
6757};
6758#endif
6759
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006760#define ANSI_INDEX_NONE 0
6761
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006762 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006763cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006764{
6765 int idx;
6766
6767 if (nr < 16)
6768 {
LemonBoyd2a46622022-05-01 17:43:33 +01006769#if defined(MSWIN)
6770 idx = cterm_ansi_idx[nr];
6771#else
6772 idx = nr;
6773#endif
6774 *r = ansi_table[idx][0];
6775 *g = ansi_table[idx][1];
6776 *b = ansi_table[idx][2];
6777 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006778 }
6779 else if (nr < 232)
6780 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006781 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006782 idx = nr - 16;
6783 *r = cube_value[idx / 36 % 6];
6784 *g = cube_value[idx / 6 % 6];
6785 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006786 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006787 }
6788 else if (nr < 256)
6789 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006790 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006791 idx = nr - 232;
6792 *r = grey_ramp[idx];
6793 *g = grey_ramp[idx];
6794 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006795 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006796 }
6797 else
6798 {
6799 *r = 0;
6800 *g = 0;
6801 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006802 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006803 }
6804}
6805#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006806
Bram Moolenaarf4140482020-02-15 23:06:45 +01006807/*
6808 * Replace K_BS by <BS> and K_DEL by <DEL>
6809 */
6810 void
6811term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len)
6812{
6813 int i;
6814 int c;
6815
6816 for (i = ta_len; i < ta_len + len; ++i)
6817 {
6818 if (ta_buf[i] == CSI && len - i > 2)
6819 {
6820 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6821 if (c == K_DEL || c == K_KDEL || c == K_BS)
6822 {
6823 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
6824 (size_t)(len - i - 2));
6825 if (c == K_DEL || c == K_KDEL)
6826 ta_buf[i] = DEL;
6827 else
6828 ta_buf[i] = Ctrl_H;
6829 len -= 2;
6830 }
6831 }
6832 else if (ta_buf[i] == '\r')
6833 ta_buf[i] = '\n';
6834 if (has_mbyte)
6835 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6836 }
6837}