blob: 4044d751a0e72fb1078738fa93551023f715d2a1 [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"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691# endif
692
693# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
694/*
695 * VT320 is working as an ANSI terminal compatible DEC terminal.
696 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 * TODO:- rewrite ESC[ codes to CSI
698 * - keyboard languages (CSI ? 26 n)
699 */
700 {(int)KS_NAME, "vt320"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000701 {(int)KS_CE, "\033[K"},
702 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000704 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000706 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000708 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000710 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000712 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000714 {(int)KS_CL, "\033[H\033[2J"},
715 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100716 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000717 {(int)KS_ME, "\033[0m"},
718 {(int)KS_MR, "\033[7m"},
719 {(int)KS_MD, "\033[1m"}, // bold mode
720 {(int)KS_SE, "\033[22m"},// normal mode
721 {(int)KS_UE, "\033[24m"},// exit underscore mode
722 {(int)KS_US, "\033[4m"}, // underscore mode
723 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
724 {(int)KS_CZR, "\033[0m"}, // italic mode end
725 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
726 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
727 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
728 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 {(int)KS_MS, "y"},
730 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100731 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 {(int)KS_LE, "\b"},
733# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000734 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000736 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737# endif
738# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000739 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000741 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000743 {K_UP, "\033[A"},
744 {K_DOWN, "\033[B"},
745 {K_RIGHT, "\033[C"},
746 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200747 // Note: cursor key sequences for application cursor mode are omitted,
748 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000749 {K_F1, "\033[11~"},
750 {K_F2, "\033[12~"},
751 {K_F3, "\033[13~"},
752 {K_F4, "\033[14~"},
753 {K_F5, "\033[15~"},
754 {K_F6, "\033[17~"},
755 {K_F7, "\033[18~"},
756 {K_F8, "\033[19~"},
757 {K_F9, "\033[20~"},
758 {K_F10, "\033[21~"},
759 {K_F11, "\033[23~"},
760 {K_F12, "\033[24~"},
761 {K_F13, "\033[25~"},
762 {K_F14, "\033[26~"},
763 {K_F15, "\033[28~"}, // Help
764 {K_F16, "\033[29~"}, // Select
765 {K_F17, "\033[31~"},
766 {K_F18, "\033[32~"},
767 {K_F19, "\033[33~"},
768 {K_F20, "\033[34~"},
769 {K_INS, "\033[2~"},
770 {K_DEL, "\033[3~"},
771 {K_HOME, "\033[1~"},
772 {K_END, "\033[4~"},
773 {K_PAGEUP, "\033[5~"},
774 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200775 // These sequences starting with <Esc> O may interfere with what the user
776 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000777 {K_KPLUS, "\033Ok"}, // keypad plus
778 {K_KMINUS, "\033Om"}, // keypad minus
779 {K_KDIVIDE, "\033Oo"}, // keypad /
780 {K_KMULTIPLY, "\033Oj"}, // keypad *
781 {K_KENTER, "\033OM"}, // keypad Enter
782 {K_K0, "\033Op"}, // keypad 0
783 {K_K1, "\033Oq"}, // keypad 1
784 {K_K2, "\033Or"}, // keypad 2
785 {K_K3, "\033Os"}, // keypad 3
786 {K_K4, "\033Ot"}, // keypad 4
787 {K_K5, "\033Ou"}, // keypad 5
788 {K_K6, "\033Ov"}, // keypad 6
789 {K_K7, "\033Ow"}, // keypad 7
790 {K_K8, "\033Ox"}, // keypad 8
791 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100792 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793# endif
794
Bram Moolenaare3f915d2020-07-14 23:02:44 +0200795# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796/*
797 * Ordinary vt52
798 */
799 {(int)KS_NAME, "vt52"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000800 {(int)KS_CE, "\033K"},
801 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100802# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000803 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100804# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000805 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000808 {(int)KS_SR, "\033I"},
809 {(int)KS_AL, "\033L"},
810 {(int)KS_DL, "\033M"},
811 {K_UP, "\033A"},
812 {K_DOWN, "\033B"},
813 {K_LEFT, "\033D"},
814 {K_RIGHT, "\033C"},
815 {K_F1, "\033P"},
816 {K_F2, "\033Q"},
817 {K_F3, "\033R"},
818 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820# endif
821
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200822# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200823 {(int)KS_NAME, "xterm"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000824 {(int)KS_CE, "\033[K"},
825 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000827 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000829 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000831 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000833 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000835 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836# endif
837# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000838 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000840 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000842 {(int)KS_CL, "\033[H\033[2J"},
843 {(int)KS_CD, "\033[J"},
844 {(int)KS_ME, "\033[m"},
845 {(int)KS_MR, "\033[7m"},
846 {(int)KS_MD, "\033[1m"},
847 {(int)KS_UE, "\033[m"},
848 {(int)KS_US, "\033[4m"},
849 {(int)KS_STE, "\033[29m"},
850 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 {(int)KS_MS, "y"},
852 {(int)KS_UT, "y"},
853 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000854 {(int)KS_VI, "\033[?25l"},
855 {(int)KS_VE, "\033[?25h"},
856 {(int)KS_VS, "\033[?12h"},
857 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200858# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000859 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200860# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000861 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200862# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000863 {(int)KS_CRC, "\033[?12$p"},
864 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000866 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000868 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000870 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000872 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000874 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000876 {(int)KS_KS, "\033[?1h\033="},
877 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000879 {(int)KS_TI, "\0337\033[?47h"},
880 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000882 {(int)KS_CTI, "\033[>4;2m"},
883 {(int)KS_CTE, "\033[>4;m"},
884 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000886 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000888 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200889 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000891 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
892 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
893 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000895 {(int)KS_CWS, "\033[8;%d;%dt"},
896 {(int)KS_CWP, "\033[3;%d;%dt"},
897 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000899 {(int)KS_CRV, "\033[>c"},
900 {(int)KS_RFG, "\033]10;?\007"},
901 {(int)KS_RBG, "\033]11;?\007"},
902 {(int)KS_U7, "\033[6n"},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200903# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100904 // These are printf strings, not terminal codes.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000905 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
906 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
907 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200908# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000909 {(int)KS_CAU, "\033[58;5;%dm"},
910 {(int)KS_CBE, "\033[?2004h"},
911 {(int)KS_CBD, "\033[?2004l"},
912 {(int)KS_CST, "\033[22;2t"},
913 {(int)KS_CRT, "\033[23;2t"},
914 {(int)KS_SSI, "\033[22;1t"},
915 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100916# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000917 {(int)KS_FD, "\033[?1004l"},
918 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100919# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000920
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000921 {K_UP, "\033O*A"},
922 {K_DOWN, "\033O*B"},
923 {K_RIGHT, "\033O*C"},
924 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100925 // An extra set of cursor keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000926 {K_XUP, "\033[@;*A"},
927 {K_XDOWN, "\033[@;*B"},
928 {K_XRIGHT, "\033[@;*C"},
929 {K_XLEFT, "\033[@;*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100930 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000931 {K_XF1, "\033O*P"},
932 {K_XF2, "\033O*Q"},
933 {K_XF3, "\033O*R"},
934 {K_XF4, "\033O*S"},
935 {K_F1, "\033[11;*~"},
936 {K_F2, "\033[12;*~"},
937 {K_F3, "\033[13;*~"},
938 {K_F4, "\033[14;*~"},
939 {K_F5, "\033[15;*~"},
940 {K_F6, "\033[17;*~"},
941 {K_F7, "\033[18;*~"},
942 {K_F8, "\033[19;*~"},
943 {K_F9, "\033[20;*~"},
944 {K_F10, "\033[21;*~"},
945 {K_F11, "\033[23;*~"},
946 {K_F12, "\033[24;*~"},
947 {K_S_TAB, "\033[Z"},
948 {K_HELP, "\033[28;*~"},
949 {K_UNDO, "\033[26;*~"},
950 {K_INS, "\033[2;*~"},
951 {K_HOME, "\033[1;*H"},
952 // {K_S_HOME, "\033O2H"},
953 // {K_C_HOME, "\033O5H"},
954 {K_KHOME, "\033[1;*~"},
955 {K_XHOME, "\033O*H"}, // other Home
956 {K_ZHOME, "\033[7;*~"}, // other Home
957 {K_END, "\033[1;*F"},
958 // {K_S_END, "\033O2F"},
959 // {K_C_END, "\033O5F"},
960 {K_KEND, "\033[4;*~"},
961 {K_XEND, "\033O*F"}, // other End
962 {K_ZEND, "\033[8;*~"},
963 {K_PAGEUP, "\033[5;*~"},
964 {K_PAGEDOWN, "\033[6;*~"},
965 {K_KPLUS, "\033O*k"}, // keypad plus
966 {K_KMINUS, "\033O*m"}, // keypad minus
967 {K_KDIVIDE, "\033O*o"}, // keypad /
968 {K_KMULTIPLY, "\033O*j"}, // keypad *
969 {K_KENTER, "\033O*M"}, // keypad Enter
970 {K_KPOINT, "\033O*n"}, // keypad .
971 {K_K0, "\033O*p"}, // keypad 0
972 {K_K1, "\033O*q"}, // keypad 1
973 {K_K2, "\033O*r"}, // keypad 2
974 {K_K3, "\033O*s"}, // keypad 3
975 {K_K4, "\033O*t"}, // keypad 4
976 {K_K5, "\033O*u"}, // keypad 5
977 {K_K6, "\033O*v"}, // keypad 6
978 {K_K7, "\033O*w"}, // keypad 7
979 {K_K8, "\033O*x"}, // keypad 8
980 {K_K9, "\033O*y"}, // keypad 9
981 {K_KDEL, "\033[3;*~"}, // keypad Del
982 {K_PS, "\033[200~"}, // paste start
983 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
985 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000986 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
987 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100988 // F14 and F15 are missing, because they send the same codes as the undo
989 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000990 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
991 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
992 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
993 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
994 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000995
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000996 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
997 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
998 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
999 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
1000 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
1001 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
1002 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
1003 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
1004 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
1005 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001006
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001007 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
1008 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
1009 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
1010 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
1011 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
1012 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
1013 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014# endif
1015
1016# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1017/*
1018 * iris-ansi for Silicon Graphics machines.
1019 */
1020 {(int)KS_NAME, "iris-ansi"},
1021 {(int)KS_CE, "\033[K"},
1022 {(int)KS_CD, "\033[J"},
1023 {(int)KS_AL, "\033[L"},
1024# ifdef TERMINFO
1025 {(int)KS_CAL, "\033[%p1%dL"},
1026# else
1027 {(int)KS_CAL, "\033[%dL"},
1028# endif
1029 {(int)KS_DL, "\033[M"},
1030# ifdef TERMINFO
1031 {(int)KS_CDL, "\033[%p1%dM"},
1032# else
1033 {(int)KS_CDL, "\033[%dM"},
1034# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001035#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036# ifdef TERMINFO
1037 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1038# else
1039 {(int)KS_CS, "\033[%i%d;%dr"},
1040# endif
1041#endif
1042 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001043 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
1044 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {(int)KS_TI, "\033[=6h"},
1046 {(int)KS_TE, "\033[=6l"},
1047 {(int)KS_SE, "\033[21;27m"},
1048 {(int)KS_SO, "\033[1;7m"},
1049 {(int)KS_ME, "\033[m"},
1050 {(int)KS_MR, "\033[7m"},
1051 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001052 {(int)KS_CCO, "8"}, // allow 8 colors
1053 {(int)KS_CZH, "\033[3m"}, // italic mode on
1054 {(int)KS_CZR, "\033[23m"}, // italic mode off
1055 {(int)KS_US, "\033[4m"}, // underline on
1056 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001058 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
1059 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
1060 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
1061 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001063 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
1064 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
1065 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
1066 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001068 {(int)KS_MS, "y"}, // guessed
1069 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 {(int)KS_LE, "\b"},
1071# ifdef TERMINFO
1072 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1073# else
1074 {(int)KS_CM, "\033[%i%d;%dH"},
1075# endif
1076 {(int)KS_SR, "\033M"},
1077# ifdef TERMINFO
1078 {(int)KS_CRI, "\033[%p1%dC"},
1079# else
1080 {(int)KS_CRI, "\033[%dC"},
1081# endif
1082 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001083 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001085 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086# ifdef TERMINFO
1087 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1088 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1089# else
1090 {(int)KS_CWS, "\033[203;%d;%d/y"},
1091 {(int)KS_CWP, "\033[205;%d;%d/y"},
1092# endif
1093 {K_UP, "\033[A"},
1094 {K_DOWN, "\033[B"},
1095 {K_LEFT, "\033[D"},
1096 {K_RIGHT, "\033[C"},
1097 {K_S_UP, "\033[161q"},
1098 {K_S_DOWN, "\033[164q"},
1099 {K_S_LEFT, "\033[158q"},
1100 {K_S_RIGHT, "\033[167q"},
1101 {K_F1, "\033[001q"},
1102 {K_F2, "\033[002q"},
1103 {K_F3, "\033[003q"},
1104 {K_F4, "\033[004q"},
1105 {K_F5, "\033[005q"},
1106 {K_F6, "\033[006q"},
1107 {K_F7, "\033[007q"},
1108 {K_F8, "\033[008q"},
1109 {K_F9, "\033[009q"},
1110 {K_F10, "\033[010q"},
1111 {K_F11, "\033[011q"},
1112 {K_F12, "\033[012q"},
1113 {K_S_F1, "\033[013q"},
1114 {K_S_F2, "\033[014q"},
1115 {K_S_F3, "\033[015q"},
1116 {K_S_F4, "\033[016q"},
1117 {K_S_F5, "\033[017q"},
1118 {K_S_F6, "\033[018q"},
1119 {K_S_F7, "\033[019q"},
1120 {K_S_F8, "\033[020q"},
1121 {K_S_F9, "\033[021q"},
1122 {K_S_F10, "\033[022q"},
1123 {K_S_F11, "\033[023q"},
1124 {K_S_F12, "\033[024q"},
1125 {K_INS, "\033[139q"},
1126 {K_HOME, "\033[H"},
1127 {K_END, "\033[146q"},
1128 {K_PAGEUP, "\033[150q"},
1129 {K_PAGEDOWN, "\033[154q"},
1130# endif
1131
1132# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1133/*
1134 * for debugging
1135 */
1136 {(int)KS_NAME, "debug"},
1137 {(int)KS_CE, "[CE]"},
1138 {(int)KS_CD, "[CD]"},
1139 {(int)KS_AL, "[AL]"},
1140# ifdef TERMINFO
1141 {(int)KS_CAL, "[CAL%p1%d]"},
1142# else
1143 {(int)KS_CAL, "[CAL%d]"},
1144# endif
1145 {(int)KS_DL, "[DL]"},
1146# ifdef TERMINFO
1147 {(int)KS_CDL, "[CDL%p1%d]"},
1148# else
1149 {(int)KS_CDL, "[CDL%d]"},
1150# endif
1151# ifdef TERMINFO
1152 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1153# else
1154 {(int)KS_CS, "[%dCS%d]"},
1155# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001156# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001158# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160# endif
1161# ifdef TERMINFO
1162 {(int)KS_CAB, "[CAB%p1%d]"},
1163 {(int)KS_CAF, "[CAF%p1%d]"},
1164 {(int)KS_CSB, "[CSB%p1%d]"},
1165 {(int)KS_CSF, "[CSF%p1%d]"},
1166# else
1167 {(int)KS_CAB, "[CAB%d]"},
1168 {(int)KS_CAF, "[CAF%d]"},
1169 {(int)KS_CSB, "[CSB%d]"},
1170 {(int)KS_CSF, "[CSF%d]"},
1171# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001172 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {(int)KS_OP, "[OP]"},
1174 {(int)KS_LE, "[LE]"},
1175 {(int)KS_CL, "[CL]"},
1176 {(int)KS_VI, "[VI]"},
1177 {(int)KS_VE, "[VE]"},
1178 {(int)KS_VS, "[VS]"},
1179 {(int)KS_ME, "[ME]"},
1180 {(int)KS_MR, "[MR]"},
1181 {(int)KS_MB, "[MB]"},
1182 {(int)KS_MD, "[MD]"},
1183 {(int)KS_SE, "[SE]"},
1184 {(int)KS_SO, "[SO]"},
1185 {(int)KS_UE, "[UE]"},
1186 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001187 {(int)KS_UCE, "[UCE]"},
1188 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001189 {(int)KS_STE, "[STE]"},
1190 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {(int)KS_MS, "[MS]"},
1192 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001193 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194# ifdef TERMINFO
1195 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1196# else
1197 {(int)KS_CM, "[%dCM%d]"},
1198# endif
1199 {(int)KS_SR, "[SR]"},
1200# ifdef TERMINFO
1201 {(int)KS_CRI, "[CRI%p1%d]"},
1202# else
1203 {(int)KS_CRI, "[CRI%d]"},
1204# endif
1205 {(int)KS_VB, "[VB]"},
1206 {(int)KS_KS, "[KS]"},
1207 {(int)KS_KE, "[KE]"},
1208 {(int)KS_TI, "[TI]"},
1209 {(int)KS_TE, "[TE]"},
1210 {(int)KS_CIS, "[CIS]"},
1211 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001212 {(int)KS_CSC, "[CSC]"},
1213 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {(int)KS_TS, "[TS]"},
1215 {(int)KS_FS, "[FS]"},
1216# ifdef TERMINFO
1217 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1218 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1219# else
1220 {(int)KS_CWS, "[%dCWS%d]"},
1221 {(int)KS_CWP, "[%dCWP%d]"},
1222# endif
1223 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001224 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001225 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001226 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {K_UP, "[KU]"},
1228 {K_DOWN, "[KD]"},
1229 {K_LEFT, "[KL]"},
1230 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001231 {K_XUP, "[xKU]"},
1232 {K_XDOWN, "[xKD]"},
1233 {K_XLEFT, "[xKL]"},
1234 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {K_S_UP, "[S-KU]"},
1236 {K_S_DOWN, "[S-KD]"},
1237 {K_S_LEFT, "[S-KL]"},
1238 {K_C_LEFT, "[C-KL]"},
1239 {K_S_RIGHT, "[S-KR]"},
1240 {K_C_RIGHT, "[C-KR]"},
1241 {K_F1, "[F1]"},
1242 {K_XF1, "[xF1]"},
1243 {K_F2, "[F2]"},
1244 {K_XF2, "[xF2]"},
1245 {K_F3, "[F3]"},
1246 {K_XF3, "[xF3]"},
1247 {K_F4, "[F4]"},
1248 {K_XF4, "[xF4]"},
1249 {K_F5, "[F5]"},
1250 {K_F6, "[F6]"},
1251 {K_F7, "[F7]"},
1252 {K_F8, "[F8]"},
1253 {K_F9, "[F9]"},
1254 {K_F10, "[F10]"},
1255 {K_F11, "[F11]"},
1256 {K_F12, "[F12]"},
1257 {K_S_F1, "[S-F1]"},
1258 {K_S_XF1, "[S-xF1]"},
1259 {K_S_F2, "[S-F2]"},
1260 {K_S_XF2, "[S-xF2]"},
1261 {K_S_F3, "[S-F3]"},
1262 {K_S_XF3, "[S-xF3]"},
1263 {K_S_F4, "[S-F4]"},
1264 {K_S_XF4, "[S-xF4]"},
1265 {K_S_F5, "[S-F5]"},
1266 {K_S_F6, "[S-F6]"},
1267 {K_S_F7, "[S-F7]"},
1268 {K_S_F8, "[S-F8]"},
1269 {K_S_F9, "[S-F9]"},
1270 {K_S_F10, "[S-F10]"},
1271 {K_S_F11, "[S-F11]"},
1272 {K_S_F12, "[S-F12]"},
1273 {K_HELP, "[HELP]"},
1274 {K_UNDO, "[UNDO]"},
1275 {K_BS, "[BS]"},
1276 {K_INS, "[INS]"},
1277 {K_KINS, "[KINS]"},
1278 {K_DEL, "[DEL]"},
1279 {K_KDEL, "[KDEL]"},
1280 {K_HOME, "[HOME]"},
1281 {K_S_HOME, "[C-HOME]"},
1282 {K_C_HOME, "[C-HOME]"},
1283 {K_KHOME, "[KHOME]"},
1284 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001285 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {K_END, "[END]"},
1287 {K_S_END, "[C-END]"},
1288 {K_C_END, "[C-END]"},
1289 {K_KEND, "[KEND]"},
1290 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001291 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {K_PAGEUP, "[PAGEUP]"},
1293 {K_PAGEDOWN, "[PAGEDOWN]"},
1294 {K_KPAGEUP, "[KPAGEUP]"},
1295 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1296 {K_MOUSE, "[MOUSE]"},
1297 {K_KPLUS, "[KPLUS]"},
1298 {K_KMINUS, "[KMINUS]"},
1299 {K_KDIVIDE, "[KDIVIDE]"},
1300 {K_KMULTIPLY, "[KMULTIPLY]"},
1301 {K_KENTER, "[KENTER]"},
1302 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001303 {K_PS, "[PASTE-START]"},
1304 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {K_K0, "[K0]"},
1306 {K_K1, "[K1]"},
1307 {K_K2, "[K2]"},
1308 {K_K3, "[K3]"},
1309 {K_K4, "[K4]"},
1310 {K_K5, "[K5]"},
1311 {K_K6, "[K6]"},
1312 {K_K7, "[K7]"},
1313 {K_K8, "[K8]"},
1314 {K_K9, "[K9]"},
1315# endif
1316
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001317#endif // NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
1319/*
1320 * The most minimal terminal: only clear screen and cursor positioning
1321 * Always included.
1322 */
1323 {(int)KS_NAME, "dumb"},
1324 {(int)KS_CL, "\014"},
1325#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001326 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001328 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329#endif
1330
1331/*
1332 * end marker
1333 */
1334 {(int)KS_NAME, NULL}
1335
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001336}; // end of builtin_termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001338#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001339 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001340termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001341{
Bram Moolenaarab302212016-04-26 20:59:29 +02001342 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001343}
1344
1345 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001346termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001347{
1348 guicolor_T t;
1349
1350 if (*name == NUL)
1351 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001352 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001353
1354 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001355 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001356 return t;
1357}
1358
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001359 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001360termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001361{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001362 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001363}
1364#endif
1365
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366/*
1367 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369#ifdef AMIGA
1370# define DEFAULT_TERM (char_u *)"amiga"
1371#endif
1372
1373#ifdef MSWIN
1374# define DEFAULT_TERM (char_u *)"win32"
1375#endif
1376
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001377#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378# define DEFAULT_TERM (char_u *)"ansi"
1379#endif
1380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381#ifdef VMS
1382# define DEFAULT_TERM (char_u *)"vt320"
1383#endif
1384
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001385#ifdef __HAIKU__
1386# undef DEFAULT_TERM
1387# define DEFAULT_TERM (char_u *)"xterm"
1388#endif
1389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390#ifndef DEFAULT_TERM
1391# define DEFAULT_TERM (char_u *)"dumb"
1392#endif
1393
1394/*
1395 * Term_strings contains currently used terminal output strings.
1396 * It is initialized with the default values by parse_builtin_tcap().
1397 * The values can be changed by setting the option with the same name.
1398 */
1399char_u *(term_strings[(int)KS_LAST + 1]);
1400
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001401static int need_gather = FALSE; // need to fill termleader[]
1402static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001404static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001405
1406/*
1407 * Structure and table to store terminal features that can be detected by
1408 * querying the terminal. Either by inspecting the termresponse or a more
1409 * specific request. Besides this there are:
1410 * t_colors - number of colors supported
1411 */
1412typedef struct {
1413 char *tpr_name;
1414 int tpr_set_by_termresponse;
1415 int tpr_status;
1416} termprop_T;
1417
1418// Values for tpr_status.
1419#define TPR_UNKNOWN 'u'
1420#define TPR_YES 'y'
1421#define TPR_NO 'n'
1422#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1423#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1424#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1425
1426// can request the cursor style without messing up the display
1427#define TPR_CURSOR_STYLE 0
1428// can request the cursor blink mode without messing up the display
1429#define TPR_CURSOR_BLINK 1
1430// can set the underline color with t_8u without resetting other colors
1431#define TPR_UNDERLINE_RGB 2
1432// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1433#define TPR_MOUSE 3
1434// table size
1435#define TPR_COUNT 4
1436
1437static termprop_T term_props[TPR_COUNT];
1438
1439/*
1440 * Initialize the term_props table.
1441 * When "all" is FALSE only set those that are detected from the version
1442 * response.
1443 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001444 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001445init_term_props(int all)
1446{
1447 int i;
1448
1449 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1450 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1451 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1452 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1453 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1454 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1455 term_props[TPR_MOUSE].tpr_name = "mouse";
1456 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
1457
1458 for (i = 0; i < TPR_COUNT; ++i)
1459 if (all || term_props[i].tpr_set_by_termresponse)
1460 term_props[i].tpr_status = TPR_UNKNOWN;
1461}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462#endif
1463
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001464#if defined(FEAT_EVAL) || defined(PROTO)
1465 void
1466f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1467{
1468# ifdef FEAT_TERMRESPONSE
1469 int i;
1470# endif
1471
1472 if (rettv_dict_alloc(rettv) != OK)
1473 return;
1474# ifdef FEAT_TERMRESPONSE
1475 for (i = 0; i < TPR_COUNT; ++i)
1476 {
1477 char_u value[2];
1478
1479 value[0] = term_props[i].tpr_status;
1480 value[1] = NUL;
1481 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1482 }
1483# endif
1484}
1485#endif
1486
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001488find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 struct builtin_term *p;
1491
1492 p = builtin_termcaps;
1493 while (p->bt_string != NULL)
1494 {
1495 if (p->bt_entry == (int)KS_NAME)
1496 {
1497#ifdef UNIX
1498 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1499 return p;
1500 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1501 return p;
1502 else
1503#endif
1504#ifdef VMS
1505 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1506 return p;
1507 else
1508#endif
1509 if (STRCMP(term, p->bt_string) == 0)
1510 return p;
1511 }
1512 ++p;
1513 }
1514 return p;
1515}
1516
1517/*
1518 * Parsing of the builtin termcap entries.
1519 * Caller should check if 'name' is a valid builtin term.
1520 * The terminal's name is not set, as this is already done in termcapinit().
1521 */
1522 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001523parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
1525 struct builtin_term *p;
1526 char_u name[2];
1527 int term_8bit;
1528
1529 p = find_builtin_term(term);
1530 term_8bit = term_is_8bit(term);
1531
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001532 // Do not parse if builtin term not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 if (p->bt_string == NULL)
1534 return;
1535
1536 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1537 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001538 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001540 // Only set the value if it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 if (term_strings[p->bt_entry] == NULL
1542 || term_strings[p->bt_entry] == empty_option)
1543 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001544#ifdef FEAT_EVAL
1545 int opt_idx = -1;
1546#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001547 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1549 {
1550 char_u *s, *t;
1551
1552 s = vim_strsave((char_u *)p->bt_string);
1553 if (s != NULL)
1554 {
1555 for (t = s; *t; ++t)
1556 if (term_7to8bit(t))
1557 {
1558 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001559 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 }
1561 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001562#ifdef FEAT_EVAL
1563 opt_idx =
1564#endif
1565 set_term_option_alloced(
1566 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 }
1568 }
1569 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001572#ifdef FEAT_EVAL
1573 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1574#endif
1575 }
1576#ifdef FEAT_EVAL
1577 set_term_option_sctx_idx(NULL, opt_idx);
1578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580 }
1581 else
1582 {
1583 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1584 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1585 if (find_termcode(name) == NULL)
1586 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1587 }
1588 }
1589}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591/*
1592 * Set number of colors.
1593 * Store it as a number in t_colors.
1594 * Store it as a string in T_CCO (using nr_colors[]).
1595 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001596 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001597set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001599 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601 t_colors = nr;
1602 if (t_colors > 1)
1603 sprintf((char *)nr_colors, "%d", t_colors);
1604 else
1605 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001606 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001608
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001609#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001610/*
1611 * Set the color count to "val" and redraw if it changed.
1612 */
1613 static void
1614may_adjust_color_count(int val)
1615{
1616 if (val != t_colors)
1617 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001618 // Nr of colors changed, initialize highlighting and
1619 // redraw everything. This causes a redraw, which usually
1620 // clears the message. Try keeping the message if it
1621 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001622 set_keep_msg_from_hist();
1623 set_color_count(val);
1624 init_highlight(TRUE, FALSE);
1625# ifdef DEBUG_TERMRESPONSE
1626 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001627 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001628
Bram Moolenaarb255b902018-04-24 21:40:10 +02001629 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001630 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001631# else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001632 redraw_asap(CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001633# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001634 }
1635}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636#endif
1637
1638#ifdef HAVE_TGETENT
1639static char *(key_names[]) =
1640{
Bram Moolenaar87202262020-05-24 17:23:45 +02001641# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001642 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001644# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 "#2", "#4", "%i", "*7",
1647 "k1", "k2", "k3", "k4", "k5", "k6",
1648 "k7", "k8", "k9", "k;", "F1", "F2",
1649 "%1", "&8", "kb", "kI", "kD", "kh",
1650 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1651 NULL
1652};
1653#endif
1654
Bram Moolenaar9289df52018-05-10 14:40:57 +02001655#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001656 static void
1657get_term_entries(int *height, int *width)
1658{
1659 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001660 enum SpecialKey dest; // index in term_strings[]
1661 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001662 } string_names[] =
1663 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1664 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1665 {KS_CL, "cl"}, {KS_CD, "cd"},
1666 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1667 {KS_ME, "me"}, {KS_MR, "mr"},
1668 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1669 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1670 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1671 {KS_STE,"Te"}, {KS_STS,"Ts"},
1672 {KS_CM, "cm"}, {KS_SR, "sr"},
1673 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1674 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001675 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001676 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001677 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1678 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001679 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1680 {KS_VS, "vs"}, {KS_CVS, "VS"},
1681 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1682 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1683 {KS_TS, "ts"}, {KS_FS, "fs"},
1684 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1685 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1686 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001687 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001688 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1689 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001690 {KS_CST, "ST"}, {KS_CRT, "RT"},
1691 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001692 {(enum SpecialKey)0, NULL}
1693 };
1694 int i;
1695 char_u *p;
1696 static char_u tstrbuf[TBUFSZ];
1697 char_u *tp = tstrbuf;
1698
1699 /*
1700 * get output strings
1701 */
1702 for (i = 0; string_names[i].name != NULL; ++i)
1703 {
1704 if (TERM_STR(string_names[i].dest) == NULL
1705 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001706 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001707 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001708#ifdef FEAT_EVAL
1709 set_term_option_sctx_idx(string_names[i].name, -1);
1710#endif
1711 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001712 }
1713
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001714 // tgetflag() returns 1 if the flag is present, 0 if not and
1715 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001716 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1717 T_MS = (char_u *)"y";
1718 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1719 T_XS = (char_u *)"y";
1720 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1721 T_XN = (char_u *)"y";
1722 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1723 T_DB = (char_u *)"y";
1724 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1725 T_DA = (char_u *)"y";
1726 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1727 T_UT = (char_u *)"y";
1728
1729 /*
1730 * get key codes
1731 */
1732 for (i = 0; key_names[i] != NULL; ++i)
1733 if (find_termcode((char_u *)key_names[i]) == NULL)
1734 {
1735 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001736 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001737 if (p != NULL
1738 && (*p != Ctrl_H
1739 || key_names[i][0] != 'k'
1740 || key_names[i][1] != 'l'))
1741 add_termcode((char_u *)key_names[i], p, FALSE);
1742 }
1743
1744 if (*height == 0)
1745 *height = tgetnum("li");
1746 if (*width == 0)
1747 *width = tgetnum("co");
1748
1749 /*
1750 * Get number of colors (if not done already).
1751 */
1752 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001753 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001754 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001755#ifdef FEAT_EVAL
1756 set_term_option_sctx_idx("Co", -1);
1757#endif
1758 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001759
1760# ifndef hpux
1761 BC = (char *)TGETSTR("bc", &tp);
1762 UP = (char *)TGETSTR("up", &tp);
1763 p = TGETSTR("pc", &tp);
1764 if (p)
1765 PC = *p;
1766# endif
1767}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001768#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001769
1770 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001771report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001772{
1773 struct builtin_term *termp;
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001774 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001775
1776 mch_errmsg("\r\n");
1777 if (error_msg != NULL)
1778 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001779 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001780 mch_errmsg("\r\n");
1781 }
1782 mch_errmsg("'");
1783 mch_errmsg((char *)term);
1784 mch_errmsg(_("' not known. Available builtin terminals are:"));
1785 mch_errmsg("\r\n");
1786 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1787 {
Bram Moolenaar0f5575d2021-07-30 21:18:03 +02001788 if (termp->bt_entry == (int)KS_NAME
1789 && STRCMP(termp->bt_string, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001790 {
1791#ifdef HAVE_TGETENT
1792 mch_errmsg(" builtin_");
1793#else
1794 mch_errmsg(" ");
1795#endif
1796 mch_errmsg(termp->bt_string);
1797 mch_errmsg("\r\n");
1798 }
1799 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001800 // Output extra 'cmdheight' line breaks to avoid that the following error
1801 // message overwrites the last terminal name.
1802 for (i = 1; i < p_ch; ++i)
1803 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001804}
1805
1806 static void
1807report_default_term(char_u *term)
1808{
1809 mch_errmsg(_("defaulting to '"));
1810 mch_errmsg((char *)term);
1811 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001812 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001813 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001814 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001815 out_flush();
1816 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001817 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001818 }
1819}
1820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821/*
1822 * Set terminal options for terminal "term".
1823 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1824 *
1825 * While doing this, until ttest(), some options may be NULL, be careful.
1826 */
1827 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001828set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
1830 struct builtin_term *termp;
1831#ifdef HAVE_TGETENT
1832 int builtin_first = p_tbi;
1833 int try;
1834 int termcap_cleared = FALSE;
1835#endif
1836 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001837 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 char_u *bs_p, *del_p;
1839
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001840 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001841 if (silent_mode)
1842 return OK;
1843
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001844 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
1846 if (term_is_builtin(term))
1847 {
1848 term += 8;
1849#ifdef HAVE_TGETENT
1850 builtin_first = 1;
1851#endif
1852 }
1853
1854/*
1855 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1856 * If builtin_first is TRUE:
1857 * 0. try builtin termcap
1858 * 1. try external termcap
1859 * 2. if both fail default to a builtin terminal
1860 * If builtin_first is FALSE:
1861 * 1. try external termcap
1862 * 2. try builtin termcap, if both fail default to a builtin terminal
1863 */
1864#ifdef HAVE_TGETENT
1865 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1866 {
1867 /*
1868 * Use external termcap
1869 */
1870 if (try == 1)
1871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
1874 /*
1875 * If the external termcap does not have a matching entry, try the
1876 * builtin ones.
1877 */
1878 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (!termcap_cleared)
1881 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001882 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 termcap_cleared = TRUE;
1884 }
1885
Bram Moolenaar69e05692018-05-10 14:11:52 +02001886 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
1888 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001889 else // try == 0 || try == 2
1890#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 /*
1892 * Use builtin termcap
1893 */
1894 {
1895#ifdef HAVE_TGETENT
1896 /*
1897 * If builtin termcap was already used, there is no need to search
1898 * for the builtin termcap again, quit now.
1899 */
1900 if (try == 2 && builtin_first && termcap_cleared)
1901 break;
1902#endif
1903 /*
1904 * search for 'term' in builtin_termcaps[]
1905 */
1906 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001907 if (termp->bt_string == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
1909#ifdef HAVE_TGETENT
1910 /*
1911 * If try == 0, first try the external termcap. If that is not
1912 * found we'll get back here with try == 2.
1913 * If termcap_cleared is set we used the external termcap,
1914 * don't complain about not finding the term in the builtin
1915 * termcap.
1916 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001917 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001919 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 break;
1921#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001922 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001924 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (starting != NO_SCREEN)
1926 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001927 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 wait_return(TRUE);
1929 return FAIL;
1930 }
1931 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001932 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001933 set_string_option_direct((char_u *)"term", -1, term,
1934 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 display_errors();
1936 }
1937 out_flush();
1938#ifdef HAVE_TGETENT
1939 if (!termcap_cleared)
1940 {
1941#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001942 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943#ifdef HAVE_TGETENT
1944 termcap_cleared = TRUE;
1945 }
1946#endif
1947 parse_builtin_tcap(term);
1948#ifdef FEAT_GUI
1949 if (term_is_gui(term))
1950 {
1951 out_flush();
1952 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001953 // If starting the GUI failed, don't do any of the other
1954 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (!gui.in_use)
1956 return FAIL;
1957#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001958 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959#endif
1960 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001961#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
1963#ifdef HAVE_TGETENT
1964 }
1965#endif
1966
1967/*
1968 * special: There is no info in the termcap about whether the cursor
1969 * positioning is relative to the start of the screen or to the start of the
1970 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1971 * relative.
1972 */
1973 if (STRCMP(term, "pcterm") == 0)
1974 T_CCS = (char_u *)"yes";
1975 else
1976 T_CCS = empty_option;
1977
1978#ifdef UNIX
1979/*
1980 * Any "stty" settings override the default for t_kb from the termcap.
1981 * This is in os_unix.c, because it depends a lot on the version of unix that
1982 * is being used.
1983 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1984 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001985# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001987# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 get_stty();
1989#endif
1990
1991/*
1992 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1993 * didn't work, use the default CTRL-H
1994 * The default for t_kD is DEL, unless t_kb is DEL.
1995 * The vim_strsave'd strings are probably lost forever, well it's only two
1996 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1997 * directly.
1998 */
1999#ifdef FEAT_GUI
2000 if (!gui.in_use)
2001#endif
2002 {
2003 bs_p = find_termcode((char_u *)"kb");
2004 del_p = find_termcode((char_u *)"kD");
2005 if (bs_p == NULL || *bs_p == NUL)
2006 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2007 if ((del_p == NULL || *del_p == NUL) &&
2008 (bs_p == NULL || *bs_p != DEL))
2009 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2010 }
2011
2012#if defined(UNIX) || defined(VMS)
2013 term_is_xterm = vim_is_xterm(term);
2014#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002015#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002016 // Reset terminal properties that are set based on the termresponse, which
2017 // will be sent out soon.
2018 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002021#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 /*
2023 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2024 * The termcode for the mouse is added as a side effect in option.c.
2025 */
2026 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002027 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002029# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002030 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
2032 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002033 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 else
2035 p = (char_u *)"xterm";
2036 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002037# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002039 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002040 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002041 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2042 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002043 reset_option_was_set((char_u *)"ttym");
2044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002046# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002048# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002050 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002052#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055
Bram Moolenaarbf789742021-01-16 11:21:40 +01002056#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002057 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002058 if (use_xterm_like_mouse(term))
2059 {
2060 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002061
2062 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002063 name[0] = KS_EXTRA;
2064 name[1] = KE_FOCUSGAINED;
2065 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002066 add_termcode(name, (char_u *)"\033[I", FALSE);
2067
2068 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002069 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002070 add_termcode(name, (char_u *)"\033[O", FALSE);
2071
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002072 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002073 }
2074#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002075#if (defined(UNIX) || defined(VMS))
2076 // First time after setting 'term' a focus event is always reported.
2077 focus_state = MAYBE;
2078#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002081 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 if (STRCMP(term, DEFAULT_TERM) != 0)
2083 term_console = FALSE;
2084 else
2085 {
2086 term_console = TRUE;
2087# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002088 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089# endif
2090 }
2091#endif
2092
2093#if defined(UNIX) || defined(VMS)
2094 /*
2095 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2096 */
2097 if (vim_is_fastterm(term))
2098 p_tf = TRUE;
2099#endif
2100#ifdef USE_TERM_CONSOLE
2101 /*
2102 * 'ttyfast' is default on consoles
2103 */
2104 if (term_console)
2105 p_tf = TRUE;
2106#endif
2107
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002108 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002110 full_screen = TRUE; // we can use termcap codes from now on
2111 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002113 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002114 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01002115 write_t_8u_state = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116#endif
2117
2118 /*
2119 * Initialize the terminal with the appropriate termcap codes.
2120 * Set the mouse and window title if possible.
2121 * Don't do this when starting, need to parse the .vimrc first, because it
2122 * may redefine t_TI etc.
2123 */
2124 if (starting != NO_SCREEN)
2125 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002126 starttermcap(); // may change terminal mode
2127 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002128 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
2130
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002131 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (width <= 0 || height <= 0)
2133 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002134 // termcap failed to report size
2135 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002137#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002138 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002140 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141#endif
2142 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002143 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 if (starting != NO_SCREEN)
2145 {
2146 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002147 scroll_region_reset(); // In case Rows changed
2148 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002151 buf_T *buf;
2152 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154 /*
2155 * Execute the TermChanged autocommands for each buffer that is
2156 * loaded.
2157 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002158 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
2160 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002161 {
2162 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2164 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002165 // restore curwin/curbuf and a few other things
2166 aucmd_restbuf(&aco);
2167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
2171
2172#ifdef FEAT_TERMRESPONSE
2173 may_req_termresponse();
2174#endif
2175
2176 return OK;
2177}
2178
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002179#if defined(EXITFREE) || defined(PROTO)
2180
2181# ifdef HAVE_DEL_CURTERM
2182# undef TERMINAL // name clash in term.h
2183# include <term.h> // declares cur_term
2184# endif
2185
2186/*
2187 * If supported, delete "cur_term", which caches terminal related entries.
2188 * Avoids that valgrind reports possibly lost memory.
2189 */
2190 void
2191free_cur_term()
2192{
2193# ifdef HAVE_DEL_CURTERM
2194 if (cur_term)
2195 del_curterm(cur_term);
2196# endif
2197}
2198
2199#endif
2200
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201#ifdef HAVE_TGETENT
2202/*
2203 * Call tgetent()
2204 * Return error message if it fails, NULL if it's OK.
2205 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002206 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002207tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
2209 int i;
2210
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002211 // Note: Valgrind may report a leak here, because the library keeps one
2212 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002214 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002216 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217# endif
2218 )
2219 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002220 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2221 // tgetent() with the always existing "dumb" entry to avoid a crash or
2222 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 (void)TGETENT(tbuf, "dumb");
2224
2225 if (i < 0)
2226# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002227 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (i == 0)
2229# endif
2230#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002231 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002233 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234#endif
2235 }
2236 return NULL;
2237}
2238
2239/*
2240 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2241 * Fix that here.
2242 */
2243 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002244vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245{
2246 char *p;
2247
2248 p = tgetstr(s, (char **)pp);
2249 if (p == (char *)-1)
2250 p = NULL;
2251 return (char_u *)p;
2252}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002253#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002255#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256/*
2257 * Get Columns and Rows from the termcap. Used after a window signal if the
2258 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2259 * and "li" entries never change. But on some systems this works.
2260 * Errors while getting the entries are ignored.
2261 */
2262 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002263getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002264 long *cp, // pointer to columns
2265 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 char_u tbuf[TBUFSZ];
2268
2269 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2270 {
2271 if (*cp == 0)
2272 *cp = tgetnum("co");
2273 if (*rp == 0)
2274 *rp = tgetnum("li");
2275 }
2276}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002277#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
2279/*
2280 * Get a string entry from the termcap and add it to the list of termcodes.
2281 * Used for <t_xx> special keys.
2282 * Give an error message for failure when not sourcing.
2283 * If force given, replace an existing entry.
2284 * Return FAIL if the entry was not found, OK if the entry was added.
2285 */
2286 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002287add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
2289 char_u *term;
2290 int key;
2291 struct builtin_term *termp;
2292#ifdef HAVE_TGETENT
2293 char_u *string;
2294 int i;
2295 int builtin_first;
2296 char_u tbuf[TBUFSZ];
2297 char_u tstrbuf[TBUFSZ];
2298 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002299 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300#endif
2301
2302/*
2303 * If the GUI is running or will start in a moment, we only support the keys
2304 * that the GUI can produce.
2305 */
2306#ifdef FEAT_GUI
2307 if (gui.in_use || gui.starting)
2308 return gui_mch_haskey(name);
2309#endif
2310
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002311 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 return OK;
2313
2314 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002315 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 return FAIL;
2317
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002318 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
2320 term += 8;
2321#ifdef HAVE_TGETENT
2322 builtin_first = TRUE;
2323#endif
2324 }
2325#ifdef HAVE_TGETENT
2326 else
2327 builtin_first = p_tbi;
2328#endif
2329
2330#ifdef HAVE_TGETENT
2331/*
2332 * We can get the entry from the builtin termcap and from the external one.
2333 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2334 * builtin termcap first.
2335 * If 'ttybuiltin' is off, try external termcap first.
2336 */
2337 for (i = 0; i < 2; ++i)
2338 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002339 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340#endif
2341 /*
2342 * Search in builtin termcap
2343 */
2344 {
2345 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002346 if (termp->bt_string != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002349 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 while (termp->bt_entry != (int)KS_NAME)
2351 {
2352 if ((int)termp->bt_entry == key)
2353 {
2354 add_termcode(name, (char_u *)termp->bt_string,
2355 term_is_8bit(term));
2356 return OK;
2357 }
2358 ++termp;
2359 }
2360 }
2361 }
2362#ifdef HAVE_TGETENT
2363 else
2364 /*
2365 * Search in external termcap
2366 */
2367 {
2368 error_msg = tgetent_error(tbuf, term);
2369 if (error_msg == NULL)
2370 {
2371 string = TGETSTR((char *)name, &tp);
2372 if (string != NULL && *string != NUL)
2373 {
2374 add_termcode(name, string, FALSE);
2375 return OK;
2376 }
2377 }
2378 }
2379 }
2380#endif
2381
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002382 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
2384#ifdef HAVE_TGETENT
2385 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002386 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 else
2388#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002389 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
2391 return FAIL;
2392}
2393
2394 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002395term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396{
2397 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2398}
2399
2400/*
2401 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2402 * Assume that the terminal is using 8-bit controls when the name contains
2403 * "8bit", like in "xterm-8bit".
2404 */
2405 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002406term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
2408 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2409}
2410
2411/*
2412 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002413 * <Esc>[ -> CSI <M_C_[>
2414 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 * <Esc>O -> <M-C-O>
2416 */
2417 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002418term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419{
2420 if (*p == ESC)
2421 {
2422 if (p[1] == '[')
2423 return CSI;
2424 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002425 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (p[1] == 'O')
2427 return 0x8f;
2428 }
2429 return 0;
2430}
2431
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002432#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002434term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435{
2436 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2437}
2438#endif
2439
2440#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2441
2442 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002443tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444{
2445 static char_u buf[16];
2446 char_u *p;
2447
2448 p = buf + 15;
2449 *p = '\0';
2450 do
2451 {
2452 --p;
2453 *p = (char_u) (i % 10 + '0');
2454 i /= 10;
2455 }
2456 while (i > 0 && p > buf);
2457 return p;
2458}
2459#endif
2460
2461#ifndef HAVE_TGETENT
2462
2463/*
2464 * minimal tgoto() implementation.
2465 * no padding and we only parse for %i %d and %+char
2466 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002467 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002468tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 static char buf[30];
2471 char *p, *s, *e;
2472
2473 if (!cm)
2474 return "OOPS";
2475 e = buf + 29;
2476 for (s = buf; s < e && *cm; cm++)
2477 {
2478 if (*cm != '%')
2479 {
2480 *s++ = *cm;
2481 continue;
2482 }
2483 switch (*++cm)
2484 {
2485 case 'd':
2486 p = (char *)tltoa((unsigned long)y);
2487 y = x;
2488 while (*p)
2489 *s++ = *p++;
2490 break;
2491 case 'i':
2492 x++;
2493 y++;
2494 break;
2495 case '+':
2496 *s++ = (char)(*++cm + y);
2497 y = x;
2498 break;
2499 case '%':
2500 *s++ = *cm;
2501 break;
2502 default:
2503 return "OOPS";
2504 }
2505 }
2506 *s = '\0';
2507 return buf;
2508}
2509
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002510#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
2512/*
2513 * Set the terminal name and initialize the terminal options.
2514 * If "name" is NULL or empty, get the terminal name from the environment.
2515 * If that fails, use the default terminal name.
2516 */
2517 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002518termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519{
2520 char_u *term;
2521
2522 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002523 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 term = name;
2525
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526#ifndef MSWIN
2527 if (term == NULL)
2528 term = mch_getenv((char_u *)"TERM");
2529#endif
2530 if (term == NULL || *term == NUL)
2531 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002532 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002534 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 set_string_default("term", term);
2536 set_string_default("ttytype", term);
2537
2538 /*
2539 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2540 */
2541 set_termname(T_NAME != NULL ? T_NAME : term);
2542}
2543
2544/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002545 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002547#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002548
2549// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002551
2552static int out_pos = 0; // number of chars in out_buf
2553
2554// Since the maximum number of SGR parameters shown as a normal value range is
2555// 16, the escape sequence length can be 4 * 16 + lead + tail.
2556#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558/*
2559 * out_flush(): flush the output buffer
2560 */
2561 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002562out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
2564 int len;
2565
2566 if (out_pos != 0)
2567 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002568 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 len = out_pos;
2570 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002571 ui_write(out_buf, len, FALSE);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002572#ifdef FEAT_JOB_CHANNEL
2573 if (ch_log_output)
2574 {
2575 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002576 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002577# ifdef FEAT_GUI
2578 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2579# endif
2580 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002581 out_buf);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002582 ch_log_output = FALSE;
2583 }
2584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
2586}
2587
Bram Moolenaara338adc2018-01-31 20:51:47 +01002588/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002589 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2590 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002591 */
2592 void
2593out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002594 int force UNUSED, // when TRUE, update cursor even when not moved
2595 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002596{
2597 mch_disable_flush();
2598 out_flush();
2599 mch_enable_flush();
2600#ifdef FEAT_GUI
2601 if (gui.in_use)
2602 {
2603 gui_update_cursor(force, clear_selection);
2604 gui_may_flush();
2605 }
2606#endif
2607}
2608
2609
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610/*
2611 * Sometimes a byte out of a multi-byte character is written with out_char().
2612 * To avoid flushing half of the character, call this function first.
2613 */
2614 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002615out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616{
2617 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2618 out_flush();
2619}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
2621#ifdef FEAT_GUI
2622/*
2623 * out_trash(): Throw away the contents of the output buffer
2624 */
2625 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002626out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627{
2628 out_pos = 0;
2629}
2630#endif
2631
2632/*
2633 * out_char(c): put a byte into the output buffer.
2634 * Flush it if it becomes full.
2635 * This should not be used for outputting text on the screen (use functions
2636 * like msg_puts() and screen_putchar() for that).
2637 */
2638 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002639out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
Bram Moolenaard0573012017-10-28 21:11:06 +02002641#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002642 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 out_char('\r');
2644#endif
2645
2646 out_buf[out_pos++] = c;
2647
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002648 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (out_pos >= OUT_SIZE || p_wd)
2650 out_flush();
2651}
2652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002654 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002656 static int
2657out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002659 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 if (out_pos >= OUT_SIZE)
2662 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002663 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664}
2665
2666/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002667 * A never-padding out_str().
2668 * Use this whenever you don't want to run the string through tputs().
2669 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 * is likely to strip off leading digits, that it mistakes for padding
2671 * information, and "%i", "%d", etc.
2672 * This should only be used for writing terminal codes, not for outputting
2673 * normal text (use functions like msg_puts() and screen_putchar() for that).
2674 */
2675 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002676out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002678 // avoid terminal strings being split up
2679 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 while (*s)
2683 out_char_nf(*s++);
2684
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002685 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (p_wd)
2687 out_flush();
2688}
2689
2690/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002691 * A conditional-flushing out_str, mainly for visualbell.
2692 * Handles a delay internally, because termlib may not respect the delay or do
2693 * it at the wrong time.
2694 * Note: Only for terminal strings.
2695 */
2696 void
2697out_str_cf(char_u *s)
2698{
2699 if (s != NULL && *s)
2700 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002701#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002702 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002703#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002704
2705#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002706 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002707 if (gui.in_use)
2708 {
2709 out_str_nf(s);
2710 return;
2711 }
2712#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002713 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002714 out_flush();
2715#ifdef HAVE_TGETENT
2716 for (p = s; *s; ++s)
2717 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002718 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002719 if (*s == '$' && *(s + 1) == '<')
2720 {
2721 char_u save_c = *s;
2722 int duration = atoi((char *)s + 2);
2723
2724 *s = NUL;
2725 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2726 *s = save_c;
2727 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002728# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002729 // Only sleep here if we can limit this happening in
2730 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002731 p = vim_strchr(s, '>');
2732 if (p == NULL || duration <= 0)
2733 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002734 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002735 p = s;
2736 }
2737 else
2738 {
2739 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002740 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002741 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002742# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002743 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002744 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002745# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002746 break;
2747 }
2748 }
2749 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2750#else
2751 while (*s)
2752 out_char_nf(*s++);
2753#endif
2754
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002755 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002756 if (p_wd)
2757 out_flush();
2758 }
2759}
2760
2761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002763 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 * This should only be used for writing terminal codes, not for outputting
2765 * normal text (use functions like msg_puts() and screen_putchar() for that).
2766 */
2767 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002768out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
2770 if (s != NULL && *s)
2771 {
2772#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002773 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (gui.in_use)
2775 {
2776 out_str_nf(s);
2777 return;
2778 }
2779#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002780 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002781 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 out_flush();
2783#ifdef HAVE_TGETENT
2784 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2785#else
2786 while (*s)
2787 out_char_nf(*s++);
2788#endif
2789
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002790 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (p_wd)
2792 out_flush();
2793 }
2794}
2795
2796/*
2797 * cursor positioning using termcap parser. (jw)
2798 */
2799 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002800term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
2802 OUT_STR(tgoto((char *)T_CM, col, row));
2803}
2804
2805 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002806term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807{
2808 OUT_STR(tgoto((char *)T_CRI, 0, i));
2809}
2810
2811 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002812term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813{
2814 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2815}
2816
2817 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002818term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
2820 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2821}
2822
2823#if defined(HAVE_TGETENT) || defined(PROTO)
2824 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002825term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002827 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (x < 0)
2829 x = 0;
2830 if (y < 0)
2831 y = 0;
2832 OUT_STR(tgoto((char *)T_CWP, y, x));
2833}
2834
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002835# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2836/*
2837 * Return TRUE if we can request the terminal for a response.
2838 */
2839 static int
2840can_get_termresponse()
2841{
2842 return cur_tmode == TMODE_RAW
2843 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002844# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002845 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002846# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002847 && p_ek;
2848}
2849
Bram Moolenaarafd78262019-05-10 23:10:31 +02002850/*
2851 * Set "status" to STATUS_SENT.
2852 */
2853 static void
2854termrequest_sent(termrequest_T *status)
2855{
2856 status->tr_progress = STATUS_SENT;
2857 status->tr_start = time(NULL);
2858}
2859
2860/*
2861 * Return TRUE if any of the requests are in STATUS_SENT.
2862 */
2863 static int
2864termrequest_any_pending()
2865{
2866 int i;
2867 time_t now = time(NULL);
2868
2869 for (i = 0; all_termrequests[i] != NULL; ++i)
2870 {
2871 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2872 {
2873 if (all_termrequests[i]->tr_start > 0 && now > 0
2874 && all_termrequests[i]->tr_start + 2 < now)
2875 // Sent the request more than 2 seconds ago and didn't get a
2876 // response, assume it failed.
2877 all_termrequests[i]->tr_progress = STATUS_FAIL;
2878 else
2879 return TRUE;
2880 }
2881 }
2882 return FALSE;
2883}
2884
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002885static int winpos_x = -1;
2886static int winpos_y = -1;
2887static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002888
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002889# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002890/*
2891 * Try getting the Vim window position from the terminal.
2892 * Returns OK or FAIL.
2893 */
2894 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002895term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002896{
2897 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002898 int prev_winpos_x = winpos_x;
2899 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002900
2901 if (*T_CGP == NUL || !can_get_termresponse())
2902 return FAIL;
2903 winpos_x = -1;
2904 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002905 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002906 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002907 OUT_STR(T_CGP);
2908 out_flush();
2909
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002910 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002911 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002912 {
2913 (void)vpeekc_nomap();
2914 if (winpos_x >= 0 && winpos_y >= 0)
2915 {
2916 *x = winpos_x;
2917 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002918 return OK;
2919 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002920 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002921 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002922 // Do not reset "did_request_winpos", if we timed out the response might
2923 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002924
2925 winpos_x = prev_winpos_x;
2926 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002927 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002928 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002929 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002930 *x = winpos_x;
2931 *y = winpos_y;
2932 return OK;
2933 }
2934
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002935 return FALSE;
2936}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002937# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002938# endif
2939
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002941term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002943 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944}
2945#endif
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002948term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
2950 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002951 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002952 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002954 // Special handling of 16 colors, because termcap can't handle it
2955 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
2956 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002958 && ((s[0] == ESC && s[1] == '[')
2959#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2960 || (s[0] == ESC && s[1] == '|')
2961#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02002962 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 && s[i] != NUL
2964 && (STRCMP(s + i + 1, "%p1%dm") == 0
2965 || STRCMP(s + i + 1, "%dm") == 0)
2966 && (s[i] == '3' || s[i] == '4'))
2967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002969 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002971 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002973 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002974#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002975 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002976#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002977 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02002978 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2979 : (n >= 16 ? "48;5;" : "10");
2980
2981 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2983 }
2984 else
2985 OUT_STR(tgoto((char *)s, 0, n));
2986}
2987
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002988 void
2989term_fg_color(int n)
2990{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002991 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002992 if (*T_CAF)
2993 term_color(T_CAF, n);
2994 else if (*T_CSF)
2995 term_color(T_CSF, n);
2996}
2997
2998 void
2999term_bg_color(int n)
3000{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003001 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003002 if (*T_CAB)
3003 term_color(T_CAB, n);
3004 else if (*T_CSB)
3005 term_color(T_CSB, n);
3006}
3007
Bram Moolenaare023e882020-05-31 16:42:30 +02003008 void
3009term_ul_color(int n)
3010{
3011 if (*T_CAU)
3012 term_color(T_CAU, n);
3013}
3014
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003015/*
3016 * Return "dark" or "light" depending on the kind of terminal.
3017 * This is just guessing! Recognized are:
3018 * "linux" Linux console
3019 * "screen.linux" Linux console with screen
3020 * "cygwin.*" Cygwin shell
3021 * "putty.*" Putty program
3022 * We also check the COLORFGBG environment variable, which is set by
3023 * rxvt and derivatives. This variable contains either two or three
3024 * values separated by semicolons; we want the last value in either
3025 * case. If this value is 0-6 or 8, our background is dark.
3026 */
3027 char_u *
3028term_bg_default(void)
3029{
3030#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003031 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003032 return (char_u *)"dark";
3033#else
3034 char_u *p;
3035
3036 if (STRCMP(T_NAME, "linux") == 0
3037 || STRCMP(T_NAME, "screen.linux") == 0
3038 || STRNCMP(T_NAME, "cygwin", 6) == 0
3039 || STRNCMP(T_NAME, "putty", 5) == 0
3040 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3041 && (p = vim_strrchr(p, ';')) != NULL
3042 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3043 && p[2] == NUL))
3044 return (char_u *)"dark";
3045 return (char_u *)"light";
3046#endif
3047}
3048
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003049#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003050
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003051#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3052#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3053#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003054
3055 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003056term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003057{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003058#define MAX_COLOR_STR_LEN 100
3059 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003060
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003061 if (*s == NUL)
3062 return;
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003063 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003064 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003065#ifdef FEAT_VTP
3066 if (use_wt())
3067 {
3068 out_flush();
3069 buf[1] = '[';
3070 vtp_printf(buf);
3071 }
3072 else
3073#endif
3074 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003075}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003076
3077 void
3078term_fg_rgb_color(guicolor_T rgb)
3079{
3080 term_rgb_color(T_8F, rgb);
3081}
3082
3083 void
3084term_bg_rgb_color(guicolor_T rgb)
3085{
3086 term_rgb_color(T_8B, rgb);
3087}
Bram Moolenaare023e882020-05-31 16:42:30 +02003088
3089 void
3090term_ul_rgb_color(guicolor_T rgb)
3091{
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01003092# ifdef FEAT_TERMRESPONSE
3093 if (write_t_8u_state != OK)
3094 write_t_8u_state = MAYBE;
3095 else
3096# endif
3097 term_rgb_color(T_8U, rgb);
Bram Moolenaare023e882020-05-31 16:42:30 +02003098}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003099#endif
3100
Bram Moolenaar651fca82021-11-29 20:39:38 +00003101#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102/*
3103 * Generic function to set window title, using t_ts and t_fs.
3104 */
3105 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003106term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003108#ifdef FEAT_JOB_CHANNEL
3109 ch_log_output = TRUE;
3110#endif
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003111 // t_ts takes one argument: column in status line
3112 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003114 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 out_flush();
3116}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003117
3118/*
3119 * Tell the terminal to push (save) the title and/or icon, so that it can be
3120 * popped (restored) later.
3121 */
3122 void
3123term_push_title(int which)
3124{
Bram Moolenaar27821262019-05-08 16:41:09 +02003125 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003126 {
3127 OUT_STR(T_CST);
3128 out_flush();
3129 }
3130
Bram Moolenaar27821262019-05-08 16:41:09 +02003131 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003132 {
3133 OUT_STR(T_SSI);
3134 out_flush();
3135 }
3136}
3137
3138/*
3139 * Tell the terminal to pop the title and/or icon.
3140 */
3141 void
3142term_pop_title(int which)
3143{
Bram Moolenaar27821262019-05-08 16:41:09 +02003144 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003145 {
3146 OUT_STR(T_CRT);
3147 out_flush();
3148 }
3149
Bram Moolenaar27821262019-05-08 16:41:09 +02003150 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003151 {
3152 OUT_STR(T_SRI);
3153 out_flush();
3154 }
3155}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156#endif
3157
3158/*
3159 * Make sure we have a valid set or terminal options.
3160 * Replace all entries that are NULL by empty_option
3161 */
3162 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003163ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003165 char_u *env_colors;
3166
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003167 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168
3169 /*
3170 * MUST have "cm": cursor motion.
3171 */
3172 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003173 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174
3175 /*
3176 * if "cs" defined, use a scroll region, it's faster.
3177 */
3178 if (*T_CS != NUL)
3179 scroll_region = TRUE;
3180 else
3181 scroll_region = FALSE;
3182
3183 if (pairs)
3184 {
3185 /*
3186 * optional pairs
3187 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003188 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 if (*T_ME == NUL)
3190 T_ME = T_MR = T_MD = T_MB = empty_option;
3191 if (*T_SO == NUL || *T_SE == NUL)
3192 T_SO = T_SE = empty_option;
3193 if (*T_US == NUL || *T_UE == NUL)
3194 T_US = T_UE = empty_option;
3195 if (*T_CZH == NUL || *T_CZR == NUL)
3196 T_CZH = T_CZR = empty_option;
3197
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003198 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (*T_VE == NUL)
3200 T_VI = empty_option;
3201
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003202 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 if (*T_ME == NUL)
3204 {
3205 T_ME = T_SE;
3206 T_MR = T_SO;
3207 T_MD = T_SO;
3208 }
3209
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003210 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 if (*T_SO == NUL)
3212 {
3213 T_SE = T_ME;
3214 if (*T_MR == NUL)
3215 T_SO = T_MD;
3216 else
3217 T_SO = T_MR;
3218 }
3219
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003220 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 if (*T_CZH == NUL)
3222 {
3223 T_CZR = T_ME;
3224 if (*T_MR == NUL)
3225 T_CZH = T_MD;
3226 else
3227 T_CZH = T_MR;
3228 }
3229
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003230 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (*T_CSB == NUL || *T_CSF == NUL)
3232 {
3233 T_CSB = empty_option;
3234 T_CSF = empty_option;
3235 }
3236
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003237 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 if (*T_CAB == NUL || *T_CAF == NUL)
3239 {
3240 T_CAB = empty_option;
3241 T_CAF = empty_option;
3242 }
3243
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003244 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003246 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003248 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 p_wiv = (*T_XS != NUL);
3250 }
3251 need_gather = TRUE;
3252
Bram Moolenaar759d8152020-04-26 16:52:49 +02003253 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3254 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003256#ifdef FEAT_GUI
3257 if (!gui.in_use)
3258#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003259 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003260 env_colors = mch_getenv((char_u *)"COLORS");
3261 if (env_colors != NULL && isdigit(*env_colors))
3262 {
3263 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003264
Bram Moolenaar759d8152020-04-26 16:52:49 +02003265 if (colors != t_colors)
3266 set_color_count(colors);
3267 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269}
3270
3271#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3272 || defined(PROTO)
3273/*
3274 * Represent the given long_u as individual bytes, with the most significant
3275 * byte first, and store them in dst.
3276 */
3277 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003278add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
3280 int i;
3281 int shift;
3282
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003283 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 {
3285 shift = 8 * (sizeof(long_u) - i);
3286 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3287 }
3288}
3289
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290/*
3291 * Interpret the next string of bytes in buf as a long integer, with the most
3292 * significant byte first. Note that it is assumed that buf has been through
3293 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3294 * Puts result in val, and returns the number of bytes read from buf
3295 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3296 * were present.
3297 */
3298 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003299get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
3301 int len;
3302 char_u bytes[sizeof(long_u)];
3303 int i;
3304 int shift;
3305
3306 *val = 0;
3307 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3308 if (len != -1)
3309 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003310 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
3312 shift = 8 * (sizeof(long_u) - 1 - i);
3313 *val += (long_u)bytes[i] << shift;
3314 }
3315 }
3316 return len;
3317}
3318#endif
3319
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320/*
3321 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3322 * that buf has been through inchar(). Returns the actual number of bytes used
3323 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3324 * available.
3325 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003326 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003327get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 int len = 0;
3330 int i;
3331 char_u c;
3332
3333 for (i = 0; i < num_bytes; i++)
3334 {
3335 if ((c = buf[len++]) == NUL)
3336 return -1;
3337 if (c == K_SPECIAL)
3338 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003339 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 return -1;
3341 if (buf[len++] == (int)KS_ZERO)
3342 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003343 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3344 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003345 if (buf[len++] == (int)KE_CSI)
3346 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003348 else if (c == CSI && buf[len] == KS_EXTRA
3349 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003350 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3351 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003352 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 bytes[i] = c;
3354 }
3355 return len;
3356}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
3358/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003359 * Check if the new shell size is valid, correct it if it's too small or way
3360 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 */
3362 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003363check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003365 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003367 limit_screen_size();
3368}
3369
3370/*
3371 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3372 */
3373 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003374limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003375{
3376 if (Columns < MIN_COLUMNS)
3377 Columns = MIN_COLUMNS;
3378 else if (Columns > 10000)
3379 Columns = 10000;
3380 if (Rows > 1000)
3381 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382}
3383
Bram Moolenaar86b68352004-12-27 21:59:20 +00003384/*
3385 * Invoked just before the screen structures are going to be (re)allocated.
3386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003388win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389{
3390 static int old_Rows = 0;
3391 static int old_Columns = 0;
3392
3393 if (old_Rows != Rows || old_Columns != Columns)
3394 ui_new_shellsize();
3395 if (old_Rows != Rows)
3396 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003397 // If 'window' uses the whole screen, keep it using that.
3398 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003399 if (p_window == old_Rows - 1
3400 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003401 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003403 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 }
3405 if (old_Columns != Columns)
3406 {
3407 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003408 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 }
3410}
3411
3412/*
3413 * Call this function when the Vim shell has been resized in any way.
3414 * Will obtain the current size and redraw (also when size didn't change).
3415 */
3416 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003417shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 set_shellsize(0, 0, FALSE);
3420}
3421
3422/*
3423 * Check if the shell size changed. Handle a resize.
3424 * When the size didn't change, nothing happens.
3425 */
3426 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003427shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
3429 int old_Rows = Rows;
3430 int old_Columns = Columns;
3431
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003432 if (!exiting
3433#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003434 // Do not get the size when executing a shell command during
3435 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003436 && !gui.starting
3437#endif
3438 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003439 {
3440 (void)ui_get_shellsize();
3441 check_shellsize();
3442 if (old_Rows != Rows || old_Columns != Columns)
3443 shell_resized();
3444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445}
3446
3447/*
3448 * Set size of the Vim shell.
3449 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3450 * window size (this is used for the :win command).
3451 * If 'mustset' is FALSE, we may try to get the real window size and if
3452 * it fails use 'width' and 'height'.
3453 */
3454 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003455set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
3457 static int busy = FALSE;
3458
3459 /*
3460 * Avoid recursiveness, can happen when setting the window size causes
3461 * another window-changed signal.
3462 */
3463 if (busy)
3464 return;
3465
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003466 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 return;
3468
Bram Moolenaara971b822011-09-14 14:43:25 +02003469 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003471 // postpone the resizing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 State = SETWSIZE;
3473 return;
3474 }
3475
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003476 if (updating_screen)
3477 // resizing while in update_screen() may cause a crash
3478 return;
3479
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003480 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003481 // buffer (or window) has already been closed and removing a scrollbar
3482 // causes a resize event. Don't resize then, it will happen after entering
3483 // another buffer.
3484 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003485 return;
3486
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 ++busy;
3488
3489#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003490 out_flush(); // must do this before mch_get_shellsize() for
3491 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492#endif
3493
3494 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3495 {
3496 Rows = height;
3497 Columns = width;
3498 check_shellsize();
3499 ui_set_shellsize(mustset);
3500 }
3501 else
3502 check_shellsize();
3503
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003504 // The window layout used to be adjusted here, but it now happens in
3505 // screenalloc() (also invoked from screenclear()). That is because the
3506 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
3508 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3509 screenclear();
3510 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003511 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
3513 if (starting != NO_SCREEN)
3514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 changed_line_abv_curs();
3518 invalidate_botline();
3519
3520 /*
3521 * We only redraw when it's needed:
3522 * - While at the more prompt or executing an external command, don't
3523 * redraw, but position the cursor.
3524 * - While editing the command line, only redraw that.
3525 * - in Ex mode, don't redraw anything.
3526 * - Otherwise, redraw right now, and position the cursor.
3527 * Always need to call update_screen() or screenalloc(), to make
3528 * sure Rows/Columns and the size of ScreenLines[] is correct!
3529 */
3530 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3531 || exmode_active)
3532 {
3533 screenalloc(FALSE);
3534 repeat_message();
3535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 else
3537 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003538 if (curwin->w_p_scb)
3539 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003540 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003541 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003542 update_screen(NOT_VALID);
3543 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003544 }
3545 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003546 {
3547 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003548 if (pum_visible())
3549 {
3550 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003551 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003552 }
Bram Moolenaara5e66212017-09-29 22:42:33 +02003553 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003554 if (redrawing())
3555 setcursor();
3556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003558 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560 out_flush();
3561 --busy;
3562}
3563
3564/*
3565 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3566 * commands and Ex mode).
3567 */
3568 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003569settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
3571#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003572 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 if (gui.in_use)
3574 return;
3575#endif
3576
3577 if (full_screen)
3578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003580 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3581 * set the terminal to raw mode, even though we think it already is,
3582 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 * When we think the terminal is normal, don't try to set it to
3584 * normal again, because that causes problems (logout!) on some
3585 * machines.
3586 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003587 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
3589#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003590# ifdef FEAT_GUI
3591 if (!gui.in_use && !gui.starting)
3592# endif
3593 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003594 // May need to check for T_CRV response and termcodes, it
3595 // doesn't work in Cooked mode, an external program may get
3596 // them.
3597 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003598 (void)vpeekc_nomap();
3599 check_for_codes_from_term();
3600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003603 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003604
3605 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3606 // Avoid doing this too often, on some terminals the codes are not
3607 // handled properly.
3608 if (termcap_active && tmode != TMODE_SLEEP
3609 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003610 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003611#ifdef FEAT_JOB_CHANNEL
3612 ch_log_output = TRUE;
3613#endif
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003614 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003615 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003616 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003617 out_str(T_CTE); // possibly disables modifyOtherKeys
3618 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003619 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003620 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003621 out_str(T_BE); // enable bracketed paste mode (should
3622 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003623 out_str(T_CTI); // possibly enables modifyOtherKeys
3624 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003627 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003630 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 out_flush();
3632 }
3633#ifdef FEAT_TERMRESPONSE
3634 may_req_termresponse();
3635#endif
3636 }
3637}
3638
3639 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003640starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
3642 if (full_screen && !termcap_active)
3643 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003644#ifdef FEAT_JOB_CHANNEL
3645 ch_log_output = TRUE;
3646#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003647 out_str(T_TI); // start termcap mode
3648 out_str(T_CTI); // start "raw" mode
3649 out_str(T_KS); // start "keypad transmit" mode
3650 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003651
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003652#if defined(UNIX) || defined(VMS)
3653 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003654 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003655 out_str(T_FE);
3656#endif
3657
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 out_flush();
3659 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003660 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003662# ifdef FEAT_GUI
3663 if (!gui.in_use && !gui.starting)
3664# endif
3665 {
3666 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003667 // Immediately check for a response. If t_Co changes, we don't
3668 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003669 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003670 check_for_codes_from_term();
3671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672#endif
3673 }
3674}
3675
3676 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003677stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678{
3679 screen_stop_highlight();
3680 reset_cterm_colors();
3681 if (termcap_active)
3682 {
3683#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003684# ifdef FEAT_GUI
3685 if (!gui.in_use && !gui.starting)
3686# endif
3687 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003688 // May need to discard T_CRV, T_U7 or T_RBG response.
3689 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003690 {
3691# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003692 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003693 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003694# endif
3695# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003696 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003697 if (exiting)
3698 tcflush(fileno(stdin), TCIFLUSH);
3699# endif
3700 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003701 // Check for termcodes first, otherwise an external program may
3702 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003703 check_for_codes_from_term();
3704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705#endif
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003706#ifdef FEAT_JOB_CHANNEL
3707 ch_log_output = TRUE;
3708#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003709
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003710#if defined(UNIX) || defined(VMS)
3711 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003712 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003713 out_str(T_FD);
3714#endif
3715
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003716 out_str(T_BD); // disable bracketed paste mode
3717 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 out_flush();
3719 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003720 cursor_on(); // just in case it is still off
3721 out_str(T_CTE); // stop "raw" mode
3722 out_str(T_TE); // stop termcap mode
3723 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 out_flush();
3725 }
3726}
3727
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003728#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729/*
3730 * Request version string (for xterm) when needed.
3731 * Only do this after switching to raw mode, otherwise the result will be
3732 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003733 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003734 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 * Only do this after termcap mode has been started, otherwise the codes for
3736 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003737 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3738 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003739 * On Unix only do it when both output and input are a tty (avoid writing
3740 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 * The result is caught in check_termcode().
3742 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003743 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003744may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003746 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003747 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003748 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 && *T_CRV != NUL)
3750 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003751#ifdef FEAT_JOB_CHANNEL
3752 ch_log_output = TRUE;
3753#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003754 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003756 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003757 // check for the characters now, otherwise they might be eaten by
3758 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 out_flush();
3760 (void)vpeekc_nomap();
3761 }
3762}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003763
Bram Moolenaar9584b312013-03-13 19:29:28 +01003764/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003765 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3766 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003767 * Note that this goes out before T_CRV, so that the result can be used when
3768 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003769 */
3770 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003771check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003772{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003773 int did_send = FALSE;
3774
3775 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3776 return;
3777
Bram Moolenaarafd78262019-05-10 23:10:31 +02003778 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003779 && !option_was_set((char_u *)"ambiwidth"))
3780 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003781 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003782
Bram Moolenaara45551a2020-06-09 15:57:37 +02003783 // Ambiguous width check.
3784 // Check how the terminal treats ambiguous character width (UAX #11).
3785 // First, we move the cursor to (1, 0) and print a test ambiguous
3786 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3787 // the current cursor position. If the terminal treats \u25bd as
3788 // single width, the position is (1, 1), or if it is treated as double
3789 // width, that will be (1, 2). This function has the side effect that
3790 // changes cursor position, so it must be called immediately after
3791 // entering termcap mode.
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003792#ifdef FEAT_JOB_CHANNEL
3793 ch_log_output = TRUE;
3794#endif
Bram Moolenaara45551a2020-06-09 15:57:37 +02003795 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003796 // Do this in the second row. In the first row the returned sequence
3797 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003798 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003799 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003800 out_str(buf);
3801 out_str(T_U7);
3802 termrequest_sent(&u7_status);
3803 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003804 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003805
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003806 // This overwrites a few characters on the screen, a redraw is needed
3807 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003808 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003809 term_windgoto(1, 0);
3810 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003811 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003812 }
3813
Bram Moolenaard1f34e62022-01-07 19:24:20 +00003814 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02003815 {
3816 // 2. Check compatibility with xterm.
3817 // We move the cursor to (2, 0), print a test sequence and then query
3818 // the current cursor position. If the terminal properly handles
3819 // unknown DCS string and CSI sequence with intermediate byte, the test
3820 // sequence is ignored and the cursor does not move. If the terminal
3821 // handles test sequence incorrectly, a garbage string is displayed and
3822 // the cursor does move.
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003823#ifdef FEAT_JOB_CHANNEL
3824 ch_log_output = TRUE;
3825#endif
Bram Moolenaara45551a2020-06-09 15:57:37 +02003826 LOG_TR(("Sending xterm compatibility test sequence."));
3827 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003828 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003829 term_windgoto(2, 0);
3830 // send the test DCS string.
3831 out_str((char_u *)"\033Pzz\033\\");
3832 // send the test CSI sequence with intermediate byte.
3833 out_str((char_u *)"\033[0%m");
3834 out_str(T_U7);
3835 termrequest_sent(&xcc_status);
3836 out_flush();
3837 did_send = TRUE;
3838
3839 // If the terminal handles test sequence incorrectly, garbage text is
3840 // displayed. Clear them out for now.
3841 screen_stop_highlight();
3842 term_windgoto(2, 0);
3843 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003844 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003845 }
3846
3847 if (did_send)
3848 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003849 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003850
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003851 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003852 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003853
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003854 // check for the characters now, otherwise they might be eaten by
3855 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003856 out_flush();
3857 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003858 }
3859}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003860
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003861/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003862 * Similar to requesting the version string: Request the terminal background
3863 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003864 */
3865 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003866may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003867{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003868 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003869 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003870 int didit = FALSE;
3871
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003872# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003873 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003874 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003875 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003876#ifdef FEAT_JOB_CHANNEL
3877 ch_log_output = TRUE;
3878#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003879 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003880 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003881 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003882 didit = TRUE;
3883 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003884# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003885
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003886 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003887 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003888 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003889#ifdef FEAT_JOB_CHANNEL
3890 ch_log_output = TRUE;
3891#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003892 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003893 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003894 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003895 didit = TRUE;
3896 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003897
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003898 if (didit)
3899 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003900 // check for the characters now, otherwise they might be eaten by
3901 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003902 out_flush();
3903 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003904 }
3905 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003906}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003907
Bram Moolenaar2951b772013-07-03 12:45:31 +02003908# ifdef DEBUG_TERMRESPONSE
3909 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003910log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003911{
3912 static FILE *fd_tr = NULL;
3913 static proftime_T start;
3914 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003915 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003916
3917 if (fd_tr == NULL)
3918 {
3919 fd_tr = fopen("termresponse.log", "w");
3920 profile_start(&start);
3921 }
3922 now = start;
3923 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003924 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3925 must_redraw == NOT_VALID ? "NV"
3926 : must_redraw == CLEAR ? "CL" : " ");
3927 va_start(ap, fmt);
3928 vfprintf(fd_tr, fmt, ap);
3929 va_end(ap);
3930 fputc('\n', fd_tr);
3931 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003932}
3933# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934#endif
3935
3936/*
3937 * Return TRUE when saving and restoring the screen.
3938 */
3939 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003940swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941{
3942 return (full_screen && *T_TI != NUL);
3943}
3944
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945/*
3946 * By outputting the 'cursor very visible' termcap code, for some windowed
3947 * terminals this makes the screen scrolled to the correct position.
3948 * Used when starting Vim or returning from a shell.
3949 */
3950 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003951scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003953 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003955#ifdef FEAT_JOB_CHANNEL
3956 ch_log_output = TRUE;
3957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003959 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003960 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
3962}
3963
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003964// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965static int cursor_is_off = FALSE;
3966
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003967// True if cursor is not visible due to an ongoing cursor-less sleep
3968static int cursor_is_asleep = FALSE;
3969
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003971 * Enable the cursor without checking if it's already enabled.
3972 */
3973 void
3974cursor_on_force(void)
3975{
3976 out_str(T_VE);
3977 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003978 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02003979}
3980
3981/*
3982 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 */
3984 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003985cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003987 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003988 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989}
3990
3991/*
3992 * Disable the cursor.
3993 */
3994 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003995cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003997 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003999 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 cursor_is_off = TRUE;
4001 }
4002}
4003
Dominique Pelle748b3082022-01-08 12:41:16 +00004004#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004005/*
4006 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
4007 */
4008 int
4009cursor_is_sleeping(void)
4010{
4011 return cursor_is_asleep;
4012}
Dominique Pelle748b3082022-01-08 12:41:16 +00004013#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02004014
4015/*
4016 * Disable the cursor and mark it disabled by cursor-less sleep
4017 */
4018 void
4019cursor_sleep(void)
4020{
4021 cursor_is_asleep = TRUE;
4022 cursor_off();
4023}
4024
4025/*
4026 * Enable the cursor and mark it not disabled by cursor-less sleep
4027 */
4028 void
4029cursor_unsleep(void)
4030{
4031 cursor_is_asleep = FALSE;
4032 cursor_on();
4033}
4034
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004035#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004037 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004038 */
4039 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004040term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004041{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004042 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004043 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004044
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004045 // Only do something when redrawing the screen and we can restore the
4046 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004047 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004048 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004049# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004050 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004051 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004052 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004053# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004054 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004055 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004056
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004057 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004058 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004059 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004060 {
4061 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004062 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004063 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004064 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004065 if (*p != NUL)
4066 {
4067 out_str(p);
4068 showing_mode = REPLACE;
4069 }
4070 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004071 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004072 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004073 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004074 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004075 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004076 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004077 showing_mode = INSERT;
4078 }
4079 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004080 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004081 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004082 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004083 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004084 }
4085}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004086
4087# if defined(FEAT_TERMINAL) || defined(PROTO)
4088 void
4089term_cursor_color(char_u *color)
4090{
4091 if (*T_CSC != NUL)
4092 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004093 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004094 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004095 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004096 out_flush();
4097 }
4098}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004099# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004100
Bram Moolenaar4db25542017-08-28 22:43:05 +02004101 int
4102blink_state_is_inverted()
4103{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004104#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004105 return rbm_status.tr_progress == STATUS_GOT
4106 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004107 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004108#else
4109 return FALSE;
4110#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004111}
4112
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004113/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004114 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004115 */
4116 void
4117term_cursor_shape(int shape, int blink)
4118{
4119 if (*T_CSH != NUL)
4120 {
4121 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4122 out_flush();
4123 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004124 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004125 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004126 int do_blink = blink;
4127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004128 // t_SH is empty: try setting just the blink state.
4129 // The blink flags are XORed together, if the initial blinking from
4130 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004131 if (blink_state_is_inverted())
4132 do_blink = !blink;
4133
4134 if (do_blink && *T_VS != NUL)
4135 {
4136 out_str(T_VS);
4137 out_flush();
4138 }
4139 else if (!do_blink && *T_CVS != NUL)
4140 {
4141 out_str(T_CVS);
4142 out_flush();
4143 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004144 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004145}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004146#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004147
4148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 * Set scrolling region for window 'wp'.
4150 * The region starts 'off' lines from the start of the window.
4151 * Also set the vertical scroll region for a vertically split window. Always
4152 * the full width of the window, excluding the vertical separator.
4153 */
4154 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004155scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156{
4157 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4158 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004160 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4161 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004162 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163}
4164
4165/*
4166 * Reset scrolling region to the whole screen.
4167 */
4168 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004169scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
4171 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 if (*T_CSV != NUL)
4173 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004174 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175}
4176
4177
4178/*
4179 * List of terminal codes that are currently recognized.
4180 */
4181
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004182static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004184 char_u name[2]; // termcap name of entry
4185 char_u *code; // terminal code (in allocated memory)
4186 int len; // STRLEN(code)
4187 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188} *termcodes = NULL;
4189
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004190static int tc_max_len = 0; // number of entries that termcodes[] can hold
4191static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004193static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004194
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004196clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
4198 while (tc_len > 0)
4199 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004200 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 tc_max_len = 0;
4202
4203#ifdef HAVE_TGETENT
4204 BC = (char *)empty_option;
4205 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004206 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 ospeed = 0;
4208#endif
4209
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004210 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211}
4212
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004213#define ATC_FROM_TERM 55
4214
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215/*
4216 * Add a new entry to the list of terminal codes.
4217 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004218 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4219 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 */
4221 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004222add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223{
4224 struct termcode *new_tc;
4225 int i, j;
4226 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004227 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
4229 if (string == NULL || *string == NUL)
4230 {
4231 del_termcode(name);
4232 return;
4233 }
4234
Bram Moolenaar4f974752019-02-17 17:44:42 +01004235#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004236 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004237#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004238# ifdef VIMDLL
4239 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004240 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004241 else
4242# endif
4243 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 if (s == NULL)
4246 return;
4247
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004248 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004249 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004251 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 s[0] = term_7to8bit(string);
4253 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004254
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004255#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4256# ifdef VIMDLL
4257 if (!gui.in_use)
4258# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004259 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004260 if (s[0] == K_NUL)
4261 {
4262 STRMOVE(s + 1, s);
4263 s[1] = 3;
4264 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004265 }
4266#endif
4267
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004268 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004270 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
4272 /*
4273 * need to make space for more entries
4274 */
4275 if (tc_len == tc_max_len)
4276 {
4277 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004278 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 if (new_tc == NULL)
4280 {
4281 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004282 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 return;
4284 }
4285 for (i = 0; i < tc_len; ++i)
4286 new_tc[i] = termcodes[i];
4287 vim_free(termcodes);
4288 termcodes = new_tc;
4289 }
4290
4291 /*
4292 * Look for existing entry with the same name, it is replaced.
4293 * Look for an existing entry that is alphabetical higher, the new entry
4294 * is inserted in front of it.
4295 */
4296 for (i = 0; i < tc_len; ++i)
4297 {
4298 if (termcodes[i].name[0] < name[0])
4299 continue;
4300 if (termcodes[i].name[0] == name[0])
4301 {
4302 if (termcodes[i].name[1] < name[1])
4303 continue;
4304 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004305 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 */
4307 if (termcodes[i].name[1] == name[1])
4308 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004309 if (flags == ATC_FROM_TERM && (j = termcode_star(
4310 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004311 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004312 // Don't replace ESC[123;*X or ESC O*X with another when
4313 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004314 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004315 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004316 && s[len - 1]
4317 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004318 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004319 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004320 vim_free(s);
4321 return;
4322 }
4323 }
4324 else
4325 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004326 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004327 vim_free(termcodes[i].code);
4328 --tc_len;
4329 break;
4330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332 }
4333 /*
4334 * Found alphabetical larger entry, move rest to insert new entry
4335 */
4336 for (j = tc_len; j > i; --j)
4337 termcodes[j] = termcodes[j - 1];
4338 break;
4339 }
4340
4341 termcodes[i].name[0] = name[0];
4342 termcodes[i].name[1] = name[1];
4343 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004344 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004345
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004346 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4347 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004348 termcodes[i].modlen = 0;
4349 j = termcode_star(s, len);
4350 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004351 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004352 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004353 // For "CSI[@;X" the "@" is not included in "modlen".
4354 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4355 --termcodes[i].modlen;
4356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 ++tc_len;
4358}
4359
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004360/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004361 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004362 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004363 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004364 */
4365 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004366termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004367{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004368 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004369 if (len >= 3 && code[len - 2] == '*')
4370 {
4371 if (len >= 5 && code[len - 3] == ';')
4372 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004373 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004374 return 1;
4375 }
4376 return 0;
4377}
4378
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004380find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381{
4382 int i;
4383
4384 for (i = 0; i < tc_len; ++i)
4385 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4386 return termcodes[i].code;
4387 return NULL;
4388}
4389
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004391get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392{
4393 if (i >= tc_len)
4394 return NULL;
4395 return &termcodes[i].name[0];
4396}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004398/*
4399 * Returns the length of the terminal code at index 'idx'.
4400 */
4401 int
4402get_termcode_len(int idx)
4403{
4404 return termcodes[idx].len;
4405}
4406
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004407 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004408del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409{
4410 int i;
4411
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004412 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 return;
4414
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004415 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416
4417 for (i = 0; i < tc_len; ++i)
4418 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4419 {
4420 del_termcode_idx(i);
4421 return;
4422 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004423 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424}
4425
4426 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004427del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428{
4429 int i;
4430
4431 vim_free(termcodes[idx].code);
4432 --tc_len;
4433 for (i = idx; i < tc_len; ++i)
4434 termcodes[i] = termcodes[i + 1];
4435}
4436
4437#ifdef FEAT_TERMRESPONSE
4438/*
4439 * Called when detected that the terminal sends 8-bit codes.
4440 * Convert all 7-bit codes to their 8-bit equivalent.
4441 */
4442 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004443switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444{
4445 int i;
4446 int c;
4447
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004448 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 if (!term_is_8bit(T_NAME))
4450 {
4451 for (i = 0; i < tc_len; ++i)
4452 {
4453 c = term_7to8bit(termcodes[i].code);
4454 if (c != 0)
4455 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004456 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 termcodes[i].code[0] = c;
4458 }
4459 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004460 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004463 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464}
4465#endif
4466
4467#ifdef CHECK_DOUBLE_CLICK
4468static linenr_T orig_topline = 0;
4469# ifdef FEAT_DIFF
4470static int orig_topfill = 0;
4471# endif
4472#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004473#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004475 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 * "orig_topline" is used to avoid detecting a double-click when the window
4477 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4478 */
4479/*
4480 * Set orig_topline. Used when jumping to another window, so that a double
4481 * click still works.
4482 */
4483 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004484set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485{
4486 orig_topline = wp->w_topline;
4487# ifdef FEAT_DIFF
4488 orig_topfill = wp->w_topfill;
4489# endif
4490}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004491
4492/*
4493 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4494 * topline and topfill.
4495 */
4496 int
4497is_mouse_topline(win_T *wp)
4498{
4499 return orig_topline == wp->w_topline
4500#ifdef FEAT_DIFF
4501 && orig_topfill == wp->w_topfill
4502#endif
4503 ;
4504}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505#endif
4506
4507/*
zeertzjqfc78a032022-04-26 22:11:38 +01004508 * If "buf" is NULL put "string[new_slen]" in typebuf; "buflen" is not used.
4509 * If "buf" is not NULL put "string[new_slen]" in "buf[bufsize]" and adjust
4510 * "buflen".
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004511 * Remove "slen" bytes.
4512 * Returns FAIL for error.
4513 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004514 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004515put_string_in_typebuf(
4516 int offset,
4517 int slen,
4518 char_u *string,
4519 int new_slen,
4520 char_u *buf,
4521 int bufsize,
4522 int *buflen)
4523{
4524 int extra = new_slen - slen;
4525
4526 string[new_slen] = NUL;
4527 if (buf == NULL)
4528 {
4529 if (extra < 0)
4530 // remove matched chars, taking care of noremap
4531 del_typebuf(-extra, offset);
4532 else if (extra > 0)
4533 // insert the extra space we need
zeertzjq12e21e32022-04-27 11:58:01 +01004534 if (ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE)
4535 == FAIL)
4536 return FAIL;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004537
4538 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4539 // typebuf.tb_buf[]!
4540 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4541 (size_t)new_slen);
4542 }
4543 else
4544 {
4545 if (extra < 0)
4546 // remove matched characters
4547 mch_memmove(buf + offset, buf + offset - extra,
4548 (size_t)(*buflen + offset + extra));
4549 else if (extra > 0)
4550 {
4551 // Insert the extra space we need. If there is insufficient
4552 // space return -1.
4553 if (*buflen + extra + new_slen >= bufsize)
4554 return FAIL;
4555 mch_memmove(buf + offset + extra, buf + offset,
4556 (size_t)(*buflen - offset));
4557 }
4558 mch_memmove(buf + offset, string, (size_t)new_slen);
4559 *buflen = *buflen + extra + new_slen;
4560 }
4561 return OK;
4562}
4563
4564/*
4565 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4566 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004567 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004568decode_modifiers(int n)
4569{
4570 int code = n - 1;
4571 int modifiers = 0;
4572
4573 if (code & 1)
4574 modifiers |= MOD_MASK_SHIFT;
4575 if (code & 2)
4576 modifiers |= MOD_MASK_ALT;
4577 if (code & 4)
4578 modifiers |= MOD_MASK_CTRL;
4579 if (code & 8)
4580 modifiers |= MOD_MASK_META;
4581 return modifiers;
4582}
4583
4584 static int
4585modifiers2keycode(int modifiers, int *key, char_u *string)
4586{
4587 int new_slen = 0;
4588
4589 if (modifiers != 0)
4590 {
4591 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004592 // make mappings work. This may result in a special key, such as
4593 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004594 *key = simplify_key(*key, &modifiers);
4595 if (modifiers != 0)
4596 {
4597 string[new_slen++] = K_SPECIAL;
4598 string[new_slen++] = (int)KS_MODIFIER;
4599 string[new_slen++] = modifiers;
4600 }
4601 }
4602 return new_slen;
4603}
4604
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004605#ifdef FEAT_TERMRESPONSE
4606/*
4607 * Handle a cursor position report.
4608 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004609 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004610handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004611{
4612 if (arg[0] == 2 && arg[1] >= 2)
4613 {
4614 char *aw = NULL;
4615
4616 LOG_TR(("Received U7 status: %s", tp));
4617 u7_status.tr_progress = STATUS_GOT;
4618 did_cursorhold = TRUE;
4619 if (arg[1] == 2)
4620 aw = "single";
4621 else if (arg[1] == 3)
4622 aw = "double";
4623 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4624 {
4625 // Setting the option causes a screen redraw. Do
4626 // that right away if possible, keeping any
4627 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004628 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004629# ifdef DEBUG_TERMRESPONSE
4630 {
4631 int r = redraw_asap(CLEAR);
4632
4633 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4634 }
4635# else
4636 redraw_asap(CLEAR);
4637# endif
4638# ifdef FEAT_EVAL
4639 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4640# endif
4641 }
4642 }
4643 else if (arg[0] == 3)
4644 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004645 int value;
4646
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004647 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004648 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004649
4650 // Third row: xterm compatibility test.
4651 // If the cursor is on the first column then the terminal can handle
4652 // the request for cursor style and blinking.
4653 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4654 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4655 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004656 }
4657}
4658
4659/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004660 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004661 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004662 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004663 */
4664 static void
4665handle_version_response(int first, int *arg, int argc, char_u *tp)
4666{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004667 // The xterm version. It is set to zero when it can't be an actual xterm
4668 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004669 int version = arg[1];
4670
4671 LOG_TR(("Received CRV response: %s", tp));
4672 crv_status.tr_progress = STATUS_GOT;
4673 did_cursorhold = TRUE;
4674
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004675 // Reset terminal properties that are set based on the termresponse.
4676 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004677 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004678 init_term_props(
4679#ifdef FEAT_EVAL
4680 reset_term_props_on_termresponse
4681#else
4682 FALSE
4683#endif
4684 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004685
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004686 // If this code starts with CSI, you can bet that the
4687 // terminal uses 8-bit codes.
4688 if (tp[0] == CSI)
4689 switch_to_8bit();
4690
4691 // Screen sends 40500.
4692 // rxvt sends its version number: "20703" is 2.7.3.
4693 // Ignore it for when the user has set 'term' to xterm,
4694 // even though it's an rxvt.
4695 if (version > 20000)
4696 version = 0;
4697
zeertzjqfc78a032022-04-26 22:11:38 +01004698 // Figure out more if the response is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004699 if (first == '>' && argc == 3)
4700 {
4701 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004702
4703 // mintty 2.9.5 sends 77;20905;0c.
4704 // (77 is ASCII 'M' for mintty.)
4705 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004706 {
4707 // mintty can do SGR mouse reporting
4708 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4709 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004710
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004711 // If xterm version >= 141 try to get termcap codes. For other
4712 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004713 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004714 {
4715 LOG_TR(("Enable checking for XT codes"));
4716 check_for_codes = TRUE;
4717 need_gather = TRUE;
4718 req_codes_from_term();
4719 }
4720
4721 // libvterm sends 0;100;0
4722 if (version == 100 && arg[0] == 0 && arg[2] == 0)
4723 {
4724 // If run from Vim $COLORS is set to the number of
4725 // colors the terminal supports. Otherwise assume
4726 // 256, libvterm supports even more.
4727 if (mch_getenv((char_u *)"COLORS") == NULL)
4728 may_adjust_color_count(256);
4729 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004730 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004731 }
4732
4733 if (version == 95)
4734 {
4735 // Mac Terminal.app sends 1;95;0
4736 if (arg[0] == 1 && arg[2] == 0)
4737 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004738 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4739 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004740 }
4741 // iTerm2 sends 0;95;0
4742 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004743 {
4744 // iTerm2 can do SGR mouse reporting
4745 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4746 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004747 // old iTerm2 sends 0;95;
4748 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004749 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004750 }
4751
4752 // screen sends 83;40500;0 83 is 'S' in ASCII.
4753 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004754 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004755 // screen supports SGR mouse codes since 4.7.0
4756 if (arg[1] >= 40700)
4757 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4758 else
4759 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4760 }
4761
4762 // If no recognized terminal has set mouse behavior, assume xterm.
4763 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4764 {
4765 // Xterm version 277 supports SGR.
4766 // Xterm version >= 95 supports mouse dragging.
4767 if (version >= 277)
4768 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004769 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004770 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004771 }
4772
4773 // Detect terminals that set $TERM to something like
4774 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004775 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004776 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004777 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004778 // xfce4-terminal sends 1;2802;0.
4779 // screen sends 83;40500;0
4780 // Assuming any version number over 2500 is not an
4781 // xterm (without the limit for rxvt and screen).
4782 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004783 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004784
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004785 else if (version == 136 && arg[2] == 0)
4786 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004787 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004788
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004789 // PuTTY sends 0;136;0
4790 if (arg[0] == 0)
4791 {
4792 // supports sgr-like mouse reporting.
4793 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4794 }
4795 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004796 }
4797
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004798 // Konsole sends 0;115;0 - but t_u8 does not actually work, therefore
4799 // commented out.
4800 // else if (version == 115 && arg[0] == 0 && arg[2] == 0)
4801 // term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004802
4803 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004804 // 30600/40500 is a version number of GNU screen. DA2 support is added
4805 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4806 // compatibility checking does not detect GNU screen.
4807 if (arg[0] == 83 && arg[1] >= 30600)
4808 {
4809 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4810 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4811 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004812
4813 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004814 // 95, so assume anything below 95 is not xterm and hopefully supports
4815 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004816 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004817 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004818
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004819 // Getting the cursor style is only supported properly by xterm since
4820 // version 279 (otherwise it returns 0x18).
4821 if (version < 279)
4822 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4823
4824 /*
4825 * Take action on the detected properties.
4826 */
4827
4828 // Unless the underline RGB color is expected to work, disable "t_8u".
4829 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004830 // This may cause some flicker. Alternative would be to set "t_8u"
4831 // here if the terminal is expected to support it, but that might
4832 // conflict with what was set in the .vimrc.
Bram Moolenaardbec26d2022-04-20 19:08:50 +01004833 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES
4834 && *T_8U != NUL
4835 && !option_was_set((char_u *)"t_8u"))
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004836 {
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004837 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4838 OPT_FREE, 0);
Bram Moolenaar280aebf2022-04-17 17:34:42 +01004839 }
Bram Moolenaar366f0bd2022-04-17 19:20:33 +01004840 if (*T_8U != NUL && write_t_8u_state == MAYBE)
4841 // Did skip writing t_8u, a complete redraw is needed.
4842 redraw_later_clear();
zeertzjqfc78a032022-04-26 22:11:38 +01004843 write_t_8u_state = OK; // can output t_8u now
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004844
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004845 // Only set 'ttymouse' automatically if it was not set
4846 // by the user already.
4847 if (!option_was_set((char_u *)"ttym")
4848 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4849 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4850 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004851 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004852 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4853 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4854 }
4855
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004856 // Only request the cursor style if t_SH and t_RS are
4857 // set. Only supported properly by xterm since version
4858 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004859 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004860 // Not for Terminal.app, it can't handle t_RS, it
4861 // echoes the characters to the screen.
4862 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004863 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004864 && *T_CSH != NUL
4865 && *T_CRS != NUL)
4866 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02004867#ifdef FEAT_JOB_CHANNEL
4868 ch_log_output = TRUE;
4869#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004870 LOG_TR(("Sending cursor style request"));
4871 out_str(T_CRS);
4872 termrequest_sent(&rcs_status);
4873 need_flush = TRUE;
4874 }
4875
4876 // Only request the cursor blink mode if t_RC set. Not
4877 // for Gnome terminal, it can't handle t_RC, it
4878 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004879 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004880 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004881 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004882 && *T_CRC != NUL)
4883 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02004884#ifdef FEAT_JOB_CHANNEL
4885 ch_log_output = TRUE;
4886#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004887 LOG_TR(("Sending cursor blink mode request"));
4888 out_str(T_CRC);
4889 termrequest_sent(&rbm_status);
4890 need_flush = TRUE;
4891 }
4892
4893 if (need_flush)
4894 out_flush();
4895 }
4896}
4897
4898/*
4899 * Handle a sequence with key and modifier, one of:
4900 * {lead}27;{modifier};{key}~
4901 * {lead}{key};{modifier}u
4902 * Returns the difference in length.
4903 */
4904 static int
4905handle_key_with_modifier(
4906 int *arg,
4907 int trail,
4908 int csi_len,
4909 int offset,
4910 char_u *buf,
4911 int bufsize,
4912 int *buflen)
4913{
4914 int key;
4915 int modifiers;
4916 int new_slen;
4917 char_u string[MAX_KEY_CODE_LEN + 1];
4918
4919 seenModifyOtherKeys = TRUE;
4920 if (trail == 'u')
4921 key = arg[0];
4922 else
4923 key = arg[2];
4924
4925 modifiers = decode_modifiers(arg[1]);
4926
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02004927 // Some keys need adjustment when the Ctrl modifier is used.
4928 key = may_adjust_key_for_ctrl(modifiers, key);
4929
Bram Moolenaaref6746f2020-06-20 14:43:23 +02004930 // May remove the shift modifier if it's already included in the key.
4931 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004932
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004933 // insert modifiers with KS_MODIFIER
4934 new_slen = modifiers2keycode(modifiers, &key, string);
4935
Bram Moolenaar749bc952020-10-31 16:33:47 +01004936 if (IS_SPECIAL(key))
4937 {
4938 string[new_slen++] = K_SPECIAL;
4939 string[new_slen++] = KEY2TERMCAP0(key);
4940 string[new_slen++] = KEY2TERMCAP1(key);
4941 }
4942 else if (has_mbyte)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004943 new_slen += (*mb_char2bytes)(key, string + new_slen);
4944 else
4945 string[new_slen++] = key;
4946
4947 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4948 buf, bufsize, buflen) == FAIL)
4949 return -1;
4950 return new_slen - csi_len + offset;
4951}
4952
4953/*
4954 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004955 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004956 *
4957 * - Cursor position report: {lead}{row};{col}R
4958 * The final byte must be 'R'. It is used for checking the
4959 * ambiguous-width character state.
4960 *
4961 * - window position reply: {lead}3;{x};{y}t
4962 *
4963 * - key with modifiers when modifyOtherKeys is enabled:
4964 * {lead}27;{modifier};{key}~
4965 * {lead}{key};{modifier}u
4966 * Return 0 for no match, -1 for partial match, > 0 for full match.
4967 */
4968 static int
4969handle_csi(
4970 char_u *tp,
4971 int len,
4972 char_u *argp,
4973 int offset,
4974 char_u *buf,
4975 int bufsize,
4976 int *buflen,
4977 char_u *key_name,
4978 int *slen)
4979{
4980 int first = -1; // optional char right after {lead}
4981 int trail; // char that ends CSI sequence
4982 int arg[3] = {-1, -1, -1}; // argument numbers
4983 int argc; // number of arguments
4984 char_u *ap = argp;
4985 int csi_len;
4986
4987 // Check for non-digit after CSI.
4988 if (!VIM_ISDIGIT(*ap))
4989 first = *ap++;
4990
4991 // Find up to three argument numbers.
4992 for (argc = 0; argc < 3; )
4993 {
4994 if (ap >= tp + len)
4995 return -1;
4996 if (*ap == ';')
4997 arg[argc++] = -1; // omitted number
4998 else if (VIM_ISDIGIT(*ap))
4999 {
5000 arg[argc] = 0;
5001 for (;;)
5002 {
5003 if (ap >= tp + len)
5004 return -1;
5005 if (!VIM_ISDIGIT(*ap))
5006 break;
5007 arg[argc] = arg[argc] * 10 + (*ap - '0');
5008 ++ap;
5009 }
5010 ++argc;
5011 }
5012 if (*ap == ';')
5013 ++ap;
5014 else
5015 break;
5016 }
5017
5018 // mrxvt has been reported to have "+" in the version. Assume
5019 // the escape sequence ends with a letter or one of "{|}~".
5020 while (ap < tp + len
5021 && !(*ap >= '{' && *ap <= '~')
5022 && !ASCII_ISALPHA(*ap))
5023 ++ap;
5024 if (ap >= tp + len)
5025 return -1;
5026 trail = *ap;
5027 csi_len = (int)(ap - tp) + 1;
5028
5029 // Cursor position report: Eat it when there are 2 arguments
5030 // and it ends in 'R'. Also when u7_status is not "sent", it
5031 // may be from a previous Vim that just exited. But not for
5032 // <S-F3>, it sends something similar, check for row and column
5033 // to make sense.
5034 if (first == -1 && argc == 2 && trail == 'R')
5035 {
5036 handle_u7_response(arg, tp, csi_len);
5037
5038 key_name[0] = (int)KS_EXTRA;
5039 key_name[1] = (int)KE_IGNORE;
5040 *slen = csi_len;
5041 }
5042
5043 // Version string: Eat it when there is at least one digit and
5044 // it ends in 'c'
5045 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5046 {
5047 handle_version_response(first, arg, argc, tp);
5048
5049 *slen = csi_len;
5050# ifdef FEAT_EVAL
5051 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
5052# endif
5053 apply_autocmds(EVENT_TERMRESPONSE,
5054 NULL, NULL, FALSE, curbuf);
5055 key_name[0] = (int)KS_EXTRA;
5056 key_name[1] = (int)KE_IGNORE;
5057 }
5058
5059 // Check blinking cursor from xterm:
5060 // {lead}?12;1$y set
5061 // {lead}?12;2$y not set
5062 //
5063 // {lead} can be <Esc>[ or CSI
5064 else if (rbm_status.tr_progress == STATUS_SENT
5065 && first == '?'
5066 && ap == argp + 6
5067 && arg[0] == 12
5068 && ap[-1] == '$'
5069 && trail == 'y')
5070 {
5071 initial_cursor_blink = (arg[1] == '1');
5072 rbm_status.tr_progress = STATUS_GOT;
5073 LOG_TR(("Received cursor blinking mode response: %s", tp));
5074 key_name[0] = (int)KS_EXTRA;
5075 key_name[1] = (int)KE_IGNORE;
5076 *slen = csi_len;
5077# ifdef FEAT_EVAL
5078 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
5079# endif
5080 }
5081
5082 // Check for a window position response from the terminal:
5083 // {lead}3;{x};{y}t
5084 else if (did_request_winpos && argc == 3 && arg[0] == 3
5085 && trail == 't')
5086 {
5087 winpos_x = arg[1];
5088 winpos_y = arg[2];
5089 // got finished code: consume it
5090 key_name[0] = (int)KS_EXTRA;
5091 key_name[1] = (int)KE_IGNORE;
5092 *slen = csi_len;
5093
5094 if (--did_request_winpos <= 0)
5095 winpos_status.tr_progress = STATUS_GOT;
5096 }
5097
5098 // Key with modifier:
5099 // {lead}27;{modifier};{key}~
5100 // {lead}{key};{modifier}u
5101 else if ((arg[0] == 27 && argc == 3 && trail == '~')
5102 || (argc == 2 && trail == 'u'))
5103 {
5104 return len + handle_key_with_modifier(arg, trail,
5105 csi_len, offset, buf, bufsize, buflen);
5106 }
5107
5108 // else: Unknown CSI sequence. We could drop it, but then the
5109 // user can't create a map for it.
5110 return 0;
5111}
5112
5113/*
5114 * Handle an OSC sequence, fore/background color response from the terminal:
5115 *
5116 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5117 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5118 *
5119 * {code} is 10 for foreground, 11 for background
5120 * {lead} can be <Esc>] or OSC
5121 * {tail} can be '\007', <Esc>\ or STERM.
5122 *
5123 * Consume any code that starts with "{lead}11;", it's also
5124 * possible that "rgba" is following.
5125 */
5126 static int
5127handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5128{
5129 int i, j;
5130
5131 j = 1 + (tp[0] == ESC);
5132 if (len >= j + 3 && (argp[0] != '1'
5133 || (argp[1] != '1' && argp[1] != '0')
5134 || argp[2] != ';'))
5135 i = 0; // no match
5136 else
5137 for (i = j; i < len; ++i)
5138 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5139 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5140 {
5141 int is_bg = argp[1] == '1';
5142 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5143 && tp[j + 16] == '/';
5144
5145 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5146 && (is_4digit
5147 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5148 {
5149 char_u *tp_r = tp + j + 7;
5150 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5151 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
5152# ifdef FEAT_TERMINAL
5153 int rval, gval, bval;
5154
5155 rval = hexhex2nr(tp_r);
5156 gval = hexhex2nr(tp_b);
5157 bval = hexhex2nr(tp_g);
5158# endif
5159 if (is_bg)
5160 {
5161 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5162 *tp_b) ? "light" : "dark";
5163
5164 LOG_TR(("Received RBG response: %s", tp));
5165 rbg_status.tr_progress = STATUS_GOT;
5166# ifdef FEAT_TERMINAL
5167 bg_r = rval;
5168 bg_g = gval;
5169 bg_b = bval;
5170# endif
5171 if (!option_was_set((char_u *)"bg")
5172 && STRCMP(p_bg, new_bg_val) != 0)
5173 {
5174 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005175 set_option_value_give_err((char_u *)"bg",
5176 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005177 reset_option_was_set((char_u *)"bg");
5178 redraw_asap(CLEAR);
5179 }
5180 }
5181# ifdef FEAT_TERMINAL
5182 else
5183 {
5184 LOG_TR(("Received RFG response: %s", tp));
5185 rfg_status.tr_progress = STATUS_GOT;
5186 fg_r = rval;
5187 fg_g = gval;
5188 fg_b = bval;
5189 }
5190# endif
5191 }
5192
5193 // got finished code: consume it
5194 key_name[0] = (int)KS_EXTRA;
5195 key_name[1] = (int)KE_IGNORE;
5196 *slen = i + 1 + (tp[i] == ESC);
5197# ifdef FEAT_EVAL
5198 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5199 : VV_TERMRFGRESP, tp, *slen);
5200# endif
5201 break;
5202 }
5203 if (i == len)
5204 {
5205 LOG_TR(("not enough characters for RB"));
5206 return FAIL;
5207 }
5208 return OK;
5209}
5210
5211/*
5212 * Check for key code response from xterm:
5213 * {lead}{flag}+r<hex bytes><{tail}
5214 *
5215 * {lead} can be <Esc>P or DCS
5216 * {flag} can be '0' or '1'
5217 * {tail} can be Esc>\ or STERM
5218 *
5219 * Check for cursor shape response from xterm:
5220 * {lead}1$r<digit> q{tail}
5221 *
5222 * {lead} can be <Esc>P or DCS
5223 * {tail} can be <Esc>\ or STERM
5224 *
5225 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5226 */
5227 static int
5228handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5229{
5230 int i, j;
5231
5232 j = 1 + (tp[0] == ESC);
5233 if (len < j + 3)
5234 i = len; // need more chars
5235 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
5236 i = 0; // no match
5237 else if (argp[1] == '+')
5238 // key code response
5239 for (i = j; i < len; ++i)
5240 {
5241 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5242 || tp[i] == STERM)
5243 {
5244 if (i - j >= 3)
5245 got_code_from_term(tp + j, i);
5246 key_name[0] = (int)KS_EXTRA;
5247 key_name[1] = (int)KE_IGNORE;
5248 *slen = i + 1 + (tp[i] == ESC);
5249 break;
5250 }
5251 }
5252 else
5253 {
5254 // Probably the cursor shape response. Make sure that "i"
5255 // is equal to "len" when there are not sufficient
5256 // characters.
5257 for (i = j + 3; i < len; ++i)
5258 {
5259 if (i - j == 3 && !isdigit(tp[i]))
5260 break;
5261 if (i - j == 4 && tp[i] != ' ')
5262 break;
5263 if (i - j == 5 && tp[i] != 'q')
5264 break;
5265 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5266 break;
5267 if ((i - j == 6 && tp[i] == STERM)
5268 || (i - j == 7 && tp[i] == '\\'))
5269 {
5270 int number = argp[3] - '0';
5271
5272 // 0, 1 = block blink, 2 = block
5273 // 3 = underline blink, 4 = underline
5274 // 5 = vertical bar blink, 6 = vertical bar
5275 number = number == 0 ? 1 : number;
5276 initial_cursor_shape = (number + 1) / 2;
5277 // The blink flag is actually inverted, compared to
5278 // the value set with T_SH.
5279 initial_cursor_shape_blink =
5280 (number & 1) ? FALSE : TRUE;
5281 rcs_status.tr_progress = STATUS_GOT;
5282 LOG_TR(("Received cursor shape response: %s", tp));
5283
5284 key_name[0] = (int)KS_EXTRA;
5285 key_name[1] = (int)KE_IGNORE;
5286 *slen = i + 1;
5287# ifdef FEAT_EVAL
5288 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5289# endif
5290 break;
5291 }
5292 }
5293 }
5294
5295 if (i == len)
5296 {
5297 // These codes arrive many together, each code can be
5298 // truncated at any point.
5299 LOG_TR(("not enough characters for XT"));
5300 return FAIL;
5301 }
5302 return OK;
5303}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005304#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005305
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 * Check if typebuf.tb_buf[] contains a terminal key code.
5308 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005309 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005311 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 * With a match, the match is removed, the replacement code is inserted in
5313 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5314 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005315 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5316 * "buflen" is then the length of the string in buf[] and is updated for
5317 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 */
5319 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005320check_termcode(
5321 int max_offset,
5322 char_u *buf,
5323 int bufsize,
5324 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325{
5326 char_u *tp;
5327 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005328 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005329 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005331 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 int offset;
5333 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005334 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005335 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005336 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005337 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 char_u string[MAX_KEY_CODE_LEN + 1];
5339 int i, j;
5340 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342
5343 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5344
5345 /*
5346 * Speed up the checks for terminal codes by gathering all first bytes
5347 * used in termleader[]. Often this is just a single <Esc>.
5348 */
5349 if (need_gather)
5350 gather_termleader();
5351
5352 /*
5353 * Check at several positions in typebuf.tb_buf[], to catch something like
5354 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5355 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005356 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 * This is used often, KEEP IT FAST!
5358 */
5359 for (offset = 0; offset < max_offset; ++offset)
5360 {
5361 if (buf == NULL)
5362 {
5363 if (offset >= typebuf.tb_len)
5364 break;
5365 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005366 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 }
5368 else
5369 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005370 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 break;
5372 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005373 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375
5376 /*
5377 * Don't check characters after K_SPECIAL, those are already
5378 * translated terminal chars (avoid translating ~@^Hx).
5379 */
5380 if (*tp == K_SPECIAL)
5381 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005382 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 continue;
5384 }
5385
5386 /*
5387 * Skip this position if the character does not appear as the first
5388 * character in term_strings. This speeds up a lot, since most
5389 * termcodes start with the same character (ESC or CSI).
5390 */
5391 i = *tp;
5392 for (p = termleader; *p && *p != i; ++p)
5393 ;
5394 if (*p == NUL)
5395 continue;
5396
5397 /*
5398 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5399 * are in Insert mode.
5400 */
5401 if (*tp == ESC && !p_ek && (State & INSERT))
5402 continue;
5403
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005404 key_name[0] = NUL; // no key name found yet
5405 key_name[1] = NUL; // no key name found yet
5406 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407
5408#ifdef FEAT_GUI
5409 if (gui.in_use)
5410 {
5411 /*
5412 * GUI special key codes are all of the form [CSI xx].
5413 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005414 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 {
5416 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005417 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 slen = 3;
5419 key_name[0] = tp[1];
5420 key_name[1] = tp[2];
5421 }
5422 }
5423 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005424#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005426 int mouse_index_found = -1;
5427
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 for (idx = 0; idx < tc_len; ++idx)
5429 {
5430 /*
5431 * Ignore the entry if we are not at the start of
5432 * typebuf.tb_buf[]
5433 * and there are not enough characters to make a match.
5434 * But only when the 'K' flag is in 'cpoptions'.
5435 */
5436 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005437 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 if (cpo_koffset && offset && len < slen)
5439 continue;
5440 if (STRNCMP(termcodes[idx].code, tp,
5441 (size_t)(slen > len ? len : slen)) == 0)
5442 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005443 int looks_like_mouse_start = FALSE;
5444
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005445 if (len < slen) // got a partial sequence
5446 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447
5448 /*
5449 * When found a keypad key, check if there is another key
5450 * that matches and use that one. This makes <Home> to be
5451 * found instead of <kHome> when they produce the same
5452 * key code.
5453 */
5454 if (termcodes[idx].name[0] == 'K'
5455 && VIM_ISDIGIT(termcodes[idx].name[1]))
5456 {
5457 for (j = idx + 1; j < tc_len; ++j)
5458 if (termcodes[j].len == slen &&
5459 STRNCMP(termcodes[idx].code,
5460 termcodes[j].code, slen) == 0)
5461 {
5462 idx = j;
5463 break;
5464 }
5465 }
5466
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005467 if (slen == 2 && len > 2
5468 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005469 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005470 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005471 // The mouse termcode "ESC [" is also the prefix of
5472 // "ESC [ I" (focus gained) and other keys. Check some
5473 // more bytes to find out.
5474 if (!isdigit(tp[2]))
5475 {
5476 // ESC [ without number following: Only use it when
5477 // there is no other match.
5478 looks_like_mouse_start = TRUE;
5479 }
5480 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5481 {
5482 char_u *nr = tp + 2;
5483 int count = 0;
5484
5485 // If a digit is following it could be a key with
5486 // modifier, e.g., ESC [ 1;2P. Can be confused
5487 // with DEC_MOUSE, which requires four numbers
5488 // following. If not then it can't be a DEC_MOUSE
5489 // code.
5490 for (;;)
5491 {
5492 ++count;
5493 (void)getdigits(&nr);
5494 if (nr >= tp + len)
5495 return -1; // partial sequence
5496 if (*nr != ';')
5497 break;
5498 ++nr;
5499 if (nr >= tp + len)
5500 return -1; // partial sequence
5501 }
5502 if (count < 4)
5503 continue; // no match
5504 }
5505 }
5506 if (looks_like_mouse_start)
5507 {
5508 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005509 if (mouse_index_found < 0)
5510 mouse_index_found = idx;
5511 }
5512 else
5513 {
5514 key_name[0] = termcodes[idx].name[0];
5515 key_name[1] = termcodes[idx].name[1];
5516 break;
5517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005519
5520 /*
5521 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005522 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005523 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005524 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5525 * When there is a modifier the * matches a number.
5526 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005527 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005528 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005529 {
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005530 int at_code;
5531
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005532 modslen = termcodes[idx].modlen;
5533 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005534 continue;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005535 at_code = termcodes[idx].code[modslen] == '@';
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005536 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005537 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005538 {
5539 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005540
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005541 if (len <= modslen) // got a partial sequence
5542 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005543
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005544 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005545 // no modifiers
5546 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005547 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005548 // no match for "code;*X" with "code;"
5549 continue;
5550 else if (at_code && tp[modslen] != '1')
5551 // no match for "<Esc>[@" with "<Esc>[1"
5552 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005553 else
5554 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005555 // Skip over the digits, the final char must
5556 // follow. URXVT can use a negative value, thus
5557 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005558 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005559 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005560 ;
5561 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005562 if (len < j) // got a partial sequence
5563 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005564 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005565 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005566
Bram Moolenaara529ce02017-06-22 22:37:57 +02005567 modifiers_start = tp + slen - 2;
5568
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005569 // Match! Convert modifier bits.
5570 n = atoi((char *)modifiers_start);
5571 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005572
5573 slen = j;
5574 }
5575 key_name[0] = termcodes[idx].name[0];
5576 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005577 break;
5578 }
5579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005581 if (idx == tc_len && mouse_index_found >= 0)
5582 {
5583 key_name[0] = termcodes[mouse_index_found].name[0];
5584 key_name[1] = termcodes[mouse_index_found].name[1];
5585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587
5588#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005589 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005590 // Mouse codes of DEC and pterm start with <ESC>[. When
5591 // detecting the start of these mouse codes they might as well be
5592 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005593# ifdef FEAT_MOUSE_DEC
5594 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005595# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005596# ifdef FEAT_MOUSE_PTERM
5597 || key_name[0] == KS_PTERM_MOUSE
5598# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005599 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005601 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5602
5603 /*
5604 * Check for responses from the terminal starting with {lead}:
5605 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005606 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005607 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005608 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005609 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005610 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005611 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005612 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005613 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005614 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005615 * - window position reply: {lead}3;{x};{y}t
5616 *
5617 * - key with modifiers when modifyOtherKeys is enabled:
5618 * {lead}27;{modifier};{key}~
5619 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005620 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005621 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005622 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005623 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005625 int resp = handle_csi(tp, len, argp, offset, buf,
5626 bufsize, buflen, key_name, &slen);
5627 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005628 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005629# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005630 if (resp == -1)
5631 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005632# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005633 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005634 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005635 }
5636
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005637 // Check for fore/background color response from the terminal,
5638 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005639 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005640 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5641 || tp[0] == OSC))
5642 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005643 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005644 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 }
5646
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005647 // Check for key code response from xterm,
5648 // starting with <Esc>P or DCS
Bram Moolenaarafd78262019-05-10 23:10:31 +02005649 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005650 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 || tp[0] == DCS))
5652 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005653 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005654 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 }
5656 }
5657#endif
5658
5659 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005660 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005662 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005664#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 /*
5666 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5667 * so that we know which window to scroll later.
5668 */
5669 if (gui.in_use
5670 && key_name[0] == (int)KS_EXTRA
5671 && (key_name[1] == (int)KE_X1MOUSE
5672 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005673 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005674 || key_name[1] == (int)KE_MOUSELEFT
5675 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 || key_name[1] == (int)KE_MOUSEDOWN
5677 || key_name[1] == (int)KE_MOUSEUP))
5678 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005679 char_u bytes[6];
5680 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5681
5682 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 return -1;
5684 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5685 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5686 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005687 // equal to K_MOUSEMOVE
5688 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
5689 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 /*
5694 * If it is a mouse click, get the coordinates.
5695 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005696 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005697#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005698 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005699#endif
5700#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005701 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005702#endif
5703#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005704 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005705#endif
5706#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005707 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005708#endif
5709#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005710 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005711#endif
5712#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005713 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005714#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005715 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005716 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005718 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5719 &modifiers) == -1)
5720 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
5723#ifdef FEAT_GUI
5724 /*
5725 * If using the GUI, then we get menu and scrollbar events.
5726 *
5727 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5728 * four bytes which are to be taken as a pointer to the vimmenu_T
5729 * structure.
5730 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005731 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005732 * is one byte with the tab index.
5733 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5735 * by one byte representing the scrollbar number, and then four bytes
5736 * representing a long_u which is the new value of the scrollbar.
5737 *
5738 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5739 * KE_FILLER followed by four bytes representing a long_u which is the
5740 * new value of the scrollbar.
5741 */
5742# ifdef FEAT_MENU
5743 else if (key_name[0] == (int)KS_MENU)
5744 {
5745 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005746 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 if (num_bytes == -1)
5749 return -1;
5750 current_menu = (vimmenu_T *)val;
5751 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005752
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005753 // The menu may have been deleted right after it was used, check
5754 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005755 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5756 {
5757 key_name[0] = KS_EXTRA;
5758 key_name[1] = (int)KE_IGNORE;
5759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
5761# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005762# ifdef FEAT_GUI_TABLINE
5763 else if (key_name[0] == (int)KS_TABLINE)
5764 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005765 // Selecting tabline tab or using its menu.
5766 char_u bytes[6];
5767 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5768
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005769 if (num_bytes == -1)
5770 return -1;
5771 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005772 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005773 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005774 slen += num_bytes;
5775 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005776 else if (key_name[0] == (int)KS_TABMENU)
5777 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005778 // Selecting tabline tab or using its menu.
5779 char_u bytes[6];
5780 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5781
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005782 if (num_bytes == -1)
5783 return -1;
5784 current_tab = (int)bytes[0];
5785 current_tabmenu = (int)bytes[1];
5786 slen += num_bytes;
5787 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789# ifndef USE_ON_FLY_SCROLL
5790 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5791 {
5792 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005793 char_u bytes[6];
5794 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005796 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 j = 0;
5798 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5799 && tp[j + 2] != NUL; ++i)
5800 {
5801 j += 3;
5802 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5803 if (num_bytes == -1)
5804 break;
5805 if (i == 0)
5806 current_scrollbar = (int)bytes[0];
5807 else if (current_scrollbar != (int)bytes[0])
5808 break;
5809 j += num_bytes;
5810 num_bytes = get_long_from_buf(tp + j, &val);
5811 if (num_bytes == -1)
5812 break;
5813 scrollbar_value = val;
5814 j += num_bytes;
5815 slen = j;
5816 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005817 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 return -1;
5819 }
5820 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5821 {
5822 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005823 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005825 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 j = 0;
5827 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5828 && tp[j + 2] != NUL; ++i)
5829 {
5830 j += 3;
5831 num_bytes = get_long_from_buf(tp + j, &val);
5832 if (num_bytes == -1)
5833 break;
5834 scrollbar_value = val;
5835 j += num_bytes;
5836 slen = j;
5837 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005838 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 return -1;
5840 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005841# endif // !USE_ON_FLY_SCROLL
5842#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005844#if (defined(UNIX) || defined(VMS))
5845 /*
5846 * Handle FocusIn/FocusOut event sequences reported by XTerm.
5847 * (CSI I/CSI O)
5848 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00005849 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005850# ifdef FEAT_GUI
5851 && !gui.in_use
5852# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005853 )
5854 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005855 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005856 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005857 if (!focus_state)
5858 {
5859 ui_focus_change(TRUE);
5860 did_cursorhold = TRUE;
5861 focus_state = TRUE;
5862 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005863 key_name[1] = (int)KE_IGNORE;
5864 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01005865 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005866 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005867 if (focus_state)
5868 {
5869 ui_focus_change(FALSE);
5870 did_cursorhold = TRUE;
5871 focus_state = FALSE;
5872 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005873 key_name[1] = (int)KE_IGNORE;
5874 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005875 }
5876#endif
5877
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005878 /*
5879 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5880 */
5881 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5882
5883 /*
5884 * Add any modifier codes to our string.
5885 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005886 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005887
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005888 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005889 key_name[0] = KEY2TERMCAP0(key);
5890 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005892 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005893 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005894 if (has_mbyte)
5895 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5896 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005897 string[new_slen++] = key_name[1];
5898 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005899 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5900 && key_name[1] == KE_IGNORE)
5901 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005902 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5903 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005904 retval = KEYLEN_REMOVED;
5905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 else
5907 {
5908 string[new_slen++] = K_SPECIAL;
5909 string[new_slen++] = key_name[0];
5910 string[new_slen++] = key_name[1];
5911 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005912 if (put_string_in_typebuf(offset, slen, string, new_slen,
5913 buf, bufsize, buflen) == FAIL)
5914 return -1;
5915 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 }
5917
Bram Moolenaar2951b772013-07-03 12:45:31 +02005918#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005919 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005920#endif
5921
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005922 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923}
5924
Bram Moolenaar9377df32017-10-15 13:22:01 +02005925#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005926/*
5927 * Get the text foreground color, if known.
5928 */
5929 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005930term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005931{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005932 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005933 {
5934 *r = fg_r;
5935 *g = fg_g;
5936 *b = fg_b;
5937 }
5938}
5939
5940/*
5941 * Get the text background color, if known.
5942 */
5943 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005944term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005945{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005946 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005947 {
5948 *r = bg_r;
5949 *g = bg_g;
5950 *b = bg_b;
5951 }
5952}
5953#endif
5954
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955/*
5956 * Replace any terminal code strings in from[] with the equivalent internal
5957 * vim representation. This is used for the "from" and "to" part of a
5958 * mapping, and the "to" part of a menu command.
5959 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5960 * '<'.
5961 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5962 *
5963 * The replacement is done in result[] and finally copied into allocated
5964 * memory. If this all works well *bufp is set to the allocated memory and a
5965 * pointer to it is returned. If something fails *bufp is set to NULL and from
5966 * is returned.
5967 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005968 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5969 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5970 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5971 * used instead of a CTRL-V.
5972 *
5973 * Flags:
5974 * REPTERM_FROM_PART see above
5975 * REPTERM_DO_LT also translate <lt>
5976 * REPTERM_SPECIAL always accept <key> notation
5977 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5978 *
5979 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5980 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005982 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005983replace_termcodes(
5984 char_u *from,
5985 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005986 int flags,
5987 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 int i;
5990 int slen;
5991 int key;
Bram Moolenaara9549c92022-04-17 14:18:11 +01005992 size_t dlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005994 int do_backslash; // backslash is a special character
5995 int do_special; // recognize <> key codes
5996 int do_key_code; // recognize raw key codes
5997 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01005998 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
6000 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02006001 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
6002 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01006004 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005
6006 /*
6007 * Allocate space for the translation. Worst case a single character is
6008 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01006009 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01006011 ga_init2(&ga, 1L, 100);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006012 if (ga_grow(&ga, (int)(STRLEN(src) * 6 + 1)) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 {
6014 *bufp = NULL;
6015 return from;
6016 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01006017 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018
6019 /*
6020 * Check for #n at start only: function key n
6021 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006022 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 {
6024 result[dlen++] = K_SPECIAL;
6025 result[dlen++] = 'k';
6026 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006027 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006029 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 src += 2;
6031 }
6032
6033 /*
6034 * Copy each byte from *from to result[dlen]
6035 */
6036 while (*src != NUL)
6037 {
6038 /*
6039 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006040 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006042 if (do_special && ((flags & REPTERM_DO_LT)
6043 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 {
6045#ifdef FEAT_EVAL
6046 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006047 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6048 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006050 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6051 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 */
6053 if (STRNICMP(src, "<SID>", 5) == 0)
6054 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006055 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006056 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 else
6058 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006059 char_u *dot;
6060 long sid = current_sctx.sc_sid;
6061
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006063 if (in_vim9script()
6064 && (dot = vim_strchr(src, '.')) != NULL)
6065 {
6066 imported_T *imp = find_imported(src, dot - src, FALSE);
6067
6068 if (imp != NULL)
6069 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006070 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6071 size_t len;
6072
Bram Moolenaar89445512022-04-14 12:58:23 +01006073 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006074 if (si->sn_autoload_prefix != NULL)
6075 {
6076 // Turn "<SID>name.Func"
6077 // into "scriptname#Func".
6078 len = STRLEN(si->sn_autoload_prefix);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006079 if (ga_grow(&ga,
6080 (int)(STRLEN(src) * 6 + len + 1)) == FAIL)
Bram Moolenaar648dd882022-04-14 21:36:15 +01006081 {
6082 ga_clear(&ga);
6083 *bufp = NULL;
6084 return from;
6085 }
6086 result = ga.ga_data;
6087 STRCPY(result + dlen, si->sn_autoload_prefix);
6088 dlen += len;
6089 continue;
6090 }
6091 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006092 }
6093 }
6094
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 result[dlen++] = K_SPECIAL;
6096 result[dlen++] = (int)KS_EXTRA;
6097 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006098 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaara9549c92022-04-17 14:18:11 +01006099 dlen += STRLEN(result + dlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 result[dlen++] = '_';
6101 continue;
6102 }
6103 }
6104#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006105 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6106 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
6107 did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 if (slen)
6109 {
6110 dlen += slen;
6111 continue;
6112 }
6113 }
6114
6115 /*
6116 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6117 * Note that this is also checked after replacing the <> form.
6118 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6119 * it could be a character in the file.
6120 */
6121 if (do_key_code)
6122 {
6123 i = find_term_bykeys(src);
6124 if (i >= 0)
6125 {
6126 result[dlen++] = K_SPECIAL;
6127 result[dlen++] = termcodes[i].name[0];
6128 result[dlen++] = termcodes[i].name[1];
6129 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006130 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 continue;
6132 }
6133 }
6134
6135#ifdef FEAT_EVAL
6136 if (do_special)
6137 {
6138 char_u *p, *s, len;
6139
6140 /*
6141 * Replace <Leader> by the value of "mapleader".
6142 * Replace <LocalLeader> by the value of "maplocalleader".
6143 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6144 */
6145 if (STRNICMP(src, "<Leader>", 8) == 0)
6146 {
6147 len = 8;
6148 p = get_var_value((char_u *)"g:mapleader");
6149 }
6150 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6151 {
6152 len = 13;
6153 p = get_var_value((char_u *)"g:maplocalleader");
6154 }
6155 else
6156 {
6157 len = 0;
6158 p = NULL;
6159 }
6160 if (len != 0)
6161 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006162 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6164 s = (char_u *)"\\";
6165 else
6166 s = p;
6167 while (*s != NUL)
6168 result[dlen++] = *s++;
6169 src += len;
6170 continue;
6171 }
6172 }
6173#endif
6174
6175 /*
6176 * Remove CTRL-V and ignore the next character.
6177 * For "from" side the CTRL-V at the end is included, for the "to"
6178 * part it is removed.
6179 * If 'cpoptions' does not contain 'B', also accept a backslash.
6180 */
6181 key = *src;
6182 if (key == Ctrl_V || (do_backslash && key == '\\'))
6183 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006184 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (*src == NUL)
6186 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006187 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 result[dlen++] = key;
6189 break;
6190 }
6191 }
6192
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006193 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006194 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 {
6196 /*
6197 * If the character is K_SPECIAL, replace it with K_SPECIAL
6198 * KS_SPECIAL KE_FILLER.
6199 * If compiled with the GUI replace CSI with K_CSI.
6200 */
6201 if (*src == K_SPECIAL)
6202 {
6203 result[dlen++] = K_SPECIAL;
6204 result[dlen++] = KS_SPECIAL;
6205 result[dlen++] = KE_FILLER;
6206 }
6207# ifdef FEAT_GUI
6208 else if (*src == CSI)
6209 {
6210 result[dlen++] = K_SPECIAL;
6211 result[dlen++] = KS_EXTRA;
6212 result[dlen++] = (int)KE_CSI;
6213 }
6214# endif
6215 else
6216 result[dlen++] = *src;
6217 ++src;
6218 }
6219 }
6220 result[dlen] = NUL;
6221
6222 /*
6223 * Copy the new string to allocated memory.
6224 * If this fails, just return from.
6225 */
6226 if ((*bufp = vim_strsave(result)) != NULL)
6227 from = *bufp;
6228 vim_free(result);
6229 return from;
6230}
6231
6232/*
6233 * Find a termcode with keys 'src' (must be NUL terminated).
6234 * Return the index in termcodes[], or -1 if not found.
6235 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006236 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006237find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
6239 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006240 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
6242 for (i = 0; i < tc_len; ++i)
6243 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006244 if (slen == termcodes[i].len
6245 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 return i;
6247 }
6248 return -1;
6249}
6250
6251/*
6252 * Gather the first characters in the terminal key codes into a string.
6253 * Used to speed up check_termcode().
6254 */
6255 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006256gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257{
6258 int i;
6259 int len = 0;
6260
6261#ifdef FEAT_GUI
6262 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006263 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264#endif
6265#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006266 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006267 termleader[len++] = DCS; // the termcode response starts with DCS
6268 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269#endif
6270 termleader[len] = NUL;
6271
6272 for (i = 0; i < tc_len; ++i)
6273 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6274 {
6275 termleader[len++] = termcodes[i].code[0];
6276 termleader[len] = NUL;
6277 }
6278
6279 need_gather = FALSE;
6280}
6281
6282/*
6283 * Show all termcodes (for ":set termcap")
6284 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006285 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 */
6287 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006288show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
6290 int col;
6291 int *items;
6292 int item_count;
6293 int run;
6294 int row, rows;
6295 int cols;
6296 int i;
6297 int len;
6298
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006299#define INC3 27 // try to make three columns
6300#define INC2 40 // try to make two columns
6301#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006303 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006305 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 if (items == NULL)
6307 return;
6308
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006309 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006310 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311
6312 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006313 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006315 * 2. display the medium items (medium length strings)
6316 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006317 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006319 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 {
6321 /*
6322 * collect the items in items[]
6323 */
6324 item_count = 0;
6325 for (i = 0; i < tc_len; i++)
6326 {
6327 len = show_one_termcode(termcodes[i].name,
6328 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006329 if ((flags & OPT_ONECOLUMN) ||
6330 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006331 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006332 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 items[item_count++] = i;
6334 }
6335
6336 /*
6337 * display the items
6338 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006339 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006341 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 if (cols == 0)
6343 cols = 1;
6344 rows = (item_count + cols - 1) / cols;
6345 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006346 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 rows = item_count;
6348 for (row = 0; row < rows && !got_int; ++row)
6349 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006350 msg_putchar('\n'); // go to next line
6351 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 break;
6353 col = 0;
6354 for (i = row; i < item_count; i += rows)
6355 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006356 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 show_one_termcode(termcodes[items[i]].name,
6358 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006359 if (run == 2)
6360 col += INC2;
6361 else
6362 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 }
6364 out_flush();
6365 ui_breakcheck();
6366 }
6367 }
6368 vim_free(items);
6369}
6370
6371/*
6372 * Show one termcode entry.
6373 * Output goes into IObuff[]
6374 */
6375 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006376show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377{
6378 char_u *p;
6379 int len;
6380
6381 if (name[0] > '~')
6382 {
6383 IObuff[0] = ' ';
6384 IObuff[1] = ' ';
6385 IObuff[2] = ' ';
6386 IObuff[3] = ' ';
6387 }
6388 else
6389 {
6390 IObuff[0] = 't';
6391 IObuff[1] = '_';
6392 IObuff[2] = name[0];
6393 IObuff[3] = name[1];
6394 }
6395 IObuff[4] = ' ';
6396
6397 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6398 if (p[1] != 't')
6399 STRCPY(IObuff + 5, p);
6400 else
6401 IObuff[5] = NUL;
6402 len = (int)STRLEN(IObuff);
6403 do
6404 IObuff[len++] = ' ';
6405 while (len < 17);
6406 IObuff[len] = NUL;
6407 if (code == NULL)
6408 len += 4;
6409 else
6410 len += vim_strsize(code);
6411
6412 if (printit)
6413 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006414 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006416 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 else
6418 msg_outtrans(code);
6419 }
6420 return len;
6421}
6422
6423#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6424/*
6425 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6426 * termcap codes from the terminal itself.
6427 * We get them one by one to avoid a very long response string.
6428 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006429static int xt_index_in = 0;
6430static int xt_index_out = 0;
6431
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006433req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434{
6435 xt_index_out = 0;
6436 xt_index_in = 0;
6437 req_more_codes_from_term();
6438}
6439
6440 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006441req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006443 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 int old_idx = xt_index_out;
6445
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006446 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 if (exiting)
6448 return;
6449
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006450 // Send up to 10 more requests out than we received. Avoid sending too
6451 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6453 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006454 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006455
Bram Moolenaar86394aa2020-09-05 14:27:24 +02006456#ifdef FEAT_JOB_CHANNEL
6457 ch_log_output = TRUE;
6458#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02006459 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6460 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 out_str_nf((char_u *)buf);
6462 ++xt_index_out;
6463 }
6464
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006465 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 if (xt_index_out != old_idx)
6467 out_flush();
6468}
6469
6470/*
6471 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6472 * A "0" instead of the "1" indicates a code that isn't supported.
6473 * Both <name> and <string> are encoded in hex.
6474 * "code" points to the "0" or "1".
6475 */
6476 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006477got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
6479#define XT_LEN 100
6480 char_u name[3];
6481 char_u str[XT_LEN];
6482 int i;
6483 int j = 0;
6484 int c;
6485
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006486 // A '1' means the code is supported, a '0' means it isn't.
6487 // When half the length is > XT_LEN we can't use it.
6488 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6490 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006491 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 name[0] = hexhex2nr(code + 3);
6493 name[1] = hexhex2nr(code + 5);
6494 name[2] = NUL;
6495 for (i = 0; key_names[i] != NULL; ++i)
6496 {
6497 if (STRCMP(key_names[i], name) == 0)
6498 {
6499 xt_index_in = i;
6500 break;
6501 }
6502 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006503
Bram Moolenaarb255b902018-04-24 21:40:10 +02006504 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6505
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 if (key_names[i] != NULL)
6507 {
6508 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6509 str[j++] = c;
6510 str[j] = NUL;
6511 if (name[0] == 'C' && name[1] == 'o')
6512 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006513 // Color count is not a key code.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00006514 may_adjust_color_count(atoi((char *)str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 }
6516 else
6517 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006518 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 i = find_term_bykeys(str);
6520 if (i >= 0)
6521 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006522 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 }
6524 }
6525 }
6526
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006527 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 ++xt_index_in;
6529 req_more_codes_from_term();
6530}
6531
6532/*
6533 * Check if there are any unanswered requests and deal with them.
6534 * This is called before starting an external program or getting direct
6535 * keyboard input. We don't want responses to be send to that program or
6536 * handled as typed text.
6537 */
6538 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006539check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540{
6541 int c;
6542
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006543 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6545 return;
6546
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006547 // Vgetc() will check for and handle any response.
6548 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 ++no_mapping;
6550 ++allow_keys;
6551 for (;;)
6552 {
6553 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006554 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 break;
6556
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006557 // If a response is recognized it's replaced with K_IGNORE, must read
6558 // it from the input stream. If there is no K_IGNORE we can't do
6559 // anything, break here (there might be some responses further on, but
6560 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 if (c != K_SPECIAL && c != K_IGNORE)
6562 break;
6563 c = vgetc();
6564 if (c != K_IGNORE)
6565 {
6566 vungetc(c);
6567 break;
6568 }
6569 }
6570 --no_mapping;
6571 --allow_keys;
6572}
6573#endif
6574
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006575#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576static char ksme_str[20];
6577static char ksmr_str[20];
6578static char ksmd_str[20];
6579
6580/*
6581 * For Win32 console: update termcap codes for existing console attributes.
6582 */
6583 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006584update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585{
6586 struct builtin_term *p;
6587
6588 p = find_builtin_term(DEFAULT_TERM);
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006589 sprintf(ksme_str, "\033|%dm", attr);
6590 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
6591 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592
6593 while (p->bt_string != NULL)
6594 {
6595 if (p->bt_entry == (int)KS_ME)
6596 p->bt_string = &ksme_str[0];
6597 else if (p->bt_entry == (int)KS_MR)
6598 p->bt_string = &ksmr_str[0];
6599 else if (p->bt_entry == (int)KS_MD)
6600 p->bt_string = &ksmd_str[0];
6601 ++p;
6602 }
6603}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006604
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006605# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006606# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006607struct ks_tbl_s
6608{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006609 int code; // value of KS_
6610 char *vtp; // code in vtp mode
6611 char *vtp2; // code in vtp2 mode
6612 char buf[KSSIZE]; // save buffer in non-vtp mode
6613 char vbuf[KSSIZE]; // save buffer in vtp mode
6614 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6615 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006616};
6617
6618static struct ks_tbl_s ks_tbl[] =
6619{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006620 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6621 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6622 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6623 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6624 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6625 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6626 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6627 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6628 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006629# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006630 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6631 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006632 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6633 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006634# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006635 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6636 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006637 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6638 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006639# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006640 {(int)KS_CCO, "256", "256"}, // colors
6641 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006642};
6643
6644 static struct builtin_term *
6645find_first_tcap(
6646 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006647 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006648{
6649 struct builtin_term *p;
6650
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006651 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006652 if (p->bt_entry == code)
6653 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006654 return NULL;
6655}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006656# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006657
6658/*
6659 * For Win32 console: replace the sequence immediately after termguicolors.
6660 */
6661 void
6662swap_tcap(void)
6663{
6664# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006665 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006666 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006667 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006668 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006669 int mode;
6670 enum
6671 {
6672 CMODEINDEX,
6673 CMODE24,
6674 CMODE256
6675 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006676
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006677 // buffer initialization
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 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006685 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6686 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6687 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6688
6689 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6690 bt->bt_string = &ks->arr[0];
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;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006694 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006695 }
6696
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006697 if (p_tgc)
6698 mode = CMODE24;
6699 else if (t_colors >= 256)
6700 mode = CMODE256;
6701 else
6702 mode = CMODEINDEX;
6703
6704 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006705 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006706 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6707 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006708 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006709 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006710 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006711 case CMODEINDEX:
6712 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6713 break;
6714 case CMODE24:
6715 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6716 break;
6717 default:
6718 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006719 }
6720 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006721 }
6722
6723 if (mode != curr_mode)
6724 {
6725 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006726 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006727 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6728 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006729 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006730 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006731 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006732 case CMODEINDEX:
6733 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6734 break;
6735 case CMODE24:
6736 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6737 break;
6738 default:
6739 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006740 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006741 }
6742 }
6743
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006744 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006745 }
6746# endif
6747}
6748
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006750
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006751
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006752#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006753 || defined(PROTO)
6754static int cube_value[] = {
6755 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6756};
6757
6758static int grey_ramp[] = {
6759 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6760 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6761};
6762
Christian Brabandt2f7e00a2022-05-02 00:06:51 +01006763static char_u ansi_table[16][3] = {
LemonBoyd2a46622022-05-01 17:43:33 +01006764// R G B
6765 { 0, 0, 0}, // black
6766 {224, 0, 0}, // dark red
6767 { 0, 224, 0}, // dark green
6768 {224, 224, 0}, // dark yellow / brown
6769 { 0, 0, 224}, // dark blue
6770 {224, 0, 224}, // dark magenta
6771 { 0, 224, 224}, // dark cyan
6772 {224, 224, 224}, // light grey
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006773
LemonBoyd2a46622022-05-01 17:43:33 +01006774 {128, 128, 128}, // dark grey
6775 {255, 64, 64}, // light red
6776 { 64, 255, 64}, // light green
6777 {255, 255, 64}, // yellow
6778 { 64, 64, 255}, // light blue
6779 {255, 64, 255}, // light magenta
6780 { 64, 255, 255}, // light cyan
6781 {255, 255, 255}, // white
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006782};
6783
LemonBoyd2a46622022-05-01 17:43:33 +01006784#if defined(MSWIN)
6785// Mapping between cterm indices < 16 and their counterpart in the ANSI palette.
6786static const char_u cterm_ansi_idx[] = {
6787 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
6788};
6789#endif
6790
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006791#define ANSI_INDEX_NONE 0
6792
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006793 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006794cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006795{
6796 int idx;
6797
6798 if (nr < 16)
6799 {
LemonBoyd2a46622022-05-01 17:43:33 +01006800#if defined(MSWIN)
6801 idx = cterm_ansi_idx[nr];
6802#else
6803 idx = nr;
6804#endif
6805 *r = ansi_table[idx][0];
6806 *g = ansi_table[idx][1];
6807 *b = ansi_table[idx][2];
6808 *ansi_idx = idx + 1;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006809 }
6810 else if (nr < 232)
6811 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006812 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006813 idx = nr - 16;
6814 *r = cube_value[idx / 36 % 6];
6815 *g = cube_value[idx / 6 % 6];
6816 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006817 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006818 }
6819 else if (nr < 256)
6820 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006821 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006822 idx = nr - 232;
6823 *r = grey_ramp[idx];
6824 *g = grey_ramp[idx];
6825 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006826 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006827 }
6828 else
6829 {
6830 *r = 0;
6831 *g = 0;
6832 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006833 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006834 }
6835}
6836#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006837
Bram Moolenaarf4140482020-02-15 23:06:45 +01006838/*
6839 * Replace K_BS by <BS> and K_DEL by <DEL>
6840 */
6841 void
6842term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len)
6843{
6844 int i;
6845 int c;
6846
6847 for (i = ta_len; i < ta_len + len; ++i)
6848 {
6849 if (ta_buf[i] == CSI && len - i > 2)
6850 {
6851 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6852 if (c == K_DEL || c == K_KDEL || c == K_BS)
6853 {
6854 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
6855 (size_t)(len - i - 2));
6856 if (c == K_DEL || c == K_KDEL)
6857 ta_buf[i] = DEL;
6858 else
6859 ta_buf[i] = Ctrl_H;
6860 len -= 2;
6861 }
6862 }
6863 else if (ta_buf[i] == '\r')
6864 ta_buf[i] = '\n';
6865 if (has_mbyte)
6866 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6867 }
6868}