blob: f93fde84bc5bea2d5d052cbfc481cff6d00fa064 [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")},
931# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100932 {(int)KS_CBE, IF_EB("\033[?2004h", ESC_STR "[?2004h")},
933 {(int)KS_CBD, IF_EB("\033[?2004l", ESC_STR "[?2004l")},
Bram Moolenaar40385db2018-08-07 22:31:44 +0200934 {(int)KS_CST, IF_EB("\033[22;2t", ESC_STR "[22;2t")},
935 {(int)KS_CRT, IF_EB("\033[23;2t", ESC_STR "[23;2t")},
936 {(int)KS_SSI, IF_EB("\033[22;1t", ESC_STR "[22;1t")},
937 {(int)KS_SRI, IF_EB("\033[23;1t", ESC_STR "[23;1t")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000938
939 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
940 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
941 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
942 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100943 // An extra set of cursor keys for vt100 mode
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000944 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
945 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
946 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
947 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100948 // An extra set of function keys for vt100 mode
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000949 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
950 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
951 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
952 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000953 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
954 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
955 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
956 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
957 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
958 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
959 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
960 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
961 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
962 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
963 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
964 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000966 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
967 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
968 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
969 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100970 // {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")},
971 // {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")},
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000972 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100973 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, // other Home
974 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, // other Home
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000975 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100976 // {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")},
977 // {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000978 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100979 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, // other End
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000980 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000981 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
982 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100983 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, // keypad plus
984 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, // keypad minus
985 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, // keypad /
986 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, // keypad *
987 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, // keypad Enter
988 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, // keypad .
989 {K_K0, IF_EB("\033O*p", ESC_STR "O*p")}, // keypad 0
990 {K_K1, IF_EB("\033O*q", ESC_STR "O*q")}, // keypad 1
991 {K_K2, IF_EB("\033O*r", ESC_STR "O*r")}, // keypad 2
992 {K_K3, IF_EB("\033O*s", ESC_STR "O*s")}, // keypad 3
993 {K_K4, IF_EB("\033O*t", ESC_STR "O*t")}, // keypad 4
994 {K_K5, IF_EB("\033O*u", ESC_STR "O*u")}, // keypad 5
995 {K_K6, IF_EB("\033O*v", ESC_STR "O*v")}, // keypad 6
996 {K_K7, IF_EB("\033O*w", ESC_STR "O*w")}, // keypad 7
997 {K_K8, IF_EB("\033O*x", ESC_STR "O*x")}, // keypad 8
998 {K_K9, IF_EB("\033O*y", ESC_STR "O*y")}, // keypad 9
999 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, // keypad Del
1000 {K_PS, IF_EB("\033[200~", ESC_STR "[200~")}, // paste start
1001 {K_PE, IF_EB("\033[201~", ESC_STR "[201~")}, // paste end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002
1003 {BT_EXTRA_KEYS, ""},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001004 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, // F0
1005 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, // F13
1006 // F14 and F15 are missing, because they send the same codes as the undo
1007 // and help key, although they don't work on all keyboards.
1008 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, // F16
1009 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, // F17
1010 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, // F18
1011 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, // F19
1012 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, // F20
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001013
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001014 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, // F21
1015 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, // F22
1016 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, // F23
1017 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, // F24
1018 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, // F25
1019 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, // F26
1020 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, // F27
1021 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, // F28
1022 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, // F29
1023 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, // F30
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001024
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001025 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, // F31
1026 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, // F32
1027 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, // F33
1028 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, // F34
1029 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, // F35
1030 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, // F36
1031 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, // F37
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032# endif
1033
1034# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1035/*
1036 * iris-ansi for Silicon Graphics machines.
1037 */
1038 {(int)KS_NAME, "iris-ansi"},
1039 {(int)KS_CE, "\033[K"},
1040 {(int)KS_CD, "\033[J"},
1041 {(int)KS_AL, "\033[L"},
1042# ifdef TERMINFO
1043 {(int)KS_CAL, "\033[%p1%dL"},
1044# else
1045 {(int)KS_CAL, "\033[%dL"},
1046# endif
1047 {(int)KS_DL, "\033[M"},
1048# ifdef TERMINFO
1049 {(int)KS_CDL, "\033[%p1%dM"},
1050# else
1051 {(int)KS_CDL, "\033[%dM"},
1052# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001053#if 0 // The scroll region is not working as Vim expects.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054# ifdef TERMINFO
1055 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1056# else
1057 {(int)KS_CS, "\033[%i%d;%dr"},
1058# endif
1059#endif
1060 {(int)KS_CL, "\033[H\033[2J"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001061 {(int)KS_VE, "\033[9/y\033[12/y"}, // These aren't documented
1062 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, // These aren't documented
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 {(int)KS_TI, "\033[=6h"},
1064 {(int)KS_TE, "\033[=6l"},
1065 {(int)KS_SE, "\033[21;27m"},
1066 {(int)KS_SO, "\033[1;7m"},
1067 {(int)KS_ME, "\033[m"},
1068 {(int)KS_MR, "\033[7m"},
1069 {(int)KS_MD, "\033[1m"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001070 {(int)KS_CCO, "8"}, // allow 8 colors
1071 {(int)KS_CZH, "\033[3m"}, // italic mode on
1072 {(int)KS_CZR, "\033[23m"}, // italic mode off
1073 {(int)KS_US, "\033[4m"}, // underline on
1074 {(int)KS_UE, "\033[24m"}, // underline off
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075# ifdef TERMINFO
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001076 {(int)KS_CAB, "\033[4%p1%dm"}, // set background color (ANSI)
1077 {(int)KS_CAF, "\033[3%p1%dm"}, // set foreground color (ANSI)
1078 {(int)KS_CSB, "\033[102;%p1%dm"}, // set screen background color
1079 {(int)KS_CSF, "\033[101;%p1%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001081 {(int)KS_CAB, "\033[4%dm"}, // set background color (ANSI)
1082 {(int)KS_CAF, "\033[3%dm"}, // set foreground color (ANSI)
1083 {(int)KS_CSB, "\033[102;%dm"}, // set screen background color
1084 {(int)KS_CSF, "\033[101;%dm"}, // set screen foreground color
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085# endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001086 {(int)KS_MS, "y"}, // guessed
1087 {(int)KS_UT, "y"}, // guessed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {(int)KS_LE, "\b"},
1089# ifdef TERMINFO
1090 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1091# else
1092 {(int)KS_CM, "\033[%i%d;%dH"},
1093# endif
1094 {(int)KS_SR, "\033M"},
1095# ifdef TERMINFO
1096 {(int)KS_CRI, "\033[%p1%dC"},
1097# else
1098 {(int)KS_CRI, "\033[%dC"},
1099# endif
1100 {(int)KS_CIS, "\033P3.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001101 {(int)KS_CIE, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {(int)KS_TS, "\033P1.y"},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001103 {(int)KS_FS, "\234"}, // ST "String Terminator"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104# ifdef TERMINFO
1105 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1106 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1107# else
1108 {(int)KS_CWS, "\033[203;%d;%d/y"},
1109 {(int)KS_CWP, "\033[205;%d;%d/y"},
1110# endif
1111 {K_UP, "\033[A"},
1112 {K_DOWN, "\033[B"},
1113 {K_LEFT, "\033[D"},
1114 {K_RIGHT, "\033[C"},
1115 {K_S_UP, "\033[161q"},
1116 {K_S_DOWN, "\033[164q"},
1117 {K_S_LEFT, "\033[158q"},
1118 {K_S_RIGHT, "\033[167q"},
1119 {K_F1, "\033[001q"},
1120 {K_F2, "\033[002q"},
1121 {K_F3, "\033[003q"},
1122 {K_F4, "\033[004q"},
1123 {K_F5, "\033[005q"},
1124 {K_F6, "\033[006q"},
1125 {K_F7, "\033[007q"},
1126 {K_F8, "\033[008q"},
1127 {K_F9, "\033[009q"},
1128 {K_F10, "\033[010q"},
1129 {K_F11, "\033[011q"},
1130 {K_F12, "\033[012q"},
1131 {K_S_F1, "\033[013q"},
1132 {K_S_F2, "\033[014q"},
1133 {K_S_F3, "\033[015q"},
1134 {K_S_F4, "\033[016q"},
1135 {K_S_F5, "\033[017q"},
1136 {K_S_F6, "\033[018q"},
1137 {K_S_F7, "\033[019q"},
1138 {K_S_F8, "\033[020q"},
1139 {K_S_F9, "\033[021q"},
1140 {K_S_F10, "\033[022q"},
1141 {K_S_F11, "\033[023q"},
1142 {K_S_F12, "\033[024q"},
1143 {K_INS, "\033[139q"},
1144 {K_HOME, "\033[H"},
1145 {K_END, "\033[146q"},
1146 {K_PAGEUP, "\033[150q"},
1147 {K_PAGEDOWN, "\033[154q"},
1148# endif
1149
1150# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1151/*
1152 * for debugging
1153 */
1154 {(int)KS_NAME, "debug"},
1155 {(int)KS_CE, "[CE]"},
1156 {(int)KS_CD, "[CD]"},
1157 {(int)KS_AL, "[AL]"},
1158# ifdef TERMINFO
1159 {(int)KS_CAL, "[CAL%p1%d]"},
1160# else
1161 {(int)KS_CAL, "[CAL%d]"},
1162# endif
1163 {(int)KS_DL, "[DL]"},
1164# ifdef TERMINFO
1165 {(int)KS_CDL, "[CDL%p1%d]"},
1166# else
1167 {(int)KS_CDL, "[CDL%d]"},
1168# endif
1169# ifdef TERMINFO
1170 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1171# else
1172 {(int)KS_CS, "[%dCS%d]"},
1173# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001174# ifdef TERMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
Bram Moolenaar4033c552017-09-16 20:54:51 +02001176# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 {(int)KS_CSV, "[%dCSV%d]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178# endif
1179# ifdef TERMINFO
1180 {(int)KS_CAB, "[CAB%p1%d]"},
1181 {(int)KS_CAF, "[CAF%p1%d]"},
1182 {(int)KS_CSB, "[CSB%p1%d]"},
1183 {(int)KS_CSF, "[CSF%p1%d]"},
1184# else
1185 {(int)KS_CAB, "[CAB%d]"},
1186 {(int)KS_CAF, "[CAF%d]"},
1187 {(int)KS_CSB, "[CSB%d]"},
1188 {(int)KS_CSF, "[CSF%d]"},
1189# endif
1190 {(int)KS_OP, "[OP]"},
1191 {(int)KS_LE, "[LE]"},
1192 {(int)KS_CL, "[CL]"},
1193 {(int)KS_VI, "[VI]"},
1194 {(int)KS_VE, "[VE]"},
1195 {(int)KS_VS, "[VS]"},
1196 {(int)KS_ME, "[ME]"},
1197 {(int)KS_MR, "[MR]"},
1198 {(int)KS_MB, "[MB]"},
1199 {(int)KS_MD, "[MD]"},
1200 {(int)KS_SE, "[SE]"},
1201 {(int)KS_SO, "[SO]"},
1202 {(int)KS_UE, "[UE]"},
1203 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001204 {(int)KS_UCE, "[UCE]"},
1205 {(int)KS_UCS, "[UCS]"},
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02001206 {(int)KS_STE, "[STE]"},
1207 {(int)KS_STS, "[STS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {(int)KS_MS, "[MS]"},
1209 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001210 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211# ifdef TERMINFO
1212 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1213# else
1214 {(int)KS_CM, "[%dCM%d]"},
1215# endif
1216 {(int)KS_SR, "[SR]"},
1217# ifdef TERMINFO
1218 {(int)KS_CRI, "[CRI%p1%d]"},
1219# else
1220 {(int)KS_CRI, "[CRI%d]"},
1221# endif
1222 {(int)KS_VB, "[VB]"},
1223 {(int)KS_KS, "[KS]"},
1224 {(int)KS_KE, "[KE]"},
1225 {(int)KS_TI, "[TI]"},
1226 {(int)KS_TE, "[TE]"},
1227 {(int)KS_CIS, "[CIS]"},
1228 {(int)KS_CIE, "[CIE]"},
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001229 {(int)KS_CSC, "[CSC]"},
1230 {(int)KS_CEC, "[CEC]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {(int)KS_TS, "[TS]"},
1232 {(int)KS_FS, "[FS]"},
1233# ifdef TERMINFO
1234 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1235 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1236# else
1237 {(int)KS_CWS, "[%dCWS%d]"},
1238 {(int)KS_CWP, "[%dCWP%d]"},
1239# endif
1240 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001241 {(int)KS_U7, "[U7]"},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02001242 {(int)KS_RFG, "[RFG]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001243 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 {K_UP, "[KU]"},
1245 {K_DOWN, "[KD]"},
1246 {K_LEFT, "[KL]"},
1247 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001248 {K_XUP, "[xKU]"},
1249 {K_XDOWN, "[xKD]"},
1250 {K_XLEFT, "[xKL]"},
1251 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {K_S_UP, "[S-KU]"},
1253 {K_S_DOWN, "[S-KD]"},
1254 {K_S_LEFT, "[S-KL]"},
1255 {K_C_LEFT, "[C-KL]"},
1256 {K_S_RIGHT, "[S-KR]"},
1257 {K_C_RIGHT, "[C-KR]"},
1258 {K_F1, "[F1]"},
1259 {K_XF1, "[xF1]"},
1260 {K_F2, "[F2]"},
1261 {K_XF2, "[xF2]"},
1262 {K_F3, "[F3]"},
1263 {K_XF3, "[xF3]"},
1264 {K_F4, "[F4]"},
1265 {K_XF4, "[xF4]"},
1266 {K_F5, "[F5]"},
1267 {K_F6, "[F6]"},
1268 {K_F7, "[F7]"},
1269 {K_F8, "[F8]"},
1270 {K_F9, "[F9]"},
1271 {K_F10, "[F10]"},
1272 {K_F11, "[F11]"},
1273 {K_F12, "[F12]"},
1274 {K_S_F1, "[S-F1]"},
1275 {K_S_XF1, "[S-xF1]"},
1276 {K_S_F2, "[S-F2]"},
1277 {K_S_XF2, "[S-xF2]"},
1278 {K_S_F3, "[S-F3]"},
1279 {K_S_XF3, "[S-xF3]"},
1280 {K_S_F4, "[S-F4]"},
1281 {K_S_XF4, "[S-xF4]"},
1282 {K_S_F5, "[S-F5]"},
1283 {K_S_F6, "[S-F6]"},
1284 {K_S_F7, "[S-F7]"},
1285 {K_S_F8, "[S-F8]"},
1286 {K_S_F9, "[S-F9]"},
1287 {K_S_F10, "[S-F10]"},
1288 {K_S_F11, "[S-F11]"},
1289 {K_S_F12, "[S-F12]"},
1290 {K_HELP, "[HELP]"},
1291 {K_UNDO, "[UNDO]"},
1292 {K_BS, "[BS]"},
1293 {K_INS, "[INS]"},
1294 {K_KINS, "[KINS]"},
1295 {K_DEL, "[DEL]"},
1296 {K_KDEL, "[KDEL]"},
1297 {K_HOME, "[HOME]"},
1298 {K_S_HOME, "[C-HOME]"},
1299 {K_C_HOME, "[C-HOME]"},
1300 {K_KHOME, "[KHOME]"},
1301 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001302 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 {K_END, "[END]"},
1304 {K_S_END, "[C-END]"},
1305 {K_C_END, "[C-END]"},
1306 {K_KEND, "[KEND]"},
1307 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001308 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {K_PAGEUP, "[PAGEUP]"},
1310 {K_PAGEDOWN, "[PAGEDOWN]"},
1311 {K_KPAGEUP, "[KPAGEUP]"},
1312 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1313 {K_MOUSE, "[MOUSE]"},
1314 {K_KPLUS, "[KPLUS]"},
1315 {K_KMINUS, "[KMINUS]"},
1316 {K_KDIVIDE, "[KDIVIDE]"},
1317 {K_KMULTIPLY, "[KMULTIPLY]"},
1318 {K_KENTER, "[KENTER]"},
1319 {K_KPOINT, "[KPOINT]"},
Bram Moolenaarec2da362017-01-21 20:04:22 +01001320 {K_PS, "[PASTE-START]"},
1321 {K_PE, "[PASTE-END]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 {K_K0, "[K0]"},
1323 {K_K1, "[K1]"},
1324 {K_K2, "[K2]"},
1325 {K_K3, "[K3]"},
1326 {K_K4, "[K4]"},
1327 {K_K5, "[K5]"},
1328 {K_K6, "[K6]"},
1329 {K_K7, "[K7]"},
1330 {K_K8, "[K8]"},
1331 {K_K9, "[K9]"},
1332# endif
1333
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001334#endif // NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
1336/*
1337 * The most minimal terminal: only clear screen and cursor positioning
1338 * Always included.
1339 */
1340 {(int)KS_NAME, "dumb"},
1341 {(int)KS_CL, "\014"},
1342#ifdef TERMINFO
1343 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1344 ESC_STR "[%i%p1%d;%p2%dH")},
1345#else
1346 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1347#endif
1348
1349/*
1350 * end marker
1351 */
1352 {(int)KS_NAME, NULL}
1353
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001354}; // end of builtin_termcaps
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001356#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001357 static guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001358termgui_mch_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001359{
Bram Moolenaarab302212016-04-26 20:59:29 +02001360 return gui_get_color_cmn(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001361}
1362
1363 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001364termgui_get_color(char_u *name)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001365{
1366 guicolor_T t;
1367
1368 if (*name == NUL)
1369 return INVALCOLOR;
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001370 t = termgui_mch_get_color(name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001371
1372 if (t == INVALCOLOR)
Bram Moolenaar87202262020-05-24 17:23:45 +02001373 semsg(_(e_alloc_color), name);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001374 return t;
1375}
1376
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001377 guicolor_T
Bram Moolenaar61be73b2016-04-29 22:59:22 +02001378termgui_mch_get_rgb(guicolor_T color)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001379{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02001380 return color;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001381}
1382#endif
1383
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384/*
1385 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#ifdef AMIGA
1388# define DEFAULT_TERM (char_u *)"amiga"
1389#endif
1390
1391#ifdef MSWIN
1392# define DEFAULT_TERM (char_u *)"win32"
1393#endif
1394
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395#if defined(UNIX) && !defined(__MINT__)
1396# define DEFAULT_TERM (char_u *)"ansi"
1397#endif
1398
1399#ifdef __MINT__
1400# define DEFAULT_TERM (char_u *)"vt52"
1401#endif
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403#ifdef VMS
1404# define DEFAULT_TERM (char_u *)"vt320"
1405#endif
1406
Bram Moolenaarb3f74062020-02-26 16:16:53 +01001407#ifdef __HAIKU__
1408# undef DEFAULT_TERM
1409# define DEFAULT_TERM (char_u *)"xterm"
1410#endif
1411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412#ifndef DEFAULT_TERM
1413# define DEFAULT_TERM (char_u *)"dumb"
1414#endif
1415
1416/*
1417 * Term_strings contains currently used terminal output strings.
1418 * It is initialized with the default values by parse_builtin_tcap().
1419 * The values can be changed by setting the option with the same name.
1420 */
1421char_u *(term_strings[(int)KS_LAST + 1]);
1422
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001423static int need_gather = FALSE; // need to fill termleader[]
1424static char_u termleader[256 + 1]; // for check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425#ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001426static int check_for_codes = FALSE; // check for key code response
1427static int is_not_xterm = FALSE; // recognized not-really-xterm
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428#endif
1429
1430 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001431find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 struct builtin_term *p;
1434
1435 p = builtin_termcaps;
1436 while (p->bt_string != NULL)
1437 {
1438 if (p->bt_entry == (int)KS_NAME)
1439 {
1440#ifdef UNIX
1441 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1442 return p;
1443 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1444 return p;
1445 else
1446#endif
1447#ifdef VMS
1448 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1449 return p;
1450 else
1451#endif
1452 if (STRCMP(term, p->bt_string) == 0)
1453 return p;
1454 }
1455 ++p;
1456 }
1457 return p;
1458}
1459
1460/*
1461 * Parsing of the builtin termcap entries.
1462 * Caller should check if 'name' is a valid builtin term.
1463 * The terminal's name is not set, as this is already done in termcapinit().
1464 */
1465 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001466parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
1468 struct builtin_term *p;
1469 char_u name[2];
1470 int term_8bit;
1471
1472 p = find_builtin_term(term);
1473 term_8bit = term_is_8bit(term);
1474
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001475 // Do not parse if builtin term not found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (p->bt_string == NULL)
1477 return;
1478
1479 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1480 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001481 if ((int)p->bt_entry >= 0) // KS_xx entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001483 // Only set the value if it wasn't set yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (term_strings[p->bt_entry] == NULL
1485 || term_strings[p->bt_entry] == empty_option)
1486 {
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001487#ifdef FEAT_EVAL
1488 int opt_idx = -1;
1489#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001490 // 8bit terminal: use CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1492 {
1493 char_u *s, *t;
1494
1495 s = vim_strsave((char_u *)p->bt_string);
1496 if (s != NULL)
1497 {
1498 for (t = s; *t; ++t)
1499 if (term_7to8bit(t))
1500 {
1501 *t = term_7to8bit(t);
Bram Moolenaar18085fa2018-07-10 17:33:45 +02001502 STRMOVE(t + 1, t + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
1504 term_strings[p->bt_entry] = s;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001505#ifdef FEAT_EVAL
1506 opt_idx =
1507#endif
1508 set_term_option_alloced(
1509 &term_strings[p->bt_entry]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
1511 }
1512 else
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 term_strings[p->bt_entry] = (char_u *)p->bt_string;
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001515#ifdef FEAT_EVAL
1516 opt_idx = get_term_opt_idx(&term_strings[p->bt_entry]);
1517#endif
1518 }
1519#ifdef FEAT_EVAL
1520 set_term_option_sctx_idx(NULL, opt_idx);
1521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 }
1523 }
1524 else
1525 {
1526 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1527 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1528 if (find_termcode(name) == NULL)
1529 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1530 }
1531 }
1532}
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001533
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534/*
1535 * Set number of colors.
1536 * Store it as a number in t_colors.
1537 * Store it as a string in T_CCO (using nr_colors[]).
1538 */
Bram Moolenaaracc770a2020-04-12 15:11:06 +02001539 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001540set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001542 char_u nr_colors[20]; // string for number of colors
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
1544 t_colors = nr;
1545 if (t_colors > 1)
1546 sprintf((char *)nr_colors, "%d", t_colors);
1547 else
1548 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001549 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550}
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001551
Bram Moolenaard7d3cbe2017-07-22 21:15:42 +02001552#if defined(FEAT_TERMRESPONSE)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001553/*
1554 * Set the color count to "val" and redraw if it changed.
1555 */
1556 static void
1557may_adjust_color_count(int val)
1558{
1559 if (val != t_colors)
1560 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001561 // Nr of colors changed, initialize highlighting and
1562 // redraw everything. This causes a redraw, which usually
1563 // clears the message. Try keeping the message if it
1564 // might work.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001565 set_keep_msg_from_hist();
1566 set_color_count(val);
1567 init_highlight(TRUE, FALSE);
1568# ifdef DEBUG_TERMRESPONSE
1569 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02001570 int r = redraw_asap(CLEAR);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001571
Bram Moolenaarb255b902018-04-24 21:40:10 +02001572 log_tr("Received t_Co, redraw_asap(): %d", r);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001573 }
Bram Moolenaar87202262020-05-24 17:23:45 +02001574# else
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001575 redraw_asap(CLEAR);
Bram Moolenaar87202262020-05-24 17:23:45 +02001576# endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02001577 }
1578}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579#endif
1580
1581#ifdef HAVE_TGETENT
1582static char *(key_names[]) =
1583{
Bram Moolenaar87202262020-05-24 17:23:45 +02001584# ifdef FEAT_TERMRESPONSE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001585 // Do this one first, it may cause a screen redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 "Co",
Bram Moolenaar87202262020-05-24 17:23:45 +02001587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 "#2", "#4", "%i", "*7",
1590 "k1", "k2", "k3", "k4", "k5", "k6",
1591 "k7", "k8", "k9", "k;", "F1", "F2",
1592 "%1", "&8", "kb", "kI", "kD", "kh",
1593 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1594 NULL
1595};
1596#endif
1597
Bram Moolenaar9289df52018-05-10 14:40:57 +02001598#ifdef HAVE_TGETENT
Bram Moolenaar69e05692018-05-10 14:11:52 +02001599 static void
1600get_term_entries(int *height, int *width)
1601{
1602 static struct {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001603 enum SpecialKey dest; // index in term_strings[]
1604 char *name; // termcap name for string
Bram Moolenaar69e05692018-05-10 14:11:52 +02001605 } string_names[] =
1606 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1607 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1608 {KS_CL, "cl"}, {KS_CD, "cd"},
1609 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1610 {KS_ME, "me"}, {KS_MR, "mr"},
1611 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1612 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1613 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1614 {KS_STE,"Te"}, {KS_STS,"Ts"},
1615 {KS_CM, "cm"}, {KS_SR, "sr"},
1616 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1617 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
Bram Moolenaar171a9212019-10-12 21:08:59 +02001618 {KS_CTI, "TI"}, {KS_CTE, "TE"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001619 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1620 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1621 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1622 {KS_VS, "vs"}, {KS_CVS, "VS"},
1623 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1624 {KS_CSC, "SC"}, {KS_CEC, "EC"},
1625 {KS_TS, "ts"}, {KS_FS, "fs"},
1626 {KS_CWP, "WP"}, {KS_CWS, "WS"},
1627 {KS_CSI, "SI"}, {KS_CEI, "EI"},
1628 {KS_U7, "u7"}, {KS_RFG, "RF"}, {KS_RBG, "RB"},
1629 {KS_8F, "8f"}, {KS_8B, "8b"},
1630 {KS_CBE, "BE"}, {KS_CBD, "BD"},
1631 {KS_CPS, "PS"}, {KS_CPE, "PE"},
Bram Moolenaar40385db2018-08-07 22:31:44 +02001632 {KS_CST, "ST"}, {KS_CRT, "RT"},
1633 {KS_SSI, "Si"}, {KS_SRI, "Ri"},
Bram Moolenaar69e05692018-05-10 14:11:52 +02001634 {(enum SpecialKey)0, NULL}
1635 };
1636 int i;
1637 char_u *p;
1638 static char_u tstrbuf[TBUFSZ];
1639 char_u *tp = tstrbuf;
1640
1641 /*
1642 * get output strings
1643 */
1644 for (i = 0; string_names[i].name != NULL; ++i)
1645 {
1646 if (TERM_STR(string_names[i].dest) == NULL
1647 || TERM_STR(string_names[i].dest) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001648 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001649 TERM_STR(string_names[i].dest) = TGETSTR(string_names[i].name, &tp);
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001650#ifdef FEAT_EVAL
1651 set_term_option_sctx_idx(string_names[i].name, -1);
1652#endif
1653 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001654 }
1655
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001656 // tgetflag() returns 1 if the flag is present, 0 if not and
1657 // possibly -1 if the flag doesn't exist.
Bram Moolenaar69e05692018-05-10 14:11:52 +02001658 if ((T_MS == NULL || T_MS == empty_option) && tgetflag("ms") > 0)
1659 T_MS = (char_u *)"y";
1660 if ((T_XS == NULL || T_XS == empty_option) && tgetflag("xs") > 0)
1661 T_XS = (char_u *)"y";
1662 if ((T_XN == NULL || T_XN == empty_option) && tgetflag("xn") > 0)
1663 T_XN = (char_u *)"y";
1664 if ((T_DB == NULL || T_DB == empty_option) && tgetflag("db") > 0)
1665 T_DB = (char_u *)"y";
1666 if ((T_DA == NULL || T_DA == empty_option) && tgetflag("da") > 0)
1667 T_DA = (char_u *)"y";
1668 if ((T_UT == NULL || T_UT == empty_option) && tgetflag("ut") > 0)
1669 T_UT = (char_u *)"y";
1670
1671 /*
1672 * get key codes
1673 */
1674 for (i = 0; key_names[i] != NULL; ++i)
1675 if (find_termcode((char_u *)key_names[i]) == NULL)
1676 {
1677 p = TGETSTR(key_names[i], &tp);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001678 // if cursor-left == backspace, ignore it (televideo 925)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001679 if (p != NULL
1680 && (*p != Ctrl_H
1681 || key_names[i][0] != 'k'
1682 || key_names[i][1] != 'l'))
1683 add_termcode((char_u *)key_names[i], p, FALSE);
1684 }
1685
1686 if (*height == 0)
1687 *height = tgetnum("li");
1688 if (*width == 0)
1689 *width = tgetnum("co");
1690
1691 /*
1692 * Get number of colors (if not done already).
1693 */
1694 if (TERM_STR(KS_CCO) == NULL || TERM_STR(KS_CCO) == empty_option)
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001695 {
Bram Moolenaar69e05692018-05-10 14:11:52 +02001696 set_color_count(tgetnum("Co"));
Bram Moolenaar35bc7d62018-10-02 14:45:10 +02001697#ifdef FEAT_EVAL
1698 set_term_option_sctx_idx("Co", -1);
1699#endif
1700 }
Bram Moolenaar69e05692018-05-10 14:11:52 +02001701
1702# ifndef hpux
1703 BC = (char *)TGETSTR("bc", &tp);
1704 UP = (char *)TGETSTR("up", &tp);
1705 p = TGETSTR("pc", &tp);
1706 if (p)
1707 PC = *p;
1708# endif
1709}
Bram Moolenaar9289df52018-05-10 14:40:57 +02001710#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001711
1712 static void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001713report_term_error(char *error_msg, char_u *term)
Bram Moolenaar69e05692018-05-10 14:11:52 +02001714{
1715 struct builtin_term *termp;
1716
1717 mch_errmsg("\r\n");
1718 if (error_msg != NULL)
1719 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001720 mch_errmsg(error_msg);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001721 mch_errmsg("\r\n");
1722 }
1723 mch_errmsg("'");
1724 mch_errmsg((char *)term);
1725 mch_errmsg(_("' not known. Available builtin terminals are:"));
1726 mch_errmsg("\r\n");
1727 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL; ++termp)
1728 {
1729 if (termp->bt_entry == (int)KS_NAME)
1730 {
1731#ifdef HAVE_TGETENT
1732 mch_errmsg(" builtin_");
1733#else
1734 mch_errmsg(" ");
1735#endif
1736 mch_errmsg(termp->bt_string);
1737 mch_errmsg("\r\n");
1738 }
1739 }
1740}
1741
1742 static void
1743report_default_term(char_u *term)
1744{
1745 mch_errmsg(_("defaulting to '"));
1746 mch_errmsg((char *)term);
1747 mch_errmsg("'\r\n");
1748 if (emsg_silent == 0)
1749 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001750 screen_start(); // don't know where cursor is now
Bram Moolenaar69e05692018-05-10 14:11:52 +02001751 out_flush();
1752 if (!is_not_a_term())
Bram Moolenaareda1da02019-11-17 17:06:33 +01001753 ui_delay(2007L, TRUE);
Bram Moolenaar69e05692018-05-10 14:11:52 +02001754 }
1755}
1756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757/*
1758 * Set terminal options for terminal "term".
1759 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1760 *
1761 * While doing this, until ttest(), some options may be NULL, be careful.
1762 */
1763 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001764set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765{
1766 struct builtin_term *termp;
1767#ifdef HAVE_TGETENT
1768 int builtin_first = p_tbi;
1769 int try;
1770 int termcap_cleared = FALSE;
1771#endif
1772 int width = 0, height = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001773 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 char_u *bs_p, *del_p;
1775
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001776 // In silect mode (ex -s) we don't use the 'term' option.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001777 if (silent_mode)
1778 return OK;
1779
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001780 detected_8bit = FALSE; // reset 8-bit detection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 if (term_is_builtin(term))
1783 {
1784 term += 8;
1785#ifdef HAVE_TGETENT
1786 builtin_first = 1;
1787#endif
1788 }
1789
1790/*
1791 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1792 * If builtin_first is TRUE:
1793 * 0. try builtin termcap
1794 * 1. try external termcap
1795 * 2. if both fail default to a builtin terminal
1796 * If builtin_first is FALSE:
1797 * 1. try external termcap
1798 * 2. try builtin termcap, if both fail default to a builtin terminal
1799 */
1800#ifdef HAVE_TGETENT
1801 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1802 {
1803 /*
1804 * Use external termcap
1805 */
1806 if (try == 1)
1807 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 char_u tbuf[TBUFSZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
1810 /*
1811 * If the external termcap does not have a matching entry, try the
1812 * builtin ones.
1813 */
1814 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (!termcap_cleared)
1817 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001818 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 termcap_cleared = TRUE;
1820 }
1821
Bram Moolenaar69e05692018-05-10 14:11:52 +02001822 get_term_entries(&height, &width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 }
1824 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001825 else // try == 0 || try == 2
1826#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 /*
1828 * Use builtin termcap
1829 */
1830 {
1831#ifdef HAVE_TGETENT
1832 /*
1833 * If builtin termcap was already used, there is no need to search
1834 * for the builtin termcap again, quit now.
1835 */
1836 if (try == 2 && builtin_first && termcap_cleared)
1837 break;
1838#endif
1839 /*
1840 * search for 'term' in builtin_termcaps[]
1841 */
1842 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001843 if (termp->bt_string == NULL) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
1845#ifdef HAVE_TGETENT
1846 /*
1847 * If try == 0, first try the external termcap. If that is not
1848 * found we'll get back here with try == 2.
1849 * If termcap_cleared is set we used the external termcap,
1850 * don't complain about not finding the term in the builtin
1851 * termcap.
1852 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001853 if (try == 0) // try external one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 continue;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001855 if (termcap_cleared) // found in external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 break;
1857#endif
Bram Moolenaar69e05692018-05-10 14:11:52 +02001858 report_term_error(error_msg, term);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001860 // when user typed :set term=xxx, quit here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 if (starting != NO_SCREEN)
1862 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001863 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 wait_return(TRUE);
1865 return FAIL;
1866 }
1867 term = DEFAULT_TERM;
Bram Moolenaar69e05692018-05-10 14:11:52 +02001868 report_default_term(term);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001869 set_string_option_direct((char_u *)"term", -1, term,
1870 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 display_errors();
1872 }
1873 out_flush();
1874#ifdef HAVE_TGETENT
1875 if (!termcap_cleared)
1876 {
1877#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001878 clear_termoptions(); // clear old options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879#ifdef HAVE_TGETENT
1880 termcap_cleared = TRUE;
1881 }
1882#endif
1883 parse_builtin_tcap(term);
1884#ifdef FEAT_GUI
1885 if (term_is_gui(term))
1886 {
1887 out_flush();
1888 gui_init();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001889 // If starting the GUI failed, don't do any of the other
1890 // things for this terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (!gui.in_use)
1892 return FAIL;
1893#ifdef HAVE_TGETENT
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001894 break; // don't try using external termcap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895#endif
1896 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001897#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 }
1899#ifdef HAVE_TGETENT
1900 }
1901#endif
1902
1903/*
1904 * special: There is no info in the termcap about whether the cursor
1905 * positioning is relative to the start of the screen or to the start of the
1906 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1907 * relative.
1908 */
1909 if (STRCMP(term, "pcterm") == 0)
1910 T_CCS = (char_u *)"yes";
1911 else
1912 T_CCS = empty_option;
1913
1914#ifdef UNIX
1915/*
1916 * Any "stty" settings override the default for t_kb from the termcap.
1917 * This is in os_unix.c, because it depends a lot on the version of unix that
1918 * is being used.
1919 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1920 */
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001921# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (!gui.in_use)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 get_stty();
1925#endif
1926
1927/*
1928 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1929 * didn't work, use the default CTRL-H
1930 * The default for t_kD is DEL, unless t_kb is DEL.
1931 * The vim_strsave'd strings are probably lost forever, well it's only two
1932 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1933 * directly.
1934 */
1935#ifdef FEAT_GUI
1936 if (!gui.in_use)
1937#endif
1938 {
1939 bs_p = find_termcode((char_u *)"kb");
1940 del_p = find_termcode((char_u *)"kD");
1941 if (bs_p == NULL || *bs_p == NUL)
1942 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1943 if ((del_p == NULL || *del_p == NUL) &&
1944 (bs_p == NULL || *bs_p != DEL))
1945 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1946 }
1947
1948#if defined(UNIX) || defined(VMS)
1949 term_is_xterm = vim_is_xterm(term);
1950#endif
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02001951#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3cea8a92019-10-17 21:55:24 +02001952 is_not_xterm = FALSE;
1953 is_mac_terminal = FALSE;
Bram Moolenaar0d2c4bf2019-10-17 22:17:02 +02001954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001956#if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 /*
1958 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1959 * The termcode for the mouse is added as a side effect in option.c.
1960 */
1961 {
Bram Moolenaar6d006f92017-06-23 22:35:34 +02001962 char_u *p = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001964# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001965 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 {
1967 if (use_xterm_mouse())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001968 p = NULL; // keep existing value, might be "xterm2"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 else
1970 p = (char_u *)"xterm";
1971 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001974 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001976 // Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1977 // "xterm2" in check_termcode().
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001978 reset_option_was_set((char_u *)"ttym");
1979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 if (p == NULL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001981# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 || gui.in_use
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 )
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001985 check_mouse_termcode(); // set mouse termcode anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001987#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991#ifdef USE_TERM_CONSOLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001992 // DEFAULT_TERM indicates that it is the machine console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 if (STRCMP(term, DEFAULT_TERM) != 0)
1994 term_console = FALSE;
1995 else
1996 {
1997 term_console = TRUE;
1998# ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001999 win_resize_on(); // enable window resizing reports
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000# endif
2001 }
2002#endif
2003
2004#if defined(UNIX) || defined(VMS)
2005 /*
2006 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
2007 */
2008 if (vim_is_fastterm(term))
2009 p_tf = TRUE;
2010#endif
2011#ifdef USE_TERM_CONSOLE
2012 /*
2013 * 'ttyfast' is default on consoles
2014 */
2015 if (term_console)
2016 p_tf = TRUE;
2017#endif
2018
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002019 ttest(TRUE); // make sure we have a valid set of terminal codes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002021 full_screen = TRUE; // we can use termcap codes from now on
2022 set_term_defaults(); // use current values as defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02002024 LOG_TR(("setting crv_status to STATUS_GET"));
Bram Moolenaarafd78262019-05-10 23:10:31 +02002025 crv_status.tr_progress = STATUS_GET; // Get terminal version later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026#endif
2027
2028 /*
2029 * Initialize the terminal with the appropriate termcap codes.
2030 * Set the mouse and window title if possible.
2031 * Don't do this when starting, need to parse the .vimrc first, because it
2032 * may redefine t_TI etc.
2033 */
2034 if (starting != NO_SCREEN)
2035 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002036 starttermcap(); // may change terminal mode
2037 setmouse(); // may start using the mouse
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038#ifdef FEAT_TITLE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002039 maketitle(); // may display window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040#endif
2041 }
2042
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002043 // display initial screen after ttest() checking. jw.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 if (width <= 0 || height <= 0)
2045 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002046 // termcap failed to report size
2047 // set defaults, in case ui_get_shellsize() also fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 width = 80;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002049#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002050 height = 25; // console is often 25 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051#else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002052 height = 24; // most terminals are 24 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053#endif
2054 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002055 set_shellsize(width, height, FALSE); // may change Rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if (starting != NO_SCREEN)
2057 {
2058 if (scroll_region)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002059 scroll_region_reset(); // In case Rows changed
2060 check_map_keycodes(); // check mappings for terminal codes used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002063 buf_T *buf;
2064 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
2066 /*
2067 * Execute the TermChanged autocommands for each buffer that is
2068 * loaded.
2069 */
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002070 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
2072 if (curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002073 {
2074 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2076 curbuf);
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002077 // restore curwin/curbuf and a few other things
2078 aucmd_restbuf(&aco);
2079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 }
2083
2084#ifdef FEAT_TERMRESPONSE
2085 may_req_termresponse();
2086#endif
2087
2088 return OK;
2089}
2090
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091#ifdef HAVE_TGETENT
2092/*
2093 * Call tgetent()
2094 * Return error message if it fails, NULL if it's OK.
2095 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002096 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002097tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
2099 int i;
2100
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002101 // Note: Valgrind may report a leak here, because the library keeps one
2102 // buffer around that we can't ever free.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 i = TGETENT(tbuf, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002104 if (i < 0 // -1 is always an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105# ifdef TGETENT_ZERO_ERR
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002106 || i == 0 // sometimes zero is also an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107# endif
2108 )
2109 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002110 // On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2111 // tgetent() with the always existing "dumb" entry to avoid a crash or
2112 // hang.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 (void)TGETENT(tbuf, "dumb");
2114
2115 if (i < 0)
2116# ifdef TGETENT_ZERO_ERR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002117 return _("E557: Cannot open termcap file");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 if (i == 0)
2119# endif
2120#ifdef TERMINFO
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002121 return _("E558: Terminal entry not found in terminfo");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002123 return _("E559: Terminal entry not found in termcap");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124#endif
2125 }
2126 return NULL;
2127}
2128
2129/*
2130 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2131 * Fix that here.
2132 */
2133 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002134vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135{
2136 char *p;
2137
2138 p = tgetstr(s, (char **)pp);
2139 if (p == (char *)-1)
2140 p = NULL;
2141 return (char_u *)p;
2142}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002143#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002145#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146/*
2147 * Get Columns and Rows from the termcap. Used after a window signal if the
2148 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2149 * and "li" entries never change. But on some systems this works.
2150 * Errors while getting the entries are ignored.
2151 */
2152 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002153getlinecol(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002154 long *cp, // pointer to columns
2155 long *rp) // pointer to rows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
2157 char_u tbuf[TBUFSZ];
2158
2159 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2160 {
2161 if (*cp == 0)
2162 *cp = tgetnum("co");
2163 if (*rp == 0)
2164 *rp = tgetnum("li");
2165 }
2166}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002167#endif // defined(HAVE_TGETENT) && defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
2169/*
2170 * Get a string entry from the termcap and add it to the list of termcodes.
2171 * Used for <t_xx> special keys.
2172 * Give an error message for failure when not sourcing.
2173 * If force given, replace an existing entry.
2174 * Return FAIL if the entry was not found, OK if the entry was added.
2175 */
2176 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002177add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178{
2179 char_u *term;
2180 int key;
2181 struct builtin_term *termp;
2182#ifdef HAVE_TGETENT
2183 char_u *string;
2184 int i;
2185 int builtin_first;
2186 char_u tbuf[TBUFSZ];
2187 char_u tstrbuf[TBUFSZ];
2188 char_u *tp = tstrbuf;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002189 char *error_msg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190#endif
2191
2192/*
2193 * If the GUI is running or will start in a moment, we only support the keys
2194 * that the GUI can produce.
2195 */
2196#ifdef FEAT_GUI
2197 if (gui.in_use || gui.starting)
2198 return gui_mch_haskey(name);
2199#endif
2200
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002201 if (!force && find_termcode(name) != NULL) // it's already there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 return OK;
2203
2204 term = T_NAME;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002205 if (term == NULL || *term == NUL) // 'term' not defined yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 return FAIL;
2207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002208 if (term_is_builtin(term)) // name starts with "builtin_"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
2210 term += 8;
2211#ifdef HAVE_TGETENT
2212 builtin_first = TRUE;
2213#endif
2214 }
2215#ifdef HAVE_TGETENT
2216 else
2217 builtin_first = p_tbi;
2218#endif
2219
2220#ifdef HAVE_TGETENT
2221/*
2222 * We can get the entry from the builtin termcap and from the external one.
2223 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2224 * builtin termcap first.
2225 * If 'ttybuiltin' is off, try external termcap first.
2226 */
2227 for (i = 0; i < 2; ++i)
2228 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002229 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230#endif
2231 /*
2232 * Search in builtin termcap
2233 */
2234 {
2235 termp = find_builtin_term(term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002236 if (termp->bt_string != NULL) // found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
2238 key = TERMCAP2KEY(name[0], name[1]);
Bram Moolenaare7808482018-03-06 13:17:23 +01002239 ++termp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 while (termp->bt_entry != (int)KS_NAME)
2241 {
2242 if ((int)termp->bt_entry == key)
2243 {
2244 add_termcode(name, (char_u *)termp->bt_string,
2245 term_is_8bit(term));
2246 return OK;
2247 }
2248 ++termp;
2249 }
2250 }
2251 }
2252#ifdef HAVE_TGETENT
2253 else
2254 /*
2255 * Search in external termcap
2256 */
2257 {
2258 error_msg = tgetent_error(tbuf, term);
2259 if (error_msg == NULL)
2260 {
2261 string = TGETSTR((char *)name, &tp);
2262 if (string != NULL && *string != NUL)
2263 {
2264 add_termcode(name, string, FALSE);
2265 return OK;
2266 }
2267 }
2268 }
2269 }
2270#endif
2271
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002272 if (SOURCING_NAME == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
2274#ifdef HAVE_TGETENT
2275 if (error_msg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002276 emsg(error_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 else
2278#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002279 semsg(_("E436: No \"%s\" entry in termcap"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281 return FAIL;
2282}
2283
2284 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002285term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286{
2287 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2288}
2289
2290/*
2291 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2292 * Assume that the terminal is using 8-bit controls when the name contains
2293 * "8bit", like in "xterm-8bit".
2294 */
2295 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002296term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297{
2298 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2299}
2300
2301/*
2302 * Translate terminal control chars from 7-bit to 8-bit:
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002303 * <Esc>[ -> CSI <M_C_[>
2304 * <Esc>] -> OSC <M-C-]>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 * <Esc>O -> <M-C-O>
2306 */
2307 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002308term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309{
2310 if (*p == ESC)
2311 {
2312 if (p[1] == '[')
2313 return CSI;
2314 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002315 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 if (p[1] == 'O')
2317 return 0x8f;
2318 }
2319 return 0;
2320}
2321
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002322#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002324term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
2326 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2327}
2328#endif
2329
2330#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2331
2332 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002333tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
2335 static char_u buf[16];
2336 char_u *p;
2337
2338 p = buf + 15;
2339 *p = '\0';
2340 do
2341 {
2342 --p;
2343 *p = (char_u) (i % 10 + '0');
2344 i /= 10;
2345 }
2346 while (i > 0 && p > buf);
2347 return p;
2348}
2349#endif
2350
2351#ifndef HAVE_TGETENT
2352
2353/*
2354 * minimal tgoto() implementation.
2355 * no padding and we only parse for %i %d and %+char
2356 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002357 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002358tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
2360 static char buf[30];
2361 char *p, *s, *e;
2362
2363 if (!cm)
2364 return "OOPS";
2365 e = buf + 29;
2366 for (s = buf; s < e && *cm; cm++)
2367 {
2368 if (*cm != '%')
2369 {
2370 *s++ = *cm;
2371 continue;
2372 }
2373 switch (*++cm)
2374 {
2375 case 'd':
2376 p = (char *)tltoa((unsigned long)y);
2377 y = x;
2378 while (*p)
2379 *s++ = *p++;
2380 break;
2381 case 'i':
2382 x++;
2383 y++;
2384 break;
2385 case '+':
2386 *s++ = (char)(*++cm + y);
2387 y = x;
2388 break;
2389 case '%':
2390 *s++ = *cm;
2391 break;
2392 default:
2393 return "OOPS";
2394 }
2395 }
2396 *s = '\0';
2397 return buf;
2398}
2399
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002400#endif // HAVE_TGETENT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
2402/*
2403 * Set the terminal name and initialize the terminal options.
2404 * If "name" is NULL or empty, get the terminal name from the environment.
2405 * If that fails, use the default terminal name.
2406 */
2407 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002408termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409{
2410 char_u *term;
2411
2412 if (name != NULL && *name == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002413 name = NULL; // empty name is equal to no name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 term = name;
2415
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416#ifndef MSWIN
2417 if (term == NULL)
2418 term = mch_getenv((char_u *)"TERM");
2419#endif
2420 if (term == NULL || *term == NUL)
2421 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002422 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002424 // Set the default terminal name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 set_string_default("term", term);
2426 set_string_default("ttytype", term);
2427
2428 /*
2429 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2430 */
2431 set_termname(T_NAME != NULL ? T_NAME : term);
2432}
2433
2434/*
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002435 * The number of calls to ui_write is reduced by using "out_buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 */
Bram Moolenaara06ecab2016-07-16 14:47:36 +02002437#define OUT_SIZE 2047
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002438
2439// add one to allow mch_write() in os_win32.c to append a NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440static char_u out_buf[OUT_SIZE + 1];
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002441
2442static int out_pos = 0; // number of chars in out_buf
2443
2444// Since the maximum number of SGR parameters shown as a normal value range is
2445// 16, the escape sequence length can be 4 * 16 + lead + tail.
2446#define MAX_ESC_SEQ_LEN 80
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447
2448/*
2449 * out_flush(): flush the output buffer
2450 */
2451 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002452out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453{
2454 int len;
2455
2456 if (out_pos != 0)
2457 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002458 // set out_pos to 0 before ui_write, to avoid recursiveness
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 len = out_pos;
2460 out_pos = 0;
2461 ui_write(out_buf, len);
2462 }
2463}
2464
Bram Moolenaara338adc2018-01-31 20:51:47 +01002465/*
Bram Moolenaard23a8232018-02-10 18:45:26 +01002466 * out_flush_cursor(): flush the output buffer and redraw the cursor.
2467 * Does not flush recursively in the GUI to avoid slow drawing.
Bram Moolenaara338adc2018-01-31 20:51:47 +01002468 */
2469 void
2470out_flush_cursor(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002471 int force UNUSED, // when TRUE, update cursor even when not moved
2472 int clear_selection UNUSED) // clear selection under cursor
Bram Moolenaara338adc2018-01-31 20:51:47 +01002473{
2474 mch_disable_flush();
2475 out_flush();
2476 mch_enable_flush();
2477#ifdef FEAT_GUI
2478 if (gui.in_use)
2479 {
2480 gui_update_cursor(force, clear_selection);
2481 gui_may_flush();
2482 }
2483#endif
2484}
2485
2486
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487/*
2488 * Sometimes a byte out of a multi-byte character is written with out_char().
2489 * To avoid flushing half of the character, call this function first.
2490 */
2491 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002492out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2495 out_flush();
2496}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
2498#ifdef FEAT_GUI
2499/*
2500 * out_trash(): Throw away the contents of the output buffer
2501 */
2502 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002503out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504{
2505 out_pos = 0;
2506}
2507#endif
2508
2509/*
2510 * out_char(c): put a byte into the output buffer.
2511 * Flush it if it becomes full.
2512 * This should not be used for outputting text on the screen (use functions
2513 * like msg_puts() and screen_putchar() for that).
2514 */
2515 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002516out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517{
Bram Moolenaard0573012017-10-28 21:11:06 +02002518#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002519 if (c == '\n') // turn LF into CR-LF (CRMOD doesn't seem to do this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 out_char('\r');
2521#endif
2522
2523 out_buf[out_pos++] = c;
2524
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002525 // For testing we flush each time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (out_pos >= OUT_SIZE || p_wd)
2527 out_flush();
2528}
2529
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002531 * Output "c" like out_char(), but don't flush when p_wd is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 */
2533 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002534out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 out_buf[out_pos++] = c;
2537
2538 if (out_pos >= OUT_SIZE)
2539 out_flush();
2540}
2541
2542/*
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002543 * A never-padding out_str().
2544 * Use this whenever you don't want to run the string through tputs().
2545 * tputs() above is harmless, but tputs() from the termcap library
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 * is likely to strip off leading digits, that it mistakes for padding
2547 * information, and "%i", "%d", etc.
2548 * This should only be used for writing terminal codes, not for outputting
2549 * normal text (use functions like msg_puts() and screen_putchar() for that).
2550 */
2551 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002552out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553{
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002554 // avoid terminal strings being split up
2555 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 out_flush();
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002557
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 while (*s)
2559 out_char_nf(*s++);
2560
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002561 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 if (p_wd)
2563 out_flush();
2564}
2565
2566/*
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002567 * A conditional-flushing out_str, mainly for visualbell.
2568 * Handles a delay internally, because termlib may not respect the delay or do
2569 * it at the wrong time.
2570 * Note: Only for terminal strings.
2571 */
2572 void
2573out_str_cf(char_u *s)
2574{
2575 if (s != NULL && *s)
2576 {
Bram Moolenaarc2226842017-06-29 22:27:24 +02002577#ifdef HAVE_TGETENT
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002578 char_u *p;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002579#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002580
2581#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002582 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002583 if (gui.in_use)
2584 {
2585 out_str_nf(s);
2586 return;
2587 }
2588#endif
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002589 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002590 out_flush();
2591#ifdef HAVE_TGETENT
2592 for (p = s; *s; ++s)
2593 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002594 // flush just before delay command
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002595 if (*s == '$' && *(s + 1) == '<')
2596 {
2597 char_u save_c = *s;
2598 int duration = atoi((char *)s + 2);
2599
2600 *s = NUL;
2601 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2602 *s = save_c;
2603 out_flush();
Bram Moolenaarc2226842017-06-29 22:27:24 +02002604# ifdef ELAPSED_FUNC
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002605 // Only sleep here if we can limit this happening in
2606 // vim_beep().
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002607 p = vim_strchr(s, '>');
2608 if (p == NULL || duration <= 0)
2609 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002610 // can't parse the time, don't sleep here
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002611 p = s;
2612 }
2613 else
2614 {
2615 ++p;
2616 do_sleep(duration);
2617 }
Bram Moolenaarc2226842017-06-29 22:27:24 +02002618# else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002619 // Rely on the terminal library to sleep.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002620 p = s;
Bram Moolenaarc2226842017-06-29 22:27:24 +02002621# endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002622 break;
2623 }
2624 }
2625 tputs((char *)p, 1, TPUTSFUNCAST out_char_nf);
2626#else
2627 while (*s)
2628 out_char_nf(*s++);
2629#endif
2630
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002631 // For testing we write one string at a time.
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02002632 if (p_wd)
2633 out_flush();
2634 }
2635}
2636
2637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 * out_str(s): Put a character string a byte at a time into the output buffer.
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002639 * If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 * This should only be used for writing terminal codes, not for outputting
2641 * normal text (use functions like msg_puts() and screen_putchar() for that).
2642 */
2643 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002644out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645{
2646 if (s != NULL && *s)
2647 {
2648#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002649 // Don't use tputs() when GUI is used, ncurses crashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (gui.in_use)
2651 {
2652 out_str_nf(s);
2653 return;
2654 }
2655#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002656 // avoid terminal strings being split up
Bram Moolenaar5da04ef2019-04-03 21:15:58 +02002657 if (out_pos > OUT_SIZE - MAX_ESC_SEQ_LEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 out_flush();
2659#ifdef HAVE_TGETENT
2660 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2661#else
2662 while (*s)
2663 out_char_nf(*s++);
2664#endif
2665
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002666 // For testing we write one string at a time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (p_wd)
2668 out_flush();
2669 }
2670}
2671
2672/*
2673 * cursor positioning using termcap parser. (jw)
2674 */
2675 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002676term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678 OUT_STR(tgoto((char *)T_CM, col, row));
2679}
2680
2681 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002682term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
2684 OUT_STR(tgoto((char *)T_CRI, 0, i));
2685}
2686
2687 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002688term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
2690 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2691}
2692
2693 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002694term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
2696 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2697}
2698
2699#if defined(HAVE_TGETENT) || defined(PROTO)
2700 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002701term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002703 // Can't handle a negative value here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (x < 0)
2705 x = 0;
2706 if (y < 0)
2707 y = 0;
2708 OUT_STR(tgoto((char *)T_CWP, y, x));
2709}
2710
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002711# if defined(FEAT_TERMRESPONSE) || defined(PROTO)
2712/*
2713 * Return TRUE if we can request the terminal for a response.
2714 */
2715 static int
2716can_get_termresponse()
2717{
2718 return cur_tmode == TMODE_RAW
2719 && termcap_active
Bram Moolenaarafd78262019-05-10 23:10:31 +02002720# ifdef UNIX
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002721 && (is_not_a_term() || (isatty(1) && isatty(read_cmd_fd)))
Bram Moolenaarafd78262019-05-10 23:10:31 +02002722# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002723 && p_ek;
2724}
2725
Bram Moolenaarafd78262019-05-10 23:10:31 +02002726/*
2727 * Set "status" to STATUS_SENT.
2728 */
2729 static void
2730termrequest_sent(termrequest_T *status)
2731{
2732 status->tr_progress = STATUS_SENT;
2733 status->tr_start = time(NULL);
2734}
2735
2736/*
2737 * Return TRUE if any of the requests are in STATUS_SENT.
2738 */
2739 static int
2740termrequest_any_pending()
2741{
2742 int i;
2743 time_t now = time(NULL);
2744
2745 for (i = 0; all_termrequests[i] != NULL; ++i)
2746 {
2747 if (all_termrequests[i]->tr_progress == STATUS_SENT)
2748 {
2749 if (all_termrequests[i]->tr_start > 0 && now > 0
2750 && all_termrequests[i]->tr_start + 2 < now)
2751 // Sent the request more than 2 seconds ago and didn't get a
2752 // response, assume it failed.
2753 all_termrequests[i]->tr_progress = STATUS_FAIL;
2754 else
2755 return TRUE;
2756 }
2757 }
2758 return FALSE;
2759}
2760
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002761static int winpos_x = -1;
2762static int winpos_y = -1;
2763static int did_request_winpos = 0;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002764
Bram Moolenaar6bc93052019-04-06 20:00:19 +02002765# if defined(FEAT_EVAL) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002766/*
2767 * Try getting the Vim window position from the terminal.
2768 * Returns OK or FAIL.
2769 */
2770 int
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01002771term_get_winpos(int *x, int *y, varnumber_T timeout)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002772{
2773 int count = 0;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002774 int prev_winpos_x = winpos_x;
2775 int prev_winpos_y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002776
2777 if (*T_CGP == NUL || !can_get_termresponse())
2778 return FAIL;
2779 winpos_x = -1;
2780 winpos_y = -1;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002781 ++did_request_winpos;
Bram Moolenaarafd78262019-05-10 23:10:31 +02002782 termrequest_sent(&winpos_status);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002783 OUT_STR(T_CGP);
2784 out_flush();
2785
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002786 // Try reading the result for "timeout" msec.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002787 while (count++ <= timeout / 10 && !got_int)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002788 {
2789 (void)vpeekc_nomap();
2790 if (winpos_x >= 0 && winpos_y >= 0)
2791 {
2792 *x = winpos_x;
2793 *y = winpos_y;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002794 return OK;
2795 }
Bram Moolenaareda1da02019-11-17 17:06:33 +01002796 ui_delay(10L, FALSE);
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002797 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002798 // Do not reset "did_request_winpos", if we timed out the response might
2799 // still come later and we must consume it.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002800
2801 winpos_x = prev_winpos_x;
2802 winpos_y = prev_winpos_y;
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002803 if (timeout < 10 && prev_winpos_y >= 0 && prev_winpos_x >= 0)
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002804 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002805 // Polling: return previous values if we have them.
Bram Moolenaar89894aa2018-03-05 22:43:10 +01002806 *x = winpos_x;
2807 *y = winpos_y;
2808 return OK;
2809 }
2810
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002811 return FALSE;
2812}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002813# endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02002814# endif
2815
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 void
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002817term_set_winsize(int height, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02002819 OUT_STR(tgoto((char *)T_CWS, width, height));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820}
2821#endif
2822
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002824term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
2826 char buf[20];
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002827 int i = *s == CSI ? 1 : 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002828 // index in s[] just after <Esc>[ or CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002830 // Special handling of 16 colors, because termcap can't handle it
2831 // Also accept "\e[3%dm" for TERMINFO, it is sometimes used
2832 // Also accept CSI instead of <Esc>[
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 if (n >= 8 && t_colors >= 16
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002834 && ((s[0] == ESC && s[1] == '[')
2835#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2836 || (s[0] == ESC && s[1] == '|')
2837#endif
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02002838 || (s[0] == CSI && (i = 1) == 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 && s[i] != NUL
2840 && (STRCMP(s + i + 1, "%p1%dm") == 0
2841 || STRCMP(s + i + 1, "%dm") == 0)
2842 && (s[i] == '3' || s[i] == '4'))
2843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844#ifdef TERMINFO
Bram Moolenaar827b1652016-05-05 18:14:03 +02002845 char *format = "%s%s%%p1%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846#else
Bram Moolenaar827b1652016-05-05 18:14:03 +02002847 char *format = "%s%s%%dm";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002849 char *lead = i == 2 ? (
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002850#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Bram Moolenaard315cf52018-05-23 20:30:56 +02002851 s[1] == '|' ? IF_EB("\033|", ESC_STR "|") :
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02002852#endif
Bram Moolenaard315cf52018-05-23 20:30:56 +02002853 IF_EB("\033[", ESC_STR "[")) : "\233";
2854 char *tail = s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2855 : (n >= 16 ? "48;5;" : "10");
2856
2857 sprintf(buf, format, lead, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2859 }
2860 else
2861 OUT_STR(tgoto((char *)s, 0, n));
2862}
2863
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002864 void
2865term_fg_color(int n)
2866{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002867 // Use "AF" termcap entry if present, "Sf" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002868 if (*T_CAF)
2869 term_color(T_CAF, n);
2870 else if (*T_CSF)
2871 term_color(T_CSF, n);
2872}
2873
2874 void
2875term_bg_color(int n)
2876{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002877 // Use "AB" termcap entry if present, "Sb" entry otherwise
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002878 if (*T_CAB)
2879 term_color(T_CAB, n);
2880 else if (*T_CSB)
2881 term_color(T_CSB, n);
2882}
2883
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002884/*
2885 * Return "dark" or "light" depending on the kind of terminal.
2886 * This is just guessing! Recognized are:
2887 * "linux" Linux console
2888 * "screen.linux" Linux console with screen
2889 * "cygwin.*" Cygwin shell
2890 * "putty.*" Putty program
2891 * We also check the COLORFGBG environment variable, which is set by
2892 * rxvt and derivatives. This variable contains either two or three
2893 * values separated by semicolons; we want the last value in either
2894 * case. If this value is 0-6 or 8, our background is dark.
2895 */
2896 char_u *
2897term_bg_default(void)
2898{
2899#if defined(MSWIN)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002900 // DOS console is nearly always black
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002901 return (char_u *)"dark";
2902#else
2903 char_u *p;
2904
2905 if (STRCMP(T_NAME, "linux") == 0
2906 || STRCMP(T_NAME, "screen.linux") == 0
2907 || STRNCMP(T_NAME, "cygwin", 6) == 0
2908 || STRNCMP(T_NAME, "putty", 5) == 0
2909 || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
2910 && (p = vim_strrchr(p, ';')) != NULL
2911 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
2912 && p[2] == NUL))
2913 return (char_u *)"dark";
2914 return (char_u *)"light";
2915#endif
2916}
2917
Bram Moolenaar61be73b2016-04-29 22:59:22 +02002918#if defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002919
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002920#define RED(rgb) (((long_u)(rgb) >> 16) & 0xFF)
2921#define GREEN(rgb) (((long_u)(rgb) >> 8) & 0xFF)
2922#define BLUE(rgb) (((long_u)(rgb) ) & 0xFF)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002923
2924 static void
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002925term_rgb_color(char_u *s, guicolor_T rgb)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002926{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002927#define MAX_COLOR_STR_LEN 100
2928 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002929
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002930 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002931 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar06b7b582020-05-30 17:49:25 +02002932#ifdef FEAT_VTP
2933 if (use_wt())
2934 {
2935 out_flush();
2936 buf[1] = '[';
2937 vtp_printf(buf);
2938 }
2939 else
2940#endif
2941 OUT_STR(buf);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002942}
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002943
2944 void
2945term_fg_rgb_color(guicolor_T rgb)
2946{
2947 term_rgb_color(T_8F, rgb);
2948}
2949
2950 void
2951term_bg_rgb_color(guicolor_T rgb)
2952{
2953 term_rgb_color(T_8B, rgb);
2954}
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002955#endif
2956
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002957#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2958 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959/*
2960 * Generic function to set window title, using t_ts and t_fs.
2961 */
2962 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002963term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002965 // t_ts takes one argument: column in status line
2966 OUT_STR(tgoto((char *)T_TS, 0, 0)); // set title start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 out_str_nf(title);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02002968 out_str(T_FS); // set title end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 out_flush();
2970}
Bram Moolenaar40385db2018-08-07 22:31:44 +02002971
2972/*
2973 * Tell the terminal to push (save) the title and/or icon, so that it can be
2974 * popped (restored) later.
2975 */
2976 void
2977term_push_title(int which)
2978{
Bram Moolenaar27821262019-05-08 16:41:09 +02002979 if ((which & SAVE_RESTORE_TITLE) && T_CST != NULL && *T_CST != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002980 {
2981 OUT_STR(T_CST);
2982 out_flush();
2983 }
2984
Bram Moolenaar27821262019-05-08 16:41:09 +02002985 if ((which & SAVE_RESTORE_ICON) && T_SSI != NULL && *T_SSI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002986 {
2987 OUT_STR(T_SSI);
2988 out_flush();
2989 }
2990}
2991
2992/*
2993 * Tell the terminal to pop the title and/or icon.
2994 */
2995 void
2996term_pop_title(int which)
2997{
Bram Moolenaar27821262019-05-08 16:41:09 +02002998 if ((which & SAVE_RESTORE_TITLE) && T_CRT != NULL && *T_CRT != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02002999 {
3000 OUT_STR(T_CRT);
3001 out_flush();
3002 }
3003
Bram Moolenaar27821262019-05-08 16:41:09 +02003004 if ((which & SAVE_RESTORE_ICON) && T_SRI != NULL && *T_SRI != NUL)
Bram Moolenaar40385db2018-08-07 22:31:44 +02003005 {
3006 OUT_STR(T_SRI);
3007 out_flush();
3008 }
3009}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010#endif
3011
3012/*
3013 * Make sure we have a valid set or terminal options.
3014 * Replace all entries that are NULL by empty_option
3015 */
3016 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003017ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018{
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003019 char_u *env_colors;
3020
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003021 check_options(); // make sure no options are NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
3023 /*
3024 * MUST have "cm": cursor motion.
3025 */
3026 if (*T_CM == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003027 emsg(_("E437: terminal capability \"cm\" required"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028
3029 /*
3030 * if "cs" defined, use a scroll region, it's faster.
3031 */
3032 if (*T_CS != NUL)
3033 scroll_region = TRUE;
3034 else
3035 scroll_region = FALSE;
3036
3037 if (pairs)
3038 {
3039 /*
3040 * optional pairs
3041 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003042 // TP goes to normal mode for TI (invert) and TB (bold)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 if (*T_ME == NUL)
3044 T_ME = T_MR = T_MD = T_MB = empty_option;
3045 if (*T_SO == NUL || *T_SE == NUL)
3046 T_SO = T_SE = empty_option;
3047 if (*T_US == NUL || *T_UE == NUL)
3048 T_US = T_UE = empty_option;
3049 if (*T_CZH == NUL || *T_CZR == NUL)
3050 T_CZH = T_CZR = empty_option;
3051
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003052 // T_VE is needed even though T_VI is not defined
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 if (*T_VE == NUL)
3054 T_VI = empty_option;
3055
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003056 // if 'mr' or 'me' is not defined use 'so' and 'se'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 if (*T_ME == NUL)
3058 {
3059 T_ME = T_SE;
3060 T_MR = T_SO;
3061 T_MD = T_SO;
3062 }
3063
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003064 // if 'so' or 'se' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 if (*T_SO == NUL)
3066 {
3067 T_SE = T_ME;
3068 if (*T_MR == NUL)
3069 T_SO = T_MD;
3070 else
3071 T_SO = T_MR;
3072 }
3073
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003074 // if 'ZH' or 'ZR' is not defined use 'mr' and 'me'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 if (*T_CZH == NUL)
3076 {
3077 T_CZR = T_ME;
3078 if (*T_MR == NUL)
3079 T_CZH = T_MD;
3080 else
3081 T_CZH = T_MR;
3082 }
3083
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003084 // "Sb" and "Sf" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 if (*T_CSB == NUL || *T_CSF == NUL)
3086 {
3087 T_CSB = empty_option;
3088 T_CSF = empty_option;
3089 }
3090
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003091 // "AB" and "AF" come in pairs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 if (*T_CAB == NUL || *T_CAF == NUL)
3093 {
3094 T_CAB = empty_option;
3095 T_CAF = empty_option;
3096 }
3097
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003098 // if 'Sb' and 'AB' are not defined, reset "Co"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00003100 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003102 // Set 'weirdinvert' according to value of 't_xs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 p_wiv = (*T_XS != NUL);
3104 }
3105 need_gather = TRUE;
3106
Bram Moolenaar759d8152020-04-26 16:52:49 +02003107 // Set t_colors to the value of $COLORS or t_Co. Ignore $COLORS in the
3108 // GUI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 t_colors = atoi((char *)T_CCO);
Bram Moolenaar759d8152020-04-26 16:52:49 +02003110#ifdef FEAT_GUI
3111 if (!gui.in_use)
3112#endif
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003113 {
Bram Moolenaar759d8152020-04-26 16:52:49 +02003114 env_colors = mch_getenv((char_u *)"COLORS");
3115 if (env_colors != NULL && isdigit(*env_colors))
3116 {
3117 int colors = atoi((char *)env_colors);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003118
Bram Moolenaar759d8152020-04-26 16:52:49 +02003119 if (colors != t_colors)
3120 set_color_count(colors);
3121 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02003122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123}
3124
3125#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
3126 || defined(PROTO)
3127/*
3128 * Represent the given long_u as individual bytes, with the most significant
3129 * byte first, and store them in dst.
3130 */
3131 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003132add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134 int i;
3135 int shift;
3136
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003137 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
3139 shift = 8 * (sizeof(long_u) - i);
3140 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
3141 }
3142}
3143
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144/*
3145 * Interpret the next string of bytes in buf as a long integer, with the most
3146 * significant byte first. Note that it is assumed that buf has been through
3147 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
3148 * Puts result in val, and returns the number of bytes read from buf
3149 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
3150 * were present.
3151 */
3152 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003153get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
3155 int len;
3156 char_u bytes[sizeof(long_u)];
3157 int i;
3158 int shift;
3159
3160 *val = 0;
3161 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
3162 if (len != -1)
3163 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003164 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 {
3166 shift = 8 * (sizeof(long_u) - 1 - i);
3167 *val += (long_u)bytes[i] << shift;
3168 }
3169 }
3170 return len;
3171}
3172#endif
3173
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174/*
3175 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
3176 * that buf has been through inchar(). Returns the actual number of bytes used
3177 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
3178 * available.
3179 */
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02003180 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003181get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
3183 int len = 0;
3184 int i;
3185 char_u c;
3186
3187 for (i = 0; i < num_bytes; i++)
3188 {
3189 if ((c = buf[len++]) == NUL)
3190 return -1;
3191 if (c == K_SPECIAL)
3192 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003193 if (buf[len] == NUL || buf[len + 1] == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 return -1;
3195 if (buf[len++] == (int)KS_ZERO)
3196 c = NUL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003197 // else it should be KS_SPECIAL; when followed by KE_FILLER c is
3198 // K_SPECIAL, or followed by KE_CSI and c must be CSI.
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003199 if (buf[len++] == (int)KE_CSI)
3200 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003202 else if (c == CSI && buf[len] == KS_EXTRA
3203 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003204 // CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3205 // the start of a special key, see add_to_input_buf_csi().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003206 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 bytes[i] = c;
3208 }
3209 return len;
3210}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211
3212/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003213 * Check if the new shell size is valid, correct it if it's too small or way
3214 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 */
3216 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003217check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003219 if (Rows < min_rows()) // need room for one window and command line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003221 limit_screen_size();
3222}
3223
3224/*
3225 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3226 */
3227 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003228limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003229{
3230 if (Columns < MIN_COLUMNS)
3231 Columns = MIN_COLUMNS;
3232 else if (Columns > 10000)
3233 Columns = 10000;
3234 if (Rows > 1000)
3235 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236}
3237
Bram Moolenaar86b68352004-12-27 21:59:20 +00003238/*
3239 * Invoked just before the screen structures are going to be (re)allocated.
3240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003242win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243{
3244 static int old_Rows = 0;
3245 static int old_Columns = 0;
3246
3247 if (old_Rows != Rows || old_Columns != Columns)
3248 ui_new_shellsize();
3249 if (old_Rows != Rows)
3250 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003251 // if 'window' uses the whole screen, keep it using that
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003252 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003253 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 old_Rows = Rows;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003255 shell_new_rows(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 }
3257 if (old_Columns != Columns)
3258 {
3259 old_Columns = Columns;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003260 shell_new_columns(); // update window sizes
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 }
3262}
3263
3264/*
3265 * Call this function when the Vim shell has been resized in any way.
3266 * Will obtain the current size and redraw (also when size didn't change).
3267 */
3268 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003269shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
3271 set_shellsize(0, 0, FALSE);
3272}
3273
3274/*
3275 * Check if the shell size changed. Handle a resize.
3276 * When the size didn't change, nothing happens.
3277 */
3278 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003279shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
3281 int old_Rows = Rows;
3282 int old_Columns = Columns;
3283
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003284 if (!exiting
3285#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003286 // Do not get the size when executing a shell command during
3287 // startup.
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003288 && !gui.starting
3289#endif
3290 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003291 {
3292 (void)ui_get_shellsize();
3293 check_shellsize();
3294 if (old_Rows != Rows || old_Columns != Columns)
3295 shell_resized();
3296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297}
3298
3299/*
3300 * Set size of the Vim shell.
3301 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3302 * window size (this is used for the :win command).
3303 * If 'mustset' is FALSE, we may try to get the real window size and if
3304 * it fails use 'width' and 'height'.
3305 */
3306 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003307set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
3309 static int busy = FALSE;
3310
3311 /*
3312 * Avoid recursiveness, can happen when setting the window size causes
3313 * another window-changed signal.
3314 */
3315 if (busy)
3316 return;
3317
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003318 if (width < 0 || height < 0) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 return;
3320
Bram Moolenaara971b822011-09-14 14:43:25 +02003321 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 {
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003323 // postpone the resizing
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 State = SETWSIZE;
3325 return;
3326 }
3327
Bram Moolenaar847a5d62019-07-12 15:37:13 +02003328 if (updating_screen)
3329 // resizing while in update_screen() may cause a crash
3330 return;
3331
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003332 // curwin->w_buffer can be NULL when we are closing a window and the
3333 // buffer has already been closed and removing a scrollbar causes a resize
3334 // event. Don't resize then, it will happen after entering another buffer.
Bram Moolenaara971b822011-09-14 14:43:25 +02003335 if (curwin->w_buffer == NULL)
3336 return;
3337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 ++busy;
3339
3340#ifdef AMIGA
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003341 out_flush(); // must do this before mch_get_shellsize() for
3342 // some obscure reason
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343#endif
3344
3345 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3346 {
3347 Rows = height;
3348 Columns = width;
3349 check_shellsize();
3350 ui_set_shellsize(mustset);
3351 }
3352 else
3353 check_shellsize();
3354
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003355 // The window layout used to be adjusted here, but it now happens in
3356 // screenalloc() (also invoked from screenclear()). That is because the
3357 // "busy" check above may skip this, but not screenalloc().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
3359 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3360 screenclear();
3361 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003362 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
3364 if (starting != NO_SCREEN)
3365 {
3366#ifdef FEAT_TITLE
3367 maketitle();
3368#endif
3369 changed_line_abv_curs();
3370 invalidate_botline();
3371
3372 /*
3373 * We only redraw when it's needed:
3374 * - While at the more prompt or executing an external command, don't
3375 * redraw, but position the cursor.
3376 * - While editing the command line, only redraw that.
3377 * - in Ex mode, don't redraw anything.
3378 * - Otherwise, redraw right now, and position the cursor.
3379 * Always need to call update_screen() or screenalloc(), to make
3380 * sure Rows/Columns and the size of ScreenLines[] is correct!
3381 */
3382 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3383 || exmode_active)
3384 {
3385 screenalloc(FALSE);
3386 repeat_message();
3387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 else
3389 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003390 if (curwin->w_p_scb)
3391 do_check_scrollbind(TRUE);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003392 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003393 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003394 update_screen(NOT_VALID);
3395 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003396 }
3397 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003398 {
3399 update_topline();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003400 if (pum_visible())
3401 {
3402 redraw_later(NOT_VALID);
Bram Moolenaara5e66212017-09-29 22:42:33 +02003403 ins_compl_show_pum();
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003404 }
Bram Moolenaara5e66212017-09-29 22:42:33 +02003405 update_screen(NOT_VALID);
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003406 if (redrawing())
3407 setcursor();
3408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003410 cursor_on(); // redrawing may have switched it off
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 }
3412 out_flush();
3413 --busy;
3414}
3415
3416/*
3417 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3418 * commands and Ex mode).
3419 */
3420 void
Bram Moolenaarf4e16ae2020-05-17 16:10:11 +02003421settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
3423#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003424 // don't set the term where gvim was started to any mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 if (gui.in_use)
3426 return;
3427#endif
3428
3429 if (full_screen)
3430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 /*
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003432 * When returning after calling a shell cur_tmode is TMODE_UNKNOWN,
3433 * set the terminal to raw mode, even though we think it already is,
3434 * because the shell program may have reset the terminal mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 * When we think the terminal is normal, don't try to set it to
3436 * normal again, because that causes problems (logout!) on some
3437 * machines.
3438 */
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02003439 if (tmode != cur_tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 {
3441#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003442# ifdef FEAT_GUI
3443 if (!gui.in_use && !gui.starting)
3444# endif
3445 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003446 // May need to check for T_CRV response and termcodes, it
3447 // doesn't work in Cooked mode, an external program may get
3448 // them.
3449 if (tmode != TMODE_RAW && termrequest_any_pending())
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003450 (void)vpeekc_nomap();
3451 check_for_codes_from_term();
3452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 if (tmode != TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003455 mch_setmouse(FALSE); // switch mouse off
Bram Moolenaar26e86442020-05-17 14:06:16 +02003456
3457 // Disable bracketed paste and modifyOtherKeys in cooked mode.
3458 // Avoid doing this too often, on some terminals the codes are not
3459 // handled properly.
3460 if (termcap_active && tmode != TMODE_SLEEP
3461 && cur_tmode != TMODE_SLEEP)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003462 {
3463 if (tmode != TMODE_RAW)
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003464 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003465 out_str(T_BD); // disable bracketed paste mode
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003466 out_str(T_CTE); // possibly disables modifyOtherKeys
3467 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003468 else
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003469 {
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003470 out_str(T_BE); // enable bracketed paste mode (should
3471 // be before mch_settmode().
Bram Moolenaar645e3fe2020-05-16 15:05:04 +02003472 out_str(T_CTI); // possibly enables modifyOtherKeys
3473 }
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 out_flush();
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003476 mch_settmode(tmode); // machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 cur_tmode = tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 if (tmode == TMODE_RAW)
Bram Moolenaar958eabe2019-04-21 17:22:33 +02003479 setmouse(); // may switch mouse on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 out_flush();
3481 }
3482#ifdef FEAT_TERMRESPONSE
3483 may_req_termresponse();
3484#endif
3485 }
3486}
3487
3488 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003489starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
3491 if (full_screen && !termcap_active)
3492 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003493 out_str(T_TI); // start termcap mode
3494 out_str(T_CTI); // start "raw" mode
3495 out_str(T_KS); // start "keypad transmit" mode
3496 out_str(T_BE); // enable bracketed paste mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 out_flush();
3498 termcap_active = TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003499 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003501# ifdef FEAT_GUI
3502 if (!gui.in_use && !gui.starting)
3503# endif
3504 {
3505 may_req_termresponse();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003506 // Immediately check for a response. If t_Co changes, we don't
3507 // want to redraw with wrong colors first.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003508 if (crv_status.tr_progress == STATUS_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003509 check_for_codes_from_term();
3510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511#endif
3512 }
3513}
3514
3515 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003516stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517{
3518 screen_stop_highlight();
3519 reset_cterm_colors();
3520 if (termcap_active)
3521 {
3522#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003523# ifdef FEAT_GUI
3524 if (!gui.in_use && !gui.starting)
3525# endif
3526 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003527 // May need to discard T_CRV, T_U7 or T_RBG response.
3528 if (termrequest_any_pending())
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003529 {
3530# ifdef UNIX
Bram Moolenaarafd78262019-05-10 23:10:31 +02003531 // Give the terminal a chance to respond.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003532 mch_delay(100L, FALSE);
3533# endif
3534# ifdef TCIFLUSH
Bram Moolenaarafd78262019-05-10 23:10:31 +02003535 // Discard data received but not read.
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003536 if (exiting)
3537 tcflush(fileno(stdin), TCIFLUSH);
3538# endif
3539 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003540 // Check for termcodes first, otherwise an external program may
3541 // get them.
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003542 check_for_codes_from_term();
3543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003545 out_str(T_BD); // disable bracketed paste mode
3546 out_str(T_KE); // stop "keypad transmit" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 out_flush();
3548 termcap_active = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003549 cursor_on(); // just in case it is still off
3550 out_str(T_CTE); // stop "raw" mode
3551 out_str(T_TE); // stop termcap mode
3552 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 out_flush();
3554 }
3555}
3556
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003557#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558/*
3559 * Request version string (for xterm) when needed.
3560 * Only do this after switching to raw mode, otherwise the result will be
3561 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003562 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003563 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 * Only do this after termcap mode has been started, otherwise the codes for
3565 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003566 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3567 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003568 * On Unix only do it when both output and input are a tty (avoid writing
3569 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 * The result is caught in check_termcode().
3571 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003572 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003573may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003575 if (crv_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003576 && can_get_termresponse()
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003577 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 && *T_CRV != NUL)
3579 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003580 LOG_TR(("Sending CRV request"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 out_str(T_CRV);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003582 termrequest_sent(&crv_status);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003583 // check for the characters now, otherwise they might be eaten by
3584 // get_keystroke()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 out_flush();
3586 (void)vpeekc_nomap();
3587 }
3588}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003589
Bram Moolenaar9584b312013-03-13 19:29:28 +01003590/*
3591 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003592 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003593 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003594 * If the terminal treats \u25bd as single width, the position is (1, 1),
3595 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003596 * This function has the side effect that changes cursor position, so
3597 * it must be called immediately after entering termcap mode.
3598 */
3599 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003600may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003601{
Bram Moolenaarafd78262019-05-10 23:10:31 +02003602 if (u7_status.tr_progress == STATUS_GET
Bram Moolenaarba6ec182017-04-04 22:41:10 +02003603 && can_get_termresponse()
3604 && starting == 0
Bram Moolenaar9584b312013-03-13 19:29:28 +01003605 && *T_U7 != NUL
3606 && !option_was_set((char_u *)"ambiwidth"))
3607 {
Bram Moolenaarafd78262019-05-10 23:10:31 +02003608 char_u buf[16];
Bram Moolenaar9584b312013-03-13 19:29:28 +01003609
Bram Moolenaarafd78262019-05-10 23:10:31 +02003610 LOG_TR(("Sending U7 request"));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003611 // Do this in the second row. In the first row the returned sequence
3612 // may be CSI 1;2R, which is the same as <S-F3>.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003613 term_windgoto(1, 0);
3614 buf[mb_char2bytes(0x25bd, buf)] = 0;
3615 out_str(buf);
3616 out_str(T_U7);
3617 termrequest_sent(&u7_status);
3618 out_flush();
Bram Moolenaar976787d2017-06-04 15:45:50 +02003619
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003620 // This overwrites a few characters on the screen, a redraw is needed
3621 // after this. Clear them out for now.
Bram Moolenaar06029a82019-07-24 14:25:26 +02003622 screen_stop_highlight();
Bram Moolenaarafd78262019-05-10 23:10:31 +02003623 term_windgoto(1, 0);
3624 out_str((char_u *)" ");
3625 term_windgoto(0, 0);
Bram Moolenaar976787d2017-06-04 15:45:50 +02003626
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003627 // Need to reset the known cursor position.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003628 screen_start();
Bram Moolenaar05684312017-12-09 15:11:24 +01003629
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003630 // check for the characters now, otherwise they might be eaten by
3631 // get_keystroke()
Bram Moolenaarafd78262019-05-10 23:10:31 +02003632 out_flush();
3633 (void)vpeekc_nomap();
Bram Moolenaar9584b312013-03-13 19:29:28 +01003634 }
3635}
Bram Moolenaar2951b772013-07-03 12:45:31 +02003636
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003637/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003638 * Similar to requesting the version string: Request the terminal background
3639 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003640 */
3641 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003642may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003643{
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003644 if (can_get_termresponse() && starting == 0)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003645 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003646 int didit = FALSE;
3647
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003648# ifdef FEAT_TERMINAL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003649 // Only request foreground if t_RF is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003650 if (rfg_status.tr_progress == STATUS_GET && *T_RFG != NUL)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003651 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003652 LOG_TR(("Sending FG request"));
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003653 out_str(T_RFG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003654 termrequest_sent(&rfg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003655 didit = TRUE;
3656 }
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02003657# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003658
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003659 // Only request background if t_RB is set.
Bram Moolenaarafd78262019-05-10 23:10:31 +02003660 if (rbg_status.tr_progress == STATUS_GET && *T_RBG != NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003661 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02003662 LOG_TR(("Sending BG request"));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003663 out_str(T_RBG);
Bram Moolenaarafd78262019-05-10 23:10:31 +02003664 termrequest_sent(&rbg_status);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003665 didit = TRUE;
3666 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003667
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003668 if (didit)
3669 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003670 // check for the characters now, otherwise they might be eaten by
3671 // get_keystroke()
Bram Moolenaar833e0e32017-08-26 15:16:03 +02003672 out_flush();
3673 (void)vpeekc_nomap();
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003674 }
3675 }
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003676}
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003677
Bram Moolenaar2951b772013-07-03 12:45:31 +02003678# ifdef DEBUG_TERMRESPONSE
3679 static void
Bram Moolenaarb255b902018-04-24 21:40:10 +02003680log_tr(const char *fmt, ...)
Bram Moolenaar2951b772013-07-03 12:45:31 +02003681{
3682 static FILE *fd_tr = NULL;
3683 static proftime_T start;
3684 proftime_T now;
Bram Moolenaarb255b902018-04-24 21:40:10 +02003685 va_list ap;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003686
3687 if (fd_tr == NULL)
3688 {
3689 fd_tr = fopen("termresponse.log", "w");
3690 profile_start(&start);
3691 }
3692 now = start;
3693 profile_end(&now);
Bram Moolenaarb255b902018-04-24 21:40:10 +02003694 fprintf(fd_tr, "%s: %s ", profile_msg(&now),
3695 must_redraw == NOT_VALID ? "NV"
3696 : must_redraw == CLEAR ? "CL" : " ");
3697 va_start(ap, fmt);
3698 vfprintf(fd_tr, fmt, ap);
3699 va_end(ap);
3700 fputc('\n', fd_tr);
3701 fflush(fd_tr);
Bram Moolenaar2951b772013-07-03 12:45:31 +02003702}
3703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704#endif
3705
3706/*
3707 * Return TRUE when saving and restoring the screen.
3708 */
3709 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003710swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 return (full_screen && *T_TI != NUL);
3713}
3714
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715/*
3716 * By outputting the 'cursor very visible' termcap code, for some windowed
3717 * terminals this makes the screen scrolled to the correct position.
3718 * Used when starting Vim or returning from a shell.
3719 */
3720 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003721scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003723 if (*T_VS != NUL && *T_CVS != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
3725 out_str(T_VS);
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003726 out_str(T_CVS);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003727 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729}
3730
3731static int cursor_is_off = FALSE;
3732
3733/*
Bram Moolenaar2e310482018-08-21 13:09:10 +02003734 * Enable the cursor without checking if it's already enabled.
3735 */
3736 void
3737cursor_on_force(void)
3738{
3739 out_str(T_VE);
3740 cursor_is_off = FALSE;
3741}
3742
3743/*
3744 * Enable the cursor if it's currently off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 */
3746 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003747cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
3749 if (cursor_is_off)
Bram Moolenaar2e310482018-08-21 13:09:10 +02003750 cursor_on_force();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751}
3752
3753/*
3754 * Disable the cursor.
3755 */
3756 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003757cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003759 if (full_screen && !cursor_is_off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003761 out_str(T_VI); // disable cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 cursor_is_off = TRUE;
3763 }
3764}
3765
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003766#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003768 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003769 */
3770 void
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003771term_cursor_mode(int forced)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003772{
Bram Moolenaarc9770922017-08-12 20:11:53 +02003773 static int showing_mode = -1;
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003774 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003775
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003776 // Only do something when redrawing the screen and we can restore the
3777 // mode.
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003778 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003779 {
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003780# ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003781 if (forced && initial_cursor_shape > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003782 // Restore to initial values.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003783 term_cursor_shape(initial_cursor_shape, initial_cursor_blink);
Bram Moolenaar37b9b812017-08-19 23:23:43 +02003784# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003785 return;
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02003786 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003787
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003788 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003789 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003790 if (forced || showing_mode != REPLACE)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003791 {
3792 if (*T_CSR != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003793 p = T_CSR; // Replace mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003794 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003795 p = T_CSI; // fall back to Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003796 if (*p != NUL)
3797 {
3798 out_str(p);
3799 showing_mode = REPLACE;
3800 }
3801 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003802 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003803 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003804 {
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003805 if ((forced || showing_mode != INSERT) && *T_CSI != NUL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003806 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003807 out_str(T_CSI); // Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003808 showing_mode = INSERT;
3809 }
3810 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003811 else if (forced || showing_mode != NORMAL)
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003812 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003813 out_str(T_CEI); // non-Insert mode cursor
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003814 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003815 }
3816}
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003817
3818# if defined(FEAT_TERMINAL) || defined(PROTO)
3819 void
3820term_cursor_color(char_u *color)
3821{
3822 if (*T_CSC != NUL)
3823 {
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003824 out_str(T_CSC); // set cursor color start
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003825 out_str_nf(color);
Bram Moolenaarec6f7352019-10-24 17:49:27 +02003826 out_str(T_CEC); // set cursor color end
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003827 out_flush();
3828 }
3829}
Bram Moolenaarfc8bec02017-08-19 19:57:34 +02003830# endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003831
Bram Moolenaar4db25542017-08-28 22:43:05 +02003832 int
3833blink_state_is_inverted()
3834{
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003835#ifdef FEAT_TERMRESPONSE
Bram Moolenaar66761db2019-06-05 22:07:51 +02003836 return rbm_status.tr_progress == STATUS_GOT
3837 && rcs_status.tr_progress == STATUS_GOT
Bram Moolenaar4db25542017-08-28 22:43:05 +02003838 && initial_cursor_blink != initial_cursor_shape_blink;
Bram Moolenaar3c37a8e2017-08-28 23:00:55 +02003839#else
3840 return FALSE;
3841#endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02003842}
3843
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003844/*
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003845 * "shape": 1 = block, 2 = underline, 3 = vertical bar
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003846 */
3847 void
3848term_cursor_shape(int shape, int blink)
3849{
3850 if (*T_CSH != NUL)
3851 {
3852 OUT_STR(tgoto((char *)T_CSH, 0, shape * 2 - blink));
3853 out_flush();
3854 }
Bram Moolenaar4db25542017-08-28 22:43:05 +02003855 else
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003856 {
Bram Moolenaar4db25542017-08-28 22:43:05 +02003857 int do_blink = blink;
3858
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003859 // t_SH is empty: try setting just the blink state.
3860 // The blink flags are XORed together, if the initial blinking from
3861 // style and shape differs, we need to invert the flag here.
Bram Moolenaar4db25542017-08-28 22:43:05 +02003862 if (blink_state_is_inverted())
3863 do_blink = !blink;
3864
3865 if (do_blink && *T_VS != NUL)
3866 {
3867 out_str(T_VS);
3868 out_flush();
3869 }
3870 else if (!do_blink && *T_CVS != NUL)
3871 {
3872 out_str(T_CVS);
3873 out_flush();
3874 }
Bram Moolenaarce1c3272017-08-20 15:05:15 +02003875 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02003876}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003877#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003878
3879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 * Set scrolling region for window 'wp'.
3881 * The region starts 'off' lines from the start of the window.
3882 * Also set the vertical scroll region for a vertically split window. Always
3883 * the full width of the window, excluding the vertical separator.
3884 */
3885 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003886scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887{
3888 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3889 W_WINROW(wp) + off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 if (*T_CSV != NUL && wp->w_width != Columns)
Bram Moolenaar53f81742017-09-22 14:35:51 +02003891 OUT_STR(tgoto((char *)T_CSV, wp->w_wincol + wp->w_width - 1,
3892 wp->w_wincol));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003893 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894}
3895
3896/*
3897 * Reset scrolling region to the whole screen.
3898 */
3899 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003900scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901{
3902 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 if (*T_CSV != NUL)
3904 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003905 screen_start(); // don't know where cursor is now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906}
3907
3908
3909/*
3910 * List of terminal codes that are currently recognized.
3911 */
3912
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003913static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003915 char_u name[2]; // termcap name of entry
3916 char_u *code; // terminal code (in allocated memory)
3917 int len; // STRLEN(code)
3918 int modlen; // length of part before ";*~".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919} *termcodes = NULL;
3920
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003921static int tc_max_len = 0; // number of entries that termcodes[] can hold
3922static int tc_len = 0; // current number of entries in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003924static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003925
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003927clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928{
3929 while (tc_len > 0)
3930 vim_free(termcodes[--tc_len].code);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003931 VIM_CLEAR(termcodes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 tc_max_len = 0;
3933
3934#ifdef HAVE_TGETENT
3935 BC = (char *)empty_option;
3936 UP = (char *)empty_option;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003937 PC = NUL; // set pad character to NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 ospeed = 0;
3939#endif
3940
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003941 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942}
3943
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003944#define ATC_FROM_TERM 55
3945
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946/*
3947 * Add a new entry to the list of terminal codes.
3948 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003949 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3950 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 */
3952 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003953add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
3955 struct termcode *new_tc;
3956 int i, j;
3957 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003958 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959
3960 if (string == NULL || *string == NUL)
3961 {
3962 del_termcode(name);
3963 return;
3964 }
3965
Bram Moolenaar4f974752019-02-17 17:44:42 +01003966#if defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar45500912014-07-09 20:51:07 +02003967 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3968#else
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003969# ifdef VIMDLL
3970 if (!gui.in_use)
3971 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3972 else
3973# endif
3974 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 if (s == NULL)
3977 return;
3978
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003979 // Change leading <Esc>[ to CSI, change <Esc>O to <M-O>.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003980 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003982 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 s[0] = term_7to8bit(string);
3984 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003985
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003986#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
3987# ifdef VIMDLL
3988 if (!gui.in_use)
3989# endif
Bram Moolenaar45500912014-07-09 20:51:07 +02003990 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003991 if (s[0] == K_NUL)
3992 {
3993 STRMOVE(s + 1, s);
3994 s[1] = 3;
3995 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003996 }
3997#endif
3998
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003999 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004001 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
4003 /*
4004 * need to make space for more entries
4005 */
4006 if (tc_len == tc_max_len)
4007 {
4008 tc_max_len += 20;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004009 new_tc = ALLOC_MULT(struct termcode, tc_max_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (new_tc == NULL)
4011 {
4012 tc_max_len -= 20;
4013 return;
4014 }
4015 for (i = 0; i < tc_len; ++i)
4016 new_tc[i] = termcodes[i];
4017 vim_free(termcodes);
4018 termcodes = new_tc;
4019 }
4020
4021 /*
4022 * Look for existing entry with the same name, it is replaced.
4023 * Look for an existing entry that is alphabetical higher, the new entry
4024 * is inserted in front of it.
4025 */
4026 for (i = 0; i < tc_len; ++i)
4027 {
4028 if (termcodes[i].name[0] < name[0])
4029 continue;
4030 if (termcodes[i].name[0] == name[0])
4031 {
4032 if (termcodes[i].name[1] < name[1])
4033 continue;
4034 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004035 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 */
4037 if (termcodes[i].name[1] == name[1])
4038 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004039 if (flags == ATC_FROM_TERM && (j = termcode_star(
4040 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004041 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004042 // Don't replace ESC[123;*X or ESC O*X with another when
4043 // invoked from got_code_from_term().
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004044 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004045 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004046 && s[len - 1]
4047 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004048 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004049 // They are equal but for the ";*": don't add it.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004050 vim_free(s);
4051 return;
4052 }
4053 }
4054 else
4055 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004056 // Replace old code.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004057 vim_free(termcodes[i].code);
4058 --tc_len;
4059 break;
4060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062 }
4063 /*
4064 * Found alphabetical larger entry, move rest to insert new entry
4065 */
4066 for (j = tc_len; j > i; --j)
4067 termcodes[j] = termcodes[j - 1];
4068 break;
4069 }
4070
4071 termcodes[i].name[0] = name[0];
4072 termcodes[i].name[1] = name[1];
4073 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004074 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004075
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004076 // For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
4077 // accept modifiers.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004078 termcodes[i].modlen = 0;
4079 j = termcode_star(s, len);
4080 if (j > 0)
4081 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 ++tc_len;
4083}
4084
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004085/*
Bram Moolenaara529ce02017-06-22 22:37:57 +02004086 * Check termcode "code[len]" for ending in ;*X or *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004087 * The "X" can be any character.
Bram Moolenaara529ce02017-06-22 22:37:57 +02004088 * Return 0 if not found, 2 for ;*X and 1 for *X.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004089 */
4090 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004091termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004092{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004093 // Shortest is <M-O>*X. With ; shortest is <CSI>1;*X
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004094 if (len >= 3 && code[len - 2] == '*')
4095 {
4096 if (len >= 5 && code[len - 3] == ';')
4097 return 2;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004098 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004099 return 1;
4100 }
4101 return 0;
4102}
4103
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004105find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
4107 int i;
4108
4109 for (i = 0; i < tc_len; ++i)
4110 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4111 return termcodes[i].code;
4112 return NULL;
4113}
4114
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004116get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117{
4118 if (i >= tc_len)
4119 return NULL;
4120 return &termcodes[i].name[0];
4121}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004123/*
4124 * Returns the length of the terminal code at index 'idx'.
4125 */
4126 int
4127get_termcode_len(int idx)
4128{
4129 return termcodes[idx].len;
4130}
4131
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004132 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004133del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
4135 int i;
4136
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004137 if (termcodes == NULL) // nothing there yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 return;
4139
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004140 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
4142 for (i = 0; i < tc_len; ++i)
4143 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
4144 {
4145 del_termcode_idx(i);
4146 return;
4147 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004148 // not found. Give error message?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149}
4150
4151 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004152del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153{
4154 int i;
4155
4156 vim_free(termcodes[idx].code);
4157 --tc_len;
4158 for (i = idx; i < tc_len; ++i)
4159 termcodes[i] = termcodes[i + 1];
4160}
4161
4162#ifdef FEAT_TERMRESPONSE
4163/*
4164 * Called when detected that the terminal sends 8-bit codes.
4165 * Convert all 7-bit codes to their 8-bit equivalent.
4166 */
4167 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004168switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
4170 int i;
4171 int c;
4172
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004173 // Only need to do something when not already using 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 if (!term_is_8bit(T_NAME))
4175 {
4176 for (i = 0; i < tc_len; ++i)
4177 {
4178 c = term_7to8bit(termcodes[i].code);
4179 if (c != 0)
4180 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00004181 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 termcodes[i].code[0] = c;
4183 }
4184 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004185 need_gather = TRUE; // need to fill termleader[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187 detected_8bit = TRUE;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004188 LOG_TR(("Switching to 8 bit"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189}
4190#endif
4191
4192#ifdef CHECK_DOUBLE_CLICK
4193static linenr_T orig_topline = 0;
4194# ifdef FEAT_DIFF
4195static int orig_topfill = 0;
4196# endif
4197#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004198#if defined(CHECK_DOUBLE_CLICK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199/*
4200 * Checking for double clicks ourselves.
4201 * "orig_topline" is used to avoid detecting a double-click when the window
4202 * contents scrolled (e.g., when 'scrolloff' is non-zero).
4203 */
4204/*
4205 * Set orig_topline. Used when jumping to another window, so that a double
4206 * click still works.
4207 */
4208 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004209set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
4211 orig_topline = wp->w_topline;
4212# ifdef FEAT_DIFF
4213 orig_topfill = wp->w_topfill;
4214# endif
4215}
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02004216
4217/*
4218 * Returns TRUE if the top line and top fill of window 'wp' matches the saved
4219 * topline and topfill.
4220 */
4221 int
4222is_mouse_topline(win_T *wp)
4223{
4224 return orig_topline == wp->w_topline
4225#ifdef FEAT_DIFF
4226 && orig_topfill == wp->w_topfill
4227#endif
4228 ;
4229}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230#endif
4231
4232/*
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004233 * Put "string[new_slen]" in typebuf, or in "buf[bufsize]" if "buf" is not NULL.
4234 * Remove "slen" bytes.
4235 * Returns FAIL for error.
4236 */
4237 static int
4238put_string_in_typebuf(
4239 int offset,
4240 int slen,
4241 char_u *string,
4242 int new_slen,
4243 char_u *buf,
4244 int bufsize,
4245 int *buflen)
4246{
4247 int extra = new_slen - slen;
4248
4249 string[new_slen] = NUL;
4250 if (buf == NULL)
4251 {
4252 if (extra < 0)
4253 // remove matched chars, taking care of noremap
4254 del_typebuf(-extra, offset);
4255 else if (extra > 0)
4256 // insert the extra space we need
4257 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
4258
4259 // Careful: del_typebuf() and ins_typebuf() may have reallocated
4260 // typebuf.tb_buf[]!
4261 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4262 (size_t)new_slen);
4263 }
4264 else
4265 {
4266 if (extra < 0)
4267 // remove matched characters
4268 mch_memmove(buf + offset, buf + offset - extra,
4269 (size_t)(*buflen + offset + extra));
4270 else if (extra > 0)
4271 {
4272 // Insert the extra space we need. If there is insufficient
4273 // space return -1.
4274 if (*buflen + extra + new_slen >= bufsize)
4275 return FAIL;
4276 mch_memmove(buf + offset + extra, buf + offset,
4277 (size_t)(*buflen - offset));
4278 }
4279 mch_memmove(buf + offset, string, (size_t)new_slen);
4280 *buflen = *buflen + extra + new_slen;
4281 }
4282 return OK;
4283}
4284
4285/*
4286 * Decode a modifier number as xterm provides it into MOD_MASK bits.
4287 */
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01004288 int
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004289decode_modifiers(int n)
4290{
4291 int code = n - 1;
4292 int modifiers = 0;
4293
4294 if (code & 1)
4295 modifiers |= MOD_MASK_SHIFT;
4296 if (code & 2)
4297 modifiers |= MOD_MASK_ALT;
4298 if (code & 4)
4299 modifiers |= MOD_MASK_CTRL;
4300 if (code & 8)
4301 modifiers |= MOD_MASK_META;
4302 return modifiers;
4303}
4304
4305 static int
4306modifiers2keycode(int modifiers, int *key, char_u *string)
4307{
4308 int new_slen = 0;
4309
4310 if (modifiers != 0)
4311 {
4312 // Some keys have the modifier included. Need to handle that here to
4313 // make mappings work.
4314 *key = simplify_key(*key, &modifiers);
4315 if (modifiers != 0)
4316 {
4317 string[new_slen++] = K_SPECIAL;
4318 string[new_slen++] = (int)KS_MODIFIER;
4319 string[new_slen++] = modifiers;
4320 }
4321 }
4322 return new_slen;
4323}
4324
4325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 * Check if typebuf.tb_buf[] contains a terminal key code.
4327 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4328 * + max_offset].
4329 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004330 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 * With a match, the match is removed, the replacement code is inserted in
4332 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4333 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004334 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4335 * "buflen" is then the length of the string in buf[] and is updated for
4336 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 */
4338 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004339check_termcode(
4340 int max_offset,
4341 char_u *buf,
4342 int bufsize,
4343 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344{
4345 char_u *tp;
4346 char_u *p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004347 int slen = 0; // init for GCC
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004348 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004350 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 int offset;
4352 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004353 int modifiers;
Bram Moolenaar090209b2017-06-23 22:45:33 +02004354 char_u *modifiers_start = NULL;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004355 int key;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004356 int new_slen; // Length of what will replace the termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 char_u string[MAX_KEY_CODE_LEN + 1];
4358 int i, j;
4359 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 int cpo_koffset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
4362 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4363
4364 /*
4365 * Speed up the checks for terminal codes by gathering all first bytes
4366 * used in termleader[]. Often this is just a single <Esc>.
4367 */
4368 if (need_gather)
4369 gather_termleader();
4370
4371 /*
4372 * Check at several positions in typebuf.tb_buf[], to catch something like
4373 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4374 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004375 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 * This is used often, KEEP IT FAST!
4377 */
4378 for (offset = 0; offset < max_offset; ++offset)
4379 {
4380 if (buf == NULL)
4381 {
4382 if (offset >= typebuf.tb_len)
4383 break;
4384 tp = typebuf.tb_buf + typebuf.tb_off + offset;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004385 len = typebuf.tb_len - offset; // length of the input
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 }
4387 else
4388 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004389 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 break;
4391 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004392 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 }
4394
4395 /*
4396 * Don't check characters after K_SPECIAL, those are already
4397 * translated terminal chars (avoid translating ~@^Hx).
4398 */
4399 if (*tp == K_SPECIAL)
4400 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004401 offset += 2; // there are always 2 extra characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 continue;
4403 }
4404
4405 /*
4406 * Skip this position if the character does not appear as the first
4407 * character in term_strings. This speeds up a lot, since most
4408 * termcodes start with the same character (ESC or CSI).
4409 */
4410 i = *tp;
4411 for (p = termleader; *p && *p != i; ++p)
4412 ;
4413 if (*p == NUL)
4414 continue;
4415
4416 /*
4417 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4418 * are in Insert mode.
4419 */
4420 if (*tp == ESC && !p_ek && (State & INSERT))
4421 continue;
4422
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004423 key_name[0] = NUL; // no key name found yet
4424 key_name[1] = NUL; // no key name found yet
4425 modifiers = 0; // no modifiers yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427#ifdef FEAT_GUI
4428 if (gui.in_use)
4429 {
4430 /*
4431 * GUI special key codes are all of the form [CSI xx].
4432 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004433 if (*tp == CSI) // Special key from GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 {
4435 if (len < 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004436 return -1; // Shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 slen = 3;
4438 key_name[0] = tp[1];
4439 key_name[1] = tp[2];
4440 }
4441 }
4442 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004443#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
4445 for (idx = 0; idx < tc_len; ++idx)
4446 {
4447 /*
4448 * Ignore the entry if we are not at the start of
4449 * typebuf.tb_buf[]
4450 * and there are not enough characters to make a match.
4451 * But only when the 'K' flag is in 'cpoptions'.
4452 */
4453 slen = termcodes[idx].len;
Bram Moolenaara529ce02017-06-22 22:37:57 +02004454 modifiers_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 if (cpo_koffset && offset && len < slen)
4456 continue;
4457 if (STRNCMP(termcodes[idx].code, tp,
4458 (size_t)(slen > len ? len : slen)) == 0)
4459 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004460 if (len < slen) // got a partial sequence
4461 return -1; // need to get more chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
4463 /*
4464 * When found a keypad key, check if there is another key
4465 * that matches and use that one. This makes <Home> to be
4466 * found instead of <kHome> when they produce the same
4467 * key code.
4468 */
4469 if (termcodes[idx].name[0] == 'K'
4470 && VIM_ISDIGIT(termcodes[idx].name[1]))
4471 {
4472 for (j = idx + 1; j < tc_len; ++j)
4473 if (termcodes[j].len == slen &&
4474 STRNCMP(termcodes[idx].code,
4475 termcodes[j].code, slen) == 0)
4476 {
4477 idx = j;
4478 break;
4479 }
4480 }
4481
4482 key_name[0] = termcodes[idx].name[0];
4483 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 break;
4485 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004486
4487 /*
4488 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004489 * <Esc>[123;*X (modslen == slen - 3)
4490 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4491 * When there is a modifier the * matches a number.
4492 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004493 */
4494 if (termcodes[idx].modlen > 0)
4495 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004496 modslen = termcodes[idx].modlen;
4497 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004498 continue;
4499 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004500 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004501 {
4502 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004503
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004504 if (len <= modslen) // got a partial sequence
4505 return -1; // need to get more chars
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004506
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004507 if (tp[modslen] == termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004508 slen = modslen + 1; // no modifiers
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004509 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004510 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004511 else
4512 {
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004513 // Skip over the digits, the final char must
4514 // follow. URXVT can use a negative value, thus
4515 // also accept '-'.
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02004516 for (j = slen - 2; j < len && (isdigit(tp[j])
Bram Moolenaarbb7e1b42019-05-02 20:24:12 +02004517 || tp[j] == '-' || tp[j] == ';'); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004518 ;
4519 ++j;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004520 if (len < j) // got a partial sequence
4521 return -1; // need to get more chars
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004522 if (tp[j - 1] != termcodes[idx].code[slen - 1])
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004523 continue; // no match
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004524
Bram Moolenaara529ce02017-06-22 22:37:57 +02004525 modifiers_start = tp + slen - 2;
4526
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004527 // Match! Convert modifier bits.
4528 n = atoi((char *)modifiers_start);
4529 modifiers |= decode_modifiers(n);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004530
4531 slen = j;
4532 }
4533 key_name[0] = termcodes[idx].name[0];
4534 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004535 break;
4536 }
4537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 }
4539 }
4540
4541#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004542 if (key_name[0] == NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004543 // Mouse codes of DEC and pterm start with <ESC>[. When
4544 // detecting the start of these mouse codes they might as well be
4545 // another key code or terminal response.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004546# ifdef FEAT_MOUSE_DEC
4547 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004548# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004549# ifdef FEAT_MOUSE_PTERM
4550 || key_name[0] == KS_PTERM_MOUSE
4551# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004552 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004554 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
4555
4556 /*
4557 * Check for responses from the terminal starting with {lead}:
4558 * "<Esc>[" or CSI followed by [0-9>?]
Bram Moolenaar9584b312013-03-13 19:29:28 +01004559 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004560 * - Xterm version string: {lead}>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01004561 * Also eat other possible responses to t_RV, rxvt returns
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004562 * "{lead}?1;2c".
Bram Moolenaar9584b312013-03-13 19:29:28 +01004563 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004564 * - Cursor position report: {lead}{row};{col}R
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004565 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004566 * ambiguous-width character state.
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004567 *
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004568 * - window position reply: {lead}3;{x};{y}t
4569 *
4570 * - key with modifiers when modifyOtherKeys is enabled:
4571 * {lead}27;{modifier};{key}~
4572 * {lead}{key};{modifier}u
Bram Moolenaar9584b312013-03-13 19:29:28 +01004573 */
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004574 if (((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004575 || (tp[0] == CSI && len >= 2))
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004576 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004578 int first = -1; // optional char right after {lead}
4579 int trail; // char that ends CSI sequence
4580 int arg[3] = {-1, -1, -1}; // argument numbers
4581 int argc; // number of arguments
4582 char_u *ap = argp;
4583 int csi_len;
Bram Moolenaar8f14bb52017-07-25 22:06:43 +02004584
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004585 // Check for non-digit after CSI.
4586 if (!VIM_ISDIGIT(*ap))
4587 first = *ap++;
4588
4589 // Find up to three argument numbers.
4590 for (argc = 0; argc < 3; )
4591 {
4592 if (ap >= tp + len)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004593 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004594not_enough:
4595 LOG_TR(("Not enough characters for CSI sequence"));
4596 return -1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004597 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004598 if (*ap == ';')
4599 arg[argc++] = -1; // omitted number
4600 else if (VIM_ISDIGIT(*ap))
4601 {
4602 arg[argc] = 0;
4603 for (;;)
4604 {
4605 if (ap >= tp + len)
4606 goto not_enough;
4607 if (!VIM_ISDIGIT(*ap))
4608 break;
4609 arg[argc] = arg[argc] * 10 + (*ap - '0');
4610 ++ap;
4611 }
4612 ++argc;
4613 }
4614 if (*ap == ';')
4615 ++ap;
4616 else
4617 break;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004618 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004619 // mrxvt has been reported to have "+" in the version. Assume
4620 // the escape sequence ends with a letter or one of "{|}~".
4621 while (ap < tp + len
4622 && !(*ap >= '{' && *ap <= '~')
4623 && !ASCII_ISALPHA(*ap))
4624 ++ap;
4625 if (ap >= tp + len)
4626 goto not_enough;
4627 trail = *ap;
4628 csi_len = (int)(ap - tp) + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004629
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004630 // Cursor position report: Eat it when there are 2 arguments
4631 // and it ends in 'R'. Also when u7_status is not "sent", it
4632 // may be from a previous Vim that just exited. But not for
4633 // <S-F3>, it sends something similar, check for row and column
4634 // to make sense.
4635 if (first == -1 && argc == 2 && trail == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004636 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004637 if (arg[0] == 2 && arg[1] >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004638 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004639 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004640
Bram Moolenaarb255b902018-04-24 21:40:10 +02004641 LOG_TR(("Received U7 status: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004642 u7_status.tr_progress = STATUS_GOT;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004643 did_cursorhold = TRUE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004644 if (arg[1] == 2)
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004645 aw = "single";
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004646 else if (arg[1] == 3)
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004647 aw = "double";
4648 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4649 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004650 // Setting the option causes a screen redraw. Do
4651 // that right away if possible, keeping any
4652 // messages.
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004653 set_option_value((char_u *)"ambw", 0L,
4654 (char_u *)aw, 0);
4655# ifdef DEBUG_TERMRESPONSE
4656 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004657 int r = redraw_asap(CLEAR);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004658
Bram Moolenaarb255b902018-04-24 21:40:10 +02004659 log_tr("set 'ambiwidth', redraw_asap(): %d", r);
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004660 }
4661# else
4662 redraw_asap(CLEAR);
4663# endif
4664 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004665 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004666 key_name[0] = (int)KS_EXTRA;
4667 key_name[1] = (int)KE_IGNORE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004668 slen = csi_len;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004669# ifdef FEAT_EVAL
4670 set_vim_var_string(VV_TERMU7RESP, tp, slen);
4671# endif
Bram Moolenaar9584b312013-03-13 19:29:28 +01004672 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004673
4674 // Version string: Eat it when there is at least one digit and
4675 // it ends in 'c'
4676 else if (*T_CRV != NUL && ap > argp + 1 && trail == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004678 int version = arg[1];
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004679
Bram Moolenaarb255b902018-04-24 21:40:10 +02004680 LOG_TR(("Received CRV response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004681 crv_status.tr_progress = STATUS_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004682 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004684 // If this code starts with CSI, you can bet that the
4685 // terminal uses 8-bit codes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 if (tp[0] == CSI)
4687 switch_to_8bit();
4688
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004689 // Screen sends 40500.
Bram Moolenaar89577b32019-10-18 21:26:05 +02004690 // rxvt sends its version number: "20703" is 2.7.3.
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004691 // Ignore it for when the user has set 'term' to xterm,
4692 // even though it's an rxvt.
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004693 if (version > 20000)
4694 version = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004696 if (first == '>' && argc == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004698 int need_flush = FALSE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004699 int is_iterm2 = FALSE;
Bram Moolenaar20091c12018-12-07 13:18:19 +01004700 int is_mintty = FALSE;
Bram Moolenaar89577b32019-10-18 21:26:05 +02004701 int is_screen = FALSE;
Bram Moolenaar20091c12018-12-07 13:18:19 +01004702
4703 // mintty 2.9.5 sends 77;20905;0c.
4704 // (77 is ASCII 'M' for mintty.)
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004705 if (arg[0] == 77)
Bram Moolenaar20091c12018-12-07 13:18:19 +01004706 is_mintty = TRUE;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004707
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004708 // if xterm version >= 141 try to get termcap codes
Bram Moolenaar995e4af2017-09-01 20:24:03 +02004709 if (version >= 141)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004711 LOG_TR(("Enable checking for XT codes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 check_for_codes = TRUE;
4713 need_gather = TRUE;
4714 req_codes_from_term();
4715 }
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004716
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004717 // libvterm sends 0;100;0
4718 if (version == 100 && arg[0] == 0 && arg[2] == 0)
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004719 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004720 // If run from Vim $COLORS is set to the number of
4721 // colors the terminal supports. Otherwise assume
4722 // 256, libvterm supports even more.
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004723 if (mch_getenv((char_u *)"COLORS") == NULL)
4724 may_adjust_color_count(256);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004725 // Libvterm can handle SGR mouse reporting.
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004726 if (!option_was_set((char_u *)"ttym"))
4727 set_option_value((char_u *)"ttym", 0L,
4728 (char_u *)"sgr", 0);
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004729 }
4730
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004731 if (version == 95)
4732 {
Bram Moolenaare330ef42018-07-06 23:11:40 +02004733 // Mac Terminal.app sends 1;95;0
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004734 if (arg[0] == 1 && arg[2] == 0)
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004735 {
4736 is_not_xterm = TRUE;
4737 is_mac_terminal = TRUE;
4738 }
Bram Moolenaare330ef42018-07-06 23:11:40 +02004739 // iTerm2 sends 0;95;0
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004740 else if (arg[0] == 0 && arg[2] == 0)
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004741 is_iterm2 = TRUE;
Bram Moolenaare330ef42018-07-06 23:11:40 +02004742 // old iTerm2 sends 0;95;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004743 else if (arg[0] == 0 && arg[2] == -1)
Bram Moolenaare330ef42018-07-06 23:11:40 +02004744 is_not_xterm = TRUE;
Bram Moolenaar9c6ce0e2017-11-16 22:07:13 +01004745 }
4746
Bram Moolenaar89577b32019-10-18 21:26:05 +02004747 // screen sends 83;40500;0 83 is 'S' in ASCII.
4748 if (arg[0] == 83)
4749 is_screen = TRUE;
4750
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004751 // Only set 'ttymouse' automatically if it was not set
4752 // by the user already.
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004753 if (!option_was_set((char_u *)"ttym"))
4754 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004755 // Xterm version 277 supports SGR. Also support
Bram Moolenaar89577b32019-10-18 21:26:05 +02004756 // Terminal.app, iTerm2, mintty, and screen 4.7+.
4757 if ((!is_screen && version >= 277)
4758 || is_iterm2
4759 || is_mac_terminal
4760 || is_mintty
4761 || (is_screen && arg[1] >= 40700))
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004762 set_option_value((char_u *)"ttym", 0L,
4763 (char_u *)"sgr", 0);
Bram Moolenaar314ca7c2020-04-17 16:40:31 +02004764 // For xterm version >= 95 mouse dragging works.
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01004765 else if (version >= 95)
Bram Moolenaar52a2f0f2017-11-04 15:16:56 +01004766 set_option_value((char_u *)"ttym", 0L,
4767 (char_u *)"xterm2", 0);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02004768 }
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004769
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004770 // Detect terminals that set $TERM to something like
Bram Moolenaar314ca7c2020-04-17 16:40:31 +02004771 // "xterm-256color" but are not fully xterm compatible.
Bram Moolenaarc2127982017-09-11 20:34:13 +02004772
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004773 // Gnome terminal sends 1;3801;0, 1;4402;0 or 1;2501;0.
4774 // xfce4-terminal sends 1;2802;0.
4775 // screen sends 83;40500;0
4776 // Assuming any version number over 2500 is not an
4777 // xterm (without the limit for rxvt and screen).
4778 if (arg[1] >= 2500)
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004779 is_not_xterm = TRUE;
4780
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004781 // PuTTY sends 0;136;0
4782 // vandyke SecureCRT sends 1;136;0
4783 else if (version == 136 && arg[2] == 0)
Bram Moolenaar314ca7c2020-04-17 16:40:31 +02004784 {
Bram Moolenaar2db0ec42017-08-31 20:17:59 +02004785 is_not_xterm = TRUE;
4786
Bram Moolenaar314ca7c2020-04-17 16:40:31 +02004787 // PuTTY supports sgr-like mouse reporting, but
4788 // only set 'ttymouse' if it was not set by the
4789 // user already.
4790 if (arg[0] == 0
4791 && !option_was_set((char_u *)"ttym"))
4792 set_option_value((char_u *)"ttym", 0L,
4793 (char_u *)"sgr", 0);
4794 }
4795
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004796 // Konsole sends 0;115;0
4797 else if (version == 115 && arg[0] == 0 && arg[2] == 0)
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004798 is_not_xterm = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004799
Bram Moolenaar668324e2018-06-30 17:09:26 +02004800 // Xterm first responded to this request at patch level
4801 // 95, so assume anything below 95 is not xterm.
4802 if (version < 95)
4803 is_not_xterm = TRUE;
4804
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004805 // Only request the cursor style if t_SH and t_RS are
4806 // set. Only supported properly by xterm since version
4807 // 279 (otherwise it returns 0x18).
4808 // Not for Terminal.app, it can't handle t_RS, it
4809 // echoes the characters to the screen.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004810 if (rcs_status.tr_progress == STATUS_GET
Bram Moolenaare22bbf62017-09-19 20:47:16 +02004811 && version >= 279
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004812 && !is_not_xterm
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004813 && *T_CSH != NUL
4814 && *T_CRS != NUL)
4815 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004816 LOG_TR(("Sending cursor style request"));
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004817 out_str(T_CRS);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004818 termrequest_sent(&rcs_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004819 need_flush = TRUE;
Bram Moolenaar833e0e32017-08-26 15:16:03 +02004820 }
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004821
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004822 // Only request the cursor blink mode if t_RC set. Not
4823 // for Gnome terminal, it can't handle t_RC, it
4824 // echoes the characters to the screen.
Bram Moolenaarafd78262019-05-10 23:10:31 +02004825 if (rbm_status.tr_progress == STATUS_GET
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004826 && !is_not_xterm
4827 && *T_CRC != NUL)
4828 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02004829 LOG_TR(("Sending cursor blink mode request"));
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004830 out_str(T_CRC);
Bram Moolenaarafd78262019-05-10 23:10:31 +02004831 termrequest_sent(&rbm_status);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004832 need_flush = TRUE;
4833 }
4834
4835 if (need_flush)
4836 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004838 slen = csi_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839# ifdef FEAT_EVAL
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004840 set_vim_var_string(VV_TERMRESPONSE, tp, slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 apply_autocmds(EVENT_TERMRESPONSE,
4843 NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 key_name[0] = (int)KS_EXTRA;
4845 key_name[1] = (int)KE_IGNORE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004847
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004848 // Check blinking cursor from xterm:
4849 // {lead}?12;1$y set
4850 // {lead}?12;2$y not set
4851 //
4852 // {lead} can be <Esc>[ or CSI
Bram Moolenaarafd78262019-05-10 23:10:31 +02004853 else if (rbm_status.tr_progress == STATUS_SENT
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004854 && first == '?'
4855 && ap == argp + 6
4856 && arg[0] == 12
4857 && ap[-1] == '$'
4858 && trail == 'y')
Bram Moolenaar4db25542017-08-28 22:43:05 +02004859 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004860 initial_cursor_blink = (arg[1] == '1');
Bram Moolenaarafd78262019-05-10 23:10:31 +02004861 rbm_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02004862 LOG_TR(("Received cursor blinking mode response: %s", tp));
Bram Moolenaar4db25542017-08-28 22:43:05 +02004863 key_name[0] = (int)KS_EXTRA;
4864 key_name[1] = (int)KE_IGNORE;
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004865 slen = csi_len;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02004866# ifdef FEAT_EVAL
4867 set_vim_var_string(VV_TERMBLINKRESP, tp, slen);
4868# endif
Bram Moolenaar4db25542017-08-28 22:43:05 +02004869 }
4870
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004871 // Check for a window position response from the terminal:
4872 // {lead}3;{x};{y}t
4873 else if (did_request_winpos && argc == 3 && arg[0] == 3
4874 && trail == 't')
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004875 {
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004876 winpos_x = arg[1];
4877 winpos_y = arg[2];
4878 // got finished code: consume it
4879 key_name[0] = (int)KS_EXTRA;
4880 key_name[1] = (int)KE_IGNORE;
4881 slen = csi_len;
Bram Moolenaar89894aa2018-03-05 22:43:10 +01004882
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004883 if (--did_request_winpos <= 0)
4884 winpos_status.tr_progress = STATUS_GOT;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02004885 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004886
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004887 // Key with modifier:
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004888 // {lead}27;{modifier};{key}~
4889 // {lead}{key};{modifier}u
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004890 else if ((arg[0] == 27 && argc == 3 && trail == '~')
4891 || (argc == 2 && trail == 'u'))
4892 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02004893 seenModifyOtherKeys = TRUE;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004894 if (trail == 'u')
4895 key = arg[0];
4896 else
4897 key = arg[2];
4898
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004899 modifiers = decode_modifiers(arg[1]);
Bram Moolenaard1e2f392019-10-12 18:22:50 +02004900
4901 // Some keys already have Shift included, pass them as
Bram Moolenaar459fd782019-10-13 16:43:39 +02004902 // normal keys. Not when Ctrl is also used, because <C-H>
4903 // and <C-S-H> are different.
Bram Moolenaard1e2f392019-10-12 18:22:50 +02004904 if (modifiers == MOD_MASK_SHIFT
4905 && ((key >= '@' && key <= 'Z')
4906 || key == '^' || key == '_'
4907 || (key >= '{' && key <= '~')))
4908 modifiers = 0;
4909
Bram Moolenaar459fd782019-10-13 16:43:39 +02004910 // When used with Ctrl we always make a letter upper case,
4911 // so that mapping <C-H> and <C-h> are the same. Typing
4912 // <C-S-H> also uses "H" but modifier is different.
4913 if ((modifiers & MOD_MASK_CTRL) && ASCII_ISALPHA(key))
4914 key = TOUPPER_ASC(key);
4915
Bram Moolenaard1e2f392019-10-12 18:22:50 +02004916 // insert modifiers with KS_MODIFIER
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02004917 new_slen = modifiers2keycode(modifiers, &key, string);
4918 slen = csi_len;
4919
4920 if (has_mbyte)
4921 new_slen += (*mb_char2bytes)(key, string + new_slen);
4922 else
4923 string[new_slen++] = key;
4924
4925 if (put_string_in_typebuf(offset, slen, string, new_slen,
4926 buf, bufsize, buflen) == FAIL)
4927 return -1;
4928 return len + new_slen - slen + offset;
4929 }
Bram Moolenaarc3e555b2019-10-08 20:15:39 +02004930
4931 // else: Unknown CSI sequence. We could drop it, but then the
4932 // user can't create a map for it.
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004933 }
4934
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004935 // Check for fore/background color response from the terminal:
4936 //
4937 // {lead}{code};rgb:{rrrr}/{gggg}/{bbbb}{tail}
4938 // or {lead}{code};rgb:{rr}/{gg}/{bb}{tail}
4939 //
4940 // {code} is 10 for foreground, 11 for background
4941 // {lead} can be <Esc>] or OSC
4942 // {tail} can be '\007', <Esc>\ or STERM.
4943 //
4944 // Consume any code that starts with "{lead}11;", it's also
4945 // possible that "rgba" is following.
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004946 else if ((*T_RBG != NUL || *T_RFG != NUL)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004947 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4948 || tp[0] == OSC))
4949 {
4950 j = 1 + (tp[0] == ESC);
4951 if (len >= j + 3 && (argp[0] != '1'
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004952 || (argp[1] != '1' && argp[1] != '0')
4953 || argp[2] != ';'))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004954 i = 0; // no match
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004955 else
4956 for (i = j; i < len; ++i)
4957 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4958 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004959 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004960 int is_bg = argp[1] == '1';
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004961 int is_4digit = i - j >= 21 && tp[j + 11] == '/'
4962 && tp[j + 16] == '/';
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004963
Bram Moolenaar12e71eb2019-06-06 15:19:31 +02004964 if (i - j >= 15 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004965 && (is_4digit
4966 || (tp[j + 9] == '/' && tp[i + 12 == '/'])))
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004967 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02004968 char_u *tp_r = tp + j + 7;
4969 char_u *tp_g = tp + j + (is_4digit ? 12 : 10);
4970 char_u *tp_b = tp + j + (is_4digit ? 17 : 13);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004971# ifdef FEAT_TERMINAL
Bram Moolenaarcea254f2019-06-04 21:41:28 +02004972 int rval, gval, bval;
4973
Bram Moolenaar32e19772019-06-05 22:57:04 +02004974 rval = hexhex2nr(tp_r);
4975 gval = hexhex2nr(tp_b);
4976 bval = hexhex2nr(tp_g);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004977# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004978 if (is_bg)
4979 {
Bram Moolenaar32e19772019-06-05 22:57:04 +02004980 char *new_bg_val = (3 * '6' < *tp_r + *tp_g +
4981 *tp_b) ? "light" : "dark";
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004982
Bram Moolenaarb255b902018-04-24 21:40:10 +02004983 LOG_TR(("Received RBG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02004984 rbg_status.tr_progress = STATUS_GOT;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004985# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004986 bg_r = rval;
4987 bg_g = gval;
4988 bg_b = bval;
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004989# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004990 if (!option_was_set((char_u *)"bg")
Bram Moolenaar32e19772019-06-05 22:57:04 +02004991 && STRCMP(p_bg, new_bg_val) != 0)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004992 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004993 // value differs, apply it
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004994 set_option_value((char_u *)"bg", 0L,
Bram Moolenaar32e19772019-06-05 22:57:04 +02004995 (char_u *)new_bg_val, 0);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004996 reset_option_was_set((char_u *)"bg");
4997 redraw_asap(CLEAR);
4998 }
Bram Moolenaar4b974d52017-06-04 15:37:46 +02004999 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005000# ifdef FEAT_TERMINAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005001 else
5002 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005003 LOG_TR(("Received RFG response: %s", tp));
Bram Moolenaarafd78262019-05-10 23:10:31 +02005004 rfg_status.tr_progress = STATUS_GOT;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005005 fg_r = rval;
5006 fg_g = gval;
5007 fg_b = bval;
5008 }
Bram Moolenaar6bc93052019-04-06 20:00:19 +02005009# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005010 }
5011
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005012 // got finished code: consume it
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005013 key_name[0] = (int)KS_EXTRA;
5014 key_name[1] = (int)KE_IGNORE;
5015 slen = i + 1 + (tp[i] == ESC);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005016# ifdef FEAT_EVAL
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005017 set_vim_var_string(is_bg ? VV_TERMRBGRESP
5018 : VV_TERMRFGRESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005019# endif
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005020 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02005021 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005022 if (i == len)
5023 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005024 LOG_TR(("not enough characters for RB"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005025 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02005026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005029 // Check for key code response from xterm:
5030 // {lead}{flag}+r<hex bytes><{tail}
5031 //
5032 // {lead} can be <Esc>P or DCS
5033 // {flag} can be '0' or '1'
5034 // {tail} can be Esc>\ or STERM
5035 //
5036 // Check for cursor shape response from xterm:
5037 // {lead}1$r<digit> q{tail}
5038 //
5039 // {lead} can be <Esc>P or DCS
5040 // {tail} can be <Esc>\ or STERM
5041 //
5042 // Consume any code that starts with "{lead}.+r" or "{lead}.$r".
Bram Moolenaarafd78262019-05-10 23:10:31 +02005043 else if ((check_for_codes || rcs_status.tr_progress == STATUS_SENT)
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005044 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 || tp[0] == DCS))
5046 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005047 j = 1 + (tp[0] == ESC);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005048 if (len < j + 3)
Bram Moolenaar66761db2019-06-05 22:07:51 +02005049 i = len; // need more chars
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005050 else if ((argp[1] != '+' && argp[1] != '$') || argp[2] != 'r')
Bram Moolenaar66761db2019-06-05 22:07:51 +02005051 i = 0; // no match
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005052 else if (argp[1] == '+')
Bram Moolenaar66761db2019-06-05 22:07:51 +02005053 // key code response
5054 for (i = j; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02005056 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
5057 || tp[i] == STERM)
5058 {
5059 if (i - j >= 3)
5060 got_code_from_term(tp + j, i);
5061 key_name[0] = (int)KS_EXTRA;
5062 key_name[1] = (int)KE_IGNORE;
5063 slen = i + 1 + (tp[i] == ESC);
5064 break;
5065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
Bram Moolenaar590ec872018-03-02 20:58:42 +01005067 else
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005068 {
Bram Moolenaar66761db2019-06-05 22:07:51 +02005069 // Probably the cursor shape response. Make sure that "i"
5070 // is equal to "len" when there are not sufficient
5071 // characters.
Bram Moolenaar590ec872018-03-02 20:58:42 +01005072 for (i = j + 3; i < len; ++i)
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005073 {
Bram Moolenaar590ec872018-03-02 20:58:42 +01005074 if (i - j == 3 && !isdigit(tp[i]))
5075 break;
5076 if (i - j == 4 && tp[i] != ' ')
5077 break;
5078 if (i - j == 5 && tp[i] != 'q')
5079 break;
5080 if (i - j == 6 && tp[i] != ESC && tp[i] != STERM)
5081 break;
5082 if ((i - j == 6 && tp[i] == STERM)
5083 || (i - j == 7 && tp[i] == '\\'))
5084 {
5085 int number = argp[3] - '0';
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005086
Bram Moolenaar66761db2019-06-05 22:07:51 +02005087 // 0, 1 = block blink, 2 = block
5088 // 3 = underline blink, 4 = underline
5089 // 5 = vertical bar blink, 6 = vertical bar
Bram Moolenaar590ec872018-03-02 20:58:42 +01005090 number = number == 0 ? 1 : number;
5091 initial_cursor_shape = (number + 1) / 2;
Bram Moolenaar66761db2019-06-05 22:07:51 +02005092 // The blink flag is actually inverted, compared to
5093 // the value set with T_SH.
Bram Moolenaar590ec872018-03-02 20:58:42 +01005094 initial_cursor_shape_blink =
Bram Moolenaar4db25542017-08-28 22:43:05 +02005095 (number & 1) ? FALSE : TRUE;
Bram Moolenaarafd78262019-05-10 23:10:31 +02005096 rcs_status.tr_progress = STATUS_GOT;
Bram Moolenaarb255b902018-04-24 21:40:10 +02005097 LOG_TR(("Received cursor shape response: %s", tp));
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005098
Bram Moolenaar590ec872018-03-02 20:58:42 +01005099 key_name[0] = (int)KS_EXTRA;
5100 key_name[1] = (int)KE_IGNORE;
5101 slen = i + 1;
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005102# ifdef FEAT_EVAL
Bram Moolenaar590ec872018-03-02 20:58:42 +01005103 set_vim_var_string(VV_TERMSTYLERESP, tp, slen);
Bram Moolenaarf3af54e2017-08-30 14:53:06 +02005104# endif
Bram Moolenaar590ec872018-03-02 20:58:42 +01005105 break;
5106 }
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005107 }
5108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109
5110 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02005111 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005112 // These codes arrive many together, each code can be
5113 // truncated at any point.
Bram Moolenaarb255b902018-04-24 21:40:10 +02005114 LOG_TR(("not enough characters for XT"));
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02005115 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02005116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
5118 }
5119#endif
5120
5121 if (key_name[0] == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005122 continue; // No match at this position, try next one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005124 // We only get here when we have a complete termcode match
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005126#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 /*
5128 * Only in the GUI: Fetch the pointer coordinates of the scroll event
5129 * so that we know which window to scroll later.
5130 */
5131 if (gui.in_use
5132 && key_name[0] == (int)KS_EXTRA
5133 && (key_name[1] == (int)KE_X1MOUSE
5134 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005135 || key_name[1] == (int)KE_MOUSELEFT
5136 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 || key_name[1] == (int)KE_MOUSEDOWN
5138 || key_name[1] == (int)KE_MOUSEUP))
5139 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005140 char_u bytes[6];
5141 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
5142
5143 if (num_bytes == -1) // not enough coordinates
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 return -1;
5145 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
5146 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
5147 slen += num_bytes;
5148 }
5149 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 /*
5152 * If it is a mouse click, get the coordinates.
5153 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005154 if (key_name[0] == KS_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005155#ifdef FEAT_MOUSE_GPM
Bram Moolenaarbedf0912019-05-04 16:58:45 +02005156 || key_name[0] == KS_GPM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005157#endif
5158#ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005159 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005160#endif
5161#ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005162 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005163#endif
5164#ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005165 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005166#endif
5167#ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005168 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005169#endif
5170#ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005171 || key_name[0] == KS_URXVT_MOUSE
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02005172#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02005173 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01005174 || key_name[0] == KS_SGR_MOUSE_RELEASE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005176 if (check_termcode_mouse(tp, &slen, key_name, modifiers_start, idx,
5177 &modifiers) == -1)
5178 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180
5181#ifdef FEAT_GUI
5182 /*
5183 * If using the GUI, then we get menu and scrollbar events.
5184 *
5185 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5186 * four bytes which are to be taken as a pointer to the vimmenu_T
5187 * structure.
5188 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005189 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005190 * is one byte with the tab index.
5191 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5193 * by one byte representing the scrollbar number, and then four bytes
5194 * representing a long_u which is the new value of the scrollbar.
5195 *
5196 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5197 * KE_FILLER followed by four bytes representing a long_u which is the
5198 * new value of the scrollbar.
5199 */
5200# ifdef FEAT_MENU
5201 else if (key_name[0] == (int)KS_MENU)
5202 {
5203 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005204 int num_bytes = get_long_from_buf(tp + slen, &val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 if (num_bytes == -1)
5207 return -1;
5208 current_menu = (vimmenu_T *)val;
5209 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005210
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005211 // The menu may have been deleted right after it was used, check
5212 // for that.
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005213 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5214 {
5215 key_name[0] = KS_EXTRA;
5216 key_name[1] = (int)KE_IGNORE;
5217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005220# ifdef FEAT_GUI_TABLINE
5221 else if (key_name[0] == (int)KS_TABLINE)
5222 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005223 // Selecting tabline tab or using its menu.
5224 char_u bytes[6];
5225 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5226
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005227 if (num_bytes == -1)
5228 return -1;
5229 current_tab = (int)bytes[0];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005230 if (current_tab == 255) // -1 in a byte gives 255
Bram Moolenaar07354542007-09-15 12:07:46 +00005231 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005232 slen += num_bytes;
5233 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005234 else if (key_name[0] == (int)KS_TABMENU)
5235 {
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005236 // Selecting tabline tab or using its menu.
5237 char_u bytes[6];
5238 int num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5239
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005240 if (num_bytes == -1)
5241 return -1;
5242 current_tab = (int)bytes[0];
5243 current_tabmenu = (int)bytes[1];
5244 slen += num_bytes;
5245 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005246# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247# ifndef USE_ON_FLY_SCROLL
5248 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5249 {
5250 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005251 char_u bytes[6];
5252 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005254 // Get the last scrollbar event in the queue of the same type
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 j = 0;
5256 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5257 && tp[j + 2] != NUL; ++i)
5258 {
5259 j += 3;
5260 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5261 if (num_bytes == -1)
5262 break;
5263 if (i == 0)
5264 current_scrollbar = (int)bytes[0];
5265 else if (current_scrollbar != (int)bytes[0])
5266 break;
5267 j += num_bytes;
5268 num_bytes = get_long_from_buf(tp + j, &val);
5269 if (num_bytes == -1)
5270 break;
5271 scrollbar_value = val;
5272 j += num_bytes;
5273 slen = j;
5274 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005275 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 return -1;
5277 }
5278 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5279 {
5280 long_u val;
Bram Moolenaarb8ff5c22019-09-23 21:16:54 +02005281 int num_bytes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005283 // Get the last horiz. scrollbar event in the queue
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 j = 0;
5285 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5286 && tp[j + 2] != NUL; ++i)
5287 {
5288 j += 3;
5289 num_bytes = get_long_from_buf(tp + j, &val);
5290 if (num_bytes == -1)
5291 break;
5292 scrollbar_value = val;
5293 j += num_bytes;
5294 slen = j;
5295 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005296 if (i == 0) // not enough characters to make one
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 return -1;
5298 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005299# endif // !USE_ON_FLY_SCROLL
5300#endif // FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005302 /*
5303 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5304 */
5305 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5306
5307 /*
5308 * Add any modifier codes to our string.
5309 */
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005310 new_slen = modifiers2keycode(modifiers, &key, string);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005311
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005312 // Finally, add the special key code to our string
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005313 key_name[0] = KEY2TERMCAP0(key);
5314 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005316 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005317 // from ":set <M-b>=xx"
Bram Moolenaar6768a332009-01-22 17:33:49 +00005318 if (has_mbyte)
5319 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5320 else
Bram Moolenaar6768a332009-01-22 17:33:49 +00005321 string[new_slen++] = key_name[1];
5322 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005323 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5324 && key_name[1] == KE_IGNORE)
5325 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005326 // Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5327 // to indicate what happened.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005328 retval = KEYLEN_REMOVED;
5329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 else
5331 {
5332 string[new_slen++] = K_SPECIAL;
5333 string[new_slen++] = key_name[0];
5334 string[new_slen++] = key_name[1];
5335 }
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02005336 if (put_string_in_typebuf(offset, slen, string, new_slen,
5337 buf, bufsize, buflen) == FAIL)
5338 return -1;
5339 return retval == 0 ? (len + new_slen - slen + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
5341
Bram Moolenaar2951b772013-07-03 12:45:31 +02005342#ifdef FEAT_TERMRESPONSE
Bram Moolenaarb255b902018-04-24 21:40:10 +02005343 LOG_TR(("normal character"));
Bram Moolenaar2951b772013-07-03 12:45:31 +02005344#endif
5345
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005346 return 0; // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347}
5348
Bram Moolenaar9377df32017-10-15 13:22:01 +02005349#if (defined(FEAT_TERMINAL) && defined(FEAT_TERMRESPONSE)) || defined(PROTO)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005350/*
5351 * Get the text foreground color, if known.
5352 */
5353 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005354term_get_fg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005355{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005356 if (rfg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005357 {
5358 *r = fg_r;
5359 *g = fg_g;
5360 *b = fg_b;
5361 }
5362}
5363
5364/*
5365 * Get the text background color, if known.
5366 */
5367 void
Bram Moolenaar00ce63d2017-10-15 21:44:45 +02005368term_get_bg_color(char_u *r, char_u *g, char_u *b)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005369{
Bram Moolenaarafd78262019-05-10 23:10:31 +02005370 if (rbg_status.tr_progress == STATUS_GOT)
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02005371 {
5372 *r = bg_r;
5373 *g = bg_g;
5374 *b = bg_b;
5375 }
5376}
5377#endif
5378
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379/*
5380 * Replace any terminal code strings in from[] with the equivalent internal
5381 * vim representation. This is used for the "from" and "to" part of a
5382 * mapping, and the "to" part of a menu command.
5383 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5384 * '<'.
5385 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5386 *
5387 * The replacement is done in result[] and finally copied into allocated
5388 * memory. If this all works well *bufp is set to the allocated memory and a
5389 * pointer to it is returned. If something fails *bufp is set to NULL and from
5390 * is returned.
5391 *
Bram Moolenaar459fd782019-10-13 16:43:39 +02005392 * CTRL-V characters are removed. When "flags" has REPTERM_FROM_PART, a
5393 * trailing CTRL-V is included, otherwise it is removed (for ":map xx ^V", maps
5394 * xx to nothing). When 'cpoptions' does not contain 'B', a backslash can be
5395 * used instead of a CTRL-V.
5396 *
5397 * Flags:
5398 * REPTERM_FROM_PART see above
5399 * REPTERM_DO_LT also translate <lt>
5400 * REPTERM_SPECIAL always accept <key> notation
5401 * REPTERM_NO_SIMPLIFY do not simplify <C-H> to 0x08 and set 8th bit for <A-x>
5402 *
5403 * "did_simplify" is set when some <C-H> or <A-x> code was simplified, unless
5404 * it is NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005406 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005407replace_termcodes(
5408 char_u *from,
5409 char_u **bufp,
Bram Moolenaar459fd782019-10-13 16:43:39 +02005410 int flags,
5411 int *did_simplify)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412{
5413 int i;
5414 int slen;
5415 int key;
5416 int dlen = 0;
5417 char_u *src;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005418 int do_backslash; // backslash is a special character
5419 int do_special; // recognize <> key codes
5420 int do_key_code; // recognize raw key codes
5421 char_u *result; // buffer for resulting string
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
5423 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar459fd782019-10-13 16:43:39 +02005424 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
5425 || (flags & REPTERM_SPECIAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5427
5428 /*
5429 * Allocate space for the translation. Worst case a single character is
5430 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5431 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005432 result = alloc(STRLEN(from) * 6 + 1);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005433 if (result == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 {
5435 *bufp = NULL;
5436 return from;
5437 }
5438
5439 src = from;
5440
5441 /*
5442 * Check for #n at start only: function key n
5443 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005444 if ((flags & REPTERM_FROM_PART) && src[0] == '#' && VIM_ISDIGIT(src[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
5446 result[dlen++] = K_SPECIAL;
5447 result[dlen++] = 'k';
5448 if (src[1] == '0')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005449 result[dlen++] = ';'; // #0 is F10 is "k;"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005451 result[dlen++] = src[1]; // #3 is F3 is "k3"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 src += 2;
5453 }
5454
5455 /*
5456 * Copy each byte from *from to result[dlen]
5457 */
5458 while (*src != NUL)
5459 {
5460 /*
5461 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005462 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 */
Bram Moolenaar459fd782019-10-13 16:43:39 +02005464 if (do_special && ((flags & REPTERM_DO_LT)
5465 || STRNCMP(src, "<lt>", 4) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
5467#ifdef FEAT_EVAL
5468 /*
5469 * Replace <SID> by K_SNR <script-nr> _.
5470 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5471 */
5472 if (STRNICMP(src, "<SID>", 5) == 0)
5473 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005474 if (current_sctx.sc_sid <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005475 emsg(_(e_usingsid));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 else
5477 {
5478 src += 5;
5479 result[dlen++] = K_SPECIAL;
5480 result[dlen++] = (int)KS_EXTRA;
5481 result[dlen++] = (int)KE_SNR;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005482 sprintf((char *)result + dlen, "%ld",
5483 (long)current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 dlen += (int)STRLEN(result + dlen);
5485 result[dlen++] = '_';
5486 continue;
5487 }
5488 }
5489#endif
5490
Bram Moolenaarebe9d342020-05-30 21:52:54 +02005491 slen = trans_special(&src, result + dlen, FSK_KEYCODE
5492 | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
5493 did_simplify);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 if (slen)
5495 {
5496 dlen += slen;
5497 continue;
5498 }
5499 }
5500
5501 /*
5502 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5503 * Note that this is also checked after replacing the <> form.
5504 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5505 * it could be a character in the file.
5506 */
5507 if (do_key_code)
5508 {
5509 i = find_term_bykeys(src);
5510 if (i >= 0)
5511 {
5512 result[dlen++] = K_SPECIAL;
5513 result[dlen++] = termcodes[i].name[0];
5514 result[dlen++] = termcodes[i].name[1];
5515 src += termcodes[i].len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005516 // If terminal code matched, continue after it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 continue;
5518 }
5519 }
5520
5521#ifdef FEAT_EVAL
5522 if (do_special)
5523 {
5524 char_u *p, *s, len;
5525
5526 /*
5527 * Replace <Leader> by the value of "mapleader".
5528 * Replace <LocalLeader> by the value of "maplocalleader".
5529 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5530 */
5531 if (STRNICMP(src, "<Leader>", 8) == 0)
5532 {
5533 len = 8;
5534 p = get_var_value((char_u *)"g:mapleader");
5535 }
5536 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5537 {
5538 len = 13;
5539 p = get_var_value((char_u *)"g:maplocalleader");
5540 }
5541 else
5542 {
5543 len = 0;
5544 p = NULL;
5545 }
5546 if (len != 0)
5547 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005548 // Allow up to 8 * 6 characters for "mapleader".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5550 s = (char_u *)"\\";
5551 else
5552 s = p;
5553 while (*s != NUL)
5554 result[dlen++] = *s++;
5555 src += len;
5556 continue;
5557 }
5558 }
5559#endif
5560
5561 /*
5562 * Remove CTRL-V and ignore the next character.
5563 * For "from" side the CTRL-V at the end is included, for the "to"
5564 * part it is removed.
5565 * If 'cpoptions' does not contain 'B', also accept a backslash.
5566 */
5567 key = *src;
5568 if (key == Ctrl_V || (do_backslash && key == '\\'))
5569 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005570 ++src; // skip CTRL-V or backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 if (*src == NUL)
5572 {
Bram Moolenaar459fd782019-10-13 16:43:39 +02005573 if (flags & REPTERM_FROM_PART)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 result[dlen++] = key;
5575 break;
5576 }
5577 }
5578
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005579 // skip multibyte char correctly
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005580 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {
5582 /*
5583 * If the character is K_SPECIAL, replace it with K_SPECIAL
5584 * KS_SPECIAL KE_FILLER.
5585 * If compiled with the GUI replace CSI with K_CSI.
5586 */
5587 if (*src == K_SPECIAL)
5588 {
5589 result[dlen++] = K_SPECIAL;
5590 result[dlen++] = KS_SPECIAL;
5591 result[dlen++] = KE_FILLER;
5592 }
5593# ifdef FEAT_GUI
5594 else if (*src == CSI)
5595 {
5596 result[dlen++] = K_SPECIAL;
5597 result[dlen++] = KS_EXTRA;
5598 result[dlen++] = (int)KE_CSI;
5599 }
5600# endif
5601 else
5602 result[dlen++] = *src;
5603 ++src;
5604 }
5605 }
5606 result[dlen] = NUL;
5607
5608 /*
5609 * Copy the new string to allocated memory.
5610 * If this fails, just return from.
5611 */
5612 if ((*bufp = vim_strsave(result)) != NULL)
5613 from = *bufp;
5614 vim_free(result);
5615 return from;
5616}
5617
5618/*
5619 * Find a termcode with keys 'src' (must be NUL terminated).
5620 * Return the index in termcodes[], or -1 if not found.
5621 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005622 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005623find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
5625 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005626 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627
5628 for (i = 0; i < tc_len; ++i)
5629 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005630 if (slen == termcodes[i].len
5631 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 return i;
5633 }
5634 return -1;
5635}
5636
5637/*
5638 * Gather the first characters in the terminal key codes into a string.
5639 * Used to speed up check_termcode().
5640 */
5641 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005642gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643{
5644 int i;
5645 int len = 0;
5646
5647#ifdef FEAT_GUI
5648 if (gui.in_use)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005649 termleader[len++] = CSI; // the GUI codes are not in termcodes[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650#endif
5651#ifdef FEAT_TERMRESPONSE
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02005652 if (check_for_codes || *T_CRS != NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005653 termleader[len++] = DCS; // the termcode response starts with DCS
5654 // in 8-bit mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655#endif
5656 termleader[len] = NUL;
5657
5658 for (i = 0; i < tc_len; ++i)
5659 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5660 {
5661 termleader[len++] = termcodes[i].code[0];
5662 termleader[len] = NUL;
5663 }
5664
5665 need_gather = FALSE;
5666}
5667
5668/*
5669 * Show all termcodes (for ":set termcap")
5670 * This code looks a lot like showoptions(), but is different.
5671 */
5672 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005673show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674{
5675 int col;
5676 int *items;
5677 int item_count;
5678 int run;
5679 int row, rows;
5680 int cols;
5681 int i;
5682 int len;
5683
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005684#define INC3 27 // try to make three columns
5685#define INC2 40 // try to make two columns
5686#define GAP 2 // spaces between columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005688 if (tc_len == 0) // no terminal codes (must be GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 return;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005690 items = ALLOC_MULT(int, tc_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 if (items == NULL)
5692 return;
5693
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005694 // Highlight title
Bram Moolenaar32526b32019-01-19 17:43:09 +01005695 msg_puts_title(_("\n--- Terminal keys ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696
5697 /*
5698 * do the loop two times:
5699 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005700 * 2. display the medium items (medium length strings)
5701 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005703 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
5705 /*
5706 * collect the items in items[]
5707 */
5708 item_count = 0;
5709 for (i = 0; i < tc_len; i++)
5710 {
5711 len = show_one_termcode(termcodes[i].name,
5712 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005713 if (len <= INC3 - GAP ? run == 1
5714 : len <= INC2 - GAP ? run == 2
5715 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 items[item_count++] = i;
5717 }
5718
5719 /*
5720 * display the items
5721 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005722 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005724 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 if (cols == 0)
5726 cols = 1;
5727 rows = (item_count + cols - 1) / cols;
5728 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005729 else // run == 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 rows = item_count;
5731 for (row = 0; row < rows && !got_int; ++row)
5732 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005733 msg_putchar('\n'); // go to next line
5734 if (got_int) // 'q' typed in more
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 break;
5736 col = 0;
5737 for (i = row; i < item_count; i += rows)
5738 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005739 msg_col = col; // make columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 show_one_termcode(termcodes[items[i]].name,
5741 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005742 if (run == 2)
5743 col += INC2;
5744 else
5745 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 }
5747 out_flush();
5748 ui_breakcheck();
5749 }
5750 }
5751 vim_free(items);
5752}
5753
5754/*
5755 * Show one termcode entry.
5756 * Output goes into IObuff[]
5757 */
5758 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005759show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760{
5761 char_u *p;
5762 int len;
5763
5764 if (name[0] > '~')
5765 {
5766 IObuff[0] = ' ';
5767 IObuff[1] = ' ';
5768 IObuff[2] = ' ';
5769 IObuff[3] = ' ';
5770 }
5771 else
5772 {
5773 IObuff[0] = 't';
5774 IObuff[1] = '_';
5775 IObuff[2] = name[0];
5776 IObuff[3] = name[1];
5777 }
5778 IObuff[4] = ' ';
5779
5780 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5781 if (p[1] != 't')
5782 STRCPY(IObuff + 5, p);
5783 else
5784 IObuff[5] = NUL;
5785 len = (int)STRLEN(IObuff);
5786 do
5787 IObuff[len++] = ' ';
5788 while (len < 17);
5789 IObuff[len] = NUL;
5790 if (code == NULL)
5791 len += 4;
5792 else
5793 len += vim_strsize(code);
5794
5795 if (printit)
5796 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005797 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 if (code == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005799 msg_puts("NULL");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 else
5801 msg_outtrans(code);
5802 }
5803 return len;
5804}
5805
5806#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5807/*
5808 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5809 * termcap codes from the terminal itself.
5810 * We get them one by one to avoid a very long response string.
5811 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005812static int xt_index_in = 0;
5813static int xt_index_out = 0;
5814
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005816req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817{
5818 xt_index_out = 0;
5819 xt_index_in = 0;
5820 req_more_codes_from_term();
5821}
5822
5823 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005824req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 char buf[11];
5827 int old_idx = xt_index_out;
5828
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005829 // Don't do anything when going to exit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 if (exiting)
5831 return;
5832
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005833 // Send up to 10 more requests out than we received. Avoid sending too
5834 // many, there can be a buffer overflow somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5836 {
Bram Moolenaarb255b902018-04-24 21:40:10 +02005837 char *key_name = key_names[xt_index_out];
Bram Moolenaar2951b772013-07-03 12:45:31 +02005838
Bram Moolenaarb255b902018-04-24 21:40:10 +02005839 LOG_TR(("Requesting XT %d: %s", xt_index_out, key_name));
5840 sprintf(buf, "\033P+q%02x%02x\033\\", key_name[0], key_name[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 out_str_nf((char_u *)buf);
5842 ++xt_index_out;
5843 }
5844
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005845 // Send the codes out right away.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 if (xt_index_out != old_idx)
5847 out_flush();
5848}
5849
5850/*
5851 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5852 * A "0" instead of the "1" indicates a code that isn't supported.
5853 * Both <name> and <string> are encoded in hex.
5854 * "code" points to the "0" or "1".
5855 */
5856 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005857got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858{
5859#define XT_LEN 100
5860 char_u name[3];
5861 char_u str[XT_LEN];
5862 int i;
5863 int j = 0;
5864 int c;
5865
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005866 // A '1' means the code is supported, a '0' means it isn't.
5867 // When half the length is > XT_LEN we can't use it.
5868 // Our names are currently all 2 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5870 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005871 // Get the name from the response and find it in the table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 name[0] = hexhex2nr(code + 3);
5873 name[1] = hexhex2nr(code + 5);
5874 name[2] = NUL;
5875 for (i = 0; key_names[i] != NULL; ++i)
5876 {
5877 if (STRCMP(key_names[i], name) == 0)
5878 {
5879 xt_index_in = i;
5880 break;
5881 }
5882 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005883
Bram Moolenaarb255b902018-04-24 21:40:10 +02005884 LOG_TR(("Received XT %d: %s", xt_index_in, (char *)name));
5885
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 if (key_names[i] != NULL)
5887 {
5888 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5889 str[j++] = c;
5890 str[j] = NUL;
5891 if (name[0] == 'C' && name[1] == 'o')
5892 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005893 // Color count is not a key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 i = atoi((char *)str);
Bram Moolenaarb7a8dfe2017-07-22 20:33:05 +02005895 may_adjust_color_count(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 }
5897 else
5898 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005899 // First delete any existing entry with the same code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 i = find_term_bykeys(str);
5901 if (i >= 0)
5902 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005903 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 }
5905 }
5906 }
5907
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005908 // May request more codes now that we received one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 ++xt_index_in;
5910 req_more_codes_from_term();
5911}
5912
5913/*
5914 * Check if there are any unanswered requests and deal with them.
5915 * This is called before starting an external program or getting direct
5916 * keyboard input. We don't want responses to be send to that program or
5917 * handled as typed text.
5918 */
5919 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005920check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921{
5922 int c;
5923
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005924 // If no codes requested or all are answered, no need to wait.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5926 return;
5927
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005928 // Vgetc() will check for and handle any response.
5929 // Keep calling vpeekc() until we don't get any responses.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 ++no_mapping;
5931 ++allow_keys;
5932 for (;;)
5933 {
5934 c = vpeekc();
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005935 if (c == NUL) // nothing available
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 break;
5937
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005938 // If a response is recognized it's replaced with K_IGNORE, must read
5939 // it from the input stream. If there is no K_IGNORE we can't do
5940 // anything, break here (there might be some responses further on, but
5941 // we don't want to throw away any typed chars).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 if (c != K_SPECIAL && c != K_IGNORE)
5943 break;
5944 c = vgetc();
5945 if (c != K_IGNORE)
5946 {
5947 vungetc(c);
5948 break;
5949 }
5950 }
5951 --no_mapping;
5952 --allow_keys;
5953}
5954#endif
5955
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005956#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957static char ksme_str[20];
5958static char ksmr_str[20];
5959static char ksmd_str[20];
5960
5961/*
5962 * For Win32 console: update termcap codes for existing console attributes.
5963 */
5964 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005965update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966{
5967 struct builtin_term *p;
5968
5969 p = find_builtin_term(DEFAULT_TERM);
5970 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
5971 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005972 attr | 0x08); // FOREGROUND_INTENSITY
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
5974 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
5975
5976 while (p->bt_string != NULL)
5977 {
5978 if (p->bt_entry == (int)KS_ME)
5979 p->bt_string = &ksme_str[0];
5980 else if (p->bt_entry == (int)KS_MR)
5981 p->bt_string = &ksmr_str[0];
5982 else if (p->bt_entry == (int)KS_MD)
5983 p->bt_string = &ksmd_str[0];
5984 ++p;
5985 }
5986}
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005987
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01005988# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02005989# define KSSIZE 20
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005990struct ks_tbl_s
5991{
Bram Moolenaard4f73432018-09-21 12:24:12 +02005992 int code; // value of KS_
5993 char *vtp; // code in vtp mode
5994 char *vtp2; // code in vtp2 mode
5995 char buf[KSSIZE]; // save buffer in non-vtp mode
5996 char vbuf[KSSIZE]; // save buffer in vtp mode
5997 char v2buf[KSSIZE]; // save buffer in vtp2 mode
5998 char arr[KSSIZE]; // real buffer
Bram Moolenaarcafafb32018-02-22 21:07:09 +01005999};
6000
6001static struct ks_tbl_s ks_tbl[] =
6002{
Bram Moolenaard4f73432018-09-21 12:24:12 +02006003 {(int)KS_ME, "\033|0m", "\033|0m"}, // normal
6004 {(int)KS_MR, "\033|7m", "\033|7m"}, // reverse
6005 {(int)KS_MD, "\033|1m", "\033|1m"}, // bold
6006 {(int)KS_SO, "\033|91m", "\033|91m"}, // standout: bright red text
6007 {(int)KS_SE, "\033|39m", "\033|39m"}, // standout end: default color
6008 {(int)KS_CZH, "\033|95m", "\033|95m"}, // italic: bright magenta text
6009 {(int)KS_CZR, "\033|0m", "\033|0m"}, // italic end
6010 {(int)KS_US, "\033|4m", "\033|4m"}, // underscore
6011 {(int)KS_UE, "\033|24m", "\033|24m"}, // underscore end
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006012# ifdef TERMINFO
Bram Moolenaard4f73432018-09-21 12:24:12 +02006013 {(int)KS_CAB, "\033|%p1%db", "\033|%p14%dm"}, // set background color
6014 {(int)KS_CAF, "\033|%p1%df", "\033|%p13%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006015 {(int)KS_CS, "\033|%p1%d;%p2%dR", "\033|%p1%d;%p2%dR"},
6016 {(int)KS_CSV, "\033|%p1%d;%p2%dV", "\033|%p1%d;%p2%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006017# else
Bram Moolenaard4f73432018-09-21 12:24:12 +02006018 {(int)KS_CAB, "\033|%db", "\033|4%dm"}, // set background color
6019 {(int)KS_CAF, "\033|%df", "\033|3%dm"}, // set foreground color
Bram Moolenaar6982f422019-02-16 16:48:01 +01006020 {(int)KS_CS, "\033|%d;%dR", "\033|%d;%dR"},
6021 {(int)KS_CSV, "\033|%d;%dV", "\033|%d;%dV"},
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006022# endif
Bram Moolenaard4f73432018-09-21 12:24:12 +02006023 {(int)KS_CCO, "256", "256"}, // colors
6024 {(int)KS_NAME} // terminator
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006025};
6026
6027 static struct builtin_term *
6028find_first_tcap(
6029 char_u *name,
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006030 int code)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006031{
6032 struct builtin_term *p;
6033
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006034 for (p = find_builtin_term(name); p->bt_string != NULL; ++p)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006035 if (p->bt_entry == code)
6036 return p;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006037 return NULL;
6038}
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006039# endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006040
6041/*
6042 * For Win32 console: replace the sequence immediately after termguicolors.
6043 */
6044 void
6045swap_tcap(void)
6046{
6047# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006048 static int init_done = FALSE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006049 static int curr_mode;
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006050 struct ks_tbl_s *ks;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006051 struct builtin_term *bt;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006052 int mode;
6053 enum
6054 {
6055 CMODEINDEX,
6056 CMODE24,
6057 CMODE256
6058 };
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006059
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006060 // buffer initialization
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006061 if (!init_done)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006062 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006063 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006064 {
6065 bt = find_first_tcap(DEFAULT_TERM, ks->code);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006066 if (bt != NULL)
6067 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006068 STRNCPY(ks->buf, bt->bt_string, KSSIZE);
6069 STRNCPY(ks->vbuf, ks->vtp, KSSIZE);
6070 STRNCPY(ks->v2buf, ks->vtp2, KSSIZE);
6071
6072 STRNCPY(ks->arr, bt->bt_string, KSSIZE);
6073 bt->bt_string = &ks->arr[0];
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006074 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006075 }
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006076 init_done = TRUE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006077 curr_mode = CMODEINDEX;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006078 }
6079
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006080 if (p_tgc)
6081 mode = CMODE24;
6082 else if (t_colors >= 256)
6083 mode = CMODE256;
6084 else
6085 mode = CMODEINDEX;
6086
6087 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006088 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006089 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6090 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006091 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006092 switch (curr_mode)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006093 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006094 case CMODEINDEX:
6095 STRNCPY(&ks->buf[0], bt->bt_string, KSSIZE);
6096 break;
6097 case CMODE24:
6098 STRNCPY(&ks->vbuf[0], bt->bt_string, KSSIZE);
6099 break;
6100 default:
6101 STRNCPY(&ks->v2buf[0], bt->bt_string, KSSIZE);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006102 }
6103 }
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006104 }
6105
6106 if (mode != curr_mode)
6107 {
6108 for (ks = ks_tbl; ks->code != (int)KS_NAME; ks++)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006109 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006110 bt = find_first_tcap(DEFAULT_TERM, ks->code);
6111 if (bt != NULL)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006112 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006113 switch (mode)
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006114 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006115 case CMODEINDEX:
6116 STRNCPY(bt->bt_string, &ks->buf[0], KSSIZE);
6117 break;
6118 case CMODE24:
6119 STRNCPY(bt->bt_string, &ks->vbuf[0], KSSIZE);
6120 break;
6121 default:
6122 STRNCPY(bt->bt_string, &ks->v2buf[0], KSSIZE);
Bram Moolenaarcc0f2be2018-02-23 18:23:30 +01006123 }
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006124 }
6125 }
6126
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006127 curr_mode = mode;
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006128 }
6129# endif
6130}
6131
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132#endif
Bram Moolenaarab302212016-04-26 20:59:29 +02006133
Bram Moolenaar61be73b2016-04-29 22:59:22 +02006134#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
Bram Moolenaarab302212016-04-26 20:59:29 +02006135 static int
6136hex_digit(int c)
6137{
6138 if (isdigit(c))
6139 return c - '0';
6140 c = TOLOWER_ASC(c);
6141 if (c >= 'a' && c <= 'f')
6142 return c - 'a' + 10;
6143 return 0x1ffffff;
6144}
6145
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006146# ifdef VIMDLL
6147 static guicolor_T
6148gui_adjust_rgb(guicolor_T c)
6149{
6150 if (gui.in_use)
6151 return c;
6152 else
6153 return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff);
6154}
6155# else
6156# define gui_adjust_rgb(c) (c)
6157# endif
6158
Bram Moolenaarab302212016-04-26 20:59:29 +02006159 guicolor_T
6160gui_get_color_cmn(char_u *name)
6161{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006162 // On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
6163 // values as used by the MS-Windows GDI api. It should be used only for
6164 // MS-Windows GDI builds.
Bram Moolenaar4f974752019-02-17 17:44:42 +01006165# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
Bram Moolenaar28726652017-01-06 18:16:19 +01006166# undef RGB
6167# endif
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006168# ifndef RGB
6169# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
6170# endif
6171# define LINE_LEN 100
Bram Moolenaarab302212016-04-26 20:59:29 +02006172 FILE *fd;
6173 char line[LINE_LEN];
6174 char_u *fname;
6175 int r, g, b, i;
6176 guicolor_T color;
6177
6178 struct rgbcolor_table_S {
6179 char_u *color_name;
6180 guicolor_T color;
6181 };
6182
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006183 // Only non X11 colors (not present in rgb.txt) and colors in
6184 // color_names[], useful when $VIMRUNTIME is not found,.
Bram Moolenaarab302212016-04-26 20:59:29 +02006185 static struct rgbcolor_table_S rgb_table[] = {
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006186 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
6187 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
6188 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
6189 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
6190 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
6191 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
6192 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
6193 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
6194 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
6195 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
6196 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006197 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, // No X11
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006198 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006199 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
6200 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar21477462016-08-08 20:43:27 +02006201 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
Bram Moolenaarecadf432018-03-20 11:17:04 +01006202 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006203 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006204 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
6205 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
6206 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
6207 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
6208 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006209 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, // No X11
6210 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, // No X11
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006211 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
6212 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006213 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaarca8942c2016-07-19 23:36:31 +02006214 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006215 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
6216 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaarab302212016-04-26 20:59:29 +02006217 };
6218
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006219 static struct rgbcolor_table_S *colornames_table;
6220 static int size = 0;
Bram Moolenaarab302212016-04-26 20:59:29 +02006221
6222 if (name[0] == '#' && STRLEN(name) == 7)
6223 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006224 // Name is in "#rrggbb" format
Bram Moolenaar283ee8b2016-04-27 20:36:31 +02006225 color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
Bram Moolenaarab302212016-04-26 20:59:29 +02006226 ((hex_digit(name[3]) << 4) + hex_digit(name[4])),
6227 ((hex_digit(name[5]) << 4) + hex_digit(name[6])));
6228 if (color > 0xffffff)
6229 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006230 return gui_adjust_rgb(color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006231 }
6232
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006233 // Check if the name is one of the colors we know
Bram Moolenaarab302212016-04-26 20:59:29 +02006234 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
6235 if (STRICMP(name, rgb_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006236 return gui_adjust_rgb(rgb_table[i].color);
Bram Moolenaarab302212016-04-26 20:59:29 +02006237
6238 /*
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006239 * Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
Bram Moolenaarab302212016-04-26 20:59:29 +02006240 */
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006241 if (size == 0)
Bram Moolenaarab302212016-04-26 20:59:29 +02006242 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006243 int counting;
Bram Moolenaarab302212016-04-26 20:59:29 +02006244
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006245 // colornames_table not yet initialized
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006246 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
6247 if (fname == NULL)
6248 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006249
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006250 fd = fopen((char *)fname, "rt");
6251 vim_free(fname);
6252 if (fd == NULL)
Bram Moolenaarab302212016-04-26 20:59:29 +02006253 {
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006254 if (p_verbose > 1)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006255 verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006256 size = -1; // don't try again
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006257 return INVALCOLOR;
Bram Moolenaarab302212016-04-26 20:59:29 +02006258 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006259
6260 for (counting = 1; counting >= 0; --counting)
6261 {
6262 if (!counting)
6263 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006264 colornames_table = ALLOC_MULT(struct rgbcolor_table_S, size);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006265 if (colornames_table == NULL)
6266 {
6267 fclose(fd);
6268 return INVALCOLOR;
6269 }
6270 rewind(fd);
6271 }
6272 size = 0;
6273
6274 while (!feof(fd))
6275 {
6276 size_t len;
6277 int pos;
6278
Bram Moolenaar42335f52018-09-13 15:33:43 +02006279 vim_ignoredp = fgets(line, LINE_LEN, fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006280 len = strlen(line);
6281
6282 if (len <= 1 || line[len - 1] != '\n')
6283 continue;
6284
6285 line[len - 1] = '\0';
6286
6287 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
6288 if (i != 3)
6289 continue;
6290
6291 if (!counting)
6292 {
6293 char_u *s = vim_strsave((char_u *)line + pos);
6294
6295 if (s == NULL)
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006296 {
6297 fclose(fd);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006298 return INVALCOLOR;
Bram Moolenaar2e45d212016-07-22 22:12:38 +02006299 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006300 colornames_table[size].color_name = s;
6301 colornames_table[size].color = (guicolor_T)RGB(r, g, b);
6302 }
6303 size++;
Bram Moolenaar0ea21e42019-02-12 20:46:48 +01006304
6305 // The distributed rgb.txt has less than 1000 entries. Limit to
6306 // 10000, just in case the file was messed up.
6307 if (size == 10000)
6308 break;
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006309 }
6310 }
6311 fclose(fd);
Bram Moolenaarab302212016-04-26 20:59:29 +02006312 }
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006313
6314 for (i = 0; i < size; i++)
6315 if (STRICMP(name, colornames_table[i].color_name) == 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006316 return gui_adjust_rgb(colornames_table[i].color);
Bram Moolenaar68015bb2016-07-19 21:05:21 +02006317
Bram Moolenaarab302212016-04-26 20:59:29 +02006318 return INVALCOLOR;
6319}
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006320
6321 guicolor_T
6322gui_get_rgb_color_cmn(int r, int g, int b)
6323{
6324 guicolor_T color = RGB(r, g, b);
6325
6326 if (color > 0xffffff)
6327 return INVALCOLOR;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006328 return gui_adjust_rgb(color);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02006329}
Bram Moolenaarab302212016-04-26 20:59:29 +02006330#endif
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006331
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006332#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006333 || defined(PROTO)
6334static int cube_value[] = {
6335 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF
6336};
6337
6338static int grey_ramp[] = {
6339 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
6340 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
6341};
6342
Bram Moolenaar9894e392018-05-05 14:29:06 +02006343static char_u ansi_table[16][4] = {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006344// R G B idx
6345 { 0, 0, 0, 1}, // black
6346 {224, 0, 0, 2}, // dark red
6347 { 0, 224, 0, 3}, // dark green
6348 {224, 224, 0, 4}, // dark yellow / brown
6349 { 0, 0, 224, 5}, // dark blue
6350 {224, 0, 224, 6}, // dark magenta
6351 { 0, 224, 224, 7}, // dark cyan
6352 {224, 224, 224, 8}, // light grey
6353
6354 {128, 128, 128, 9}, // dark grey
6355 {255, 64, 64, 10}, // light red
6356 { 64, 255, 64, 11}, // light green
6357 {255, 255, 64, 12}, // yellow
6358 { 64, 64, 255, 13}, // light blue
6359 {255, 64, 255, 14}, // light magenta
6360 { 64, 255, 255, 15}, // light cyan
6361 {255, 255, 255, 16}, // white
6362};
6363
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006364#define ANSI_INDEX_NONE 0
6365
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006366 void
Bram Moolenaar9894e392018-05-05 14:29:06 +02006367cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006368{
6369 int idx;
6370
6371 if (nr < 16)
6372 {
6373 *r = ansi_table[nr][0];
6374 *g = ansi_table[nr][1];
6375 *b = ansi_table[nr][2];
6376 *ansi_idx = ansi_table[nr][3];
6377 }
6378 else if (nr < 232)
6379 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006380 // 216 color cube
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006381 idx = nr - 16;
6382 *r = cube_value[idx / 36 % 6];
6383 *g = cube_value[idx / 6 % 6];
6384 *b = cube_value[idx % 6];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006385 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006386 }
6387 else if (nr < 256)
6388 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006389 // 24 grey scale ramp
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006390 idx = nr - 232;
6391 *r = grey_ramp[idx];
6392 *g = grey_ramp[idx];
6393 *b = grey_ramp[idx];
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006394 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006395 }
6396 else
6397 {
6398 *r = 0;
6399 *g = 0;
6400 *b = 0;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006401 *ansi_idx = ANSI_INDEX_NONE;
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02006402 }
6403}
6404#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006405
Bram Moolenaarf4140482020-02-15 23:06:45 +01006406/*
6407 * Replace K_BS by <BS> and K_DEL by <DEL>
6408 */
6409 void
6410term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len)
6411{
6412 int i;
6413 int c;
6414
6415 for (i = ta_len; i < ta_len + len; ++i)
6416 {
6417 if (ta_buf[i] == CSI && len - i > 2)
6418 {
6419 c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6420 if (c == K_DEL || c == K_KDEL || c == K_BS)
6421 {
6422 mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
6423 (size_t)(len - i - 2));
6424 if (c == K_DEL || c == K_KDEL)
6425 ta_buf[i] = DEL;
6426 else
6427 ta_buf[i] = Ctrl_H;
6428 len -= 2;
6429 }
6430 }
6431 else if (ta_buf[i] == '\r')
6432 ta_buf[i] = '\n';
6433 if (has_mbyte)
6434 i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6435 }
6436}