blob: 8f3ee600f0cad8ddebc39993a3f6f3256e88afd6 [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 Moolenaar071d4272004-06-13 20:20:40 +0000168# endif
169
170/*
171 * Don't declare these variables if termcap.h contains them.
172 * Autoconf checks if these variables should be declared extern (not all
173 * systems have them).
174 * Some versions define ospeed to be speed_t, but that is incompatible with
175 * BSD, where ospeed is short and speed_t is long.
176 */
177# ifndef HAVE_OSPEED
178# ifdef OSPEED_EXTERN
179extern short ospeed;
180# else
181short ospeed;
182# endif
183# endif
184# ifndef HAVE_UP_BC_PC
185# ifdef UP_BC_PC_EXTERN
186extern char *UP, *BC, PC;
187# else
188char *UP, *BC, PC;
189# endif
190# endif
191
192# define TGETSTR(s, p) vim_tgetstr((s), (p))
193# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100194static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100195#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100197static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100199#if (defined(UNIX) || defined(VMS))
Bram Moolenaar8d5daf22022-03-02 17:16:39 +0000200static int focus_state = MAYBE; // TRUE if the Vim window has focus
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100201#endif
202
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200203#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100204// When the cursor shape was detected these values are used:
205// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200206static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100208// The blink flag from the style response may be inverted from the actual
209// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200210static int initial_cursor_shape_blink = FALSE;
211
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100212// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200213static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200214#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200215
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000216static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217{
218
219#if defined(FEAT_GUI)
220/*
221 * GUI pseudo term-cap.
222 */
223 {(int)KS_NAME, "gui"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000224 {(int)KS_CE, "\033|$"},
225 {(int)KS_AL, "\033|i"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000227 {(int)KS_CAL, "\033|%p1%dI"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000229 {(int)KS_CAL, "\033|%dI"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000231 {(int)KS_DL, "\033|d"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000233 {(int)KS_CDL, "\033|%p1%dD"},
234 {(int)KS_CS, "\033|%p1%d;%p2%dR"},
235 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000237 {(int)KS_CDL, "\033|%dD"},
238 {(int)KS_CS, "\033|%d;%dR"},
239 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000241 {(int)KS_CL, "\033|C"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100242 // attributes switched on with 'h', off with * 'H'
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000243 {(int)KS_ME, "\033|31H"}, // HL_ALL
244 {(int)KS_MR, "\033|1h"}, // HL_INVERSE
245 {(int)KS_MD, "\033|2h"}, // HL_BOLD
246 {(int)KS_SE, "\033|16H"}, // HL_STANDOUT
247 {(int)KS_SO, "\033|16h"}, // HL_STANDOUT
248 {(int)KS_UE, "\033|8H"}, // HL_UNDERLINE
249 {(int)KS_US, "\033|8h"}, // HL_UNDERLINE
250 {(int)KS_UCE, "\033|8C"}, // HL_UNDERCURL
251 {(int)KS_UCS, "\033|8c"}, // HL_UNDERCURL
252 {(int)KS_STE, "\033|4C"}, // HL_STRIKETHROUGH
253 {(int)KS_STS, "\033|4c"}, // HL_STRIKETHROUGH
254 {(int)KS_CZR, "\033|4H"}, // HL_ITALIC
255 {(int)KS_CZH, "\033|4h"}, // HL_ITALIC
256 {(int)KS_VB, "\033|f"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 {(int)KS_MS, "y"},
258 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100259 {(int)KS_XN, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100260 {(int)KS_LE, "\b"}, // cursor-left = BS
261 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000263 {(int)KS_CM, "\033|%p1%d;%p2%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000265 {(int)KS_CM, "\033|%d;%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100267 // there are no key sequences here, the GUI sequences are recognized
268 // in check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269#endif
270
271#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272
273# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
274/*
275 * Amiga console window, default for Amiga
276 */
277 {(int)KS_NAME, "amiga"},
278 {(int)KS_CE, "\033[K"},
279 {(int)KS_CD, "\033[J"},
280 {(int)KS_AL, "\033[L"},
281# ifdef TERMINFO
282 {(int)KS_CAL, "\033[%p1%dL"},
283# else
284 {(int)KS_CAL, "\033[%dL"},
285# endif
286 {(int)KS_DL, "\033[M"},
287# ifdef TERMINFO
288 {(int)KS_CDL, "\033[%p1%dM"},
289# else
290 {(int)KS_CDL, "\033[%dM"},
291# endif
292 {(int)KS_CL, "\014"},
293 {(int)KS_VI, "\033[0 p"},
294 {(int)KS_VE, "\033[1 p"},
295 {(int)KS_ME, "\033[0m"},
296 {(int)KS_MR, "\033[7m"},
297 {(int)KS_MD, "\033[1m"},
298 {(int)KS_SE, "\033[0m"},
299 {(int)KS_SO, "\033[33m"},
300 {(int)KS_US, "\033[4m"},
301 {(int)KS_UE, "\033[0m"},
302 {(int)KS_CZH, "\033[3m"},
303 {(int)KS_CZR, "\033[0m"},
Bram Moolenaar2d718262020-11-21 12:44:56 +0100304#if defined(__amigaos4__) || defined(__MORPHOS__) || defined(__AROS__)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100305 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100307 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
308 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100310 {(int)KS_CAB, "\033[4%dm"}, // set background color
311 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100313 {(int)KS_OP, "\033[m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#endif
315 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100316 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 {(int)KS_LE, "\b"},
318# ifdef TERMINFO
319 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
320# else
321 {(int)KS_CM, "\033[%i%d;%dH"},
322# endif
323#if defined(__MORPHOS__)
324 {(int)KS_SR, "\033M"},
325#endif
326# ifdef TERMINFO
327 {(int)KS_CRI, "\033[%p1%dC"},
328# else
329 {(int)KS_CRI, "\033[%dC"},
330# endif
331 {K_UP, "\233A"},
332 {K_DOWN, "\233B"},
333 {K_LEFT, "\233D"},
334 {K_RIGHT, "\233C"},
335 {K_S_UP, "\233T"},
336 {K_S_DOWN, "\233S"},
337 {K_S_LEFT, "\233 A"},
338 {K_S_RIGHT, "\233 @"},
339 {K_S_TAB, "\233Z"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100340 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 {K_F2, "\233\061~"},
342 {K_F3, "\233\062~"},
343 {K_F4, "\233\063~"},
344 {K_F5, "\233\064~"},
345 {K_F6, "\233\065~"},
346 {K_F7, "\233\066~"},
347 {K_F8, "\233\067~"},
348 {K_F9, "\233\070~"},
349 {K_F10, "\233\071~"},
350 {K_S_F1, "\233\061\060~"},
351 {K_S_F2, "\233\061\061~"},
352 {K_S_F3, "\233\061\062~"},
353 {K_S_F4, "\233\061\063~"},
354 {K_S_F5, "\233\061\064~"},
355 {K_S_F6, "\233\061\065~"},
356 {K_S_F7, "\233\061\066~"},
357 {K_S_F8, "\233\061\067~"},
358 {K_S_F9, "\233\061\070~"},
359 {K_S_F10, "\233\061\071~"},
360 {K_HELP, "\233?~"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100361 {K_INS, "\233\064\060~"}, // 101 key keyboard
362 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
363 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
364 {K_HOME, "\233\064\064~"}, // 101 key keyboard
365 {K_END, "\233\064\065~"}, // 101 key keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366
367 {BT_EXTRA_KEYS, ""},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100368 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
369 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
370 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371# endif
372
Bram Moolenaar041c7102020-05-30 18:14:57 +0200373# ifdef ALL_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374/*
Bram Moolenaar041c7102020-05-30 18:14:57 +0200375 * almost standard ANSI terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 {(int)KS_CE, "\033[K"},
378 {(int)KS_CD, "\033[J"},
379 {(int)KS_AL, "\033[L"},
380# ifdef TERMINFO
381 {(int)KS_CAL, "\033[%p1%dL"},
382# else
383 {(int)KS_CAL, "\033[%dL"},
384# endif
385 {(int)KS_DL, "\033[M"},
386# ifdef TERMINFO
387 {(int)KS_CDL, "\033[%p1%dM"},
388# else
389 {(int)KS_CDL, "\033[%dM"},
390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {(int)KS_CL, "\033[H\033[2J"},
392#ifdef notyet
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100393 {(int)KS_VI, "[VI]"}, // cursor invisible, VT320: CSI ? 25 l
394 {(int)KS_VE, "[VE]"}, // cursor visible, VT320: CSI ? 25 h
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100396 {(int)KS_ME, "\033[m"}, // normal mode
397 {(int)KS_MR, "\033[7m"}, // reverse
398 {(int)KS_MD, "\033[1m"}, // bold
399 {(int)KS_SO, "\033[31m"}, // standout mode: red
400 {(int)KS_SE, "\033[m"}, // standout end
401 {(int)KS_CZH, "\033[35m"}, // italic: purple
402 {(int)KS_CZR, "\033[m"}, // italic end
403 {(int)KS_US, "\033[4m"}, // underscore mode
404 {(int)KS_UE, "\033[m"}, // underscore end
405 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100407 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
408 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100410 {(int)KS_CAB, "\033[4%dm"}, // set background color
411 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100413 {(int)KS_OP, "\033[m"}, // reset colors
414 {(int)KS_MS, "y"}, // safe to move cur in reverse mode
415 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 {(int)KS_LE, "\b"},
417# ifdef TERMINFO
418 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
419# else
420 {(int)KS_CM, "\033[%i%d;%dH"},
421# endif
422 {(int)KS_SR, "\033M"},
423# ifdef TERMINFO
424 {(int)KS_CRI, "\033[%p1%dC"},
425# else
426 {(int)KS_CRI, "\033[%dC"},
427# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428
429 {K_UP, "\033[A"},
430 {K_DOWN, "\033[B"},
431 {K_LEFT, "\033[D"},
432 {K_RIGHT, "\033[C"},
433# endif
434
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200435# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436/*
437 * standard ANSI terminal, default for unix
438 */
439 {(int)KS_NAME, "ansi"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000440 {(int)KS_CE, "\033[K"},
441 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000443 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000445 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000447 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000449 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000451 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000453 {(int)KS_CL, "\033[H\033[2J"},
454 {(int)KS_ME, "\033[0m"},
455 {(int)KS_MR, "\033[7m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100457 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 {(int)KS_LE, "\b"},
459# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000460 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000462 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463# endif
464# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000465 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000467 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468# endif
469# endif
470
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200471# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472/*
473 * These codes are valid when nansi.sys or equivalent has been installed.
474 * Function keys on a PC are preceded with a NUL. These are converted into
475 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
476 * CTRL-arrow is used instead of SHIFT-arrow.
477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 {(int)KS_NAME, "pcansi"},
479 {(int)KS_DL, "\033[M"},
480 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 {(int)KS_CE, "\033[K"},
482 {(int)KS_CL, "\033[2J"},
483 {(int)KS_ME, "\033[0m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100484 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
485 {(int)KS_MD, "\033[1m"}, // bold: white text
486 {(int)KS_SE, "\033[0m"}, // standout end
487 {(int)KS_SO, "\033[31m"}, // standout: white on blue
488 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
489 {(int)KS_CZR, "\033[0m"}, // italic mode end
490 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
491 {(int)KS_UE, "\033[0m"}, // underscore mode end
492 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100494 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
495 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100497 {(int)KS_CAB, "\033[4%dm"}, // set background color
498 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100500 {(int)KS_OP, "\033[0m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100502 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 {(int)KS_LE, "\b"},
504# ifdef TERMINFO
505 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
506# else
507 {(int)KS_CM, "\033[%i%d;%dH"},
508# endif
509# ifdef TERMINFO
510 {(int)KS_CRI, "\033[%p1%dC"},
511# else
512 {(int)KS_CRI, "\033[%dC"},
513# endif
514 {K_UP, "\316H"},
515 {K_DOWN, "\316P"},
516 {K_LEFT, "\316K"},
517 {K_RIGHT, "\316M"},
518 {K_S_LEFT, "\316s"},
519 {K_S_RIGHT, "\316t"},
520 {K_F1, "\316;"},
521 {K_F2, "\316<"},
522 {K_F3, "\316="},
523 {K_F4, "\316>"},
524 {K_F5, "\316?"},
525 {K_F6, "\316@"},
526 {K_F7, "\316A"},
527 {K_F8, "\316B"},
528 {K_F9, "\316C"},
529 {K_F10, "\316D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100530 {K_F11, "\316\205"}, // guessed
531 {K_F12, "\316\206"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 {K_S_F1, "\316T"},
533 {K_S_F2, "\316U"},
534 {K_S_F3, "\316V"},
535 {K_S_F4, "\316W"},
536 {K_S_F5, "\316X"},
537 {K_S_F6, "\316Y"},
538 {K_S_F7, "\316Z"},
539 {K_S_F8, "\316["},
540 {K_S_F9, "\316\\"},
541 {K_S_F10, "\316]"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100542 {K_S_F11, "\316\207"}, // guessed
543 {K_S_F12, "\316\210"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 {K_INS, "\316R"},
545 {K_DEL, "\316S"},
546 {K_HOME, "\316G"},
547 {K_END, "\316O"},
548 {K_PAGEDOWN, "\316Q"},
549 {K_PAGEUP, "\316I"},
550# endif
551
Bram Moolenaar4f974752019-02-17 17:44:42 +0100552# if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553/*
554 * These codes are valid for the Win32 Console . The entries that start with
555 * ESC | are translated into console calls in os_win32.c. The function keys
556 * are also translated in os_win32.c.
557 */
558 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100559 {(int)KS_CE, "\033|K"}, // clear to end of line
560 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100562 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100564 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100566 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100568 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
569 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100571 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
572 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100574 {(int)KS_CL, "\033|J"}, // clear screen
575 {(int)KS_CD, "\033|j"}, // clear to end of display
576 {(int)KS_VI, "\033|v"}, // cursor invisible
577 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578
Bram Moolenaar6982f422019-02-16 16:48:01 +0100579 {(int)KS_ME, "\033|0m"}, // normal
580 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
581 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100583 {(int)KS_SO, "\033|31m"}, // standout: white on blue
584 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100586 {(int)KS_SO, "\033|F"}, // standout: high intensity
587 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100589 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
590 {(int)KS_CZR, "\033|0m"}, // italic end
591 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
592 {(int)KS_UE, "\033|0m"}, // underscore end
593 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100595 {(int)KS_CAB, "\033|%p1%db"}, // set background color
596 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100598 {(int)KS_CAB, "\033|%db"}, // set background color
599 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600# endif
601
Bram Moolenaar6982f422019-02-16 16:48:01 +0100602 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100604 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 {(int)KS_LE, "\b"},
606# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100607 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100609 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100611 {(int)KS_VB, "\033|B"}, // visual bell
612 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
613 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100615 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100617 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100619# ifdef FEAT_TERMGUICOLORS
620 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
621 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
624 {K_UP, "\316H"},
625 {K_DOWN, "\316P"},
626 {K_LEFT, "\316K"},
627 {K_RIGHT, "\316M"},
628 {K_S_UP, "\316\304"},
629 {K_S_DOWN, "\316\317"},
630 {K_S_LEFT, "\316\311"},
631 {K_C_LEFT, "\316s"},
632 {K_S_RIGHT, "\316\313"},
633 {K_C_RIGHT, "\316t"},
634 {K_S_TAB, "\316\017"},
635 {K_F1, "\316;"},
636 {K_F2, "\316<"},
637 {K_F3, "\316="},
638 {K_F4, "\316>"},
639 {K_F5, "\316?"},
640 {K_F6, "\316@"},
641 {K_F7, "\316A"},
642 {K_F8, "\316B"},
643 {K_F9, "\316C"},
644 {K_F10, "\316D"},
645 {K_F11, "\316\205"},
646 {K_F12, "\316\206"},
647 {K_S_F1, "\316T"},
648 {K_S_F2, "\316U"},
649 {K_S_F3, "\316V"},
650 {K_S_F4, "\316W"},
651 {K_S_F5, "\316X"},
652 {K_S_F6, "\316Y"},
653 {K_S_F7, "\316Z"},
654 {K_S_F8, "\316["},
655 {K_S_F9, "\316\\"},
656 {K_S_F10, "\316]"},
657 {K_S_F11, "\316\207"},
658 {K_S_F12, "\316\210"},
659 {K_INS, "\316R"},
660 {K_DEL, "\316S"},
661 {K_HOME, "\316G"},
662 {K_S_HOME, "\316\302"},
663 {K_C_HOME, "\316w"},
664 {K_END, "\316O"},
665 {K_S_END, "\316\315"},
666 {K_C_END, "\316u"},
667 {K_PAGEDOWN, "\316Q"},
668 {K_PAGEUP, "\316I"},
669 {K_KPLUS, "\316N"},
670 {K_KMINUS, "\316J"},
671 {K_KMULTIPLY, "\316\067"},
672 {K_K0, "\316\332"},
673 {K_K1, "\316\336"},
674 {K_K2, "\316\342"},
675 {K_K3, "\316\346"},
676 {K_K4, "\316\352"},
677 {K_K5, "\316\356"},
678 {K_K6, "\316\362"},
679 {K_K7, "\316\366"},
680 {K_K8, "\316\372"},
681 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100682 {K_BS, "\316x"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683# endif
684
685# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
686/*
687 * VT320 is working as an ANSI terminal compatible DEC terminal.
688 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 * TODO:- rewrite ESC[ codes to CSI
690 * - keyboard languages (CSI ? 26 n)
691 */
692 {(int)KS_NAME, "vt320"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000693 {(int)KS_CE, "\033[K"},
694 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000696 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000698 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000700 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000702 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000704 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000706 {(int)KS_CL, "\033[H\033[2J"},
707 {(int)KS_CD, "\033[J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100708 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000709 {(int)KS_ME, "\033[0m"},
710 {(int)KS_MR, "\033[7m"},
711 {(int)KS_MD, "\033[1m"}, // bold mode
712 {(int)KS_SE, "\033[22m"},// normal mode
713 {(int)KS_UE, "\033[24m"},// exit underscore mode
714 {(int)KS_US, "\033[4m"}, // underscore mode
715 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
716 {(int)KS_CZR, "\033[0m"}, // italic mode end
717 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
718 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
719 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
720 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {(int)KS_MS, "y"},
722 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100723 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {(int)KS_LE, "\b"},
725# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000726 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000728 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729# endif
730# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000731 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000733 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000735 {K_UP, "\033[A"},
736 {K_DOWN, "\033[B"},
737 {K_RIGHT, "\033[C"},
738 {K_LEFT, "\033[D"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200739 // Note: cursor key sequences for application cursor mode are omitted,
740 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000741 {K_F1, "\033[11~"},
742 {K_F2, "\033[12~"},
743 {K_F3, "\033[13~"},
744 {K_F4, "\033[14~"},
745 {K_F5, "\033[15~"},
746 {K_F6, "\033[17~"},
747 {K_F7, "\033[18~"},
748 {K_F8, "\033[19~"},
749 {K_F9, "\033[20~"},
750 {K_F10, "\033[21~"},
751 {K_F11, "\033[23~"},
752 {K_F12, "\033[24~"},
753 {K_F13, "\033[25~"},
754 {K_F14, "\033[26~"},
755 {K_F15, "\033[28~"}, // Help
756 {K_F16, "\033[29~"}, // Select
757 {K_F17, "\033[31~"},
758 {K_F18, "\033[32~"},
759 {K_F19, "\033[33~"},
760 {K_F20, "\033[34~"},
761 {K_INS, "\033[2~"},
762 {K_DEL, "\033[3~"},
763 {K_HOME, "\033[1~"},
764 {K_END, "\033[4~"},
765 {K_PAGEUP, "\033[5~"},
766 {K_PAGEDOWN, "\033[6~"},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200767 // These sequences starting with <Esc> O may interfere with what the user
768 // is typing. Remove these if that bothers you.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000769 {K_KPLUS, "\033Ok"}, // keypad plus
770 {K_KMINUS, "\033Om"}, // keypad minus
771 {K_KDIVIDE, "\033Oo"}, // keypad /
772 {K_KMULTIPLY, "\033Oj"}, // keypad *
773 {K_KENTER, "\033OM"}, // keypad Enter
774 {K_K0, "\033Op"}, // keypad 0
775 {K_K1, "\033Oq"}, // keypad 1
776 {K_K2, "\033Or"}, // keypad 2
777 {K_K3, "\033Os"}, // keypad 3
778 {K_K4, "\033Ot"}, // keypad 4
779 {K_K5, "\033Ou"}, // keypad 5
780 {K_K6, "\033Ov"}, // keypad 6
781 {K_K7, "\033Ow"}, // keypad 7
782 {K_K8, "\033Ox"}, // keypad 8
783 {K_K9, "\033Oy"}, // keypad 9
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100784 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785# endif
786
Bram Moolenaare3f915d2020-07-14 23:02:44 +0200787# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788/*
789 * Ordinary vt52
790 */
791 {(int)KS_NAME, "vt52"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000792 {(int)KS_CE, "\033K"},
793 {(int)KS_CD, "\033J"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100794# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000795 {(int)KS_CM, "\033Y%p1%' '%+%c%p2%' '%+%c"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100796# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000797 {(int)KS_CM, "\033Y%+ %+ "},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100798# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000800 {(int)KS_SR, "\033I"},
801 {(int)KS_AL, "\033L"},
802 {(int)KS_DL, "\033M"},
803 {K_UP, "\033A"},
804 {K_DOWN, "\033B"},
805 {K_LEFT, "\033D"},
806 {K_RIGHT, "\033C"},
807 {K_F1, "\033P"},
808 {K_F2, "\033Q"},
809 {K_F3, "\033R"},
810 {(int)KS_CL, "\033H\033J"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812# endif
813
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200814# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200815 {(int)KS_NAME, "xterm"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000816 {(int)KS_CE, "\033[K"},
817 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000819 {(int)KS_CAL, "\033[%p1%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000821 {(int)KS_CAL, "\033[%dL"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000823 {(int)KS_DL, "\033[M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000825 {(int)KS_CDL, "\033[%p1%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000827 {(int)KS_CDL, "\033[%dM"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828# endif
829# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000830 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000832 {(int)KS_CS, "\033[%i%d;%dr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000834 {(int)KS_CL, "\033[H\033[2J"},
835 {(int)KS_CD, "\033[J"},
836 {(int)KS_ME, "\033[m"},
837 {(int)KS_MR, "\033[7m"},
838 {(int)KS_MD, "\033[1m"},
839 {(int)KS_UE, "\033[m"},
840 {(int)KS_US, "\033[4m"},
841 {(int)KS_STE, "\033[29m"},
842 {(int)KS_STS, "\033[9m"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 {(int)KS_MS, "y"},
844 {(int)KS_UT, "y"},
845 {(int)KS_LE, "\b"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000846 {(int)KS_VI, "\033[?25l"},
847 {(int)KS_VE, "\033[?25h"},
848 {(int)KS_VS, "\033[?12h"},
849 {(int)KS_CVS, "\033[?12l"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200850# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000851 {(int)KS_CSH, "\033[%p1%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200852# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000853 {(int)KS_CSH, "\033[%d q"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200854# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000855 {(int)KS_CRC, "\033[?12$p"},
856 {(int)KS_CRS, "\033P$q q\033\\"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000858 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000860 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000862 {(int)KS_SR, "\033M"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000864 {(int)KS_CRI, "\033[%p1%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000866 {(int)KS_CRI, "\033[%dC"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000868 {(int)KS_KS, "\033[?1h\033="},
869 {(int)KS_KE, "\033[?1l\033>"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870# ifdef FEAT_XTERM_SAVE
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000871 {(int)KS_TI, "\0337\033[?47h"},
872 {(int)KS_TE, "\033[?47l\0338"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000874 {(int)KS_CTI, "\033[>4;2m"},
875 {(int)KS_CTE, "\033[>4;m"},
876 {(int)KS_CIS, "\033]1;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 {(int)KS_CIE, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000878 {(int)KS_TS, "\033]2;"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 {(int)KS_FS, "\007"},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000880 {(int)KS_CSC, "\033]12;"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200881 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882# ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000883 {(int)KS_CWS, "\033[8;%p1%d;%p2%dt"},
884 {(int)KS_CWP, "\033[3;%p1%d;%p2%dt"},
885 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886# else
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000887 {(int)KS_CWS, "\033[8;%d;%dt"},
888 {(int)KS_CWP, "\033[3;%d;%dt"},
889 {(int)KS_CGP, "\033[13t"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000891 {(int)KS_CRV, "\033[>c"},
892 {(int)KS_RFG, "\033]10;?\007"},
893 {(int)KS_RBG, "\033]11;?\007"},
894 {(int)KS_U7, "\033[6n"},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200895# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100896 // These are printf strings, not terminal codes.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000897 {(int)KS_8F, "\033[38;2;%lu;%lu;%lum"},
898 {(int)KS_8B, "\033[48;2;%lu;%lu;%lum"},
899 {(int)KS_8U, "\033[58;2;%lu;%lu;%lum"},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200900# endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000901 {(int)KS_CAU, "\033[58;5;%dm"},
902 {(int)KS_CBE, "\033[?2004h"},
903 {(int)KS_CBD, "\033[?2004l"},
904 {(int)KS_CST, "\033[22;2t"},
905 {(int)KS_CRT, "\033[23;2t"},
906 {(int)KS_SSI, "\033[22;1t"},
907 {(int)KS_SRI, "\033[23;1t"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100908# if (defined(UNIX) || defined(VMS))
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000909 {(int)KS_FD, "\033[?1004l"},
910 {(int)KS_FE, "\033[?1004h"},
Bram Moolenaar681fc3f2021-01-14 17:35:21 +0100911# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000912
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000913 {K_UP, "\033O*A"},
914 {K_DOWN, "\033O*B"},
915 {K_RIGHT, "\033O*C"},
916 {K_LEFT, "\033O*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100917 // An extra set of cursor keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000918 {K_XUP, "\033[@;*A"},
919 {K_XDOWN, "\033[@;*B"},
920 {K_XRIGHT, "\033[@;*C"},
921 {K_XLEFT, "\033[@;*D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100922 // An extra set of function keys for vt100 mode
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000923 {K_XF1, "\033O*P"},
924 {K_XF2, "\033O*Q"},
925 {K_XF3, "\033O*R"},
926 {K_XF4, "\033O*S"},
927 {K_F1, "\033[11;*~"},
928 {K_F2, "\033[12;*~"},
929 {K_F3, "\033[13;*~"},
930 {K_F4, "\033[14;*~"},
931 {K_F5, "\033[15;*~"},
932 {K_F6, "\033[17;*~"},
933 {K_F7, "\033[18;*~"},
934 {K_F8, "\033[19;*~"},
935 {K_F9, "\033[20;*~"},
936 {K_F10, "\033[21;*~"},
937 {K_F11, "\033[23;*~"},
938 {K_F12, "\033[24;*~"},
939 {K_S_TAB, "\033[Z"},
940 {K_HELP, "\033[28;*~"},
941 {K_UNDO, "\033[26;*~"},
942 {K_INS, "\033[2;*~"},
943 {K_HOME, "\033[1;*H"},
944 // {K_S_HOME, "\033O2H"},
945 // {K_C_HOME, "\033O5H"},
946 {K_KHOME, "\033[1;*~"},
947 {K_XHOME, "\033O*H"}, // other Home
948 {K_ZHOME, "\033[7;*~"}, // other Home
949 {K_END, "\033[1;*F"},
950 // {K_S_END, "\033O2F"},
951 // {K_C_END, "\033O5F"},
952 {K_KEND, "\033[4;*~"},
953 {K_XEND, "\033O*F"}, // other End
954 {K_ZEND, "\033[8;*~"},
955 {K_PAGEUP, "\033[5;*~"},
956 {K_PAGEDOWN, "\033[6;*~"},
957 {K_KPLUS, "\033O*k"}, // keypad plus
958 {K_KMINUS, "\033O*m"}, // keypad minus
959 {K_KDIVIDE, "\033O*o"}, // keypad /
960 {K_KMULTIPLY, "\033O*j"}, // keypad *
961 {K_KENTER, "\033O*M"}, // keypad Enter
962 {K_KPOINT, "\033O*n"}, // keypad .
963 {K_K0, "\033O*p"}, // keypad 0
964 {K_K1, "\033O*q"}, // keypad 1
965 {K_K2, "\033O*r"}, // keypad 2
966 {K_K3, "\033O*s"}, // keypad 3
967 {K_K4, "\033O*t"}, // keypad 4
968 {K_K5, "\033O*u"}, // keypad 5
969 {K_K6, "\033O*v"}, // keypad 6
970 {K_K7, "\033O*w"}, // keypad 7
971 {K_K8, "\033O*x"}, // keypad 8
972 {K_K9, "\033O*y"}, // keypad 9
973 {K_KDEL, "\033[3;*~"}, // keypad Del
974 {K_PS, "\033[200~"}, // paste start
975 {K_PE, "\033[201~"}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
977 {BT_EXTRA_KEYS, ""},
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000978 {TERMCAP2KEY('k', '0'), "\033[10;*~"}, // F0
979 {TERMCAP2KEY('F', '3'), "\033[25;*~"}, // F13
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100980 // F14 and F15 are missing, because they send the same codes as the undo
981 // and help key, although they don't work on all keyboards.
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000982 {TERMCAP2KEY('F', '6'), "\033[29;*~"}, // F16
983 {TERMCAP2KEY('F', '7'), "\033[31;*~"}, // F17
984 {TERMCAP2KEY('F', '8'), "\033[32;*~"}, // F18
985 {TERMCAP2KEY('F', '9'), "\033[33;*~"}, // F19
986 {TERMCAP2KEY('F', 'A'), "\033[34;*~"}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000987
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000988 {TERMCAP2KEY('F', 'B'), "\033[42;*~"}, // F21
989 {TERMCAP2KEY('F', 'C'), "\033[43;*~"}, // F22
990 {TERMCAP2KEY('F', 'D'), "\033[44;*~"}, // F23
991 {TERMCAP2KEY('F', 'E'), "\033[45;*~"}, // F24
992 {TERMCAP2KEY('F', 'F'), "\033[46;*~"}, // F25
993 {TERMCAP2KEY('F', 'G'), "\033[47;*~"}, // F26
994 {TERMCAP2KEY('F', 'H'), "\033[48;*~"}, // F27
995 {TERMCAP2KEY('F', 'I'), "\033[49;*~"}, // F28
996 {TERMCAP2KEY('F', 'J'), "\033[50;*~"}, // F29
997 {TERMCAP2KEY('F', 'K'), "\033[51;*~"}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000998
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000999 {TERMCAP2KEY('F', 'L'), "\033[52;*~"}, // F31
1000 {TERMCAP2KEY('F', 'M'), "\033[53;*~"}, // F32
1001 {TERMCAP2KEY('F', 'N'), "\033[54;*~"}, // F33
1002 {TERMCAP2KEY('F', 'O'), "\033[55;*~"}, // F34
1003 {TERMCAP2KEY('F', 'P'), "\033[56;*~"}, // F35
1004 {TERMCAP2KEY('F', 'Q'), "\033[57;*~"}, // F36
1005 {TERMCAP2KEY('F', 'R'), "\033[58;*~"}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006# endif
1007
1008# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1009/*
1010 * iris-ansi for Silicon Graphics machines.
1011 */
1012 {(int)KS_NAME, "iris-ansi"},
1013 {(int)KS_CE, "\033[K"},
1014 {(int)KS_CD, "\033[J"},
1015 {(int)KS_AL, "\033[L"},
1016# ifdef TERMINFO
1017 {(int)KS_CAL, "\033[%p1%dL"},
1018# else
1019 {(int)KS_CAL, "\033[%dL"},
1020# endif
1021 {(int)KS_DL, "\033[M"},
1022# ifdef TERMINFO
1023 {(int)KS_CDL, "\033[%p1%dM"},
1024# else
1025 {(int)KS_CDL, "\033[%dM"},
1026# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001027#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028# ifdef TERMINFO
1029 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1030# else
1031 {(int)KS_CS, "\033[%i%d;%dr"},
1032# endif
1033#endif
1034 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001035 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
1036 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 {(int)KS_TI, "\033[=6h"},
1038 {(int)KS_TE, "\033[=6l"},
1039 {(int)KS_SE, "\033[21;27m"},
1040 {(int)KS_SO, "\033[1;7m"},
1041 {(int)KS_ME, "\033[m"},
1042 {(int)KS_MR, "\033[7m"},
1043 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001044 {(int)KS_CCO, "8"}, // allow 8 colors
1045 {(int)KS_CZH, "\033[3m"}, // italic mode on
1046 {(int)KS_CZR, "\033[23m"}, // italic mode off
1047 {(int)KS_US, "\033[4m"}, // underline on
1048 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001050 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
1051 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
1052 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
1053 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001055 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
1056 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
1057 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
1058 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001060 {(int)KS_MS, "y"}, // guessed
1061 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 {(int)KS_LE, "\b"},
1063# ifdef TERMINFO
1064 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1065# else
1066 {(int)KS_CM, "\033[%i%d;%dH"},
1067# endif
1068 {(int)KS_SR, "\033M"},
1069# ifdef TERMINFO
1070 {(int)KS_CRI, "\033[%p1%dC"},
1071# else
1072 {(int)KS_CRI, "\033[%dC"},
1073# endif
1074 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001075 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001077 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078# ifdef TERMINFO
1079 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1080 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1081# else
1082 {(int)KS_CWS, "\033[203;%d;%d/y"},
1083 {(int)KS_CWP, "\033[205;%d;%d/y"},
1084# endif
1085 {K_UP, "\033[A"},
1086 {K_DOWN, "\033[B"},
1087 {K_LEFT, "\033[D"},
1088 {K_RIGHT, "\033[C"},
1089 {K_S_UP, "\033[161q"},
1090 {K_S_DOWN, "\033[164q"},
1091 {K_S_LEFT, "\033[158q"},
1092 {K_S_RIGHT, "\033[167q"},
1093 {K_F1, "\033[001q"},
1094 {K_F2, "\033[002q"},
1095 {K_F3, "\033[003q"},
1096 {K_F4, "\033[004q"},
1097 {K_F5, "\033[005q"},
1098 {K_F6, "\033[006q"},
1099 {K_F7, "\033[007q"},
1100 {K_F8, "\033[008q"},
1101 {K_F9, "\033[009q"},
1102 {K_F10, "\033[010q"},
1103 {K_F11, "\033[011q"},
1104 {K_F12, "\033[012q"},
1105 {K_S_F1, "\033[013q"},
1106 {K_S_F2, "\033[014q"},
1107 {K_S_F3, "\033[015q"},
1108 {K_S_F4, "\033[016q"},
1109 {K_S_F5, "\033[017q"},
1110 {K_S_F6, "\033[018q"},
1111 {K_S_F7, "\033[019q"},
1112 {K_S_F8, "\033[020q"},
1113 {K_S_F9, "\033[021q"},
1114 {K_S_F10, "\033[022q"},
1115 {K_S_F11, "\033[023q"},
1116 {K_S_F12, "\033[024q"},
1117 {K_INS, "\033[139q"},
1118 {K_HOME, "\033[H"},
1119 {K_END, "\033[146q"},
1120 {K_PAGEUP, "\033[150q"},
1121 {K_PAGEDOWN, "\033[154q"},
1122# endif
1123
1124# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1125/*
1126 * for debugging
1127 */
1128 {(int)KS_NAME, "debug"},
1129 {(int)KS_CE, "[CE]"},
1130 {(int)KS_CD, "[CD]"},
1131 {(int)KS_AL, "[AL]"},
1132# ifdef TERMINFO
1133 {(int)KS_CAL, "[CAL%p1%d]"},
1134# else
1135 {(int)KS_CAL, "[CAL%d]"},
1136# endif
1137 {(int)KS_DL, "[DL]"},
1138# ifdef TERMINFO
1139 {(int)KS_CDL, "[CDL%p1%d]"},
1140# else
1141 {(int)KS_CDL, "[CDL%d]"},
1142# endif
1143# ifdef TERMINFO
1144 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1145# else
1146 {(int)KS_CS, "[%dCS%d]"},
1147# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001148# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001150# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152# endif
1153# ifdef TERMINFO
1154 {(int)KS_CAB, "[CAB%p1%d]"},
1155 {(int)KS_CAF, "[CAF%p1%d]"},
1156 {(int)KS_CSB, "[CSB%p1%d]"},
1157 {(int)KS_CSF, "[CSF%p1%d]"},
1158# else
1159 {(int)KS_CAB, "[CAB%d]"},
1160 {(int)KS_CAF, "[CAF%d]"},
1161 {(int)KS_CSB, "[CSB%d]"},
1162 {(int)KS_CSF, "[CSF%d]"},
1163# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001164 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {(int)KS_OP, "[OP]"},
1166 {(int)KS_LE, "[LE]"},
1167 {(int)KS_CL, "[CL]"},
1168 {(int)KS_VI, "[VI]"},
1169 {(int)KS_VE, "[VE]"},
1170 {(int)KS_VS, "[VS]"},
1171 {(int)KS_ME, "[ME]"},
1172 {(int)KS_MR, "[MR]"},
1173 {(int)KS_MB, "[MB]"},
1174 {(int)KS_MD, "[MD]"},
1175 {(int)KS_SE, "[SE]"},
1176 {(int)KS_SO, "[SO]"},
1177 {(int)KS_UE, "[UE]"},
1178 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001179 {(int)KS_UCE, "[UCE]"},
1180 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001181 {(int)KS_STE, "[STE]"},
1182 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {(int)KS_MS, "[MS]"},
1184 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001185 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186# ifdef TERMINFO
1187 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1188# else
1189 {(int)KS_CM, "[%dCM%d]"},
1190# endif
1191 {(int)KS_SR, "[SR]"},
1192# ifdef TERMINFO
1193 {(int)KS_CRI, "[CRI%p1%d]"},
1194# else
1195 {(int)KS_CRI, "[CRI%d]"},
1196# endif
1197 {(int)KS_VB, "[VB]"},
1198 {(int)KS_KS, "[KS]"},
1199 {(int)KS_KE, "[KE]"},
1200 {(int)KS_TI, "[TI]"},
1201 {(int)KS_TE, "[TE]"},
1202 {(int)KS_CIS, "[CIS]"},
1203 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001204 {(int)KS_CSC, "[CSC]"},
1205 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 {(int)KS_TS, "[TS]"},
1207 {(int)KS_FS, "[FS]"},
1208# ifdef TERMINFO
1209 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1210 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1211# else
1212 {(int)KS_CWS, "[%dCWS%d]"},
1213 {(int)KS_CWP, "[%dCWP%d]"},
1214# endif
1215 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001216 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001217 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001218 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 {K_UP, "[KU]"},
1220 {K_DOWN, "[KD]"},
1221 {K_LEFT, "[KL]"},
1222 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001223 {K_XUP, "[xKU]"},
1224 {K_XDOWN, "[xKD]"},
1225 {K_XLEFT, "[xKL]"},
1226 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {K_S_UP, "[S-KU]"},
1228 {K_S_DOWN, "[S-KD]"},
1229 {K_S_LEFT, "[S-KL]"},
1230 {K_C_LEFT, "[C-KL]"},
1231 {K_S_RIGHT, "[S-KR]"},
1232 {K_C_RIGHT, "[C-KR]"},
1233 {K_F1, "[F1]"},
1234 {K_XF1, "[xF1]"},
1235 {K_F2, "[F2]"},
1236 {K_XF2, "[xF2]"},
1237 {K_F3, "[F3]"},
1238 {K_XF3, "[xF3]"},
1239 {K_F4, "[F4]"},
1240 {K_XF4, "[xF4]"},
1241 {K_F5, "[F5]"},
1242 {K_F6, "[F6]"},
1243 {K_F7, "[F7]"},
1244 {K_F8, "[F8]"},
1245 {K_F9, "[F9]"},
1246 {K_F10, "[F10]"},
1247 {K_F11, "[F11]"},
1248 {K_F12, "[F12]"},
1249 {K_S_F1, "[S-F1]"},
1250 {K_S_XF1, "[S-xF1]"},
1251 {K_S_F2, "[S-F2]"},
1252 {K_S_XF2, "[S-xF2]"},
1253 {K_S_F3, "[S-F3]"},
1254 {K_S_XF3, "[S-xF3]"},
1255 {K_S_F4, "[S-F4]"},
1256 {K_S_XF4, "[S-xF4]"},
1257 {K_S_F5, "[S-F5]"},
1258 {K_S_F6, "[S-F6]"},
1259 {K_S_F7, "[S-F7]"},
1260 {K_S_F8, "[S-F8]"},
1261 {K_S_F9, "[S-F9]"},
1262 {K_S_F10, "[S-F10]"},
1263 {K_S_F11, "[S-F11]"},
1264 {K_S_F12, "[S-F12]"},
1265 {K_HELP, "[HELP]"},
1266 {K_UNDO, "[UNDO]"},
1267 {K_BS, "[BS]"},
1268 {K_INS, "[INS]"},
1269 {K_KINS, "[KINS]"},
1270 {K_DEL, "[DEL]"},
1271 {K_KDEL, "[KDEL]"},
1272 {K_HOME, "[HOME]"},
1273 {K_S_HOME, "[C-HOME]"},
1274 {K_C_HOME, "[C-HOME]"},
1275 {K_KHOME, "[KHOME]"},
1276 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001277 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {K_END, "[END]"},
1279 {K_S_END, "[C-END]"},
1280 {K_C_END, "[C-END]"},
1281 {K_KEND, "[KEND]"},
1282 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001283 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 {K_PAGEUP, "[PAGEUP]"},
1285 {K_PAGEDOWN, "[PAGEDOWN]"},
1286 {K_KPAGEUP, "[KPAGEUP]"},
1287 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1288 {K_MOUSE, "[MOUSE]"},
1289 {K_KPLUS, "[KPLUS]"},
1290 {K_KMINUS, "[KMINUS]"},
1291 {K_KDIVIDE, "[KDIVIDE]"},
1292 {K_KMULTIPLY, "[KMULTIPLY]"},
1293 {K_KENTER, "[KENTER]"},
1294 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001295 {K_PS, "[PASTE-START]"},
1296 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 {K_K0, "[K0]"},
1298 {K_K1, "[K1]"},
1299 {K_K2, "[K2]"},
1300 {K_K3, "[K3]"},
1301 {K_K4, "[K4]"},
1302 {K_K5, "[K5]"},
1303 {K_K6, "[K6]"},
1304 {K_K7, "[K7]"},
1305 {K_K8, "[K8]"},
1306 {K_K9, "[K9]"},
1307# endif
1308
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001309#endif // NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310
1311/*
1312 * The most minimal terminal: only clear screen and cursor positioning
1313 * Always included.
1314 */
1315 {(int)KS_NAME, "dumb"},
1316 {(int)KS_CL, "\014"},
1317#ifdef TERMINFO
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001318 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319#else
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001320 {(int)KS_CM, "\033[%i%d;%dH"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321#endif
1322
1323/*
1324 * end marker
1325 */
1326 {(int)KS_NAME, NULL}
1327
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001328}; // end of builtin_termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001330#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001331 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001332termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001333{
Bram Moolenaarab302212016-04-26 20:59:29 +02001334 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001335}
1336
1337 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001338termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001339{
1340 guicolor_T t;
1341
1342 if (*name == NUL)
1343 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001344 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001345
1346 if (t == INVALCOLOR)
Drew Vogele30d1022021-10-24 20:35:07 +01001347 semsg(_(e_cannot_allocate_color_str), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001348 return t;
1349}
1350
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001351 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001352termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001353{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001354 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001355}
1356#endif
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358/*
1359 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361#ifdef AMIGA
1362# define DEFAULT_TERM (char_u *)"amiga"
1363#endif
1364
1365#ifdef MSWIN
1366# define DEFAULT_TERM (char_u *)"win32"
1367#endif
1368
Bram Moolenaare3f915d2020-07-14 23:02:44 +02001369#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370# define DEFAULT_TERM (char_u *)"ansi"
1371#endif
1372
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373#ifdef VMS
1374# define DEFAULT_TERM (char_u *)"vt320"
1375#endif
1376
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001377#ifdef __HAIKU__
1378# undef DEFAULT_TERM
1379# define DEFAULT_TERM (char_u *)"xterm"
1380#endif
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382#ifndef DEFAULT_TERM
1383# define DEFAULT_TERM (char_u *)"dumb"
1384#endif
1385
1386/*
1387 * Term_strings contains currently used terminal output strings.
1388 * It is initialized with the default values by parse_builtin_tcap().
1389 * The values can be changed by setting the option with the same name.
1390 */
1391char_u *(term_strings[(int)KS_LAST + 1]);
1392
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001393static int need_gather = FALSE; // need to fill termleader[]
1394static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001396static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001397
1398/*
1399 * Structure and table to store terminal features that can be detected by
1400 * querying the terminal. Either by inspecting the termresponse or a more
1401 * specific request. Besides this there are:
1402 * t_colors - number of colors supported
1403 */
1404typedef struct {
1405 char *tpr_name;
1406 int tpr_set_by_termresponse;
1407 int tpr_status;
1408} termprop_T;
1409
1410// Values for tpr_status.
1411#define TPR_UNKNOWN 'u'
1412#define TPR_YES 'y'
1413#define TPR_NO 'n'
1414#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1415#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1416#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1417
1418// can request the cursor style without messing up the display
1419#define TPR_CURSOR_STYLE 0
1420// can request the cursor blink mode without messing up the display
1421#define TPR_CURSOR_BLINK 1
1422// can set the underline color with t_8u without resetting other colors
1423#define TPR_UNDERLINE_RGB 2
1424// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1425#define TPR_MOUSE 3
1426// table size
1427#define TPR_COUNT 4
1428
1429static termprop_T term_props[TPR_COUNT];
1430
1431/*
1432 * Initialize the term_props table.
1433 * When "all" is FALSE only set those that are detected from the version
1434 * response.
1435 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001436 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001437init_term_props(int all)
1438{
1439 int i;
1440
1441 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1442 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1443 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1444 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1445 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1446 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1447 term_props[TPR_MOUSE].tpr_name = "mouse";
1448 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
1449
1450 for (i = 0; i < TPR_COUNT; ++i)
1451 if (all || term_props[i].tpr_set_by_termresponse)
1452 term_props[i].tpr_status = TPR_UNKNOWN;
1453}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454#endif
1455
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001456#if defined(FEAT_EVAL) || defined(PROTO)
1457 void
1458f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1459{
1460# ifdef FEAT_TERMRESPONSE
1461 int i;
1462# endif
1463
1464 if (rettv_dict_alloc(rettv) != OK)
1465 return;
1466# ifdef FEAT_TERMRESPONSE
1467 for (i = 0; i < TPR_COUNT; ++i)
1468 {
1469 char_u value[2];
1470
1471 value[0] = term_props[i].tpr_status;
1472 value[1] = NUL;
1473 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1474 }
1475# endif
1476}
1477#endif
1478
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001480find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481{
1482 struct builtin_term *p;
1483
1484 p = builtin_termcaps;
1485 while (p->bt_string != NULL)
1486 {
1487 if (p->bt_entry == (int)KS_NAME)
1488 {
1489#ifdef UNIX
1490 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1491 return p;
1492 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1493 return p;
1494 else
1495#endif
1496#ifdef VMS
1497 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1498 return p;
1499 else
1500#endif
1501 if (STRCMP(term, p->bt_string) == 0)
1502 return p;
1503 }
1504 ++p;
1505 }
1506 return p;
1507}
1508
1509/*
1510 * Parsing of the builtin termcap entries.
1511 * Caller should check if 'name' is a valid builtin term.
1512 * The terminal's name is not set, as this is already done in termcapinit().
1513 */
1514 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001515parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
1517 struct builtin_term *p;
1518 char_u name[2];
1519 int term_8bit;
1520
1521 p = find_builtin_term(term);
1522 term_8bit = term_is_8bit(term);
1523
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001524 // Do not parse if builtin term not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (p->bt_string == NULL)
1526 return;
1527
1528 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1529 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001530 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001532 // Only set the value if it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 if (term_strings[p->bt_entry] == NULL
1534 || term_strings[p->bt_entry] == empty_option)
1535 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001536#ifdef FEAT_EVAL
1537 int opt_idx = -1;
1538#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001539 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1541 {
1542 char_u *s, *t;
1543
1544 s = vim_strsave((char_u *)p->bt_string);
1545 if (s != NULL)
1546 {
1547 for (t = s; *t; ++t)
1548 if (term_7to8bit(t))
1549 {
1550 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001551 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 }
1553 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001554#ifdef FEAT_EVAL
1555 opt_idx =
1556#endif
1557 set_term_option_alloced(
1558 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 }
1560 }
1561 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001562 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001564#ifdef FEAT_EVAL
1565 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1566#endif
1567 }
1568#ifdef FEAT_EVAL
1569 set_term_option_sctx_idx(NULL, opt_idx);
1570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 }
1572 }
1573 else
1574 {
1575 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1576 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1577 if (find_termcode(name) == NULL)
1578 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1579 }
1580 }
1581}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001582
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583/*
1584 * Set number of colors.
1585 * Store it as a number in t_colors.
1586 * Store it as a string in T_CCO (using nr_colors[]).
1587 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001588 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001589set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001591 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
1593 t_colors = nr;
1594 if (t_colors > 1)
1595 sprintf((char *)nr_colors, "%d", t_colors);
1596 else
1597 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001598 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001600
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001601#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001602/*
1603 * Set the color count to "val" and redraw if it changed.
1604 */
1605 static void
1606may_adjust_color_count(int val)
1607{
1608 if (val != t_colors)
1609 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001610 // Nr of colors changed, initialize highlighting and
1611 // redraw everything. This causes a redraw, which usually
1612 // clears the message. Try keeping the message if it
1613 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001614 set_keep_msg_from_hist();
1615 set_color_count(val);
1616 init_highlight(TRUE, FALSE);
1617# ifdef DEBUG_TERMRESPONSE
1618 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001619 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001620
Bram Moolenaarb255b902018-04-24 21:40:10 +02001621 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001622 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001623# else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001624 redraw_asap(CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001625# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001626 }
1627}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628#endif
1629
1630#ifdef HAVE_TGETENT
1631static char *(key_names[]) =
1632{
Bram Moolenaar87202262020-05-24 17:23:45 +02001633# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001634 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 "#2", "#4", "%i", "*7",
1639 "k1", "k2", "k3", "k4", "k5", "k6",
1640 "k7", "k8", "k9", "k;", "F1", "F2",
1641 "%1", "&8", "kb", "kI", "kD", "kh",
1642 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1643 NULL
1644};
1645#endif
1646
Bram Moolenaar9289df52018-05-10 14:40:57 +02001647#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001648 static void
1649get_term_entries(int *height, int *width)
1650{
1651 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001652 enum SpecialKey dest; // index in term_strings[]
1653 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001654 } string_names[] =
1655 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1656 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1657 {KS_CL, "cl"}, {KS_CD, "cd"},
1658 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1659 {KS_ME, "me"}, {KS_MR, "mr"},
1660 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1661 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1662 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1663 {KS_STE,"Te"}, {KS_STS,"Ts"},
1664 {KS_CM, "cm"}, {KS_SR, "sr"},
1665 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1666 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001667 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001668 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001669 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1670 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001671 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1672 {KS_VS, "vs"}, {KS_CVS, "VS"},
1673 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1674 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1675 {KS_TS, "ts"}, {KS_FS, "fs"},
1676 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1677 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1678 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001679 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001680 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1681 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001682 {KS_CST, "ST"}, {KS_CRT, "RT"},
1683 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001684 {(enum SpecialKey)0, NULL}
1685 };
1686 int i;
1687 char_u *p;
1688 static char_u tstrbuf[TBUFSZ];
1689 char_u *tp = tstrbuf;
1690
1691 /*
1692 * get output strings
1693 */
1694 for (i = 0; string_names[i].name != NULL; ++i)
1695 {
1696 if (TERM_STR(string_names[i].dest) == NULL
1697 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001698 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001699 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001700#ifdef FEAT_EVAL
1701 set_term_option_sctx_idx(string_names[i].name, -1);
1702#endif
1703 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001704 }
1705
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001706 // tgetflag() returns 1 if the flag is present, 0 if not and
1707 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001708 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1709 T_MS = (char_u *)"y";
1710 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1711 T_XS = (char_u *)"y";
1712 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1713 T_XN = (char_u *)"y";
1714 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1715 T_DB = (char_u *)"y";
1716 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1717 T_DA = (char_u *)"y";
1718 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1719 T_UT = (char_u *)"y";
1720
1721 /*
1722 * get key codes
1723 */
1724 for (i = 0; key_names[i] != NULL; ++i)
1725 if (find_termcode((char_u *)key_names[i]) == NULL)
1726 {
1727 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001728 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001729 if (p != NULL
1730 && (*p != Ctrl_H
1731 || key_names[i][0] != 'k'
1732 || key_names[i][1] != 'l'))
1733 add_termcode((char_u *)key_names[i], p, FALSE);
1734 }
1735
1736 if (*height == 0)
1737 *height = tgetnum("li");
1738 if (*width == 0)
1739 *width = tgetnum("co");
1740
1741 /*
1742 * Get number of colors (if not done already).
1743 */
1744 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001745 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001746 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001747#ifdef FEAT_EVAL
1748 set_term_option_sctx_idx("Co", -1);
1749#endif
1750 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001751
1752# ifndef hpux
1753 BC = (char *)TGETSTR("bc", &tp);
1754 UP = (char *)TGETSTR("up", &tp);
1755 p = TGETSTR("pc", &tp);
1756 if (p)
1757 PC = *p;
1758# endif
1759}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001760#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001761
1762 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001763report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001764{
1765 struct builtin_term *termp;
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001766 int i;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001767
1768 mch_errmsg("\r\n");
1769 if (error_msg != NULL)
1770 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001771 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001772 mch_errmsg("\r\n");
1773 }
1774 mch_errmsg("'");
1775 mch_errmsg((char *)term);
1776 mch_errmsg(_("' not known. Available builtin terminals are:"));
1777 mch_errmsg("\r\n");
1778 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1779 {
Bram Moolenaar0f5575d2021-07-30 21:18:03 +02001780 if (termp->bt_entry == (int)KS_NAME
1781 && STRCMP(termp->bt_string, "gui") != 0)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001782 {
1783#ifdef HAVE_TGETENT
1784 mch_errmsg(" builtin_");
1785#else
1786 mch_errmsg(" ");
1787#endif
1788 mch_errmsg(termp->bt_string);
1789 mch_errmsg("\r\n");
1790 }
1791 }
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02001792 // Output extra 'cmdheight' line breaks to avoid that the following error
1793 // message overwrites the last terminal name.
1794 for (i = 1; i < p_ch; ++i)
1795 mch_errmsg("\r\n");
Bram Moolenaar69e05692018-05-10 14:11:52 +02001796}
1797
1798 static void
1799report_default_term(char_u *term)
1800{
1801 mch_errmsg(_("defaulting to '"));
1802 mch_errmsg((char *)term);
1803 mch_errmsg("'\r\n");
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001804 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001805 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001806 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001807 out_flush();
1808 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001809 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001810 }
1811}
1812
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813/*
1814 * Set terminal options for terminal "term".
1815 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1816 *
1817 * While doing this, until ttest(), some options may be NULL, be careful.
1818 */
1819 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001820set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821{
1822 struct builtin_term *termp;
1823#ifdef HAVE_TGETENT
1824 int builtin_first = p_tbi;
1825 int try;
1826 int termcap_cleared = FALSE;
1827#endif
1828 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001829 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 char_u *bs_p, *del_p;
1831
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001832 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001833 if (silent_mode)
1834 return OK;
1835
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001836 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
1838 if (term_is_builtin(term))
1839 {
1840 term += 8;
1841#ifdef HAVE_TGETENT
1842 builtin_first = 1;
1843#endif
1844 }
1845
1846/*
1847 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1848 * If builtin_first is TRUE:
1849 * 0. try builtin termcap
1850 * 1. try external termcap
1851 * 2. if both fail default to a builtin terminal
1852 * If builtin_first is FALSE:
1853 * 1. try external termcap
1854 * 2. try builtin termcap, if both fail default to a builtin terminal
1855 */
1856#ifdef HAVE_TGETENT
1857 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1858 {
1859 /*
1860 * Use external termcap
1861 */
1862 if (try == 1)
1863 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
1866 /*
1867 * If the external termcap does not have a matching entry, try the
1868 * builtin ones.
1869 */
1870 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (!termcap_cleared)
1873 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001874 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 termcap_cleared = TRUE;
1876 }
1877
Bram Moolenaar69e05692018-05-10 14:11:52 +02001878 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 }
1880 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001881 else // try == 0 || try == 2
1882#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 /*
1884 * Use builtin termcap
1885 */
1886 {
1887#ifdef HAVE_TGETENT
1888 /*
1889 * If builtin termcap was already used, there is no need to search
1890 * for the builtin termcap again, quit now.
1891 */
1892 if (try == 2 && builtin_first && termcap_cleared)
1893 break;
1894#endif
1895 /*
1896 * search for 'term' in builtin_termcaps[]
1897 */
1898 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001899 if (termp->bt_string == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
1901#ifdef HAVE_TGETENT
1902 /*
1903 * If try == 0, first try the external termcap. If that is not
1904 * found we'll get back here with try == 2.
1905 * If termcap_cleared is set we used the external termcap,
1906 * don't complain about not finding the term in the builtin
1907 * termcap.
1908 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001909 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001911 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 break;
1913#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001914 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001916 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (starting != NO_SCREEN)
1918 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001919 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 wait_return(TRUE);
1921 return FAIL;
1922 }
1923 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001924 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001925 set_string_option_direct((char_u *)"term", -1, term,
1926 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 display_errors();
1928 }
1929 out_flush();
1930#ifdef HAVE_TGETENT
1931 if (!termcap_cleared)
1932 {
1933#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001934 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935#ifdef HAVE_TGETENT
1936 termcap_cleared = TRUE;
1937 }
1938#endif
1939 parse_builtin_tcap(term);
1940#ifdef FEAT_GUI
1941 if (term_is_gui(term))
1942 {
1943 out_flush();
1944 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001945 // If starting the GUI failed, don't do any of the other
1946 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (!gui.in_use)
1948 return FAIL;
1949#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001950 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951#endif
1952 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001953#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 }
1955#ifdef HAVE_TGETENT
1956 }
1957#endif
1958
1959/*
1960 * special: There is no info in the termcap about whether the cursor
1961 * positioning is relative to the start of the screen or to the start of the
1962 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1963 * relative.
1964 */
1965 if (STRCMP(term, "pcterm") == 0)
1966 T_CCS = (char_u *)"yes";
1967 else
1968 T_CCS = empty_option;
1969
1970#ifdef UNIX
1971/*
1972 * Any "stty" settings override the default for t_kb from the termcap.
1973 * This is in os_unix.c, because it depends a lot on the version of unix that
1974 * is being used.
1975 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1976 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001977# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 get_stty();
1981#endif
1982
1983/*
1984 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1985 * didn't work, use the default CTRL-H
1986 * The default for t_kD is DEL, unless t_kb is DEL.
1987 * The vim_strsave'd strings are probably lost forever, well it's only two
1988 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1989 * directly.
1990 */
1991#ifdef FEAT_GUI
1992 if (!gui.in_use)
1993#endif
1994 {
1995 bs_p = find_termcode((char_u *)"kb");
1996 del_p = find_termcode((char_u *)"kD");
1997 if (bs_p == NULL || *bs_p == NUL)
1998 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1999 if ((del_p == NULL || *del_p == NUL) &&
2000 (bs_p == NULL || *bs_p != DEL))
2001 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2002 }
2003
2004#if defined(UNIX) || defined(VMS)
2005 term_is_xterm = vim_is_xterm(term);
2006#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002007#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002008 // Reset terminal properties that are set based on the termresponse, which
2009 // will be sent out soon.
2010 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002013#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 /*
2015 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2016 * The termcode for the mouse is added as a side effect in option.c.
2017 */
2018 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002019 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002021# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002022 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 {
2024 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002025 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 else
2027 p = (char_u *)"xterm";
2028 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002029# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002031 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002032 set_option_value_give_err((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002033 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2034 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002035 reset_option_was_set((char_u *)"ttym");
2036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002038# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002042 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002044#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
Bram Moolenaarbf789742021-01-16 11:21:40 +01002048#ifdef FEAT_MOUSE_XTERM
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002049 // Focus reporting is supported by xterm compatible terminals and tmux.
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002050 if (use_xterm_like_mouse(term))
2051 {
2052 char_u name[3];
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002053
2054 // handle focus in event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002055 name[0] = KS_EXTRA;
2056 name[1] = KE_FOCUSGAINED;
2057 name[2] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002058 add_termcode(name, (char_u *)"\033[I", FALSE);
2059
2060 // handle focus out event
Bram Moolenaarbf789742021-01-16 11:21:40 +01002061 name[1] = KE_FOCUSLOST;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002062 add_termcode(name, (char_u *)"\033[O", FALSE);
2063
Bram Moolenaar51b477f2021-03-03 15:24:28 +01002064 need_gather = TRUE;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002065 }
2066#endif
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00002067#if (defined(UNIX) || defined(VMS))
2068 // First time after setting 'term' a focus event is always reported.
2069 focus_state = MAYBE;
2070#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002071
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002073 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 if (STRCMP(term, DEFAULT_TERM) != 0)
2075 term_console = FALSE;
2076 else
2077 {
2078 term_console = TRUE;
2079# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002080 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081# endif
2082 }
2083#endif
2084
2085#if defined(UNIX) || defined(VMS)
2086 /*
2087 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2088 */
2089 if (vim_is_fastterm(term))
2090 p_tf = TRUE;
2091#endif
2092#ifdef USE_TERM_CONSOLE
2093 /*
2094 * 'ttyfast' is default on consoles
2095 */
2096 if (term_console)
2097 p_tf = TRUE;
2098#endif
2099
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002100 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002102 full_screen = TRUE; // we can use termcap codes from now on
2103 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002105 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002106 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107#endif
2108
2109 /*
2110 * Initialize the terminal with the appropriate termcap codes.
2111 * Set the mouse and window title if possible.
2112 * Don't do this when starting, need to parse the .vimrc first, because it
2113 * may redefine t_TI etc.
2114 */
2115 if (starting != NO_SCREEN)
2116 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002117 starttermcap(); // may change terminal mode
2118 setmouse(); // may start using the mouse
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002119 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
2121
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002122 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 if (width <= 0 || height <= 0)
2124 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002125 // termcap failed to report size
2126 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002128#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002129 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002131 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132#endif
2133 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002134 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 if (starting != NO_SCREEN)
2136 {
2137 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002138 scroll_region_reset(); // In case Rows changed
2139 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002142 buf_T *buf;
2143 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
2145 /*
2146 * Execute the TermChanged autocommands for each buffer that is
2147 * loaded.
2148 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002149 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002152 {
2153 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2155 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002156 // restore curwin/curbuf and a few other things
2157 aucmd_restbuf(&aco);
2158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 }
2162
2163#ifdef FEAT_TERMRESPONSE
2164 may_req_termresponse();
2165#endif
2166
2167 return OK;
2168}
2169
Bram Moolenaarb3a29552021-11-19 11:28:04 +00002170#if defined(EXITFREE) || defined(PROTO)
2171
2172# ifdef HAVE_DEL_CURTERM
2173# undef TERMINAL // name clash in term.h
2174# include <term.h> // declares cur_term
2175# endif
2176
2177/*
2178 * If supported, delete "cur_term", which caches terminal related entries.
2179 * Avoids that valgrind reports possibly lost memory.
2180 */
2181 void
2182free_cur_term()
2183{
2184# ifdef HAVE_DEL_CURTERM
2185 if (cur_term)
2186 del_curterm(cur_term);
2187# endif
2188}
2189
2190#endif
2191
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192#ifdef HAVE_TGETENT
2193/*
2194 * Call tgetent()
2195 * Return error message if it fails, NULL if it's OK.
2196 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002197 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002198tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
2200 int i;
2201
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002202 // Note: Valgrind may report a leak here, because the library keeps one
2203 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002205 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002207 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208# endif
2209 )
2210 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002211 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2212 // tgetent() with the always existing "dumb" entry to avoid a crash or
2213 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 (void)TGETENT(tbuf, "dumb");
2215
2216 if (i < 0)
2217# ifdef TGETENT_ZERO_ERR
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002218 return _(e_cannot_open_termcap_file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 if (i == 0)
2220# endif
2221#ifdef TERMINFO
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002222 return _(e_terminal_entry_not_found_in_terminfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223#else
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002224 return _(e_terminal_entry_not_found_in_termcap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225#endif
2226 }
2227 return NULL;
2228}
2229
2230/*
2231 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2232 * Fix that here.
2233 */
2234 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002235vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236{
2237 char *p;
2238
2239 p = tgetstr(s, (char **)pp);
2240 if (p == (char *)-1)
2241 p = NULL;
2242 return (char_u *)p;
2243}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002244#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002246#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247/*
2248 * Get Columns and Rows from the termcap. Used after a window signal if the
2249 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2250 * and "li" entries never change. But on some systems this works.
2251 * Errors while getting the entries are ignored.
2252 */
2253 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002254getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002255 long *cp, // pointer to columns
2256 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257{
2258 char_u tbuf[TBUFSZ];
2259
2260 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2261 {
2262 if (*cp == 0)
2263 *cp = tgetnum("co");
2264 if (*rp == 0)
2265 *rp = tgetnum("li");
2266 }
2267}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002268#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269
2270/*
2271 * Get a string entry from the termcap and add it to the list of termcodes.
2272 * Used for <t_xx> special keys.
2273 * Give an error message for failure when not sourcing.
2274 * If force given, replace an existing entry.
2275 * Return FAIL if the entry was not found, OK if the entry was added.
2276 */
2277 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002278add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 char_u *term;
2281 int key;
2282 struct builtin_term *termp;
2283#ifdef HAVE_TGETENT
2284 char_u *string;
2285 int i;
2286 int builtin_first;
2287 char_u tbuf[TBUFSZ];
2288 char_u tstrbuf[TBUFSZ];
2289 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002290 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291#endif
2292
2293/*
2294 * If the GUI is running or will start in a moment, we only support the keys
2295 * that the GUI can produce.
2296 */
2297#ifdef FEAT_GUI
2298 if (gui.in_use || gui.starting)
2299 return gui_mch_haskey(name);
2300#endif
2301
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002302 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 return OK;
2304
2305 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002306 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 return FAIL;
2308
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002309 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
2311 term += 8;
2312#ifdef HAVE_TGETENT
2313 builtin_first = TRUE;
2314#endif
2315 }
2316#ifdef HAVE_TGETENT
2317 else
2318 builtin_first = p_tbi;
2319#endif
2320
2321#ifdef HAVE_TGETENT
2322/*
2323 * We can get the entry from the builtin termcap and from the external one.
2324 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2325 * builtin termcap first.
2326 * If 'ttybuiltin' is off, try external termcap first.
2327 */
2328 for (i = 0; i < 2; ++i)
2329 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002330 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331#endif
2332 /*
2333 * Search in builtin termcap
2334 */
2335 {
2336 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002337 if (termp->bt_string != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002340 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 while (termp->bt_entry != (int)KS_NAME)
2342 {
2343 if ((int)termp->bt_entry == key)
2344 {
2345 add_termcode(name, (char_u *)termp->bt_string,
2346 term_is_8bit(term));
2347 return OK;
2348 }
2349 ++termp;
2350 }
2351 }
2352 }
2353#ifdef HAVE_TGETENT
2354 else
2355 /*
2356 * Search in external termcap
2357 */
2358 {
2359 error_msg = tgetent_error(tbuf, term);
2360 if (error_msg == NULL)
2361 {
2362 string = TGETSTR((char *)name, &tp);
2363 if (string != NULL && *string != NUL)
2364 {
2365 add_termcode(name, string, FALSE);
2366 return OK;
2367 }
2368 }
2369 }
2370 }
2371#endif
2372
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002373 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
2375#ifdef HAVE_TGETENT
2376 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002377 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 else
2379#endif
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002380 semsg(_(e_no_str_entry_in_termcap), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 }
2382 return FAIL;
2383}
2384
2385 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002386term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387{
2388 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2389}
2390
2391/*
2392 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2393 * Assume that the terminal is using 8-bit controls when the name contains
2394 * "8bit", like in "xterm-8bit".
2395 */
2396 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002397term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398{
2399 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2400}
2401
2402/*
2403 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002404 * <Esc>[ -> CSI <M_C_[>
2405 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 * <Esc>O -> <M-C-O>
2407 */
2408 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002409term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
2411 if (*p == ESC)
2412 {
2413 if (p[1] == '[')
2414 return CSI;
2415 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002416 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (p[1] == 'O')
2418 return 0x8f;
2419 }
2420 return 0;
2421}
2422
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002423#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002425term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426{
2427 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2428}
2429#endif
2430
2431#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2432
2433 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002434tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435{
2436 static char_u buf[16];
2437 char_u *p;
2438
2439 p = buf + 15;
2440 *p = '\0';
2441 do
2442 {
2443 --p;
2444 *p = (char_u) (i % 10 + '0');
2445 i /= 10;
2446 }
2447 while (i > 0 && p > buf);
2448 return p;
2449}
2450#endif
2451
2452#ifndef HAVE_TGETENT
2453
2454/*
2455 * minimal tgoto() implementation.
2456 * no padding and we only parse for %i %d and %+char
2457 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002458 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002459tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460{
2461 static char buf[30];
2462 char *p, *s, *e;
2463
2464 if (!cm)
2465 return "OOPS";
2466 e = buf + 29;
2467 for (s = buf; s < e && *cm; cm++)
2468 {
2469 if (*cm != '%')
2470 {
2471 *s++ = *cm;
2472 continue;
2473 }
2474 switch (*++cm)
2475 {
2476 case 'd':
2477 p = (char *)tltoa((unsigned long)y);
2478 y = x;
2479 while (*p)
2480 *s++ = *p++;
2481 break;
2482 case 'i':
2483 x++;
2484 y++;
2485 break;
2486 case '+':
2487 *s++ = (char)(*++cm + y);
2488 y = x;
2489 break;
2490 case '%':
2491 *s++ = *cm;
2492 break;
2493 default:
2494 return "OOPS";
2495 }
2496 }
2497 *s = '\0';
2498 return buf;
2499}
2500
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002501#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503/*
2504 * Set the terminal name and initialize the terminal options.
2505 * If "name" is NULL or empty, get the terminal name from the environment.
2506 * If that fails, use the default terminal name.
2507 */
2508 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002509termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
2511 char_u *term;
2512
2513 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002514 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 term = name;
2516
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517#ifndef MSWIN
2518 if (term == NULL)
2519 term = mch_getenv((char_u *)"TERM");
2520#endif
2521 if (term == NULL || *term == NUL)
2522 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002523 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002525 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 set_string_default("term", term);
2527 set_string_default("ttytype", term);
2528
2529 /*
2530 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2531 */
2532 set_termname(T_NAME != NULL ? T_NAME : term);
2533}
2534
2535/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002536 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002538#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002539
2540// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002542
2543static int out_pos = 0; // number of chars in out_buf
2544
2545// Since the maximum number of SGR parameters shown as a normal value range is
2546// 16, the escape sequence length can be 4 * 16 + lead + tail.
2547#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
2549/*
2550 * out_flush(): flush the output buffer
2551 */
2552 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002553out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
2555 int len;
2556
2557 if (out_pos != 0)
2558 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002559 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 len = out_pos;
2561 out_pos = 0;
Bram Moolenaar4c868302021-03-22 16:19:45 +01002562 ui_write(out_buf, len, FALSE);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002563#ifdef FEAT_JOB_CHANNEL
2564 if (ch_log_output)
2565 {
2566 out_buf[len] = NUL;
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002567 ch_log(NULL, "raw %s output: \"%s\"",
Bram Moolenaare1ee58a2021-01-14 19:04:44 +01002568# ifdef FEAT_GUI
2569 (gui.in_use && !gui.dying && !gui.starting) ? "GUI" :
2570# endif
2571 "terminal",
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01002572 out_buf);
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002573 ch_log_output = FALSE;
2574 }
2575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
2577}
2578
Bram Moolenaara338adc2018-01-31 20:51:47 +01002579/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002580 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2581 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002582 */
2583 void
2584out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002585 int force UNUSED, // when TRUE, update cursor even when not moved
2586 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002587{
2588 mch_disable_flush();
2589 out_flush();
2590 mch_enable_flush();
2591#ifdef FEAT_GUI
2592 if (gui.in_use)
2593 {
2594 gui_update_cursor(force, clear_selection);
2595 gui_may_flush();
2596 }
2597#endif
2598}
2599
2600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601/*
2602 * Sometimes a byte out of a multi-byte character is written with out_char().
2603 * To avoid flushing half of the character, call this function first.
2604 */
2605 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002606out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2609 out_flush();
2610}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
2612#ifdef FEAT_GUI
2613/*
2614 * out_trash(): Throw away the contents of the output buffer
2615 */
2616 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002617out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
2619 out_pos = 0;
2620}
2621#endif
2622
2623/*
2624 * out_char(c): put a byte into the output buffer.
2625 * Flush it if it becomes full.
2626 * This should not be used for outputting text on the screen (use functions
2627 * like msg_puts() and screen_putchar() for that).
2628 */
2629 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002630out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
Bram Moolenaard0573012017-10-28 21:11:06 +02002632#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002633 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 out_char('\r');
2635#endif
2636
2637 out_buf[out_pos++] = c;
2638
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002639 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (out_pos >= OUT_SIZE || p_wd)
2641 out_flush();
2642}
2643
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002645 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 */
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002647 static int
2648out_char_nf(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002650 out_buf[out_pos++] = (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
2652 if (out_pos >= OUT_SIZE)
2653 out_flush();
Bram Moolenaar86394aa2020-09-05 14:27:24 +02002654 return (unsigned)c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655}
2656
2657/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002658 * A never-padding out_str().
2659 * Use this whenever you don't want to run the string through tputs().
2660 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 * is likely to strip off leading digits, that it mistakes for padding
2662 * information, and "%i", "%d", etc.
2663 * This should only be used for writing terminal codes, not for outputting
2664 * normal text (use functions like msg_puts() and screen_putchar() for that).
2665 */
2666 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002667out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002669 // avoid terminal strings being split up
2670 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002672
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 while (*s)
2674 out_char_nf(*s++);
2675
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002676 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 if (p_wd)
2678 out_flush();
2679}
2680
2681/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002682 * A conditional-flushing out_str, mainly for visualbell.
2683 * Handles a delay internally, because termlib may not respect the delay or do
2684 * it at the wrong time.
2685 * Note: Only for terminal strings.
2686 */
2687 void
2688out_str_cf(char_u *s)
2689{
2690 if (s != NULL && *s)
2691 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002692#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002693 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002694#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002695
2696#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002697 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002698 if (gui.in_use)
2699 {
2700 out_str_nf(s);
2701 return;
2702 }
2703#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002704 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002705 out_flush();
2706#ifdef HAVE_TGETENT
2707 for (p = s; *s; ++s)
2708 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002709 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002710 if (*s == '$' && *(s + 1) == '<')
2711 {
2712 char_u save_c = *s;
2713 int duration = atoi((char *)s + 2);
2714
2715 *s = NUL;
2716 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2717 *s = save_c;
2718 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002719# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002720 // Only sleep here if we can limit this happening in
2721 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002722 p = vim_strchr(s, '>');
2723 if (p == NULL || duration <= 0)
2724 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002725 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002726 p = s;
2727 }
2728 else
2729 {
2730 ++p;
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01002731 do_sleep(duration, FALSE);
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002732 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002733# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002734 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002735 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002736# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002737 break;
2738 }
2739 }
2740 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2741#else
2742 while (*s)
2743 out_char_nf(*s++);
2744#endif
2745
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002746 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002747 if (p_wd)
2748 out_flush();
2749 }
2750}
2751
2752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002754 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 * This should only be used for writing terminal codes, not for outputting
2756 * normal text (use functions like msg_puts() and screen_putchar() for that).
2757 */
2758 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002759out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760{
2761 if (s != NULL && *s)
2762 {
2763#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002764 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 if (gui.in_use)
2766 {
2767 out_str_nf(s);
2768 return;
2769 }
2770#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002771 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002772 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 out_flush();
2774#ifdef HAVE_TGETENT
2775 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2776#else
2777 while (*s)
2778 out_char_nf(*s++);
2779#endif
2780
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002781 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 if (p_wd)
2783 out_flush();
2784 }
2785}
2786
2787/*
2788 * cursor positioning using termcap parser. (jw)
2789 */
2790 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002791term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792{
2793 OUT_STR(tgoto((char *)T_CM, col, row));
2794}
2795
2796 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002797term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798{
2799 OUT_STR(tgoto((char *)T_CRI, 0, i));
2800}
2801
2802 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002803term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804{
2805 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2806}
2807
2808 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002809term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810{
2811 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2812}
2813
2814#if defined(HAVE_TGETENT) || defined(PROTO)
2815 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002816term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002818 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 if (x < 0)
2820 x = 0;
2821 if (y < 0)
2822 y = 0;
2823 OUT_STR(tgoto((char *)T_CWP, y, x));
2824}
2825
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002826# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2827/*
2828 * Return TRUE if we can request the terminal for a response.
2829 */
2830 static int
2831can_get_termresponse()
2832{
2833 return cur_tmode == TMODE_RAW
2834 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002835# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002836 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002837# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002838 && p_ek;
2839}
2840
Bram Moolenaarafd78262019-05-10 23:10:31 +02002841/*
2842 * Set "status" to STATUS_SENT.
2843 */
2844 static void
2845termrequest_sent(termrequest_T *status)
2846{
2847 status->tr_progress = STATUS_SENT;
2848 status->tr_start = time(NULL);
2849}
2850
2851/*
2852 * Return TRUE if any of the requests are in STATUS_SENT.
2853 */
2854 static int
2855termrequest_any_pending()
2856{
2857 int i;
2858 time_t now = time(NULL);
2859
2860 for (i = 0; all_termrequests[i] != NULL; ++i)
2861 {
2862 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2863 {
2864 if (all_termrequests[i]->tr_start > 0 && now > 0
2865 && all_termrequests[i]->tr_start + 2 < now)
2866 // Sent the request more than 2 seconds ago and didn't get a
2867 // response, assume it failed.
2868 all_termrequests[i]->tr_progress = STATUS_FAIL;
2869 else
2870 return TRUE;
2871 }
2872 }
2873 return FALSE;
2874}
2875
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002876static int winpos_x = -1;
2877static int winpos_y = -1;
2878static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002879
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002880# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002881/*
2882 * Try getting the Vim window position from the terminal.
2883 * Returns OK or FAIL.
2884 */
2885 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002886term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002887{
2888 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002889 int prev_winpos_x = winpos_x;
2890 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002891
2892 if (*T_CGP == NUL || !can_get_termresponse())
2893 return FAIL;
2894 winpos_x = -1;
2895 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002896 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002897 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002898 OUT_STR(T_CGP);
2899 out_flush();
2900
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002901 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002902 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002903 {
2904 (void)vpeekc_nomap();
2905 if (winpos_x >= 0 && winpos_y >= 0)
2906 {
2907 *x = winpos_x;
2908 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002909 return OK;
2910 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002911 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002912 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002913 // Do not reset "did_request_winpos", if we timed out the response might
2914 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002915
2916 winpos_x = prev_winpos_x;
2917 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002918 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002919 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002920 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002921 *x = winpos_x;
2922 *y = winpos_y;
2923 return OK;
2924 }
2925
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002926 return FALSE;
2927}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002928# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002929# endif
2930
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002932term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002934 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935}
2936#endif
2937
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002939term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
2941 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002942 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002943 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002945 // Special handling of 16 colors, because termcap can't handle it
2946 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
2947 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002949 && ((s[0] == ESC && s[1] == '[')
2950#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2951 || (s[0] == ESC && s[1] == '|')
2952#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02002953 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 && s[i] != NUL
2955 && (STRCMP(s + i + 1, "%p1%dm") == 0
2956 || STRCMP(s + i + 1, "%dm") == 0)
2957 && (s[i] == '3' || s[i] == '4'))
2958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002960 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002962 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002964 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002965#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002966 s[1] == '|' ? "\033|" :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002967#endif
Bram Moolenaar424bcae2022-01-31 14:59:41 +00002968 "\033[") : "\233";
Bram Moolenaard315cf52018-05-23 20:30:56 +02002969 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2970 : (n >= 16 ? "48;5;" : "10");
2971
2972 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2974 }
2975 else
2976 OUT_STR(tgoto((char *)s, 0, n));
2977}
2978
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002979 void
2980term_fg_color(int n)
2981{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002982 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002983 if (*T_CAF)
2984 term_color(T_CAF, n);
2985 else if (*T_CSF)
2986 term_color(T_CSF, n);
2987}
2988
2989 void
2990term_bg_color(int n)
2991{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002992 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002993 if (*T_CAB)
2994 term_color(T_CAB, n);
2995 else if (*T_CSB)
2996 term_color(T_CSB, n);
2997}
2998
Bram Moolenaare023e882020-05-31 16:42:30 +02002999 void
3000term_ul_color(int n)
3001{
3002 if (*T_CAU)
3003 term_color(T_CAU, n);
3004}
3005
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003006/*
3007 * Return "dark" or "light" depending on the kind of terminal.
3008 * This is just guessing! Recognized are:
3009 * "linux" Linux console
3010 * "screen.linux" Linux console with screen
3011 * "cygwin.*" Cygwin shell
3012 * "putty.*" Putty program
3013 * We also check the COLORFGBG environment variable, which is set by
3014 * rxvt and derivatives. This variable contains either two or three
3015 * values separated by semicolons; we want the last value in either
3016 * case. If this value is 0-6 or 8, our background is dark.
3017 */
3018 char_u *
3019term_bg_default(void)
3020{
3021#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003022 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01003023 return (char_u *)"dark";
3024#else
3025 char_u *p;
3026
3027 if (STRCMP(T_NAME, "linux") == 0
3028 || STRCMP(T_NAME, "screen.linux") == 0
3029 || STRNCMP(T_NAME, "cygwin", 6) == 0
3030 || STRNCMP(T_NAME, "putty", 5) == 0
3031 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3032 && (p = vim_strrchr(p, ';')) != NULL
3033 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3034 && p[2] == NUL))
3035 return (char_u *)"dark";
3036 return (char_u *)"light";
3037#endif
3038}
3039
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003040#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003041
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003042#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3043#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3044#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003045
3046 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003047term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003048{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003049#define MAX_COLOR_STR_LEN 100
3050 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003051
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003052 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003053 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003054#ifdef FEAT_VTP
3055 if (use_wt())
3056 {
3057 out_flush();
3058 buf[1] = '[';
3059 vtp_printf(buf);
3060 }
3061 else
3062#endif
3063 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003064}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003065
3066 void
3067term_fg_rgb_color(guicolor_T rgb)
3068{
3069 term_rgb_color(T_8F, rgb);
3070}
3071
3072 void
3073term_bg_rgb_color(guicolor_T rgb)
3074{
3075 term_rgb_color(T_8B, rgb);
3076}
Bram Moolenaare023e882020-05-31 16:42:30 +02003077
3078 void
3079term_ul_rgb_color(guicolor_T rgb)
3080{
3081 term_rgb_color(T_8U, rgb);
3082}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003083#endif
3084
Bram Moolenaar651fca82021-11-29 20:39:38 +00003085#if (defined(UNIX) || defined(VMS) || defined(MACOS_X)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086/*
3087 * Generic function to set window title, using t_ts and t_fs.
3088 */
3089 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003090term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003092#ifdef FEAT_JOB_CHANNEL
3093 ch_log_output = TRUE;
3094#endif
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003095 // t_ts takes one argument: column in status line
3096 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003098 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 out_flush();
3100}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003101
3102/*
3103 * Tell the terminal to push (save) the title and/or icon, so that it can be
3104 * popped (restored) later.
3105 */
3106 void
3107term_push_title(int which)
3108{
Bram Moolenaar27821262019-05-08 16:41:09 +02003109 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003110 {
3111 OUT_STR(T_CST);
3112 out_flush();
3113 }
3114
Bram Moolenaar27821262019-05-08 16:41:09 +02003115 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003116 {
3117 OUT_STR(T_SSI);
3118 out_flush();
3119 }
3120}
3121
3122/*
3123 * Tell the terminal to pop the title and/or icon.
3124 */
3125 void
3126term_pop_title(int which)
3127{
Bram Moolenaar27821262019-05-08 16:41:09 +02003128 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003129 {
3130 OUT_STR(T_CRT);
3131 out_flush();
3132 }
3133
Bram Moolenaar27821262019-05-08 16:41:09 +02003134 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003135 {
3136 OUT_STR(T_SRI);
3137 out_flush();
3138 }
3139}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140#endif
3141
3142/*
3143 * Make sure we have a valid set or terminal options.
3144 * Replace all entries that are NULL by empty_option
3145 */
3146 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003147ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003149 char_u *env_colors;
3150
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003151 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 /*
3154 * MUST have "cm": cursor motion.
3155 */
3156 if (*T_CM == NUL)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003157 emsg(_(e_terminal_capability_cm_required));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
3159 /*
3160 * if "cs" defined, use a scroll region, it's faster.
3161 */
3162 if (*T_CS != NUL)
3163 scroll_region = TRUE;
3164 else
3165 scroll_region = FALSE;
3166
3167 if (pairs)
3168 {
3169 /*
3170 * optional pairs
3171 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003172 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 if (*T_ME == NUL)
3174 T_ME = T_MR = T_MD = T_MB = empty_option;
3175 if (*T_SO == NUL || *T_SE == NUL)
3176 T_SO = T_SE = empty_option;
3177 if (*T_US == NUL || *T_UE == NUL)
3178 T_US = T_UE = empty_option;
3179 if (*T_CZH == NUL || *T_CZR == NUL)
3180 T_CZH = T_CZR = empty_option;
3181
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003182 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 if (*T_VE == NUL)
3184 T_VI = empty_option;
3185
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003186 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 if (*T_ME == NUL)
3188 {
3189 T_ME = T_SE;
3190 T_MR = T_SO;
3191 T_MD = T_SO;
3192 }
3193
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003194 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 if (*T_SO == NUL)
3196 {
3197 T_SE = T_ME;
3198 if (*T_MR == NUL)
3199 T_SO = T_MD;
3200 else
3201 T_SO = T_MR;
3202 }
3203
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003204 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 if (*T_CZH == NUL)
3206 {
3207 T_CZR = T_ME;
3208 if (*T_MR == NUL)
3209 T_CZH = T_MD;
3210 else
3211 T_CZH = T_MR;
3212 }
3213
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003214 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 if (*T_CSB == NUL || *T_CSF == NUL)
3216 {
3217 T_CSB = empty_option;
3218 T_CSF = empty_option;
3219 }
3220
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003221 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (*T_CAB == NUL || *T_CAF == NUL)
3223 {
3224 T_CAB = empty_option;
3225 T_CAF = empty_option;
3226 }
3227
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003228 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003230 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003232 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 p_wiv = (*T_XS != NUL);
3234 }
3235 need_gather = TRUE;
3236
Bram Moolenaar759d8152020-04-26 16:52:49 +02003237 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3238 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003240#ifdef FEAT_GUI
3241 if (!gui.in_use)
3242#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003243 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003244 env_colors = mch_getenv((char_u *)"COLORS");
3245 if (env_colors != NULL && isdigit(*env_colors))
3246 {
3247 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003248
Bram Moolenaar759d8152020-04-26 16:52:49 +02003249 if (colors != t_colors)
3250 set_color_count(colors);
3251 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253}
3254
3255#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3256 || defined(PROTO)
3257/*
3258 * Represent the given long_u as individual bytes, with the most significant
3259 * byte first, and store them in dst.
3260 */
3261 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003262add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 int i;
3265 int shift;
3266
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003267 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
3269 shift = 8 * (sizeof(long_u) - i);
3270 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3271 }
3272}
3273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274/*
3275 * Interpret the next string of bytes in buf as a long integer, with the most
3276 * significant byte first. Note that it is assumed that buf has been through
3277 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3278 * Puts result in val, and returns the number of bytes read from buf
3279 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3280 * were present.
3281 */
3282 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003283get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
3285 int len;
3286 char_u bytes[sizeof(long_u)];
3287 int i;
3288 int shift;
3289
3290 *val = 0;
3291 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3292 if (len != -1)
3293 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003294 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
3296 shift = 8 * (sizeof(long_u) - 1 - i);
3297 *val += (long_u)bytes[i] << shift;
3298 }
3299 }
3300 return len;
3301}
3302#endif
3303
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304/*
3305 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3306 * that buf has been through inchar(). Returns the actual number of bytes used
3307 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3308 * available.
3309 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003310 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003311get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313 int len = 0;
3314 int i;
3315 char_u c;
3316
3317 for (i = 0; i < num_bytes; i++)
3318 {
3319 if ((c = buf[len++]) == NUL)
3320 return -1;
3321 if (c == K_SPECIAL)
3322 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003323 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 return -1;
3325 if (buf[len++] == (int)KS_ZERO)
3326 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003327 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3328 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003329 if (buf[len++] == (int)KE_CSI)
3330 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003332 else if (c == CSI && buf[len] == KS_EXTRA
3333 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003334 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3335 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003336 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 bytes[i] = c;
3338 }
3339 return len;
3340}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
3342/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003343 * Check if the new shell size is valid, correct it if it's too small or way
3344 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 */
3346 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003347check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003349 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003351 limit_screen_size();
3352}
3353
3354/*
3355 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3356 */
3357 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003358limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003359{
3360 if (Columns < MIN_COLUMNS)
3361 Columns = MIN_COLUMNS;
3362 else if (Columns > 10000)
3363 Columns = 10000;
3364 if (Rows > 1000)
3365 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366}
3367
Bram Moolenaar86b68352004-12-27 21:59:20 +00003368/*
3369 * Invoked just before the screen structures are going to be (re)allocated.
3370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003372win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 static int old_Rows = 0;
3375 static int old_Columns = 0;
3376
3377 if (old_Rows != Rows || old_Columns != Columns)
3378 ui_new_shellsize();
3379 if (old_Rows != Rows)
3380 {
Bram Moolenaar0a1a6a12021-03-26 14:14:18 +01003381 // If 'window' uses the whole screen, keep it using that.
3382 // Don't change it when set with "-w size" on the command line.
K.Takata6ca883d2022-03-07 13:31:15 +00003383 if (p_window == old_Rows - 1
3384 || (old_Rows == 0 && !option_was_set((char_u *)"window")))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003385 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003387 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 }
3389 if (old_Columns != Columns)
3390 {
3391 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003392 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 }
3394}
3395
3396/*
3397 * Call this function when the Vim shell has been resized in any way.
3398 * Will obtain the current size and redraw (also when size didn't change).
3399 */
3400 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003401shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402{
3403 set_shellsize(0, 0, FALSE);
3404}
3405
3406/*
3407 * Check if the shell size changed. Handle a resize.
3408 * When the size didn't change, nothing happens.
3409 */
3410 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003411shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412{
3413 int old_Rows = Rows;
3414 int old_Columns = Columns;
3415
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003416 if (!exiting
3417#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003418 // Do not get the size when executing a shell command during
3419 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003420 && !gui.starting
3421#endif
3422 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003423 {
3424 (void)ui_get_shellsize();
3425 check_shellsize();
3426 if (old_Rows != Rows || old_Columns != Columns)
3427 shell_resized();
3428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429}
3430
3431/*
3432 * Set size of the Vim shell.
3433 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3434 * window size (this is used for the :win command).
3435 * If 'mustset' is FALSE, we may try to get the real window size and if
3436 * it fails use 'width' and 'height'.
3437 */
3438 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003439set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
3441 static int busy = FALSE;
3442
3443 /*
3444 * Avoid recursiveness, can happen when setting the window size causes
3445 * another window-changed signal.
3446 */
3447 if (busy)
3448 return;
3449
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003450 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 return;
3452
Bram Moolenaara971b822011-09-14 14:43:25 +02003453 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003455 // postpone the resizing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 State = SETWSIZE;
3457 return;
3458 }
3459
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003460 if (updating_screen)
3461 // resizing while in update_screen() may cause a crash
3462 return;
3463
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003464 // curwin->w_buffer can be NULL when we are closing a window and the
Bram Moolenaar2808da32020-12-30 14:08:35 +01003465 // buffer (or window) has already been closed and removing a scrollbar
3466 // causes a resize event. Don't resize then, it will happen after entering
3467 // another buffer.
3468 if (curwin->w_buffer == NULL || curwin->w_lines == NULL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003469 return;
3470
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 ++busy;
3472
3473#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003474 out_flush(); // must do this before mch_get_shellsize() for
3475 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476#endif
3477
3478 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3479 {
3480 Rows = height;
3481 Columns = width;
3482 check_shellsize();
3483 ui_set_shellsize(mustset);
3484 }
3485 else
3486 check_shellsize();
3487
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003488 // The window layout used to be adjusted here, but it now happens in
3489 // screenalloc() (also invoked from screenclear()). That is because the
3490 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
3492 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3493 screenclear();
3494 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003495 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
3497 if (starting != NO_SCREEN)
3498 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 maketitle();
Bram Moolenaar651fca82021-11-29 20:39:38 +00003500
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 changed_line_abv_curs();
3502 invalidate_botline();
3503
3504 /*
3505 * We only redraw when it's needed:
3506 * - While at the more prompt or executing an external command, don't
3507 * redraw, but position the cursor.
3508 * - While editing the command line, only redraw that.
3509 * - in Ex mode, don't redraw anything.
3510 * - Otherwise, redraw right now, and position the cursor.
3511 * Always need to call update_screen() or screenalloc(), to make
3512 * sure Rows/Columns and the size of ScreenLines[] is correct!
3513 */
3514 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3515 || exmode_active)
3516 {
3517 screenalloc(FALSE);
3518 repeat_message();
3519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 else
3521 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003522 if (curwin->w_p_scb)
3523 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003524 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003525 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003526 update_screen(NOT_VALID);
3527 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003528 }
3529 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003530 {
3531 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003532 if (pum_visible())
3533 {
3534 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003535 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003536 }
Bram Moolenaara5e66212017-09-29 22:42:33 +02003537 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003538 if (redrawing())
3539 setcursor();
3540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003542 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
3544 out_flush();
3545 --busy;
3546}
3547
3548/*
3549 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3550 * commands and Ex mode).
3551 */
3552 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003553settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
3555#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003556 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 if (gui.in_use)
3558 return;
3559#endif
3560
3561 if (full_screen)
3562 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003564 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3565 * set the terminal to raw mode, even though we think it already is,
3566 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 * When we think the terminal is normal, don't try to set it to
3568 * normal again, because that causes problems (logout!) on some
3569 * machines.
3570 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003571 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
3573#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003574# ifdef FEAT_GUI
3575 if (!gui.in_use && !gui.starting)
3576# endif
3577 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003578 // May need to check for T_CRV response and termcodes, it
3579 // doesn't work in Cooked mode, an external program may get
3580 // them.
3581 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003582 (void)vpeekc_nomap();
3583 check_for_codes_from_term();
3584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003587 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003588
3589 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3590 // Avoid doing this too often, on some terminals the codes are not
3591 // handled properly.
3592 if (termcap_active && tmode != TMODE_SLEEP
3593 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003594 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003595#ifdef FEAT_JOB_CHANNEL
3596 ch_log_output = TRUE;
3597#endif
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003598 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003599 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003600 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003601 out_str(T_CTE); // possibly disables modifyOtherKeys
3602 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003603 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003604 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003605 out_str(T_BE); // enable bracketed paste mode (should
3606 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003607 out_str(T_CTI); // possibly enables modifyOtherKeys
3608 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003611 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003614 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 out_flush();
3616 }
3617#ifdef FEAT_TERMRESPONSE
3618 may_req_termresponse();
3619#endif
3620 }
3621}
3622
3623 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003624starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625{
3626 if (full_screen && !termcap_active)
3627 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003628#ifdef FEAT_JOB_CHANNEL
3629 ch_log_output = TRUE;
3630#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003631 out_str(T_TI); // start termcap mode
3632 out_str(T_CTI); // start "raw" mode
3633 out_str(T_KS); // start "keypad transmit" mode
3634 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003635
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003636#if defined(UNIX) || defined(VMS)
3637 // Enable xterm's focus reporting mode when 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003638 if (p_ek && *T_FE != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003639 out_str(T_FE);
3640#endif
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 out_flush();
3643 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003644 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003646# ifdef FEAT_GUI
3647 if (!gui.in_use && !gui.starting)
3648# endif
3649 {
3650 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003651 // Immediately check for a response. If t_Co changes, we don't
3652 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003653 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003654 check_for_codes_from_term();
3655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656#endif
3657 }
3658}
3659
3660 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003661stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
3663 screen_stop_highlight();
3664 reset_cterm_colors();
3665 if (termcap_active)
3666 {
3667#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003668# ifdef FEAT_GUI
3669 if (!gui.in_use && !gui.starting)
3670# endif
3671 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003672 // May need to discard T_CRV, T_U7 or T_RBG response.
3673 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003674 {
3675# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003676 // Give the terminal a chance to respond.
Bram Moolenaar0981c872020-08-23 14:28:37 +02003677 mch_delay(100L, 0);
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003678# endif
3679# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003680 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003681 if (exiting)
3682 tcflush(fileno(stdin), TCIFLUSH);
3683# endif
3684 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003685 // Check for termcodes first, otherwise an external program may
3686 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003687 check_for_codes_from_term();
3688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689#endif
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003690#ifdef FEAT_JOB_CHANNEL
3691 ch_log_output = TRUE;
3692#endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003693
Bram Moolenaar51b477f2021-03-03 15:24:28 +01003694#if defined(UNIX) || defined(VMS)
3695 // Disable xterm's focus reporting mode if 'esckeys' is set.
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00003696 if (p_ek && *T_FD != NUL)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01003697 out_str(T_FD);
3698#endif
3699
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003700 out_str(T_BD); // disable bracketed paste mode
3701 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 out_flush();
3703 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003704 cursor_on(); // just in case it is still off
3705 out_str(T_CTE); // stop "raw" mode
3706 out_str(T_TE); // stop termcap mode
3707 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 out_flush();
3709 }
3710}
3711
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003712#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713/*
3714 * Request version string (for xterm) when needed.
3715 * Only do this after switching to raw mode, otherwise the result will be
3716 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003717 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003718 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 * Only do this after termcap mode has been started, otherwise the codes for
3720 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003721 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3722 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003723 * On Unix only do it when both output and input are a tty (avoid writing
3724 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 * The result is caught in check_termcode().
3726 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003727 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003728may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003730 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003731 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003732 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 && *T_CRV != NUL)
3734 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003735#ifdef FEAT_JOB_CHANNEL
3736 ch_log_output = TRUE;
3737#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003738 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003740 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003741 // check for the characters now, otherwise they might be eaten by
3742 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 out_flush();
3744 (void)vpeekc_nomap();
3745 }
3746}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003747
Bram Moolenaar9584b312013-03-13 19:29:28 +01003748/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003749 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3750 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003751 * Note that this goes out before T_CRV, so that the result can be used when
3752 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003753 */
3754 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003755check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003756{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003757 int did_send = FALSE;
3758
3759 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3760 return;
3761
Bram Moolenaarafd78262019-05-10 23:10:31 +02003762 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003763 && !option_was_set((char_u *)"ambiwidth"))
3764 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003765 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003766
Bram Moolenaara45551a2020-06-09 15:57:37 +02003767 // Ambiguous width check.
3768 // Check how the terminal treats ambiguous character width (UAX #11).
3769 // First, we move the cursor to (1, 0) and print a test ambiguous
3770 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3771 // the current cursor position. If the terminal treats \u25bd as
3772 // single width, the position is (1, 1), or if it is treated as double
3773 // width, that will be (1, 2). This function has the side effect that
3774 // changes cursor position, so it must be called immediately after
3775 // entering termcap mode.
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003776#ifdef FEAT_JOB_CHANNEL
3777 ch_log_output = TRUE;
3778#endif
Bram Moolenaara45551a2020-06-09 15:57:37 +02003779 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003780 // Do this in the second row. In the first row the returned sequence
3781 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003782 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003783 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003784 out_str(buf);
3785 out_str(T_U7);
3786 termrequest_sent(&u7_status);
3787 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003788 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003789
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003790 // This overwrites a few characters on the screen, a redraw is needed
3791 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003792 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003793 term_windgoto(1, 0);
3794 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003795 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003796 }
3797
Bram Moolenaard1f34e62022-01-07 19:24:20 +00003798 if (xcc_status.tr_progress == STATUS_GET && Rows > 2)
Bram Moolenaara45551a2020-06-09 15:57:37 +02003799 {
3800 // 2. Check compatibility with xterm.
3801 // We move the cursor to (2, 0), print a test sequence and then query
3802 // the current cursor position. If the terminal properly handles
3803 // unknown DCS string and CSI sequence with intermediate byte, the test
3804 // sequence is ignored and the cursor does not move. If the terminal
3805 // handles test sequence incorrectly, a garbage string is displayed and
3806 // the cursor does move.
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003807#ifdef FEAT_JOB_CHANNEL
3808 ch_log_output = TRUE;
3809#endif
Bram Moolenaara45551a2020-06-09 15:57:37 +02003810 LOG_TR(("Sending xterm compatibility test sequence."));
3811 // Do this in the third row. Second row is used by ambiguous
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003812 // character width check.
Bram Moolenaara45551a2020-06-09 15:57:37 +02003813 term_windgoto(2, 0);
3814 // send the test DCS string.
3815 out_str((char_u *)"\033Pzz\033\\");
3816 // send the test CSI sequence with intermediate byte.
3817 out_str((char_u *)"\033[0%m");
3818 out_str(T_U7);
3819 termrequest_sent(&xcc_status);
3820 out_flush();
3821 did_send = TRUE;
3822
3823 // If the terminal handles test sequence incorrectly, garbage text is
3824 // displayed. Clear them out for now.
3825 screen_stop_highlight();
3826 term_windgoto(2, 0);
3827 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003828 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003829 }
3830
3831 if (did_send)
3832 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003833 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003834
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003835 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003836 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003837
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003838 // check for the characters now, otherwise they might be eaten by
3839 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003840 out_flush();
3841 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003842 }
3843}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003844
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003845/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003846 * Similar to requesting the version string: Request the terminal background
3847 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003848 */
3849 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003850may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003851{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003852 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003853 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003854 int didit = FALSE;
3855
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003856# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003857 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003858 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003859 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003860#ifdef FEAT_JOB_CHANNEL
3861 ch_log_output = TRUE;
3862#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003863 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003864 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003865 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003866 didit = TRUE;
3867 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003868# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003869
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003870 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003871 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003872 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003873#ifdef FEAT_JOB_CHANNEL
3874 ch_log_output = TRUE;
3875#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02003876 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003877 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003878 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003879 didit = TRUE;
3880 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003881
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003882 if (didit)
3883 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003884 // check for the characters now, otherwise they might be eaten by
3885 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003886 out_flush();
3887 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003888 }
3889 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003890}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003891
Bram Moolenaar2951b772013-07-03 12:45:31 +02003892# ifdef DEBUG_TERMRESPONSE
3893 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003894log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003895{
3896 static FILE *fd_tr = NULL;
3897 static proftime_T start;
3898 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003899 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003900
3901 if (fd_tr == NULL)
3902 {
3903 fd_tr = fopen("termresponse.log", "w");
3904 profile_start(&start);
3905 }
3906 now = start;
3907 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003908 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3909 must_redraw == NOT_VALID ? "NV"
3910 : must_redraw == CLEAR ? "CL" : " ");
3911 va_start(ap, fmt);
3912 vfprintf(fd_tr, fmt, ap);
3913 va_end(ap);
3914 fputc('\n', fd_tr);
3915 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003916}
3917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918#endif
3919
3920/*
3921 * Return TRUE when saving and restoring the screen.
3922 */
3923 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003924swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925{
3926 return (full_screen && *T_TI != NUL);
3927}
3928
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929/*
3930 * By outputting the 'cursor very visible' termcap code, for some windowed
3931 * terminals this makes the screen scrolled to the correct position.
3932 * Used when starting Vim or returning from a shell.
3933 */
3934 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003935scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003937 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02003939#ifdef FEAT_JOB_CHANNEL
3940 ch_log_output = TRUE;
3941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003943 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003944 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
3946}
3947
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003948// True if cursor is not visible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949static int cursor_is_off = FALSE;
3950
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003951// True if cursor is not visible due to an ongoing cursor-less sleep
3952static int cursor_is_asleep = FALSE;
3953
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003955 * Enable the cursor without checking if it's already enabled.
3956 */
3957 void
3958cursor_on_force(void)
3959{
3960 out_str(T_VE);
3961 cursor_is_off = FALSE;
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003962 cursor_is_asleep = FALSE;
Bram Moolenaar2e310482018-08-21 13:09:10 +02003963}
3964
3965/*
3966 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 */
3968 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003969cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003971 if (cursor_is_off && !cursor_is_asleep)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003972 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973}
3974
3975/*
3976 * Disable the cursor.
3977 */
3978 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003979cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003981 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003983 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 cursor_is_off = TRUE;
3985 }
3986}
3987
Dominique Pelle748b3082022-01-08 12:41:16 +00003988#ifdef FEAT_GUI
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003989/*
3990 * Check whether the cursor is invisible due to an ongoing cursor-less sleep
3991 */
3992 int
3993cursor_is_sleeping(void)
3994{
3995 return cursor_is_asleep;
3996}
Dominique Pelle748b3082022-01-08 12:41:16 +00003997#endif
Bram Moolenaar09f067f2021-04-11 13:29:18 +02003998
3999/*
4000 * Disable the cursor and mark it disabled by cursor-less sleep
4001 */
4002 void
4003cursor_sleep(void)
4004{
4005 cursor_is_asleep = TRUE;
4006 cursor_off();
4007}
4008
4009/*
4010 * Enable the cursor and mark it not disabled by cursor-less sleep
4011 */
4012 void
4013cursor_unsleep(void)
4014{
4015 cursor_is_asleep = FALSE;
4016 cursor_on();
4017}
4018
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004019#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004021 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004022 */
4023 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004024term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004025{
Bram Moolenaarc9770922017-08-12 20:11:53 +02004026 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004027 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004028
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004029 // Only do something when redrawing the screen and we can restore the
4030 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004031 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004032 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004033# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004034 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004035 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004036 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02004037# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004038 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004039 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004040
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004041 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004042 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004043 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004044 {
4045 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004046 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004047 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004048 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004049 if (*p != NUL)
4050 {
4051 out_str(p);
4052 showing_mode = REPLACE;
4053 }
4054 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004055 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004056 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004057 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004058 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004059 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004060 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004061 showing_mode = INSERT;
4062 }
4063 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004064 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004065 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004066 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02004067 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004068 }
4069}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004070
4071# if defined(FEAT_TERMINAL) || defined(PROTO)
4072 void
4073term_cursor_color(char_u *color)
4074{
4075 if (*T_CSC != NUL)
4076 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004077 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004078 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02004079 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004080 out_flush();
4081 }
4082}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02004083# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004084
Bram Moolenaar4db25542017-08-28 22:43:05 +02004085 int
4086blink_state_is_inverted()
4087{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004088#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02004089 return rbm_status.tr_progress == STATUS_GOT
4090 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02004091 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02004092#else
4093 return FALSE;
4094#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004095}
4096
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004097/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004098 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004099 */
4100 void
4101term_cursor_shape(int shape, int blink)
4102{
4103 if (*T_CSH != NUL)
4104 {
4105 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
4106 out_flush();
4107 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004108 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004109 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004110 int do_blink = blink;
4111
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004112 // t_SH is empty: try setting just the blink state.
4113 // The blink flags are XORed together, if the initial blinking from
4114 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004115 if (blink_state_is_inverted())
4116 do_blink = !blink;
4117
4118 if (do_blink && *T_VS != NUL)
4119 {
4120 out_str(T_VS);
4121 out_flush();
4122 }
4123 else if (!do_blink && *T_CVS != NUL)
4124 {
4125 out_str(T_CVS);
4126 out_flush();
4127 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004128 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004129}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004130#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004131
4132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 * Set scrolling region for window 'wp'.
4134 * The region starts 'off' lines from the start of the window.
4135 * Also set the vertical scroll region for a vertically split window. Always
4136 * the full width of the window, excluding the vertical separator.
4137 */
4138 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004139scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140{
4141 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4142 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004144 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4145 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004146 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147}
4148
4149/*
4150 * Reset scrolling region to the whole screen.
4151 */
4152 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004153scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154{
4155 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 if (*T_CSV != NUL)
4157 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004158 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159}
4160
4161
4162/*
4163 * List of terminal codes that are currently recognized.
4164 */
4165
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004166static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004168 char_u name[2]; // termcap name of entry
4169 char_u *code; // terminal code (in allocated memory)
4170 int len; // STRLEN(code)
4171 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172} *termcodes = NULL;
4173
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004174static int tc_max_len = 0; // number of entries that termcodes[] can hold
4175static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004177static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004178
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004180clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181{
4182 while (tc_len > 0)
4183 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004184 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 tc_max_len = 0;
4186
4187#ifdef HAVE_TGETENT
4188 BC = (char *)empty_option;
4189 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004190 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 ospeed = 0;
4192#endif
4193
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004194 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195}
4196
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004197#define ATC_FROM_TERM 55
4198
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199/*
4200 * Add a new entry to the list of terminal codes.
4201 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004202 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4203 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 */
4205 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004206add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208 struct termcode *new_tc;
4209 int i, j;
4210 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004211 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
4213 if (string == NULL || *string == NUL)
4214 {
4215 del_termcode(name);
4216 return;
4217 }
4218
Bram Moolenaar4f974752019-02-17 17:44:42 +01004219#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004220 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004221#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004222# ifdef VIMDLL
4223 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004224 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004225 else
4226# endif
4227 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 if (s == NULL)
4230 return;
4231
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004232 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004233 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004235 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 s[0] = term_7to8bit(string);
4237 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004238
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004239#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4240# ifdef VIMDLL
4241 if (!gui.in_use)
4242# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004243 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004244 if (s[0] == K_NUL)
4245 {
4246 STRMOVE(s + 1, s);
4247 s[1] = 3;
4248 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004249 }
4250#endif
4251
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004252 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004254 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
4256 /*
4257 * need to make space for more entries
4258 */
4259 if (tc_len == tc_max_len)
4260 {
4261 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004262 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (new_tc == NULL)
4264 {
4265 tc_max_len -= 20;
Dominique Pelle28cf44f2021-05-29 22:34:19 +02004266 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 return;
4268 }
4269 for (i = 0; i < tc_len; ++i)
4270 new_tc[i] = termcodes[i];
4271 vim_free(termcodes);
4272 termcodes = new_tc;
4273 }
4274
4275 /*
4276 * Look for existing entry with the same name, it is replaced.
4277 * Look for an existing entry that is alphabetical higher, the new entry
4278 * is inserted in front of it.
4279 */
4280 for (i = 0; i < tc_len; ++i)
4281 {
4282 if (termcodes[i].name[0] < name[0])
4283 continue;
4284 if (termcodes[i].name[0] == name[0])
4285 {
4286 if (termcodes[i].name[1] < name[1])
4287 continue;
4288 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004289 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 */
4291 if (termcodes[i].name[1] == name[1])
4292 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004293 if (flags == ATC_FROM_TERM && (j = termcode_star(
4294 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004295 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004296 // Don't replace ESC[123;*X or ESC O*X with another when
4297 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004298 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004299 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004300 && s[len - 1]
4301 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004302 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004303 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004304 vim_free(s);
4305 return;
4306 }
4307 }
4308 else
4309 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004310 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004311 vim_free(termcodes[i].code);
4312 --tc_len;
4313 break;
4314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
4316 }
4317 /*
4318 * Found alphabetical larger entry, move rest to insert new entry
4319 */
4320 for (j = tc_len; j > i; --j)
4321 termcodes[j] = termcodes[j - 1];
4322 break;
4323 }
4324
4325 termcodes[i].name[0] = name[0];
4326 termcodes[i].name[1] = name[1];
4327 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004328 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004329
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004330 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4331 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004332 termcodes[i].modlen = 0;
4333 j = termcode_star(s, len);
4334 if (j > 0)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004335 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004336 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004337 // For "CSI[@;X" the "@" is not included in "modlen".
4338 if (termcodes[i].code[termcodes[i].modlen - 1] == '@')
4339 --termcodes[i].modlen;
4340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 ++tc_len;
4342}
4343
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004344/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004345 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004346 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004347 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004348 */
4349 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004350termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004351{
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01004352 // Shortest is <M-O>*X. With ; shortest is <CSI>@;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004353 if (len >= 3 && code[len - 2] == '*')
4354 {
4355 if (len >= 5 && code[len - 3] == ';')
4356 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004357 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004358 return 1;
4359 }
4360 return 0;
4361}
4362
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004364find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365{
4366 int i;
4367
4368 for (i = 0; i < tc_len; ++i)
4369 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4370 return termcodes[i].code;
4371 return NULL;
4372}
4373
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004375get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376{
4377 if (i >= tc_len)
4378 return NULL;
4379 return &termcodes[i].name[0];
4380}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004382/*
4383 * Returns the length of the terminal code at index 'idx'.
4384 */
4385 int
4386get_termcode_len(int idx)
4387{
4388 return termcodes[idx].len;
4389}
4390
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004391 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004392del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393{
4394 int i;
4395
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004396 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 return;
4398
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004399 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
4401 for (i = 0; i < tc_len; ++i)
4402 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4403 {
4404 del_termcode_idx(i);
4405 return;
4406 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004407 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408}
4409
4410 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004411del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412{
4413 int i;
4414
4415 vim_free(termcodes[idx].code);
4416 --tc_len;
4417 for (i = idx; i < tc_len; ++i)
4418 termcodes[i] = termcodes[i + 1];
4419}
4420
4421#ifdef FEAT_TERMRESPONSE
4422/*
4423 * Called when detected that the terminal sends 8-bit codes.
4424 * Convert all 7-bit codes to their 8-bit equivalent.
4425 */
4426 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004427switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428{
4429 int i;
4430 int c;
4431
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004432 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 if (!term_is_8bit(T_NAME))
4434 {
4435 for (i = 0; i < tc_len; ++i)
4436 {
4437 c = term_7to8bit(termcodes[i].code);
4438 if (c != 0)
4439 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004440 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 termcodes[i].code[0] = c;
4442 }
4443 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004444 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004447 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448}
4449#endif
4450
4451#ifdef CHECK_DOUBLE_CLICK
4452static linenr_T orig_topline = 0;
4453# ifdef FEAT_DIFF
4454static int orig_topfill = 0;
4455# endif
4456#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004457#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458/*
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004459 * Checking for double-clicks ourselves.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 * "orig_topline" is used to avoid detecting a double-click when the window
4461 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4462 */
4463/*
4464 * Set orig_topline. Used when jumping to another window, so that a double
4465 * click still works.
4466 */
4467 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004468set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469{
4470 orig_topline = wp->w_topline;
4471# ifdef FEAT_DIFF
4472 orig_topfill = wp->w_topfill;
4473# endif
4474}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004475
4476/*
4477 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4478 * topline and topfill.
4479 */
4480 int
4481is_mouse_topline(win_T *wp)
4482{
4483 return orig_topline == wp->w_topline
4484#ifdef FEAT_DIFF
4485 && orig_topfill == wp->w_topfill
4486#endif
4487 ;
4488}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489#endif
4490
4491/*
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004492 * Put "string[new_slen]" in typebuf, or in "buf[bufsize]" if "buf" is not NULL.
4493 * Remove "slen" bytes.
4494 * Returns FAIL for error.
4495 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004496 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004497put_string_in_typebuf(
4498 int offset,
4499 int slen,
4500 char_u *string,
4501 int new_slen,
4502 char_u *buf,
4503 int bufsize,
4504 int *buflen)
4505{
4506 int extra = new_slen - slen;
4507
4508 string[new_slen] = NUL;
4509 if (buf == NULL)
4510 {
4511 if (extra < 0)
4512 // remove matched chars, taking care of noremap
4513 del_typebuf(-extra, offset);
4514 else if (extra > 0)
4515 // insert the extra space we need
4516 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
4517
4518 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4519 // typebuf.tb_buf[]!
4520 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4521 (size_t)new_slen);
4522 }
4523 else
4524 {
4525 if (extra < 0)
4526 // remove matched characters
4527 mch_memmove(buf + offset, buf + offset - extra,
4528 (size_t)(*buflen + offset + extra));
4529 else if (extra > 0)
4530 {
4531 // Insert the extra space we need. If there is insufficient
4532 // space return -1.
4533 if (*buflen + extra + new_slen >= bufsize)
4534 return FAIL;
4535 mch_memmove(buf + offset + extra, buf + offset,
4536 (size_t)(*buflen - offset));
4537 }
4538 mch_memmove(buf + offset, string, (size_t)new_slen);
4539 *buflen = *buflen + extra + new_slen;
4540 }
4541 return OK;
4542}
4543
4544/*
4545 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4546 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004547 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004548decode_modifiers(int n)
4549{
4550 int code = n - 1;
4551 int modifiers = 0;
4552
4553 if (code & 1)
4554 modifiers |= MOD_MASK_SHIFT;
4555 if (code & 2)
4556 modifiers |= MOD_MASK_ALT;
4557 if (code & 4)
4558 modifiers |= MOD_MASK_CTRL;
4559 if (code & 8)
4560 modifiers |= MOD_MASK_META;
4561 return modifiers;
4562}
4563
4564 static int
4565modifiers2keycode(int modifiers, int *key, char_u *string)
4566{
4567 int new_slen = 0;
4568
4569 if (modifiers != 0)
4570 {
4571 // Some keys have the modifier included. Need to handle that here to
Bram Moolenaar749bc952020-10-31 16:33:47 +01004572 // make mappings work. This may result in a special key, such as
4573 // K_S_TAB.
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004574 *key = simplify_key(*key, &modifiers);
4575 if (modifiers != 0)
4576 {
4577 string[new_slen++] = K_SPECIAL;
4578 string[new_slen++] = (int)KS_MODIFIER;
4579 string[new_slen++] = modifiers;
4580 }
4581 }
4582 return new_slen;
4583}
4584
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004585#ifdef FEAT_TERMRESPONSE
4586/*
4587 * Handle a cursor position report.
4588 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004589 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004590handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004591{
4592 if (arg[0] == 2 && arg[1] >= 2)
4593 {
4594 char *aw = NULL;
4595
4596 LOG_TR(("Received U7 status: %s", tp));
4597 u7_status.tr_progress = STATUS_GOT;
4598 did_cursorhold = TRUE;
4599 if (arg[1] == 2)
4600 aw = "single";
4601 else if (arg[1] == 3)
4602 aw = "double";
4603 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4604 {
4605 // Setting the option causes a screen redraw. Do
4606 // that right away if possible, keeping any
4607 // messages.
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004608 set_option_value_give_err((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004609# ifdef DEBUG_TERMRESPONSE
4610 {
4611 int r = redraw_asap(CLEAR);
4612
4613 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4614 }
4615# else
4616 redraw_asap(CLEAR);
4617# endif
4618# ifdef FEAT_EVAL
4619 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4620# endif
4621 }
4622 }
4623 else if (arg[0] == 3)
4624 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004625 int value;
4626
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004627 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004628 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004629
4630 // Third row: xterm compatibility test.
4631 // If the cursor is on the first column then the terminal can handle
4632 // the request for cursor style and blinking.
4633 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4634 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4635 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004636 }
4637}
4638
4639/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004640 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004641 * Xterm and alike use '>' for {first}.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004642 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004643 */
4644 static void
4645handle_version_response(int first, int *arg, int argc, char_u *tp)
4646{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004647 // The xterm version. It is set to zero when it can't be an actual xterm
4648 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004649 int version = arg[1];
4650
4651 LOG_TR(("Received CRV response: %s", tp));
4652 crv_status.tr_progress = STATUS_GOT;
4653 did_cursorhold = TRUE;
4654
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004655 // Reset terminal properties that are set based on the termresponse.
4656 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004657 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004658 init_term_props(
4659#ifdef FEAT_EVAL
4660 reset_term_props_on_termresponse
4661#else
4662 FALSE
4663#endif
4664 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004665
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004666 // If this code starts with CSI, you can bet that the
4667 // terminal uses 8-bit codes.
4668 if (tp[0] == CSI)
4669 switch_to_8bit();
4670
4671 // Screen sends 40500.
4672 // rxvt sends its version number: "20703" is 2.7.3.
4673 // Ignore it for when the user has set 'term' to xterm,
4674 // even though it's an rxvt.
4675 if (version > 20000)
4676 version = 0;
4677
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004678 // Figure out more if the reeponse is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004679 if (first == '>' && argc == 3)
4680 {
4681 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004682
4683 // mintty 2.9.5 sends 77;20905;0c.
4684 // (77 is ASCII 'M' for mintty.)
4685 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004686 {
4687 // mintty can do SGR mouse reporting
4688 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4689 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004690
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004691 // If xterm version >= 141 try to get termcap codes. For other
4692 // terminals the request should be ignored.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00004693 if (version >= 141 && p_xtermcodes)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004694 {
4695 LOG_TR(("Enable checking for XT codes"));
4696 check_for_codes = TRUE;
4697 need_gather = TRUE;
4698 req_codes_from_term();
4699 }
4700
4701 // libvterm sends 0;100;0
4702 if (version == 100 && arg[0] == 0 && arg[2] == 0)
4703 {
4704 // If run from Vim $COLORS is set to the number of
4705 // colors the terminal supports. Otherwise assume
4706 // 256, libvterm supports even more.
4707 if (mch_getenv((char_u *)"COLORS") == NULL)
4708 may_adjust_color_count(256);
4709 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004710 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004711 }
4712
4713 if (version == 95)
4714 {
4715 // Mac Terminal.app sends 1;95;0
4716 if (arg[0] == 1 && arg[2] == 0)
4717 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004718 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4719 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004720 }
4721 // iTerm2 sends 0;95;0
4722 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004723 {
4724 // iTerm2 can do SGR mouse reporting
4725 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4726 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004727 // old iTerm2 sends 0;95;
4728 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004729 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004730 }
4731
4732 // screen sends 83;40500;0 83 is 'S' in ASCII.
4733 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004734 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004735 // screen supports SGR mouse codes since 4.7.0
4736 if (arg[1] >= 40700)
4737 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4738 else
4739 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4740 }
4741
4742 // If no recognized terminal has set mouse behavior, assume xterm.
4743 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4744 {
4745 // Xterm version 277 supports SGR.
4746 // Xterm version >= 95 supports mouse dragging.
4747 if (version >= 277)
4748 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004749 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004750 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004751 }
4752
4753 // Detect terminals that set $TERM to something like
4754 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004755 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004756 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004757 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004758 // xfce4-terminal sends 1;2802;0.
4759 // screen sends 83;40500;0
4760 // Assuming any version number over 2500 is not an
4761 // xterm (without the limit for rxvt and screen).
4762 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004763 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004764
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004765 else if (version == 136 && arg[2] == 0)
4766 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004767 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004768
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004769 // PuTTY sends 0;136;0
4770 if (arg[0] == 0)
4771 {
4772 // supports sgr-like mouse reporting.
4773 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4774 }
4775 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004776 }
4777
4778 // Konsole sends 0;115;0
4779 else if (version == 115 && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004780 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004781
4782 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004783 // 30600/40500 is a version number of GNU screen. DA2 support is added
4784 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4785 // compatibility checking does not detect GNU screen.
4786 if (arg[0] == 83 && arg[1] >= 30600)
4787 {
4788 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4789 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4790 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004791
4792 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004793 // 95, so assume anything below 95 is not xterm and hopefully supports
4794 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004795 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004796 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004797
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004798 // Getting the cursor style is only supported properly by xterm since
4799 // version 279 (otherwise it returns 0x18).
4800 if (version < 279)
4801 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4802
4803 /*
4804 * Take action on the detected properties.
4805 */
4806
4807 // Unless the underline RGB color is expected to work, disable "t_8u".
4808 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004809 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES && *T_8U != NUL)
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004810 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4811 OPT_FREE, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004812
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004813 // Only set 'ttymouse' automatically if it was not set
4814 // by the user already.
4815 if (!option_was_set((char_u *)"ttym")
4816 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4817 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4818 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004819 set_option_value_give_err((char_u *)"ttym", 0L,
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004820 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4821 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4822 }
4823
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004824 // Only request the cursor style if t_SH and t_RS are
4825 // set. Only supported properly by xterm since version
4826 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004827 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004828 // Not for Terminal.app, it can't handle t_RS, it
4829 // echoes the characters to the screen.
4830 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004831 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004832 && *T_CSH != NUL
4833 && *T_CRS != NUL)
4834 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02004835#ifdef FEAT_JOB_CHANNEL
4836 ch_log_output = TRUE;
4837#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004838 LOG_TR(("Sending cursor style request"));
4839 out_str(T_CRS);
4840 termrequest_sent(&rcs_status);
4841 need_flush = TRUE;
4842 }
4843
4844 // Only request the cursor blink mode if t_RC set. Not
4845 // for Gnome terminal, it can't handle t_RC, it
4846 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004847 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004848 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004849 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004850 && *T_CRC != NUL)
4851 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02004852#ifdef FEAT_JOB_CHANNEL
4853 ch_log_output = TRUE;
4854#endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004855 LOG_TR(("Sending cursor blink mode request"));
4856 out_str(T_CRC);
4857 termrequest_sent(&rbm_status);
4858 need_flush = TRUE;
4859 }
4860
4861 if (need_flush)
4862 out_flush();
4863 }
4864}
4865
4866/*
4867 * Handle a sequence with key and modifier, one of:
4868 * {lead}27;{modifier};{key}~
4869 * {lead}{key};{modifier}u
4870 * Returns the difference in length.
4871 */
4872 static int
4873handle_key_with_modifier(
4874 int *arg,
4875 int trail,
4876 int csi_len,
4877 int offset,
4878 char_u *buf,
4879 int bufsize,
4880 int *buflen)
4881{
4882 int key;
4883 int modifiers;
4884 int new_slen;
4885 char_u string[MAX_KEY_CODE_LEN + 1];
4886
4887 seenModifyOtherKeys = TRUE;
4888 if (trail == 'u')
4889 key = arg[0];
4890 else
4891 key = arg[2];
4892
4893 modifiers = decode_modifiers(arg[1]);
4894
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02004895 // Some keys need adjustment when the Ctrl modifier is used.
4896 key = may_adjust_key_for_ctrl(modifiers, key);
4897
Bram Moolenaaref6746f2020-06-20 14:43:23 +02004898 // May remove the shift modifier if it's already included in the key.
4899 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004900
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004901 // insert modifiers with KS_MODIFIER
4902 new_slen = modifiers2keycode(modifiers, &key, string);
4903
Bram Moolenaar749bc952020-10-31 16:33:47 +01004904 if (IS_SPECIAL(key))
4905 {
4906 string[new_slen++] = K_SPECIAL;
4907 string[new_slen++] = KEY2TERMCAP0(key);
4908 string[new_slen++] = KEY2TERMCAP1(key);
4909 }
4910 else if (has_mbyte)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004911 new_slen += (*mb_char2bytes)(key, string + new_slen);
4912 else
4913 string[new_slen++] = key;
4914
4915 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4916 buf, bufsize, buflen) == FAIL)
4917 return -1;
4918 return new_slen - csi_len + offset;
4919}
4920
4921/*
4922 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004923 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004924 *
4925 * - Cursor position report: {lead}{row};{col}R
4926 * The final byte must be 'R'. It is used for checking the
4927 * ambiguous-width character state.
4928 *
4929 * - window position reply: {lead}3;{x};{y}t
4930 *
4931 * - key with modifiers when modifyOtherKeys is enabled:
4932 * {lead}27;{modifier};{key}~
4933 * {lead}{key};{modifier}u
4934 * Return 0 for no match, -1 for partial match, > 0 for full match.
4935 */
4936 static int
4937handle_csi(
4938 char_u *tp,
4939 int len,
4940 char_u *argp,
4941 int offset,
4942 char_u *buf,
4943 int bufsize,
4944 int *buflen,
4945 char_u *key_name,
4946 int *slen)
4947{
4948 int first = -1; // optional char right after {lead}
4949 int trail; // char that ends CSI sequence
4950 int arg[3] = {-1, -1, -1}; // argument numbers
4951 int argc; // number of arguments
4952 char_u *ap = argp;
4953 int csi_len;
4954
4955 // Check for non-digit after CSI.
4956 if (!VIM_ISDIGIT(*ap))
4957 first = *ap++;
4958
4959 // Find up to three argument numbers.
4960 for (argc = 0; argc < 3; )
4961 {
4962 if (ap >= tp + len)
4963 return -1;
4964 if (*ap == ';')
4965 arg[argc++] = -1; // omitted number
4966 else if (VIM_ISDIGIT(*ap))
4967 {
4968 arg[argc] = 0;
4969 for (;;)
4970 {
4971 if (ap >= tp + len)
4972 return -1;
4973 if (!VIM_ISDIGIT(*ap))
4974 break;
4975 arg[argc] = arg[argc] * 10 + (*ap - '0');
4976 ++ap;
4977 }
4978 ++argc;
4979 }
4980 if (*ap == ';')
4981 ++ap;
4982 else
4983 break;
4984 }
4985
4986 // mrxvt has been reported to have "+" in the version. Assume
4987 // the escape sequence ends with a letter or one of "{|}~".
4988 while (ap < tp + len
4989 && !(*ap >= '{' && *ap <= '~')
4990 && !ASCII_ISALPHA(*ap))
4991 ++ap;
4992 if (ap >= tp + len)
4993 return -1;
4994 trail = *ap;
4995 csi_len = (int)(ap - tp) + 1;
4996
4997 // Cursor position report: Eat it when there are 2 arguments
4998 // and it ends in 'R'. Also when u7_status is not "sent", it
4999 // may be from a previous Vim that just exited. But not for
5000 // <S-F3>, it sends something similar, check for row and column
5001 // to make sense.
5002 if (first == -1 && argc == 2 && trail == 'R')
5003 {
5004 handle_u7_response(arg, tp, csi_len);
5005
5006 key_name[0] = (int)KS_EXTRA;
5007 key_name[1] = (int)KE_IGNORE;
5008 *slen = csi_len;
5009 }
5010
5011 // Version string: Eat it when there is at least one digit and
5012 // it ends in 'c'
5013 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
5014 {
5015 handle_version_response(first, arg, argc, tp);
5016
5017 *slen = csi_len;
5018# ifdef FEAT_EVAL
5019 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
5020# endif
5021 apply_autocmds(EVENT_TERMRESPONSE,
5022 NULL, NULL, FALSE, curbuf);
5023 key_name[0] = (int)KS_EXTRA;
5024 key_name[1] = (int)KE_IGNORE;
5025 }
5026
5027 // Check blinking cursor from xterm:
5028 // {lead}?12;1$y set
5029 // {lead}?12;2$y not set
5030 //
5031 // {lead} can be <Esc>[ or CSI
5032 else if (rbm_status.tr_progress == STATUS_SENT
5033 && first == '?'
5034 && ap == argp + 6
5035 && arg[0] == 12
5036 && ap[-1] == '$'
5037 && trail == 'y')
5038 {
5039 initial_cursor_blink = (arg[1] == '1');
5040 rbm_status.tr_progress = STATUS_GOT;
5041 LOG_TR(("Received cursor blinking mode response: %s", tp));
5042 key_name[0] = (int)KS_EXTRA;
5043 key_name[1] = (int)KE_IGNORE;
5044 *slen = csi_len;
5045# ifdef FEAT_EVAL
5046 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
5047# endif
5048 }
5049
5050 // Check for a window position response from the terminal:
5051 // {lead}3;{x};{y}t
5052 else if (did_request_winpos && argc == 3 && arg[0] == 3
5053 && trail == 't')
5054 {
5055 winpos_x = arg[1];
5056 winpos_y = arg[2];
5057 // got finished code: consume it
5058 key_name[0] = (int)KS_EXTRA;
5059 key_name[1] = (int)KE_IGNORE;
5060 *slen = csi_len;
5061
5062 if (--did_request_winpos <= 0)
5063 winpos_status.tr_progress = STATUS_GOT;
5064 }
5065
5066 // Key with modifier:
5067 // {lead}27;{modifier};{key}~
5068 // {lead}{key};{modifier}u
5069 else if ((arg[0] == 27 && argc == 3 && trail == '~')
5070 || (argc == 2 && trail == 'u'))
5071 {
5072 return len + handle_key_with_modifier(arg, trail,
5073 csi_len, offset, buf, bufsize, buflen);
5074 }
5075
5076 // else: Unknown CSI sequence. We could drop it, but then the
5077 // user can't create a map for it.
5078 return 0;
5079}
5080
5081/*
5082 * Handle an OSC sequence, fore/background color response from the terminal:
5083 *
5084 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
5085 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
5086 *
5087 * {code} is 10 for foreground, 11 for background
5088 * {lead} can be <Esc>] or OSC
5089 * {tail} can be '\007', <Esc>\ or STERM.
5090 *
5091 * Consume any code that starts with "{lead}11;", it's also
5092 * possible that "rgba" is following.
5093 */
5094 static int
5095handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5096{
5097 int i, j;
5098
5099 j = 1 + (tp[0] == ESC);
5100 if (len >= j + 3 && (argp[0] != '1'
5101 || (argp[1] != '1' && argp[1] != '0')
5102 || argp[2] != ';'))
5103 i = 0; // no match
5104 else
5105 for (i = j; i < len; ++i)
5106 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
5107 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
5108 {
5109 int is_bg = argp[1] == '1';
5110 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
5111 && tp[j + 16] == '/';
5112
5113 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
5114 && (is_4digit
5115 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
5116 {
5117 char_u *tp_r = tp + j + 7;
5118 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
5119 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
5120# ifdef FEAT_TERMINAL
5121 int rval, gval, bval;
5122
5123 rval = hexhex2nr(tp_r);
5124 gval = hexhex2nr(tp_b);
5125 bval = hexhex2nr(tp_g);
5126# endif
5127 if (is_bg)
5128 {
5129 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5130 *tp_b) ? "light" : "dark";
5131
5132 LOG_TR(("Received RBG response: %s", tp));
5133 rbg_status.tr_progress = STATUS_GOT;
5134# ifdef FEAT_TERMINAL
5135 bg_r = rval;
5136 bg_g = gval;
5137 bg_b = bval;
5138# endif
5139 if (!option_was_set((char_u *)"bg")
5140 && STRCMP(p_bg, new_bg_val) != 0)
5141 {
5142 // value differs, apply it
Bram Moolenaar31e5c602022-04-15 13:53:33 +01005143 set_option_value_give_err((char_u *)"bg",
5144 0L, (char_u *)new_bg_val, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005145 reset_option_was_set((char_u *)"bg");
5146 redraw_asap(CLEAR);
5147 }
5148 }
5149# ifdef FEAT_TERMINAL
5150 else
5151 {
5152 LOG_TR(("Received RFG response: %s", tp));
5153 rfg_status.tr_progress = STATUS_GOT;
5154 fg_r = rval;
5155 fg_g = gval;
5156 fg_b = bval;
5157 }
5158# endif
5159 }
5160
5161 // got finished code: consume it
5162 key_name[0] = (int)KS_EXTRA;
5163 key_name[1] = (int)KE_IGNORE;
5164 *slen = i + 1 + (tp[i] == ESC);
5165# ifdef FEAT_EVAL
5166 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5167 : VV_TERMRFGRESP, tp, *slen);
5168# endif
5169 break;
5170 }
5171 if (i == len)
5172 {
5173 LOG_TR(("not enough characters for RB"));
5174 return FAIL;
5175 }
5176 return OK;
5177}
5178
5179/*
5180 * Check for key code response from xterm:
5181 * {lead}{flag}+r<hex bytes><{tail}
5182 *
5183 * {lead} can be <Esc>P or DCS
5184 * {flag} can be '0' or '1'
5185 * {tail} can be Esc>\ or STERM
5186 *
5187 * Check for cursor shape response from xterm:
5188 * {lead}1$r<digit> q{tail}
5189 *
5190 * {lead} can be <Esc>P or DCS
5191 * {tail} can be <Esc>\ or STERM
5192 *
5193 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5194 */
5195 static int
5196handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5197{
5198 int i, j;
5199
5200 j = 1 + (tp[0] == ESC);
5201 if (len < j + 3)
5202 i = len; // need more chars
5203 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
5204 i = 0; // no match
5205 else if (argp[1] == '+')
5206 // key code response
5207 for (i = j; i < len; ++i)
5208 {
5209 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5210 || tp[i] == STERM)
5211 {
5212 if (i - j >= 3)
5213 got_code_from_term(tp + j, i);
5214 key_name[0] = (int)KS_EXTRA;
5215 key_name[1] = (int)KE_IGNORE;
5216 *slen = i + 1 + (tp[i] == ESC);
5217 break;
5218 }
5219 }
5220 else
5221 {
5222 // Probably the cursor shape response. Make sure that "i"
5223 // is equal to "len" when there are not sufficient
5224 // characters.
5225 for (i = j + 3; i < len; ++i)
5226 {
5227 if (i - j == 3 && !isdigit(tp[i]))
5228 break;
5229 if (i - j == 4 && tp[i] != ' ')
5230 break;
5231 if (i - j == 5 && tp[i] != 'q')
5232 break;
5233 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5234 break;
5235 if ((i - j == 6 && tp[i] == STERM)
5236 || (i - j == 7 && tp[i] == '\\'))
5237 {
5238 int number = argp[3] - '0';
5239
5240 // 0, 1 = block blink, 2 = block
5241 // 3 = underline blink, 4 = underline
5242 // 5 = vertical bar blink, 6 = vertical bar
5243 number = number == 0 ? 1 : number;
5244 initial_cursor_shape = (number + 1) / 2;
5245 // The blink flag is actually inverted, compared to
5246 // the value set with T_SH.
5247 initial_cursor_shape_blink =
5248 (number & 1) ? FALSE : TRUE;
5249 rcs_status.tr_progress = STATUS_GOT;
5250 LOG_TR(("Received cursor shape response: %s", tp));
5251
5252 key_name[0] = (int)KS_EXTRA;
5253 key_name[1] = (int)KE_IGNORE;
5254 *slen = i + 1;
5255# ifdef FEAT_EVAL
5256 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5257# endif
5258 break;
5259 }
5260 }
5261 }
5262
5263 if (i == len)
5264 {
5265 // These codes arrive many together, each code can be
5266 // truncated at any point.
5267 LOG_TR(("not enough characters for XT"));
5268 return FAIL;
5269 }
5270 return OK;
5271}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005272#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005273
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 * Check if typebuf.tb_buf[] contains a terminal key code.
5276 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005277 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005279 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 * With a match, the match is removed, the replacement code is inserted in
5281 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5282 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005283 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5284 * "buflen" is then the length of the string in buf[] and is updated for
5285 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 */
5287 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005288check_termcode(
5289 int max_offset,
5290 char_u *buf,
5291 int bufsize,
5292 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293{
5294 char_u *tp;
5295 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005296 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005297 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005299 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 int offset;
5301 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005302 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005303 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005304 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005305 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 char_u string[MAX_KEY_CODE_LEN + 1];
5307 int i, j;
5308 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310
5311 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5312
5313 /*
5314 * Speed up the checks for terminal codes by gathering all first bytes
5315 * used in termleader[]. Often this is just a single <Esc>.
5316 */
5317 if (need_gather)
5318 gather_termleader();
5319
5320 /*
5321 * Check at several positions in typebuf.tb_buf[], to catch something like
5322 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5323 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005324 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 * This is used often, KEEP IT FAST!
5326 */
5327 for (offset = 0; offset < max_offset; ++offset)
5328 {
5329 if (buf == NULL)
5330 {
5331 if (offset >= typebuf.tb_len)
5332 break;
5333 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005334 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336 else
5337 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005338 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 break;
5340 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005341 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
5343
5344 /*
5345 * Don't check characters after K_SPECIAL, those are already
5346 * translated terminal chars (avoid translating ~@^Hx).
5347 */
5348 if (*tp == K_SPECIAL)
5349 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005350 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 continue;
5352 }
5353
5354 /*
5355 * Skip this position if the character does not appear as the first
5356 * character in term_strings. This speeds up a lot, since most
5357 * termcodes start with the same character (ESC or CSI).
5358 */
5359 i = *tp;
5360 for (p = termleader; *p && *p != i; ++p)
5361 ;
5362 if (*p == NUL)
5363 continue;
5364
5365 /*
5366 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5367 * are in Insert mode.
5368 */
5369 if (*tp == ESC && !p_ek && (State & INSERT))
5370 continue;
5371
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005372 key_name[0] = NUL; // no key name found yet
5373 key_name[1] = NUL; // no key name found yet
5374 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
5376#ifdef FEAT_GUI
5377 if (gui.in_use)
5378 {
5379 /*
5380 * GUI special key codes are all of the form [CSI xx].
5381 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005382 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 {
5384 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005385 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 slen = 3;
5387 key_name[0] = tp[1];
5388 key_name[1] = tp[2];
5389 }
5390 }
5391 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005392#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005394 int mouse_index_found = -1;
5395
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 for (idx = 0; idx < tc_len; ++idx)
5397 {
5398 /*
5399 * Ignore the entry if we are not at the start of
5400 * typebuf.tb_buf[]
5401 * and there are not enough characters to make a match.
5402 * But only when the 'K' flag is in 'cpoptions'.
5403 */
5404 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005405 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 if (cpo_koffset && offset && len < slen)
5407 continue;
5408 if (STRNCMP(termcodes[idx].code, tp,
5409 (size_t)(slen > len ? len : slen)) == 0)
5410 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005411 int looks_like_mouse_start = FALSE;
5412
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005413 if (len < slen) // got a partial sequence
5414 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
5416 /*
5417 * When found a keypad key, check if there is another key
5418 * that matches and use that one. This makes <Home> to be
5419 * found instead of <kHome> when they produce the same
5420 * key code.
5421 */
5422 if (termcodes[idx].name[0] == 'K'
5423 && VIM_ISDIGIT(termcodes[idx].name[1]))
5424 {
5425 for (j = idx + 1; j < tc_len; ++j)
5426 if (termcodes[j].len == slen &&
5427 STRNCMP(termcodes[idx].code,
5428 termcodes[j].code, slen) == 0)
5429 {
5430 idx = j;
5431 break;
5432 }
5433 }
5434
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005435 if (slen == 2 && len > 2
5436 && termcodes[idx].code[0] == ESC
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005437 && termcodes[idx].code[1] == '[')
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005438 {
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00005439 // The mouse termcode "ESC [" is also the prefix of
5440 // "ESC [ I" (focus gained) and other keys. Check some
5441 // more bytes to find out.
5442 if (!isdigit(tp[2]))
5443 {
5444 // ESC [ without number following: Only use it when
5445 // there is no other match.
5446 looks_like_mouse_start = TRUE;
5447 }
5448 else if (termcodes[idx].name[0] == KS_DEC_MOUSE)
5449 {
5450 char_u *nr = tp + 2;
5451 int count = 0;
5452
5453 // If a digit is following it could be a key with
5454 // modifier, e.g., ESC [ 1;2P. Can be confused
5455 // with DEC_MOUSE, which requires four numbers
5456 // following. If not then it can't be a DEC_MOUSE
5457 // code.
5458 for (;;)
5459 {
5460 ++count;
5461 (void)getdigits(&nr);
5462 if (nr >= tp + len)
5463 return -1; // partial sequence
5464 if (*nr != ';')
5465 break;
5466 ++nr;
5467 if (nr >= tp + len)
5468 return -1; // partial sequence
5469 }
5470 if (count < 4)
5471 continue; // no match
5472 }
5473 }
5474 if (looks_like_mouse_start)
5475 {
5476 // Only use it when there is no other match.
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005477 if (mouse_index_found < 0)
5478 mouse_index_found = idx;
5479 }
5480 else
5481 {
5482 key_name[0] = termcodes[idx].name[0];
5483 key_name[1] = termcodes[idx].name[1];
5484 break;
5485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005487
5488 /*
5489 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005490 * <Esc>[123;*X (modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005491 * <Esc>[@;*X (matches <Esc>[X and <Esc>[1;9X )
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005492 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5493 * When there is a modifier the * matches a number.
5494 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005495 */
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005496 if (termcodes[idx].modlen > 0 && mouse_index_found < 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005497 {
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005498 int at_code;
5499
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005500 modslen = termcodes[idx].modlen;
5501 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005502 continue;
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005503 at_code = termcodes[idx].code[modslen] == '@';
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005504 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005505 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005506 {
5507 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005508
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005509 if (len <= modslen) // got a partial sequence
5510 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005511
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005512 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005513 // no modifiers
5514 slen = modslen + 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005515 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +01005516 // no match for "code;*X" with "code;"
5517 continue;
5518 else if (at_code && tp[modslen] != '1')
5519 // no match for "<Esc>[@" with "<Esc>[1"
5520 continue;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005521 else
5522 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005523 // Skip over the digits, the final char must
5524 // follow. URXVT can use a negative value, thus
5525 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005526 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005527 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005528 ;
5529 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005530 if (len < j) // got a partial sequence
5531 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005532 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005533 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005534
Bram Moolenaara529ce02017-06-22 22:37:57 +02005535 modifiers_start = tp + slen - 2;
5536
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005537 // Match! Convert modifier bits.
5538 n = atoi((char *)modifiers_start);
5539 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005540
5541 slen = j;
5542 }
5543 key_name[0] = termcodes[idx].name[0];
5544 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005545 break;
5546 }
5547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
Bram Moolenaar92e5df82021-01-30 15:39:47 +01005549 if (idx == tc_len && mouse_index_found >= 0)
5550 {
5551 key_name[0] = termcodes[mouse_index_found].name[0];
5552 key_name[1] = termcodes[mouse_index_found].name[1];
5553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 }
5555
5556#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005557 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005558 // Mouse codes of DEC and pterm start with <ESC>[. When
5559 // detecting the start of these mouse codes they might as well be
5560 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005561# ifdef FEAT_MOUSE_DEC
5562 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005563# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005564# ifdef FEAT_MOUSE_PTERM
5565 || key_name[0] == KS_PTERM_MOUSE
5566# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005567 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005569 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5570
5571 /*
5572 * Check for responses from the terminal starting with {lead}:
5573 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005574 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005575 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005576 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005577 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005578 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005579 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005580 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005581 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005582 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005583 * - window position reply: {lead}3;{x};{y}t
5584 *
5585 * - key with modifiers when modifyOtherKeys is enabled:
5586 * {lead}27;{modifier};{key}~
5587 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005588 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005589 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005590 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005591 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005593 int resp = handle_csi(tp, len, argp, offset, buf,
5594 bufsize, buflen, key_name, &slen);
5595 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005596 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005597# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005598 if (resp == -1)
5599 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005600# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005601 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005602 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005603 }
5604
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005605 // Check for fore/background color response from the terminal,
5606 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005607 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005608 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5609 || tp[0] == OSC))
5610 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005611 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005612 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005615 // Check for key code response from xterm,
5616 // starting with <Esc>P or DCS
Bram Moolenaarafd78262019-05-10 23:10:31 +02005617 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005618 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 || tp[0] == DCS))
5620 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005621 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005622 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 }
5624 }
5625#endif
5626
5627 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005628 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005630 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005632#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 /*
5634 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5635 * so that we know which window to scroll later.
5636 */
5637 if (gui.in_use
5638 && key_name[0] == (int)KS_EXTRA
5639 && (key_name[1] == (int)KE_X1MOUSE
5640 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005641 || key_name[1] == (int)KE_MOUSEMOVE_XY
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005642 || key_name[1] == (int)KE_MOUSELEFT
5643 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 || key_name[1] == (int)KE_MOUSEDOWN
5645 || key_name[1] == (int)KE_MOUSEUP))
5646 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005647 char_u bytes[6];
5648 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5649
5650 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 return -1;
5652 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5653 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5654 slen += num_bytes;
Bram Moolenaar445f11d2021-06-08 20:13:31 +02005655 // equal to K_MOUSEMOVE
5656 if (key_name[1] == (int)KE_MOUSEMOVE_XY)
5657 key_name[1] = (int)KE_MOUSEMOVE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 }
5659 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 /*
5662 * If it is a mouse click, get the coordinates.
5663 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005664 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005665#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005666 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005667#endif
5668#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005669 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005670#endif
5671#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005672 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005673#endif
5674#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005675 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005676#endif
5677#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005678 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005679#endif
5680#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005681 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005682#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005683 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005684 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005686 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5687 &modifiers) == -1)
5688 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
5691#ifdef FEAT_GUI
5692 /*
5693 * If using the GUI, then we get menu and scrollbar events.
5694 *
5695 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5696 * four bytes which are to be taken as a pointer to the vimmenu_T
5697 * structure.
5698 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005699 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005700 * is one byte with the tab index.
5701 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5703 * by one byte representing the scrollbar number, and then four bytes
5704 * representing a long_u which is the new value of the scrollbar.
5705 *
5706 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5707 * KE_FILLER followed by four bytes representing a long_u which is the
5708 * new value of the scrollbar.
5709 */
5710# ifdef FEAT_MENU
5711 else if (key_name[0] == (int)KS_MENU)
5712 {
5713 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005714 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 if (num_bytes == -1)
5717 return -1;
5718 current_menu = (vimmenu_T *)val;
5719 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005720
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005721 // The menu may have been deleted right after it was used, check
5722 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005723 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5724 {
5725 key_name[0] = KS_EXTRA;
5726 key_name[1] = (int)KE_IGNORE;
5727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
5729# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005730# ifdef FEAT_GUI_TABLINE
5731 else if (key_name[0] == (int)KS_TABLINE)
5732 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005733 // Selecting tabline tab or using its menu.
5734 char_u bytes[6];
5735 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5736
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005737 if (num_bytes == -1)
5738 return -1;
5739 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005740 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005741 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005742 slen += num_bytes;
5743 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005744 else if (key_name[0] == (int)KS_TABMENU)
5745 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005746 // Selecting tabline tab or using its menu.
5747 char_u bytes[6];
5748 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5749
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005750 if (num_bytes == -1)
5751 return -1;
5752 current_tab = (int)bytes[0];
5753 current_tabmenu = (int)bytes[1];
5754 slen += num_bytes;
5755 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005756# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757# ifndef USE_ON_FLY_SCROLL
5758 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5759 {
5760 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005761 char_u bytes[6];
5762 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005764 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 j = 0;
5766 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5767 && tp[j + 2] != NUL; ++i)
5768 {
5769 j += 3;
5770 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5771 if (num_bytes == -1)
5772 break;
5773 if (i == 0)
5774 current_scrollbar = (int)bytes[0];
5775 else if (current_scrollbar != (int)bytes[0])
5776 break;
5777 j += num_bytes;
5778 num_bytes = get_long_from_buf(tp + j, &val);
5779 if (num_bytes == -1)
5780 break;
5781 scrollbar_value = val;
5782 j += num_bytes;
5783 slen = j;
5784 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005785 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 return -1;
5787 }
5788 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5789 {
5790 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005791 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005793 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 j = 0;
5795 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5796 && tp[j + 2] != NUL; ++i)
5797 {
5798 j += 3;
5799 num_bytes = get_long_from_buf(tp + j, &val);
5800 if (num_bytes == -1)
5801 break;
5802 scrollbar_value = val;
5803 j += num_bytes;
5804 slen = j;
5805 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005806 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 return -1;
5808 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005809# endif // !USE_ON_FLY_SCROLL
5810#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005812#if (defined(UNIX) || defined(VMS))
5813 /*
5814 * Handle FocusIn/FocusOut event sequences reported by XTerm.
5815 * (CSI I/CSI O)
5816 */
Bram Moolenaar8d5daf22022-03-02 17:16:39 +00005817 if (key_name[0] == KS_EXTRA
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005818# ifdef FEAT_GUI
5819 && !gui.in_use
5820# endif
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005821 )
5822 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005823 if (key_name[1] == KE_FOCUSGAINED)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005824 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005825 if (!focus_state)
5826 {
5827 ui_focus_change(TRUE);
5828 did_cursorhold = TRUE;
5829 focus_state = TRUE;
5830 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005831 key_name[1] = (int)KE_IGNORE;
5832 }
Bram Moolenaard44cc592021-01-14 21:57:58 +01005833 else if (key_name[1] == KE_FOCUSLOST)
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005834 {
Bram Moolenaard44cc592021-01-14 21:57:58 +01005835 if (focus_state)
5836 {
5837 ui_focus_change(FALSE);
5838 did_cursorhold = TRUE;
5839 focus_state = FALSE;
5840 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005841 key_name[1] = (int)KE_IGNORE;
5842 }
Bram Moolenaar681fc3f2021-01-14 17:35:21 +01005843 }
5844#endif
5845
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005846 /*
5847 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5848 */
5849 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5850
5851 /*
5852 * Add any modifier codes to our string.
5853 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005854 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005855
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005856 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005857 key_name[0] = KEY2TERMCAP0(key);
5858 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005860 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005861 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005862 if (has_mbyte)
5863 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5864 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005865 string[new_slen++] = key_name[1];
5866 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005867 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5868 && key_name[1] == KE_IGNORE)
5869 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005870 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5871 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005872 retval = KEYLEN_REMOVED;
5873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 else
5875 {
5876 string[new_slen++] = K_SPECIAL;
5877 string[new_slen++] = key_name[0];
5878 string[new_slen++] = key_name[1];
5879 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005880 if (put_string_in_typebuf(offset, slen, string, new_slen,
5881 buf, bufsize, buflen) == FAIL)
5882 return -1;
5883 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 }
5885
Bram Moolenaar2951b772013-07-03 12:45:31 +02005886#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005887 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005888#endif
5889
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005890 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891}
5892
Bram Moolenaar9377df32017-10-15 13:22:01 +02005893#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005894/*
5895 * Get the text foreground color, if known.
5896 */
5897 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005898term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005899{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005900 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005901 {
5902 *r = fg_r;
5903 *g = fg_g;
5904 *b = fg_b;
5905 }
5906}
5907
5908/*
5909 * Get the text background color, if known.
5910 */
5911 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005912term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005913{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005914 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005915 {
5916 *r = bg_r;
5917 *g = bg_g;
5918 *b = bg_b;
5919 }
5920}
5921#endif
5922
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923/*
5924 * Replace any terminal code strings in from[] with the equivalent internal
5925 * vim representation. This is used for the "from" and "to" part of a
5926 * mapping, and the "to" part of a menu command.
5927 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5928 * '<'.
5929 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5930 *
5931 * The replacement is done in result[] and finally copied into allocated
5932 * memory. If this all works well *bufp is set to the allocated memory and a
5933 * pointer to it is returned. If something fails *bufp is set to NULL and from
5934 * is returned.
5935 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005936 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5937 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5938 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5939 * used instead of a CTRL-V.
5940 *
5941 * Flags:
5942 * REPTERM_FROM_PART see above
5943 * REPTERM_DO_LT also translate <lt>
5944 * REPTERM_SPECIAL always accept <key> notation
5945 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5946 *
5947 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5948 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005950 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005951replace_termcodes(
5952 char_u *from,
5953 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005954 int flags,
5955 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956{
5957 int i;
5958 int slen;
5959 int key;
5960 int dlen = 0;
5961 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005962 int do_backslash; // backslash is a special character
5963 int do_special; // recognize <> key codes
5964 int do_key_code; // recognize raw key codes
5965 char_u *result; // buffer for resulting string
Bram Moolenaar648dd882022-04-14 21:36:15 +01005966 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967
5968 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02005969 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
5970 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
Bram Moolenaar648dd882022-04-14 21:36:15 +01005972 src = from;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973
5974 /*
5975 * Allocate space for the translation. Worst case a single character is
5976 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
Bram Moolenaar648dd882022-04-14 21:36:15 +01005977 * In the rare case more might be needed ga_grow() must be called again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 */
Bram Moolenaar648dd882022-04-14 21:36:15 +01005979 ga_init2(&ga, 1L, 100);
5980 if (ga_grow(&ga, STRLEN(src) * 6 + 1) == FAIL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 {
5982 *bufp = NULL;
5983 return from;
5984 }
Bram Moolenaar648dd882022-04-14 21:36:15 +01005985 result = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
5987 /*
5988 * Check for #n at start only: function key n
5989 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005990 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 {
5992 result[dlen++] = K_SPECIAL;
5993 result[dlen++] = 'k';
5994 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005995 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005997 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 src += 2;
5999 }
6000
6001 /*
6002 * Copy each byte from *from to result[dlen]
6003 */
6004 while (*src != NUL)
6005 {
6006 /*
6007 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02006008 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02006010 if (do_special && ((flags & REPTERM_DO_LT)
6011 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 {
6013#ifdef FEAT_EVAL
6014 /*
Bram Moolenaar89445512022-04-14 12:58:23 +01006015 * Change <SID>Func to K_SNR <script-nr> _Func. This name is used
6016 * for script-locla user functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
Bram Moolenaar89445512022-04-14 12:58:23 +01006018 * Also change <SID>name.Func to K_SNR <import-script-nr> _Func.
6019 * Only if "name" is recognized as an import.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 */
6021 if (STRNICMP(src, "<SID>", 5) == 0)
6022 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006023 if (current_sctx.sc_sid <= 0)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00006024 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 else
6026 {
Bram Moolenaar89445512022-04-14 12:58:23 +01006027 char_u *dot;
6028 long sid = current_sctx.sc_sid;
6029
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 src += 5;
Bram Moolenaar89445512022-04-14 12:58:23 +01006031 if (in_vim9script()
6032 && (dot = vim_strchr(src, '.')) != NULL)
6033 {
6034 imported_T *imp = find_imported(src, dot - src, FALSE);
6035
6036 if (imp != NULL)
6037 {
Bram Moolenaar648dd882022-04-14 21:36:15 +01006038 scriptitem_T *si = SCRIPT_ITEM(imp->imp_sid);
6039 size_t len;
6040
Bram Moolenaar89445512022-04-14 12:58:23 +01006041 src = dot + 1;
Bram Moolenaar648dd882022-04-14 21:36:15 +01006042 if (si->sn_autoload_prefix != NULL)
6043 {
6044 // Turn "<SID>name.Func"
6045 // into "scriptname#Func".
6046 len = STRLEN(si->sn_autoload_prefix);
6047 if (ga_grow(&ga, STRLEN(src) * 6 + len + 1)
6048 == FAIL)
6049 {
6050 ga_clear(&ga);
6051 *bufp = NULL;
6052 return from;
6053 }
6054 result = ga.ga_data;
6055 STRCPY(result + dlen, si->sn_autoload_prefix);
6056 dlen += len;
6057 continue;
6058 }
6059 sid = imp->imp_sid;
Bram Moolenaar89445512022-04-14 12:58:23 +01006060 }
6061 }
6062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 result[dlen++] = K_SPECIAL;
6064 result[dlen++] = (int)KS_EXTRA;
6065 result[dlen++] = (int)KE_SNR;
Bram Moolenaar89445512022-04-14 12:58:23 +01006066 sprintf((char *)result + dlen, "%ld", sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 dlen += (int)STRLEN(result + dlen);
6068 result[dlen++] = '_';
6069 continue;
6070 }
6071 }
6072#endif
Bram Moolenaarebe9d342020-05-30 21:52:54 +02006073 slen = trans_special(&src, result + dlen, FSK_KEYCODE
6074 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
6075 did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 if (slen)
6077 {
6078 dlen += slen;
6079 continue;
6080 }
6081 }
6082
6083 /*
6084 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
6085 * Note that this is also checked after replacing the <> form.
6086 * Single character codes are NOT replaced (e.g. ^H or DEL), because
6087 * it could be a character in the file.
6088 */
6089 if (do_key_code)
6090 {
6091 i = find_term_bykeys(src);
6092 if (i >= 0)
6093 {
6094 result[dlen++] = K_SPECIAL;
6095 result[dlen++] = termcodes[i].name[0];
6096 result[dlen++] = termcodes[i].name[1];
6097 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006098 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 continue;
6100 }
6101 }
6102
6103#ifdef FEAT_EVAL
6104 if (do_special)
6105 {
6106 char_u *p, *s, len;
6107
6108 /*
6109 * Replace <Leader> by the value of "mapleader".
6110 * Replace <LocalLeader> by the value of "maplocalleader".
6111 * If "mapleader" or "maplocalleader" isn't set use a backslash.
6112 */
6113 if (STRNICMP(src, "<Leader>", 8) == 0)
6114 {
6115 len = 8;
6116 p = get_var_value((char_u *)"g:mapleader");
6117 }
6118 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
6119 {
6120 len = 13;
6121 p = get_var_value((char_u *)"g:maplocalleader");
6122 }
6123 else
6124 {
6125 len = 0;
6126 p = NULL;
6127 }
6128 if (len != 0)
6129 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006130 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
6132 s = (char_u *)"\\";
6133 else
6134 s = p;
6135 while (*s != NUL)
6136 result[dlen++] = *s++;
6137 src += len;
6138 continue;
6139 }
6140 }
6141#endif
6142
6143 /*
6144 * Remove CTRL-V and ignore the next character.
6145 * For "from" side the CTRL-V at the end is included, for the "to"
6146 * part it is removed.
6147 * If 'cpoptions' does not contain 'B', also accept a backslash.
6148 */
6149 key = *src;
6150 if (key == Ctrl_V || (do_backslash && key == '\\'))
6151 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006152 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 if (*src == NUL)
6154 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02006155 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 result[dlen++] = key;
6157 break;
6158 }
6159 }
6160
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006161 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006162 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 {
6164 /*
6165 * If the character is K_SPECIAL, replace it with K_SPECIAL
6166 * KS_SPECIAL KE_FILLER.
6167 * If compiled with the GUI replace CSI with K_CSI.
6168 */
6169 if (*src == K_SPECIAL)
6170 {
6171 result[dlen++] = K_SPECIAL;
6172 result[dlen++] = KS_SPECIAL;
6173 result[dlen++] = KE_FILLER;
6174 }
6175# ifdef FEAT_GUI
6176 else if (*src == CSI)
6177 {
6178 result[dlen++] = K_SPECIAL;
6179 result[dlen++] = KS_EXTRA;
6180 result[dlen++] = (int)KE_CSI;
6181 }
6182# endif
6183 else
6184 result[dlen++] = *src;
6185 ++src;
6186 }
6187 }
6188 result[dlen] = NUL;
6189
6190 /*
6191 * Copy the new string to allocated memory.
6192 * If this fails, just return from.
6193 */
6194 if ((*bufp = vim_strsave(result)) != NULL)
6195 from = *bufp;
6196 vim_free(result);
6197 return from;
6198}
6199
6200/*
6201 * Find a termcode with keys 'src' (must be NUL terminated).
6202 * Return the index in termcodes[], or -1 if not found.
6203 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02006204 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006205find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206{
6207 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006208 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209
6210 for (i = 0; i < tc_len; ++i)
6211 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01006212 if (slen == termcodes[i].len
6213 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 return i;
6215 }
6216 return -1;
6217}
6218
6219/*
6220 * Gather the first characters in the terminal key codes into a string.
6221 * Used to speed up check_termcode().
6222 */
6223 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006224gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225{
6226 int i;
6227 int len = 0;
6228
6229#ifdef FEAT_GUI
6230 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006231 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232#endif
6233#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02006234 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006235 termleader[len++] = DCS; // the termcode response starts with DCS
6236 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237#endif
6238 termleader[len] = NUL;
6239
6240 for (i = 0; i < tc_len; ++i)
6241 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
6242 {
6243 termleader[len++] = termcodes[i].code[0];
6244 termleader[len] = NUL;
6245 }
6246
6247 need_gather = FALSE;
6248}
6249
6250/*
6251 * Show all termcodes (for ":set termcap")
6252 * This code looks a lot like showoptions(), but is different.
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006253 * "flags" can have OPT_ONECOLUMN.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 */
6255 void
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006256show_termcodes(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257{
6258 int col;
6259 int *items;
6260 int item_count;
6261 int run;
6262 int row, rows;
6263 int cols;
6264 int i;
6265 int len;
6266
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006267#define INC3 27 // try to make three columns
6268#define INC2 40 // try to make two columns
6269#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006271 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006273 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 if (items == NULL)
6275 return;
6276
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006277 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006278 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279
6280 /*
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006281 * Do the loop three times:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006283 * 2. display the medium items (medium length strings)
6284 * 3. display the long items (remaining strings)
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006285 * When "flags" has OPT_ONECOLUMN do everything in 3.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 */
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006287 for (run = (flags & OPT_ONECOLUMN) ? 3 : 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 {
6289 /*
6290 * collect the items in items[]
6291 */
6292 item_count = 0;
6293 for (i = 0; i < tc_len; i++)
6294 {
6295 len = show_one_termcode(termcodes[i].name,
6296 termcodes[i].code, FALSE);
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006297 if ((flags & OPT_ONECOLUMN) ||
6298 (len <= INC3 - GAP ? run == 1
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006299 : len <= INC2 - GAP ? run == 2
Bram Moolenaar15a24f02021-12-03 20:43:24 +00006300 : run == 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 items[item_count++] = i;
6302 }
6303
6304 /*
6305 * display the items
6306 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006307 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006309 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 if (cols == 0)
6311 cols = 1;
6312 rows = (item_count + cols - 1) / cols;
6313 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006314 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 rows = item_count;
6316 for (row = 0; row < rows && !got_int; ++row)
6317 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006318 msg_putchar('\n'); // go to next line
6319 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 break;
6321 col = 0;
6322 for (i = row; i < item_count; i += rows)
6323 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006324 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 show_one_termcode(termcodes[items[i]].name,
6326 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006327 if (run == 2)
6328 col += INC2;
6329 else
6330 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 }
6332 out_flush();
6333 ui_breakcheck();
6334 }
6335 }
6336 vim_free(items);
6337}
6338
6339/*
6340 * Show one termcode entry.
6341 * Output goes into IObuff[]
6342 */
6343 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006344show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
6346 char_u *p;
6347 int len;
6348
6349 if (name[0] > '~')
6350 {
6351 IObuff[0] = ' ';
6352 IObuff[1] = ' ';
6353 IObuff[2] = ' ';
6354 IObuff[3] = ' ';
6355 }
6356 else
6357 {
6358 IObuff[0] = 't';
6359 IObuff[1] = '_';
6360 IObuff[2] = name[0];
6361 IObuff[3] = name[1];
6362 }
6363 IObuff[4] = ' ';
6364
6365 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6366 if (p[1] != 't')
6367 STRCPY(IObuff + 5, p);
6368 else
6369 IObuff[5] = NUL;
6370 len = (int)STRLEN(IObuff);
6371 do
6372 IObuff[len++] = ' ';
6373 while (len < 17);
6374 IObuff[len] = NUL;
6375 if (code == NULL)
6376 len += 4;
6377 else
6378 len += vim_strsize(code);
6379
6380 if (printit)
6381 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006382 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006384 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 else
6386 msg_outtrans(code);
6387 }
6388 return len;
6389}
6390
6391#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6392/*
6393 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6394 * termcap codes from the terminal itself.
6395 * We get them one by one to avoid a very long response string.
6396 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006397static int xt_index_in = 0;
6398static int xt_index_out = 0;
6399
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006401req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
6403 xt_index_out = 0;
6404 xt_index_in = 0;
6405 req_more_codes_from_term();
6406}
6407
6408 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006409req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410{
=?UTF-8?q?Dundar=20G=C3=B6c?=f01a6532022-03-09 13:00:54 +00006411 char buf[23]; // extra size to shut up LGTM
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 int old_idx = xt_index_out;
6413
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006414 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 if (exiting)
6416 return;
6417
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006418 // Send up to 10 more requests out than we received. Avoid sending too
6419 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6421 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006422 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006423
Bram Moolenaar86394aa2020-09-05 14:27:24 +02006424#ifdef FEAT_JOB_CHANNEL
6425 ch_log_output = TRUE;
6426#endif
Bram Moolenaarb255b902018-04-24 21:40:10 +02006427 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6428 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 out_str_nf((char_u *)buf);
6430 ++xt_index_out;
6431 }
6432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006433 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 if (xt_index_out != old_idx)
6435 out_flush();
6436}
6437
6438/*
6439 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6440 * A "0" instead of the "1" indicates a code that isn't supported.
6441 * Both <name> and <string> are encoded in hex.
6442 * "code" points to the "0" or "1".
6443 */
6444 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006445got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
6447#define XT_LEN 100
6448 char_u name[3];
6449 char_u str[XT_LEN];
6450 int i;
6451 int j = 0;
6452 int c;
6453
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006454 // A '1' means the code is supported, a '0' means it isn't.
6455 // When half the length is > XT_LEN we can't use it.
6456 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6458 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006459 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 name[0] = hexhex2nr(code + 3);
6461 name[1] = hexhex2nr(code + 5);
6462 name[2] = NUL;
6463 for (i = 0; key_names[i] != NULL; ++i)
6464 {
6465 if (STRCMP(key_names[i], name) == 0)
6466 {
6467 xt_index_in = i;
6468 break;
6469 }
6470 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006471
Bram Moolenaarb255b902018-04-24 21:40:10 +02006472 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6473
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 if (key_names[i] != NULL)
6475 {
6476 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6477 str[j++] = c;
6478 str[j] = NUL;
6479 if (name[0] == 'C' && name[1] == 'o')
6480 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006481 // Color count is not a key code.
Bram Moolenaar6f79e612021-12-21 09:12:23 +00006482 may_adjust_color_count(atoi((char *)str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 }
6484 else
6485 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006486 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 i = find_term_bykeys(str);
6488 if (i >= 0)
6489 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006490 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 }
6492 }
6493 }
6494
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006495 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 ++xt_index_in;
6497 req_more_codes_from_term();
6498}
6499
6500/*
6501 * Check if there are any unanswered requests and deal with them.
6502 * This is called before starting an external program or getting direct
6503 * keyboard input. We don't want responses to be send to that program or
6504 * handled as typed text.
6505 */
6506 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006507check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
6509 int c;
6510
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006511 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6513 return;
6514
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006515 // Vgetc() will check for and handle any response.
6516 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 ++no_mapping;
6518 ++allow_keys;
6519 for (;;)
6520 {
6521 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006522 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 break;
6524
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006525 // If a response is recognized it's replaced with K_IGNORE, must read
6526 // it from the input stream. If there is no K_IGNORE we can't do
6527 // anything, break here (there might be some responses further on, but
6528 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 if (c != K_SPECIAL && c != K_IGNORE)
6530 break;
6531 c = vgetc();
6532 if (c != K_IGNORE)
6533 {
6534 vungetc(c);
6535 break;
6536 }
6537 }
6538 --no_mapping;
6539 --allow_keys;
6540}
6541#endif
6542
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006543#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544static char ksme_str[20];
6545static char ksmr_str[20];
6546static char ksmd_str[20];
6547
6548/*
6549 * For Win32 console: update termcap codes for existing console attributes.
6550 */
6551 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006552update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553{
6554 struct builtin_term *p;
6555
6556 p = find_builtin_term(DEFAULT_TERM);
Bram Moolenaar424bcae2022-01-31 14:59:41 +00006557 sprintf(ksme_str, "\033|%dm", attr);
6558 sprintf(ksmd_str, "\033|%dm", attr | 0x08); // FOREGROUND_INTENSITY
6559 sprintf(ksmr_str, "\033|%dm", ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
6561 while (p->bt_string != NULL)
6562 {
6563 if (p->bt_entry == (int)KS_ME)
6564 p->bt_string = &ksme_str[0];
6565 else if (p->bt_entry == (int)KS_MR)
6566 p->bt_string = &ksmr_str[0];
6567 else if (p->bt_entry == (int)KS_MD)
6568 p->bt_string = &ksmd_str[0];
6569 ++p;
6570 }
6571}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006572
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006573# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006574# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006575struct ks_tbl_s
6576{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006577 int code; // value of KS_
6578 char *vtp; // code in vtp mode
6579 char *vtp2; // code in vtp2 mode
6580 char buf[KSSIZE]; // save buffer in non-vtp mode
6581 char vbuf[KSSIZE]; // save buffer in vtp mode
6582 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6583 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006584};
6585
6586static struct ks_tbl_s ks_tbl[] =
6587{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006588 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6589 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6590 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6591 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6592 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6593 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6594 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6595 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6596 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006597# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006598 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6599 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006600 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6601 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006602# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006603 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6604 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006605 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6606 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006607# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006608 {(int)KS_CCO, "256", "256"}, // colors
6609 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006610};
6611
6612 static struct builtin_term *
6613find_first_tcap(
6614 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006615 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006616{
6617 struct builtin_term *p;
6618
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006619 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006620 if (p->bt_entry == code)
6621 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006622 return NULL;
6623}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006624# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006625
6626/*
6627 * For Win32 console: replace the sequence immediately after termguicolors.
6628 */
6629 void
6630swap_tcap(void)
6631{
6632# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006633 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006634 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006635 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006636 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006637 int mode;
6638 enum
6639 {
6640 CMODEINDEX,
6641 CMODE24,
6642 CMODE256
6643 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006644
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006645 // buffer initialization
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006646 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006647 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006648 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006649 {
6650 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006651 if (bt != NULL)
6652 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006653 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6654 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6655 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6656
6657 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6658 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006659 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006660 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006661 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006662 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006663 }
6664
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006665 if (p_tgc)
6666 mode = CMODE24;
6667 else if (t_colors >= 256)
6668 mode = CMODE256;
6669 else
6670 mode = CMODEINDEX;
6671
6672 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006673 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006674 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6675 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006676 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006677 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006678 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006679 case CMODEINDEX:
6680 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6681 break;
6682 case CMODE24:
6683 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6684 break;
6685 default:
6686 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006687 }
6688 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006689 }
6690
6691 if (mode != curr_mode)
6692 {
6693 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006694 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006695 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6696 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006697 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006698 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006699 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006700 case CMODEINDEX:
6701 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6702 break;
6703 case CMODE24:
6704 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6705 break;
6706 default:
6707 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006708 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006709 }
6710 }
6711
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006712 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006713 }
6714# endif
6715}
6716
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006718
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006719
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006720#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006721 || defined(PROTO)
6722static int cube_value[] = {
6723 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6724};
6725
6726static int grey_ramp[] = {
6727 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6728 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6729};
6730
Bram Moolenaar9894e392018-05-05 14:29:06 +02006731static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006732// R G B idx
6733 { 0, 0, 0, 1}, // black
6734 {224, 0, 0, 2}, // dark red
6735 { 0, 224, 0, 3}, // dark green
6736 {224, 224, 0, 4}, // dark yellow / brown
6737 { 0, 0, 224, 5}, // dark blue
6738 {224, 0, 224, 6}, // dark magenta
6739 { 0, 224, 224, 7}, // dark cyan
6740 {224, 224, 224, 8}, // light grey
6741
6742 {128, 128, 128, 9}, // dark grey
6743 {255, 64, 64, 10}, // light red
6744 { 64, 255, 64, 11}, // light green
6745 {255, 255, 64, 12}, // yellow
6746 { 64, 64, 255, 13}, // light blue
6747 {255, 64, 255, 14}, // light magenta
6748 { 64, 255, 255, 15}, // light cyan
6749 {255, 255, 255, 16}, // white
6750};
6751
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006752#define ANSI_INDEX_NONE 0
6753
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006754 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006755cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006756{
6757 int idx;
6758
6759 if (nr < 16)
6760 {
6761 *r = ansi_table[nr][0];
6762 *g = ansi_table[nr][1];
6763 *b = ansi_table[nr][2];
6764 *ansi_idx = ansi_table[nr][3];
6765 }
6766 else if (nr < 232)
6767 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006768 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006769 idx = nr - 16;
6770 *r = cube_value[idx / 36 % 6];
6771 *g = cube_value[idx / 6 % 6];
6772 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006773 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006774 }
6775 else if (nr < 256)
6776 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006777 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006778 idx = nr - 232;
6779 *r = grey_ramp[idx];
6780 *g = grey_ramp[idx];
6781 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006782 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006783 }
6784 else
6785 {
6786 *r = 0;
6787 *g = 0;
6788 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006789 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006790 }
6791}
6792#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006793
Bram Moolenaarf4140482020-02-15 23:06:45 +01006794/*
6795 * Replace K_BS by <BS> and K_DEL by <DEL>
6796 */
6797 void
6798term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len)
6799{
6800 int i;
6801 int c;
6802
6803 for (i = ta_len; i < ta_len + len; ++i)
6804 {
6805 if (ta_buf[i] == CSI && len - i > 2)
6806 {
6807 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6808 if (c == K_DEL || c == K_KDEL || c == K_BS)
6809 {
6810 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
6811 (size_t)(len - i - 2));
6812 if (c == K_DEL || c == K_KDEL)
6813 ta_buf[i] = DEL;
6814 else
6815 ta_buf[i] = Ctrl_H;
6816 len -= 2;
6817 }
6818 }
6819 else if (ta_buf[i] == '\r')
6820 ta_buf[i] = '\n';
6821 if (has_mbyte)
6822 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6823 }
6824}