blob: 6a6ab78ed0d2e602f0e9b67d0851ea65d1da6bb0 [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 *
13 * primitive termcap support for Amiga, MSDOS, and Win32 included
14 *
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);
80static void gather_termleader(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010082static void req_codes_from_term(void);
83static void req_more_codes_from_term(void);
84static void got_code_from_term(char_u *code, int len);
85static void check_for_codes_from_term(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#endif
87#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +000088 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
89 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010090static int get_bytes_from_buf(char_u *, char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010092static void del_termcode_idx(int idx);
93static int term_is_builtin(char_u *name);
94static int term_7to8bit(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#ifdef FEAT_TERMRESPONSE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010096static void switch_to_8bit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#endif
98
99#ifdef HAVE_TGETENT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100100static char_u *tgetent_error(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
102/*
103 * Here is our own prototype for tgetstr(), any prototypes from the include
104 * files have been disabled by the define at the start of this file.
105 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100106char *tgetstr(char *, char **);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
108# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200109 /* Change this to "if 1" to debug what happens with termresponse. */
110# if 0
111# define DEBUG_TERMRESPONSE
112 static void log_tr(char *msg);
113# define LOG_TR(msg) log_tr(msg)
114# else
115# define LOG_TR(msg)
116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117/* Request Terminal Version status: */
118# define CRV_GET 1 /* send T_CRV when switched to RAW mode */
119# define CRV_SENT 2 /* did send T_CRV, waiting for answer */
120# define CRV_GOT 3 /* received T_CRV response */
121static int crv_status = CRV_GET;
Bram Moolenaar9584b312013-03-13 19:29:28 +0100122/* Request Cursor position report: */
123# define U7_GET 1 /* send T_U7 when switched to RAW mode */
124# define U7_SENT 2 /* did send T_U7, waiting for answer */
125# define U7_GOT 3 /* received T_U7 response */
126static int u7_status = U7_GET;
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200127/* Request background color report: */
128# define RBG_GET 1 /* send T_RBG when switched to RAW mode */
129# define RBG_SENT 2 /* did send T_RBG, waiting for answer */
130# define RBG_GOT 3 /* received T_RBG response */
131static int rbg_status = RBG_GET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132# endif
133
134/*
135 * Don't declare these variables if termcap.h contains them.
136 * Autoconf checks if these variables should be declared extern (not all
137 * systems have them).
138 * Some versions define ospeed to be speed_t, but that is incompatible with
139 * BSD, where ospeed is short and speed_t is long.
140 */
141# ifndef HAVE_OSPEED
142# ifdef OSPEED_EXTERN
143extern short ospeed;
144# else
145short ospeed;
146# endif
147# endif
148# ifndef HAVE_UP_BC_PC
149# ifdef UP_BC_PC_EXTERN
150extern char *UP, *BC, PC;
151# else
152char *UP, *BC, PC;
153# endif
154# endif
155
156# define TGETSTR(s, p) vim_tgetstr((s), (p))
157# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100158static char_u *vim_tgetstr(char *s, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif /* HAVE_TGETENT */
160
161static int detected_8bit = FALSE; /* detected 8-bit terminal */
162
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000163static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164{
165
166#if defined(FEAT_GUI)
167/*
168 * GUI pseudo term-cap.
169 */
170 {(int)KS_NAME, "gui"},
171 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
172 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
173# ifdef TERMINFO
174 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
175# else
176 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
177# endif
178 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
179# ifdef TERMINFO
180 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
181 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
182# ifdef FEAT_VERTSPLIT
183 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
184# endif
185# else
186 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
187 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
188# ifdef FEAT_VERTSPLIT
189 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
190# endif
191# endif
192 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
193 /* attributes switched on with 'h', off with * 'H' */
194 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
195 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
196 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
197 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
198 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
199 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
200 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000201 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
202 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
204 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
205 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
206 {(int)KS_MS, "y"},
207 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100208 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {(int)KS_LE, "\b"}, /* cursor-left = BS */
210 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
211# ifdef TERMINFO
212 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
213# else
214 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
215# endif
216 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000217 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#endif
219
220#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221
222# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
223/*
224 * Amiga console window, default for Amiga
225 */
226 {(int)KS_NAME, "amiga"},
227 {(int)KS_CE, "\033[K"},
228 {(int)KS_CD, "\033[J"},
229 {(int)KS_AL, "\033[L"},
230# ifdef TERMINFO
231 {(int)KS_CAL, "\033[%p1%dL"},
232# else
233 {(int)KS_CAL, "\033[%dL"},
234# endif
235 {(int)KS_DL, "\033[M"},
236# ifdef TERMINFO
237 {(int)KS_CDL, "\033[%p1%dM"},
238# else
239 {(int)KS_CDL, "\033[%dM"},
240# endif
241 {(int)KS_CL, "\014"},
242 {(int)KS_VI, "\033[0 p"},
243 {(int)KS_VE, "\033[1 p"},
244 {(int)KS_ME, "\033[0m"},
245 {(int)KS_MR, "\033[7m"},
246 {(int)KS_MD, "\033[1m"},
247 {(int)KS_SE, "\033[0m"},
248 {(int)KS_SO, "\033[33m"},
249 {(int)KS_US, "\033[4m"},
250 {(int)KS_UE, "\033[0m"},
251 {(int)KS_CZH, "\033[3m"},
252 {(int)KS_CZR, "\033[0m"},
253#if defined(__MORPHOS__) || defined(__AROS__)
254 {(int)KS_CCO, "8"}, /* allow 8 colors */
255# ifdef TERMINFO
256 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
257 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
258# else
259 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
260 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
261# endif
262 {(int)KS_OP, "\033[m"}, /* reset colors */
263#endif
264 {(int)KS_MS, "y"},
265 {(int)KS_UT, "y"}, /* guessed */
266 {(int)KS_LE, "\b"},
267# ifdef TERMINFO
268 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
269# else
270 {(int)KS_CM, "\033[%i%d;%dH"},
271# endif
272#if defined(__MORPHOS__)
273 {(int)KS_SR, "\033M"},
274#endif
275# ifdef TERMINFO
276 {(int)KS_CRI, "\033[%p1%dC"},
277# else
278 {(int)KS_CRI, "\033[%dC"},
279# endif
280 {K_UP, "\233A"},
281 {K_DOWN, "\233B"},
282 {K_LEFT, "\233D"},
283 {K_RIGHT, "\233C"},
284 {K_S_UP, "\233T"},
285 {K_S_DOWN, "\233S"},
286 {K_S_LEFT, "\233 A"},
287 {K_S_RIGHT, "\233 @"},
288 {K_S_TAB, "\233Z"},
289 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
290 {K_F2, "\233\061~"},
291 {K_F3, "\233\062~"},
292 {K_F4, "\233\063~"},
293 {K_F5, "\233\064~"},
294 {K_F6, "\233\065~"},
295 {K_F7, "\233\066~"},
296 {K_F8, "\233\067~"},
297 {K_F9, "\233\070~"},
298 {K_F10, "\233\071~"},
299 {K_S_F1, "\233\061\060~"},
300 {K_S_F2, "\233\061\061~"},
301 {K_S_F3, "\233\061\062~"},
302 {K_S_F4, "\233\061\063~"},
303 {K_S_F5, "\233\061\064~"},
304 {K_S_F6, "\233\061\065~"},
305 {K_S_F7, "\233\061\066~"},
306 {K_S_F8, "\233\061\067~"},
307 {K_S_F9, "\233\061\070~"},
308 {K_S_F10, "\233\061\071~"},
309 {K_HELP, "\233?~"},
310 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
311 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
312 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
313 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
314 {K_END, "\233\064\065~"}, /* 101 key keyboard */
315
316 {BT_EXTRA_KEYS, ""},
317 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
318 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
319 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
320# endif
321
322# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
323/*
324 * almost standard ANSI terminal, default for bebox
325 */
326 {(int)KS_NAME, "beos-ansi"},
327 {(int)KS_CE, "\033[K"},
328 {(int)KS_CD, "\033[J"},
329 {(int)KS_AL, "\033[L"},
330# ifdef TERMINFO
331 {(int)KS_CAL, "\033[%p1%dL"},
332# else
333 {(int)KS_CAL, "\033[%dL"},
334# endif
335 {(int)KS_DL, "\033[M"},
336# ifdef TERMINFO
337 {(int)KS_CDL, "\033[%p1%dM"},
338# else
339 {(int)KS_CDL, "\033[%dM"},
340# endif
341#ifdef BEOS_PR_OR_BETTER
342# ifdef TERMINFO
343 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
344# else
345 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
346# endif
347#endif
348 {(int)KS_CL, "\033[H\033[2J"},
349#ifdef notyet
350 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
351 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
352#endif
353 {(int)KS_ME, "\033[m"}, /* normal mode */
354 {(int)KS_MR, "\033[7m"}, /* reverse */
355 {(int)KS_MD, "\033[1m"}, /* bold */
356 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
357 {(int)KS_SE, "\033[m"}, /* standout end */
358 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
359 {(int)KS_CZR, "\033[m"}, /* italic end */
360 {(int)KS_US, "\033[4m"}, /* underscore mode */
361 {(int)KS_UE, "\033[m"}, /* underscore end */
362 {(int)KS_CCO, "8"}, /* allow 8 colors */
363# ifdef TERMINFO
364 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
365 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
366# else
367 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
368 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
369# endif
370 {(int)KS_OP, "\033[m"}, /* reset colors */
371 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
372 {(int)KS_UT, "y"}, /* guessed */
373 {(int)KS_LE, "\b"},
374# ifdef TERMINFO
375 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
376# else
377 {(int)KS_CM, "\033[%i%d;%dH"},
378# endif
379 {(int)KS_SR, "\033M"},
380# ifdef TERMINFO
381 {(int)KS_CRI, "\033[%p1%dC"},
382# else
383 {(int)KS_CRI, "\033[%dC"},
384# endif
385#if defined(BEOS_DR8)
386 {(int)KS_DB, ""}, /* hack! see screen.c */
387#endif
388
389 {K_UP, "\033[A"},
390 {K_DOWN, "\033[B"},
391 {K_LEFT, "\033[D"},
392 {K_RIGHT, "\033[C"},
393# endif
394
395# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) || defined(__EMX__)
396/*
397 * standard ANSI terminal, default for unix
398 */
399 {(int)KS_NAME, "ansi"},
400 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
401 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
402# ifdef TERMINFO
403 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
404# else
405 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
406# endif
407 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
408# ifdef TERMINFO
409 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
410# else
411 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
412# endif
413 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
414 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
415 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
416 {(int)KS_MS, "y"},
417 {(int)KS_UT, "y"}, /* guessed */
418 {(int)KS_LE, "\b"},
419# ifdef TERMINFO
420 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
421# else
422 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
423# endif
424# ifdef TERMINFO
425 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
426# else
427 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
428# endif
429# endif
430
431# if defined(MSDOS) || defined(ALL_BUILTIN_TCAPS) || defined(__EMX__)
432/*
433 * These codes are valid when nansi.sys or equivalent has been installed.
434 * Function keys on a PC are preceded with a NUL. These are converted into
435 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
436 * CTRL-arrow is used instead of SHIFT-arrow.
437 */
438#ifdef __EMX__
439 {(int)KS_NAME, "os2ansi"},
440#else
441 {(int)KS_NAME, "pcansi"},
442 {(int)KS_DL, "\033[M"},
443 {(int)KS_AL, "\033[L"},
444#endif
445 {(int)KS_CE, "\033[K"},
446 {(int)KS_CL, "\033[2J"},
447 {(int)KS_ME, "\033[0m"},
448 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
449 {(int)KS_MD, "\033[1m"}, /* bold: white text */
450 {(int)KS_SE, "\033[0m"}, /* standout end */
451 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
452 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
453 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
454 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
455 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
456 {(int)KS_CCO, "8"}, /* allow 8 colors */
457# ifdef TERMINFO
458 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
459 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
460# else
461 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
462 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
463# endif
464 {(int)KS_OP, "\033[0m"}, /* reset colors */
465 {(int)KS_MS, "y"},
466 {(int)KS_UT, "y"}, /* guessed */
467 {(int)KS_LE, "\b"},
468# ifdef TERMINFO
469 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
470# else
471 {(int)KS_CM, "\033[%i%d;%dH"},
472# endif
473# ifdef TERMINFO
474 {(int)KS_CRI, "\033[%p1%dC"},
475# else
476 {(int)KS_CRI, "\033[%dC"},
477# endif
478 {K_UP, "\316H"},
479 {K_DOWN, "\316P"},
480 {K_LEFT, "\316K"},
481 {K_RIGHT, "\316M"},
482 {K_S_LEFT, "\316s"},
483 {K_S_RIGHT, "\316t"},
484 {K_F1, "\316;"},
485 {K_F2, "\316<"},
486 {K_F3, "\316="},
487 {K_F4, "\316>"},
488 {K_F5, "\316?"},
489 {K_F6, "\316@"},
490 {K_F7, "\316A"},
491 {K_F8, "\316B"},
492 {K_F9, "\316C"},
493 {K_F10, "\316D"},
494 {K_F11, "\316\205"}, /* guessed */
495 {K_F12, "\316\206"}, /* guessed */
496 {K_S_F1, "\316T"},
497 {K_S_F2, "\316U"},
498 {K_S_F3, "\316V"},
499 {K_S_F4, "\316W"},
500 {K_S_F5, "\316X"},
501 {K_S_F6, "\316Y"},
502 {K_S_F7, "\316Z"},
503 {K_S_F8, "\316["},
504 {K_S_F9, "\316\\"},
505 {K_S_F10, "\316]"},
506 {K_S_F11, "\316\207"}, /* guessed */
507 {K_S_F12, "\316\210"}, /* guessed */
508 {K_INS, "\316R"},
509 {K_DEL, "\316S"},
510 {K_HOME, "\316G"},
511 {K_END, "\316O"},
512 {K_PAGEDOWN, "\316Q"},
513 {K_PAGEUP, "\316I"},
514# endif
515
516# if defined(MSDOS)
517/*
518 * These codes are valid for the pc video. The entries that start with ESC |
519 * are translated into conio calls in os_msdos.c. Default for MSDOS.
520 */
521 {(int)KS_NAME, "pcterm"},
522 {(int)KS_CE, "\033|K"},
523 {(int)KS_AL, "\033|L"},
524 {(int)KS_DL, "\033|M"},
525# ifdef TERMINFO
526 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},
527# ifdef FEAT_VERTSPLIT
528 {(int)KS_CSV, "\033|%i%p1%d;%p2%dV"},
529# endif
530# else
531 {(int)KS_CS, "\033|%i%d;%dr"},
532# ifdef FEAT_VERTSPLIT
533 {(int)KS_CSV, "\033|%i%d;%dV"},
534# endif
535# endif
536 {(int)KS_CL, "\033|J"},
537 {(int)KS_ME, "\033|0m"}, /* normal */
538 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgrey */
539 {(int)KS_MD, "\033|15m"}, /* bold: white text */
540 {(int)KS_SE, "\033|0m"}, /* standout end */
541 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
542 {(int)KS_CZH, "\033|225m"}, /* italic mode: blue text on yellow */
543 {(int)KS_CZR, "\033|0m"}, /* italic mode end */
544 {(int)KS_US, "\033|67m"}, /* underscore mode: cyan text on red */
545 {(int)KS_UE, "\033|0m"}, /* underscore mode end */
546 {(int)KS_CCO, "16"}, /* allow 16 colors */
547# ifdef TERMINFO
548 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
549 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
550# else
551 {(int)KS_CAB, "\033|%db"}, /* set background color */
552 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
553# endif
554 {(int)KS_MS, "y"},
555 {(int)KS_UT, "y"},
556 {(int)KS_LE, "\b"},
557# ifdef TERMINFO
558 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},
559# else
560 {(int)KS_CM, "\033|%i%d;%dH"},
561# endif
562#ifdef DJGPP
563 {(int)KS_VB, "\033|B"}, /* visual bell */
564#endif
565 {K_UP, "\316H"},
566 {K_DOWN, "\316P"},
567 {K_LEFT, "\316K"},
568 {K_RIGHT, "\316M"},
569 {K_S_LEFT, "\316s"},
570 {K_S_RIGHT, "\316t"},
571 {K_S_TAB, "\316\017"},
572 {K_F1, "\316;"},
573 {K_F2, "\316<"},
574 {K_F3, "\316="},
575 {K_F4, "\316>"},
576 {K_F5, "\316?"},
577 {K_F6, "\316@"},
578 {K_F7, "\316A"},
579 {K_F8, "\316B"},
580 {K_F9, "\316C"},
581 {K_F10, "\316D"},
582 {K_F11, "\316\205"},
583 {K_F12, "\316\206"},
584 {K_S_F1, "\316T"},
585 {K_S_F2, "\316U"},
586 {K_S_F3, "\316V"},
587 {K_S_F4, "\316W"},
588 {K_S_F5, "\316X"},
589 {K_S_F6, "\316Y"},
590 {K_S_F7, "\316Z"},
591 {K_S_F8, "\316["},
592 {K_S_F9, "\316\\"},
593 {K_S_F10, "\316]"},
594 {K_S_F11, "\316\207"},
595 {K_S_F12, "\316\210"},
596 {K_INS, "\316R"},
597 {K_DEL, "\316S"},
598 {K_HOME, "\316G"},
599 {K_END, "\316O"},
600 {K_PAGEDOWN, "\316Q"},
601 {K_PAGEUP, "\316I"},
602 {K_KPLUS, "\316N"},
603 {K_KMINUS, "\316J"},
604 {K_KMULTIPLY, "\3167"},
605 {K_K0, "\316\332"},
606 {K_K1, "\316\336"},
607 {K_K2, "\316\342"},
608 {K_K3, "\316\346"},
609 {K_K4, "\316\352"},
610 {K_K5, "\316\356"},
611 {K_K6, "\316\362"},
612 {K_K7, "\316\366"},
613 {K_K8, "\316\372"},
614 {K_K9, "\316\376"},
615# endif
616
617# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS) || defined(__EMX__)
618/*
619 * These codes are valid for the Win32 Console . The entries that start with
620 * ESC | are translated into console calls in os_win32.c. The function keys
621 * are also translated in os_win32.c.
622 */
623 {(int)KS_NAME, "win32"},
624 {(int)KS_CE, "\033|K"}, /* clear to end of line */
625 {(int)KS_AL, "\033|L"}, /* add new blank line */
626# ifdef TERMINFO
627 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
628# else
629 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
630# endif
631 {(int)KS_DL, "\033|M"}, /* delete line */
632# ifdef TERMINFO
633 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
634# else
635 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
636# endif
637 {(int)KS_CL, "\033|J"}, /* clear screen */
638 {(int)KS_CD, "\033|j"}, /* clear to end of display */
639 {(int)KS_VI, "\033|v"}, /* cursor invisible */
640 {(int)KS_VE, "\033|V"}, /* cursor visible */
641
642 {(int)KS_ME, "\033|0m"}, /* normal */
643 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
644 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
645#if 1
646 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
647 {(int)KS_SE, "\033|0m"}, /* standout end */
648#else
649 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
650 {(int)KS_SE, "\033|f"}, /* standout end */
651#endif
652 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
653 {(int)KS_CZR, "\033|0m"}, /* italic end */
654 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
655 {(int)KS_UE, "\033|0m"}, /* underscore end */
656 {(int)KS_CCO, "16"}, /* allow 16 colors */
657# ifdef TERMINFO
658 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
659 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
660# else
661 {(int)KS_CAB, "\033|%db"}, /* set background color */
662 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
663# endif
664
665 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
666 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100667 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 {(int)KS_LE, "\b"},
669# ifdef TERMINFO
670 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
671# else
672 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
673# endif
674 {(int)KS_VB, "\033|B"}, /* visual bell */
675 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
676 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
677# ifdef TERMINFO
678 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
679# else
680 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
681# endif
682
683 {K_UP, "\316H"},
684 {K_DOWN, "\316P"},
685 {K_LEFT, "\316K"},
686 {K_RIGHT, "\316M"},
687 {K_S_UP, "\316\304"},
688 {K_S_DOWN, "\316\317"},
689 {K_S_LEFT, "\316\311"},
690 {K_C_LEFT, "\316s"},
691 {K_S_RIGHT, "\316\313"},
692 {K_C_RIGHT, "\316t"},
693 {K_S_TAB, "\316\017"},
694 {K_F1, "\316;"},
695 {K_F2, "\316<"},
696 {K_F3, "\316="},
697 {K_F4, "\316>"},
698 {K_F5, "\316?"},
699 {K_F6, "\316@"},
700 {K_F7, "\316A"},
701 {K_F8, "\316B"},
702 {K_F9, "\316C"},
703 {K_F10, "\316D"},
704 {K_F11, "\316\205"},
705 {K_F12, "\316\206"},
706 {K_S_F1, "\316T"},
707 {K_S_F2, "\316U"},
708 {K_S_F3, "\316V"},
709 {K_S_F4, "\316W"},
710 {K_S_F5, "\316X"},
711 {K_S_F6, "\316Y"},
712 {K_S_F7, "\316Z"},
713 {K_S_F8, "\316["},
714 {K_S_F9, "\316\\"},
715 {K_S_F10, "\316]"},
716 {K_S_F11, "\316\207"},
717 {K_S_F12, "\316\210"},
718 {K_INS, "\316R"},
719 {K_DEL, "\316S"},
720 {K_HOME, "\316G"},
721 {K_S_HOME, "\316\302"},
722 {K_C_HOME, "\316w"},
723 {K_END, "\316O"},
724 {K_S_END, "\316\315"},
725 {K_C_END, "\316u"},
726 {K_PAGEDOWN, "\316Q"},
727 {K_PAGEUP, "\316I"},
728 {K_KPLUS, "\316N"},
729 {K_KMINUS, "\316J"},
730 {K_KMULTIPLY, "\316\067"},
731 {K_K0, "\316\332"},
732 {K_K1, "\316\336"},
733 {K_K2, "\316\342"},
734 {K_K3, "\316\346"},
735 {K_K4, "\316\352"},
736 {K_K5, "\316\356"},
737 {K_K6, "\316\362"},
738 {K_K7, "\316\366"},
739 {K_K8, "\316\372"},
740 {K_K9, "\316\376"},
741# endif
742
743# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
744/*
745 * VT320 is working as an ANSI terminal compatible DEC terminal.
746 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
747 * Note: K_F1...K_F5 are for internal use, should not be defined.
748 * TODO:- rewrite ESC[ codes to CSI
749 * - keyboard languages (CSI ? 26 n)
750 */
751 {(int)KS_NAME, "vt320"},
752 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
753 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
754# ifdef TERMINFO
755 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
756# else
757 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
758# endif
759 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
760# ifdef TERMINFO
761 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
762# else
763 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
764# endif
765 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000766 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
767 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
769 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000770 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
771 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
772 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
773 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
774 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
775 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
776 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
777 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
778 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
779 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 {(int)KS_MS, "y"},
781 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100782 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {(int)KS_LE, "\b"},
784# ifdef TERMINFO
785 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
786 ESC_STR "[%i%p1%d;%p2%dH")},
787# else
788 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
789# endif
790# ifdef TERMINFO
791 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
792# else
793 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
794# endif
795 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
796 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
797 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
798 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000799 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
800 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
801 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
802 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
803 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
805 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
806 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
807 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
808 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000809 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
811 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
812 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
813 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
814 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
815 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
816 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
817 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
818 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
819 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
820 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
821 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
822 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
823 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
824 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
825 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
826 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
827 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
828 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
829 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
830 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
831# endif
832
833# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
834/*
835 * Ordinary vt52
836 */
837 {(int)KS_NAME, "vt52"},
838 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
839 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100840# ifdef TERMINFO
841 {(int)KS_CM, IF_EB("\033Y%p1%' '%+%c%p2%' '%+%c",
842 ESC_STR "Y%p1%' '%+%c%p2%' '%+%c")},
843# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100845# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 {(int)KS_LE, "\b"},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100847 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
849 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 {K_UP, IF_EB("\033A", ESC_STR "A")},
851 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
852 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
853 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
Bram Moolenaar2a1b4742015-11-24 18:15:51 +0100854 {K_F1, IF_EB("\033P", ESC_STR "P")},
855 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
856 {K_F3, IF_EB("\033R", ESC_STR "R")},
857# ifdef __MINT__
858 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
859 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
860 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
861 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
862 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
864 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
865 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
866 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 {K_F4, IF_EB("\033S", ESC_STR "S")},
868 {K_F5, IF_EB("\033T", ESC_STR "T")},
869 {K_F6, IF_EB("\033U", ESC_STR "U")},
870 {K_F7, IF_EB("\033V", ESC_STR "V")},
871 {K_F8, IF_EB("\033W", ESC_STR "W")},
872 {K_F9, IF_EB("\033X", ESC_STR "X")},
873 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
874 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
875 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
876 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
877 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
878 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
879 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
880 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
881 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
882 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
883 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
884 {K_INS, IF_EB("\033I", ESC_STR "I")},
885 {K_HOME, IF_EB("\033E", ESC_STR "E")},
886 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
887 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
888# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 {(int)KS_MS, "y"},
891# endif
892# endif
893
894# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) || defined(__EMX__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {(int)KS_NAME, "xterm"},
896 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
897 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
898# ifdef TERMINFO
899 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
900# else
901 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
902# endif
903 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
904# ifdef TERMINFO
905 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
906# else
907 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
908# endif
909# ifdef TERMINFO
910 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
911 ESC_STR "[%i%p1%d;%p2%dr")},
912# else
913 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
914# endif
915 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
916 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
917 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
918 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
919 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
920 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
921 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
922 {(int)KS_MS, "y"},
923 {(int)KS_UT, "y"},
924 {(int)KS_LE, "\b"},
925# ifdef TERMINFO
926 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
927 ESC_STR "[%i%p1%d;%p2%dH")},
928# else
929 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
930# endif
931 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
932# ifdef TERMINFO
933 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
934# else
935 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
936# endif
937 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
938 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
939# ifdef FEAT_XTERM_SAVE
940 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
941 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
942 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
943# endif
944 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
945 {(int)KS_CIE, "\007"},
946 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
947 {(int)KS_FS, "\007"},
948# ifdef TERMINFO
949 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
950 ESC_STR "[8;%p1%d;%p2%dt")},
951 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
952 ESC_STR "[3;%p1%d;%p2%dt")},
953# else
954 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
955 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
956# endif
957 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200958 {(int)KS_RBG, IF_EB("\033]11;?\007", ESC_STR "]11;?\007")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100959 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000960
961 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
962 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
963 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
964 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
965 /* An extra set of cursor keys for vt100 mode */
966 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
967 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
968 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
969 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000971 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
972 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
973 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
974 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000975 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
976 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
977 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
978 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
979 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
980 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
981 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
982 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
983 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
984 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
985 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
986 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000988 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
989 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
990 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
991 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000992 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
993 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000994 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000995 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000996 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000997 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000998 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
999 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001000 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001001 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001002 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001003 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
1004 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001005 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
1006 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
1007 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
1008 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
1009 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
1010 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001011 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
1013 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001014 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
1015 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
1016 /* F14 and F15 are missing, because they send the same codes as the undo
1017 * and help key, although they don't work on all keyboards. */
1018 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
1019 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
1020 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1021 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1022 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1023
1024 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1025 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1026 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1027 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1028 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1029 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1030 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1031 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1032 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1033 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1034
1035 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1036 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1037 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1038 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1039 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1040 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1041 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042# endif
1043
1044# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1045/*
1046 * iris-ansi for Silicon Graphics machines.
1047 */
1048 {(int)KS_NAME, "iris-ansi"},
1049 {(int)KS_CE, "\033[K"},
1050 {(int)KS_CD, "\033[J"},
1051 {(int)KS_AL, "\033[L"},
1052# ifdef TERMINFO
1053 {(int)KS_CAL, "\033[%p1%dL"},
1054# else
1055 {(int)KS_CAL, "\033[%dL"},
1056# endif
1057 {(int)KS_DL, "\033[M"},
1058# ifdef TERMINFO
1059 {(int)KS_CDL, "\033[%p1%dM"},
1060# else
1061 {(int)KS_CDL, "\033[%dM"},
1062# endif
1063#if 0 /* The scroll region is not working as Vim expects. */
1064# ifdef TERMINFO
1065 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1066# else
1067 {(int)KS_CS, "\033[%i%d;%dr"},
1068# endif
1069#endif
1070 {(int)KS_CL, "\033[H\033[2J"},
1071 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1072 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1073 {(int)KS_TI, "\033[=6h"},
1074 {(int)KS_TE, "\033[=6l"},
1075 {(int)KS_SE, "\033[21;27m"},
1076 {(int)KS_SO, "\033[1;7m"},
1077 {(int)KS_ME, "\033[m"},
1078 {(int)KS_MR, "\033[7m"},
1079 {(int)KS_MD, "\033[1m"},
1080 {(int)KS_CCO, "8"}, /* allow 8 colors */
1081 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1082 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1083 {(int)KS_US, "\033[4m"}, /* underline on */
1084 {(int)KS_UE, "\033[24m"}, /* underline off */
1085# ifdef TERMINFO
1086 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1087 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1088 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1089 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1090# else
1091 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1092 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1093 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1094 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1095# endif
1096 {(int)KS_MS, "y"}, /* guessed */
1097 {(int)KS_UT, "y"}, /* guessed */
1098 {(int)KS_LE, "\b"},
1099# ifdef TERMINFO
1100 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1101# else
1102 {(int)KS_CM, "\033[%i%d;%dH"},
1103# endif
1104 {(int)KS_SR, "\033M"},
1105# ifdef TERMINFO
1106 {(int)KS_CRI, "\033[%p1%dC"},
1107# else
1108 {(int)KS_CRI, "\033[%dC"},
1109# endif
1110 {(int)KS_CIS, "\033P3.y"},
1111 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1112 {(int)KS_TS, "\033P1.y"},
1113 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1114# ifdef TERMINFO
1115 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1116 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1117# else
1118 {(int)KS_CWS, "\033[203;%d;%d/y"},
1119 {(int)KS_CWP, "\033[205;%d;%d/y"},
1120# endif
1121 {K_UP, "\033[A"},
1122 {K_DOWN, "\033[B"},
1123 {K_LEFT, "\033[D"},
1124 {K_RIGHT, "\033[C"},
1125 {K_S_UP, "\033[161q"},
1126 {K_S_DOWN, "\033[164q"},
1127 {K_S_LEFT, "\033[158q"},
1128 {K_S_RIGHT, "\033[167q"},
1129 {K_F1, "\033[001q"},
1130 {K_F2, "\033[002q"},
1131 {K_F3, "\033[003q"},
1132 {K_F4, "\033[004q"},
1133 {K_F5, "\033[005q"},
1134 {K_F6, "\033[006q"},
1135 {K_F7, "\033[007q"},
1136 {K_F8, "\033[008q"},
1137 {K_F9, "\033[009q"},
1138 {K_F10, "\033[010q"},
1139 {K_F11, "\033[011q"},
1140 {K_F12, "\033[012q"},
1141 {K_S_F1, "\033[013q"},
1142 {K_S_F2, "\033[014q"},
1143 {K_S_F3, "\033[015q"},
1144 {K_S_F4, "\033[016q"},
1145 {K_S_F5, "\033[017q"},
1146 {K_S_F6, "\033[018q"},
1147 {K_S_F7, "\033[019q"},
1148 {K_S_F8, "\033[020q"},
1149 {K_S_F9, "\033[021q"},
1150 {K_S_F10, "\033[022q"},
1151 {K_S_F11, "\033[023q"},
1152 {K_S_F12, "\033[024q"},
1153 {K_INS, "\033[139q"},
1154 {K_HOME, "\033[H"},
1155 {K_END, "\033[146q"},
1156 {K_PAGEUP, "\033[150q"},
1157 {K_PAGEDOWN, "\033[154q"},
1158# endif
1159
1160# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1161/*
1162 * for debugging
1163 */
1164 {(int)KS_NAME, "debug"},
1165 {(int)KS_CE, "[CE]"},
1166 {(int)KS_CD, "[CD]"},
1167 {(int)KS_AL, "[AL]"},
1168# ifdef TERMINFO
1169 {(int)KS_CAL, "[CAL%p1%d]"},
1170# else
1171 {(int)KS_CAL, "[CAL%d]"},
1172# endif
1173 {(int)KS_DL, "[DL]"},
1174# ifdef TERMINFO
1175 {(int)KS_CDL, "[CDL%p1%d]"},
1176# else
1177 {(int)KS_CDL, "[CDL%d]"},
1178# endif
1179# ifdef TERMINFO
1180 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1181# else
1182 {(int)KS_CS, "[%dCS%d]"},
1183# endif
1184# ifdef FEAT_VERTSPLIT
1185# ifdef TERMINFO
1186 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1187# else
1188 {(int)KS_CSV, "[%dCSV%d]"},
1189# endif
1190# endif
1191# ifdef TERMINFO
1192 {(int)KS_CAB, "[CAB%p1%d]"},
1193 {(int)KS_CAF, "[CAF%p1%d]"},
1194 {(int)KS_CSB, "[CSB%p1%d]"},
1195 {(int)KS_CSF, "[CSF%p1%d]"},
1196# else
1197 {(int)KS_CAB, "[CAB%d]"},
1198 {(int)KS_CAF, "[CAF%d]"},
1199 {(int)KS_CSB, "[CSB%d]"},
1200 {(int)KS_CSF, "[CSF%d]"},
1201# endif
1202 {(int)KS_OP, "[OP]"},
1203 {(int)KS_LE, "[LE]"},
1204 {(int)KS_CL, "[CL]"},
1205 {(int)KS_VI, "[VI]"},
1206 {(int)KS_VE, "[VE]"},
1207 {(int)KS_VS, "[VS]"},
1208 {(int)KS_ME, "[ME]"},
1209 {(int)KS_MR, "[MR]"},
1210 {(int)KS_MB, "[MB]"},
1211 {(int)KS_MD, "[MD]"},
1212 {(int)KS_SE, "[SE]"},
1213 {(int)KS_SO, "[SO]"},
1214 {(int)KS_UE, "[UE]"},
1215 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001216 {(int)KS_UCE, "[UCE]"},
1217 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {(int)KS_MS, "[MS]"},
1219 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001220 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221# ifdef TERMINFO
1222 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1223# else
1224 {(int)KS_CM, "[%dCM%d]"},
1225# endif
1226 {(int)KS_SR, "[SR]"},
1227# ifdef TERMINFO
1228 {(int)KS_CRI, "[CRI%p1%d]"},
1229# else
1230 {(int)KS_CRI, "[CRI%d]"},
1231# endif
1232 {(int)KS_VB, "[VB]"},
1233 {(int)KS_KS, "[KS]"},
1234 {(int)KS_KE, "[KE]"},
1235 {(int)KS_TI, "[TI]"},
1236 {(int)KS_TE, "[TE]"},
1237 {(int)KS_CIS, "[CIS]"},
1238 {(int)KS_CIE, "[CIE]"},
1239 {(int)KS_TS, "[TS]"},
1240 {(int)KS_FS, "[FS]"},
1241# ifdef TERMINFO
1242 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1243 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1244# else
1245 {(int)KS_CWS, "[%dCWS%d]"},
1246 {(int)KS_CWP, "[%dCWP%d]"},
1247# endif
1248 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001249 {(int)KS_U7, "[U7]"},
Bram Moolenaarb5c32652015-06-25 17:03:36 +02001250 {(int)KS_RBG, "[RBG]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {K_UP, "[KU]"},
1252 {K_DOWN, "[KD]"},
1253 {K_LEFT, "[KL]"},
1254 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001255 {K_XUP, "[xKU]"},
1256 {K_XDOWN, "[xKD]"},
1257 {K_XLEFT, "[xKL]"},
1258 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {K_S_UP, "[S-KU]"},
1260 {K_S_DOWN, "[S-KD]"},
1261 {K_S_LEFT, "[S-KL]"},
1262 {K_C_LEFT, "[C-KL]"},
1263 {K_S_RIGHT, "[S-KR]"},
1264 {K_C_RIGHT, "[C-KR]"},
1265 {K_F1, "[F1]"},
1266 {K_XF1, "[xF1]"},
1267 {K_F2, "[F2]"},
1268 {K_XF2, "[xF2]"},
1269 {K_F3, "[F3]"},
1270 {K_XF3, "[xF3]"},
1271 {K_F4, "[F4]"},
1272 {K_XF4, "[xF4]"},
1273 {K_F5, "[F5]"},
1274 {K_F6, "[F6]"},
1275 {K_F7, "[F7]"},
1276 {K_F8, "[F8]"},
1277 {K_F9, "[F9]"},
1278 {K_F10, "[F10]"},
1279 {K_F11, "[F11]"},
1280 {K_F12, "[F12]"},
1281 {K_S_F1, "[S-F1]"},
1282 {K_S_XF1, "[S-xF1]"},
1283 {K_S_F2, "[S-F2]"},
1284 {K_S_XF2, "[S-xF2]"},
1285 {K_S_F3, "[S-F3]"},
1286 {K_S_XF3, "[S-xF3]"},
1287 {K_S_F4, "[S-F4]"},
1288 {K_S_XF4, "[S-xF4]"},
1289 {K_S_F5, "[S-F5]"},
1290 {K_S_F6, "[S-F6]"},
1291 {K_S_F7, "[S-F7]"},
1292 {K_S_F8, "[S-F8]"},
1293 {K_S_F9, "[S-F9]"},
1294 {K_S_F10, "[S-F10]"},
1295 {K_S_F11, "[S-F11]"},
1296 {K_S_F12, "[S-F12]"},
1297 {K_HELP, "[HELP]"},
1298 {K_UNDO, "[UNDO]"},
1299 {K_BS, "[BS]"},
1300 {K_INS, "[INS]"},
1301 {K_KINS, "[KINS]"},
1302 {K_DEL, "[DEL]"},
1303 {K_KDEL, "[KDEL]"},
1304 {K_HOME, "[HOME]"},
1305 {K_S_HOME, "[C-HOME]"},
1306 {K_C_HOME, "[C-HOME]"},
1307 {K_KHOME, "[KHOME]"},
1308 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001309 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 {K_END, "[END]"},
1311 {K_S_END, "[C-END]"},
1312 {K_C_END, "[C-END]"},
1313 {K_KEND, "[KEND]"},
1314 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001315 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {K_PAGEUP, "[PAGEUP]"},
1317 {K_PAGEDOWN, "[PAGEDOWN]"},
1318 {K_KPAGEUP, "[KPAGEUP]"},
1319 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1320 {K_MOUSE, "[MOUSE]"},
1321 {K_KPLUS, "[KPLUS]"},
1322 {K_KMINUS, "[KMINUS]"},
1323 {K_KDIVIDE, "[KDIVIDE]"},
1324 {K_KMULTIPLY, "[KMULTIPLY]"},
1325 {K_KENTER, "[KENTER]"},
1326 {K_KPOINT, "[KPOINT]"},
1327 {K_K0, "[K0]"},
1328 {K_K1, "[K1]"},
1329 {K_K2, "[K2]"},
1330 {K_K3, "[K3]"},
1331 {K_K4, "[K4]"},
1332 {K_K5, "[K5]"},
1333 {K_K6, "[K6]"},
1334 {K_K7, "[K7]"},
1335 {K_K8, "[K8]"},
1336 {K_K9, "[K9]"},
1337# endif
1338
1339#endif /* NO_BUILTIN_TCAPS */
1340
1341/*
1342 * The most minimal terminal: only clear screen and cursor positioning
1343 * Always included.
1344 */
1345 {(int)KS_NAME, "dumb"},
1346 {(int)KS_CL, "\014"},
1347#ifdef TERMINFO
1348 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1349 ESC_STR "[%i%p1%d;%p2%dH")},
1350#else
1351 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1352#endif
1353
1354/*
1355 * end marker
1356 */
1357 {(int)KS_NAME, NULL}
1358
1359}; /* end of builtin_termcaps */
1360
1361/*
1362 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364#ifdef AMIGA
1365# define DEFAULT_TERM (char_u *)"amiga"
1366#endif
1367
1368#ifdef MSWIN
1369# define DEFAULT_TERM (char_u *)"win32"
1370#endif
1371
1372#ifdef MSDOS
1373# define DEFAULT_TERM (char_u *)"pcterm"
1374#endif
1375
1376#if defined(UNIX) && !defined(__MINT__)
1377# define DEFAULT_TERM (char_u *)"ansi"
1378#endif
1379
1380#ifdef __MINT__
1381# define DEFAULT_TERM (char_u *)"vt52"
1382#endif
1383
1384#ifdef __EMX__
1385# define DEFAULT_TERM (char_u *)"os2ansi"
1386#endif
1387
1388#ifdef VMS
1389# define DEFAULT_TERM (char_u *)"vt320"
1390#endif
1391
1392#ifdef __BEOS__
1393# undef DEFAULT_TERM
1394# define DEFAULT_TERM (char_u *)"beos-ansi"
1395#endif
1396
1397#ifndef DEFAULT_TERM
1398# define DEFAULT_TERM (char_u *)"dumb"
1399#endif
1400
1401/*
1402 * Term_strings contains currently used terminal output strings.
1403 * It is initialized with the default values by parse_builtin_tcap().
1404 * The values can be changed by setting the option with the same name.
1405 */
1406char_u *(term_strings[(int)KS_LAST + 1]);
1407
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001408static int need_gather = FALSE; /* need to fill termleader[] */
1409static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001411static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412#endif
1413
1414 static struct builtin_term *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001415find_builtin_term(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
1417 struct builtin_term *p;
1418
1419 p = builtin_termcaps;
1420 while (p->bt_string != NULL)
1421 {
1422 if (p->bt_entry == (int)KS_NAME)
1423 {
1424#ifdef UNIX
1425 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1426 return p;
1427 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1428 return p;
1429 else
1430#endif
1431#ifdef VMS
1432 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1433 return p;
1434 else
1435#endif
1436 if (STRCMP(term, p->bt_string) == 0)
1437 return p;
1438 }
1439 ++p;
1440 }
1441 return p;
1442}
1443
1444/*
1445 * Parsing of the builtin termcap entries.
1446 * Caller should check if 'name' is a valid builtin term.
1447 * The terminal's name is not set, as this is already done in termcapinit().
1448 */
1449 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001450parse_builtin_tcap(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
1452 struct builtin_term *p;
1453 char_u name[2];
1454 int term_8bit;
1455
1456 p = find_builtin_term(term);
1457 term_8bit = term_is_8bit(term);
1458
1459 /* Do not parse if builtin term not found */
1460 if (p->bt_string == NULL)
1461 return;
1462
1463 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1464 {
1465 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1466 {
1467 /* Only set the value if it wasn't set yet. */
1468 if (term_strings[p->bt_entry] == NULL
1469 || term_strings[p->bt_entry] == empty_option)
1470 {
1471 /* 8bit terminal: use CSI instead of <Esc>[ */
1472 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1473 {
1474 char_u *s, *t;
1475
1476 s = vim_strsave((char_u *)p->bt_string);
1477 if (s != NULL)
1478 {
1479 for (t = s; *t; ++t)
1480 if (term_7to8bit(t))
1481 {
1482 *t = term_7to8bit(t);
1483 STRCPY(t + 1, t + 2);
1484 }
1485 term_strings[p->bt_entry] = s;
1486 set_term_option_alloced(&term_strings[p->bt_entry]);
1487 }
1488 }
1489 else
1490 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1491 }
1492 }
1493 else
1494 {
1495 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1496 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1497 if (find_termcode(name) == NULL)
1498 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1499 }
1500 }
1501}
1502#if defined(HAVE_TGETENT) || defined(FEAT_TERMRESPONSE)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001503static void set_color_count(int nr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
1505/*
1506 * Set number of colors.
1507 * Store it as a number in t_colors.
1508 * Store it as a string in T_CCO (using nr_colors[]).
1509 */
1510 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001511set_color_count(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512{
1513 char_u nr_colors[20]; /* string for number of colors */
1514
1515 t_colors = nr;
1516 if (t_colors > 1)
1517 sprintf((char *)nr_colors, "%d", t_colors);
1518 else
1519 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001520 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521}
1522#endif
1523
1524#ifdef HAVE_TGETENT
1525static char *(key_names[]) =
1526{
1527#ifdef FEAT_TERMRESPONSE
1528 /* Do this one first, it may cause a screen redraw. */
1529 "Co",
1530#endif
1531 "ku", "kd", "kr", "kl",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 "#2", "#4", "%i", "*7",
1533 "k1", "k2", "k3", "k4", "k5", "k6",
1534 "k7", "k8", "k9", "k;", "F1", "F2",
1535 "%1", "&8", "kb", "kI", "kD", "kh",
1536 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1537 NULL
1538};
1539#endif
1540
1541/*
1542 * Set terminal options for terminal "term".
1543 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1544 *
1545 * While doing this, until ttest(), some options may be NULL, be careful.
1546 */
1547 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001548set_termname(char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
1550 struct builtin_term *termp;
1551#ifdef HAVE_TGETENT
1552 int builtin_first = p_tbi;
1553 int try;
1554 int termcap_cleared = FALSE;
1555#endif
1556 int width = 0, height = 0;
1557 char_u *error_msg = NULL;
1558 char_u *bs_p, *del_p;
1559
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001560 /* In silect mode (ex -s) we don't use the 'term' option. */
1561 if (silent_mode)
1562 return OK;
1563
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 detected_8bit = FALSE; /* reset 8-bit detection */
1565
1566 if (term_is_builtin(term))
1567 {
1568 term += 8;
1569#ifdef HAVE_TGETENT
1570 builtin_first = 1;
1571#endif
1572 }
1573
1574/*
1575 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1576 * If builtin_first is TRUE:
1577 * 0. try builtin termcap
1578 * 1. try external termcap
1579 * 2. if both fail default to a builtin terminal
1580 * If builtin_first is FALSE:
1581 * 1. try external termcap
1582 * 2. try builtin termcap, if both fail default to a builtin terminal
1583 */
1584#ifdef HAVE_TGETENT
1585 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1586 {
1587 /*
1588 * Use external termcap
1589 */
1590 if (try == 1)
1591 {
1592 char_u *p;
1593 static char_u tstrbuf[TBUFSZ];
1594 int i;
1595 char_u tbuf[TBUFSZ];
1596 char_u *tp;
1597 static struct {
1598 enum SpecialKey dest; /* index in term_strings[] */
1599 char *name; /* termcap name for string */
1600 } string_names[] =
1601 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1602 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1603 {KS_CL, "cl"}, {KS_CD, "cd"},
1604 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1605 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1606 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1607 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001608 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1609 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1611 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1612 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1613 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1614 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1615 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1616 {KS_TS, "ts"}, {KS_FS, "fs"},
1617 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001618 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001619 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {(enum SpecialKey)0, NULL}
1621 };
1622
1623 /*
1624 * If the external termcap does not have a matching entry, try the
1625 * builtin ones.
1626 */
1627 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1628 {
1629 tp = tstrbuf;
1630 if (!termcap_cleared)
1631 {
1632 clear_termoptions(); /* clear old options */
1633 termcap_cleared = TRUE;
1634 }
1635
1636 /* get output strings */
1637 for (i = 0; string_names[i].name != NULL; ++i)
1638 {
1639 if (term_str(string_names[i].dest) == NULL
1640 || term_str(string_names[i].dest) == empty_option)
1641 term_str(string_names[i].dest) =
1642 TGETSTR(string_names[i].name, &tp);
1643 }
1644
1645 /* tgetflag() returns 1 if the flag is present, 0 if not and
1646 * possibly -1 if the flag doesn't exist. */
1647 if ((T_MS == NULL || T_MS == empty_option)
1648 && tgetflag("ms") > 0)
1649 T_MS = (char_u *)"y";
1650 if ((T_XS == NULL || T_XS == empty_option)
1651 && tgetflag("xs") > 0)
1652 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001653 if ((T_XN == NULL || T_XN == empty_option)
1654 && tgetflag("xn") > 0)
1655 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if ((T_DB == NULL || T_DB == empty_option)
1657 && tgetflag("db") > 0)
1658 T_DB = (char_u *)"y";
1659 if ((T_DA == NULL || T_DA == empty_option)
1660 && tgetflag("da") > 0)
1661 T_DA = (char_u *)"y";
1662 if ((T_UT == NULL || T_UT == empty_option)
1663 && tgetflag("ut") > 0)
1664 T_UT = (char_u *)"y";
1665
1666
1667 /*
1668 * get key codes
1669 */
1670 for (i = 0; key_names[i] != NULL; ++i)
1671 {
1672 if (find_termcode((char_u *)key_names[i]) == NULL)
1673 {
1674 p = TGETSTR(key_names[i], &tp);
1675 /* if cursor-left == backspace, ignore it (televideo
1676 * 925) */
1677 if (p != NULL
1678 && (*p != Ctrl_H
1679 || key_names[i][0] != 'k'
1680 || key_names[i][1] != 'l'))
1681 add_termcode((char_u *)key_names[i], p, FALSE);
1682 }
1683 }
1684
1685 if (height == 0)
1686 height = tgetnum("li");
1687 if (width == 0)
1688 width = tgetnum("co");
1689
1690 /*
1691 * Get number of colors (if not done already).
1692 */
1693 if (term_str(KS_CCO) == NULL
1694 || term_str(KS_CCO) == empty_option)
1695 set_color_count(tgetnum("Co"));
1696
1697# ifndef hpux
1698 BC = (char *)TGETSTR("bc", &tp);
1699 UP = (char *)TGETSTR("up", &tp);
1700 p = TGETSTR("pc", &tp);
1701 if (p)
1702 PC = *p;
1703# endif /* hpux */
1704 }
1705 }
1706 else /* try == 0 || try == 2 */
1707#endif /* HAVE_TGETENT */
1708 /*
1709 * Use builtin termcap
1710 */
1711 {
1712#ifdef HAVE_TGETENT
1713 /*
1714 * If builtin termcap was already used, there is no need to search
1715 * for the builtin termcap again, quit now.
1716 */
1717 if (try == 2 && builtin_first && termcap_cleared)
1718 break;
1719#endif
1720 /*
1721 * search for 'term' in builtin_termcaps[]
1722 */
1723 termp = find_builtin_term(term);
1724 if (termp->bt_string == NULL) /* did not find it */
1725 {
1726#ifdef HAVE_TGETENT
1727 /*
1728 * If try == 0, first try the external termcap. If that is not
1729 * found we'll get back here with try == 2.
1730 * If termcap_cleared is set we used the external termcap,
1731 * don't complain about not finding the term in the builtin
1732 * termcap.
1733 */
1734 if (try == 0) /* try external one */
1735 continue;
1736 if (termcap_cleared) /* found in external termcap */
1737 break;
1738#endif
1739
1740 mch_errmsg("\r\n");
1741 if (error_msg != NULL)
1742 {
1743 mch_errmsg((char *)error_msg);
1744 mch_errmsg("\r\n");
1745 }
1746 mch_errmsg("'");
1747 mch_errmsg((char *)term);
1748 mch_errmsg(_("' not known. Available builtin terminals are:"));
1749 mch_errmsg("\r\n");
1750 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1751 ++termp)
1752 {
1753 if (termp->bt_entry == (int)KS_NAME)
1754 {
1755#ifdef HAVE_TGETENT
1756 mch_errmsg(" builtin_");
1757#else
1758 mch_errmsg(" ");
1759#endif
1760 mch_errmsg(termp->bt_string);
1761 mch_errmsg("\r\n");
1762 }
1763 }
1764 /* when user typed :set term=xxx, quit here */
1765 if (starting != NO_SCREEN)
1766 {
1767 screen_start(); /* don't know where cursor is now */
1768 wait_return(TRUE);
1769 return FAIL;
1770 }
1771 term = DEFAULT_TERM;
1772 mch_errmsg(_("defaulting to '"));
1773 mch_errmsg((char *)term);
1774 mch_errmsg("'\r\n");
1775 if (emsg_silent == 0)
1776 {
1777 screen_start(); /* don't know where cursor is now */
1778 out_flush();
1779 ui_delay(2000L, TRUE);
1780 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001781 set_string_option_direct((char_u *)"term", -1, term,
1782 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 display_errors();
1784 }
1785 out_flush();
1786#ifdef HAVE_TGETENT
1787 if (!termcap_cleared)
1788 {
1789#endif
1790 clear_termoptions(); /* clear old options */
1791#ifdef HAVE_TGETENT
1792 termcap_cleared = TRUE;
1793 }
1794#endif
1795 parse_builtin_tcap(term);
1796#ifdef FEAT_GUI
1797 if (term_is_gui(term))
1798 {
1799 out_flush();
1800 gui_init();
1801 /* If starting the GUI failed, don't do any of the other
1802 * things for this terminal */
1803 if (!gui.in_use)
1804 return FAIL;
1805#ifdef HAVE_TGETENT
1806 break; /* don't try using external termcap */
1807#endif
1808 }
1809#endif /* FEAT_GUI */
1810 }
1811#ifdef HAVE_TGETENT
1812 }
1813#endif
1814
1815/*
1816 * special: There is no info in the termcap about whether the cursor
1817 * positioning is relative to the start of the screen or to the start of the
1818 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1819 * relative.
1820 */
1821 if (STRCMP(term, "pcterm") == 0)
1822 T_CCS = (char_u *)"yes";
1823 else
1824 T_CCS = empty_option;
1825
1826#ifdef UNIX
1827/*
1828 * Any "stty" settings override the default for t_kb from the termcap.
1829 * This is in os_unix.c, because it depends a lot on the version of unix that
1830 * is being used.
1831 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1832 */
1833#ifdef FEAT_GUI
1834 if (!gui.in_use)
1835#endif
1836 get_stty();
1837#endif
1838
1839/*
1840 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1841 * didn't work, use the default CTRL-H
1842 * The default for t_kD is DEL, unless t_kb is DEL.
1843 * The vim_strsave'd strings are probably lost forever, well it's only two
1844 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1845 * directly.
1846 */
1847#ifdef FEAT_GUI
1848 if (!gui.in_use)
1849#endif
1850 {
1851 bs_p = find_termcode((char_u *)"kb");
1852 del_p = find_termcode((char_u *)"kD");
1853 if (bs_p == NULL || *bs_p == NUL)
1854 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1855 if ((del_p == NULL || *del_p == NUL) &&
1856 (bs_p == NULL || *bs_p != DEL))
1857 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1858 }
1859
1860#if defined(UNIX) || defined(VMS)
1861 term_is_xterm = vim_is_xterm(term);
1862#endif
1863
1864#ifdef FEAT_MOUSE
1865# if defined(UNIX) || defined(VMS)
1866# ifdef FEAT_MOUSE_TTY
1867 /*
1868 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1869 * The termcode for the mouse is added as a side effect in option.c.
1870 */
1871 {
1872 char_u *p;
1873
1874 p = (char_u *)"";
1875# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001876 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
1878 if (use_xterm_mouse())
1879 p = NULL; /* keep existing value, might be "xterm2" */
1880 else
1881 p = (char_u *)"xterm";
1882 }
1883# endif
1884 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001885 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001887 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1888 * "xterm2" in check_termcode(). */
1889 reset_option_was_set((char_u *)"ttym");
1890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (p == NULL
1892# ifdef FEAT_GUI
1893 || gui.in_use
1894# endif
1895 )
1896 check_mouse_termcode(); /* set mouse termcode anyway */
1897 }
1898# endif
1899# else
1900 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1901# endif
1902#endif /* FEAT_MOUSE */
1903
1904#ifdef FEAT_SNIFF
1905 {
1906 char_u name[2];
1907
1908 name[0] = (int)KS_EXTRA;
1909 name[1] = (int)KE_SNIFF;
1910 add_termcode(name, (char_u *)"\233sniff", FALSE);
1911 }
1912#endif
1913
1914#ifdef USE_TERM_CONSOLE
1915 /* DEFAULT_TERM indicates that it is the machine console. */
1916 if (STRCMP(term, DEFAULT_TERM) != 0)
1917 term_console = FALSE;
1918 else
1919 {
1920 term_console = TRUE;
1921# ifdef AMIGA
1922 win_resize_on(); /* enable window resizing reports */
1923# endif
1924 }
1925#endif
1926
1927#if defined(UNIX) || defined(VMS)
1928 /*
1929 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1930 */
1931 if (vim_is_fastterm(term))
1932 p_tf = TRUE;
1933#endif
1934#ifdef USE_TERM_CONSOLE
1935 /*
1936 * 'ttyfast' is default on consoles
1937 */
1938 if (term_console)
1939 p_tf = TRUE;
1940#endif
1941
1942 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1943
1944 full_screen = TRUE; /* we can use termcap codes from now on */
1945 set_term_defaults(); /* use current values as defaults */
1946#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +02001947 LOG_TR("setting crv_status to CRV_GET");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 crv_status = CRV_GET; /* Get terminal version later */
1949#endif
1950
1951 /*
1952 * Initialize the terminal with the appropriate termcap codes.
1953 * Set the mouse and window title if possible.
1954 * Don't do this when starting, need to parse the .vimrc first, because it
1955 * may redefine t_TI etc.
1956 */
1957 if (starting != NO_SCREEN)
1958 {
1959 starttermcap(); /* may change terminal mode */
1960#ifdef FEAT_MOUSE
1961 setmouse(); /* may start using the mouse */
1962#endif
1963#ifdef FEAT_TITLE
1964 maketitle(); /* may display window title */
1965#endif
1966 }
1967
1968 /* display initial screen after ttest() checking. jw. */
1969 if (width <= 0 || height <= 0)
1970 {
1971 /* termcap failed to report size */
1972 /* set defaults, in case ui_get_shellsize() also fails */
1973 width = 80;
1974#if defined(MSDOS) || defined(WIN3264)
1975 height = 25; /* console is often 25 lines */
1976#else
1977 height = 24; /* most terminals are 24 lines */
1978#endif
1979 }
1980 set_shellsize(width, height, FALSE); /* may change Rows */
1981 if (starting != NO_SCREEN)
1982 {
1983 if (scroll_region)
1984 scroll_region_reset(); /* In case Rows changed */
1985 check_map_keycodes(); /* check mappings for terminal codes used */
1986
1987#ifdef FEAT_AUTOCMD
1988 {
1989 buf_T *old_curbuf;
1990
1991 /*
1992 * Execute the TermChanged autocommands for each buffer that is
1993 * loaded.
1994 */
1995 old_curbuf = curbuf;
1996 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
1997 {
1998 if (curbuf->b_ml.ml_mfp != NULL)
1999 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2000 curbuf);
2001 }
2002 if (buf_valid(old_curbuf))
2003 curbuf = old_curbuf;
2004 }
2005#endif
2006 }
2007
2008#ifdef FEAT_TERMRESPONSE
2009 may_req_termresponse();
2010#endif
2011
2012 return OK;
2013}
2014
2015#if defined(FEAT_MOUSE) || defined(PROTO)
2016
2017# ifdef FEAT_MOUSE_TTY
2018# define HMT_NORMAL 1
2019# define HMT_NETTERM 2
2020# define HMT_DEC 4
2021# define HMT_JSBTERM 8
2022# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002023# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002024# define HMT_SGR 64
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025static int has_mouse_termcode = 0;
2026# endif
2027
Bram Moolenaar864207d2008-06-24 22:14:38 +00002028# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002030set_mouse_termcode(
2031 int n, /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2032 char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033{
2034 char_u name[2];
2035
2036 name[0] = n;
2037 name[1] = KE_FILLER;
2038 add_termcode(name, s, FALSE);
2039# ifdef FEAT_MOUSE_TTY
2040# ifdef FEAT_MOUSE_JSB
2041 if (n == KS_JSBTERM_MOUSE)
2042 has_mouse_termcode |= HMT_JSBTERM;
2043 else
2044# endif
2045# ifdef FEAT_MOUSE_NET
2046 if (n == KS_NETTERM_MOUSE)
2047 has_mouse_termcode |= HMT_NETTERM;
2048 else
2049# endif
2050# ifdef FEAT_MOUSE_DEC
2051 if (n == KS_DEC_MOUSE)
2052 has_mouse_termcode |= HMT_DEC;
2053 else
2054# endif
2055# ifdef FEAT_MOUSE_PTERM
2056 if (n == KS_PTERM_MOUSE)
2057 has_mouse_termcode |= HMT_PTERM;
2058 else
2059# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002060# ifdef FEAT_MOUSE_URXVT
2061 if (n == KS_URXVT_MOUSE)
2062 has_mouse_termcode |= HMT_URXVT;
2063 else
2064# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002065# ifdef FEAT_MOUSE_SGR
2066 if (n == KS_SGR_MOUSE)
2067 has_mouse_termcode |= HMT_SGR;
2068 else
2069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 has_mouse_termcode |= HMT_NORMAL;
2071# endif
2072}
2073# endif
2074
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002075# if ((defined(UNIX) || defined(VMS)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002076 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002078del_mouse_termcode(
2079 int n) /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
2081 char_u name[2];
2082
2083 name[0] = n;
2084 name[1] = KE_FILLER;
2085 del_termcode(name);
2086# ifdef FEAT_MOUSE_TTY
2087# ifdef FEAT_MOUSE_JSB
2088 if (n == KS_JSBTERM_MOUSE)
2089 has_mouse_termcode &= ~HMT_JSBTERM;
2090 else
2091# endif
2092# ifdef FEAT_MOUSE_NET
2093 if (n == KS_NETTERM_MOUSE)
2094 has_mouse_termcode &= ~HMT_NETTERM;
2095 else
2096# endif
2097# ifdef FEAT_MOUSE_DEC
2098 if (n == KS_DEC_MOUSE)
2099 has_mouse_termcode &= ~HMT_DEC;
2100 else
2101# endif
2102# ifdef FEAT_MOUSE_PTERM
2103 if (n == KS_PTERM_MOUSE)
2104 has_mouse_termcode &= ~HMT_PTERM;
2105 else
2106# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002107# ifdef FEAT_MOUSE_URXVT
2108 if (n == KS_URXVT_MOUSE)
2109 has_mouse_termcode &= ~HMT_URXVT;
2110 else
2111# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002112# ifdef FEAT_MOUSE_SGR
2113 if (n == KS_SGR_MOUSE)
2114 has_mouse_termcode &= ~HMT_SGR;
2115 else
2116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 has_mouse_termcode &= ~HMT_NORMAL;
2118# endif
2119}
2120# endif
2121#endif
2122
2123#ifdef HAVE_TGETENT
2124/*
2125 * Call tgetent()
2126 * Return error message if it fails, NULL if it's OK.
2127 */
2128 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002129tgetent_error(char_u *tbuf, char_u *term)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130{
2131 int i;
2132
2133 i = TGETENT(tbuf, term);
2134 if (i < 0 /* -1 is always an error */
2135# ifdef TGETENT_ZERO_ERR
2136 || i == 0 /* sometimes zero is also an error */
2137# endif
2138 )
2139 {
2140 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2141 * tgetent() with the always existing "dumb" entry to avoid a crash or
2142 * hang. */
2143 (void)TGETENT(tbuf, "dumb");
2144
2145 if (i < 0)
2146# ifdef TGETENT_ZERO_ERR
2147 return (char_u *)_("E557: Cannot open termcap file");
2148 if (i == 0)
2149# endif
2150#ifdef TERMINFO
2151 return (char_u *)_("E558: Terminal entry not found in terminfo");
2152#else
2153 return (char_u *)_("E559: Terminal entry not found in termcap");
2154#endif
2155 }
2156 return NULL;
2157}
2158
2159/*
2160 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2161 * Fix that here.
2162 */
2163 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002164vim_tgetstr(char *s, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165{
2166 char *p;
2167
2168 p = tgetstr(s, (char **)pp);
2169 if (p == (char *)-1)
2170 p = NULL;
2171 return (char_u *)p;
2172}
2173#endif /* HAVE_TGETENT */
2174
2175#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(__EMX__) || defined(VMS) || defined(MACOS_X))
2176/*
2177 * Get Columns and Rows from the termcap. Used after a window signal if the
2178 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2179 * and "li" entries never change. But on some systems this works.
2180 * Errors while getting the entries are ignored.
2181 */
2182 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002183getlinecol(
2184 long *cp, /* pointer to columns */
2185 long *rp) /* pointer to rows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186{
2187 char_u tbuf[TBUFSZ];
2188
2189 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2190 {
2191 if (*cp == 0)
2192 *cp = tgetnum("co");
2193 if (*rp == 0)
2194 *rp = tgetnum("li");
2195 }
2196}
2197#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2198
2199/*
2200 * Get a string entry from the termcap and add it to the list of termcodes.
2201 * Used for <t_xx> special keys.
2202 * Give an error message for failure when not sourcing.
2203 * If force given, replace an existing entry.
2204 * Return FAIL if the entry was not found, OK if the entry was added.
2205 */
2206 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002207add_termcap_entry(char_u *name, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
2209 char_u *term;
2210 int key;
2211 struct builtin_term *termp;
2212#ifdef HAVE_TGETENT
2213 char_u *string;
2214 int i;
2215 int builtin_first;
2216 char_u tbuf[TBUFSZ];
2217 char_u tstrbuf[TBUFSZ];
2218 char_u *tp = tstrbuf;
2219 char_u *error_msg = NULL;
2220#endif
2221
2222/*
2223 * If the GUI is running or will start in a moment, we only support the keys
2224 * that the GUI can produce.
2225 */
2226#ifdef FEAT_GUI
2227 if (gui.in_use || gui.starting)
2228 return gui_mch_haskey(name);
2229#endif
2230
2231 if (!force && find_termcode(name) != NULL) /* it's already there */
2232 return OK;
2233
2234 term = T_NAME;
2235 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2236 return FAIL;
2237
2238 if (term_is_builtin(term)) /* name starts with "builtin_" */
2239 {
2240 term += 8;
2241#ifdef HAVE_TGETENT
2242 builtin_first = TRUE;
2243#endif
2244 }
2245#ifdef HAVE_TGETENT
2246 else
2247 builtin_first = p_tbi;
2248#endif
2249
2250#ifdef HAVE_TGETENT
2251/*
2252 * We can get the entry from the builtin termcap and from the external one.
2253 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2254 * builtin termcap first.
2255 * If 'ttybuiltin' is off, try external termcap first.
2256 */
2257 for (i = 0; i < 2; ++i)
2258 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002259 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260#endif
2261 /*
2262 * Search in builtin termcap
2263 */
2264 {
2265 termp = find_builtin_term(term);
2266 if (termp->bt_string != NULL) /* found it */
2267 {
2268 key = TERMCAP2KEY(name[0], name[1]);
2269 while (termp->bt_entry != (int)KS_NAME)
2270 {
2271 if ((int)termp->bt_entry == key)
2272 {
2273 add_termcode(name, (char_u *)termp->bt_string,
2274 term_is_8bit(term));
2275 return OK;
2276 }
2277 ++termp;
2278 }
2279 }
2280 }
2281#ifdef HAVE_TGETENT
2282 else
2283 /*
2284 * Search in external termcap
2285 */
2286 {
2287 error_msg = tgetent_error(tbuf, term);
2288 if (error_msg == NULL)
2289 {
2290 string = TGETSTR((char *)name, &tp);
2291 if (string != NULL && *string != NUL)
2292 {
2293 add_termcode(name, string, FALSE);
2294 return OK;
2295 }
2296 }
2297 }
2298 }
2299#endif
2300
2301 if (sourcing_name == NULL)
2302 {
2303#ifdef HAVE_TGETENT
2304 if (error_msg != NULL)
2305 EMSG(error_msg);
2306 else
2307#endif
2308 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2309 }
2310 return FAIL;
2311}
2312
2313 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002314term_is_builtin(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315{
2316 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2317}
2318
2319/*
2320 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2321 * Assume that the terminal is using 8-bit controls when the name contains
2322 * "8bit", like in "xterm-8bit".
2323 */
2324 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002325term_is_8bit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326{
2327 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2328}
2329
2330/*
2331 * Translate terminal control chars from 7-bit to 8-bit:
2332 * <Esc>[ -> CSI
2333 * <Esc>] -> <M-C-]>
2334 * <Esc>O -> <M-C-O>
2335 */
2336 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002337term_7to8bit(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338{
2339 if (*p == ESC)
2340 {
2341 if (p[1] == '[')
2342 return CSI;
2343 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002344 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (p[1] == 'O')
2346 return 0x8f;
2347 }
2348 return 0;
2349}
2350
2351#ifdef FEAT_GUI
2352 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002353term_is_gui(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354{
2355 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2356}
2357#endif
2358
2359#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2360
2361 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002362tltoa(unsigned long i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
2364 static char_u buf[16];
2365 char_u *p;
2366
2367 p = buf + 15;
2368 *p = '\0';
2369 do
2370 {
2371 --p;
2372 *p = (char_u) (i % 10 + '0');
2373 i /= 10;
2374 }
2375 while (i > 0 && p > buf);
2376 return p;
2377}
2378#endif
2379
2380#ifndef HAVE_TGETENT
2381
2382/*
2383 * minimal tgoto() implementation.
2384 * no padding and we only parse for %i %d and %+char
2385 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002386static char *tgoto(char *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002388 static char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002389tgoto(char *cm, int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
2391 static char buf[30];
2392 char *p, *s, *e;
2393
2394 if (!cm)
2395 return "OOPS";
2396 e = buf + 29;
2397 for (s = buf; s < e && *cm; cm++)
2398 {
2399 if (*cm != '%')
2400 {
2401 *s++ = *cm;
2402 continue;
2403 }
2404 switch (*++cm)
2405 {
2406 case 'd':
2407 p = (char *)tltoa((unsigned long)y);
2408 y = x;
2409 while (*p)
2410 *s++ = *p++;
2411 break;
2412 case 'i':
2413 x++;
2414 y++;
2415 break;
2416 case '+':
2417 *s++ = (char)(*++cm + y);
2418 y = x;
2419 break;
2420 case '%':
2421 *s++ = *cm;
2422 break;
2423 default:
2424 return "OOPS";
2425 }
2426 }
2427 *s = '\0';
2428 return buf;
2429}
2430
2431#endif /* HAVE_TGETENT */
2432
2433/*
2434 * Set the terminal name and initialize the terminal options.
2435 * If "name" is NULL or empty, get the terminal name from the environment.
2436 * If that fails, use the default terminal name.
2437 */
2438 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002439termcapinit(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
2441 char_u *term;
2442
2443 if (name != NULL && *name == NUL)
2444 name = NULL; /* empty name is equal to no name */
2445 term = name;
2446
2447#ifdef __BEOS__
2448 /*
2449 * TERM environment variable is normally set to 'ansi' on the Bebox;
2450 * Since the BeBox doesn't quite support full ANSI yet, we use our
2451 * own custom 'ansi-beos' termcap instead, unless the -T option has
2452 * been given on the command line.
2453 */
2454 if (term == NULL
2455 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2456 term = DEFAULT_TERM;
2457#endif
2458#ifndef MSWIN
2459 if (term == NULL)
2460 term = mch_getenv((char_u *)"TERM");
2461#endif
2462 if (term == NULL || *term == NUL)
2463 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002464 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
2466 /* Set the default terminal name. */
2467 set_string_default("term", term);
2468 set_string_default("ttytype", term);
2469
2470 /*
2471 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2472 */
2473 set_termname(T_NAME != NULL ? T_NAME : term);
2474}
2475
2476/*
2477 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2478 */
2479#ifdef DOS16
2480# define OUT_SIZE 255 /* only have 640K total... */
2481#else
2482# ifdef FEAT_GUI_W16
2483# define OUT_SIZE 1023 /* Save precious 1K near data */
2484# else
2485# define OUT_SIZE 2047
2486# endif
2487#endif
2488 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2489static char_u out_buf[OUT_SIZE + 1];
2490static int out_pos = 0; /* number of chars in out_buf */
2491
2492/*
2493 * out_flush(): flush the output buffer
2494 */
2495 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002496out_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 int len;
2499
2500 if (out_pos != 0)
2501 {
2502 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2503 len = out_pos;
2504 out_pos = 0;
2505 ui_write(out_buf, len);
2506 }
2507}
2508
2509#if defined(FEAT_MBYTE) || defined(PROTO)
2510/*
2511 * Sometimes a byte out of a multi-byte character is written with out_char().
2512 * To avoid flushing half of the character, call this function first.
2513 */
2514 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002515out_flush_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516{
2517 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2518 out_flush();
2519}
2520#endif
2521
2522#ifdef FEAT_GUI
2523/*
2524 * out_trash(): Throw away the contents of the output buffer
2525 */
2526 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002527out_trash(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528{
2529 out_pos = 0;
2530}
2531#endif
2532
2533/*
2534 * out_char(c): put a byte into the output buffer.
2535 * Flush it if it becomes full.
2536 * This should not be used for outputting text on the screen (use functions
2537 * like msg_puts() and screen_putchar() for that).
2538 */
2539 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002540out_char(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541{
2542#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2543 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2544 out_char('\r');
2545#endif
2546
2547 out_buf[out_pos++] = c;
2548
2549 /* For testing we flush each time. */
2550 if (out_pos >= OUT_SIZE || p_wd)
2551 out_flush();
2552}
2553
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002554static void out_char_nf(unsigned);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
2556/*
2557 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2558 */
2559 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002560out_char_nf(unsigned c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561{
2562#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2563 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2564 out_char_nf('\r');
2565#endif
2566
2567 out_buf[out_pos++] = c;
2568
2569 if (out_pos >= OUT_SIZE)
2570 out_flush();
2571}
2572
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002573#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2574 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575/*
2576 * A never-padding out_str.
2577 * use this whenever you don't want to run the string through tputs.
2578 * tputs above is harmless, but tputs from the termcap library
2579 * is likely to strip off leading digits, that it mistakes for padding
2580 * information, and "%i", "%d", etc.
2581 * This should only be used for writing terminal codes, not for outputting
2582 * normal text (use functions like msg_puts() and screen_putchar() for that).
2583 */
2584 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002585out_str_nf(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
2587 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2588 out_flush();
2589 while (*s)
2590 out_char_nf(*s++);
2591
2592 /* For testing we write one string at a time. */
2593 if (p_wd)
2594 out_flush();
2595}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
2598/*
2599 * out_str(s): Put a character string a byte at a time into the output buffer.
2600 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2601 * This should only be used for writing terminal codes, not for outputting
2602 * normal text (use functions like msg_puts() and screen_putchar() for that).
2603 */
2604 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002605out_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
2607 if (s != NULL && *s)
2608 {
2609#ifdef FEAT_GUI
2610 /* Don't use tputs() when GUI is used, ncurses crashes. */
2611 if (gui.in_use)
2612 {
2613 out_str_nf(s);
2614 return;
2615 }
2616#endif
2617 /* avoid terminal strings being split up */
2618 if (out_pos > OUT_SIZE - 20)
2619 out_flush();
2620#ifdef HAVE_TGETENT
2621 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2622#else
2623 while (*s)
2624 out_char_nf(*s++);
2625#endif
2626
2627 /* For testing we write one string at a time. */
2628 if (p_wd)
2629 out_flush();
2630 }
2631}
2632
2633/*
2634 * cursor positioning using termcap parser. (jw)
2635 */
2636 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002637term_windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638{
2639 OUT_STR(tgoto((char *)T_CM, col, row));
2640}
2641
2642 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002643term_cursor_right(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644{
2645 OUT_STR(tgoto((char *)T_CRI, 0, i));
2646}
2647
2648 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002649term_append_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
2651 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2652}
2653
2654 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002655term_delete_lines(int line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
2657 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2658}
2659
2660#if defined(HAVE_TGETENT) || defined(PROTO)
2661 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002662term_set_winpos(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663{
2664 /* Can't handle a negative value here */
2665 if (x < 0)
2666 x = 0;
2667 if (y < 0)
2668 y = 0;
2669 OUT_STR(tgoto((char *)T_CWP, y, x));
2670}
2671
2672 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002673term_set_winsize(int width, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
2675 OUT_STR(tgoto((char *)T_CWS, height, width));
2676}
2677#endif
2678
2679 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002680term_fg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
2682 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2683 if (*T_CAF)
2684 term_color(T_CAF, n);
2685 else if (*T_CSF)
2686 term_color(T_CSF, n);
2687}
2688
2689 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002690term_bg_color(int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2693 if (*T_CAB)
2694 term_color(T_CAB, n);
2695 else if (*T_CSB)
2696 term_color(T_CSB, n);
2697}
2698
2699 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002700term_color(char_u *s, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
2702 char buf[20];
2703 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2704
2705 /* Special handling of 16 colors, because termcap can't handle it */
2706 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2707 /* Also accept CSI instead of <Esc>[ */
2708 if (n >= 8 && t_colors >= 16
2709 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2710 && s[i] != NUL
2711 && (STRCMP(s + i + 1, "%p1%dm") == 0
2712 || STRCMP(s + i + 1, "%dm") == 0)
2713 && (s[i] == '3' || s[i] == '4'))
2714 {
2715 sprintf(buf,
2716#ifdef TERMINFO
2717 "%s%s%%p1%%dm",
2718#else
2719 "%s%s%%dm",
2720#endif
2721 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2722 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2723 : (n >= 16 ? "48;5;" : "10"));
2724 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2725 }
2726 else
2727 OUT_STR(tgoto((char *)s, 0, n));
2728}
2729
Bram Moolenaare7fedb62015-12-31 19:07:19 +01002730#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) \
2731 || defined(MACOS_X))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732/*
2733 * Generic function to set window title, using t_ts and t_fs.
2734 */
2735 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002736term_settitle(char_u *title)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737{
2738 /* t_ts takes one argument: column in status line */
2739 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2740 out_str_nf(title);
2741 out_str(T_FS); /* set title end */
2742 out_flush();
2743}
2744#endif
2745
2746/*
2747 * Make sure we have a valid set or terminal options.
2748 * Replace all entries that are NULL by empty_option
2749 */
2750 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002751ttest(int pairs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752{
2753 check_options(); /* make sure no options are NULL */
2754
2755 /*
2756 * MUST have "cm": cursor motion.
2757 */
2758 if (*T_CM == NUL)
2759 EMSG(_("E437: terminal capability \"cm\" required"));
2760
2761 /*
2762 * if "cs" defined, use a scroll region, it's faster.
2763 */
2764 if (*T_CS != NUL)
2765 scroll_region = TRUE;
2766 else
2767 scroll_region = FALSE;
2768
2769 if (pairs)
2770 {
2771 /*
2772 * optional pairs
2773 */
2774 /* TP goes to normal mode for TI (invert) and TB (bold) */
2775 if (*T_ME == NUL)
2776 T_ME = T_MR = T_MD = T_MB = empty_option;
2777 if (*T_SO == NUL || *T_SE == NUL)
2778 T_SO = T_SE = empty_option;
2779 if (*T_US == NUL || *T_UE == NUL)
2780 T_US = T_UE = empty_option;
2781 if (*T_CZH == NUL || *T_CZR == NUL)
2782 T_CZH = T_CZR = empty_option;
2783
2784 /* T_VE is needed even though T_VI is not defined */
2785 if (*T_VE == NUL)
2786 T_VI = empty_option;
2787
2788 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2789 if (*T_ME == NUL)
2790 {
2791 T_ME = T_SE;
2792 T_MR = T_SO;
2793 T_MD = T_SO;
2794 }
2795
2796 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2797 if (*T_SO == NUL)
2798 {
2799 T_SE = T_ME;
2800 if (*T_MR == NUL)
2801 T_SO = T_MD;
2802 else
2803 T_SO = T_MR;
2804 }
2805
2806 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2807 if (*T_CZH == NUL)
2808 {
2809 T_CZR = T_ME;
2810 if (*T_MR == NUL)
2811 T_CZH = T_MD;
2812 else
2813 T_CZH = T_MR;
2814 }
2815
2816 /* "Sb" and "Sf" come in pairs */
2817 if (*T_CSB == NUL || *T_CSF == NUL)
2818 {
2819 T_CSB = empty_option;
2820 T_CSF = empty_option;
2821 }
2822
2823 /* "AB" and "AF" come in pairs */
2824 if (*T_CAB == NUL || *T_CAF == NUL)
2825 {
2826 T_CAB = empty_option;
2827 T_CAF = empty_option;
2828 }
2829
2830 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2831 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002832 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
2834 /* Set 'weirdinvert' according to value of 't_xs' */
2835 p_wiv = (*T_XS != NUL);
2836 }
2837 need_gather = TRUE;
2838
2839 /* Set t_colors to the value of t_Co. */
2840 t_colors = atoi((char *)T_CCO);
2841}
2842
2843#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2844 || defined(PROTO)
2845/*
2846 * Represent the given long_u as individual bytes, with the most significant
2847 * byte first, and store them in dst.
2848 */
2849 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002850add_long_to_buf(long_u val, char_u *dst)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851{
2852 int i;
2853 int shift;
2854
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002855 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
2857 shift = 8 * (sizeof(long_u) - i);
2858 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2859 }
2860}
2861
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002862static int get_long_from_buf(char_u *buf, long_u *val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
2864/*
2865 * Interpret the next string of bytes in buf as a long integer, with the most
2866 * significant byte first. Note that it is assumed that buf has been through
2867 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
2868 * Puts result in val, and returns the number of bytes read from buf
2869 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
2870 * were present.
2871 */
2872 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002873get_long_from_buf(char_u *buf, long_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
2875 int len;
2876 char_u bytes[sizeof(long_u)];
2877 int i;
2878 int shift;
2879
2880 *val = 0;
2881 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
2882 if (len != -1)
2883 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002884 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
2886 shift = 8 * (sizeof(long_u) - 1 - i);
2887 *val += (long_u)bytes[i] << shift;
2888 }
2889 }
2890 return len;
2891}
2892#endif
2893
2894#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002895 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
2896 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897/*
2898 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
2899 * that buf has been through inchar(). Returns the actual number of bytes used
2900 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
2901 * available.
2902 */
2903 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002904get_bytes_from_buf(char_u *buf, char_u *bytes, int num_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 int len = 0;
2907 int i;
2908 char_u c;
2909
2910 for (i = 0; i < num_bytes; i++)
2911 {
2912 if ((c = buf[len++]) == NUL)
2913 return -1;
2914 if (c == K_SPECIAL)
2915 {
2916 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
2917 return -1;
2918 if (buf[len++] == (int)KS_ZERO)
2919 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02002920 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
2921 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
2922 if (buf[len++] == (int)KE_CSI)
2923 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00002925 else if (c == CSI && buf[len] == KS_EXTRA
2926 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002927 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
2928 * the start of a special key, see add_to_input_buf_csi(). */
2929 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 bytes[i] = c;
2931 }
2932 return len;
2933}
2934#endif
2935
2936/*
Bram Moolenaare057d402013-06-30 17:51:51 +02002937 * Check if the new shell size is valid, correct it if it's too small or way
2938 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 */
2940 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002941check_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 if (Rows < min_rows()) /* need room for one window and command line */
2944 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02002945 limit_screen_size();
2946}
2947
2948/*
2949 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
2950 */
2951 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002952limit_screen_size(void)
Bram Moolenaare057d402013-06-30 17:51:51 +02002953{
2954 if (Columns < MIN_COLUMNS)
2955 Columns = MIN_COLUMNS;
2956 else if (Columns > 10000)
2957 Columns = 10000;
2958 if (Rows > 1000)
2959 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960}
2961
Bram Moolenaar86b68352004-12-27 21:59:20 +00002962/*
2963 * Invoked just before the screen structures are going to be (re)allocated.
2964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002966win_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967{
2968 static int old_Rows = 0;
2969 static int old_Columns = 0;
2970
2971 if (old_Rows != Rows || old_Columns != Columns)
2972 ui_new_shellsize();
2973 if (old_Rows != Rows)
2974 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002975 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002976 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002977 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 old_Rows = Rows;
2979 shell_new_rows(); /* update window sizes */
2980 }
2981 if (old_Columns != Columns)
2982 {
2983 old_Columns = Columns;
2984#ifdef FEAT_VERTSPLIT
2985 shell_new_columns(); /* update window sizes */
2986#endif
2987 }
2988}
2989
2990/*
2991 * Call this function when the Vim shell has been resized in any way.
2992 * Will obtain the current size and redraw (also when size didn't change).
2993 */
2994 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002995shell_resized(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 set_shellsize(0, 0, FALSE);
2998}
2999
3000/*
3001 * Check if the shell size changed. Handle a resize.
3002 * When the size didn't change, nothing happens.
3003 */
3004 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003005shell_resized_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
3007 int old_Rows = Rows;
3008 int old_Columns = Columns;
3009
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003010 if (!exiting
3011#ifdef FEAT_GUI
3012 /* Do not get the size when executing a shell command during
3013 * startup. */
3014 && !gui.starting
3015#endif
3016 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003017 {
3018 (void)ui_get_shellsize();
3019 check_shellsize();
3020 if (old_Rows != Rows || old_Columns != Columns)
3021 shell_resized();
3022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023}
3024
3025/*
3026 * Set size of the Vim shell.
3027 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3028 * window size (this is used for the :win command).
3029 * If 'mustset' is FALSE, we may try to get the real window size and if
3030 * it fails use 'width' and 'height'.
3031 */
3032 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003033set_shellsize(int width, int height, int mustset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034{
3035 static int busy = FALSE;
3036
3037 /*
3038 * Avoid recursiveness, can happen when setting the window size causes
3039 * another window-changed signal.
3040 */
3041 if (busy)
3042 return;
3043
3044 if (width < 0 || height < 0) /* just checking... */
3045 return;
3046
Bram Moolenaara971b822011-09-14 14:43:25 +02003047 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003049 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 State = SETWSIZE;
3051 return;
3052 }
3053
Bram Moolenaara971b822011-09-14 14:43:25 +02003054 /* curwin->w_buffer can be NULL when we are closing a window and the
3055 * buffer has already been closed and removing a scrollbar causes a resize
3056 * event. Don't resize then, it will happen after entering another buffer.
3057 */
3058 if (curwin->w_buffer == NULL)
3059 return;
3060
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 ++busy;
3062
3063#ifdef AMIGA
3064 out_flush(); /* must do this before mch_get_shellsize() for
3065 some obscure reason */
3066#endif
3067
3068 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3069 {
3070 Rows = height;
3071 Columns = width;
3072 check_shellsize();
3073 ui_set_shellsize(mustset);
3074 }
3075 else
3076 check_shellsize();
3077
3078 /* The window layout used to be adjusted here, but it now happens in
3079 * screenalloc() (also invoked from screenclear()). That is because the
3080 * "busy" check above may skip this, but not screenalloc(). */
3081
3082 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3083 screenclear();
3084 else
3085 screen_start(); /* don't know where cursor is now */
3086
3087 if (starting != NO_SCREEN)
3088 {
3089#ifdef FEAT_TITLE
3090 maketitle();
3091#endif
3092 changed_line_abv_curs();
3093 invalidate_botline();
3094
3095 /*
3096 * We only redraw when it's needed:
3097 * - While at the more prompt or executing an external command, don't
3098 * redraw, but position the cursor.
3099 * - While editing the command line, only redraw that.
3100 * - in Ex mode, don't redraw anything.
3101 * - Otherwise, redraw right now, and position the cursor.
3102 * Always need to call update_screen() or screenalloc(), to make
3103 * sure Rows/Columns and the size of ScreenLines[] is correct!
3104 */
3105 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3106 || exmode_active)
3107 {
3108 screenalloc(FALSE);
3109 repeat_message();
3110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 else
3112 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003113#ifdef FEAT_SCROLLBIND
3114 if (curwin->w_p_scb)
3115 do_check_scrollbind(TRUE);
3116#endif
3117 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003118 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003119 update_screen(NOT_VALID);
3120 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003121 }
3122 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003123 {
3124 update_topline();
3125#if defined(FEAT_INS_EXPAND)
3126 if (pum_visible())
3127 {
3128 redraw_later(NOT_VALID);
3129 ins_compl_show_pum(); /* This includes the redraw. */
3130 }
3131 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003132#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003133 update_screen(NOT_VALID);
3134 if (redrawing())
3135 setcursor();
3136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 }
3138 cursor_on(); /* redrawing may have switched it off */
3139 }
3140 out_flush();
3141 --busy;
3142}
3143
3144/*
3145 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3146 * commands and Ex mode).
3147 */
3148 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003149settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
3151#ifdef FEAT_GUI
3152 /* don't set the term where gvim was started to any mode */
3153 if (gui.in_use)
3154 return;
3155#endif
3156
3157 if (full_screen)
3158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 /*
3160 * When returning after calling a shell we want to really set the
3161 * terminal to raw mode, even though we think it already is, because
3162 * the shell program may have reset the terminal mode.
3163 * When we think the terminal is normal, don't try to set it to
3164 * normal again, because that causes problems (logout!) on some
3165 * machines.
3166 */
3167 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3168 {
3169#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003170# ifdef FEAT_GUI
3171 if (!gui.in_use && !gui.starting)
3172# endif
3173 {
3174 /* May need to check for T_CRV response and termcodes, it
3175 * doesn't work in Cooked mode, an external program may get
3176 * them. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003177 if (tmode != TMODE_RAW && (crv_status == CRV_SENT
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003178 || u7_status == U7_SENT
3179 || rbg_status == RBG_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003180 (void)vpeekc_nomap();
3181 check_for_codes_from_term();
3182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183#endif
3184#ifdef FEAT_MOUSE_TTY
3185 if (tmode != TMODE_RAW)
3186 mch_setmouse(FALSE); /* switch mouse off */
3187#endif
3188 out_flush();
3189 mch_settmode(tmode); /* machine specific function */
3190 cur_tmode = tmode;
3191#ifdef FEAT_MOUSE
3192 if (tmode == TMODE_RAW)
3193 setmouse(); /* may switch mouse on */
3194#endif
3195 out_flush();
3196 }
3197#ifdef FEAT_TERMRESPONSE
3198 may_req_termresponse();
3199#endif
3200 }
3201}
3202
3203 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003204starttermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205{
3206 if (full_screen && !termcap_active)
3207 {
3208 out_str(T_TI); /* start termcap mode */
3209 out_str(T_KS); /* start "keypad transmit" mode */
3210 out_flush();
3211 termcap_active = TRUE;
3212 screen_start(); /* don't know where cursor is now */
3213#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003214# ifdef FEAT_GUI
3215 if (!gui.in_use && !gui.starting)
3216# endif
3217 {
3218 may_req_termresponse();
3219 /* Immediately check for a response. If t_Co changes, we don't
3220 * want to redraw with wrong colors first. */
Bram Moolenaar90013c62014-05-22 18:14:31 +02003221 if (crv_status == CRV_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003222 check_for_codes_from_term();
3223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224#endif
3225 }
3226}
3227
3228 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003229stoptermcap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
3231 screen_stop_highlight();
3232 reset_cterm_colors();
3233 if (termcap_active)
3234 {
3235#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003236# ifdef FEAT_GUI
3237 if (!gui.in_use && !gui.starting)
3238# endif
3239 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003240 /* May need to discard T_CRV, T_U7 or T_RBG response. */
3241 if (crv_status == CRV_SENT || u7_status == U7_SENT
3242 || rbg_status == RBG_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003243 {
3244# ifdef UNIX
3245 /* Give the terminal a chance to respond. */
3246 mch_delay(100L, FALSE);
3247# endif
3248# ifdef TCIFLUSH
3249 /* Discard data received but not read. */
3250 if (exiting)
3251 tcflush(fileno(stdin), TCIFLUSH);
3252# endif
3253 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003254 /* Check for termcodes first, otherwise an external program may
3255 * get them. */
3256 check_for_codes_from_term();
3257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258#endif
3259 out_str(T_KE); /* stop "keypad transmit" mode */
3260 out_flush();
3261 termcap_active = FALSE;
3262 cursor_on(); /* just in case it is still off */
3263 out_str(T_TE); /* stop termcap mode */
3264 screen_start(); /* don't know where cursor is now */
3265 out_flush();
3266 }
3267}
3268
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003269#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270/*
3271 * Request version string (for xterm) when needed.
3272 * Only do this after switching to raw mode, otherwise the result will be
3273 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003274 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003275 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 * Only do this after termcap mode has been started, otherwise the codes for
3277 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003278 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3279 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003280 * On Unix only do it when both output and input are a tty (avoid writing
3281 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 * The result is caught in check_termcode().
3283 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003284 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003285may_req_termresponse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287 if (crv_status == CRV_GET
3288 && cur_tmode == TMODE_RAW
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003289 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 && termcap_active
Bram Moolenaarebefac62005-12-28 22:39:57 +00003291 && p_ek
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003292# ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 && isatty(1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003294 && isatty(read_cmd_fd)
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 && *T_CRV != NUL)
3297 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02003298 LOG_TR("Sending CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 out_str(T_CRV);
3300 crv_status = CRV_SENT;
3301 /* check for the characters now, otherwise they might be eaten by
3302 * get_keystroke() */
3303 out_flush();
3304 (void)vpeekc_nomap();
3305 }
3306}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003307
3308# if defined(FEAT_MBYTE) || defined(PROTO)
3309/*
3310 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003311 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003312 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003313 * If the terminal treats \u25bd as single width, the position is (1, 1),
3314 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003315 * This function has the side effect that changes cursor position, so
3316 * it must be called immediately after entering termcap mode.
3317 */
3318 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003319may_req_ambiguous_char_width(void)
Bram Moolenaar9584b312013-03-13 19:29:28 +01003320{
3321 if (u7_status == U7_GET
3322 && cur_tmode == TMODE_RAW
3323 && termcap_active
3324 && p_ek
3325# ifdef UNIX
3326 && isatty(1)
3327 && isatty(read_cmd_fd)
3328# endif
3329 && *T_U7 != NUL
3330 && !option_was_set((char_u *)"ambiwidth"))
3331 {
3332 char_u buf[16];
3333
Bram Moolenaar2951b772013-07-03 12:45:31 +02003334 LOG_TR("Sending U7 request");
3335 /* Do this in the second row. In the first row the returned sequence
3336 * may be CSI 1;2R, which is the same as <S-F3>. */
3337 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003338 buf[mb_char2bytes(0x25bd, buf)] = 0;
3339 out_str(buf);
3340 out_str(T_U7);
3341 u7_status = U7_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003342 out_flush();
3343 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003344 out_str((char_u *)" ");
3345 term_windgoto(0, 0);
3346 /* check for the characters now, otherwise they might be eaten by
3347 * get_keystroke() */
3348 out_flush();
3349 (void)vpeekc_nomap();
3350 }
3351}
3352# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003353
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003354#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
3355/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003356 * Similar to requesting the version string: Request the terminal background
3357 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003358 */
3359 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003360may_req_bg_color(void)
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003361{
3362 if (rbg_status == RBG_GET
3363 && cur_tmode == TMODE_RAW
3364 && termcap_active
3365 && p_ek
3366# ifdef UNIX
3367 && isatty(1)
3368 && isatty(read_cmd_fd)
3369# endif
3370 && *T_RBG != NUL
3371 && !option_was_set((char_u *)"bg"))
3372 {
3373 LOG_TR("Sending BG request");
3374 out_str(T_RBG);
3375 rbg_status = RBG_SENT;
3376 /* check for the characters now, otherwise they might be eaten by
3377 * get_keystroke() */
3378 out_flush();
3379 (void)vpeekc_nomap();
3380 }
3381}
3382# endif
3383
Bram Moolenaar2951b772013-07-03 12:45:31 +02003384# ifdef DEBUG_TERMRESPONSE
3385 static void
3386log_tr(char *msg)
3387{
3388 static FILE *fd_tr = NULL;
3389 static proftime_T start;
3390 proftime_T now;
3391
3392 if (fd_tr == NULL)
3393 {
3394 fd_tr = fopen("termresponse.log", "w");
3395 profile_start(&start);
3396 }
3397 now = start;
3398 profile_end(&now);
3399 fprintf(fd_tr, "%s: %s %s\n",
3400 profile_msg(&now),
3401 must_redraw == NOT_VALID ? "NV"
3402 : must_redraw == CLEAR ? "CL" : " ",
3403 msg);
3404}
3405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406#endif
3407
3408/*
3409 * Return TRUE when saving and restoring the screen.
3410 */
3411 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003412swapping_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
3414 return (full_screen && *T_TI != NUL);
3415}
3416
3417#ifdef FEAT_MOUSE
3418/*
3419 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3420 */
3421 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003422setmouse(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
3424# ifdef FEAT_MOUSE_TTY
3425 int checkfor;
3426# endif
3427
3428# ifdef FEAT_MOUSESHAPE
3429 update_mouseshape(-1);
3430# endif
3431
3432# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3433# ifdef FEAT_GUI
3434 /* In the GUI the mouse is always enabled. */
3435 if (gui.in_use)
3436 return;
3437# endif
3438 /* be quick when mouse is off */
3439 if (*p_mouse == NUL || has_mouse_termcode == 0)
3440 return;
3441
3442 /* don't switch mouse on when not in raw mode (Ex mode) */
3443 if (cur_tmode != TMODE_RAW)
3444 {
3445 mch_setmouse(FALSE);
3446 return;
3447 }
3448
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 if (VIsual_active)
3450 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003451 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 checkfor = MOUSE_RETURN;
3453 else if (State & INSERT)
3454 checkfor = MOUSE_INSERT;
3455 else if (State & CMDLINE)
3456 checkfor = MOUSE_COMMAND;
3457 else if (State == CONFIRM || State == EXTERNCMD)
3458 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3459 else
3460 checkfor = MOUSE_NORMAL; /* assume normal mode */
3461
3462 if (mouse_has(checkfor))
3463 mch_setmouse(TRUE);
3464 else
3465 mch_setmouse(FALSE);
3466# endif
3467}
3468
3469/*
3470 * Return TRUE if
3471 * - "c" is in 'mouse', or
3472 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3473 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3474 * normal editing mode (not at hit-return message).
3475 */
3476 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003477mouse_has(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
3479 char_u *p;
3480
3481 for (p = p_mouse; *p; ++p)
3482 switch (*p)
3483 {
3484 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3485 return TRUE;
3486 break;
3487 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3488 return TRUE;
3489 break;
3490 default: if (c == *p) return TRUE; break;
3491 }
3492 return FALSE;
3493}
3494
3495/*
3496 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3497 */
3498 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003499mouse_model_popup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
3501 return (p_mousem[0] == 'p');
3502}
3503#endif
3504
3505/*
3506 * By outputting the 'cursor very visible' termcap code, for some windowed
3507 * terminals this makes the screen scrolled to the correct position.
3508 * Used when starting Vim or returning from a shell.
3509 */
3510 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003511scroll_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512{
3513 if (*T_VS != NUL)
3514 {
3515 out_str(T_VS);
3516 out_str(T_VE);
3517 screen_start(); /* don't know where cursor is now */
3518 }
3519}
3520
3521static int cursor_is_off = FALSE;
3522
3523/*
3524 * Enable the cursor.
3525 */
3526 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003527cursor_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528{
3529 if (cursor_is_off)
3530 {
3531 out_str(T_VE);
3532 cursor_is_off = FALSE;
3533 }
3534}
3535
3536/*
3537 * Disable the cursor.
3538 */
3539 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003540cursor_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541{
3542 if (full_screen)
3543 {
3544 if (!cursor_is_off)
3545 out_str(T_VI); /* disable cursor */
3546 cursor_is_off = TRUE;
3547 }
3548}
3549
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003550#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003552 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003553 */
3554 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003555term_cursor_shape(void)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003556{
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003557 static int showing_mode = NORMAL;
3558 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003559
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003560 /* Only do something when redrawing the screen and we can restore the
3561 * mode. */
3562 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003563 return;
3564
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003565 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003566 {
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003567 if (showing_mode != REPLACE)
3568 {
3569 if (*T_CSR != NUL)
3570 p = T_CSR; /* Replace mode cursor */
3571 else
3572 p = T_CSI; /* fall back to Insert mode cursor */
3573 if (*p != NUL)
3574 {
3575 out_str(p);
3576 showing_mode = REPLACE;
3577 }
3578 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003579 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003580 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003581 {
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003582 if (showing_mode != INSERT && *T_CSI != NUL)
3583 {
3584 out_str(T_CSI); /* Insert mode cursor */
3585 showing_mode = INSERT;
3586 }
3587 }
3588 else if (showing_mode != NORMAL)
3589 {
3590 out_str(T_CEI); /* non-Insert mode cursor */
3591 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003592 }
3593}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003594#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003595
3596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 * Set scrolling region for window 'wp'.
3598 * The region starts 'off' lines from the start of the window.
3599 * Also set the vertical scroll region for a vertically split window. Always
3600 * the full width of the window, excluding the vertical separator.
3601 */
3602 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003603scroll_region_set(win_T *wp, int off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
3605 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3606 W_WINROW(wp) + off));
3607#ifdef FEAT_VERTSPLIT
3608 if (*T_CSV != NUL && wp->w_width != Columns)
3609 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3610 W_WINCOL(wp)));
3611#endif
3612 screen_start(); /* don't know where cursor is now */
3613}
3614
3615/*
3616 * Reset scrolling region to the whole screen.
3617 */
3618 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003619scroll_region_reset(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
3621 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
3622#ifdef FEAT_VERTSPLIT
3623 if (*T_CSV != NUL)
3624 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3625#endif
3626 screen_start(); /* don't know where cursor is now */
3627}
3628
3629
3630/*
3631 * List of terminal codes that are currently recognized.
3632 */
3633
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003634static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635{
3636 char_u name[2]; /* termcap name of entry */
3637 char_u *code; /* terminal code (in allocated memory) */
3638 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003639 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640} *termcodes = NULL;
3641
3642static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3643static int tc_len = 0; /* current number of entries in termcodes[] */
3644
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003645static int termcode_star(char_u *code, int len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003648clear_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649{
3650 while (tc_len > 0)
3651 vim_free(termcodes[--tc_len].code);
3652 vim_free(termcodes);
3653 termcodes = NULL;
3654 tc_max_len = 0;
3655
3656#ifdef HAVE_TGETENT
3657 BC = (char *)empty_option;
3658 UP = (char *)empty_option;
3659 PC = NUL; /* set pad character to NUL */
3660 ospeed = 0;
3661#endif
3662
3663 need_gather = TRUE; /* need to fill termleader[] */
3664}
3665
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003666#define ATC_FROM_TERM 55
3667
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668/*
3669 * Add a new entry to the list of terminal codes.
3670 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003671 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3672 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 */
3674 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003675add_termcode(char_u *name, char_u *string, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
3677 struct termcode *new_tc;
3678 int i, j;
3679 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003680 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
3682 if (string == NULL || *string == NUL)
3683 {
3684 del_termcode(name);
3685 return;
3686 }
3687
Bram Moolenaar45500912014-07-09 20:51:07 +02003688#if defined(WIN3264) && !defined(FEAT_GUI)
3689 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3690#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 if (s == NULL)
3694 return;
3695
3696 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003697 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003699 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 s[0] = term_7to8bit(string);
3701 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003702
3703#if defined(WIN3264) && !defined(FEAT_GUI)
3704 if (s[0] == K_NUL)
3705 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003706 STRMOVE(s + 1, s);
3707 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003708 }
3709#endif
3710
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003711 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
3713 need_gather = TRUE; /* need to fill termleader[] */
3714
3715 /*
3716 * need to make space for more entries
3717 */
3718 if (tc_len == tc_max_len)
3719 {
3720 tc_max_len += 20;
3721 new_tc = (struct termcode *)alloc(
3722 (unsigned)(tc_max_len * sizeof(struct termcode)));
3723 if (new_tc == NULL)
3724 {
3725 tc_max_len -= 20;
3726 return;
3727 }
3728 for (i = 0; i < tc_len; ++i)
3729 new_tc[i] = termcodes[i];
3730 vim_free(termcodes);
3731 termcodes = new_tc;
3732 }
3733
3734 /*
3735 * Look for existing entry with the same name, it is replaced.
3736 * Look for an existing entry that is alphabetical higher, the new entry
3737 * is inserted in front of it.
3738 */
3739 for (i = 0; i < tc_len; ++i)
3740 {
3741 if (termcodes[i].name[0] < name[0])
3742 continue;
3743 if (termcodes[i].name[0] == name[0])
3744 {
3745 if (termcodes[i].name[1] < name[1])
3746 continue;
3747 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003748 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 */
3750 if (termcodes[i].name[1] == name[1])
3751 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003752 if (flags == ATC_FROM_TERM && (j = termcode_star(
3753 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003754 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003755 /* Don't replace ESC[123;*X or ESC O*X with another when
3756 * invoked from got_code_from_term(). */
3757 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003758 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003759 && s[len - 1]
3760 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003761 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003762 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003763 vim_free(s);
3764 return;
3765 }
3766 }
3767 else
3768 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003769 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003770 vim_free(termcodes[i].code);
3771 --tc_len;
3772 break;
3773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775 }
3776 /*
3777 * Found alphabetical larger entry, move rest to insert new entry
3778 */
3779 for (j = tc_len; j > i; --j)
3780 termcodes[j] = termcodes[j - 1];
3781 break;
3782 }
3783
3784 termcodes[i].name[0] = name[0];
3785 termcodes[i].name[1] = name[1];
3786 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003787 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003788
3789 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3790 * accept modifiers. */
3791 termcodes[i].modlen = 0;
3792 j = termcode_star(s, len);
3793 if (j > 0)
3794 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 ++tc_len;
3796}
3797
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003798/*
3799 * Check termcode "code[len]" for ending in ;*X, <Esc>O*X or <M-O>*X.
3800 * The "X" can be any character.
3801 * Return 0 if not found, 2 for ;*X and 1 for O*X and <M-O>*X.
3802 */
3803 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003804termcode_star(char_u *code, int len)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003805{
3806 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3807 if (len >= 3 && code[len - 2] == '*')
3808 {
3809 if (len >= 5 && code[len - 3] == ';')
3810 return 2;
3811 if ((len >= 4 && code[len - 3] == 'O') || code[len - 3] == 'O' + 128)
3812 return 1;
3813 }
3814 return 0;
3815}
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003818find_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819{
3820 int i;
3821
3822 for (i = 0; i < tc_len; ++i)
3823 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3824 return termcodes[i].code;
3825 return NULL;
3826}
3827
3828#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3829 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003830get_termcode(int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831{
3832 if (i >= tc_len)
3833 return NULL;
3834 return &termcodes[i].name[0];
3835}
3836#endif
3837
3838 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003839del_termcode(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840{
3841 int i;
3842
3843 if (termcodes == NULL) /* nothing there yet */
3844 return;
3845
3846 need_gather = TRUE; /* need to fill termleader[] */
3847
3848 for (i = 0; i < tc_len; ++i)
3849 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3850 {
3851 del_termcode_idx(i);
3852 return;
3853 }
3854 /* not found. Give error message? */
3855}
3856
3857 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003858del_termcode_idx(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
3860 int i;
3861
3862 vim_free(termcodes[idx].code);
3863 --tc_len;
3864 for (i = idx; i < tc_len; ++i)
3865 termcodes[i] = termcodes[i + 1];
3866}
3867
3868#ifdef FEAT_TERMRESPONSE
3869/*
3870 * Called when detected that the terminal sends 8-bit codes.
3871 * Convert all 7-bit codes to their 8-bit equivalent.
3872 */
3873 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003874switch_to_8bit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
3876 int i;
3877 int c;
3878
3879 /* Only need to do something when not already using 8-bit codes. */
3880 if (!term_is_8bit(T_NAME))
3881 {
3882 for (i = 0; i < tc_len; ++i)
3883 {
3884 c = term_7to8bit(termcodes[i].code);
3885 if (c != 0)
3886 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003887 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 termcodes[i].code[0] = c;
3889 }
3890 }
3891 need_gather = TRUE; /* need to fill termleader[] */
3892 }
3893 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003894 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895}
3896#endif
3897
3898#ifdef CHECK_DOUBLE_CLICK
3899static linenr_T orig_topline = 0;
3900# ifdef FEAT_DIFF
3901static int orig_topfill = 0;
3902# endif
3903#endif
3904#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
3905/*
3906 * Checking for double clicks ourselves.
3907 * "orig_topline" is used to avoid detecting a double-click when the window
3908 * contents scrolled (e.g., when 'scrolloff' is non-zero).
3909 */
3910/*
3911 * Set orig_topline. Used when jumping to another window, so that a double
3912 * click still works.
3913 */
3914 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003915set_mouse_topline(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916{
3917 orig_topline = wp->w_topline;
3918# ifdef FEAT_DIFF
3919 orig_topfill = wp->w_topfill;
3920# endif
3921}
3922#endif
3923
3924/*
3925 * Check if typebuf.tb_buf[] contains a terminal key code.
3926 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
3927 * + max_offset].
3928 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003929 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 * With a match, the match is removed, the replacement code is inserted in
3931 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
3932 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003933 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
3934 * "buflen" is then the length of the string in buf[] and is updated for
3935 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 */
3937 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003938check_termcode(
3939 int max_offset,
3940 char_u *buf,
3941 int bufsize,
3942 int *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943{
3944 char_u *tp;
3945 char_u *p;
3946 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003947 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003949 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 int offset;
3951 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003952 int modifiers;
3953 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 int new_slen;
3955 int extra;
3956 char_u string[MAX_KEY_CODE_LEN + 1];
3957 int i, j;
3958 int idx = 0;
3959#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00003960# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
3961 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 char_u bytes[6];
3963 int num_bytes;
3964# endif
3965 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 int is_click, is_drag;
3967 int wheel_code = 0;
3968 int current_button;
3969 static int held_button = MOUSE_RELEASE;
3970 static int orig_num_clicks = 1;
3971 static int orig_mouse_code = 0x0;
3972# ifdef CHECK_DOUBLE_CLICK
3973 static int orig_mouse_col = 0;
3974 static int orig_mouse_row = 0;
3975 static struct timeval orig_mouse_time = {0, 0};
3976 /* time of previous mouse click */
3977 struct timeval mouse_time; /* time of current mouse click */
3978 long timediff; /* elapsed time in msec */
3979# endif
3980#endif
3981 int cpo_koffset;
3982#ifdef FEAT_MOUSE_GPM
3983 extern int gpm_flag; /* gpm library variable */
3984#endif
3985
3986 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
3987
3988 /*
3989 * Speed up the checks for terminal codes by gathering all first bytes
3990 * used in termleader[]. Often this is just a single <Esc>.
3991 */
3992 if (need_gather)
3993 gather_termleader();
3994
3995 /*
3996 * Check at several positions in typebuf.tb_buf[], to catch something like
3997 * "x<Up>" that can be mapped. Stop at max_offset, because characters
3998 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01003999 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 * This is used often, KEEP IT FAST!
4001 */
4002 for (offset = 0; offset < max_offset; ++offset)
4003 {
4004 if (buf == NULL)
4005 {
4006 if (offset >= typebuf.tb_len)
4007 break;
4008 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4009 len = typebuf.tb_len - offset; /* length of the input */
4010 }
4011 else
4012 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004013 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 break;
4015 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004016 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018
4019 /*
4020 * Don't check characters after K_SPECIAL, those are already
4021 * translated terminal chars (avoid translating ~@^Hx).
4022 */
4023 if (*tp == K_SPECIAL)
4024 {
4025 offset += 2; /* there are always 2 extra characters */
4026 continue;
4027 }
4028
4029 /*
4030 * Skip this position if the character does not appear as the first
4031 * character in term_strings. This speeds up a lot, since most
4032 * termcodes start with the same character (ESC or CSI).
4033 */
4034 i = *tp;
4035 for (p = termleader; *p && *p != i; ++p)
4036 ;
4037 if (*p == NUL)
4038 continue;
4039
4040 /*
4041 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4042 * are in Insert mode.
4043 */
4044 if (*tp == ESC && !p_ek && (State & INSERT))
4045 continue;
4046
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004048 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004049 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050
4051#ifdef FEAT_GUI
4052 if (gui.in_use)
4053 {
4054 /*
4055 * GUI special key codes are all of the form [CSI xx].
4056 */
4057 if (*tp == CSI) /* Special key from GUI */
4058 {
4059 if (len < 3)
4060 return -1; /* Shouldn't happen */
4061 slen = 3;
4062 key_name[0] = tp[1];
4063 key_name[1] = tp[2];
4064 }
4065 }
4066 else
4067#endif /* FEAT_GUI */
4068 {
4069 for (idx = 0; idx < tc_len; ++idx)
4070 {
4071 /*
4072 * Ignore the entry if we are not at the start of
4073 * typebuf.tb_buf[]
4074 * and there are not enough characters to make a match.
4075 * But only when the 'K' flag is in 'cpoptions'.
4076 */
4077 slen = termcodes[idx].len;
4078 if (cpo_koffset && offset && len < slen)
4079 continue;
4080 if (STRNCMP(termcodes[idx].code, tp,
4081 (size_t)(slen > len ? len : slen)) == 0)
4082 {
4083 if (len < slen) /* got a partial sequence */
4084 return -1; /* need to get more chars */
4085
4086 /*
4087 * When found a keypad key, check if there is another key
4088 * that matches and use that one. This makes <Home> to be
4089 * found instead of <kHome> when they produce the same
4090 * key code.
4091 */
4092 if (termcodes[idx].name[0] == 'K'
4093 && VIM_ISDIGIT(termcodes[idx].name[1]))
4094 {
4095 for (j = idx + 1; j < tc_len; ++j)
4096 if (termcodes[j].len == slen &&
4097 STRNCMP(termcodes[idx].code,
4098 termcodes[j].code, slen) == 0)
4099 {
4100 idx = j;
4101 break;
4102 }
4103 }
4104
4105 key_name[0] = termcodes[idx].name[0];
4106 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 break;
4108 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004109
4110 /*
4111 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004112 * <Esc>[123;*X (modslen == slen - 3)
4113 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4114 * When there is a modifier the * matches a number.
4115 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004116 */
4117 if (termcodes[idx].modlen > 0)
4118 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004119 modslen = termcodes[idx].modlen;
4120 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004121 continue;
4122 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004123 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004124 {
4125 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004126
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004127 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004128 return -1; /* need to get more chars */
4129
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004130 if (tp[modslen] == termcodes[idx].code[slen - 1])
4131 slen = modslen + 1; /* no modifiers */
4132 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004133 continue; /* no match */
4134 else
4135 {
4136 /* Skip over the digits, the final char must
4137 * follow. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004138 for (j = slen - 2; j < len && isdigit(tp[j]); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004139 ;
4140 ++j;
4141 if (len < j) /* got a partial sequence */
4142 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004143 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4144 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004145
4146 /* Match! Convert modifier bits. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004147 n = atoi((char *)tp + slen - 2) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004148 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004149 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004150 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004151 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004152 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004153 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004154 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004155 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004156
4157 slen = j;
4158 }
4159 key_name[0] = termcodes[idx].name[0];
4160 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004161 break;
4162 }
4163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
4165 }
4166
4167#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004168 if (key_name[0] == NUL
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004169 /* Mouse codes of DEC, pterm, and URXVT start with <ESC>[. When
4170 * detecting the start of these mouse codes they might as well be
4171 * another key code or terminal response. */
4172# ifdef FEAT_MOUSE_DEC
4173 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004174# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004175# ifdef FEAT_MOUSE_PTERM
4176 || key_name[0] == KS_PTERM_MOUSE
4177# endif
4178# ifdef FEAT_MOUSE_URXVT
4179 || key_name[0] == KS_URXVT_MOUSE
4180# endif
4181 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004183 /* Check for some responses from the terminal starting with
4184 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004185 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004186 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01004187 * Also eat other possible responses to t_RV, rxvt returns
4188 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4189 * mrxvt has been reported to have "+" in the version. Assume
4190 * the escape sequence ends with a letter or one of "{|}~".
4191 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004192 * - Cursor position report: <Esc>[{row};{col}R
4193 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004194 * ambiguous-width character state.
4195 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004196 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004197
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004198 if ((*T_CRV != NUL || *T_U7 != NUL)
4199 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004200 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004201 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004203#ifdef FEAT_MBYTE
4204 int col;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004205 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 j = 0;
4208 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004209 for (i = 2 + (tp[0] != CSI); i < len
4210 && !(tp[i] >= '{' && tp[i] <= '~')
4211 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 if (tp[i] == ';' && ++j == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004213 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004214 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004215#ifdef FEAT_MBYTE
4216 row_char = tp[i - 1];
4217#endif
4218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004220 {
4221 LOG_TR("Not enough characters for CRV");
4222 return -1;
4223 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004224#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004225 if (extra > 0)
4226 col = atoi((char *)tp + extra);
4227 else
4228 col = 0;
4229
4230 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4231 * u7_status is not "sent", it may be from a previous Vim that
4232 * just exited. But not for <S-F3>, it sends something
4233 * similar, check for row and column to make sense. */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004234 if (j == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004235 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004236 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004237 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004238 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004239
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004240 LOG_TR("Received U7 status");
4241 u7_status = U7_GOT;
4242# ifdef FEAT_AUTOCMD
4243 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004244# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004245 if (col == 2)
4246 aw = "single";
4247 else if (col == 3)
4248 aw = "double";
4249 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4250 {
4251 /* Setting the option causes a screen redraw. Do
4252 * that right away if possible, keeping any
4253 * messages. */
4254 set_option_value((char_u *)"ambw", 0L,
4255 (char_u *)aw, 0);
4256# ifdef DEBUG_TERMRESPONSE
4257 {
4258 char buf[100];
4259 int r = redraw_asap(CLEAR);
4260
4261 sprintf(buf,
4262 "set 'ambiwidth', redraw_asap(): %d",
4263 r);
4264 log_tr(buf);
4265 }
4266# else
4267 redraw_asap(CLEAR);
4268# endif
4269 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004270 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004271 key_name[0] = (int)KS_EXTRA;
4272 key_name[1] = (int)KE_IGNORE;
4273 slen = i + 1;
4274 }
4275 else
4276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004278 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004280 LOG_TR("Received CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 crv_status = CRV_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004282# ifdef FEAT_AUTOCMD
4283 did_cursorhold = TRUE;
4284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285
4286 /* If this code starts with CSI, you can bet that the
4287 * terminal uses 8-bit codes. */
4288 if (tp[0] == CSI)
4289 switch_to_8bit();
4290
4291 /* rxvt sends its version number: "20703" is 2.7.3.
4292 * Ignore it for when the user has set 'term' to xterm,
4293 * even though it's an rxvt. */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004294 if (extra > 0)
4295 extra = atoi((char *)tp + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 if (extra > 20000)
4297 extra = 0;
4298
4299 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
4300 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004301 /* Only set 'ttymouse' automatically if it was not set
4302 * by the user already. */
4303 if (!option_was_set((char_u *)"ttym"))
4304 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004305# ifdef TTYM_SGR
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004306 if (extra >= 277)
4307 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004308 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004309 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004310# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004311 /* if xterm version >= 95 use mouse dragging */
4312 if (extra >= 95)
4313 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004315 }
4316
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 /* if xterm version >= 141 try to get termcap codes */
4318 if (extra >= 141)
4319 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004320 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 check_for_codes = TRUE;
4322 need_gather = TRUE;
4323 req_codes_from_term();
4324 }
4325 }
4326# ifdef FEAT_EVAL
4327 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4328# endif
4329# ifdef FEAT_AUTOCMD
4330 apply_autocmds(EVENT_TERMRESPONSE,
4331 NULL, NULL, FALSE, curbuf);
4332# endif
4333 key_name[0] = (int)KS_EXTRA;
4334 key_name[1] = (int)KE_IGNORE;
4335 slen = i + 1;
4336 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004337 }
4338
4339 /* Check for background color response from the terminal:
4340 *
4341 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4342 *
4343 * {lead} can be <Esc>] or OSC
4344 * {tail} can be '\007', <Esc>\ or STERM.
4345 *
4346 * Consume any code that starts with "{lead}11;", it's also
4347 * possible that "rgba" is following.
4348 */
4349 else if (*T_RBG != NUL
4350 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4351 || tp[0] == OSC))
4352 {
4353 j = 1 + (tp[0] == ESC);
4354 if (len >= j + 3 && (argp[0] != '1'
4355 || argp[1] != '1' || argp[2] != ';'))
4356 i = 0; /* no match */
4357 else
4358 for (i = j; i < len; ++i)
4359 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4360 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004361 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004362 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4363 && tp[j + 11] == '/' && tp[j + 16] == '/'
4364 && !option_was_set((char_u *)"bg"))
4365 {/* TODO: don't set option when already the right value */
4366 LOG_TR("Received RBG");
4367 rbg_status = RBG_GOT;
4368 set_option_value((char_u *)"bg", 0L, (char_u *)(
4369 (3 * '6' < tp[j+7] + tp[j+12] + tp[j+17])
4370 ? "light" : "dark"), 0);
4371 reset_option_was_set((char_u *)"bg");
4372 redraw_asap(CLEAR);
4373 }
4374
4375 /* got finished code: consume it */
4376 key_name[0] = (int)KS_EXTRA;
4377 key_name[1] = (int)KE_IGNORE;
4378 slen = i + 1 + (tp[i] == ESC);
4379 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004380 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004381 if (i == len)
4382 {
4383 LOG_TR("not enough characters for RB");
4384 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 }
4387
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004388 /* Check for key code response from xterm:
4389 *
4390 * {lead}{flag}+r<hex bytes><{tail}
4391 *
4392 * {lead} can be <Esc>P or DCS
4393 * {flag} can be '0' or '1'
4394 * {tail} can be Esc>\ or STERM
4395 *
4396 * Consume any code that starts with "{lead}.+r".
4397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 else if (check_for_codes
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004399 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 || tp[0] == DCS))
4401 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004402 j = 1 + (tp[0] == ESC);
4403 if (len >= j + 3 && (argp[1] != '+' || argp[2] != 'r'))
4404 i = 0; /* no match */
4405 else
4406 for (i = j; i < len; ++i)
4407 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 || tp[i] == STERM)
4409 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004410 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 got_code_from_term(tp + j, i);
4412 key_name[0] = (int)KS_EXTRA;
4413 key_name[1] = (int)KE_IGNORE;
4414 slen = i + 1 + (tp[i] == ESC);
4415 break;
4416 }
4417
4418 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004419 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004420 /* These codes arrive many together, each code can be
4421 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004422 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004423 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
4426 }
4427#endif
4428
4429 if (key_name[0] == NUL)
4430 continue; /* No match at this position, try next one */
4431
4432 /* We only get here when we have a complete termcode match */
4433
4434#ifdef FEAT_MOUSE
4435# ifdef FEAT_GUI
4436 /*
4437 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4438 * so that we know which window to scroll later.
4439 */
4440 if (gui.in_use
4441 && key_name[0] == (int)KS_EXTRA
4442 && (key_name[1] == (int)KE_X1MOUSE
4443 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004444 || key_name[1] == (int)KE_MOUSELEFT
4445 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 || key_name[1] == (int)KE_MOUSEDOWN
4447 || key_name[1] == (int)KE_MOUSEUP))
4448 {
4449 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4450 if (num_bytes == -1) /* not enough coordinates */
4451 return -1;
4452 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4453 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4454 slen += num_bytes;
4455 }
4456 else
4457# endif
4458 /*
4459 * If it is a mouse click, get the coordinates.
4460 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004461 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004463 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464# endif
4465# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004466 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467# endif
4468# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004469 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470# endif
4471# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004472 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004474# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004475 || key_name[0] == KS_URXVT_MOUSE
4476# endif
4477# ifdef FEAT_MOUSE_SGR
4478 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 )
4481 {
4482 is_click = is_drag = FALSE;
4483
Bram Moolenaar864207d2008-06-24 22:14:38 +00004484# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4485 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 if (key_name[0] == (int)KS_MOUSE)
4487 {
4488 /*
4489 * For xterm and MSDOS we get "<t_mouse>scr", where
4490 * s == encoded button state:
4491 * 0x20 = left button down
4492 * 0x21 = middle button down
4493 * 0x22 = right button down
4494 * 0x23 = any button release
4495 * 0x60 = button 4 down (scroll wheel down)
4496 * 0x61 = button 5 down (scroll wheel up)
4497 * add 0x04 for SHIFT
4498 * add 0x08 for ALT
4499 * add 0x10 for CTRL
4500 * add 0x20 for mouse drag (0x40 is drag with left button)
4501 * c == column + ' ' + 1 == column + 33
4502 * r == row + ' ' + 1 == row + 33
4503 *
4504 * The coordinates are passed on through global variables.
4505 * Ugly, but this avoids trouble with mouse clicks at an
4506 * unexpected moment and allows for mapping them.
4507 */
4508 for (;;)
4509 {
4510#ifdef FEAT_GUI
4511 if (gui.in_use)
4512 {
4513 /* GUI uses more bits for columns > 223 */
4514 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4515 if (num_bytes == -1) /* not enough coordinates */
4516 return -1;
4517 mouse_code = bytes[0];
4518 mouse_col = 128 * (bytes[1] - ' ' - 1)
4519 + bytes[2] - ' ' - 1;
4520 mouse_row = 128 * (bytes[3] - ' ' - 1)
4521 + bytes[4] - ' ' - 1;
4522 }
4523 else
4524#endif
4525 {
4526 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4527 if (num_bytes == -1) /* not enough coordinates */
4528 return -1;
4529 mouse_code = bytes[0];
4530 mouse_col = bytes[1] - ' ' - 1;
4531 mouse_row = bytes[2] - ' ' - 1;
4532 }
4533 slen += num_bytes;
4534
4535 /* If the following bytes is also a mouse code and it has
4536 * the same code, dump this one and get the next. This
4537 * makes dragging a whole lot faster. */
4538#ifdef FEAT_GUI
4539 if (gui.in_use)
4540 j = 3;
4541 else
4542#endif
4543 j = termcodes[idx].len;
4544 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4545 && tp[slen + j] == mouse_code
4546 && tp[slen + j + 1] != NUL
4547 && tp[slen + j + 2] != NUL
4548#ifdef FEAT_GUI
4549 && (!gui.in_use
4550 || (tp[slen + j + 3] != NUL
4551 && tp[slen + j + 4] != NUL))
4552#endif
4553 )
4554 slen += j;
4555 else
4556 break;
4557 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004560# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4561 if (key_name[0] == KS_URXVT_MOUSE
4562 || key_name[0] == KS_SGR_MOUSE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004563 {
4564 for (;;)
4565 {
4566 /* URXVT 1015 mouse reporting mode:
4567 * Almost identical to xterm mouse mode, except the values
4568 * are decimal instead of bytes.
4569 *
4570 * \033[%d;%d;%dM
4571 * ^-- row
4572 * ^----- column
4573 * ^-------- code
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004574 *
4575 * SGR 1006 mouse reporting mode:
4576 * Almost identical to xterm mouse mode, except the values
4577 * are decimal instead of bytes.
4578 *
4579 * \033[<%d;%d;%dM
4580 * ^-- row
4581 * ^----- column
4582 * ^-------- code
4583 *
4584 * \033[<%d;%d;%dm : mouse release event
4585 * ^-- row
4586 * ^----- column
4587 * ^-------- code
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004588 */
4589 p = tp + slen;
4590
4591 mouse_code = getdigits(&p);
4592 if (*p++ != ';')
4593 return -1;
4594
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004595 /* when mouse reporting is SGR, add 32 to mouse code */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004596 if (key_name[0] == KS_SGR_MOUSE)
4597 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004598
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004599 mouse_col = getdigits(&p) - 1;
4600 if (*p++ != ';')
4601 return -1;
4602
4603 mouse_row = getdigits(&p) - 1;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004604 if (key_name[0] == KS_SGR_MOUSE && *p == 'm')
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004605 mouse_code |= MOUSE_RELEASE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004606 else if (*p != 'M')
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004607 return -1;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004608 p++;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004609
4610 slen += (int)(p - (tp + slen));
4611
4612 /* skip this one if next one has same code (like xterm
4613 * case) */
4614 j = termcodes[idx].len;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004615 if (STRNCMP(tp, tp + slen, (size_t)j) == 0)
4616 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004617 int slen2;
4618 int cmd_complete = 0;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004619
4620 /* check if the command is complete by looking for the
4621 * 'M' */
4622 for (slen2 = slen; slen2 < len; slen2++)
4623 {
4624 if (tp[slen2] == 'M'
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004625 || (key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004626 && tp[slen2] == 'm'))
4627 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004628 cmd_complete = 1;
4629 break;
4630 }
4631 }
4632 p += j;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004633 if (cmd_complete && getdigits(&p) == mouse_code)
4634 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004635 slen += j; /* skip the \033[ */
4636 continue;
4637 }
4638 }
4639 break;
4640 }
4641 }
4642# endif
4643
4644 if (key_name[0] == (int)KS_MOUSE
4645#ifdef FEAT_MOUSE_URXVT
4646 || key_name[0] == (int)KS_URXVT_MOUSE
4647#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004648#ifdef FEAT_MOUSE_SGR
4649 || key_name[0] == KS_SGR_MOUSE
4650#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004651 )
4652 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653# if !defined(MSWIN) && !defined(MSDOS)
4654 /*
4655 * Handle mouse events.
4656 * Recognize the xterm mouse wheel, but not in the GUI, the
4657 * Linux console with GPM and the MS-DOS or Win32 console
4658 * (multi-clicks use >= 0x60).
4659 */
4660 if (mouse_code >= MOUSEWHEEL_LOW
4661# ifdef FEAT_GUI
4662 && !gui.in_use
4663# endif
4664# ifdef FEAT_MOUSE_GPM
4665 && gpm_flag == 0
4666# endif
4667 )
4668 {
4669 /* Keep the mouse_code before it's changed, so that we
4670 * remember that it was a mouse wheel click. */
4671 wheel_code = mouse_code;
4672 }
4673# ifdef FEAT_MOUSE_XTERM
4674 else if (held_button == MOUSE_RELEASE
4675# ifdef FEAT_GUI
4676 && !gui.in_use
4677# endif
4678 && (mouse_code == 0x23 || mouse_code == 0x24))
4679 {
4680 /* Apparently used by rxvt scroll wheel. */
4681 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4682 }
4683# endif
4684
4685# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4686 else if (use_xterm_mouse() > 1)
4687 {
4688 if (mouse_code & MOUSE_DRAG_XTERM)
4689 mouse_code |= MOUSE_DRAG;
4690 }
4691# endif
4692# ifdef FEAT_XCLIPBOARD
4693 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4694 {
4695 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4696 stop_xterm_trace();
4697 else
4698 start_xterm_trace(mouse_code);
4699 }
4700# endif
4701# endif
4702 }
4703# endif /* !UNIX || FEAT_MOUSE_XTERM */
4704# ifdef FEAT_MOUSE_NET
4705 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4706 {
4707 int mc, mr;
4708
4709 /* expect a rather limited sequence like: balancing {
4710 * \033}6,45\r
4711 * '6' is the row, 45 is the column
4712 */
4713 p = tp + slen;
4714 mr = getdigits(&p);
4715 if (*p++ != ',')
4716 return -1;
4717 mc = getdigits(&p);
4718 if (*p++ != '\r')
4719 return -1;
4720
4721 mouse_col = mc - 1;
4722 mouse_row = mr - 1;
4723 mouse_code = MOUSE_LEFT;
4724 slen += (int)(p - (tp + slen));
4725 }
4726# endif /* FEAT_MOUSE_NET */
4727# ifdef FEAT_MOUSE_JSB
4728 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4729 {
4730 int mult, val, iter, button, status;
4731
4732 /* JSBTERM Input Model
4733 * \033[0~zw uniq escape sequence
4734 * (L-x) Left button pressed - not pressed x not reporting
4735 * (M-x) Middle button pressed - not pressed x not reporting
4736 * (R-x) Right button pressed - not pressed x not reporting
4737 * (SDmdu) Single , Double click, m mouse move d button down
4738 * u button up
4739 * ### X cursor position padded to 3 digits
4740 * ### Y cursor position padded to 3 digits
4741 * (s-x) SHIFT key pressed - not pressed x not reporting
4742 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004743 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 */
4745
4746 p = tp + slen;
4747 button = mouse_code = 0;
4748 switch (*p++)
4749 {
4750 case 'L': button = 1; break;
4751 case '-': break;
4752 case 'x': break; /* ignore sequence */
4753 default: return -1; /* Unknown Result */
4754 }
4755 switch (*p++)
4756 {
4757 case 'M': button |= 2; break;
4758 case '-': break;
4759 case 'x': break; /* ignore sequence */
4760 default: return -1; /* Unknown Result */
4761 }
4762 switch (*p++)
4763 {
4764 case 'R': button |= 4; break;
4765 case '-': break;
4766 case 'x': break; /* ignore sequence */
4767 default: return -1; /* Unknown Result */
4768 }
4769 status = *p++;
4770 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4771 mult /= 10, p++)
4772 if (*p >= '0' && *p <= '9')
4773 val += (*p - '0') * mult;
4774 else
4775 return -1;
4776 mouse_col = val;
4777 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4778 mult /= 10, p++)
4779 if (*p >= '0' && *p <= '9')
4780 val += (*p - '0') * mult;
4781 else
4782 return -1;
4783 mouse_row = val;
4784 switch (*p++)
4785 {
4786 case 's': button |= 8; break; /* SHIFT key Pressed */
4787 case '-': break; /* Not Pressed */
4788 case 'x': break; /* Not Reporting */
4789 default: return -1; /* Unknown Result */
4790 }
4791 switch (*p++)
4792 {
4793 case 'c': button |= 16; break; /* CTRL key Pressed */
4794 case '-': break; /* Not Pressed */
4795 case 'x': break; /* Not Reporting */
4796 default: return -1; /* Unknown Result */
4797 }
4798 if (*p++ != '\033')
4799 return -1;
4800 if (*p++ != '\\')
4801 return -1;
4802 switch (status)
4803 {
4804 case 'D': /* Double Click */
4805 case 'S': /* Single Click */
4806 if (button & 1) mouse_code |= MOUSE_LEFT;
4807 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4808 if (button & 4) mouse_code |= MOUSE_RIGHT;
4809 if (button & 8) mouse_code |= MOUSE_SHIFT;
4810 if (button & 16) mouse_code |= MOUSE_CTRL;
4811 break;
4812 case 'm': /* Mouse move */
4813 if (button & 1) mouse_code |= MOUSE_LEFT;
4814 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4815 if (button & 4) mouse_code |= MOUSE_RIGHT;
4816 if (button & 8) mouse_code |= MOUSE_SHIFT;
4817 if (button & 16) mouse_code |= MOUSE_CTRL;
4818 if ((button & 7) != 0)
4819 {
4820 held_button = mouse_code;
4821 mouse_code |= MOUSE_DRAG;
4822 }
4823 is_drag = TRUE;
4824 showmode();
4825 break;
4826 case 'd': /* Button Down */
4827 if (button & 1) mouse_code |= MOUSE_LEFT;
4828 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4829 if (button & 4) mouse_code |= MOUSE_RIGHT;
4830 if (button & 8) mouse_code |= MOUSE_SHIFT;
4831 if (button & 16) mouse_code |= MOUSE_CTRL;
4832 break;
4833 case 'u': /* Button Up */
4834 if (button & 1)
4835 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4836 if (button & 2)
4837 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4838 if (button & 4)
4839 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4840 if (button & 8)
4841 mouse_code |= MOUSE_SHIFT;
4842 if (button & 16)
4843 mouse_code |= MOUSE_CTRL;
4844 break;
4845 default: return -1; /* Unknown Result */
4846 }
4847
4848 slen += (p - (tp + slen));
4849 }
4850# endif /* FEAT_MOUSE_JSB */
4851# ifdef FEAT_MOUSE_DEC
4852 if (key_name[0] == (int)KS_DEC_MOUSE)
4853 {
4854 /* The DEC Locator Input Model
4855 * Netterm delivers the code sequence:
4856 * \033[2;4;24;80&w (left button down)
4857 * \033[3;0;24;80&w (left button up)
4858 * \033[6;1;24;80&w (right button down)
4859 * \033[7;0;24;80&w (right button up)
4860 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4861 * Pe is the event code
4862 * Pb is the button code
4863 * Pr is the row coordinate
4864 * Pc is the column coordinate
4865 * Pp is the third coordinate (page number)
4866 * Pe, the event code indicates what event caused this report
4867 * The following event codes are defined:
4868 * 0 - request, the terminal received an explicit request
4869 * for a locator report, but the locator is unavailable
4870 * 1 - request, the terminal received an explicit request
4871 * for a locator report
4872 * 2 - left button down
4873 * 3 - left button up
4874 * 4 - middle button down
4875 * 5 - middle button up
4876 * 6 - right button down
4877 * 7 - right button up
4878 * 8 - fourth button down
4879 * 9 - fourth button up
4880 * 10 - locator outside filter rectangle
4881 * Pb, the button code, ASCII decimal 0-15 indicating which
4882 * buttons are down if any. The state of the four buttons
4883 * on the locator correspond to the low four bits of the
4884 * decimal value,
4885 * "1" means button depressed
4886 * 0 - no buttons down,
4887 * 1 - right,
4888 * 2 - middle,
4889 * 4 - left,
4890 * 8 - fourth
4891 * Pr is the row coordinate of the locator position in the page,
4892 * encoded as an ASCII decimal value.
4893 * If Pr is omitted, the locator position is undefined
4894 * (outside the terminal window for example).
4895 * Pc is the column coordinate of the locator position in the
4896 * page, encoded as an ASCII decimal value.
4897 * If Pc is omitted, the locator position is undefined
4898 * (outside the terminal window for example).
4899 * Pp is the page coordinate of the locator position
4900 * encoded as an ASCII decimal value.
4901 * The page coordinate may be omitted if the locator is on
4902 * page one (the default). We ignore it anyway.
4903 */
4904 int Pe, Pb, Pr, Pc;
4905
4906 p = tp + slen;
4907
4908 /* get event status */
4909 Pe = getdigits(&p);
4910 if (*p++ != ';')
4911 return -1;
4912
4913 /* get button status */
4914 Pb = getdigits(&p);
4915 if (*p++ != ';')
4916 return -1;
4917
4918 /* get row status */
4919 Pr = getdigits(&p);
4920 if (*p++ != ';')
4921 return -1;
4922
4923 /* get column status */
4924 Pc = getdigits(&p);
4925
4926 /* the page parameter is optional */
4927 if (*p == ';')
4928 {
4929 p++;
4930 (void)getdigits(&p);
4931 }
4932 if (*p++ != '&')
4933 return -1;
4934 if (*p++ != 'w')
4935 return -1;
4936
4937 mouse_code = 0;
4938 switch (Pe)
4939 {
4940 case 0: return -1; /* position request while unavailable */
4941 case 1: /* a response to a locator position request includes
4942 the status of all buttons */
4943 Pb &= 7; /* mask off and ignore fourth button */
4944 if (Pb & 4)
4945 mouse_code = MOUSE_LEFT;
4946 if (Pb & 2)
4947 mouse_code = MOUSE_MIDDLE;
4948 if (Pb & 1)
4949 mouse_code = MOUSE_RIGHT;
4950 if (Pb)
4951 {
4952 held_button = mouse_code;
4953 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004954 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956 is_drag = TRUE;
4957 showmode();
4958 break;
4959 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004960 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 break;
4962 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
4963 break;
4964 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004965 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 break;
4967 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
4968 break;
4969 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004970 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 break;
4972 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
4973 break;
4974 case 8: return -1; /* fourth button down */
4975 case 9: return -1; /* fourth button up */
4976 case 10: return -1; /* mouse outside of filter rectangle */
4977 default: return -1; /* should never occur */
4978 }
4979
4980 mouse_col = Pc - 1;
4981 mouse_row = Pr - 1;
4982
4983 slen += (int)(p - (tp + slen));
4984 }
4985# endif /* FEAT_MOUSE_DEC */
4986# ifdef FEAT_MOUSE_PTERM
4987 if (key_name[0] == (int)KS_PTERM_MOUSE)
4988 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004989 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990
4991 p = tp + slen;
4992
4993 action = getdigits(&p);
4994 if (*p++ != ';')
4995 return -1;
4996
4997 mouse_row = getdigits(&p);
4998 if (*p++ != ';')
4999 return -1;
5000 mouse_col = getdigits(&p);
5001 if (*p++ != ';')
5002 return -1;
5003
5004 button = getdigits(&p);
5005 mouse_code = 0;
5006
5007 switch( button )
5008 {
5009 case 4: mouse_code = MOUSE_LEFT; break;
5010 case 1: mouse_code = MOUSE_RIGHT; break;
5011 case 2: mouse_code = MOUSE_MIDDLE; break;
5012 default: return -1;
5013 }
5014
5015 switch( action )
5016 {
5017 case 31: /* Initial press */
5018 if (*p++ != ';')
5019 return -1;
5020
5021 num_clicks = getdigits(&p); /* Not used */
5022 break;
5023
5024 case 32: /* Release */
5025 mouse_code |= MOUSE_RELEASE;
5026 break;
5027
5028 case 33: /* Drag */
5029 held_button = mouse_code;
5030 mouse_code |= MOUSE_DRAG;
5031 break;
5032
5033 default:
5034 return -1;
5035 }
5036
5037 if (*p++ != 't')
5038 return -1;
5039
5040 slen += (p - (tp + slen));
5041 }
5042# endif /* FEAT_MOUSE_PTERM */
5043
5044 /* Interpret the mouse code */
5045 current_button = (mouse_code & MOUSE_CLICK_MASK);
5046 if (current_button == MOUSE_RELEASE
5047# ifdef FEAT_MOUSE_XTERM
5048 && wheel_code == 0
5049# endif
5050 )
5051 {
5052 /*
5053 * If we get a mouse drag or release event when
5054 * there is no mouse button held down (held_button ==
5055 * MOUSE_RELEASE), produce a K_IGNORE below.
5056 * (can happen when you hold down two buttons
5057 * and then let them go, or click in the menu bar, but not
5058 * on a menu, and drag into the text).
5059 */
5060 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5061 is_drag = TRUE;
5062 current_button = held_button;
5063 }
5064 else if (wheel_code == 0)
5065 {
5066# ifdef CHECK_DOUBLE_CLICK
5067# ifdef FEAT_MOUSE_GPM
5068# ifdef FEAT_GUI
5069 /*
5070 * Only for Unix, when GUI or gpm is not active, we handle
5071 * multi-clicks here.
5072 */
5073 if (gpm_flag == 0 && !gui.in_use)
5074# else
5075 if (gpm_flag == 0)
5076# endif
5077# else
5078# ifdef FEAT_GUI
5079 if (!gui.in_use)
5080# endif
5081# endif
5082 {
5083 /*
5084 * Compute the time elapsed since the previous mouse click.
5085 */
5086 gettimeofday(&mouse_time, NULL);
5087 timediff = (mouse_time.tv_usec
5088 - orig_mouse_time.tv_usec) / 1000;
5089 if (timediff < 0)
5090 --orig_mouse_time.tv_sec;
5091 timediff += (mouse_time.tv_sec
5092 - orig_mouse_time.tv_sec) * 1000;
5093 orig_mouse_time = mouse_time;
5094 if (mouse_code == orig_mouse_code
5095 && timediff < p_mouset
5096 && orig_num_clicks != 4
5097 && orig_mouse_col == mouse_col
5098 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005099 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005101 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005103 )
5104#ifdef FEAT_WINDOWS
5105 /* Double click in tab pages line also works
5106 * when window contents changes. */
5107 || (mouse_row == 0 && firstwin->w_winrow > 0)
5108#endif
5109 )
5110 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 ++orig_num_clicks;
5112 else
5113 orig_num_clicks = 1;
5114 orig_mouse_col = mouse_col;
5115 orig_mouse_row = mouse_row;
5116 orig_topline = curwin->w_topline;
5117#ifdef FEAT_DIFF
5118 orig_topfill = curwin->w_topfill;
5119#endif
5120 }
5121# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5122 else
5123 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5124# endif
5125# else
5126 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5127# endif
5128 is_click = TRUE;
5129 orig_mouse_code = mouse_code;
5130 }
5131 if (!is_drag)
5132 held_button = mouse_code & MOUSE_CLICK_MASK;
5133
5134 /*
5135 * Translate the actual mouse event into a pseudo mouse event.
5136 * First work out what modifiers are to be used.
5137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if (orig_mouse_code & MOUSE_SHIFT)
5139 modifiers |= MOD_MASK_SHIFT;
5140 if (orig_mouse_code & MOUSE_CTRL)
5141 modifiers |= MOD_MASK_CTRL;
5142 if (orig_mouse_code & MOUSE_ALT)
5143 modifiers |= MOD_MASK_ALT;
5144 if (orig_num_clicks == 2)
5145 modifiers |= MOD_MASK_2CLICK;
5146 else if (orig_num_clicks == 3)
5147 modifiers |= MOD_MASK_3CLICK;
5148 else if (orig_num_clicks == 4)
5149 modifiers |= MOD_MASK_4CLICK;
5150
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 /* Work out our pseudo mouse event */
5152 key_name[0] = (int)KS_EXTRA;
5153 if (wheel_code != 0)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005154 {
5155 if (wheel_code & MOUSE_CTRL)
5156 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005157 if (wheel_code & MOUSE_ALT)
5158 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 key_name[1] = (wheel_code & 1)
5160 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 else
5163 key_name[1] = get_pseudo_mouse_code(current_button,
5164 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005165
5166 /* Make sure the mouse position is valid. Some terminals may
5167 * return weird values. */
5168 if (mouse_col >= Columns)
5169 mouse_col = Columns - 1;
5170 if (mouse_row >= Rows)
5171 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173#endif /* FEAT_MOUSE */
5174
5175#ifdef FEAT_GUI
5176 /*
5177 * If using the GUI, then we get menu and scrollbar events.
5178 *
5179 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5180 * four bytes which are to be taken as a pointer to the vimmenu_T
5181 * structure.
5182 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005183 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005184 * is one byte with the tab index.
5185 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5187 * by one byte representing the scrollbar number, and then four bytes
5188 * representing a long_u which is the new value of the scrollbar.
5189 *
5190 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5191 * KE_FILLER followed by four bytes representing a long_u which is the
5192 * new value of the scrollbar.
5193 */
5194# ifdef FEAT_MENU
5195 else if (key_name[0] == (int)KS_MENU)
5196 {
5197 long_u val;
5198
5199 num_bytes = get_long_from_buf(tp + slen, &val);
5200 if (num_bytes == -1)
5201 return -1;
5202 current_menu = (vimmenu_T *)val;
5203 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005204
5205 /* The menu may have been deleted right after it was used, check
5206 * for that. */
5207 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5208 {
5209 key_name[0] = KS_EXTRA;
5210 key_name[1] = (int)KE_IGNORE;
5211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005214# ifdef FEAT_GUI_TABLINE
5215 else if (key_name[0] == (int)KS_TABLINE)
5216 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005217 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005218 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5219 if (num_bytes == -1)
5220 return -1;
5221 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005222 if (current_tab == 255) /* -1 in a byte gives 255 */
5223 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005224 slen += num_bytes;
5225 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005226 else if (key_name[0] == (int)KS_TABMENU)
5227 {
5228 /* Selecting tabline tab or using its menu. */
5229 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5230 if (num_bytes == -1)
5231 return -1;
5232 current_tab = (int)bytes[0];
5233 current_tabmenu = (int)bytes[1];
5234 slen += num_bytes;
5235 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237# ifndef USE_ON_FLY_SCROLL
5238 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5239 {
5240 long_u val;
5241
5242 /* Get the last scrollbar event in the queue of the same type */
5243 j = 0;
5244 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5245 && tp[j + 2] != NUL; ++i)
5246 {
5247 j += 3;
5248 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5249 if (num_bytes == -1)
5250 break;
5251 if (i == 0)
5252 current_scrollbar = (int)bytes[0];
5253 else if (current_scrollbar != (int)bytes[0])
5254 break;
5255 j += num_bytes;
5256 num_bytes = get_long_from_buf(tp + j, &val);
5257 if (num_bytes == -1)
5258 break;
5259 scrollbar_value = val;
5260 j += num_bytes;
5261 slen = j;
5262 }
5263 if (i == 0) /* not enough characters to make one */
5264 return -1;
5265 }
5266 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5267 {
5268 long_u val;
5269
5270 /* Get the last horiz. scrollbar event in the queue */
5271 j = 0;
5272 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5273 && tp[j + 2] != NUL; ++i)
5274 {
5275 j += 3;
5276 num_bytes = get_long_from_buf(tp + j, &val);
5277 if (num_bytes == -1)
5278 break;
5279 scrollbar_value = val;
5280 j += num_bytes;
5281 slen = j;
5282 }
5283 if (i == 0) /* not enough characters to make one */
5284 return -1;
5285 }
5286# endif /* !USE_ON_FLY_SCROLL */
5287#endif /* FEAT_GUI */
5288
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005289 /*
5290 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5291 */
5292 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5293
5294 /*
5295 * Add any modifier codes to our string.
5296 */
5297 new_slen = 0; /* Length of what will replace the termcode */
5298 if (modifiers != 0)
5299 {
5300 /* Some keys have the modifier included. Need to handle that here
5301 * to make mappings work. */
5302 key = simplify_key(key, &modifiers);
5303 if (modifiers != 0)
5304 {
5305 string[new_slen++] = K_SPECIAL;
5306 string[new_slen++] = (int)KS_MODIFIER;
5307 string[new_slen++] = modifiers;
5308 }
5309 }
5310
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005312 key_name[0] = KEY2TERMCAP0(key);
5313 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005315 {
5316 /* from ":set <M-b>=xx" */
5317#ifdef FEAT_MBYTE
5318 if (has_mbyte)
5319 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5320 else
5321#endif
5322 string[new_slen++] = key_name[1];
5323 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005324 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5325 && key_name[1] == KE_IGNORE)
5326 {
5327 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5328 * to indicate what happened. */
5329 retval = KEYLEN_REMOVED;
5330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 else
5332 {
5333 string[new_slen++] = K_SPECIAL;
5334 string[new_slen++] = key_name[0];
5335 string[new_slen++] = key_name[1];
5336 }
5337 string[new_slen] = NUL;
5338 extra = new_slen - slen;
5339 if (buf == NULL)
5340 {
5341 if (extra < 0)
5342 /* remove matched chars, taking care of noremap */
5343 del_typebuf(-extra, offset);
5344 else if (extra > 0)
5345 /* insert the extra space we need */
5346 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5347
5348 /*
5349 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5350 * typebuf.tb_buf[]!
5351 */
5352 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5353 (size_t)new_slen);
5354 }
5355 else
5356 {
5357 if (extra < 0)
5358 /* remove matched characters */
5359 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005360 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005362 {
5363 /* Insert the extra space we need. If there is insufficient
5364 * space return -1. */
5365 if (*buflen + extra + new_slen >= bufsize)
5366 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005368 (size_t)(*buflen - offset));
5369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005371 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005373 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375
Bram Moolenaar2951b772013-07-03 12:45:31 +02005376#ifdef FEAT_TERMRESPONSE
5377 LOG_TR("normal character");
5378#endif
5379
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 return 0; /* no match found */
5381}
5382
5383/*
5384 * Replace any terminal code strings in from[] with the equivalent internal
5385 * vim representation. This is used for the "from" and "to" part of a
5386 * mapping, and the "to" part of a menu command.
5387 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5388 * '<'.
5389 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5390 *
5391 * The replacement is done in result[] and finally copied into allocated
5392 * memory. If this all works well *bufp is set to the allocated memory and a
5393 * pointer to it is returned. If something fails *bufp is set to NULL and from
5394 * is returned.
5395 *
5396 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5397 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5398 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5399 * instead of a CTRL-V.
5400 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005401 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005402replace_termcodes(
5403 char_u *from,
5404 char_u **bufp,
5405 int from_part,
5406 int do_lt, /* also translate <lt> */
5407 int special) /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408{
5409 int i;
5410 int slen;
5411 int key;
5412 int dlen = 0;
5413 char_u *src;
5414 int do_backslash; /* backslash is a special character */
5415 int do_special; /* recognize <> key codes */
5416 int do_key_code; /* recognize raw key codes */
5417 char_u *result; /* buffer for resulting string */
5418
5419 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005420 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5422
5423 /*
5424 * Allocate space for the translation. Worst case a single character is
5425 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5426 */
5427 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5428 if (result == NULL) /* out of memory */
5429 {
5430 *bufp = NULL;
5431 return from;
5432 }
5433
5434 src = from;
5435
5436 /*
5437 * Check for #n at start only: function key n
5438 */
5439 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5440 {
5441 result[dlen++] = K_SPECIAL;
5442 result[dlen++] = 'k';
5443 if (src[1] == '0')
5444 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5445 else
5446 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5447 src += 2;
5448 }
5449
5450 /*
5451 * Copy each byte from *from to result[dlen]
5452 */
5453 while (*src != NUL)
5454 {
5455 /*
5456 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005457 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 */
5459 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5460 {
5461#ifdef FEAT_EVAL
5462 /*
5463 * Replace <SID> by K_SNR <script-nr> _.
5464 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5465 */
5466 if (STRNICMP(src, "<SID>", 5) == 0)
5467 {
5468 if (current_SID <= 0)
5469 EMSG(_(e_usingsid));
5470 else
5471 {
5472 src += 5;
5473 result[dlen++] = K_SPECIAL;
5474 result[dlen++] = (int)KS_EXTRA;
5475 result[dlen++] = (int)KE_SNR;
5476 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5477 dlen += (int)STRLEN(result + dlen);
5478 result[dlen++] = '_';
5479 continue;
5480 }
5481 }
5482#endif
5483
5484 slen = trans_special(&src, result + dlen, TRUE);
5485 if (slen)
5486 {
5487 dlen += slen;
5488 continue;
5489 }
5490 }
5491
5492 /*
5493 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5494 * Note that this is also checked after replacing the <> form.
5495 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5496 * it could be a character in the file.
5497 */
5498 if (do_key_code)
5499 {
5500 i = find_term_bykeys(src);
5501 if (i >= 0)
5502 {
5503 result[dlen++] = K_SPECIAL;
5504 result[dlen++] = termcodes[i].name[0];
5505 result[dlen++] = termcodes[i].name[1];
5506 src += termcodes[i].len;
5507 /* If terminal code matched, continue after it. */
5508 continue;
5509 }
5510 }
5511
5512#ifdef FEAT_EVAL
5513 if (do_special)
5514 {
5515 char_u *p, *s, len;
5516
5517 /*
5518 * Replace <Leader> by the value of "mapleader".
5519 * Replace <LocalLeader> by the value of "maplocalleader".
5520 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5521 */
5522 if (STRNICMP(src, "<Leader>", 8) == 0)
5523 {
5524 len = 8;
5525 p = get_var_value((char_u *)"g:mapleader");
5526 }
5527 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5528 {
5529 len = 13;
5530 p = get_var_value((char_u *)"g:maplocalleader");
5531 }
5532 else
5533 {
5534 len = 0;
5535 p = NULL;
5536 }
5537 if (len != 0)
5538 {
5539 /* Allow up to 8 * 6 characters for "mapleader". */
5540 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5541 s = (char_u *)"\\";
5542 else
5543 s = p;
5544 while (*s != NUL)
5545 result[dlen++] = *s++;
5546 src += len;
5547 continue;
5548 }
5549 }
5550#endif
5551
5552 /*
5553 * Remove CTRL-V and ignore the next character.
5554 * For "from" side the CTRL-V at the end is included, for the "to"
5555 * part it is removed.
5556 * If 'cpoptions' does not contain 'B', also accept a backslash.
5557 */
5558 key = *src;
5559 if (key == Ctrl_V || (do_backslash && key == '\\'))
5560 {
5561 ++src; /* skip CTRL-V or backslash */
5562 if (*src == NUL)
5563 {
5564 if (from_part)
5565 result[dlen++] = key;
5566 break;
5567 }
5568 }
5569
5570#ifdef FEAT_MBYTE
5571 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005572 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573#endif
5574 {
5575 /*
5576 * If the character is K_SPECIAL, replace it with K_SPECIAL
5577 * KS_SPECIAL KE_FILLER.
5578 * If compiled with the GUI replace CSI with K_CSI.
5579 */
5580 if (*src == K_SPECIAL)
5581 {
5582 result[dlen++] = K_SPECIAL;
5583 result[dlen++] = KS_SPECIAL;
5584 result[dlen++] = KE_FILLER;
5585 }
5586# ifdef FEAT_GUI
5587 else if (*src == CSI)
5588 {
5589 result[dlen++] = K_SPECIAL;
5590 result[dlen++] = KS_EXTRA;
5591 result[dlen++] = (int)KE_CSI;
5592 }
5593# endif
5594 else
5595 result[dlen++] = *src;
5596 ++src;
5597 }
5598 }
5599 result[dlen] = NUL;
5600
5601 /*
5602 * Copy the new string to allocated memory.
5603 * If this fails, just return from.
5604 */
5605 if ((*bufp = vim_strsave(result)) != NULL)
5606 from = *bufp;
5607 vim_free(result);
5608 return from;
5609}
5610
5611/*
5612 * Find a termcode with keys 'src' (must be NUL terminated).
5613 * Return the index in termcodes[], or -1 if not found.
5614 */
5615 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005616find_term_bykeys(char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617{
5618 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005619 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620
5621 for (i = 0; i < tc_len; ++i)
5622 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005623 if (slen == termcodes[i].len
5624 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 return i;
5626 }
5627 return -1;
5628}
5629
5630/*
5631 * Gather the first characters in the terminal key codes into a string.
5632 * Used to speed up check_termcode().
5633 */
5634 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005635gather_termleader(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 int i;
5638 int len = 0;
5639
5640#ifdef FEAT_GUI
5641 if (gui.in_use)
5642 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5643#endif
5644#ifdef FEAT_TERMRESPONSE
5645 if (check_for_codes)
5646 termleader[len++] = DCS; /* the termcode response starts with DCS
5647 in 8-bit mode */
5648#endif
5649 termleader[len] = NUL;
5650
5651 for (i = 0; i < tc_len; ++i)
5652 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5653 {
5654 termleader[len++] = termcodes[i].code[0];
5655 termleader[len] = NUL;
5656 }
5657
5658 need_gather = FALSE;
5659}
5660
5661/*
5662 * Show all termcodes (for ":set termcap")
5663 * This code looks a lot like showoptions(), but is different.
5664 */
5665 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005666show_termcodes(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667{
5668 int col;
5669 int *items;
5670 int item_count;
5671 int run;
5672 int row, rows;
5673 int cols;
5674 int i;
5675 int len;
5676
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005677#define INC3 27 /* try to make three columns */
5678#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679#define GAP 2 /* spaces between columns */
5680
5681 if (tc_len == 0) /* no terminal codes (must be GUI) */
5682 return;
5683 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5684 if (items == NULL)
5685 return;
5686
5687 /* Highlight title */
5688 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5689
5690 /*
5691 * do the loop two times:
5692 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005693 * 2. display the medium items (medium length strings)
5694 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005696 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
5698 /*
5699 * collect the items in items[]
5700 */
5701 item_count = 0;
5702 for (i = 0; i < tc_len; i++)
5703 {
5704 len = show_one_termcode(termcodes[i].name,
5705 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005706 if (len <= INC3 - GAP ? run == 1
5707 : len <= INC2 - GAP ? run == 2
5708 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 items[item_count++] = i;
5710 }
5711
5712 /*
5713 * display the items
5714 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005715 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005717 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 if (cols == 0)
5719 cols = 1;
5720 rows = (item_count + cols - 1) / cols;
5721 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005722 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 rows = item_count;
5724 for (row = 0; row < rows && !got_int; ++row)
5725 {
5726 msg_putchar('\n'); /* go to next line */
5727 if (got_int) /* 'q' typed in more */
5728 break;
5729 col = 0;
5730 for (i = row; i < item_count; i += rows)
5731 {
5732 msg_col = col; /* make columns */
5733 show_one_termcode(termcodes[items[i]].name,
5734 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005735 if (run == 2)
5736 col += INC2;
5737 else
5738 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 }
5740 out_flush();
5741 ui_breakcheck();
5742 }
5743 }
5744 vim_free(items);
5745}
5746
5747/*
5748 * Show one termcode entry.
5749 * Output goes into IObuff[]
5750 */
5751 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005752show_one_termcode(char_u *name, char_u *code, int printit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753{
5754 char_u *p;
5755 int len;
5756
5757 if (name[0] > '~')
5758 {
5759 IObuff[0] = ' ';
5760 IObuff[1] = ' ';
5761 IObuff[2] = ' ';
5762 IObuff[3] = ' ';
5763 }
5764 else
5765 {
5766 IObuff[0] = 't';
5767 IObuff[1] = '_';
5768 IObuff[2] = name[0];
5769 IObuff[3] = name[1];
5770 }
5771 IObuff[4] = ' ';
5772
5773 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5774 if (p[1] != 't')
5775 STRCPY(IObuff + 5, p);
5776 else
5777 IObuff[5] = NUL;
5778 len = (int)STRLEN(IObuff);
5779 do
5780 IObuff[len++] = ' ';
5781 while (len < 17);
5782 IObuff[len] = NUL;
5783 if (code == NULL)
5784 len += 4;
5785 else
5786 len += vim_strsize(code);
5787
5788 if (printit)
5789 {
5790 msg_puts(IObuff);
5791 if (code == NULL)
5792 msg_puts((char_u *)"NULL");
5793 else
5794 msg_outtrans(code);
5795 }
5796 return len;
5797}
5798
5799#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5800/*
5801 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5802 * termcap codes from the terminal itself.
5803 * We get them one by one to avoid a very long response string.
5804 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005805static int xt_index_in = 0;
5806static int xt_index_out = 0;
5807
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005809req_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810{
5811 xt_index_out = 0;
5812 xt_index_in = 0;
5813 req_more_codes_from_term();
5814}
5815
5816 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005817req_more_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818{
5819 char buf[11];
5820 int old_idx = xt_index_out;
5821
5822 /* Don't do anything when going to exit. */
5823 if (exiting)
5824 return;
5825
5826 /* Send up to 10 more requests out than we received. Avoid sending too
5827 * many, there can be a buffer overflow somewhere. */
5828 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5829 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02005830# ifdef DEBUG_TERMRESPONSE
5831 char dbuf[100];
5832
5833 sprintf(dbuf, "Requesting XT %d: %s",
5834 xt_index_out, key_names[xt_index_out]);
5835 log_tr(dbuf);
5836# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 sprintf(buf, "\033P+q%02x%02x\033\\",
5838 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5839 out_str_nf((char_u *)buf);
5840 ++xt_index_out;
5841 }
5842
5843 /* Send the codes out right away. */
5844 if (xt_index_out != old_idx)
5845 out_flush();
5846}
5847
5848/*
5849 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5850 * A "0" instead of the "1" indicates a code that isn't supported.
5851 * Both <name> and <string> are encoded in hex.
5852 * "code" points to the "0" or "1".
5853 */
5854 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005855got_code_from_term(char_u *code, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856{
5857#define XT_LEN 100
5858 char_u name[3];
5859 char_u str[XT_LEN];
5860 int i;
5861 int j = 0;
5862 int c;
5863
5864 /* A '1' means the code is supported, a '0' means it isn't.
5865 * When half the length is > XT_LEN we can't use it.
5866 * Our names are currently all 2 characters. */
5867 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5868 {
5869 /* Get the name from the response and find it in the table. */
5870 name[0] = hexhex2nr(code + 3);
5871 name[1] = hexhex2nr(code + 5);
5872 name[2] = NUL;
5873 for (i = 0; key_names[i] != NULL; ++i)
5874 {
5875 if (STRCMP(key_names[i], name) == 0)
5876 {
5877 xt_index_in = i;
5878 break;
5879 }
5880 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005881# ifdef DEBUG_TERMRESPONSE
5882 {
5883 char buf[100];
5884
5885 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
5886 log_tr(buf);
5887 }
5888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 if (key_names[i] != NULL)
5890 {
5891 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5892 str[j++] = c;
5893 str[j] = NUL;
5894 if (name[0] == 'C' && name[1] == 'o')
5895 {
5896 /* Color count is not a key code. */
5897 i = atoi((char *)str);
5898 if (i != t_colors)
5899 {
5900 /* Nr of colors changed, initialize highlighting and
Bram Moolenaar238a5642006-02-21 22:12:05 +00005901 * redraw everything. This causes a redraw, which usually
5902 * clears the message. Try keeping the message if it
5903 * might work. */
5904 set_keep_msg_from_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 set_color_count(i);
5906 init_highlight(TRUE, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +02005907#ifdef DEBUG_TERMRESPONSE
5908 {
5909 char buf[100];
5910 int r = redraw_asap(CLEAR);
5911
5912 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
5913 log_tr(buf);
5914 }
5915#else
5916 redraw_asap(CLEAR);
5917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 }
5919 }
5920 else
5921 {
5922 /* First delete any existing entry with the same code. */
5923 i = find_term_bykeys(str);
5924 if (i >= 0)
5925 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005926 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 }
5928 }
5929 }
5930
5931 /* May request more codes now that we received one. */
5932 ++xt_index_in;
5933 req_more_codes_from_term();
5934}
5935
5936/*
5937 * Check if there are any unanswered requests and deal with them.
5938 * This is called before starting an external program or getting direct
5939 * keyboard input. We don't want responses to be send to that program or
5940 * handled as typed text.
5941 */
5942 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005943check_for_codes_from_term(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944{
5945 int c;
5946
5947 /* If no codes requested or all are answered, no need to wait. */
5948 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5949 return;
5950
5951 /* Vgetc() will check for and handle any response.
5952 * Keep calling vpeekc() until we don't get any responses. */
5953 ++no_mapping;
5954 ++allow_keys;
5955 for (;;)
5956 {
5957 c = vpeekc();
5958 if (c == NUL) /* nothing available */
5959 break;
5960
5961 /* If a response is recognized it's replaced with K_IGNORE, must read
5962 * it from the input stream. If there is no K_IGNORE we can't do
5963 * anything, break here (there might be some responses further on, but
5964 * we don't want to throw away any typed chars). */
5965 if (c != K_SPECIAL && c != K_IGNORE)
5966 break;
5967 c = vgetc();
5968 if (c != K_IGNORE)
5969 {
5970 vungetc(c);
5971 break;
5972 }
5973 }
5974 --no_mapping;
5975 --allow_keys;
5976}
5977#endif
5978
5979#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5980/*
5981 * Translate an internal mapping/abbreviation representation into the
5982 * corresponding external one recognized by :map/:abbrev commands;
5983 * respects the current B/k/< settings of 'cpoption'.
5984 *
5985 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00005986 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 *
5988 * It uses a growarray to build the translation string since the
5989 * latter can be wider than the original description. The caller has to
5990 * free the string afterwards.
5991 *
5992 * Returns NULL when there is a problem.
5993 */
5994 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005995translate_mapping(
5996 char_u *str,
5997 int expmap) /* TRUE when expanding mappings on command-line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998{
5999 garray_T ga;
6000 int c;
6001 int modifiers;
6002 int cpo_bslash;
6003 int cpo_special;
6004 int cpo_keycode;
6005
6006 ga_init(&ga);
6007 ga.ga_itemsize = 1;
6008 ga.ga_growsize = 40;
6009
6010 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6011 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6012 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6013
6014 for (; *str; ++str)
6015 {
6016 c = *str;
6017 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6018 {
6019 modifiers = 0;
6020 if (str[1] == KS_MODIFIER)
6021 {
6022 str++;
6023 modifiers = *++str;
6024 c = *++str;
6025 }
6026 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6027 {
6028 int i;
6029
6030 /* try to find special key in termcodes */
6031 for (i = 0; i < tc_len; ++i)
6032 if (termcodes[i].name[0] == str[1]
6033 && termcodes[i].name[1] == str[2])
6034 break;
6035 if (i < tc_len)
6036 {
6037 ga_concat(&ga, termcodes[i].code);
6038 str += 2;
6039 continue; /* for (str) */
6040 }
6041 }
6042 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6043 {
6044 if (expmap && cpo_special)
6045 {
6046 ga_clear(&ga);
6047 return NULL;
6048 }
6049 c = TO_SPECIAL(str[1], str[2]);
6050 if (c == K_ZERO) /* display <Nul> as ^@ */
6051 c = NUL;
6052 str += 2;
6053 }
6054 if (IS_SPECIAL(c) || modifiers) /* special key */
6055 {
6056 if (expmap && cpo_special)
6057 {
6058 ga_clear(&ga);
6059 return NULL;
6060 }
6061 ga_concat(&ga, get_special_key_name(c, modifiers));
6062 continue; /* for (str) */
6063 }
6064 }
6065 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6066 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6067 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6068 if (c)
6069 ga_append(&ga, c);
6070 }
6071 ga_append(&ga, NUL);
6072 return (char_u *)(ga.ga_data);
6073}
6074#endif
6075
6076#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6077static char ksme_str[20];
6078static char ksmr_str[20];
6079static char ksmd_str[20];
6080
6081/*
6082 * For Win32 console: update termcap codes for existing console attributes.
6083 */
6084 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006085update_tcap(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086{
6087 struct builtin_term *p;
6088
6089 p = find_builtin_term(DEFAULT_TERM);
6090 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6091 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6092 attr | 0x08); /* FOREGROUND_INTENSITY */
6093 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6094 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6095
6096 while (p->bt_string != NULL)
6097 {
6098 if (p->bt_entry == (int)KS_ME)
6099 p->bt_string = &ksme_str[0];
6100 else if (p->bt_entry == (int)KS_MR)
6101 p->bt_string = &ksmr_str[0];
6102 else if (p->bt_entry == (int)KS_MD)
6103 p->bt_string = &ksmd_str[0];
6104 ++p;
6105 }
6106}
6107#endif