blob: f66236725ac8384302c820a56b46fcbc64e018af [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 */
24
25#define tgetstr tgetstr_defined_wrong
26#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
30# include <termios.h> /* seems to be required for some Linux */
31# 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
74/* start of keys that are not directly used by Vim but can be mapped */
75#define BT_EXTRA_KEYS 0x101
76
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010077static struct builtin_term *find_builtin_term(char_u *name);
78static void parse_builtin_tcap(char_u *s);
79static void term_color(char_u *s, int n);
Bram Moolenaar8a633e32016-04-21 21:10:14 +020080#ifdef FEAT_TERMTRUECOLOR
81static void term_rgb_color(char_u *s, long_u rgb);
82#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010083static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010085static void req_codes_from_term(void);
86static void req_more_codes_from_term(void);
87static void got_code_from_term(char_u *code, int len);
88static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#endif
90#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +000091 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
92 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010093static int get_bytes_from_buf(char_u *, char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010095static void del_termcode_idx(int idx);
96static int term_is_builtin(char_u *name);
97static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010099static void switch_to_8bit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#endif
101
102#ifdef HAVE_TGETENT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100103static char_u *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
105/*
106 * Here is our own prototype for tgetstr(), any prototypes from the include
107 * files have been disabled by the define at the start of this file.
108 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100109char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
111# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200112 /* Change this to "if 1" to debug what happens with termresponse. */
113# if 0
114# define DEBUG_TERMRESPONSE
115 static void log_tr(char *msg);
116# define LOG_TR(msg) log_tr(msg)
117# else
118# define LOG_TR(msg)
119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120/* Request Terminal Version status: */
121# define CRV_GET 1 /* send T_CRV when switched to RAW mode */
122# define CRV_SENT 2 /* did send T_CRV, waiting for answer */
123# define CRV_GOT 3 /* received T_CRV response */
124static int crv_status = CRV_GET;
Bram Moolenaar9584b312013-03-13 19:29:28 +0100125/* Request Cursor position report: */
126# define U7_GET 1 /* send T_U7 when switched to RAW mode */
127# define U7_SENT 2 /* did send T_U7, waiting for answer */
128# define U7_GOT 3 /* received T_U7 response */
129static int u7_status = U7_GET;
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200130/* Request background color report: */
131# define RBG_GET 1 /* send T_RBG when switched to RAW mode */
132# define RBG_SENT 2 /* did send T_RBG, waiting for answer */
133# define RBG_GOT 3 /* received T_RBG response */
134static int rbg_status = RBG_GET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135# endif
136
137/*
138 * Don't declare these variables if termcap.h contains them.
139 * Autoconf checks if these variables should be declared extern (not all
140 * systems have them).
141 * Some versions define ospeed to be speed_t, but that is incompatible with
142 * BSD, where ospeed is short and speed_t is long.
143 */
144# ifndef HAVE_OSPEED
145# ifdef OSPEED_EXTERN
146extern short ospeed;
147# else
148short ospeed;
149# endif
150# endif
151# ifndef HAVE_UP_BC_PC
152# ifdef UP_BC_PC_EXTERN
153extern char *UP, *BC, PC;
154# else
155char *UP, *BC, PC;
156# endif
157# endif
158
159# define TGETSTR(s, p) vim_tgetstr((s), (p))
160# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100161static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif /* HAVE_TGETENT */
163
164static int detected_8bit = FALSE; /* detected 8-bit terminal */
165
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000166static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167{
168
169#if defined(FEAT_GUI)
170/*
171 * GUI pseudo term-cap.
172 */
173 {(int)KS_NAME, "gui"},
174 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
175 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
176# ifdef TERMINFO
177 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
178# else
179 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
180# endif
181 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
182# ifdef TERMINFO
183 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
184 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100185# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
187# endif
188# else
189 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
190 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100191# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
193# endif
194# endif
195 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
196 /* attributes switched on with 'h', off with * 'H' */
197 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
198 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
199 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
200 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
201 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
202 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
203 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000204 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
205 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
207 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
208 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
209 {(int)KS_MS, "y"},
210 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100211 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 {(int)KS_LE, "\b"}, /* cursor-left = BS */
213 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
214# ifdef TERMINFO
215 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
216# else
217 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
218# endif
219 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000220 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#endif
222
223#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
225# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
226/*
227 * Amiga console window, default for Amiga
228 */
229 {(int)KS_NAME, "amiga"},
230 {(int)KS_CE, "\033[K"},
231 {(int)KS_CD, "\033[J"},
232 {(int)KS_AL, "\033[L"},
233# ifdef TERMINFO
234 {(int)KS_CAL, "\033[%p1%dL"},
235# else
236 {(int)KS_CAL, "\033[%dL"},
237# endif
238 {(int)KS_DL, "\033[M"},
239# ifdef TERMINFO
240 {(int)KS_CDL, "\033[%p1%dM"},
241# else
242 {(int)KS_CDL, "\033[%dM"},
243# endif
244 {(int)KS_CL, "\014"},
245 {(int)KS_VI, "\033[0 p"},
246 {(int)KS_VE, "\033[1 p"},
247 {(int)KS_ME, "\033[0m"},
248 {(int)KS_MR, "\033[7m"},
249 {(int)KS_MD, "\033[1m"},
250 {(int)KS_SE, "\033[0m"},
251 {(int)KS_SO, "\033[33m"},
252 {(int)KS_US, "\033[4m"},
253 {(int)KS_UE, "\033[0m"},
254 {(int)KS_CZH, "\033[3m"},
255 {(int)KS_CZR, "\033[0m"},
256#if defined(__MORPHOS__) || defined(__AROS__)
257 {(int)KS_CCO, "8"}, /* allow 8 colors */
258# ifdef TERMINFO
259 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
260 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
261# else
262 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
263 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
264# endif
265 {(int)KS_OP, "\033[m"}, /* reset colors */
266#endif
267 {(int)KS_MS, "y"},
268 {(int)KS_UT, "y"}, /* guessed */
269 {(int)KS_LE, "\b"},
270# ifdef TERMINFO
271 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
272# else
273 {(int)KS_CM, "\033[%i%d;%dH"},
274# endif
275#if defined(__MORPHOS__)
276 {(int)KS_SR, "\033M"},
277#endif
278# ifdef TERMINFO
279 {(int)KS_CRI, "\033[%p1%dC"},
280# else
281 {(int)KS_CRI, "\033[%dC"},
282# endif
283 {K_UP, "\233A"},
284 {K_DOWN, "\233B"},
285 {K_LEFT, "\233D"},
286 {K_RIGHT, "\233C"},
287 {K_S_UP, "\233T"},
288 {K_S_DOWN, "\233S"},
289 {K_S_LEFT, "\233 A"},
290 {K_S_RIGHT, "\233 @"},
291 {K_S_TAB, "\233Z"},
292 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
293 {K_F2, "\233\061~"},
294 {K_F3, "\233\062~"},
295 {K_F4, "\233\063~"},
296 {K_F5, "\233\064~"},
297 {K_F6, "\233\065~"},
298 {K_F7, "\233\066~"},
299 {K_F8, "\233\067~"},
300 {K_F9, "\233\070~"},
301 {K_F10, "\233\071~"},
302 {K_S_F1, "\233\061\060~"},
303 {K_S_F2, "\233\061\061~"},
304 {K_S_F3, "\233\061\062~"},
305 {K_S_F4, "\233\061\063~"},
306 {K_S_F5, "\233\061\064~"},
307 {K_S_F6, "\233\061\065~"},
308 {K_S_F7, "\233\061\066~"},
309 {K_S_F8, "\233\061\067~"},
310 {K_S_F9, "\233\061\070~"},
311 {K_S_F10, "\233\061\071~"},
312 {K_HELP, "\233?~"},
313 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
314 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
315 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
316 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
317 {K_END, "\233\064\065~"}, /* 101 key keyboard */
318
319 {BT_EXTRA_KEYS, ""},
320 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
321 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
322 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
323# endif
324
325# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
326/*
327 * almost standard ANSI terminal, default for bebox
328 */
329 {(int)KS_NAME, "beos-ansi"},
330 {(int)KS_CE, "\033[K"},
331 {(int)KS_CD, "\033[J"},
332 {(int)KS_AL, "\033[L"},
333# ifdef TERMINFO
334 {(int)KS_CAL, "\033[%p1%dL"},
335# else
336 {(int)KS_CAL, "\033[%dL"},
337# endif
338 {(int)KS_DL, "\033[M"},
339# ifdef TERMINFO
340 {(int)KS_CDL, "\033[%p1%dM"},
341# else
342 {(int)KS_CDL, "\033[%dM"},
343# endif
344#ifdef BEOS_PR_OR_BETTER
345# ifdef TERMINFO
346 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
347# else
348 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
349# endif
350#endif
351 {(int)KS_CL, "\033[H\033[2J"},
352#ifdef notyet
353 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
354 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
355#endif
356 {(int)KS_ME, "\033[m"}, /* normal mode */
357 {(int)KS_MR, "\033[7m"}, /* reverse */
358 {(int)KS_MD, "\033[1m"}, /* bold */
359 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
360 {(int)KS_SE, "\033[m"}, /* standout end */
361 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
362 {(int)KS_CZR, "\033[m"}, /* italic end */
363 {(int)KS_US, "\033[4m"}, /* underscore mode */
364 {(int)KS_UE, "\033[m"}, /* underscore end */
365 {(int)KS_CCO, "8"}, /* allow 8 colors */
366# ifdef TERMINFO
367 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
368 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
369# else
370 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
371 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
372# endif
373 {(int)KS_OP, "\033[m"}, /* reset colors */
374 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
375 {(int)KS_UT, "y"}, /* guessed */
376 {(int)KS_LE, "\b"},
377# ifdef TERMINFO
378 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
379# else
380 {(int)KS_CM, "\033[%i%d;%dH"},
381# endif
382 {(int)KS_SR, "\033M"},
383# ifdef TERMINFO
384 {(int)KS_CRI, "\033[%p1%dC"},
385# else
386 {(int)KS_CRI, "\033[%dC"},
387# endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200388# if defined(BEOS_DR8)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {(int)KS_DB, ""}, /* hack! see screen.c */
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
392 {K_UP, "\033[A"},
393 {K_DOWN, "\033[B"},
394 {K_LEFT, "\033[D"},
395 {K_RIGHT, "\033[C"},
396# endif
397
398# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) || defined(__EMX__)
399/*
400 * standard ANSI terminal, default for unix
401 */
402 {(int)KS_NAME, "ansi"},
403 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
404 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
405# ifdef TERMINFO
406 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
407# else
408 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
409# endif
410 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
411# ifdef TERMINFO
412 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
413# else
414 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
415# endif
416 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
417 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
418 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
419 {(int)KS_MS, "y"},
420 {(int)KS_UT, "y"}, /* guessed */
421 {(int)KS_LE, "\b"},
422# ifdef TERMINFO
423 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
424# else
425 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
426# endif
427# ifdef TERMINFO
428 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
429# else
430 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
431# endif
432# endif
433
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100434# if defined(ALL_BUILTIN_TCAPS) || defined(__EMX__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435/*
436 * These codes are valid when nansi.sys or equivalent has been installed.
437 * Function keys on a PC are preceded with a NUL. These are converted into
438 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
439 * CTRL-arrow is used instead of SHIFT-arrow.
440 */
441#ifdef __EMX__
442 {(int)KS_NAME, "os2ansi"},
443#else
444 {(int)KS_NAME, "pcansi"},
445 {(int)KS_DL, "\033[M"},
446 {(int)KS_AL, "\033[L"},
447#endif
448 {(int)KS_CE, "\033[K"},
449 {(int)KS_CL, "\033[2J"},
450 {(int)KS_ME, "\033[0m"},
451 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
452 {(int)KS_MD, "\033[1m"}, /* bold: white text */
453 {(int)KS_SE, "\033[0m"}, /* standout end */
454 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
455 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
456 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
457 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
458 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
459 {(int)KS_CCO, "8"}, /* allow 8 colors */
460# ifdef TERMINFO
461 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
462 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
463# else
464 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
465 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
466# endif
467 {(int)KS_OP, "\033[0m"}, /* reset colors */
468 {(int)KS_MS, "y"},
469 {(int)KS_UT, "y"}, /* guessed */
470 {(int)KS_LE, "\b"},
471# ifdef TERMINFO
472 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
473# else
474 {(int)KS_CM, "\033[%i%d;%dH"},
475# endif
476# ifdef TERMINFO
477 {(int)KS_CRI, "\033[%p1%dC"},
478# else
479 {(int)KS_CRI, "\033[%dC"},
480# endif
481 {K_UP, "\316H"},
482 {K_DOWN, "\316P"},
483 {K_LEFT, "\316K"},
484 {K_RIGHT, "\316M"},
485 {K_S_LEFT, "\316s"},
486 {K_S_RIGHT, "\316t"},
487 {K_F1, "\316;"},
488 {K_F2, "\316<"},
489 {K_F3, "\316="},
490 {K_F4, "\316>"},
491 {K_F5, "\316?"},
492 {K_F6, "\316@"},
493 {K_F7, "\316A"},
494 {K_F8, "\316B"},
495 {K_F9, "\316C"},
496 {K_F10, "\316D"},
497 {K_F11, "\316\205"}, /* guessed */
498 {K_F12, "\316\206"}, /* guessed */
499 {K_S_F1, "\316T"},
500 {K_S_F2, "\316U"},
501 {K_S_F3, "\316V"},
502 {K_S_F4, "\316W"},
503 {K_S_F5, "\316X"},
504 {K_S_F6, "\316Y"},
505 {K_S_F7, "\316Z"},
506 {K_S_F8, "\316["},
507 {K_S_F9, "\316\\"},
508 {K_S_F10, "\316]"},
509 {K_S_F11, "\316\207"}, /* guessed */
510 {K_S_F12, "\316\210"}, /* guessed */
511 {K_INS, "\316R"},
512 {K_DEL, "\316S"},
513 {K_HOME, "\316G"},
514 {K_END, "\316O"},
515 {K_PAGEDOWN, "\316Q"},
516 {K_PAGEUP, "\316I"},
517# endif
518
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS) || defined(__EMX__)
520/*
521 * These codes are valid for the Win32 Console . The entries that start with
522 * ESC | are translated into console calls in os_win32.c. The function keys
523 * are also translated in os_win32.c.
524 */
525 {(int)KS_NAME, "win32"},
526 {(int)KS_CE, "\033|K"}, /* clear to end of line */
527 {(int)KS_AL, "\033|L"}, /* add new blank line */
528# ifdef TERMINFO
529 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
530# else
531 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
532# endif
533 {(int)KS_DL, "\033|M"}, /* delete line */
534# ifdef TERMINFO
535 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
536# else
537 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
538# endif
539 {(int)KS_CL, "\033|J"}, /* clear screen */
540 {(int)KS_CD, "\033|j"}, /* clear to end of display */
541 {(int)KS_VI, "\033|v"}, /* cursor invisible */
542 {(int)KS_VE, "\033|V"}, /* cursor visible */
543
544 {(int)KS_ME, "\033|0m"}, /* normal */
545 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
546 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
547#if 1
548 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
549 {(int)KS_SE, "\033|0m"}, /* standout end */
550#else
551 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
552 {(int)KS_SE, "\033|f"}, /* standout end */
553#endif
554 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
555 {(int)KS_CZR, "\033|0m"}, /* italic end */
556 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
557 {(int)KS_UE, "\033|0m"}, /* underscore end */
558 {(int)KS_CCO, "16"}, /* allow 16 colors */
559# ifdef TERMINFO
560 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
561 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
562# else
563 {(int)KS_CAB, "\033|%db"}, /* set background color */
564 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
565# endif
566
567 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
568 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100569 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {(int)KS_LE, "\b"},
571# ifdef TERMINFO
572 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
573# else
574 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
575# endif
576 {(int)KS_VB, "\033|B"}, /* visual bell */
577 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
578 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
579# ifdef TERMINFO
580 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
581# else
582 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
583# endif
584
585 {K_UP, "\316H"},
586 {K_DOWN, "\316P"},
587 {K_LEFT, "\316K"},
588 {K_RIGHT, "\316M"},
589 {K_S_UP, "\316\304"},
590 {K_S_DOWN, "\316\317"},
591 {K_S_LEFT, "\316\311"},
592 {K_C_LEFT, "\316s"},
593 {K_S_RIGHT, "\316\313"},
594 {K_C_RIGHT, "\316t"},
595 {K_S_TAB, "\316\017"},
596 {K_F1, "\316;"},
597 {K_F2, "\316<"},
598 {K_F3, "\316="},
599 {K_F4, "\316>"},
600 {K_F5, "\316?"},
601 {K_F6, "\316@"},
602 {K_F7, "\316A"},
603 {K_F8, "\316B"},
604 {K_F9, "\316C"},
605 {K_F10, "\316D"},
606 {K_F11, "\316\205"},
607 {K_F12, "\316\206"},
608 {K_S_F1, "\316T"},
609 {K_S_F2, "\316U"},
610 {K_S_F3, "\316V"},
611 {K_S_F4, "\316W"},
612 {K_S_F5, "\316X"},
613 {K_S_F6, "\316Y"},
614 {K_S_F7, "\316Z"},
615 {K_S_F8, "\316["},
616 {K_S_F9, "\316\\"},
617 {K_S_F10, "\316]"},
618 {K_S_F11, "\316\207"},
619 {K_S_F12, "\316\210"},
620 {K_INS, "\316R"},
621 {K_DEL, "\316S"},
622 {K_HOME, "\316G"},
623 {K_S_HOME, "\316\302"},
624 {K_C_HOME, "\316w"},
625 {K_END, "\316O"},
626 {K_S_END, "\316\315"},
627 {K_C_END, "\316u"},
628 {K_PAGEDOWN, "\316Q"},
629 {K_PAGEUP, "\316I"},
630 {K_KPLUS, "\316N"},
631 {K_KMINUS, "\316J"},
632 {K_KMULTIPLY, "\316\067"},
633 {K_K0, "\316\332"},
634 {K_K1, "\316\336"},
635 {K_K2, "\316\342"},
636 {K_K3, "\316\346"},
637 {K_K4, "\316\352"},
638 {K_K5, "\316\356"},
639 {K_K6, "\316\362"},
640 {K_K7, "\316\366"},
641 {K_K8, "\316\372"},
642 {K_K9, "\316\376"},
643# endif
644
645# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
646/*
647 * VT320 is working as an ANSI terminal compatible DEC terminal.
648 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
649 * Note: K_F1...K_F5 are for internal use, should not be defined.
650 * TODO:- rewrite ESC[ codes to CSI
651 * - keyboard languages (CSI ? 26 n)
652 */
653 {(int)KS_NAME, "vt320"},
654 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
655 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
656# ifdef TERMINFO
657 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
658# else
659 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
660# endif
661 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
662# ifdef TERMINFO
663 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
664# else
665 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
666# endif
667 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000668 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
669 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
671 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000672 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
673 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
674 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
675 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
676 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
677 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
678 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
679 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
680 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
681 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {(int)KS_MS, "y"},
683 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100684 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {(int)KS_LE, "\b"},
686# ifdef TERMINFO
687 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
688 ESC_STR "[%i%p1%d;%p2%dH")},
689# else
690 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
691# endif
692# ifdef TERMINFO
693 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
694# else
695 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
696# endif
697 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
698 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
699 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
700 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000701 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
702 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
703 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
704 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
705 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
707 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
708 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
709 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
710 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000711 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
713 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
714 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
715 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
716 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
717 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
718 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
719 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
720 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
721 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
722 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
723 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
724 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
725 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
726 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
727 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
728 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
729 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
730 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
731 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
732 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
733# endif
734
735# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
736/*
737 * Ordinary vt52
738 */
739 {(int)KS_NAME, "vt52"},
740 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
741 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100742# ifdef TERMINFO
743 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
744 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
745# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100747# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100749 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
751 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 {K_UP, IF_EB("\033A", ESC_STR "A")},
753 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
754 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
755 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100756 {K_F1, IF_EB("\033P", ESC_STR "P")},
757 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
758 {K_F3, IF_EB("\033R", ESC_STR "R")},
759# ifdef __MINT__
760 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
761 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
762 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
763 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
764 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
766 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
767 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
768 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 {K_F4, IF_EB("\033S", ESC_STR "S")},
770 {K_F5, IF_EB("\033T", ESC_STR "T")},
771 {K_F6, IF_EB("\033U", ESC_STR "U")},
772 {K_F7, IF_EB("\033V", ESC_STR "V")},
773 {K_F8, IF_EB("\033W", ESC_STR "W")},
774 {K_F9, IF_EB("\033X", ESC_STR "X")},
775 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
776 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
777 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
778 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
779 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
780 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
781 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
782 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
783 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
784 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
785 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
786 {K_INS, IF_EB("\033I", ESC_STR "I")},
787 {K_HOME, IF_EB("\033E", ESC_STR "E")},
788 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
789 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
790# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {(int)KS_MS, "y"},
793# endif
794# endif
795
Bram Moolenaar8a633e32016-04-21 21:10:14 +0200796# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) || defined(__EMX__)
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200797 {(int)KS_NAME, "xterm"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
799 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
800# ifdef TERMINFO
801 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
802# else
803 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
804# endif
805 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
806# ifdef TERMINFO
807 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
808# else
809 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
810# endif
811# ifdef TERMINFO
812 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
813 ESC_STR "[%i%p1%d;%p2%dr")},
814# else
815 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
816# endif
817 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
818 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
819 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
820 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
821 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
822 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
823 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
824 {(int)KS_MS, "y"},
825 {(int)KS_UT, "y"},
826 {(int)KS_LE, "\b"},
827# ifdef TERMINFO
828 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
829 ESC_STR "[%i%p1%d;%p2%dH")},
830# else
831 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
832# endif
833 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
834# ifdef TERMINFO
835 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
836# else
837 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
838# endif
839 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
840 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
841# ifdef FEAT_XTERM_SAVE
842 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
843 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
844 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
845# endif
846 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
847 {(int)KS_CIE, "\007"},
848 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
849 {(int)KS_FS, "\007"},
850# ifdef TERMINFO
851 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
852 ESC_STR "[8;%p1%d;%p2%dt")},
853 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
854 ESC_STR "[3;%p1%d;%p2%dt")},
855# else
856 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
857 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
858# endif
859 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200860 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100861 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaarb2fa54a2016-04-22 21:11:09 +0200862# ifdef FEAT_TERMTRUECOLOR
863 /* These are printf strings, not terminal codes. */
864 {(int)KS_8F, IF_EB("\033[38;2;%lu;%lu;%lum", ESC_STR "[38;2;%lu;%lu;%lum")},
865 {(int)KS_8B, IF_EB("\033[48;2;%lu;%lu;%lum", ESC_STR "[48;2;%lu;%lu;%lum")},
866# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000867
868 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
869 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
870 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
871 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
872 /* An extra set of cursor keys for vt100 mode */
873 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
874 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
875 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
876 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000878 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
879 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
880 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
881 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000882 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
883 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
884 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
885 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
886 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
887 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
888 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
889 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
890 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
891 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
892 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
893 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000895 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
896 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
897 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
898 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000899 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
900 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000901 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000902 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000903 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000904 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000905 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
906 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000907 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000908 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000909 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000910 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
911 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000912 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
913 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
914 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
915 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
916 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
917 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000918 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919
920 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000921 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
922 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
923 /* F14 and F15 are missing, because they send the same codes as the undo
924 * and help key, although they don't work on all keyboards. */
925 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
926 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
927 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
928 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
929 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
930
931 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
932 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
933 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
934 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
935 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
936 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
937 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
938 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
939 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
940 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
941
942 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
943 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
944 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
945 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
946 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
947 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
948 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949# endif
950
951# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
952/*
953 * iris-ansi for Silicon Graphics machines.
954 */
955 {(int)KS_NAME, "iris-ansi"},
956 {(int)KS_CE, "\033[K"},
957 {(int)KS_CD, "\033[J"},
958 {(int)KS_AL, "\033[L"},
959# ifdef TERMINFO
960 {(int)KS_CAL, "\033[%p1%dL"},
961# else
962 {(int)KS_CAL, "\033[%dL"},
963# endif
964 {(int)KS_DL, "\033[M"},
965# ifdef TERMINFO
966 {(int)KS_CDL, "\033[%p1%dM"},
967# else
968 {(int)KS_CDL, "\033[%dM"},
969# endif
970#if 0 /* The scroll region is not working as Vim expects. */
971# ifdef TERMINFO
972 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
973# else
974 {(int)KS_CS, "\033[%i%d;%dr"},
975# endif
976#endif
977 {(int)KS_CL, "\033[H\033[2J"},
978 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
979 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
980 {(int)KS_TI, "\033[=6h"},
981 {(int)KS_TE, "\033[=6l"},
982 {(int)KS_SE, "\033[21;27m"},
983 {(int)KS_SO, "\033[1;7m"},
984 {(int)KS_ME, "\033[m"},
985 {(int)KS_MR, "\033[7m"},
986 {(int)KS_MD, "\033[1m"},
987 {(int)KS_CCO, "8"}, /* allow 8 colors */
988 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
989 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
990 {(int)KS_US, "\033[4m"}, /* underline on */
991 {(int)KS_UE, "\033[24m"}, /* underline off */
992# ifdef TERMINFO
993 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
994 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
995 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
996 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
997# else
998 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
999 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1000 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1001 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1002# endif
1003 {(int)KS_MS, "y"}, /* guessed */
1004 {(int)KS_UT, "y"}, /* guessed */
1005 {(int)KS_LE, "\b"},
1006# ifdef TERMINFO
1007 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1008# else
1009 {(int)KS_CM, "\033[%i%d;%dH"},
1010# endif
1011 {(int)KS_SR, "\033M"},
1012# ifdef TERMINFO
1013 {(int)KS_CRI, "\033[%p1%dC"},
1014# else
1015 {(int)KS_CRI, "\033[%dC"},
1016# endif
1017 {(int)KS_CIS, "\033P3.y"},
1018 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1019 {(int)KS_TS, "\033P1.y"},
1020 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1021# ifdef TERMINFO
1022 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1023 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1024# else
1025 {(int)KS_CWS, "\033[203;%d;%d/y"},
1026 {(int)KS_CWP, "\033[205;%d;%d/y"},
1027# endif
1028 {K_UP, "\033[A"},
1029 {K_DOWN, "\033[B"},
1030 {K_LEFT, "\033[D"},
1031 {K_RIGHT, "\033[C"},
1032 {K_S_UP, "\033[161q"},
1033 {K_S_DOWN, "\033[164q"},
1034 {K_S_LEFT, "\033[158q"},
1035 {K_S_RIGHT, "\033[167q"},
1036 {K_F1, "\033[001q"},
1037 {K_F2, "\033[002q"},
1038 {K_F3, "\033[003q"},
1039 {K_F4, "\033[004q"},
1040 {K_F5, "\033[005q"},
1041 {K_F6, "\033[006q"},
1042 {K_F7, "\033[007q"},
1043 {K_F8, "\033[008q"},
1044 {K_F9, "\033[009q"},
1045 {K_F10, "\033[010q"},
1046 {K_F11, "\033[011q"},
1047 {K_F12, "\033[012q"},
1048 {K_S_F1, "\033[013q"},
1049 {K_S_F2, "\033[014q"},
1050 {K_S_F3, "\033[015q"},
1051 {K_S_F4, "\033[016q"},
1052 {K_S_F5, "\033[017q"},
1053 {K_S_F6, "\033[018q"},
1054 {K_S_F7, "\033[019q"},
1055 {K_S_F8, "\033[020q"},
1056 {K_S_F9, "\033[021q"},
1057 {K_S_F10, "\033[022q"},
1058 {K_S_F11, "\033[023q"},
1059 {K_S_F12, "\033[024q"},
1060 {K_INS, "\033[139q"},
1061 {K_HOME, "\033[H"},
1062 {K_END, "\033[146q"},
1063 {K_PAGEUP, "\033[150q"},
1064 {K_PAGEDOWN, "\033[154q"},
1065# endif
1066
1067# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1068/*
1069 * for debugging
1070 */
1071 {(int)KS_NAME, "debug"},
1072 {(int)KS_CE, "[CE]"},
1073 {(int)KS_CD, "[CD]"},
1074 {(int)KS_AL, "[AL]"},
1075# ifdef TERMINFO
1076 {(int)KS_CAL, "[CAL%p1%d]"},
1077# else
1078 {(int)KS_CAL, "[CAL%d]"},
1079# endif
1080 {(int)KS_DL, "[DL]"},
1081# ifdef TERMINFO
1082 {(int)KS_CDL, "[CDL%p1%d]"},
1083# else
1084 {(int)KS_CDL, "[CDL%d]"},
1085# endif
1086# ifdef TERMINFO
1087 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1088# else
1089 {(int)KS_CS, "[%dCS%d]"},
1090# endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001091# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092# ifdef TERMINFO
1093 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1094# else
1095 {(int)KS_CSV, "[%dCSV%d]"},
1096# endif
1097# endif
1098# ifdef TERMINFO
1099 {(int)KS_CAB, "[CAB%p1%d]"},
1100 {(int)KS_CAF, "[CAF%p1%d]"},
1101 {(int)KS_CSB, "[CSB%p1%d]"},
1102 {(int)KS_CSF, "[CSF%p1%d]"},
1103# else
1104 {(int)KS_CAB, "[CAB%d]"},
1105 {(int)KS_CAF, "[CAF%d]"},
1106 {(int)KS_CSB, "[CSB%d]"},
1107 {(int)KS_CSF, "[CSF%d]"},
1108# endif
1109 {(int)KS_OP, "[OP]"},
1110 {(int)KS_LE, "[LE]"},
1111 {(int)KS_CL, "[CL]"},
1112 {(int)KS_VI, "[VI]"},
1113 {(int)KS_VE, "[VE]"},
1114 {(int)KS_VS, "[VS]"},
1115 {(int)KS_ME, "[ME]"},
1116 {(int)KS_MR, "[MR]"},
1117 {(int)KS_MB, "[MB]"},
1118 {(int)KS_MD, "[MD]"},
1119 {(int)KS_SE, "[SE]"},
1120 {(int)KS_SO, "[SO]"},
1121 {(int)KS_UE, "[UE]"},
1122 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001123 {(int)KS_UCE, "[UCE]"},
1124 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {(int)KS_MS, "[MS]"},
1126 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001127 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128# ifdef TERMINFO
1129 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1130# else
1131 {(int)KS_CM, "[%dCM%d]"},
1132# endif
1133 {(int)KS_SR, "[SR]"},
1134# ifdef TERMINFO
1135 {(int)KS_CRI, "[CRI%p1%d]"},
1136# else
1137 {(int)KS_CRI, "[CRI%d]"},
1138# endif
1139 {(int)KS_VB, "[VB]"},
1140 {(int)KS_KS, "[KS]"},
1141 {(int)KS_KE, "[KE]"},
1142 {(int)KS_TI, "[TI]"},
1143 {(int)KS_TE, "[TE]"},
1144 {(int)KS_CIS, "[CIS]"},
1145 {(int)KS_CIE, "[CIE]"},
1146 {(int)KS_TS, "[TS]"},
1147 {(int)KS_FS, "[FS]"},
1148# ifdef TERMINFO
1149 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1150 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1151# else
1152 {(int)KS_CWS, "[%dCWS%d]"},
1153 {(int)KS_CWP, "[%dCWP%d]"},
1154# endif
1155 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001156 {(int)KS_U7, "[U7]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001157 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 {K_UP, "[KU]"},
1159 {K_DOWN, "[KD]"},
1160 {K_LEFT, "[KL]"},
1161 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001162 {K_XUP, "[xKU]"},
1163 {K_XDOWN, "[xKD]"},
1164 {K_XLEFT, "[xKL]"},
1165 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {K_S_UP, "[S-KU]"},
1167 {K_S_DOWN, "[S-KD]"},
1168 {K_S_LEFT, "[S-KL]"},
1169 {K_C_LEFT, "[C-KL]"},
1170 {K_S_RIGHT, "[S-KR]"},
1171 {K_C_RIGHT, "[C-KR]"},
1172 {K_F1, "[F1]"},
1173 {K_XF1, "[xF1]"},
1174 {K_F2, "[F2]"},
1175 {K_XF2, "[xF2]"},
1176 {K_F3, "[F3]"},
1177 {K_XF3, "[xF3]"},
1178 {K_F4, "[F4]"},
1179 {K_XF4, "[xF4]"},
1180 {K_F5, "[F5]"},
1181 {K_F6, "[F6]"},
1182 {K_F7, "[F7]"},
1183 {K_F8, "[F8]"},
1184 {K_F9, "[F9]"},
1185 {K_F10, "[F10]"},
1186 {K_F11, "[F11]"},
1187 {K_F12, "[F12]"},
1188 {K_S_F1, "[S-F1]"},
1189 {K_S_XF1, "[S-xF1]"},
1190 {K_S_F2, "[S-F2]"},
1191 {K_S_XF2, "[S-xF2]"},
1192 {K_S_F3, "[S-F3]"},
1193 {K_S_XF3, "[S-xF3]"},
1194 {K_S_F4, "[S-F4]"},
1195 {K_S_XF4, "[S-xF4]"},
1196 {K_S_F5, "[S-F5]"},
1197 {K_S_F6, "[S-F6]"},
1198 {K_S_F7, "[S-F7]"},
1199 {K_S_F8, "[S-F8]"},
1200 {K_S_F9, "[S-F9]"},
1201 {K_S_F10, "[S-F10]"},
1202 {K_S_F11, "[S-F11]"},
1203 {K_S_F12, "[S-F12]"},
1204 {K_HELP, "[HELP]"},
1205 {K_UNDO, "[UNDO]"},
1206 {K_BS, "[BS]"},
1207 {K_INS, "[INS]"},
1208 {K_KINS, "[KINS]"},
1209 {K_DEL, "[DEL]"},
1210 {K_KDEL, "[KDEL]"},
1211 {K_HOME, "[HOME]"},
1212 {K_S_HOME, "[C-HOME]"},
1213 {K_C_HOME, "[C-HOME]"},
1214 {K_KHOME, "[KHOME]"},
1215 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001216 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 {K_END, "[END]"},
1218 {K_S_END, "[C-END]"},
1219 {K_C_END, "[C-END]"},
1220 {K_KEND, "[KEND]"},
1221 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001222 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {K_PAGEUP, "[PAGEUP]"},
1224 {K_PAGEDOWN, "[PAGEDOWN]"},
1225 {K_KPAGEUP, "[KPAGEUP]"},
1226 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1227 {K_MOUSE, "[MOUSE]"},
1228 {K_KPLUS, "[KPLUS]"},
1229 {K_KMINUS, "[KMINUS]"},
1230 {K_KDIVIDE, "[KDIVIDE]"},
1231 {K_KMULTIPLY, "[KMULTIPLY]"},
1232 {K_KENTER, "[KENTER]"},
1233 {K_KPOINT, "[KPOINT]"},
1234 {K_K0, "[K0]"},
1235 {K_K1, "[K1]"},
1236 {K_K2, "[K2]"},
1237 {K_K3, "[K3]"},
1238 {K_K4, "[K4]"},
1239 {K_K5, "[K5]"},
1240 {K_K6, "[K6]"},
1241 {K_K7, "[K7]"},
1242 {K_K8, "[K8]"},
1243 {K_K9, "[K9]"},
1244# endif
1245
1246#endif /* NO_BUILTIN_TCAPS */
1247
1248/*
1249 * The most minimal terminal: only clear screen and cursor positioning
1250 * Always included.
1251 */
1252 {(int)KS_NAME, "dumb"},
1253 {(int)KS_CL, "\014"},
1254#ifdef TERMINFO
1255 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1256 ESC_STR "[%i%p1%d;%p2%dH")},
1257#else
1258 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1259#endif
1260
1261/*
1262 * end marker
1263 */
1264 {(int)KS_NAME, NULL}
1265
1266}; /* end of builtin_termcaps */
1267
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001268#if defined(FEAT_TERMTRUECOLOR) || defined(PROTO)
1269# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
1270struct rgbcolor_table_S {
1271 char_u *color_name;
1272 guicolor_T color;
1273};
Bram Moolenaar380130f2016-04-22 11:24:43 +02001274
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001275static struct rgbcolor_table_S rgb_table[] = {
1276 {(char_u *)"black", RGB(0x00, 0x00, 0x00)},
Bram Moolenaard10abe52016-04-24 15:41:33 +02001277 {(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
1278 {(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
1279 {(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
1280 {(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
1281 {(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
1282 {(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
1283 {(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
1284 {(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
1285 {(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
1286 {(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
1287 {(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, /* No X11 */
1288 {(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001289 {(char_u *)"gray10", RGB(0x1A, 0x1A, 0x1A)},
1290 {(char_u *)"gray20", RGB(0x33, 0x33, 0x33)},
1291 {(char_u *)"gray30", RGB(0x4D, 0x4D, 0x4D)},
1292 {(char_u *)"gray40", RGB(0x66, 0x66, 0x66)},
1293 {(char_u *)"gray50", RGB(0x7F, 0x7F, 0x7F)},
1294 {(char_u *)"gray60", RGB(0x99, 0x99, 0x99)},
1295 {(char_u *)"gray70", RGB(0xB3, 0xB3, 0xB3)},
1296 {(char_u *)"gray80", RGB(0xCC, 0xCC, 0xCC)},
1297 {(char_u *)"gray90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaard10abe52016-04-24 15:41:33 +02001298 {(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
1299 {(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001300 {(char_u *)"grey10", RGB(0x1A, 0x1A, 0x1A)},
1301 {(char_u *)"grey20", RGB(0x33, 0x33, 0x33)},
1302 {(char_u *)"grey30", RGB(0x4D, 0x4D, 0x4D)},
1303 {(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
1304 {(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
1305 {(char_u *)"grey60", RGB(0x99, 0x99, 0x99)},
1306 {(char_u *)"grey70", RGB(0xB3, 0xB3, 0xB3)},
1307 {(char_u *)"grey80", RGB(0xCC, 0xCC, 0xCC)},
1308 {(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
Bram Moolenaard10abe52016-04-24 15:41:33 +02001309 {(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
1310 {(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
1311 {(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
1312 {(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
1313 {(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
1314 {(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, /* No X11 */
1315 {(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, /* No X11 */
1316 {(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
1317 {(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
1318 {(char_u *)"orange", RGB(0xFF, 0xA5, 0x00)},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001319 {(char_u *)"purple", RGB(0xA0, 0x20, 0xF0)},
Bram Moolenaard10abe52016-04-24 15:41:33 +02001320 {(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001321 {(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
1322 {(char_u *)"slateblue", RGB(0x6A, 0x5A, 0xCD)},
Bram Moolenaard10abe52016-04-24 15:41:33 +02001323 {(char_u *)"violet", RGB(0xEE, 0x82, 0xEE)},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001324 {(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
Bram Moolenaard10abe52016-04-24 15:41:33 +02001325 {(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001326};
1327
1328 static int
1329hex_digit(int c)
1330{
1331 if (isdigit(c))
1332 return c - '0';
1333 c = TOLOWER_ASC(c);
1334 if (c >= 'a' && c <= 'f')
1335 return c - 'a' + 10;
1336 return 0x1ffffff;
1337}
1338
1339 guicolor_T
1340termtrue_mch_get_color(char_u *name)
1341{
1342 guicolor_T color;
1343 int i;
1344
1345 if (*name == '#' && strlen((char *) name) == 7)
1346 {
1347 color = RGB(((hex_digit(name[1])<<4) + hex_digit(name[2])),
1348 ((hex_digit(name[3])<<4) + hex_digit(name[4])),
1349 ((hex_digit(name[5])<<4) + hex_digit(name[6])));
1350 if (color > 0xffffff)
1351 return INVALCOLOR;
1352 return color;
1353 }
1354 else
1355 {
1356 /* Check if the name is one of the colors we know */
Bram Moolenaar380130f2016-04-22 11:24:43 +02001357 for (i = 0; i < (int)(sizeof(rgb_table) / sizeof(rgb_table[0])); i++)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001358 if (STRICMP(name, rgb_table[i].color_name) == 0)
1359 return rgb_table[i].color;
1360 }
1361
1362 /*
1363 * Last attempt. Look in the file "$VIM/rgb.txt".
1364 */
1365 {
1366#define LINE_LEN 100
1367 FILE *fd;
1368 char line[LINE_LEN];
1369 char_u *fname;
1370 int r, g, b;
1371
1372 fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
1373 if (fname == NULL)
1374 return INVALCOLOR;
1375
1376 fd = fopen((char *)fname, "rt");
1377 vim_free(fname);
1378 if (fd == NULL)
1379 return INVALCOLOR;
1380
1381 while (!feof(fd))
1382 {
1383 int len;
1384 int pos;
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001385
Bram Moolenaar902647d2016-04-22 11:49:06 +02001386 (void)fgets(line, LINE_LEN, fd);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001387 len = strlen(line);
1388
1389 if (len <= 1 || line[len-1] != '\n')
1390 continue;
1391
1392 line[len-1] = '\0';
1393
1394 i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
1395 if (i != 3)
1396 continue;
1397
Bram Moolenaar902647d2016-04-22 11:49:06 +02001398 if (STRICMP(line + pos, name) == 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001399 {
1400 fclose(fd);
1401 return (guicolor_T) RGB(r, g, b);
1402 }
1403 }
1404 fclose(fd);
1405 }
1406
1407 return INVALCOLOR;
1408}
1409
1410 guicolor_T
1411termtrue_get_color(char_u *name)
1412{
1413 guicolor_T t;
1414
1415 if (*name == NUL)
1416 return INVALCOLOR;
1417 t = termtrue_mch_get_color(name);
1418
1419 if (t == INVALCOLOR)
1420 EMSG2(_("E254: Cannot allocate color %s"), name);
1421 return t;
1422}
1423
1424 long_u
1425termtrue_mch_get_rgb(guicolor_T color)
1426{
1427 return (long_u) color;
1428}
1429#endif
1430
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431/*
1432 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434#ifdef AMIGA
1435# define DEFAULT_TERM (char_u *)"amiga"
1436#endif
1437
1438#ifdef MSWIN
1439# define DEFAULT_TERM (char_u *)"win32"
1440#endif
1441
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442#if defined(UNIX) && !defined(__MINT__)
1443# define DEFAULT_TERM (char_u *)"ansi"
1444#endif
1445
1446#ifdef __MINT__
1447# define DEFAULT_TERM (char_u *)"vt52"
1448#endif
1449
1450#ifdef __EMX__
1451# define DEFAULT_TERM (char_u *)"os2ansi"
1452#endif
1453
1454#ifdef VMS
1455# define DEFAULT_TERM (char_u *)"vt320"
1456#endif
1457
1458#ifdef __BEOS__
1459# undef DEFAULT_TERM
1460# define DEFAULT_TERM (char_u *)"beos-ansi"
1461#endif
1462
1463#ifndef DEFAULT_TERM
1464# define DEFAULT_TERM (char_u *)"dumb"
1465#endif
1466
1467/*
1468 * Term_strings contains currently used terminal output strings.
1469 * It is initialized with the default values by parse_builtin_tcap().
1470 * The values can be changed by setting the option with the same name.
1471 */
1472char_u *(term_strings[(int)KS_LAST + 1]);
1473
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001474static int need_gather = FALSE; /* need to fill termleader[] */
1475static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001477static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478#endif
1479
1480 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001481find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 struct builtin_term *p;
1484
1485 p = builtin_termcaps;
1486 while (p->bt_string != NULL)
1487 {
1488 if (p->bt_entry == (int)KS_NAME)
1489 {
1490#ifdef UNIX
1491 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1492 return p;
1493 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1494 return p;
1495 else
1496#endif
1497#ifdef VMS
1498 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1499 return p;
1500 else
1501#endif
1502 if (STRCMP(term, p->bt_string) == 0)
1503 return p;
1504 }
1505 ++p;
1506 }
1507 return p;
1508}
1509
1510/*
1511 * Parsing of the builtin termcap entries.
1512 * Caller should check if 'name' is a valid builtin term.
1513 * The terminal's name is not set, as this is already done in termcapinit().
1514 */
1515 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001516parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518 struct builtin_term *p;
1519 char_u name[2];
1520 int term_8bit;
1521
1522 p = find_builtin_term(term);
1523 term_8bit = term_is_8bit(term);
1524
1525 /* Do not parse if builtin term not found */
1526 if (p->bt_string == NULL)
1527 return;
1528
1529 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1530 {
1531 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1532 {
1533 /* Only set the value if it wasn't set yet. */
1534 if (term_strings[p->bt_entry] == NULL
1535 || term_strings[p->bt_entry] == empty_option)
1536 {
1537 /* 8bit terminal: use CSI instead of <Esc>[ */
1538 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1539 {
1540 char_u *s, *t;
1541
1542 s = vim_strsave((char_u *)p->bt_string);
1543 if (s != NULL)
1544 {
1545 for (t = s; *t; ++t)
1546 if (term_7to8bit(t))
1547 {
1548 *t = term_7to8bit(t);
1549 STRCPY(t + 1, t + 2);
1550 }
1551 term_strings[p->bt_entry] = s;
1552 set_term_option_alloced(&term_strings[p->bt_entry]);
1553 }
1554 }
1555 else
1556 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1557 }
1558 }
1559 else
1560 {
1561 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1562 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1563 if (find_termcode(name) == NULL)
1564 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1565 }
1566 }
1567}
1568#if defined(HAVE_TGETENT) || defined(FEAT_TERMRESPONSE)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001569static void set_color_count(int nr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
1571/*
1572 * Set number of colors.
1573 * Store it as a number in t_colors.
1574 * Store it as a string in T_CCO (using nr_colors[]).
1575 */
1576 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001577set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
1579 char_u nr_colors[20]; /* string for number of colors */
1580
1581 t_colors = nr;
1582 if (t_colors > 1)
1583 sprintf((char *)nr_colors, "%d", t_colors);
1584 else
1585 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001586 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587}
1588#endif
1589
1590#ifdef HAVE_TGETENT
1591static char *(key_names[]) =
1592{
1593#ifdef FEAT_TERMRESPONSE
1594 /* Do this one first, it may cause a screen redraw. */
1595 "Co",
1596#endif
1597 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 "#2", "#4", "%i", "*7",
1599 "k1", "k2", "k3", "k4", "k5", "k6",
1600 "k7", "k8", "k9", "k;", "F1", "F2",
1601 "%1", "&8", "kb", "kI", "kD", "kh",
1602 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1603 NULL
1604};
1605#endif
1606
1607/*
1608 * Set terminal options for terminal "term".
1609 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1610 *
1611 * While doing this, until ttest(), some options may be NULL, be careful.
1612 */
1613 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001614set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615{
1616 struct builtin_term *termp;
1617#ifdef HAVE_TGETENT
1618 int builtin_first = p_tbi;
1619 int try;
1620 int termcap_cleared = FALSE;
1621#endif
1622 int width = 0, height = 0;
1623 char_u *error_msg = NULL;
1624 char_u *bs_p, *del_p;
1625
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001626 /* In silect mode (ex -s) we don't use the 'term' option. */
1627 if (silent_mode)
1628 return OK;
1629
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 detected_8bit = FALSE; /* reset 8-bit detection */
1631
1632 if (term_is_builtin(term))
1633 {
1634 term += 8;
1635#ifdef HAVE_TGETENT
1636 builtin_first = 1;
1637#endif
1638 }
1639
1640/*
1641 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1642 * If builtin_first is TRUE:
1643 * 0. try builtin termcap
1644 * 1. try external termcap
1645 * 2. if both fail default to a builtin terminal
1646 * If builtin_first is FALSE:
1647 * 1. try external termcap
1648 * 2. try builtin termcap, if both fail default to a builtin terminal
1649 */
1650#ifdef HAVE_TGETENT
1651 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1652 {
1653 /*
1654 * Use external termcap
1655 */
1656 if (try == 1)
1657 {
1658 char_u *p;
1659 static char_u tstrbuf[TBUFSZ];
1660 int i;
1661 char_u tbuf[TBUFSZ];
1662 char_u *tp;
1663 static struct {
1664 enum SpecialKey dest; /* index in term_strings[] */
1665 char *name; /* termcap name for string */
1666 } string_names[] =
1667 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1668 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1669 {KS_CL, "cl"}, {KS_CD, "cd"},
1670 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1671 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1672 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1673 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001674 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1675 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1677 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1678 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1679 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1680 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1681 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1682 {KS_TS, "ts"}, {KS_FS, "fs"},
1683 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001684 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001685 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar8a633e32016-04-21 21:10:14 +02001686 {KS_8F, "8f"}, {KS_8B, "8b"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 {(enum SpecialKey)0, NULL}
1688 };
1689
1690 /*
1691 * If the external termcap does not have a matching entry, try the
1692 * builtin ones.
1693 */
1694 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1695 {
1696 tp = tstrbuf;
1697 if (!termcap_cleared)
1698 {
1699 clear_termoptions(); /* clear old options */
1700 termcap_cleared = TRUE;
1701 }
1702
1703 /* get output strings */
1704 for (i = 0; string_names[i].name != NULL; ++i)
1705 {
1706 if (term_str(string_names[i].dest) == NULL
1707 || term_str(string_names[i].dest) == empty_option)
1708 term_str(string_names[i].dest) =
1709 TGETSTR(string_names[i].name, &tp);
1710 }
1711
1712 /* tgetflag() returns 1 if the flag is present, 0 if not and
1713 * possibly -1 if the flag doesn't exist. */
1714 if ((T_MS == NULL || T_MS == empty_option)
1715 && tgetflag("ms") > 0)
1716 T_MS = (char_u *)"y";
1717 if ((T_XS == NULL || T_XS == empty_option)
1718 && tgetflag("xs") > 0)
1719 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001720 if ((T_XN == NULL || T_XN == empty_option)
1721 && tgetflag("xn") > 0)
1722 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if ((T_DB == NULL || T_DB == empty_option)
1724 && tgetflag("db") > 0)
1725 T_DB = (char_u *)"y";
1726 if ((T_DA == NULL || T_DA == empty_option)
1727 && tgetflag("da") > 0)
1728 T_DA = (char_u *)"y";
1729 if ((T_UT == NULL || T_UT == empty_option)
1730 && tgetflag("ut") > 0)
1731 T_UT = (char_u *)"y";
1732
1733
1734 /*
1735 * get key codes
1736 */
1737 for (i = 0; key_names[i] != NULL; ++i)
1738 {
1739 if (find_termcode((char_u *)key_names[i]) == NULL)
1740 {
1741 p = TGETSTR(key_names[i], &tp);
1742 /* if cursor-left == backspace, ignore it (televideo
1743 * 925) */
1744 if (p != NULL
1745 && (*p != Ctrl_H
1746 || key_names[i][0] != 'k'
1747 || key_names[i][1] != 'l'))
1748 add_termcode((char_u *)key_names[i], p, FALSE);
1749 }
1750 }
1751
1752 if (height == 0)
1753 height = tgetnum("li");
1754 if (width == 0)
1755 width = tgetnum("co");
1756
1757 /*
1758 * Get number of colors (if not done already).
1759 */
1760 if (term_str(KS_CCO) == NULL
1761 || term_str(KS_CCO) == empty_option)
1762 set_color_count(tgetnum("Co"));
1763
1764# ifndef hpux
1765 BC = (char *)TGETSTR("bc", &tp);
1766 UP = (char *)TGETSTR("up", &tp);
1767 p = TGETSTR("pc", &tp);
1768 if (p)
1769 PC = *p;
1770# endif /* hpux */
1771 }
1772 }
1773 else /* try == 0 || try == 2 */
1774#endif /* HAVE_TGETENT */
1775 /*
1776 * Use builtin termcap
1777 */
1778 {
1779#ifdef HAVE_TGETENT
1780 /*
1781 * If builtin termcap was already used, there is no need to search
1782 * for the builtin termcap again, quit now.
1783 */
1784 if (try == 2 && builtin_first && termcap_cleared)
1785 break;
1786#endif
1787 /*
1788 * search for 'term' in builtin_termcaps[]
1789 */
1790 termp = find_builtin_term(term);
1791 if (termp->bt_string == NULL) /* did not find it */
1792 {
1793#ifdef HAVE_TGETENT
1794 /*
1795 * If try == 0, first try the external termcap. If that is not
1796 * found we'll get back here with try == 2.
1797 * If termcap_cleared is set we used the external termcap,
1798 * don't complain about not finding the term in the builtin
1799 * termcap.
1800 */
1801 if (try == 0) /* try external one */
1802 continue;
1803 if (termcap_cleared) /* found in external termcap */
1804 break;
1805#endif
1806
1807 mch_errmsg("\r\n");
1808 if (error_msg != NULL)
1809 {
1810 mch_errmsg((char *)error_msg);
1811 mch_errmsg("\r\n");
1812 }
1813 mch_errmsg("'");
1814 mch_errmsg((char *)term);
1815 mch_errmsg(_("' not known. Available builtin terminals are:"));
1816 mch_errmsg("\r\n");
1817 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1818 ++termp)
1819 {
1820 if (termp->bt_entry == (int)KS_NAME)
1821 {
1822#ifdef HAVE_TGETENT
1823 mch_errmsg(" builtin_");
1824#else
1825 mch_errmsg(" ");
1826#endif
1827 mch_errmsg(termp->bt_string);
1828 mch_errmsg("\r\n");
1829 }
1830 }
1831 /* when user typed :set term=xxx, quit here */
1832 if (starting != NO_SCREEN)
1833 {
1834 screen_start(); /* don't know where cursor is now */
1835 wait_return(TRUE);
1836 return FAIL;
1837 }
1838 term = DEFAULT_TERM;
1839 mch_errmsg(_("defaulting to '"));
1840 mch_errmsg((char *)term);
1841 mch_errmsg("'\r\n");
1842 if (emsg_silent == 0)
1843 {
1844 screen_start(); /* don't know where cursor is now */
1845 out_flush();
1846 ui_delay(2000L, TRUE);
1847 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001848 set_string_option_direct((char_u *)"term", -1, term,
1849 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 display_errors();
1851 }
1852 out_flush();
1853#ifdef HAVE_TGETENT
1854 if (!termcap_cleared)
1855 {
1856#endif
1857 clear_termoptions(); /* clear old options */
1858#ifdef HAVE_TGETENT
1859 termcap_cleared = TRUE;
1860 }
1861#endif
1862 parse_builtin_tcap(term);
1863#ifdef FEAT_GUI
1864 if (term_is_gui(term))
1865 {
1866 out_flush();
1867 gui_init();
1868 /* If starting the GUI failed, don't do any of the other
1869 * things for this terminal */
1870 if (!gui.in_use)
1871 return FAIL;
1872#ifdef HAVE_TGETENT
1873 break; /* don't try using external termcap */
1874#endif
1875 }
1876#endif /* FEAT_GUI */
1877 }
1878#ifdef HAVE_TGETENT
1879 }
1880#endif
1881
1882/*
1883 * special: There is no info in the termcap about whether the cursor
1884 * positioning is relative to the start of the screen or to the start of the
1885 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1886 * relative.
1887 */
1888 if (STRCMP(term, "pcterm") == 0)
1889 T_CCS = (char_u *)"yes";
1890 else
1891 T_CCS = empty_option;
1892
1893#ifdef UNIX
1894/*
1895 * Any "stty" settings override the default for t_kb from the termcap.
1896 * This is in os_unix.c, because it depends a lot on the version of unix that
1897 * is being used.
1898 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1899 */
1900#ifdef FEAT_GUI
1901 if (!gui.in_use)
1902#endif
1903 get_stty();
1904#endif
1905
1906/*
1907 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1908 * didn't work, use the default CTRL-H
1909 * The default for t_kD is DEL, unless t_kb is DEL.
1910 * The vim_strsave'd strings are probably lost forever, well it's only two
1911 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1912 * directly.
1913 */
1914#ifdef FEAT_GUI
1915 if (!gui.in_use)
1916#endif
1917 {
1918 bs_p = find_termcode((char_u *)"kb");
1919 del_p = find_termcode((char_u *)"kD");
1920 if (bs_p == NULL || *bs_p == NUL)
1921 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1922 if ((del_p == NULL || *del_p == NUL) &&
1923 (bs_p == NULL || *bs_p != DEL))
1924 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1925 }
1926
1927#if defined(UNIX) || defined(VMS)
1928 term_is_xterm = vim_is_xterm(term);
1929#endif
1930
1931#ifdef FEAT_MOUSE
1932# if defined(UNIX) || defined(VMS)
1933# ifdef FEAT_MOUSE_TTY
1934 /*
1935 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1936 * The termcode for the mouse is added as a side effect in option.c.
1937 */
1938 {
1939 char_u *p;
1940
1941 p = (char_u *)"";
1942# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001943 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 if (use_xterm_mouse())
1946 p = NULL; /* keep existing value, might be "xterm2" */
1947 else
1948 p = (char_u *)"xterm";
1949 }
1950# endif
1951 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001954 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1955 * "xterm2" in check_termcode(). */
1956 reset_option_was_set((char_u *)"ttym");
1957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 if (p == NULL
1959# ifdef FEAT_GUI
1960 || gui.in_use
1961# endif
1962 )
1963 check_mouse_termcode(); /* set mouse termcode anyway */
1964 }
1965# endif
1966# else
1967 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1968# endif
1969#endif /* FEAT_MOUSE */
1970
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971#ifdef USE_TERM_CONSOLE
1972 /* DEFAULT_TERM indicates that it is the machine console. */
1973 if (STRCMP(term, DEFAULT_TERM) != 0)
1974 term_console = FALSE;
1975 else
1976 {
1977 term_console = TRUE;
1978# ifdef AMIGA
1979 win_resize_on(); /* enable window resizing reports */
1980# endif
1981 }
1982#endif
1983
1984#if defined(UNIX) || defined(VMS)
1985 /*
1986 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1987 */
1988 if (vim_is_fastterm(term))
1989 p_tf = TRUE;
1990#endif
1991#ifdef USE_TERM_CONSOLE
1992 /*
1993 * 'ttyfast' is default on consoles
1994 */
1995 if (term_console)
1996 p_tf = TRUE;
1997#endif
1998
1999 ttest(TRUE); /* make sure we have a valid set of terminal codes */
2000
2001 full_screen = TRUE; /* we can use termcap codes from now on */
2002 set_term_defaults(); /* use current values as defaults */
2003#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +02002004 LOG_TR("setting crv_status to CRV_GET");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 crv_status = CRV_GET; /* Get terminal version later */
2006#endif
2007
2008 /*
2009 * Initialize the terminal with the appropriate termcap codes.
2010 * Set the mouse and window title if possible.
2011 * Don't do this when starting, need to parse the .vimrc first, because it
2012 * may redefine t_TI etc.
2013 */
2014 if (starting != NO_SCREEN)
2015 {
2016 starttermcap(); /* may change terminal mode */
2017#ifdef FEAT_MOUSE
2018 setmouse(); /* may start using the mouse */
2019#endif
2020#ifdef FEAT_TITLE
2021 maketitle(); /* may display window title */
2022#endif
2023 }
2024
2025 /* display initial screen after ttest() checking. jw. */
2026 if (width <= 0 || height <= 0)
2027 {
2028 /* termcap failed to report size */
2029 /* set defaults, in case ui_get_shellsize() also fails */
2030 width = 80;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01002031#if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 height = 25; /* console is often 25 lines */
2033#else
2034 height = 24; /* most terminals are 24 lines */
2035#endif
2036 }
2037 set_shellsize(width, height, FALSE); /* may change Rows */
2038 if (starting != NO_SCREEN)
2039 {
2040 if (scroll_region)
2041 scroll_region_reset(); /* In case Rows changed */
2042 check_map_keycodes(); /* check mappings for terminal codes used */
2043
2044#ifdef FEAT_AUTOCMD
2045 {
2046 buf_T *old_curbuf;
2047
2048 /*
2049 * Execute the TermChanged autocommands for each buffer that is
2050 * loaded.
2051 */
2052 old_curbuf = curbuf;
2053 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
2054 {
2055 if (curbuf->b_ml.ml_mfp != NULL)
2056 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2057 curbuf);
2058 }
2059 if (buf_valid(old_curbuf))
2060 curbuf = old_curbuf;
2061 }
2062#endif
2063 }
2064
2065#ifdef FEAT_TERMRESPONSE
2066 may_req_termresponse();
2067#endif
2068
2069 return OK;
2070}
2071
2072#if defined(FEAT_MOUSE) || defined(PROTO)
2073
2074# ifdef FEAT_MOUSE_TTY
2075# define HMT_NORMAL 1
2076# define HMT_NETTERM 2
2077# define HMT_DEC 4
2078# define HMT_JSBTERM 8
2079# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002080# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002081# define HMT_SGR 64
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082static int has_mouse_termcode = 0;
2083# endif
2084
Bram Moolenaar864207d2008-06-24 22:14:38 +00002085# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002087set_mouse_termcode(
2088 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2089 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090{
2091 char_u name[2];
2092
2093 name[0] = n;
2094 name[1] = KE_FILLER;
2095 add_termcode(name, s, FALSE);
2096# ifdef FEAT_MOUSE_TTY
2097# ifdef FEAT_MOUSE_JSB
2098 if (n == KS_JSBTERM_MOUSE)
2099 has_mouse_termcode |= HMT_JSBTERM;
2100 else
2101# endif
2102# ifdef FEAT_MOUSE_NET
2103 if (n == KS_NETTERM_MOUSE)
2104 has_mouse_termcode |= HMT_NETTERM;
2105 else
2106# endif
2107# ifdef FEAT_MOUSE_DEC
2108 if (n == KS_DEC_MOUSE)
2109 has_mouse_termcode |= HMT_DEC;
2110 else
2111# endif
2112# ifdef FEAT_MOUSE_PTERM
2113 if (n == KS_PTERM_MOUSE)
2114 has_mouse_termcode |= HMT_PTERM;
2115 else
2116# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002117# ifdef FEAT_MOUSE_URXVT
2118 if (n == KS_URXVT_MOUSE)
2119 has_mouse_termcode |= HMT_URXVT;
2120 else
2121# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002122# ifdef FEAT_MOUSE_SGR
2123 if (n == KS_SGR_MOUSE)
2124 has_mouse_termcode |= HMT_SGR;
2125 else
2126# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 has_mouse_termcode |= HMT_NORMAL;
2128# endif
2129}
2130# endif
2131
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002132# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002133 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002135del_mouse_termcode(
2136 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137{
2138 char_u name[2];
2139
2140 name[0] = n;
2141 name[1] = KE_FILLER;
2142 del_termcode(name);
2143# ifdef FEAT_MOUSE_TTY
2144# ifdef FEAT_MOUSE_JSB
2145 if (n == KS_JSBTERM_MOUSE)
2146 has_mouse_termcode &= ~HMT_JSBTERM;
2147 else
2148# endif
2149# ifdef FEAT_MOUSE_NET
2150 if (n == KS_NETTERM_MOUSE)
2151 has_mouse_termcode &= ~HMT_NETTERM;
2152 else
2153# endif
2154# ifdef FEAT_MOUSE_DEC
2155 if (n == KS_DEC_MOUSE)
2156 has_mouse_termcode &= ~HMT_DEC;
2157 else
2158# endif
2159# ifdef FEAT_MOUSE_PTERM
2160 if (n == KS_PTERM_MOUSE)
2161 has_mouse_termcode &= ~HMT_PTERM;
2162 else
2163# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002164# ifdef FEAT_MOUSE_URXVT
2165 if (n == KS_URXVT_MOUSE)
2166 has_mouse_termcode &= ~HMT_URXVT;
2167 else
2168# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002169# ifdef FEAT_MOUSE_SGR
2170 if (n == KS_SGR_MOUSE)
2171 has_mouse_termcode &= ~HMT_SGR;
2172 else
2173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 has_mouse_termcode &= ~HMT_NORMAL;
2175# endif
2176}
2177# endif
2178#endif
2179
2180#ifdef HAVE_TGETENT
2181/*
2182 * Call tgetent()
2183 * Return error message if it fails, NULL if it's OK.
2184 */
2185 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002186tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187{
2188 int i;
2189
2190 i = TGETENT(tbuf, term);
2191 if (i < 0 /* -1 is always an error */
2192# ifdef TGETENT_ZERO_ERR
2193 || i == 0 /* sometimes zero is also an error */
2194# endif
2195 )
2196 {
2197 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2198 * tgetent() with the always existing "dumb" entry to avoid a crash or
2199 * hang. */
2200 (void)TGETENT(tbuf, "dumb");
2201
2202 if (i < 0)
2203# ifdef TGETENT_ZERO_ERR
2204 return (char_u *)_("E557: Cannot open termcap file");
2205 if (i == 0)
2206# endif
2207#ifdef TERMINFO
2208 return (char_u *)_("E558: Terminal entry not found in terminfo");
2209#else
2210 return (char_u *)_("E559: Terminal entry not found in termcap");
2211#endif
2212 }
2213 return NULL;
2214}
2215
2216/*
2217 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2218 * Fix that here.
2219 */
2220 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002221vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222{
2223 char *p;
2224
2225 p = tgetstr(s, (char **)pp);
2226 if (p == (char *)-1)
2227 p = NULL;
2228 return (char_u *)p;
2229}
2230#endif /* HAVE_TGETENT */
2231
2232#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(__EMX__) || defined(VMS) || defined(MACOS_X))
2233/*
2234 * Get Columns and Rows from the termcap. Used after a window signal if the
2235 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2236 * and "li" entries never change. But on some systems this works.
2237 * Errors while getting the entries are ignored.
2238 */
2239 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002240getlinecol(
2241 long *cp, /* pointer to columns */
2242 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243{
2244 char_u tbuf[TBUFSZ];
2245
2246 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2247 {
2248 if (*cp == 0)
2249 *cp = tgetnum("co");
2250 if (*rp == 0)
2251 *rp = tgetnum("li");
2252 }
2253}
2254#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2255
2256/*
2257 * Get a string entry from the termcap and add it to the list of termcodes.
2258 * Used for <t_xx> special keys.
2259 * Give an error message for failure when not sourcing.
2260 * If force given, replace an existing entry.
2261 * Return FAIL if the entry was not found, OK if the entry was added.
2262 */
2263 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002264add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
2266 char_u *term;
2267 int key;
2268 struct builtin_term *termp;
2269#ifdef HAVE_TGETENT
2270 char_u *string;
2271 int i;
2272 int builtin_first;
2273 char_u tbuf[TBUFSZ];
2274 char_u tstrbuf[TBUFSZ];
2275 char_u *tp = tstrbuf;
2276 char_u *error_msg = NULL;
2277#endif
2278
2279/*
2280 * If the GUI is running or will start in a moment, we only support the keys
2281 * that the GUI can produce.
2282 */
2283#ifdef FEAT_GUI
2284 if (gui.in_use || gui.starting)
2285 return gui_mch_haskey(name);
2286#endif
2287
2288 if (!force && find_termcode(name) != NULL) /* it's already there */
2289 return OK;
2290
2291 term = T_NAME;
2292 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2293 return FAIL;
2294
2295 if (term_is_builtin(term)) /* name starts with "builtin_" */
2296 {
2297 term += 8;
2298#ifdef HAVE_TGETENT
2299 builtin_first = TRUE;
2300#endif
2301 }
2302#ifdef HAVE_TGETENT
2303 else
2304 builtin_first = p_tbi;
2305#endif
2306
2307#ifdef HAVE_TGETENT
2308/*
2309 * We can get the entry from the builtin termcap and from the external one.
2310 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2311 * builtin termcap first.
2312 * If 'ttybuiltin' is off, try external termcap first.
2313 */
2314 for (i = 0; i < 2; ++i)
2315 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002316 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317#endif
2318 /*
2319 * Search in builtin termcap
2320 */
2321 {
2322 termp = find_builtin_term(term);
2323 if (termp->bt_string != NULL) /* found it */
2324 {
2325 key = TERMCAP2KEY(name[0], name[1]);
2326 while (termp->bt_entry != (int)KS_NAME)
2327 {
2328 if ((int)termp->bt_entry == key)
2329 {
2330 add_termcode(name, (char_u *)termp->bt_string,
2331 term_is_8bit(term));
2332 return OK;
2333 }
2334 ++termp;
2335 }
2336 }
2337 }
2338#ifdef HAVE_TGETENT
2339 else
2340 /*
2341 * Search in external termcap
2342 */
2343 {
2344 error_msg = tgetent_error(tbuf, term);
2345 if (error_msg == NULL)
2346 {
2347 string = TGETSTR((char *)name, &tp);
2348 if (string != NULL && *string != NUL)
2349 {
2350 add_termcode(name, string, FALSE);
2351 return OK;
2352 }
2353 }
2354 }
2355 }
2356#endif
2357
2358 if (sourcing_name == NULL)
2359 {
2360#ifdef HAVE_TGETENT
2361 if (error_msg != NULL)
2362 EMSG(error_msg);
2363 else
2364#endif
2365 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2366 }
2367 return FAIL;
2368}
2369
2370 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002371term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2374}
2375
2376/*
2377 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2378 * Assume that the terminal is using 8-bit controls when the name contains
2379 * "8bit", like in "xterm-8bit".
2380 */
2381 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002382term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383{
2384 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2385}
2386
2387/*
2388 * Translate terminal control chars from 7-bit to 8-bit:
2389 * <Esc>[ -> CSI
2390 * <Esc>] -> <M-C-]>
2391 * <Esc>O -> <M-C-O>
2392 */
2393 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002394term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395{
2396 if (*p == ESC)
2397 {
2398 if (p[1] == '[')
2399 return CSI;
2400 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002401 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 if (p[1] == 'O')
2403 return 0x8f;
2404 }
2405 return 0;
2406}
2407
2408#ifdef FEAT_GUI
2409 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002410term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
2412 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2413}
2414#endif
2415
2416#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2417
2418 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002419tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421 static char_u buf[16];
2422 char_u *p;
2423
2424 p = buf + 15;
2425 *p = '\0';
2426 do
2427 {
2428 --p;
2429 *p = (char_u) (i % 10 + '0');
2430 i /= 10;
2431 }
2432 while (i > 0 && p > buf);
2433 return p;
2434}
2435#endif
2436
2437#ifndef HAVE_TGETENT
2438
2439/*
2440 * minimal tgoto() implementation.
2441 * no padding and we only parse for %i %d and %+char
2442 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002443static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002445 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002446tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447{
2448 static char buf[30];
2449 char *p, *s, *e;
2450
2451 if (!cm)
2452 return "OOPS";
2453 e = buf + 29;
2454 for (s = buf; s < e && *cm; cm++)
2455 {
2456 if (*cm != '%')
2457 {
2458 *s++ = *cm;
2459 continue;
2460 }
2461 switch (*++cm)
2462 {
2463 case 'd':
2464 p = (char *)tltoa((unsigned long)y);
2465 y = x;
2466 while (*p)
2467 *s++ = *p++;
2468 break;
2469 case 'i':
2470 x++;
2471 y++;
2472 break;
2473 case '+':
2474 *s++ = (char)(*++cm + y);
2475 y = x;
2476 break;
2477 case '%':
2478 *s++ = *cm;
2479 break;
2480 default:
2481 return "OOPS";
2482 }
2483 }
2484 *s = '\0';
2485 return buf;
2486}
2487
2488#endif /* HAVE_TGETENT */
2489
2490/*
2491 * Set the terminal name and initialize the terminal options.
2492 * If "name" is NULL or empty, get the terminal name from the environment.
2493 * If that fails, use the default terminal name.
2494 */
2495 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002496termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 char_u *term;
2499
2500 if (name != NULL && *name == NUL)
2501 name = NULL; /* empty name is equal to no name */
2502 term = name;
2503
2504#ifdef __BEOS__
2505 /*
2506 * TERM environment variable is normally set to 'ansi' on the Bebox;
2507 * Since the BeBox doesn't quite support full ANSI yet, we use our
2508 * own custom 'ansi-beos' termcap instead, unless the -T option has
2509 * been given on the command line.
2510 */
2511 if (term == NULL
2512 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2513 term = DEFAULT_TERM;
2514#endif
2515#ifndef MSWIN
2516 if (term == NULL)
2517 term = mch_getenv((char_u *)"TERM");
2518#endif
2519 if (term == NULL || *term == NUL)
2520 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002521 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
2523 /* Set the default terminal name. */
2524 set_string_default("term", term);
2525 set_string_default("ttytype", term);
2526
2527 /*
2528 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2529 */
2530 set_termname(T_NAME != NULL ? T_NAME : term);
2531}
2532
2533/*
2534 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2535 */
2536#ifdef DOS16
2537# define OUT_SIZE 255 /* only have 640K total... */
2538#else
Bram Moolenaare89ff042016-02-20 22:17:05 +01002539# define OUT_SIZE 2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540#endif
2541 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2542static char_u out_buf[OUT_SIZE + 1];
2543static int out_pos = 0; /* number of chars in out_buf */
2544
2545/*
2546 * out_flush(): flush the output buffer
2547 */
2548 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002549out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
2551 int len;
2552
2553 if (out_pos != 0)
2554 {
2555 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2556 len = out_pos;
2557 out_pos = 0;
2558 ui_write(out_buf, len);
2559 }
2560}
2561
2562#if defined(FEAT_MBYTE) || defined(PROTO)
2563/*
2564 * Sometimes a byte out of a multi-byte character is written with out_char().
2565 * To avoid flushing half of the character, call this function first.
2566 */
2567 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002568out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569{
2570 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2571 out_flush();
2572}
2573#endif
2574
2575#ifdef FEAT_GUI
2576/*
2577 * out_trash(): Throw away the contents of the output buffer
2578 */
2579 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002580out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
2582 out_pos = 0;
2583}
2584#endif
2585
2586/*
2587 * out_char(c): put a byte into the output buffer.
2588 * Flush it if it becomes full.
2589 * This should not be used for outputting text on the screen (use functions
2590 * like msg_puts() and screen_putchar() for that).
2591 */
2592 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002593out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
2595#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2596 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2597 out_char('\r');
2598#endif
2599
2600 out_buf[out_pos++] = c;
2601
2602 /* For testing we flush each time. */
2603 if (out_pos >= OUT_SIZE || p_wd)
2604 out_flush();
2605}
2606
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002607static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608
2609/*
2610 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2611 */
2612 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002613out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
2615#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2616 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2617 out_char_nf('\r');
2618#endif
2619
2620 out_buf[out_pos++] = c;
2621
2622 if (out_pos >= OUT_SIZE)
2623 out_flush();
2624}
2625
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002626#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2627 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628/*
2629 * A never-padding out_str.
2630 * use this whenever you don't want to run the string through tputs.
2631 * tputs above is harmless, but tputs from the termcap library
2632 * is likely to strip off leading digits, that it mistakes for padding
2633 * information, and "%i", "%d", etc.
2634 * This should only be used for writing terminal codes, not for outputting
2635 * normal text (use functions like msg_puts() and screen_putchar() for that).
2636 */
2637 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002638out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
2640 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2641 out_flush();
2642 while (*s)
2643 out_char_nf(*s++);
2644
2645 /* For testing we write one string at a time. */
2646 if (p_wd)
2647 out_flush();
2648}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651/*
2652 * out_str(s): Put a character string a byte at a time into the output buffer.
2653 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2654 * This should only be used for writing terminal codes, not for outputting
2655 * normal text (use functions like msg_puts() and screen_putchar() for that).
2656 */
2657 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002658out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
2660 if (s != NULL && *s)
2661 {
2662#ifdef FEAT_GUI
2663 /* Don't use tputs() when GUI is used, ncurses crashes. */
2664 if (gui.in_use)
2665 {
2666 out_str_nf(s);
2667 return;
2668 }
2669#endif
2670 /* avoid terminal strings being split up */
2671 if (out_pos > OUT_SIZE - 20)
2672 out_flush();
2673#ifdef HAVE_TGETENT
2674 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2675#else
2676 while (*s)
2677 out_char_nf(*s++);
2678#endif
2679
2680 /* For testing we write one string at a time. */
2681 if (p_wd)
2682 out_flush();
2683 }
2684}
2685
2686/*
2687 * cursor positioning using termcap parser. (jw)
2688 */
2689 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002690term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 OUT_STR(tgoto((char *)T_CM, col, row));
2693}
2694
2695 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002696term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
2698 OUT_STR(tgoto((char *)T_CRI, 0, i));
2699}
2700
2701 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002702term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
2704 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2705}
2706
2707 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002708term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709{
2710 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2711}
2712
2713#if defined(HAVE_TGETENT) || defined(PROTO)
2714 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002715term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716{
2717 /* Can't handle a negative value here */
2718 if (x < 0)
2719 x = 0;
2720 if (y < 0)
2721 y = 0;
2722 OUT_STR(tgoto((char *)T_CWP, y, x));
2723}
2724
2725 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002726term_set_winsize(int width, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727{
2728 OUT_STR(tgoto((char *)T_CWS, height, width));
2729}
2730#endif
2731
2732 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002733term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734{
2735 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2736 if (*T_CAF)
2737 term_color(T_CAF, n);
2738 else if (*T_CSF)
2739 term_color(T_CSF, n);
2740}
2741
2742 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002743term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744{
2745 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2746 if (*T_CAB)
2747 term_color(T_CAB, n);
2748 else if (*T_CSB)
2749 term_color(T_CSB, n);
2750}
2751
2752 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002753term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754{
2755 char buf[20];
2756 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2757
2758 /* Special handling of 16 colors, because termcap can't handle it */
2759 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2760 /* Also accept CSI instead of <Esc>[ */
2761 if (n >= 8 && t_colors >= 16
2762 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2763 && s[i] != NUL
2764 && (STRCMP(s + i + 1, "%p1%dm") == 0
2765 || STRCMP(s + i + 1, "%dm") == 0)
2766 && (s[i] == '3' || s[i] == '4'))
2767 {
2768 sprintf(buf,
2769#ifdef TERMINFO
2770 "%s%s%%p1%%dm",
2771#else
2772 "%s%s%%dm",
2773#endif
2774 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2775 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2776 : (n >= 16 ? "48;5;" : "10"));
2777 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2778 }
2779 else
2780 OUT_STR(tgoto((char *)s, 0, n));
2781}
2782
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002783#if defined(FEAT_TERMTRUECOLOR) || defined(PROTO)
2784 void
2785term_fg_rgb_color(long_u rgb)
2786{
2787 term_rgb_color(T_8F, rgb);
2788}
2789
2790 void
2791term_bg_rgb_color(long_u rgb)
2792{
2793 term_rgb_color(T_8B, rgb);
2794}
2795
2796#define RED(rgb) ((rgb>>16)&0xFF)
2797#define GREEN(rgb) ((rgb>> 8)&0xFF)
2798#define BLUE(rgb) ((rgb )&0xFF)
2799
2800 static void
2801term_rgb_color(char_u *s, long_u rgb)
2802{
Bram Moolenaar380130f2016-04-22 11:24:43 +02002803#define MAX_COLOR_STR_LEN 100
2804 char buf[MAX_COLOR_STR_LEN];
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002805
Bram Moolenaara1c487e2016-04-22 20:20:19 +02002806 vim_snprintf(buf, MAX_COLOR_STR_LEN,
Bram Moolenaar380130f2016-04-22 11:24:43 +02002807 (char *)s, RED(rgb), GREEN(rgb), BLUE(rgb));
Bram Moolenaar8a633e32016-04-21 21:10:14 +02002808 OUT_STR(buf);
2809}
2810#endif
2811
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002812#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2813 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814/*
2815 * Generic function to set window title, using t_ts and t_fs.
2816 */
2817 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002818term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
2820 /* t_ts takes one argument: column in status line */
2821 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2822 out_str_nf(title);
2823 out_str(T_FS); /* set title end */
2824 out_flush();
2825}
2826#endif
2827
2828/*
2829 * Make sure we have a valid set or terminal options.
2830 * Replace all entries that are NULL by empty_option
2831 */
2832 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002833ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
2835 check_options(); /* make sure no options are NULL */
2836
2837 /*
2838 * MUST have "cm": cursor motion.
2839 */
2840 if (*T_CM == NUL)
2841 EMSG(_("E437: terminal capability \"cm\" required"));
2842
2843 /*
2844 * if "cs" defined, use a scroll region, it's faster.
2845 */
2846 if (*T_CS != NUL)
2847 scroll_region = TRUE;
2848 else
2849 scroll_region = FALSE;
2850
2851 if (pairs)
2852 {
2853 /*
2854 * optional pairs
2855 */
2856 /* TP goes to normal mode for TI (invert) and TB (bold) */
2857 if (*T_ME == NUL)
2858 T_ME = T_MR = T_MD = T_MB = empty_option;
2859 if (*T_SO == NUL || *T_SE == NUL)
2860 T_SO = T_SE = empty_option;
2861 if (*T_US == NUL || *T_UE == NUL)
2862 T_US = T_UE = empty_option;
2863 if (*T_CZH == NUL || *T_CZR == NUL)
2864 T_CZH = T_CZR = empty_option;
2865
2866 /* T_VE is needed even though T_VI is not defined */
2867 if (*T_VE == NUL)
2868 T_VI = empty_option;
2869
2870 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2871 if (*T_ME == NUL)
2872 {
2873 T_ME = T_SE;
2874 T_MR = T_SO;
2875 T_MD = T_SO;
2876 }
2877
2878 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2879 if (*T_SO == NUL)
2880 {
2881 T_SE = T_ME;
2882 if (*T_MR == NUL)
2883 T_SO = T_MD;
2884 else
2885 T_SO = T_MR;
2886 }
2887
2888 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2889 if (*T_CZH == NUL)
2890 {
2891 T_CZR = T_ME;
2892 if (*T_MR == NUL)
2893 T_CZH = T_MD;
2894 else
2895 T_CZH = T_MR;
2896 }
2897
2898 /* "Sb" and "Sf" come in pairs */
2899 if (*T_CSB == NUL || *T_CSF == NUL)
2900 {
2901 T_CSB = empty_option;
2902 T_CSF = empty_option;
2903 }
2904
2905 /* "AB" and "AF" come in pairs */
2906 if (*T_CAB == NUL || *T_CAF == NUL)
2907 {
2908 T_CAB = empty_option;
2909 T_CAF = empty_option;
2910 }
2911
2912 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2913 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002914 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915
2916 /* Set 'weirdinvert' according to value of 't_xs' */
2917 p_wiv = (*T_XS != NUL);
2918 }
2919 need_gather = TRUE;
2920
2921 /* Set t_colors to the value of t_Co. */
2922 t_colors = atoi((char *)T_CCO);
2923}
2924
2925#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2926 || defined(PROTO)
2927/*
2928 * Represent the given long_u as individual bytes, with the most significant
2929 * byte first, and store them in dst.
2930 */
2931 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002932add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
2934 int i;
2935 int shift;
2936
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002937 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 {
2939 shift = 8 * (sizeof(long_u) - i);
2940 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2941 }
2942}
2943
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002944static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
2946/*
2947 * Interpret the next string of bytes in buf as a long integer, with the most
2948 * significant byte first. Note that it is assumed that buf has been through
2949 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
2950 * Puts result in val, and returns the number of bytes read from buf
2951 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
2952 * were present.
2953 */
2954 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002955get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956{
2957 int len;
2958 char_u bytes[sizeof(long_u)];
2959 int i;
2960 int shift;
2961
2962 *val = 0;
2963 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
2964 if (len != -1)
2965 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002966 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 {
2968 shift = 8 * (sizeof(long_u) - 1 - i);
2969 *val += (long_u)bytes[i] << shift;
2970 }
2971 }
2972 return len;
2973}
2974#endif
2975
2976#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002977 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
2978 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979/*
2980 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
2981 * that buf has been through inchar(). Returns the actual number of bytes used
2982 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
2983 * available.
2984 */
2985 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002986get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
2988 int len = 0;
2989 int i;
2990 char_u c;
2991
2992 for (i = 0; i < num_bytes; i++)
2993 {
2994 if ((c = buf[len++]) == NUL)
2995 return -1;
2996 if (c == K_SPECIAL)
2997 {
2998 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
2999 return -1;
3000 if (buf[len++] == (int)KS_ZERO)
3001 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02003002 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
3003 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
3004 if (buf[len++] == (int)KE_CSI)
3005 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00003007 else if (c == CSI && buf[len] == KS_EXTRA
3008 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003009 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
3010 * the start of a special key, see add_to_input_buf_csi(). */
3011 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 bytes[i] = c;
3013 }
3014 return len;
3015}
3016#endif
3017
3018/*
Bram Moolenaare057d402013-06-30 17:51:51 +02003019 * Check if the new shell size is valid, correct it if it's too small or way
3020 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 */
3022 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003023check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 if (Rows < min_rows()) /* need room for one window and command line */
3026 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02003027 limit_screen_size();
3028}
3029
3030/*
3031 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
3032 */
3033 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003034limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02003035{
3036 if (Columns < MIN_COLUMNS)
3037 Columns = MIN_COLUMNS;
3038 else if (Columns > 10000)
3039 Columns = 10000;
3040 if (Rows > 1000)
3041 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042}
3043
Bram Moolenaar86b68352004-12-27 21:59:20 +00003044/*
3045 * Invoked just before the screen structures are going to be (re)allocated.
3046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003048win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
3050 static int old_Rows = 0;
3051 static int old_Columns = 0;
3052
3053 if (old_Rows != Rows || old_Columns != Columns)
3054 ui_new_shellsize();
3055 if (old_Rows != Rows)
3056 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003057 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003058 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003059 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 old_Rows = Rows;
3061 shell_new_rows(); /* update window sizes */
3062 }
3063 if (old_Columns != Columns)
3064 {
3065 old_Columns = Columns;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003066#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 shell_new_columns(); /* update window sizes */
3068#endif
3069 }
3070}
3071
3072/*
3073 * Call this function when the Vim shell has been resized in any way.
3074 * Will obtain the current size and redraw (also when size didn't change).
3075 */
3076 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003077shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
3079 set_shellsize(0, 0, FALSE);
3080}
3081
3082/*
3083 * Check if the shell size changed. Handle a resize.
3084 * When the size didn't change, nothing happens.
3085 */
3086 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003087shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
3089 int old_Rows = Rows;
3090 int old_Columns = Columns;
3091
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003092 if (!exiting
3093#ifdef FEAT_GUI
3094 /* Do not get the size when executing a shell command during
3095 * startup. */
3096 && !gui.starting
3097#endif
3098 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003099 {
3100 (void)ui_get_shellsize();
3101 check_shellsize();
3102 if (old_Rows != Rows || old_Columns != Columns)
3103 shell_resized();
3104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105}
3106
3107/*
3108 * Set size of the Vim shell.
3109 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3110 * window size (this is used for the :win command).
3111 * If 'mustset' is FALSE, we may try to get the real window size and if
3112 * it fails use 'width' and 'height'.
3113 */
3114 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003115set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
3117 static int busy = FALSE;
3118
3119 /*
3120 * Avoid recursiveness, can happen when setting the window size causes
3121 * another window-changed signal.
3122 */
3123 if (busy)
3124 return;
3125
3126 if (width < 0 || height < 0) /* just checking... */
3127 return;
3128
Bram Moolenaara971b822011-09-14 14:43:25 +02003129 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003131 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 State = SETWSIZE;
3133 return;
3134 }
3135
Bram Moolenaara971b822011-09-14 14:43:25 +02003136 /* curwin->w_buffer can be NULL when we are closing a window and the
3137 * buffer has already been closed and removing a scrollbar causes a resize
3138 * event. Don't resize then, it will happen after entering another buffer.
3139 */
3140 if (curwin->w_buffer == NULL)
3141 return;
3142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 ++busy;
3144
3145#ifdef AMIGA
3146 out_flush(); /* must do this before mch_get_shellsize() for
3147 some obscure reason */
3148#endif
3149
3150 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3151 {
3152 Rows = height;
3153 Columns = width;
3154 check_shellsize();
3155 ui_set_shellsize(mustset);
3156 }
3157 else
3158 check_shellsize();
3159
3160 /* The window layout used to be adjusted here, but it now happens in
3161 * screenalloc() (also invoked from screenclear()). That is because the
3162 * "busy" check above may skip this, but not screenalloc(). */
3163
3164 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3165 screenclear();
3166 else
3167 screen_start(); /* don't know where cursor is now */
3168
3169 if (starting != NO_SCREEN)
3170 {
3171#ifdef FEAT_TITLE
3172 maketitle();
3173#endif
3174 changed_line_abv_curs();
3175 invalidate_botline();
3176
3177 /*
3178 * We only redraw when it's needed:
3179 * - While at the more prompt or executing an external command, don't
3180 * redraw, but position the cursor.
3181 * - While editing the command line, only redraw that.
3182 * - in Ex mode, don't redraw anything.
3183 * - Otherwise, redraw right now, and position the cursor.
3184 * Always need to call update_screen() or screenalloc(), to make
3185 * sure Rows/Columns and the size of ScreenLines[] is correct!
3186 */
3187 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3188 || exmode_active)
3189 {
3190 screenalloc(FALSE);
3191 repeat_message();
3192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 else
3194 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003195#ifdef FEAT_SCROLLBIND
3196 if (curwin->w_p_scb)
3197 do_check_scrollbind(TRUE);
3198#endif
3199 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003200 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003201 update_screen(NOT_VALID);
3202 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003203 }
3204 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003205 {
3206 update_topline();
3207#if defined(FEAT_INS_EXPAND)
3208 if (pum_visible())
3209 {
3210 redraw_later(NOT_VALID);
3211 ins_compl_show_pum(); /* This includes the redraw. */
3212 }
3213 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003214#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003215 update_screen(NOT_VALID);
3216 if (redrawing())
3217 setcursor();
3218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 }
3220 cursor_on(); /* redrawing may have switched it off */
3221 }
3222 out_flush();
3223 --busy;
3224}
3225
3226/*
3227 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3228 * commands and Ex mode).
3229 */
3230 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003231settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
3233#ifdef FEAT_GUI
3234 /* don't set the term where gvim was started to any mode */
3235 if (gui.in_use)
3236 return;
3237#endif
3238
3239 if (full_screen)
3240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 /*
3242 * When returning after calling a shell we want to really set the
3243 * terminal to raw mode, even though we think it already is, because
3244 * the shell program may have reset the terminal mode.
3245 * When we think the terminal is normal, don't try to set it to
3246 * normal again, because that causes problems (logout!) on some
3247 * machines.
3248 */
3249 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3250 {
3251#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003252# ifdef FEAT_GUI
3253 if (!gui.in_use && !gui.starting)
3254# endif
3255 {
3256 /* May need to check for T_CRV response and termcodes, it
3257 * doesn't work in Cooked mode, an external program may get
3258 * them. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003259 if (tmode != TMODE_RAW && (crv_status == CRV_SENT
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003260 || u7_status == U7_SENT
3261 || rbg_status == RBG_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003262 (void)vpeekc_nomap();
3263 check_for_codes_from_term();
3264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265#endif
3266#ifdef FEAT_MOUSE_TTY
3267 if (tmode != TMODE_RAW)
3268 mch_setmouse(FALSE); /* switch mouse off */
3269#endif
3270 out_flush();
3271 mch_settmode(tmode); /* machine specific function */
3272 cur_tmode = tmode;
3273#ifdef FEAT_MOUSE
3274 if (tmode == TMODE_RAW)
3275 setmouse(); /* may switch mouse on */
3276#endif
3277 out_flush();
3278 }
3279#ifdef FEAT_TERMRESPONSE
3280 may_req_termresponse();
3281#endif
3282 }
3283}
3284
3285 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003286starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
3288 if (full_screen && !termcap_active)
3289 {
3290 out_str(T_TI); /* start termcap mode */
3291 out_str(T_KS); /* start "keypad transmit" mode */
3292 out_flush();
3293 termcap_active = TRUE;
3294 screen_start(); /* don't know where cursor is now */
3295#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003296# ifdef FEAT_GUI
3297 if (!gui.in_use && !gui.starting)
3298# endif
3299 {
3300 may_req_termresponse();
3301 /* Immediately check for a response. If t_Co changes, we don't
3302 * want to redraw with wrong colors first. */
Bram Moolenaar90013c62014-05-22 18:14:31 +02003303 if (crv_status == CRV_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003304 check_for_codes_from_term();
3305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#endif
3307 }
3308}
3309
3310 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003311stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313 screen_stop_highlight();
3314 reset_cterm_colors();
3315 if (termcap_active)
3316 {
3317#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003318# ifdef FEAT_GUI
3319 if (!gui.in_use && !gui.starting)
3320# endif
3321 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003322 /* May need to discard T_CRV, T_U7 or T_RBG response. */
3323 if (crv_status == CRV_SENT || u7_status == U7_SENT
3324 || rbg_status == RBG_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003325 {
3326# ifdef UNIX
3327 /* Give the terminal a chance to respond. */
3328 mch_delay(100L, FALSE);
3329# endif
3330# ifdef TCIFLUSH
3331 /* Discard data received but not read. */
3332 if (exiting)
3333 tcflush(fileno(stdin), TCIFLUSH);
3334# endif
3335 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003336 /* Check for termcodes first, otherwise an external program may
3337 * get them. */
3338 check_for_codes_from_term();
3339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340#endif
3341 out_str(T_KE); /* stop "keypad transmit" mode */
3342 out_flush();
3343 termcap_active = FALSE;
3344 cursor_on(); /* just in case it is still off */
3345 out_str(T_TE); /* stop termcap mode */
3346 screen_start(); /* don't know where cursor is now */
3347 out_flush();
3348 }
3349}
3350
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003351#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352/*
3353 * Request version string (for xterm) when needed.
3354 * Only do this after switching to raw mode, otherwise the result will be
3355 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003356 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003357 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 * Only do this after termcap mode has been started, otherwise the codes for
3359 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003360 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3361 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003362 * On Unix only do it when both output and input are a tty (avoid writing
3363 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 * The result is caught in check_termcode().
3365 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003366 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003367may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369 if (crv_status == CRV_GET
3370 && cur_tmode == TMODE_RAW
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003371 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 && termcap_active
Bram Moolenaarebefac62005-12-28 22:39:57 +00003373 && p_ek
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003374# ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 && isatty(1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003376 && isatty(read_cmd_fd)
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 && *T_CRV != NUL)
3379 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02003380 LOG_TR("Sending CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 out_str(T_CRV);
3382 crv_status = CRV_SENT;
3383 /* check for the characters now, otherwise they might be eaten by
3384 * get_keystroke() */
3385 out_flush();
3386 (void)vpeekc_nomap();
3387 }
3388}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003389
3390# if defined(FEAT_MBYTE) || defined(PROTO)
3391/*
3392 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003393 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003394 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003395 * If the terminal treats \u25bd as single width, the position is (1, 1),
3396 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003397 * This function has the side effect that changes cursor position, so
3398 * it must be called immediately after entering termcap mode.
3399 */
3400 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003401may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003402{
3403 if (u7_status == U7_GET
3404 && cur_tmode == TMODE_RAW
3405 && termcap_active
3406 && p_ek
3407# ifdef UNIX
3408 && isatty(1)
3409 && isatty(read_cmd_fd)
3410# endif
3411 && *T_U7 != NUL
3412 && !option_was_set((char_u *)"ambiwidth"))
3413 {
3414 char_u buf[16];
3415
Bram Moolenaar2951b772013-07-03 12:45:31 +02003416 LOG_TR("Sending U7 request");
3417 /* Do this in the second row. In the first row the returned sequence
3418 * may be CSI 1;2R, which is the same as <S-F3>. */
3419 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003420 buf[mb_char2bytes(0x25bd, buf)] = 0;
3421 out_str(buf);
3422 out_str(T_U7);
3423 u7_status = U7_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003424 out_flush();
3425 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003426 out_str((char_u *)" ");
3427 term_windgoto(0, 0);
3428 /* check for the characters now, otherwise they might be eaten by
3429 * get_keystroke() */
3430 out_flush();
3431 (void)vpeekc_nomap();
3432 }
3433}
3434# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003435
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003436#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
3437/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003438 * Similar to requesting the version string: Request the terminal background
3439 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003440 */
3441 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003442may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003443{
3444 if (rbg_status == RBG_GET
3445 && cur_tmode == TMODE_RAW
3446 && termcap_active
3447 && p_ek
3448# ifdef UNIX
3449 && isatty(1)
3450 && isatty(read_cmd_fd)
3451# endif
3452 && *T_RBG != NUL
3453 && !option_was_set((char_u *)"bg"))
3454 {
3455 LOG_TR("Sending BG request");
3456 out_str(T_RBG);
3457 rbg_status = RBG_SENT;
3458 /* check for the characters now, otherwise they might be eaten by
3459 * get_keystroke() */
3460 out_flush();
3461 (void)vpeekc_nomap();
3462 }
3463}
3464# endif
3465
Bram Moolenaar2951b772013-07-03 12:45:31 +02003466# ifdef DEBUG_TERMRESPONSE
3467 static void
3468log_tr(char *msg)
3469{
3470 static FILE *fd_tr = NULL;
3471 static proftime_T start;
3472 proftime_T now;
3473
3474 if (fd_tr == NULL)
3475 {
3476 fd_tr = fopen("termresponse.log", "w");
3477 profile_start(&start);
3478 }
3479 now = start;
3480 profile_end(&now);
3481 fprintf(fd_tr, "%s: %s %s\n",
3482 profile_msg(&now),
3483 must_redraw == NOT_VALID ? "NV"
3484 : must_redraw == CLEAR ? "CL" : " ",
3485 msg);
3486}
3487# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488#endif
3489
3490/*
3491 * Return TRUE when saving and restoring the screen.
3492 */
3493 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003494swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496 return (full_screen && *T_TI != NUL);
3497}
3498
3499#ifdef FEAT_MOUSE
3500/*
3501 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3502 */
3503 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003504setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505{
3506# ifdef FEAT_MOUSE_TTY
3507 int checkfor;
3508# endif
3509
3510# ifdef FEAT_MOUSESHAPE
3511 update_mouseshape(-1);
3512# endif
3513
3514# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3515# ifdef FEAT_GUI
3516 /* In the GUI the mouse is always enabled. */
3517 if (gui.in_use)
3518 return;
3519# endif
3520 /* be quick when mouse is off */
3521 if (*p_mouse == NUL || has_mouse_termcode == 0)
3522 return;
3523
3524 /* don't switch mouse on when not in raw mode (Ex mode) */
3525 if (cur_tmode != TMODE_RAW)
3526 {
3527 mch_setmouse(FALSE);
3528 return;
3529 }
3530
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 if (VIsual_active)
3532 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003533 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 checkfor = MOUSE_RETURN;
3535 else if (State & INSERT)
3536 checkfor = MOUSE_INSERT;
3537 else if (State & CMDLINE)
3538 checkfor = MOUSE_COMMAND;
3539 else if (State == CONFIRM || State == EXTERNCMD)
3540 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3541 else
3542 checkfor = MOUSE_NORMAL; /* assume normal mode */
3543
3544 if (mouse_has(checkfor))
3545 mch_setmouse(TRUE);
3546 else
3547 mch_setmouse(FALSE);
3548# endif
3549}
3550
3551/*
3552 * Return TRUE if
3553 * - "c" is in 'mouse', or
3554 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3555 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3556 * normal editing mode (not at hit-return message).
3557 */
3558 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003559mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
3561 char_u *p;
3562
3563 for (p = p_mouse; *p; ++p)
3564 switch (*p)
3565 {
3566 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3567 return TRUE;
3568 break;
3569 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3570 return TRUE;
3571 break;
3572 default: if (c == *p) return TRUE; break;
3573 }
3574 return FALSE;
3575}
3576
3577/*
3578 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3579 */
3580 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003581mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582{
3583 return (p_mousem[0] == 'p');
3584}
3585#endif
3586
3587/*
3588 * By outputting the 'cursor very visible' termcap code, for some windowed
3589 * terminals this makes the screen scrolled to the correct position.
3590 * Used when starting Vim or returning from a shell.
3591 */
3592 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003593scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594{
3595 if (*T_VS != NUL)
3596 {
3597 out_str(T_VS);
3598 out_str(T_VE);
3599 screen_start(); /* don't know where cursor is now */
3600 }
3601}
3602
3603static int cursor_is_off = FALSE;
3604
3605/*
3606 * Enable the cursor.
3607 */
3608 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003609cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
3611 if (cursor_is_off)
3612 {
3613 out_str(T_VE);
3614 cursor_is_off = FALSE;
3615 }
3616}
3617
3618/*
3619 * Disable the cursor.
3620 */
3621 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003622cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623{
3624 if (full_screen)
3625 {
3626 if (!cursor_is_off)
3627 out_str(T_VI); /* disable cursor */
3628 cursor_is_off = TRUE;
3629 }
3630}
3631
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003632#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003634 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003635 */
3636 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003637term_cursor_shape(void)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003638{
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003639 static int showing_mode = NORMAL;
3640 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003641
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003642 /* Only do something when redrawing the screen and we can restore the
3643 * mode. */
3644 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003645 return;
3646
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003647 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003648 {
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003649 if (showing_mode != REPLACE)
3650 {
3651 if (*T_CSR != NUL)
3652 p = T_CSR; /* Replace mode cursor */
3653 else
3654 p = T_CSI; /* fall back to Insert mode cursor */
3655 if (*p != NUL)
3656 {
3657 out_str(p);
3658 showing_mode = REPLACE;
3659 }
3660 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003661 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003662 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003663 {
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003664 if (showing_mode != INSERT && *T_CSI != NUL)
3665 {
3666 out_str(T_CSI); /* Insert mode cursor */
3667 showing_mode = INSERT;
3668 }
3669 }
3670 else if (showing_mode != NORMAL)
3671 {
3672 out_str(T_CEI); /* non-Insert mode cursor */
3673 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003674 }
3675}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003676#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003677
3678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 * Set scrolling region for window 'wp'.
3680 * The region starts 'off' lines from the start of the window.
3681 * Also set the vertical scroll region for a vertically split window. Always
3682 * the full width of the window, excluding the vertical separator.
3683 */
3684 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003685scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
3687 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3688 W_WINROW(wp) + off));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003689#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 if (*T_CSV != NUL && wp->w_width != Columns)
3691 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3692 W_WINCOL(wp)));
3693#endif
3694 screen_start(); /* don't know where cursor is now */
3695}
3696
3697/*
3698 * Reset scrolling region to the whole screen.
3699 */
3700 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003701scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702{
3703 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003704#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 if (*T_CSV != NUL)
3706 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3707#endif
3708 screen_start(); /* don't know where cursor is now */
3709}
3710
3711
3712/*
3713 * List of terminal codes that are currently recognized.
3714 */
3715
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003716static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717{
3718 char_u name[2]; /* termcap name of entry */
3719 char_u *code; /* terminal code (in allocated memory) */
3720 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003721 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722} *termcodes = NULL;
3723
3724static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3725static int tc_len = 0; /* current number of entries in termcodes[] */
3726
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003727static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003728
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003730clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731{
3732 while (tc_len > 0)
3733 vim_free(termcodes[--tc_len].code);
3734 vim_free(termcodes);
3735 termcodes = NULL;
3736 tc_max_len = 0;
3737
3738#ifdef HAVE_TGETENT
3739 BC = (char *)empty_option;
3740 UP = (char *)empty_option;
3741 PC = NUL; /* set pad character to NUL */
3742 ospeed = 0;
3743#endif
3744
3745 need_gather = TRUE; /* need to fill termleader[] */
3746}
3747
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003748#define ATC_FROM_TERM 55
3749
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750/*
3751 * Add a new entry to the list of terminal codes.
3752 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003753 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3754 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 */
3756 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003757add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
3759 struct termcode *new_tc;
3760 int i, j;
3761 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003762 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763
3764 if (string == NULL || *string == NUL)
3765 {
3766 del_termcode(name);
3767 return;
3768 }
3769
Bram Moolenaar45500912014-07-09 20:51:07 +02003770#if defined(WIN3264) && !defined(FEAT_GUI)
3771 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3772#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 if (s == NULL)
3776 return;
3777
3778 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003779 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003781 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 s[0] = term_7to8bit(string);
3783 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003784
3785#if defined(WIN3264) && !defined(FEAT_GUI)
3786 if (s[0] == K_NUL)
3787 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003788 STRMOVE(s + 1, s);
3789 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003790 }
3791#endif
3792
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003793 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794
3795 need_gather = TRUE; /* need to fill termleader[] */
3796
3797 /*
3798 * need to make space for more entries
3799 */
3800 if (tc_len == tc_max_len)
3801 {
3802 tc_max_len += 20;
3803 new_tc = (struct termcode *)alloc(
3804 (unsigned)(tc_max_len * sizeof(struct termcode)));
3805 if (new_tc == NULL)
3806 {
3807 tc_max_len -= 20;
3808 return;
3809 }
3810 for (i = 0; i < tc_len; ++i)
3811 new_tc[i] = termcodes[i];
3812 vim_free(termcodes);
3813 termcodes = new_tc;
3814 }
3815
3816 /*
3817 * Look for existing entry with the same name, it is replaced.
3818 * Look for an existing entry that is alphabetical higher, the new entry
3819 * is inserted in front of it.
3820 */
3821 for (i = 0; i < tc_len; ++i)
3822 {
3823 if (termcodes[i].name[0] < name[0])
3824 continue;
3825 if (termcodes[i].name[0] == name[0])
3826 {
3827 if (termcodes[i].name[1] < name[1])
3828 continue;
3829 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003830 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 */
3832 if (termcodes[i].name[1] == name[1])
3833 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003834 if (flags == ATC_FROM_TERM && (j = termcode_star(
3835 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003836 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003837 /* Don't replace ESC[123;*X or ESC O*X with another when
3838 * invoked from got_code_from_term(). */
3839 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003840 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003841 && s[len - 1]
3842 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003843 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003844 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003845 vim_free(s);
3846 return;
3847 }
3848 }
3849 else
3850 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003851 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003852 vim_free(termcodes[i].code);
3853 --tc_len;
3854 break;
3855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
3857 }
3858 /*
3859 * Found alphabetical larger entry, move rest to insert new entry
3860 */
3861 for (j = tc_len; j > i; --j)
3862 termcodes[j] = termcodes[j - 1];
3863 break;
3864 }
3865
3866 termcodes[i].name[0] = name[0];
3867 termcodes[i].name[1] = name[1];
3868 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003869 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003870
3871 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3872 * accept modifiers. */
3873 termcodes[i].modlen = 0;
3874 j = termcode_star(s, len);
3875 if (j > 0)
3876 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 ++tc_len;
3878}
3879
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003880/*
3881 * Check termcode "code[len]" for ending in ;*X, <Esc>O*X or <M-O>*X.
3882 * The "X" can be any character.
3883 * Return 0 if not found, 2 for ;*X and 1 for O*X and <M-O>*X.
3884 */
3885 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003886termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003887{
3888 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3889 if (len >= 3 && code[len - 2] == '*')
3890 {
3891 if (len >= 5 && code[len - 3] == ';')
3892 return 2;
3893 if ((len >= 4 && code[len - 3] == 'O') || code[len - 3] == 'O' + 128)
3894 return 1;
3895 }
3896 return 0;
3897}
3898
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003900find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901{
3902 int i;
3903
3904 for (i = 0; i < tc_len; ++i)
3905 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3906 return termcodes[i].code;
3907 return NULL;
3908}
3909
3910#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3911 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003912get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913{
3914 if (i >= tc_len)
3915 return NULL;
3916 return &termcodes[i].name[0];
3917}
3918#endif
3919
3920 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003921del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922{
3923 int i;
3924
3925 if (termcodes == NULL) /* nothing there yet */
3926 return;
3927
3928 need_gather = TRUE; /* need to fill termleader[] */
3929
3930 for (i = 0; i < tc_len; ++i)
3931 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3932 {
3933 del_termcode_idx(i);
3934 return;
3935 }
3936 /* not found. Give error message? */
3937}
3938
3939 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003940del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941{
3942 int i;
3943
3944 vim_free(termcodes[idx].code);
3945 --tc_len;
3946 for (i = idx; i < tc_len; ++i)
3947 termcodes[i] = termcodes[i + 1];
3948}
3949
3950#ifdef FEAT_TERMRESPONSE
3951/*
3952 * Called when detected that the terminal sends 8-bit codes.
3953 * Convert all 7-bit codes to their 8-bit equivalent.
3954 */
3955 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003956switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957{
3958 int i;
3959 int c;
3960
3961 /* Only need to do something when not already using 8-bit codes. */
3962 if (!term_is_8bit(T_NAME))
3963 {
3964 for (i = 0; i < tc_len; ++i)
3965 {
3966 c = term_7to8bit(termcodes[i].code);
3967 if (c != 0)
3968 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003969 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 termcodes[i].code[0] = c;
3971 }
3972 }
3973 need_gather = TRUE; /* need to fill termleader[] */
3974 }
3975 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003976 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977}
3978#endif
3979
3980#ifdef CHECK_DOUBLE_CLICK
3981static linenr_T orig_topline = 0;
3982# ifdef FEAT_DIFF
3983static int orig_topfill = 0;
3984# endif
3985#endif
3986#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
3987/*
3988 * Checking for double clicks ourselves.
3989 * "orig_topline" is used to avoid detecting a double-click when the window
3990 * contents scrolled (e.g., when 'scrolloff' is non-zero).
3991 */
3992/*
3993 * Set orig_topline. Used when jumping to another window, so that a double
3994 * click still works.
3995 */
3996 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003997set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998{
3999 orig_topline = wp->w_topline;
4000# ifdef FEAT_DIFF
4001 orig_topfill = wp->w_topfill;
4002# endif
4003}
4004#endif
4005
4006/*
4007 * Check if typebuf.tb_buf[] contains a terminal key code.
4008 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
4009 * + max_offset].
4010 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004011 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 * With a match, the match is removed, the replacement code is inserted in
4013 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
4014 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004015 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
4016 * "buflen" is then the length of the string in buf[] and is updated for
4017 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 */
4019 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004020check_termcode(
4021 int max_offset,
4022 char_u *buf,
4023 int bufsize,
4024 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025{
4026 char_u *tp;
4027 char_u *p;
4028 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004029 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004031 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 int offset;
4033 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004034 int modifiers;
4035 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 int new_slen;
4037 int extra;
4038 char_u string[MAX_KEY_CODE_LEN + 1];
4039 int i, j;
4040 int idx = 0;
4041#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004042# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4043 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 char_u bytes[6];
4045 int num_bytes;
4046# endif
4047 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 int is_click, is_drag;
4049 int wheel_code = 0;
4050 int current_button;
4051 static int held_button = MOUSE_RELEASE;
4052 static int orig_num_clicks = 1;
4053 static int orig_mouse_code = 0x0;
4054# ifdef CHECK_DOUBLE_CLICK
4055 static int orig_mouse_col = 0;
4056 static int orig_mouse_row = 0;
4057 static struct timeval orig_mouse_time = {0, 0};
4058 /* time of previous mouse click */
4059 struct timeval mouse_time; /* time of current mouse click */
4060 long timediff; /* elapsed time in msec */
4061# endif
4062#endif
4063 int cpo_koffset;
4064#ifdef FEAT_MOUSE_GPM
4065 extern int gpm_flag; /* gpm library variable */
4066#endif
4067
4068 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4069
4070 /*
4071 * Speed up the checks for terminal codes by gathering all first bytes
4072 * used in termleader[]. Often this is just a single <Esc>.
4073 */
4074 if (need_gather)
4075 gather_termleader();
4076
4077 /*
4078 * Check at several positions in typebuf.tb_buf[], to catch something like
4079 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4080 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004081 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 * This is used often, KEEP IT FAST!
4083 */
4084 for (offset = 0; offset < max_offset; ++offset)
4085 {
4086 if (buf == NULL)
4087 {
4088 if (offset >= typebuf.tb_len)
4089 break;
4090 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4091 len = typebuf.tb_len - offset; /* length of the input */
4092 }
4093 else
4094 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004095 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 break;
4097 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004098 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
4100
4101 /*
4102 * Don't check characters after K_SPECIAL, those are already
4103 * translated terminal chars (avoid translating ~@^Hx).
4104 */
4105 if (*tp == K_SPECIAL)
4106 {
4107 offset += 2; /* there are always 2 extra characters */
4108 continue;
4109 }
4110
4111 /*
4112 * Skip this position if the character does not appear as the first
4113 * character in term_strings. This speeds up a lot, since most
4114 * termcodes start with the same character (ESC or CSI).
4115 */
4116 i = *tp;
4117 for (p = termleader; *p && *p != i; ++p)
4118 ;
4119 if (*p == NUL)
4120 continue;
4121
4122 /*
4123 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4124 * are in Insert mode.
4125 */
4126 if (*tp == ESC && !p_ek && (State & INSERT))
4127 continue;
4128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004130 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004131 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133#ifdef FEAT_GUI
4134 if (gui.in_use)
4135 {
4136 /*
4137 * GUI special key codes are all of the form [CSI xx].
4138 */
4139 if (*tp == CSI) /* Special key from GUI */
4140 {
4141 if (len < 3)
4142 return -1; /* Shouldn't happen */
4143 slen = 3;
4144 key_name[0] = tp[1];
4145 key_name[1] = tp[2];
4146 }
4147 }
4148 else
4149#endif /* FEAT_GUI */
4150 {
4151 for (idx = 0; idx < tc_len; ++idx)
4152 {
4153 /*
4154 * Ignore the entry if we are not at the start of
4155 * typebuf.tb_buf[]
4156 * and there are not enough characters to make a match.
4157 * But only when the 'K' flag is in 'cpoptions'.
4158 */
4159 slen = termcodes[idx].len;
4160 if (cpo_koffset && offset && len < slen)
4161 continue;
4162 if (STRNCMP(termcodes[idx].code, tp,
4163 (size_t)(slen > len ? len : slen)) == 0)
4164 {
4165 if (len < slen) /* got a partial sequence */
4166 return -1; /* need to get more chars */
4167
4168 /*
4169 * When found a keypad key, check if there is another key
4170 * that matches and use that one. This makes <Home> to be
4171 * found instead of <kHome> when they produce the same
4172 * key code.
4173 */
4174 if (termcodes[idx].name[0] == 'K'
4175 && VIM_ISDIGIT(termcodes[idx].name[1]))
4176 {
4177 for (j = idx + 1; j < tc_len; ++j)
4178 if (termcodes[j].len == slen &&
4179 STRNCMP(termcodes[idx].code,
4180 termcodes[j].code, slen) == 0)
4181 {
4182 idx = j;
4183 break;
4184 }
4185 }
4186
4187 key_name[0] = termcodes[idx].name[0];
4188 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 break;
4190 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004191
4192 /*
4193 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004194 * <Esc>[123;*X (modslen == slen - 3)
4195 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4196 * When there is a modifier the * matches a number.
4197 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004198 */
4199 if (termcodes[idx].modlen > 0)
4200 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004201 modslen = termcodes[idx].modlen;
4202 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004203 continue;
4204 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004205 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004206 {
4207 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004208
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004209 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004210 return -1; /* need to get more chars */
4211
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004212 if (tp[modslen] == termcodes[idx].code[slen - 1])
4213 slen = modslen + 1; /* no modifiers */
4214 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004215 continue; /* no match */
4216 else
4217 {
4218 /* Skip over the digits, the final char must
4219 * follow. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004220 for (j = slen - 2; j < len && isdigit(tp[j]); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004221 ;
4222 ++j;
4223 if (len < j) /* got a partial sequence */
4224 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004225 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4226 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004227
4228 /* Match! Convert modifier bits. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004229 n = atoi((char *)tp + slen - 2) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004230 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004231 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004232 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004233 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004234 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004235 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004236 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004237 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004238
4239 slen = j;
4240 }
4241 key_name[0] = termcodes[idx].name[0];
4242 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004243 break;
4244 }
4245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247 }
4248
4249#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004250 if (key_name[0] == NUL
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004251 /* Mouse codes of DEC, pterm, and URXVT start with <ESC>[. When
4252 * detecting the start of these mouse codes they might as well be
4253 * another key code or terminal response. */
4254# ifdef FEAT_MOUSE_DEC
4255 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004256# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004257# ifdef FEAT_MOUSE_PTERM
4258 || key_name[0] == KS_PTERM_MOUSE
4259# endif
4260# ifdef FEAT_MOUSE_URXVT
4261 || key_name[0] == KS_URXVT_MOUSE
4262# endif
4263 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004265 /* Check for some responses from the terminal starting with
4266 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004267 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004268 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01004269 * Also eat other possible responses to t_RV, rxvt returns
4270 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4271 * mrxvt has been reported to have "+" in the version. Assume
4272 * the escape sequence ends with a letter or one of "{|}~".
4273 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004274 * - Cursor position report: <Esc>[{row};{col}R
4275 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004276 * ambiguous-width character state.
4277 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004278 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004279
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004280 if ((*T_CRV != NUL || *T_U7 != NUL)
4281 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004282 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004283 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004285#ifdef FEAT_MBYTE
4286 int col;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004287 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 j = 0;
4290 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004291 for (i = 2 + (tp[0] != CSI); i < len
4292 && !(tp[i] >= '{' && tp[i] <= '~')
4293 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 if (tp[i] == ';' && ++j == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004295 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004296 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004297#ifdef FEAT_MBYTE
4298 row_char = tp[i - 1];
4299#endif
4300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004302 {
4303 LOG_TR("Not enough characters for CRV");
4304 return -1;
4305 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004306#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004307 if (extra > 0)
4308 col = atoi((char *)tp + extra);
4309 else
4310 col = 0;
4311
4312 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4313 * u7_status is not "sent", it may be from a previous Vim that
4314 * just exited. But not for <S-F3>, it sends something
4315 * similar, check for row and column to make sense. */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004316 if (j == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004317 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004318 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004319 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004320 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004321
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004322 LOG_TR("Received U7 status");
4323 u7_status = U7_GOT;
4324# ifdef FEAT_AUTOCMD
4325 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004326# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004327 if (col == 2)
4328 aw = "single";
4329 else if (col == 3)
4330 aw = "double";
4331 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4332 {
4333 /* Setting the option causes a screen redraw. Do
4334 * that right away if possible, keeping any
4335 * messages. */
4336 set_option_value((char_u *)"ambw", 0L,
4337 (char_u *)aw, 0);
4338# ifdef DEBUG_TERMRESPONSE
4339 {
4340 char buf[100];
4341 int r = redraw_asap(CLEAR);
4342
4343 sprintf(buf,
4344 "set 'ambiwidth', redraw_asap(): %d",
4345 r);
4346 log_tr(buf);
4347 }
4348# else
4349 redraw_asap(CLEAR);
4350# endif
4351 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004352 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004353 key_name[0] = (int)KS_EXTRA;
4354 key_name[1] = (int)KE_IGNORE;
4355 slen = i + 1;
4356 }
4357 else
4358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004360 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004362 LOG_TR("Received CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 crv_status = CRV_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004364# ifdef FEAT_AUTOCMD
4365 did_cursorhold = TRUE;
4366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
4368 /* If this code starts with CSI, you can bet that the
4369 * terminal uses 8-bit codes. */
4370 if (tp[0] == CSI)
4371 switch_to_8bit();
4372
4373 /* rxvt sends its version number: "20703" is 2.7.3.
4374 * Ignore it for when the user has set 'term' to xterm,
4375 * even though it's an rxvt. */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004376 if (extra > 0)
4377 extra = atoi((char *)tp + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 if (extra > 20000)
4379 extra = 0;
4380
4381 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
4382 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004383 /* Only set 'ttymouse' automatically if it was not set
4384 * by the user already. */
4385 if (!option_was_set((char_u *)"ttym"))
4386 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004387# ifdef TTYM_SGR
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004388 if (extra >= 277)
4389 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004390 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004391 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004392# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004393 /* if xterm version >= 95 use mouse dragging */
4394 if (extra >= 95)
4395 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004397 }
4398
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 /* if xterm version >= 141 try to get termcap codes */
4400 if (extra >= 141)
4401 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004402 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 check_for_codes = TRUE;
4404 need_gather = TRUE;
4405 req_codes_from_term();
4406 }
4407 }
4408# ifdef FEAT_EVAL
4409 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4410# endif
4411# ifdef FEAT_AUTOCMD
4412 apply_autocmds(EVENT_TERMRESPONSE,
4413 NULL, NULL, FALSE, curbuf);
4414# endif
4415 key_name[0] = (int)KS_EXTRA;
4416 key_name[1] = (int)KE_IGNORE;
4417 slen = i + 1;
4418 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004419 }
4420
4421 /* Check for background color response from the terminal:
4422 *
4423 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4424 *
4425 * {lead} can be <Esc>] or OSC
4426 * {tail} can be '\007', <Esc>\ or STERM.
4427 *
4428 * Consume any code that starts with "{lead}11;", it's also
4429 * possible that "rgba" is following.
4430 */
4431 else if (*T_RBG != NUL
4432 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4433 || tp[0] == OSC))
4434 {
4435 j = 1 + (tp[0] == ESC);
4436 if (len >= j + 3 && (argp[0] != '1'
4437 || argp[1] != '1' || argp[2] != ';'))
4438 i = 0; /* no match */
4439 else
4440 for (i = j; i < len; ++i)
4441 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4442 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004443 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004444 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4445 && tp[j + 11] == '/' && tp[j + 16] == '/'
4446 && !option_was_set((char_u *)"bg"))
4447 {/* TODO: don't set option when already the right value */
4448 LOG_TR("Received RBG");
4449 rbg_status = RBG_GOT;
4450 set_option_value((char_u *)"bg", 0L, (char_u *)(
4451 (3 * '6' < tp[j+7] + tp[j+12] + tp[j+17])
4452 ? "light" : "dark"), 0);
4453 reset_option_was_set((char_u *)"bg");
4454 redraw_asap(CLEAR);
4455 }
4456
4457 /* got finished code: consume it */
4458 key_name[0] = (int)KS_EXTRA;
4459 key_name[1] = (int)KE_IGNORE;
4460 slen = i + 1 + (tp[i] == ESC);
4461 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004462 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004463 if (i == len)
4464 {
4465 LOG_TR("not enough characters for RB");
4466 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004470 /* Check for key code response from xterm:
4471 *
4472 * {lead}{flag}+r<hex bytes><{tail}
4473 *
4474 * {lead} can be <Esc>P or DCS
4475 * {flag} can be '0' or '1'
4476 * {tail} can be Esc>\ or STERM
4477 *
4478 * Consume any code that starts with "{lead}.+r".
4479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 else if (check_for_codes
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004481 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 || tp[0] == DCS))
4483 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004484 j = 1 + (tp[0] == ESC);
4485 if (len >= j + 3 && (argp[1] != '+' || argp[2] != 'r'))
4486 i = 0; /* no match */
4487 else
4488 for (i = j; i < len; ++i)
4489 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 || tp[i] == STERM)
4491 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004492 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 got_code_from_term(tp + j, i);
4494 key_name[0] = (int)KS_EXTRA;
4495 key_name[1] = (int)KE_IGNORE;
4496 slen = i + 1 + (tp[i] == ESC);
4497 break;
4498 }
4499
4500 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004501 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004502 /* These codes arrive many together, each code can be
4503 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004504 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004505 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508 }
4509#endif
4510
4511 if (key_name[0] == NUL)
4512 continue; /* No match at this position, try next one */
4513
4514 /* We only get here when we have a complete termcode match */
4515
4516#ifdef FEAT_MOUSE
4517# ifdef FEAT_GUI
4518 /*
4519 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4520 * so that we know which window to scroll later.
4521 */
4522 if (gui.in_use
4523 && key_name[0] == (int)KS_EXTRA
4524 && (key_name[1] == (int)KE_X1MOUSE
4525 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004526 || key_name[1] == (int)KE_MOUSELEFT
4527 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 || key_name[1] == (int)KE_MOUSEDOWN
4529 || key_name[1] == (int)KE_MOUSEUP))
4530 {
4531 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4532 if (num_bytes == -1) /* not enough coordinates */
4533 return -1;
4534 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4535 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4536 slen += num_bytes;
4537 }
4538 else
4539# endif
4540 /*
4541 * If it is a mouse click, get the coordinates.
4542 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004543 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004545 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546# endif
4547# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004548 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549# endif
4550# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004551 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552# endif
4553# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004554 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004556# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004557 || key_name[0] == KS_URXVT_MOUSE
4558# endif
4559# ifdef FEAT_MOUSE_SGR
4560 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004561# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 )
4563 {
4564 is_click = is_drag = FALSE;
4565
Bram Moolenaar864207d2008-06-24 22:14:38 +00004566# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4567 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 if (key_name[0] == (int)KS_MOUSE)
4569 {
4570 /*
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004571 * For xterm we get "<t_mouse>scr", where
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 * s == encoded button state:
4573 * 0x20 = left button down
4574 * 0x21 = middle button down
4575 * 0x22 = right button down
4576 * 0x23 = any button release
4577 * 0x60 = button 4 down (scroll wheel down)
4578 * 0x61 = button 5 down (scroll wheel up)
4579 * add 0x04 for SHIFT
4580 * add 0x08 for ALT
4581 * add 0x10 for CTRL
4582 * add 0x20 for mouse drag (0x40 is drag with left button)
4583 * c == column + ' ' + 1 == column + 33
4584 * r == row + ' ' + 1 == row + 33
4585 *
4586 * The coordinates are passed on through global variables.
4587 * Ugly, but this avoids trouble with mouse clicks at an
4588 * unexpected moment and allows for mapping them.
4589 */
4590 for (;;)
4591 {
4592#ifdef FEAT_GUI
4593 if (gui.in_use)
4594 {
4595 /* GUI uses more bits for columns > 223 */
4596 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4597 if (num_bytes == -1) /* not enough coordinates */
4598 return -1;
4599 mouse_code = bytes[0];
4600 mouse_col = 128 * (bytes[1] - ' ' - 1)
4601 + bytes[2] - ' ' - 1;
4602 mouse_row = 128 * (bytes[3] - ' ' - 1)
4603 + bytes[4] - ' ' - 1;
4604 }
4605 else
4606#endif
4607 {
4608 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4609 if (num_bytes == -1) /* not enough coordinates */
4610 return -1;
4611 mouse_code = bytes[0];
4612 mouse_col = bytes[1] - ' ' - 1;
4613 mouse_row = bytes[2] - ' ' - 1;
4614 }
4615 slen += num_bytes;
4616
4617 /* If the following bytes is also a mouse code and it has
4618 * the same code, dump this one and get the next. This
4619 * makes dragging a whole lot faster. */
4620#ifdef FEAT_GUI
4621 if (gui.in_use)
4622 j = 3;
4623 else
4624#endif
4625 j = termcodes[idx].len;
4626 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4627 && tp[slen + j] == mouse_code
4628 && tp[slen + j + 1] != NUL
4629 && tp[slen + j + 2] != NUL
4630#ifdef FEAT_GUI
4631 && (!gui.in_use
4632 || (tp[slen + j + 3] != NUL
4633 && tp[slen + j + 4] != NUL))
4634#endif
4635 )
4636 slen += j;
4637 else
4638 break;
4639 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004642# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4643 if (key_name[0] == KS_URXVT_MOUSE
4644 || key_name[0] == KS_SGR_MOUSE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004645 {
4646 for (;;)
4647 {
4648 /* URXVT 1015 mouse reporting mode:
4649 * Almost identical to xterm mouse mode, except the values
4650 * are decimal instead of bytes.
4651 *
4652 * \033[%d;%d;%dM
4653 * ^-- row
4654 * ^----- column
4655 * ^-------- code
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004656 *
4657 * SGR 1006 mouse reporting mode:
4658 * Almost identical to xterm mouse mode, except the values
4659 * are decimal instead of bytes.
4660 *
4661 * \033[<%d;%d;%dM
4662 * ^-- row
4663 * ^----- column
4664 * ^-------- code
4665 *
4666 * \033[<%d;%d;%dm : mouse release event
4667 * ^-- row
4668 * ^----- column
4669 * ^-------- code
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004670 */
4671 p = tp + slen;
4672
4673 mouse_code = getdigits(&p);
4674 if (*p++ != ';')
4675 return -1;
4676
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004677 /* when mouse reporting is SGR, add 32 to mouse code */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004678 if (key_name[0] == KS_SGR_MOUSE)
4679 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004680
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004681 mouse_col = getdigits(&p) - 1;
4682 if (*p++ != ';')
4683 return -1;
4684
4685 mouse_row = getdigits(&p) - 1;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004686 if (key_name[0] == KS_SGR_MOUSE && *p == 'm')
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004687 mouse_code |= MOUSE_RELEASE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004688 else if (*p != 'M')
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004689 return -1;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004690 p++;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004691
4692 slen += (int)(p - (tp + slen));
4693
4694 /* skip this one if next one has same code (like xterm
4695 * case) */
4696 j = termcodes[idx].len;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004697 if (STRNCMP(tp, tp + slen, (size_t)j) == 0)
4698 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004699 int slen2;
4700 int cmd_complete = 0;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004701
4702 /* check if the command is complete by looking for the
4703 * 'M' */
4704 for (slen2 = slen; slen2 < len; slen2++)
4705 {
4706 if (tp[slen2] == 'M'
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004707 || (key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004708 && tp[slen2] == 'm'))
4709 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004710 cmd_complete = 1;
4711 break;
4712 }
4713 }
4714 p += j;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004715 if (cmd_complete && getdigits(&p) == mouse_code)
4716 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004717 slen += j; /* skip the \033[ */
4718 continue;
4719 }
4720 }
4721 break;
4722 }
4723 }
4724# endif
4725
4726 if (key_name[0] == (int)KS_MOUSE
4727#ifdef FEAT_MOUSE_URXVT
4728 || key_name[0] == (int)KS_URXVT_MOUSE
4729#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004730#ifdef FEAT_MOUSE_SGR
4731 || key_name[0] == KS_SGR_MOUSE
4732#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004733 )
4734 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004735# if !defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 /*
4737 * Handle mouse events.
4738 * Recognize the xterm mouse wheel, but not in the GUI, the
4739 * Linux console with GPM and the MS-DOS or Win32 console
4740 * (multi-clicks use >= 0x60).
4741 */
4742 if (mouse_code >= MOUSEWHEEL_LOW
4743# ifdef FEAT_GUI
4744 && !gui.in_use
4745# endif
4746# ifdef FEAT_MOUSE_GPM
4747 && gpm_flag == 0
4748# endif
4749 )
4750 {
4751 /* Keep the mouse_code before it's changed, so that we
4752 * remember that it was a mouse wheel click. */
4753 wheel_code = mouse_code;
4754 }
4755# ifdef FEAT_MOUSE_XTERM
4756 else if (held_button == MOUSE_RELEASE
4757# ifdef FEAT_GUI
4758 && !gui.in_use
4759# endif
4760 && (mouse_code == 0x23 || mouse_code == 0x24))
4761 {
4762 /* Apparently used by rxvt scroll wheel. */
4763 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4764 }
4765# endif
4766
4767# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4768 else if (use_xterm_mouse() > 1)
4769 {
4770 if (mouse_code & MOUSE_DRAG_XTERM)
4771 mouse_code |= MOUSE_DRAG;
4772 }
4773# endif
4774# ifdef FEAT_XCLIPBOARD
4775 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4776 {
4777 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4778 stop_xterm_trace();
4779 else
4780 start_xterm_trace(mouse_code);
4781 }
4782# endif
4783# endif
4784 }
4785# endif /* !UNIX || FEAT_MOUSE_XTERM */
4786# ifdef FEAT_MOUSE_NET
4787 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4788 {
4789 int mc, mr;
4790
4791 /* expect a rather limited sequence like: balancing {
4792 * \033}6,45\r
4793 * '6' is the row, 45 is the column
4794 */
4795 p = tp + slen;
4796 mr = getdigits(&p);
4797 if (*p++ != ',')
4798 return -1;
4799 mc = getdigits(&p);
4800 if (*p++ != '\r')
4801 return -1;
4802
4803 mouse_col = mc - 1;
4804 mouse_row = mr - 1;
4805 mouse_code = MOUSE_LEFT;
4806 slen += (int)(p - (tp + slen));
4807 }
4808# endif /* FEAT_MOUSE_NET */
4809# ifdef FEAT_MOUSE_JSB
4810 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4811 {
4812 int mult, val, iter, button, status;
4813
4814 /* JSBTERM Input Model
4815 * \033[0~zw uniq escape sequence
4816 * (L-x) Left button pressed - not pressed x not reporting
4817 * (M-x) Middle button pressed - not pressed x not reporting
4818 * (R-x) Right button pressed - not pressed x not reporting
4819 * (SDmdu) Single , Double click, m mouse move d button down
4820 * u button up
4821 * ### X cursor position padded to 3 digits
4822 * ### Y cursor position padded to 3 digits
4823 * (s-x) SHIFT key pressed - not pressed x not reporting
4824 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004825 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 */
4827
4828 p = tp + slen;
4829 button = mouse_code = 0;
4830 switch (*p++)
4831 {
4832 case 'L': button = 1; break;
4833 case '-': break;
4834 case 'x': break; /* ignore sequence */
4835 default: return -1; /* Unknown Result */
4836 }
4837 switch (*p++)
4838 {
4839 case 'M': button |= 2; break;
4840 case '-': break;
4841 case 'x': break; /* ignore sequence */
4842 default: return -1; /* Unknown Result */
4843 }
4844 switch (*p++)
4845 {
4846 case 'R': button |= 4; break;
4847 case '-': break;
4848 case 'x': break; /* ignore sequence */
4849 default: return -1; /* Unknown Result */
4850 }
4851 status = *p++;
4852 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4853 mult /= 10, p++)
4854 if (*p >= '0' && *p <= '9')
4855 val += (*p - '0') * mult;
4856 else
4857 return -1;
4858 mouse_col = val;
4859 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4860 mult /= 10, p++)
4861 if (*p >= '0' && *p <= '9')
4862 val += (*p - '0') * mult;
4863 else
4864 return -1;
4865 mouse_row = val;
4866 switch (*p++)
4867 {
4868 case 's': button |= 8; break; /* SHIFT key Pressed */
4869 case '-': break; /* Not Pressed */
4870 case 'x': break; /* Not Reporting */
4871 default: return -1; /* Unknown Result */
4872 }
4873 switch (*p++)
4874 {
4875 case 'c': button |= 16; break; /* CTRL key Pressed */
4876 case '-': break; /* Not Pressed */
4877 case 'x': break; /* Not Reporting */
4878 default: return -1; /* Unknown Result */
4879 }
4880 if (*p++ != '\033')
4881 return -1;
4882 if (*p++ != '\\')
4883 return -1;
4884 switch (status)
4885 {
4886 case 'D': /* Double Click */
4887 case 'S': /* Single Click */
4888 if (button & 1) mouse_code |= MOUSE_LEFT;
4889 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4890 if (button & 4) mouse_code |= MOUSE_RIGHT;
4891 if (button & 8) mouse_code |= MOUSE_SHIFT;
4892 if (button & 16) mouse_code |= MOUSE_CTRL;
4893 break;
4894 case 'm': /* Mouse move */
4895 if (button & 1) mouse_code |= MOUSE_LEFT;
4896 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4897 if (button & 4) mouse_code |= MOUSE_RIGHT;
4898 if (button & 8) mouse_code |= MOUSE_SHIFT;
4899 if (button & 16) mouse_code |= MOUSE_CTRL;
4900 if ((button & 7) != 0)
4901 {
4902 held_button = mouse_code;
4903 mouse_code |= MOUSE_DRAG;
4904 }
4905 is_drag = TRUE;
4906 showmode();
4907 break;
4908 case 'd': /* Button Down */
4909 if (button & 1) mouse_code |= MOUSE_LEFT;
4910 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4911 if (button & 4) mouse_code |= MOUSE_RIGHT;
4912 if (button & 8) mouse_code |= MOUSE_SHIFT;
4913 if (button & 16) mouse_code |= MOUSE_CTRL;
4914 break;
4915 case 'u': /* Button Up */
4916 if (button & 1)
4917 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4918 if (button & 2)
4919 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4920 if (button & 4)
4921 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4922 if (button & 8)
4923 mouse_code |= MOUSE_SHIFT;
4924 if (button & 16)
4925 mouse_code |= MOUSE_CTRL;
4926 break;
4927 default: return -1; /* Unknown Result */
4928 }
4929
4930 slen += (p - (tp + slen));
4931 }
4932# endif /* FEAT_MOUSE_JSB */
4933# ifdef FEAT_MOUSE_DEC
4934 if (key_name[0] == (int)KS_DEC_MOUSE)
4935 {
4936 /* The DEC Locator Input Model
4937 * Netterm delivers the code sequence:
4938 * \033[2;4;24;80&w (left button down)
4939 * \033[3;0;24;80&w (left button up)
4940 * \033[6;1;24;80&w (right button down)
4941 * \033[7;0;24;80&w (right button up)
4942 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4943 * Pe is the event code
4944 * Pb is the button code
4945 * Pr is the row coordinate
4946 * Pc is the column coordinate
4947 * Pp is the third coordinate (page number)
4948 * Pe, the event code indicates what event caused this report
4949 * The following event codes are defined:
4950 * 0 - request, the terminal received an explicit request
4951 * for a locator report, but the locator is unavailable
4952 * 1 - request, the terminal received an explicit request
4953 * for a locator report
4954 * 2 - left button down
4955 * 3 - left button up
4956 * 4 - middle button down
4957 * 5 - middle button up
4958 * 6 - right button down
4959 * 7 - right button up
4960 * 8 - fourth button down
4961 * 9 - fourth button up
4962 * 10 - locator outside filter rectangle
4963 * Pb, the button code, ASCII decimal 0-15 indicating which
4964 * buttons are down if any. The state of the four buttons
4965 * on the locator correspond to the low four bits of the
4966 * decimal value,
4967 * "1" means button depressed
4968 * 0 - no buttons down,
4969 * 1 - right,
4970 * 2 - middle,
4971 * 4 - left,
4972 * 8 - fourth
4973 * Pr is the row coordinate of the locator position in the page,
4974 * encoded as an ASCII decimal value.
4975 * If Pr is omitted, the locator position is undefined
4976 * (outside the terminal window for example).
4977 * Pc is the column coordinate of the locator position in the
4978 * page, encoded as an ASCII decimal value.
4979 * If Pc is omitted, the locator position is undefined
4980 * (outside the terminal window for example).
4981 * Pp is the page coordinate of the locator position
4982 * encoded as an ASCII decimal value.
4983 * The page coordinate may be omitted if the locator is on
4984 * page one (the default). We ignore it anyway.
4985 */
4986 int Pe, Pb, Pr, Pc;
4987
4988 p = tp + slen;
4989
4990 /* get event status */
4991 Pe = getdigits(&p);
4992 if (*p++ != ';')
4993 return -1;
4994
4995 /* get button status */
4996 Pb = getdigits(&p);
4997 if (*p++ != ';')
4998 return -1;
4999
5000 /* get row status */
5001 Pr = getdigits(&p);
5002 if (*p++ != ';')
5003 return -1;
5004
5005 /* get column status */
5006 Pc = getdigits(&p);
5007
5008 /* the page parameter is optional */
5009 if (*p == ';')
5010 {
5011 p++;
5012 (void)getdigits(&p);
5013 }
5014 if (*p++ != '&')
5015 return -1;
5016 if (*p++ != 'w')
5017 return -1;
5018
5019 mouse_code = 0;
5020 switch (Pe)
5021 {
5022 case 0: return -1; /* position request while unavailable */
5023 case 1: /* a response to a locator position request includes
5024 the status of all buttons */
5025 Pb &= 7; /* mask off and ignore fourth button */
5026 if (Pb & 4)
5027 mouse_code = MOUSE_LEFT;
5028 if (Pb & 2)
5029 mouse_code = MOUSE_MIDDLE;
5030 if (Pb & 1)
5031 mouse_code = MOUSE_RIGHT;
5032 if (Pb)
5033 {
5034 held_button = mouse_code;
5035 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005036 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038 is_drag = TRUE;
5039 showmode();
5040 break;
5041 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005042 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 break;
5044 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5045 break;
5046 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005047 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 break;
5049 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5050 break;
5051 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005052 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 break;
5054 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5055 break;
5056 case 8: return -1; /* fourth button down */
5057 case 9: return -1; /* fourth button up */
5058 case 10: return -1; /* mouse outside of filter rectangle */
5059 default: return -1; /* should never occur */
5060 }
5061
5062 mouse_col = Pc - 1;
5063 mouse_row = Pr - 1;
5064
5065 slen += (int)(p - (tp + slen));
5066 }
5067# endif /* FEAT_MOUSE_DEC */
5068# ifdef FEAT_MOUSE_PTERM
5069 if (key_name[0] == (int)KS_PTERM_MOUSE)
5070 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005071 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 p = tp + slen;
5074
5075 action = getdigits(&p);
5076 if (*p++ != ';')
5077 return -1;
5078
5079 mouse_row = getdigits(&p);
5080 if (*p++ != ';')
5081 return -1;
5082 mouse_col = getdigits(&p);
5083 if (*p++ != ';')
5084 return -1;
5085
5086 button = getdigits(&p);
5087 mouse_code = 0;
5088
5089 switch( button )
5090 {
5091 case 4: mouse_code = MOUSE_LEFT; break;
5092 case 1: mouse_code = MOUSE_RIGHT; break;
5093 case 2: mouse_code = MOUSE_MIDDLE; break;
5094 default: return -1;
5095 }
5096
5097 switch( action )
5098 {
5099 case 31: /* Initial press */
5100 if (*p++ != ';')
5101 return -1;
5102
5103 num_clicks = getdigits(&p); /* Not used */
5104 break;
5105
5106 case 32: /* Release */
5107 mouse_code |= MOUSE_RELEASE;
5108 break;
5109
5110 case 33: /* Drag */
5111 held_button = mouse_code;
5112 mouse_code |= MOUSE_DRAG;
5113 break;
5114
5115 default:
5116 return -1;
5117 }
5118
5119 if (*p++ != 't')
5120 return -1;
5121
5122 slen += (p - (tp + slen));
5123 }
5124# endif /* FEAT_MOUSE_PTERM */
5125
5126 /* Interpret the mouse code */
5127 current_button = (mouse_code & MOUSE_CLICK_MASK);
5128 if (current_button == MOUSE_RELEASE
5129# ifdef FEAT_MOUSE_XTERM
5130 && wheel_code == 0
5131# endif
5132 )
5133 {
5134 /*
5135 * If we get a mouse drag or release event when
5136 * there is no mouse button held down (held_button ==
5137 * MOUSE_RELEASE), produce a K_IGNORE below.
5138 * (can happen when you hold down two buttons
5139 * and then let them go, or click in the menu bar, but not
5140 * on a menu, and drag into the text).
5141 */
5142 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5143 is_drag = TRUE;
5144 current_button = held_button;
5145 }
5146 else if (wheel_code == 0)
5147 {
5148# ifdef CHECK_DOUBLE_CLICK
5149# ifdef FEAT_MOUSE_GPM
5150# ifdef FEAT_GUI
5151 /*
5152 * Only for Unix, when GUI or gpm is not active, we handle
5153 * multi-clicks here.
5154 */
5155 if (gpm_flag == 0 && !gui.in_use)
5156# else
5157 if (gpm_flag == 0)
5158# endif
5159# else
5160# ifdef FEAT_GUI
5161 if (!gui.in_use)
5162# endif
5163# endif
5164 {
5165 /*
5166 * Compute the time elapsed since the previous mouse click.
5167 */
5168 gettimeofday(&mouse_time, NULL);
5169 timediff = (mouse_time.tv_usec
5170 - orig_mouse_time.tv_usec) / 1000;
5171 if (timediff < 0)
5172 --orig_mouse_time.tv_sec;
5173 timediff += (mouse_time.tv_sec
5174 - orig_mouse_time.tv_sec) * 1000;
5175 orig_mouse_time = mouse_time;
5176 if (mouse_code == orig_mouse_code
5177 && timediff < p_mouset
5178 && orig_num_clicks != 4
5179 && orig_mouse_col == mouse_col
5180 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005181 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005183 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005185 )
5186#ifdef FEAT_WINDOWS
5187 /* Double click in tab pages line also works
5188 * when window contents changes. */
5189 || (mouse_row == 0 && firstwin->w_winrow > 0)
5190#endif
5191 )
5192 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 ++orig_num_clicks;
5194 else
5195 orig_num_clicks = 1;
5196 orig_mouse_col = mouse_col;
5197 orig_mouse_row = mouse_row;
5198 orig_topline = curwin->w_topline;
5199#ifdef FEAT_DIFF
5200 orig_topfill = curwin->w_topfill;
5201#endif
5202 }
5203# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5204 else
5205 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5206# endif
5207# else
5208 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5209# endif
5210 is_click = TRUE;
5211 orig_mouse_code = mouse_code;
5212 }
5213 if (!is_drag)
5214 held_button = mouse_code & MOUSE_CLICK_MASK;
5215
5216 /*
5217 * Translate the actual mouse event into a pseudo mouse event.
5218 * First work out what modifiers are to be used.
5219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 if (orig_mouse_code & MOUSE_SHIFT)
5221 modifiers |= MOD_MASK_SHIFT;
5222 if (orig_mouse_code & MOUSE_CTRL)
5223 modifiers |= MOD_MASK_CTRL;
5224 if (orig_mouse_code & MOUSE_ALT)
5225 modifiers |= MOD_MASK_ALT;
5226 if (orig_num_clicks == 2)
5227 modifiers |= MOD_MASK_2CLICK;
5228 else if (orig_num_clicks == 3)
5229 modifiers |= MOD_MASK_3CLICK;
5230 else if (orig_num_clicks == 4)
5231 modifiers |= MOD_MASK_4CLICK;
5232
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 /* Work out our pseudo mouse event */
5234 key_name[0] = (int)KS_EXTRA;
5235 if (wheel_code != 0)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005236 {
5237 if (wheel_code & MOUSE_CTRL)
5238 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005239 if (wheel_code & MOUSE_ALT)
5240 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 key_name[1] = (wheel_code & 1)
5242 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 else
5245 key_name[1] = get_pseudo_mouse_code(current_button,
5246 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005247
5248 /* Make sure the mouse position is valid. Some terminals may
5249 * return weird values. */
5250 if (mouse_col >= Columns)
5251 mouse_col = Columns - 1;
5252 if (mouse_row >= Rows)
5253 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 }
5255#endif /* FEAT_MOUSE */
5256
5257#ifdef FEAT_GUI
5258 /*
5259 * If using the GUI, then we get menu and scrollbar events.
5260 *
5261 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5262 * four bytes which are to be taken as a pointer to the vimmenu_T
5263 * structure.
5264 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005265 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005266 * is one byte with the tab index.
5267 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5269 * by one byte representing the scrollbar number, and then four bytes
5270 * representing a long_u which is the new value of the scrollbar.
5271 *
5272 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5273 * KE_FILLER followed by four bytes representing a long_u which is the
5274 * new value of the scrollbar.
5275 */
5276# ifdef FEAT_MENU
5277 else if (key_name[0] == (int)KS_MENU)
5278 {
5279 long_u val;
5280
5281 num_bytes = get_long_from_buf(tp + slen, &val);
5282 if (num_bytes == -1)
5283 return -1;
5284 current_menu = (vimmenu_T *)val;
5285 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005286
5287 /* The menu may have been deleted right after it was used, check
5288 * for that. */
5289 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5290 {
5291 key_name[0] = KS_EXTRA;
5292 key_name[1] = (int)KE_IGNORE;
5293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 }
5295# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005296# ifdef FEAT_GUI_TABLINE
5297 else if (key_name[0] == (int)KS_TABLINE)
5298 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005299 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005300 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5301 if (num_bytes == -1)
5302 return -1;
5303 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005304 if (current_tab == 255) /* -1 in a byte gives 255 */
5305 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005306 slen += num_bytes;
5307 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005308 else if (key_name[0] == (int)KS_TABMENU)
5309 {
5310 /* Selecting tabline tab or using its menu. */
5311 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5312 if (num_bytes == -1)
5313 return -1;
5314 current_tab = (int)bytes[0];
5315 current_tabmenu = (int)bytes[1];
5316 slen += num_bytes;
5317 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319# ifndef USE_ON_FLY_SCROLL
5320 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5321 {
5322 long_u val;
5323
5324 /* Get the last scrollbar event in the queue of the same type */
5325 j = 0;
5326 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5327 && tp[j + 2] != NUL; ++i)
5328 {
5329 j += 3;
5330 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5331 if (num_bytes == -1)
5332 break;
5333 if (i == 0)
5334 current_scrollbar = (int)bytes[0];
5335 else if (current_scrollbar != (int)bytes[0])
5336 break;
5337 j += num_bytes;
5338 num_bytes = get_long_from_buf(tp + j, &val);
5339 if (num_bytes == -1)
5340 break;
5341 scrollbar_value = val;
5342 j += num_bytes;
5343 slen = j;
5344 }
5345 if (i == 0) /* not enough characters to make one */
5346 return -1;
5347 }
5348 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5349 {
5350 long_u val;
5351
5352 /* Get the last horiz. scrollbar event in the queue */
5353 j = 0;
5354 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5355 && tp[j + 2] != NUL; ++i)
5356 {
5357 j += 3;
5358 num_bytes = get_long_from_buf(tp + j, &val);
5359 if (num_bytes == -1)
5360 break;
5361 scrollbar_value = val;
5362 j += num_bytes;
5363 slen = j;
5364 }
5365 if (i == 0) /* not enough characters to make one */
5366 return -1;
5367 }
5368# endif /* !USE_ON_FLY_SCROLL */
5369#endif /* FEAT_GUI */
5370
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005371 /*
5372 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5373 */
5374 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5375
5376 /*
5377 * Add any modifier codes to our string.
5378 */
5379 new_slen = 0; /* Length of what will replace the termcode */
5380 if (modifiers != 0)
5381 {
5382 /* Some keys have the modifier included. Need to handle that here
5383 * to make mappings work. */
5384 key = simplify_key(key, &modifiers);
5385 if (modifiers != 0)
5386 {
5387 string[new_slen++] = K_SPECIAL;
5388 string[new_slen++] = (int)KS_MODIFIER;
5389 string[new_slen++] = modifiers;
5390 }
5391 }
5392
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005394 key_name[0] = KEY2TERMCAP0(key);
5395 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005397 {
5398 /* from ":set <M-b>=xx" */
5399#ifdef FEAT_MBYTE
5400 if (has_mbyte)
5401 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5402 else
5403#endif
5404 string[new_slen++] = key_name[1];
5405 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005406 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5407 && key_name[1] == KE_IGNORE)
5408 {
5409 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5410 * to indicate what happened. */
5411 retval = KEYLEN_REMOVED;
5412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 else
5414 {
5415 string[new_slen++] = K_SPECIAL;
5416 string[new_slen++] = key_name[0];
5417 string[new_slen++] = key_name[1];
5418 }
5419 string[new_slen] = NUL;
5420 extra = new_slen - slen;
5421 if (buf == NULL)
5422 {
5423 if (extra < 0)
5424 /* remove matched chars, taking care of noremap */
5425 del_typebuf(-extra, offset);
5426 else if (extra > 0)
5427 /* insert the extra space we need */
5428 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5429
5430 /*
5431 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5432 * typebuf.tb_buf[]!
5433 */
5434 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5435 (size_t)new_slen);
5436 }
5437 else
5438 {
5439 if (extra < 0)
5440 /* remove matched characters */
5441 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005442 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005444 {
5445 /* Insert the extra space we need. If there is insufficient
5446 * space return -1. */
5447 if (*buflen + extra + new_slen >= bufsize)
5448 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005450 (size_t)(*buflen - offset));
5451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005453 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005455 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 }
5457
Bram Moolenaar2951b772013-07-03 12:45:31 +02005458#ifdef FEAT_TERMRESPONSE
5459 LOG_TR("normal character");
5460#endif
5461
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 return 0; /* no match found */
5463}
5464
5465/*
5466 * Replace any terminal code strings in from[] with the equivalent internal
5467 * vim representation. This is used for the "from" and "to" part of a
5468 * mapping, and the "to" part of a menu command.
5469 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5470 * '<'.
5471 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5472 *
5473 * The replacement is done in result[] and finally copied into allocated
5474 * memory. If this all works well *bufp is set to the allocated memory and a
5475 * pointer to it is returned. If something fails *bufp is set to NULL and from
5476 * is returned.
5477 *
5478 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5479 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5480 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5481 * instead of a CTRL-V.
5482 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005483 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005484replace_termcodes(
5485 char_u *from,
5486 char_u **bufp,
5487 int from_part,
5488 int do_lt, /* also translate <lt> */
5489 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490{
5491 int i;
5492 int slen;
5493 int key;
5494 int dlen = 0;
5495 char_u *src;
5496 int do_backslash; /* backslash is a special character */
5497 int do_special; /* recognize <> key codes */
5498 int do_key_code; /* recognize raw key codes */
5499 char_u *result; /* buffer for resulting string */
5500
5501 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005502 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5504
5505 /*
5506 * Allocate space for the translation. Worst case a single character is
5507 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5508 */
5509 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5510 if (result == NULL) /* out of memory */
5511 {
5512 *bufp = NULL;
5513 return from;
5514 }
5515
5516 src = from;
5517
5518 /*
5519 * Check for #n at start only: function key n
5520 */
5521 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5522 {
5523 result[dlen++] = K_SPECIAL;
5524 result[dlen++] = 'k';
5525 if (src[1] == '0')
5526 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5527 else
5528 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5529 src += 2;
5530 }
5531
5532 /*
5533 * Copy each byte from *from to result[dlen]
5534 */
5535 while (*src != NUL)
5536 {
5537 /*
5538 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005539 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 */
5541 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5542 {
5543#ifdef FEAT_EVAL
5544 /*
5545 * Replace <SID> by K_SNR <script-nr> _.
5546 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5547 */
5548 if (STRNICMP(src, "<SID>", 5) == 0)
5549 {
5550 if (current_SID <= 0)
5551 EMSG(_(e_usingsid));
5552 else
5553 {
5554 src += 5;
5555 result[dlen++] = K_SPECIAL;
5556 result[dlen++] = (int)KS_EXTRA;
5557 result[dlen++] = (int)KE_SNR;
5558 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5559 dlen += (int)STRLEN(result + dlen);
5560 result[dlen++] = '_';
5561 continue;
5562 }
5563 }
5564#endif
5565
5566 slen = trans_special(&src, result + dlen, TRUE);
5567 if (slen)
5568 {
5569 dlen += slen;
5570 continue;
5571 }
5572 }
5573
5574 /*
5575 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5576 * Note that this is also checked after replacing the <> form.
5577 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5578 * it could be a character in the file.
5579 */
5580 if (do_key_code)
5581 {
5582 i = find_term_bykeys(src);
5583 if (i >= 0)
5584 {
5585 result[dlen++] = K_SPECIAL;
5586 result[dlen++] = termcodes[i].name[0];
5587 result[dlen++] = termcodes[i].name[1];
5588 src += termcodes[i].len;
5589 /* If terminal code matched, continue after it. */
5590 continue;
5591 }
5592 }
5593
5594#ifdef FEAT_EVAL
5595 if (do_special)
5596 {
5597 char_u *p, *s, len;
5598
5599 /*
5600 * Replace <Leader> by the value of "mapleader".
5601 * Replace <LocalLeader> by the value of "maplocalleader".
5602 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5603 */
5604 if (STRNICMP(src, "<Leader>", 8) == 0)
5605 {
5606 len = 8;
5607 p = get_var_value((char_u *)"g:mapleader");
5608 }
5609 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5610 {
5611 len = 13;
5612 p = get_var_value((char_u *)"g:maplocalleader");
5613 }
5614 else
5615 {
5616 len = 0;
5617 p = NULL;
5618 }
5619 if (len != 0)
5620 {
5621 /* Allow up to 8 * 6 characters for "mapleader". */
5622 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5623 s = (char_u *)"\\";
5624 else
5625 s = p;
5626 while (*s != NUL)
5627 result[dlen++] = *s++;
5628 src += len;
5629 continue;
5630 }
5631 }
5632#endif
5633
5634 /*
5635 * Remove CTRL-V and ignore the next character.
5636 * For "from" side the CTRL-V at the end is included, for the "to"
5637 * part it is removed.
5638 * If 'cpoptions' does not contain 'B', also accept a backslash.
5639 */
5640 key = *src;
5641 if (key == Ctrl_V || (do_backslash && key == '\\'))
5642 {
5643 ++src; /* skip CTRL-V or backslash */
5644 if (*src == NUL)
5645 {
5646 if (from_part)
5647 result[dlen++] = key;
5648 break;
5649 }
5650 }
5651
5652#ifdef FEAT_MBYTE
5653 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005654 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655#endif
5656 {
5657 /*
5658 * If the character is K_SPECIAL, replace it with K_SPECIAL
5659 * KS_SPECIAL KE_FILLER.
5660 * If compiled with the GUI replace CSI with K_CSI.
5661 */
5662 if (*src == K_SPECIAL)
5663 {
5664 result[dlen++] = K_SPECIAL;
5665 result[dlen++] = KS_SPECIAL;
5666 result[dlen++] = KE_FILLER;
5667 }
5668# ifdef FEAT_GUI
5669 else if (*src == CSI)
5670 {
5671 result[dlen++] = K_SPECIAL;
5672 result[dlen++] = KS_EXTRA;
5673 result[dlen++] = (int)KE_CSI;
5674 }
5675# endif
5676 else
5677 result[dlen++] = *src;
5678 ++src;
5679 }
5680 }
5681 result[dlen] = NUL;
5682
5683 /*
5684 * Copy the new string to allocated memory.
5685 * If this fails, just return from.
5686 */
5687 if ((*bufp = vim_strsave(result)) != NULL)
5688 from = *bufp;
5689 vim_free(result);
5690 return from;
5691}
5692
5693/*
5694 * Find a termcode with keys 'src' (must be NUL terminated).
5695 * Return the index in termcodes[], or -1 if not found.
5696 */
5697 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005698find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005701 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
5703 for (i = 0; i < tc_len; ++i)
5704 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005705 if (slen == termcodes[i].len
5706 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 return i;
5708 }
5709 return -1;
5710}
5711
5712/*
5713 * Gather the first characters in the terminal key codes into a string.
5714 * Used to speed up check_termcode().
5715 */
5716 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005717gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
5719 int i;
5720 int len = 0;
5721
5722#ifdef FEAT_GUI
5723 if (gui.in_use)
5724 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5725#endif
5726#ifdef FEAT_TERMRESPONSE
5727 if (check_for_codes)
5728 termleader[len++] = DCS; /* the termcode response starts with DCS
5729 in 8-bit mode */
5730#endif
5731 termleader[len] = NUL;
5732
5733 for (i = 0; i < tc_len; ++i)
5734 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5735 {
5736 termleader[len++] = termcodes[i].code[0];
5737 termleader[len] = NUL;
5738 }
5739
5740 need_gather = FALSE;
5741}
5742
5743/*
5744 * Show all termcodes (for ":set termcap")
5745 * This code looks a lot like showoptions(), but is different.
5746 */
5747 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005748show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750 int col;
5751 int *items;
5752 int item_count;
5753 int run;
5754 int row, rows;
5755 int cols;
5756 int i;
5757 int len;
5758
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005759#define INC3 27 /* try to make three columns */
5760#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761#define GAP 2 /* spaces between columns */
5762
5763 if (tc_len == 0) /* no terminal codes (must be GUI) */
5764 return;
5765 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5766 if (items == NULL)
5767 return;
5768
5769 /* Highlight title */
5770 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5771
5772 /*
5773 * do the loop two times:
5774 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005775 * 2. display the medium items (medium length strings)
5776 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005778 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 {
5780 /*
5781 * collect the items in items[]
5782 */
5783 item_count = 0;
5784 for (i = 0; i < tc_len; i++)
5785 {
5786 len = show_one_termcode(termcodes[i].name,
5787 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005788 if (len <= INC3 - GAP ? run == 1
5789 : len <= INC2 - GAP ? run == 2
5790 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 items[item_count++] = i;
5792 }
5793
5794 /*
5795 * display the items
5796 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005797 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005799 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 if (cols == 0)
5801 cols = 1;
5802 rows = (item_count + cols - 1) / cols;
5803 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005804 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 rows = item_count;
5806 for (row = 0; row < rows && !got_int; ++row)
5807 {
5808 msg_putchar('\n'); /* go to next line */
5809 if (got_int) /* 'q' typed in more */
5810 break;
5811 col = 0;
5812 for (i = row; i < item_count; i += rows)
5813 {
5814 msg_col = col; /* make columns */
5815 show_one_termcode(termcodes[items[i]].name,
5816 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005817 if (run == 2)
5818 col += INC2;
5819 else
5820 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 }
5822 out_flush();
5823 ui_breakcheck();
5824 }
5825 }
5826 vim_free(items);
5827}
5828
5829/*
5830 * Show one termcode entry.
5831 * Output goes into IObuff[]
5832 */
5833 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005834show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836 char_u *p;
5837 int len;
5838
5839 if (name[0] > '~')
5840 {
5841 IObuff[0] = ' ';
5842 IObuff[1] = ' ';
5843 IObuff[2] = ' ';
5844 IObuff[3] = ' ';
5845 }
5846 else
5847 {
5848 IObuff[0] = 't';
5849 IObuff[1] = '_';
5850 IObuff[2] = name[0];
5851 IObuff[3] = name[1];
5852 }
5853 IObuff[4] = ' ';
5854
5855 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5856 if (p[1] != 't')
5857 STRCPY(IObuff + 5, p);
5858 else
5859 IObuff[5] = NUL;
5860 len = (int)STRLEN(IObuff);
5861 do
5862 IObuff[len++] = ' ';
5863 while (len < 17);
5864 IObuff[len] = NUL;
5865 if (code == NULL)
5866 len += 4;
5867 else
5868 len += vim_strsize(code);
5869
5870 if (printit)
5871 {
5872 msg_puts(IObuff);
5873 if (code == NULL)
5874 msg_puts((char_u *)"NULL");
5875 else
5876 msg_outtrans(code);
5877 }
5878 return len;
5879}
5880
5881#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5882/*
5883 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5884 * termcap codes from the terminal itself.
5885 * We get them one by one to avoid a very long response string.
5886 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005887static int xt_index_in = 0;
5888static int xt_index_out = 0;
5889
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005891req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892{
5893 xt_index_out = 0;
5894 xt_index_in = 0;
5895 req_more_codes_from_term();
5896}
5897
5898 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005899req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 char buf[11];
5902 int old_idx = xt_index_out;
5903
5904 /* Don't do anything when going to exit. */
5905 if (exiting)
5906 return;
5907
5908 /* Send up to 10 more requests out than we received. Avoid sending too
5909 * many, there can be a buffer overflow somewhere. */
5910 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5911 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02005912# ifdef DEBUG_TERMRESPONSE
5913 char dbuf[100];
5914
5915 sprintf(dbuf, "Requesting XT %d: %s",
5916 xt_index_out, key_names[xt_index_out]);
5917 log_tr(dbuf);
5918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 sprintf(buf, "\033P+q%02x%02x\033\\",
5920 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5921 out_str_nf((char_u *)buf);
5922 ++xt_index_out;
5923 }
5924
5925 /* Send the codes out right away. */
5926 if (xt_index_out != old_idx)
5927 out_flush();
5928}
5929
5930/*
5931 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5932 * A "0" instead of the "1" indicates a code that isn't supported.
5933 * Both <name> and <string> are encoded in hex.
5934 * "code" points to the "0" or "1".
5935 */
5936 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005937got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938{
5939#define XT_LEN 100
5940 char_u name[3];
5941 char_u str[XT_LEN];
5942 int i;
5943 int j = 0;
5944 int c;
5945
5946 /* A '1' means the code is supported, a '0' means it isn't.
5947 * When half the length is > XT_LEN we can't use it.
5948 * Our names are currently all 2 characters. */
5949 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5950 {
5951 /* Get the name from the response and find it in the table. */
5952 name[0] = hexhex2nr(code + 3);
5953 name[1] = hexhex2nr(code + 5);
5954 name[2] = NUL;
5955 for (i = 0; key_names[i] != NULL; ++i)
5956 {
5957 if (STRCMP(key_names[i], name) == 0)
5958 {
5959 xt_index_in = i;
5960 break;
5961 }
5962 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005963# ifdef DEBUG_TERMRESPONSE
5964 {
5965 char buf[100];
5966
5967 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
5968 log_tr(buf);
5969 }
5970# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 if (key_names[i] != NULL)
5972 {
5973 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5974 str[j++] = c;
5975 str[j] = NUL;
5976 if (name[0] == 'C' && name[1] == 'o')
5977 {
5978 /* Color count is not a key code. */
5979 i = atoi((char *)str);
5980 if (i != t_colors)
5981 {
5982 /* Nr of colors changed, initialize highlighting and
Bram Moolenaar238a5642006-02-21 22:12:05 +00005983 * redraw everything. This causes a redraw, which usually
5984 * clears the message. Try keeping the message if it
5985 * might work. */
5986 set_keep_msg_from_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 set_color_count(i);
5988 init_highlight(TRUE, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +02005989#ifdef DEBUG_TERMRESPONSE
5990 {
5991 char buf[100];
5992 int r = redraw_asap(CLEAR);
5993
5994 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
5995 log_tr(buf);
5996 }
5997#else
5998 redraw_asap(CLEAR);
5999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
6001 }
6002 else
6003 {
6004 /* First delete any existing entry with the same code. */
6005 i = find_term_bykeys(str);
6006 if (i >= 0)
6007 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006008 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 }
6010 }
6011 }
6012
6013 /* May request more codes now that we received one. */
6014 ++xt_index_in;
6015 req_more_codes_from_term();
6016}
6017
6018/*
6019 * Check if there are any unanswered requests and deal with them.
6020 * This is called before starting an external program or getting direct
6021 * keyboard input. We don't want responses to be send to that program or
6022 * handled as typed text.
6023 */
6024 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006025check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
6027 int c;
6028
6029 /* If no codes requested or all are answered, no need to wait. */
6030 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6031 return;
6032
6033 /* Vgetc() will check for and handle any response.
6034 * Keep calling vpeekc() until we don't get any responses. */
6035 ++no_mapping;
6036 ++allow_keys;
6037 for (;;)
6038 {
6039 c = vpeekc();
6040 if (c == NUL) /* nothing available */
6041 break;
6042
6043 /* If a response is recognized it's replaced with K_IGNORE, must read
6044 * it from the input stream. If there is no K_IGNORE we can't do
6045 * anything, break here (there might be some responses further on, but
6046 * we don't want to throw away any typed chars). */
6047 if (c != K_SPECIAL && c != K_IGNORE)
6048 break;
6049 c = vgetc();
6050 if (c != K_IGNORE)
6051 {
6052 vungetc(c);
6053 break;
6054 }
6055 }
6056 --no_mapping;
6057 --allow_keys;
6058}
6059#endif
6060
6061#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6062/*
6063 * Translate an internal mapping/abbreviation representation into the
6064 * corresponding external one recognized by :map/:abbrev commands;
6065 * respects the current B/k/< settings of 'cpoption'.
6066 *
6067 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006068 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 *
6070 * It uses a growarray to build the translation string since the
6071 * latter can be wider than the original description. The caller has to
6072 * free the string afterwards.
6073 *
6074 * Returns NULL when there is a problem.
6075 */
6076 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006077translate_mapping(
6078 char_u *str,
6079 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080{
6081 garray_T ga;
6082 int c;
6083 int modifiers;
6084 int cpo_bslash;
6085 int cpo_special;
6086 int cpo_keycode;
6087
6088 ga_init(&ga);
6089 ga.ga_itemsize = 1;
6090 ga.ga_growsize = 40;
6091
6092 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6093 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6094 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6095
6096 for (; *str; ++str)
6097 {
6098 c = *str;
6099 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6100 {
6101 modifiers = 0;
6102 if (str[1] == KS_MODIFIER)
6103 {
6104 str++;
6105 modifiers = *++str;
6106 c = *++str;
6107 }
6108 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6109 {
6110 int i;
6111
6112 /* try to find special key in termcodes */
6113 for (i = 0; i < tc_len; ++i)
6114 if (termcodes[i].name[0] == str[1]
6115 && termcodes[i].name[1] == str[2])
6116 break;
6117 if (i < tc_len)
6118 {
6119 ga_concat(&ga, termcodes[i].code);
6120 str += 2;
6121 continue; /* for (str) */
6122 }
6123 }
6124 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6125 {
6126 if (expmap && cpo_special)
6127 {
6128 ga_clear(&ga);
6129 return NULL;
6130 }
6131 c = TO_SPECIAL(str[1], str[2]);
6132 if (c == K_ZERO) /* display <Nul> as ^@ */
6133 c = NUL;
6134 str += 2;
6135 }
6136 if (IS_SPECIAL(c) || modifiers) /* special key */
6137 {
6138 if (expmap && cpo_special)
6139 {
6140 ga_clear(&ga);
6141 return NULL;
6142 }
6143 ga_concat(&ga, get_special_key_name(c, modifiers));
6144 continue; /* for (str) */
6145 }
6146 }
6147 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6148 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6149 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6150 if (c)
6151 ga_append(&ga, c);
6152 }
6153 ga_append(&ga, NUL);
6154 return (char_u *)(ga.ga_data);
6155}
6156#endif
6157
6158#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6159static char ksme_str[20];
6160static char ksmr_str[20];
6161static char ksmd_str[20];
6162
6163/*
6164 * For Win32 console: update termcap codes for existing console attributes.
6165 */
6166 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006167update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168{
6169 struct builtin_term *p;
6170
6171 p = find_builtin_term(DEFAULT_TERM);
6172 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6173 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6174 attr | 0x08); /* FOREGROUND_INTENSITY */
6175 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6176 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6177
6178 while (p->bt_string != NULL)
6179 {
6180 if (p->bt_entry == (int)KS_ME)
6181 p->bt_string = &ksme_str[0];
6182 else if (p->bt_entry == (int)KS_MR)
6183 p->bt_string = &ksmr_str[0];
6184 else if (p->bt_entry == (int)KS_MD)
6185 p->bt_string = &ksmd_str[0];
6186 ++p;
6187 }
6188}
6189#endif