blob: 3c0ca779b490435a4b98a450ad5e4552bd2a0eaa [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 */
40# ifdef VMS
41# define TPUTSFUNCAST
42# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
46# define TPUTSFUNCAST (int (*)())
47# 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 Moolenaarb255b902018-04-24 21:40:10 +0200103static void log_tr(const char *fmt, ...);
104# 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 Moolenaar37b9b812017-08-19 23:23:43 +0200199#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100200// When the cursor shape was detected these values are used:
201// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200202static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200203
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100204// The blink flag from the style response may be inverted from the actual
205// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200206static int initial_cursor_shape_blink = FALSE;
207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100208// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200209static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200210#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200211
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000212static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213{
214
215#if defined(FEAT_GUI)
216/*
217 * GUI pseudo term-cap.
218 */
219 {(int)KS_NAME, "gui"},
220 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
221 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
222# ifdef TERMINFO
223 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
224# else
225 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
226# endif
227 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
228# ifdef TERMINFO
229 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
230 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# else
233 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
234 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# endif
237 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100238 // attributes switched on with 'h', off with * 'H'
239 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, // HL_ALL
240 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, // HL_INVERSE
241 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, // HL_BOLD
242 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, // HL_STANDOUT
243 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, // HL_STANDOUT
244 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, // HL_UNDERLINE
245 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, // HL_UNDERLINE
246 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, // HL_UNDERCURL
247 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, // HL_UNDERCURL
248 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, // HL_STRIKETHROUGH
249 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, // HL_STRIKETHROUGH
250 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, // HL_ITALIC
251 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, // HL_ITALIC
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
253 {(int)KS_MS, "y"},
254 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100255 {(int)KS_XN, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100256 {(int)KS_LE, "\b"}, // cursor-left = BS
257 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258# ifdef TERMINFO
259 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
260# else
261 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
262# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100263 // there are no key sequences here, the GUI sequences are recognized
264 // in check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#endif
266
267#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268
269# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
270/*
271 * Amiga console window, default for Amiga
272 */
273 {(int)KS_NAME, "amiga"},
274 {(int)KS_CE, "\033[K"},
275 {(int)KS_CD, "\033[J"},
276 {(int)KS_AL, "\033[L"},
277# ifdef TERMINFO
278 {(int)KS_CAL, "\033[%p1%dL"},
279# else
280 {(int)KS_CAL, "\033[%dL"},
281# endif
282 {(int)KS_DL, "\033[M"},
283# ifdef TERMINFO
284 {(int)KS_CDL, "\033[%p1%dM"},
285# else
286 {(int)KS_CDL, "\033[%dM"},
287# endif
288 {(int)KS_CL, "\014"},
289 {(int)KS_VI, "\033[0 p"},
290 {(int)KS_VE, "\033[1 p"},
291 {(int)KS_ME, "\033[0m"},
292 {(int)KS_MR, "\033[7m"},
293 {(int)KS_MD, "\033[1m"},
294 {(int)KS_SE, "\033[0m"},
295 {(int)KS_SO, "\033[33m"},
296 {(int)KS_US, "\033[4m"},
297 {(int)KS_UE, "\033[0m"},
298 {(int)KS_CZH, "\033[3m"},
299 {(int)KS_CZR, "\033[0m"},
300#if defined(__MORPHOS__) || defined(__AROS__)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100301 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100303 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
304 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100306 {(int)KS_CAB, "\033[4%dm"}, // set background color
307 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100309 {(int)KS_OP, "\033[m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#endif
311 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100312 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 {(int)KS_LE, "\b"},
314# ifdef TERMINFO
315 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
316# else
317 {(int)KS_CM, "\033[%i%d;%dH"},
318# endif
319#if defined(__MORPHOS__)
320 {(int)KS_SR, "\033M"},
321#endif
322# ifdef TERMINFO
323 {(int)KS_CRI, "\033[%p1%dC"},
324# else
325 {(int)KS_CRI, "\033[%dC"},
326# endif
327 {K_UP, "\233A"},
328 {K_DOWN, "\233B"},
329 {K_LEFT, "\233D"},
330 {K_RIGHT, "\233C"},
331 {K_S_UP, "\233T"},
332 {K_S_DOWN, "\233S"},
333 {K_S_LEFT, "\233 A"},
334 {K_S_RIGHT, "\233 @"},
335 {K_S_TAB, "\233Z"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100336 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 {K_F2, "\233\061~"},
338 {K_F3, "\233\062~"},
339 {K_F4, "\233\063~"},
340 {K_F5, "\233\064~"},
341 {K_F6, "\233\065~"},
342 {K_F7, "\233\066~"},
343 {K_F8, "\233\067~"},
344 {K_F9, "\233\070~"},
345 {K_F10, "\233\071~"},
346 {K_S_F1, "\233\061\060~"},
347 {K_S_F2, "\233\061\061~"},
348 {K_S_F3, "\233\061\062~"},
349 {K_S_F4, "\233\061\063~"},
350 {K_S_F5, "\233\061\064~"},
351 {K_S_F6, "\233\061\065~"},
352 {K_S_F7, "\233\061\066~"},
353 {K_S_F8, "\233\061\067~"},
354 {K_S_F9, "\233\061\070~"},
355 {K_S_F10, "\233\061\071~"},
356 {K_HELP, "\233?~"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100357 {K_INS, "\233\064\060~"}, // 101 key keyboard
358 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
359 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
360 {K_HOME, "\233\064\064~"}, // 101 key keyboard
361 {K_END, "\233\064\065~"}, // 101 key keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
363 {BT_EXTRA_KEYS, ""},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100364 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
365 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
366 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367# endif
368
Bram Moolenaar041c7102020-05-30 18:14:57 +0200369# ifdef ALL_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370/*
Bram Moolenaar041c7102020-05-30 18:14:57 +0200371 * almost standard ANSI terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 {(int)KS_CE, "\033[K"},
374 {(int)KS_CD, "\033[J"},
375 {(int)KS_AL, "\033[L"},
376# ifdef TERMINFO
377 {(int)KS_CAL, "\033[%p1%dL"},
378# else
379 {(int)KS_CAL, "\033[%dL"},
380# endif
381 {(int)KS_DL, "\033[M"},
382# ifdef TERMINFO
383 {(int)KS_CDL, "\033[%p1%dM"},
384# else
385 {(int)KS_CDL, "\033[%dM"},
386# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 {(int)KS_CL, "\033[H\033[2J"},
388#ifdef notyet
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100389 {(int)KS_VI, "[VI]"}, // cursor invisible, VT320: CSI ? 25 l
390 {(int)KS_VE, "[VE]"}, // cursor visible, VT320: CSI ? 25 h
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100392 {(int)KS_ME, "\033[m"}, // normal mode
393 {(int)KS_MR, "\033[7m"}, // reverse
394 {(int)KS_MD, "\033[1m"}, // bold
395 {(int)KS_SO, "\033[31m"}, // standout mode: red
396 {(int)KS_SE, "\033[m"}, // standout end
397 {(int)KS_CZH, "\033[35m"}, // italic: purple
398 {(int)KS_CZR, "\033[m"}, // italic end
399 {(int)KS_US, "\033[4m"}, // underscore mode
400 {(int)KS_UE, "\033[m"}, // underscore end
401 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100403 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
404 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100406 {(int)KS_CAB, "\033[4%dm"}, // set background color
407 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100409 {(int)KS_OP, "\033[m"}, // reset colors
410 {(int)KS_MS, "y"}, // safe to move cur in reverse mode
411 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {(int)KS_LE, "\b"},
413# ifdef TERMINFO
414 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
415# else
416 {(int)KS_CM, "\033[%i%d;%dH"},
417# endif
418 {(int)KS_SR, "\033M"},
419# ifdef TERMINFO
420 {(int)KS_CRI, "\033[%p1%dC"},
421# else
422 {(int)KS_CRI, "\033[%dC"},
423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424
425 {K_UP, "\033[A"},
426 {K_DOWN, "\033[B"},
427 {K_LEFT, "\033[D"},
428 {K_RIGHT, "\033[C"},
429# endif
430
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200431# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432/*
433 * standard ANSI terminal, default for unix
434 */
435 {(int)KS_NAME, "ansi"},
436 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
437 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
438# ifdef TERMINFO
439 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
440# else
441 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
442# endif
443 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
444# ifdef TERMINFO
445 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
446# else
447 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
448# endif
449 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
450 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
451 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
452 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100453 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 {(int)KS_LE, "\b"},
455# ifdef TERMINFO
456 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
457# else
458 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
459# endif
460# ifdef TERMINFO
461 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
462# else
463 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
464# endif
465# endif
466
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200467# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468/*
469 * These codes are valid when nansi.sys or equivalent has been installed.
470 * Function keys on a PC are preceded with a NUL. These are converted into
471 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
472 * CTRL-arrow is used instead of SHIFT-arrow.
473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 {(int)KS_NAME, "pcansi"},
475 {(int)KS_DL, "\033[M"},
476 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 {(int)KS_CE, "\033[K"},
478 {(int)KS_CL, "\033[2J"},
479 {(int)KS_ME, "\033[0m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100480 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
481 {(int)KS_MD, "\033[1m"}, // bold: white text
482 {(int)KS_SE, "\033[0m"}, // standout end
483 {(int)KS_SO, "\033[31m"}, // standout: white on blue
484 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
485 {(int)KS_CZR, "\033[0m"}, // italic mode end
486 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
487 {(int)KS_UE, "\033[0m"}, // underscore mode end
488 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100490 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
491 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100493 {(int)KS_CAB, "\033[4%dm"}, // set background color
494 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100496 {(int)KS_OP, "\033[0m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100498 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 {(int)KS_LE, "\b"},
500# ifdef TERMINFO
501 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
502# else
503 {(int)KS_CM, "\033[%i%d;%dH"},
504# endif
505# ifdef TERMINFO
506 {(int)KS_CRI, "\033[%p1%dC"},
507# else
508 {(int)KS_CRI, "\033[%dC"},
509# endif
510 {K_UP, "\316H"},
511 {K_DOWN, "\316P"},
512 {K_LEFT, "\316K"},
513 {K_RIGHT, "\316M"},
514 {K_S_LEFT, "\316s"},
515 {K_S_RIGHT, "\316t"},
516 {K_F1, "\316;"},
517 {K_F2, "\316<"},
518 {K_F3, "\316="},
519 {K_F4, "\316>"},
520 {K_F5, "\316?"},
521 {K_F6, "\316@"},
522 {K_F7, "\316A"},
523 {K_F8, "\316B"},
524 {K_F9, "\316C"},
525 {K_F10, "\316D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100526 {K_F11, "\316\205"}, // guessed
527 {K_F12, "\316\206"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 {K_S_F1, "\316T"},
529 {K_S_F2, "\316U"},
530 {K_S_F3, "\316V"},
531 {K_S_F4, "\316W"},
532 {K_S_F5, "\316X"},
533 {K_S_F6, "\316Y"},
534 {K_S_F7, "\316Z"},
535 {K_S_F8, "\316["},
536 {K_S_F9, "\316\\"},
537 {K_S_F10, "\316]"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100538 {K_S_F11, "\316\207"}, // guessed
539 {K_S_F12, "\316\210"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {K_INS, "\316R"},
541 {K_DEL, "\316S"},
542 {K_HOME, "\316G"},
543 {K_END, "\316O"},
544 {K_PAGEDOWN, "\316Q"},
545 {K_PAGEUP, "\316I"},
546# endif
547
Bram Moolenaar4f974752019-02-17 17:44:42 +0100548# if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549/*
550 * These codes are valid for the Win32 Console . The entries that start with
551 * ESC | are translated into console calls in os_win32.c. The function keys
552 * are also translated in os_win32.c.
553 */
554 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100555 {(int)KS_CE, "\033|K"}, // clear to end of line
556 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100558 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100560 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100562 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100564 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
565 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100567 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
568 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100570 {(int)KS_CL, "\033|J"}, // clear screen
571 {(int)KS_CD, "\033|j"}, // clear to end of display
572 {(int)KS_VI, "\033|v"}, // cursor invisible
573 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
Bram Moolenaar6982f422019-02-16 16:48:01 +0100575 {(int)KS_ME, "\033|0m"}, // normal
576 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
577 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100579 {(int)KS_SO, "\033|31m"}, // standout: white on blue
580 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100582 {(int)KS_SO, "\033|F"}, // standout: high intensity
583 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100585 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
586 {(int)KS_CZR, "\033|0m"}, // italic end
587 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
588 {(int)KS_UE, "\033|0m"}, // underscore end
589 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100591 {(int)KS_CAB, "\033|%p1%db"}, // set background color
592 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100594 {(int)KS_CAB, "\033|%db"}, // set background color
595 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596# endif
597
Bram Moolenaar6982f422019-02-16 16:48:01 +0100598 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100600 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 {(int)KS_LE, "\b"},
602# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100603 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100605 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100607 {(int)KS_VB, "\033|B"}, // visual bell
608 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
609 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100611 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100613 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100615# ifdef FEAT_TERMGUICOLORS
616 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
617 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
620 {K_UP, "\316H"},
621 {K_DOWN, "\316P"},
622 {K_LEFT, "\316K"},
623 {K_RIGHT, "\316M"},
624 {K_S_UP, "\316\304"},
625 {K_S_DOWN, "\316\317"},
626 {K_S_LEFT, "\316\311"},
627 {K_C_LEFT, "\316s"},
628 {K_S_RIGHT, "\316\313"},
629 {K_C_RIGHT, "\316t"},
630 {K_S_TAB, "\316\017"},
631 {K_F1, "\316;"},
632 {K_F2, "\316<"},
633 {K_F3, "\316="},
634 {K_F4, "\316>"},
635 {K_F5, "\316?"},
636 {K_F6, "\316@"},
637 {K_F7, "\316A"},
638 {K_F8, "\316B"},
639 {K_F9, "\316C"},
640 {K_F10, "\316D"},
641 {K_F11, "\316\205"},
642 {K_F12, "\316\206"},
643 {K_S_F1, "\316T"},
644 {K_S_F2, "\316U"},
645 {K_S_F3, "\316V"},
646 {K_S_F4, "\316W"},
647 {K_S_F5, "\316X"},
648 {K_S_F6, "\316Y"},
649 {K_S_F7, "\316Z"},
650 {K_S_F8, "\316["},
651 {K_S_F9, "\316\\"},
652 {K_S_F10, "\316]"},
653 {K_S_F11, "\316\207"},
654 {K_S_F12, "\316\210"},
655 {K_INS, "\316R"},
656 {K_DEL, "\316S"},
657 {K_HOME, "\316G"},
658 {K_S_HOME, "\316\302"},
659 {K_C_HOME, "\316w"},
660 {K_END, "\316O"},
661 {K_S_END, "\316\315"},
662 {K_C_END, "\316u"},
663 {K_PAGEDOWN, "\316Q"},
664 {K_PAGEUP, "\316I"},
665 {K_KPLUS, "\316N"},
666 {K_KMINUS, "\316J"},
667 {K_KMULTIPLY, "\316\067"},
668 {K_K0, "\316\332"},
669 {K_K1, "\316\336"},
670 {K_K2, "\316\342"},
671 {K_K3, "\316\346"},
672 {K_K4, "\316\352"},
673 {K_K5, "\316\356"},
674 {K_K6, "\316\362"},
675 {K_K7, "\316\366"},
676 {K_K8, "\316\372"},
677 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100678 {K_BS, "\316x"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679# endif
680
681# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
682/*
683 * VT320 is working as an ANSI terminal compatible DEC terminal.
684 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 * TODO:- rewrite ESC[ codes to CSI
686 * - keyboard languages (CSI ? 26 n)
687 */
688 {(int)KS_NAME, "vt320"},
689 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
690 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
691# ifdef TERMINFO
692 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
693# else
694 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
695# endif
696 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
697# ifdef TERMINFO
698 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
699# else
700 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
701# endif
702 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000703 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100704 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
706 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100707 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, // bold mode
708 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},// normal mode
709 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},// exit underscore mode
710 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, // underscore mode
711 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, // italic mode: blue text on yellow
712 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, // italic mode end
713 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, // set background color (ANSI)
714 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, // set foreground color (ANSI)
715 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, // set screen background color
716 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 {(int)KS_MS, "y"},
718 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100719 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {(int)KS_LE, "\b"},
721# ifdef TERMINFO
722 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
723 ESC_STR "[%i%p1%d;%p2%dH")},
724# else
725 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
726# endif
727# ifdef TERMINFO
728 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
729# else
730 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
731# endif
732 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
733 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
734 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
735 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200736 // Note: cursor key sequences for application cursor mode are omitted,
737 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000738 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
739 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
740 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
741 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
742 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
744 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
745 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
746 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
747 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000748 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
750 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
751 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100752 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, // Help
753 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, // Select
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
755 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
756 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
757 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
758 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
759 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
760 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
761 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
762 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
763 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200764 // These sequences starting with <Esc> O may interfere with what the user
765 // is typing. Remove these if that bothers you.
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100766 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, // keypad plus
767 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, // keypad minus
768 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, // keypad /
769 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, // keypad *
770 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, // keypad Enter
771 {K_K0, IF_EB("\033Op", ESC_STR "Op")}, // keypad 0
772 {K_K1, IF_EB("\033Oq", ESC_STR "Oq")}, // keypad 1
773 {K_K2, IF_EB("\033Or", ESC_STR "Or")}, // keypad 2
774 {K_K3, IF_EB("\033Os", ESC_STR "Os")}, // keypad 3
775 {K_K4, IF_EB("\033Ot", ESC_STR "Ot")}, // keypad 4
776 {K_K5, IF_EB("\033Ou", ESC_STR "Ou")}, // keypad 5
777 {K_K6, IF_EB("\033Ov", ESC_STR "Ov")}, // keypad 6
778 {K_K7, IF_EB("\033Ow", ESC_STR "Ow")}, // keypad 7
779 {K_K8, IF_EB("\033Ox", ESC_STR "Ox")}, // keypad 8
780 {K_K9, IF_EB("\033Oy", ESC_STR "Oy")}, // keypad 9
781 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782# endif
783
Bram Moolenaare3f915d2020-07-14 23:02:44 +0200784# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785/*
786 * Ordinary vt52
787 */
788 {(int)KS_NAME, "vt52"},
789 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
790 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100791# ifdef TERMINFO
792 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
793 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
794# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100798 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
800 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 {K_UP, IF_EB("\033A", ESC_STR "A")},
802 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
803 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
804 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100805 {K_F1, IF_EB("\033P", ESC_STR "P")},
806 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
807 {K_F3, IF_EB("\033R", ESC_STR "R")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 {(int)KS_MS, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810# endif
811
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200812# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200813 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
815 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
816# ifdef TERMINFO
817 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
818# else
819 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
820# endif
821 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
822# ifdef TERMINFO
823 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
824# else
825 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
826# endif
827# ifdef TERMINFO
828 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
829 ESC_STR "[%i%p1%d;%p2%dr")},
830# else
831 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
832# endif
833 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
834 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
835 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
836 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
837 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
838 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
839 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200840 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
841 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 {(int)KS_MS, "y"},
843 {(int)KS_UT, "y"},
844 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200845 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
846 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200847 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200848 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200849# ifdef TERMINFO
850 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
851# else
852 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
853# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200854 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200855 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856# ifdef TERMINFO
857 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
858 ESC_STR "[%i%p1%d;%p2%dH")},
859# else
860 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
861# endif
862 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
863# ifdef TERMINFO
864 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
865# else
866 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
867# endif
868 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
869 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
870# ifdef FEAT_XTERM_SAVE
871 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
Bram Moolenaard02e5082020-02-08 14:22:53 +0100872 {(int)KS_TE, IF_EB("\033[?47l\0338",
873 ESC_STR_nc "[?47l" ESC_STR_nc "8")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874# endif
Bram Moolenaar4b570182019-10-20 19:53:22 +0200875 {(int)KS_CTI, IF_EB("\033[>4;2m", ESC_STR_nc "[>4;2m")},
876 {(int)KS_CTE, IF_EB("\033[>4;m", ESC_STR_nc "[>4;m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
878 {(int)KS_CIE, "\007"},
879 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
880 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200881 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
882 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883# ifdef TERMINFO
884 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
885 ESC_STR "[8;%p1%d;%p2%dt")},
886 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
887 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200888 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889# else
890 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
891 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200892 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893# endif
894 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200895 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200896 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100897 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200898# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100899 // These are printf strings, not terminal codes.
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200900 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
901 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
Bram Moolenaare023e882020-05-31 16:42:30 +0200902 {(int)KS_8U, IF_EB("\033[58;2;%lu;%lu;%lum", ESC_STR "[58;2;%lu;%lu;%lum")},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200903# endif
Bram Moolenaare023e882020-05-31 16:42:30 +0200904 {(int)KS_CAU, IF_EB("\033[58;5;%dm", ESC_STR "[58;5;%dm")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100905 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
906 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200907 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
908 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
909 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
910 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000911
912 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
913 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
914 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
915 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100916 // An extra set of cursor keys for vt100 mode
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000917 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
918 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
919 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
920 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100921 // An extra set of function keys for vt100 mode
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000922 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
923 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
924 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
925 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000926 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
927 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
928 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
929 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
930 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
931 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
932 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
933 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
934 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
935 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
936 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
937 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000939 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
940 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
941 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
942 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100943 // {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")},
944 // {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000945 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100946 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, // other Home
947 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, // other Home
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000948 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100949 // {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")},
950 // {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000951 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100952 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, // other End
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000953 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000954 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
955 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100956 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, // keypad plus
957 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, // keypad minus
958 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, // keypad /
959 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, // keypad *
960 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, // keypad Enter
961 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, // keypad .
962 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, // keypad 0
963 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, // keypad 1
964 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, // keypad 2
965 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, // keypad 3
966 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, // keypad 4
967 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, // keypad 5
968 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, // keypad 6
969 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, // keypad 7
970 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, // keypad 8
971 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, // keypad 9
972 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, // keypad Del
973 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, // paste start
974 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
976 {BT_EXTRA_KEYS, ""},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100977 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, // F0
978 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, // F13
979 // F14 and F15 are missing, because they send the same codes as the undo
980 // and help key, although they don't work on all keyboards.
981 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, // F16
982 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, // F17
983 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, // F18
984 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, // F19
985 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000986
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100987 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, // F21
988 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, // F22
989 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, // F23
990 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, // F24
991 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, // F25
992 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, // F26
993 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, // F27
994 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, // F28
995 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, // F29
996 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000997
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100998 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, // F31
999 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, // F32
1000 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, // F33
1001 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, // F34
1002 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, // F35
1003 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, // F36
1004 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005# endif
1006
1007# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1008/*
1009 * iris-ansi for Silicon Graphics machines.
1010 */
1011 {(int)KS_NAME, "iris-ansi"},
1012 {(int)KS_CE, "\033[K"},
1013 {(int)KS_CD, "\033[J"},
1014 {(int)KS_AL, "\033[L"},
1015# ifdef TERMINFO
1016 {(int)KS_CAL, "\033[%p1%dL"},
1017# else
1018 {(int)KS_CAL, "\033[%dL"},
1019# endif
1020 {(int)KS_DL, "\033[M"},
1021# ifdef TERMINFO
1022 {(int)KS_CDL, "\033[%p1%dM"},
1023# else
1024 {(int)KS_CDL, "\033[%dM"},
1025# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001026#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027# ifdef TERMINFO
1028 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1029# else
1030 {(int)KS_CS, "\033[%i%d;%dr"},
1031# endif
1032#endif
1033 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001034 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
1035 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 {(int)KS_TI, "\033[=6h"},
1037 {(int)KS_TE, "\033[=6l"},
1038 {(int)KS_SE, "\033[21;27m"},
1039 {(int)KS_SO, "\033[1;7m"},
1040 {(int)KS_ME, "\033[m"},
1041 {(int)KS_MR, "\033[7m"},
1042 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001043 {(int)KS_CCO, "8"}, // allow 8 colors
1044 {(int)KS_CZH, "\033[3m"}, // italic mode on
1045 {(int)KS_CZR, "\033[23m"}, // italic mode off
1046 {(int)KS_US, "\033[4m"}, // underline on
1047 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001049 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
1050 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
1051 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
1052 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001054 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
1055 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
1056 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
1057 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001059 {(int)KS_MS, "y"}, // guessed
1060 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 {(int)KS_LE, "\b"},
1062# ifdef TERMINFO
1063 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1064# else
1065 {(int)KS_CM, "\033[%i%d;%dH"},
1066# endif
1067 {(int)KS_SR, "\033M"},
1068# ifdef TERMINFO
1069 {(int)KS_CRI, "\033[%p1%dC"},
1070# else
1071 {(int)KS_CRI, "\033[%dC"},
1072# endif
1073 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001074 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001076 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077# ifdef TERMINFO
1078 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1079 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1080# else
1081 {(int)KS_CWS, "\033[203;%d;%d/y"},
1082 {(int)KS_CWP, "\033[205;%d;%d/y"},
1083# endif
1084 {K_UP, "\033[A"},
1085 {K_DOWN, "\033[B"},
1086 {K_LEFT, "\033[D"},
1087 {K_RIGHT, "\033[C"},
1088 {K_S_UP, "\033[161q"},
1089 {K_S_DOWN, "\033[164q"},
1090 {K_S_LEFT, "\033[158q"},
1091 {K_S_RIGHT, "\033[167q"},
1092 {K_F1, "\033[001q"},
1093 {K_F2, "\033[002q"},
1094 {K_F3, "\033[003q"},
1095 {K_F4, "\033[004q"},
1096 {K_F5, "\033[005q"},
1097 {K_F6, "\033[006q"},
1098 {K_F7, "\033[007q"},
1099 {K_F8, "\033[008q"},
1100 {K_F9, "\033[009q"},
1101 {K_F10, "\033[010q"},
1102 {K_F11, "\033[011q"},
1103 {K_F12, "\033[012q"},
1104 {K_S_F1, "\033[013q"},
1105 {K_S_F2, "\033[014q"},
1106 {K_S_F3, "\033[015q"},
1107 {K_S_F4, "\033[016q"},
1108 {K_S_F5, "\033[017q"},
1109 {K_S_F6, "\033[018q"},
1110 {K_S_F7, "\033[019q"},
1111 {K_S_F8, "\033[020q"},
1112 {K_S_F9, "\033[021q"},
1113 {K_S_F10, "\033[022q"},
1114 {K_S_F11, "\033[023q"},
1115 {K_S_F12, "\033[024q"},
1116 {K_INS, "\033[139q"},
1117 {K_HOME, "\033[H"},
1118 {K_END, "\033[146q"},
1119 {K_PAGEUP, "\033[150q"},
1120 {K_PAGEDOWN, "\033[154q"},
1121# endif
1122
1123# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1124/*
1125 * for debugging
1126 */
1127 {(int)KS_NAME, "debug"},
1128 {(int)KS_CE, "[CE]"},
1129 {(int)KS_CD, "[CD]"},
1130 {(int)KS_AL, "[AL]"},
1131# ifdef TERMINFO
1132 {(int)KS_CAL, "[CAL%p1%d]"},
1133# else
1134 {(int)KS_CAL, "[CAL%d]"},
1135# endif
1136 {(int)KS_DL, "[DL]"},
1137# ifdef TERMINFO
1138 {(int)KS_CDL, "[CDL%p1%d]"},
1139# else
1140 {(int)KS_CDL, "[CDL%d]"},
1141# endif
1142# ifdef TERMINFO
1143 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1144# else
1145 {(int)KS_CS, "[%dCS%d]"},
1146# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001147# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001149# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151# endif
1152# ifdef TERMINFO
1153 {(int)KS_CAB, "[CAB%p1%d]"},
1154 {(int)KS_CAF, "[CAF%p1%d]"},
1155 {(int)KS_CSB, "[CSB%p1%d]"},
1156 {(int)KS_CSF, "[CSF%p1%d]"},
1157# else
1158 {(int)KS_CAB, "[CAB%d]"},
1159 {(int)KS_CAF, "[CAF%d]"},
1160 {(int)KS_CSB, "[CSB%d]"},
1161 {(int)KS_CSF, "[CSF%d]"},
1162# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001163 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {(int)KS_OP, "[OP]"},
1165 {(int)KS_LE, "[LE]"},
1166 {(int)KS_CL, "[CL]"},
1167 {(int)KS_VI, "[VI]"},
1168 {(int)KS_VE, "[VE]"},
1169 {(int)KS_VS, "[VS]"},
1170 {(int)KS_ME, "[ME]"},
1171 {(int)KS_MR, "[MR]"},
1172 {(int)KS_MB, "[MB]"},
1173 {(int)KS_MD, "[MD]"},
1174 {(int)KS_SE, "[SE]"},
1175 {(int)KS_SO, "[SO]"},
1176 {(int)KS_UE, "[UE]"},
1177 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001178 {(int)KS_UCE, "[UCE]"},
1179 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001180 {(int)KS_STE, "[STE]"},
1181 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {(int)KS_MS, "[MS]"},
1183 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001184 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185# ifdef TERMINFO
1186 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1187# else
1188 {(int)KS_CM, "[%dCM%d]"},
1189# endif
1190 {(int)KS_SR, "[SR]"},
1191# ifdef TERMINFO
1192 {(int)KS_CRI, "[CRI%p1%d]"},
1193# else
1194 {(int)KS_CRI, "[CRI%d]"},
1195# endif
1196 {(int)KS_VB, "[VB]"},
1197 {(int)KS_KS, "[KS]"},
1198 {(int)KS_KE, "[KE]"},
1199 {(int)KS_TI, "[TI]"},
1200 {(int)KS_TE, "[TE]"},
1201 {(int)KS_CIS, "[CIS]"},
1202 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001203 {(int)KS_CSC, "[CSC]"},
1204 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 {(int)KS_TS, "[TS]"},
1206 {(int)KS_FS, "[FS]"},
1207# ifdef TERMINFO
1208 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1209 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1210# else
1211 {(int)KS_CWS, "[%dCWS%d]"},
1212 {(int)KS_CWP, "[%dCWP%d]"},
1213# endif
1214 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001215 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001216 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001217 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {K_UP, "[KU]"},
1219 {K_DOWN, "[KD]"},
1220 {K_LEFT, "[KL]"},
1221 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001222 {K_XUP, "[xKU]"},
1223 {K_XDOWN, "[xKD]"},
1224 {K_XLEFT, "[xKL]"},
1225 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {K_S_UP, "[S-KU]"},
1227 {K_S_DOWN, "[S-KD]"},
1228 {K_S_LEFT, "[S-KL]"},
1229 {K_C_LEFT, "[C-KL]"},
1230 {K_S_RIGHT, "[S-KR]"},
1231 {K_C_RIGHT, "[C-KR]"},
1232 {K_F1, "[F1]"},
1233 {K_XF1, "[xF1]"},
1234 {K_F2, "[F2]"},
1235 {K_XF2, "[xF2]"},
1236 {K_F3, "[F3]"},
1237 {K_XF3, "[xF3]"},
1238 {K_F4, "[F4]"},
1239 {K_XF4, "[xF4]"},
1240 {K_F5, "[F5]"},
1241 {K_F6, "[F6]"},
1242 {K_F7, "[F7]"},
1243 {K_F8, "[F8]"},
1244 {K_F9, "[F9]"},
1245 {K_F10, "[F10]"},
1246 {K_F11, "[F11]"},
1247 {K_F12, "[F12]"},
1248 {K_S_F1, "[S-F1]"},
1249 {K_S_XF1, "[S-xF1]"},
1250 {K_S_F2, "[S-F2]"},
1251 {K_S_XF2, "[S-xF2]"},
1252 {K_S_F3, "[S-F3]"},
1253 {K_S_XF3, "[S-xF3]"},
1254 {K_S_F4, "[S-F4]"},
1255 {K_S_XF4, "[S-xF4]"},
1256 {K_S_F5, "[S-F5]"},
1257 {K_S_F6, "[S-F6]"},
1258 {K_S_F7, "[S-F7]"},
1259 {K_S_F8, "[S-F8]"},
1260 {K_S_F9, "[S-F9]"},
1261 {K_S_F10, "[S-F10]"},
1262 {K_S_F11, "[S-F11]"},
1263 {K_S_F12, "[S-F12]"},
1264 {K_HELP, "[HELP]"},
1265 {K_UNDO, "[UNDO]"},
1266 {K_BS, "[BS]"},
1267 {K_INS, "[INS]"},
1268 {K_KINS, "[KINS]"},
1269 {K_DEL, "[DEL]"},
1270 {K_KDEL, "[KDEL]"},
1271 {K_HOME, "[HOME]"},
1272 {K_S_HOME, "[C-HOME]"},
1273 {K_C_HOME, "[C-HOME]"},
1274 {K_KHOME, "[KHOME]"},
1275 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001276 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 {K_END, "[END]"},
1278 {K_S_END, "[C-END]"},
1279 {K_C_END, "[C-END]"},
1280 {K_KEND, "[KEND]"},
1281 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001282 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 {K_PAGEUP, "[PAGEUP]"},
1284 {K_PAGEDOWN, "[PAGEDOWN]"},
1285 {K_KPAGEUP, "[KPAGEUP]"},
1286 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1287 {K_MOUSE, "[MOUSE]"},
1288 {K_KPLUS, "[KPLUS]"},
1289 {K_KMINUS, "[KMINUS]"},
1290 {K_KDIVIDE, "[KDIVIDE]"},
1291 {K_KMULTIPLY, "[KMULTIPLY]"},
1292 {K_KENTER, "[KENTER]"},
1293 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001294 {K_PS, "[PASTE-START]"},
1295 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {K_K0, "[K0]"},
1297 {K_K1, "[K1]"},
1298 {K_K2, "[K2]"},
1299 {K_K3, "[K3]"},
1300 {K_K4, "[K4]"},
1301 {K_K5, "[K5]"},
1302 {K_K6, "[K6]"},
1303 {K_K7, "[K7]"},
1304 {K_K8, "[K8]"},
1305 {K_K9, "[K9]"},
1306# endif
1307
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001308#endif // NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309
1310/*
1311 * The most minimal terminal: only clear screen and cursor positioning
1312 * Always included.
1313 */
1314 {(int)KS_NAME, "dumb"},
1315 {(int)KS_CL, "\014"},
1316#ifdef TERMINFO
1317 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1318 ESC_STR "[%i%p1%d;%p2%dH")},
1319#else
1320 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1321#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)
Bram Moolenaar87202262020-05-24 17:23:45 +02001347 semsg(_(e_alloc_color), 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;
1766
1767 mch_errmsg("\r\n");
1768 if (error_msg != NULL)
1769 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001770 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001771 mch_errmsg("\r\n");
1772 }
1773 mch_errmsg("'");
1774 mch_errmsg((char *)term);
1775 mch_errmsg(_("' not known. Available builtin terminals are:"));
1776 mch_errmsg("\r\n");
1777 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1778 {
1779 if (termp->bt_entry == (int)KS_NAME)
1780 {
1781#ifdef HAVE_TGETENT
1782 mch_errmsg(" builtin_");
1783#else
1784 mch_errmsg(" ");
1785#endif
1786 mch_errmsg(termp->bt_string);
1787 mch_errmsg("\r\n");
1788 }
1789 }
1790}
1791
1792 static void
1793report_default_term(char_u *term)
1794{
1795 mch_errmsg(_("defaulting to '"));
1796 mch_errmsg((char *)term);
1797 mch_errmsg("'\r\n");
1798 if (emsg_silent == 0)
1799 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001800 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001801 out_flush();
1802 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001803 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001804 }
1805}
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807/*
1808 * Set terminal options for terminal "term".
1809 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1810 *
1811 * While doing this, until ttest(), some options may be NULL, be careful.
1812 */
1813 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001814set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
1816 struct builtin_term *termp;
1817#ifdef HAVE_TGETENT
1818 int builtin_first = p_tbi;
1819 int try;
1820 int termcap_cleared = FALSE;
1821#endif
1822 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001823 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 char_u *bs_p, *del_p;
1825
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001826 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001827 if (silent_mode)
1828 return OK;
1829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001830 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832 if (term_is_builtin(term))
1833 {
1834 term += 8;
1835#ifdef HAVE_TGETENT
1836 builtin_first = 1;
1837#endif
1838 }
1839
1840/*
1841 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1842 * If builtin_first is TRUE:
1843 * 0. try builtin termcap
1844 * 1. try external termcap
1845 * 2. if both fail default to a builtin terminal
1846 * If builtin_first is FALSE:
1847 * 1. try external termcap
1848 * 2. try builtin termcap, if both fail default to a builtin terminal
1849 */
1850#ifdef HAVE_TGETENT
1851 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1852 {
1853 /*
1854 * Use external termcap
1855 */
1856 if (try == 1)
1857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859
1860 /*
1861 * If the external termcap does not have a matching entry, try the
1862 * builtin ones.
1863 */
1864 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 if (!termcap_cleared)
1867 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001868 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 termcap_cleared = TRUE;
1870 }
1871
Bram Moolenaar69e05692018-05-10 14:11:52 +02001872 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 }
1874 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001875 else // try == 0 || try == 2
1876#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 /*
1878 * Use builtin termcap
1879 */
1880 {
1881#ifdef HAVE_TGETENT
1882 /*
1883 * If builtin termcap was already used, there is no need to search
1884 * for the builtin termcap again, quit now.
1885 */
1886 if (try == 2 && builtin_first && termcap_cleared)
1887 break;
1888#endif
1889 /*
1890 * search for 'term' in builtin_termcaps[]
1891 */
1892 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001893 if (termp->bt_string == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 {
1895#ifdef HAVE_TGETENT
1896 /*
1897 * If try == 0, first try the external termcap. If that is not
1898 * found we'll get back here with try == 2.
1899 * If termcap_cleared is set we used the external termcap,
1900 * don't complain about not finding the term in the builtin
1901 * termcap.
1902 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001903 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001905 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 break;
1907#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001908 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001910 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (starting != NO_SCREEN)
1912 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001913 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 wait_return(TRUE);
1915 return FAIL;
1916 }
1917 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001918 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001919 set_string_option_direct((char_u *)"term", -1, term,
1920 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 display_errors();
1922 }
1923 out_flush();
1924#ifdef HAVE_TGETENT
1925 if (!termcap_cleared)
1926 {
1927#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001928 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929#ifdef HAVE_TGETENT
1930 termcap_cleared = TRUE;
1931 }
1932#endif
1933 parse_builtin_tcap(term);
1934#ifdef FEAT_GUI
1935 if (term_is_gui(term))
1936 {
1937 out_flush();
1938 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001939 // If starting the GUI failed, don't do any of the other
1940 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (!gui.in_use)
1942 return FAIL;
1943#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001944 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945#endif
1946 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001947#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949#ifdef HAVE_TGETENT
1950 }
1951#endif
1952
1953/*
1954 * special: There is no info in the termcap about whether the cursor
1955 * positioning is relative to the start of the screen or to the start of the
1956 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1957 * relative.
1958 */
1959 if (STRCMP(term, "pcterm") == 0)
1960 T_CCS = (char_u *)"yes";
1961 else
1962 T_CCS = empty_option;
1963
1964#ifdef UNIX
1965/*
1966 * Any "stty" settings override the default for t_kb from the termcap.
1967 * This is in os_unix.c, because it depends a lot on the version of unix that
1968 * is being used.
1969 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1970 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001971# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 get_stty();
1975#endif
1976
1977/*
1978 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1979 * didn't work, use the default CTRL-H
1980 * The default for t_kD is DEL, unless t_kb is DEL.
1981 * The vim_strsave'd strings are probably lost forever, well it's only two
1982 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1983 * directly.
1984 */
1985#ifdef FEAT_GUI
1986 if (!gui.in_use)
1987#endif
1988 {
1989 bs_p = find_termcode((char_u *)"kb");
1990 del_p = find_termcode((char_u *)"kD");
1991 if (bs_p == NULL || *bs_p == NUL)
1992 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1993 if ((del_p == NULL || *del_p == NUL) &&
1994 (bs_p == NULL || *bs_p != DEL))
1995 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1996 }
1997
1998#if defined(UNIX) || defined(VMS)
1999 term_is_xterm = vim_is_xterm(term);
2000#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002001#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002002 // Reset terminal properties that are set based on the termresponse, which
2003 // will be sent out soon.
2004 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002007#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 /*
2009 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2010 * The termcode for the mouse is added as a side effect in option.c.
2011 */
2012 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002013 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002015# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002016 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
2018 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002019 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 else
2021 p = (char_u *)"xterm";
2022 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002025 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002027 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2028 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002029 reset_option_was_set((char_u *)"ttym");
2030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002032# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002036 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002038#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002043 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 if (STRCMP(term, DEFAULT_TERM) != 0)
2045 term_console = FALSE;
2046 else
2047 {
2048 term_console = TRUE;
2049# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002050 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051# endif
2052 }
2053#endif
2054
2055#if defined(UNIX) || defined(VMS)
2056 /*
2057 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2058 */
2059 if (vim_is_fastterm(term))
2060 p_tf = TRUE;
2061#endif
2062#ifdef USE_TERM_CONSOLE
2063 /*
2064 * 'ttyfast' is default on consoles
2065 */
2066 if (term_console)
2067 p_tf = TRUE;
2068#endif
2069
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002070 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002072 full_screen = TRUE; // we can use termcap codes from now on
2073 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002075 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002076 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077#endif
2078
2079 /*
2080 * Initialize the terminal with the appropriate termcap codes.
2081 * Set the mouse and window title if possible.
2082 * Don't do this when starting, need to parse the .vimrc first, because it
2083 * may redefine t_TI etc.
2084 */
2085 if (starting != NO_SCREEN)
2086 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002087 starttermcap(); // may change terminal mode
2088 setmouse(); // may start using the mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089#ifdef FEAT_TITLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002090 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091#endif
2092 }
2093
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002094 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 if (width <= 0 || height <= 0)
2096 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002097 // termcap failed to report size
2098 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002100#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002101 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002103 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104#endif
2105 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002106 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 if (starting != NO_SCREEN)
2108 {
2109 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002110 scroll_region_reset(); // In case Rows changed
2111 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002114 buf_T *buf;
2115 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117 /*
2118 * Execute the TermChanged autocommands for each buffer that is
2119 * loaded.
2120 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002121 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 {
2123 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002124 {
2125 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2127 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002128 // restore curwin/curbuf and a few other things
2129 aucmd_restbuf(&aco);
2130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 }
2134
2135#ifdef FEAT_TERMRESPONSE
2136 may_req_termresponse();
2137#endif
2138
2139 return OK;
2140}
2141
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142#ifdef HAVE_TGETENT
2143/*
2144 * Call tgetent()
2145 * Return error message if it fails, NULL if it's OK.
2146 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002147 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002148tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
2150 int i;
2151
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002152 // Note: Valgrind may report a leak here, because the library keeps one
2153 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002155 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002157 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158# endif
2159 )
2160 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002161 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2162 // tgetent() with the always existing "dumb" entry to avoid a crash or
2163 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 (void)TGETENT(tbuf, "dumb");
2165
2166 if (i < 0)
2167# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002168 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 if (i == 0)
2170# endif
2171#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002172 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002174 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175#endif
2176 }
2177 return NULL;
2178}
2179
2180/*
2181 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2182 * Fix that here.
2183 */
2184 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002185vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186{
2187 char *p;
2188
2189 p = tgetstr(s, (char **)pp);
2190 if (p == (char *)-1)
2191 p = NULL;
2192 return (char_u *)p;
2193}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002194#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002196#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197/*
2198 * Get Columns and Rows from the termcap. Used after a window signal if the
2199 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2200 * and "li" entries never change. But on some systems this works.
2201 * Errors while getting the entries are ignored.
2202 */
2203 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002204getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002205 long *cp, // pointer to columns
2206 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207{
2208 char_u tbuf[TBUFSZ];
2209
2210 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2211 {
2212 if (*cp == 0)
2213 *cp = tgetnum("co");
2214 if (*rp == 0)
2215 *rp = tgetnum("li");
2216 }
2217}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002218#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220/*
2221 * Get a string entry from the termcap and add it to the list of termcodes.
2222 * Used for <t_xx> special keys.
2223 * Give an error message for failure when not sourcing.
2224 * If force given, replace an existing entry.
2225 * Return FAIL if the entry was not found, OK if the entry was added.
2226 */
2227 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002228add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229{
2230 char_u *term;
2231 int key;
2232 struct builtin_term *termp;
2233#ifdef HAVE_TGETENT
2234 char_u *string;
2235 int i;
2236 int builtin_first;
2237 char_u tbuf[TBUFSZ];
2238 char_u tstrbuf[TBUFSZ];
2239 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002240 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241#endif
2242
2243/*
2244 * If the GUI is running or will start in a moment, we only support the keys
2245 * that the GUI can produce.
2246 */
2247#ifdef FEAT_GUI
2248 if (gui.in_use || gui.starting)
2249 return gui_mch_haskey(name);
2250#endif
2251
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002252 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 return OK;
2254
2255 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002256 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 return FAIL;
2258
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002259 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
2261 term += 8;
2262#ifdef HAVE_TGETENT
2263 builtin_first = TRUE;
2264#endif
2265 }
2266#ifdef HAVE_TGETENT
2267 else
2268 builtin_first = p_tbi;
2269#endif
2270
2271#ifdef HAVE_TGETENT
2272/*
2273 * We can get the entry from the builtin termcap and from the external one.
2274 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2275 * builtin termcap first.
2276 * If 'ttybuiltin' is off, try external termcap first.
2277 */
2278 for (i = 0; i < 2; ++i)
2279 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002280 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281#endif
2282 /*
2283 * Search in builtin termcap
2284 */
2285 {
2286 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002287 if (termp->bt_string != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002290 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 while (termp->bt_entry != (int)KS_NAME)
2292 {
2293 if ((int)termp->bt_entry == key)
2294 {
2295 add_termcode(name, (char_u *)termp->bt_string,
2296 term_is_8bit(term));
2297 return OK;
2298 }
2299 ++termp;
2300 }
2301 }
2302 }
2303#ifdef HAVE_TGETENT
2304 else
2305 /*
2306 * Search in external termcap
2307 */
2308 {
2309 error_msg = tgetent_error(tbuf, term);
2310 if (error_msg == NULL)
2311 {
2312 string = TGETSTR((char *)name, &tp);
2313 if (string != NULL && *string != NUL)
2314 {
2315 add_termcode(name, string, FALSE);
2316 return OK;
2317 }
2318 }
2319 }
2320 }
2321#endif
2322
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002323 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
2325#ifdef HAVE_TGETENT
2326 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002327 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 else
2329#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002330 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
2332 return FAIL;
2333}
2334
2335 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002336term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337{
2338 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2339}
2340
2341/*
2342 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2343 * Assume that the terminal is using 8-bit controls when the name contains
2344 * "8bit", like in "xterm-8bit".
2345 */
2346 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002347term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
2349 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2350}
2351
2352/*
2353 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002354 * <Esc>[ -> CSI <M_C_[>
2355 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 * <Esc>O -> <M-C-O>
2357 */
2358 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002359term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360{
2361 if (*p == ESC)
2362 {
2363 if (p[1] == '[')
2364 return CSI;
2365 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002366 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 if (p[1] == 'O')
2368 return 0x8f;
2369 }
2370 return 0;
2371}
2372
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002373#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002375term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376{
2377 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2378}
2379#endif
2380
2381#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2382
2383 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002384tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385{
2386 static char_u buf[16];
2387 char_u *p;
2388
2389 p = buf + 15;
2390 *p = '\0';
2391 do
2392 {
2393 --p;
2394 *p = (char_u) (i % 10 + '0');
2395 i /= 10;
2396 }
2397 while (i > 0 && p > buf);
2398 return p;
2399}
2400#endif
2401
2402#ifndef HAVE_TGETENT
2403
2404/*
2405 * minimal tgoto() implementation.
2406 * no padding and we only parse for %i %d and %+char
2407 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002408 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002409tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
2411 static char buf[30];
2412 char *p, *s, *e;
2413
2414 if (!cm)
2415 return "OOPS";
2416 e = buf + 29;
2417 for (s = buf; s < e && *cm; cm++)
2418 {
2419 if (*cm != '%')
2420 {
2421 *s++ = *cm;
2422 continue;
2423 }
2424 switch (*++cm)
2425 {
2426 case 'd':
2427 p = (char *)tltoa((unsigned long)y);
2428 y = x;
2429 while (*p)
2430 *s++ = *p++;
2431 break;
2432 case 'i':
2433 x++;
2434 y++;
2435 break;
2436 case '+':
2437 *s++ = (char)(*++cm + y);
2438 y = x;
2439 break;
2440 case '%':
2441 *s++ = *cm;
2442 break;
2443 default:
2444 return "OOPS";
2445 }
2446 }
2447 *s = '\0';
2448 return buf;
2449}
2450
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002451#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
2453/*
2454 * Set the terminal name and initialize the terminal options.
2455 * If "name" is NULL or empty, get the terminal name from the environment.
2456 * If that fails, use the default terminal name.
2457 */
2458 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002459termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460{
2461 char_u *term;
2462
2463 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002464 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 term = name;
2466
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467#ifndef MSWIN
2468 if (term == NULL)
2469 term = mch_getenv((char_u *)"TERM");
2470#endif
2471 if (term == NULL || *term == NUL)
2472 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002473 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002475 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 set_string_default("term", term);
2477 set_string_default("ttytype", term);
2478
2479 /*
2480 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2481 */
2482 set_termname(T_NAME != NULL ? T_NAME : term);
2483}
2484
2485/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002486 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002488#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002489
2490// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002492
2493static int out_pos = 0; // number of chars in out_buf
2494
2495// Since the maximum number of SGR parameters shown as a normal value range is
2496// 16, the escape sequence length can be 4 * 16 + lead + tail.
2497#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
2499/*
2500 * out_flush(): flush the output buffer
2501 */
2502 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002503out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504{
2505 int len;
2506
2507 if (out_pos != 0)
2508 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002509 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 len = out_pos;
2511 out_pos = 0;
2512 ui_write(out_buf, len);
2513 }
2514}
2515
Bram Moolenaara338adc2018-01-31 20:51:47 +01002516/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002517 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2518 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002519 */
2520 void
2521out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002522 int force UNUSED, // when TRUE, update cursor even when not moved
2523 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002524{
2525 mch_disable_flush();
2526 out_flush();
2527 mch_enable_flush();
2528#ifdef FEAT_GUI
2529 if (gui.in_use)
2530 {
2531 gui_update_cursor(force, clear_selection);
2532 gui_may_flush();
2533 }
2534#endif
2535}
2536
2537
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538/*
2539 * Sometimes a byte out of a multi-byte character is written with out_char().
2540 * To avoid flushing half of the character, call this function first.
2541 */
2542 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002543out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2546 out_flush();
2547}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
2549#ifdef FEAT_GUI
2550/*
2551 * out_trash(): Throw away the contents of the output buffer
2552 */
2553 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002554out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
2556 out_pos = 0;
2557}
2558#endif
2559
2560/*
2561 * out_char(c): put a byte into the output buffer.
2562 * Flush it if it becomes full.
2563 * This should not be used for outputting text on the screen (use functions
2564 * like msg_puts() and screen_putchar() for that).
2565 */
2566 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002567out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568{
Bram Moolenaard0573012017-10-28 21:11:06 +02002569#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002570 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 out_char('\r');
2572#endif
2573
2574 out_buf[out_pos++] = c;
2575
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002576 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 if (out_pos >= OUT_SIZE || p_wd)
2578 out_flush();
2579}
2580
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002582 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 */
2584 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002585out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 out_buf[out_pos++] = c;
2588
2589 if (out_pos >= OUT_SIZE)
2590 out_flush();
2591}
2592
2593/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002594 * A never-padding out_str().
2595 * Use this whenever you don't want to run the string through tputs().
2596 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 * is likely to strip off leading digits, that it mistakes for padding
2598 * information, and "%i", "%d", etc.
2599 * This should only be used for writing terminal codes, not for outputting
2600 * normal text (use functions like msg_puts() and screen_putchar() for that).
2601 */
2602 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002603out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002605 // avoid terminal strings being split up
2606 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 while (*s)
2610 out_char_nf(*s++);
2611
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002612 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 if (p_wd)
2614 out_flush();
2615}
2616
2617/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002618 * A conditional-flushing out_str, mainly for visualbell.
2619 * Handles a delay internally, because termlib may not respect the delay or do
2620 * it at the wrong time.
2621 * Note: Only for terminal strings.
2622 */
2623 void
2624out_str_cf(char_u *s)
2625{
2626 if (s != NULL && *s)
2627 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002628#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002629 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002630#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002631
2632#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002633 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002634 if (gui.in_use)
2635 {
2636 out_str_nf(s);
2637 return;
2638 }
2639#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002640 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002641 out_flush();
2642#ifdef HAVE_TGETENT
2643 for (p = s; *s; ++s)
2644 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002645 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002646 if (*s == '$' && *(s + 1) == '<')
2647 {
2648 char_u save_c = *s;
2649 int duration = atoi((char *)s + 2);
2650
2651 *s = NUL;
2652 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2653 *s = save_c;
2654 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002655# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002656 // Only sleep here if we can limit this happening in
2657 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002658 p = vim_strchr(s, '>');
2659 if (p == NULL || duration <= 0)
2660 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002661 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002662 p = s;
2663 }
2664 else
2665 {
2666 ++p;
2667 do_sleep(duration);
2668 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002669# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002670 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002671 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002672# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002673 break;
2674 }
2675 }
2676 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2677#else
2678 while (*s)
2679 out_char_nf(*s++);
2680#endif
2681
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002682 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002683 if (p_wd)
2684 out_flush();
2685 }
2686}
2687
2688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002690 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 * This should only be used for writing terminal codes, not for outputting
2692 * normal text (use functions like msg_puts() and screen_putchar() for that).
2693 */
2694 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002695out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696{
2697 if (s != NULL && *s)
2698 {
2699#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002700 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 if (gui.in_use)
2702 {
2703 out_str_nf(s);
2704 return;
2705 }
2706#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002707 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002708 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 out_flush();
2710#ifdef HAVE_TGETENT
2711 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2712#else
2713 while (*s)
2714 out_char_nf(*s++);
2715#endif
2716
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002717 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (p_wd)
2719 out_flush();
2720 }
2721}
2722
2723/*
2724 * cursor positioning using termcap parser. (jw)
2725 */
2726 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002727term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729 OUT_STR(tgoto((char *)T_CM, col, row));
2730}
2731
2732 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002733term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734{
2735 OUT_STR(tgoto((char *)T_CRI, 0, i));
2736}
2737
2738 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002739term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740{
2741 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2742}
2743
2744 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002745term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
2747 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2748}
2749
2750#if defined(HAVE_TGETENT) || defined(PROTO)
2751 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002752term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002754 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 if (x < 0)
2756 x = 0;
2757 if (y < 0)
2758 y = 0;
2759 OUT_STR(tgoto((char *)T_CWP, y, x));
2760}
2761
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002762# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2763/*
2764 * Return TRUE if we can request the terminal for a response.
2765 */
2766 static int
2767can_get_termresponse()
2768{
2769 return cur_tmode == TMODE_RAW
2770 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002771# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002772 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002773# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002774 && p_ek;
2775}
2776
Bram Moolenaarafd78262019-05-10 23:10:31 +02002777/*
2778 * Set "status" to STATUS_SENT.
2779 */
2780 static void
2781termrequest_sent(termrequest_T *status)
2782{
2783 status->tr_progress = STATUS_SENT;
2784 status->tr_start = time(NULL);
2785}
2786
2787/*
2788 * Return TRUE if any of the requests are in STATUS_SENT.
2789 */
2790 static int
2791termrequest_any_pending()
2792{
2793 int i;
2794 time_t now = time(NULL);
2795
2796 for (i = 0; all_termrequests[i] != NULL; ++i)
2797 {
2798 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2799 {
2800 if (all_termrequests[i]->tr_start > 0 && now > 0
2801 && all_termrequests[i]->tr_start + 2 < now)
2802 // Sent the request more than 2 seconds ago and didn't get a
2803 // response, assume it failed.
2804 all_termrequests[i]->tr_progress = STATUS_FAIL;
2805 else
2806 return TRUE;
2807 }
2808 }
2809 return FALSE;
2810}
2811
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002812static int winpos_x = -1;
2813static int winpos_y = -1;
2814static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002815
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002816# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002817/*
2818 * Try getting the Vim window position from the terminal.
2819 * Returns OK or FAIL.
2820 */
2821 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002822term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002823{
2824 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002825 int prev_winpos_x = winpos_x;
2826 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002827
2828 if (*T_CGP == NUL || !can_get_termresponse())
2829 return FAIL;
2830 winpos_x = -1;
2831 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002832 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002833 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002834 OUT_STR(T_CGP);
2835 out_flush();
2836
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002837 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002838 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002839 {
2840 (void)vpeekc_nomap();
2841 if (winpos_x >= 0 && winpos_y >= 0)
2842 {
2843 *x = winpos_x;
2844 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002845 return OK;
2846 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002847 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002848 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002849 // Do not reset "did_request_winpos", if we timed out the response might
2850 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002851
2852 winpos_x = prev_winpos_x;
2853 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002854 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002855 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002856 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002857 *x = winpos_x;
2858 *y = winpos_y;
2859 return OK;
2860 }
2861
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002862 return FALSE;
2863}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002864# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002865# endif
2866
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002868term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002870 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871}
2872#endif
2873
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002875term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876{
2877 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002878 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002879 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002881 // Special handling of 16 colors, because termcap can't handle it
2882 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
2883 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002885 && ((s[0] == ESC && s[1] == '[')
2886#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2887 || (s[0] == ESC && s[1] == '|')
2888#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02002889 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 && s[i] != NUL
2891 && (STRCMP(s + i + 1, "%p1%dm") == 0
2892 || STRCMP(s + i + 1, "%dm") == 0)
2893 && (s[i] == '3' || s[i] == '4'))
2894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002896 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002898 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002900 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002901#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02002902 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002903#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002904 IF_EB("\033[", ESC_STR "[")) : "\233";
2905 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2906 : (n >= 16 ? "48;5;" : "10");
2907
2908 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2910 }
2911 else
2912 OUT_STR(tgoto((char *)s, 0, n));
2913}
2914
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002915 void
2916term_fg_color(int n)
2917{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002918 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002919 if (*T_CAF)
2920 term_color(T_CAF, n);
2921 else if (*T_CSF)
2922 term_color(T_CSF, n);
2923}
2924
2925 void
2926term_bg_color(int n)
2927{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002928 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002929 if (*T_CAB)
2930 term_color(T_CAB, n);
2931 else if (*T_CSB)
2932 term_color(T_CSB, n);
2933}
2934
Bram Moolenaare023e882020-05-31 16:42:30 +02002935 void
2936term_ul_color(int n)
2937{
2938 if (*T_CAU)
2939 term_color(T_CAU, n);
2940}
2941
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002942/*
2943 * Return "dark" or "light" depending on the kind of terminal.
2944 * This is just guessing! Recognized are:
2945 * "linux" Linux console
2946 * "screen.linux" Linux console with screen
2947 * "cygwin.*" Cygwin shell
2948 * "putty.*" Putty program
2949 * We also check the COLORFGBG environment variable, which is set by
2950 * rxvt and derivatives. This variable contains either two or three
2951 * values separated by semicolons; we want the last value in either
2952 * case. If this value is 0-6 or 8, our background is dark.
2953 */
2954 char_u *
2955term_bg_default(void)
2956{
2957#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002958 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002959 return (char_u *)"dark";
2960#else
2961 char_u *p;
2962
2963 if (STRCMP(T_NAME, "linux") == 0
2964 || STRCMP(T_NAME, "screen.linux") == 0
2965 || STRNCMP(T_NAME, "cygwin", 6) == 0
2966 || STRNCMP(T_NAME, "putty", 5) == 0
2967 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
2968 && (p = vim_strrchr(p, ';')) != NULL
2969 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
2970 && p[2] == NUL))
2971 return (char_u *)"dark";
2972 return (char_u *)"light";
2973#endif
2974}
2975
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002976#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002977
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002978#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2979#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2980#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002981
2982 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002983term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002984{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002985#define MAX_COLOR_STR_LEN 100
2986 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002987
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002988 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002989 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02002990#ifdef FEAT_VTP
2991 if (use_wt())
2992 {
2993 out_flush();
2994 buf[1] = '[';
2995 vtp_printf(buf);
2996 }
2997 else
2998#endif
2999 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003000}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003001
3002 void
3003term_fg_rgb_color(guicolor_T rgb)
3004{
3005 term_rgb_color(T_8F, rgb);
3006}
3007
3008 void
3009term_bg_rgb_color(guicolor_T rgb)
3010{
3011 term_rgb_color(T_8B, rgb);
3012}
Bram Moolenaare023e882020-05-31 16:42:30 +02003013
3014 void
3015term_ul_rgb_color(guicolor_T rgb)
3016{
3017 term_rgb_color(T_8U, rgb);
3018}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003019#endif
3020
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003021#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
3022 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023/*
3024 * Generic function to set window title, using t_ts and t_fs.
3025 */
3026 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003027term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003029 // t_ts takes one argument: column in status line
3030 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003032 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 out_flush();
3034}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003035
3036/*
3037 * Tell the terminal to push (save) the title and/or icon, so that it can be
3038 * popped (restored) later.
3039 */
3040 void
3041term_push_title(int which)
3042{
Bram Moolenaar27821262019-05-08 16:41:09 +02003043 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003044 {
3045 OUT_STR(T_CST);
3046 out_flush();
3047 }
3048
Bram Moolenaar27821262019-05-08 16:41:09 +02003049 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003050 {
3051 OUT_STR(T_SSI);
3052 out_flush();
3053 }
3054}
3055
3056/*
3057 * Tell the terminal to pop the title and/or icon.
3058 */
3059 void
3060term_pop_title(int which)
3061{
Bram Moolenaar27821262019-05-08 16:41:09 +02003062 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003063 {
3064 OUT_STR(T_CRT);
3065 out_flush();
3066 }
3067
Bram Moolenaar27821262019-05-08 16:41:09 +02003068 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003069 {
3070 OUT_STR(T_SRI);
3071 out_flush();
3072 }
3073}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074#endif
3075
3076/*
3077 * Make sure we have a valid set or terminal options.
3078 * Replace all entries that are NULL by empty_option
3079 */
3080 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003081ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003083 char_u *env_colors;
3084
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003085 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086
3087 /*
3088 * MUST have "cm": cursor motion.
3089 */
3090 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003091 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
3093 /*
3094 * if "cs" defined, use a scroll region, it's faster.
3095 */
3096 if (*T_CS != NUL)
3097 scroll_region = TRUE;
3098 else
3099 scroll_region = FALSE;
3100
3101 if (pairs)
3102 {
3103 /*
3104 * optional pairs
3105 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003106 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 if (*T_ME == NUL)
3108 T_ME = T_MR = T_MD = T_MB = empty_option;
3109 if (*T_SO == NUL || *T_SE == NUL)
3110 T_SO = T_SE = empty_option;
3111 if (*T_US == NUL || *T_UE == NUL)
3112 T_US = T_UE = empty_option;
3113 if (*T_CZH == NUL || *T_CZR == NUL)
3114 T_CZH = T_CZR = empty_option;
3115
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003116 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 if (*T_VE == NUL)
3118 T_VI = empty_option;
3119
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003120 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 if (*T_ME == NUL)
3122 {
3123 T_ME = T_SE;
3124 T_MR = T_SO;
3125 T_MD = T_SO;
3126 }
3127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003128 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 if (*T_SO == NUL)
3130 {
3131 T_SE = T_ME;
3132 if (*T_MR == NUL)
3133 T_SO = T_MD;
3134 else
3135 T_SO = T_MR;
3136 }
3137
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003138 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 if (*T_CZH == NUL)
3140 {
3141 T_CZR = T_ME;
3142 if (*T_MR == NUL)
3143 T_CZH = T_MD;
3144 else
3145 T_CZH = T_MR;
3146 }
3147
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003148 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (*T_CSB == NUL || *T_CSF == NUL)
3150 {
3151 T_CSB = empty_option;
3152 T_CSF = empty_option;
3153 }
3154
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003155 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 if (*T_CAB == NUL || *T_CAF == NUL)
3157 {
3158 T_CAB = empty_option;
3159 T_CAF = empty_option;
3160 }
3161
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003162 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003164 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003166 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 p_wiv = (*T_XS != NUL);
3168 }
3169 need_gather = TRUE;
3170
Bram Moolenaar759d8152020-04-26 16:52:49 +02003171 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3172 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003174#ifdef FEAT_GUI
3175 if (!gui.in_use)
3176#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003177 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003178 env_colors = mch_getenv((char_u *)"COLORS");
3179 if (env_colors != NULL && isdigit(*env_colors))
3180 {
3181 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003182
Bram Moolenaar759d8152020-04-26 16:52:49 +02003183 if (colors != t_colors)
3184 set_color_count(colors);
3185 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187}
3188
3189#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3190 || defined(PROTO)
3191/*
3192 * Represent the given long_u as individual bytes, with the most significant
3193 * byte first, and store them in dst.
3194 */
3195 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003196add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197{
3198 int i;
3199 int shift;
3200
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003201 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
3203 shift = 8 * (sizeof(long_u) - i);
3204 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3205 }
3206}
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208/*
3209 * Interpret the next string of bytes in buf as a long integer, with the most
3210 * significant byte first. Note that it is assumed that buf has been through
3211 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3212 * Puts result in val, and returns the number of bytes read from buf
3213 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3214 * were present.
3215 */
3216 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003217get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218{
3219 int len;
3220 char_u bytes[sizeof(long_u)];
3221 int i;
3222 int shift;
3223
3224 *val = 0;
3225 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3226 if (len != -1)
3227 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003228 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 {
3230 shift = 8 * (sizeof(long_u) - 1 - i);
3231 *val += (long_u)bytes[i] << shift;
3232 }
3233 }
3234 return len;
3235}
3236#endif
3237
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238/*
3239 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3240 * that buf has been through inchar(). Returns the actual number of bytes used
3241 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3242 * available.
3243 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003244 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003245get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
3247 int len = 0;
3248 int i;
3249 char_u c;
3250
3251 for (i = 0; i < num_bytes; i++)
3252 {
3253 if ((c = buf[len++]) == NUL)
3254 return -1;
3255 if (c == K_SPECIAL)
3256 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003257 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 return -1;
3259 if (buf[len++] == (int)KS_ZERO)
3260 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003261 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3262 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003263 if (buf[len++] == (int)KE_CSI)
3264 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003266 else if (c == CSI && buf[len] == KS_EXTRA
3267 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003268 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3269 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003270 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 bytes[i] = c;
3272 }
3273 return len;
3274}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003277 * Check if the new shell size is valid, correct it if it's too small or way
3278 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 */
3280 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003281check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003283 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003285 limit_screen_size();
3286}
3287
3288/*
3289 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3290 */
3291 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003292limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003293{
3294 if (Columns < MIN_COLUMNS)
3295 Columns = MIN_COLUMNS;
3296 else if (Columns > 10000)
3297 Columns = 10000;
3298 if (Rows > 1000)
3299 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300}
3301
Bram Moolenaar86b68352004-12-27 21:59:20 +00003302/*
3303 * Invoked just before the screen structures are going to be (re)allocated.
3304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003306win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307{
3308 static int old_Rows = 0;
3309 static int old_Columns = 0;
3310
3311 if (old_Rows != Rows || old_Columns != Columns)
3312 ui_new_shellsize();
3313 if (old_Rows != Rows)
3314 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003315 // if 'window' uses the whole screen, keep it using that
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003316 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003317 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003319 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
3321 if (old_Columns != Columns)
3322 {
3323 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003324 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
3326}
3327
3328/*
3329 * Call this function when the Vim shell has been resized in any way.
3330 * Will obtain the current size and redraw (also when size didn't change).
3331 */
3332 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003333shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335 set_shellsize(0, 0, FALSE);
3336}
3337
3338/*
3339 * Check if the shell size changed. Handle a resize.
3340 * When the size didn't change, nothing happens.
3341 */
3342 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003343shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 int old_Rows = Rows;
3346 int old_Columns = Columns;
3347
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003348 if (!exiting
3349#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003350 // Do not get the size when executing a shell command during
3351 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003352 && !gui.starting
3353#endif
3354 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003355 {
3356 (void)ui_get_shellsize();
3357 check_shellsize();
3358 if (old_Rows != Rows || old_Columns != Columns)
3359 shell_resized();
3360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361}
3362
3363/*
3364 * Set size of the Vim shell.
3365 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3366 * window size (this is used for the :win command).
3367 * If 'mustset' is FALSE, we may try to get the real window size and if
3368 * it fails use 'width' and 'height'.
3369 */
3370 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003371set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
3373 static int busy = FALSE;
3374
3375 /*
3376 * Avoid recursiveness, can happen when setting the window size causes
3377 * another window-changed signal.
3378 */
3379 if (busy)
3380 return;
3381
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003382 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 return;
3384
Bram Moolenaara971b822011-09-14 14:43:25 +02003385 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003387 // postpone the resizing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 State = SETWSIZE;
3389 return;
3390 }
3391
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003392 if (updating_screen)
3393 // resizing while in update_screen() may cause a crash
3394 return;
3395
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003396 // curwin->w_buffer can be NULL when we are closing a window and the
3397 // buffer has already been closed and removing a scrollbar causes a resize
3398 // event. Don't resize then, it will happen after entering another buffer.
Bram Moolenaara971b822011-09-14 14:43:25 +02003399 if (curwin->w_buffer == NULL)
3400 return;
3401
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 ++busy;
3403
3404#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003405 out_flush(); // must do this before mch_get_shellsize() for
3406 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407#endif
3408
3409 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3410 {
3411 Rows = height;
3412 Columns = width;
3413 check_shellsize();
3414 ui_set_shellsize(mustset);
3415 }
3416 else
3417 check_shellsize();
3418
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003419 // The window layout used to be adjusted here, but it now happens in
3420 // screenalloc() (also invoked from screenclear()). That is because the
3421 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
3423 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3424 screenclear();
3425 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003426 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
3428 if (starting != NO_SCREEN)
3429 {
3430#ifdef FEAT_TITLE
3431 maketitle();
3432#endif
3433 changed_line_abv_curs();
3434 invalidate_botline();
3435
3436 /*
3437 * We only redraw when it's needed:
3438 * - While at the more prompt or executing an external command, don't
3439 * redraw, but position the cursor.
3440 * - While editing the command line, only redraw that.
3441 * - in Ex mode, don't redraw anything.
3442 * - Otherwise, redraw right now, and position the cursor.
3443 * Always need to call update_screen() or screenalloc(), to make
3444 * sure Rows/Columns and the size of ScreenLines[] is correct!
3445 */
3446 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3447 || exmode_active)
3448 {
3449 screenalloc(FALSE);
3450 repeat_message();
3451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 else
3453 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003454 if (curwin->w_p_scb)
3455 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003456 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003457 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003458 update_screen(NOT_VALID);
3459 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003460 }
3461 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003462 {
3463 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003464 if (pum_visible())
3465 {
3466 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003467 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003468 }
Bram Moolenaara5e66212017-09-29 22:42:33 +02003469 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003470 if (redrawing())
3471 setcursor();
3472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003474 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
3476 out_flush();
3477 --busy;
3478}
3479
3480/*
3481 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3482 * commands and Ex mode).
3483 */
3484 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003485settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
3487#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003488 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 if (gui.in_use)
3490 return;
3491#endif
3492
3493 if (full_screen)
3494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003496 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3497 * set the terminal to raw mode, even though we think it already is,
3498 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 * When we think the terminal is normal, don't try to set it to
3500 * normal again, because that causes problems (logout!) on some
3501 * machines.
3502 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003503 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
3505#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003506# ifdef FEAT_GUI
3507 if (!gui.in_use && !gui.starting)
3508# endif
3509 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003510 // May need to check for T_CRV response and termcodes, it
3511 // doesn't work in Cooked mode, an external program may get
3512 // them.
3513 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003514 (void)vpeekc_nomap();
3515 check_for_codes_from_term();
3516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003519 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003520
3521 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3522 // Avoid doing this too often, on some terminals the codes are not
3523 // handled properly.
3524 if (termcap_active && tmode != TMODE_SLEEP
3525 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003526 {
3527 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003528 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003529 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003530 out_str(T_CTE); // possibly disables modifyOtherKeys
3531 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003532 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003533 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003534 out_str(T_BE); // enable bracketed paste mode (should
3535 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003536 out_str(T_CTI); // possibly enables modifyOtherKeys
3537 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003540 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003543 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 out_flush();
3545 }
3546#ifdef FEAT_TERMRESPONSE
3547 may_req_termresponse();
3548#endif
3549 }
3550}
3551
3552 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003553starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
3555 if (full_screen && !termcap_active)
3556 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003557 out_str(T_TI); // start termcap mode
3558 out_str(T_CTI); // start "raw" mode
3559 out_str(T_KS); // start "keypad transmit" mode
3560 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 out_flush();
3562 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003563 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003565# ifdef FEAT_GUI
3566 if (!gui.in_use && !gui.starting)
3567# endif
3568 {
3569 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003570 // Immediately check for a response. If t_Co changes, we don't
3571 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003572 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003573 check_for_codes_from_term();
3574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575#endif
3576 }
3577}
3578
3579 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003580stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581{
3582 screen_stop_highlight();
3583 reset_cterm_colors();
3584 if (termcap_active)
3585 {
3586#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003587# ifdef FEAT_GUI
3588 if (!gui.in_use && !gui.starting)
3589# endif
3590 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003591 // May need to discard T_CRV, T_U7 or T_RBG response.
3592 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003593 {
3594# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003595 // Give the terminal a chance to respond.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003596 mch_delay(100L, FALSE);
3597# endif
3598# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003599 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003600 if (exiting)
3601 tcflush(fileno(stdin), TCIFLUSH);
3602# endif
3603 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003604 // Check for termcodes first, otherwise an external program may
3605 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003606 check_for_codes_from_term();
3607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003609 out_str(T_BD); // disable bracketed paste mode
3610 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 out_flush();
3612 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003613 cursor_on(); // just in case it is still off
3614 out_str(T_CTE); // stop "raw" mode
3615 out_str(T_TE); // stop termcap mode
3616 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 out_flush();
3618 }
3619}
3620
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003621#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622/*
3623 * Request version string (for xterm) when needed.
3624 * Only do this after switching to raw mode, otherwise the result will be
3625 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003626 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003627 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 * Only do this after termcap mode has been started, otherwise the codes for
3629 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003630 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3631 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003632 * On Unix only do it when both output and input are a tty (avoid writing
3633 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 * The result is caught in check_termcode().
3635 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003636 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003637may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003639 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003640 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003641 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 && *T_CRV != NUL)
3643 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003644 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003646 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003647 // check for the characters now, otherwise they might be eaten by
3648 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 out_flush();
3650 (void)vpeekc_nomap();
3651 }
3652}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003653
Bram Moolenaar9584b312013-03-13 19:29:28 +01003654/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003655 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3656 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003657 * Note that this goes out before T_CRV, so that the result can be used when
3658 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003659 */
3660 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003661check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003662{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003663 int did_send = FALSE;
3664
3665 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3666 return;
3667
Bram Moolenaarafd78262019-05-10 23:10:31 +02003668 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003669 && !option_was_set((char_u *)"ambiwidth"))
3670 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003671 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003672
Bram Moolenaara45551a2020-06-09 15:57:37 +02003673 // Ambiguous width check.
3674 // Check how the terminal treats ambiguous character width (UAX #11).
3675 // First, we move the cursor to (1, 0) and print a test ambiguous
3676 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3677 // the current cursor position. If the terminal treats \u25bd as
3678 // single width, the position is (1, 1), or if it is treated as double
3679 // width, that will be (1, 2). This function has the side effect that
3680 // changes cursor position, so it must be called immediately after
3681 // entering termcap mode.
3682 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003683 // Do this in the second row. In the first row the returned sequence
3684 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003685 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003686 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003687 out_str(buf);
3688 out_str(T_U7);
3689 termrequest_sent(&u7_status);
3690 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003691 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003692
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003693 // This overwrites a few characters on the screen, a redraw is needed
3694 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003695 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003696 term_windgoto(1, 0);
3697 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003698 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003699 }
3700
3701 if (xcc_status.tr_progress == STATUS_GET)
3702 {
3703 // 2. Check compatibility with xterm.
3704 // We move the cursor to (2, 0), print a test sequence and then query
3705 // the current cursor position. If the terminal properly handles
3706 // unknown DCS string and CSI sequence with intermediate byte, the test
3707 // sequence is ignored and the cursor does not move. If the terminal
3708 // handles test sequence incorrectly, a garbage string is displayed and
3709 // the cursor does move.
3710 LOG_TR(("Sending xterm compatibility test sequence."));
3711 // Do this in the third row. Second row is used by ambiguous
3712 // chararacter width check.
3713 term_windgoto(2, 0);
3714 // send the test DCS string.
3715 out_str((char_u *)"\033Pzz\033\\");
3716 // send the test CSI sequence with intermediate byte.
3717 out_str((char_u *)"\033[0%m");
3718 out_str(T_U7);
3719 termrequest_sent(&xcc_status);
3720 out_flush();
3721 did_send = TRUE;
3722
3723 // If the terminal handles test sequence incorrectly, garbage text is
3724 // displayed. Clear them out for now.
3725 screen_stop_highlight();
3726 term_windgoto(2, 0);
3727 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003728 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003729 }
3730
3731 if (did_send)
3732 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003733 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003734
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003735 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003736 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003737
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003738 // check for the characters now, otherwise they might be eaten by
3739 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003740 out_flush();
3741 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003742 }
3743}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003744
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003745/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003746 * Similar to requesting the version string: Request the terminal background
3747 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003748 */
3749 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003750may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003751{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003752 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003753 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003754 int didit = FALSE;
3755
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003756# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003757 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003758 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003759 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003760 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003761 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003762 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003763 didit = TRUE;
3764 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003765# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003766
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003767 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003768 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003769 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003770 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003771 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003772 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003773 didit = TRUE;
3774 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003775
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003776 if (didit)
3777 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003778 // check for the characters now, otherwise they might be eaten by
3779 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003780 out_flush();
3781 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003782 }
3783 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003784}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003785
Bram Moolenaar2951b772013-07-03 12:45:31 +02003786# ifdef DEBUG_TERMRESPONSE
3787 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003788log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003789{
3790 static FILE *fd_tr = NULL;
3791 static proftime_T start;
3792 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003793 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003794
3795 if (fd_tr == NULL)
3796 {
3797 fd_tr = fopen("termresponse.log", "w");
3798 profile_start(&start);
3799 }
3800 now = start;
3801 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003802 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3803 must_redraw == NOT_VALID ? "NV"
3804 : must_redraw == CLEAR ? "CL" : " ");
3805 va_start(ap, fmt);
3806 vfprintf(fd_tr, fmt, ap);
3807 va_end(ap);
3808 fputc('\n', fd_tr);
3809 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003810}
3811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812#endif
3813
3814/*
3815 * Return TRUE when saving and restoring the screen.
3816 */
3817 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003818swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819{
3820 return (full_screen && *T_TI != NUL);
3821}
3822
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823/*
3824 * By outputting the 'cursor very visible' termcap code, for some windowed
3825 * terminals this makes the screen scrolled to the correct position.
3826 * Used when starting Vim or returning from a shell.
3827 */
3828 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003829scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003831 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 {
3833 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003834 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003835 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837}
3838
3839static int cursor_is_off = FALSE;
3840
3841/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003842 * Enable the cursor without checking if it's already enabled.
3843 */
3844 void
3845cursor_on_force(void)
3846{
3847 out_str(T_VE);
3848 cursor_is_off = FALSE;
3849}
3850
3851/*
3852 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 */
3854 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003855cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856{
3857 if (cursor_is_off)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003858 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859}
3860
3861/*
3862 * Disable the cursor.
3863 */
3864 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003865cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003867 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003869 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 cursor_is_off = TRUE;
3871 }
3872}
3873
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003874#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003876 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003877 */
3878 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003879term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003880{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003881 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003882 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003883
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003884 // Only do something when redrawing the screen and we can restore the
3885 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003886 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003887 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003888# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003889 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003890 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003891 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003892# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003893 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003894 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003895
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003896 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003897 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003898 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003899 {
3900 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003901 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003902 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003903 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003904 if (*p != NUL)
3905 {
3906 out_str(p);
3907 showing_mode = REPLACE;
3908 }
3909 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003910 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003911 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003912 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003913 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003914 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003915 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003916 showing_mode = INSERT;
3917 }
3918 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003919 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003920 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003921 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003922 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003923 }
3924}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003925
3926# if defined(FEAT_TERMINAL) || defined(PROTO)
3927 void
3928term_cursor_color(char_u *color)
3929{
3930 if (*T_CSC != NUL)
3931 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003932 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003933 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003934 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003935 out_flush();
3936 }
3937}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003938# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003939
Bram Moolenaar4db25542017-08-28 22:43:05 +02003940 int
3941blink_state_is_inverted()
3942{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003943#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02003944 return rbm_status.tr_progress == STATUS_GOT
3945 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003946 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003947#else
3948 return FALSE;
3949#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003950}
3951
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003952/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003953 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003954 */
3955 void
3956term_cursor_shape(int shape, int blink)
3957{
3958 if (*T_CSH != NUL)
3959 {
3960 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3961 out_flush();
3962 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003963 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003964 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003965 int do_blink = blink;
3966
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003967 // t_SH is empty: try setting just the blink state.
3968 // The blink flags are XORed together, if the initial blinking from
3969 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02003970 if (blink_state_is_inverted())
3971 do_blink = !blink;
3972
3973 if (do_blink && *T_VS != NUL)
3974 {
3975 out_str(T_VS);
3976 out_flush();
3977 }
3978 else if (!do_blink && *T_CVS != NUL)
3979 {
3980 out_str(T_CVS);
3981 out_flush();
3982 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003983 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003984}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003985#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003986
3987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 * Set scrolling region for window 'wp'.
3989 * The region starts 'off' lines from the start of the window.
3990 * Also set the vertical scroll region for a vertically split window. Always
3991 * the full width of the window, excluding the vertical separator.
3992 */
3993 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003994scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995{
3996 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3997 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003999 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4000 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004001 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002}
4003
4004/*
4005 * Reset scrolling region to the whole screen.
4006 */
4007 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004008scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009{
4010 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 if (*T_CSV != NUL)
4012 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004013 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014}
4015
4016
4017/*
4018 * List of terminal codes that are currently recognized.
4019 */
4020
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004021static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004023 char_u name[2]; // termcap name of entry
4024 char_u *code; // terminal code (in allocated memory)
4025 int len; // STRLEN(code)
4026 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027} *termcodes = NULL;
4028
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004029static int tc_max_len = 0; // number of entries that termcodes[] can hold
4030static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004032static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004033
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004035clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036{
4037 while (tc_len > 0)
4038 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004039 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 tc_max_len = 0;
4041
4042#ifdef HAVE_TGETENT
4043 BC = (char *)empty_option;
4044 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004045 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 ospeed = 0;
4047#endif
4048
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004049 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050}
4051
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004052#define ATC_FROM_TERM 55
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054/*
4055 * Add a new entry to the list of terminal codes.
4056 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004057 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4058 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 */
4060 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004061add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062{
4063 struct termcode *new_tc;
4064 int i, j;
4065 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004066 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068 if (string == NULL || *string == NUL)
4069 {
4070 del_termcode(name);
4071 return;
4072 }
4073
Bram Moolenaar4f974752019-02-17 17:44:42 +01004074#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004075 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004076#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004077# ifdef VIMDLL
4078 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004079 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004080 else
4081# endif
4082 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 if (s == NULL)
4085 return;
4086
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004087 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004088 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004090 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 s[0] = term_7to8bit(string);
4092 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004093
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004094#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4095# ifdef VIMDLL
4096 if (!gui.in_use)
4097# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004098 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004099 if (s[0] == K_NUL)
4100 {
4101 STRMOVE(s + 1, s);
4102 s[1] = 3;
4103 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004104 }
4105#endif
4106
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004107 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004109 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110
4111 /*
4112 * need to make space for more entries
4113 */
4114 if (tc_len == tc_max_len)
4115 {
4116 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004117 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 if (new_tc == NULL)
4119 {
4120 tc_max_len -= 20;
4121 return;
4122 }
4123 for (i = 0; i < tc_len; ++i)
4124 new_tc[i] = termcodes[i];
4125 vim_free(termcodes);
4126 termcodes = new_tc;
4127 }
4128
4129 /*
4130 * Look for existing entry with the same name, it is replaced.
4131 * Look for an existing entry that is alphabetical higher, the new entry
4132 * is inserted in front of it.
4133 */
4134 for (i = 0; i < tc_len; ++i)
4135 {
4136 if (termcodes[i].name[0] < name[0])
4137 continue;
4138 if (termcodes[i].name[0] == name[0])
4139 {
4140 if (termcodes[i].name[1] < name[1])
4141 continue;
4142 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004143 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 */
4145 if (termcodes[i].name[1] == name[1])
4146 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004147 if (flags == ATC_FROM_TERM && (j = termcode_star(
4148 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004149 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004150 // Don't replace ESC[123;*X or ESC O*X with another when
4151 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004152 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004153 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004154 && s[len - 1]
4155 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004156 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004157 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004158 vim_free(s);
4159 return;
4160 }
4161 }
4162 else
4163 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004164 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004165 vim_free(termcodes[i].code);
4166 --tc_len;
4167 break;
4168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 }
4170 }
4171 /*
4172 * Found alphabetical larger entry, move rest to insert new entry
4173 */
4174 for (j = tc_len; j > i; --j)
4175 termcodes[j] = termcodes[j - 1];
4176 break;
4177 }
4178
4179 termcodes[i].name[0] = name[0];
4180 termcodes[i].name[1] = name[1];
4181 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004182 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004183
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004184 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4185 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004186 termcodes[i].modlen = 0;
4187 j = termcode_star(s, len);
4188 if (j > 0)
4189 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 ++tc_len;
4191}
4192
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004193/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004194 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004195 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004196 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004197 */
4198 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004199termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004200{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004201 // Shortest is <M-O>*X. With ; shortest is <CSI>1;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004202 if (len >= 3 && code[len - 2] == '*')
4203 {
4204 if (len >= 5 && code[len - 3] == ';')
4205 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004206 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004207 return 1;
4208 }
4209 return 0;
4210}
4211
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004213find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214{
4215 int i;
4216
4217 for (i = 0; i < tc_len; ++i)
4218 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4219 return termcodes[i].code;
4220 return NULL;
4221}
4222
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004224get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
4226 if (i >= tc_len)
4227 return NULL;
4228 return &termcodes[i].name[0];
4229}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004231/*
4232 * Returns the length of the terminal code at index 'idx'.
4233 */
4234 int
4235get_termcode_len(int idx)
4236{
4237 return termcodes[idx].len;
4238}
4239
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004240 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004241del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242{
4243 int i;
4244
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004245 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 return;
4247
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004248 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249
4250 for (i = 0; i < tc_len; ++i)
4251 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4252 {
4253 del_termcode_idx(i);
4254 return;
4255 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004256 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257}
4258
4259 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004260del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261{
4262 int i;
4263
4264 vim_free(termcodes[idx].code);
4265 --tc_len;
4266 for (i = idx; i < tc_len; ++i)
4267 termcodes[i] = termcodes[i + 1];
4268}
4269
4270#ifdef FEAT_TERMRESPONSE
4271/*
4272 * Called when detected that the terminal sends 8-bit codes.
4273 * Convert all 7-bit codes to their 8-bit equivalent.
4274 */
4275 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004276switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277{
4278 int i;
4279 int c;
4280
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004281 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 if (!term_is_8bit(T_NAME))
4283 {
4284 for (i = 0; i < tc_len; ++i)
4285 {
4286 c = term_7to8bit(termcodes[i].code);
4287 if (c != 0)
4288 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004289 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 termcodes[i].code[0] = c;
4291 }
4292 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004293 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004296 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297}
4298#endif
4299
4300#ifdef CHECK_DOUBLE_CLICK
4301static linenr_T orig_topline = 0;
4302# ifdef FEAT_DIFF
4303static int orig_topfill = 0;
4304# endif
4305#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004306#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307/*
4308 * Checking for double clicks ourselves.
4309 * "orig_topline" is used to avoid detecting a double-click when the window
4310 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4311 */
4312/*
4313 * Set orig_topline. Used when jumping to another window, so that a double
4314 * click still works.
4315 */
4316 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004317set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318{
4319 orig_topline = wp->w_topline;
4320# ifdef FEAT_DIFF
4321 orig_topfill = wp->w_topfill;
4322# endif
4323}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004324
4325/*
4326 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4327 * topline and topfill.
4328 */
4329 int
4330is_mouse_topline(win_T *wp)
4331{
4332 return orig_topline == wp->w_topline
4333#ifdef FEAT_DIFF
4334 && orig_topfill == wp->w_topfill
4335#endif
4336 ;
4337}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338#endif
4339
4340/*
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004341 * Put "string[new_slen]" in typebuf, or in "buf[bufsize]" if "buf" is not NULL.
4342 * Remove "slen" bytes.
4343 * Returns FAIL for error.
4344 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004345 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004346put_string_in_typebuf(
4347 int offset,
4348 int slen,
4349 char_u *string,
4350 int new_slen,
4351 char_u *buf,
4352 int bufsize,
4353 int *buflen)
4354{
4355 int extra = new_slen - slen;
4356
4357 string[new_slen] = NUL;
4358 if (buf == NULL)
4359 {
4360 if (extra < 0)
4361 // remove matched chars, taking care of noremap
4362 del_typebuf(-extra, offset);
4363 else if (extra > 0)
4364 // insert the extra space we need
4365 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
4366
4367 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4368 // typebuf.tb_buf[]!
4369 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4370 (size_t)new_slen);
4371 }
4372 else
4373 {
4374 if (extra < 0)
4375 // remove matched characters
4376 mch_memmove(buf + offset, buf + offset - extra,
4377 (size_t)(*buflen + offset + extra));
4378 else if (extra > 0)
4379 {
4380 // Insert the extra space we need. If there is insufficient
4381 // space return -1.
4382 if (*buflen + extra + new_slen >= bufsize)
4383 return FAIL;
4384 mch_memmove(buf + offset + extra, buf + offset,
4385 (size_t)(*buflen - offset));
4386 }
4387 mch_memmove(buf + offset, string, (size_t)new_slen);
4388 *buflen = *buflen + extra + new_slen;
4389 }
4390 return OK;
4391}
4392
4393/*
4394 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4395 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004396 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004397decode_modifiers(int n)
4398{
4399 int code = n - 1;
4400 int modifiers = 0;
4401
4402 if (code & 1)
4403 modifiers |= MOD_MASK_SHIFT;
4404 if (code & 2)
4405 modifiers |= MOD_MASK_ALT;
4406 if (code & 4)
4407 modifiers |= MOD_MASK_CTRL;
4408 if (code & 8)
4409 modifiers |= MOD_MASK_META;
4410 return modifiers;
4411}
4412
4413 static int
4414modifiers2keycode(int modifiers, int *key, char_u *string)
4415{
4416 int new_slen = 0;
4417
4418 if (modifiers != 0)
4419 {
4420 // Some keys have the modifier included. Need to handle that here to
4421 // make mappings work.
4422 *key = simplify_key(*key, &modifiers);
4423 if (modifiers != 0)
4424 {
4425 string[new_slen++] = K_SPECIAL;
4426 string[new_slen++] = (int)KS_MODIFIER;
4427 string[new_slen++] = modifiers;
4428 }
4429 }
4430 return new_slen;
4431}
4432
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004433#ifdef FEAT_TERMRESPONSE
4434/*
4435 * Handle a cursor position report.
4436 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004437 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004438handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004439{
4440 if (arg[0] == 2 && arg[1] >= 2)
4441 {
4442 char *aw = NULL;
4443
4444 LOG_TR(("Received U7 status: %s", tp));
4445 u7_status.tr_progress = STATUS_GOT;
4446 did_cursorhold = TRUE;
4447 if (arg[1] == 2)
4448 aw = "single";
4449 else if (arg[1] == 3)
4450 aw = "double";
4451 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4452 {
4453 // Setting the option causes a screen redraw. Do
4454 // that right away if possible, keeping any
4455 // messages.
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004456 set_option_value((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004457# ifdef DEBUG_TERMRESPONSE
4458 {
4459 int r = redraw_asap(CLEAR);
4460
4461 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4462 }
4463# else
4464 redraw_asap(CLEAR);
4465# endif
4466# ifdef FEAT_EVAL
4467 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4468# endif
4469 }
4470 }
4471 else if (arg[0] == 3)
4472 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004473 int value;
4474
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004475 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004476 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004477
4478 // Third row: xterm compatibility test.
4479 // If the cursor is on the first column then the terminal can handle
4480 // the request for cursor style and blinking.
4481 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4482 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4483 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004484 }
4485}
4486
4487/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004488 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
4489 * Xterm and alikes use '>' for {first}.
4490 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004491 */
4492 static void
4493handle_version_response(int first, int *arg, int argc, char_u *tp)
4494{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004495 // The xterm version. It is set to zero when it can't be an actual xterm
4496 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004497 int version = arg[1];
4498
4499 LOG_TR(("Received CRV response: %s", tp));
4500 crv_status.tr_progress = STATUS_GOT;
4501 did_cursorhold = TRUE;
4502
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004503 // Reset terminal properties that are set based on the termresponse.
4504 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004505 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004506 init_term_props(
4507#ifdef FEAT_EVAL
4508 reset_term_props_on_termresponse
4509#else
4510 FALSE
4511#endif
4512 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004513
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004514 // If this code starts with CSI, you can bet that the
4515 // terminal uses 8-bit codes.
4516 if (tp[0] == CSI)
4517 switch_to_8bit();
4518
4519 // Screen sends 40500.
4520 // rxvt sends its version number: "20703" is 2.7.3.
4521 // Ignore it for when the user has set 'term' to xterm,
4522 // even though it's an rxvt.
4523 if (version > 20000)
4524 version = 0;
4525
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004526 // Figure out more if the reeponse is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004527 if (first == '>' && argc == 3)
4528 {
4529 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004530
4531 // mintty 2.9.5 sends 77;20905;0c.
4532 // (77 is ASCII 'M' for mintty.)
4533 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004534 {
4535 // mintty can do SGR mouse reporting
4536 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4537 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004538
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004539 // If xterm version >= 141 try to get termcap codes. For other
4540 // terminals the request should be ignored.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004541 if (version >= 141)
4542 {
4543 LOG_TR(("Enable checking for XT codes"));
4544 check_for_codes = TRUE;
4545 need_gather = TRUE;
4546 req_codes_from_term();
4547 }
4548
4549 // libvterm sends 0;100;0
4550 if (version == 100 && arg[0] == 0 && arg[2] == 0)
4551 {
4552 // If run from Vim $COLORS is set to the number of
4553 // colors the terminal supports. Otherwise assume
4554 // 256, libvterm supports even more.
4555 if (mch_getenv((char_u *)"COLORS") == NULL)
4556 may_adjust_color_count(256);
4557 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004558 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004559 }
4560
4561 if (version == 95)
4562 {
4563 // Mac Terminal.app sends 1;95;0
4564 if (arg[0] == 1 && arg[2] == 0)
4565 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004566 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4567 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004568 }
4569 // iTerm2 sends 0;95;0
4570 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004571 {
4572 // iTerm2 can do SGR mouse reporting
4573 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4574 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004575 // old iTerm2 sends 0;95;
4576 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004577 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004578 }
4579
4580 // screen sends 83;40500;0 83 is 'S' in ASCII.
4581 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004582 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004583 // screen supports SGR mouse codes since 4.7.0
4584 if (arg[1] >= 40700)
4585 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4586 else
4587 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4588 }
4589
4590 // If no recognized terminal has set mouse behavior, assume xterm.
4591 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4592 {
4593 // Xterm version 277 supports SGR.
4594 // Xterm version >= 95 supports mouse dragging.
4595 if (version >= 277)
4596 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004597 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004598 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004599 }
4600
4601 // Detect terminals that set $TERM to something like
4602 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004603 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004604 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004605 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004606 // xfce4-terminal sends 1;2802;0.
4607 // screen sends 83;40500;0
4608 // Assuming any version number over 2500 is not an
4609 // xterm (without the limit for rxvt and screen).
4610 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004611 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004612
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004613 else if (version == 136 && arg[2] == 0)
4614 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004615 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004616
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004617 // PuTTY sends 0;136;0
4618 if (arg[0] == 0)
4619 {
4620 // supports sgr-like mouse reporting.
4621 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4622 }
4623 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004624 }
4625
4626 // Konsole sends 0;115;0
4627 else if (version == 115 && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004628 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004629
4630 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004631 // 30600/40500 is a version number of GNU screen. DA2 support is added
4632 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4633 // compatibility checking does not detect GNU screen.
4634 if (arg[0] == 83 && arg[1] >= 30600)
4635 {
4636 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4637 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4638 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004639
4640 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004641 // 95, so assume anything below 95 is not xterm and hopefully supports
4642 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004643 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004644 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004645
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004646 // Getting the cursor style is only supported properly by xterm since
4647 // version 279 (otherwise it returns 0x18).
4648 if (version < 279)
4649 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4650
4651 /*
4652 * Take action on the detected properties.
4653 */
4654
4655 // Unless the underline RGB color is expected to work, disable "t_8u".
4656 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004657 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES && *T_8U != NUL)
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004658 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4659 OPT_FREE, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004660
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004661 // Only set 'ttymouse' automatically if it was not set
4662 // by the user already.
4663 if (!option_was_set((char_u *)"ttym")
4664 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4665 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4666 {
4667 set_option_value((char_u *)"ttym", 0L,
4668 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4669 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4670 }
4671
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004672 // Only request the cursor style if t_SH and t_RS are
4673 // set. Only supported properly by xterm since version
4674 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004675 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004676 // Not for Terminal.app, it can't handle t_RS, it
4677 // echoes the characters to the screen.
4678 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004679 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004680 && *T_CSH != NUL
4681 && *T_CRS != NUL)
4682 {
4683 LOG_TR(("Sending cursor style request"));
4684 out_str(T_CRS);
4685 termrequest_sent(&rcs_status);
4686 need_flush = TRUE;
4687 }
4688
4689 // Only request the cursor blink mode if t_RC set. Not
4690 // for Gnome terminal, it can't handle t_RC, it
4691 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004692 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004693 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004694 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004695 && *T_CRC != NUL)
4696 {
4697 LOG_TR(("Sending cursor blink mode request"));
4698 out_str(T_CRC);
4699 termrequest_sent(&rbm_status);
4700 need_flush = TRUE;
4701 }
4702
4703 if (need_flush)
4704 out_flush();
4705 }
4706}
4707
4708/*
4709 * Handle a sequence with key and modifier, one of:
4710 * {lead}27;{modifier};{key}~
4711 * {lead}{key};{modifier}u
4712 * Returns the difference in length.
4713 */
4714 static int
4715handle_key_with_modifier(
4716 int *arg,
4717 int trail,
4718 int csi_len,
4719 int offset,
4720 char_u *buf,
4721 int bufsize,
4722 int *buflen)
4723{
4724 int key;
4725 int modifiers;
4726 int new_slen;
4727 char_u string[MAX_KEY_CODE_LEN + 1];
4728
4729 seenModifyOtherKeys = TRUE;
4730 if (trail == 'u')
4731 key = arg[0];
4732 else
4733 key = arg[2];
4734
4735 modifiers = decode_modifiers(arg[1]);
4736
Bram Moolenaaref6746f2020-06-20 14:43:23 +02004737 // May remove the shift modifier if it's already included in the key.
4738 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004739
4740 // When used with Ctrl we always make a letter upper case,
4741 // so that mapping <C-H> and <C-h> are the same. Typing
4742 // <C-S-H> also uses "H" but modifier is different.
4743 if ((modifiers & MOD_MASK_CTRL) && ASCII_ISALPHA(key))
4744 key = TOUPPER_ASC(key);
4745
4746 // insert modifiers with KS_MODIFIER
4747 new_slen = modifiers2keycode(modifiers, &key, string);
4748
4749 if (has_mbyte)
4750 new_slen += (*mb_char2bytes)(key, string + new_slen);
4751 else
4752 string[new_slen++] = key;
4753
4754 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4755 buf, bufsize, buflen) == FAIL)
4756 return -1;
4757 return new_slen - csi_len + offset;
4758}
4759
4760/*
4761 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004762 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004763 *
4764 * - Cursor position report: {lead}{row};{col}R
4765 * The final byte must be 'R'. It is used for checking the
4766 * ambiguous-width character state.
4767 *
4768 * - window position reply: {lead}3;{x};{y}t
4769 *
4770 * - key with modifiers when modifyOtherKeys is enabled:
4771 * {lead}27;{modifier};{key}~
4772 * {lead}{key};{modifier}u
4773 * Return 0 for no match, -1 for partial match, > 0 for full match.
4774 */
4775 static int
4776handle_csi(
4777 char_u *tp,
4778 int len,
4779 char_u *argp,
4780 int offset,
4781 char_u *buf,
4782 int bufsize,
4783 int *buflen,
4784 char_u *key_name,
4785 int *slen)
4786{
4787 int first = -1; // optional char right after {lead}
4788 int trail; // char that ends CSI sequence
4789 int arg[3] = {-1, -1, -1}; // argument numbers
4790 int argc; // number of arguments
4791 char_u *ap = argp;
4792 int csi_len;
4793
4794 // Check for non-digit after CSI.
4795 if (!VIM_ISDIGIT(*ap))
4796 first = *ap++;
4797
4798 // Find up to three argument numbers.
4799 for (argc = 0; argc < 3; )
4800 {
4801 if (ap >= tp + len)
4802 return -1;
4803 if (*ap == ';')
4804 arg[argc++] = -1; // omitted number
4805 else if (VIM_ISDIGIT(*ap))
4806 {
4807 arg[argc] = 0;
4808 for (;;)
4809 {
4810 if (ap >= tp + len)
4811 return -1;
4812 if (!VIM_ISDIGIT(*ap))
4813 break;
4814 arg[argc] = arg[argc] * 10 + (*ap - '0');
4815 ++ap;
4816 }
4817 ++argc;
4818 }
4819 if (*ap == ';')
4820 ++ap;
4821 else
4822 break;
4823 }
4824
4825 // mrxvt has been reported to have "+" in the version. Assume
4826 // the escape sequence ends with a letter or one of "{|}~".
4827 while (ap < tp + len
4828 && !(*ap >= '{' && *ap <= '~')
4829 && !ASCII_ISALPHA(*ap))
4830 ++ap;
4831 if (ap >= tp + len)
4832 return -1;
4833 trail = *ap;
4834 csi_len = (int)(ap - tp) + 1;
4835
4836 // Cursor position report: Eat it when there are 2 arguments
4837 // and it ends in 'R'. Also when u7_status is not "sent", it
4838 // may be from a previous Vim that just exited. But not for
4839 // <S-F3>, it sends something similar, check for row and column
4840 // to make sense.
4841 if (first == -1 && argc == 2 && trail == 'R')
4842 {
4843 handle_u7_response(arg, tp, csi_len);
4844
4845 key_name[0] = (int)KS_EXTRA;
4846 key_name[1] = (int)KE_IGNORE;
4847 *slen = csi_len;
4848 }
4849
4850 // Version string: Eat it when there is at least one digit and
4851 // it ends in 'c'
4852 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
4853 {
4854 handle_version_response(first, arg, argc, tp);
4855
4856 *slen = csi_len;
4857# ifdef FEAT_EVAL
4858 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
4859# endif
4860 apply_autocmds(EVENT_TERMRESPONSE,
4861 NULL, NULL, FALSE, curbuf);
4862 key_name[0] = (int)KS_EXTRA;
4863 key_name[1] = (int)KE_IGNORE;
4864 }
4865
4866 // Check blinking cursor from xterm:
4867 // {lead}?12;1$y set
4868 // {lead}?12;2$y not set
4869 //
4870 // {lead} can be <Esc>[ or CSI
4871 else if (rbm_status.tr_progress == STATUS_SENT
4872 && first == '?'
4873 && ap == argp + 6
4874 && arg[0] == 12
4875 && ap[-1] == '$'
4876 && trail == 'y')
4877 {
4878 initial_cursor_blink = (arg[1] == '1');
4879 rbm_status.tr_progress = STATUS_GOT;
4880 LOG_TR(("Received cursor blinking mode response: %s", tp));
4881 key_name[0] = (int)KS_EXTRA;
4882 key_name[1] = (int)KE_IGNORE;
4883 *slen = csi_len;
4884# ifdef FEAT_EVAL
4885 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
4886# endif
4887 }
4888
4889 // Check for a window position response from the terminal:
4890 // {lead}3;{x};{y}t
4891 else if (did_request_winpos && argc == 3 && arg[0] == 3
4892 && trail == 't')
4893 {
4894 winpos_x = arg[1];
4895 winpos_y = arg[2];
4896 // got finished code: consume it
4897 key_name[0] = (int)KS_EXTRA;
4898 key_name[1] = (int)KE_IGNORE;
4899 *slen = csi_len;
4900
4901 if (--did_request_winpos <= 0)
4902 winpos_status.tr_progress = STATUS_GOT;
4903 }
4904
4905 // Key with modifier:
4906 // {lead}27;{modifier};{key}~
4907 // {lead}{key};{modifier}u
4908 else if ((arg[0] == 27 && argc == 3 && trail == '~')
4909 || (argc == 2 && trail == 'u'))
4910 {
4911 return len + handle_key_with_modifier(arg, trail,
4912 csi_len, offset, buf, bufsize, buflen);
4913 }
4914
4915 // else: Unknown CSI sequence. We could drop it, but then the
4916 // user can't create a map for it.
4917 return 0;
4918}
4919
4920/*
4921 * Handle an OSC sequence, fore/background color response from the terminal:
4922 *
4923 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
4924 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
4925 *
4926 * {code} is 10 for foreground, 11 for background
4927 * {lead} can be <Esc>] or OSC
4928 * {tail} can be '\007', <Esc>\ or STERM.
4929 *
4930 * Consume any code that starts with "{lead}11;", it's also
4931 * possible that "rgba" is following.
4932 */
4933 static int
4934handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
4935{
4936 int i, j;
4937
4938 j = 1 + (tp[0] == ESC);
4939 if (len >= j + 3 && (argp[0] != '1'
4940 || (argp[1] != '1' && argp[1] != '0')
4941 || argp[2] != ';'))
4942 i = 0; // no match
4943 else
4944 for (i = j; i < len; ++i)
4945 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4946 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
4947 {
4948 int is_bg = argp[1] == '1';
4949 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
4950 && tp[j + 16] == '/';
4951
4952 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4953 && (is_4digit
4954 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
4955 {
4956 char_u *tp_r = tp + j + 7;
4957 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
4958 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
4959# ifdef FEAT_TERMINAL
4960 int rval, gval, bval;
4961
4962 rval = hexhex2nr(tp_r);
4963 gval = hexhex2nr(tp_b);
4964 bval = hexhex2nr(tp_g);
4965# endif
4966 if (is_bg)
4967 {
4968 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
4969 *tp_b) ? "light" : "dark";
4970
4971 LOG_TR(("Received RBG response: %s", tp));
4972 rbg_status.tr_progress = STATUS_GOT;
4973# ifdef FEAT_TERMINAL
4974 bg_r = rval;
4975 bg_g = gval;
4976 bg_b = bval;
4977# endif
4978 if (!option_was_set((char_u *)"bg")
4979 && STRCMP(p_bg, new_bg_val) != 0)
4980 {
4981 // value differs, apply it
4982 set_option_value((char_u *)"bg", 0L,
4983 (char_u *)new_bg_val, 0);
4984 reset_option_was_set((char_u *)"bg");
4985 redraw_asap(CLEAR);
4986 }
4987 }
4988# ifdef FEAT_TERMINAL
4989 else
4990 {
4991 LOG_TR(("Received RFG response: %s", tp));
4992 rfg_status.tr_progress = STATUS_GOT;
4993 fg_r = rval;
4994 fg_g = gval;
4995 fg_b = bval;
4996 }
4997# endif
4998 }
4999
5000 // got finished code: consume it
5001 key_name[0] = (int)KS_EXTRA;
5002 key_name[1] = (int)KE_IGNORE;
5003 *slen = i + 1 + (tp[i] == ESC);
5004# ifdef FEAT_EVAL
5005 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5006 : VV_TERMRFGRESP, tp, *slen);
5007# endif
5008 break;
5009 }
5010 if (i == len)
5011 {
5012 LOG_TR(("not enough characters for RB"));
5013 return FAIL;
5014 }
5015 return OK;
5016}
5017
5018/*
5019 * Check for key code response from xterm:
5020 * {lead}{flag}+r<hex bytes><{tail}
5021 *
5022 * {lead} can be <Esc>P or DCS
5023 * {flag} can be '0' or '1'
5024 * {tail} can be Esc>\ or STERM
5025 *
5026 * Check for cursor shape response from xterm:
5027 * {lead}1$r<digit> q{tail}
5028 *
5029 * {lead} can be <Esc>P or DCS
5030 * {tail} can be <Esc>\ or STERM
5031 *
5032 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5033 */
5034 static int
5035handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5036{
5037 int i, j;
5038
5039 j = 1 + (tp[0] == ESC);
5040 if (len < j + 3)
5041 i = len; // need more chars
5042 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
5043 i = 0; // no match
5044 else if (argp[1] == '+')
5045 // key code response
5046 for (i = j; i < len; ++i)
5047 {
5048 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5049 || tp[i] == STERM)
5050 {
5051 if (i - j >= 3)
5052 got_code_from_term(tp + j, i);
5053 key_name[0] = (int)KS_EXTRA;
5054 key_name[1] = (int)KE_IGNORE;
5055 *slen = i + 1 + (tp[i] == ESC);
5056 break;
5057 }
5058 }
5059 else
5060 {
5061 // Probably the cursor shape response. Make sure that "i"
5062 // is equal to "len" when there are not sufficient
5063 // characters.
5064 for (i = j + 3; i < len; ++i)
5065 {
5066 if (i - j == 3 && !isdigit(tp[i]))
5067 break;
5068 if (i - j == 4 && tp[i] != ' ')
5069 break;
5070 if (i - j == 5 && tp[i] != 'q')
5071 break;
5072 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5073 break;
5074 if ((i - j == 6 && tp[i] == STERM)
5075 || (i - j == 7 && tp[i] == '\\'))
5076 {
5077 int number = argp[3] - '0';
5078
5079 // 0, 1 = block blink, 2 = block
5080 // 3 = underline blink, 4 = underline
5081 // 5 = vertical bar blink, 6 = vertical bar
5082 number = number == 0 ? 1 : number;
5083 initial_cursor_shape = (number + 1) / 2;
5084 // The blink flag is actually inverted, compared to
5085 // the value set with T_SH.
5086 initial_cursor_shape_blink =
5087 (number & 1) ? FALSE : TRUE;
5088 rcs_status.tr_progress = STATUS_GOT;
5089 LOG_TR(("Received cursor shape response: %s", tp));
5090
5091 key_name[0] = (int)KS_EXTRA;
5092 key_name[1] = (int)KE_IGNORE;
5093 *slen = i + 1;
5094# ifdef FEAT_EVAL
5095 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5096# endif
5097 break;
5098 }
5099 }
5100 }
5101
5102 if (i == len)
5103 {
5104 // These codes arrive many together, each code can be
5105 // truncated at any point.
5106 LOG_TR(("not enough characters for XT"));
5107 return FAIL;
5108 }
5109 return OK;
5110}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005111#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005112
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 * Check if typebuf.tb_buf[] contains a terminal key code.
5115 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005116 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005118 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 * With a match, the match is removed, the replacement code is inserted in
5120 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5121 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005122 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5123 * "buflen" is then the length of the string in buf[] and is updated for
5124 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 */
5126 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005127check_termcode(
5128 int max_offset,
5129 char_u *buf,
5130 int bufsize,
5131 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
5133 char_u *tp;
5134 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005135 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005136 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005138 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 int offset;
5140 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005141 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005142 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005143 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005144 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 char_u string[MAX_KEY_CODE_LEN + 1];
5146 int i, j;
5147 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149
5150 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5151
5152 /*
5153 * Speed up the checks for terminal codes by gathering all first bytes
5154 * used in termleader[]. Often this is just a single <Esc>.
5155 */
5156 if (need_gather)
5157 gather_termleader();
5158
5159 /*
5160 * Check at several positions in typebuf.tb_buf[], to catch something like
5161 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5162 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005163 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 * This is used often, KEEP IT FAST!
5165 */
5166 for (offset = 0; offset < max_offset; ++offset)
5167 {
5168 if (buf == NULL)
5169 {
5170 if (offset >= typebuf.tb_len)
5171 break;
5172 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005173 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
5175 else
5176 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005177 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 break;
5179 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005180 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182
5183 /*
5184 * Don't check characters after K_SPECIAL, those are already
5185 * translated terminal chars (avoid translating ~@^Hx).
5186 */
5187 if (*tp == K_SPECIAL)
5188 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005189 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 continue;
5191 }
5192
5193 /*
5194 * Skip this position if the character does not appear as the first
5195 * character in term_strings. This speeds up a lot, since most
5196 * termcodes start with the same character (ESC or CSI).
5197 */
5198 i = *tp;
5199 for (p = termleader; *p && *p != i; ++p)
5200 ;
5201 if (*p == NUL)
5202 continue;
5203
5204 /*
5205 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5206 * are in Insert mode.
5207 */
5208 if (*tp == ESC && !p_ek && (State & INSERT))
5209 continue;
5210
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005211 key_name[0] = NUL; // no key name found yet
5212 key_name[1] = NUL; // no key name found yet
5213 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
5215#ifdef FEAT_GUI
5216 if (gui.in_use)
5217 {
5218 /*
5219 * GUI special key codes are all of the form [CSI xx].
5220 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005221 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 {
5223 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005224 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 slen = 3;
5226 key_name[0] = tp[1];
5227 key_name[1] = tp[2];
5228 }
5229 }
5230 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005231#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
5233 for (idx = 0; idx < tc_len; ++idx)
5234 {
5235 /*
5236 * Ignore the entry if we are not at the start of
5237 * typebuf.tb_buf[]
5238 * and there are not enough characters to make a match.
5239 * But only when the 'K' flag is in 'cpoptions'.
5240 */
5241 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005242 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 if (cpo_koffset && offset && len < slen)
5244 continue;
5245 if (STRNCMP(termcodes[idx].code, tp,
5246 (size_t)(slen > len ? len : slen)) == 0)
5247 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005248 if (len < slen) // got a partial sequence
5249 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250
5251 /*
5252 * When found a keypad key, check if there is another key
5253 * that matches and use that one. This makes <Home> to be
5254 * found instead of <kHome> when they produce the same
5255 * key code.
5256 */
5257 if (termcodes[idx].name[0] == 'K'
5258 && VIM_ISDIGIT(termcodes[idx].name[1]))
5259 {
5260 for (j = idx + 1; j < tc_len; ++j)
5261 if (termcodes[j].len == slen &&
5262 STRNCMP(termcodes[idx].code,
5263 termcodes[j].code, slen) == 0)
5264 {
5265 idx = j;
5266 break;
5267 }
5268 }
5269
5270 key_name[0] = termcodes[idx].name[0];
5271 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 break;
5273 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005274
5275 /*
5276 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005277 * <Esc>[123;*X (modslen == slen - 3)
5278 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5279 * When there is a modifier the * matches a number.
5280 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005281 */
5282 if (termcodes[idx].modlen > 0)
5283 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005284 modslen = termcodes[idx].modlen;
5285 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005286 continue;
5287 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005288 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005289 {
5290 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005291
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005292 if (len <= modslen) // got a partial sequence
5293 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005294
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005295 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005296 slen = modslen + 1; // no modifiers
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005297 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005298 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005299 else
5300 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005301 // Skip over the digits, the final char must
5302 // follow. URXVT can use a negative value, thus
5303 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005304 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005305 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005306 ;
5307 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005308 if (len < j) // got a partial sequence
5309 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005310 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005311 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005312
Bram Moolenaara529ce02017-06-22 22:37:57 +02005313 modifiers_start = tp + slen - 2;
5314
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005315 // Match! Convert modifier bits.
5316 n = atoi((char *)modifiers_start);
5317 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005318
5319 slen = j;
5320 }
5321 key_name[0] = termcodes[idx].name[0];
5322 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005323 break;
5324 }
5325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
5327 }
5328
5329#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005330 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005331 // Mouse codes of DEC and pterm start with <ESC>[. When
5332 // detecting the start of these mouse codes they might as well be
5333 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005334# ifdef FEAT_MOUSE_DEC
5335 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005336# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005337# ifdef FEAT_MOUSE_PTERM
5338 || key_name[0] == KS_PTERM_MOUSE
5339# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005340 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005342 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5343
5344 /*
5345 * Check for responses from the terminal starting with {lead}:
5346 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005347 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005348 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005349 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005350 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005351 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005352 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005353 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005354 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005355 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005356 * - window position reply: {lead}3;{x};{y}t
5357 *
5358 * - key with modifiers when modifyOtherKeys is enabled:
5359 * {lead}27;{modifier};{key}~
5360 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005361 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005362 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005363 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005364 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005366 int resp = handle_csi(tp, len, argp, offset, buf,
5367 bufsize, buflen, key_name, &slen);
5368 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005369 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005370# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005371 if (resp == -1)
5372 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005373# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005374 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005375 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005376 }
5377
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005378 // Check for fore/background color response from the terminal,
5379 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005380 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005381 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5382 || tp[0] == OSC))
5383 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005384 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005385 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
5387
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005388 // Check for key code response from xterm,
5389 // starting with <Esc>P or DCS
Bram Moolenaarafd78262019-05-10 23:10:31 +02005390 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005391 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 || tp[0] == DCS))
5393 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005394 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005395 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 }
5397 }
5398#endif
5399
5400 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005401 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005403 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005405#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 /*
5407 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5408 * so that we know which window to scroll later.
5409 */
5410 if (gui.in_use
5411 && key_name[0] == (int)KS_EXTRA
5412 && (key_name[1] == (int)KE_X1MOUSE
5413 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005414 || key_name[1] == (int)KE_MOUSELEFT
5415 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 || key_name[1] == (int)KE_MOUSEDOWN
5417 || key_name[1] == (int)KE_MOUSEUP))
5418 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005419 char_u bytes[6];
5420 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5421
5422 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 return -1;
5424 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5425 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5426 slen += num_bytes;
5427 }
5428 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 /*
5431 * If it is a mouse click, get the coordinates.
5432 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005433 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005434#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005435 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005436#endif
5437#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005438 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005439#endif
5440#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005441 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005442#endif
5443#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005444 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005445#endif
5446#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005447 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005448#endif
5449#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005450 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005451#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005452 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005453 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005455 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5456 &modifiers) == -1)
5457 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459
5460#ifdef FEAT_GUI
5461 /*
5462 * If using the GUI, then we get menu and scrollbar events.
5463 *
5464 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5465 * four bytes which are to be taken as a pointer to the vimmenu_T
5466 * structure.
5467 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005468 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005469 * is one byte with the tab index.
5470 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5472 * by one byte representing the scrollbar number, and then four bytes
5473 * representing a long_u which is the new value of the scrollbar.
5474 *
5475 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5476 * KE_FILLER followed by four bytes representing a long_u which is the
5477 * new value of the scrollbar.
5478 */
5479# ifdef FEAT_MENU
5480 else if (key_name[0] == (int)KS_MENU)
5481 {
5482 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005483 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 if (num_bytes == -1)
5486 return -1;
5487 current_menu = (vimmenu_T *)val;
5488 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005489
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005490 // The menu may have been deleted right after it was used, check
5491 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005492 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5493 {
5494 key_name[0] = KS_EXTRA;
5495 key_name[1] = (int)KE_IGNORE;
5496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 }
5498# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005499# ifdef FEAT_GUI_TABLINE
5500 else if (key_name[0] == (int)KS_TABLINE)
5501 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005502 // Selecting tabline tab or using its menu.
5503 char_u bytes[6];
5504 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5505
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005506 if (num_bytes == -1)
5507 return -1;
5508 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005509 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005510 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005511 slen += num_bytes;
5512 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005513 else if (key_name[0] == (int)KS_TABMENU)
5514 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005515 // Selecting tabline tab or using its menu.
5516 char_u bytes[6];
5517 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5518
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005519 if (num_bytes == -1)
5520 return -1;
5521 current_tab = (int)bytes[0];
5522 current_tabmenu = (int)bytes[1];
5523 slen += num_bytes;
5524 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526# ifndef USE_ON_FLY_SCROLL
5527 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5528 {
5529 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005530 char_u bytes[6];
5531 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005533 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 j = 0;
5535 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5536 && tp[j + 2] != NUL; ++i)
5537 {
5538 j += 3;
5539 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5540 if (num_bytes == -1)
5541 break;
5542 if (i == 0)
5543 current_scrollbar = (int)bytes[0];
5544 else if (current_scrollbar != (int)bytes[0])
5545 break;
5546 j += num_bytes;
5547 num_bytes = get_long_from_buf(tp + j, &val);
5548 if (num_bytes == -1)
5549 break;
5550 scrollbar_value = val;
5551 j += num_bytes;
5552 slen = j;
5553 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005554 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 return -1;
5556 }
5557 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5558 {
5559 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005560 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005562 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 j = 0;
5564 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5565 && tp[j + 2] != NUL; ++i)
5566 {
5567 j += 3;
5568 num_bytes = get_long_from_buf(tp + j, &val);
5569 if (num_bytes == -1)
5570 break;
5571 scrollbar_value = val;
5572 j += num_bytes;
5573 slen = j;
5574 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005575 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 return -1;
5577 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005578# endif // !USE_ON_FLY_SCROLL
5579#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005581 /*
5582 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5583 */
5584 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5585
5586 /*
5587 * Add any modifier codes to our string.
5588 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005589 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005590
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005591 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005592 key_name[0] = KEY2TERMCAP0(key);
5593 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005595 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005596 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005597 if (has_mbyte)
5598 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5599 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005600 string[new_slen++] = key_name[1];
5601 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005602 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5603 && key_name[1] == KE_IGNORE)
5604 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005605 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5606 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005607 retval = KEYLEN_REMOVED;
5608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 else
5610 {
5611 string[new_slen++] = K_SPECIAL;
5612 string[new_slen++] = key_name[0];
5613 string[new_slen++] = key_name[1];
5614 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005615 if (put_string_in_typebuf(offset, slen, string, new_slen,
5616 buf, bufsize, buflen) == FAIL)
5617 return -1;
5618 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 }
5620
Bram Moolenaar2951b772013-07-03 12:45:31 +02005621#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005622 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005623#endif
5624
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005625 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626}
5627
Bram Moolenaar9377df32017-10-15 13:22:01 +02005628#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005629/*
5630 * Get the text foreground color, if known.
5631 */
5632 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005633term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005634{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005635 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005636 {
5637 *r = fg_r;
5638 *g = fg_g;
5639 *b = fg_b;
5640 }
5641}
5642
5643/*
5644 * Get the text background color, if known.
5645 */
5646 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005647term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005648{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005649 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005650 {
5651 *r = bg_r;
5652 *g = bg_g;
5653 *b = bg_b;
5654 }
5655}
5656#endif
5657
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658/*
5659 * Replace any terminal code strings in from[] with the equivalent internal
5660 * vim representation. This is used for the "from" and "to" part of a
5661 * mapping, and the "to" part of a menu command.
5662 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5663 * '<'.
5664 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5665 *
5666 * The replacement is done in result[] and finally copied into allocated
5667 * memory. If this all works well *bufp is set to the allocated memory and a
5668 * pointer to it is returned. If something fails *bufp is set to NULL and from
5669 * is returned.
5670 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005671 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5672 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5673 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5674 * used instead of a CTRL-V.
5675 *
5676 * Flags:
5677 * REPTERM_FROM_PART see above
5678 * REPTERM_DO_LT also translate <lt>
5679 * REPTERM_SPECIAL always accept <key> notation
5680 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5681 *
5682 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5683 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005685 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005686replace_termcodes(
5687 char_u *from,
5688 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005689 int flags,
5690 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691{
5692 int i;
5693 int slen;
5694 int key;
5695 int dlen = 0;
5696 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005697 int do_backslash; // backslash is a special character
5698 int do_special; // recognize <> key codes
5699 int do_key_code; // recognize raw key codes
5700 char_u *result; // buffer for resulting string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701
5702 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02005703 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
5704 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5706
5707 /*
5708 * Allocate space for the translation. Worst case a single character is
5709 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5710 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005711 result = alloc(STRLEN(from) * 6 + 1);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005712 if (result == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
5714 *bufp = NULL;
5715 return from;
5716 }
5717
5718 src = from;
5719
5720 /*
5721 * Check for #n at start only: function key n
5722 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005723 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 {
5725 result[dlen++] = K_SPECIAL;
5726 result[dlen++] = 'k';
5727 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005728 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005730 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 src += 2;
5732 }
5733
5734 /*
5735 * Copy each byte from *from to result[dlen]
5736 */
5737 while (*src != NUL)
5738 {
5739 /*
5740 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005741 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005743 if (do_special && ((flags & REPTERM_DO_LT)
5744 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
5746#ifdef FEAT_EVAL
5747 /*
5748 * Replace <SID> by K_SNR <script-nr> _.
5749 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5750 */
5751 if (STRNICMP(src, "<SID>", 5) == 0)
5752 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005753 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005754 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 else
5756 {
5757 src += 5;
5758 result[dlen++] = K_SPECIAL;
5759 result[dlen++] = (int)KS_EXTRA;
5760 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005761 sprintf((char *)result + dlen, "%ld",
5762 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 dlen += (int)STRLEN(result + dlen);
5764 result[dlen++] = '_';
5765 continue;
5766 }
5767 }
5768#endif
5769
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005770 slen = trans_special(&src, result + dlen, FSK_KEYCODE
5771 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
5772 did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 if (slen)
5774 {
5775 dlen += slen;
5776 continue;
5777 }
5778 }
5779
5780 /*
5781 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5782 * Note that this is also checked after replacing the <> form.
5783 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5784 * it could be a character in the file.
5785 */
5786 if (do_key_code)
5787 {
5788 i = find_term_bykeys(src);
5789 if (i >= 0)
5790 {
5791 result[dlen++] = K_SPECIAL;
5792 result[dlen++] = termcodes[i].name[0];
5793 result[dlen++] = termcodes[i].name[1];
5794 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005795 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 continue;
5797 }
5798 }
5799
5800#ifdef FEAT_EVAL
5801 if (do_special)
5802 {
5803 char_u *p, *s, len;
5804
5805 /*
5806 * Replace <Leader> by the value of "mapleader".
5807 * Replace <LocalLeader> by the value of "maplocalleader".
5808 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5809 */
5810 if (STRNICMP(src, "<Leader>", 8) == 0)
5811 {
5812 len = 8;
5813 p = get_var_value((char_u *)"g:mapleader");
5814 }
5815 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5816 {
5817 len = 13;
5818 p = get_var_value((char_u *)"g:maplocalleader");
5819 }
5820 else
5821 {
5822 len = 0;
5823 p = NULL;
5824 }
5825 if (len != 0)
5826 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005827 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5829 s = (char_u *)"\\";
5830 else
5831 s = p;
5832 while (*s != NUL)
5833 result[dlen++] = *s++;
5834 src += len;
5835 continue;
5836 }
5837 }
5838#endif
5839
5840 /*
5841 * Remove CTRL-V and ignore the next character.
5842 * For "from" side the CTRL-V at the end is included, for the "to"
5843 * part it is removed.
5844 * If 'cpoptions' does not contain 'B', also accept a backslash.
5845 */
5846 key = *src;
5847 if (key == Ctrl_V || (do_backslash && key == '\\'))
5848 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005849 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 if (*src == NUL)
5851 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02005852 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 result[dlen++] = key;
5854 break;
5855 }
5856 }
5857
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005858 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005859 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 {
5861 /*
5862 * If the character is K_SPECIAL, replace it with K_SPECIAL
5863 * KS_SPECIAL KE_FILLER.
5864 * If compiled with the GUI replace CSI with K_CSI.
5865 */
5866 if (*src == K_SPECIAL)
5867 {
5868 result[dlen++] = K_SPECIAL;
5869 result[dlen++] = KS_SPECIAL;
5870 result[dlen++] = KE_FILLER;
5871 }
5872# ifdef FEAT_GUI
5873 else if (*src == CSI)
5874 {
5875 result[dlen++] = K_SPECIAL;
5876 result[dlen++] = KS_EXTRA;
5877 result[dlen++] = (int)KE_CSI;
5878 }
5879# endif
5880 else
5881 result[dlen++] = *src;
5882 ++src;
5883 }
5884 }
5885 result[dlen] = NUL;
5886
5887 /*
5888 * Copy the new string to allocated memory.
5889 * If this fails, just return from.
5890 */
5891 if ((*bufp = vim_strsave(result)) != NULL)
5892 from = *bufp;
5893 vim_free(result);
5894 return from;
5895}
5896
5897/*
5898 * Find a termcode with keys 'src' (must be NUL terminated).
5899 * Return the index in termcodes[], or -1 if not found.
5900 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005901 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005902find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903{
5904 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005905 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906
5907 for (i = 0; i < tc_len; ++i)
5908 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005909 if (slen == termcodes[i].len
5910 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 return i;
5912 }
5913 return -1;
5914}
5915
5916/*
5917 * Gather the first characters in the terminal key codes into a string.
5918 * Used to speed up check_termcode().
5919 */
5920 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005921gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922{
5923 int i;
5924 int len = 0;
5925
5926#ifdef FEAT_GUI
5927 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005928 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929#endif
5930#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005931 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005932 termleader[len++] = DCS; // the termcode response starts with DCS
5933 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934#endif
5935 termleader[len] = NUL;
5936
5937 for (i = 0; i < tc_len; ++i)
5938 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5939 {
5940 termleader[len++] = termcodes[i].code[0];
5941 termleader[len] = NUL;
5942 }
5943
5944 need_gather = FALSE;
5945}
5946
5947/*
5948 * Show all termcodes (for ":set termcap")
5949 * This code looks a lot like showoptions(), but is different.
5950 */
5951 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005952show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953{
5954 int col;
5955 int *items;
5956 int item_count;
5957 int run;
5958 int row, rows;
5959 int cols;
5960 int i;
5961 int len;
5962
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005963#define INC3 27 // try to make three columns
5964#define INC2 40 // try to make two columns
5965#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005967 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005969 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 if (items == NULL)
5971 return;
5972
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005973 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01005974 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975
5976 /*
5977 * do the loop two times:
5978 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005979 * 2. display the medium items (medium length strings)
5980 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005982 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 {
5984 /*
5985 * collect the items in items[]
5986 */
5987 item_count = 0;
5988 for (i = 0; i < tc_len; i++)
5989 {
5990 len = show_one_termcode(termcodes[i].name,
5991 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005992 if (len <= INC3 - GAP ? run == 1
5993 : len <= INC2 - GAP ? run == 2
5994 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 items[item_count++] = i;
5996 }
5997
5998 /*
5999 * display the items
6000 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006001 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006003 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 if (cols == 0)
6005 cols = 1;
6006 rows = (item_count + cols - 1) / cols;
6007 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006008 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 rows = item_count;
6010 for (row = 0; row < rows && !got_int; ++row)
6011 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006012 msg_putchar('\n'); // go to next line
6013 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 break;
6015 col = 0;
6016 for (i = row; i < item_count; i += rows)
6017 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006018 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 show_one_termcode(termcodes[items[i]].name,
6020 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006021 if (run == 2)
6022 col += INC2;
6023 else
6024 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 }
6026 out_flush();
6027 ui_breakcheck();
6028 }
6029 }
6030 vim_free(items);
6031}
6032
6033/*
6034 * Show one termcode entry.
6035 * Output goes into IObuff[]
6036 */
6037 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006038show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039{
6040 char_u *p;
6041 int len;
6042
6043 if (name[0] > '~')
6044 {
6045 IObuff[0] = ' ';
6046 IObuff[1] = ' ';
6047 IObuff[2] = ' ';
6048 IObuff[3] = ' ';
6049 }
6050 else
6051 {
6052 IObuff[0] = 't';
6053 IObuff[1] = '_';
6054 IObuff[2] = name[0];
6055 IObuff[3] = name[1];
6056 }
6057 IObuff[4] = ' ';
6058
6059 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6060 if (p[1] != 't')
6061 STRCPY(IObuff + 5, p);
6062 else
6063 IObuff[5] = NUL;
6064 len = (int)STRLEN(IObuff);
6065 do
6066 IObuff[len++] = ' ';
6067 while (len < 17);
6068 IObuff[len] = NUL;
6069 if (code == NULL)
6070 len += 4;
6071 else
6072 len += vim_strsize(code);
6073
6074 if (printit)
6075 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006076 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006078 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 else
6080 msg_outtrans(code);
6081 }
6082 return len;
6083}
6084
6085#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6086/*
6087 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6088 * termcap codes from the terminal itself.
6089 * We get them one by one to avoid a very long response string.
6090 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006091static int xt_index_in = 0;
6092static int xt_index_out = 0;
6093
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006095req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096{
6097 xt_index_out = 0;
6098 xt_index_in = 0;
6099 req_more_codes_from_term();
6100}
6101
6102 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006103req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 char buf[11];
6106 int old_idx = xt_index_out;
6107
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006108 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 if (exiting)
6110 return;
6111
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006112 // Send up to 10 more requests out than we received. Avoid sending too
6113 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6115 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006116 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006117
Bram Moolenaarb255b902018-04-24 21:40:10 +02006118 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6119 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 out_str_nf((char_u *)buf);
6121 ++xt_index_out;
6122 }
6123
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006124 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 if (xt_index_out != old_idx)
6126 out_flush();
6127}
6128
6129/*
6130 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6131 * A "0" instead of the "1" indicates a code that isn't supported.
6132 * Both <name> and <string> are encoded in hex.
6133 * "code" points to the "0" or "1".
6134 */
6135 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006136got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137{
6138#define XT_LEN 100
6139 char_u name[3];
6140 char_u str[XT_LEN];
6141 int i;
6142 int j = 0;
6143 int c;
6144
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006145 // A '1' means the code is supported, a '0' means it isn't.
6146 // When half the length is > XT_LEN we can't use it.
6147 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6149 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006150 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 name[0] = hexhex2nr(code + 3);
6152 name[1] = hexhex2nr(code + 5);
6153 name[2] = NUL;
6154 for (i = 0; key_names[i] != NULL; ++i)
6155 {
6156 if (STRCMP(key_names[i], name) == 0)
6157 {
6158 xt_index_in = i;
6159 break;
6160 }
6161 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006162
Bram Moolenaarb255b902018-04-24 21:40:10 +02006163 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6164
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (key_names[i] != NULL)
6166 {
6167 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6168 str[j++] = c;
6169 str[j] = NUL;
6170 if (name[0] == 'C' && name[1] == 'o')
6171 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006172 // Color count is not a key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006174 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 }
6176 else
6177 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006178 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 i = find_term_bykeys(str);
6180 if (i >= 0)
6181 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006182 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 }
6184 }
6185 }
6186
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006187 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 ++xt_index_in;
6189 req_more_codes_from_term();
6190}
6191
6192/*
6193 * Check if there are any unanswered requests and deal with them.
6194 * This is called before starting an external program or getting direct
6195 * keyboard input. We don't want responses to be send to that program or
6196 * handled as typed text.
6197 */
6198 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006199check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200{
6201 int c;
6202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006203 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6205 return;
6206
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006207 // Vgetc() will check for and handle any response.
6208 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 ++no_mapping;
6210 ++allow_keys;
6211 for (;;)
6212 {
6213 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006214 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 break;
6216
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006217 // If a response is recognized it's replaced with K_IGNORE, must read
6218 // it from the input stream. If there is no K_IGNORE we can't do
6219 // anything, break here (there might be some responses further on, but
6220 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 if (c != K_SPECIAL && c != K_IGNORE)
6222 break;
6223 c = vgetc();
6224 if (c != K_IGNORE)
6225 {
6226 vungetc(c);
6227 break;
6228 }
6229 }
6230 --no_mapping;
6231 --allow_keys;
6232}
6233#endif
6234
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006235#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236static char ksme_str[20];
6237static char ksmr_str[20];
6238static char ksmd_str[20];
6239
6240/*
6241 * For Win32 console: update termcap codes for existing console attributes.
6242 */
6243 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006244update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245{
6246 struct builtin_term *p;
6247
6248 p = find_builtin_term(DEFAULT_TERM);
6249 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6250 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006251 attr | 0x08); // FOREGROUND_INTENSITY
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6253 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6254
6255 while (p->bt_string != NULL)
6256 {
6257 if (p->bt_entry == (int)KS_ME)
6258 p->bt_string = &ksme_str[0];
6259 else if (p->bt_entry == (int)KS_MR)
6260 p->bt_string = &ksmr_str[0];
6261 else if (p->bt_entry == (int)KS_MD)
6262 p->bt_string = &ksmd_str[0];
6263 ++p;
6264 }
6265}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006266
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006267# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006268# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006269struct ks_tbl_s
6270{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006271 int code; // value of KS_
6272 char *vtp; // code in vtp mode
6273 char *vtp2; // code in vtp2 mode
6274 char buf[KSSIZE]; // save buffer in non-vtp mode
6275 char vbuf[KSSIZE]; // save buffer in vtp mode
6276 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6277 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006278};
6279
6280static struct ks_tbl_s ks_tbl[] =
6281{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006282 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6283 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6284 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6285 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6286 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6287 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6288 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6289 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6290 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006291# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006292 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6293 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006294 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6295 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006296# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006297 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6298 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006299 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6300 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006301# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006302 {(int)KS_CCO, "256", "256"}, // colors
6303 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006304};
6305
6306 static struct builtin_term *
6307find_first_tcap(
6308 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006309 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006310{
6311 struct builtin_term *p;
6312
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006313 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006314 if (p->bt_entry == code)
6315 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006316 return NULL;
6317}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006318# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006319
6320/*
6321 * For Win32 console: replace the sequence immediately after termguicolors.
6322 */
6323 void
6324swap_tcap(void)
6325{
6326# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006327 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006328 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006329 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006330 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006331 int mode;
6332 enum
6333 {
6334 CMODEINDEX,
6335 CMODE24,
6336 CMODE256
6337 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006338
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006339 // buffer initialization
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006340 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006341 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006342 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006343 {
6344 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006345 if (bt != NULL)
6346 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006347 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6348 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6349 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6350
6351 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6352 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006353 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006354 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006355 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006356 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006357 }
6358
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006359 if (p_tgc)
6360 mode = CMODE24;
6361 else if (t_colors >= 256)
6362 mode = CMODE256;
6363 else
6364 mode = CMODEINDEX;
6365
6366 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006367 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006368 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6369 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006370 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006371 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006372 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006373 case CMODEINDEX:
6374 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6375 break;
6376 case CMODE24:
6377 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6378 break;
6379 default:
6380 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006381 }
6382 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006383 }
6384
6385 if (mode != curr_mode)
6386 {
6387 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006388 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006389 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6390 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006391 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006392 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006393 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006394 case CMODEINDEX:
6395 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6396 break;
6397 case CMODE24:
6398 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6399 break;
6400 default:
6401 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006402 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006403 }
6404 }
6405
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006406 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006407 }
6408# endif
6409}
6410
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006412
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006413#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006414 static int
6415hex_digit(int c)
6416{
6417 if (isdigit(c))
6418 return c - '0';
6419 c = TOLOWER_ASC(c);
6420 if (c >= 'a' && c <= 'f')
6421 return c - 'a' + 10;
6422 return 0x1ffffff;
6423}
6424
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006425# ifdef VIMDLL
6426 static guicolor_T
6427gui_adjust_rgb(guicolor_T c)
6428{
6429 if (gui.in_use)
6430 return c;
6431 else
6432 return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff);
6433}
6434# else
6435# define gui_adjust_rgb(c) (c)
6436# endif
6437
Bram Moolenaarab302212016-04-26 20:59:29 +02006438 guicolor_T
6439gui_get_color_cmn(char_u *name)
6440{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006441 // On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6442 // values as used by the MS-Windows GDI api. It should be used only for
6443 // MS-Windows GDI builds.
Bram Moolenaar4f974752019-02-17 17:44:42 +01006444# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar28726652017-01-06 18:16:19 +01006445# undef RGB
6446# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006447# ifndef RGB
6448# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6449# endif
6450# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006451 FILE *fd;
6452 char line[LINE_LEN];
6453 char_u *fname;
6454 int r, g, b, i;
6455 guicolor_T color;
6456
6457 struct rgbcolor_table_S {
6458 char_u *color_name;
6459 guicolor_T color;
6460 };
6461
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006462 // Only non X11 colors (not present in rgb.txt) and colors in
6463 // color_names[], useful when $VIMRUNTIME is not found,.
Bram Moolenaarab302212016-04-26 20:59:29 +02006464 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006465 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6466 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6467 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6468 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6469 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6470 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6471 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6472 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6473 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6474 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6475 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006476 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, // No X11
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006477 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006478 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6479 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006480 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006481 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006482 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006483 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6484 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6485 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6486 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6487 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006488 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, // No X11
6489 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, // No X11
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006490 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6491 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006492 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006493 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006494 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6495 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006496 };
6497
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006498 static struct rgbcolor_table_S *colornames_table;
6499 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006500
6501 if (name[0] == '#' && STRLEN(name) == 7)
6502 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006503 // Name is in "#rrggbb" format
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006504 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006505 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6506 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6507 if (color > 0xffffff)
6508 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006509 return gui_adjust_rgb(color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006510 }
6511
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006512 // Check if the name is one of the colors we know
Bram Moolenaarab302212016-04-26 20:59:29 +02006513 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6514 if (STRICMP(name, rgb_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006515 return gui_adjust_rgb(rgb_table[i].color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006516
6517 /*
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006518 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
Bram Moolenaarab302212016-04-26 20:59:29 +02006519 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006520 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006521 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006522 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006523
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006524 // colornames_table not yet initialized
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006525 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6526 if (fname == NULL)
6527 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006528
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006529 fd = fopen((char *)fname, "rt");
6530 vim_free(fname);
6531 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006532 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006533 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006534 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006535 size = -1; // don't try again
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006536 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006537 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006538
6539 for (counting = 1; counting >= 0; --counting)
6540 {
6541 if (!counting)
6542 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006543 colornames_table = ALLOC_MULT(struct rgbcolor_table_S, size);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006544 if (colornames_table == NULL)
6545 {
6546 fclose(fd);
6547 return INVALCOLOR;
6548 }
6549 rewind(fd);
6550 }
6551 size = 0;
6552
6553 while (!feof(fd))
6554 {
6555 size_t len;
6556 int pos;
6557
Bram Moolenaar42335f52018-09-13 15:33:43 +02006558 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006559 len = strlen(line);
6560
6561 if (len <= 1 || line[len - 1] != '\n')
6562 continue;
6563
6564 line[len - 1] = '\0';
6565
6566 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6567 if (i != 3)
6568 continue;
6569
6570 if (!counting)
6571 {
6572 char_u *s = vim_strsave((char_u *)line + pos);
6573
6574 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006575 {
6576 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006577 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006578 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006579 colornames_table[size].color_name = s;
6580 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6581 }
6582 size++;
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006583
6584 // The distributed rgb.txt has less than 1000 entries. Limit to
6585 // 10000, just in case the file was messed up.
6586 if (size == 10000)
6587 break;
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006588 }
6589 }
6590 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006591 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006592
6593 for (i = 0; i < size; i++)
6594 if (STRICMP(name, colornames_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006595 return gui_adjust_rgb(colornames_table[i].color);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006596
Bram Moolenaarab302212016-04-26 20:59:29 +02006597 return INVALCOLOR;
6598}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006599
6600 guicolor_T
6601gui_get_rgb_color_cmn(int r, int g, int b)
6602{
6603 guicolor_T color = RGB(r, g, b);
6604
6605 if (color > 0xffffff)
6606 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006607 return gui_adjust_rgb(color);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006608}
Bram Moolenaarab302212016-04-26 20:59:29 +02006609#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006610
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006611#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006612 || defined(PROTO)
6613static int cube_value[] = {
6614 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6615};
6616
6617static int grey_ramp[] = {
6618 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6619 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6620};
6621
Bram Moolenaar9894e392018-05-05 14:29:06 +02006622static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006623// R G B idx
6624 { 0, 0, 0, 1}, // black
6625 {224, 0, 0, 2}, // dark red
6626 { 0, 224, 0, 3}, // dark green
6627 {224, 224, 0, 4}, // dark yellow / brown
6628 { 0, 0, 224, 5}, // dark blue
6629 {224, 0, 224, 6}, // dark magenta
6630 { 0, 224, 224, 7}, // dark cyan
6631 {224, 224, 224, 8}, // light grey
6632
6633 {128, 128, 128, 9}, // dark grey
6634 {255, 64, 64, 10}, // light red
6635 { 64, 255, 64, 11}, // light green
6636 {255, 255, 64, 12}, // yellow
6637 { 64, 64, 255, 13}, // light blue
6638 {255, 64, 255, 14}, // light magenta
6639 { 64, 255, 255, 15}, // light cyan
6640 {255, 255, 255, 16}, // white
6641};
6642
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006643#define ANSI_INDEX_NONE 0
6644
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006645 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006646cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006647{
6648 int idx;
6649
6650 if (nr < 16)
6651 {
6652 *r = ansi_table[nr][0];
6653 *g = ansi_table[nr][1];
6654 *b = ansi_table[nr][2];
6655 *ansi_idx = ansi_table[nr][3];
6656 }
6657 else if (nr < 232)
6658 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006659 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006660 idx = nr - 16;
6661 *r = cube_value[idx / 36 % 6];
6662 *g = cube_value[idx / 6 % 6];
6663 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006664 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006665 }
6666 else if (nr < 256)
6667 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006668 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006669 idx = nr - 232;
6670 *r = grey_ramp[idx];
6671 *g = grey_ramp[idx];
6672 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006673 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006674 }
6675 else
6676 {
6677 *r = 0;
6678 *g = 0;
6679 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006680 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006681 }
6682}
6683#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006684
Bram Moolenaarf4140482020-02-15 23:06:45 +01006685/*
6686 * Replace K_BS by <BS> and K_DEL by <DEL>
6687 */
6688 void
6689term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len)
6690{
6691 int i;
6692 int c;
6693
6694 for (i = ta_len; i < ta_len + len; ++i)
6695 {
6696 if (ta_buf[i] == CSI && len - i > 2)
6697 {
6698 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6699 if (c == K_DEL || c == K_KDEL || c == K_BS)
6700 {
6701 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
6702 (size_t)(len - i - 2));
6703 if (c == K_DEL || c == K_KDEL)
6704 ta_buf[i] = DEL;
6705 else
6706 ta_buf[i] = Ctrl_H;
6707 len -= 2;
6708 }
6709 }
6710 else if (ta_buf[i] == '\r')
6711 ta_buf[i] = '\n';
6712 if (has_mbyte)
6713 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6714 }
6715}