blob: 07acc11b2acbc35208376882a59ea8d47a23e83a [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
784# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
785/*
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")},
808# ifdef __MINT__
809 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
810 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
811 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
812 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
813 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
815 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
816 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
817 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 {K_F4, IF_EB("\033S", ESC_STR "S")},
819 {K_F5, IF_EB("\033T", ESC_STR "T")},
820 {K_F6, IF_EB("\033U", ESC_STR "U")},
821 {K_F7, IF_EB("\033V", ESC_STR "V")},
822 {K_F8, IF_EB("\033W", ESC_STR "W")},
823 {K_F9, IF_EB("\033X", ESC_STR "X")},
824 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
825 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
826 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
827 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
828 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
829 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
830 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
831 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
832 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
833 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
834 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
835 {K_INS, IF_EB("\033I", ESC_STR "I")},
836 {K_HOME, IF_EB("\033E", ESC_STR "E")},
837 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
838 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
839# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 {(int)KS_MS, "y"},
842# endif
843# endif
844
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200845# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200846 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
848 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
849# ifdef TERMINFO
850 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
851# else
852 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
853# endif
854 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
855# ifdef TERMINFO
856 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
857# else
858 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
859# endif
860# ifdef TERMINFO
861 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
862 ESC_STR "[%i%p1%d;%p2%dr")},
863# else
864 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
865# endif
866 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
867 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
868 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
869 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
870 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
871 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
872 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200873 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
874 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 {(int)KS_MS, "y"},
876 {(int)KS_UT, "y"},
877 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200878 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
879 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200880 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200881 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200882# ifdef TERMINFO
883 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
884# else
885 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
886# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200887 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200888 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889# ifdef TERMINFO
890 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
891 ESC_STR "[%i%p1%d;%p2%dH")},
892# else
893 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
894# endif
895 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
896# ifdef TERMINFO
897 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
898# else
899 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
900# endif
901 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
902 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
903# ifdef FEAT_XTERM_SAVE
904 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
Bram Moolenaard02e5082020-02-08 14:22:53 +0100905 {(int)KS_TE, IF_EB("\033[?47l\0338",
906 ESC_STR_nc "[?47l" ESC_STR_nc "8")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907# endif
Bram Moolenaar4b570182019-10-20 19:53:22 +0200908 {(int)KS_CTI, IF_EB("\033[>4;2m", ESC_STR_nc "[>4;2m")},
909 {(int)KS_CTE, IF_EB("\033[>4;m", ESC_STR_nc "[>4;m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
911 {(int)KS_CIE, "\007"},
912 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
913 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200914 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
915 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916# ifdef TERMINFO
917 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
918 ESC_STR "[8;%p1%d;%p2%dt")},
919 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
920 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200921 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922# else
923 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
924 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200925 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926# endif
927 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200928 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200929 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100930 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200931# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100932 // These are printf strings, not terminal codes.
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200933 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
934 {(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 +0200935 {(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 +0200936# endif
Bram Moolenaare023e882020-05-31 16:42:30 +0200937 {(int)KS_CAU, IF_EB("\033[58;5;%dm", ESC_STR "[58;5;%dm")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100938 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
939 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200940 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
941 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
942 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
943 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000944
945 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
946 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
947 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
948 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100949 // An extra set of cursor keys for vt100 mode
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000950 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
951 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
952 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
953 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100954 // An extra set of function keys for vt100 mode
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000955 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
956 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
957 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
958 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000959 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
960 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
961 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
962 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
963 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
964 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
965 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
966 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
967 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
968 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
969 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
970 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000972 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
973 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
974 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
975 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100976 // {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")},
977 // {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000978 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100979 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, // other Home
980 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, // other Home
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000981 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100982 // {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")},
983 // {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000984 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100985 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, // other End
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000986 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000987 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
988 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100989 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, // keypad plus
990 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, // keypad minus
991 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, // keypad /
992 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, // keypad *
993 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, // keypad Enter
994 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, // keypad .
995 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, // keypad 0
996 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, // keypad 1
997 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, // keypad 2
998 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, // keypad 3
999 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, // keypad 4
1000 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, // keypad 5
1001 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, // keypad 6
1002 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, // keypad 7
1003 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, // keypad 8
1004 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, // keypad 9
1005 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, // keypad Del
1006 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, // paste start
1007 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
1009 {BT_EXTRA_KEYS, ""},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001010 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, // F0
1011 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, // F13
1012 // F14 and F15 are missing, because they send the same codes as the undo
1013 // and help key, although they don't work on all keyboards.
1014 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, // F16
1015 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, // F17
1016 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, // F18
1017 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, // F19
1018 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001019
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001020 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, // F21
1021 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, // F22
1022 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, // F23
1023 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, // F24
1024 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, // F25
1025 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, // F26
1026 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, // F27
1027 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, // F28
1028 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, // F29
1029 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001030
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001031 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, // F31
1032 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, // F32
1033 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, // F33
1034 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, // F34
1035 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, // F35
1036 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, // F36
1037 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038# endif
1039
1040# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1041/*
1042 * iris-ansi for Silicon Graphics machines.
1043 */
1044 {(int)KS_NAME, "iris-ansi"},
1045 {(int)KS_CE, "\033[K"},
1046 {(int)KS_CD, "\033[J"},
1047 {(int)KS_AL, "\033[L"},
1048# ifdef TERMINFO
1049 {(int)KS_CAL, "\033[%p1%dL"},
1050# else
1051 {(int)KS_CAL, "\033[%dL"},
1052# endif
1053 {(int)KS_DL, "\033[M"},
1054# ifdef TERMINFO
1055 {(int)KS_CDL, "\033[%p1%dM"},
1056# else
1057 {(int)KS_CDL, "\033[%dM"},
1058# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001059#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060# ifdef TERMINFO
1061 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1062# else
1063 {(int)KS_CS, "\033[%i%d;%dr"},
1064# endif
1065#endif
1066 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001067 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
1068 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 {(int)KS_TI, "\033[=6h"},
1070 {(int)KS_TE, "\033[=6l"},
1071 {(int)KS_SE, "\033[21;27m"},
1072 {(int)KS_SO, "\033[1;7m"},
1073 {(int)KS_ME, "\033[m"},
1074 {(int)KS_MR, "\033[7m"},
1075 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001076 {(int)KS_CCO, "8"}, // allow 8 colors
1077 {(int)KS_CZH, "\033[3m"}, // italic mode on
1078 {(int)KS_CZR, "\033[23m"}, // italic mode off
1079 {(int)KS_US, "\033[4m"}, // underline on
1080 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001082 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
1083 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
1084 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
1085 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001087 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
1088 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
1089 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
1090 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001092 {(int)KS_MS, "y"}, // guessed
1093 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {(int)KS_LE, "\b"},
1095# ifdef TERMINFO
1096 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1097# else
1098 {(int)KS_CM, "\033[%i%d;%dH"},
1099# endif
1100 {(int)KS_SR, "\033M"},
1101# ifdef TERMINFO
1102 {(int)KS_CRI, "\033[%p1%dC"},
1103# else
1104 {(int)KS_CRI, "\033[%dC"},
1105# endif
1106 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001107 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001109 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110# ifdef TERMINFO
1111 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1112 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1113# else
1114 {(int)KS_CWS, "\033[203;%d;%d/y"},
1115 {(int)KS_CWP, "\033[205;%d;%d/y"},
1116# endif
1117 {K_UP, "\033[A"},
1118 {K_DOWN, "\033[B"},
1119 {K_LEFT, "\033[D"},
1120 {K_RIGHT, "\033[C"},
1121 {K_S_UP, "\033[161q"},
1122 {K_S_DOWN, "\033[164q"},
1123 {K_S_LEFT, "\033[158q"},
1124 {K_S_RIGHT, "\033[167q"},
1125 {K_F1, "\033[001q"},
1126 {K_F2, "\033[002q"},
1127 {K_F3, "\033[003q"},
1128 {K_F4, "\033[004q"},
1129 {K_F5, "\033[005q"},
1130 {K_F6, "\033[006q"},
1131 {K_F7, "\033[007q"},
1132 {K_F8, "\033[008q"},
1133 {K_F9, "\033[009q"},
1134 {K_F10, "\033[010q"},
1135 {K_F11, "\033[011q"},
1136 {K_F12, "\033[012q"},
1137 {K_S_F1, "\033[013q"},
1138 {K_S_F2, "\033[014q"},
1139 {K_S_F3, "\033[015q"},
1140 {K_S_F4, "\033[016q"},
1141 {K_S_F5, "\033[017q"},
1142 {K_S_F6, "\033[018q"},
1143 {K_S_F7, "\033[019q"},
1144 {K_S_F8, "\033[020q"},
1145 {K_S_F9, "\033[021q"},
1146 {K_S_F10, "\033[022q"},
1147 {K_S_F11, "\033[023q"},
1148 {K_S_F12, "\033[024q"},
1149 {K_INS, "\033[139q"},
1150 {K_HOME, "\033[H"},
1151 {K_END, "\033[146q"},
1152 {K_PAGEUP, "\033[150q"},
1153 {K_PAGEDOWN, "\033[154q"},
1154# endif
1155
1156# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1157/*
1158 * for debugging
1159 */
1160 {(int)KS_NAME, "debug"},
1161 {(int)KS_CE, "[CE]"},
1162 {(int)KS_CD, "[CD]"},
1163 {(int)KS_AL, "[AL]"},
1164# ifdef TERMINFO
1165 {(int)KS_CAL, "[CAL%p1%d]"},
1166# else
1167 {(int)KS_CAL, "[CAL%d]"},
1168# endif
1169 {(int)KS_DL, "[DL]"},
1170# ifdef TERMINFO
1171 {(int)KS_CDL, "[CDL%p1%d]"},
1172# else
1173 {(int)KS_CDL, "[CDL%d]"},
1174# endif
1175# ifdef TERMINFO
1176 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1177# else
1178 {(int)KS_CS, "[%dCS%d]"},
1179# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001180# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001182# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184# endif
1185# ifdef TERMINFO
1186 {(int)KS_CAB, "[CAB%p1%d]"},
1187 {(int)KS_CAF, "[CAF%p1%d]"},
1188 {(int)KS_CSB, "[CSB%p1%d]"},
1189 {(int)KS_CSF, "[CSF%p1%d]"},
1190# else
1191 {(int)KS_CAB, "[CAB%d]"},
1192 {(int)KS_CAF, "[CAF%d]"},
1193 {(int)KS_CSB, "[CSB%d]"},
1194 {(int)KS_CSF, "[CSF%d]"},
1195# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001196 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {(int)KS_OP, "[OP]"},
1198 {(int)KS_LE, "[LE]"},
1199 {(int)KS_CL, "[CL]"},
1200 {(int)KS_VI, "[VI]"},
1201 {(int)KS_VE, "[VE]"},
1202 {(int)KS_VS, "[VS]"},
1203 {(int)KS_ME, "[ME]"},
1204 {(int)KS_MR, "[MR]"},
1205 {(int)KS_MB, "[MB]"},
1206 {(int)KS_MD, "[MD]"},
1207 {(int)KS_SE, "[SE]"},
1208 {(int)KS_SO, "[SO]"},
1209 {(int)KS_UE, "[UE]"},
1210 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001211 {(int)KS_UCE, "[UCE]"},
1212 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001213 {(int)KS_STE, "[STE]"},
1214 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 {(int)KS_MS, "[MS]"},
1216 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001217 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218# ifdef TERMINFO
1219 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1220# else
1221 {(int)KS_CM, "[%dCM%d]"},
1222# endif
1223 {(int)KS_SR, "[SR]"},
1224# ifdef TERMINFO
1225 {(int)KS_CRI, "[CRI%p1%d]"},
1226# else
1227 {(int)KS_CRI, "[CRI%d]"},
1228# endif
1229 {(int)KS_VB, "[VB]"},
1230 {(int)KS_KS, "[KS]"},
1231 {(int)KS_KE, "[KE]"},
1232 {(int)KS_TI, "[TI]"},
1233 {(int)KS_TE, "[TE]"},
1234 {(int)KS_CIS, "[CIS]"},
1235 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001236 {(int)KS_CSC, "[CSC]"},
1237 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {(int)KS_TS, "[TS]"},
1239 {(int)KS_FS, "[FS]"},
1240# ifdef TERMINFO
1241 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1242 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1243# else
1244 {(int)KS_CWS, "[%dCWS%d]"},
1245 {(int)KS_CWP, "[%dCWP%d]"},
1246# endif
1247 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001248 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001249 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001250 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {K_UP, "[KU]"},
1252 {K_DOWN, "[KD]"},
1253 {K_LEFT, "[KL]"},
1254 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001255 {K_XUP, "[xKU]"},
1256 {K_XDOWN, "[xKD]"},
1257 {K_XLEFT, "[xKL]"},
1258 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {K_S_UP, "[S-KU]"},
1260 {K_S_DOWN, "[S-KD]"},
1261 {K_S_LEFT, "[S-KL]"},
1262 {K_C_LEFT, "[C-KL]"},
1263 {K_S_RIGHT, "[S-KR]"},
1264 {K_C_RIGHT, "[C-KR]"},
1265 {K_F1, "[F1]"},
1266 {K_XF1, "[xF1]"},
1267 {K_F2, "[F2]"},
1268 {K_XF2, "[xF2]"},
1269 {K_F3, "[F3]"},
1270 {K_XF3, "[xF3]"},
1271 {K_F4, "[F4]"},
1272 {K_XF4, "[xF4]"},
1273 {K_F5, "[F5]"},
1274 {K_F6, "[F6]"},
1275 {K_F7, "[F7]"},
1276 {K_F8, "[F8]"},
1277 {K_F9, "[F9]"},
1278 {K_F10, "[F10]"},
1279 {K_F11, "[F11]"},
1280 {K_F12, "[F12]"},
1281 {K_S_F1, "[S-F1]"},
1282 {K_S_XF1, "[S-xF1]"},
1283 {K_S_F2, "[S-F2]"},
1284 {K_S_XF2, "[S-xF2]"},
1285 {K_S_F3, "[S-F3]"},
1286 {K_S_XF3, "[S-xF3]"},
1287 {K_S_F4, "[S-F4]"},
1288 {K_S_XF4, "[S-xF4]"},
1289 {K_S_F5, "[S-F5]"},
1290 {K_S_F6, "[S-F6]"},
1291 {K_S_F7, "[S-F7]"},
1292 {K_S_F8, "[S-F8]"},
1293 {K_S_F9, "[S-F9]"},
1294 {K_S_F10, "[S-F10]"},
1295 {K_S_F11, "[S-F11]"},
1296 {K_S_F12, "[S-F12]"},
1297 {K_HELP, "[HELP]"},
1298 {K_UNDO, "[UNDO]"},
1299 {K_BS, "[BS]"},
1300 {K_INS, "[INS]"},
1301 {K_KINS, "[KINS]"},
1302 {K_DEL, "[DEL]"},
1303 {K_KDEL, "[KDEL]"},
1304 {K_HOME, "[HOME]"},
1305 {K_S_HOME, "[C-HOME]"},
1306 {K_C_HOME, "[C-HOME]"},
1307 {K_KHOME, "[KHOME]"},
1308 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001309 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 {K_END, "[END]"},
1311 {K_S_END, "[C-END]"},
1312 {K_C_END, "[C-END]"},
1313 {K_KEND, "[KEND]"},
1314 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001315 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {K_PAGEUP, "[PAGEUP]"},
1317 {K_PAGEDOWN, "[PAGEDOWN]"},
1318 {K_KPAGEUP, "[KPAGEUP]"},
1319 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1320 {K_MOUSE, "[MOUSE]"},
1321 {K_KPLUS, "[KPLUS]"},
1322 {K_KMINUS, "[KMINUS]"},
1323 {K_KDIVIDE, "[KDIVIDE]"},
1324 {K_KMULTIPLY, "[KMULTIPLY]"},
1325 {K_KENTER, "[KENTER]"},
1326 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001327 {K_PS, "[PASTE-START]"},
1328 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {K_K0, "[K0]"},
1330 {K_K1, "[K1]"},
1331 {K_K2, "[K2]"},
1332 {K_K3, "[K3]"},
1333 {K_K4, "[K4]"},
1334 {K_K5, "[K5]"},
1335 {K_K6, "[K6]"},
1336 {K_K7, "[K7]"},
1337 {K_K8, "[K8]"},
1338 {K_K9, "[K9]"},
1339# endif
1340
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001341#endif // NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343/*
1344 * The most minimal terminal: only clear screen and cursor positioning
1345 * Always included.
1346 */
1347 {(int)KS_NAME, "dumb"},
1348 {(int)KS_CL, "\014"},
1349#ifdef TERMINFO
1350 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1351 ESC_STR "[%i%p1%d;%p2%dH")},
1352#else
1353 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1354#endif
1355
1356/*
1357 * end marker
1358 */
1359 {(int)KS_NAME, NULL}
1360
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001361}; // end of builtin_termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001363#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001364 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001365termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001366{
Bram Moolenaarab302212016-04-26 20:59:29 +02001367 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001368}
1369
1370 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001371termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001372{
1373 guicolor_T t;
1374
1375 if (*name == NUL)
1376 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001377 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001378
1379 if (t == INVALCOLOR)
Bram Moolenaar87202262020-05-24 17:23:45 +02001380 semsg(_(e_alloc_color), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001381 return t;
1382}
1383
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001384 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001385termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001386{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001387 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001388}
1389#endif
1390
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391/*
1392 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394#ifdef AMIGA
1395# define DEFAULT_TERM (char_u *)"amiga"
1396#endif
1397
1398#ifdef MSWIN
1399# define DEFAULT_TERM (char_u *)"win32"
1400#endif
1401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#if defined(UNIX) && !defined(__MINT__)
1403# define DEFAULT_TERM (char_u *)"ansi"
1404#endif
1405
1406#ifdef __MINT__
1407# define DEFAULT_TERM (char_u *)"vt52"
1408#endif
1409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410#ifdef VMS
1411# define DEFAULT_TERM (char_u *)"vt320"
1412#endif
1413
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001414#ifdef __HAIKU__
1415# undef DEFAULT_TERM
1416# define DEFAULT_TERM (char_u *)"xterm"
1417#endif
1418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419#ifndef DEFAULT_TERM
1420# define DEFAULT_TERM (char_u *)"dumb"
1421#endif
1422
1423/*
1424 * Term_strings contains currently used terminal output strings.
1425 * It is initialized with the default values by parse_builtin_tcap().
1426 * The values can be changed by setting the option with the same name.
1427 */
1428char_u *(term_strings[(int)KS_LAST + 1]);
1429
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001430static int need_gather = FALSE; // need to fill termleader[]
1431static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001433static int check_for_codes = FALSE; // check for key code response
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001434
1435/*
1436 * Structure and table to store terminal features that can be detected by
1437 * querying the terminal. Either by inspecting the termresponse or a more
1438 * specific request. Besides this there are:
1439 * t_colors - number of colors supported
1440 */
1441typedef struct {
1442 char *tpr_name;
1443 int tpr_set_by_termresponse;
1444 int tpr_status;
1445} termprop_T;
1446
1447// Values for tpr_status.
1448#define TPR_UNKNOWN 'u'
1449#define TPR_YES 'y'
1450#define TPR_NO 'n'
1451#define TPR_MOUSE_XTERM 'x' // use "xterm" for 'ttymouse'
1452#define TPR_MOUSE_XTERM2 '2' // use "xterm2" for 'ttymouse'
1453#define TPR_MOUSE_SGR 's' // use "sgr" for 'ttymouse'
1454
1455// can request the cursor style without messing up the display
1456#define TPR_CURSOR_STYLE 0
1457// can request the cursor blink mode without messing up the display
1458#define TPR_CURSOR_BLINK 1
1459// can set the underline color with t_8u without resetting other colors
1460#define TPR_UNDERLINE_RGB 2
1461// mouse support - TPR_MOUSE_XTERM, TPR_MOUSE_XTERM2 or TPR_MOUSE_SGR
1462#define TPR_MOUSE 3
1463// table size
1464#define TPR_COUNT 4
1465
1466static termprop_T term_props[TPR_COUNT];
1467
1468/*
1469 * Initialize the term_props table.
1470 * When "all" is FALSE only set those that are detected from the version
1471 * response.
1472 */
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001473 void
Bram Moolenaar517f00f2020-06-10 12:15:51 +02001474init_term_props(int all)
1475{
1476 int i;
1477
1478 term_props[TPR_CURSOR_STYLE].tpr_name = "cursor_style";
1479 term_props[TPR_CURSOR_STYLE].tpr_set_by_termresponse = FALSE;
1480 term_props[TPR_CURSOR_BLINK].tpr_name = "cursor_blink_mode";
1481 term_props[TPR_CURSOR_BLINK].tpr_set_by_termresponse = FALSE;
1482 term_props[TPR_UNDERLINE_RGB].tpr_name = "underline_rgb";
1483 term_props[TPR_UNDERLINE_RGB].tpr_set_by_termresponse = TRUE;
1484 term_props[TPR_MOUSE].tpr_name = "mouse";
1485 term_props[TPR_MOUSE].tpr_set_by_termresponse = TRUE;
1486
1487 for (i = 0; i < TPR_COUNT; ++i)
1488 if (all || term_props[i].tpr_set_by_termresponse)
1489 term_props[i].tpr_status = TPR_UNKNOWN;
1490}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491#endif
1492
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001493#if defined(FEAT_EVAL) || defined(PROTO)
1494 void
1495f_terminalprops(typval_T *argvars UNUSED, typval_T *rettv)
1496{
1497# ifdef FEAT_TERMRESPONSE
1498 int i;
1499# endif
1500
1501 if (rettv_dict_alloc(rettv) != OK)
1502 return;
1503# ifdef FEAT_TERMRESPONSE
1504 for (i = 0; i < TPR_COUNT; ++i)
1505 {
1506 char_u value[2];
1507
1508 value[0] = term_props[i].tpr_status;
1509 value[1] = NUL;
1510 dict_add_string(rettv->vval.v_dict, term_props[i].tpr_name, value);
1511 }
1512# endif
1513}
1514#endif
1515
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001517find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519 struct builtin_term *p;
1520
1521 p = builtin_termcaps;
1522 while (p->bt_string != NULL)
1523 {
1524 if (p->bt_entry == (int)KS_NAME)
1525 {
1526#ifdef UNIX
1527 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1528 return p;
1529 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1530 return p;
1531 else
1532#endif
1533#ifdef VMS
1534 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1535 return p;
1536 else
1537#endif
1538 if (STRCMP(term, p->bt_string) == 0)
1539 return p;
1540 }
1541 ++p;
1542 }
1543 return p;
1544}
1545
1546/*
1547 * Parsing of the builtin termcap entries.
1548 * Caller should check if 'name' is a valid builtin term.
1549 * The terminal's name is not set, as this is already done in termcapinit().
1550 */
1551 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001552parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
1554 struct builtin_term *p;
1555 char_u name[2];
1556 int term_8bit;
1557
1558 p = find_builtin_term(term);
1559 term_8bit = term_is_8bit(term);
1560
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001561 // Do not parse if builtin term not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (p->bt_string == NULL)
1563 return;
1564
1565 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1566 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001567 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001569 // Only set the value if it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 if (term_strings[p->bt_entry] == NULL
1571 || term_strings[p->bt_entry] == empty_option)
1572 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001573#ifdef FEAT_EVAL
1574 int opt_idx = -1;
1575#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001576 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1578 {
1579 char_u *s, *t;
1580
1581 s = vim_strsave((char_u *)p->bt_string);
1582 if (s != NULL)
1583 {
1584 for (t = s; *t; ++t)
1585 if (term_7to8bit(t))
1586 {
1587 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001588 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 }
1590 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001591#ifdef FEAT_EVAL
1592 opt_idx =
1593#endif
1594 set_term_option_alloced(
1595 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
1597 }
1598 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001599 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001601#ifdef FEAT_EVAL
1602 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1603#endif
1604 }
1605#ifdef FEAT_EVAL
1606 set_term_option_sctx_idx(NULL, opt_idx);
1607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 }
1610 else
1611 {
1612 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1613 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1614 if (find_termcode(name) == NULL)
1615 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1616 }
1617 }
1618}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620/*
1621 * Set number of colors.
1622 * Store it as a number in t_colors.
1623 * Store it as a string in T_CCO (using nr_colors[]).
1624 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001625 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001626set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001628 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
1630 t_colors = nr;
1631 if (t_colors > 1)
1632 sprintf((char *)nr_colors, "%d", t_colors);
1633 else
1634 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001635 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001637
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001638#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001639/*
1640 * Set the color count to "val" and redraw if it changed.
1641 */
1642 static void
1643may_adjust_color_count(int val)
1644{
1645 if (val != t_colors)
1646 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001647 // Nr of colors changed, initialize highlighting and
1648 // redraw everything. This causes a redraw, which usually
1649 // clears the message. Try keeping the message if it
1650 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001651 set_keep_msg_from_hist();
1652 set_color_count(val);
1653 init_highlight(TRUE, FALSE);
1654# ifdef DEBUG_TERMRESPONSE
1655 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001656 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001657
Bram Moolenaarb255b902018-04-24 21:40:10 +02001658 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001659 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001660# else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001661 redraw_asap(CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001662# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001663 }
1664}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665#endif
1666
1667#ifdef HAVE_TGETENT
1668static char *(key_names[]) =
1669{
Bram Moolenaar87202262020-05-24 17:23:45 +02001670# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001671 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001673# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 "#2", "#4", "%i", "*7",
1676 "k1", "k2", "k3", "k4", "k5", "k6",
1677 "k7", "k8", "k9", "k;", "F1", "F2",
1678 "%1", "&8", "kb", "kI", "kD", "kh",
1679 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1680 NULL
1681};
1682#endif
1683
Bram Moolenaar9289df52018-05-10 14:40:57 +02001684#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001685 static void
1686get_term_entries(int *height, int *width)
1687{
1688 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001689 enum SpecialKey dest; // index in term_strings[]
1690 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001691 } string_names[] =
1692 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1693 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1694 {KS_CL, "cl"}, {KS_CD, "cd"},
1695 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1696 {KS_ME, "me"}, {KS_MR, "mr"},
1697 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1698 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1699 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1700 {KS_STE,"Te"}, {KS_STS,"Ts"},
1701 {KS_CM, "cm"}, {KS_SR, "sr"},
1702 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1703 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001704 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001705 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001706 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1707 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001708 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1709 {KS_VS, "vs"}, {KS_CVS, "VS"},
1710 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1711 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1712 {KS_TS, "ts"}, {KS_FS, "fs"},
1713 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1714 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1715 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001716 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001717 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1718 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001719 {KS_CST, "ST"}, {KS_CRT, "RT"},
1720 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001721 {(enum SpecialKey)0, NULL}
1722 };
1723 int i;
1724 char_u *p;
1725 static char_u tstrbuf[TBUFSZ];
1726 char_u *tp = tstrbuf;
1727
1728 /*
1729 * get output strings
1730 */
1731 for (i = 0; string_names[i].name != NULL; ++i)
1732 {
1733 if (TERM_STR(string_names[i].dest) == NULL
1734 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001735 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001736 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001737#ifdef FEAT_EVAL
1738 set_term_option_sctx_idx(string_names[i].name, -1);
1739#endif
1740 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001741 }
1742
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001743 // tgetflag() returns 1 if the flag is present, 0 if not and
1744 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001745 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1746 T_MS = (char_u *)"y";
1747 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1748 T_XS = (char_u *)"y";
1749 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1750 T_XN = (char_u *)"y";
1751 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1752 T_DB = (char_u *)"y";
1753 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1754 T_DA = (char_u *)"y";
1755 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1756 T_UT = (char_u *)"y";
1757
1758 /*
1759 * get key codes
1760 */
1761 for (i = 0; key_names[i] != NULL; ++i)
1762 if (find_termcode((char_u *)key_names[i]) == NULL)
1763 {
1764 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001765 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001766 if (p != NULL
1767 && (*p != Ctrl_H
1768 || key_names[i][0] != 'k'
1769 || key_names[i][1] != 'l'))
1770 add_termcode((char_u *)key_names[i], p, FALSE);
1771 }
1772
1773 if (*height == 0)
1774 *height = tgetnum("li");
1775 if (*width == 0)
1776 *width = tgetnum("co");
1777
1778 /*
1779 * Get number of colors (if not done already).
1780 */
1781 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001782 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001783 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001784#ifdef FEAT_EVAL
1785 set_term_option_sctx_idx("Co", -1);
1786#endif
1787 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001788
1789# ifndef hpux
1790 BC = (char *)TGETSTR("bc", &tp);
1791 UP = (char *)TGETSTR("up", &tp);
1792 p = TGETSTR("pc", &tp);
1793 if (p)
1794 PC = *p;
1795# endif
1796}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001797#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001798
1799 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001800report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001801{
1802 struct builtin_term *termp;
1803
1804 mch_errmsg("\r\n");
1805 if (error_msg != NULL)
1806 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001807 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001808 mch_errmsg("\r\n");
1809 }
1810 mch_errmsg("'");
1811 mch_errmsg((char *)term);
1812 mch_errmsg(_("' not known. Available builtin terminals are:"));
1813 mch_errmsg("\r\n");
1814 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1815 {
1816 if (termp->bt_entry == (int)KS_NAME)
1817 {
1818#ifdef HAVE_TGETENT
1819 mch_errmsg(" builtin_");
1820#else
1821 mch_errmsg(" ");
1822#endif
1823 mch_errmsg(termp->bt_string);
1824 mch_errmsg("\r\n");
1825 }
1826 }
1827}
1828
1829 static void
1830report_default_term(char_u *term)
1831{
1832 mch_errmsg(_("defaulting to '"));
1833 mch_errmsg((char *)term);
1834 mch_errmsg("'\r\n");
1835 if (emsg_silent == 0)
1836 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001837 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001838 out_flush();
1839 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001840 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001841 }
1842}
1843
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844/*
1845 * Set terminal options for terminal "term".
1846 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1847 *
1848 * While doing this, until ttest(), some options may be NULL, be careful.
1849 */
1850 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001851set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852{
1853 struct builtin_term *termp;
1854#ifdef HAVE_TGETENT
1855 int builtin_first = p_tbi;
1856 int try;
1857 int termcap_cleared = FALSE;
1858#endif
1859 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001860 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 char_u *bs_p, *del_p;
1862
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001863 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001864 if (silent_mode)
1865 return OK;
1866
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001867 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
1869 if (term_is_builtin(term))
1870 {
1871 term += 8;
1872#ifdef HAVE_TGETENT
1873 builtin_first = 1;
1874#endif
1875 }
1876
1877/*
1878 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1879 * If builtin_first is TRUE:
1880 * 0. try builtin termcap
1881 * 1. try external termcap
1882 * 2. if both fail default to a builtin terminal
1883 * If builtin_first is FALSE:
1884 * 1. try external termcap
1885 * 2. try builtin termcap, if both fail default to a builtin terminal
1886 */
1887#ifdef HAVE_TGETENT
1888 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1889 {
1890 /*
1891 * Use external termcap
1892 */
1893 if (try == 1)
1894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
1897 /*
1898 * If the external termcap does not have a matching entry, try the
1899 * builtin ones.
1900 */
1901 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1902 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (!termcap_cleared)
1904 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001905 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 termcap_cleared = TRUE;
1907 }
1908
Bram Moolenaar69e05692018-05-10 14:11:52 +02001909 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 }
1911 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001912 else // try == 0 || try == 2
1913#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 /*
1915 * Use builtin termcap
1916 */
1917 {
1918#ifdef HAVE_TGETENT
1919 /*
1920 * If builtin termcap was already used, there is no need to search
1921 * for the builtin termcap again, quit now.
1922 */
1923 if (try == 2 && builtin_first && termcap_cleared)
1924 break;
1925#endif
1926 /*
1927 * search for 'term' in builtin_termcaps[]
1928 */
1929 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001930 if (termp->bt_string == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
1932#ifdef HAVE_TGETENT
1933 /*
1934 * If try == 0, first try the external termcap. If that is not
1935 * found we'll get back here with try == 2.
1936 * If termcap_cleared is set we used the external termcap,
1937 * don't complain about not finding the term in the builtin
1938 * termcap.
1939 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001940 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001942 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 break;
1944#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001945 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001947 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (starting != NO_SCREEN)
1949 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001950 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 wait_return(TRUE);
1952 return FAIL;
1953 }
1954 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001955 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001956 set_string_option_direct((char_u *)"term", -1, term,
1957 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 display_errors();
1959 }
1960 out_flush();
1961#ifdef HAVE_TGETENT
1962 if (!termcap_cleared)
1963 {
1964#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001965 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966#ifdef HAVE_TGETENT
1967 termcap_cleared = TRUE;
1968 }
1969#endif
1970 parse_builtin_tcap(term);
1971#ifdef FEAT_GUI
1972 if (term_is_gui(term))
1973 {
1974 out_flush();
1975 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001976 // If starting the GUI failed, don't do any of the other
1977 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (!gui.in_use)
1979 return FAIL;
1980#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001981 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982#endif
1983 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001984#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 }
1986#ifdef HAVE_TGETENT
1987 }
1988#endif
1989
1990/*
1991 * special: There is no info in the termcap about whether the cursor
1992 * positioning is relative to the start of the screen or to the start of the
1993 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1994 * relative.
1995 */
1996 if (STRCMP(term, "pcterm") == 0)
1997 T_CCS = (char_u *)"yes";
1998 else
1999 T_CCS = empty_option;
2000
2001#ifdef UNIX
2002/*
2003 * Any "stty" settings override the default for t_kb from the termcap.
2004 * This is in os_unix.c, because it depends a lot on the version of unix that
2005 * is being used.
2006 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
2007 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002008# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02002010# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 get_stty();
2012#endif
2013
2014/*
2015 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
2016 * didn't work, use the default CTRL-H
2017 * The default for t_kD is DEL, unless t_kb is DEL.
2018 * The vim_strsave'd strings are probably lost forever, well it's only two
2019 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
2020 * directly.
2021 */
2022#ifdef FEAT_GUI
2023 if (!gui.in_use)
2024#endif
2025 {
2026 bs_p = find_termcode((char_u *)"kb");
2027 del_p = find_termcode((char_u *)"kD");
2028 if (bs_p == NULL || *bs_p == NUL)
2029 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
2030 if ((del_p == NULL || *del_p == NUL) &&
2031 (bs_p == NULL || *bs_p != DEL))
2032 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
2033 }
2034
2035#if defined(UNIX) || defined(VMS)
2036 term_is_xterm = vim_is_xterm(term);
2037#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002038#ifdef FEAT_TERMRESPONSE
Bram Moolenaar517f00f2020-06-10 12:15:51 +02002039 // Reset terminal properties that are set based on the termresponse, which
2040 // will be sent out soon.
2041 init_term_props(FALSE);
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02002042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002044#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 /*
2046 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
2047 * The termcode for the mouse is added as a side effect in option.c.
2048 */
2049 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02002050 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002052# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00002053 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
2055 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002056 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 else
2058 p = (char_u *)"xterm";
2059 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002064 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
2065 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01002066 reset_option_was_set((char_u *)"ttym");
2067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002069# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002071# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002073 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002075#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002080 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (STRCMP(term, DEFAULT_TERM) != 0)
2082 term_console = FALSE;
2083 else
2084 {
2085 term_console = TRUE;
2086# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002087 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088# endif
2089 }
2090#endif
2091
2092#if defined(UNIX) || defined(VMS)
2093 /*
2094 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2095 */
2096 if (vim_is_fastterm(term))
2097 p_tf = TRUE;
2098#endif
2099#ifdef USE_TERM_CONSOLE
2100 /*
2101 * 'ttyfast' is default on consoles
2102 */
2103 if (term_console)
2104 p_tf = TRUE;
2105#endif
2106
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002107 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002109 full_screen = TRUE; // we can use termcap codes from now on
2110 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002112 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002113 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114#endif
2115
2116 /*
2117 * Initialize the terminal with the appropriate termcap codes.
2118 * Set the mouse and window title if possible.
2119 * Don't do this when starting, need to parse the .vimrc first, because it
2120 * may redefine t_TI etc.
2121 */
2122 if (starting != NO_SCREEN)
2123 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002124 starttermcap(); // may change terminal mode
2125 setmouse(); // may start using the mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126#ifdef FEAT_TITLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002127 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128#endif
2129 }
2130
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002131 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (width <= 0 || height <= 0)
2133 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002134 // termcap failed to report size
2135 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002137#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002138 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002140 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141#endif
2142 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002143 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 if (starting != NO_SCREEN)
2145 {
2146 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002147 scroll_region_reset(); // In case Rows changed
2148 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002151 buf_T *buf;
2152 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154 /*
2155 * Execute the TermChanged autocommands for each buffer that is
2156 * loaded.
2157 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002158 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
2160 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002161 {
2162 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2164 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002165 // restore curwin/curbuf and a few other things
2166 aucmd_restbuf(&aco);
2167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
2171
2172#ifdef FEAT_TERMRESPONSE
2173 may_req_termresponse();
2174#endif
2175
2176 return OK;
2177}
2178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179#ifdef HAVE_TGETENT
2180/*
2181 * Call tgetent()
2182 * Return error message if it fails, NULL if it's OK.
2183 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002184 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002185tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186{
2187 int i;
2188
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002189 // Note: Valgrind may report a leak here, because the library keeps one
2190 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002192 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002194 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195# endif
2196 )
2197 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002198 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2199 // tgetent() with the always existing "dumb" entry to avoid a crash or
2200 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 (void)TGETENT(tbuf, "dumb");
2202
2203 if (i < 0)
2204# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002205 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 if (i == 0)
2207# endif
2208#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002209 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002211 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212#endif
2213 }
2214 return NULL;
2215}
2216
2217/*
2218 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2219 * Fix that here.
2220 */
2221 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002222vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223{
2224 char *p;
2225
2226 p = tgetstr(s, (char **)pp);
2227 if (p == (char *)-1)
2228 p = NULL;
2229 return (char_u *)p;
2230}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002231#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002233#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234/*
2235 * Get Columns and Rows from the termcap. Used after a window signal if the
2236 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2237 * and "li" entries never change. But on some systems this works.
2238 * Errors while getting the entries are ignored.
2239 */
2240 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002241getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002242 long *cp, // pointer to columns
2243 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244{
2245 char_u tbuf[TBUFSZ];
2246
2247 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2248 {
2249 if (*cp == 0)
2250 *cp = tgetnum("co");
2251 if (*rp == 0)
2252 *rp = tgetnum("li");
2253 }
2254}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002255#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256
2257/*
2258 * Get a string entry from the termcap and add it to the list of termcodes.
2259 * Used for <t_xx> special keys.
2260 * Give an error message for failure when not sourcing.
2261 * If force given, replace an existing entry.
2262 * Return FAIL if the entry was not found, OK if the entry was added.
2263 */
2264 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002265add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 char_u *term;
2268 int key;
2269 struct builtin_term *termp;
2270#ifdef HAVE_TGETENT
2271 char_u *string;
2272 int i;
2273 int builtin_first;
2274 char_u tbuf[TBUFSZ];
2275 char_u tstrbuf[TBUFSZ];
2276 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002277 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278#endif
2279
2280/*
2281 * If the GUI is running or will start in a moment, we only support the keys
2282 * that the GUI can produce.
2283 */
2284#ifdef FEAT_GUI
2285 if (gui.in_use || gui.starting)
2286 return gui_mch_haskey(name);
2287#endif
2288
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002289 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 return OK;
2291
2292 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002293 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 return FAIL;
2295
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002296 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 term += 8;
2299#ifdef HAVE_TGETENT
2300 builtin_first = TRUE;
2301#endif
2302 }
2303#ifdef HAVE_TGETENT
2304 else
2305 builtin_first = p_tbi;
2306#endif
2307
2308#ifdef HAVE_TGETENT
2309/*
2310 * We can get the entry from the builtin termcap and from the external one.
2311 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2312 * builtin termcap first.
2313 * If 'ttybuiltin' is off, try external termcap first.
2314 */
2315 for (i = 0; i < 2; ++i)
2316 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002317 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318#endif
2319 /*
2320 * Search in builtin termcap
2321 */
2322 {
2323 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002324 if (termp->bt_string != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
2326 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002327 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 while (termp->bt_entry != (int)KS_NAME)
2329 {
2330 if ((int)termp->bt_entry == key)
2331 {
2332 add_termcode(name, (char_u *)termp->bt_string,
2333 term_is_8bit(term));
2334 return OK;
2335 }
2336 ++termp;
2337 }
2338 }
2339 }
2340#ifdef HAVE_TGETENT
2341 else
2342 /*
2343 * Search in external termcap
2344 */
2345 {
2346 error_msg = tgetent_error(tbuf, term);
2347 if (error_msg == NULL)
2348 {
2349 string = TGETSTR((char *)name, &tp);
2350 if (string != NULL && *string != NUL)
2351 {
2352 add_termcode(name, string, FALSE);
2353 return OK;
2354 }
2355 }
2356 }
2357 }
2358#endif
2359
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002360 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
2362#ifdef HAVE_TGETENT
2363 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002364 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 else
2366#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002367 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369 return FAIL;
2370}
2371
2372 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002373term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374{
2375 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2376}
2377
2378/*
2379 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2380 * Assume that the terminal is using 8-bit controls when the name contains
2381 * "8bit", like in "xterm-8bit".
2382 */
2383 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002384term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385{
2386 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2387}
2388
2389/*
2390 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002391 * <Esc>[ -> CSI <M_C_[>
2392 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 * <Esc>O -> <M-C-O>
2394 */
2395 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002396term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
2398 if (*p == ESC)
2399 {
2400 if (p[1] == '[')
2401 return CSI;
2402 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002403 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 if (p[1] == 'O')
2405 return 0x8f;
2406 }
2407 return 0;
2408}
2409
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002410#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002412term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
2414 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2415}
2416#endif
2417
2418#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2419
2420 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002421tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
2423 static char_u buf[16];
2424 char_u *p;
2425
2426 p = buf + 15;
2427 *p = '\0';
2428 do
2429 {
2430 --p;
2431 *p = (char_u) (i % 10 + '0');
2432 i /= 10;
2433 }
2434 while (i > 0 && p > buf);
2435 return p;
2436}
2437#endif
2438
2439#ifndef HAVE_TGETENT
2440
2441/*
2442 * minimal tgoto() implementation.
2443 * no padding and we only parse for %i %d and %+char
2444 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002445 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002446tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447{
2448 static char buf[30];
2449 char *p, *s, *e;
2450
2451 if (!cm)
2452 return "OOPS";
2453 e = buf + 29;
2454 for (s = buf; s < e && *cm; cm++)
2455 {
2456 if (*cm != '%')
2457 {
2458 *s++ = *cm;
2459 continue;
2460 }
2461 switch (*++cm)
2462 {
2463 case 'd':
2464 p = (char *)tltoa((unsigned long)y);
2465 y = x;
2466 while (*p)
2467 *s++ = *p++;
2468 break;
2469 case 'i':
2470 x++;
2471 y++;
2472 break;
2473 case '+':
2474 *s++ = (char)(*++cm + y);
2475 y = x;
2476 break;
2477 case '%':
2478 *s++ = *cm;
2479 break;
2480 default:
2481 return "OOPS";
2482 }
2483 }
2484 *s = '\0';
2485 return buf;
2486}
2487
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002488#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
2490/*
2491 * Set the terminal name and initialize the terminal options.
2492 * If "name" is NULL or empty, get the terminal name from the environment.
2493 * If that fails, use the default terminal name.
2494 */
2495 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002496termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 char_u *term;
2499
2500 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002501 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 term = name;
2503
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504#ifndef MSWIN
2505 if (term == NULL)
2506 term = mch_getenv((char_u *)"TERM");
2507#endif
2508 if (term == NULL || *term == NUL)
2509 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002510 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002512 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 set_string_default("term", term);
2514 set_string_default("ttytype", term);
2515
2516 /*
2517 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2518 */
2519 set_termname(T_NAME != NULL ? T_NAME : term);
2520}
2521
2522/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002523 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002525#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002526
2527// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002529
2530static int out_pos = 0; // number of chars in out_buf
2531
2532// Since the maximum number of SGR parameters shown as a normal value range is
2533// 16, the escape sequence length can be 4 * 16 + lead + tail.
2534#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
2536/*
2537 * out_flush(): flush the output buffer
2538 */
2539 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002540out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541{
2542 int len;
2543
2544 if (out_pos != 0)
2545 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002546 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 len = out_pos;
2548 out_pos = 0;
2549 ui_write(out_buf, len);
2550 }
2551}
2552
Bram Moolenaara338adc2018-01-31 20:51:47 +01002553/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002554 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2555 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002556 */
2557 void
2558out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002559 int force UNUSED, // when TRUE, update cursor even when not moved
2560 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002561{
2562 mch_disable_flush();
2563 out_flush();
2564 mch_enable_flush();
2565#ifdef FEAT_GUI
2566 if (gui.in_use)
2567 {
2568 gui_update_cursor(force, clear_selection);
2569 gui_may_flush();
2570 }
2571#endif
2572}
2573
2574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575/*
2576 * Sometimes a byte out of a multi-byte character is written with out_char().
2577 * To avoid flushing half of the character, call this function first.
2578 */
2579 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002580out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
2582 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2583 out_flush();
2584}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585
2586#ifdef FEAT_GUI
2587/*
2588 * out_trash(): Throw away the contents of the output buffer
2589 */
2590 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002591out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
2593 out_pos = 0;
2594}
2595#endif
2596
2597/*
2598 * out_char(c): put a byte into the output buffer.
2599 * Flush it if it becomes full.
2600 * This should not be used for outputting text on the screen (use functions
2601 * like msg_puts() and screen_putchar() for that).
2602 */
2603 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002604out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
Bram Moolenaard0573012017-10-28 21:11:06 +02002606#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002607 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 out_char('\r');
2609#endif
2610
2611 out_buf[out_pos++] = c;
2612
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002613 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 if (out_pos >= OUT_SIZE || p_wd)
2615 out_flush();
2616}
2617
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002619 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 */
2621 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002622out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 out_buf[out_pos++] = c;
2625
2626 if (out_pos >= OUT_SIZE)
2627 out_flush();
2628}
2629
2630/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002631 * A never-padding out_str().
2632 * Use this whenever you don't want to run the string through tputs().
2633 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 * is likely to strip off leading digits, that it mistakes for padding
2635 * information, and "%i", "%d", etc.
2636 * This should only be used for writing terminal codes, not for outputting
2637 * normal text (use functions like msg_puts() and screen_putchar() for that).
2638 */
2639 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002640out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002642 // avoid terminal strings being split up
2643 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 while (*s)
2647 out_char_nf(*s++);
2648
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002649 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (p_wd)
2651 out_flush();
2652}
2653
2654/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002655 * A conditional-flushing out_str, mainly for visualbell.
2656 * Handles a delay internally, because termlib may not respect the delay or do
2657 * it at the wrong time.
2658 * Note: Only for terminal strings.
2659 */
2660 void
2661out_str_cf(char_u *s)
2662{
2663 if (s != NULL && *s)
2664 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002665#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002666 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002667#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002668
2669#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002670 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002671 if (gui.in_use)
2672 {
2673 out_str_nf(s);
2674 return;
2675 }
2676#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002677 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002678 out_flush();
2679#ifdef HAVE_TGETENT
2680 for (p = s; *s; ++s)
2681 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002682 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002683 if (*s == '$' && *(s + 1) == '<')
2684 {
2685 char_u save_c = *s;
2686 int duration = atoi((char *)s + 2);
2687
2688 *s = NUL;
2689 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2690 *s = save_c;
2691 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002692# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002693 // Only sleep here if we can limit this happening in
2694 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002695 p = vim_strchr(s, '>');
2696 if (p == NULL || duration <= 0)
2697 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002698 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002699 p = s;
2700 }
2701 else
2702 {
2703 ++p;
2704 do_sleep(duration);
2705 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002706# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002707 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002708 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002709# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002710 break;
2711 }
2712 }
2713 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2714#else
2715 while (*s)
2716 out_char_nf(*s++);
2717#endif
2718
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002719 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002720 if (p_wd)
2721 out_flush();
2722 }
2723}
2724
2725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002727 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 * This should only be used for writing terminal codes, not for outputting
2729 * normal text (use functions like msg_puts() and screen_putchar() for that).
2730 */
2731 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002732out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733{
2734 if (s != NULL && *s)
2735 {
2736#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002737 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 if (gui.in_use)
2739 {
2740 out_str_nf(s);
2741 return;
2742 }
2743#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002744 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002745 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 out_flush();
2747#ifdef HAVE_TGETENT
2748 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2749#else
2750 while (*s)
2751 out_char_nf(*s++);
2752#endif
2753
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002754 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 if (p_wd)
2756 out_flush();
2757 }
2758}
2759
2760/*
2761 * cursor positioning using termcap parser. (jw)
2762 */
2763 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002764term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765{
2766 OUT_STR(tgoto((char *)T_CM, col, row));
2767}
2768
2769 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002770term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771{
2772 OUT_STR(tgoto((char *)T_CRI, 0, i));
2773}
2774
2775 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002776term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777{
2778 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2779}
2780
2781 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002782term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
2784 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2785}
2786
2787#if defined(HAVE_TGETENT) || defined(PROTO)
2788 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002789term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002791 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (x < 0)
2793 x = 0;
2794 if (y < 0)
2795 y = 0;
2796 OUT_STR(tgoto((char *)T_CWP, y, x));
2797}
2798
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002799# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2800/*
2801 * Return TRUE if we can request the terminal for a response.
2802 */
2803 static int
2804can_get_termresponse()
2805{
2806 return cur_tmode == TMODE_RAW
2807 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002808# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002809 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002810# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002811 && p_ek;
2812}
2813
Bram Moolenaarafd78262019-05-10 23:10:31 +02002814/*
2815 * Set "status" to STATUS_SENT.
2816 */
2817 static void
2818termrequest_sent(termrequest_T *status)
2819{
2820 status->tr_progress = STATUS_SENT;
2821 status->tr_start = time(NULL);
2822}
2823
2824/*
2825 * Return TRUE if any of the requests are in STATUS_SENT.
2826 */
2827 static int
2828termrequest_any_pending()
2829{
2830 int i;
2831 time_t now = time(NULL);
2832
2833 for (i = 0; all_termrequests[i] != NULL; ++i)
2834 {
2835 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2836 {
2837 if (all_termrequests[i]->tr_start > 0 && now > 0
2838 && all_termrequests[i]->tr_start + 2 < now)
2839 // Sent the request more than 2 seconds ago and didn't get a
2840 // response, assume it failed.
2841 all_termrequests[i]->tr_progress = STATUS_FAIL;
2842 else
2843 return TRUE;
2844 }
2845 }
2846 return FALSE;
2847}
2848
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002849static int winpos_x = -1;
2850static int winpos_y = -1;
2851static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002852
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002853# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002854/*
2855 * Try getting the Vim window position from the terminal.
2856 * Returns OK or FAIL.
2857 */
2858 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002859term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002860{
2861 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002862 int prev_winpos_x = winpos_x;
2863 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002864
2865 if (*T_CGP == NUL || !can_get_termresponse())
2866 return FAIL;
2867 winpos_x = -1;
2868 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002869 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002870 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002871 OUT_STR(T_CGP);
2872 out_flush();
2873
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002874 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002875 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002876 {
2877 (void)vpeekc_nomap();
2878 if (winpos_x >= 0 && winpos_y >= 0)
2879 {
2880 *x = winpos_x;
2881 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002882 return OK;
2883 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002884 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002885 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002886 // Do not reset "did_request_winpos", if we timed out the response might
2887 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002888
2889 winpos_x = prev_winpos_x;
2890 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002891 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002892 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002893 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002894 *x = winpos_x;
2895 *y = winpos_y;
2896 return OK;
2897 }
2898
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002899 return FALSE;
2900}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002901# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002902# endif
2903
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002905term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002907 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908}
2909#endif
2910
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002912term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
2914 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002915 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002916 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002918 // Special handling of 16 colors, because termcap can't handle it
2919 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
2920 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002922 && ((s[0] == ESC && s[1] == '[')
2923#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2924 || (s[0] == ESC && s[1] == '|')
2925#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02002926 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 && s[i] != NUL
2928 && (STRCMP(s + i + 1, "%p1%dm") == 0
2929 || STRCMP(s + i + 1, "%dm") == 0)
2930 && (s[i] == '3' || s[i] == '4'))
2931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002933 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002935 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002937 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002938#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02002939 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002940#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002941 IF_EB("\033[", ESC_STR "[")) : "\233";
2942 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2943 : (n >= 16 ? "48;5;" : "10");
2944
2945 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2947 }
2948 else
2949 OUT_STR(tgoto((char *)s, 0, n));
2950}
2951
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002952 void
2953term_fg_color(int n)
2954{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002955 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002956 if (*T_CAF)
2957 term_color(T_CAF, n);
2958 else if (*T_CSF)
2959 term_color(T_CSF, n);
2960}
2961
2962 void
2963term_bg_color(int n)
2964{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002965 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002966 if (*T_CAB)
2967 term_color(T_CAB, n);
2968 else if (*T_CSB)
2969 term_color(T_CSB, n);
2970}
2971
Bram Moolenaare023e882020-05-31 16:42:30 +02002972 void
2973term_ul_color(int n)
2974{
2975 if (*T_CAU)
2976 term_color(T_CAU, n);
2977}
2978
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002979/*
2980 * Return "dark" or "light" depending on the kind of terminal.
2981 * This is just guessing! Recognized are:
2982 * "linux" Linux console
2983 * "screen.linux" Linux console with screen
2984 * "cygwin.*" Cygwin shell
2985 * "putty.*" Putty program
2986 * We also check the COLORFGBG environment variable, which is set by
2987 * rxvt and derivatives. This variable contains either two or three
2988 * values separated by semicolons; we want the last value in either
2989 * case. If this value is 0-6 or 8, our background is dark.
2990 */
2991 char_u *
2992term_bg_default(void)
2993{
2994#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002995 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002996 return (char_u *)"dark";
2997#else
2998 char_u *p;
2999
3000 if (STRCMP(T_NAME, "linux") == 0
3001 || STRCMP(T_NAME, "screen.linux") == 0
3002 || STRNCMP(T_NAME, "cygwin", 6) == 0
3003 || STRNCMP(T_NAME, "putty", 5) == 0
3004 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
3005 && (p = vim_strrchr(p, ';')) != NULL
3006 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
3007 && p[2] == NUL))
3008 return (char_u *)"dark";
3009 return (char_u *)"light";
3010#endif
3011}
3012
Bram Moolenaar61be73b2016-04-29 22:59:22 +02003013#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003014
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003015#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
3016#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
3017#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003018
3019 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003020term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003021{
Bram Moolenaar380130f2016-04-22 11:24:43 +02003022#define MAX_COLOR_STR_LEN 100
3023 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003024
Bram Moolenaara1c487e2016-04-22 20:20:19 +02003025 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02003026 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02003027#ifdef FEAT_VTP
3028 if (use_wt())
3029 {
3030 out_flush();
3031 buf[1] = '[';
3032 vtp_printf(buf);
3033 }
3034 else
3035#endif
3036 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003037}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02003038
3039 void
3040term_fg_rgb_color(guicolor_T rgb)
3041{
3042 term_rgb_color(T_8F, rgb);
3043}
3044
3045 void
3046term_bg_rgb_color(guicolor_T rgb)
3047{
3048 term_rgb_color(T_8B, rgb);
3049}
Bram Moolenaare023e882020-05-31 16:42:30 +02003050
3051 void
3052term_ul_rgb_color(guicolor_T rgb)
3053{
3054 term_rgb_color(T_8U, rgb);
3055}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02003056#endif
3057
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003058#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
3059 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060/*
3061 * Generic function to set window title, using t_ts and t_fs.
3062 */
3063 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003064term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065{
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003066 // t_ts takes one argument: column in status line
3067 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003069 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 out_flush();
3071}
Bram Moolenaar40385db2018-08-07 22:31:44 +02003072
3073/*
3074 * Tell the terminal to push (save) the title and/or icon, so that it can be
3075 * popped (restored) later.
3076 */
3077 void
3078term_push_title(int which)
3079{
Bram Moolenaar27821262019-05-08 16:41:09 +02003080 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003081 {
3082 OUT_STR(T_CST);
3083 out_flush();
3084 }
3085
Bram Moolenaar27821262019-05-08 16:41:09 +02003086 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003087 {
3088 OUT_STR(T_SSI);
3089 out_flush();
3090 }
3091}
3092
3093/*
3094 * Tell the terminal to pop the title and/or icon.
3095 */
3096 void
3097term_pop_title(int which)
3098{
Bram Moolenaar27821262019-05-08 16:41:09 +02003099 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003100 {
3101 OUT_STR(T_CRT);
3102 out_flush();
3103 }
3104
Bram Moolenaar27821262019-05-08 16:41:09 +02003105 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003106 {
3107 OUT_STR(T_SRI);
3108 out_flush();
3109 }
3110}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111#endif
3112
3113/*
3114 * Make sure we have a valid set or terminal options.
3115 * Replace all entries that are NULL by empty_option
3116 */
3117 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003118ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003120 char_u *env_colors;
3121
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003122 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
3124 /*
3125 * MUST have "cm": cursor motion.
3126 */
3127 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003128 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129
3130 /*
3131 * if "cs" defined, use a scroll region, it's faster.
3132 */
3133 if (*T_CS != NUL)
3134 scroll_region = TRUE;
3135 else
3136 scroll_region = FALSE;
3137
3138 if (pairs)
3139 {
3140 /*
3141 * optional pairs
3142 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003143 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 if (*T_ME == NUL)
3145 T_ME = T_MR = T_MD = T_MB = empty_option;
3146 if (*T_SO == NUL || *T_SE == NUL)
3147 T_SO = T_SE = empty_option;
3148 if (*T_US == NUL || *T_UE == NUL)
3149 T_US = T_UE = empty_option;
3150 if (*T_CZH == NUL || *T_CZR == NUL)
3151 T_CZH = T_CZR = empty_option;
3152
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003153 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 if (*T_VE == NUL)
3155 T_VI = empty_option;
3156
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003157 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 if (*T_ME == NUL)
3159 {
3160 T_ME = T_SE;
3161 T_MR = T_SO;
3162 T_MD = T_SO;
3163 }
3164
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003165 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (*T_SO == NUL)
3167 {
3168 T_SE = T_ME;
3169 if (*T_MR == NUL)
3170 T_SO = T_MD;
3171 else
3172 T_SO = T_MR;
3173 }
3174
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003175 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 if (*T_CZH == NUL)
3177 {
3178 T_CZR = T_ME;
3179 if (*T_MR == NUL)
3180 T_CZH = T_MD;
3181 else
3182 T_CZH = T_MR;
3183 }
3184
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003185 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (*T_CSB == NUL || *T_CSF == NUL)
3187 {
3188 T_CSB = empty_option;
3189 T_CSF = empty_option;
3190 }
3191
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003192 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 if (*T_CAB == NUL || *T_CAF == NUL)
3194 {
3195 T_CAB = empty_option;
3196 T_CAF = empty_option;
3197 }
3198
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003199 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003201 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003203 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 p_wiv = (*T_XS != NUL);
3205 }
3206 need_gather = TRUE;
3207
Bram Moolenaar759d8152020-04-26 16:52:49 +02003208 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3209 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003211#ifdef FEAT_GUI
3212 if (!gui.in_use)
3213#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003214 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003215 env_colors = mch_getenv((char_u *)"COLORS");
3216 if (env_colors != NULL && isdigit(*env_colors))
3217 {
3218 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003219
Bram Moolenaar759d8152020-04-26 16:52:49 +02003220 if (colors != t_colors)
3221 set_color_count(colors);
3222 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224}
3225
3226#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3227 || defined(PROTO)
3228/*
3229 * Represent the given long_u as individual bytes, with the most significant
3230 * byte first, and store them in dst.
3231 */
3232 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003233add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
3235 int i;
3236 int shift;
3237
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003238 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
3240 shift = 8 * (sizeof(long_u) - i);
3241 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3242 }
3243}
3244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245/*
3246 * Interpret the next string of bytes in buf as a long integer, with the most
3247 * significant byte first. Note that it is assumed that buf has been through
3248 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3249 * Puts result in val, and returns the number of bytes read from buf
3250 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3251 * were present.
3252 */
3253 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003254get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
3256 int len;
3257 char_u bytes[sizeof(long_u)];
3258 int i;
3259 int shift;
3260
3261 *val = 0;
3262 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3263 if (len != -1)
3264 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003265 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
3267 shift = 8 * (sizeof(long_u) - 1 - i);
3268 *val += (long_u)bytes[i] << shift;
3269 }
3270 }
3271 return len;
3272}
3273#endif
3274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275/*
3276 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3277 * that buf has been through inchar(). Returns the actual number of bytes used
3278 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3279 * available.
3280 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003281 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003282get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
3284 int len = 0;
3285 int i;
3286 char_u c;
3287
3288 for (i = 0; i < num_bytes; i++)
3289 {
3290 if ((c = buf[len++]) == NUL)
3291 return -1;
3292 if (c == K_SPECIAL)
3293 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003294 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 return -1;
3296 if (buf[len++] == (int)KS_ZERO)
3297 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003298 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3299 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003300 if (buf[len++] == (int)KE_CSI)
3301 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003303 else if (c == CSI && buf[len] == KS_EXTRA
3304 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003305 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3306 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003307 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 bytes[i] = c;
3309 }
3310 return len;
3311}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
3313/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003314 * Check if the new shell size is valid, correct it if it's too small or way
3315 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 */
3317 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003318check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003320 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003322 limit_screen_size();
3323}
3324
3325/*
3326 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3327 */
3328 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003329limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003330{
3331 if (Columns < MIN_COLUMNS)
3332 Columns = MIN_COLUMNS;
3333 else if (Columns > 10000)
3334 Columns = 10000;
3335 if (Rows > 1000)
3336 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337}
3338
Bram Moolenaar86b68352004-12-27 21:59:20 +00003339/*
3340 * Invoked just before the screen structures are going to be (re)allocated.
3341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003343win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 static int old_Rows = 0;
3346 static int old_Columns = 0;
3347
3348 if (old_Rows != Rows || old_Columns != Columns)
3349 ui_new_shellsize();
3350 if (old_Rows != Rows)
3351 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003352 // if 'window' uses the whole screen, keep it using that
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003353 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003354 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003356 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
3358 if (old_Columns != Columns)
3359 {
3360 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003361 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
3363}
3364
3365/*
3366 * Call this function when the Vim shell has been resized in any way.
3367 * Will obtain the current size and redraw (also when size didn't change).
3368 */
3369 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003370shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371{
3372 set_shellsize(0, 0, FALSE);
3373}
3374
3375/*
3376 * Check if the shell size changed. Handle a resize.
3377 * When the size didn't change, nothing happens.
3378 */
3379 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003380shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 int old_Rows = Rows;
3383 int old_Columns = Columns;
3384
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003385 if (!exiting
3386#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003387 // Do not get the size when executing a shell command during
3388 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003389 && !gui.starting
3390#endif
3391 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003392 {
3393 (void)ui_get_shellsize();
3394 check_shellsize();
3395 if (old_Rows != Rows || old_Columns != Columns)
3396 shell_resized();
3397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398}
3399
3400/*
3401 * Set size of the Vim shell.
3402 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3403 * window size (this is used for the :win command).
3404 * If 'mustset' is FALSE, we may try to get the real window size and if
3405 * it fails use 'width' and 'height'.
3406 */
3407 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003408set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409{
3410 static int busy = FALSE;
3411
3412 /*
3413 * Avoid recursiveness, can happen when setting the window size causes
3414 * another window-changed signal.
3415 */
3416 if (busy)
3417 return;
3418
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003419 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 return;
3421
Bram Moolenaara971b822011-09-14 14:43:25 +02003422 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003424 // postpone the resizing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 State = SETWSIZE;
3426 return;
3427 }
3428
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003429 if (updating_screen)
3430 // resizing while in update_screen() may cause a crash
3431 return;
3432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003433 // curwin->w_buffer can be NULL when we are closing a window and the
3434 // buffer has already been closed and removing a scrollbar causes a resize
3435 // event. Don't resize then, it will happen after entering another buffer.
Bram Moolenaara971b822011-09-14 14:43:25 +02003436 if (curwin->w_buffer == NULL)
3437 return;
3438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 ++busy;
3440
3441#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003442 out_flush(); // must do this before mch_get_shellsize() for
3443 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444#endif
3445
3446 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3447 {
3448 Rows = height;
3449 Columns = width;
3450 check_shellsize();
3451 ui_set_shellsize(mustset);
3452 }
3453 else
3454 check_shellsize();
3455
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003456 // The window layout used to be adjusted here, but it now happens in
3457 // screenalloc() (also invoked from screenclear()). That is because the
3458 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
3460 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3461 screenclear();
3462 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003463 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
3465 if (starting != NO_SCREEN)
3466 {
3467#ifdef FEAT_TITLE
3468 maketitle();
3469#endif
3470 changed_line_abv_curs();
3471 invalidate_botline();
3472
3473 /*
3474 * We only redraw when it's needed:
3475 * - While at the more prompt or executing an external command, don't
3476 * redraw, but position the cursor.
3477 * - While editing the command line, only redraw that.
3478 * - in Ex mode, don't redraw anything.
3479 * - Otherwise, redraw right now, and position the cursor.
3480 * Always need to call update_screen() or screenalloc(), to make
3481 * sure Rows/Columns and the size of ScreenLines[] is correct!
3482 */
3483 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3484 || exmode_active)
3485 {
3486 screenalloc(FALSE);
3487 repeat_message();
3488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 else
3490 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003491 if (curwin->w_p_scb)
3492 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003493 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003494 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003495 update_screen(NOT_VALID);
3496 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003497 }
3498 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003499 {
3500 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003501 if (pum_visible())
3502 {
3503 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003504 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003505 }
Bram Moolenaara5e66212017-09-29 22:42:33 +02003506 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003507 if (redrawing())
3508 setcursor();
3509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003511 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
3513 out_flush();
3514 --busy;
3515}
3516
3517/*
3518 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3519 * commands and Ex mode).
3520 */
3521 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003522settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523{
3524#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003525 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 if (gui.in_use)
3527 return;
3528#endif
3529
3530 if (full_screen)
3531 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003533 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3534 * set the terminal to raw mode, even though we think it already is,
3535 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 * When we think the terminal is normal, don't try to set it to
3537 * normal again, because that causes problems (logout!) on some
3538 * machines.
3539 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003540 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
3542#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003543# ifdef FEAT_GUI
3544 if (!gui.in_use && !gui.starting)
3545# endif
3546 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003547 // May need to check for T_CRV response and termcodes, it
3548 // doesn't work in Cooked mode, an external program may get
3549 // them.
3550 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003551 (void)vpeekc_nomap();
3552 check_for_codes_from_term();
3553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003556 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003557
3558 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3559 // Avoid doing this too often, on some terminals the codes are not
3560 // handled properly.
3561 if (termcap_active && tmode != TMODE_SLEEP
3562 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003563 {
3564 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003565 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003566 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003567 out_str(T_CTE); // possibly disables modifyOtherKeys
3568 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003569 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003570 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003571 out_str(T_BE); // enable bracketed paste mode (should
3572 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003573 out_str(T_CTI); // possibly enables modifyOtherKeys
3574 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003577 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003580 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 out_flush();
3582 }
3583#ifdef FEAT_TERMRESPONSE
3584 may_req_termresponse();
3585#endif
3586 }
3587}
3588
3589 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003590starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591{
3592 if (full_screen && !termcap_active)
3593 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003594 out_str(T_TI); // start termcap mode
3595 out_str(T_CTI); // start "raw" mode
3596 out_str(T_KS); // start "keypad transmit" mode
3597 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 out_flush();
3599 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003600 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003602# ifdef FEAT_GUI
3603 if (!gui.in_use && !gui.starting)
3604# endif
3605 {
3606 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003607 // Immediately check for a response. If t_Co changes, we don't
3608 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003609 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003610 check_for_codes_from_term();
3611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612#endif
3613 }
3614}
3615
3616 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003617stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618{
3619 screen_stop_highlight();
3620 reset_cterm_colors();
3621 if (termcap_active)
3622 {
3623#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003624# ifdef FEAT_GUI
3625 if (!gui.in_use && !gui.starting)
3626# endif
3627 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003628 // May need to discard T_CRV, T_U7 or T_RBG response.
3629 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003630 {
3631# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003632 // Give the terminal a chance to respond.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003633 mch_delay(100L, FALSE);
3634# endif
3635# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003636 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003637 if (exiting)
3638 tcflush(fileno(stdin), TCIFLUSH);
3639# endif
3640 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003641 // Check for termcodes first, otherwise an external program may
3642 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003643 check_for_codes_from_term();
3644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003646 out_str(T_BD); // disable bracketed paste mode
3647 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 out_flush();
3649 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003650 cursor_on(); // just in case it is still off
3651 out_str(T_CTE); // stop "raw" mode
3652 out_str(T_TE); // stop termcap mode
3653 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 out_flush();
3655 }
3656}
3657
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003658#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659/*
3660 * Request version string (for xterm) when needed.
3661 * Only do this after switching to raw mode, otherwise the result will be
3662 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003663 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003664 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 * Only do this after termcap mode has been started, otherwise the codes for
3666 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003667 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3668 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003669 * On Unix only do it when both output and input are a tty (avoid writing
3670 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 * The result is caught in check_termcode().
3672 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003673 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003674may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003676 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003677 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003678 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 && *T_CRV != NUL)
3680 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003681 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003683 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003684 // check for the characters now, otherwise they might be eaten by
3685 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 out_flush();
3687 (void)vpeekc_nomap();
3688 }
3689}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003690
Bram Moolenaar9584b312013-03-13 19:29:28 +01003691/*
Bram Moolenaara45551a2020-06-09 15:57:37 +02003692 * Send sequences to the terminal and check with t_u7 how the cursor moves, to
3693 * find out properties of the terminal.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02003694 * Note that this goes out before T_CRV, so that the result can be used when
3695 * the termresponse arrives.
Bram Moolenaar9584b312013-03-13 19:29:28 +01003696 */
3697 void
Bram Moolenaara45551a2020-06-09 15:57:37 +02003698check_terminal_behavior(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003699{
Bram Moolenaara45551a2020-06-09 15:57:37 +02003700 int did_send = FALSE;
3701
3702 if (!can_get_termresponse() || starting != 0 || *T_U7 == NUL)
3703 return;
3704
Bram Moolenaarafd78262019-05-10 23:10:31 +02003705 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaar9584b312013-03-13 19:29:28 +01003706 && !option_was_set((char_u *)"ambiwidth"))
3707 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003708 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003709
Bram Moolenaara45551a2020-06-09 15:57:37 +02003710 // Ambiguous width check.
3711 // Check how the terminal treats ambiguous character width (UAX #11).
3712 // First, we move the cursor to (1, 0) and print a test ambiguous
3713 // character \u25bd (WHITE DOWN-POINTING TRIANGLE) and then query
3714 // the current cursor position. If the terminal treats \u25bd as
3715 // single width, the position is (1, 1), or if it is treated as double
3716 // width, that will be (1, 2). This function has the side effect that
3717 // changes cursor position, so it must be called immediately after
3718 // entering termcap mode.
3719 LOG_TR(("Sending request for ambiwidth check"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003720 // Do this in the second row. In the first row the returned sequence
3721 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003722 term_windgoto(1, 0);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003723 buf[mb_char2bytes(0x25bd, buf)] = NUL;
Bram Moolenaarafd78262019-05-10 23:10:31 +02003724 out_str(buf);
3725 out_str(T_U7);
3726 termrequest_sent(&u7_status);
3727 out_flush();
Bram Moolenaara45551a2020-06-09 15:57:37 +02003728 did_send = TRUE;
Bram Moolenaar976787d2017-06-04 15:45:50 +02003729
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003730 // This overwrites a few characters on the screen, a redraw is needed
3731 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003732 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003733 term_windgoto(1, 0);
3734 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003735 line_was_clobbered(1);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003736 }
3737
3738 if (xcc_status.tr_progress == STATUS_GET)
3739 {
3740 // 2. Check compatibility with xterm.
3741 // We move the cursor to (2, 0), print a test sequence and then query
3742 // the current cursor position. If the terminal properly handles
3743 // unknown DCS string and CSI sequence with intermediate byte, the test
3744 // sequence is ignored and the cursor does not move. If the terminal
3745 // handles test sequence incorrectly, a garbage string is displayed and
3746 // the cursor does move.
3747 LOG_TR(("Sending xterm compatibility test sequence."));
3748 // Do this in the third row. Second row is used by ambiguous
3749 // chararacter width check.
3750 term_windgoto(2, 0);
3751 // send the test DCS string.
3752 out_str((char_u *)"\033Pzz\033\\");
3753 // send the test CSI sequence with intermediate byte.
3754 out_str((char_u *)"\033[0%m");
3755 out_str(T_U7);
3756 termrequest_sent(&xcc_status);
3757 out_flush();
3758 did_send = TRUE;
3759
3760 // If the terminal handles test sequence incorrectly, garbage text is
3761 // displayed. Clear them out for now.
3762 screen_stop_highlight();
3763 term_windgoto(2, 0);
3764 out_str((char_u *)" ");
Bram Moolenaar96916ac2020-07-08 23:09:28 +02003765 line_was_clobbered(2);
Bram Moolenaara45551a2020-06-09 15:57:37 +02003766 }
3767
3768 if (did_send)
3769 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003770 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003771
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003772 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003773 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003774
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003775 // check for the characters now, otherwise they might be eaten by
3776 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003777 out_flush();
3778 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003779 }
3780}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003781
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003782/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003783 * Similar to requesting the version string: Request the terminal background
3784 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003785 */
3786 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003787may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003788{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003789 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003790 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003791 int didit = FALSE;
3792
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003793# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003794 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003795 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003796 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003797 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003798 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003799 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003800 didit = TRUE;
3801 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003802# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003803
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003804 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003805 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003806 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003807 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003808 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003809 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003810 didit = TRUE;
3811 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003812
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003813 if (didit)
3814 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003815 // check for the characters now, otherwise they might be eaten by
3816 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003817 out_flush();
3818 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003819 }
3820 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003821}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003822
Bram Moolenaar2951b772013-07-03 12:45:31 +02003823# ifdef DEBUG_TERMRESPONSE
3824 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003825log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003826{
3827 static FILE *fd_tr = NULL;
3828 static proftime_T start;
3829 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003830 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003831
3832 if (fd_tr == NULL)
3833 {
3834 fd_tr = fopen("termresponse.log", "w");
3835 profile_start(&start);
3836 }
3837 now = start;
3838 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003839 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3840 must_redraw == NOT_VALID ? "NV"
3841 : must_redraw == CLEAR ? "CL" : " ");
3842 va_start(ap, fmt);
3843 vfprintf(fd_tr, fmt, ap);
3844 va_end(ap);
3845 fputc('\n', fd_tr);
3846 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003847}
3848# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849#endif
3850
3851/*
3852 * Return TRUE when saving and restoring the screen.
3853 */
3854 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003855swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856{
3857 return (full_screen && *T_TI != NUL);
3858}
3859
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860/*
3861 * By outputting the 'cursor very visible' termcap code, for some windowed
3862 * terminals this makes the screen scrolled to the correct position.
3863 * Used when starting Vim or returning from a shell.
3864 */
3865 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003866scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003868 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
3870 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003871 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003872 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 }
3874}
3875
3876static int cursor_is_off = FALSE;
3877
3878/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003879 * Enable the cursor without checking if it's already enabled.
3880 */
3881 void
3882cursor_on_force(void)
3883{
3884 out_str(T_VE);
3885 cursor_is_off = FALSE;
3886}
3887
3888/*
3889 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 */
3891 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003892cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893{
3894 if (cursor_is_off)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003895 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896}
3897
3898/*
3899 * Disable the cursor.
3900 */
3901 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003902cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003904 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003906 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 cursor_is_off = TRUE;
3908 }
3909}
3910
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003911#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003913 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003914 */
3915 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003916term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003917{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003918 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003919 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003920
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003921 // Only do something when redrawing the screen and we can restore the
3922 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003923 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003924 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003925# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003926 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003927 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003928 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003929# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003930 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003931 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003932
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003933 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003934 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003935 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003936 {
3937 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003938 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003939 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003940 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003941 if (*p != NUL)
3942 {
3943 out_str(p);
3944 showing_mode = REPLACE;
3945 }
3946 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003947 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003948 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003949 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003950 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003951 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003952 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003953 showing_mode = INSERT;
3954 }
3955 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003956 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003957 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003958 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003959 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003960 }
3961}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003962
3963# if defined(FEAT_TERMINAL) || defined(PROTO)
3964 void
3965term_cursor_color(char_u *color)
3966{
3967 if (*T_CSC != NUL)
3968 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003969 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003970 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003971 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003972 out_flush();
3973 }
3974}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003975# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003976
Bram Moolenaar4db25542017-08-28 22:43:05 +02003977 int
3978blink_state_is_inverted()
3979{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003980#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02003981 return rbm_status.tr_progress == STATUS_GOT
3982 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003983 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003984#else
3985 return FALSE;
3986#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003987}
3988
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003989/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003990 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003991 */
3992 void
3993term_cursor_shape(int shape, int blink)
3994{
3995 if (*T_CSH != NUL)
3996 {
3997 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3998 out_flush();
3999 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02004000 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004001 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02004002 int do_blink = blink;
4003
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004004 // t_SH is empty: try setting just the blink state.
4005 // The blink flags are XORed together, if the initial blinking from
4006 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02004007 if (blink_state_is_inverted())
4008 do_blink = !blink;
4009
4010 if (do_blink && *T_VS != NUL)
4011 {
4012 out_str(T_VS);
4013 out_flush();
4014 }
4015 else if (!do_blink && *T_CVS != NUL)
4016 {
4017 out_str(T_CVS);
4018 out_flush();
4019 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02004020 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02004021}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004022#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004023
4024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 * Set scrolling region for window 'wp'.
4026 * The region starts 'off' lines from the start of the window.
4027 * Also set the vertical scroll region for a vertically split window. Always
4028 * the full width of the window, excluding the vertical separator.
4029 */
4030 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004031scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032{
4033 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
4034 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02004036 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
4037 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004038 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039}
4040
4041/*
4042 * Reset scrolling region to the whole screen.
4043 */
4044 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004045scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046{
4047 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 if (*T_CSV != NUL)
4049 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004050 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051}
4052
4053
4054/*
4055 * List of terminal codes that are currently recognized.
4056 */
4057
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00004058static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004060 char_u name[2]; // termcap name of entry
4061 char_u *code; // terminal code (in allocated memory)
4062 int len; // STRLEN(code)
4063 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064} *termcodes = NULL;
4065
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004066static int tc_max_len = 0; // number of entries that termcodes[] can hold
4067static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004069static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004070
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004072clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073{
4074 while (tc_len > 0)
4075 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004076 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 tc_max_len = 0;
4078
4079#ifdef HAVE_TGETENT
4080 BC = (char *)empty_option;
4081 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004082 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 ospeed = 0;
4084#endif
4085
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004086 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087}
4088
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004089#define ATC_FROM_TERM 55
4090
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091/*
4092 * Add a new entry to the list of terminal codes.
4093 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004094 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
4095 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 */
4097 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004098add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
4100 struct termcode *new_tc;
4101 int i, j;
4102 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004103 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104
4105 if (string == NULL || *string == NUL)
4106 {
4107 del_termcode(name);
4108 return;
4109 }
4110
Bram Moolenaar4f974752019-02-17 17:44:42 +01004111#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004112 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaar45500912014-07-09 20:51:07 +02004113#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004114# ifdef VIMDLL
4115 if (!gui.in_use)
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004116 s = vim_strnsave(string, STRLEN(string) + 1);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004117 else
4118# endif
4119 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02004120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 if (s == NULL)
4122 return;
4123
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004124 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004125 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004127 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 s[0] = term_7to8bit(string);
4129 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004130
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004131#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4132# ifdef VIMDLL
4133 if (!gui.in_use)
4134# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004135 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004136 if (s[0] == K_NUL)
4137 {
4138 STRMOVE(s + 1, s);
4139 s[1] = 3;
4140 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004141 }
4142#endif
4143
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004144 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004146 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
4148 /*
4149 * need to make space for more entries
4150 */
4151 if (tc_len == tc_max_len)
4152 {
4153 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004154 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 if (new_tc == NULL)
4156 {
4157 tc_max_len -= 20;
4158 return;
4159 }
4160 for (i = 0; i < tc_len; ++i)
4161 new_tc[i] = termcodes[i];
4162 vim_free(termcodes);
4163 termcodes = new_tc;
4164 }
4165
4166 /*
4167 * Look for existing entry with the same name, it is replaced.
4168 * Look for an existing entry that is alphabetical higher, the new entry
4169 * is inserted in front of it.
4170 */
4171 for (i = 0; i < tc_len; ++i)
4172 {
4173 if (termcodes[i].name[0] < name[0])
4174 continue;
4175 if (termcodes[i].name[0] == name[0])
4176 {
4177 if (termcodes[i].name[1] < name[1])
4178 continue;
4179 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004180 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 */
4182 if (termcodes[i].name[1] == name[1])
4183 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004184 if (flags == ATC_FROM_TERM && (j = termcode_star(
4185 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004186 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004187 // Don't replace ESC[123;*X or ESC O*X with another when
4188 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004189 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004190 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004191 && s[len - 1]
4192 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004193 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004194 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004195 vim_free(s);
4196 return;
4197 }
4198 }
4199 else
4200 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004201 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004202 vim_free(termcodes[i].code);
4203 --tc_len;
4204 break;
4205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
4207 }
4208 /*
4209 * Found alphabetical larger entry, move rest to insert new entry
4210 */
4211 for (j = tc_len; j > i; --j)
4212 termcodes[j] = termcodes[j - 1];
4213 break;
4214 }
4215
4216 termcodes[i].name[0] = name[0];
4217 termcodes[i].name[1] = name[1];
4218 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004219 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004220
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004221 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4222 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004223 termcodes[i].modlen = 0;
4224 j = termcode_star(s, len);
4225 if (j > 0)
4226 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 ++tc_len;
4228}
4229
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004230/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004231 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004232 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004233 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004234 */
4235 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004236termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004237{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004238 // Shortest is <M-O>*X. With ; shortest is <CSI>1;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004239 if (len >= 3 && code[len - 2] == '*')
4240 {
4241 if (len >= 5 && code[len - 3] == ';')
4242 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004243 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004244 return 1;
4245 }
4246 return 0;
4247}
4248
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004250find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
4252 int i;
4253
4254 for (i = 0; i < tc_len; ++i)
4255 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4256 return termcodes[i].code;
4257 return NULL;
4258}
4259
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004261get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262{
4263 if (i >= tc_len)
4264 return NULL;
4265 return &termcodes[i].name[0];
4266}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004268/*
4269 * Returns the length of the terminal code at index 'idx'.
4270 */
4271 int
4272get_termcode_len(int idx)
4273{
4274 return termcodes[idx].len;
4275}
4276
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004277 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004278del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279{
4280 int i;
4281
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004282 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 return;
4284
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004285 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
4287 for (i = 0; i < tc_len; ++i)
4288 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4289 {
4290 del_termcode_idx(i);
4291 return;
4292 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004293 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294}
4295
4296 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004297del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298{
4299 int i;
4300
4301 vim_free(termcodes[idx].code);
4302 --tc_len;
4303 for (i = idx; i < tc_len; ++i)
4304 termcodes[i] = termcodes[i + 1];
4305}
4306
4307#ifdef FEAT_TERMRESPONSE
4308/*
4309 * Called when detected that the terminal sends 8-bit codes.
4310 * Convert all 7-bit codes to their 8-bit equivalent.
4311 */
4312 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004313switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314{
4315 int i;
4316 int c;
4317
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004318 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 if (!term_is_8bit(T_NAME))
4320 {
4321 for (i = 0; i < tc_len; ++i)
4322 {
4323 c = term_7to8bit(termcodes[i].code);
4324 if (c != 0)
4325 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004326 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 termcodes[i].code[0] = c;
4328 }
4329 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004330 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004333 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334}
4335#endif
4336
4337#ifdef CHECK_DOUBLE_CLICK
4338static linenr_T orig_topline = 0;
4339# ifdef FEAT_DIFF
4340static int orig_topfill = 0;
4341# endif
4342#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004343#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344/*
4345 * Checking for double clicks ourselves.
4346 * "orig_topline" is used to avoid detecting a double-click when the window
4347 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4348 */
4349/*
4350 * Set orig_topline. Used when jumping to another window, so that a double
4351 * click still works.
4352 */
4353 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004354set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355{
4356 orig_topline = wp->w_topline;
4357# ifdef FEAT_DIFF
4358 orig_topfill = wp->w_topfill;
4359# endif
4360}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004361
4362/*
4363 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4364 * topline and topfill.
4365 */
4366 int
4367is_mouse_topline(win_T *wp)
4368{
4369 return orig_topline == wp->w_topline
4370#ifdef FEAT_DIFF
4371 && orig_topfill == wp->w_topfill
4372#endif
4373 ;
4374}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375#endif
4376
4377/*
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004378 * Put "string[new_slen]" in typebuf, or in "buf[bufsize]" if "buf" is not NULL.
4379 * Remove "slen" bytes.
4380 * Returns FAIL for error.
4381 */
Bram Moolenaar975a8802020-06-06 22:36:24 +02004382 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004383put_string_in_typebuf(
4384 int offset,
4385 int slen,
4386 char_u *string,
4387 int new_slen,
4388 char_u *buf,
4389 int bufsize,
4390 int *buflen)
4391{
4392 int extra = new_slen - slen;
4393
4394 string[new_slen] = NUL;
4395 if (buf == NULL)
4396 {
4397 if (extra < 0)
4398 // remove matched chars, taking care of noremap
4399 del_typebuf(-extra, offset);
4400 else if (extra > 0)
4401 // insert the extra space we need
4402 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
4403
4404 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4405 // typebuf.tb_buf[]!
4406 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4407 (size_t)new_slen);
4408 }
4409 else
4410 {
4411 if (extra < 0)
4412 // remove matched characters
4413 mch_memmove(buf + offset, buf + offset - extra,
4414 (size_t)(*buflen + offset + extra));
4415 else if (extra > 0)
4416 {
4417 // Insert the extra space we need. If there is insufficient
4418 // space return -1.
4419 if (*buflen + extra + new_slen >= bufsize)
4420 return FAIL;
4421 mch_memmove(buf + offset + extra, buf + offset,
4422 (size_t)(*buflen - offset));
4423 }
4424 mch_memmove(buf + offset, string, (size_t)new_slen);
4425 *buflen = *buflen + extra + new_slen;
4426 }
4427 return OK;
4428}
4429
4430/*
4431 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4432 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004433 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004434decode_modifiers(int n)
4435{
4436 int code = n - 1;
4437 int modifiers = 0;
4438
4439 if (code & 1)
4440 modifiers |= MOD_MASK_SHIFT;
4441 if (code & 2)
4442 modifiers |= MOD_MASK_ALT;
4443 if (code & 4)
4444 modifiers |= MOD_MASK_CTRL;
4445 if (code & 8)
4446 modifiers |= MOD_MASK_META;
4447 return modifiers;
4448}
4449
4450 static int
4451modifiers2keycode(int modifiers, int *key, char_u *string)
4452{
4453 int new_slen = 0;
4454
4455 if (modifiers != 0)
4456 {
4457 // Some keys have the modifier included. Need to handle that here to
4458 // make mappings work.
4459 *key = simplify_key(*key, &modifiers);
4460 if (modifiers != 0)
4461 {
4462 string[new_slen++] = K_SPECIAL;
4463 string[new_slen++] = (int)KS_MODIFIER;
4464 string[new_slen++] = modifiers;
4465 }
4466 }
4467 return new_slen;
4468}
4469
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004470#ifdef FEAT_TERMRESPONSE
4471/*
4472 * Handle a cursor position report.
4473 */
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004474 static void
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004475handle_u7_response(int *arg, char_u *tp UNUSED, int csi_len UNUSED)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004476{
4477 if (arg[0] == 2 && arg[1] >= 2)
4478 {
4479 char *aw = NULL;
4480
4481 LOG_TR(("Received U7 status: %s", tp));
4482 u7_status.tr_progress = STATUS_GOT;
4483 did_cursorhold = TRUE;
4484 if (arg[1] == 2)
4485 aw = "single";
4486 else if (arg[1] == 3)
4487 aw = "double";
4488 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4489 {
4490 // Setting the option causes a screen redraw. Do
4491 // that right away if possible, keeping any
4492 // messages.
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02004493 set_option_value((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004494# ifdef DEBUG_TERMRESPONSE
4495 {
4496 int r = redraw_asap(CLEAR);
4497
4498 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
4499 }
4500# else
4501 redraw_asap(CLEAR);
4502# endif
4503# ifdef FEAT_EVAL
4504 set_vim_var_string(VV_TERMU7RESP, tp, csi_len);
4505# endif
4506 }
4507 }
4508 else if (arg[0] == 3)
4509 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004510 int value;
4511
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004512 LOG_TR(("Received compatibility test result: %s", tp));
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004513 xcc_status.tr_progress = STATUS_GOT;
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004514
4515 // Third row: xterm compatibility test.
4516 // If the cursor is on the first column then the terminal can handle
4517 // the request for cursor style and blinking.
4518 value = arg[1] == 1 ? TPR_YES : TPR_NO;
4519 term_props[TPR_CURSOR_STYLE].tpr_status = value;
4520 term_props[TPR_CURSOR_BLINK].tpr_status = value;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004521 }
4522}
4523
4524/*
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004525 * Handle a response to T_CRV: {lead}{first}{x};{vers};{y}c
4526 * Xterm and alikes use '>' for {first}.
4527 * Rxvt sends "{lead}?1;2c".
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004528 */
4529 static void
4530handle_version_response(int first, int *arg, int argc, char_u *tp)
4531{
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004532 // The xterm version. It is set to zero when it can't be an actual xterm
4533 // version.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004534 int version = arg[1];
4535
4536 LOG_TR(("Received CRV response: %s", tp));
4537 crv_status.tr_progress = STATUS_GOT;
4538 did_cursorhold = TRUE;
4539
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004540 // Reset terminal properties that are set based on the termresponse.
4541 // Mainly useful for tests that send the termresponse multiple times.
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02004542 // For testing all props can be reset.
Bram Moolenaar142499d2020-06-13 16:39:31 +02004543 init_term_props(
4544#ifdef FEAT_EVAL
4545 reset_term_props_on_termresponse
4546#else
4547 FALSE
4548#endif
4549 );
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004550
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004551 // If this code starts with CSI, you can bet that the
4552 // terminal uses 8-bit codes.
4553 if (tp[0] == CSI)
4554 switch_to_8bit();
4555
4556 // Screen sends 40500.
4557 // rxvt sends its version number: "20703" is 2.7.3.
4558 // Ignore it for when the user has set 'term' to xterm,
4559 // even though it's an rxvt.
4560 if (version > 20000)
4561 version = 0;
4562
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004563 // Figure out more if the reeponse is CSI > 99 ; 99 ; 99 c
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004564 if (first == '>' && argc == 3)
4565 {
4566 int need_flush = FALSE;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004567
4568 // mintty 2.9.5 sends 77;20905;0c.
4569 // (77 is ASCII 'M' for mintty.)
4570 if (arg[0] == 77)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004571 {
4572 // mintty can do SGR mouse reporting
4573 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4574 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004575
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004576 // If xterm version >= 141 try to get termcap codes. For other
4577 // terminals the request should be ignored.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004578 if (version >= 141)
4579 {
4580 LOG_TR(("Enable checking for XT codes"));
4581 check_for_codes = TRUE;
4582 need_gather = TRUE;
4583 req_codes_from_term();
4584 }
4585
4586 // libvterm sends 0;100;0
4587 if (version == 100 && arg[0] == 0 && arg[2] == 0)
4588 {
4589 // If run from Vim $COLORS is set to the number of
4590 // colors the terminal supports. Otherwise assume
4591 // 256, libvterm supports even more.
4592 if (mch_getenv((char_u *)"COLORS") == NULL)
4593 may_adjust_color_count(256);
4594 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004595 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004596 }
4597
4598 if (version == 95)
4599 {
4600 // Mac Terminal.app sends 1;95;0
4601 if (arg[0] == 1 && arg[2] == 0)
4602 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004603 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
4604 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004605 }
4606 // iTerm2 sends 0;95;0
4607 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004608 {
4609 // iTerm2 can do SGR mouse reporting
4610 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4611 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004612 // old iTerm2 sends 0;95;
4613 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004614 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004615 }
4616
4617 // screen sends 83;40500;0 83 is 'S' in ASCII.
4618 if (arg[0] == 83)
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004619 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004620 // screen supports SGR mouse codes since 4.7.0
4621 if (arg[1] >= 40700)
4622 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4623 else
4624 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM;
4625 }
4626
4627 // If no recognized terminal has set mouse behavior, assume xterm.
4628 if (term_props[TPR_MOUSE].tpr_status == TPR_UNKNOWN)
4629 {
4630 // Xterm version 277 supports SGR.
4631 // Xterm version >= 95 supports mouse dragging.
4632 if (version >= 277)
4633 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004634 else if (version >= 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004635 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_XTERM2;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004636 }
4637
4638 // Detect terminals that set $TERM to something like
4639 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004640 //
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004641 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004642 // Newer Gnome-terminal sends 65;6001;1.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004643 // xfce4-terminal sends 1;2802;0.
4644 // screen sends 83;40500;0
4645 // Assuming any version number over 2500 is not an
4646 // xterm (without the limit for rxvt and screen).
4647 if (arg[1] >= 2500)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004648 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004649
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004650 else if (version == 136 && arg[2] == 0)
4651 {
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004652 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004653
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004654 // PuTTY sends 0;136;0
4655 if (arg[0] == 0)
4656 {
4657 // supports sgr-like mouse reporting.
4658 term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
4659 }
4660 // vandyke SecureCRT sends 1;136;0
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004661 }
4662
4663 // Konsole sends 0;115;0
4664 else if (version == 115 && arg[0] == 0 && arg[2] == 0)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004665 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004666
4667 // GNU screen sends 83;30600;0, 83;40500;0, etc.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004668 // 30600/40500 is a version number of GNU screen. DA2 support is added
4669 // on 3.6. DCS string has a special meaning to GNU screen, but xterm
4670 // compatibility checking does not detect GNU screen.
4671 if (arg[0] == 83 && arg[1] >= 30600)
4672 {
4673 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4674 term_props[TPR_CURSOR_BLINK].tpr_status = TPR_NO;
4675 }
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004676
4677 // Xterm first responded to this request at patch level
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004678 // 95, so assume anything below 95 is not xterm and hopefully supports
4679 // the underline RGB color sequence.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004680 if (version < 95)
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004681 term_props[TPR_UNDERLINE_RGB].tpr_status = TPR_YES;
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004682
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004683 // Getting the cursor style is only supported properly by xterm since
4684 // version 279 (otherwise it returns 0x18).
4685 if (version < 279)
4686 term_props[TPR_CURSOR_STYLE].tpr_status = TPR_NO;
4687
4688 /*
4689 * Take action on the detected properties.
4690 */
4691
4692 // Unless the underline RGB color is expected to work, disable "t_8u".
4693 // It does not work for the real Xterm, it resets the background color.
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02004694 if (term_props[TPR_UNDERLINE_RGB].tpr_status != TPR_YES && *T_8U != NUL)
Bram Moolenaar8e20f752020-06-14 16:43:47 +02004695 set_string_option_direct((char_u *)"t_8u", -1, (char_u *)"",
4696 OPT_FREE, 0);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004697
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004698 // Only set 'ttymouse' automatically if it was not set
4699 // by the user already.
4700 if (!option_was_set((char_u *)"ttym")
4701 && (term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_XTERM2
4702 || term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR))
4703 {
4704 set_option_value((char_u *)"ttym", 0L,
4705 term_props[TPR_MOUSE].tpr_status == TPR_MOUSE_SGR
4706 ? (char_u *)"sgr" : (char_u *)"xterm2", 0);
4707 }
4708
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004709 // Only request the cursor style if t_SH and t_RS are
4710 // set. Only supported properly by xterm since version
4711 // 279 (otherwise it returns 0x18).
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004712 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004713 // Not for Terminal.app, it can't handle t_RS, it
4714 // echoes the characters to the screen.
4715 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004716 && term_props[TPR_CURSOR_STYLE].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004717 && *T_CSH != NUL
4718 && *T_CRS != NUL)
4719 {
4720 LOG_TR(("Sending cursor style request"));
4721 out_str(T_CRS);
4722 termrequest_sent(&rcs_status);
4723 need_flush = TRUE;
4724 }
4725
4726 // Only request the cursor blink mode if t_RC set. Not
4727 // for Gnome terminal, it can't handle t_RC, it
4728 // echoes the characters to the screen.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004729 // Only when getting the cursor style was detected to work.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004730 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004731 && term_props[TPR_CURSOR_BLINK].tpr_status == TPR_YES
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004732 && *T_CRC != NUL)
4733 {
4734 LOG_TR(("Sending cursor blink mode request"));
4735 out_str(T_CRC);
4736 termrequest_sent(&rbm_status);
4737 need_flush = TRUE;
4738 }
4739
4740 if (need_flush)
4741 out_flush();
4742 }
4743}
4744
4745/*
4746 * Handle a sequence with key and modifier, one of:
4747 * {lead}27;{modifier};{key}~
4748 * {lead}{key};{modifier}u
4749 * Returns the difference in length.
4750 */
4751 static int
4752handle_key_with_modifier(
4753 int *arg,
4754 int trail,
4755 int csi_len,
4756 int offset,
4757 char_u *buf,
4758 int bufsize,
4759 int *buflen)
4760{
4761 int key;
4762 int modifiers;
4763 int new_slen;
4764 char_u string[MAX_KEY_CODE_LEN + 1];
4765
4766 seenModifyOtherKeys = TRUE;
4767 if (trail == 'u')
4768 key = arg[0];
4769 else
4770 key = arg[2];
4771
4772 modifiers = decode_modifiers(arg[1]);
4773
Bram Moolenaaref6746f2020-06-20 14:43:23 +02004774 // May remove the shift modifier if it's already included in the key.
4775 modifiers = may_remove_shift_modifier(modifiers, key);
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004776
4777 // When used with Ctrl we always make a letter upper case,
4778 // so that mapping <C-H> and <C-h> are the same. Typing
4779 // <C-S-H> also uses "H" but modifier is different.
4780 if ((modifiers & MOD_MASK_CTRL) && ASCII_ISALPHA(key))
4781 key = TOUPPER_ASC(key);
4782
4783 // insert modifiers with KS_MODIFIER
4784 new_slen = modifiers2keycode(modifiers, &key, string);
4785
4786 if (has_mbyte)
4787 new_slen += (*mb_char2bytes)(key, string + new_slen);
4788 else
4789 string[new_slen++] = key;
4790
4791 if (put_string_in_typebuf(offset, csi_len, string, new_slen,
4792 buf, bufsize, buflen) == FAIL)
4793 return -1;
4794 return new_slen - csi_len + offset;
4795}
4796
4797/*
4798 * Handle a CSI escape sequence.
Bram Moolenaar517f00f2020-06-10 12:15:51 +02004799 * - Xterm version string.
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02004800 *
4801 * - Cursor position report: {lead}{row};{col}R
4802 * The final byte must be 'R'. It is used for checking the
4803 * ambiguous-width character state.
4804 *
4805 * - window position reply: {lead}3;{x};{y}t
4806 *
4807 * - key with modifiers when modifyOtherKeys is enabled:
4808 * {lead}27;{modifier};{key}~
4809 * {lead}{key};{modifier}u
4810 * Return 0 for no match, -1 for partial match, > 0 for full match.
4811 */
4812 static int
4813handle_csi(
4814 char_u *tp,
4815 int len,
4816 char_u *argp,
4817 int offset,
4818 char_u *buf,
4819 int bufsize,
4820 int *buflen,
4821 char_u *key_name,
4822 int *slen)
4823{
4824 int first = -1; // optional char right after {lead}
4825 int trail; // char that ends CSI sequence
4826 int arg[3] = {-1, -1, -1}; // argument numbers
4827 int argc; // number of arguments
4828 char_u *ap = argp;
4829 int csi_len;
4830
4831 // Check for non-digit after CSI.
4832 if (!VIM_ISDIGIT(*ap))
4833 first = *ap++;
4834
4835 // Find up to three argument numbers.
4836 for (argc = 0; argc < 3; )
4837 {
4838 if (ap >= tp + len)
4839 return -1;
4840 if (*ap == ';')
4841 arg[argc++] = -1; // omitted number
4842 else if (VIM_ISDIGIT(*ap))
4843 {
4844 arg[argc] = 0;
4845 for (;;)
4846 {
4847 if (ap >= tp + len)
4848 return -1;
4849 if (!VIM_ISDIGIT(*ap))
4850 break;
4851 arg[argc] = arg[argc] * 10 + (*ap - '0');
4852 ++ap;
4853 }
4854 ++argc;
4855 }
4856 if (*ap == ';')
4857 ++ap;
4858 else
4859 break;
4860 }
4861
4862 // mrxvt has been reported to have "+" in the version. Assume
4863 // the escape sequence ends with a letter or one of "{|}~".
4864 while (ap < tp + len
4865 && !(*ap >= '{' && *ap <= '~')
4866 && !ASCII_ISALPHA(*ap))
4867 ++ap;
4868 if (ap >= tp + len)
4869 return -1;
4870 trail = *ap;
4871 csi_len = (int)(ap - tp) + 1;
4872
4873 // Cursor position report: Eat it when there are 2 arguments
4874 // and it ends in 'R'. Also when u7_status is not "sent", it
4875 // may be from a previous Vim that just exited. But not for
4876 // <S-F3>, it sends something similar, check for row and column
4877 // to make sense.
4878 if (first == -1 && argc == 2 && trail == 'R')
4879 {
4880 handle_u7_response(arg, tp, csi_len);
4881
4882 key_name[0] = (int)KS_EXTRA;
4883 key_name[1] = (int)KE_IGNORE;
4884 *slen = csi_len;
4885 }
4886
4887 // Version string: Eat it when there is at least one digit and
4888 // it ends in 'c'
4889 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
4890 {
4891 handle_version_response(first, arg, argc, tp);
4892
4893 *slen = csi_len;
4894# ifdef FEAT_EVAL
4895 set_vim_var_string(VV_TERMRESPONSE, tp, *slen);
4896# endif
4897 apply_autocmds(EVENT_TERMRESPONSE,
4898 NULL, NULL, FALSE, curbuf);
4899 key_name[0] = (int)KS_EXTRA;
4900 key_name[1] = (int)KE_IGNORE;
4901 }
4902
4903 // Check blinking cursor from xterm:
4904 // {lead}?12;1$y set
4905 // {lead}?12;2$y not set
4906 //
4907 // {lead} can be <Esc>[ or CSI
4908 else if (rbm_status.tr_progress == STATUS_SENT
4909 && first == '?'
4910 && ap == argp + 6
4911 && arg[0] == 12
4912 && ap[-1] == '$'
4913 && trail == 'y')
4914 {
4915 initial_cursor_blink = (arg[1] == '1');
4916 rbm_status.tr_progress = STATUS_GOT;
4917 LOG_TR(("Received cursor blinking mode response: %s", tp));
4918 key_name[0] = (int)KS_EXTRA;
4919 key_name[1] = (int)KE_IGNORE;
4920 *slen = csi_len;
4921# ifdef FEAT_EVAL
4922 set_vim_var_string(VV_TERMBLINKRESP, tp, *slen);
4923# endif
4924 }
4925
4926 // Check for a window position response from the terminal:
4927 // {lead}3;{x};{y}t
4928 else if (did_request_winpos && argc == 3 && arg[0] == 3
4929 && trail == 't')
4930 {
4931 winpos_x = arg[1];
4932 winpos_y = arg[2];
4933 // got finished code: consume it
4934 key_name[0] = (int)KS_EXTRA;
4935 key_name[1] = (int)KE_IGNORE;
4936 *slen = csi_len;
4937
4938 if (--did_request_winpos <= 0)
4939 winpos_status.tr_progress = STATUS_GOT;
4940 }
4941
4942 // Key with modifier:
4943 // {lead}27;{modifier};{key}~
4944 // {lead}{key};{modifier}u
4945 else if ((arg[0] == 27 && argc == 3 && trail == '~')
4946 || (argc == 2 && trail == 'u'))
4947 {
4948 return len + handle_key_with_modifier(arg, trail,
4949 csi_len, offset, buf, bufsize, buflen);
4950 }
4951
4952 // else: Unknown CSI sequence. We could drop it, but then the
4953 // user can't create a map for it.
4954 return 0;
4955}
4956
4957/*
4958 * Handle an OSC sequence, fore/background color response from the terminal:
4959 *
4960 * {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
4961 * or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
4962 *
4963 * {code} is 10 for foreground, 11 for background
4964 * {lead} can be <Esc>] or OSC
4965 * {tail} can be '\007', <Esc>\ or STERM.
4966 *
4967 * Consume any code that starts with "{lead}11;", it's also
4968 * possible that "rgba" is following.
4969 */
4970 static int
4971handle_osc(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
4972{
4973 int i, j;
4974
4975 j = 1 + (tp[0] == ESC);
4976 if (len >= j + 3 && (argp[0] != '1'
4977 || (argp[1] != '1' && argp[1] != '0')
4978 || argp[2] != ';'))
4979 i = 0; // no match
4980 else
4981 for (i = j; i < len; ++i)
4982 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4983 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
4984 {
4985 int is_bg = argp[1] == '1';
4986 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
4987 && tp[j + 16] == '/';
4988
4989 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4990 && (is_4digit
4991 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
4992 {
4993 char_u *tp_r = tp + j + 7;
4994 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
4995 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
4996# ifdef FEAT_TERMINAL
4997 int rval, gval, bval;
4998
4999 rval = hexhex2nr(tp_r);
5000 gval = hexhex2nr(tp_b);
5001 bval = hexhex2nr(tp_g);
5002# endif
5003 if (is_bg)
5004 {
5005 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5006 *tp_b) ? "light" : "dark";
5007
5008 LOG_TR(("Received RBG response: %s", tp));
5009 rbg_status.tr_progress = STATUS_GOT;
5010# ifdef FEAT_TERMINAL
5011 bg_r = rval;
5012 bg_g = gval;
5013 bg_b = bval;
5014# endif
5015 if (!option_was_set((char_u *)"bg")
5016 && STRCMP(p_bg, new_bg_val) != 0)
5017 {
5018 // value differs, apply it
5019 set_option_value((char_u *)"bg", 0L,
5020 (char_u *)new_bg_val, 0);
5021 reset_option_was_set((char_u *)"bg");
5022 redraw_asap(CLEAR);
5023 }
5024 }
5025# ifdef FEAT_TERMINAL
5026 else
5027 {
5028 LOG_TR(("Received RFG response: %s", tp));
5029 rfg_status.tr_progress = STATUS_GOT;
5030 fg_r = rval;
5031 fg_g = gval;
5032 fg_b = bval;
5033 }
5034# endif
5035 }
5036
5037 // got finished code: consume it
5038 key_name[0] = (int)KS_EXTRA;
5039 key_name[1] = (int)KE_IGNORE;
5040 *slen = i + 1 + (tp[i] == ESC);
5041# ifdef FEAT_EVAL
5042 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5043 : VV_TERMRFGRESP, tp, *slen);
5044# endif
5045 break;
5046 }
5047 if (i == len)
5048 {
5049 LOG_TR(("not enough characters for RB"));
5050 return FAIL;
5051 }
5052 return OK;
5053}
5054
5055/*
5056 * Check for key code response from xterm:
5057 * {lead}{flag}+r<hex bytes><{tail}
5058 *
5059 * {lead} can be <Esc>P or DCS
5060 * {flag} can be '0' or '1'
5061 * {tail} can be Esc>\ or STERM
5062 *
5063 * Check for cursor shape response from xterm:
5064 * {lead}1$r<digit> q{tail}
5065 *
5066 * {lead} can be <Esc>P or DCS
5067 * {tail} can be <Esc>\ or STERM
5068 *
5069 * Consume any code that starts with "{lead}.+r" or "{lead}.$r".
5070 */
5071 static int
5072handle_dcs(char_u *tp, char_u *argp, int len, char_u *key_name, int *slen)
5073{
5074 int i, j;
5075
5076 j = 1 + (tp[0] == ESC);
5077 if (len < j + 3)
5078 i = len; // need more chars
5079 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
5080 i = 0; // no match
5081 else if (argp[1] == '+')
5082 // key code response
5083 for (i = j; i < len; ++i)
5084 {
5085 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5086 || tp[i] == STERM)
5087 {
5088 if (i - j >= 3)
5089 got_code_from_term(tp + j, i);
5090 key_name[0] = (int)KS_EXTRA;
5091 key_name[1] = (int)KE_IGNORE;
5092 *slen = i + 1 + (tp[i] == ESC);
5093 break;
5094 }
5095 }
5096 else
5097 {
5098 // Probably the cursor shape response. Make sure that "i"
5099 // is equal to "len" when there are not sufficient
5100 // characters.
5101 for (i = j + 3; i < len; ++i)
5102 {
5103 if (i - j == 3 && !isdigit(tp[i]))
5104 break;
5105 if (i - j == 4 && tp[i] != ' ')
5106 break;
5107 if (i - j == 5 && tp[i] != 'q')
5108 break;
5109 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5110 break;
5111 if ((i - j == 6 && tp[i] == STERM)
5112 || (i - j == 7 && tp[i] == '\\'))
5113 {
5114 int number = argp[3] - '0';
5115
5116 // 0, 1 = block blink, 2 = block
5117 // 3 = underline blink, 4 = underline
5118 // 5 = vertical bar blink, 6 = vertical bar
5119 number = number == 0 ? 1 : number;
5120 initial_cursor_shape = (number + 1) / 2;
5121 // The blink flag is actually inverted, compared to
5122 // the value set with T_SH.
5123 initial_cursor_shape_blink =
5124 (number & 1) ? FALSE : TRUE;
5125 rcs_status.tr_progress = STATUS_GOT;
5126 LOG_TR(("Received cursor shape response: %s", tp));
5127
5128 key_name[0] = (int)KS_EXTRA;
5129 key_name[1] = (int)KE_IGNORE;
5130 *slen = i + 1;
5131# ifdef FEAT_EVAL
5132 set_vim_var_string(VV_TERMSTYLERESP, tp, *slen);
5133# endif
5134 break;
5135 }
5136 }
5137 }
5138
5139 if (i == len)
5140 {
5141 // These codes arrive many together, each code can be
5142 // truncated at any point.
5143 LOG_TR(("not enough characters for XT"));
5144 return FAIL;
5145 }
5146 return OK;
5147}
Bram Moolenaar0ca8b5b2020-06-09 21:35:36 +02005148#endif // FEAT_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005149
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 * Check if typebuf.tb_buf[] contains a terminal key code.
5152 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
Bram Moolenaar975a8802020-06-06 22:36:24 +02005153 * + "max_offset"].
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005155 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 * With a match, the match is removed, the replacement code is inserted in
5157 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
5158 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005159 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
5160 * "buflen" is then the length of the string in buf[] and is updated for
5161 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 */
5163 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005164check_termcode(
5165 int max_offset,
5166 char_u *buf,
5167 int bufsize,
5168 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169{
5170 char_u *tp;
5171 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005172 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005173 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005175 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 int offset;
5177 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005178 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02005179 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005180 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005181 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 char_u string[MAX_KEY_CODE_LEN + 1];
5183 int i, j;
5184 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186
5187 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
5188
5189 /*
5190 * Speed up the checks for terminal codes by gathering all first bytes
5191 * used in termleader[]. Often this is just a single <Esc>.
5192 */
5193 if (need_gather)
5194 gather_termleader();
5195
5196 /*
5197 * Check at several positions in typebuf.tb_buf[], to catch something like
5198 * "x<Up>" that can be mapped. Stop at max_offset, because characters
5199 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005200 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 * This is used often, KEEP IT FAST!
5202 */
5203 for (offset = 0; offset < max_offset; ++offset)
5204 {
5205 if (buf == NULL)
5206 {
5207 if (offset >= typebuf.tb_len)
5208 break;
5209 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005210 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
5212 else
5213 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005214 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 break;
5216 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005217 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219
5220 /*
5221 * Don't check characters after K_SPECIAL, those are already
5222 * translated terminal chars (avoid translating ~@^Hx).
5223 */
5224 if (*tp == K_SPECIAL)
5225 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005226 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 continue;
5228 }
5229
5230 /*
5231 * Skip this position if the character does not appear as the first
5232 * character in term_strings. This speeds up a lot, since most
5233 * termcodes start with the same character (ESC or CSI).
5234 */
5235 i = *tp;
5236 for (p = termleader; *p && *p != i; ++p)
5237 ;
5238 if (*p == NUL)
5239 continue;
5240
5241 /*
5242 * Skip this position if p_ek is not set and tp[0] is an ESC and we
5243 * are in Insert mode.
5244 */
5245 if (*tp == ESC && !p_ek && (State & INSERT))
5246 continue;
5247
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005248 key_name[0] = NUL; // no key name found yet
5249 key_name[1] = NUL; // no key name found yet
5250 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251
5252#ifdef FEAT_GUI
5253 if (gui.in_use)
5254 {
5255 /*
5256 * GUI special key codes are all of the form [CSI xx].
5257 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005258 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
5260 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005261 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 slen = 3;
5263 key_name[0] = tp[1];
5264 key_name[1] = tp[2];
5265 }
5266 }
5267 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005268#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 {
5270 for (idx = 0; idx < tc_len; ++idx)
5271 {
5272 /*
5273 * Ignore the entry if we are not at the start of
5274 * typebuf.tb_buf[]
5275 * and there are not enough characters to make a match.
5276 * But only when the 'K' flag is in 'cpoptions'.
5277 */
5278 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02005279 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 if (cpo_koffset && offset && len < slen)
5281 continue;
5282 if (STRNCMP(termcodes[idx].code, tp,
5283 (size_t)(slen > len ? len : slen)) == 0)
5284 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005285 if (len < slen) // got a partial sequence
5286 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
5288 /*
5289 * When found a keypad key, check if there is another key
5290 * that matches and use that one. This makes <Home> to be
5291 * found instead of <kHome> when they produce the same
5292 * key code.
5293 */
5294 if (termcodes[idx].name[0] == 'K'
5295 && VIM_ISDIGIT(termcodes[idx].name[1]))
5296 {
5297 for (j = idx + 1; j < tc_len; ++j)
5298 if (termcodes[j].len == slen &&
5299 STRNCMP(termcodes[idx].code,
5300 termcodes[j].code, slen) == 0)
5301 {
5302 idx = j;
5303 break;
5304 }
5305 }
5306
5307 key_name[0] = termcodes[idx].name[0];
5308 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 break;
5310 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005311
5312 /*
5313 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005314 * <Esc>[123;*X (modslen == slen - 3)
5315 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
5316 * When there is a modifier the * matches a number.
5317 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005318 */
5319 if (termcodes[idx].modlen > 0)
5320 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005321 modslen = termcodes[idx].modlen;
5322 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005323 continue;
5324 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005325 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005326 {
5327 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005328
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005329 if (len <= modslen) // got a partial sequence
5330 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005331
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005332 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005333 slen = modslen + 1; // no modifiers
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005334 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005335 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005336 else
5337 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005338 // Skip over the digits, the final char must
5339 // follow. URXVT can use a negative value, thus
5340 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005341 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02005342 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005343 ;
5344 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005345 if (len < j) // got a partial sequence
5346 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005347 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005348 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005349
Bram Moolenaara529ce02017-06-22 22:37:57 +02005350 modifiers_start = tp + slen - 2;
5351
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005352 // Match! Convert modifier bits.
5353 n = atoi((char *)modifiers_start);
5354 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005355
5356 slen = j;
5357 }
5358 key_name[0] = termcodes[idx].name[0];
5359 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005360 break;
5361 }
5362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 }
5364 }
5365
5366#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02005367 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005368 // Mouse codes of DEC and pterm start with <ESC>[. When
5369 // detecting the start of these mouse codes they might as well be
5370 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005371# ifdef FEAT_MOUSE_DEC
5372 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01005373# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005374# ifdef FEAT_MOUSE_PTERM
5375 || key_name[0] == KS_PTERM_MOUSE
5376# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005377 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005379 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
5380
5381 /*
5382 * Check for responses from the terminal starting with {lead}:
5383 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01005384 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005385 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01005386 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005387 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01005388 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005389 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005390 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01005391 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005392 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005393 * - window position reply: {lead}3;{x};{y}t
5394 *
5395 * - key with modifiers when modifyOtherKeys is enabled:
5396 * {lead}27;{modifier};{key}~
5397 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01005398 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005399 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02005400 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005401 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005403 int resp = handle_csi(tp, len, argp, offset, buf,
5404 bufsize, buflen, key_name, &slen);
5405 if (resp != 0)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02005406 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005407# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005408 if (resp == -1)
5409 LOG_TR(("Not enough characters for CSI sequence"));
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005410# endif
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005411 return resp;
Bram Moolenaar9584b312013-03-13 19:29:28 +01005412 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005413 }
5414
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005415 // Check for fore/background color response from the terminal,
5416 // starting} with <Esc>] or OSC
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005417 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005418 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
5419 || tp[0] == OSC))
5420 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005421 if (handle_osc(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005422 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 }
5424
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005425 // Check for key code response from xterm,
5426 // starting with <Esc>P or DCS
Bram Moolenaarafd78262019-05-10 23:10:31 +02005427 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005428 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 || tp[0] == DCS))
5430 {
Bram Moolenaar218cb0f2020-06-09 21:26:36 +02005431 if (handle_dcs(tp, argp, len, key_name, &slen) == FAIL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005432 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 }
5434 }
5435#endif
5436
5437 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005438 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005440 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005442#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 /*
5444 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5445 * so that we know which window to scroll later.
5446 */
5447 if (gui.in_use
5448 && key_name[0] == (int)KS_EXTRA
5449 && (key_name[1] == (int)KE_X1MOUSE
5450 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005451 || key_name[1] == (int)KE_MOUSELEFT
5452 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 || key_name[1] == (int)KE_MOUSEDOWN
5454 || key_name[1] == (int)KE_MOUSEUP))
5455 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005456 char_u bytes[6];
5457 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5458
5459 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 return -1;
5461 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5462 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5463 slen += num_bytes;
5464 }
5465 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 /*
5468 * If it is a mouse click, get the coordinates.
5469 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005470 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005471#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005472 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005473#endif
5474#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005475 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005476#endif
5477#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005478 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005479#endif
5480#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005481 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005482#endif
5483#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005484 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005485#endif
5486#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005487 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005488#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005489 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005490 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005492 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5493 &modifiers) == -1)
5494 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
5497#ifdef FEAT_GUI
5498 /*
5499 * If using the GUI, then we get menu and scrollbar events.
5500 *
5501 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5502 * four bytes which are to be taken as a pointer to the vimmenu_T
5503 * structure.
5504 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005505 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005506 * is one byte with the tab index.
5507 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5509 * by one byte representing the scrollbar number, and then four bytes
5510 * representing a long_u which is the new value of the scrollbar.
5511 *
5512 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5513 * KE_FILLER followed by four bytes representing a long_u which is the
5514 * new value of the scrollbar.
5515 */
5516# ifdef FEAT_MENU
5517 else if (key_name[0] == (int)KS_MENU)
5518 {
5519 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005520 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 if (num_bytes == -1)
5523 return -1;
5524 current_menu = (vimmenu_T *)val;
5525 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005526
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005527 // The menu may have been deleted right after it was used, check
5528 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005529 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5530 {
5531 key_name[0] = KS_EXTRA;
5532 key_name[1] = (int)KE_IGNORE;
5533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
5535# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005536# ifdef FEAT_GUI_TABLINE
5537 else if (key_name[0] == (int)KS_TABLINE)
5538 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005539 // Selecting tabline tab or using its menu.
5540 char_u bytes[6];
5541 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5542
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005543 if (num_bytes == -1)
5544 return -1;
5545 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005546 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005547 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005548 slen += num_bytes;
5549 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005550 else if (key_name[0] == (int)KS_TABMENU)
5551 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005552 // Selecting tabline tab or using its menu.
5553 char_u bytes[6];
5554 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5555
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005556 if (num_bytes == -1)
5557 return -1;
5558 current_tab = (int)bytes[0];
5559 current_tabmenu = (int)bytes[1];
5560 slen += num_bytes;
5561 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005562# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563# ifndef USE_ON_FLY_SCROLL
5564 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5565 {
5566 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005567 char_u bytes[6];
5568 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005570 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 j = 0;
5572 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5573 && tp[j + 2] != NUL; ++i)
5574 {
5575 j += 3;
5576 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5577 if (num_bytes == -1)
5578 break;
5579 if (i == 0)
5580 current_scrollbar = (int)bytes[0];
5581 else if (current_scrollbar != (int)bytes[0])
5582 break;
5583 j += num_bytes;
5584 num_bytes = get_long_from_buf(tp + j, &val);
5585 if (num_bytes == -1)
5586 break;
5587 scrollbar_value = val;
5588 j += num_bytes;
5589 slen = j;
5590 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005591 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 return -1;
5593 }
5594 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5595 {
5596 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005597 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005599 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 j = 0;
5601 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5602 && tp[j + 2] != NUL; ++i)
5603 {
5604 j += 3;
5605 num_bytes = get_long_from_buf(tp + j, &val);
5606 if (num_bytes == -1)
5607 break;
5608 scrollbar_value = val;
5609 j += num_bytes;
5610 slen = j;
5611 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005612 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 return -1;
5614 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005615# endif // !USE_ON_FLY_SCROLL
5616#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005618 /*
5619 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5620 */
5621 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5622
5623 /*
5624 * Add any modifier codes to our string.
5625 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005626 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005627
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005628 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005629 key_name[0] = KEY2TERMCAP0(key);
5630 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005632 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005633 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005634 if (has_mbyte)
5635 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5636 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005637 string[new_slen++] = key_name[1];
5638 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005639 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5640 && key_name[1] == KE_IGNORE)
5641 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005642 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5643 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005644 retval = KEYLEN_REMOVED;
5645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 else
5647 {
5648 string[new_slen++] = K_SPECIAL;
5649 string[new_slen++] = key_name[0];
5650 string[new_slen++] = key_name[1];
5651 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005652 if (put_string_in_typebuf(offset, slen, string, new_slen,
5653 buf, bufsize, buflen) == FAIL)
5654 return -1;
5655 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 }
5657
Bram Moolenaar2951b772013-07-03 12:45:31 +02005658#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005659 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005660#endif
5661
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005662 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663}
5664
Bram Moolenaar9377df32017-10-15 13:22:01 +02005665#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005666/*
5667 * Get the text foreground color, if known.
5668 */
5669 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005670term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005671{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005672 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005673 {
5674 *r = fg_r;
5675 *g = fg_g;
5676 *b = fg_b;
5677 }
5678}
5679
5680/*
5681 * Get the text background color, if known.
5682 */
5683 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005684term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005685{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005686 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005687 {
5688 *r = bg_r;
5689 *g = bg_g;
5690 *b = bg_b;
5691 }
5692}
5693#endif
5694
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695/*
5696 * Replace any terminal code strings in from[] with the equivalent internal
5697 * vim representation. This is used for the "from" and "to" part of a
5698 * mapping, and the "to" part of a menu command.
5699 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5700 * '<'.
5701 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5702 *
5703 * The replacement is done in result[] and finally copied into allocated
5704 * memory. If this all works well *bufp is set to the allocated memory and a
5705 * pointer to it is returned. If something fails *bufp is set to NULL and from
5706 * is returned.
5707 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005708 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5709 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5710 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5711 * used instead of a CTRL-V.
5712 *
5713 * Flags:
5714 * REPTERM_FROM_PART see above
5715 * REPTERM_DO_LT also translate <lt>
5716 * REPTERM_SPECIAL always accept <key> notation
5717 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5718 *
5719 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5720 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005722 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005723replace_termcodes(
5724 char_u *from,
5725 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005726 int flags,
5727 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728{
5729 int i;
5730 int slen;
5731 int key;
5732 int dlen = 0;
5733 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005734 int do_backslash; // backslash is a special character
5735 int do_special; // recognize <> key codes
5736 int do_key_code; // recognize raw key codes
5737 char_u *result; // buffer for resulting string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
5739 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02005740 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
5741 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5743
5744 /*
5745 * Allocate space for the translation. Worst case a single character is
5746 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5747 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005748 result = alloc(STRLEN(from) * 6 + 1);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005749 if (result == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
5751 *bufp = NULL;
5752 return from;
5753 }
5754
5755 src = from;
5756
5757 /*
5758 * Check for #n at start only: function key n
5759 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005760 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 {
5762 result[dlen++] = K_SPECIAL;
5763 result[dlen++] = 'k';
5764 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005765 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005767 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 src += 2;
5769 }
5770
5771 /*
5772 * Copy each byte from *from to result[dlen]
5773 */
5774 while (*src != NUL)
5775 {
5776 /*
5777 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005778 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005780 if (do_special && ((flags & REPTERM_DO_LT)
5781 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 {
5783#ifdef FEAT_EVAL
5784 /*
5785 * Replace <SID> by K_SNR <script-nr> _.
5786 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5787 */
5788 if (STRNICMP(src, "<SID>", 5) == 0)
5789 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005790 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005791 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 else
5793 {
5794 src += 5;
5795 result[dlen++] = K_SPECIAL;
5796 result[dlen++] = (int)KS_EXTRA;
5797 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005798 sprintf((char *)result + dlen, "%ld",
5799 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 dlen += (int)STRLEN(result + dlen);
5801 result[dlen++] = '_';
5802 continue;
5803 }
5804 }
5805#endif
5806
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005807 slen = trans_special(&src, result + dlen, FSK_KEYCODE
5808 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
5809 did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 if (slen)
5811 {
5812 dlen += slen;
5813 continue;
5814 }
5815 }
5816
5817 /*
5818 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5819 * Note that this is also checked after replacing the <> form.
5820 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5821 * it could be a character in the file.
5822 */
5823 if (do_key_code)
5824 {
5825 i = find_term_bykeys(src);
5826 if (i >= 0)
5827 {
5828 result[dlen++] = K_SPECIAL;
5829 result[dlen++] = termcodes[i].name[0];
5830 result[dlen++] = termcodes[i].name[1];
5831 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005832 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 continue;
5834 }
5835 }
5836
5837#ifdef FEAT_EVAL
5838 if (do_special)
5839 {
5840 char_u *p, *s, len;
5841
5842 /*
5843 * Replace <Leader> by the value of "mapleader".
5844 * Replace <LocalLeader> by the value of "maplocalleader".
5845 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5846 */
5847 if (STRNICMP(src, "<Leader>", 8) == 0)
5848 {
5849 len = 8;
5850 p = get_var_value((char_u *)"g:mapleader");
5851 }
5852 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5853 {
5854 len = 13;
5855 p = get_var_value((char_u *)"g:maplocalleader");
5856 }
5857 else
5858 {
5859 len = 0;
5860 p = NULL;
5861 }
5862 if (len != 0)
5863 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005864 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5866 s = (char_u *)"\\";
5867 else
5868 s = p;
5869 while (*s != NUL)
5870 result[dlen++] = *s++;
5871 src += len;
5872 continue;
5873 }
5874 }
5875#endif
5876
5877 /*
5878 * Remove CTRL-V and ignore the next character.
5879 * For "from" side the CTRL-V at the end is included, for the "to"
5880 * part it is removed.
5881 * If 'cpoptions' does not contain 'B', also accept a backslash.
5882 */
5883 key = *src;
5884 if (key == Ctrl_V || (do_backslash && key == '\\'))
5885 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005886 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 if (*src == NUL)
5888 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02005889 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 result[dlen++] = key;
5891 break;
5892 }
5893 }
5894
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005895 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005896 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 {
5898 /*
5899 * If the character is K_SPECIAL, replace it with K_SPECIAL
5900 * KS_SPECIAL KE_FILLER.
5901 * If compiled with the GUI replace CSI with K_CSI.
5902 */
5903 if (*src == K_SPECIAL)
5904 {
5905 result[dlen++] = K_SPECIAL;
5906 result[dlen++] = KS_SPECIAL;
5907 result[dlen++] = KE_FILLER;
5908 }
5909# ifdef FEAT_GUI
5910 else if (*src == CSI)
5911 {
5912 result[dlen++] = K_SPECIAL;
5913 result[dlen++] = KS_EXTRA;
5914 result[dlen++] = (int)KE_CSI;
5915 }
5916# endif
5917 else
5918 result[dlen++] = *src;
5919 ++src;
5920 }
5921 }
5922 result[dlen] = NUL;
5923
5924 /*
5925 * Copy the new string to allocated memory.
5926 * If this fails, just return from.
5927 */
5928 if ((*bufp = vim_strsave(result)) != NULL)
5929 from = *bufp;
5930 vim_free(result);
5931 return from;
5932}
5933
5934/*
5935 * Find a termcode with keys 'src' (must be NUL terminated).
5936 * Return the index in termcodes[], or -1 if not found.
5937 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005938 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005939find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940{
5941 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005942 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943
5944 for (i = 0; i < tc_len; ++i)
5945 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005946 if (slen == termcodes[i].len
5947 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 return i;
5949 }
5950 return -1;
5951}
5952
5953/*
5954 * Gather the first characters in the terminal key codes into a string.
5955 * Used to speed up check_termcode().
5956 */
5957 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005958gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959{
5960 int i;
5961 int len = 0;
5962
5963#ifdef FEAT_GUI
5964 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005965 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966#endif
5967#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005968 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005969 termleader[len++] = DCS; // the termcode response starts with DCS
5970 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971#endif
5972 termleader[len] = NUL;
5973
5974 for (i = 0; i < tc_len; ++i)
5975 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5976 {
5977 termleader[len++] = termcodes[i].code[0];
5978 termleader[len] = NUL;
5979 }
5980
5981 need_gather = FALSE;
5982}
5983
5984/*
5985 * Show all termcodes (for ":set termcap")
5986 * This code looks a lot like showoptions(), but is different.
5987 */
5988 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005989show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
5991 int col;
5992 int *items;
5993 int item_count;
5994 int run;
5995 int row, rows;
5996 int cols;
5997 int i;
5998 int len;
5999
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006000#define INC3 27 // try to make three columns
6001#define INC2 40 // try to make two columns
6002#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006004 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006006 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 if (items == NULL)
6008 return;
6009
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006010 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01006011 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012
6013 /*
6014 * do the loop two times:
6015 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006016 * 2. display the medium items (medium length strings)
6017 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006019 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 {
6021 /*
6022 * collect the items in items[]
6023 */
6024 item_count = 0;
6025 for (i = 0; i < tc_len; i++)
6026 {
6027 len = show_one_termcode(termcodes[i].name,
6028 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006029 if (len <= INC3 - GAP ? run == 1
6030 : len <= INC2 - GAP ? run == 2
6031 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 items[item_count++] = i;
6033 }
6034
6035 /*
6036 * display the items
6037 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006038 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006040 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 if (cols == 0)
6042 cols = 1;
6043 rows = (item_count + cols - 1) / cols;
6044 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006045 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 rows = item_count;
6047 for (row = 0; row < rows && !got_int; ++row)
6048 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006049 msg_putchar('\n'); // go to next line
6050 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 break;
6052 col = 0;
6053 for (i = row; i < item_count; i += rows)
6054 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006055 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 show_one_termcode(termcodes[items[i]].name,
6057 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006058 if (run == 2)
6059 col += INC2;
6060 else
6061 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 }
6063 out_flush();
6064 ui_breakcheck();
6065 }
6066 }
6067 vim_free(items);
6068}
6069
6070/*
6071 * Show one termcode entry.
6072 * Output goes into IObuff[]
6073 */
6074 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006075show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076{
6077 char_u *p;
6078 int len;
6079
6080 if (name[0] > '~')
6081 {
6082 IObuff[0] = ' ';
6083 IObuff[1] = ' ';
6084 IObuff[2] = ' ';
6085 IObuff[3] = ' ';
6086 }
6087 else
6088 {
6089 IObuff[0] = 't';
6090 IObuff[1] = '_';
6091 IObuff[2] = name[0];
6092 IObuff[3] = name[1];
6093 }
6094 IObuff[4] = ' ';
6095
6096 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
6097 if (p[1] != 't')
6098 STRCPY(IObuff + 5, p);
6099 else
6100 IObuff[5] = NUL;
6101 len = (int)STRLEN(IObuff);
6102 do
6103 IObuff[len++] = ' ';
6104 while (len < 17);
6105 IObuff[len] = NUL;
6106 if (code == NULL)
6107 len += 4;
6108 else
6109 len += vim_strsize(code);
6110
6111 if (printit)
6112 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006113 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006115 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 else
6117 msg_outtrans(code);
6118 }
6119 return len;
6120}
6121
6122#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
6123/*
6124 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
6125 * termcap codes from the terminal itself.
6126 * We get them one by one to avoid a very long response string.
6127 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02006128static int xt_index_in = 0;
6129static int xt_index_out = 0;
6130
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006132req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
6134 xt_index_out = 0;
6135 xt_index_in = 0;
6136 req_more_codes_from_term();
6137}
6138
6139 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006140req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141{
6142 char buf[11];
6143 int old_idx = xt_index_out;
6144
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006145 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (exiting)
6147 return;
6148
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006149 // Send up to 10 more requests out than we received. Avoid sending too
6150 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
6152 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02006153 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02006154
Bram Moolenaarb255b902018-04-24 21:40:10 +02006155 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
6156 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 out_str_nf((char_u *)buf);
6158 ++xt_index_out;
6159 }
6160
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006161 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 if (xt_index_out != old_idx)
6163 out_flush();
6164}
6165
6166/*
6167 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
6168 * A "0" instead of the "1" indicates a code that isn't supported.
6169 * Both <name> and <string> are encoded in hex.
6170 * "code" points to the "0" or "1".
6171 */
6172 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006173got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174{
6175#define XT_LEN 100
6176 char_u name[3];
6177 char_u str[XT_LEN];
6178 int i;
6179 int j = 0;
6180 int c;
6181
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006182 // A '1' means the code is supported, a '0' means it isn't.
6183 // When half the length is > XT_LEN we can't use it.
6184 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
6186 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006187 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 name[0] = hexhex2nr(code + 3);
6189 name[1] = hexhex2nr(code + 5);
6190 name[2] = NUL;
6191 for (i = 0; key_names[i] != NULL; ++i)
6192 {
6193 if (STRCMP(key_names[i], name) == 0)
6194 {
6195 xt_index_in = i;
6196 break;
6197 }
6198 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02006199
Bram Moolenaarb255b902018-04-24 21:40:10 +02006200 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
6201
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 if (key_names[i] != NULL)
6203 {
6204 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
6205 str[j++] = c;
6206 str[j] = NUL;
6207 if (name[0] == 'C' && name[1] == 'o')
6208 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006209 // Color count is not a key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02006211 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 }
6213 else
6214 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006215 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 i = find_term_bykeys(str);
6217 if (i >= 0)
6218 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006219 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 }
6221 }
6222 }
6223
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006224 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 ++xt_index_in;
6226 req_more_codes_from_term();
6227}
6228
6229/*
6230 * Check if there are any unanswered requests and deal with them.
6231 * This is called before starting an external program or getting direct
6232 * keyboard input. We don't want responses to be send to that program or
6233 * handled as typed text.
6234 */
6235 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006236check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
6238 int c;
6239
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006240 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6242 return;
6243
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006244 // Vgetc() will check for and handle any response.
6245 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 ++no_mapping;
6247 ++allow_keys;
6248 for (;;)
6249 {
6250 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006251 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 break;
6253
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006254 // If a response is recognized it's replaced with K_IGNORE, must read
6255 // it from the input stream. If there is no K_IGNORE we can't do
6256 // anything, break here (there might be some responses further on, but
6257 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (c != K_SPECIAL && c != K_IGNORE)
6259 break;
6260 c = vgetc();
6261 if (c != K_IGNORE)
6262 {
6263 vungetc(c);
6264 break;
6265 }
6266 }
6267 --no_mapping;
6268 --allow_keys;
6269}
6270#endif
6271
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006272#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273static char ksme_str[20];
6274static char ksmr_str[20];
6275static char ksmd_str[20];
6276
6277/*
6278 * For Win32 console: update termcap codes for existing console attributes.
6279 */
6280 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006281update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
6283 struct builtin_term *p;
6284
6285 p = find_builtin_term(DEFAULT_TERM);
6286 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6287 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006288 attr | 0x08); // FOREGROUND_INTENSITY
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6290 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6291
6292 while (p->bt_string != NULL)
6293 {
6294 if (p->bt_entry == (int)KS_ME)
6295 p->bt_string = &ksme_str[0];
6296 else if (p->bt_entry == (int)KS_MR)
6297 p->bt_string = &ksmr_str[0];
6298 else if (p->bt_entry == (int)KS_MD)
6299 p->bt_string = &ksmd_str[0];
6300 ++p;
6301 }
6302}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006303
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006304# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006305# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006306struct ks_tbl_s
6307{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006308 int code; // value of KS_
6309 char *vtp; // code in vtp mode
6310 char *vtp2; // code in vtp2 mode
6311 char buf[KSSIZE]; // save buffer in non-vtp mode
6312 char vbuf[KSSIZE]; // save buffer in vtp mode
6313 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6314 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006315};
6316
6317static struct ks_tbl_s ks_tbl[] =
6318{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006319 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6320 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6321 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6322 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6323 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6324 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6325 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6326 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6327 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006328# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006329 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6330 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006331 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6332 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006333# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006334 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6335 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006336 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6337 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006338# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006339 {(int)KS_CCO, "256", "256"}, // colors
6340 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006341};
6342
6343 static struct builtin_term *
6344find_first_tcap(
6345 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006346 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006347{
6348 struct builtin_term *p;
6349
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006350 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006351 if (p->bt_entry == code)
6352 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006353 return NULL;
6354}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006355# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006356
6357/*
6358 * For Win32 console: replace the sequence immediately after termguicolors.
6359 */
6360 void
6361swap_tcap(void)
6362{
6363# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006364 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006365 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006366 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006367 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006368 int mode;
6369 enum
6370 {
6371 CMODEINDEX,
6372 CMODE24,
6373 CMODE256
6374 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006375
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006376 // buffer initialization
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006377 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006378 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006379 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006380 {
6381 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006382 if (bt != NULL)
6383 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006384 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6385 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6386 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6387
6388 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6389 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006390 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006391 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006392 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006393 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006394 }
6395
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006396 if (p_tgc)
6397 mode = CMODE24;
6398 else if (t_colors >= 256)
6399 mode = CMODE256;
6400 else
6401 mode = CMODEINDEX;
6402
6403 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006404 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006405 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6406 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006407 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006408 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006409 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006410 case CMODEINDEX:
6411 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6412 break;
6413 case CMODE24:
6414 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6415 break;
6416 default:
6417 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006418 }
6419 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006420 }
6421
6422 if (mode != curr_mode)
6423 {
6424 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006425 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006426 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6427 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006428 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006429 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006430 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006431 case CMODEINDEX:
6432 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6433 break;
6434 case CMODE24:
6435 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6436 break;
6437 default:
6438 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006439 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006440 }
6441 }
6442
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006443 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006444 }
6445# endif
6446}
6447
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006449
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006450#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006451 static int
6452hex_digit(int c)
6453{
6454 if (isdigit(c))
6455 return c - '0';
6456 c = TOLOWER_ASC(c);
6457 if (c >= 'a' && c <= 'f')
6458 return c - 'a' + 10;
6459 return 0x1ffffff;
6460}
6461
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006462# ifdef VIMDLL
6463 static guicolor_T
6464gui_adjust_rgb(guicolor_T c)
6465{
6466 if (gui.in_use)
6467 return c;
6468 else
6469 return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff);
6470}
6471# else
6472# define gui_adjust_rgb(c) (c)
6473# endif
6474
Bram Moolenaarab302212016-04-26 20:59:29 +02006475 guicolor_T
6476gui_get_color_cmn(char_u *name)
6477{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006478 // On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6479 // values as used by the MS-Windows GDI api. It should be used only for
6480 // MS-Windows GDI builds.
Bram Moolenaar4f974752019-02-17 17:44:42 +01006481# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar28726652017-01-06 18:16:19 +01006482# undef RGB
6483# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006484# ifndef RGB
6485# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6486# endif
6487# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006488 FILE *fd;
6489 char line[LINE_LEN];
6490 char_u *fname;
6491 int r, g, b, i;
6492 guicolor_T color;
6493
6494 struct rgbcolor_table_S {
6495 char_u *color_name;
6496 guicolor_T color;
6497 };
6498
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006499 // Only non X11 colors (not present in rgb.txt) and colors in
6500 // color_names[], useful when $VIMRUNTIME is not found,.
Bram Moolenaarab302212016-04-26 20:59:29 +02006501 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006502 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6503 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6504 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6505 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6506 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6507 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6508 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6509 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6510 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6511 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6512 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006513 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, // No X11
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006514 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006515 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6516 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006517 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006518 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006519 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006520 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6521 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6522 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6523 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6524 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006525 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, // No X11
6526 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, // No X11
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006527 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6528 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006529 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006530 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006531 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6532 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006533 };
6534
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006535 static struct rgbcolor_table_S *colornames_table;
6536 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006537
6538 if (name[0] == '#' && STRLEN(name) == 7)
6539 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006540 // Name is in "#rrggbb" format
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006541 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006542 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6543 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6544 if (color > 0xffffff)
6545 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006546 return gui_adjust_rgb(color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006547 }
6548
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006549 // Check if the name is one of the colors we know
Bram Moolenaarab302212016-04-26 20:59:29 +02006550 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6551 if (STRICMP(name, rgb_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006552 return gui_adjust_rgb(rgb_table[i].color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006553
6554 /*
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006555 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
Bram Moolenaarab302212016-04-26 20:59:29 +02006556 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006557 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006558 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006559 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006560
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006561 // colornames_table not yet initialized
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006562 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6563 if (fname == NULL)
6564 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006565
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006566 fd = fopen((char *)fname, "rt");
6567 vim_free(fname);
6568 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006569 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006570 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006571 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006572 size = -1; // don't try again
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006573 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006574 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006575
6576 for (counting = 1; counting >= 0; --counting)
6577 {
6578 if (!counting)
6579 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006580 colornames_table = ALLOC_MULT(struct rgbcolor_table_S, size);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006581 if (colornames_table == NULL)
6582 {
6583 fclose(fd);
6584 return INVALCOLOR;
6585 }
6586 rewind(fd);
6587 }
6588 size = 0;
6589
6590 while (!feof(fd))
6591 {
6592 size_t len;
6593 int pos;
6594
Bram Moolenaar42335f52018-09-13 15:33:43 +02006595 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006596 len = strlen(line);
6597
6598 if (len <= 1 || line[len - 1] != '\n')
6599 continue;
6600
6601 line[len - 1] = '\0';
6602
6603 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6604 if (i != 3)
6605 continue;
6606
6607 if (!counting)
6608 {
6609 char_u *s = vim_strsave((char_u *)line + pos);
6610
6611 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006612 {
6613 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006614 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006615 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006616 colornames_table[size].color_name = s;
6617 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6618 }
6619 size++;
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006620
6621 // The distributed rgb.txt has less than 1000 entries. Limit to
6622 // 10000, just in case the file was messed up.
6623 if (size == 10000)
6624 break;
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006625 }
6626 }
6627 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006628 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006629
6630 for (i = 0; i < size; i++)
6631 if (STRICMP(name, colornames_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006632 return gui_adjust_rgb(colornames_table[i].color);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006633
Bram Moolenaarab302212016-04-26 20:59:29 +02006634 return INVALCOLOR;
6635}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006636
6637 guicolor_T
6638gui_get_rgb_color_cmn(int r, int g, int b)
6639{
6640 guicolor_T color = RGB(r, g, b);
6641
6642 if (color > 0xffffff)
6643 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006644 return gui_adjust_rgb(color);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006645}
Bram Moolenaarab302212016-04-26 20:59:29 +02006646#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006647
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006648#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006649 || defined(PROTO)
6650static int cube_value[] = {
6651 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6652};
6653
6654static int grey_ramp[] = {
6655 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6656 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6657};
6658
Bram Moolenaar9894e392018-05-05 14:29:06 +02006659static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006660// R G B idx
6661 { 0, 0, 0, 1}, // black
6662 {224, 0, 0, 2}, // dark red
6663 { 0, 224, 0, 3}, // dark green
6664 {224, 224, 0, 4}, // dark yellow / brown
6665 { 0, 0, 224, 5}, // dark blue
6666 {224, 0, 224, 6}, // dark magenta
6667 { 0, 224, 224, 7}, // dark cyan
6668 {224, 224, 224, 8}, // light grey
6669
6670 {128, 128, 128, 9}, // dark grey
6671 {255, 64, 64, 10}, // light red
6672 { 64, 255, 64, 11}, // light green
6673 {255, 255, 64, 12}, // yellow
6674 { 64, 64, 255, 13}, // light blue
6675 {255, 64, 255, 14}, // light magenta
6676 { 64, 255, 255, 15}, // light cyan
6677 {255, 255, 255, 16}, // white
6678};
6679
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006680#define ANSI_INDEX_NONE 0
6681
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006682 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006683cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006684{
6685 int idx;
6686
6687 if (nr < 16)
6688 {
6689 *r = ansi_table[nr][0];
6690 *g = ansi_table[nr][1];
6691 *b = ansi_table[nr][2];
6692 *ansi_idx = ansi_table[nr][3];
6693 }
6694 else if (nr < 232)
6695 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006696 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006697 idx = nr - 16;
6698 *r = cube_value[idx / 36 % 6];
6699 *g = cube_value[idx / 6 % 6];
6700 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006701 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006702 }
6703 else if (nr < 256)
6704 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006705 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006706 idx = nr - 232;
6707 *r = grey_ramp[idx];
6708 *g = grey_ramp[idx];
6709 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006710 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006711 }
6712 else
6713 {
6714 *r = 0;
6715 *g = 0;
6716 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006717 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006718 }
6719}
6720#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006721
Bram Moolenaarf4140482020-02-15 23:06:45 +01006722/*
6723 * Replace K_BS by <BS> and K_DEL by <DEL>
6724 */
6725 void
6726term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len)
6727{
6728 int i;
6729 int c;
6730
6731 for (i = ta_len; i < ta_len + len; ++i)
6732 {
6733 if (ta_buf[i] == CSI && len - i > 2)
6734 {
6735 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6736 if (c == K_DEL || c == K_KDEL || c == K_BS)
6737 {
6738 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
6739 (size_t)(len - i - 2));
6740 if (c == K_DEL || c == K_KDEL)
6741 ta_buf[i] = DEL;
6742 else
6743 ta_buf[i] = Ctrl_H;
6744 len -= 2;
6745 }
6746 }
6747 else if (ta_buf[i] == '\r')
6748 ta_buf[i] = '\n';
6749 if (has_mbyte)
6750 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6751 }
6752}