blob: b4b5c091a83836b9a6de2c58c8071adadff17d56 [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 Moolenaar9377df32017-10-15 13:22:01 +0200129# ifdef FEAT_TERMINAL
Bram Moolenaarafd78262019-05-10 23:10:31 +0200130// Request foreground color report:
131static termrequest_T rfg_status = TERMREQUEST_INIT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200132static int fg_r = 0;
133static int fg_g = 0;
134static int fg_b = 0;
135static int bg_r = 255;
136static int bg_g = 255;
137static int bg_b = 255;
Bram Moolenaar9377df32017-10-15 13:22:01 +0200138# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200139
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100140// Request background color report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200141static termrequest_T rbg_status = TERMREQUEST_INIT;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200142
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100143// Request cursor blinking mode report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200144static termrequest_T rbm_status = TERMREQUEST_INIT;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200145
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100146// Request cursor style report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200147static termrequest_T rcs_status = TERMREQUEST_INIT;
Bram Moolenaar89894aa2018-03-05 22:43:10 +0100148
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100149// Request window's position report:
Bram Moolenaarafd78262019-05-10 23:10:31 +0200150static termrequest_T winpos_status = TERMREQUEST_INIT;
151
152static termrequest_T *all_termrequests[] = {
153 &crv_status,
154 &u7_status,
155# ifdef FEAT_TERMINAL
156 &rfg_status,
157# endif
158 &rbg_status,
159 &rbm_status,
160 &rcs_status,
161 &winpos_status,
162 NULL
163};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164# endif
165
166/*
167 * Don't declare these variables if termcap.h contains them.
168 * Autoconf checks if these variables should be declared extern (not all
169 * systems have them).
170 * Some versions define ospeed to be speed_t, but that is incompatible with
171 * BSD, where ospeed is short and speed_t is long.
172 */
173# ifndef HAVE_OSPEED
174# ifdef OSPEED_EXTERN
175extern short ospeed;
176# else
177short ospeed;
178# endif
179# endif
180# ifndef HAVE_UP_BC_PC
181# ifdef UP_BC_PC_EXTERN
182extern char *UP, *BC, PC;
183# else
184char *UP, *BC, PC;
185# endif
186# endif
187
188# define TGETSTR(s, p) vim_tgetstr((s), (p))
189# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100190static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100191#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100193static int detected_8bit = FALSE; // detected 8-bit terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200195#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100196// When the cursor shape was detected these values are used:
197// 1: block, 2: underline, 3: vertical bar
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200198static int initial_cursor_shape = 0;
Bram Moolenaar4db25542017-08-28 22:43:05 +0200199
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100200// The blink flag from the style response may be inverted from the actual
201// blinking state, xterm XORs the flags.
Bram Moolenaar4db25542017-08-28 22:43:05 +0200202static int initial_cursor_shape_blink = FALSE;
203
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100204// The blink flag from the blinking-cursor mode response
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200205static int initial_cursor_blink = FALSE;
Bram Moolenaar37b9b812017-08-19 23:23:43 +0200206#endif
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200207
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000208static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209{
210
211#if defined(FEAT_GUI)
212/*
213 * GUI pseudo term-cap.
214 */
215 {(int)KS_NAME, "gui"},
216 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
217 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
218# ifdef TERMINFO
219 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
220# else
221 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
222# endif
223 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
224# ifdef TERMINFO
225 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
226 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228# else
229 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
230 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# endif
233 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100234 // attributes switched on with 'h', off with * 'H'
235 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, // HL_ALL
236 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, // HL_INVERSE
237 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, // HL_BOLD
238 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, // HL_STANDOUT
239 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, // HL_STANDOUT
240 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, // HL_UNDERLINE
241 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, // HL_UNDERLINE
242 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, // HL_UNDERCURL
243 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, // HL_UNDERCURL
244 {(int)KS_STE, IF_EB("\033|4C", ESC_STR "|4C")}, // HL_STRIKETHROUGH
245 {(int)KS_STS, IF_EB("\033|4c", ESC_STR "|4c")}, // HL_STRIKETHROUGH
246 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, // HL_ITALIC
247 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, // HL_ITALIC
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
249 {(int)KS_MS, "y"},
250 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100251 {(int)KS_XN, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100252 {(int)KS_LE, "\b"}, // cursor-left = BS
253 {(int)KS_ND, "\014"}, // cursor-right = CTRL-L
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254# ifdef TERMINFO
255 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
256# else
257 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
258# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100259 // there are no key sequences here, the GUI sequences are recognized
260 // in check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261#endif
262
263#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
265# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
266/*
267 * Amiga console window, default for Amiga
268 */
269 {(int)KS_NAME, "amiga"},
270 {(int)KS_CE, "\033[K"},
271 {(int)KS_CD, "\033[J"},
272 {(int)KS_AL, "\033[L"},
273# ifdef TERMINFO
274 {(int)KS_CAL, "\033[%p1%dL"},
275# else
276 {(int)KS_CAL, "\033[%dL"},
277# endif
278 {(int)KS_DL, "\033[M"},
279# ifdef TERMINFO
280 {(int)KS_CDL, "\033[%p1%dM"},
281# else
282 {(int)KS_CDL, "\033[%dM"},
283# endif
284 {(int)KS_CL, "\014"},
285 {(int)KS_VI, "\033[0 p"},
286 {(int)KS_VE, "\033[1 p"},
287 {(int)KS_ME, "\033[0m"},
288 {(int)KS_MR, "\033[7m"},
289 {(int)KS_MD, "\033[1m"},
290 {(int)KS_SE, "\033[0m"},
291 {(int)KS_SO, "\033[33m"},
292 {(int)KS_US, "\033[4m"},
293 {(int)KS_UE, "\033[0m"},
294 {(int)KS_CZH, "\033[3m"},
295 {(int)KS_CZR, "\033[0m"},
296#if defined(__MORPHOS__) || defined(__AROS__)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100297 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100299 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
300 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100302 {(int)KS_CAB, "\033[4%dm"}, // set background color
303 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100305 {(int)KS_OP, "\033[m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
307 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100308 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 {(int)KS_LE, "\b"},
310# ifdef TERMINFO
311 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
312# else
313 {(int)KS_CM, "\033[%i%d;%dH"},
314# endif
315#if defined(__MORPHOS__)
316 {(int)KS_SR, "\033M"},
317#endif
318# ifdef TERMINFO
319 {(int)KS_CRI, "\033[%p1%dC"},
320# else
321 {(int)KS_CRI, "\033[%dC"},
322# endif
323 {K_UP, "\233A"},
324 {K_DOWN, "\233B"},
325 {K_LEFT, "\233D"},
326 {K_RIGHT, "\233C"},
327 {K_S_UP, "\233T"},
328 {K_S_DOWN, "\233S"},
329 {K_S_LEFT, "\233 A"},
330 {K_S_RIGHT, "\233 @"},
331 {K_S_TAB, "\233Z"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100332 {K_F1, "\233\060~"},// some compilers don't dig "\2330"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 {K_F2, "\233\061~"},
334 {K_F3, "\233\062~"},
335 {K_F4, "\233\063~"},
336 {K_F5, "\233\064~"},
337 {K_F6, "\233\065~"},
338 {K_F7, "\233\066~"},
339 {K_F8, "\233\067~"},
340 {K_F9, "\233\070~"},
341 {K_F10, "\233\071~"},
342 {K_S_F1, "\233\061\060~"},
343 {K_S_F2, "\233\061\061~"},
344 {K_S_F3, "\233\061\062~"},
345 {K_S_F4, "\233\061\063~"},
346 {K_S_F5, "\233\061\064~"},
347 {K_S_F6, "\233\061\065~"},
348 {K_S_F7, "\233\061\066~"},
349 {K_S_F8, "\233\061\067~"},
350 {K_S_F9, "\233\061\070~"},
351 {K_S_F10, "\233\061\071~"},
352 {K_HELP, "\233?~"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100353 {K_INS, "\233\064\060~"}, // 101 key keyboard
354 {K_PAGEUP, "\233\064\061~"}, // 101 key keyboard
355 {K_PAGEDOWN, "\233\064\062~"}, // 101 key keyboard
356 {K_HOME, "\233\064\064~"}, // 101 key keyboard
357 {K_END, "\233\064\065~"}, // 101 key keyboard
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
359 {BT_EXTRA_KEYS, ""},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100360 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, // shifted home key
361 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, // shifted insert key
362 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, // shifted end key
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363# endif
364
Bram Moolenaar041c7102020-05-30 18:14:57 +0200365# ifdef ALL_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366/*
Bram Moolenaar041c7102020-05-30 18:14:57 +0200367 * almost standard ANSI terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 {(int)KS_CE, "\033[K"},
370 {(int)KS_CD, "\033[J"},
371 {(int)KS_AL, "\033[L"},
372# ifdef TERMINFO
373 {(int)KS_CAL, "\033[%p1%dL"},
374# else
375 {(int)KS_CAL, "\033[%dL"},
376# endif
377 {(int)KS_DL, "\033[M"},
378# ifdef TERMINFO
379 {(int)KS_CDL, "\033[%p1%dM"},
380# else
381 {(int)KS_CDL, "\033[%dM"},
382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {(int)KS_CL, "\033[H\033[2J"},
384#ifdef notyet
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100385 {(int)KS_VI, "[VI]"}, // cursor invisible, VT320: CSI ? 25 l
386 {(int)KS_VE, "[VE]"}, // cursor visible, VT320: CSI ? 25 h
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100388 {(int)KS_ME, "\033[m"}, // normal mode
389 {(int)KS_MR, "\033[7m"}, // reverse
390 {(int)KS_MD, "\033[1m"}, // bold
391 {(int)KS_SO, "\033[31m"}, // standout mode: red
392 {(int)KS_SE, "\033[m"}, // standout end
393 {(int)KS_CZH, "\033[35m"}, // italic: purple
394 {(int)KS_CZR, "\033[m"}, // italic end
395 {(int)KS_US, "\033[4m"}, // underscore mode
396 {(int)KS_UE, "\033[m"}, // underscore end
397 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100399 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
400 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100402 {(int)KS_CAB, "\033[4%dm"}, // set background color
403 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100405 {(int)KS_OP, "\033[m"}, // reset colors
406 {(int)KS_MS, "y"}, // safe to move cur in reverse mode
407 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {(int)KS_LE, "\b"},
409# ifdef TERMINFO
410 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
411# else
412 {(int)KS_CM, "\033[%i%d;%dH"},
413# endif
414 {(int)KS_SR, "\033M"},
415# ifdef TERMINFO
416 {(int)KS_CRI, "\033[%p1%dC"},
417# else
418 {(int)KS_CRI, "\033[%dC"},
419# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420
421 {K_UP, "\033[A"},
422 {K_DOWN, "\033[B"},
423 {K_LEFT, "\033[D"},
424 {K_RIGHT, "\033[C"},
425# endif
426
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200427# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428/*
429 * standard ANSI terminal, default for unix
430 */
431 {(int)KS_NAME, "ansi"},
432 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
433 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
434# ifdef TERMINFO
435 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
436# else
437 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
438# endif
439 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
440# ifdef TERMINFO
441 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
442# else
443 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
444# endif
445 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
446 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
447 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
448 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100449 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 {(int)KS_LE, "\b"},
451# ifdef TERMINFO
452 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
453# else
454 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
455# endif
456# ifdef TERMINFO
457 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
458# else
459 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
460# endif
461# endif
462
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200463# if defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464/*
465 * These codes are valid when nansi.sys or equivalent has been installed.
466 * Function keys on a PC are preceded with a NUL. These are converted into
467 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
468 * CTRL-arrow is used instead of SHIFT-arrow.
469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 {(int)KS_NAME, "pcansi"},
471 {(int)KS_DL, "\033[M"},
472 {(int)KS_AL, "\033[L"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 {(int)KS_CE, "\033[K"},
474 {(int)KS_CL, "\033[2J"},
475 {(int)KS_ME, "\033[0m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100476 {(int)KS_MR, "\033[5m"}, // reverse: black on lightgrey
477 {(int)KS_MD, "\033[1m"}, // bold: white text
478 {(int)KS_SE, "\033[0m"}, // standout end
479 {(int)KS_SO, "\033[31m"}, // standout: white on blue
480 {(int)KS_CZH, "\033[34;43m"}, // italic mode: blue text on yellow
481 {(int)KS_CZR, "\033[0m"}, // italic mode end
482 {(int)KS_US, "\033[36;41m"}, // underscore mode: cyan text on red
483 {(int)KS_UE, "\033[0m"}, // underscore mode end
484 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100486 {(int)KS_CAB, "\033[4%p1%dm"},// set background color
487 {(int)KS_CAF, "\033[3%p1%dm"},// set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100489 {(int)KS_CAB, "\033[4%dm"}, // set background color
490 {(int)KS_CAF, "\033[3%dm"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100492 {(int)KS_OP, "\033[0m"}, // reset colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 {(int)KS_MS, "y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100494 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 {(int)KS_LE, "\b"},
496# ifdef TERMINFO
497 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
498# else
499 {(int)KS_CM, "\033[%i%d;%dH"},
500# endif
501# ifdef TERMINFO
502 {(int)KS_CRI, "\033[%p1%dC"},
503# else
504 {(int)KS_CRI, "\033[%dC"},
505# endif
506 {K_UP, "\316H"},
507 {K_DOWN, "\316P"},
508 {K_LEFT, "\316K"},
509 {K_RIGHT, "\316M"},
510 {K_S_LEFT, "\316s"},
511 {K_S_RIGHT, "\316t"},
512 {K_F1, "\316;"},
513 {K_F2, "\316<"},
514 {K_F3, "\316="},
515 {K_F4, "\316>"},
516 {K_F5, "\316?"},
517 {K_F6, "\316@"},
518 {K_F7, "\316A"},
519 {K_F8, "\316B"},
520 {K_F9, "\316C"},
521 {K_F10, "\316D"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100522 {K_F11, "\316\205"}, // guessed
523 {K_F12, "\316\206"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {K_S_F1, "\316T"},
525 {K_S_F2, "\316U"},
526 {K_S_F3, "\316V"},
527 {K_S_F4, "\316W"},
528 {K_S_F5, "\316X"},
529 {K_S_F6, "\316Y"},
530 {K_S_F7, "\316Z"},
531 {K_S_F8, "\316["},
532 {K_S_F9, "\316\\"},
533 {K_S_F10, "\316]"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100534 {K_S_F11, "\316\207"}, // guessed
535 {K_S_F12, "\316\210"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 {K_INS, "\316R"},
537 {K_DEL, "\316S"},
538 {K_HOME, "\316G"},
539 {K_END, "\316O"},
540 {K_PAGEDOWN, "\316Q"},
541 {K_PAGEUP, "\316I"},
542# endif
543
Bram Moolenaar4f974752019-02-17 17:44:42 +0100544# if defined(MSWIN) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545/*
546 * These codes are valid for the Win32 Console . The entries that start with
547 * ESC | are translated into console calls in os_win32.c. The function keys
548 * are also translated in os_win32.c.
549 */
550 {(int)KS_NAME, "win32"},
Bram Moolenaar6982f422019-02-16 16:48:01 +0100551 {(int)KS_CE, "\033|K"}, // clear to end of line
552 {(int)KS_AL, "\033|L"}, // add new blank line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100554 {(int)KS_CAL, "\033|%p1%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100556 {(int)KS_CAL, "\033|%dL"}, // add number of new blank lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100558 {(int)KS_DL, "\033|M"}, // delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100560 {(int)KS_CDL, "\033|%p1%dM"}, // delete number of lines
561 {(int)KS_CSV, "\033|%p1%d;%p2%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100563 {(int)KS_CDL, "\033|%dM"}, // delete number of lines
564 {(int)KS_CSV, "\033|%d;%dV"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100566 {(int)KS_CL, "\033|J"}, // clear screen
567 {(int)KS_CD, "\033|j"}, // clear to end of display
568 {(int)KS_VI, "\033|v"}, // cursor invisible
569 {(int)KS_VE, "\033|V"}, // cursor visible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
Bram Moolenaar6982f422019-02-16 16:48:01 +0100571 {(int)KS_ME, "\033|0m"}, // normal
572 {(int)KS_MR, "\033|112m"}, // reverse: black on lightgray
573 {(int)KS_MD, "\033|15m"}, // bold: white on black
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574#if 1
Bram Moolenaar6982f422019-02-16 16:48:01 +0100575 {(int)KS_SO, "\033|31m"}, // standout: white on blue
576 {(int)KS_SE, "\033|0m"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577#else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100578 {(int)KS_SO, "\033|F"}, // standout: high intensity
579 {(int)KS_SE, "\033|f"}, // standout end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580#endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100581 {(int)KS_CZH, "\033|225m"}, // italic: blue text on yellow
582 {(int)KS_CZR, "\033|0m"}, // italic end
583 {(int)KS_US, "\033|67m"}, // underscore: cyan text on red
584 {(int)KS_UE, "\033|0m"}, // underscore end
585 {(int)KS_CCO, "16"}, // allow 16 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100587 {(int)KS_CAB, "\033|%p1%db"}, // set background color
588 {(int)KS_CAF, "\033|%p1%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100590 {(int)KS_CAB, "\033|%db"}, // set background color
591 {(int)KS_CAF, "\033|%df"}, // set foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592# endif
593
Bram Moolenaar6982f422019-02-16 16:48:01 +0100594 {(int)KS_MS, "y"}, // save to move cur in reverse mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100596 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 {(int)KS_LE, "\b"},
598# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100599 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100601 {(int)KS_CM, "\033|%i%d;%dH"}, // cursor motion
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602# endif
Bram Moolenaar6982f422019-02-16 16:48:01 +0100603 {(int)KS_VB, "\033|B"}, // visual bell
604 {(int)KS_TI, "\033|S"}, // put terminal in termcap mode
605 {(int)KS_TE, "\033|E"}, // out of termcap mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606# ifdef TERMINFO
Bram Moolenaar6982f422019-02-16 16:48:01 +0100607 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608# else
Bram Moolenaar6982f422019-02-16 16:48:01 +0100609 {(int)KS_CS, "\033|%i%d;%dr"}, // scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100611# ifdef FEAT_TERMGUICOLORS
612 {(int)KS_8F, "\033|38;2;%lu;%lu;%lum"},
613 {(int)KS_8B, "\033|48;2;%lu;%lu;%lum"},
614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
616 {K_UP, "\316H"},
617 {K_DOWN, "\316P"},
618 {K_LEFT, "\316K"},
619 {K_RIGHT, "\316M"},
620 {K_S_UP, "\316\304"},
621 {K_S_DOWN, "\316\317"},
622 {K_S_LEFT, "\316\311"},
623 {K_C_LEFT, "\316s"},
624 {K_S_RIGHT, "\316\313"},
625 {K_C_RIGHT, "\316t"},
626 {K_S_TAB, "\316\017"},
627 {K_F1, "\316;"},
628 {K_F2, "\316<"},
629 {K_F3, "\316="},
630 {K_F4, "\316>"},
631 {K_F5, "\316?"},
632 {K_F6, "\316@"},
633 {K_F7, "\316A"},
634 {K_F8, "\316B"},
635 {K_F9, "\316C"},
636 {K_F10, "\316D"},
637 {K_F11, "\316\205"},
638 {K_F12, "\316\206"},
639 {K_S_F1, "\316T"},
640 {K_S_F2, "\316U"},
641 {K_S_F3, "\316V"},
642 {K_S_F4, "\316W"},
643 {K_S_F5, "\316X"},
644 {K_S_F6, "\316Y"},
645 {K_S_F7, "\316Z"},
646 {K_S_F8, "\316["},
647 {K_S_F9, "\316\\"},
648 {K_S_F10, "\316]"},
649 {K_S_F11, "\316\207"},
650 {K_S_F12, "\316\210"},
651 {K_INS, "\316R"},
652 {K_DEL, "\316S"},
653 {K_HOME, "\316G"},
654 {K_S_HOME, "\316\302"},
655 {K_C_HOME, "\316w"},
656 {K_END, "\316O"},
657 {K_S_END, "\316\315"},
658 {K_C_END, "\316u"},
659 {K_PAGEDOWN, "\316Q"},
660 {K_PAGEUP, "\316I"},
661 {K_KPLUS, "\316N"},
662 {K_KMINUS, "\316J"},
663 {K_KMULTIPLY, "\316\067"},
664 {K_K0, "\316\332"},
665 {K_K1, "\316\336"},
666 {K_K2, "\316\342"},
667 {K_K3, "\316\346"},
668 {K_K4, "\316\352"},
669 {K_K5, "\316\356"},
670 {K_K6, "\316\362"},
671 {K_K7, "\316\366"},
672 {K_K8, "\316\372"},
673 {K_K9, "\316\376"},
Bram Moolenaarb70a47b2019-03-30 22:11:21 +0100674 {K_BS, "\316x"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675# endif
676
677# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
678/*
679 * VT320 is working as an ANSI terminal compatible DEC terminal.
680 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 * TODO:- rewrite ESC[ codes to CSI
682 * - keyboard languages (CSI ? 26 n)
683 */
684 {(int)KS_NAME, "vt320"},
685 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
686 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
687# ifdef TERMINFO
688 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
689# else
690 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
691# endif
692 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
693# ifdef TERMINFO
694 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
695# else
696 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
697# endif
698 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000699 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100700 {(int)KS_CCO, "8"}, // allow 8 colors
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
702 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100703 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, // bold mode
704 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},// normal mode
705 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},// exit underscore mode
706 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, // underscore mode
707 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, // italic mode: blue text on yellow
708 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, // italic mode end
709 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, // set background color (ANSI)
710 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, // set foreground color (ANSI)
711 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, // set screen background color
712 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {(int)KS_MS, "y"},
714 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100715 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {(int)KS_LE, "\b"},
717# ifdef TERMINFO
718 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
719 ESC_STR "[%i%p1%d;%p2%dH")},
720# else
721 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
722# endif
723# ifdef TERMINFO
724 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
725# else
726 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
727# endif
728 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
729 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
730 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
731 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200732 // Note: cursor key sequences for application cursor mode are omitted,
733 // because they interfere with typed commands: <Esc>OA.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000734 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
735 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
736 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
737 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
738 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
740 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
741 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
742 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
743 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000744 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
746 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
747 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100748 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, // Help
749 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, // Select
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
751 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
752 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
753 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
754 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
755 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
756 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
757 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
758 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
759 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
Bram Moolenaare6882bd2018-07-03 17:16:59 +0200760 // These sequences starting with <Esc> O may interfere with what the user
761 // is typing. Remove these if that bothers you.
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100762 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, // keypad plus
763 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, // keypad minus
764 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, // keypad /
765 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, // keypad *
766 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, // keypad Enter
767 {K_K0, IF_EB("\033Op", ESC_STR "Op")}, // keypad 0
768 {K_K1, IF_EB("\033Oq", ESC_STR "Oq")}, // keypad 1
769 {K_K2, IF_EB("\033Or", ESC_STR "Or")}, // keypad 2
770 {K_K3, IF_EB("\033Os", ESC_STR "Os")}, // keypad 3
771 {K_K4, IF_EB("\033Ot", ESC_STR "Ot")}, // keypad 4
772 {K_K5, IF_EB("\033Ou", ESC_STR "Ou")}, // keypad 5
773 {K_K6, IF_EB("\033Ov", ESC_STR "Ov")}, // keypad 6
774 {K_K7, IF_EB("\033Ow", ESC_STR "Ow")}, // keypad 7
775 {K_K8, IF_EB("\033Ox", ESC_STR "Ox")}, // keypad 8
776 {K_K9, IF_EB("\033Oy", ESC_STR "Oy")}, // keypad 9
777 {K_BS, "\x7f"}, // for some reason 0177 doesn't work
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778# endif
779
780# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
781/*
782 * Ordinary vt52
783 */
784 {(int)KS_NAME, "vt52"},
785 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
786 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100787# ifdef TERMINFO
788 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
789 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
790# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100794 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
796 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 {K_UP, IF_EB("\033A", ESC_STR "A")},
798 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
799 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
800 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100801 {K_F1, IF_EB("\033P", ESC_STR "P")},
802 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
803 {K_F3, IF_EB("\033R", ESC_STR "R")},
804# ifdef __MINT__
805 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
806 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
807 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
808 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
809 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
811 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
812 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
813 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {K_F4, IF_EB("\033S", ESC_STR "S")},
815 {K_F5, IF_EB("\033T", ESC_STR "T")},
816 {K_F6, IF_EB("\033U", ESC_STR "U")},
817 {K_F7, IF_EB("\033V", ESC_STR "V")},
818 {K_F8, IF_EB("\033W", ESC_STR "W")},
819 {K_F9, IF_EB("\033X", ESC_STR "X")},
820 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
821 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
822 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
823 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
824 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
825 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
826 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
827 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
828 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
829 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
830 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
831 {K_INS, IF_EB("\033I", ESC_STR "I")},
832 {K_HOME, IF_EB("\033E", ESC_STR "E")},
833 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
834 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
835# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 {(int)KS_MS, "y"},
838# endif
839# endif
840
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200841# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200842 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
844 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
845# ifdef TERMINFO
846 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
847# else
848 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
849# endif
850 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
851# ifdef TERMINFO
852 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
853# else
854 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
855# endif
856# ifdef TERMINFO
857 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
858 ESC_STR "[%i%p1%d;%p2%dr")},
859# else
860 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
861# endif
862 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
863 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
864 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
865 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
866 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
867 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
868 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200869 {(int)KS_STE, IF_EB("\033[29m", ESC_STR "[29m")},
870 {(int)KS_STS, IF_EB("\033[9m", ESC_STR "[9m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 {(int)KS_MS, "y"},
872 {(int)KS_UT, "y"},
873 {(int)KS_LE, "\b"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200874 {(int)KS_VI, IF_EB("\033[?25l", ESC_STR "[?25l")},
875 {(int)KS_VE, IF_EB("\033[?25h", ESC_STR "[?25h")},
Bram Moolenaar93c92ef2017-08-19 21:11:57 +0200876 {(int)KS_VS, IF_EB("\033[?12h", ESC_STR "[?12h")},
Bram Moolenaarce1c3272017-08-20 15:05:15 +0200877 {(int)KS_CVS, IF_EB("\033[?12l", ESC_STR "[?12l")},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200878# ifdef TERMINFO
879 {(int)KS_CSH, IF_EB("\033[%p1%d q", ESC_STR "[%p1%d q")},
880# else
881 {(int)KS_CSH, IF_EB("\033[%d q", ESC_STR "[%d q")},
882# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +0200883 {(int)KS_CRC, IF_EB("\033[?12$p", ESC_STR "[?12$p")},
Bram Moolenaar3eee06e2017-08-19 19:40:50 +0200884 {(int)KS_CRS, IF_EB("\033P$q q\033\\", ESC_STR "P$q q" ESC_STR "\\")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885# ifdef TERMINFO
886 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
887 ESC_STR "[%i%p1%d;%p2%dH")},
888# else
889 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
890# endif
891 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
892# ifdef TERMINFO
893 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
894# else
895 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
896# endif
897 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
898 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
899# ifdef FEAT_XTERM_SAVE
900 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
Bram Moolenaard02e5082020-02-08 14:22:53 +0100901 {(int)KS_TE, IF_EB("\033[?47l\0338",
902 ESC_STR_nc "[?47l" ESC_STR_nc "8")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903# endif
Bram Moolenaar4b570182019-10-20 19:53:22 +0200904 {(int)KS_CTI, IF_EB("\033[>4;2m", ESC_STR_nc "[>4;2m")},
905 {(int)KS_CTE, IF_EB("\033[>4;m", ESC_STR_nc "[>4;m")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
907 {(int)KS_CIE, "\007"},
908 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
909 {(int)KS_FS, "\007"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200910 {(int)KS_CSC, IF_EB("\033]12;", ESC_STR "]12;")},
911 {(int)KS_CEC, "\007"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912# ifdef TERMINFO
913 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
914 ESC_STR "[8;%p1%d;%p2%dt")},
915 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
916 ESC_STR "[3;%p1%d;%p2%dt")},
Bram Moolenaarba6ec182017-04-04 22:41:10 +0200917 {(int)KS_CGP, IF_EB("\033[13t", ESC_STR "[13t")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918# else
919 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
920 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%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# endif
923 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200924 {(int)KS_RFG, IF_EB("\033]10;?\007", ESC_STR "]10;?\007")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200925 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100926 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaar61be73b2016-04-29 22:59:22 +0200927# ifdef FEAT_TERMGUICOLORS
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100928 // These are printf strings, not terminal codes.
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200929 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
930 {(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 +0200931 {(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 +0200932# endif
Bram Moolenaare023e882020-05-31 16:42:30 +0200933 {(int)KS_CAU, IF_EB("\033[58;5;%dm", ESC_STR "[58;5;%dm")},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100934 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
935 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200936 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
937 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
938 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
939 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000940
941 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
942 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
943 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
944 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100945 // An extra set of cursor keys for vt100 mode
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000946 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
947 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
948 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
949 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100950 // An extra set of function keys for vt100 mode
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000951 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
952 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
953 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
954 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000955 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
956 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
957 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
958 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
959 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
960 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
961 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
962 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
963 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
964 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
965 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
966 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000968 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
969 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
970 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
971 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100972 // {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")},
973 // {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000974 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100975 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, // other Home
976 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, // other Home
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000977 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100978 // {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")},
979 // {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000980 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100981 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, // other End
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000982 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000983 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
984 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100985 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, // keypad plus
986 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, // keypad minus
987 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, // keypad /
988 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, // keypad *
989 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, // keypad Enter
990 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, // keypad .
991 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, // keypad 0
992 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, // keypad 1
993 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, // keypad 2
994 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, // keypad 3
995 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, // keypad 4
996 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, // keypad 5
997 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, // keypad 6
998 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, // keypad 7
999 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, // keypad 8
1000 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, // keypad 9
1001 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, // keypad Del
1002 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, // paste start
1003 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
1005 {BT_EXTRA_KEYS, ""},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001006 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, // F0
1007 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, // F13
1008 // F14 and F15 are missing, because they send the same codes as the undo
1009 // and help key, although they don't work on all keyboards.
1010 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, // F16
1011 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, // F17
1012 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, // F18
1013 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, // F19
1014 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001015
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001016 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, // F21
1017 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, // F22
1018 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, // F23
1019 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, // F24
1020 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, // F25
1021 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, // F26
1022 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, // F27
1023 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, // F28
1024 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, // F29
1025 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001026
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001027 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, // F31
1028 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, // F32
1029 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, // F33
1030 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, // F34
1031 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, // F35
1032 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, // F36
1033 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034# endif
1035
1036# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1037/*
1038 * iris-ansi for Silicon Graphics machines.
1039 */
1040 {(int)KS_NAME, "iris-ansi"},
1041 {(int)KS_CE, "\033[K"},
1042 {(int)KS_CD, "\033[J"},
1043 {(int)KS_AL, "\033[L"},
1044# ifdef TERMINFO
1045 {(int)KS_CAL, "\033[%p1%dL"},
1046# else
1047 {(int)KS_CAL, "\033[%dL"},
1048# endif
1049 {(int)KS_DL, "\033[M"},
1050# ifdef TERMINFO
1051 {(int)KS_CDL, "\033[%p1%dM"},
1052# else
1053 {(int)KS_CDL, "\033[%dM"},
1054# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001055#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056# ifdef TERMINFO
1057 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1058# else
1059 {(int)KS_CS, "\033[%i%d;%dr"},
1060# endif
1061#endif
1062 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001063 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
1064 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 {(int)KS_TI, "\033[=6h"},
1066 {(int)KS_TE, "\033[=6l"},
1067 {(int)KS_SE, "\033[21;27m"},
1068 {(int)KS_SO, "\033[1;7m"},
1069 {(int)KS_ME, "\033[m"},
1070 {(int)KS_MR, "\033[7m"},
1071 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001072 {(int)KS_CCO, "8"}, // allow 8 colors
1073 {(int)KS_CZH, "\033[3m"}, // italic mode on
1074 {(int)KS_CZR, "\033[23m"}, // italic mode off
1075 {(int)KS_US, "\033[4m"}, // underline on
1076 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001078 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
1079 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
1080 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
1081 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001083 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
1084 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
1085 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
1086 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001088 {(int)KS_MS, "y"}, // guessed
1089 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 {(int)KS_LE, "\b"},
1091# ifdef TERMINFO
1092 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1093# else
1094 {(int)KS_CM, "\033[%i%d;%dH"},
1095# endif
1096 {(int)KS_SR, "\033M"},
1097# ifdef TERMINFO
1098 {(int)KS_CRI, "\033[%p1%dC"},
1099# else
1100 {(int)KS_CRI, "\033[%dC"},
1101# endif
1102 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001103 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001105 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106# ifdef TERMINFO
1107 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1108 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1109# else
1110 {(int)KS_CWS, "\033[203;%d;%d/y"},
1111 {(int)KS_CWP, "\033[205;%d;%d/y"},
1112# endif
1113 {K_UP, "\033[A"},
1114 {K_DOWN, "\033[B"},
1115 {K_LEFT, "\033[D"},
1116 {K_RIGHT, "\033[C"},
1117 {K_S_UP, "\033[161q"},
1118 {K_S_DOWN, "\033[164q"},
1119 {K_S_LEFT, "\033[158q"},
1120 {K_S_RIGHT, "\033[167q"},
1121 {K_F1, "\033[001q"},
1122 {K_F2, "\033[002q"},
1123 {K_F3, "\033[003q"},
1124 {K_F4, "\033[004q"},
1125 {K_F5, "\033[005q"},
1126 {K_F6, "\033[006q"},
1127 {K_F7, "\033[007q"},
1128 {K_F8, "\033[008q"},
1129 {K_F9, "\033[009q"},
1130 {K_F10, "\033[010q"},
1131 {K_F11, "\033[011q"},
1132 {K_F12, "\033[012q"},
1133 {K_S_F1, "\033[013q"},
1134 {K_S_F2, "\033[014q"},
1135 {K_S_F3, "\033[015q"},
1136 {K_S_F4, "\033[016q"},
1137 {K_S_F5, "\033[017q"},
1138 {K_S_F6, "\033[018q"},
1139 {K_S_F7, "\033[019q"},
1140 {K_S_F8, "\033[020q"},
1141 {K_S_F9, "\033[021q"},
1142 {K_S_F10, "\033[022q"},
1143 {K_S_F11, "\033[023q"},
1144 {K_S_F12, "\033[024q"},
1145 {K_INS, "\033[139q"},
1146 {K_HOME, "\033[H"},
1147 {K_END, "\033[146q"},
1148 {K_PAGEUP, "\033[150q"},
1149 {K_PAGEDOWN, "\033[154q"},
1150# endif
1151
1152# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1153/*
1154 * for debugging
1155 */
1156 {(int)KS_NAME, "debug"},
1157 {(int)KS_CE, "[CE]"},
1158 {(int)KS_CD, "[CD]"},
1159 {(int)KS_AL, "[AL]"},
1160# ifdef TERMINFO
1161 {(int)KS_CAL, "[CAL%p1%d]"},
1162# else
1163 {(int)KS_CAL, "[CAL%d]"},
1164# endif
1165 {(int)KS_DL, "[DL]"},
1166# ifdef TERMINFO
1167 {(int)KS_CDL, "[CDL%p1%d]"},
1168# else
1169 {(int)KS_CDL, "[CDL%d]"},
1170# endif
1171# ifdef TERMINFO
1172 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1173# else
1174 {(int)KS_CS, "[%dCS%d]"},
1175# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001176# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001178# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180# endif
1181# ifdef TERMINFO
1182 {(int)KS_CAB, "[CAB%p1%d]"},
1183 {(int)KS_CAF, "[CAF%p1%d]"},
1184 {(int)KS_CSB, "[CSB%p1%d]"},
1185 {(int)KS_CSF, "[CSF%p1%d]"},
1186# else
1187 {(int)KS_CAB, "[CAB%d]"},
1188 {(int)KS_CAF, "[CAF%d]"},
1189 {(int)KS_CSB, "[CSB%d]"},
1190 {(int)KS_CSF, "[CSF%d]"},
1191# endif
Bram Moolenaare023e882020-05-31 16:42:30 +02001192 {(int)KS_CAU, "[CAU%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 {(int)KS_OP, "[OP]"},
1194 {(int)KS_LE, "[LE]"},
1195 {(int)KS_CL, "[CL]"},
1196 {(int)KS_VI, "[VI]"},
1197 {(int)KS_VE, "[VE]"},
1198 {(int)KS_VS, "[VS]"},
1199 {(int)KS_ME, "[ME]"},
1200 {(int)KS_MR, "[MR]"},
1201 {(int)KS_MB, "[MB]"},
1202 {(int)KS_MD, "[MD]"},
1203 {(int)KS_SE, "[SE]"},
1204 {(int)KS_SO, "[SO]"},
1205 {(int)KS_UE, "[UE]"},
1206 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001207 {(int)KS_UCE, "[UCE]"},
1208 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001209 {(int)KS_STE, "[STE]"},
1210 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {(int)KS_MS, "[MS]"},
1212 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001213 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214# ifdef TERMINFO
1215 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1216# else
1217 {(int)KS_CM, "[%dCM%d]"},
1218# endif
1219 {(int)KS_SR, "[SR]"},
1220# ifdef TERMINFO
1221 {(int)KS_CRI, "[CRI%p1%d]"},
1222# else
1223 {(int)KS_CRI, "[CRI%d]"},
1224# endif
1225 {(int)KS_VB, "[VB]"},
1226 {(int)KS_KS, "[KS]"},
1227 {(int)KS_KE, "[KE]"},
1228 {(int)KS_TI, "[TI]"},
1229 {(int)KS_TE, "[TE]"},
1230 {(int)KS_CIS, "[CIS]"},
1231 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001232 {(int)KS_CSC, "[CSC]"},
1233 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {(int)KS_TS, "[TS]"},
1235 {(int)KS_FS, "[FS]"},
1236# ifdef TERMINFO
1237 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1238 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1239# else
1240 {(int)KS_CWS, "[%dCWS%d]"},
1241 {(int)KS_CWP, "[%dCWP%d]"},
1242# endif
1243 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001244 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001245 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001246 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {K_UP, "[KU]"},
1248 {K_DOWN, "[KD]"},
1249 {K_LEFT, "[KL]"},
1250 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001251 {K_XUP, "[xKU]"},
1252 {K_XDOWN, "[xKD]"},
1253 {K_XLEFT, "[xKL]"},
1254 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 {K_S_UP, "[S-KU]"},
1256 {K_S_DOWN, "[S-KD]"},
1257 {K_S_LEFT, "[S-KL]"},
1258 {K_C_LEFT, "[C-KL]"},
1259 {K_S_RIGHT, "[S-KR]"},
1260 {K_C_RIGHT, "[C-KR]"},
1261 {K_F1, "[F1]"},
1262 {K_XF1, "[xF1]"},
1263 {K_F2, "[F2]"},
1264 {K_XF2, "[xF2]"},
1265 {K_F3, "[F3]"},
1266 {K_XF3, "[xF3]"},
1267 {K_F4, "[F4]"},
1268 {K_XF4, "[xF4]"},
1269 {K_F5, "[F5]"},
1270 {K_F6, "[F6]"},
1271 {K_F7, "[F7]"},
1272 {K_F8, "[F8]"},
1273 {K_F9, "[F9]"},
1274 {K_F10, "[F10]"},
1275 {K_F11, "[F11]"},
1276 {K_F12, "[F12]"},
1277 {K_S_F1, "[S-F1]"},
1278 {K_S_XF1, "[S-xF1]"},
1279 {K_S_F2, "[S-F2]"},
1280 {K_S_XF2, "[S-xF2]"},
1281 {K_S_F3, "[S-F3]"},
1282 {K_S_XF3, "[S-xF3]"},
1283 {K_S_F4, "[S-F4]"},
1284 {K_S_XF4, "[S-xF4]"},
1285 {K_S_F5, "[S-F5]"},
1286 {K_S_F6, "[S-F6]"},
1287 {K_S_F7, "[S-F7]"},
1288 {K_S_F8, "[S-F8]"},
1289 {K_S_F9, "[S-F9]"},
1290 {K_S_F10, "[S-F10]"},
1291 {K_S_F11, "[S-F11]"},
1292 {K_S_F12, "[S-F12]"},
1293 {K_HELP, "[HELP]"},
1294 {K_UNDO, "[UNDO]"},
1295 {K_BS, "[BS]"},
1296 {K_INS, "[INS]"},
1297 {K_KINS, "[KINS]"},
1298 {K_DEL, "[DEL]"},
1299 {K_KDEL, "[KDEL]"},
1300 {K_HOME, "[HOME]"},
1301 {K_S_HOME, "[C-HOME]"},
1302 {K_C_HOME, "[C-HOME]"},
1303 {K_KHOME, "[KHOME]"},
1304 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001305 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {K_END, "[END]"},
1307 {K_S_END, "[C-END]"},
1308 {K_C_END, "[C-END]"},
1309 {K_KEND, "[KEND]"},
1310 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001311 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {K_PAGEUP, "[PAGEUP]"},
1313 {K_PAGEDOWN, "[PAGEDOWN]"},
1314 {K_KPAGEUP, "[KPAGEUP]"},
1315 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1316 {K_MOUSE, "[MOUSE]"},
1317 {K_KPLUS, "[KPLUS]"},
1318 {K_KMINUS, "[KMINUS]"},
1319 {K_KDIVIDE, "[KDIVIDE]"},
1320 {K_KMULTIPLY, "[KMULTIPLY]"},
1321 {K_KENTER, "[KENTER]"},
1322 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001323 {K_PS, "[PASTE-START]"},
1324 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 {K_K0, "[K0]"},
1326 {K_K1, "[K1]"},
1327 {K_K2, "[K2]"},
1328 {K_K3, "[K3]"},
1329 {K_K4, "[K4]"},
1330 {K_K5, "[K5]"},
1331 {K_K6, "[K6]"},
1332 {K_K7, "[K7]"},
1333 {K_K8, "[K8]"},
1334 {K_K9, "[K9]"},
1335# endif
1336
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001337#endif // NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
1339/*
1340 * The most minimal terminal: only clear screen and cursor positioning
1341 * Always included.
1342 */
1343 {(int)KS_NAME, "dumb"},
1344 {(int)KS_CL, "\014"},
1345#ifdef TERMINFO
1346 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1347 ESC_STR "[%i%p1%d;%p2%dH")},
1348#else
1349 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1350#endif
1351
1352/*
1353 * end marker
1354 */
1355 {(int)KS_NAME, NULL}
1356
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001357}; // end of builtin_termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001359#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001360 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001361termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001362{
Bram Moolenaarab302212016-04-26 20:59:29 +02001363 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001364}
1365
1366 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001367termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001368{
1369 guicolor_T t;
1370
1371 if (*name == NUL)
1372 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001373 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001374
1375 if (t == INVALCOLOR)
Bram Moolenaar87202262020-05-24 17:23:45 +02001376 semsg(_(e_alloc_color), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001377 return t;
1378}
1379
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001380 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001381termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001382{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001383 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001384}
1385#endif
1386
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387/*
1388 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390#ifdef AMIGA
1391# define DEFAULT_TERM (char_u *)"amiga"
1392#endif
1393
1394#ifdef MSWIN
1395# define DEFAULT_TERM (char_u *)"win32"
1396#endif
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#if defined(UNIX) && !defined(__MINT__)
1399# define DEFAULT_TERM (char_u *)"ansi"
1400#endif
1401
1402#ifdef __MINT__
1403# define DEFAULT_TERM (char_u *)"vt52"
1404#endif
1405
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406#ifdef VMS
1407# define DEFAULT_TERM (char_u *)"vt320"
1408#endif
1409
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001410#ifdef __HAIKU__
1411# undef DEFAULT_TERM
1412# define DEFAULT_TERM (char_u *)"xterm"
1413#endif
1414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415#ifndef DEFAULT_TERM
1416# define DEFAULT_TERM (char_u *)"dumb"
1417#endif
1418
1419/*
1420 * Term_strings contains currently used terminal output strings.
1421 * It is initialized with the default values by parse_builtin_tcap().
1422 * The values can be changed by setting the option with the same name.
1423 */
1424char_u *(term_strings[(int)KS_LAST + 1]);
1425
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001426static int need_gather = FALSE; // need to fill termleader[]
1427static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001429static int check_for_codes = FALSE; // check for key code response
1430static int is_not_xterm = FALSE; // recognized not-really-xterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431#endif
1432
1433 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001434find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435{
1436 struct builtin_term *p;
1437
1438 p = builtin_termcaps;
1439 while (p->bt_string != NULL)
1440 {
1441 if (p->bt_entry == (int)KS_NAME)
1442 {
1443#ifdef UNIX
1444 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1445 return p;
1446 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1447 return p;
1448 else
1449#endif
1450#ifdef VMS
1451 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1452 return p;
1453 else
1454#endif
1455 if (STRCMP(term, p->bt_string) == 0)
1456 return p;
1457 }
1458 ++p;
1459 }
1460 return p;
1461}
1462
1463/*
1464 * Parsing of the builtin termcap entries.
1465 * Caller should check if 'name' is a valid builtin term.
1466 * The terminal's name is not set, as this is already done in termcapinit().
1467 */
1468 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001469parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 struct builtin_term *p;
1472 char_u name[2];
1473 int term_8bit;
1474
1475 p = find_builtin_term(term);
1476 term_8bit = term_is_8bit(term);
1477
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001478 // Do not parse if builtin term not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (p->bt_string == NULL)
1480 return;
1481
1482 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1483 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001484 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001486 // Only set the value if it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 if (term_strings[p->bt_entry] == NULL
1488 || term_strings[p->bt_entry] == empty_option)
1489 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001490#ifdef FEAT_EVAL
1491 int opt_idx = -1;
1492#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001493 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1495 {
1496 char_u *s, *t;
1497
1498 s = vim_strsave((char_u *)p->bt_string);
1499 if (s != NULL)
1500 {
1501 for (t = s; *t; ++t)
1502 if (term_7to8bit(t))
1503 {
1504 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001505 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 }
1507 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001508#ifdef FEAT_EVAL
1509 opt_idx =
1510#endif
1511 set_term_option_alloced(
1512 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
1514 }
1515 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001516 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001518#ifdef FEAT_EVAL
1519 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1520#endif
1521 }
1522#ifdef FEAT_EVAL
1523 set_term_option_sctx_idx(NULL, opt_idx);
1524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 }
1526 }
1527 else
1528 {
1529 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1530 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1531 if (find_termcode(name) == NULL)
1532 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1533 }
1534 }
1535}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001536
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537/*
1538 * Set number of colors.
1539 * Store it as a number in t_colors.
1540 * Store it as a string in T_CCO (using nr_colors[]).
1541 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001542 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001543set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001545 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
1547 t_colors = nr;
1548 if (t_colors > 1)
1549 sprintf((char *)nr_colors, "%d", t_colors);
1550 else
1551 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001552 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001554
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001555#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001556/*
1557 * Set the color count to "val" and redraw if it changed.
1558 */
1559 static void
1560may_adjust_color_count(int val)
1561{
1562 if (val != t_colors)
1563 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001564 // Nr of colors changed, initialize highlighting and
1565 // redraw everything. This causes a redraw, which usually
1566 // clears the message. Try keeping the message if it
1567 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001568 set_keep_msg_from_hist();
1569 set_color_count(val);
1570 init_highlight(TRUE, FALSE);
1571# ifdef DEBUG_TERMRESPONSE
1572 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001573 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001574
Bram Moolenaarb255b902018-04-24 21:40:10 +02001575 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001576 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001577# else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001578 redraw_asap(CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001579# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001580 }
1581}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582#endif
1583
1584#ifdef HAVE_TGETENT
1585static char *(key_names[]) =
1586{
Bram Moolenaar87202262020-05-24 17:23:45 +02001587# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001588 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 "#2", "#4", "%i", "*7",
1593 "k1", "k2", "k3", "k4", "k5", "k6",
1594 "k7", "k8", "k9", "k;", "F1", "F2",
1595 "%1", "&8", "kb", "kI", "kD", "kh",
1596 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1597 NULL
1598};
1599#endif
1600
Bram Moolenaar9289df52018-05-10 14:40:57 +02001601#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001602 static void
1603get_term_entries(int *height, int *width)
1604{
1605 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001606 enum SpecialKey dest; // index in term_strings[]
1607 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001608 } string_names[] =
1609 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1610 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1611 {KS_CL, "cl"}, {KS_CD, "cd"},
1612 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1613 {KS_ME, "me"}, {KS_MR, "mr"},
1614 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1615 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1616 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1617 {KS_STE,"Te"}, {KS_STS,"Ts"},
1618 {KS_CM, "cm"}, {KS_SR, "sr"},
1619 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1620 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001621 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001622 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001623 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_CAU,"AU"},
1624 {KS_LE, "le"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001625 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1626 {KS_VS, "vs"}, {KS_CVS, "VS"},
1627 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1628 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1629 {KS_TS, "ts"}, {KS_FS, "fs"},
1630 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1631 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1632 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
Bram Moolenaare023e882020-05-31 16:42:30 +02001633 {KS_8F, "8f"}, {KS_8B, "8b"}, {KS_8U, "8u"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001634 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1635 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001636 {KS_CST, "ST"}, {KS_CRT, "RT"},
1637 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001638 {(enum SpecialKey)0, NULL}
1639 };
1640 int i;
1641 char_u *p;
1642 static char_u tstrbuf[TBUFSZ];
1643 char_u *tp = tstrbuf;
1644
1645 /*
1646 * get output strings
1647 */
1648 for (i = 0; string_names[i].name != NULL; ++i)
1649 {
1650 if (TERM_STR(string_names[i].dest) == NULL
1651 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001652 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001653 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001654#ifdef FEAT_EVAL
1655 set_term_option_sctx_idx(string_names[i].name, -1);
1656#endif
1657 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001658 }
1659
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001660 // tgetflag() returns 1 if the flag is present, 0 if not and
1661 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001662 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1663 T_MS = (char_u *)"y";
1664 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1665 T_XS = (char_u *)"y";
1666 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1667 T_XN = (char_u *)"y";
1668 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1669 T_DB = (char_u *)"y";
1670 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1671 T_DA = (char_u *)"y";
1672 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1673 T_UT = (char_u *)"y";
1674
1675 /*
1676 * get key codes
1677 */
1678 for (i = 0; key_names[i] != NULL; ++i)
1679 if (find_termcode((char_u *)key_names[i]) == NULL)
1680 {
1681 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001682 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001683 if (p != NULL
1684 && (*p != Ctrl_H
1685 || key_names[i][0] != 'k'
1686 || key_names[i][1] != 'l'))
1687 add_termcode((char_u *)key_names[i], p, FALSE);
1688 }
1689
1690 if (*height == 0)
1691 *height = tgetnum("li");
1692 if (*width == 0)
1693 *width = tgetnum("co");
1694
1695 /*
1696 * Get number of colors (if not done already).
1697 */
1698 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001699 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001700 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001701#ifdef FEAT_EVAL
1702 set_term_option_sctx_idx("Co", -1);
1703#endif
1704 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001705
1706# ifndef hpux
1707 BC = (char *)TGETSTR("bc", &tp);
1708 UP = (char *)TGETSTR("up", &tp);
1709 p = TGETSTR("pc", &tp);
1710 if (p)
1711 PC = *p;
1712# endif
1713}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001714#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001715
1716 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001717report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001718{
1719 struct builtin_term *termp;
1720
1721 mch_errmsg("\r\n");
1722 if (error_msg != NULL)
1723 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001724 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001725 mch_errmsg("\r\n");
1726 }
1727 mch_errmsg("'");
1728 mch_errmsg((char *)term);
1729 mch_errmsg(_("' not known. Available builtin terminals are:"));
1730 mch_errmsg("\r\n");
1731 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1732 {
1733 if (termp->bt_entry == (int)KS_NAME)
1734 {
1735#ifdef HAVE_TGETENT
1736 mch_errmsg(" builtin_");
1737#else
1738 mch_errmsg(" ");
1739#endif
1740 mch_errmsg(termp->bt_string);
1741 mch_errmsg("\r\n");
1742 }
1743 }
1744}
1745
1746 static void
1747report_default_term(char_u *term)
1748{
1749 mch_errmsg(_("defaulting to '"));
1750 mch_errmsg((char *)term);
1751 mch_errmsg("'\r\n");
1752 if (emsg_silent == 0)
1753 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001754 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001755 out_flush();
1756 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001757 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001758 }
1759}
1760
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761/*
1762 * Set terminal options for terminal "term".
1763 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1764 *
1765 * While doing this, until ttest(), some options may be NULL, be careful.
1766 */
1767 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001768set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769{
1770 struct builtin_term *termp;
1771#ifdef HAVE_TGETENT
1772 int builtin_first = p_tbi;
1773 int try;
1774 int termcap_cleared = FALSE;
1775#endif
1776 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001777 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 char_u *bs_p, *del_p;
1779
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001780 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001781 if (silent_mode)
1782 return OK;
1783
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001784 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
1786 if (term_is_builtin(term))
1787 {
1788 term += 8;
1789#ifdef HAVE_TGETENT
1790 builtin_first = 1;
1791#endif
1792 }
1793
1794/*
1795 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1796 * If builtin_first is TRUE:
1797 * 0. try builtin termcap
1798 * 1. try external termcap
1799 * 2. if both fail default to a builtin terminal
1800 * If builtin_first is FALSE:
1801 * 1. try external termcap
1802 * 2. try builtin termcap, if both fail default to a builtin terminal
1803 */
1804#ifdef HAVE_TGETENT
1805 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1806 {
1807 /*
1808 * Use external termcap
1809 */
1810 if (try == 1)
1811 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
1814 /*
1815 * If the external termcap does not have a matching entry, try the
1816 * builtin ones.
1817 */
1818 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 if (!termcap_cleared)
1821 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001822 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 termcap_cleared = TRUE;
1824 }
1825
Bram Moolenaar69e05692018-05-10 14:11:52 +02001826 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 }
1828 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001829 else // try == 0 || try == 2
1830#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 /*
1832 * Use builtin termcap
1833 */
1834 {
1835#ifdef HAVE_TGETENT
1836 /*
1837 * If builtin termcap was already used, there is no need to search
1838 * for the builtin termcap again, quit now.
1839 */
1840 if (try == 2 && builtin_first && termcap_cleared)
1841 break;
1842#endif
1843 /*
1844 * search for 'term' in builtin_termcaps[]
1845 */
1846 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001847 if (termp->bt_string == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
1849#ifdef HAVE_TGETENT
1850 /*
1851 * If try == 0, first try the external termcap. If that is not
1852 * found we'll get back here with try == 2.
1853 * If termcap_cleared is set we used the external termcap,
1854 * don't complain about not finding the term in the builtin
1855 * termcap.
1856 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001857 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001859 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 break;
1861#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001862 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001864 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 if (starting != NO_SCREEN)
1866 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001867 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 wait_return(TRUE);
1869 return FAIL;
1870 }
1871 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001872 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001873 set_string_option_direct((char_u *)"term", -1, term,
1874 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 display_errors();
1876 }
1877 out_flush();
1878#ifdef HAVE_TGETENT
1879 if (!termcap_cleared)
1880 {
1881#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001882 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883#ifdef HAVE_TGETENT
1884 termcap_cleared = TRUE;
1885 }
1886#endif
1887 parse_builtin_tcap(term);
1888#ifdef FEAT_GUI
1889 if (term_is_gui(term))
1890 {
1891 out_flush();
1892 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001893 // If starting the GUI failed, don't do any of the other
1894 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (!gui.in_use)
1896 return FAIL;
1897#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001898 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899#endif
1900 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001901#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
1903#ifdef HAVE_TGETENT
1904 }
1905#endif
1906
1907/*
1908 * special: There is no info in the termcap about whether the cursor
1909 * positioning is relative to the start of the screen or to the start of the
1910 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1911 * relative.
1912 */
1913 if (STRCMP(term, "pcterm") == 0)
1914 T_CCS = (char_u *)"yes";
1915 else
1916 T_CCS = empty_option;
1917
1918#ifdef UNIX
1919/*
1920 * Any "stty" settings override the default for t_kb from the termcap.
1921 * This is in os_unix.c, because it depends a lot on the version of unix that
1922 * is being used.
1923 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1924 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001925# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 get_stty();
1929#endif
1930
1931/*
1932 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1933 * didn't work, use the default CTRL-H
1934 * The default for t_kD is DEL, unless t_kb is DEL.
1935 * The vim_strsave'd strings are probably lost forever, well it's only two
1936 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1937 * directly.
1938 */
1939#ifdef FEAT_GUI
1940 if (!gui.in_use)
1941#endif
1942 {
1943 bs_p = find_termcode((char_u *)"kb");
1944 del_p = find_termcode((char_u *)"kD");
1945 if (bs_p == NULL || *bs_p == NUL)
1946 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1947 if ((del_p == NULL || *del_p == NUL) &&
1948 (bs_p == NULL || *bs_p != DEL))
1949 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1950 }
1951
1952#if defined(UNIX) || defined(VMS)
1953 term_is_xterm = vim_is_xterm(term);
1954#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02001955#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3cea8a92019-10-17 21:55:24 +02001956 is_not_xterm = FALSE;
1957 is_mac_terminal = FALSE;
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02001958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001960#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 /*
1962 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1963 * The termcode for the mouse is added as a side effect in option.c.
1964 */
1965 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001966 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001968# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001969 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
1971 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001972 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 else
1974 p = (char_u *)"xterm";
1975 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001976# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001978 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001980 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1981 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001982 reset_option_was_set((char_u *)"ttym");
1983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001985# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001987# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001989 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001991#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001996 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (STRCMP(term, DEFAULT_TERM) != 0)
1998 term_console = FALSE;
1999 else
2000 {
2001 term_console = TRUE;
2002# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002003 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004# endif
2005 }
2006#endif
2007
2008#if defined(UNIX) || defined(VMS)
2009 /*
2010 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2011 */
2012 if (vim_is_fastterm(term))
2013 p_tf = TRUE;
2014#endif
2015#ifdef USE_TERM_CONSOLE
2016 /*
2017 * 'ttyfast' is default on consoles
2018 */
2019 if (term_console)
2020 p_tf = TRUE;
2021#endif
2022
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002023 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002025 full_screen = TRUE; // we can use termcap codes from now on
2026 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002028 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002029 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030#endif
2031
2032 /*
2033 * Initialize the terminal with the appropriate termcap codes.
2034 * Set the mouse and window title if possible.
2035 * Don't do this when starting, need to parse the .vimrc first, because it
2036 * may redefine t_TI etc.
2037 */
2038 if (starting != NO_SCREEN)
2039 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002040 starttermcap(); // may change terminal mode
2041 setmouse(); // may start using the mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042#ifdef FEAT_TITLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002043 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044#endif
2045 }
2046
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002047 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 if (width <= 0 || height <= 0)
2049 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002050 // termcap failed to report size
2051 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002053#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002054 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002056 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057#endif
2058 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002059 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 if (starting != NO_SCREEN)
2061 {
2062 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002063 scroll_region_reset(); // In case Rows changed
2064 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002067 buf_T *buf;
2068 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 /*
2071 * Execute the TermChanged autocommands for each buffer that is
2072 * loaded.
2073 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002074 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
2076 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002077 {
2078 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2080 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002081 // restore curwin/curbuf and a few other things
2082 aucmd_restbuf(&aco);
2083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
2087
2088#ifdef FEAT_TERMRESPONSE
2089 may_req_termresponse();
2090#endif
2091
2092 return OK;
2093}
2094
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095#ifdef HAVE_TGETENT
2096/*
2097 * Call tgetent()
2098 * Return error message if it fails, NULL if it's OK.
2099 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002100 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002101tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102{
2103 int i;
2104
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002105 // Note: Valgrind may report a leak here, because the library keeps one
2106 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002108 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002110 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111# endif
2112 )
2113 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002114 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2115 // tgetent() with the always existing "dumb" entry to avoid a crash or
2116 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 (void)TGETENT(tbuf, "dumb");
2118
2119 if (i < 0)
2120# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002121 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 if (i == 0)
2123# endif
2124#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002125 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002127 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128#endif
2129 }
2130 return NULL;
2131}
2132
2133/*
2134 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2135 * Fix that here.
2136 */
2137 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002138vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139{
2140 char *p;
2141
2142 p = tgetstr(s, (char **)pp);
2143 if (p == (char *)-1)
2144 p = NULL;
2145 return (char_u *)p;
2146}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002147#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002149#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150/*
2151 * Get Columns and Rows from the termcap. Used after a window signal if the
2152 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2153 * and "li" entries never change. But on some systems this works.
2154 * Errors while getting the entries are ignored.
2155 */
2156 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002157getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002158 long *cp, // pointer to columns
2159 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160{
2161 char_u tbuf[TBUFSZ];
2162
2163 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2164 {
2165 if (*cp == 0)
2166 *cp = tgetnum("co");
2167 if (*rp == 0)
2168 *rp = tgetnum("li");
2169 }
2170}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002171#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
2173/*
2174 * Get a string entry from the termcap and add it to the list of termcodes.
2175 * Used for <t_xx> special keys.
2176 * Give an error message for failure when not sourcing.
2177 * If force given, replace an existing entry.
2178 * Return FAIL if the entry was not found, OK if the entry was added.
2179 */
2180 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002181add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183 char_u *term;
2184 int key;
2185 struct builtin_term *termp;
2186#ifdef HAVE_TGETENT
2187 char_u *string;
2188 int i;
2189 int builtin_first;
2190 char_u tbuf[TBUFSZ];
2191 char_u tstrbuf[TBUFSZ];
2192 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002193 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194#endif
2195
2196/*
2197 * If the GUI is running or will start in a moment, we only support the keys
2198 * that the GUI can produce.
2199 */
2200#ifdef FEAT_GUI
2201 if (gui.in_use || gui.starting)
2202 return gui_mch_haskey(name);
2203#endif
2204
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002205 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 return OK;
2207
2208 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002209 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 return FAIL;
2211
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002212 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
2214 term += 8;
2215#ifdef HAVE_TGETENT
2216 builtin_first = TRUE;
2217#endif
2218 }
2219#ifdef HAVE_TGETENT
2220 else
2221 builtin_first = p_tbi;
2222#endif
2223
2224#ifdef HAVE_TGETENT
2225/*
2226 * We can get the entry from the builtin termcap and from the external one.
2227 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2228 * builtin termcap first.
2229 * If 'ttybuiltin' is off, try external termcap first.
2230 */
2231 for (i = 0; i < 2; ++i)
2232 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002233 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234#endif
2235 /*
2236 * Search in builtin termcap
2237 */
2238 {
2239 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002240 if (termp->bt_string != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 {
2242 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002243 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 while (termp->bt_entry != (int)KS_NAME)
2245 {
2246 if ((int)termp->bt_entry == key)
2247 {
2248 add_termcode(name, (char_u *)termp->bt_string,
2249 term_is_8bit(term));
2250 return OK;
2251 }
2252 ++termp;
2253 }
2254 }
2255 }
2256#ifdef HAVE_TGETENT
2257 else
2258 /*
2259 * Search in external termcap
2260 */
2261 {
2262 error_msg = tgetent_error(tbuf, term);
2263 if (error_msg == NULL)
2264 {
2265 string = TGETSTR((char *)name, &tp);
2266 if (string != NULL && *string != NUL)
2267 {
2268 add_termcode(name, string, FALSE);
2269 return OK;
2270 }
2271 }
2272 }
2273 }
2274#endif
2275
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002276 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
2278#ifdef HAVE_TGETENT
2279 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002280 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 else
2282#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002283 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 }
2285 return FAIL;
2286}
2287
2288 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002289term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290{
2291 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2292}
2293
2294/*
2295 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2296 * Assume that the terminal is using 8-bit controls when the name contains
2297 * "8bit", like in "xterm-8bit".
2298 */
2299 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002300term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301{
2302 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2303}
2304
2305/*
2306 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002307 * <Esc>[ -> CSI <M_C_[>
2308 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 * <Esc>O -> <M-C-O>
2310 */
2311 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002312term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313{
2314 if (*p == ESC)
2315 {
2316 if (p[1] == '[')
2317 return CSI;
2318 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002319 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (p[1] == 'O')
2321 return 0x8f;
2322 }
2323 return 0;
2324}
2325
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002326#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002328term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
2330 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2331}
2332#endif
2333
2334#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2335
2336 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002337tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338{
2339 static char_u buf[16];
2340 char_u *p;
2341
2342 p = buf + 15;
2343 *p = '\0';
2344 do
2345 {
2346 --p;
2347 *p = (char_u) (i % 10 + '0');
2348 i /= 10;
2349 }
2350 while (i > 0 && p > buf);
2351 return p;
2352}
2353#endif
2354
2355#ifndef HAVE_TGETENT
2356
2357/*
2358 * minimal tgoto() implementation.
2359 * no padding and we only parse for %i %d and %+char
2360 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002361 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002362tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
2364 static char buf[30];
2365 char *p, *s, *e;
2366
2367 if (!cm)
2368 return "OOPS";
2369 e = buf + 29;
2370 for (s = buf; s < e && *cm; cm++)
2371 {
2372 if (*cm != '%')
2373 {
2374 *s++ = *cm;
2375 continue;
2376 }
2377 switch (*++cm)
2378 {
2379 case 'd':
2380 p = (char *)tltoa((unsigned long)y);
2381 y = x;
2382 while (*p)
2383 *s++ = *p++;
2384 break;
2385 case 'i':
2386 x++;
2387 y++;
2388 break;
2389 case '+':
2390 *s++ = (char)(*++cm + y);
2391 y = x;
2392 break;
2393 case '%':
2394 *s++ = *cm;
2395 break;
2396 default:
2397 return "OOPS";
2398 }
2399 }
2400 *s = '\0';
2401 return buf;
2402}
2403
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002404#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406/*
2407 * Set the terminal name and initialize the terminal options.
2408 * If "name" is NULL or empty, get the terminal name from the environment.
2409 * If that fails, use the default terminal name.
2410 */
2411 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002412termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
2414 char_u *term;
2415
2416 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002417 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 term = name;
2419
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420#ifndef MSWIN
2421 if (term == NULL)
2422 term = mch_getenv((char_u *)"TERM");
2423#endif
2424 if (term == NULL || *term == NUL)
2425 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002426 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002428 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 set_string_default("term", term);
2430 set_string_default("ttytype", term);
2431
2432 /*
2433 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2434 */
2435 set_termname(T_NAME != NULL ? T_NAME : term);
2436}
2437
2438/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002439 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002441#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002442
2443// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002445
2446static int out_pos = 0; // number of chars in out_buf
2447
2448// Since the maximum number of SGR parameters shown as a normal value range is
2449// 16, the escape sequence length can be 4 * 16 + lead + tail.
2450#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
2452/*
2453 * out_flush(): flush the output buffer
2454 */
2455 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002456out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457{
2458 int len;
2459
2460 if (out_pos != 0)
2461 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002462 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 len = out_pos;
2464 out_pos = 0;
2465 ui_write(out_buf, len);
2466 }
2467}
2468
Bram Moolenaara338adc2018-01-31 20:51:47 +01002469/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002470 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2471 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002472 */
2473 void
2474out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002475 int force UNUSED, // when TRUE, update cursor even when not moved
2476 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002477{
2478 mch_disable_flush();
2479 out_flush();
2480 mch_enable_flush();
2481#ifdef FEAT_GUI
2482 if (gui.in_use)
2483 {
2484 gui_update_cursor(force, clear_selection);
2485 gui_may_flush();
2486 }
2487#endif
2488}
2489
2490
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491/*
2492 * Sometimes a byte out of a multi-byte character is written with out_char().
2493 * To avoid flushing half of the character, call this function first.
2494 */
2495 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002496out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2499 out_flush();
2500}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502#ifdef FEAT_GUI
2503/*
2504 * out_trash(): Throw away the contents of the output buffer
2505 */
2506 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002507out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509 out_pos = 0;
2510}
2511#endif
2512
2513/*
2514 * out_char(c): put a byte into the output buffer.
2515 * Flush it if it becomes full.
2516 * This should not be used for outputting text on the screen (use functions
2517 * like msg_puts() and screen_putchar() for that).
2518 */
2519 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002520out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521{
Bram Moolenaard0573012017-10-28 21:11:06 +02002522#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002523 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 out_char('\r');
2525#endif
2526
2527 out_buf[out_pos++] = c;
2528
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002529 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 if (out_pos >= OUT_SIZE || p_wd)
2531 out_flush();
2532}
2533
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002535 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 */
2537 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002538out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 out_buf[out_pos++] = c;
2541
2542 if (out_pos >= OUT_SIZE)
2543 out_flush();
2544}
2545
2546/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002547 * A never-padding out_str().
2548 * Use this whenever you don't want to run the string through tputs().
2549 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 * is likely to strip off leading digits, that it mistakes for padding
2551 * information, and "%i", "%d", etc.
2552 * This should only be used for writing terminal codes, not for outputting
2553 * normal text (use functions like msg_puts() and screen_putchar() for that).
2554 */
2555 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002556out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002558 // avoid terminal strings being split up
2559 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002561
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 while (*s)
2563 out_char_nf(*s++);
2564
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002565 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (p_wd)
2567 out_flush();
2568}
2569
2570/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002571 * A conditional-flushing out_str, mainly for visualbell.
2572 * Handles a delay internally, because termlib may not respect the delay or do
2573 * it at the wrong time.
2574 * Note: Only for terminal strings.
2575 */
2576 void
2577out_str_cf(char_u *s)
2578{
2579 if (s != NULL && *s)
2580 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002581#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002582 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002583#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002584
2585#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002586 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002587 if (gui.in_use)
2588 {
2589 out_str_nf(s);
2590 return;
2591 }
2592#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002593 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002594 out_flush();
2595#ifdef HAVE_TGETENT
2596 for (p = s; *s; ++s)
2597 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002598 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002599 if (*s == '$' && *(s + 1) == '<')
2600 {
2601 char_u save_c = *s;
2602 int duration = atoi((char *)s + 2);
2603
2604 *s = NUL;
2605 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2606 *s = save_c;
2607 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002608# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002609 // Only sleep here if we can limit this happening in
2610 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002611 p = vim_strchr(s, '>');
2612 if (p == NULL || duration <= 0)
2613 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002614 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002615 p = s;
2616 }
2617 else
2618 {
2619 ++p;
2620 do_sleep(duration);
2621 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002622# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002623 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002624 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002625# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002626 break;
2627 }
2628 }
2629 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2630#else
2631 while (*s)
2632 out_char_nf(*s++);
2633#endif
2634
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002635 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002636 if (p_wd)
2637 out_flush();
2638 }
2639}
2640
2641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002643 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 * This should only be used for writing terminal codes, not for outputting
2645 * normal text (use functions like msg_puts() and screen_putchar() for that).
2646 */
2647 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002648out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
2650 if (s != NULL && *s)
2651 {
2652#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002653 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 if (gui.in_use)
2655 {
2656 out_str_nf(s);
2657 return;
2658 }
2659#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002660 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002661 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 out_flush();
2663#ifdef HAVE_TGETENT
2664 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2665#else
2666 while (*s)
2667 out_char_nf(*s++);
2668#endif
2669
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002670 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (p_wd)
2672 out_flush();
2673 }
2674}
2675
2676/*
2677 * cursor positioning using termcap parser. (jw)
2678 */
2679 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002680term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
2682 OUT_STR(tgoto((char *)T_CM, col, row));
2683}
2684
2685 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002686term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687{
2688 OUT_STR(tgoto((char *)T_CRI, 0, i));
2689}
2690
2691 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002692term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2695}
2696
2697 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002698term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699{
2700 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2701}
2702
2703#if defined(HAVE_TGETENT) || defined(PROTO)
2704 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002705term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002707 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (x < 0)
2709 x = 0;
2710 if (y < 0)
2711 y = 0;
2712 OUT_STR(tgoto((char *)T_CWP, y, x));
2713}
2714
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002715# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2716/*
2717 * Return TRUE if we can request the terminal for a response.
2718 */
2719 static int
2720can_get_termresponse()
2721{
2722 return cur_tmode == TMODE_RAW
2723 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002724# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002725 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002726# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002727 && p_ek;
2728}
2729
Bram Moolenaarafd78262019-05-10 23:10:31 +02002730/*
2731 * Set "status" to STATUS_SENT.
2732 */
2733 static void
2734termrequest_sent(termrequest_T *status)
2735{
2736 status->tr_progress = STATUS_SENT;
2737 status->tr_start = time(NULL);
2738}
2739
2740/*
2741 * Return TRUE if any of the requests are in STATUS_SENT.
2742 */
2743 static int
2744termrequest_any_pending()
2745{
2746 int i;
2747 time_t now = time(NULL);
2748
2749 for (i = 0; all_termrequests[i] != NULL; ++i)
2750 {
2751 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2752 {
2753 if (all_termrequests[i]->tr_start > 0 && now > 0
2754 && all_termrequests[i]->tr_start + 2 < now)
2755 // Sent the request more than 2 seconds ago and didn't get a
2756 // response, assume it failed.
2757 all_termrequests[i]->tr_progress = STATUS_FAIL;
2758 else
2759 return TRUE;
2760 }
2761 }
2762 return FALSE;
2763}
2764
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002765static int winpos_x = -1;
2766static int winpos_y = -1;
2767static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002768
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002769# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002770/*
2771 * Try getting the Vim window position from the terminal.
2772 * Returns OK or FAIL.
2773 */
2774 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002775term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002776{
2777 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002778 int prev_winpos_x = winpos_x;
2779 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002780
2781 if (*T_CGP == NUL || !can_get_termresponse())
2782 return FAIL;
2783 winpos_x = -1;
2784 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002785 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002786 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002787 OUT_STR(T_CGP);
2788 out_flush();
2789
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002790 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002791 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002792 {
2793 (void)vpeekc_nomap();
2794 if (winpos_x >= 0 && winpos_y >= 0)
2795 {
2796 *x = winpos_x;
2797 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002798 return OK;
2799 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002800 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002801 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002802 // Do not reset "did_request_winpos", if we timed out the response might
2803 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002804
2805 winpos_x = prev_winpos_x;
2806 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002807 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002808 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002809 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002810 *x = winpos_x;
2811 *y = winpos_y;
2812 return OK;
2813 }
2814
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002815 return FALSE;
2816}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002817# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002818# endif
2819
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002821term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002823 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824}
2825#endif
2826
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002828term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829{
2830 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002831 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002832 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002834 // Special handling of 16 colors, because termcap can't handle it
2835 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
2836 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002838 && ((s[0] == ESC && s[1] == '[')
2839#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2840 || (s[0] == ESC && s[1] == '|')
2841#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02002842 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 && s[i] != NUL
2844 && (STRCMP(s + i + 1, "%p1%dm") == 0
2845 || STRCMP(s + i + 1, "%dm") == 0)
2846 && (s[i] == '3' || s[i] == '4'))
2847 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002849 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002851 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002853 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002854#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02002855 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002856#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002857 IF_EB("\033[", ESC_STR "[")) : "\233";
2858 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2859 : (n >= 16 ? "48;5;" : "10");
2860
2861 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2863 }
2864 else
2865 OUT_STR(tgoto((char *)s, 0, n));
2866}
2867
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002868 void
2869term_fg_color(int n)
2870{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002871 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002872 if (*T_CAF)
2873 term_color(T_CAF, n);
2874 else if (*T_CSF)
2875 term_color(T_CSF, n);
2876}
2877
2878 void
2879term_bg_color(int n)
2880{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002881 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002882 if (*T_CAB)
2883 term_color(T_CAB, n);
2884 else if (*T_CSB)
2885 term_color(T_CSB, n);
2886}
2887
Bram Moolenaare023e882020-05-31 16:42:30 +02002888 void
2889term_ul_color(int n)
2890{
2891 if (*T_CAU)
2892 term_color(T_CAU, n);
2893}
2894
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002895/*
2896 * Return "dark" or "light" depending on the kind of terminal.
2897 * This is just guessing! Recognized are:
2898 * "linux" Linux console
2899 * "screen.linux" Linux console with screen
2900 * "cygwin.*" Cygwin shell
2901 * "putty.*" Putty program
2902 * We also check the COLORFGBG environment variable, which is set by
2903 * rxvt and derivatives. This variable contains either two or three
2904 * values separated by semicolons; we want the last value in either
2905 * case. If this value is 0-6 or 8, our background is dark.
2906 */
2907 char_u *
2908term_bg_default(void)
2909{
2910#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002911 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002912 return (char_u *)"dark";
2913#else
2914 char_u *p;
2915
2916 if (STRCMP(T_NAME, "linux") == 0
2917 || STRCMP(T_NAME, "screen.linux") == 0
2918 || STRNCMP(T_NAME, "cygwin", 6) == 0
2919 || STRNCMP(T_NAME, "putty", 5) == 0
2920 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
2921 && (p = vim_strrchr(p, ';')) != NULL
2922 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
2923 && p[2] == NUL))
2924 return (char_u *)"dark";
2925 return (char_u *)"light";
2926#endif
2927}
2928
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002929#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002930
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002931#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2932#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2933#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002934
2935 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002936term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002937{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002938#define MAX_COLOR_STR_LEN 100
2939 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002940
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002941 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002942 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02002943#ifdef FEAT_VTP
2944 if (use_wt())
2945 {
2946 out_flush();
2947 buf[1] = '[';
2948 vtp_printf(buf);
2949 }
2950 else
2951#endif
2952 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002953}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002954
2955 void
2956term_fg_rgb_color(guicolor_T rgb)
2957{
2958 term_rgb_color(T_8F, rgb);
2959}
2960
2961 void
2962term_bg_rgb_color(guicolor_T rgb)
2963{
2964 term_rgb_color(T_8B, rgb);
2965}
Bram Moolenaare023e882020-05-31 16:42:30 +02002966
2967 void
2968term_ul_rgb_color(guicolor_T rgb)
2969{
2970 term_rgb_color(T_8U, rgb);
2971}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002972#endif
2973
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002974#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2975 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976/*
2977 * Generic function to set window title, using t_ts and t_fs.
2978 */
2979 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002980term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002982 // t_ts takes one argument: column in status line
2983 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002985 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 out_flush();
2987}
Bram Moolenaar40385db2018-08-07 22:31:44 +02002988
2989/*
2990 * Tell the terminal to push (save) the title and/or icon, so that it can be
2991 * popped (restored) later.
2992 */
2993 void
2994term_push_title(int which)
2995{
Bram Moolenaar27821262019-05-08 16:41:09 +02002996 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002997 {
2998 OUT_STR(T_CST);
2999 out_flush();
3000 }
3001
Bram Moolenaar27821262019-05-08 16:41:09 +02003002 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003003 {
3004 OUT_STR(T_SSI);
3005 out_flush();
3006 }
3007}
3008
3009/*
3010 * Tell the terminal to pop the title and/or icon.
3011 */
3012 void
3013term_pop_title(int which)
3014{
Bram Moolenaar27821262019-05-08 16:41:09 +02003015 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003016 {
3017 OUT_STR(T_CRT);
3018 out_flush();
3019 }
3020
Bram Moolenaar27821262019-05-08 16:41:09 +02003021 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003022 {
3023 OUT_STR(T_SRI);
3024 out_flush();
3025 }
3026}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027#endif
3028
3029/*
3030 * Make sure we have a valid set or terminal options.
3031 * Replace all entries that are NULL by empty_option
3032 */
3033 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003034ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003036 char_u *env_colors;
3037
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003038 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039
3040 /*
3041 * MUST have "cm": cursor motion.
3042 */
3043 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003044 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045
3046 /*
3047 * if "cs" defined, use a scroll region, it's faster.
3048 */
3049 if (*T_CS != NUL)
3050 scroll_region = TRUE;
3051 else
3052 scroll_region = FALSE;
3053
3054 if (pairs)
3055 {
3056 /*
3057 * optional pairs
3058 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003059 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 if (*T_ME == NUL)
3061 T_ME = T_MR = T_MD = T_MB = empty_option;
3062 if (*T_SO == NUL || *T_SE == NUL)
3063 T_SO = T_SE = empty_option;
3064 if (*T_US == NUL || *T_UE == NUL)
3065 T_US = T_UE = empty_option;
3066 if (*T_CZH == NUL || *T_CZR == NUL)
3067 T_CZH = T_CZR = empty_option;
3068
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003069 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 if (*T_VE == NUL)
3071 T_VI = empty_option;
3072
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003073 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 if (*T_ME == NUL)
3075 {
3076 T_ME = T_SE;
3077 T_MR = T_SO;
3078 T_MD = T_SO;
3079 }
3080
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003081 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (*T_SO == NUL)
3083 {
3084 T_SE = T_ME;
3085 if (*T_MR == NUL)
3086 T_SO = T_MD;
3087 else
3088 T_SO = T_MR;
3089 }
3090
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003091 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 if (*T_CZH == NUL)
3093 {
3094 T_CZR = T_ME;
3095 if (*T_MR == NUL)
3096 T_CZH = T_MD;
3097 else
3098 T_CZH = T_MR;
3099 }
3100
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003101 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if (*T_CSB == NUL || *T_CSF == NUL)
3103 {
3104 T_CSB = empty_option;
3105 T_CSF = empty_option;
3106 }
3107
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003108 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 if (*T_CAB == NUL || *T_CAF == NUL)
3110 {
3111 T_CAB = empty_option;
3112 T_CAF = empty_option;
3113 }
3114
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003115 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003117 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003119 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 p_wiv = (*T_XS != NUL);
3121 }
3122 need_gather = TRUE;
3123
Bram Moolenaar759d8152020-04-26 16:52:49 +02003124 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3125 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003127#ifdef FEAT_GUI
3128 if (!gui.in_use)
3129#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003130 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003131 env_colors = mch_getenv((char_u *)"COLORS");
3132 if (env_colors != NULL && isdigit(*env_colors))
3133 {
3134 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003135
Bram Moolenaar759d8152020-04-26 16:52:49 +02003136 if (colors != t_colors)
3137 set_color_count(colors);
3138 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140}
3141
3142#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3143 || defined(PROTO)
3144/*
3145 * Represent the given long_u as individual bytes, with the most significant
3146 * byte first, and store them in dst.
3147 */
3148 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003149add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
3151 int i;
3152 int shift;
3153
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003154 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 {
3156 shift = 8 * (sizeof(long_u) - i);
3157 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3158 }
3159}
3160
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161/*
3162 * Interpret the next string of bytes in buf as a long integer, with the most
3163 * significant byte first. Note that it is assumed that buf has been through
3164 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3165 * Puts result in val, and returns the number of bytes read from buf
3166 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3167 * were present.
3168 */
3169 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003170get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
3172 int len;
3173 char_u bytes[sizeof(long_u)];
3174 int i;
3175 int shift;
3176
3177 *val = 0;
3178 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3179 if (len != -1)
3180 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003181 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 {
3183 shift = 8 * (sizeof(long_u) - 1 - i);
3184 *val += (long_u)bytes[i] << shift;
3185 }
3186 }
3187 return len;
3188}
3189#endif
3190
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191/*
3192 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3193 * that buf has been through inchar(). Returns the actual number of bytes used
3194 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3195 * available.
3196 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003197 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003198get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199{
3200 int len = 0;
3201 int i;
3202 char_u c;
3203
3204 for (i = 0; i < num_bytes; i++)
3205 {
3206 if ((c = buf[len++]) == NUL)
3207 return -1;
3208 if (c == K_SPECIAL)
3209 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003210 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 return -1;
3212 if (buf[len++] == (int)KS_ZERO)
3213 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003214 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3215 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003216 if (buf[len++] == (int)KE_CSI)
3217 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003219 else if (c == CSI && buf[len] == KS_EXTRA
3220 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003221 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3222 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003223 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 bytes[i] = c;
3225 }
3226 return len;
3227}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228
3229/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003230 * Check if the new shell size is valid, correct it if it's too small or way
3231 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 */
3233 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003234check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003236 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003238 limit_screen_size();
3239}
3240
3241/*
3242 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3243 */
3244 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003245limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003246{
3247 if (Columns < MIN_COLUMNS)
3248 Columns = MIN_COLUMNS;
3249 else if (Columns > 10000)
3250 Columns = 10000;
3251 if (Rows > 1000)
3252 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253}
3254
Bram Moolenaar86b68352004-12-27 21:59:20 +00003255/*
3256 * Invoked just before the screen structures are going to be (re)allocated.
3257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003259win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261 static int old_Rows = 0;
3262 static int old_Columns = 0;
3263
3264 if (old_Rows != Rows || old_Columns != Columns)
3265 ui_new_shellsize();
3266 if (old_Rows != Rows)
3267 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003268 // if 'window' uses the whole screen, keep it using that
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003269 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003270 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003272 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
3274 if (old_Columns != Columns)
3275 {
3276 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003277 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
3279}
3280
3281/*
3282 * Call this function when the Vim shell has been resized in any way.
3283 * Will obtain the current size and redraw (also when size didn't change).
3284 */
3285 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003286shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
3288 set_shellsize(0, 0, FALSE);
3289}
3290
3291/*
3292 * Check if the shell size changed. Handle a resize.
3293 * When the size didn't change, nothing happens.
3294 */
3295 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003296shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 int old_Rows = Rows;
3299 int old_Columns = Columns;
3300
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003301 if (!exiting
3302#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003303 // Do not get the size when executing a shell command during
3304 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003305 && !gui.starting
3306#endif
3307 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003308 {
3309 (void)ui_get_shellsize();
3310 check_shellsize();
3311 if (old_Rows != Rows || old_Columns != Columns)
3312 shell_resized();
3313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314}
3315
3316/*
3317 * Set size of the Vim shell.
3318 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3319 * window size (this is used for the :win command).
3320 * If 'mustset' is FALSE, we may try to get the real window size and if
3321 * it fails use 'width' and 'height'.
3322 */
3323 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003324set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
3326 static int busy = FALSE;
3327
3328 /*
3329 * Avoid recursiveness, can happen when setting the window size causes
3330 * another window-changed signal.
3331 */
3332 if (busy)
3333 return;
3334
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003335 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 return;
3337
Bram Moolenaara971b822011-09-14 14:43:25 +02003338 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003340 // postpone the resizing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 State = SETWSIZE;
3342 return;
3343 }
3344
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003345 if (updating_screen)
3346 // resizing while in update_screen() may cause a crash
3347 return;
3348
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003349 // curwin->w_buffer can be NULL when we are closing a window and the
3350 // buffer has already been closed and removing a scrollbar causes a resize
3351 // event. Don't resize then, it will happen after entering another buffer.
Bram Moolenaara971b822011-09-14 14:43:25 +02003352 if (curwin->w_buffer == NULL)
3353 return;
3354
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 ++busy;
3356
3357#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003358 out_flush(); // must do this before mch_get_shellsize() for
3359 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360#endif
3361
3362 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3363 {
3364 Rows = height;
3365 Columns = width;
3366 check_shellsize();
3367 ui_set_shellsize(mustset);
3368 }
3369 else
3370 check_shellsize();
3371
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003372 // The window layout used to be adjusted here, but it now happens in
3373 // screenalloc() (also invoked from screenclear()). That is because the
3374 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
3376 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3377 screenclear();
3378 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003379 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
3381 if (starting != NO_SCREEN)
3382 {
3383#ifdef FEAT_TITLE
3384 maketitle();
3385#endif
3386 changed_line_abv_curs();
3387 invalidate_botline();
3388
3389 /*
3390 * We only redraw when it's needed:
3391 * - While at the more prompt or executing an external command, don't
3392 * redraw, but position the cursor.
3393 * - While editing the command line, only redraw that.
3394 * - in Ex mode, don't redraw anything.
3395 * - Otherwise, redraw right now, and position the cursor.
3396 * Always need to call update_screen() or screenalloc(), to make
3397 * sure Rows/Columns and the size of ScreenLines[] is correct!
3398 */
3399 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3400 || exmode_active)
3401 {
3402 screenalloc(FALSE);
3403 repeat_message();
3404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 else
3406 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003407 if (curwin->w_p_scb)
3408 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003409 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003410 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003411 update_screen(NOT_VALID);
3412 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003413 }
3414 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003415 {
3416 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003417 if (pum_visible())
3418 {
3419 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003420 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003421 }
Bram Moolenaara5e66212017-09-29 22:42:33 +02003422 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003423 if (redrawing())
3424 setcursor();
3425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003427 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
3429 out_flush();
3430 --busy;
3431}
3432
3433/*
3434 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3435 * commands and Ex mode).
3436 */
3437 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003438settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
3440#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003441 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (gui.in_use)
3443 return;
3444#endif
3445
3446 if (full_screen)
3447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003449 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3450 * set the terminal to raw mode, even though we think it already is,
3451 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 * When we think the terminal is normal, don't try to set it to
3453 * normal again, because that causes problems (logout!) on some
3454 * machines.
3455 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003456 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
3458#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003459# ifdef FEAT_GUI
3460 if (!gui.in_use && !gui.starting)
3461# endif
3462 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003463 // May need to check for T_CRV response and termcodes, it
3464 // doesn't work in Cooked mode, an external program may get
3465 // them.
3466 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003467 (void)vpeekc_nomap();
3468 check_for_codes_from_term();
3469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003472 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003473
3474 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3475 // Avoid doing this too often, on some terminals the codes are not
3476 // handled properly.
3477 if (termcap_active && tmode != TMODE_SLEEP
3478 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003479 {
3480 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003481 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003482 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003483 out_str(T_CTE); // possibly disables modifyOtherKeys
3484 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003485 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003486 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003487 out_str(T_BE); // enable bracketed paste mode (should
3488 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003489 out_str(T_CTI); // possibly enables modifyOtherKeys
3490 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003493 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003496 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 out_flush();
3498 }
3499#ifdef FEAT_TERMRESPONSE
3500 may_req_termresponse();
3501#endif
3502 }
3503}
3504
3505 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003506starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
3508 if (full_screen && !termcap_active)
3509 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003510 out_str(T_TI); // start termcap mode
3511 out_str(T_CTI); // start "raw" mode
3512 out_str(T_KS); // start "keypad transmit" mode
3513 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 out_flush();
3515 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003516 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003518# ifdef FEAT_GUI
3519 if (!gui.in_use && !gui.starting)
3520# endif
3521 {
3522 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003523 // Immediately check for a response. If t_Co changes, we don't
3524 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003525 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003526 check_for_codes_from_term();
3527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528#endif
3529 }
3530}
3531
3532 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003533stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534{
3535 screen_stop_highlight();
3536 reset_cterm_colors();
3537 if (termcap_active)
3538 {
3539#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003540# ifdef FEAT_GUI
3541 if (!gui.in_use && !gui.starting)
3542# endif
3543 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003544 // May need to discard T_CRV, T_U7 or T_RBG response.
3545 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003546 {
3547# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003548 // Give the terminal a chance to respond.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003549 mch_delay(100L, FALSE);
3550# endif
3551# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003552 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003553 if (exiting)
3554 tcflush(fileno(stdin), TCIFLUSH);
3555# endif
3556 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003557 // Check for termcodes first, otherwise an external program may
3558 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003559 check_for_codes_from_term();
3560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003562 out_str(T_BD); // disable bracketed paste mode
3563 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 out_flush();
3565 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003566 cursor_on(); // just in case it is still off
3567 out_str(T_CTE); // stop "raw" mode
3568 out_str(T_TE); // stop termcap mode
3569 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 out_flush();
3571 }
3572}
3573
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003574#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575/*
3576 * Request version string (for xterm) when needed.
3577 * Only do this after switching to raw mode, otherwise the result will be
3578 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003579 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003580 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 * Only do this after termcap mode has been started, otherwise the codes for
3582 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003583 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3584 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003585 * On Unix only do it when both output and input are a tty (avoid writing
3586 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 * The result is caught in check_termcode().
3588 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003589 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003590may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003592 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003593 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003594 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 && *T_CRV != NUL)
3596 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003597 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003599 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003600 // check for the characters now, otherwise they might be eaten by
3601 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 out_flush();
3603 (void)vpeekc_nomap();
3604 }
3605}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003606
Bram Moolenaar9584b312013-03-13 19:29:28 +01003607/*
3608 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003609 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003610 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003611 * If the terminal treats \u25bd as single width, the position is (1, 1),
3612 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003613 * This function has the side effect that changes cursor position, so
3614 * it must be called immediately after entering termcap mode.
3615 */
3616 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003617may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003618{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003619 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003620 && can_get_termresponse()
3621 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003622 && *T_U7 != NUL
3623 && !option_was_set((char_u *)"ambiwidth"))
3624 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003625 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003626
Bram Moolenaarafd78262019-05-10 23:10:31 +02003627 LOG_TR(("Sending U7 request"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003628 // Do this in the second row. In the first row the returned sequence
3629 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003630 term_windgoto(1, 0);
3631 buf[mb_char2bytes(0x25bd, buf)] = 0;
3632 out_str(buf);
3633 out_str(T_U7);
3634 termrequest_sent(&u7_status);
3635 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003636
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003637 // This overwrites a few characters on the screen, a redraw is needed
3638 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003639 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003640 term_windgoto(1, 0);
3641 out_str((char_u *)" ");
3642 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003643
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003644 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003645 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003646
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003647 // check for the characters now, otherwise they might be eaten by
3648 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003649 out_flush();
3650 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003651 }
3652}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003653
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003654/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003655 * Similar to requesting the version string: Request the terminal background
3656 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003657 */
3658 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003659may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003660{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003661 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003662 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003663 int didit = FALSE;
3664
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003665# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003666 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003667 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003668 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003669 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003670 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003671 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003672 didit = TRUE;
3673 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003674# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003675
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003676 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003677 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003678 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003679 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003680 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003681 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003682 didit = TRUE;
3683 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003684
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003685 if (didit)
3686 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003687 // check for the characters now, otherwise they might be eaten by
3688 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003689 out_flush();
3690 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003691 }
3692 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003693}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003694
Bram Moolenaar2951b772013-07-03 12:45:31 +02003695# ifdef DEBUG_TERMRESPONSE
3696 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003697log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003698{
3699 static FILE *fd_tr = NULL;
3700 static proftime_T start;
3701 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003702 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003703
3704 if (fd_tr == NULL)
3705 {
3706 fd_tr = fopen("termresponse.log", "w");
3707 profile_start(&start);
3708 }
3709 now = start;
3710 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003711 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3712 must_redraw == NOT_VALID ? "NV"
3713 : must_redraw == CLEAR ? "CL" : " ");
3714 va_start(ap, fmt);
3715 vfprintf(fd_tr, fmt, ap);
3716 va_end(ap);
3717 fputc('\n', fd_tr);
3718 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003719}
3720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721#endif
3722
3723/*
3724 * Return TRUE when saving and restoring the screen.
3725 */
3726 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003727swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728{
3729 return (full_screen && *T_TI != NUL);
3730}
3731
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732/*
3733 * By outputting the 'cursor very visible' termcap code, for some windowed
3734 * terminals this makes the screen scrolled to the correct position.
3735 * Used when starting Vim or returning from a shell.
3736 */
3737 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003738scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003740 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
3742 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003743 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003744 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
3746}
3747
3748static int cursor_is_off = FALSE;
3749
3750/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003751 * Enable the cursor without checking if it's already enabled.
3752 */
3753 void
3754cursor_on_force(void)
3755{
3756 out_str(T_VE);
3757 cursor_is_off = FALSE;
3758}
3759
3760/*
3761 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 */
3763 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003764cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765{
3766 if (cursor_is_off)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003767 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768}
3769
3770/*
3771 * Disable the cursor.
3772 */
3773 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003774cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003776 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003778 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 cursor_is_off = TRUE;
3780 }
3781}
3782
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003783#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003785 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003786 */
3787 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003788term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003789{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003790 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003791 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003792
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003793 // Only do something when redrawing the screen and we can restore the
3794 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003795 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003796 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003797# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003798 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003799 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003800 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003801# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003802 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003803 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003804
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003805 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003806 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003807 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003808 {
3809 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003810 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003811 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003812 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003813 if (*p != NUL)
3814 {
3815 out_str(p);
3816 showing_mode = REPLACE;
3817 }
3818 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003819 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003820 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003821 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003822 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003823 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003824 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003825 showing_mode = INSERT;
3826 }
3827 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003828 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003829 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003830 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003831 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003832 }
3833}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003834
3835# if defined(FEAT_TERMINAL) || defined(PROTO)
3836 void
3837term_cursor_color(char_u *color)
3838{
3839 if (*T_CSC != NUL)
3840 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003841 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003842 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003843 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003844 out_flush();
3845 }
3846}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003847# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003848
Bram Moolenaar4db25542017-08-28 22:43:05 +02003849 int
3850blink_state_is_inverted()
3851{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003852#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02003853 return rbm_status.tr_progress == STATUS_GOT
3854 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003855 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003856#else
3857 return FALSE;
3858#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003859}
3860
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003861/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003862 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003863 */
3864 void
3865term_cursor_shape(int shape, int blink)
3866{
3867 if (*T_CSH != NUL)
3868 {
3869 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3870 out_flush();
3871 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003872 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003873 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003874 int do_blink = blink;
3875
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003876 // t_SH is empty: try setting just the blink state.
3877 // The blink flags are XORed together, if the initial blinking from
3878 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02003879 if (blink_state_is_inverted())
3880 do_blink = !blink;
3881
3882 if (do_blink && *T_VS != NUL)
3883 {
3884 out_str(T_VS);
3885 out_flush();
3886 }
3887 else if (!do_blink && *T_CVS != NUL)
3888 {
3889 out_str(T_CVS);
3890 out_flush();
3891 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003892 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003893}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003894#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003895
3896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 * Set scrolling region for window 'wp'.
3898 * The region starts 'off' lines from the start of the window.
3899 * Also set the vertical scroll region for a vertically split window. Always
3900 * the full width of the window, excluding the vertical separator.
3901 */
3902 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003903scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
3905 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3906 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003908 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
3909 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003910 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911}
3912
3913/*
3914 * Reset scrolling region to the whole screen.
3915 */
3916 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003917scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918{
3919 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 if (*T_CSV != NUL)
3921 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003922 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923}
3924
3925
3926/*
3927 * List of terminal codes that are currently recognized.
3928 */
3929
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003930static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003932 char_u name[2]; // termcap name of entry
3933 char_u *code; // terminal code (in allocated memory)
3934 int len; // STRLEN(code)
3935 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936} *termcodes = NULL;
3937
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003938static int tc_max_len = 0; // number of entries that termcodes[] can hold
3939static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003941static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003942
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003944clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945{
3946 while (tc_len > 0)
3947 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003948 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 tc_max_len = 0;
3950
3951#ifdef HAVE_TGETENT
3952 BC = (char *)empty_option;
3953 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003954 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 ospeed = 0;
3956#endif
3957
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003958 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959}
3960
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003961#define ATC_FROM_TERM 55
3962
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963/*
3964 * Add a new entry to the list of terminal codes.
3965 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003966 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3967 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 */
3969 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003970add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971{
3972 struct termcode *new_tc;
3973 int i, j;
3974 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003975 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
3977 if (string == NULL || *string == NUL)
3978 {
3979 del_termcode(name);
3980 return;
3981 }
3982
Bram Moolenaar4f974752019-02-17 17:44:42 +01003983#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar45500912014-07-09 20:51:07 +02003984 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3985#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003986# ifdef VIMDLL
3987 if (!gui.in_use)
3988 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3989 else
3990# endif
3991 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 if (s == NULL)
3994 return;
3995
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003996 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003997 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003999 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 s[0] = term_7to8bit(string);
4001 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004002
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004003#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
4004# ifdef VIMDLL
4005 if (!gui.in_use)
4006# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02004007 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004008 if (s[0] == K_NUL)
4009 {
4010 STRMOVE(s + 1, s);
4011 s[1] = 3;
4012 }
Bram Moolenaar45500912014-07-09 20:51:07 +02004013 }
4014#endif
4015
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004016 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004018 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
4020 /*
4021 * need to make space for more entries
4022 */
4023 if (tc_len == tc_max_len)
4024 {
4025 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004026 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 if (new_tc == NULL)
4028 {
4029 tc_max_len -= 20;
4030 return;
4031 }
4032 for (i = 0; i < tc_len; ++i)
4033 new_tc[i] = termcodes[i];
4034 vim_free(termcodes);
4035 termcodes = new_tc;
4036 }
4037
4038 /*
4039 * Look for existing entry with the same name, it is replaced.
4040 * Look for an existing entry that is alphabetical higher, the new entry
4041 * is inserted in front of it.
4042 */
4043 for (i = 0; i < tc_len; ++i)
4044 {
4045 if (termcodes[i].name[0] < name[0])
4046 continue;
4047 if (termcodes[i].name[0] == name[0])
4048 {
4049 if (termcodes[i].name[1] < name[1])
4050 continue;
4051 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004052 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 */
4054 if (termcodes[i].name[1] == name[1])
4055 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004056 if (flags == ATC_FROM_TERM && (j = termcode_star(
4057 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004058 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004059 // Don't replace ESC[123;*X or ESC O*X with another when
4060 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004061 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004062 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004063 && s[len - 1]
4064 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004065 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004066 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004067 vim_free(s);
4068 return;
4069 }
4070 }
4071 else
4072 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004073 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004074 vim_free(termcodes[i].code);
4075 --tc_len;
4076 break;
4077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079 }
4080 /*
4081 * Found alphabetical larger entry, move rest to insert new entry
4082 */
4083 for (j = tc_len; j > i; --j)
4084 termcodes[j] = termcodes[j - 1];
4085 break;
4086 }
4087
4088 termcodes[i].name[0] = name[0];
4089 termcodes[i].name[1] = name[1];
4090 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004091 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004092
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004093 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4094 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004095 termcodes[i].modlen = 0;
4096 j = termcode_star(s, len);
4097 if (j > 0)
4098 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 ++tc_len;
4100}
4101
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004102/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004103 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004104 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004105 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004106 */
4107 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004108termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004109{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004110 // Shortest is <M-O>*X. With ; shortest is <CSI>1;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004111 if (len >= 3 && code[len - 2] == '*')
4112 {
4113 if (len >= 5 && code[len - 3] == ';')
4114 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004115 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004116 return 1;
4117 }
4118 return 0;
4119}
4120
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004122find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123{
4124 int i;
4125
4126 for (i = 0; i < tc_len; ++i)
4127 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4128 return termcodes[i].code;
4129 return NULL;
4130}
4131
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004133get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
4135 if (i >= tc_len)
4136 return NULL;
4137 return &termcodes[i].name[0];
4138}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004140/*
4141 * Returns the length of the terminal code at index 'idx'.
4142 */
4143 int
4144get_termcode_len(int idx)
4145{
4146 return termcodes[idx].len;
4147}
4148
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004149 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004150del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151{
4152 int i;
4153
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004154 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 return;
4156
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004157 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158
4159 for (i = 0; i < tc_len; ++i)
4160 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4161 {
4162 del_termcode_idx(i);
4163 return;
4164 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004165 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166}
4167
4168 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004169del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
4171 int i;
4172
4173 vim_free(termcodes[idx].code);
4174 --tc_len;
4175 for (i = idx; i < tc_len; ++i)
4176 termcodes[i] = termcodes[i + 1];
4177}
4178
4179#ifdef FEAT_TERMRESPONSE
4180/*
4181 * Called when detected that the terminal sends 8-bit codes.
4182 * Convert all 7-bit codes to their 8-bit equivalent.
4183 */
4184 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004185switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186{
4187 int i;
4188 int c;
4189
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004190 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 if (!term_is_8bit(T_NAME))
4192 {
4193 for (i = 0; i < tc_len; ++i)
4194 {
4195 c = term_7to8bit(termcodes[i].code);
4196 if (c != 0)
4197 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004198 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 termcodes[i].code[0] = c;
4200 }
4201 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004202 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004205 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206}
4207#endif
4208
4209#ifdef CHECK_DOUBLE_CLICK
4210static linenr_T orig_topline = 0;
4211# ifdef FEAT_DIFF
4212static int orig_topfill = 0;
4213# endif
4214#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004215#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216/*
4217 * Checking for double clicks ourselves.
4218 * "orig_topline" is used to avoid detecting a double-click when the window
4219 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4220 */
4221/*
4222 * Set orig_topline. Used when jumping to another window, so that a double
4223 * click still works.
4224 */
4225 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004226set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227{
4228 orig_topline = wp->w_topline;
4229# ifdef FEAT_DIFF
4230 orig_topfill = wp->w_topfill;
4231# endif
4232}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004233
4234/*
4235 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4236 * topline and topfill.
4237 */
4238 int
4239is_mouse_topline(win_T *wp)
4240{
4241 return orig_topline == wp->w_topline
4242#ifdef FEAT_DIFF
4243 && orig_topfill == wp->w_topfill
4244#endif
4245 ;
4246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247#endif
4248
4249/*
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004250 * Put "string[new_slen]" in typebuf, or in "buf[bufsize]" if "buf" is not NULL.
4251 * Remove "slen" bytes.
4252 * Returns FAIL for error.
4253 */
4254 static int
4255put_string_in_typebuf(
4256 int offset,
4257 int slen,
4258 char_u *string,
4259 int new_slen,
4260 char_u *buf,
4261 int bufsize,
4262 int *buflen)
4263{
4264 int extra = new_slen - slen;
4265
4266 string[new_slen] = NUL;
4267 if (buf == NULL)
4268 {
4269 if (extra < 0)
4270 // remove matched chars, taking care of noremap
4271 del_typebuf(-extra, offset);
4272 else if (extra > 0)
4273 // insert the extra space we need
4274 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
4275
4276 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4277 // typebuf.tb_buf[]!
4278 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4279 (size_t)new_slen);
4280 }
4281 else
4282 {
4283 if (extra < 0)
4284 // remove matched characters
4285 mch_memmove(buf + offset, buf + offset - extra,
4286 (size_t)(*buflen + offset + extra));
4287 else if (extra > 0)
4288 {
4289 // Insert the extra space we need. If there is insufficient
4290 // space return -1.
4291 if (*buflen + extra + new_slen >= bufsize)
4292 return FAIL;
4293 mch_memmove(buf + offset + extra, buf + offset,
4294 (size_t)(*buflen - offset));
4295 }
4296 mch_memmove(buf + offset, string, (size_t)new_slen);
4297 *buflen = *buflen + extra + new_slen;
4298 }
4299 return OK;
4300}
4301
4302/*
4303 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4304 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004305 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004306decode_modifiers(int n)
4307{
4308 int code = n - 1;
4309 int modifiers = 0;
4310
4311 if (code & 1)
4312 modifiers |= MOD_MASK_SHIFT;
4313 if (code & 2)
4314 modifiers |= MOD_MASK_ALT;
4315 if (code & 4)
4316 modifiers |= MOD_MASK_CTRL;
4317 if (code & 8)
4318 modifiers |= MOD_MASK_META;
4319 return modifiers;
4320}
4321
4322 static int
4323modifiers2keycode(int modifiers, int *key, char_u *string)
4324{
4325 int new_slen = 0;
4326
4327 if (modifiers != 0)
4328 {
4329 // Some keys have the modifier included. Need to handle that here to
4330 // make mappings work.
4331 *key = simplify_key(*key, &modifiers);
4332 if (modifiers != 0)
4333 {
4334 string[new_slen++] = K_SPECIAL;
4335 string[new_slen++] = (int)KS_MODIFIER;
4336 string[new_slen++] = modifiers;
4337 }
4338 }
4339 return new_slen;
4340}
4341
4342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 * Check if typebuf.tb_buf[] contains a terminal key code.
4344 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4345 * + max_offset].
4346 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004347 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 * With a match, the match is removed, the replacement code is inserted in
4349 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4350 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004351 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4352 * "buflen" is then the length of the string in buf[] and is updated for
4353 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 */
4355 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004356check_termcode(
4357 int max_offset,
4358 char_u *buf,
4359 int bufsize,
4360 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361{
4362 char_u *tp;
4363 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004364 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004365 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004367 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 int offset;
4369 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004370 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004371 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004372 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004373 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 char_u string[MAX_KEY_CODE_LEN + 1];
4375 int i, j;
4376 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4380
4381 /*
4382 * Speed up the checks for terminal codes by gathering all first bytes
4383 * used in termleader[]. Often this is just a single <Esc>.
4384 */
4385 if (need_gather)
4386 gather_termleader();
4387
4388 /*
4389 * Check at several positions in typebuf.tb_buf[], to catch something like
4390 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4391 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004392 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 * This is used often, KEEP IT FAST!
4394 */
4395 for (offset = 0; offset < max_offset; ++offset)
4396 {
4397 if (buf == NULL)
4398 {
4399 if (offset >= typebuf.tb_len)
4400 break;
4401 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004402 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
4404 else
4405 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004406 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 break;
4408 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004409 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411
4412 /*
4413 * Don't check characters after K_SPECIAL, those are already
4414 * translated terminal chars (avoid translating ~@^Hx).
4415 */
4416 if (*tp == K_SPECIAL)
4417 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004418 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 continue;
4420 }
4421
4422 /*
4423 * Skip this position if the character does not appear as the first
4424 * character in term_strings. This speeds up a lot, since most
4425 * termcodes start with the same character (ESC or CSI).
4426 */
4427 i = *tp;
4428 for (p = termleader; *p && *p != i; ++p)
4429 ;
4430 if (*p == NUL)
4431 continue;
4432
4433 /*
4434 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4435 * are in Insert mode.
4436 */
4437 if (*tp == ESC && !p_ek && (State & INSERT))
4438 continue;
4439
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004440 key_name[0] = NUL; // no key name found yet
4441 key_name[1] = NUL; // no key name found yet
4442 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
4444#ifdef FEAT_GUI
4445 if (gui.in_use)
4446 {
4447 /*
4448 * GUI special key codes are all of the form [CSI xx].
4449 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004450 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
4452 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004453 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 slen = 3;
4455 key_name[0] = tp[1];
4456 key_name[1] = tp[2];
4457 }
4458 }
4459 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004460#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 {
4462 for (idx = 0; idx < tc_len; ++idx)
4463 {
4464 /*
4465 * Ignore the entry if we are not at the start of
4466 * typebuf.tb_buf[]
4467 * and there are not enough characters to make a match.
4468 * But only when the 'K' flag is in 'cpoptions'.
4469 */
4470 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004471 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 if (cpo_koffset && offset && len < slen)
4473 continue;
4474 if (STRNCMP(termcodes[idx].code, tp,
4475 (size_t)(slen > len ? len : slen)) == 0)
4476 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004477 if (len < slen) // got a partial sequence
4478 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
4480 /*
4481 * When found a keypad key, check if there is another key
4482 * that matches and use that one. This makes <Home> to be
4483 * found instead of <kHome> when they produce the same
4484 * key code.
4485 */
4486 if (termcodes[idx].name[0] == 'K'
4487 && VIM_ISDIGIT(termcodes[idx].name[1]))
4488 {
4489 for (j = idx + 1; j < tc_len; ++j)
4490 if (termcodes[j].len == slen &&
4491 STRNCMP(termcodes[idx].code,
4492 termcodes[j].code, slen) == 0)
4493 {
4494 idx = j;
4495 break;
4496 }
4497 }
4498
4499 key_name[0] = termcodes[idx].name[0];
4500 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 break;
4502 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004503
4504 /*
4505 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004506 * <Esc>[123;*X (modslen == slen - 3)
4507 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4508 * When there is a modifier the * matches a number.
4509 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004510 */
4511 if (termcodes[idx].modlen > 0)
4512 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004513 modslen = termcodes[idx].modlen;
4514 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004515 continue;
4516 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004517 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004518 {
4519 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004520
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004521 if (len <= modslen) // got a partial sequence
4522 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004523
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004524 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004525 slen = modslen + 1; // no modifiers
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004526 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004527 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004528 else
4529 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004530 // Skip over the digits, the final char must
4531 // follow. URXVT can use a negative value, thus
4532 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004533 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004534 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004535 ;
4536 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004537 if (len < j) // got a partial sequence
4538 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004539 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004540 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004541
Bram Moolenaara529ce02017-06-22 22:37:57 +02004542 modifiers_start = tp + slen - 2;
4543
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004544 // Match! Convert modifier bits.
4545 n = atoi((char *)modifiers_start);
4546 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004547
4548 slen = j;
4549 }
4550 key_name[0] = termcodes[idx].name[0];
4551 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004552 break;
4553 }
4554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 }
4556 }
4557
4558#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004559 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004560 // Mouse codes of DEC and pterm start with <ESC>[. When
4561 // detecting the start of these mouse codes they might as well be
4562 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004563# ifdef FEAT_MOUSE_DEC
4564 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004565# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004566# ifdef FEAT_MOUSE_PTERM
4567 || key_name[0] == KS_PTERM_MOUSE
4568# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004569 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004571 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
4572
4573 /*
4574 * Check for responses from the terminal starting with {lead}:
4575 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01004576 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004577 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01004578 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004579 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01004580 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004581 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004582 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004583 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004584 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004585 * - window position reply: {lead}3;{x};{y}t
4586 *
4587 * - key with modifiers when modifyOtherKeys is enabled:
4588 * {lead}27;{modifier};{key}~
4589 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01004590 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004591 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004592 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004593 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004595 int first = -1; // optional char right after {lead}
4596 int trail; // char that ends CSI sequence
4597 int arg[3] = {-1, -1, -1}; // argument numbers
4598 int argc; // number of arguments
4599 char_u *ap = argp;
4600 int csi_len;
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004601
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004602 // Check for non-digit after CSI.
4603 if (!VIM_ISDIGIT(*ap))
4604 first = *ap++;
4605
4606 // Find up to three argument numbers.
4607 for (argc = 0; argc < 3; )
4608 {
4609 if (ap >= tp + len)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004610 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004611not_enough:
4612 LOG_TR(("Not enough characters for CSI sequence"));
4613 return -1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004614 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004615 if (*ap == ';')
4616 arg[argc++] = -1; // omitted number
4617 else if (VIM_ISDIGIT(*ap))
4618 {
4619 arg[argc] = 0;
4620 for (;;)
4621 {
4622 if (ap >= tp + len)
4623 goto not_enough;
4624 if (!VIM_ISDIGIT(*ap))
4625 break;
4626 arg[argc] = arg[argc] * 10 + (*ap - '0');
4627 ++ap;
4628 }
4629 ++argc;
4630 }
4631 if (*ap == ';')
4632 ++ap;
4633 else
4634 break;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004635 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004636 // mrxvt has been reported to have "+" in the version. Assume
4637 // the escape sequence ends with a letter or one of "{|}~".
4638 while (ap < tp + len
4639 && !(*ap >= '{' && *ap <= '~')
4640 && !ASCII_ISALPHA(*ap))
4641 ++ap;
4642 if (ap >= tp + len)
4643 goto not_enough;
4644 trail = *ap;
4645 csi_len = (int)(ap - tp) + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004646
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004647 // Cursor position report: Eat it when there are 2 arguments
4648 // and it ends in 'R'. Also when u7_status is not "sent", it
4649 // may be from a previous Vim that just exited. But not for
4650 // <S-F3>, it sends something similar, check for row and column
4651 // to make sense.
4652 if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004653 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004654 if (arg[0] == 2 && arg[1] >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004655 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004656 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004657
Bram Moolenaarb255b902018-04-24 21:40:10 +02004658 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004659 u7_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004660 did_cursorhold = TRUE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004661 if (arg[1] == 2)
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004662 aw = "single";
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004663 else if (arg[1] == 3)
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004664 aw = "double";
4665 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4666 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004667 // Setting the option causes a screen redraw. Do
4668 // that right away if possible, keeping any
4669 // messages.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004670 set_option_value((char_u *)"ambw", 0L,
4671 (char_u *)aw, 0);
4672# ifdef DEBUG_TERMRESPONSE
4673 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004674 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004675
Bram Moolenaarb255b902018-04-24 21:40:10 +02004676 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004677 }
4678# else
4679 redraw_asap(CLEAR);
4680# endif
4681 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004682 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004683 key_name[0] = (int)KS_EXTRA;
4684 key_name[1] = (int)KE_IGNORE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004685 slen = csi_len;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004686# ifdef FEAT_EVAL
4687 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4688# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004689 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004690
4691 // Version string: Eat it when there is at least one digit and
4692 // it ends in 'c'
4693 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004695 int version = arg[1];
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004696
Bram Moolenaarb255b902018-04-24 21:40:10 +02004697 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004698 crv_status.tr_progress = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004699 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004701 // If this code starts with CSI, you can bet that the
4702 // terminal uses 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 if (tp[0] == CSI)
4704 switch_to_8bit();
4705
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004706 // Screen sends 40500.
Bram Moolenaar89577b32019-10-18 21:26:05 +02004707 // rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004708 // Ignore it for when the user has set 'term' to xterm,
4709 // even though it's an rxvt.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004710 if (version > 20000)
4711 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004713 if (first == '>' && argc == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004715 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004716 int is_iterm2 = FALSE;
Bram Moolenaar20091c12018-12-07 13:18:19 +01004717 int is_mintty = FALSE;
Bram Moolenaar89577b32019-10-18 21:26:05 +02004718 int is_screen = FALSE;
Bram Moolenaar20091c12018-12-07 13:18:19 +01004719
4720 // mintty 2.9.5 sends 77;20905;0c.
4721 // (77 is ASCII 'M' for mintty.)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004722 if (arg[0] == 77)
Bram Moolenaar20091c12018-12-07 13:18:19 +01004723 is_mintty = TRUE;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004724
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004725 // if xterm version >= 141 try to get termcap codes
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004726 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004728 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 check_for_codes = TRUE;
4730 need_gather = TRUE;
4731 req_codes_from_term();
4732 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004733
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004734 // libvterm sends 0;100;0
4735 if (version == 100 && arg[0] == 0 && arg[2] == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004736 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004737 // If run from Vim $COLORS is set to the number of
4738 // colors the terminal supports. Otherwise assume
4739 // 256, libvterm supports even more.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004740 if (mch_getenv((char_u *)"COLORS") == NULL)
4741 may_adjust_color_count(256);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004742 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004743 if (!option_was_set((char_u *)"ttym"))
4744 set_option_value((char_u *)"ttym", 0L,
4745 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004746 }
4747
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004748 if (version == 95)
4749 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004750 // Mac Terminal.app sends 1;95;0
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004751 if (arg[0] == 1 && arg[2] == 0)
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004752 {
4753 is_not_xterm = TRUE;
4754 is_mac_terminal = TRUE;
4755 }
Bram Moolenaare330ef42018-07-06 23:11:40 +02004756 // iTerm2 sends 0;95;0
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004757 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004758 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004759 // old iTerm2 sends 0;95;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004760 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaare330ef42018-07-06 23:11:40 +02004761 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004762 }
4763
Bram Moolenaar89577b32019-10-18 21:26:05 +02004764 // screen sends 83;40500;0 83 is 'S' in ASCII.
4765 if (arg[0] == 83)
4766 is_screen = TRUE;
4767
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004768 // Only set 'ttymouse' automatically if it was not set
4769 // by the user already.
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004770 if (!option_was_set((char_u *)"ttym"))
4771 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004772 // Xterm version 277 supports SGR. Also support
Bram Moolenaar89577b32019-10-18 21:26:05 +02004773 // Terminal.app, iTerm2, mintty, and screen 4.7+.
4774 if ((!is_screen && version >= 277)
4775 || is_iterm2
4776 || is_mac_terminal
4777 || is_mintty
4778 || (is_screen && arg[1] >= 40700))
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004779 set_option_value((char_u *)"ttym", 0L,
4780 (char_u *)"sgr", 0);
Bram Moolenaar314ca7c2020-04-17 16:40:31 +02004781 // For xterm version >= 95 mouse dragging works.
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004782 else if (version >= 95)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004783 set_option_value((char_u *)"ttym", 0L,
4784 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004785 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004786
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004787 // Detect terminals that set $TERM to something like
Bram Moolenaar314ca7c2020-04-17 16:40:31 +02004788 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaarc2127982017-09-11 20:34:13 +02004789
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004790 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
4791 // xfce4-terminal sends 1;2802;0.
4792 // screen sends 83;40500;0
4793 // Assuming any version number over 2500 is not an
4794 // xterm (without the limit for rxvt and screen).
4795 if (arg[1] >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004796 is_not_xterm = TRUE;
4797
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004798 // PuTTY sends 0;136;0
4799 // vandyke SecureCRT sends 1;136;0
4800 else if (version == 136 && arg[2] == 0)
Bram Moolenaar314ca7c2020-04-17 16:40:31 +02004801 {
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004802 is_not_xterm = TRUE;
4803
Bram Moolenaar314ca7c2020-04-17 16:40:31 +02004804 // PuTTY supports sgr-like mouse reporting, but
4805 // only set 'ttymouse' if it was not set by the
4806 // user already.
4807 if (arg[0] == 0
4808 && !option_was_set((char_u *)"ttym"))
4809 set_option_value((char_u *)"ttym", 0L,
4810 (char_u *)"sgr", 0);
4811 }
4812
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004813 // Konsole sends 0;115;0
4814 else if (version == 115 && arg[0] == 0 && arg[2] == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004815 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004816
Bram Moolenaar668324e2018-06-30 17:09:26 +02004817 // Xterm first responded to this request at patch level
4818 // 95, so assume anything below 95 is not xterm.
4819 if (version < 95)
4820 is_not_xterm = TRUE;
4821
Bram Moolenaarea563cc2020-06-05 19:36:57 +02004822 // With the real Xterm setting the underline RGB color
4823 // clears the background color, disable "t_8u".
4824 if (!is_not_xterm && *T_8U != NUL)
4825 T_8U = empty_option;
4826
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004827 // Only request the cursor style if t_SH and t_RS are
4828 // set. Only supported properly by xterm since version
4829 // 279 (otherwise it returns 0x18).
4830 // Not for Terminal.app, it can't handle t_RS, it
4831 // echoes the characters to the screen.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004832 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004833 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004834 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004835 && *T_CSH != NUL
4836 && *T_CRS != NUL)
4837 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004838 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004839 out_str(T_CRS);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004840 termrequest_sent(&rcs_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004841 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004842 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004843
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004844 // Only request the cursor blink mode if t_RC set. Not
4845 // for Gnome terminal, it can't handle t_RC, it
4846 // echoes the characters to the screen.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004847 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004848 && !is_not_xterm
4849 && *T_CRC != NUL)
4850 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004851 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004852 out_str(T_CRC);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004853 termrequest_sent(&rbm_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004854 need_flush = TRUE;
4855 }
4856
4857 if (need_flush)
4858 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004860 slen = csi_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004862 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 apply_autocmds(EVENT_TERMRESPONSE,
4865 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 key_name[0] = (int)KS_EXTRA;
4867 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004869
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004870 // Check blinking cursor from xterm:
4871 // {lead}?12;1$y set
4872 // {lead}?12;2$y not set
4873 //
4874 // {lead} can be <Esc>[ or CSI
Bram Moolenaarafd78262019-05-10 23:10:31 +02004875 else if (rbm_status.tr_progress == STATUS_SENT
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004876 && first == '?'
4877 && ap == argp + 6
4878 && arg[0] == 12
4879 && ap[-1] == '$'
4880 && trail == 'y')
Bram Moolenaar4db25542017-08-28 22:43:05 +02004881 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004882 initial_cursor_blink = (arg[1] == '1');
Bram Moolenaarafd78262019-05-10 23:10:31 +02004883 rbm_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004884 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004885 key_name[0] = (int)KS_EXTRA;
4886 key_name[1] = (int)KE_IGNORE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004887 slen = csi_len;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004888# ifdef FEAT_EVAL
4889 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4890# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004891 }
4892
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004893 // Check for a window position response from the terminal:
4894 // {lead}3;{x};{y}t
4895 else if (did_request_winpos && argc == 3 && arg[0] == 3
4896 && trail == 't')
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004897 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004898 winpos_x = arg[1];
4899 winpos_y = arg[2];
4900 // got finished code: consume it
4901 key_name[0] = (int)KS_EXTRA;
4902 key_name[1] = (int)KE_IGNORE;
4903 slen = csi_len;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004904
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004905 if (--did_request_winpos <= 0)
4906 winpos_status.tr_progress = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004907 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004908
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004909 // Key with modifier:
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004910 // {lead}27;{modifier};{key}~
4911 // {lead}{key};{modifier}u
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004912 else if ((arg[0] == 27 && argc == 3 && trail == '~')
4913 || (argc == 2 && trail == 'u'))
4914 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02004915 seenModifyOtherKeys = TRUE;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004916 if (trail == 'u')
4917 key = arg[0];
4918 else
4919 key = arg[2];
4920
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004921 modifiers = decode_modifiers(arg[1]);
Bram Moolenaard1e2f392019-10-12 18:22:50 +02004922
4923 // Some keys already have Shift included, pass them as
Bram Moolenaar459fd782019-10-13 16:43:39 +02004924 // normal keys. Not when Ctrl is also used, because <C-H>
4925 // and <C-S-H> are different.
Bram Moolenaard1e2f392019-10-12 18:22:50 +02004926 if (modifiers == MOD_MASK_SHIFT
4927 && ((key >= '@' && key <= 'Z')
4928 || key == '^' || key == '_'
4929 || (key >= '{' && key <= '~')))
4930 modifiers = 0;
4931
Bram Moolenaar459fd782019-10-13 16:43:39 +02004932 // When used with Ctrl we always make a letter upper case,
4933 // so that mapping <C-H> and <C-h> are the same. Typing
4934 // <C-S-H> also uses "H" but modifier is different.
4935 if ((modifiers & MOD_MASK_CTRL) && ASCII_ISALPHA(key))
4936 key = TOUPPER_ASC(key);
4937
Bram Moolenaard1e2f392019-10-12 18:22:50 +02004938 // insert modifiers with KS_MODIFIER
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004939 new_slen = modifiers2keycode(modifiers, &key, string);
4940 slen = csi_len;
4941
4942 if (has_mbyte)
4943 new_slen += (*mb_char2bytes)(key, string + new_slen);
4944 else
4945 string[new_slen++] = key;
4946
4947 if (put_string_in_typebuf(offset, slen, string, new_slen,
4948 buf, bufsize, buflen) == FAIL)
4949 return -1;
4950 return len + new_slen - slen + offset;
4951 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004952
4953 // else: Unknown CSI sequence. We could drop it, but then the
4954 // user can't create a map for it.
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004955 }
4956
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004957 // Check for fore/background color response from the terminal:
4958 //
4959 // {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
4960 // or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
4961 //
4962 // {code} is 10 for foreground, 11 for background
4963 // {lead} can be <Esc>] or OSC
4964 // {tail} can be '\007', <Esc>\ or STERM.
4965 //
4966 // Consume any code that starts with "{lead}11;", it's also
4967 // possible that "rgba" is following.
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004968 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004969 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4970 || tp[0] == OSC))
4971 {
4972 j = 1 + (tp[0] == ESC);
4973 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004974 || (argp[1] != '1' && argp[1] != '0')
4975 || argp[2] != ';'))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004976 i = 0; // no match
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004977 else
4978 for (i = j; i < len; ++i)
4979 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4980 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004981 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004982 int is_bg = argp[1] == '1';
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004983 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
4984 && tp[j + 16] == '/';
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004985
Bram Moolenaar12e71eb2019-06-06 15:19:31 +02004986 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004987 && (is_4digit
4988 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004989 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02004990 char_u *tp_r = tp + j + 7;
4991 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
4992 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004993# ifdef FEAT_TERMINAL
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004994 int rval, gval, bval;
4995
Bram Moolenaar32e19772019-06-05 22:57:04 +02004996 rval = hexhex2nr(tp_r);
4997 gval = hexhex2nr(tp_b);
4998 bval = hexhex2nr(tp_g);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004999# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005000 if (is_bg)
5001 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02005002 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
5003 *tp_b) ? "light" : "dark";
Bram Moolenaar4b974d52017-06-04 15:37:46 +02005004
Bram Moolenaarb255b902018-04-24 21:40:10 +02005005 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02005006 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005007# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005008 bg_r = rval;
5009 bg_g = gval;
5010 bg_b = bval;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005011# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005012 if (!option_was_set((char_u *)"bg")
Bram Moolenaar32e19772019-06-05 22:57:04 +02005013 && STRCMP(p_bg, new_bg_val) != 0)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005014 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005015 // value differs, apply it
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005016 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar32e19772019-06-05 22:57:04 +02005017 (char_u *)new_bg_val, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005018 reset_option_was_set((char_u *)"bg");
5019 redraw_asap(CLEAR);
5020 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02005021 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005022# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005023 else
5024 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005025 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02005026 rfg_status.tr_progress = STATUS_GOT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005027 fg_r = rval;
5028 fg_g = gval;
5029 fg_b = bval;
5030 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005031# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005032 }
5033
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005034 // got finished code: consume it
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005035 key_name[0] = (int)KS_EXTRA;
5036 key_name[1] = (int)KE_IGNORE;
5037 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005038# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005039 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5040 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005041# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005042 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02005043 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005044 if (i == len)
5045 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005046 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005047 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02005048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 }
5050
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005051 // Check for key code response from xterm:
5052 // {lead}{flag}+r<hex bytes><{tail}
5053 //
5054 // {lead} can be <Esc>P or DCS
5055 // {flag} can be '0' or '1'
5056 // {tail} can be Esc>\ or STERM
5057 //
5058 // Check for cursor shape response from xterm:
5059 // {lead}1$r<digit> q{tail}
5060 //
5061 // {lead} can be <Esc>P or DCS
5062 // {tail} can be <Esc>\ or STERM
5063 //
5064 // Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaarafd78262019-05-10 23:10:31 +02005065 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005066 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 || tp[0] == DCS))
5068 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005069 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005070 if (len < j + 3)
Bram Moolenaar66761db2019-06-05 22:07:51 +02005071 i = len; // need more chars
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005072 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar66761db2019-06-05 22:07:51 +02005073 i = 0; // no match
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005074 else if (argp[1] == '+')
Bram Moolenaar66761db2019-06-05 22:07:51 +02005075 // key code response
5076 for (i = j; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02005078 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5079 || tp[i] == STERM)
5080 {
5081 if (i - j >= 3)
5082 got_code_from_term(tp + j, i);
5083 key_name[0] = (int)KS_EXTRA;
5084 key_name[1] = (int)KE_IGNORE;
5085 slen = i + 1 + (tp[i] == ESC);
5086 break;
5087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01005089 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005090 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02005091 // Probably the cursor shape response. Make sure that "i"
5092 // is equal to "len" when there are not sufficient
5093 // characters.
Bram Moolenaar590ec872018-03-02 20:58:42 +01005094 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005095 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005096 if (i - j == 3 && !isdigit(tp[i]))
5097 break;
5098 if (i - j == 4 && tp[i] != ' ')
5099 break;
5100 if (i - j == 5 && tp[i] != 'q')
5101 break;
5102 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5103 break;
5104 if ((i - j == 6 && tp[i] == STERM)
5105 || (i - j == 7 && tp[i] == '\\'))
5106 {
5107 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005108
Bram Moolenaar66761db2019-06-05 22:07:51 +02005109 // 0, 1 = block blink, 2 = block
5110 // 3 = underline blink, 4 = underline
5111 // 5 = vertical bar blink, 6 = vertical bar
Bram Moolenaar590ec872018-03-02 20:58:42 +01005112 number = number == 0 ? 1 : number;
5113 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaar66761db2019-06-05 22:07:51 +02005114 // The blink flag is actually inverted, compared to
5115 // the value set with T_SH.
Bram Moolenaar590ec872018-03-02 20:58:42 +01005116 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02005117 (number & 1) ? FALSE : TRUE;
Bram Moolenaarafd78262019-05-10 23:10:31 +02005118 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02005119 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005120
Bram Moolenaar590ec872018-03-02 20:58:42 +01005121 key_name[0] = (int)KS_EXTRA;
5122 key_name[1] = (int)KE_IGNORE;
5123 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005124# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01005125 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005126# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01005127 break;
5128 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005129 }
5130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131
5132 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02005133 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005134 // These codes arrive many together, each code can be
5135 // truncated at any point.
Bram Moolenaarb255b902018-04-24 21:40:10 +02005136 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005137 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02005138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140 }
5141#endif
5142
5143 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005144 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005146 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005148#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 /*
5150 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5151 * so that we know which window to scroll later.
5152 */
5153 if (gui.in_use
5154 && key_name[0] == (int)KS_EXTRA
5155 && (key_name[1] == (int)KE_X1MOUSE
5156 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005157 || key_name[1] == (int)KE_MOUSELEFT
5158 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 || key_name[1] == (int)KE_MOUSEDOWN
5160 || key_name[1] == (int)KE_MOUSEUP))
5161 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005162 char_u bytes[6];
5163 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5164
5165 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 return -1;
5167 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5168 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5169 slen += num_bytes;
5170 }
5171 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 /*
5174 * If it is a mouse click, get the coordinates.
5175 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005176 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005177#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005178 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005179#endif
5180#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005181 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005182#endif
5183#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005184 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005185#endif
5186#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005187 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005188#endif
5189#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005190 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005191#endif
5192#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005193 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005194#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005195 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005196 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005198 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5199 &modifiers) == -1)
5200 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202
5203#ifdef FEAT_GUI
5204 /*
5205 * If using the GUI, then we get menu and scrollbar events.
5206 *
5207 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5208 * four bytes which are to be taken as a pointer to the vimmenu_T
5209 * structure.
5210 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005211 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005212 * is one byte with the tab index.
5213 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5215 * by one byte representing the scrollbar number, and then four bytes
5216 * representing a long_u which is the new value of the scrollbar.
5217 *
5218 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5219 * KE_FILLER followed by four bytes representing a long_u which is the
5220 * new value of the scrollbar.
5221 */
5222# ifdef FEAT_MENU
5223 else if (key_name[0] == (int)KS_MENU)
5224 {
5225 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005226 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (num_bytes == -1)
5229 return -1;
5230 current_menu = (vimmenu_T *)val;
5231 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005232
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005233 // The menu may have been deleted right after it was used, check
5234 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005235 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5236 {
5237 key_name[0] = KS_EXTRA;
5238 key_name[1] = (int)KE_IGNORE;
5239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 }
5241# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005242# ifdef FEAT_GUI_TABLINE
5243 else if (key_name[0] == (int)KS_TABLINE)
5244 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005245 // Selecting tabline tab or using its menu.
5246 char_u bytes[6];
5247 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5248
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005249 if (num_bytes == -1)
5250 return -1;
5251 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005252 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005253 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005254 slen += num_bytes;
5255 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005256 else if (key_name[0] == (int)KS_TABMENU)
5257 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005258 // Selecting tabline tab or using its menu.
5259 char_u bytes[6];
5260 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5261
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005262 if (num_bytes == -1)
5263 return -1;
5264 current_tab = (int)bytes[0];
5265 current_tabmenu = (int)bytes[1];
5266 slen += num_bytes;
5267 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269# ifndef USE_ON_FLY_SCROLL
5270 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5271 {
5272 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005273 char_u bytes[6];
5274 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005276 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 j = 0;
5278 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5279 && tp[j + 2] != NUL; ++i)
5280 {
5281 j += 3;
5282 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5283 if (num_bytes == -1)
5284 break;
5285 if (i == 0)
5286 current_scrollbar = (int)bytes[0];
5287 else if (current_scrollbar != (int)bytes[0])
5288 break;
5289 j += num_bytes;
5290 num_bytes = get_long_from_buf(tp + j, &val);
5291 if (num_bytes == -1)
5292 break;
5293 scrollbar_value = val;
5294 j += num_bytes;
5295 slen = j;
5296 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005297 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 return -1;
5299 }
5300 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5301 {
5302 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005303 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005305 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 j = 0;
5307 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5308 && tp[j + 2] != NUL; ++i)
5309 {
5310 j += 3;
5311 num_bytes = get_long_from_buf(tp + j, &val);
5312 if (num_bytes == -1)
5313 break;
5314 scrollbar_value = val;
5315 j += num_bytes;
5316 slen = j;
5317 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005318 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 return -1;
5320 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005321# endif // !USE_ON_FLY_SCROLL
5322#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005324 /*
5325 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5326 */
5327 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5328
5329 /*
5330 * Add any modifier codes to our string.
5331 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005332 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005333
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005334 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005335 key_name[0] = KEY2TERMCAP0(key);
5336 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005338 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005339 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005340 if (has_mbyte)
5341 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5342 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005343 string[new_slen++] = key_name[1];
5344 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005345 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5346 && key_name[1] == KE_IGNORE)
5347 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005348 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5349 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005350 retval = KEYLEN_REMOVED;
5351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 else
5353 {
5354 string[new_slen++] = K_SPECIAL;
5355 string[new_slen++] = key_name[0];
5356 string[new_slen++] = key_name[1];
5357 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005358 if (put_string_in_typebuf(offset, slen, string, new_slen,
5359 buf, bufsize, buflen) == FAIL)
5360 return -1;
5361 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363
Bram Moolenaar2951b772013-07-03 12:45:31 +02005364#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005365 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005366#endif
5367
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005368 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369}
5370
Bram Moolenaar9377df32017-10-15 13:22:01 +02005371#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005372/*
5373 * Get the text foreground color, if known.
5374 */
5375 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005376term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005377{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005378 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005379 {
5380 *r = fg_r;
5381 *g = fg_g;
5382 *b = fg_b;
5383 }
5384}
5385
5386/*
5387 * Get the text background color, if known.
5388 */
5389 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005390term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005391{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005392 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005393 {
5394 *r = bg_r;
5395 *g = bg_g;
5396 *b = bg_b;
5397 }
5398}
5399#endif
5400
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401/*
5402 * Replace any terminal code strings in from[] with the equivalent internal
5403 * vim representation. This is used for the "from" and "to" part of a
5404 * mapping, and the "to" part of a menu command.
5405 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5406 * '<'.
5407 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5408 *
5409 * The replacement is done in result[] and finally copied into allocated
5410 * memory. If this all works well *bufp is set to the allocated memory and a
5411 * pointer to it is returned. If something fails *bufp is set to NULL and from
5412 * is returned.
5413 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005414 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5415 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5416 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5417 * used instead of a CTRL-V.
5418 *
5419 * Flags:
5420 * REPTERM_FROM_PART see above
5421 * REPTERM_DO_LT also translate <lt>
5422 * REPTERM_SPECIAL always accept <key> notation
5423 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5424 *
5425 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5426 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005428 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005429replace_termcodes(
5430 char_u *from,
5431 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005432 int flags,
5433 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434{
5435 int i;
5436 int slen;
5437 int key;
5438 int dlen = 0;
5439 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005440 int do_backslash; // backslash is a special character
5441 int do_special; // recognize <> key codes
5442 int do_key_code; // recognize raw key codes
5443 char_u *result; // buffer for resulting string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
5445 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02005446 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
5447 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5449
5450 /*
5451 * Allocate space for the translation. Worst case a single character is
5452 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5453 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005454 result = alloc(STRLEN(from) * 6 + 1);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005455 if (result == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
5457 *bufp = NULL;
5458 return from;
5459 }
5460
5461 src = from;
5462
5463 /*
5464 * Check for #n at start only: function key n
5465 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005466 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 {
5468 result[dlen++] = K_SPECIAL;
5469 result[dlen++] = 'k';
5470 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005471 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005473 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 src += 2;
5475 }
5476
5477 /*
5478 * Copy each byte from *from to result[dlen]
5479 */
5480 while (*src != NUL)
5481 {
5482 /*
5483 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005484 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005486 if (do_special && ((flags & REPTERM_DO_LT)
5487 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 {
5489#ifdef FEAT_EVAL
5490 /*
5491 * Replace <SID> by K_SNR <script-nr> _.
5492 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5493 */
5494 if (STRNICMP(src, "<SID>", 5) == 0)
5495 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005496 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005497 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 else
5499 {
5500 src += 5;
5501 result[dlen++] = K_SPECIAL;
5502 result[dlen++] = (int)KS_EXTRA;
5503 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005504 sprintf((char *)result + dlen, "%ld",
5505 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 dlen += (int)STRLEN(result + dlen);
5507 result[dlen++] = '_';
5508 continue;
5509 }
5510 }
5511#endif
5512
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005513 slen = trans_special(&src, result + dlen, FSK_KEYCODE
5514 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
5515 did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 if (slen)
5517 {
5518 dlen += slen;
5519 continue;
5520 }
5521 }
5522
5523 /*
5524 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5525 * Note that this is also checked after replacing the <> form.
5526 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5527 * it could be a character in the file.
5528 */
5529 if (do_key_code)
5530 {
5531 i = find_term_bykeys(src);
5532 if (i >= 0)
5533 {
5534 result[dlen++] = K_SPECIAL;
5535 result[dlen++] = termcodes[i].name[0];
5536 result[dlen++] = termcodes[i].name[1];
5537 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005538 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 continue;
5540 }
5541 }
5542
5543#ifdef FEAT_EVAL
5544 if (do_special)
5545 {
5546 char_u *p, *s, len;
5547
5548 /*
5549 * Replace <Leader> by the value of "mapleader".
5550 * Replace <LocalLeader> by the value of "maplocalleader".
5551 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5552 */
5553 if (STRNICMP(src, "<Leader>", 8) == 0)
5554 {
5555 len = 8;
5556 p = get_var_value((char_u *)"g:mapleader");
5557 }
5558 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5559 {
5560 len = 13;
5561 p = get_var_value((char_u *)"g:maplocalleader");
5562 }
5563 else
5564 {
5565 len = 0;
5566 p = NULL;
5567 }
5568 if (len != 0)
5569 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005570 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5572 s = (char_u *)"\\";
5573 else
5574 s = p;
5575 while (*s != NUL)
5576 result[dlen++] = *s++;
5577 src += len;
5578 continue;
5579 }
5580 }
5581#endif
5582
5583 /*
5584 * Remove CTRL-V and ignore the next character.
5585 * For "from" side the CTRL-V at the end is included, for the "to"
5586 * part it is removed.
5587 * If 'cpoptions' does not contain 'B', also accept a backslash.
5588 */
5589 key = *src;
5590 if (key == Ctrl_V || (do_backslash && key == '\\'))
5591 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005592 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 if (*src == NUL)
5594 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02005595 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 result[dlen++] = key;
5597 break;
5598 }
5599 }
5600
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005601 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005602 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 {
5604 /*
5605 * If the character is K_SPECIAL, replace it with K_SPECIAL
5606 * KS_SPECIAL KE_FILLER.
5607 * If compiled with the GUI replace CSI with K_CSI.
5608 */
5609 if (*src == K_SPECIAL)
5610 {
5611 result[dlen++] = K_SPECIAL;
5612 result[dlen++] = KS_SPECIAL;
5613 result[dlen++] = KE_FILLER;
5614 }
5615# ifdef FEAT_GUI
5616 else if (*src == CSI)
5617 {
5618 result[dlen++] = K_SPECIAL;
5619 result[dlen++] = KS_EXTRA;
5620 result[dlen++] = (int)KE_CSI;
5621 }
5622# endif
5623 else
5624 result[dlen++] = *src;
5625 ++src;
5626 }
5627 }
5628 result[dlen] = NUL;
5629
5630 /*
5631 * Copy the new string to allocated memory.
5632 * If this fails, just return from.
5633 */
5634 if ((*bufp = vim_strsave(result)) != NULL)
5635 from = *bufp;
5636 vim_free(result);
5637 return from;
5638}
5639
5640/*
5641 * Find a termcode with keys 'src' (must be NUL terminated).
5642 * Return the index in termcodes[], or -1 if not found.
5643 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005644 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005645find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
5647 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005648 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
5650 for (i = 0; i < tc_len; ++i)
5651 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005652 if (slen == termcodes[i].len
5653 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 return i;
5655 }
5656 return -1;
5657}
5658
5659/*
5660 * Gather the first characters in the terminal key codes into a string.
5661 * Used to speed up check_termcode().
5662 */
5663 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005664gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665{
5666 int i;
5667 int len = 0;
5668
5669#ifdef FEAT_GUI
5670 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005671 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672#endif
5673#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005674 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005675 termleader[len++] = DCS; // the termcode response starts with DCS
5676 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677#endif
5678 termleader[len] = NUL;
5679
5680 for (i = 0; i < tc_len; ++i)
5681 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5682 {
5683 termleader[len++] = termcodes[i].code[0];
5684 termleader[len] = NUL;
5685 }
5686
5687 need_gather = FALSE;
5688}
5689
5690/*
5691 * Show all termcodes (for ":set termcap")
5692 * This code looks a lot like showoptions(), but is different.
5693 */
5694 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005695show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696{
5697 int col;
5698 int *items;
5699 int item_count;
5700 int run;
5701 int row, rows;
5702 int cols;
5703 int i;
5704 int len;
5705
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005706#define INC3 27 // try to make three columns
5707#define INC2 40 // try to make two columns
5708#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005710 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005712 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 if (items == NULL)
5714 return;
5715
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005716 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01005717 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718
5719 /*
5720 * do the loop two times:
5721 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005722 * 2. display the medium items (medium length strings)
5723 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005725 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 {
5727 /*
5728 * collect the items in items[]
5729 */
5730 item_count = 0;
5731 for (i = 0; i < tc_len; i++)
5732 {
5733 len = show_one_termcode(termcodes[i].name,
5734 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005735 if (len <= INC3 - GAP ? run == 1
5736 : len <= INC2 - GAP ? run == 2
5737 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 items[item_count++] = i;
5739 }
5740
5741 /*
5742 * display the items
5743 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005744 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005746 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 if (cols == 0)
5748 cols = 1;
5749 rows = (item_count + cols - 1) / cols;
5750 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005751 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 rows = item_count;
5753 for (row = 0; row < rows && !got_int; ++row)
5754 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005755 msg_putchar('\n'); // go to next line
5756 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 break;
5758 col = 0;
5759 for (i = row; i < item_count; i += rows)
5760 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005761 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 show_one_termcode(termcodes[items[i]].name,
5763 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005764 if (run == 2)
5765 col += INC2;
5766 else
5767 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 }
5769 out_flush();
5770 ui_breakcheck();
5771 }
5772 }
5773 vim_free(items);
5774}
5775
5776/*
5777 * Show one termcode entry.
5778 * Output goes into IObuff[]
5779 */
5780 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005781show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782{
5783 char_u *p;
5784 int len;
5785
5786 if (name[0] > '~')
5787 {
5788 IObuff[0] = ' ';
5789 IObuff[1] = ' ';
5790 IObuff[2] = ' ';
5791 IObuff[3] = ' ';
5792 }
5793 else
5794 {
5795 IObuff[0] = 't';
5796 IObuff[1] = '_';
5797 IObuff[2] = name[0];
5798 IObuff[3] = name[1];
5799 }
5800 IObuff[4] = ' ';
5801
5802 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5803 if (p[1] != 't')
5804 STRCPY(IObuff + 5, p);
5805 else
5806 IObuff[5] = NUL;
5807 len = (int)STRLEN(IObuff);
5808 do
5809 IObuff[len++] = ' ';
5810 while (len < 17);
5811 IObuff[len] = NUL;
5812 if (code == NULL)
5813 len += 4;
5814 else
5815 len += vim_strsize(code);
5816
5817 if (printit)
5818 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005819 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005821 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 else
5823 msg_outtrans(code);
5824 }
5825 return len;
5826}
5827
5828#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5829/*
5830 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5831 * termcap codes from the terminal itself.
5832 * We get them one by one to avoid a very long response string.
5833 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005834static int xt_index_in = 0;
5835static int xt_index_out = 0;
5836
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005838req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
5840 xt_index_out = 0;
5841 xt_index_in = 0;
5842 req_more_codes_from_term();
5843}
5844
5845 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005846req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
5848 char buf[11];
5849 int old_idx = xt_index_out;
5850
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005851 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 if (exiting)
5853 return;
5854
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005855 // Send up to 10 more requests out than we received. Avoid sending too
5856 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5858 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005859 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02005860
Bram Moolenaarb255b902018-04-24 21:40:10 +02005861 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
5862 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 out_str_nf((char_u *)buf);
5864 ++xt_index_out;
5865 }
5866
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005867 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 if (xt_index_out != old_idx)
5869 out_flush();
5870}
5871
5872/*
5873 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5874 * A "0" instead of the "1" indicates a code that isn't supported.
5875 * Both <name> and <string> are encoded in hex.
5876 * "code" points to the "0" or "1".
5877 */
5878 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005879got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
5881#define XT_LEN 100
5882 char_u name[3];
5883 char_u str[XT_LEN];
5884 int i;
5885 int j = 0;
5886 int c;
5887
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005888 // A '1' means the code is supported, a '0' means it isn't.
5889 // When half the length is > XT_LEN we can't use it.
5890 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5892 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005893 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 name[0] = hexhex2nr(code + 3);
5895 name[1] = hexhex2nr(code + 5);
5896 name[2] = NUL;
5897 for (i = 0; key_names[i] != NULL; ++i)
5898 {
5899 if (STRCMP(key_names[i], name) == 0)
5900 {
5901 xt_index_in = i;
5902 break;
5903 }
5904 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005905
Bram Moolenaarb255b902018-04-24 21:40:10 +02005906 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
5907
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 if (key_names[i] != NULL)
5909 {
5910 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5911 str[j++] = c;
5912 str[j] = NUL;
5913 if (name[0] == 'C' && name[1] == 'o')
5914 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005915 // Color count is not a key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02005917 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 }
5919 else
5920 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005921 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 i = find_term_bykeys(str);
5923 if (i >= 0)
5924 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005925 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 }
5927 }
5928 }
5929
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005930 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 ++xt_index_in;
5932 req_more_codes_from_term();
5933}
5934
5935/*
5936 * Check if there are any unanswered requests and deal with them.
5937 * This is called before starting an external program or getting direct
5938 * keyboard input. We don't want responses to be send to that program or
5939 * handled as typed text.
5940 */
5941 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005942check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943{
5944 int c;
5945
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005946 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5948 return;
5949
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005950 // Vgetc() will check for and handle any response.
5951 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 ++no_mapping;
5953 ++allow_keys;
5954 for (;;)
5955 {
5956 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005957 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 break;
5959
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005960 // If a response is recognized it's replaced with K_IGNORE, must read
5961 // it from the input stream. If there is no K_IGNORE we can't do
5962 // anything, break here (there might be some responses further on, but
5963 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 if (c != K_SPECIAL && c != K_IGNORE)
5965 break;
5966 c = vgetc();
5967 if (c != K_IGNORE)
5968 {
5969 vungetc(c);
5970 break;
5971 }
5972 }
5973 --no_mapping;
5974 --allow_keys;
5975}
5976#endif
5977
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005978#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979static char ksme_str[20];
5980static char ksmr_str[20];
5981static char ksmd_str[20];
5982
5983/*
5984 * For Win32 console: update termcap codes for existing console attributes.
5985 */
5986 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005987update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 struct builtin_term *p;
5990
5991 p = find_builtin_term(DEFAULT_TERM);
5992 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
5993 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005994 attr | 0x08); // FOREGROUND_INTENSITY
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
5996 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
5997
5998 while (p->bt_string != NULL)
5999 {
6000 if (p->bt_entry == (int)KS_ME)
6001 p->bt_string = &ksme_str[0];
6002 else if (p->bt_entry == (int)KS_MR)
6003 p->bt_string = &ksmr_str[0];
6004 else if (p->bt_entry == (int)KS_MD)
6005 p->bt_string = &ksmd_str[0];
6006 ++p;
6007 }
6008}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006009
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006010# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006011# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006012struct ks_tbl_s
6013{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006014 int code; // value of KS_
6015 char *vtp; // code in vtp mode
6016 char *vtp2; // code in vtp2 mode
6017 char buf[KSSIZE]; // save buffer in non-vtp mode
6018 char vbuf[KSSIZE]; // save buffer in vtp mode
6019 char v2buf[KSSIZE]; // save buffer in vtp2 mode
6020 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006021};
6022
6023static struct ks_tbl_s ks_tbl[] =
6024{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006025 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6026 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6027 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6028 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6029 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6030 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6031 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6032 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6033 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006034# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006035 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6036 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006037 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6038 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006039# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006040 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6041 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006042 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6043 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006044# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006045 {(int)KS_CCO, "256", "256"}, // colors
6046 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006047};
6048
6049 static struct builtin_term *
6050find_first_tcap(
6051 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006052 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006053{
6054 struct builtin_term *p;
6055
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006056 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006057 if (p->bt_entry == code)
6058 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006059 return NULL;
6060}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006061# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006062
6063/*
6064 * For Win32 console: replace the sequence immediately after termguicolors.
6065 */
6066 void
6067swap_tcap(void)
6068{
6069# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006070 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006071 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006072 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006073 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006074 int mode;
6075 enum
6076 {
6077 CMODEINDEX,
6078 CMODE24,
6079 CMODE256
6080 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006081
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006082 // buffer initialization
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006083 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006084 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006085 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006086 {
6087 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006088 if (bt != NULL)
6089 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006090 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6091 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6092 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6093
6094 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6095 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006096 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006097 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006098 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006099 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006100 }
6101
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006102 if (p_tgc)
6103 mode = CMODE24;
6104 else if (t_colors >= 256)
6105 mode = CMODE256;
6106 else
6107 mode = CMODEINDEX;
6108
6109 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006110 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006111 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6112 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006113 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006114 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006115 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006116 case CMODEINDEX:
6117 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6118 break;
6119 case CMODE24:
6120 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6121 break;
6122 default:
6123 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006124 }
6125 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006126 }
6127
6128 if (mode != curr_mode)
6129 {
6130 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006131 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006132 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6133 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006134 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006135 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006136 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006137 case CMODEINDEX:
6138 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6139 break;
6140 case CMODE24:
6141 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6142 break;
6143 default:
6144 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006145 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006146 }
6147 }
6148
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006149 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006150 }
6151# endif
6152}
6153
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006155
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006156#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006157 static int
6158hex_digit(int c)
6159{
6160 if (isdigit(c))
6161 return c - '0';
6162 c = TOLOWER_ASC(c);
6163 if (c >= 'a' && c <= 'f')
6164 return c - 'a' + 10;
6165 return 0x1ffffff;
6166}
6167
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006168# ifdef VIMDLL
6169 static guicolor_T
6170gui_adjust_rgb(guicolor_T c)
6171{
6172 if (gui.in_use)
6173 return c;
6174 else
6175 return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff);
6176}
6177# else
6178# define gui_adjust_rgb(c) (c)
6179# endif
6180
Bram Moolenaarab302212016-04-26 20:59:29 +02006181 guicolor_T
6182gui_get_color_cmn(char_u *name)
6183{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006184 // On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6185 // values as used by the MS-Windows GDI api. It should be used only for
6186 // MS-Windows GDI builds.
Bram Moolenaar4f974752019-02-17 17:44:42 +01006187# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar28726652017-01-06 18:16:19 +01006188# undef RGB
6189# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006190# ifndef RGB
6191# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6192# endif
6193# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006194 FILE *fd;
6195 char line[LINE_LEN];
6196 char_u *fname;
6197 int r, g, b, i;
6198 guicolor_T color;
6199
6200 struct rgbcolor_table_S {
6201 char_u *color_name;
6202 guicolor_T color;
6203 };
6204
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006205 // Only non X11 colors (not present in rgb.txt) and colors in
6206 // color_names[], useful when $VIMRUNTIME is not found,.
Bram Moolenaarab302212016-04-26 20:59:29 +02006207 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006208 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6209 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6210 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6211 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6212 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6213 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6214 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6215 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6216 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6217 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6218 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006219 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, // No X11
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006220 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006221 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6222 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006223 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006224 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006225 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006226 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6227 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6228 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6229 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6230 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006231 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, // No X11
6232 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, // No X11
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006233 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6234 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006235 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006236 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006237 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6238 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006239 };
6240
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006241 static struct rgbcolor_table_S *colornames_table;
6242 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006243
6244 if (name[0] == '#' && STRLEN(name) == 7)
6245 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006246 // Name is in "#rrggbb" format
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006247 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006248 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6249 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6250 if (color > 0xffffff)
6251 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006252 return gui_adjust_rgb(color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006253 }
6254
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006255 // Check if the name is one of the colors we know
Bram Moolenaarab302212016-04-26 20:59:29 +02006256 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6257 if (STRICMP(name, rgb_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006258 return gui_adjust_rgb(rgb_table[i].color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006259
6260 /*
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006261 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
Bram Moolenaarab302212016-04-26 20:59:29 +02006262 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006263 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006264 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006265 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006266
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006267 // colornames_table not yet initialized
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006268 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6269 if (fname == NULL)
6270 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006271
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006272 fd = fopen((char *)fname, "rt");
6273 vim_free(fname);
6274 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006275 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006276 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006277 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006278 size = -1; // don't try again
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006279 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006280 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006281
6282 for (counting = 1; counting >= 0; --counting)
6283 {
6284 if (!counting)
6285 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006286 colornames_table = ALLOC_MULT(struct rgbcolor_table_S, size);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006287 if (colornames_table == NULL)
6288 {
6289 fclose(fd);
6290 return INVALCOLOR;
6291 }
6292 rewind(fd);
6293 }
6294 size = 0;
6295
6296 while (!feof(fd))
6297 {
6298 size_t len;
6299 int pos;
6300
Bram Moolenaar42335f52018-09-13 15:33:43 +02006301 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006302 len = strlen(line);
6303
6304 if (len <= 1 || line[len - 1] != '\n')
6305 continue;
6306
6307 line[len - 1] = '\0';
6308
6309 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6310 if (i != 3)
6311 continue;
6312
6313 if (!counting)
6314 {
6315 char_u *s = vim_strsave((char_u *)line + pos);
6316
6317 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006318 {
6319 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006320 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006321 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006322 colornames_table[size].color_name = s;
6323 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6324 }
6325 size++;
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006326
6327 // The distributed rgb.txt has less than 1000 entries. Limit to
6328 // 10000, just in case the file was messed up.
6329 if (size == 10000)
6330 break;
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006331 }
6332 }
6333 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006334 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006335
6336 for (i = 0; i < size; i++)
6337 if (STRICMP(name, colornames_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006338 return gui_adjust_rgb(colornames_table[i].color);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006339
Bram Moolenaarab302212016-04-26 20:59:29 +02006340 return INVALCOLOR;
6341}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006342
6343 guicolor_T
6344gui_get_rgb_color_cmn(int r, int g, int b)
6345{
6346 guicolor_T color = RGB(r, g, b);
6347
6348 if (color > 0xffffff)
6349 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006350 return gui_adjust_rgb(color);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006351}
Bram Moolenaarab302212016-04-26 20:59:29 +02006352#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006353
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006354#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006355 || defined(PROTO)
6356static int cube_value[] = {
6357 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6358};
6359
6360static int grey_ramp[] = {
6361 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6362 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6363};
6364
Bram Moolenaar9894e392018-05-05 14:29:06 +02006365static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006366// R G B idx
6367 { 0, 0, 0, 1}, // black
6368 {224, 0, 0, 2}, // dark red
6369 { 0, 224, 0, 3}, // dark green
6370 {224, 224, 0, 4}, // dark yellow / brown
6371 { 0, 0, 224, 5}, // dark blue
6372 {224, 0, 224, 6}, // dark magenta
6373 { 0, 224, 224, 7}, // dark cyan
6374 {224, 224, 224, 8}, // light grey
6375
6376 {128, 128, 128, 9}, // dark grey
6377 {255, 64, 64, 10}, // light red
6378 { 64, 255, 64, 11}, // light green
6379 {255, 255, 64, 12}, // yellow
6380 { 64, 64, 255, 13}, // light blue
6381 {255, 64, 255, 14}, // light magenta
6382 { 64, 255, 255, 15}, // light cyan
6383 {255, 255, 255, 16}, // white
6384};
6385
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006386#define ANSI_INDEX_NONE 0
6387
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006388 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006389cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006390{
6391 int idx;
6392
6393 if (nr < 16)
6394 {
6395 *r = ansi_table[nr][0];
6396 *g = ansi_table[nr][1];
6397 *b = ansi_table[nr][2];
6398 *ansi_idx = ansi_table[nr][3];
6399 }
6400 else if (nr < 232)
6401 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006402 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006403 idx = nr - 16;
6404 *r = cube_value[idx / 36 % 6];
6405 *g = cube_value[idx / 6 % 6];
6406 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006407 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006408 }
6409 else if (nr < 256)
6410 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006411 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006412 idx = nr - 232;
6413 *r = grey_ramp[idx];
6414 *g = grey_ramp[idx];
6415 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006416 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006417 }
6418 else
6419 {
6420 *r = 0;
6421 *g = 0;
6422 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006423 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006424 }
6425}
6426#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006427
Bram Moolenaarf4140482020-02-15 23:06:45 +01006428/*
6429 * Replace K_BS by <BS> and K_DEL by <DEL>
6430 */
6431 void
6432term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len)
6433{
6434 int i;
6435 int c;
6436
6437 for (i = ta_len; i < ta_len + len; ++i)
6438 {
6439 if (ta_buf[i] == CSI && len - i > 2)
6440 {
6441 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6442 if (c == K_DEL || c == K_KDEL || c == K_BS)
6443 {
6444 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
6445 (size_t)(len - i - 2));
6446 if (c == K_DEL || c == K_KDEL)
6447 ta_buf[i] = DEL;
6448 else
6449 ta_buf[i] = Ctrl_H;
6450 len -= 2;
6451 }
6452 }
6453 else if (ta_buf[i] == '\r')
6454 ta_buf[i] = '\n';
6455 if (has_mbyte)
6456 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6457 }
6458}