blob: b3201feb10a51ca52b701cee2046e5f12acc3dde [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
77static struct builtin_term *find_builtin_term __ARGS((char_u *name));
78static void parse_builtin_tcap __ARGS((char_u *s));
79static void term_color __ARGS((char_u *s, int n));
80static void gather_termleader __ARGS((void));
81#ifdef FEAT_TERMRESPONSE
82static void req_codes_from_term __ARGS((void));
83static void req_more_codes_from_term __ARGS((void));
84static void got_code_from_term __ARGS((char_u *code, int len));
85static void check_for_codes_from_term __ARGS((void));
86#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 Moolenaar071d4272004-06-13 20:20:40 +000090static int get_bytes_from_buf __ARGS((char_u *, char_u *, int));
91#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000092static void del_termcode_idx __ARGS((int idx));
93static int term_is_builtin __ARGS((char_u *name));
94static int term_7to8bit __ARGS((char_u *p));
95#ifdef FEAT_TERMRESPONSE
96static void switch_to_8bit __ARGS((void));
97#endif
98
99#ifdef HAVE_TGETENT
100static char_u *tgetent_error __ARGS((char_u *, char_u *));
101
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 */
106char *tgetstr __ARGS((char *, char **));
107
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))
158static char_u *vim_tgetstr __ARGS((char *s, char_u **pp));
159#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 *
1415find_builtin_term(term)
1416 char_u *term;
1417{
1418 struct builtin_term *p;
1419
1420 p = builtin_termcaps;
1421 while (p->bt_string != NULL)
1422 {
1423 if (p->bt_entry == (int)KS_NAME)
1424 {
1425#ifdef UNIX
1426 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1427 return p;
1428 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1429 return p;
1430 else
1431#endif
1432#ifdef VMS
1433 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1434 return p;
1435 else
1436#endif
1437 if (STRCMP(term, p->bt_string) == 0)
1438 return p;
1439 }
1440 ++p;
1441 }
1442 return p;
1443}
1444
1445/*
1446 * Parsing of the builtin termcap entries.
1447 * Caller should check if 'name' is a valid builtin term.
1448 * The terminal's name is not set, as this is already done in termcapinit().
1449 */
1450 static void
1451parse_builtin_tcap(term)
1452 char_u *term;
1453{
1454 struct builtin_term *p;
1455 char_u name[2];
1456 int term_8bit;
1457
1458 p = find_builtin_term(term);
1459 term_8bit = term_is_8bit(term);
1460
1461 /* Do not parse if builtin term not found */
1462 if (p->bt_string == NULL)
1463 return;
1464
1465 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1466 {
1467 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1468 {
1469 /* Only set the value if it wasn't set yet. */
1470 if (term_strings[p->bt_entry] == NULL
1471 || term_strings[p->bt_entry] == empty_option)
1472 {
1473 /* 8bit terminal: use CSI instead of <Esc>[ */
1474 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1475 {
1476 char_u *s, *t;
1477
1478 s = vim_strsave((char_u *)p->bt_string);
1479 if (s != NULL)
1480 {
1481 for (t = s; *t; ++t)
1482 if (term_7to8bit(t))
1483 {
1484 *t = term_7to8bit(t);
1485 STRCPY(t + 1, t + 2);
1486 }
1487 term_strings[p->bt_entry] = s;
1488 set_term_option_alloced(&term_strings[p->bt_entry]);
1489 }
1490 }
1491 else
1492 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1493 }
1494 }
1495 else
1496 {
1497 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1498 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1499 if (find_termcode(name) == NULL)
1500 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1501 }
1502 }
1503}
1504#if defined(HAVE_TGETENT) || defined(FEAT_TERMRESPONSE)
1505static void set_color_count __ARGS((int nr));
1506
1507/*
1508 * Set number of colors.
1509 * Store it as a number in t_colors.
1510 * Store it as a string in T_CCO (using nr_colors[]).
1511 */
1512 static void
1513set_color_count(nr)
1514 int nr;
1515{
1516 char_u nr_colors[20]; /* string for number of colors */
1517
1518 t_colors = nr;
1519 if (t_colors > 1)
1520 sprintf((char *)nr_colors, "%d", t_colors);
1521 else
1522 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001523 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524}
1525#endif
1526
1527#ifdef HAVE_TGETENT
1528static char *(key_names[]) =
1529{
1530#ifdef FEAT_TERMRESPONSE
1531 /* Do this one first, it may cause a screen redraw. */
1532 "Co",
1533#endif
1534 "ku", "kd", "kr", "kl",
1535# ifdef ARCHIE
1536 "su", "sd", /* Termcap code made up! */
1537# endif
1538 "#2", "#4", "%i", "*7",
1539 "k1", "k2", "k3", "k4", "k5", "k6",
1540 "k7", "k8", "k9", "k;", "F1", "F2",
1541 "%1", "&8", "kb", "kI", "kD", "kh",
1542 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1543 NULL
1544};
1545#endif
1546
1547/*
1548 * Set terminal options for terminal "term".
1549 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1550 *
1551 * While doing this, until ttest(), some options may be NULL, be careful.
1552 */
1553 int
1554set_termname(term)
1555 char_u *term;
1556{
1557 struct builtin_term *termp;
1558#ifdef HAVE_TGETENT
1559 int builtin_first = p_tbi;
1560 int try;
1561 int termcap_cleared = FALSE;
1562#endif
1563 int width = 0, height = 0;
1564 char_u *error_msg = NULL;
1565 char_u *bs_p, *del_p;
1566
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001567 /* In silect mode (ex -s) we don't use the 'term' option. */
1568 if (silent_mode)
1569 return OK;
1570
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 detected_8bit = FALSE; /* reset 8-bit detection */
1572
1573 if (term_is_builtin(term))
1574 {
1575 term += 8;
1576#ifdef HAVE_TGETENT
1577 builtin_first = 1;
1578#endif
1579 }
1580
1581/*
1582 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1583 * If builtin_first is TRUE:
1584 * 0. try builtin termcap
1585 * 1. try external termcap
1586 * 2. if both fail default to a builtin terminal
1587 * If builtin_first is FALSE:
1588 * 1. try external termcap
1589 * 2. try builtin termcap, if both fail default to a builtin terminal
1590 */
1591#ifdef HAVE_TGETENT
1592 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1593 {
1594 /*
1595 * Use external termcap
1596 */
1597 if (try == 1)
1598 {
1599 char_u *p;
1600 static char_u tstrbuf[TBUFSZ];
1601 int i;
1602 char_u tbuf[TBUFSZ];
1603 char_u *tp;
1604 static struct {
1605 enum SpecialKey dest; /* index in term_strings[] */
1606 char *name; /* termcap name for string */
1607 } string_names[] =
1608 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1609 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1610 {KS_CL, "cl"}, {KS_CD, "cd"},
1611 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1612 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1613 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1614 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001615 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1616 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1618 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1619 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1620 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1621 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1622 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1623 {KS_TS, "ts"}, {KS_FS, "fs"},
1624 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001625 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaare5401222015-06-25 19:16:56 +02001626 {KS_U7, "u7"}, {KS_RBG, "RB"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 {(enum SpecialKey)0, NULL}
1628 };
1629
1630 /*
1631 * If the external termcap does not have a matching entry, try the
1632 * builtin ones.
1633 */
1634 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1635 {
1636 tp = tstrbuf;
1637 if (!termcap_cleared)
1638 {
1639 clear_termoptions(); /* clear old options */
1640 termcap_cleared = TRUE;
1641 }
1642
1643 /* get output strings */
1644 for (i = 0; string_names[i].name != NULL; ++i)
1645 {
1646 if (term_str(string_names[i].dest) == NULL
1647 || term_str(string_names[i].dest) == empty_option)
1648 term_str(string_names[i].dest) =
1649 TGETSTR(string_names[i].name, &tp);
1650 }
1651
1652 /* tgetflag() returns 1 if the flag is present, 0 if not and
1653 * possibly -1 if the flag doesn't exist. */
1654 if ((T_MS == NULL || T_MS == empty_option)
1655 && tgetflag("ms") > 0)
1656 T_MS = (char_u *)"y";
1657 if ((T_XS == NULL || T_XS == empty_option)
1658 && tgetflag("xs") > 0)
1659 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001660 if ((T_XN == NULL || T_XN == empty_option)
1661 && tgetflag("xn") > 0)
1662 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if ((T_DB == NULL || T_DB == empty_option)
1664 && tgetflag("db") > 0)
1665 T_DB = (char_u *)"y";
1666 if ((T_DA == NULL || T_DA == empty_option)
1667 && tgetflag("da") > 0)
1668 T_DA = (char_u *)"y";
1669 if ((T_UT == NULL || T_UT == empty_option)
1670 && tgetflag("ut") > 0)
1671 T_UT = (char_u *)"y";
1672
1673
1674 /*
1675 * get key codes
1676 */
1677 for (i = 0; key_names[i] != NULL; ++i)
1678 {
1679 if (find_termcode((char_u *)key_names[i]) == NULL)
1680 {
1681 p = TGETSTR(key_names[i], &tp);
1682 /* if cursor-left == backspace, ignore it (televideo
1683 * 925) */
1684 if (p != NULL
1685 && (*p != Ctrl_H
1686 || key_names[i][0] != 'k'
1687 || key_names[i][1] != 'l'))
1688 add_termcode((char_u *)key_names[i], p, FALSE);
1689 }
1690 }
1691
1692 if (height == 0)
1693 height = tgetnum("li");
1694 if (width == 0)
1695 width = tgetnum("co");
1696
1697 /*
1698 * Get number of colors (if not done already).
1699 */
1700 if (term_str(KS_CCO) == NULL
1701 || term_str(KS_CCO) == empty_option)
1702 set_color_count(tgetnum("Co"));
1703
1704# ifndef hpux
1705 BC = (char *)TGETSTR("bc", &tp);
1706 UP = (char *)TGETSTR("up", &tp);
1707 p = TGETSTR("pc", &tp);
1708 if (p)
1709 PC = *p;
1710# endif /* hpux */
1711 }
1712 }
1713 else /* try == 0 || try == 2 */
1714#endif /* HAVE_TGETENT */
1715 /*
1716 * Use builtin termcap
1717 */
1718 {
1719#ifdef HAVE_TGETENT
1720 /*
1721 * If builtin termcap was already used, there is no need to search
1722 * for the builtin termcap again, quit now.
1723 */
1724 if (try == 2 && builtin_first && termcap_cleared)
1725 break;
1726#endif
1727 /*
1728 * search for 'term' in builtin_termcaps[]
1729 */
1730 termp = find_builtin_term(term);
1731 if (termp->bt_string == NULL) /* did not find it */
1732 {
1733#ifdef HAVE_TGETENT
1734 /*
1735 * If try == 0, first try the external termcap. If that is not
1736 * found we'll get back here with try == 2.
1737 * If termcap_cleared is set we used the external termcap,
1738 * don't complain about not finding the term in the builtin
1739 * termcap.
1740 */
1741 if (try == 0) /* try external one */
1742 continue;
1743 if (termcap_cleared) /* found in external termcap */
1744 break;
1745#endif
1746
1747 mch_errmsg("\r\n");
1748 if (error_msg != NULL)
1749 {
1750 mch_errmsg((char *)error_msg);
1751 mch_errmsg("\r\n");
1752 }
1753 mch_errmsg("'");
1754 mch_errmsg((char *)term);
1755 mch_errmsg(_("' not known. Available builtin terminals are:"));
1756 mch_errmsg("\r\n");
1757 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1758 ++termp)
1759 {
1760 if (termp->bt_entry == (int)KS_NAME)
1761 {
1762#ifdef HAVE_TGETENT
1763 mch_errmsg(" builtin_");
1764#else
1765 mch_errmsg(" ");
1766#endif
1767 mch_errmsg(termp->bt_string);
1768 mch_errmsg("\r\n");
1769 }
1770 }
1771 /* when user typed :set term=xxx, quit here */
1772 if (starting != NO_SCREEN)
1773 {
1774 screen_start(); /* don't know where cursor is now */
1775 wait_return(TRUE);
1776 return FAIL;
1777 }
1778 term = DEFAULT_TERM;
1779 mch_errmsg(_("defaulting to '"));
1780 mch_errmsg((char *)term);
1781 mch_errmsg("'\r\n");
1782 if (emsg_silent == 0)
1783 {
1784 screen_start(); /* don't know where cursor is now */
1785 out_flush();
1786 ui_delay(2000L, TRUE);
1787 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001788 set_string_option_direct((char_u *)"term", -1, term,
1789 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 display_errors();
1791 }
1792 out_flush();
1793#ifdef HAVE_TGETENT
1794 if (!termcap_cleared)
1795 {
1796#endif
1797 clear_termoptions(); /* clear old options */
1798#ifdef HAVE_TGETENT
1799 termcap_cleared = TRUE;
1800 }
1801#endif
1802 parse_builtin_tcap(term);
1803#ifdef FEAT_GUI
1804 if (term_is_gui(term))
1805 {
1806 out_flush();
1807 gui_init();
1808 /* If starting the GUI failed, don't do any of the other
1809 * things for this terminal */
1810 if (!gui.in_use)
1811 return FAIL;
1812#ifdef HAVE_TGETENT
1813 break; /* don't try using external termcap */
1814#endif
1815 }
1816#endif /* FEAT_GUI */
1817 }
1818#ifdef HAVE_TGETENT
1819 }
1820#endif
1821
1822/*
1823 * special: There is no info in the termcap about whether the cursor
1824 * positioning is relative to the start of the screen or to the start of the
1825 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1826 * relative.
1827 */
1828 if (STRCMP(term, "pcterm") == 0)
1829 T_CCS = (char_u *)"yes";
1830 else
1831 T_CCS = empty_option;
1832
1833#ifdef UNIX
1834/*
1835 * Any "stty" settings override the default for t_kb from the termcap.
1836 * This is in os_unix.c, because it depends a lot on the version of unix that
1837 * is being used.
1838 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1839 */
1840#ifdef FEAT_GUI
1841 if (!gui.in_use)
1842#endif
1843 get_stty();
1844#endif
1845
1846/*
1847 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1848 * didn't work, use the default CTRL-H
1849 * The default for t_kD is DEL, unless t_kb is DEL.
1850 * The vim_strsave'd strings are probably lost forever, well it's only two
1851 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1852 * directly.
1853 */
1854#ifdef FEAT_GUI
1855 if (!gui.in_use)
1856#endif
1857 {
1858 bs_p = find_termcode((char_u *)"kb");
1859 del_p = find_termcode((char_u *)"kD");
1860 if (bs_p == NULL || *bs_p == NUL)
1861 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1862 if ((del_p == NULL || *del_p == NUL) &&
1863 (bs_p == NULL || *bs_p != DEL))
1864 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1865 }
1866
1867#if defined(UNIX) || defined(VMS)
1868 term_is_xterm = vim_is_xterm(term);
1869#endif
1870
1871#ifdef FEAT_MOUSE
1872# if defined(UNIX) || defined(VMS)
1873# ifdef FEAT_MOUSE_TTY
1874 /*
1875 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1876 * The termcode for the mouse is added as a side effect in option.c.
1877 */
1878 {
1879 char_u *p;
1880
1881 p = (char_u *)"";
1882# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar864207d2008-06-24 22:14:38 +00001883 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 {
1885 if (use_xterm_mouse())
1886 p = NULL; /* keep existing value, might be "xterm2" */
1887 else
1888 p = (char_u *)"xterm";
1889 }
1890# endif
1891 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001894 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1895 * "xterm2" in check_termcode(). */
1896 reset_option_was_set((char_u *)"ttym");
1897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (p == NULL
1899# ifdef FEAT_GUI
1900 || gui.in_use
1901# endif
1902 )
1903 check_mouse_termcode(); /* set mouse termcode anyway */
1904 }
1905# endif
1906# else
1907 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1908# endif
1909#endif /* FEAT_MOUSE */
1910
1911#ifdef FEAT_SNIFF
1912 {
1913 char_u name[2];
1914
1915 name[0] = (int)KS_EXTRA;
1916 name[1] = (int)KE_SNIFF;
1917 add_termcode(name, (char_u *)"\233sniff", FALSE);
1918 }
1919#endif
1920
1921#ifdef USE_TERM_CONSOLE
1922 /* DEFAULT_TERM indicates that it is the machine console. */
1923 if (STRCMP(term, DEFAULT_TERM) != 0)
1924 term_console = FALSE;
1925 else
1926 {
1927 term_console = TRUE;
1928# ifdef AMIGA
1929 win_resize_on(); /* enable window resizing reports */
1930# endif
1931 }
1932#endif
1933
1934#if defined(UNIX) || defined(VMS)
1935 /*
1936 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1937 */
1938 if (vim_is_fastterm(term))
1939 p_tf = TRUE;
1940#endif
1941#ifdef USE_TERM_CONSOLE
1942 /*
1943 * 'ttyfast' is default on consoles
1944 */
1945 if (term_console)
1946 p_tf = TRUE;
1947#endif
1948
1949 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1950
1951 full_screen = TRUE; /* we can use termcap codes from now on */
1952 set_term_defaults(); /* use current values as defaults */
1953#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +02001954 LOG_TR("setting crv_status to CRV_GET");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 crv_status = CRV_GET; /* Get terminal version later */
1956#endif
1957
1958 /*
1959 * Initialize the terminal with the appropriate termcap codes.
1960 * Set the mouse and window title if possible.
1961 * Don't do this when starting, need to parse the .vimrc first, because it
1962 * may redefine t_TI etc.
1963 */
1964 if (starting != NO_SCREEN)
1965 {
1966 starttermcap(); /* may change terminal mode */
1967#ifdef FEAT_MOUSE
1968 setmouse(); /* may start using the mouse */
1969#endif
1970#ifdef FEAT_TITLE
1971 maketitle(); /* may display window title */
1972#endif
1973 }
1974
1975 /* display initial screen after ttest() checking. jw. */
1976 if (width <= 0 || height <= 0)
1977 {
1978 /* termcap failed to report size */
1979 /* set defaults, in case ui_get_shellsize() also fails */
1980 width = 80;
1981#if defined(MSDOS) || defined(WIN3264)
1982 height = 25; /* console is often 25 lines */
1983#else
1984 height = 24; /* most terminals are 24 lines */
1985#endif
1986 }
1987 set_shellsize(width, height, FALSE); /* may change Rows */
1988 if (starting != NO_SCREEN)
1989 {
1990 if (scroll_region)
1991 scroll_region_reset(); /* In case Rows changed */
1992 check_map_keycodes(); /* check mappings for terminal codes used */
1993
1994#ifdef FEAT_AUTOCMD
1995 {
1996 buf_T *old_curbuf;
1997
1998 /*
1999 * Execute the TermChanged autocommands for each buffer that is
2000 * loaded.
2001 */
2002 old_curbuf = curbuf;
2003 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
2004 {
2005 if (curbuf->b_ml.ml_mfp != NULL)
2006 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2007 curbuf);
2008 }
2009 if (buf_valid(old_curbuf))
2010 curbuf = old_curbuf;
2011 }
2012#endif
2013 }
2014
2015#ifdef FEAT_TERMRESPONSE
2016 may_req_termresponse();
2017#endif
2018
2019 return OK;
2020}
2021
2022#if defined(FEAT_MOUSE) || defined(PROTO)
2023
2024# ifdef FEAT_MOUSE_TTY
2025# define HMT_NORMAL 1
2026# define HMT_NETTERM 2
2027# define HMT_DEC 4
2028# define HMT_JSBTERM 8
2029# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002030# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002031# define HMT_SGR 64
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032static int has_mouse_termcode = 0;
2033# endif
2034
Bram Moolenaar864207d2008-06-24 22:14:38 +00002035# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 void
2037set_mouse_termcode(n, s)
2038 int n; /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2039 char_u *s;
2040{
2041 char_u name[2];
2042
2043 name[0] = n;
2044 name[1] = KE_FILLER;
2045 add_termcode(name, s, FALSE);
2046# ifdef FEAT_MOUSE_TTY
2047# ifdef FEAT_MOUSE_JSB
2048 if (n == KS_JSBTERM_MOUSE)
2049 has_mouse_termcode |= HMT_JSBTERM;
2050 else
2051# endif
2052# ifdef FEAT_MOUSE_NET
2053 if (n == KS_NETTERM_MOUSE)
2054 has_mouse_termcode |= HMT_NETTERM;
2055 else
2056# endif
2057# ifdef FEAT_MOUSE_DEC
2058 if (n == KS_DEC_MOUSE)
2059 has_mouse_termcode |= HMT_DEC;
2060 else
2061# endif
2062# ifdef FEAT_MOUSE_PTERM
2063 if (n == KS_PTERM_MOUSE)
2064 has_mouse_termcode |= HMT_PTERM;
2065 else
2066# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002067# ifdef FEAT_MOUSE_URXVT
2068 if (n == KS_URXVT_MOUSE)
2069 has_mouse_termcode |= HMT_URXVT;
2070 else
2071# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002072# ifdef FEAT_MOUSE_SGR
2073 if (n == KS_SGR_MOUSE)
2074 has_mouse_termcode |= HMT_SGR;
2075 else
2076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 has_mouse_termcode |= HMT_NORMAL;
2078# endif
2079}
2080# endif
2081
2082# if ((defined(UNIX) || defined(VMS) || defined(OS2)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002083 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 void
2085del_mouse_termcode(n)
2086 int n; /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2087{
2088 char_u name[2];
2089
2090 name[0] = n;
2091 name[1] = KE_FILLER;
2092 del_termcode(name);
2093# ifdef FEAT_MOUSE_TTY
2094# ifdef FEAT_MOUSE_JSB
2095 if (n == KS_JSBTERM_MOUSE)
2096 has_mouse_termcode &= ~HMT_JSBTERM;
2097 else
2098# endif
2099# ifdef FEAT_MOUSE_NET
2100 if (n == KS_NETTERM_MOUSE)
2101 has_mouse_termcode &= ~HMT_NETTERM;
2102 else
2103# endif
2104# ifdef FEAT_MOUSE_DEC
2105 if (n == KS_DEC_MOUSE)
2106 has_mouse_termcode &= ~HMT_DEC;
2107 else
2108# endif
2109# ifdef FEAT_MOUSE_PTERM
2110 if (n == KS_PTERM_MOUSE)
2111 has_mouse_termcode &= ~HMT_PTERM;
2112 else
2113# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002114# ifdef FEAT_MOUSE_URXVT
2115 if (n == KS_URXVT_MOUSE)
2116 has_mouse_termcode &= ~HMT_URXVT;
2117 else
2118# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002119# ifdef FEAT_MOUSE_SGR
2120 if (n == KS_SGR_MOUSE)
2121 has_mouse_termcode &= ~HMT_SGR;
2122 else
2123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 has_mouse_termcode &= ~HMT_NORMAL;
2125# endif
2126}
2127# endif
2128#endif
2129
2130#ifdef HAVE_TGETENT
2131/*
2132 * Call tgetent()
2133 * Return error message if it fails, NULL if it's OK.
2134 */
2135 static char_u *
2136tgetent_error(tbuf, term)
2137 char_u *tbuf;
2138 char_u *term;
2139{
2140 int i;
2141
2142 i = TGETENT(tbuf, term);
2143 if (i < 0 /* -1 is always an error */
2144# ifdef TGETENT_ZERO_ERR
2145 || i == 0 /* sometimes zero is also an error */
2146# endif
2147 )
2148 {
2149 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2150 * tgetent() with the always existing "dumb" entry to avoid a crash or
2151 * hang. */
2152 (void)TGETENT(tbuf, "dumb");
2153
2154 if (i < 0)
2155# ifdef TGETENT_ZERO_ERR
2156 return (char_u *)_("E557: Cannot open termcap file");
2157 if (i == 0)
2158# endif
2159#ifdef TERMINFO
2160 return (char_u *)_("E558: Terminal entry not found in terminfo");
2161#else
2162 return (char_u *)_("E559: Terminal entry not found in termcap");
2163#endif
2164 }
2165 return NULL;
2166}
2167
2168/*
2169 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2170 * Fix that here.
2171 */
2172 static char_u *
2173vim_tgetstr(s, pp)
2174 char *s;
2175 char_u **pp;
2176{
2177 char *p;
2178
2179 p = tgetstr(s, (char **)pp);
2180 if (p == (char *)-1)
2181 p = NULL;
2182 return (char_u *)p;
2183}
2184#endif /* HAVE_TGETENT */
2185
2186#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(__EMX__) || defined(VMS) || defined(MACOS_X))
2187/*
2188 * Get Columns and Rows from the termcap. Used after a window signal if the
2189 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2190 * and "li" entries never change. But on some systems this works.
2191 * Errors while getting the entries are ignored.
2192 */
2193 void
2194getlinecol(cp, rp)
2195 long *cp; /* pointer to columns */
2196 long *rp; /* pointer to rows */
2197{
2198 char_u tbuf[TBUFSZ];
2199
2200 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2201 {
2202 if (*cp == 0)
2203 *cp = tgetnum("co");
2204 if (*rp == 0)
2205 *rp = tgetnum("li");
2206 }
2207}
2208#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2209
2210/*
2211 * Get a string entry from the termcap and add it to the list of termcodes.
2212 * Used for <t_xx> special keys.
2213 * Give an error message for failure when not sourcing.
2214 * If force given, replace an existing entry.
2215 * Return FAIL if the entry was not found, OK if the entry was added.
2216 */
2217 int
2218add_termcap_entry(name, force)
2219 char_u *name;
2220 int force;
2221{
2222 char_u *term;
2223 int key;
2224 struct builtin_term *termp;
2225#ifdef HAVE_TGETENT
2226 char_u *string;
2227 int i;
2228 int builtin_first;
2229 char_u tbuf[TBUFSZ];
2230 char_u tstrbuf[TBUFSZ];
2231 char_u *tp = tstrbuf;
2232 char_u *error_msg = NULL;
2233#endif
2234
2235/*
2236 * If the GUI is running or will start in a moment, we only support the keys
2237 * that the GUI can produce.
2238 */
2239#ifdef FEAT_GUI
2240 if (gui.in_use || gui.starting)
2241 return gui_mch_haskey(name);
2242#endif
2243
2244 if (!force && find_termcode(name) != NULL) /* it's already there */
2245 return OK;
2246
2247 term = T_NAME;
2248 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2249 return FAIL;
2250
2251 if (term_is_builtin(term)) /* name starts with "builtin_" */
2252 {
2253 term += 8;
2254#ifdef HAVE_TGETENT
2255 builtin_first = TRUE;
2256#endif
2257 }
2258#ifdef HAVE_TGETENT
2259 else
2260 builtin_first = p_tbi;
2261#endif
2262
2263#ifdef HAVE_TGETENT
2264/*
2265 * We can get the entry from the builtin termcap and from the external one.
2266 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2267 * builtin termcap first.
2268 * If 'ttybuiltin' is off, try external termcap first.
2269 */
2270 for (i = 0; i < 2; ++i)
2271 {
Bram Moolenaar98b30a42015-11-10 15:18:02 +01002272 if ((!builtin_first) == i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273#endif
2274 /*
2275 * Search in builtin termcap
2276 */
2277 {
2278 termp = find_builtin_term(term);
2279 if (termp->bt_string != NULL) /* found it */
2280 {
2281 key = TERMCAP2KEY(name[0], name[1]);
2282 while (termp->bt_entry != (int)KS_NAME)
2283 {
2284 if ((int)termp->bt_entry == key)
2285 {
2286 add_termcode(name, (char_u *)termp->bt_string,
2287 term_is_8bit(term));
2288 return OK;
2289 }
2290 ++termp;
2291 }
2292 }
2293 }
2294#ifdef HAVE_TGETENT
2295 else
2296 /*
2297 * Search in external termcap
2298 */
2299 {
2300 error_msg = tgetent_error(tbuf, term);
2301 if (error_msg == NULL)
2302 {
2303 string = TGETSTR((char *)name, &tp);
2304 if (string != NULL && *string != NUL)
2305 {
2306 add_termcode(name, string, FALSE);
2307 return OK;
2308 }
2309 }
2310 }
2311 }
2312#endif
2313
2314 if (sourcing_name == NULL)
2315 {
2316#ifdef HAVE_TGETENT
2317 if (error_msg != NULL)
2318 EMSG(error_msg);
2319 else
2320#endif
2321 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2322 }
2323 return FAIL;
2324}
2325
2326 static int
2327term_is_builtin(name)
2328 char_u *name;
2329{
2330 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2331}
2332
2333/*
2334 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2335 * Assume that the terminal is using 8-bit controls when the name contains
2336 * "8bit", like in "xterm-8bit".
2337 */
2338 int
2339term_is_8bit(name)
2340 char_u *name;
2341{
2342 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2343}
2344
2345/*
2346 * Translate terminal control chars from 7-bit to 8-bit:
2347 * <Esc>[ -> CSI
2348 * <Esc>] -> <M-C-]>
2349 * <Esc>O -> <M-C-O>
2350 */
2351 static int
2352term_7to8bit(p)
2353 char_u *p;
2354{
2355 if (*p == ESC)
2356 {
2357 if (p[1] == '[')
2358 return CSI;
2359 if (p[1] == ']')
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02002360 return OSC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 if (p[1] == 'O')
2362 return 0x8f;
2363 }
2364 return 0;
2365}
2366
2367#ifdef FEAT_GUI
2368 int
2369term_is_gui(name)
2370 char_u *name;
2371{
2372 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2373}
2374#endif
2375
2376#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2377
2378 char_u *
2379tltoa(i)
2380 unsigned long i;
2381{
2382 static char_u buf[16];
2383 char_u *p;
2384
2385 p = buf + 15;
2386 *p = '\0';
2387 do
2388 {
2389 --p;
2390 *p = (char_u) (i % 10 + '0');
2391 i /= 10;
2392 }
2393 while (i > 0 && p > buf);
2394 return p;
2395}
2396#endif
2397
2398#ifndef HAVE_TGETENT
2399
2400/*
2401 * minimal tgoto() implementation.
2402 * no padding and we only parse for %i %d and %+char
2403 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002404static char *tgoto __ARGS((char *, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002406 static char *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407tgoto(cm, x, y)
2408 char *cm;
2409 int x, y;
2410{
2411 static char buf[30];
2412 char *p, *s, *e;
2413
2414 if (!cm)
2415 return "OOPS";
2416 e = buf + 29;
2417 for (s = buf; s < e && *cm; cm++)
2418 {
2419 if (*cm != '%')
2420 {
2421 *s++ = *cm;
2422 continue;
2423 }
2424 switch (*++cm)
2425 {
2426 case 'd':
2427 p = (char *)tltoa((unsigned long)y);
2428 y = x;
2429 while (*p)
2430 *s++ = *p++;
2431 break;
2432 case 'i':
2433 x++;
2434 y++;
2435 break;
2436 case '+':
2437 *s++ = (char)(*++cm + y);
2438 y = x;
2439 break;
2440 case '%':
2441 *s++ = *cm;
2442 break;
2443 default:
2444 return "OOPS";
2445 }
2446 }
2447 *s = '\0';
2448 return buf;
2449}
2450
2451#endif /* HAVE_TGETENT */
2452
2453/*
2454 * Set the terminal name and initialize the terminal options.
2455 * If "name" is NULL or empty, get the terminal name from the environment.
2456 * If that fails, use the default terminal name.
2457 */
2458 void
2459termcapinit(name)
2460 char_u *name;
2461{
2462 char_u *term;
2463
2464 if (name != NULL && *name == NUL)
2465 name = NULL; /* empty name is equal to no name */
2466 term = name;
2467
2468#ifdef __BEOS__
2469 /*
2470 * TERM environment variable is normally set to 'ansi' on the Bebox;
2471 * Since the BeBox doesn't quite support full ANSI yet, we use our
2472 * own custom 'ansi-beos' termcap instead, unless the -T option has
2473 * been given on the command line.
2474 */
2475 if (term == NULL
2476 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2477 term = DEFAULT_TERM;
2478#endif
2479#ifndef MSWIN
2480 if (term == NULL)
2481 term = mch_getenv((char_u *)"TERM");
2482#endif
2483 if (term == NULL || *term == NUL)
2484 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002485 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
2487 /* Set the default terminal name. */
2488 set_string_default("term", term);
2489 set_string_default("ttytype", term);
2490
2491 /*
2492 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2493 */
2494 set_termname(T_NAME != NULL ? T_NAME : term);
2495}
2496
2497/*
2498 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2499 */
2500#ifdef DOS16
2501# define OUT_SIZE 255 /* only have 640K total... */
2502#else
2503# ifdef FEAT_GUI_W16
2504# define OUT_SIZE 1023 /* Save precious 1K near data */
2505# else
2506# define OUT_SIZE 2047
2507# endif
2508#endif
2509 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2510static char_u out_buf[OUT_SIZE + 1];
2511static int out_pos = 0; /* number of chars in out_buf */
2512
2513/*
2514 * out_flush(): flush the output buffer
2515 */
2516 void
2517out_flush()
2518{
2519 int len;
2520
2521 if (out_pos != 0)
2522 {
2523 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2524 len = out_pos;
2525 out_pos = 0;
2526 ui_write(out_buf, len);
2527 }
2528}
2529
2530#if defined(FEAT_MBYTE) || defined(PROTO)
2531/*
2532 * Sometimes a byte out of a multi-byte character is written with out_char().
2533 * To avoid flushing half of the character, call this function first.
2534 */
2535 void
2536out_flush_check()
2537{
2538 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2539 out_flush();
2540}
2541#endif
2542
2543#ifdef FEAT_GUI
2544/*
2545 * out_trash(): Throw away the contents of the output buffer
2546 */
2547 void
2548out_trash()
2549{
2550 out_pos = 0;
2551}
2552#endif
2553
2554/*
2555 * out_char(c): put a byte into the output buffer.
2556 * Flush it if it becomes full.
2557 * This should not be used for outputting text on the screen (use functions
2558 * like msg_puts() and screen_putchar() for that).
2559 */
2560 void
2561out_char(c)
2562 unsigned c;
2563{
2564#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2565 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2566 out_char('\r');
2567#endif
2568
2569 out_buf[out_pos++] = c;
2570
2571 /* For testing we flush each time. */
2572 if (out_pos >= OUT_SIZE || p_wd)
2573 out_flush();
2574}
2575
2576static void out_char_nf __ARGS((unsigned));
2577
2578/*
2579 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2580 */
2581 static void
2582out_char_nf(c)
2583 unsigned c;
2584{
2585#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2586 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2587 out_char_nf('\r');
2588#endif
2589
2590 out_buf[out_pos++] = c;
2591
2592 if (out_pos >= OUT_SIZE)
2593 out_flush();
2594}
2595
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002596#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2597 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598/*
2599 * A never-padding out_str.
2600 * use this whenever you don't want to run the string through tputs.
2601 * tputs above is harmless, but tputs from the termcap library
2602 * is likely to strip off leading digits, that it mistakes for padding
2603 * information, and "%i", "%d", etc.
2604 * This should only be used for writing terminal codes, not for outputting
2605 * normal text (use functions like msg_puts() and screen_putchar() for that).
2606 */
2607 void
2608out_str_nf(s)
2609 char_u *s;
2610{
2611 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2612 out_flush();
2613 while (*s)
2614 out_char_nf(*s++);
2615
2616 /* For testing we write one string at a time. */
2617 if (p_wd)
2618 out_flush();
2619}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
2622/*
2623 * out_str(s): Put a character string a byte at a time into the output buffer.
2624 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2625 * This should only be used for writing terminal codes, not for outputting
2626 * normal text (use functions like msg_puts() and screen_putchar() for that).
2627 */
2628 void
2629out_str(s)
2630 char_u *s;
2631{
2632 if (s != NULL && *s)
2633 {
2634#ifdef FEAT_GUI
2635 /* Don't use tputs() when GUI is used, ncurses crashes. */
2636 if (gui.in_use)
2637 {
2638 out_str_nf(s);
2639 return;
2640 }
2641#endif
2642 /* avoid terminal strings being split up */
2643 if (out_pos > OUT_SIZE - 20)
2644 out_flush();
2645#ifdef HAVE_TGETENT
2646 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2647#else
2648 while (*s)
2649 out_char_nf(*s++);
2650#endif
2651
2652 /* For testing we write one string at a time. */
2653 if (p_wd)
2654 out_flush();
2655 }
2656}
2657
2658/*
2659 * cursor positioning using termcap parser. (jw)
2660 */
2661 void
2662term_windgoto(row, col)
2663 int row;
2664 int col;
2665{
2666 OUT_STR(tgoto((char *)T_CM, col, row));
2667}
2668
2669 void
2670term_cursor_right(i)
2671 int i;
2672{
2673 OUT_STR(tgoto((char *)T_CRI, 0, i));
2674}
2675
2676 void
2677term_append_lines(line_count)
2678 int line_count;
2679{
2680 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2681}
2682
2683 void
2684term_delete_lines(line_count)
2685 int line_count;
2686{
2687 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2688}
2689
2690#if defined(HAVE_TGETENT) || defined(PROTO)
2691 void
2692term_set_winpos(x, y)
2693 int x;
2694 int y;
2695{
2696 /* Can't handle a negative value here */
2697 if (x < 0)
2698 x = 0;
2699 if (y < 0)
2700 y = 0;
2701 OUT_STR(tgoto((char *)T_CWP, y, x));
2702}
2703
2704 void
2705term_set_winsize(width, height)
2706 int width;
2707 int height;
2708{
2709 OUT_STR(tgoto((char *)T_CWS, height, width));
2710}
2711#endif
2712
2713 void
2714term_fg_color(n)
2715 int n;
2716{
2717 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2718 if (*T_CAF)
2719 term_color(T_CAF, n);
2720 else if (*T_CSF)
2721 term_color(T_CSF, n);
2722}
2723
2724 void
2725term_bg_color(n)
2726 int n;
2727{
2728 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2729 if (*T_CAB)
2730 term_color(T_CAB, n);
2731 else if (*T_CSB)
2732 term_color(T_CSB, n);
2733}
2734
2735 static void
2736term_color(s, n)
2737 char_u *s;
2738 int n;
2739{
2740 char buf[20];
2741 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2742
2743 /* Special handling of 16 colors, because termcap can't handle it */
2744 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2745 /* Also accept CSI instead of <Esc>[ */
2746 if (n >= 8 && t_colors >= 16
2747 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2748 && s[i] != NUL
2749 && (STRCMP(s + i + 1, "%p1%dm") == 0
2750 || STRCMP(s + i + 1, "%dm") == 0)
2751 && (s[i] == '3' || s[i] == '4'))
2752 {
2753 sprintf(buf,
2754#ifdef TERMINFO
2755 "%s%s%%p1%%dm",
2756#else
2757 "%s%s%%dm",
2758#endif
2759 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2760 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2761 : (n >= 16 ? "48;5;" : "10"));
2762 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2763 }
2764 else
2765 OUT_STR(tgoto((char *)s, 0, n));
2766}
2767
2768#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X))) || defined(PROTO)
2769/*
2770 * Generic function to set window title, using t_ts and t_fs.
2771 */
2772 void
2773term_settitle(title)
2774 char_u *title;
2775{
2776 /* t_ts takes one argument: column in status line */
2777 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2778 out_str_nf(title);
2779 out_str(T_FS); /* set title end */
2780 out_flush();
2781}
2782#endif
2783
2784/*
2785 * Make sure we have a valid set or terminal options.
2786 * Replace all entries that are NULL by empty_option
2787 */
2788 void
2789ttest(pairs)
2790 int pairs;
2791{
2792 check_options(); /* make sure no options are NULL */
2793
2794 /*
2795 * MUST have "cm": cursor motion.
2796 */
2797 if (*T_CM == NUL)
2798 EMSG(_("E437: terminal capability \"cm\" required"));
2799
2800 /*
2801 * if "cs" defined, use a scroll region, it's faster.
2802 */
2803 if (*T_CS != NUL)
2804 scroll_region = TRUE;
2805 else
2806 scroll_region = FALSE;
2807
2808 if (pairs)
2809 {
2810 /*
2811 * optional pairs
2812 */
2813 /* TP goes to normal mode for TI (invert) and TB (bold) */
2814 if (*T_ME == NUL)
2815 T_ME = T_MR = T_MD = T_MB = empty_option;
2816 if (*T_SO == NUL || *T_SE == NUL)
2817 T_SO = T_SE = empty_option;
2818 if (*T_US == NUL || *T_UE == NUL)
2819 T_US = T_UE = empty_option;
2820 if (*T_CZH == NUL || *T_CZR == NUL)
2821 T_CZH = T_CZR = empty_option;
2822
2823 /* T_VE is needed even though T_VI is not defined */
2824 if (*T_VE == NUL)
2825 T_VI = empty_option;
2826
2827 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2828 if (*T_ME == NUL)
2829 {
2830 T_ME = T_SE;
2831 T_MR = T_SO;
2832 T_MD = T_SO;
2833 }
2834
2835 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2836 if (*T_SO == NUL)
2837 {
2838 T_SE = T_ME;
2839 if (*T_MR == NUL)
2840 T_SO = T_MD;
2841 else
2842 T_SO = T_MR;
2843 }
2844
2845 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2846 if (*T_CZH == NUL)
2847 {
2848 T_CZR = T_ME;
2849 if (*T_MR == NUL)
2850 T_CZH = T_MD;
2851 else
2852 T_CZH = T_MR;
2853 }
2854
2855 /* "Sb" and "Sf" come in pairs */
2856 if (*T_CSB == NUL || *T_CSF == NUL)
2857 {
2858 T_CSB = empty_option;
2859 T_CSF = empty_option;
2860 }
2861
2862 /* "AB" and "AF" come in pairs */
2863 if (*T_CAB == NUL || *T_CAF == NUL)
2864 {
2865 T_CAB = empty_option;
2866 T_CAF = empty_option;
2867 }
2868
2869 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2870 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002871 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
2873 /* Set 'weirdinvert' according to value of 't_xs' */
2874 p_wiv = (*T_XS != NUL);
2875 }
2876 need_gather = TRUE;
2877
2878 /* Set t_colors to the value of t_Co. */
2879 t_colors = atoi((char *)T_CCO);
2880}
2881
2882#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2883 || defined(PROTO)
2884/*
2885 * Represent the given long_u as individual bytes, with the most significant
2886 * byte first, and store them in dst.
2887 */
2888 void
2889add_long_to_buf(val, dst)
2890 long_u val;
2891 char_u *dst;
2892{
2893 int i;
2894 int shift;
2895
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002896 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 {
2898 shift = 8 * (sizeof(long_u) - i);
2899 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2900 }
2901}
2902
2903static int get_long_from_buf __ARGS((char_u *buf, long_u *val));
2904
2905/*
2906 * Interpret the next string of bytes in buf as a long integer, with the most
2907 * significant byte first. Note that it is assumed that buf has been through
2908 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
2909 * Puts result in val, and returns the number of bytes read from buf
2910 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
2911 * were present.
2912 */
2913 static int
2914get_long_from_buf(buf, val)
2915 char_u *buf;
2916 long_u *val;
2917{
2918 int len;
2919 char_u bytes[sizeof(long_u)];
2920 int i;
2921 int shift;
2922
2923 *val = 0;
2924 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
2925 if (len != -1)
2926 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002927 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 {
2929 shift = 8 * (sizeof(long_u) - 1 - i);
2930 *val += (long_u)bytes[i] << shift;
2931 }
2932 }
2933 return len;
2934}
2935#endif
2936
2937#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002938 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
2939 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940/*
2941 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
2942 * that buf has been through inchar(). Returns the actual number of bytes used
2943 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
2944 * available.
2945 */
2946 static int
2947get_bytes_from_buf(buf, bytes, num_bytes)
2948 char_u *buf;
2949 char_u *bytes;
2950 int num_bytes;
2951{
2952 int len = 0;
2953 int i;
2954 char_u c;
2955
2956 for (i = 0; i < num_bytes; i++)
2957 {
2958 if ((c = buf[len++]) == NUL)
2959 return -1;
2960 if (c == K_SPECIAL)
2961 {
2962 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
2963 return -1;
2964 if (buf[len++] == (int)KS_ZERO)
2965 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02002966 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
2967 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
2968 if (buf[len++] == (int)KE_CSI)
2969 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00002971 else if (c == CSI && buf[len] == KS_EXTRA
2972 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002973 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
2974 * the start of a special key, see add_to_input_buf_csi(). */
2975 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 bytes[i] = c;
2977 }
2978 return len;
2979}
2980#endif
2981
2982/*
Bram Moolenaare057d402013-06-30 17:51:51 +02002983 * Check if the new shell size is valid, correct it if it's too small or way
2984 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 */
2986 void
2987check_shellsize()
2988{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 if (Rows < min_rows()) /* need room for one window and command line */
2990 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02002991 limit_screen_size();
2992}
2993
2994/*
2995 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
2996 */
2997 void
2998limit_screen_size()
2999{
3000 if (Columns < MIN_COLUMNS)
3001 Columns = MIN_COLUMNS;
3002 else if (Columns > 10000)
3003 Columns = 10000;
3004 if (Rows > 1000)
3005 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006}
3007
Bram Moolenaar86b68352004-12-27 21:59:20 +00003008/*
3009 * Invoked just before the screen structures are going to be (re)allocated.
3010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 void
3012win_new_shellsize()
3013{
3014 static int old_Rows = 0;
3015 static int old_Columns = 0;
3016
3017 if (old_Rows != Rows || old_Columns != Columns)
3018 ui_new_shellsize();
3019 if (old_Rows != Rows)
3020 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003021 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003022 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003023 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 old_Rows = Rows;
3025 shell_new_rows(); /* update window sizes */
3026 }
3027 if (old_Columns != Columns)
3028 {
3029 old_Columns = Columns;
3030#ifdef FEAT_VERTSPLIT
3031 shell_new_columns(); /* update window sizes */
3032#endif
3033 }
3034}
3035
3036/*
3037 * Call this function when the Vim shell has been resized in any way.
3038 * Will obtain the current size and redraw (also when size didn't change).
3039 */
3040 void
3041shell_resized()
3042{
3043 set_shellsize(0, 0, FALSE);
3044}
3045
3046/*
3047 * Check if the shell size changed. Handle a resize.
3048 * When the size didn't change, nothing happens.
3049 */
3050 void
3051shell_resized_check()
3052{
3053 int old_Rows = Rows;
3054 int old_Columns = Columns;
3055
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003056 if (!exiting
3057#ifdef FEAT_GUI
3058 /* Do not get the size when executing a shell command during
3059 * startup. */
3060 && !gui.starting
3061#endif
3062 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003063 {
3064 (void)ui_get_shellsize();
3065 check_shellsize();
3066 if (old_Rows != Rows || old_Columns != Columns)
3067 shell_resized();
3068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069}
3070
3071/*
3072 * Set size of the Vim shell.
3073 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3074 * window size (this is used for the :win command).
3075 * If 'mustset' is FALSE, we may try to get the real window size and if
3076 * it fails use 'width' and 'height'.
3077 */
3078 void
3079set_shellsize(width, height, mustset)
3080 int width, height;
3081 int mustset;
3082{
3083 static int busy = FALSE;
3084
3085 /*
3086 * Avoid recursiveness, can happen when setting the window size causes
3087 * another window-changed signal.
3088 */
3089 if (busy)
3090 return;
3091
3092 if (width < 0 || height < 0) /* just checking... */
3093 return;
3094
Bram Moolenaara971b822011-09-14 14:43:25 +02003095 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003097 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 State = SETWSIZE;
3099 return;
3100 }
3101
Bram Moolenaara971b822011-09-14 14:43:25 +02003102 /* curwin->w_buffer can be NULL when we are closing a window and the
3103 * buffer has already been closed and removing a scrollbar causes a resize
3104 * event. Don't resize then, it will happen after entering another buffer.
3105 */
3106 if (curwin->w_buffer == NULL)
3107 return;
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 ++busy;
3110
3111#ifdef AMIGA
3112 out_flush(); /* must do this before mch_get_shellsize() for
3113 some obscure reason */
3114#endif
3115
3116 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3117 {
3118 Rows = height;
3119 Columns = width;
3120 check_shellsize();
3121 ui_set_shellsize(mustset);
3122 }
3123 else
3124 check_shellsize();
3125
3126 /* The window layout used to be adjusted here, but it now happens in
3127 * screenalloc() (also invoked from screenclear()). That is because the
3128 * "busy" check above may skip this, but not screenalloc(). */
3129
3130 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3131 screenclear();
3132 else
3133 screen_start(); /* don't know where cursor is now */
3134
3135 if (starting != NO_SCREEN)
3136 {
3137#ifdef FEAT_TITLE
3138 maketitle();
3139#endif
3140 changed_line_abv_curs();
3141 invalidate_botline();
3142
3143 /*
3144 * We only redraw when it's needed:
3145 * - While at the more prompt or executing an external command, don't
3146 * redraw, but position the cursor.
3147 * - While editing the command line, only redraw that.
3148 * - in Ex mode, don't redraw anything.
3149 * - Otherwise, redraw right now, and position the cursor.
3150 * Always need to call update_screen() or screenalloc(), to make
3151 * sure Rows/Columns and the size of ScreenLines[] is correct!
3152 */
3153 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3154 || exmode_active)
3155 {
3156 screenalloc(FALSE);
3157 repeat_message();
3158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 else
3160 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003161#ifdef FEAT_SCROLLBIND
3162 if (curwin->w_p_scb)
3163 do_check_scrollbind(TRUE);
3164#endif
3165 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003166 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003167 update_screen(NOT_VALID);
3168 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003169 }
3170 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003171 {
3172 update_topline();
3173#if defined(FEAT_INS_EXPAND)
3174 if (pum_visible())
3175 {
3176 redraw_later(NOT_VALID);
3177 ins_compl_show_pum(); /* This includes the redraw. */
3178 }
3179 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003180#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003181 update_screen(NOT_VALID);
3182 if (redrawing())
3183 setcursor();
3184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 }
3186 cursor_on(); /* redrawing may have switched it off */
3187 }
3188 out_flush();
3189 --busy;
3190}
3191
3192/*
3193 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3194 * commands and Ex mode).
3195 */
3196 void
3197settmode(tmode)
3198 int tmode;
3199{
3200#ifdef FEAT_GUI
3201 /* don't set the term where gvim was started to any mode */
3202 if (gui.in_use)
3203 return;
3204#endif
3205
3206 if (full_screen)
3207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 /*
3209 * When returning after calling a shell we want to really set the
3210 * terminal to raw mode, even though we think it already is, because
3211 * the shell program may have reset the terminal mode.
3212 * When we think the terminal is normal, don't try to set it to
3213 * normal again, because that causes problems (logout!) on some
3214 * machines.
3215 */
3216 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3217 {
3218#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003219# ifdef FEAT_GUI
3220 if (!gui.in_use && !gui.starting)
3221# endif
3222 {
3223 /* May need to check for T_CRV response and termcodes, it
3224 * doesn't work in Cooked mode, an external program may get
3225 * them. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003226 if (tmode != TMODE_RAW && (crv_status == CRV_SENT
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003227 || u7_status == U7_SENT
3228 || rbg_status == RBG_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003229 (void)vpeekc_nomap();
3230 check_for_codes_from_term();
3231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232#endif
3233#ifdef FEAT_MOUSE_TTY
3234 if (tmode != TMODE_RAW)
3235 mch_setmouse(FALSE); /* switch mouse off */
3236#endif
3237 out_flush();
3238 mch_settmode(tmode); /* machine specific function */
3239 cur_tmode = tmode;
3240#ifdef FEAT_MOUSE
3241 if (tmode == TMODE_RAW)
3242 setmouse(); /* may switch mouse on */
3243#endif
3244 out_flush();
3245 }
3246#ifdef FEAT_TERMRESPONSE
3247 may_req_termresponse();
3248#endif
3249 }
3250}
3251
3252 void
3253starttermcap()
3254{
3255 if (full_screen && !termcap_active)
3256 {
3257 out_str(T_TI); /* start termcap mode */
3258 out_str(T_KS); /* start "keypad transmit" mode */
3259 out_flush();
3260 termcap_active = TRUE;
3261 screen_start(); /* don't know where cursor is now */
3262#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003263# ifdef FEAT_GUI
3264 if (!gui.in_use && !gui.starting)
3265# endif
3266 {
3267 may_req_termresponse();
3268 /* Immediately check for a response. If t_Co changes, we don't
3269 * want to redraw with wrong colors first. */
Bram Moolenaar90013c62014-05-22 18:14:31 +02003270 if (crv_status == CRV_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003271 check_for_codes_from_term();
3272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273#endif
3274 }
3275}
3276
3277 void
3278stoptermcap()
3279{
3280 screen_stop_highlight();
3281 reset_cterm_colors();
3282 if (termcap_active)
3283 {
3284#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003285# ifdef FEAT_GUI
3286 if (!gui.in_use && !gui.starting)
3287# endif
3288 {
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003289 /* May need to discard T_CRV, T_U7 or T_RBG response. */
3290 if (crv_status == CRV_SENT || u7_status == U7_SENT
3291 || rbg_status == RBG_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003292 {
3293# ifdef UNIX
3294 /* Give the terminal a chance to respond. */
3295 mch_delay(100L, FALSE);
3296# endif
3297# ifdef TCIFLUSH
3298 /* Discard data received but not read. */
3299 if (exiting)
3300 tcflush(fileno(stdin), TCIFLUSH);
3301# endif
3302 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003303 /* Check for termcodes first, otherwise an external program may
3304 * get them. */
3305 check_for_codes_from_term();
3306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#endif
3308 out_str(T_KE); /* stop "keypad transmit" mode */
3309 out_flush();
3310 termcap_active = FALSE;
3311 cursor_on(); /* just in case it is still off */
3312 out_str(T_TE); /* stop termcap mode */
3313 screen_start(); /* don't know where cursor is now */
3314 out_flush();
3315 }
3316}
3317
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003318#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319/*
3320 * Request version string (for xterm) when needed.
3321 * Only do this after switching to raw mode, otherwise the result will be
3322 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003323 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003324 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 * Only do this after termcap mode has been started, otherwise the codes for
3326 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003327 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3328 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003329 * On Unix only do it when both output and input are a tty (avoid writing
3330 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 * The result is caught in check_termcode().
3332 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003333 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334may_req_termresponse()
3335{
3336 if (crv_status == CRV_GET
3337 && cur_tmode == TMODE_RAW
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003338 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 && termcap_active
Bram Moolenaarebefac62005-12-28 22:39:57 +00003340 && p_ek
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003341# ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 && isatty(1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003343 && isatty(read_cmd_fd)
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 && *T_CRV != NUL)
3346 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02003347 LOG_TR("Sending CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 out_str(T_CRV);
3349 crv_status = CRV_SENT;
3350 /* check for the characters now, otherwise they might be eaten by
3351 * get_keystroke() */
3352 out_flush();
3353 (void)vpeekc_nomap();
3354 }
3355}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003356
3357# if defined(FEAT_MBYTE) || defined(PROTO)
3358/*
3359 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003360 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003361 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003362 * If the terminal treats \u25bd as single width, the position is (1, 1),
3363 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003364 * This function has the side effect that changes cursor position, so
3365 * it must be called immediately after entering termcap mode.
3366 */
3367 void
Bram Moolenaar386dcde2013-09-29 16:27:47 +02003368may_req_ambiguous_char_width()
Bram Moolenaar9584b312013-03-13 19:29:28 +01003369{
3370 if (u7_status == U7_GET
3371 && cur_tmode == TMODE_RAW
3372 && termcap_active
3373 && p_ek
3374# ifdef UNIX
3375 && isatty(1)
3376 && isatty(read_cmd_fd)
3377# endif
3378 && *T_U7 != NUL
3379 && !option_was_set((char_u *)"ambiwidth"))
3380 {
3381 char_u buf[16];
3382
Bram Moolenaar2951b772013-07-03 12:45:31 +02003383 LOG_TR("Sending U7 request");
3384 /* Do this in the second row. In the first row the returned sequence
3385 * may be CSI 1;2R, which is the same as <S-F3>. */
3386 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003387 buf[mb_char2bytes(0x25bd, buf)] = 0;
3388 out_str(buf);
3389 out_str(T_U7);
3390 u7_status = U7_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003391 out_flush();
3392 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003393 out_str((char_u *)" ");
3394 term_windgoto(0, 0);
3395 /* check for the characters now, otherwise they might be eaten by
3396 * get_keystroke() */
3397 out_flush();
3398 (void)vpeekc_nomap();
3399 }
3400}
3401# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003402
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003403#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
3404/*
Bram Moolenaar4921c242015-06-27 18:34:24 +02003405 * Similar to requesting the version string: Request the terminal background
3406 * color when it is the right moment.
Bram Moolenaarb5c32652015-06-25 17:03:36 +02003407 */
3408 void
3409may_req_bg_color()
3410{
3411 if (rbg_status == RBG_GET
3412 && cur_tmode == TMODE_RAW
3413 && termcap_active
3414 && p_ek
3415# ifdef UNIX
3416 && isatty(1)
3417 && isatty(read_cmd_fd)
3418# endif
3419 && *T_RBG != NUL
3420 && !option_was_set((char_u *)"bg"))
3421 {
3422 LOG_TR("Sending BG request");
3423 out_str(T_RBG);
3424 rbg_status = RBG_SENT;
3425 /* check for the characters now, otherwise they might be eaten by
3426 * get_keystroke() */
3427 out_flush();
3428 (void)vpeekc_nomap();
3429 }
3430}
3431# endif
3432
Bram Moolenaar2951b772013-07-03 12:45:31 +02003433# ifdef DEBUG_TERMRESPONSE
3434 static void
3435log_tr(char *msg)
3436{
3437 static FILE *fd_tr = NULL;
3438 static proftime_T start;
3439 proftime_T now;
3440
3441 if (fd_tr == NULL)
3442 {
3443 fd_tr = fopen("termresponse.log", "w");
3444 profile_start(&start);
3445 }
3446 now = start;
3447 profile_end(&now);
3448 fprintf(fd_tr, "%s: %s %s\n",
3449 profile_msg(&now),
3450 must_redraw == NOT_VALID ? "NV"
3451 : must_redraw == CLEAR ? "CL" : " ",
3452 msg);
3453}
3454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455#endif
3456
3457/*
3458 * Return TRUE when saving and restoring the screen.
3459 */
3460 int
3461swapping_screen()
3462{
3463 return (full_screen && *T_TI != NUL);
3464}
3465
3466#ifdef FEAT_MOUSE
3467/*
3468 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3469 */
3470 void
3471setmouse()
3472{
3473# ifdef FEAT_MOUSE_TTY
3474 int checkfor;
3475# endif
3476
3477# ifdef FEAT_MOUSESHAPE
3478 update_mouseshape(-1);
3479# endif
3480
3481# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3482# ifdef FEAT_GUI
3483 /* In the GUI the mouse is always enabled. */
3484 if (gui.in_use)
3485 return;
3486# endif
3487 /* be quick when mouse is off */
3488 if (*p_mouse == NUL || has_mouse_termcode == 0)
3489 return;
3490
3491 /* don't switch mouse on when not in raw mode (Ex mode) */
3492 if (cur_tmode != TMODE_RAW)
3493 {
3494 mch_setmouse(FALSE);
3495 return;
3496 }
3497
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 if (VIsual_active)
3499 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003500 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 checkfor = MOUSE_RETURN;
3502 else if (State & INSERT)
3503 checkfor = MOUSE_INSERT;
3504 else if (State & CMDLINE)
3505 checkfor = MOUSE_COMMAND;
3506 else if (State == CONFIRM || State == EXTERNCMD)
3507 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3508 else
3509 checkfor = MOUSE_NORMAL; /* assume normal mode */
3510
3511 if (mouse_has(checkfor))
3512 mch_setmouse(TRUE);
3513 else
3514 mch_setmouse(FALSE);
3515# endif
3516}
3517
3518/*
3519 * Return TRUE if
3520 * - "c" is in 'mouse', or
3521 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3522 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3523 * normal editing mode (not at hit-return message).
3524 */
3525 int
3526mouse_has(c)
3527 int c;
3528{
3529 char_u *p;
3530
3531 for (p = p_mouse; *p; ++p)
3532 switch (*p)
3533 {
3534 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3535 return TRUE;
3536 break;
3537 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3538 return TRUE;
3539 break;
3540 default: if (c == *p) return TRUE; break;
3541 }
3542 return FALSE;
3543}
3544
3545/*
3546 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3547 */
3548 int
3549mouse_model_popup()
3550{
3551 return (p_mousem[0] == 'p');
3552}
3553#endif
3554
3555/*
3556 * By outputting the 'cursor very visible' termcap code, for some windowed
3557 * terminals this makes the screen scrolled to the correct position.
3558 * Used when starting Vim or returning from a shell.
3559 */
3560 void
3561scroll_start()
3562{
3563 if (*T_VS != NUL)
3564 {
3565 out_str(T_VS);
3566 out_str(T_VE);
3567 screen_start(); /* don't know where cursor is now */
3568 }
3569}
3570
3571static int cursor_is_off = FALSE;
3572
3573/*
3574 * Enable the cursor.
3575 */
3576 void
3577cursor_on()
3578{
3579 if (cursor_is_off)
3580 {
3581 out_str(T_VE);
3582 cursor_is_off = FALSE;
3583 }
3584}
3585
3586/*
3587 * Disable the cursor.
3588 */
3589 void
3590cursor_off()
3591{
3592 if (full_screen)
3593 {
3594 if (!cursor_is_off)
3595 out_str(T_VI); /* disable cursor */
3596 cursor_is_off = TRUE;
3597 }
3598}
3599
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003600#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003602 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003603 */
3604 void
3605term_cursor_shape()
3606{
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003607 static int showing_mode = NORMAL;
3608 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003609
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003610 /* Only do something when redrawing the screen and we can restore the
3611 * mode. */
3612 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003613 return;
3614
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003615 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003616 {
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003617 if (showing_mode != REPLACE)
3618 {
3619 if (*T_CSR != NUL)
3620 p = T_CSR; /* Replace mode cursor */
3621 else
3622 p = T_CSI; /* fall back to Insert mode cursor */
3623 if (*p != NUL)
3624 {
3625 out_str(p);
3626 showing_mode = REPLACE;
3627 }
3628 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003629 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003630 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003631 {
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003632 if (showing_mode != INSERT && *T_CSI != NUL)
3633 {
3634 out_str(T_CSI); /* Insert mode cursor */
3635 showing_mode = INSERT;
3636 }
3637 }
3638 else if (showing_mode != NORMAL)
3639 {
3640 out_str(T_CEI); /* non-Insert mode cursor */
3641 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003642 }
3643}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003644#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003645
3646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 * Set scrolling region for window 'wp'.
3648 * The region starts 'off' lines from the start of the window.
3649 * Also set the vertical scroll region for a vertically split window. Always
3650 * the full width of the window, excluding the vertical separator.
3651 */
3652 void
3653scroll_region_set(wp, off)
3654 win_T *wp;
3655 int off;
3656{
3657 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3658 W_WINROW(wp) + off));
3659#ifdef FEAT_VERTSPLIT
3660 if (*T_CSV != NUL && wp->w_width != Columns)
3661 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3662 W_WINCOL(wp)));
3663#endif
3664 screen_start(); /* don't know where cursor is now */
3665}
3666
3667/*
3668 * Reset scrolling region to the whole screen.
3669 */
3670 void
3671scroll_region_reset()
3672{
3673 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
3674#ifdef FEAT_VERTSPLIT
3675 if (*T_CSV != NUL)
3676 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3677#endif
3678 screen_start(); /* don't know where cursor is now */
3679}
3680
3681
3682/*
3683 * List of terminal codes that are currently recognized.
3684 */
3685
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003686static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
3688 char_u name[2]; /* termcap name of entry */
3689 char_u *code; /* terminal code (in allocated memory) */
3690 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003691 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692} *termcodes = NULL;
3693
3694static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3695static int tc_len = 0; /* current number of entries in termcodes[] */
3696
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003697static int termcode_star __ARGS((char_u *code, int len));
3698
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 void
3700clear_termcodes()
3701{
3702 while (tc_len > 0)
3703 vim_free(termcodes[--tc_len].code);
3704 vim_free(termcodes);
3705 termcodes = NULL;
3706 tc_max_len = 0;
3707
3708#ifdef HAVE_TGETENT
3709 BC = (char *)empty_option;
3710 UP = (char *)empty_option;
3711 PC = NUL; /* set pad character to NUL */
3712 ospeed = 0;
3713#endif
3714
3715 need_gather = TRUE; /* need to fill termleader[] */
3716}
3717
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003718#define ATC_FROM_TERM 55
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720/*
3721 * Add a new entry to the list of terminal codes.
3722 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003723 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3724 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 */
3726 void
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003727add_termcode(name, string, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 char_u *name;
3729 char_u *string;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003730 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731{
3732 struct termcode *new_tc;
3733 int i, j;
3734 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003735 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
3737 if (string == NULL || *string == NUL)
3738 {
3739 del_termcode(name);
3740 return;
3741 }
3742
Bram Moolenaar45500912014-07-09 20:51:07 +02003743#if defined(WIN3264) && !defined(FEAT_GUI)
3744 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3745#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 if (s == NULL)
3749 return;
3750
3751 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003752 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003754 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 s[0] = term_7to8bit(string);
3756 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003757
3758#if defined(WIN3264) && !defined(FEAT_GUI)
3759 if (s[0] == K_NUL)
3760 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003761 STRMOVE(s + 1, s);
3762 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003763 }
3764#endif
3765
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003766 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767
3768 need_gather = TRUE; /* need to fill termleader[] */
3769
3770 /*
3771 * need to make space for more entries
3772 */
3773 if (tc_len == tc_max_len)
3774 {
3775 tc_max_len += 20;
3776 new_tc = (struct termcode *)alloc(
3777 (unsigned)(tc_max_len * sizeof(struct termcode)));
3778 if (new_tc == NULL)
3779 {
3780 tc_max_len -= 20;
3781 return;
3782 }
3783 for (i = 0; i < tc_len; ++i)
3784 new_tc[i] = termcodes[i];
3785 vim_free(termcodes);
3786 termcodes = new_tc;
3787 }
3788
3789 /*
3790 * Look for existing entry with the same name, it is replaced.
3791 * Look for an existing entry that is alphabetical higher, the new entry
3792 * is inserted in front of it.
3793 */
3794 for (i = 0; i < tc_len; ++i)
3795 {
3796 if (termcodes[i].name[0] < name[0])
3797 continue;
3798 if (termcodes[i].name[0] == name[0])
3799 {
3800 if (termcodes[i].name[1] < name[1])
3801 continue;
3802 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003803 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 */
3805 if (termcodes[i].name[1] == name[1])
3806 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003807 if (flags == ATC_FROM_TERM && (j = termcode_star(
3808 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003809 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003810 /* Don't replace ESC[123;*X or ESC O*X with another when
3811 * invoked from got_code_from_term(). */
3812 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003813 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003814 && s[len - 1]
3815 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003816 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003817 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003818 vim_free(s);
3819 return;
3820 }
3821 }
3822 else
3823 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003824 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003825 vim_free(termcodes[i].code);
3826 --tc_len;
3827 break;
3828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830 }
3831 /*
3832 * Found alphabetical larger entry, move rest to insert new entry
3833 */
3834 for (j = tc_len; j > i; --j)
3835 termcodes[j] = termcodes[j - 1];
3836 break;
3837 }
3838
3839 termcodes[i].name[0] = name[0];
3840 termcodes[i].name[1] = name[1];
3841 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003842 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003843
3844 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3845 * accept modifiers. */
3846 termcodes[i].modlen = 0;
3847 j = termcode_star(s, len);
3848 if (j > 0)
3849 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 ++tc_len;
3851}
3852
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003853/*
3854 * Check termcode "code[len]" for ending in ;*X, <Esc>O*X or <M-O>*X.
3855 * The "X" can be any character.
3856 * Return 0 if not found, 2 for ;*X and 1 for O*X and <M-O>*X.
3857 */
3858 static int
3859termcode_star(code, len)
3860 char_u *code;
3861 int len;
3862{
3863 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3864 if (len >= 3 && code[len - 2] == '*')
3865 {
3866 if (len >= 5 && code[len - 3] == ';')
3867 return 2;
3868 if ((len >= 4 && code[len - 3] == 'O') || code[len - 3] == 'O' + 128)
3869 return 1;
3870 }
3871 return 0;
3872}
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 char_u *
3875find_termcode(name)
3876 char_u *name;
3877{
3878 int i;
3879
3880 for (i = 0; i < tc_len; ++i)
3881 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3882 return termcodes[i].code;
3883 return NULL;
3884}
3885
3886#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3887 char_u *
3888get_termcode(i)
3889 int i;
3890{
3891 if (i >= tc_len)
3892 return NULL;
3893 return &termcodes[i].name[0];
3894}
3895#endif
3896
3897 void
3898del_termcode(name)
3899 char_u *name;
3900{
3901 int i;
3902
3903 if (termcodes == NULL) /* nothing there yet */
3904 return;
3905
3906 need_gather = TRUE; /* need to fill termleader[] */
3907
3908 for (i = 0; i < tc_len; ++i)
3909 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3910 {
3911 del_termcode_idx(i);
3912 return;
3913 }
3914 /* not found. Give error message? */
3915}
3916
3917 static void
3918del_termcode_idx(idx)
3919 int idx;
3920{
3921 int i;
3922
3923 vim_free(termcodes[idx].code);
3924 --tc_len;
3925 for (i = idx; i < tc_len; ++i)
3926 termcodes[i] = termcodes[i + 1];
3927}
3928
3929#ifdef FEAT_TERMRESPONSE
3930/*
3931 * Called when detected that the terminal sends 8-bit codes.
3932 * Convert all 7-bit codes to their 8-bit equivalent.
3933 */
3934 static void
3935switch_to_8bit()
3936{
3937 int i;
3938 int c;
3939
3940 /* Only need to do something when not already using 8-bit codes. */
3941 if (!term_is_8bit(T_NAME))
3942 {
3943 for (i = 0; i < tc_len; ++i)
3944 {
3945 c = term_7to8bit(termcodes[i].code);
3946 if (c != 0)
3947 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003948 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 termcodes[i].code[0] = c;
3950 }
3951 }
3952 need_gather = TRUE; /* need to fill termleader[] */
3953 }
3954 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003955 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956}
3957#endif
3958
3959#ifdef CHECK_DOUBLE_CLICK
3960static linenr_T orig_topline = 0;
3961# ifdef FEAT_DIFF
3962static int orig_topfill = 0;
3963# endif
3964#endif
3965#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
3966/*
3967 * Checking for double clicks ourselves.
3968 * "orig_topline" is used to avoid detecting a double-click when the window
3969 * contents scrolled (e.g., when 'scrolloff' is non-zero).
3970 */
3971/*
3972 * Set orig_topline. Used when jumping to another window, so that a double
3973 * click still works.
3974 */
3975 void
3976set_mouse_topline(wp)
3977 win_T *wp;
3978{
3979 orig_topline = wp->w_topline;
3980# ifdef FEAT_DIFF
3981 orig_topfill = wp->w_topfill;
3982# endif
3983}
3984#endif
3985
3986/*
3987 * Check if typebuf.tb_buf[] contains a terminal key code.
3988 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
3989 * + max_offset].
3990 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003991 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 * With a match, the match is removed, the replacement code is inserted in
3993 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
3994 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003995 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
3996 * "buflen" is then the length of the string in buf[] and is updated for
3997 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 */
3999 int
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004000check_termcode(max_offset, buf, bufsize, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 int max_offset;
4002 char_u *buf;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004003 int bufsize;
4004 int *buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
4006 char_u *tp;
4007 char_u *p;
4008 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004009 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01004011 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 int offset;
4013 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004014 int modifiers;
4015 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 int new_slen;
4017 int extra;
4018 char_u string[MAX_KEY_CODE_LEN + 1];
4019 int i, j;
4020 int idx = 0;
4021#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00004022# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4023 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 char_u bytes[6];
4025 int num_bytes;
4026# endif
4027 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 int is_click, is_drag;
4029 int wheel_code = 0;
4030 int current_button;
4031 static int held_button = MOUSE_RELEASE;
4032 static int orig_num_clicks = 1;
4033 static int orig_mouse_code = 0x0;
4034# ifdef CHECK_DOUBLE_CLICK
4035 static int orig_mouse_col = 0;
4036 static int orig_mouse_row = 0;
4037 static struct timeval orig_mouse_time = {0, 0};
4038 /* time of previous mouse click */
4039 struct timeval mouse_time; /* time of current mouse click */
4040 long timediff; /* elapsed time in msec */
4041# endif
4042#endif
4043 int cpo_koffset;
4044#ifdef FEAT_MOUSE_GPM
4045 extern int gpm_flag; /* gpm library variable */
4046#endif
4047
4048 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4049
4050 /*
4051 * Speed up the checks for terminal codes by gathering all first bytes
4052 * used in termleader[]. Often this is just a single <Esc>.
4053 */
4054 if (need_gather)
4055 gather_termleader();
4056
4057 /*
4058 * Check at several positions in typebuf.tb_buf[], to catch something like
4059 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4060 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004061 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 * This is used often, KEEP IT FAST!
4063 */
4064 for (offset = 0; offset < max_offset; ++offset)
4065 {
4066 if (buf == NULL)
4067 {
4068 if (offset >= typebuf.tb_len)
4069 break;
4070 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4071 len = typebuf.tb_len - offset; /* length of the input */
4072 }
4073 else
4074 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004075 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 break;
4077 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004078 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 }
4080
4081 /*
4082 * Don't check characters after K_SPECIAL, those are already
4083 * translated terminal chars (avoid translating ~@^Hx).
4084 */
4085 if (*tp == K_SPECIAL)
4086 {
4087 offset += 2; /* there are always 2 extra characters */
4088 continue;
4089 }
4090
4091 /*
4092 * Skip this position if the character does not appear as the first
4093 * character in term_strings. This speeds up a lot, since most
4094 * termcodes start with the same character (ESC or CSI).
4095 */
4096 i = *tp;
4097 for (p = termleader; *p && *p != i; ++p)
4098 ;
4099 if (*p == NUL)
4100 continue;
4101
4102 /*
4103 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4104 * are in Insert mode.
4105 */
4106 if (*tp == ESC && !p_ek && (State & INSERT))
4107 continue;
4108
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004110 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004111 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
4113#ifdef FEAT_GUI
4114 if (gui.in_use)
4115 {
4116 /*
4117 * GUI special key codes are all of the form [CSI xx].
4118 */
4119 if (*tp == CSI) /* Special key from GUI */
4120 {
4121 if (len < 3)
4122 return -1; /* Shouldn't happen */
4123 slen = 3;
4124 key_name[0] = tp[1];
4125 key_name[1] = tp[2];
4126 }
4127 }
4128 else
4129#endif /* FEAT_GUI */
4130 {
4131 for (idx = 0; idx < tc_len; ++idx)
4132 {
4133 /*
4134 * Ignore the entry if we are not at the start of
4135 * typebuf.tb_buf[]
4136 * and there are not enough characters to make a match.
4137 * But only when the 'K' flag is in 'cpoptions'.
4138 */
4139 slen = termcodes[idx].len;
4140 if (cpo_koffset && offset && len < slen)
4141 continue;
4142 if (STRNCMP(termcodes[idx].code, tp,
4143 (size_t)(slen > len ? len : slen)) == 0)
4144 {
4145 if (len < slen) /* got a partial sequence */
4146 return -1; /* need to get more chars */
4147
4148 /*
4149 * When found a keypad key, check if there is another key
4150 * that matches and use that one. This makes <Home> to be
4151 * found instead of <kHome> when they produce the same
4152 * key code.
4153 */
4154 if (termcodes[idx].name[0] == 'K'
4155 && VIM_ISDIGIT(termcodes[idx].name[1]))
4156 {
4157 for (j = idx + 1; j < tc_len; ++j)
4158 if (termcodes[j].len == slen &&
4159 STRNCMP(termcodes[idx].code,
4160 termcodes[j].code, slen) == 0)
4161 {
4162 idx = j;
4163 break;
4164 }
4165 }
4166
4167 key_name[0] = termcodes[idx].name[0];
4168 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 break;
4170 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004171
4172 /*
4173 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004174 * <Esc>[123;*X (modslen == slen - 3)
4175 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4176 * When there is a modifier the * matches a number.
4177 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004178 */
4179 if (termcodes[idx].modlen > 0)
4180 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004181 modslen = termcodes[idx].modlen;
4182 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004183 continue;
4184 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004185 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004186 {
4187 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004188
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004189 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004190 return -1; /* need to get more chars */
4191
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004192 if (tp[modslen] == termcodes[idx].code[slen - 1])
4193 slen = modslen + 1; /* no modifiers */
4194 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004195 continue; /* no match */
4196 else
4197 {
4198 /* Skip over the digits, the final char must
4199 * follow. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004200 for (j = slen - 2; j < len && isdigit(tp[j]); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004201 ;
4202 ++j;
4203 if (len < j) /* got a partial sequence */
4204 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004205 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4206 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004207
4208 /* Match! Convert modifier bits. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004209 n = atoi((char *)tp + slen - 2) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004210 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004211 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004212 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004213 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004214 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004215 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004216 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004217 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004218
4219 slen = j;
4220 }
4221 key_name[0] = termcodes[idx].name[0];
4222 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004223 break;
4224 }
4225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
4227 }
4228
4229#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004230 if (key_name[0] == NUL
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004231 /* Mouse codes of DEC, pterm, and URXVT start with <ESC>[. When
4232 * detecting the start of these mouse codes they might as well be
4233 * another key code or terminal response. */
4234# ifdef FEAT_MOUSE_DEC
4235 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004236# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004237# ifdef FEAT_MOUSE_PTERM
4238 || key_name[0] == KS_PTERM_MOUSE
4239# endif
4240# ifdef FEAT_MOUSE_URXVT
4241 || key_name[0] == KS_URXVT_MOUSE
4242# endif
4243 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004245 /* Check for some responses from the terminal starting with
4246 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004247 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004248 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01004249 * Also eat other possible responses to t_RV, rxvt returns
4250 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4251 * mrxvt has been reported to have "+" in the version. Assume
4252 * the escape sequence ends with a letter or one of "{|}~".
4253 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004254 * - Cursor position report: <Esc>[{row};{col}R
4255 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004256 * ambiguous-width character state.
4257 */
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004258 char_u *argp = tp[0] == ESC ? tp + 2 : tp + 1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004259
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004260 if ((*T_CRV != NUL || *T_U7 != NUL)
4261 && ((tp[0] == ESC && len >= 3 && tp[1] == '[')
Bram Moolenaar46a75612013-05-15 14:22:41 +02004262 || (tp[0] == CSI && len >= 2))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004263 && (VIM_ISDIGIT(*argp) || *argp == '>' || *argp == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 {
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004265#ifdef FEAT_MBYTE
4266 int col;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004267 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 j = 0;
4270 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004271 for (i = 2 + (tp[0] != CSI); i < len
4272 && !(tp[i] >= '{' && tp[i] <= '~')
4273 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 if (tp[i] == ';' && ++j == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004275 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004276 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004277#ifdef FEAT_MBYTE
4278 row_char = tp[i - 1];
4279#endif
4280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004282 {
4283 LOG_TR("Not enough characters for CRV");
4284 return -1;
4285 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004286#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004287 if (extra > 0)
4288 col = atoi((char *)tp + extra);
4289 else
4290 col = 0;
4291
4292 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4293 * u7_status is not "sent", it may be from a previous Vim that
4294 * just exited. But not for <S-F3>, it sends something
4295 * similar, check for row and column to make sense. */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004296 if (j == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004297 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004298 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004299 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004300 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004301
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004302 LOG_TR("Received U7 status");
4303 u7_status = U7_GOT;
4304# ifdef FEAT_AUTOCMD
4305 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004306# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004307 if (col == 2)
4308 aw = "single";
4309 else if (col == 3)
4310 aw = "double";
4311 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4312 {
4313 /* Setting the option causes a screen redraw. Do
4314 * that right away if possible, keeping any
4315 * messages. */
4316 set_option_value((char_u *)"ambw", 0L,
4317 (char_u *)aw, 0);
4318# ifdef DEBUG_TERMRESPONSE
4319 {
4320 char buf[100];
4321 int r = redraw_asap(CLEAR);
4322
4323 sprintf(buf,
4324 "set 'ambiwidth', redraw_asap(): %d",
4325 r);
4326 log_tr(buf);
4327 }
4328# else
4329 redraw_asap(CLEAR);
4330# endif
4331 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004332 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004333 key_name[0] = (int)KS_EXTRA;
4334 key_name[1] = (int)KE_IGNORE;
4335 slen = i + 1;
4336 }
4337 else
4338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004340 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004342 LOG_TR("Received CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 crv_status = CRV_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004344# ifdef FEAT_AUTOCMD
4345 did_cursorhold = TRUE;
4346# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
4348 /* If this code starts with CSI, you can bet that the
4349 * terminal uses 8-bit codes. */
4350 if (tp[0] == CSI)
4351 switch_to_8bit();
4352
4353 /* rxvt sends its version number: "20703" is 2.7.3.
4354 * Ignore it for when the user has set 'term' to xterm,
4355 * even though it's an rxvt. */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004356 if (extra > 0)
4357 extra = atoi((char *)tp + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 if (extra > 20000)
4359 extra = 0;
4360
4361 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
4362 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004363 /* Only set 'ttymouse' automatically if it was not set
4364 * by the user already. */
4365 if (!option_was_set((char_u *)"ttym"))
4366 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004367# ifdef TTYM_SGR
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004368 if (extra >= 277)
4369 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004370 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004371 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004372# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004373 /* if xterm version >= 95 use mouse dragging */
4374 if (extra >= 95)
4375 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004377 }
4378
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 /* if xterm version >= 141 try to get termcap codes */
4380 if (extra >= 141)
4381 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004382 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 check_for_codes = TRUE;
4384 need_gather = TRUE;
4385 req_codes_from_term();
4386 }
4387 }
4388# ifdef FEAT_EVAL
4389 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4390# endif
4391# ifdef FEAT_AUTOCMD
4392 apply_autocmds(EVENT_TERMRESPONSE,
4393 NULL, NULL, FALSE, curbuf);
4394# endif
4395 key_name[0] = (int)KS_EXTRA;
4396 key_name[1] = (int)KE_IGNORE;
4397 slen = i + 1;
4398 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004399 }
4400
4401 /* Check for background color response from the terminal:
4402 *
4403 * {lead}11;rgb:{rrrr}/{gggg}/{bbbb}{tail}
4404 *
4405 * {lead} can be <Esc>] or OSC
4406 * {tail} can be '\007', <Esc>\ or STERM.
4407 *
4408 * Consume any code that starts with "{lead}11;", it's also
4409 * possible that "rgba" is following.
4410 */
4411 else if (*T_RBG != NUL
4412 && ((tp[0] == ESC && len >= 2 && tp[1] == ']')
4413 || tp[0] == OSC))
4414 {
4415 j = 1 + (tp[0] == ESC);
4416 if (len >= j + 3 && (argp[0] != '1'
4417 || argp[1] != '1' || argp[2] != ';'))
4418 i = 0; /* no match */
4419 else
4420 for (i = j; i < len; ++i)
4421 if (tp[i] == '\007' || (tp[0] == OSC ? tp[i] == STERM
4422 : (tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')))
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004423 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004424 if (i - j >= 21 && STRNCMP(tp + j + 3, "rgb:", 4) == 0
4425 && tp[j + 11] == '/' && tp[j + 16] == '/'
4426 && !option_was_set((char_u *)"bg"))
4427 {/* TODO: don't set option when already the right value */
4428 LOG_TR("Received RBG");
4429 rbg_status = RBG_GOT;
4430 set_option_value((char_u *)"bg", 0L, (char_u *)(
4431 (3 * '6' < tp[j+7] + tp[j+12] + tp[j+17])
4432 ? "light" : "dark"), 0);
4433 reset_option_was_set((char_u *)"bg");
4434 redraw_asap(CLEAR);
4435 }
4436
4437 /* got finished code: consume it */
4438 key_name[0] = (int)KS_EXTRA;
4439 key_name[1] = (int)KE_IGNORE;
4440 slen = i + 1 + (tp[i] == ESC);
4441 break;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004442 }
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004443 if (i == len)
4444 {
4445 LOG_TR("not enough characters for RB");
4446 return -1;
Bram Moolenaarb5c32652015-06-25 17:03:36 +02004447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004450 /* Check for key code response from xterm:
4451 *
4452 * {lead}{flag}+r<hex bytes><{tail}
4453 *
4454 * {lead} can be <Esc>P or DCS
4455 * {flag} can be '0' or '1'
4456 * {tail} can be Esc>\ or STERM
4457 *
4458 * Consume any code that starts with "{lead}.+r".
4459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 else if (check_for_codes
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004461 && ((tp[0] == ESC && len >= 2 && tp[1] == 'P')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 || tp[0] == DCS))
4463 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004464 j = 1 + (tp[0] == ESC);
4465 if (len >= j + 3 && (argp[1] != '+' || argp[2] != 'r'))
4466 i = 0; /* no match */
4467 else
4468 for (i = j; i < len; ++i)
4469 if ((tp[i] == ESC && i + 1 < len && tp[i + 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 || tp[i] == STERM)
4471 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004472 if (i - j >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 got_code_from_term(tp + j, i);
4474 key_name[0] = (int)KS_EXTRA;
4475 key_name[1] = (int)KE_IGNORE;
4476 slen = i + 1 + (tp[i] == ESC);
4477 break;
4478 }
4479
4480 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004481 {
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004482 /* These codes arrive many together, each code can be
4483 * truncated at any point. */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004484 LOG_TR("not enough characters for XT");
Bram Moolenaar46fd4df2015-07-10 14:05:10 +02004485 return -1;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488 }
4489#endif
4490
4491 if (key_name[0] == NUL)
4492 continue; /* No match at this position, try next one */
4493
4494 /* We only get here when we have a complete termcode match */
4495
4496#ifdef FEAT_MOUSE
4497# ifdef FEAT_GUI
4498 /*
4499 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4500 * so that we know which window to scroll later.
4501 */
4502 if (gui.in_use
4503 && key_name[0] == (int)KS_EXTRA
4504 && (key_name[1] == (int)KE_X1MOUSE
4505 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004506 || key_name[1] == (int)KE_MOUSELEFT
4507 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 || key_name[1] == (int)KE_MOUSEDOWN
4509 || key_name[1] == (int)KE_MOUSEUP))
4510 {
4511 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4512 if (num_bytes == -1) /* not enough coordinates */
4513 return -1;
4514 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4515 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4516 slen += num_bytes;
4517 }
4518 else
4519# endif
4520 /*
4521 * If it is a mouse click, get the coordinates.
4522 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004523 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004525 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526# endif
4527# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004528 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529# endif
4530# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004531 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532# endif
4533# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004534 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004536# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004537 || key_name[0] == KS_URXVT_MOUSE
4538# endif
4539# ifdef FEAT_MOUSE_SGR
4540 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004541# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 )
4543 {
4544 is_click = is_drag = FALSE;
4545
Bram Moolenaar864207d2008-06-24 22:14:38 +00004546# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4547 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (key_name[0] == (int)KS_MOUSE)
4549 {
4550 /*
4551 * For xterm and MSDOS we get "<t_mouse>scr", where
4552 * s == encoded button state:
4553 * 0x20 = left button down
4554 * 0x21 = middle button down
4555 * 0x22 = right button down
4556 * 0x23 = any button release
4557 * 0x60 = button 4 down (scroll wheel down)
4558 * 0x61 = button 5 down (scroll wheel up)
4559 * add 0x04 for SHIFT
4560 * add 0x08 for ALT
4561 * add 0x10 for CTRL
4562 * add 0x20 for mouse drag (0x40 is drag with left button)
4563 * c == column + ' ' + 1 == column + 33
4564 * r == row + ' ' + 1 == row + 33
4565 *
4566 * The coordinates are passed on through global variables.
4567 * Ugly, but this avoids trouble with mouse clicks at an
4568 * unexpected moment and allows for mapping them.
4569 */
4570 for (;;)
4571 {
4572#ifdef FEAT_GUI
4573 if (gui.in_use)
4574 {
4575 /* GUI uses more bits for columns > 223 */
4576 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4577 if (num_bytes == -1) /* not enough coordinates */
4578 return -1;
4579 mouse_code = bytes[0];
4580 mouse_col = 128 * (bytes[1] - ' ' - 1)
4581 + bytes[2] - ' ' - 1;
4582 mouse_row = 128 * (bytes[3] - ' ' - 1)
4583 + bytes[4] - ' ' - 1;
4584 }
4585 else
4586#endif
4587 {
4588 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4589 if (num_bytes == -1) /* not enough coordinates */
4590 return -1;
4591 mouse_code = bytes[0];
4592 mouse_col = bytes[1] - ' ' - 1;
4593 mouse_row = bytes[2] - ' ' - 1;
4594 }
4595 slen += num_bytes;
4596
4597 /* If the following bytes is also a mouse code and it has
4598 * the same code, dump this one and get the next. This
4599 * makes dragging a whole lot faster. */
4600#ifdef FEAT_GUI
4601 if (gui.in_use)
4602 j = 3;
4603 else
4604#endif
4605 j = termcodes[idx].len;
4606 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4607 && tp[slen + j] == mouse_code
4608 && tp[slen + j + 1] != NUL
4609 && tp[slen + j + 2] != NUL
4610#ifdef FEAT_GUI
4611 && (!gui.in_use
4612 || (tp[slen + j + 3] != NUL
4613 && tp[slen + j + 4] != NUL))
4614#endif
4615 )
4616 slen += j;
4617 else
4618 break;
4619 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004622# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4623 if (key_name[0] == KS_URXVT_MOUSE
4624 || key_name[0] == KS_SGR_MOUSE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004625 {
4626 for (;;)
4627 {
4628 /* URXVT 1015 mouse reporting mode:
4629 * Almost identical to xterm mouse mode, except the values
4630 * are decimal instead of bytes.
4631 *
4632 * \033[%d;%d;%dM
4633 * ^-- row
4634 * ^----- column
4635 * ^-------- code
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004636 *
4637 * SGR 1006 mouse reporting mode:
4638 * Almost identical to xterm mouse mode, except the values
4639 * are decimal instead of bytes.
4640 *
4641 * \033[<%d;%d;%dM
4642 * ^-- row
4643 * ^----- column
4644 * ^-------- code
4645 *
4646 * \033[<%d;%d;%dm : mouse release event
4647 * ^-- row
4648 * ^----- column
4649 * ^-------- code
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004650 */
4651 p = tp + slen;
4652
4653 mouse_code = getdigits(&p);
4654 if (*p++ != ';')
4655 return -1;
4656
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004657 /* when mouse reporting is SGR, add 32 to mouse code */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004658 if (key_name[0] == KS_SGR_MOUSE)
4659 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004660
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004661 mouse_col = getdigits(&p) - 1;
4662 if (*p++ != ';')
4663 return -1;
4664
4665 mouse_row = getdigits(&p) - 1;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004666 if (key_name[0] == KS_SGR_MOUSE && *p == 'm')
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004667 mouse_code |= MOUSE_RELEASE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004668 else if (*p != 'M')
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004669 return -1;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004670 p++;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004671
4672 slen += (int)(p - (tp + slen));
4673
4674 /* skip this one if next one has same code (like xterm
4675 * case) */
4676 j = termcodes[idx].len;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004677 if (STRNCMP(tp, tp + slen, (size_t)j) == 0)
4678 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004679 int slen2;
4680 int cmd_complete = 0;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004681
4682 /* check if the command is complete by looking for the
4683 * 'M' */
4684 for (slen2 = slen; slen2 < len; slen2++)
4685 {
4686 if (tp[slen2] == 'M'
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004687 || (key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004688 && tp[slen2] == 'm'))
4689 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004690 cmd_complete = 1;
4691 break;
4692 }
4693 }
4694 p += j;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004695 if (cmd_complete && getdigits(&p) == mouse_code)
4696 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004697 slen += j; /* skip the \033[ */
4698 continue;
4699 }
4700 }
4701 break;
4702 }
4703 }
4704# endif
4705
4706 if (key_name[0] == (int)KS_MOUSE
4707#ifdef FEAT_MOUSE_URXVT
4708 || key_name[0] == (int)KS_URXVT_MOUSE
4709#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004710#ifdef FEAT_MOUSE_SGR
4711 || key_name[0] == KS_SGR_MOUSE
4712#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004713 )
4714 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715# if !defined(MSWIN) && !defined(MSDOS)
4716 /*
4717 * Handle mouse events.
4718 * Recognize the xterm mouse wheel, but not in the GUI, the
4719 * Linux console with GPM and the MS-DOS or Win32 console
4720 * (multi-clicks use >= 0x60).
4721 */
4722 if (mouse_code >= MOUSEWHEEL_LOW
4723# ifdef FEAT_GUI
4724 && !gui.in_use
4725# endif
4726# ifdef FEAT_MOUSE_GPM
4727 && gpm_flag == 0
4728# endif
4729 )
4730 {
4731 /* Keep the mouse_code before it's changed, so that we
4732 * remember that it was a mouse wheel click. */
4733 wheel_code = mouse_code;
4734 }
4735# ifdef FEAT_MOUSE_XTERM
4736 else if (held_button == MOUSE_RELEASE
4737# ifdef FEAT_GUI
4738 && !gui.in_use
4739# endif
4740 && (mouse_code == 0x23 || mouse_code == 0x24))
4741 {
4742 /* Apparently used by rxvt scroll wheel. */
4743 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4744 }
4745# endif
4746
4747# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4748 else if (use_xterm_mouse() > 1)
4749 {
4750 if (mouse_code & MOUSE_DRAG_XTERM)
4751 mouse_code |= MOUSE_DRAG;
4752 }
4753# endif
4754# ifdef FEAT_XCLIPBOARD
4755 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4756 {
4757 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4758 stop_xterm_trace();
4759 else
4760 start_xterm_trace(mouse_code);
4761 }
4762# endif
4763# endif
4764 }
4765# endif /* !UNIX || FEAT_MOUSE_XTERM */
4766# ifdef FEAT_MOUSE_NET
4767 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4768 {
4769 int mc, mr;
4770
4771 /* expect a rather limited sequence like: balancing {
4772 * \033}6,45\r
4773 * '6' is the row, 45 is the column
4774 */
4775 p = tp + slen;
4776 mr = getdigits(&p);
4777 if (*p++ != ',')
4778 return -1;
4779 mc = getdigits(&p);
4780 if (*p++ != '\r')
4781 return -1;
4782
4783 mouse_col = mc - 1;
4784 mouse_row = mr - 1;
4785 mouse_code = MOUSE_LEFT;
4786 slen += (int)(p - (tp + slen));
4787 }
4788# endif /* FEAT_MOUSE_NET */
4789# ifdef FEAT_MOUSE_JSB
4790 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4791 {
4792 int mult, val, iter, button, status;
4793
4794 /* JSBTERM Input Model
4795 * \033[0~zw uniq escape sequence
4796 * (L-x) Left button pressed - not pressed x not reporting
4797 * (M-x) Middle button pressed - not pressed x not reporting
4798 * (R-x) Right button pressed - not pressed x not reporting
4799 * (SDmdu) Single , Double click, m mouse move d button down
4800 * u button up
4801 * ### X cursor position padded to 3 digits
4802 * ### Y cursor position padded to 3 digits
4803 * (s-x) SHIFT key pressed - not pressed x not reporting
4804 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004805 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 */
4807
4808 p = tp + slen;
4809 button = mouse_code = 0;
4810 switch (*p++)
4811 {
4812 case 'L': button = 1; break;
4813 case '-': break;
4814 case 'x': break; /* ignore sequence */
4815 default: return -1; /* Unknown Result */
4816 }
4817 switch (*p++)
4818 {
4819 case 'M': button |= 2; break;
4820 case '-': break;
4821 case 'x': break; /* ignore sequence */
4822 default: return -1; /* Unknown Result */
4823 }
4824 switch (*p++)
4825 {
4826 case 'R': button |= 4; break;
4827 case '-': break;
4828 case 'x': break; /* ignore sequence */
4829 default: return -1; /* Unknown Result */
4830 }
4831 status = *p++;
4832 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4833 mult /= 10, p++)
4834 if (*p >= '0' && *p <= '9')
4835 val += (*p - '0') * mult;
4836 else
4837 return -1;
4838 mouse_col = val;
4839 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4840 mult /= 10, p++)
4841 if (*p >= '0' && *p <= '9')
4842 val += (*p - '0') * mult;
4843 else
4844 return -1;
4845 mouse_row = val;
4846 switch (*p++)
4847 {
4848 case 's': button |= 8; break; /* SHIFT key Pressed */
4849 case '-': break; /* Not Pressed */
4850 case 'x': break; /* Not Reporting */
4851 default: return -1; /* Unknown Result */
4852 }
4853 switch (*p++)
4854 {
4855 case 'c': button |= 16; break; /* CTRL key Pressed */
4856 case '-': break; /* Not Pressed */
4857 case 'x': break; /* Not Reporting */
4858 default: return -1; /* Unknown Result */
4859 }
4860 if (*p++ != '\033')
4861 return -1;
4862 if (*p++ != '\\')
4863 return -1;
4864 switch (status)
4865 {
4866 case 'D': /* Double Click */
4867 case 'S': /* Single Click */
4868 if (button & 1) mouse_code |= MOUSE_LEFT;
4869 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4870 if (button & 4) mouse_code |= MOUSE_RIGHT;
4871 if (button & 8) mouse_code |= MOUSE_SHIFT;
4872 if (button & 16) mouse_code |= MOUSE_CTRL;
4873 break;
4874 case 'm': /* Mouse move */
4875 if (button & 1) mouse_code |= MOUSE_LEFT;
4876 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4877 if (button & 4) mouse_code |= MOUSE_RIGHT;
4878 if (button & 8) mouse_code |= MOUSE_SHIFT;
4879 if (button & 16) mouse_code |= MOUSE_CTRL;
4880 if ((button & 7) != 0)
4881 {
4882 held_button = mouse_code;
4883 mouse_code |= MOUSE_DRAG;
4884 }
4885 is_drag = TRUE;
4886 showmode();
4887 break;
4888 case 'd': /* Button Down */
4889 if (button & 1) mouse_code |= MOUSE_LEFT;
4890 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4891 if (button & 4) mouse_code |= MOUSE_RIGHT;
4892 if (button & 8) mouse_code |= MOUSE_SHIFT;
4893 if (button & 16) mouse_code |= MOUSE_CTRL;
4894 break;
4895 case 'u': /* Button Up */
4896 if (button & 1)
4897 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4898 if (button & 2)
4899 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4900 if (button & 4)
4901 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4902 if (button & 8)
4903 mouse_code |= MOUSE_SHIFT;
4904 if (button & 16)
4905 mouse_code |= MOUSE_CTRL;
4906 break;
4907 default: return -1; /* Unknown Result */
4908 }
4909
4910 slen += (p - (tp + slen));
4911 }
4912# endif /* FEAT_MOUSE_JSB */
4913# ifdef FEAT_MOUSE_DEC
4914 if (key_name[0] == (int)KS_DEC_MOUSE)
4915 {
4916 /* The DEC Locator Input Model
4917 * Netterm delivers the code sequence:
4918 * \033[2;4;24;80&w (left button down)
4919 * \033[3;0;24;80&w (left button up)
4920 * \033[6;1;24;80&w (right button down)
4921 * \033[7;0;24;80&w (right button up)
4922 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4923 * Pe is the event code
4924 * Pb is the button code
4925 * Pr is the row coordinate
4926 * Pc is the column coordinate
4927 * Pp is the third coordinate (page number)
4928 * Pe, the event code indicates what event caused this report
4929 * The following event codes are defined:
4930 * 0 - request, the terminal received an explicit request
4931 * for a locator report, but the locator is unavailable
4932 * 1 - request, the terminal received an explicit request
4933 * for a locator report
4934 * 2 - left button down
4935 * 3 - left button up
4936 * 4 - middle button down
4937 * 5 - middle button up
4938 * 6 - right button down
4939 * 7 - right button up
4940 * 8 - fourth button down
4941 * 9 - fourth button up
4942 * 10 - locator outside filter rectangle
4943 * Pb, the button code, ASCII decimal 0-15 indicating which
4944 * buttons are down if any. The state of the four buttons
4945 * on the locator correspond to the low four bits of the
4946 * decimal value,
4947 * "1" means button depressed
4948 * 0 - no buttons down,
4949 * 1 - right,
4950 * 2 - middle,
4951 * 4 - left,
4952 * 8 - fourth
4953 * Pr is the row coordinate of the locator position in the page,
4954 * encoded as an ASCII decimal value.
4955 * If Pr is omitted, the locator position is undefined
4956 * (outside the terminal window for example).
4957 * Pc is the column coordinate of the locator position in the
4958 * page, encoded as an ASCII decimal value.
4959 * If Pc is omitted, the locator position is undefined
4960 * (outside the terminal window for example).
4961 * Pp is the page coordinate of the locator position
4962 * encoded as an ASCII decimal value.
4963 * The page coordinate may be omitted if the locator is on
4964 * page one (the default). We ignore it anyway.
4965 */
4966 int Pe, Pb, Pr, Pc;
4967
4968 p = tp + slen;
4969
4970 /* get event status */
4971 Pe = getdigits(&p);
4972 if (*p++ != ';')
4973 return -1;
4974
4975 /* get button status */
4976 Pb = getdigits(&p);
4977 if (*p++ != ';')
4978 return -1;
4979
4980 /* get row status */
4981 Pr = getdigits(&p);
4982 if (*p++ != ';')
4983 return -1;
4984
4985 /* get column status */
4986 Pc = getdigits(&p);
4987
4988 /* the page parameter is optional */
4989 if (*p == ';')
4990 {
4991 p++;
4992 (void)getdigits(&p);
4993 }
4994 if (*p++ != '&')
4995 return -1;
4996 if (*p++ != 'w')
4997 return -1;
4998
4999 mouse_code = 0;
5000 switch (Pe)
5001 {
5002 case 0: return -1; /* position request while unavailable */
5003 case 1: /* a response to a locator position request includes
5004 the status of all buttons */
5005 Pb &= 7; /* mask off and ignore fourth button */
5006 if (Pb & 4)
5007 mouse_code = MOUSE_LEFT;
5008 if (Pb & 2)
5009 mouse_code = MOUSE_MIDDLE;
5010 if (Pb & 1)
5011 mouse_code = MOUSE_RIGHT;
5012 if (Pb)
5013 {
5014 held_button = mouse_code;
5015 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005016 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
5018 is_drag = TRUE;
5019 showmode();
5020 break;
5021 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005022 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 break;
5024 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
5025 break;
5026 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005027 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 break;
5029 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
5030 break;
5031 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00005032 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 break;
5034 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
5035 break;
5036 case 8: return -1; /* fourth button down */
5037 case 9: return -1; /* fourth button up */
5038 case 10: return -1; /* mouse outside of filter rectangle */
5039 default: return -1; /* should never occur */
5040 }
5041
5042 mouse_col = Pc - 1;
5043 mouse_row = Pr - 1;
5044
5045 slen += (int)(p - (tp + slen));
5046 }
5047# endif /* FEAT_MOUSE_DEC */
5048# ifdef FEAT_MOUSE_PTERM
5049 if (key_name[0] == (int)KS_PTERM_MOUSE)
5050 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02005051 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052
5053 p = tp + slen;
5054
5055 action = getdigits(&p);
5056 if (*p++ != ';')
5057 return -1;
5058
5059 mouse_row = getdigits(&p);
5060 if (*p++ != ';')
5061 return -1;
5062 mouse_col = getdigits(&p);
5063 if (*p++ != ';')
5064 return -1;
5065
5066 button = getdigits(&p);
5067 mouse_code = 0;
5068
5069 switch( button )
5070 {
5071 case 4: mouse_code = MOUSE_LEFT; break;
5072 case 1: mouse_code = MOUSE_RIGHT; break;
5073 case 2: mouse_code = MOUSE_MIDDLE; break;
5074 default: return -1;
5075 }
5076
5077 switch( action )
5078 {
5079 case 31: /* Initial press */
5080 if (*p++ != ';')
5081 return -1;
5082
5083 num_clicks = getdigits(&p); /* Not used */
5084 break;
5085
5086 case 32: /* Release */
5087 mouse_code |= MOUSE_RELEASE;
5088 break;
5089
5090 case 33: /* Drag */
5091 held_button = mouse_code;
5092 mouse_code |= MOUSE_DRAG;
5093 break;
5094
5095 default:
5096 return -1;
5097 }
5098
5099 if (*p++ != 't')
5100 return -1;
5101
5102 slen += (p - (tp + slen));
5103 }
5104# endif /* FEAT_MOUSE_PTERM */
5105
5106 /* Interpret the mouse code */
5107 current_button = (mouse_code & MOUSE_CLICK_MASK);
5108 if (current_button == MOUSE_RELEASE
5109# ifdef FEAT_MOUSE_XTERM
5110 && wheel_code == 0
5111# endif
5112 )
5113 {
5114 /*
5115 * If we get a mouse drag or release event when
5116 * there is no mouse button held down (held_button ==
5117 * MOUSE_RELEASE), produce a K_IGNORE below.
5118 * (can happen when you hold down two buttons
5119 * and then let them go, or click in the menu bar, but not
5120 * on a menu, and drag into the text).
5121 */
5122 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5123 is_drag = TRUE;
5124 current_button = held_button;
5125 }
5126 else if (wheel_code == 0)
5127 {
5128# ifdef CHECK_DOUBLE_CLICK
5129# ifdef FEAT_MOUSE_GPM
5130# ifdef FEAT_GUI
5131 /*
5132 * Only for Unix, when GUI or gpm is not active, we handle
5133 * multi-clicks here.
5134 */
5135 if (gpm_flag == 0 && !gui.in_use)
5136# else
5137 if (gpm_flag == 0)
5138# endif
5139# else
5140# ifdef FEAT_GUI
5141 if (!gui.in_use)
5142# endif
5143# endif
5144 {
5145 /*
5146 * Compute the time elapsed since the previous mouse click.
5147 */
5148 gettimeofday(&mouse_time, NULL);
5149 timediff = (mouse_time.tv_usec
5150 - orig_mouse_time.tv_usec) / 1000;
5151 if (timediff < 0)
5152 --orig_mouse_time.tv_sec;
5153 timediff += (mouse_time.tv_sec
5154 - orig_mouse_time.tv_sec) * 1000;
5155 orig_mouse_time = mouse_time;
5156 if (mouse_code == orig_mouse_code
5157 && timediff < p_mouset
5158 && orig_num_clicks != 4
5159 && orig_mouse_col == mouse_col
5160 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005161 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005163 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005165 )
5166#ifdef FEAT_WINDOWS
5167 /* Double click in tab pages line also works
5168 * when window contents changes. */
5169 || (mouse_row == 0 && firstwin->w_winrow > 0)
5170#endif
5171 )
5172 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 ++orig_num_clicks;
5174 else
5175 orig_num_clicks = 1;
5176 orig_mouse_col = mouse_col;
5177 orig_mouse_row = mouse_row;
5178 orig_topline = curwin->w_topline;
5179#ifdef FEAT_DIFF
5180 orig_topfill = curwin->w_topfill;
5181#endif
5182 }
5183# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5184 else
5185 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5186# endif
5187# else
5188 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5189# endif
5190 is_click = TRUE;
5191 orig_mouse_code = mouse_code;
5192 }
5193 if (!is_drag)
5194 held_button = mouse_code & MOUSE_CLICK_MASK;
5195
5196 /*
5197 * Translate the actual mouse event into a pseudo mouse event.
5198 * First work out what modifiers are to be used.
5199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if (orig_mouse_code & MOUSE_SHIFT)
5201 modifiers |= MOD_MASK_SHIFT;
5202 if (orig_mouse_code & MOUSE_CTRL)
5203 modifiers |= MOD_MASK_CTRL;
5204 if (orig_mouse_code & MOUSE_ALT)
5205 modifiers |= MOD_MASK_ALT;
5206 if (orig_num_clicks == 2)
5207 modifiers |= MOD_MASK_2CLICK;
5208 else if (orig_num_clicks == 3)
5209 modifiers |= MOD_MASK_3CLICK;
5210 else if (orig_num_clicks == 4)
5211 modifiers |= MOD_MASK_4CLICK;
5212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 /* Work out our pseudo mouse event */
5214 key_name[0] = (int)KS_EXTRA;
5215 if (wheel_code != 0)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005216 {
5217 if (wheel_code & MOUSE_CTRL)
5218 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005219 if (wheel_code & MOUSE_ALT)
5220 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 key_name[1] = (wheel_code & 1)
5222 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 else
5225 key_name[1] = get_pseudo_mouse_code(current_button,
5226 is_click, is_drag);
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005227
5228 /* Make sure the mouse position is valid. Some terminals may
5229 * return weird values. */
5230 if (mouse_col >= Columns)
5231 mouse_col = Columns - 1;
5232 if (mouse_row >= Rows)
5233 mouse_row = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
5235#endif /* FEAT_MOUSE */
5236
5237#ifdef FEAT_GUI
5238 /*
5239 * If using the GUI, then we get menu and scrollbar events.
5240 *
5241 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5242 * four bytes which are to be taken as a pointer to the vimmenu_T
5243 * structure.
5244 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005245 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005246 * is one byte with the tab index.
5247 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5249 * by one byte representing the scrollbar number, and then four bytes
5250 * representing a long_u which is the new value of the scrollbar.
5251 *
5252 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5253 * KE_FILLER followed by four bytes representing a long_u which is the
5254 * new value of the scrollbar.
5255 */
5256# ifdef FEAT_MENU
5257 else if (key_name[0] == (int)KS_MENU)
5258 {
5259 long_u val;
5260
5261 num_bytes = get_long_from_buf(tp + slen, &val);
5262 if (num_bytes == -1)
5263 return -1;
5264 current_menu = (vimmenu_T *)val;
5265 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005266
5267 /* The menu may have been deleted right after it was used, check
5268 * for that. */
5269 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5270 {
5271 key_name[0] = KS_EXTRA;
5272 key_name[1] = (int)KE_IGNORE;
5273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 }
5275# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005276# ifdef FEAT_GUI_TABLINE
5277 else if (key_name[0] == (int)KS_TABLINE)
5278 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005279 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005280 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5281 if (num_bytes == -1)
5282 return -1;
5283 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005284 if (current_tab == 255) /* -1 in a byte gives 255 */
5285 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005286 slen += num_bytes;
5287 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005288 else if (key_name[0] == (int)KS_TABMENU)
5289 {
5290 /* Selecting tabline tab or using its menu. */
5291 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5292 if (num_bytes == -1)
5293 return -1;
5294 current_tab = (int)bytes[0];
5295 current_tabmenu = (int)bytes[1];
5296 slen += num_bytes;
5297 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005298# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299# ifndef USE_ON_FLY_SCROLL
5300 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5301 {
5302 long_u val;
5303
5304 /* Get the last scrollbar event in the queue of the same type */
5305 j = 0;
5306 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5307 && tp[j + 2] != NUL; ++i)
5308 {
5309 j += 3;
5310 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5311 if (num_bytes == -1)
5312 break;
5313 if (i == 0)
5314 current_scrollbar = (int)bytes[0];
5315 else if (current_scrollbar != (int)bytes[0])
5316 break;
5317 j += num_bytes;
5318 num_bytes = get_long_from_buf(tp + j, &val);
5319 if (num_bytes == -1)
5320 break;
5321 scrollbar_value = val;
5322 j += num_bytes;
5323 slen = j;
5324 }
5325 if (i == 0) /* not enough characters to make one */
5326 return -1;
5327 }
5328 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5329 {
5330 long_u val;
5331
5332 /* Get the last horiz. scrollbar event in the queue */
5333 j = 0;
5334 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5335 && tp[j + 2] != NUL; ++i)
5336 {
5337 j += 3;
5338 num_bytes = get_long_from_buf(tp + j, &val);
5339 if (num_bytes == -1)
5340 break;
5341 scrollbar_value = val;
5342 j += num_bytes;
5343 slen = j;
5344 }
5345 if (i == 0) /* not enough characters to make one */
5346 return -1;
5347 }
5348# endif /* !USE_ON_FLY_SCROLL */
5349#endif /* FEAT_GUI */
5350
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005351 /*
5352 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5353 */
5354 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5355
5356 /*
5357 * Add any modifier codes to our string.
5358 */
5359 new_slen = 0; /* Length of what will replace the termcode */
5360 if (modifiers != 0)
5361 {
5362 /* Some keys have the modifier included. Need to handle that here
5363 * to make mappings work. */
5364 key = simplify_key(key, &modifiers);
5365 if (modifiers != 0)
5366 {
5367 string[new_slen++] = K_SPECIAL;
5368 string[new_slen++] = (int)KS_MODIFIER;
5369 string[new_slen++] = modifiers;
5370 }
5371 }
5372
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005374 key_name[0] = KEY2TERMCAP0(key);
5375 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005377 {
5378 /* from ":set <M-b>=xx" */
5379#ifdef FEAT_MBYTE
5380 if (has_mbyte)
5381 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5382 else
5383#endif
5384 string[new_slen++] = key_name[1];
5385 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005386 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5387 && key_name[1] == KE_IGNORE)
5388 {
5389 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5390 * to indicate what happened. */
5391 retval = KEYLEN_REMOVED;
5392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 else
5394 {
5395 string[new_slen++] = K_SPECIAL;
5396 string[new_slen++] = key_name[0];
5397 string[new_slen++] = key_name[1];
5398 }
5399 string[new_slen] = NUL;
5400 extra = new_slen - slen;
5401 if (buf == NULL)
5402 {
5403 if (extra < 0)
5404 /* remove matched chars, taking care of noremap */
5405 del_typebuf(-extra, offset);
5406 else if (extra > 0)
5407 /* insert the extra space we need */
5408 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5409
5410 /*
5411 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5412 * typebuf.tb_buf[]!
5413 */
5414 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5415 (size_t)new_slen);
5416 }
5417 else
5418 {
5419 if (extra < 0)
5420 /* remove matched characters */
5421 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005422 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005424 {
5425 /* Insert the extra space we need. If there is insufficient
5426 * space return -1. */
5427 if (*buflen + extra + new_slen >= bufsize)
5428 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005430 (size_t)(*buflen - offset));
5431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005433 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005435 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 }
5437
Bram Moolenaar2951b772013-07-03 12:45:31 +02005438#ifdef FEAT_TERMRESPONSE
5439 LOG_TR("normal character");
5440#endif
5441
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 return 0; /* no match found */
5443}
5444
5445/*
5446 * Replace any terminal code strings in from[] with the equivalent internal
5447 * vim representation. This is used for the "from" and "to" part of a
5448 * mapping, and the "to" part of a menu command.
5449 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5450 * '<'.
5451 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5452 *
5453 * The replacement is done in result[] and finally copied into allocated
5454 * memory. If this all works well *bufp is set to the allocated memory and a
5455 * pointer to it is returned. If something fails *bufp is set to NULL and from
5456 * is returned.
5457 *
5458 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5459 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5460 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5461 * instead of a CTRL-V.
5462 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005463 char_u *
5464replace_termcodes(from, bufp, from_part, do_lt, special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 char_u *from;
5466 char_u **bufp;
5467 int from_part;
5468 int do_lt; /* also translate <lt> */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005469 int special; /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470{
5471 int i;
5472 int slen;
5473 int key;
5474 int dlen = 0;
5475 char_u *src;
5476 int do_backslash; /* backslash is a special character */
5477 int do_special; /* recognize <> key codes */
5478 int do_key_code; /* recognize raw key codes */
5479 char_u *result; /* buffer for resulting string */
5480
5481 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005482 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5484
5485 /*
5486 * Allocate space for the translation. Worst case a single character is
5487 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5488 */
5489 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5490 if (result == NULL) /* out of memory */
5491 {
5492 *bufp = NULL;
5493 return from;
5494 }
5495
5496 src = from;
5497
5498 /*
5499 * Check for #n at start only: function key n
5500 */
5501 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5502 {
5503 result[dlen++] = K_SPECIAL;
5504 result[dlen++] = 'k';
5505 if (src[1] == '0')
5506 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5507 else
5508 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5509 src += 2;
5510 }
5511
5512 /*
5513 * Copy each byte from *from to result[dlen]
5514 */
5515 while (*src != NUL)
5516 {
5517 /*
5518 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005519 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 */
5521 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5522 {
5523#ifdef FEAT_EVAL
5524 /*
5525 * Replace <SID> by K_SNR <script-nr> _.
5526 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5527 */
5528 if (STRNICMP(src, "<SID>", 5) == 0)
5529 {
5530 if (current_SID <= 0)
5531 EMSG(_(e_usingsid));
5532 else
5533 {
5534 src += 5;
5535 result[dlen++] = K_SPECIAL;
5536 result[dlen++] = (int)KS_EXTRA;
5537 result[dlen++] = (int)KE_SNR;
5538 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5539 dlen += (int)STRLEN(result + dlen);
5540 result[dlen++] = '_';
5541 continue;
5542 }
5543 }
5544#endif
5545
5546 slen = trans_special(&src, result + dlen, TRUE);
5547 if (slen)
5548 {
5549 dlen += slen;
5550 continue;
5551 }
5552 }
5553
5554 /*
5555 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5556 * Note that this is also checked after replacing the <> form.
5557 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5558 * it could be a character in the file.
5559 */
5560 if (do_key_code)
5561 {
5562 i = find_term_bykeys(src);
5563 if (i >= 0)
5564 {
5565 result[dlen++] = K_SPECIAL;
5566 result[dlen++] = termcodes[i].name[0];
5567 result[dlen++] = termcodes[i].name[1];
5568 src += termcodes[i].len;
5569 /* If terminal code matched, continue after it. */
5570 continue;
5571 }
5572 }
5573
5574#ifdef FEAT_EVAL
5575 if (do_special)
5576 {
5577 char_u *p, *s, len;
5578
5579 /*
5580 * Replace <Leader> by the value of "mapleader".
5581 * Replace <LocalLeader> by the value of "maplocalleader".
5582 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5583 */
5584 if (STRNICMP(src, "<Leader>", 8) == 0)
5585 {
5586 len = 8;
5587 p = get_var_value((char_u *)"g:mapleader");
5588 }
5589 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5590 {
5591 len = 13;
5592 p = get_var_value((char_u *)"g:maplocalleader");
5593 }
5594 else
5595 {
5596 len = 0;
5597 p = NULL;
5598 }
5599 if (len != 0)
5600 {
5601 /* Allow up to 8 * 6 characters for "mapleader". */
5602 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5603 s = (char_u *)"\\";
5604 else
5605 s = p;
5606 while (*s != NUL)
5607 result[dlen++] = *s++;
5608 src += len;
5609 continue;
5610 }
5611 }
5612#endif
5613
5614 /*
5615 * Remove CTRL-V and ignore the next character.
5616 * For "from" side the CTRL-V at the end is included, for the "to"
5617 * part it is removed.
5618 * If 'cpoptions' does not contain 'B', also accept a backslash.
5619 */
5620 key = *src;
5621 if (key == Ctrl_V || (do_backslash && key == '\\'))
5622 {
5623 ++src; /* skip CTRL-V or backslash */
5624 if (*src == NUL)
5625 {
5626 if (from_part)
5627 result[dlen++] = key;
5628 break;
5629 }
5630 }
5631
5632#ifdef FEAT_MBYTE
5633 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005634 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635#endif
5636 {
5637 /*
5638 * If the character is K_SPECIAL, replace it with K_SPECIAL
5639 * KS_SPECIAL KE_FILLER.
5640 * If compiled with the GUI replace CSI with K_CSI.
5641 */
5642 if (*src == K_SPECIAL)
5643 {
5644 result[dlen++] = K_SPECIAL;
5645 result[dlen++] = KS_SPECIAL;
5646 result[dlen++] = KE_FILLER;
5647 }
5648# ifdef FEAT_GUI
5649 else if (*src == CSI)
5650 {
5651 result[dlen++] = K_SPECIAL;
5652 result[dlen++] = KS_EXTRA;
5653 result[dlen++] = (int)KE_CSI;
5654 }
5655# endif
5656 else
5657 result[dlen++] = *src;
5658 ++src;
5659 }
5660 }
5661 result[dlen] = NUL;
5662
5663 /*
5664 * Copy the new string to allocated memory.
5665 * If this fails, just return from.
5666 */
5667 if ((*bufp = vim_strsave(result)) != NULL)
5668 from = *bufp;
5669 vim_free(result);
5670 return from;
5671}
5672
5673/*
5674 * Find a termcode with keys 'src' (must be NUL terminated).
5675 * Return the index in termcodes[], or -1 if not found.
5676 */
5677 int
5678find_term_bykeys(src)
5679 char_u *src;
5680{
5681 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005682 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683
5684 for (i = 0; i < tc_len; ++i)
5685 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005686 if (slen == termcodes[i].len
5687 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 return i;
5689 }
5690 return -1;
5691}
5692
5693/*
5694 * Gather the first characters in the terminal key codes into a string.
5695 * Used to speed up check_termcode().
5696 */
5697 static void
5698gather_termleader()
5699{
5700 int i;
5701 int len = 0;
5702
5703#ifdef FEAT_GUI
5704 if (gui.in_use)
5705 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5706#endif
5707#ifdef FEAT_TERMRESPONSE
5708 if (check_for_codes)
5709 termleader[len++] = DCS; /* the termcode response starts with DCS
5710 in 8-bit mode */
5711#endif
5712 termleader[len] = NUL;
5713
5714 for (i = 0; i < tc_len; ++i)
5715 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5716 {
5717 termleader[len++] = termcodes[i].code[0];
5718 termleader[len] = NUL;
5719 }
5720
5721 need_gather = FALSE;
5722}
5723
5724/*
5725 * Show all termcodes (for ":set termcap")
5726 * This code looks a lot like showoptions(), but is different.
5727 */
5728 void
5729show_termcodes()
5730{
5731 int col;
5732 int *items;
5733 int item_count;
5734 int run;
5735 int row, rows;
5736 int cols;
5737 int i;
5738 int len;
5739
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005740#define INC3 27 /* try to make three columns */
5741#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742#define GAP 2 /* spaces between columns */
5743
5744 if (tc_len == 0) /* no terminal codes (must be GUI) */
5745 return;
5746 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5747 if (items == NULL)
5748 return;
5749
5750 /* Highlight title */
5751 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5752
5753 /*
5754 * do the loop two times:
5755 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005756 * 2. display the medium items (medium length strings)
5757 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005759 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 {
5761 /*
5762 * collect the items in items[]
5763 */
5764 item_count = 0;
5765 for (i = 0; i < tc_len; i++)
5766 {
5767 len = show_one_termcode(termcodes[i].name,
5768 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005769 if (len <= INC3 - GAP ? run == 1
5770 : len <= INC2 - GAP ? run == 2
5771 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 items[item_count++] = i;
5773 }
5774
5775 /*
5776 * display the items
5777 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005778 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005780 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 if (cols == 0)
5782 cols = 1;
5783 rows = (item_count + cols - 1) / cols;
5784 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005785 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 rows = item_count;
5787 for (row = 0; row < rows && !got_int; ++row)
5788 {
5789 msg_putchar('\n'); /* go to next line */
5790 if (got_int) /* 'q' typed in more */
5791 break;
5792 col = 0;
5793 for (i = row; i < item_count; i += rows)
5794 {
5795 msg_col = col; /* make columns */
5796 show_one_termcode(termcodes[items[i]].name,
5797 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005798 if (run == 2)
5799 col += INC2;
5800 else
5801 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 }
5803 out_flush();
5804 ui_breakcheck();
5805 }
5806 }
5807 vim_free(items);
5808}
5809
5810/*
5811 * Show one termcode entry.
5812 * Output goes into IObuff[]
5813 */
5814 int
5815show_one_termcode(name, code, printit)
5816 char_u *name;
5817 char_u *code;
5818 int printit;
5819{
5820 char_u *p;
5821 int len;
5822
5823 if (name[0] > '~')
5824 {
5825 IObuff[0] = ' ';
5826 IObuff[1] = ' ';
5827 IObuff[2] = ' ';
5828 IObuff[3] = ' ';
5829 }
5830 else
5831 {
5832 IObuff[0] = 't';
5833 IObuff[1] = '_';
5834 IObuff[2] = name[0];
5835 IObuff[3] = name[1];
5836 }
5837 IObuff[4] = ' ';
5838
5839 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5840 if (p[1] != 't')
5841 STRCPY(IObuff + 5, p);
5842 else
5843 IObuff[5] = NUL;
5844 len = (int)STRLEN(IObuff);
5845 do
5846 IObuff[len++] = ' ';
5847 while (len < 17);
5848 IObuff[len] = NUL;
5849 if (code == NULL)
5850 len += 4;
5851 else
5852 len += vim_strsize(code);
5853
5854 if (printit)
5855 {
5856 msg_puts(IObuff);
5857 if (code == NULL)
5858 msg_puts((char_u *)"NULL");
5859 else
5860 msg_outtrans(code);
5861 }
5862 return len;
5863}
5864
5865#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5866/*
5867 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5868 * termcap codes from the terminal itself.
5869 * We get them one by one to avoid a very long response string.
5870 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005871static int xt_index_in = 0;
5872static int xt_index_out = 0;
5873
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 static void
5875req_codes_from_term()
5876{
5877 xt_index_out = 0;
5878 xt_index_in = 0;
5879 req_more_codes_from_term();
5880}
5881
5882 static void
5883req_more_codes_from_term()
5884{
5885 char buf[11];
5886 int old_idx = xt_index_out;
5887
5888 /* Don't do anything when going to exit. */
5889 if (exiting)
5890 return;
5891
5892 /* Send up to 10 more requests out than we received. Avoid sending too
5893 * many, there can be a buffer overflow somewhere. */
5894 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5895 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02005896# ifdef DEBUG_TERMRESPONSE
5897 char dbuf[100];
5898
5899 sprintf(dbuf, "Requesting XT %d: %s",
5900 xt_index_out, key_names[xt_index_out]);
5901 log_tr(dbuf);
5902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 sprintf(buf, "\033P+q%02x%02x\033\\",
5904 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5905 out_str_nf((char_u *)buf);
5906 ++xt_index_out;
5907 }
5908
5909 /* Send the codes out right away. */
5910 if (xt_index_out != old_idx)
5911 out_flush();
5912}
5913
5914/*
5915 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5916 * A "0" instead of the "1" indicates a code that isn't supported.
5917 * Both <name> and <string> are encoded in hex.
5918 * "code" points to the "0" or "1".
5919 */
5920 static void
5921got_code_from_term(code, len)
5922 char_u *code;
5923 int len;
5924{
5925#define XT_LEN 100
5926 char_u name[3];
5927 char_u str[XT_LEN];
5928 int i;
5929 int j = 0;
5930 int c;
5931
5932 /* A '1' means the code is supported, a '0' means it isn't.
5933 * When half the length is > XT_LEN we can't use it.
5934 * Our names are currently all 2 characters. */
5935 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5936 {
5937 /* Get the name from the response and find it in the table. */
5938 name[0] = hexhex2nr(code + 3);
5939 name[1] = hexhex2nr(code + 5);
5940 name[2] = NUL;
5941 for (i = 0; key_names[i] != NULL; ++i)
5942 {
5943 if (STRCMP(key_names[i], name) == 0)
5944 {
5945 xt_index_in = i;
5946 break;
5947 }
5948 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005949# ifdef DEBUG_TERMRESPONSE
5950 {
5951 char buf[100];
5952
5953 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
5954 log_tr(buf);
5955 }
5956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 if (key_names[i] != NULL)
5958 {
5959 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5960 str[j++] = c;
5961 str[j] = NUL;
5962 if (name[0] == 'C' && name[1] == 'o')
5963 {
5964 /* Color count is not a key code. */
5965 i = atoi((char *)str);
5966 if (i != t_colors)
5967 {
5968 /* Nr of colors changed, initialize highlighting and
Bram Moolenaar238a5642006-02-21 22:12:05 +00005969 * redraw everything. This causes a redraw, which usually
5970 * clears the message. Try keeping the message if it
5971 * might work. */
5972 set_keep_msg_from_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 set_color_count(i);
5974 init_highlight(TRUE, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +02005975#ifdef DEBUG_TERMRESPONSE
5976 {
5977 char buf[100];
5978 int r = redraw_asap(CLEAR);
5979
5980 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
5981 log_tr(buf);
5982 }
5983#else
5984 redraw_asap(CLEAR);
5985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 }
5987 }
5988 else
5989 {
5990 /* First delete any existing entry with the same code. */
5991 i = find_term_bykeys(str);
5992 if (i >= 0)
5993 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005994 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 }
5996 }
5997 }
5998
5999 /* May request more codes now that we received one. */
6000 ++xt_index_in;
6001 req_more_codes_from_term();
6002}
6003
6004/*
6005 * Check if there are any unanswered requests and deal with them.
6006 * This is called before starting an external program or getting direct
6007 * keyboard input. We don't want responses to be send to that program or
6008 * handled as typed text.
6009 */
6010 static void
6011check_for_codes_from_term()
6012{
6013 int c;
6014
6015 /* If no codes requested or all are answered, no need to wait. */
6016 if (xt_index_out == 0 || xt_index_out == xt_index_in)
6017 return;
6018
6019 /* Vgetc() will check for and handle any response.
6020 * Keep calling vpeekc() until we don't get any responses. */
6021 ++no_mapping;
6022 ++allow_keys;
6023 for (;;)
6024 {
6025 c = vpeekc();
6026 if (c == NUL) /* nothing available */
6027 break;
6028
6029 /* If a response is recognized it's replaced with K_IGNORE, must read
6030 * it from the input stream. If there is no K_IGNORE we can't do
6031 * anything, break here (there might be some responses further on, but
6032 * we don't want to throw away any typed chars). */
6033 if (c != K_SPECIAL && c != K_IGNORE)
6034 break;
6035 c = vgetc();
6036 if (c != K_IGNORE)
6037 {
6038 vungetc(c);
6039 break;
6040 }
6041 }
6042 --no_mapping;
6043 --allow_keys;
6044}
6045#endif
6046
6047#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6048/*
6049 * Translate an internal mapping/abbreviation representation into the
6050 * corresponding external one recognized by :map/:abbrev commands;
6051 * respects the current B/k/< settings of 'cpoption'.
6052 *
6053 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00006054 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 *
6056 * It uses a growarray to build the translation string since the
6057 * latter can be wider than the original description. The caller has to
6058 * free the string afterwards.
6059 *
6060 * Returns NULL when there is a problem.
6061 */
6062 char_u *
6063translate_mapping(str, expmap)
6064 char_u *str;
6065 int expmap; /* TRUE when expanding mappings on command-line */
6066{
6067 garray_T ga;
6068 int c;
6069 int modifiers;
6070 int cpo_bslash;
6071 int cpo_special;
6072 int cpo_keycode;
6073
6074 ga_init(&ga);
6075 ga.ga_itemsize = 1;
6076 ga.ga_growsize = 40;
6077
6078 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
6079 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
6080 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
6081
6082 for (; *str; ++str)
6083 {
6084 c = *str;
6085 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6086 {
6087 modifiers = 0;
6088 if (str[1] == KS_MODIFIER)
6089 {
6090 str++;
6091 modifiers = *++str;
6092 c = *++str;
6093 }
6094 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
6095 {
6096 int i;
6097
6098 /* try to find special key in termcodes */
6099 for (i = 0; i < tc_len; ++i)
6100 if (termcodes[i].name[0] == str[1]
6101 && termcodes[i].name[1] == str[2])
6102 break;
6103 if (i < tc_len)
6104 {
6105 ga_concat(&ga, termcodes[i].code);
6106 str += 2;
6107 continue; /* for (str) */
6108 }
6109 }
6110 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6111 {
6112 if (expmap && cpo_special)
6113 {
6114 ga_clear(&ga);
6115 return NULL;
6116 }
6117 c = TO_SPECIAL(str[1], str[2]);
6118 if (c == K_ZERO) /* display <Nul> as ^@ */
6119 c = NUL;
6120 str += 2;
6121 }
6122 if (IS_SPECIAL(c) || modifiers) /* special key */
6123 {
6124 if (expmap && cpo_special)
6125 {
6126 ga_clear(&ga);
6127 return NULL;
6128 }
6129 ga_concat(&ga, get_special_key_name(c, modifiers));
6130 continue; /* for (str) */
6131 }
6132 }
6133 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6134 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6135 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6136 if (c)
6137 ga_append(&ga, c);
6138 }
6139 ga_append(&ga, NUL);
6140 return (char_u *)(ga.ga_data);
6141}
6142#endif
6143
6144#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6145static char ksme_str[20];
6146static char ksmr_str[20];
6147static char ksmd_str[20];
6148
6149/*
6150 * For Win32 console: update termcap codes for existing console attributes.
6151 */
6152 void
6153update_tcap(attr)
6154 int attr;
6155{
6156 struct builtin_term *p;
6157
6158 p = find_builtin_term(DEFAULT_TERM);
6159 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6160 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6161 attr | 0x08); /* FOREGROUND_INTENSITY */
6162 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6163 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6164
6165 while (p->bt_string != NULL)
6166 {
6167 if (p->bt_entry == (int)KS_ME)
6168 p->bt_string = &ksme_str[0];
6169 else if (p->bt_entry == (int)KS_MR)
6170 p->bt_string = &ksmr_str[0];
6171 else if (p->bt_entry == (int)KS_MD)
6172 p->bt_string = &ksmd_str[0];
6173 ++p;
6174 }
6175}
6176#endif