blob: ffe9d43f2b4a4be958f4ea811a66092be51a8fa0 [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 Moolenaar071d4272004-06-13 20:20:40 +0000127# endif
128
129/*
130 * Don't declare these variables if termcap.h contains them.
131 * Autoconf checks if these variables should be declared extern (not all
132 * systems have them).
133 * Some versions define ospeed to be speed_t, but that is incompatible with
134 * BSD, where ospeed is short and speed_t is long.
135 */
136# ifndef HAVE_OSPEED
137# ifdef OSPEED_EXTERN
138extern short ospeed;
139# else
140short ospeed;
141# endif
142# endif
143# ifndef HAVE_UP_BC_PC
144# ifdef UP_BC_PC_EXTERN
145extern char *UP, *BC, PC;
146# else
147char *UP, *BC, PC;
148# endif
149# endif
150
151# define TGETSTR(s, p) vim_tgetstr((s), (p))
152# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
153static char_u *vim_tgetstr __ARGS((char *s, char_u **pp));
154#endif /* HAVE_TGETENT */
155
156static int detected_8bit = FALSE; /* detected 8-bit terminal */
157
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000158static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159{
160
161#if defined(FEAT_GUI)
162/*
163 * GUI pseudo term-cap.
164 */
165 {(int)KS_NAME, "gui"},
166 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
167 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
168# ifdef TERMINFO
169 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
170# else
171 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
172# endif
173 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
174# ifdef TERMINFO
175 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
176 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
177# ifdef FEAT_VERTSPLIT
178 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
179# endif
180# else
181 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
182 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
183# ifdef FEAT_VERTSPLIT
184 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
185# endif
186# endif
187 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
188 /* attributes switched on with 'h', off with * 'H' */
189 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
190 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
191 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
192 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
193 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
194 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
195 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000196 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
197 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
199 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
200 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
201 {(int)KS_MS, "y"},
202 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100203 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 {(int)KS_LE, "\b"}, /* cursor-left = BS */
205 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
206# ifdef TERMINFO
207 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
208# else
209 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
210# endif
211 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000212 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#endif
214
215#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
217# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
218/*
219 * Amiga console window, default for Amiga
220 */
221 {(int)KS_NAME, "amiga"},
222 {(int)KS_CE, "\033[K"},
223 {(int)KS_CD, "\033[J"},
224 {(int)KS_AL, "\033[L"},
225# ifdef TERMINFO
226 {(int)KS_CAL, "\033[%p1%dL"},
227# else
228 {(int)KS_CAL, "\033[%dL"},
229# endif
230 {(int)KS_DL, "\033[M"},
231# ifdef TERMINFO
232 {(int)KS_CDL, "\033[%p1%dM"},
233# else
234 {(int)KS_CDL, "\033[%dM"},
235# endif
236 {(int)KS_CL, "\014"},
237 {(int)KS_VI, "\033[0 p"},
238 {(int)KS_VE, "\033[1 p"},
239 {(int)KS_ME, "\033[0m"},
240 {(int)KS_MR, "\033[7m"},
241 {(int)KS_MD, "\033[1m"},
242 {(int)KS_SE, "\033[0m"},
243 {(int)KS_SO, "\033[33m"},
244 {(int)KS_US, "\033[4m"},
245 {(int)KS_UE, "\033[0m"},
246 {(int)KS_CZH, "\033[3m"},
247 {(int)KS_CZR, "\033[0m"},
248#if defined(__MORPHOS__) || defined(__AROS__)
249 {(int)KS_CCO, "8"}, /* allow 8 colors */
250# ifdef TERMINFO
251 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
252 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
253# else
254 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
255 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
256# endif
257 {(int)KS_OP, "\033[m"}, /* reset colors */
258#endif
259 {(int)KS_MS, "y"},
260 {(int)KS_UT, "y"}, /* guessed */
261 {(int)KS_LE, "\b"},
262# ifdef TERMINFO
263 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
264# else
265 {(int)KS_CM, "\033[%i%d;%dH"},
266# endif
267#if defined(__MORPHOS__)
268 {(int)KS_SR, "\033M"},
269#endif
270# ifdef TERMINFO
271 {(int)KS_CRI, "\033[%p1%dC"},
272# else
273 {(int)KS_CRI, "\033[%dC"},
274# endif
275 {K_UP, "\233A"},
276 {K_DOWN, "\233B"},
277 {K_LEFT, "\233D"},
278 {K_RIGHT, "\233C"},
279 {K_S_UP, "\233T"},
280 {K_S_DOWN, "\233S"},
281 {K_S_LEFT, "\233 A"},
282 {K_S_RIGHT, "\233 @"},
283 {K_S_TAB, "\233Z"},
284 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
285 {K_F2, "\233\061~"},
286 {K_F3, "\233\062~"},
287 {K_F4, "\233\063~"},
288 {K_F5, "\233\064~"},
289 {K_F6, "\233\065~"},
290 {K_F7, "\233\066~"},
291 {K_F8, "\233\067~"},
292 {K_F9, "\233\070~"},
293 {K_F10, "\233\071~"},
294 {K_S_F1, "\233\061\060~"},
295 {K_S_F2, "\233\061\061~"},
296 {K_S_F3, "\233\061\062~"},
297 {K_S_F4, "\233\061\063~"},
298 {K_S_F5, "\233\061\064~"},
299 {K_S_F6, "\233\061\065~"},
300 {K_S_F7, "\233\061\066~"},
301 {K_S_F8, "\233\061\067~"},
302 {K_S_F9, "\233\061\070~"},
303 {K_S_F10, "\233\061\071~"},
304 {K_HELP, "\233?~"},
305 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
306 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
307 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
308 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
309 {K_END, "\233\064\065~"}, /* 101 key keyboard */
310
311 {BT_EXTRA_KEYS, ""},
312 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
313 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
314 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
315# endif
316
317# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
318/*
319 * almost standard ANSI terminal, default for bebox
320 */
321 {(int)KS_NAME, "beos-ansi"},
322 {(int)KS_CE, "\033[K"},
323 {(int)KS_CD, "\033[J"},
324 {(int)KS_AL, "\033[L"},
325# ifdef TERMINFO
326 {(int)KS_CAL, "\033[%p1%dL"},
327# else
328 {(int)KS_CAL, "\033[%dL"},
329# endif
330 {(int)KS_DL, "\033[M"},
331# ifdef TERMINFO
332 {(int)KS_CDL, "\033[%p1%dM"},
333# else
334 {(int)KS_CDL, "\033[%dM"},
335# endif
336#ifdef BEOS_PR_OR_BETTER
337# ifdef TERMINFO
338 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
339# else
340 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
341# endif
342#endif
343 {(int)KS_CL, "\033[H\033[2J"},
344#ifdef notyet
345 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
346 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
347#endif
348 {(int)KS_ME, "\033[m"}, /* normal mode */
349 {(int)KS_MR, "\033[7m"}, /* reverse */
350 {(int)KS_MD, "\033[1m"}, /* bold */
351 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
352 {(int)KS_SE, "\033[m"}, /* standout end */
353 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
354 {(int)KS_CZR, "\033[m"}, /* italic end */
355 {(int)KS_US, "\033[4m"}, /* underscore mode */
356 {(int)KS_UE, "\033[m"}, /* underscore end */
357 {(int)KS_CCO, "8"}, /* allow 8 colors */
358# ifdef TERMINFO
359 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
360 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
361# else
362 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
363 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
364# endif
365 {(int)KS_OP, "\033[m"}, /* reset colors */
366 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
367 {(int)KS_UT, "y"}, /* guessed */
368 {(int)KS_LE, "\b"},
369# ifdef TERMINFO
370 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
371# else
372 {(int)KS_CM, "\033[%i%d;%dH"},
373# endif
374 {(int)KS_SR, "\033M"},
375# ifdef TERMINFO
376 {(int)KS_CRI, "\033[%p1%dC"},
377# else
378 {(int)KS_CRI, "\033[%dC"},
379# endif
380#if defined(BEOS_DR8)
381 {(int)KS_DB, ""}, /* hack! see screen.c */
382#endif
383
384 {K_UP, "\033[A"},
385 {K_DOWN, "\033[B"},
386 {K_LEFT, "\033[D"},
387 {K_RIGHT, "\033[C"},
388# endif
389
390# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) || defined(__EMX__)
391/*
392 * standard ANSI terminal, default for unix
393 */
394 {(int)KS_NAME, "ansi"},
395 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
396 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
397# ifdef TERMINFO
398 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
399# else
400 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
401# endif
402 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
403# ifdef TERMINFO
404 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
405# else
406 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
407# endif
408 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
409 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
410 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
411 {(int)KS_MS, "y"},
412 {(int)KS_UT, "y"}, /* guessed */
413 {(int)KS_LE, "\b"},
414# ifdef TERMINFO
415 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
416# else
417 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
418# endif
419# ifdef TERMINFO
420 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
421# else
422 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
423# endif
424# endif
425
426# if defined(MSDOS) || defined(ALL_BUILTIN_TCAPS) || defined(__EMX__)
427/*
428 * These codes are valid when nansi.sys or equivalent has been installed.
429 * Function keys on a PC are preceded with a NUL. These are converted into
430 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
431 * CTRL-arrow is used instead of SHIFT-arrow.
432 */
433#ifdef __EMX__
434 {(int)KS_NAME, "os2ansi"},
435#else
436 {(int)KS_NAME, "pcansi"},
437 {(int)KS_DL, "\033[M"},
438 {(int)KS_AL, "\033[L"},
439#endif
440 {(int)KS_CE, "\033[K"},
441 {(int)KS_CL, "\033[2J"},
442 {(int)KS_ME, "\033[0m"},
443 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
444 {(int)KS_MD, "\033[1m"}, /* bold: white text */
445 {(int)KS_SE, "\033[0m"}, /* standout end */
446 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
447 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
448 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
449 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
450 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
451 {(int)KS_CCO, "8"}, /* allow 8 colors */
452# ifdef TERMINFO
453 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
454 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
455# else
456 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
457 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
458# endif
459 {(int)KS_OP, "\033[0m"}, /* reset colors */
460 {(int)KS_MS, "y"},
461 {(int)KS_UT, "y"}, /* guessed */
462 {(int)KS_LE, "\b"},
463# ifdef TERMINFO
464 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
465# else
466 {(int)KS_CM, "\033[%i%d;%dH"},
467# endif
468# ifdef TERMINFO
469 {(int)KS_CRI, "\033[%p1%dC"},
470# else
471 {(int)KS_CRI, "\033[%dC"},
472# endif
473 {K_UP, "\316H"},
474 {K_DOWN, "\316P"},
475 {K_LEFT, "\316K"},
476 {K_RIGHT, "\316M"},
477 {K_S_LEFT, "\316s"},
478 {K_S_RIGHT, "\316t"},
479 {K_F1, "\316;"},
480 {K_F2, "\316<"},
481 {K_F3, "\316="},
482 {K_F4, "\316>"},
483 {K_F5, "\316?"},
484 {K_F6, "\316@"},
485 {K_F7, "\316A"},
486 {K_F8, "\316B"},
487 {K_F9, "\316C"},
488 {K_F10, "\316D"},
489 {K_F11, "\316\205"}, /* guessed */
490 {K_F12, "\316\206"}, /* guessed */
491 {K_S_F1, "\316T"},
492 {K_S_F2, "\316U"},
493 {K_S_F3, "\316V"},
494 {K_S_F4, "\316W"},
495 {K_S_F5, "\316X"},
496 {K_S_F6, "\316Y"},
497 {K_S_F7, "\316Z"},
498 {K_S_F8, "\316["},
499 {K_S_F9, "\316\\"},
500 {K_S_F10, "\316]"},
501 {K_S_F11, "\316\207"}, /* guessed */
502 {K_S_F12, "\316\210"}, /* guessed */
503 {K_INS, "\316R"},
504 {K_DEL, "\316S"},
505 {K_HOME, "\316G"},
506 {K_END, "\316O"},
507 {K_PAGEDOWN, "\316Q"},
508 {K_PAGEUP, "\316I"},
509# endif
510
511# if defined(MSDOS)
512/*
513 * These codes are valid for the pc video. The entries that start with ESC |
514 * are translated into conio calls in os_msdos.c. Default for MSDOS.
515 */
516 {(int)KS_NAME, "pcterm"},
517 {(int)KS_CE, "\033|K"},
518 {(int)KS_AL, "\033|L"},
519 {(int)KS_DL, "\033|M"},
520# ifdef TERMINFO
521 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},
522# ifdef FEAT_VERTSPLIT
523 {(int)KS_CSV, "\033|%i%p1%d;%p2%dV"},
524# endif
525# else
526 {(int)KS_CS, "\033|%i%d;%dr"},
527# ifdef FEAT_VERTSPLIT
528 {(int)KS_CSV, "\033|%i%d;%dV"},
529# endif
530# endif
531 {(int)KS_CL, "\033|J"},
532 {(int)KS_ME, "\033|0m"}, /* normal */
533 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgrey */
534 {(int)KS_MD, "\033|15m"}, /* bold: white text */
535 {(int)KS_SE, "\033|0m"}, /* standout end */
536 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
537 {(int)KS_CZH, "\033|225m"}, /* italic mode: blue text on yellow */
538 {(int)KS_CZR, "\033|0m"}, /* italic mode end */
539 {(int)KS_US, "\033|67m"}, /* underscore mode: cyan text on red */
540 {(int)KS_UE, "\033|0m"}, /* underscore mode end */
541 {(int)KS_CCO, "16"}, /* allow 16 colors */
542# ifdef TERMINFO
543 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
544 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
545# else
546 {(int)KS_CAB, "\033|%db"}, /* set background color */
547 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
548# endif
549 {(int)KS_MS, "y"},
550 {(int)KS_UT, "y"},
551 {(int)KS_LE, "\b"},
552# ifdef TERMINFO
553 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},
554# else
555 {(int)KS_CM, "\033|%i%d;%dH"},
556# endif
557#ifdef DJGPP
558 {(int)KS_VB, "\033|B"}, /* visual bell */
559#endif
560 {K_UP, "\316H"},
561 {K_DOWN, "\316P"},
562 {K_LEFT, "\316K"},
563 {K_RIGHT, "\316M"},
564 {K_S_LEFT, "\316s"},
565 {K_S_RIGHT, "\316t"},
566 {K_S_TAB, "\316\017"},
567 {K_F1, "\316;"},
568 {K_F2, "\316<"},
569 {K_F3, "\316="},
570 {K_F4, "\316>"},
571 {K_F5, "\316?"},
572 {K_F6, "\316@"},
573 {K_F7, "\316A"},
574 {K_F8, "\316B"},
575 {K_F9, "\316C"},
576 {K_F10, "\316D"},
577 {K_F11, "\316\205"},
578 {K_F12, "\316\206"},
579 {K_S_F1, "\316T"},
580 {K_S_F2, "\316U"},
581 {K_S_F3, "\316V"},
582 {K_S_F4, "\316W"},
583 {K_S_F5, "\316X"},
584 {K_S_F6, "\316Y"},
585 {K_S_F7, "\316Z"},
586 {K_S_F8, "\316["},
587 {K_S_F9, "\316\\"},
588 {K_S_F10, "\316]"},
589 {K_S_F11, "\316\207"},
590 {K_S_F12, "\316\210"},
591 {K_INS, "\316R"},
592 {K_DEL, "\316S"},
593 {K_HOME, "\316G"},
594 {K_END, "\316O"},
595 {K_PAGEDOWN, "\316Q"},
596 {K_PAGEUP, "\316I"},
597 {K_KPLUS, "\316N"},
598 {K_KMINUS, "\316J"},
599 {K_KMULTIPLY, "\3167"},
600 {K_K0, "\316\332"},
601 {K_K1, "\316\336"},
602 {K_K2, "\316\342"},
603 {K_K3, "\316\346"},
604 {K_K4, "\316\352"},
605 {K_K5, "\316\356"},
606 {K_K6, "\316\362"},
607 {K_K7, "\316\366"},
608 {K_K8, "\316\372"},
609 {K_K9, "\316\376"},
610# endif
611
612# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS) || defined(__EMX__)
613/*
614 * These codes are valid for the Win32 Console . The entries that start with
615 * ESC | are translated into console calls in os_win32.c. The function keys
616 * are also translated in os_win32.c.
617 */
618 {(int)KS_NAME, "win32"},
619 {(int)KS_CE, "\033|K"}, /* clear to end of line */
620 {(int)KS_AL, "\033|L"}, /* add new blank line */
621# ifdef TERMINFO
622 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
623# else
624 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
625# endif
626 {(int)KS_DL, "\033|M"}, /* delete line */
627# ifdef TERMINFO
628 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
629# else
630 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
631# endif
632 {(int)KS_CL, "\033|J"}, /* clear screen */
633 {(int)KS_CD, "\033|j"}, /* clear to end of display */
634 {(int)KS_VI, "\033|v"}, /* cursor invisible */
635 {(int)KS_VE, "\033|V"}, /* cursor visible */
636
637 {(int)KS_ME, "\033|0m"}, /* normal */
638 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
639 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
640#if 1
641 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
642 {(int)KS_SE, "\033|0m"}, /* standout end */
643#else
644 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
645 {(int)KS_SE, "\033|f"}, /* standout end */
646#endif
647 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
648 {(int)KS_CZR, "\033|0m"}, /* italic end */
649 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
650 {(int)KS_UE, "\033|0m"}, /* underscore end */
651 {(int)KS_CCO, "16"}, /* allow 16 colors */
652# ifdef TERMINFO
653 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
654 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
655# else
656 {(int)KS_CAB, "\033|%db"}, /* set background color */
657 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
658# endif
659
660 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
661 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100662 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 {(int)KS_LE, "\b"},
664# ifdef TERMINFO
665 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
666# else
667 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
668# endif
669 {(int)KS_VB, "\033|B"}, /* visual bell */
670 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
671 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
672# ifdef TERMINFO
673 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
674# else
675 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
676# endif
677
678 {K_UP, "\316H"},
679 {K_DOWN, "\316P"},
680 {K_LEFT, "\316K"},
681 {K_RIGHT, "\316M"},
682 {K_S_UP, "\316\304"},
683 {K_S_DOWN, "\316\317"},
684 {K_S_LEFT, "\316\311"},
685 {K_C_LEFT, "\316s"},
686 {K_S_RIGHT, "\316\313"},
687 {K_C_RIGHT, "\316t"},
688 {K_S_TAB, "\316\017"},
689 {K_F1, "\316;"},
690 {K_F2, "\316<"},
691 {K_F3, "\316="},
692 {K_F4, "\316>"},
693 {K_F5, "\316?"},
694 {K_F6, "\316@"},
695 {K_F7, "\316A"},
696 {K_F8, "\316B"},
697 {K_F9, "\316C"},
698 {K_F10, "\316D"},
699 {K_F11, "\316\205"},
700 {K_F12, "\316\206"},
701 {K_S_F1, "\316T"},
702 {K_S_F2, "\316U"},
703 {K_S_F3, "\316V"},
704 {K_S_F4, "\316W"},
705 {K_S_F5, "\316X"},
706 {K_S_F6, "\316Y"},
707 {K_S_F7, "\316Z"},
708 {K_S_F8, "\316["},
709 {K_S_F9, "\316\\"},
710 {K_S_F10, "\316]"},
711 {K_S_F11, "\316\207"},
712 {K_S_F12, "\316\210"},
713 {K_INS, "\316R"},
714 {K_DEL, "\316S"},
715 {K_HOME, "\316G"},
716 {K_S_HOME, "\316\302"},
717 {K_C_HOME, "\316w"},
718 {K_END, "\316O"},
719 {K_S_END, "\316\315"},
720 {K_C_END, "\316u"},
721 {K_PAGEDOWN, "\316Q"},
722 {K_PAGEUP, "\316I"},
723 {K_KPLUS, "\316N"},
724 {K_KMINUS, "\316J"},
725 {K_KMULTIPLY, "\316\067"},
726 {K_K0, "\316\332"},
727 {K_K1, "\316\336"},
728 {K_K2, "\316\342"},
729 {K_K3, "\316\346"},
730 {K_K4, "\316\352"},
731 {K_K5, "\316\356"},
732 {K_K6, "\316\362"},
733 {K_K7, "\316\366"},
734 {K_K8, "\316\372"},
735 {K_K9, "\316\376"},
736# endif
737
738# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
739/*
740 * VT320 is working as an ANSI terminal compatible DEC terminal.
741 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
742 * Note: K_F1...K_F5 are for internal use, should not be defined.
743 * TODO:- rewrite ESC[ codes to CSI
744 * - keyboard languages (CSI ? 26 n)
745 */
746 {(int)KS_NAME, "vt320"},
747 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
748 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
749# ifdef TERMINFO
750 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
751# else
752 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
753# endif
754 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
755# ifdef TERMINFO
756 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
757# else
758 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
759# endif
760 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000761 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
762 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
764 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000765 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
766 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
767 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
768 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
769 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
770 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
771 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
772 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
773 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
774 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 {(int)KS_MS, "y"},
776 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100777 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {(int)KS_LE, "\b"},
779# ifdef TERMINFO
780 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
781 ESC_STR "[%i%p1%d;%p2%dH")},
782# else
783 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
784# endif
785# ifdef TERMINFO
786 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
787# else
788 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
789# endif
790 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
791 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
792 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
793 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000794 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
795 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
796 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
797 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
798 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
800 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
801 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
802 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
803 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000804 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
806 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
807 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
808 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
809 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
810 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
811 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
812 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
813 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
814 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
815 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
816 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
817 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
818 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
819 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
820 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
821 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
822 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
823 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
824 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
825 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
826# endif
827
828# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
829/*
830 * Ordinary vt52
831 */
832 {(int)KS_NAME, "vt52"},
833 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
834 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
835 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
836 {(int)KS_LE, "\b"},
837# ifdef __MINT__
838 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
839 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
840 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
841 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
842 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
843 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
844 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
845 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
846 {K_UP, IF_EB("\033A", ESC_STR "A")},
847 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
848 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
849 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
850 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
851 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
852 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
853 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
854 {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 {K_F4, IF_EB("\033S", ESC_STR "S")},
858 {K_F5, IF_EB("\033T", ESC_STR "T")},
859 {K_F6, IF_EB("\033U", ESC_STR "U")},
860 {K_F7, IF_EB("\033V", ESC_STR "V")},
861 {K_F8, IF_EB("\033W", ESC_STR "W")},
862 {K_F9, IF_EB("\033X", ESC_STR "X")},
863 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
864 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
865 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
866 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
867 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
868 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
869 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
870 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
871 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
872 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
873 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
874 {K_INS, IF_EB("\033I", ESC_STR "I")},
875 {K_HOME, IF_EB("\033E", ESC_STR "E")},
876 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
877 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
878# else
879 {(int)KS_AL, IF_EB("\033T", ESC_STR "T")},
880 {(int)KS_DL, IF_EB("\033U", ESC_STR "U")},
881 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
882 {(int)KS_ME, IF_EB("\033SO", ESC_STR "SO")},
883 {(int)KS_MR, IF_EB("\033S2", ESC_STR "S2")},
884 {(int)KS_MS, "y"},
885# endif
886# endif
887
888# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) || defined(__EMX__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {(int)KS_NAME, "xterm"},
890 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
891 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
892# ifdef TERMINFO
893 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
894# else
895 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
896# endif
897 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
898# ifdef TERMINFO
899 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
900# else
901 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
902# endif
903# ifdef TERMINFO
904 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
905 ESC_STR "[%i%p1%d;%p2%dr")},
906# else
907 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
908# endif
909 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
910 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
911 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
912 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
913 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
914 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
915 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
916 {(int)KS_MS, "y"},
917 {(int)KS_UT, "y"},
918 {(int)KS_LE, "\b"},
919# ifdef TERMINFO
920 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
921 ESC_STR "[%i%p1%d;%p2%dH")},
922# else
923 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
924# endif
925 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
926# ifdef TERMINFO
927 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
928# else
929 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
930# endif
931 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
932 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
933# ifdef FEAT_XTERM_SAVE
934 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
935 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
936 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
937# endif
938 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
939 {(int)KS_CIE, "\007"},
940 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
941 {(int)KS_FS, "\007"},
942# ifdef TERMINFO
943 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
944 ESC_STR "[8;%p1%d;%p2%dt")},
945 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
946 ESC_STR "[3;%p1%d;%p2%dt")},
947# else
948 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
949 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
950# endif
951 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100952 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000953
954 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
955 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
956 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
957 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
958 /* An extra set of cursor keys for vt100 mode */
959 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
960 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
961 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
962 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000964 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
965 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
966 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
967 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000968 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
969 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
970 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
971 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
972 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
973 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
974 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
975 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
976 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
977 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
978 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
979 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000981 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
982 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
983 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
984 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000985 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
986 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000987 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000988 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000989 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000990 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000991 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
992 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000993 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000994 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000995 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000996 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
997 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000998 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
999 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
1000 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
1001 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
1002 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
1003 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001004 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
1006 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001007 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
1008 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
1009 /* F14 and F15 are missing, because they send the same codes as the undo
1010 * and help key, although they don't work on all keyboards. */
1011 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
1012 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
1013 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1014 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1015 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1016
1017 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1018 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1019 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1020 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1021 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1022 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1023 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1024 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1025 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1026 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1027
1028 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1029 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1030 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1031 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1032 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1033 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1034 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035# endif
1036
1037# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1038/*
1039 * iris-ansi for Silicon Graphics machines.
1040 */
1041 {(int)KS_NAME, "iris-ansi"},
1042 {(int)KS_CE, "\033[K"},
1043 {(int)KS_CD, "\033[J"},
1044 {(int)KS_AL, "\033[L"},
1045# ifdef TERMINFO
1046 {(int)KS_CAL, "\033[%p1%dL"},
1047# else
1048 {(int)KS_CAL, "\033[%dL"},
1049# endif
1050 {(int)KS_DL, "\033[M"},
1051# ifdef TERMINFO
1052 {(int)KS_CDL, "\033[%p1%dM"},
1053# else
1054 {(int)KS_CDL, "\033[%dM"},
1055# endif
1056#if 0 /* The scroll region is not working as Vim expects. */
1057# ifdef TERMINFO
1058 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1059# else
1060 {(int)KS_CS, "\033[%i%d;%dr"},
1061# endif
1062#endif
1063 {(int)KS_CL, "\033[H\033[2J"},
1064 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1065 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1066 {(int)KS_TI, "\033[=6h"},
1067 {(int)KS_TE, "\033[=6l"},
1068 {(int)KS_SE, "\033[21;27m"},
1069 {(int)KS_SO, "\033[1;7m"},
1070 {(int)KS_ME, "\033[m"},
1071 {(int)KS_MR, "\033[7m"},
1072 {(int)KS_MD, "\033[1m"},
1073 {(int)KS_CCO, "8"}, /* allow 8 colors */
1074 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1075 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1076 {(int)KS_US, "\033[4m"}, /* underline on */
1077 {(int)KS_UE, "\033[24m"}, /* underline off */
1078# ifdef TERMINFO
1079 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1080 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1081 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1082 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1083# else
1084 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1085 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1086 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1087 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1088# endif
1089 {(int)KS_MS, "y"}, /* guessed */
1090 {(int)KS_UT, "y"}, /* guessed */
1091 {(int)KS_LE, "\b"},
1092# ifdef TERMINFO
1093 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1094# else
1095 {(int)KS_CM, "\033[%i%d;%dH"},
1096# endif
1097 {(int)KS_SR, "\033M"},
1098# ifdef TERMINFO
1099 {(int)KS_CRI, "\033[%p1%dC"},
1100# else
1101 {(int)KS_CRI, "\033[%dC"},
1102# endif
1103 {(int)KS_CIS, "\033P3.y"},
1104 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1105 {(int)KS_TS, "\033P1.y"},
1106 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1107# ifdef TERMINFO
1108 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1109 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1110# else
1111 {(int)KS_CWS, "\033[203;%d;%d/y"},
1112 {(int)KS_CWP, "\033[205;%d;%d/y"},
1113# endif
1114 {K_UP, "\033[A"},
1115 {K_DOWN, "\033[B"},
1116 {K_LEFT, "\033[D"},
1117 {K_RIGHT, "\033[C"},
1118 {K_S_UP, "\033[161q"},
1119 {K_S_DOWN, "\033[164q"},
1120 {K_S_LEFT, "\033[158q"},
1121 {K_S_RIGHT, "\033[167q"},
1122 {K_F1, "\033[001q"},
1123 {K_F2, "\033[002q"},
1124 {K_F3, "\033[003q"},
1125 {K_F4, "\033[004q"},
1126 {K_F5, "\033[005q"},
1127 {K_F6, "\033[006q"},
1128 {K_F7, "\033[007q"},
1129 {K_F8, "\033[008q"},
1130 {K_F9, "\033[009q"},
1131 {K_F10, "\033[010q"},
1132 {K_F11, "\033[011q"},
1133 {K_F12, "\033[012q"},
1134 {K_S_F1, "\033[013q"},
1135 {K_S_F2, "\033[014q"},
1136 {K_S_F3, "\033[015q"},
1137 {K_S_F4, "\033[016q"},
1138 {K_S_F5, "\033[017q"},
1139 {K_S_F6, "\033[018q"},
1140 {K_S_F7, "\033[019q"},
1141 {K_S_F8, "\033[020q"},
1142 {K_S_F9, "\033[021q"},
1143 {K_S_F10, "\033[022q"},
1144 {K_S_F11, "\033[023q"},
1145 {K_S_F12, "\033[024q"},
1146 {K_INS, "\033[139q"},
1147 {K_HOME, "\033[H"},
1148 {K_END, "\033[146q"},
1149 {K_PAGEUP, "\033[150q"},
1150 {K_PAGEDOWN, "\033[154q"},
1151# endif
1152
1153# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1154/*
1155 * for debugging
1156 */
1157 {(int)KS_NAME, "debug"},
1158 {(int)KS_CE, "[CE]"},
1159 {(int)KS_CD, "[CD]"},
1160 {(int)KS_AL, "[AL]"},
1161# ifdef TERMINFO
1162 {(int)KS_CAL, "[CAL%p1%d]"},
1163# else
1164 {(int)KS_CAL, "[CAL%d]"},
1165# endif
1166 {(int)KS_DL, "[DL]"},
1167# ifdef TERMINFO
1168 {(int)KS_CDL, "[CDL%p1%d]"},
1169# else
1170 {(int)KS_CDL, "[CDL%d]"},
1171# endif
1172# ifdef TERMINFO
1173 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1174# else
1175 {(int)KS_CS, "[%dCS%d]"},
1176# endif
1177# ifdef FEAT_VERTSPLIT
1178# ifdef TERMINFO
1179 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1180# else
1181 {(int)KS_CSV, "[%dCSV%d]"},
1182# endif
1183# endif
1184# ifdef TERMINFO
1185 {(int)KS_CAB, "[CAB%p1%d]"},
1186 {(int)KS_CAF, "[CAF%p1%d]"},
1187 {(int)KS_CSB, "[CSB%p1%d]"},
1188 {(int)KS_CSF, "[CSF%p1%d]"},
1189# else
1190 {(int)KS_CAB, "[CAB%d]"},
1191 {(int)KS_CAF, "[CAF%d]"},
1192 {(int)KS_CSB, "[CSB%d]"},
1193 {(int)KS_CSF, "[CSF%d]"},
1194# endif
1195 {(int)KS_OP, "[OP]"},
1196 {(int)KS_LE, "[LE]"},
1197 {(int)KS_CL, "[CL]"},
1198 {(int)KS_VI, "[VI]"},
1199 {(int)KS_VE, "[VE]"},
1200 {(int)KS_VS, "[VS]"},
1201 {(int)KS_ME, "[ME]"},
1202 {(int)KS_MR, "[MR]"},
1203 {(int)KS_MB, "[MB]"},
1204 {(int)KS_MD, "[MD]"},
1205 {(int)KS_SE, "[SE]"},
1206 {(int)KS_SO, "[SO]"},
1207 {(int)KS_UE, "[UE]"},
1208 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001209 {(int)KS_UCE, "[UCE]"},
1210 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {(int)KS_MS, "[MS]"},
1212 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001213 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214# ifdef TERMINFO
1215 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1216# else
1217 {(int)KS_CM, "[%dCM%d]"},
1218# endif
1219 {(int)KS_SR, "[SR]"},
1220# ifdef TERMINFO
1221 {(int)KS_CRI, "[CRI%p1%d]"},
1222# else
1223 {(int)KS_CRI, "[CRI%d]"},
1224# endif
1225 {(int)KS_VB, "[VB]"},
1226 {(int)KS_KS, "[KS]"},
1227 {(int)KS_KE, "[KE]"},
1228 {(int)KS_TI, "[TI]"},
1229 {(int)KS_TE, "[TE]"},
1230 {(int)KS_CIS, "[CIS]"},
1231 {(int)KS_CIE, "[CIE]"},
1232 {(int)KS_TS, "[TS]"},
1233 {(int)KS_FS, "[FS]"},
1234# ifdef TERMINFO
1235 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1236 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1237# else
1238 {(int)KS_CWS, "[%dCWS%d]"},
1239 {(int)KS_CWP, "[%dCWP%d]"},
1240# endif
1241 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001242 {(int)KS_U7, "[U7]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {K_UP, "[KU]"},
1244 {K_DOWN, "[KD]"},
1245 {K_LEFT, "[KL]"},
1246 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001247 {K_XUP, "[xKU]"},
1248 {K_XDOWN, "[xKD]"},
1249 {K_XLEFT, "[xKL]"},
1250 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {K_S_UP, "[S-KU]"},
1252 {K_S_DOWN, "[S-KD]"},
1253 {K_S_LEFT, "[S-KL]"},
1254 {K_C_LEFT, "[C-KL]"},
1255 {K_S_RIGHT, "[S-KR]"},
1256 {K_C_RIGHT, "[C-KR]"},
1257 {K_F1, "[F1]"},
1258 {K_XF1, "[xF1]"},
1259 {K_F2, "[F2]"},
1260 {K_XF2, "[xF2]"},
1261 {K_F3, "[F3]"},
1262 {K_XF3, "[xF3]"},
1263 {K_F4, "[F4]"},
1264 {K_XF4, "[xF4]"},
1265 {K_F5, "[F5]"},
1266 {K_F6, "[F6]"},
1267 {K_F7, "[F7]"},
1268 {K_F8, "[F8]"},
1269 {K_F9, "[F9]"},
1270 {K_F10, "[F10]"},
1271 {K_F11, "[F11]"},
1272 {K_F12, "[F12]"},
1273 {K_S_F1, "[S-F1]"},
1274 {K_S_XF1, "[S-xF1]"},
1275 {K_S_F2, "[S-F2]"},
1276 {K_S_XF2, "[S-xF2]"},
1277 {K_S_F3, "[S-F3]"},
1278 {K_S_XF3, "[S-xF3]"},
1279 {K_S_F4, "[S-F4]"},
1280 {K_S_XF4, "[S-xF4]"},
1281 {K_S_F5, "[S-F5]"},
1282 {K_S_F6, "[S-F6]"},
1283 {K_S_F7, "[S-F7]"},
1284 {K_S_F8, "[S-F8]"},
1285 {K_S_F9, "[S-F9]"},
1286 {K_S_F10, "[S-F10]"},
1287 {K_S_F11, "[S-F11]"},
1288 {K_S_F12, "[S-F12]"},
1289 {K_HELP, "[HELP]"},
1290 {K_UNDO, "[UNDO]"},
1291 {K_BS, "[BS]"},
1292 {K_INS, "[INS]"},
1293 {K_KINS, "[KINS]"},
1294 {K_DEL, "[DEL]"},
1295 {K_KDEL, "[KDEL]"},
1296 {K_HOME, "[HOME]"},
1297 {K_S_HOME, "[C-HOME]"},
1298 {K_C_HOME, "[C-HOME]"},
1299 {K_KHOME, "[KHOME]"},
1300 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001301 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {K_END, "[END]"},
1303 {K_S_END, "[C-END]"},
1304 {K_C_END, "[C-END]"},
1305 {K_KEND, "[KEND]"},
1306 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001307 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 {K_PAGEUP, "[PAGEUP]"},
1309 {K_PAGEDOWN, "[PAGEDOWN]"},
1310 {K_KPAGEUP, "[KPAGEUP]"},
1311 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1312 {K_MOUSE, "[MOUSE]"},
1313 {K_KPLUS, "[KPLUS]"},
1314 {K_KMINUS, "[KMINUS]"},
1315 {K_KDIVIDE, "[KDIVIDE]"},
1316 {K_KMULTIPLY, "[KMULTIPLY]"},
1317 {K_KENTER, "[KENTER]"},
1318 {K_KPOINT, "[KPOINT]"},
1319 {K_K0, "[K0]"},
1320 {K_K1, "[K1]"},
1321 {K_K2, "[K2]"},
1322 {K_K3, "[K3]"},
1323 {K_K4, "[K4]"},
1324 {K_K5, "[K5]"},
1325 {K_K6, "[K6]"},
1326 {K_K7, "[K7]"},
1327 {K_K8, "[K8]"},
1328 {K_K9, "[K9]"},
1329# endif
1330
1331#endif /* NO_BUILTIN_TCAPS */
1332
1333/*
1334 * The most minimal terminal: only clear screen and cursor positioning
1335 * Always included.
1336 */
1337 {(int)KS_NAME, "dumb"},
1338 {(int)KS_CL, "\014"},
1339#ifdef TERMINFO
1340 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1341 ESC_STR "[%i%p1%d;%p2%dH")},
1342#else
1343 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1344#endif
1345
1346/*
1347 * end marker
1348 */
1349 {(int)KS_NAME, NULL}
1350
1351}; /* end of builtin_termcaps */
1352
1353/*
1354 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356#ifdef AMIGA
1357# define DEFAULT_TERM (char_u *)"amiga"
1358#endif
1359
1360#ifdef MSWIN
1361# define DEFAULT_TERM (char_u *)"win32"
1362#endif
1363
1364#ifdef MSDOS
1365# define DEFAULT_TERM (char_u *)"pcterm"
1366#endif
1367
1368#if defined(UNIX) && !defined(__MINT__)
1369# define DEFAULT_TERM (char_u *)"ansi"
1370#endif
1371
1372#ifdef __MINT__
1373# define DEFAULT_TERM (char_u *)"vt52"
1374#endif
1375
1376#ifdef __EMX__
1377# define DEFAULT_TERM (char_u *)"os2ansi"
1378#endif
1379
1380#ifdef VMS
1381# define DEFAULT_TERM (char_u *)"vt320"
1382#endif
1383
1384#ifdef __BEOS__
1385# undef DEFAULT_TERM
1386# define DEFAULT_TERM (char_u *)"beos-ansi"
1387#endif
1388
1389#ifndef DEFAULT_TERM
1390# define DEFAULT_TERM (char_u *)"dumb"
1391#endif
1392
1393/*
1394 * Term_strings contains currently used terminal output strings.
1395 * It is initialized with the default values by parse_builtin_tcap().
1396 * The values can be changed by setting the option with the same name.
1397 */
1398char_u *(term_strings[(int)KS_LAST + 1]);
1399
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001400static int need_gather = FALSE; /* need to fill termleader[] */
1401static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001403static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#endif
1405
1406 static struct builtin_term *
1407find_builtin_term(term)
1408 char_u *term;
1409{
1410 struct builtin_term *p;
1411
1412 p = builtin_termcaps;
1413 while (p->bt_string != NULL)
1414 {
1415 if (p->bt_entry == (int)KS_NAME)
1416 {
1417#ifdef UNIX
1418 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1419 return p;
1420 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1421 return p;
1422 else
1423#endif
1424#ifdef VMS
1425 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1426 return p;
1427 else
1428#endif
1429 if (STRCMP(term, p->bt_string) == 0)
1430 return p;
1431 }
1432 ++p;
1433 }
1434 return p;
1435}
1436
1437/*
1438 * Parsing of the builtin termcap entries.
1439 * Caller should check if 'name' is a valid builtin term.
1440 * The terminal's name is not set, as this is already done in termcapinit().
1441 */
1442 static void
1443parse_builtin_tcap(term)
1444 char_u *term;
1445{
1446 struct builtin_term *p;
1447 char_u name[2];
1448 int term_8bit;
1449
1450 p = find_builtin_term(term);
1451 term_8bit = term_is_8bit(term);
1452
1453 /* Do not parse if builtin term not found */
1454 if (p->bt_string == NULL)
1455 return;
1456
1457 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1458 {
1459 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1460 {
1461 /* Only set the value if it wasn't set yet. */
1462 if (term_strings[p->bt_entry] == NULL
1463 || term_strings[p->bt_entry] == empty_option)
1464 {
1465 /* 8bit terminal: use CSI instead of <Esc>[ */
1466 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1467 {
1468 char_u *s, *t;
1469
1470 s = vim_strsave((char_u *)p->bt_string);
1471 if (s != NULL)
1472 {
1473 for (t = s; *t; ++t)
1474 if (term_7to8bit(t))
1475 {
1476 *t = term_7to8bit(t);
1477 STRCPY(t + 1, t + 2);
1478 }
1479 term_strings[p->bt_entry] = s;
1480 set_term_option_alloced(&term_strings[p->bt_entry]);
1481 }
1482 }
1483 else
1484 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1485 }
1486 }
1487 else
1488 {
1489 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1490 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1491 if (find_termcode(name) == NULL)
1492 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1493 }
1494 }
1495}
1496#if defined(HAVE_TGETENT) || defined(FEAT_TERMRESPONSE)
1497static void set_color_count __ARGS((int nr));
1498
1499/*
1500 * Set number of colors.
1501 * Store it as a number in t_colors.
1502 * Store it as a string in T_CCO (using nr_colors[]).
1503 */
1504 static void
1505set_color_count(nr)
1506 int nr;
1507{
1508 char_u nr_colors[20]; /* string for number of colors */
1509
1510 t_colors = nr;
1511 if (t_colors > 1)
1512 sprintf((char *)nr_colors, "%d", t_colors);
1513 else
1514 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001515 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516}
1517#endif
1518
1519#ifdef HAVE_TGETENT
1520static char *(key_names[]) =
1521{
1522#ifdef FEAT_TERMRESPONSE
1523 /* Do this one first, it may cause a screen redraw. */
1524 "Co",
1525#endif
1526 "ku", "kd", "kr", "kl",
1527# ifdef ARCHIE
1528 "su", "sd", /* Termcap code made up! */
1529# endif
1530 "#2", "#4", "%i", "*7",
1531 "k1", "k2", "k3", "k4", "k5", "k6",
1532 "k7", "k8", "k9", "k;", "F1", "F2",
1533 "%1", "&8", "kb", "kI", "kD", "kh",
1534 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1535 NULL
1536};
1537#endif
1538
1539/*
1540 * Set terminal options for terminal "term".
1541 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1542 *
1543 * While doing this, until ttest(), some options may be NULL, be careful.
1544 */
1545 int
1546set_termname(term)
1547 char_u *term;
1548{
1549 struct builtin_term *termp;
1550#ifdef HAVE_TGETENT
1551 int builtin_first = p_tbi;
1552 int try;
1553 int termcap_cleared = FALSE;
1554#endif
1555 int width = 0, height = 0;
1556 char_u *error_msg = NULL;
1557 char_u *bs_p, *del_p;
1558
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001559 /* In silect mode (ex -s) we don't use the 'term' option. */
1560 if (silent_mode)
1561 return OK;
1562
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 detected_8bit = FALSE; /* reset 8-bit detection */
1564
1565 if (term_is_builtin(term))
1566 {
1567 term += 8;
1568#ifdef HAVE_TGETENT
1569 builtin_first = 1;
1570#endif
1571 }
1572
1573/*
1574 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1575 * If builtin_first is TRUE:
1576 * 0. try builtin termcap
1577 * 1. try external termcap
1578 * 2. if both fail default to a builtin terminal
1579 * If builtin_first is FALSE:
1580 * 1. try external termcap
1581 * 2. try builtin termcap, if both fail default to a builtin terminal
1582 */
1583#ifdef HAVE_TGETENT
1584 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1585 {
1586 /*
1587 * Use external termcap
1588 */
1589 if (try == 1)
1590 {
1591 char_u *p;
1592 static char_u tstrbuf[TBUFSZ];
1593 int i;
1594 char_u tbuf[TBUFSZ];
1595 char_u *tp;
1596 static struct {
1597 enum SpecialKey dest; /* index in term_strings[] */
1598 char *name; /* termcap name for string */
1599 } string_names[] =
1600 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1601 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1602 {KS_CL, "cl"}, {KS_CD, "cd"},
1603 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1604 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1605 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1606 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001607 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1608 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1610 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1611 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1612 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1613 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1614 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1615 {KS_TS, "ts"}, {KS_FS, "fs"},
1616 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001617 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001618 {KS_U7, "u7"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {(enum SpecialKey)0, NULL}
1620 };
1621
1622 /*
1623 * If the external termcap does not have a matching entry, try the
1624 * builtin ones.
1625 */
1626 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1627 {
1628 tp = tstrbuf;
1629 if (!termcap_cleared)
1630 {
1631 clear_termoptions(); /* clear old options */
1632 termcap_cleared = TRUE;
1633 }
1634
1635 /* get output strings */
1636 for (i = 0; string_names[i].name != NULL; ++i)
1637 {
1638 if (term_str(string_names[i].dest) == NULL
1639 || term_str(string_names[i].dest) == empty_option)
1640 term_str(string_names[i].dest) =
1641 TGETSTR(string_names[i].name, &tp);
1642 }
1643
1644 /* tgetflag() returns 1 if the flag is present, 0 if not and
1645 * possibly -1 if the flag doesn't exist. */
1646 if ((T_MS == NULL || T_MS == empty_option)
1647 && tgetflag("ms") > 0)
1648 T_MS = (char_u *)"y";
1649 if ((T_XS == NULL || T_XS == empty_option)
1650 && tgetflag("xs") > 0)
1651 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001652 if ((T_XN == NULL || T_XN == empty_option)
1653 && tgetflag("xn") > 0)
1654 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if ((T_DB == NULL || T_DB == empty_option)
1656 && tgetflag("db") > 0)
1657 T_DB = (char_u *)"y";
1658 if ((T_DA == NULL || T_DA == empty_option)
1659 && tgetflag("da") > 0)
1660 T_DA = (char_u *)"y";
1661 if ((T_UT == NULL || T_UT == empty_option)
1662 && tgetflag("ut") > 0)
1663 T_UT = (char_u *)"y";
1664
1665
1666 /*
1667 * get key codes
1668 */
1669 for (i = 0; key_names[i] != NULL; ++i)
1670 {
1671 if (find_termcode((char_u *)key_names[i]) == NULL)
1672 {
1673 p = TGETSTR(key_names[i], &tp);
1674 /* if cursor-left == backspace, ignore it (televideo
1675 * 925) */
1676 if (p != NULL
1677 && (*p != Ctrl_H
1678 || key_names[i][0] != 'k'
1679 || key_names[i][1] != 'l'))
1680 add_termcode((char_u *)key_names[i], p, FALSE);
1681 }
1682 }
1683
1684 if (height == 0)
1685 height = tgetnum("li");
1686 if (width == 0)
1687 width = tgetnum("co");
1688
1689 /*
1690 * Get number of colors (if not done already).
1691 */
1692 if (term_str(KS_CCO) == NULL
1693 || term_str(KS_CCO) == empty_option)
1694 set_color_count(tgetnum("Co"));
1695
1696# ifndef hpux
1697 BC = (char *)TGETSTR("bc", &tp);
1698 UP = (char *)TGETSTR("up", &tp);
1699 p = TGETSTR("pc", &tp);
1700 if (p)
1701 PC = *p;
1702# endif /* hpux */
1703 }
1704 }
1705 else /* try == 0 || try == 2 */
1706#endif /* HAVE_TGETENT */
1707 /*
1708 * Use builtin termcap
1709 */
1710 {
1711#ifdef HAVE_TGETENT
1712 /*
1713 * If builtin termcap was already used, there is no need to search
1714 * for the builtin termcap again, quit now.
1715 */
1716 if (try == 2 && builtin_first && termcap_cleared)
1717 break;
1718#endif
1719 /*
1720 * search for 'term' in builtin_termcaps[]
1721 */
1722 termp = find_builtin_term(term);
1723 if (termp->bt_string == NULL) /* did not find it */
1724 {
1725#ifdef HAVE_TGETENT
1726 /*
1727 * If try == 0, first try the external termcap. If that is not
1728 * found we'll get back here with try == 2.
1729 * If termcap_cleared is set we used the external termcap,
1730 * don't complain about not finding the term in the builtin
1731 * termcap.
1732 */
1733 if (try == 0) /* try external one */
1734 continue;
1735 if (termcap_cleared) /* found in external termcap */
1736 break;
1737#endif
1738
1739 mch_errmsg("\r\n");
1740 if (error_msg != NULL)
1741 {
1742 mch_errmsg((char *)error_msg);
1743 mch_errmsg("\r\n");
1744 }
1745 mch_errmsg("'");
1746 mch_errmsg((char *)term);
1747 mch_errmsg(_("' not known. Available builtin terminals are:"));
1748 mch_errmsg("\r\n");
1749 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1750 ++termp)
1751 {
1752 if (termp->bt_entry == (int)KS_NAME)
1753 {
1754#ifdef HAVE_TGETENT
1755 mch_errmsg(" builtin_");
1756#else
1757 mch_errmsg(" ");
1758#endif
1759 mch_errmsg(termp->bt_string);
1760 mch_errmsg("\r\n");
1761 }
1762 }
1763 /* when user typed :set term=xxx, quit here */
1764 if (starting != NO_SCREEN)
1765 {
1766 screen_start(); /* don't know where cursor is now */
1767 wait_return(TRUE);
1768 return FAIL;
1769 }
1770 term = DEFAULT_TERM;
1771 mch_errmsg(_("defaulting to '"));
1772 mch_errmsg((char *)term);
1773 mch_errmsg("'\r\n");
1774 if (emsg_silent == 0)
1775 {
1776 screen_start(); /* don't know where cursor is now */
1777 out_flush();
1778 ui_delay(2000L, TRUE);
1779 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001780 set_string_option_direct((char_u *)"term", -1, term,
1781 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 display_errors();
1783 }
1784 out_flush();
1785#ifdef HAVE_TGETENT
1786 if (!termcap_cleared)
1787 {
1788#endif
1789 clear_termoptions(); /* clear old options */
1790#ifdef HAVE_TGETENT
1791 termcap_cleared = TRUE;
1792 }
1793#endif
1794 parse_builtin_tcap(term);
1795#ifdef FEAT_GUI
1796 if (term_is_gui(term))
1797 {
1798 out_flush();
1799 gui_init();
1800 /* If starting the GUI failed, don't do any of the other
1801 * things for this terminal */
1802 if (!gui.in_use)
1803 return FAIL;
1804#ifdef HAVE_TGETENT
1805 break; /* don't try using external termcap */
1806#endif
1807 }
1808#endif /* FEAT_GUI */
1809 }
1810#ifdef HAVE_TGETENT
1811 }
1812#endif
1813
1814/*
1815 * special: There is no info in the termcap about whether the cursor
1816 * positioning is relative to the start of the screen or to the start of the
1817 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1818 * relative.
1819 */
1820 if (STRCMP(term, "pcterm") == 0)
1821 T_CCS = (char_u *)"yes";
1822 else
1823 T_CCS = empty_option;
1824
1825#ifdef UNIX
1826/*
1827 * Any "stty" settings override the default for t_kb from the termcap.
1828 * This is in os_unix.c, because it depends a lot on the version of unix that
1829 * is being used.
1830 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1831 */
1832#ifdef FEAT_GUI
1833 if (!gui.in_use)
1834#endif
1835 get_stty();
1836#endif
1837
1838/*
1839 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1840 * didn't work, use the default CTRL-H
1841 * The default for t_kD is DEL, unless t_kb is DEL.
1842 * The vim_strsave'd strings are probably lost forever, well it's only two
1843 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1844 * directly.
1845 */
1846#ifdef FEAT_GUI
1847 if (!gui.in_use)
1848#endif
1849 {
1850 bs_p = find_termcode((char_u *)"kb");
1851 del_p = find_termcode((char_u *)"kD");
1852 if (bs_p == NULL || *bs_p == NUL)
1853 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1854 if ((del_p == NULL || *del_p == NUL) &&
1855 (bs_p == NULL || *bs_p != DEL))
1856 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1857 }
1858
1859#if defined(UNIX) || defined(VMS)
1860 term_is_xterm = vim_is_xterm(term);
1861#endif
1862
1863#ifdef FEAT_MOUSE
1864# if defined(UNIX) || defined(VMS)
1865# ifdef FEAT_MOUSE_TTY
1866 /*
1867 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1868 * The termcode for the mouse is added as a side effect in option.c.
1869 */
1870 {
1871 char_u *p;
1872
1873 p = (char_u *)"";
1874# ifdef FEAT_MOUSE_XTERM
1875# ifdef FEAT_CLIPBOARD
1876# ifdef FEAT_GUI
1877 if (!gui.in_use)
1878# endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001879# ifndef FEAT_CYGWIN_WIN32_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 clip_init(FALSE);
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882# endif
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 {
2272 if (!builtin_first == i)
2273#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] == ']')
2360 return 0x9d;
2361 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
3227 || u7_status == U7_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003228 (void)vpeekc_nomap();
3229 check_for_codes_from_term();
3230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231#endif
3232#ifdef FEAT_MOUSE_TTY
3233 if (tmode != TMODE_RAW)
3234 mch_setmouse(FALSE); /* switch mouse off */
3235#endif
3236 out_flush();
3237 mch_settmode(tmode); /* machine specific function */
3238 cur_tmode = tmode;
3239#ifdef FEAT_MOUSE
3240 if (tmode == TMODE_RAW)
3241 setmouse(); /* may switch mouse on */
3242#endif
3243 out_flush();
3244 }
3245#ifdef FEAT_TERMRESPONSE
3246 may_req_termresponse();
3247#endif
3248 }
3249}
3250
3251 void
3252starttermcap()
3253{
3254 if (full_screen && !termcap_active)
3255 {
3256 out_str(T_TI); /* start termcap mode */
3257 out_str(T_KS); /* start "keypad transmit" mode */
3258 out_flush();
3259 termcap_active = TRUE;
3260 screen_start(); /* don't know where cursor is now */
3261#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003262# ifdef FEAT_GUI
3263 if (!gui.in_use && !gui.starting)
3264# endif
3265 {
3266 may_req_termresponse();
3267 /* Immediately check for a response. If t_Co changes, we don't
3268 * want to redraw with wrong colors first. */
Bram Moolenaar90013c62014-05-22 18:14:31 +02003269 if (crv_status == CRV_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003270 check_for_codes_from_term();
3271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#endif
3273 }
3274}
3275
3276 void
3277stoptermcap()
3278{
3279 screen_stop_highlight();
3280 reset_cterm_colors();
3281 if (termcap_active)
3282 {
3283#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003284# ifdef FEAT_GUI
3285 if (!gui.in_use && !gui.starting)
3286# endif
3287 {
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003288 /* May need to discard T_CRV or T_U7 response. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003289 if (crv_status == CRV_SENT || u7_status == U7_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003290 {
3291# ifdef UNIX
3292 /* Give the terminal a chance to respond. */
3293 mch_delay(100L, FALSE);
3294# endif
3295# ifdef TCIFLUSH
3296 /* Discard data received but not read. */
3297 if (exiting)
3298 tcflush(fileno(stdin), TCIFLUSH);
3299# endif
3300 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003301 /* Check for termcodes first, otherwise an external program may
3302 * get them. */
3303 check_for_codes_from_term();
3304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305#endif
3306 out_str(T_KE); /* stop "keypad transmit" mode */
3307 out_flush();
3308 termcap_active = FALSE;
3309 cursor_on(); /* just in case it is still off */
3310 out_str(T_TE); /* stop termcap mode */
3311 screen_start(); /* don't know where cursor is now */
3312 out_flush();
3313 }
3314}
3315
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003316#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317/*
3318 * Request version string (for xterm) when needed.
3319 * Only do this after switching to raw mode, otherwise the result will be
3320 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003321 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003322 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 * Only do this after termcap mode has been started, otherwise the codes for
3324 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003325 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3326 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003327 * On Unix only do it when both output and input are a tty (avoid writing
3328 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 * The result is caught in check_termcode().
3330 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003331 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332may_req_termresponse()
3333{
3334 if (crv_status == CRV_GET
3335 && cur_tmode == TMODE_RAW
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003336 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 && termcap_active
Bram Moolenaarebefac62005-12-28 22:39:57 +00003338 && p_ek
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003339# ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 && isatty(1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003341 && isatty(read_cmd_fd)
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003342# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 && *T_CRV != NUL)
3344 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02003345 LOG_TR("Sending CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 out_str(T_CRV);
3347 crv_status = CRV_SENT;
3348 /* check for the characters now, otherwise they might be eaten by
3349 * get_keystroke() */
3350 out_flush();
3351 (void)vpeekc_nomap();
3352 }
3353}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003354
3355# if defined(FEAT_MBYTE) || defined(PROTO)
3356/*
3357 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003358 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003359 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003360 * If the terminal treats \u25bd as single width, the position is (1, 1),
3361 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003362 * This function has the side effect that changes cursor position, so
3363 * it must be called immediately after entering termcap mode.
3364 */
3365 void
Bram Moolenaar386dcde2013-09-29 16:27:47 +02003366may_req_ambiguous_char_width()
Bram Moolenaar9584b312013-03-13 19:29:28 +01003367{
3368 if (u7_status == U7_GET
3369 && cur_tmode == TMODE_RAW
3370 && termcap_active
3371 && p_ek
3372# ifdef UNIX
3373 && isatty(1)
3374 && isatty(read_cmd_fd)
3375# endif
3376 && *T_U7 != NUL
3377 && !option_was_set((char_u *)"ambiwidth"))
3378 {
3379 char_u buf[16];
3380
Bram Moolenaar2951b772013-07-03 12:45:31 +02003381 LOG_TR("Sending U7 request");
3382 /* Do this in the second row. In the first row the returned sequence
3383 * may be CSI 1;2R, which is the same as <S-F3>. */
3384 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003385 buf[mb_char2bytes(0x25bd, buf)] = 0;
3386 out_str(buf);
3387 out_str(T_U7);
3388 u7_status = U7_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003389 out_flush();
3390 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003391 out_str((char_u *)" ");
3392 term_windgoto(0, 0);
3393 /* check for the characters now, otherwise they might be eaten by
3394 * get_keystroke() */
3395 out_flush();
3396 (void)vpeekc_nomap();
3397 }
3398}
3399# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003400
3401# ifdef DEBUG_TERMRESPONSE
3402 static void
3403log_tr(char *msg)
3404{
3405 static FILE *fd_tr = NULL;
3406 static proftime_T start;
3407 proftime_T now;
3408
3409 if (fd_tr == NULL)
3410 {
3411 fd_tr = fopen("termresponse.log", "w");
3412 profile_start(&start);
3413 }
3414 now = start;
3415 profile_end(&now);
3416 fprintf(fd_tr, "%s: %s %s\n",
3417 profile_msg(&now),
3418 must_redraw == NOT_VALID ? "NV"
3419 : must_redraw == CLEAR ? "CL" : " ",
3420 msg);
3421}
3422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423#endif
3424
3425/*
3426 * Return TRUE when saving and restoring the screen.
3427 */
3428 int
3429swapping_screen()
3430{
3431 return (full_screen && *T_TI != NUL);
3432}
3433
3434#ifdef FEAT_MOUSE
3435/*
3436 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3437 */
3438 void
3439setmouse()
3440{
3441# ifdef FEAT_MOUSE_TTY
3442 int checkfor;
3443# endif
3444
3445# ifdef FEAT_MOUSESHAPE
3446 update_mouseshape(-1);
3447# endif
3448
3449# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3450# ifdef FEAT_GUI
3451 /* In the GUI the mouse is always enabled. */
3452 if (gui.in_use)
3453 return;
3454# endif
3455 /* be quick when mouse is off */
3456 if (*p_mouse == NUL || has_mouse_termcode == 0)
3457 return;
3458
3459 /* don't switch mouse on when not in raw mode (Ex mode) */
3460 if (cur_tmode != TMODE_RAW)
3461 {
3462 mch_setmouse(FALSE);
3463 return;
3464 }
3465
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 if (VIsual_active)
3467 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003468 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 checkfor = MOUSE_RETURN;
3470 else if (State & INSERT)
3471 checkfor = MOUSE_INSERT;
3472 else if (State & CMDLINE)
3473 checkfor = MOUSE_COMMAND;
3474 else if (State == CONFIRM || State == EXTERNCMD)
3475 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3476 else
3477 checkfor = MOUSE_NORMAL; /* assume normal mode */
3478
3479 if (mouse_has(checkfor))
3480 mch_setmouse(TRUE);
3481 else
3482 mch_setmouse(FALSE);
3483# endif
3484}
3485
3486/*
3487 * Return TRUE if
3488 * - "c" is in 'mouse', or
3489 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3490 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3491 * normal editing mode (not at hit-return message).
3492 */
3493 int
3494mouse_has(c)
3495 int c;
3496{
3497 char_u *p;
3498
3499 for (p = p_mouse; *p; ++p)
3500 switch (*p)
3501 {
3502 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3503 return TRUE;
3504 break;
3505 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3506 return TRUE;
3507 break;
3508 default: if (c == *p) return TRUE; break;
3509 }
3510 return FALSE;
3511}
3512
3513/*
3514 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3515 */
3516 int
3517mouse_model_popup()
3518{
3519 return (p_mousem[0] == 'p');
3520}
3521#endif
3522
3523/*
3524 * By outputting the 'cursor very visible' termcap code, for some windowed
3525 * terminals this makes the screen scrolled to the correct position.
3526 * Used when starting Vim or returning from a shell.
3527 */
3528 void
3529scroll_start()
3530{
3531 if (*T_VS != NUL)
3532 {
3533 out_str(T_VS);
3534 out_str(T_VE);
3535 screen_start(); /* don't know where cursor is now */
3536 }
3537}
3538
3539static int cursor_is_off = FALSE;
3540
3541/*
3542 * Enable the cursor.
3543 */
3544 void
3545cursor_on()
3546{
3547 if (cursor_is_off)
3548 {
3549 out_str(T_VE);
3550 cursor_is_off = FALSE;
3551 }
3552}
3553
3554/*
3555 * Disable the cursor.
3556 */
3557 void
3558cursor_off()
3559{
3560 if (full_screen)
3561 {
3562 if (!cursor_is_off)
3563 out_str(T_VI); /* disable cursor */
3564 cursor_is_off = TRUE;
3565 }
3566}
3567
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003568#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569/*
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003570 * Set cursor shape to match Insert or Replace mode.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003571 */
3572 void
3573term_cursor_shape()
3574{
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003575 static int showing_mode = NORMAL;
3576 char_u *p;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003577
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003578 /* Only do something when redrawing the screen and we can restore the
3579 * mode. */
3580 if (!full_screen || *T_CEI == NUL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003581 return;
3582
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003583 if ((State & REPLACE) == REPLACE)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003584 {
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003585 if (showing_mode != REPLACE)
3586 {
3587 if (*T_CSR != NUL)
3588 p = T_CSR; /* Replace mode cursor */
3589 else
3590 p = T_CSI; /* fall back to Insert mode cursor */
3591 if (*p != NUL)
3592 {
3593 out_str(p);
3594 showing_mode = REPLACE;
3595 }
3596 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003597 }
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003598 else if (State & INSERT)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003599 {
Bram Moolenaar1e7813a2015-03-31 18:31:03 +02003600 if (showing_mode != INSERT && *T_CSI != NUL)
3601 {
3602 out_str(T_CSI); /* Insert mode cursor */
3603 showing_mode = INSERT;
3604 }
3605 }
3606 else if (showing_mode != NORMAL)
3607 {
3608 out_str(T_CEI); /* non-Insert mode cursor */
3609 showing_mode = NORMAL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003610 }
3611}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003612#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003613
3614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 * Set scrolling region for window 'wp'.
3616 * The region starts 'off' lines from the start of the window.
3617 * Also set the vertical scroll region for a vertically split window. Always
3618 * the full width of the window, excluding the vertical separator.
3619 */
3620 void
3621scroll_region_set(wp, off)
3622 win_T *wp;
3623 int off;
3624{
3625 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3626 W_WINROW(wp) + off));
3627#ifdef FEAT_VERTSPLIT
3628 if (*T_CSV != NUL && wp->w_width != Columns)
3629 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3630 W_WINCOL(wp)));
3631#endif
3632 screen_start(); /* don't know where cursor is now */
3633}
3634
3635/*
3636 * Reset scrolling region to the whole screen.
3637 */
3638 void
3639scroll_region_reset()
3640{
3641 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
3642#ifdef FEAT_VERTSPLIT
3643 if (*T_CSV != NUL)
3644 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3645#endif
3646 screen_start(); /* don't know where cursor is now */
3647}
3648
3649
3650/*
3651 * List of terminal codes that are currently recognized.
3652 */
3653
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003654static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655{
3656 char_u name[2]; /* termcap name of entry */
3657 char_u *code; /* terminal code (in allocated memory) */
3658 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003659 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660} *termcodes = NULL;
3661
3662static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3663static int tc_len = 0; /* current number of entries in termcodes[] */
3664
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003665static int termcode_star __ARGS((char_u *code, int len));
3666
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 void
3668clear_termcodes()
3669{
3670 while (tc_len > 0)
3671 vim_free(termcodes[--tc_len].code);
3672 vim_free(termcodes);
3673 termcodes = NULL;
3674 tc_max_len = 0;
3675
3676#ifdef HAVE_TGETENT
3677 BC = (char *)empty_option;
3678 UP = (char *)empty_option;
3679 PC = NUL; /* set pad character to NUL */
3680 ospeed = 0;
3681#endif
3682
3683 need_gather = TRUE; /* need to fill termleader[] */
3684}
3685
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003686#define ATC_FROM_TERM 55
3687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688/*
3689 * Add a new entry to the list of terminal codes.
3690 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003691 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3692 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 */
3694 void
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003695add_termcode(name, string, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 char_u *name;
3697 char_u *string;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003698 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699{
3700 struct termcode *new_tc;
3701 int i, j;
3702 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003703 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705 if (string == NULL || *string == NUL)
3706 {
3707 del_termcode(name);
3708 return;
3709 }
3710
Bram Moolenaar45500912014-07-09 20:51:07 +02003711#if defined(WIN3264) && !defined(FEAT_GUI)
3712 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3713#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 if (s == NULL)
3717 return;
3718
3719 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003720 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003722 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 s[0] = term_7to8bit(string);
3724 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003725
3726#if defined(WIN3264) && !defined(FEAT_GUI)
3727 if (s[0] == K_NUL)
3728 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003729 STRMOVE(s + 1, s);
3730 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003731 }
3732#endif
3733
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003734 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
3736 need_gather = TRUE; /* need to fill termleader[] */
3737
3738 /*
3739 * need to make space for more entries
3740 */
3741 if (tc_len == tc_max_len)
3742 {
3743 tc_max_len += 20;
3744 new_tc = (struct termcode *)alloc(
3745 (unsigned)(tc_max_len * sizeof(struct termcode)));
3746 if (new_tc == NULL)
3747 {
3748 tc_max_len -= 20;
3749 return;
3750 }
3751 for (i = 0; i < tc_len; ++i)
3752 new_tc[i] = termcodes[i];
3753 vim_free(termcodes);
3754 termcodes = new_tc;
3755 }
3756
3757 /*
3758 * Look for existing entry with the same name, it is replaced.
3759 * Look for an existing entry that is alphabetical higher, the new entry
3760 * is inserted in front of it.
3761 */
3762 for (i = 0; i < tc_len; ++i)
3763 {
3764 if (termcodes[i].name[0] < name[0])
3765 continue;
3766 if (termcodes[i].name[0] == name[0])
3767 {
3768 if (termcodes[i].name[1] < name[1])
3769 continue;
3770 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003771 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 */
3773 if (termcodes[i].name[1] == name[1])
3774 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003775 if (flags == ATC_FROM_TERM && (j = termcode_star(
3776 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003777 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003778 /* Don't replace ESC[123;*X or ESC O*X with another when
3779 * invoked from got_code_from_term(). */
3780 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003781 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003782 && s[len - 1]
3783 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003784 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003785 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003786 vim_free(s);
3787 return;
3788 }
3789 }
3790 else
3791 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003792 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003793 vim_free(termcodes[i].code);
3794 --tc_len;
3795 break;
3796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
3798 }
3799 /*
3800 * Found alphabetical larger entry, move rest to insert new entry
3801 */
3802 for (j = tc_len; j > i; --j)
3803 termcodes[j] = termcodes[j - 1];
3804 break;
3805 }
3806
3807 termcodes[i].name[0] = name[0];
3808 termcodes[i].name[1] = name[1];
3809 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003810 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003811
3812 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3813 * accept modifiers. */
3814 termcodes[i].modlen = 0;
3815 j = termcode_star(s, len);
3816 if (j > 0)
3817 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 ++tc_len;
3819}
3820
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003821/*
3822 * Check termcode "code[len]" for ending in ;*X, <Esc>O*X or <M-O>*X.
3823 * The "X" can be any character.
3824 * Return 0 if not found, 2 for ;*X and 1 for O*X and <M-O>*X.
3825 */
3826 static int
3827termcode_star(code, len)
3828 char_u *code;
3829 int len;
3830{
3831 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3832 if (len >= 3 && code[len - 2] == '*')
3833 {
3834 if (len >= 5 && code[len - 3] == ';')
3835 return 2;
3836 if ((len >= 4 && code[len - 3] == 'O') || code[len - 3] == 'O' + 128)
3837 return 1;
3838 }
3839 return 0;
3840}
3841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 char_u *
3843find_termcode(name)
3844 char_u *name;
3845{
3846 int i;
3847
3848 for (i = 0; i < tc_len; ++i)
3849 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3850 return termcodes[i].code;
3851 return NULL;
3852}
3853
3854#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3855 char_u *
3856get_termcode(i)
3857 int i;
3858{
3859 if (i >= tc_len)
3860 return NULL;
3861 return &termcodes[i].name[0];
3862}
3863#endif
3864
3865 void
3866del_termcode(name)
3867 char_u *name;
3868{
3869 int i;
3870
3871 if (termcodes == NULL) /* nothing there yet */
3872 return;
3873
3874 need_gather = TRUE; /* need to fill termleader[] */
3875
3876 for (i = 0; i < tc_len; ++i)
3877 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3878 {
3879 del_termcode_idx(i);
3880 return;
3881 }
3882 /* not found. Give error message? */
3883}
3884
3885 static void
3886del_termcode_idx(idx)
3887 int idx;
3888{
3889 int i;
3890
3891 vim_free(termcodes[idx].code);
3892 --tc_len;
3893 for (i = idx; i < tc_len; ++i)
3894 termcodes[i] = termcodes[i + 1];
3895}
3896
3897#ifdef FEAT_TERMRESPONSE
3898/*
3899 * Called when detected that the terminal sends 8-bit codes.
3900 * Convert all 7-bit codes to their 8-bit equivalent.
3901 */
3902 static void
3903switch_to_8bit()
3904{
3905 int i;
3906 int c;
3907
3908 /* Only need to do something when not already using 8-bit codes. */
3909 if (!term_is_8bit(T_NAME))
3910 {
3911 for (i = 0; i < tc_len; ++i)
3912 {
3913 c = term_7to8bit(termcodes[i].code);
3914 if (c != 0)
3915 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003916 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 termcodes[i].code[0] = c;
3918 }
3919 }
3920 need_gather = TRUE; /* need to fill termleader[] */
3921 }
3922 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003923 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924}
3925#endif
3926
3927#ifdef CHECK_DOUBLE_CLICK
3928static linenr_T orig_topline = 0;
3929# ifdef FEAT_DIFF
3930static int orig_topfill = 0;
3931# endif
3932#endif
3933#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
3934/*
3935 * Checking for double clicks ourselves.
3936 * "orig_topline" is used to avoid detecting a double-click when the window
3937 * contents scrolled (e.g., when 'scrolloff' is non-zero).
3938 */
3939/*
3940 * Set orig_topline. Used when jumping to another window, so that a double
3941 * click still works.
3942 */
3943 void
3944set_mouse_topline(wp)
3945 win_T *wp;
3946{
3947 orig_topline = wp->w_topline;
3948# ifdef FEAT_DIFF
3949 orig_topfill = wp->w_topfill;
3950# endif
3951}
3952#endif
3953
3954/*
3955 * Check if typebuf.tb_buf[] contains a terminal key code.
3956 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
3957 * + max_offset].
3958 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003959 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 * With a match, the match is removed, the replacement code is inserted in
3961 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
3962 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003963 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
3964 * "buflen" is then the length of the string in buf[] and is updated for
3965 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 */
3967 int
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003968check_termcode(max_offset, buf, bufsize, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 int max_offset;
3970 char_u *buf;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003971 int bufsize;
3972 int *buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973{
3974 char_u *tp;
3975 char_u *p;
3976 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003977 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003979 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 int offset;
3981 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003982 int modifiers;
3983 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 int new_slen;
3985 int extra;
3986 char_u string[MAX_KEY_CODE_LEN + 1];
3987 int i, j;
3988 int idx = 0;
3989#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00003990# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
3991 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 char_u bytes[6];
3993 int num_bytes;
3994# endif
3995 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 int is_click, is_drag;
3997 int wheel_code = 0;
3998 int current_button;
3999 static int held_button = MOUSE_RELEASE;
4000 static int orig_num_clicks = 1;
4001 static int orig_mouse_code = 0x0;
4002# ifdef CHECK_DOUBLE_CLICK
4003 static int orig_mouse_col = 0;
4004 static int orig_mouse_row = 0;
4005 static struct timeval orig_mouse_time = {0, 0};
4006 /* time of previous mouse click */
4007 struct timeval mouse_time; /* time of current mouse click */
4008 long timediff; /* elapsed time in msec */
4009# endif
4010#endif
4011 int cpo_koffset;
4012#ifdef FEAT_MOUSE_GPM
4013 extern int gpm_flag; /* gpm library variable */
4014#endif
4015
4016 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4017
4018 /*
4019 * Speed up the checks for terminal codes by gathering all first bytes
4020 * used in termleader[]. Often this is just a single <Esc>.
4021 */
4022 if (need_gather)
4023 gather_termleader();
4024
4025 /*
4026 * Check at several positions in typebuf.tb_buf[], to catch something like
4027 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4028 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004029 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 * This is used often, KEEP IT FAST!
4031 */
4032 for (offset = 0; offset < max_offset; ++offset)
4033 {
4034 if (buf == NULL)
4035 {
4036 if (offset >= typebuf.tb_len)
4037 break;
4038 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4039 len = typebuf.tb_len - offset; /* length of the input */
4040 }
4041 else
4042 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004043 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 break;
4045 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004046 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
4048
4049 /*
4050 * Don't check characters after K_SPECIAL, those are already
4051 * translated terminal chars (avoid translating ~@^Hx).
4052 */
4053 if (*tp == K_SPECIAL)
4054 {
4055 offset += 2; /* there are always 2 extra characters */
4056 continue;
4057 }
4058
4059 /*
4060 * Skip this position if the character does not appear as the first
4061 * character in term_strings. This speeds up a lot, since most
4062 * termcodes start with the same character (ESC or CSI).
4063 */
4064 i = *tp;
4065 for (p = termleader; *p && *p != i; ++p)
4066 ;
4067 if (*p == NUL)
4068 continue;
4069
4070 /*
4071 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4072 * are in Insert mode.
4073 */
4074 if (*tp == ESC && !p_ek && (State & INSERT))
4075 continue;
4076
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004078 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004079 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081#ifdef FEAT_GUI
4082 if (gui.in_use)
4083 {
4084 /*
4085 * GUI special key codes are all of the form [CSI xx].
4086 */
4087 if (*tp == CSI) /* Special key from GUI */
4088 {
4089 if (len < 3)
4090 return -1; /* Shouldn't happen */
4091 slen = 3;
4092 key_name[0] = tp[1];
4093 key_name[1] = tp[2];
4094 }
4095 }
4096 else
4097#endif /* FEAT_GUI */
4098 {
4099 for (idx = 0; idx < tc_len; ++idx)
4100 {
4101 /*
4102 * Ignore the entry if we are not at the start of
4103 * typebuf.tb_buf[]
4104 * and there are not enough characters to make a match.
4105 * But only when the 'K' flag is in 'cpoptions'.
4106 */
4107 slen = termcodes[idx].len;
4108 if (cpo_koffset && offset && len < slen)
4109 continue;
4110 if (STRNCMP(termcodes[idx].code, tp,
4111 (size_t)(slen > len ? len : slen)) == 0)
4112 {
4113 if (len < slen) /* got a partial sequence */
4114 return -1; /* need to get more chars */
4115
4116 /*
4117 * When found a keypad key, check if there is another key
4118 * that matches and use that one. This makes <Home> to be
4119 * found instead of <kHome> when they produce the same
4120 * key code.
4121 */
4122 if (termcodes[idx].name[0] == 'K'
4123 && VIM_ISDIGIT(termcodes[idx].name[1]))
4124 {
4125 for (j = idx + 1; j < tc_len; ++j)
4126 if (termcodes[j].len == slen &&
4127 STRNCMP(termcodes[idx].code,
4128 termcodes[j].code, slen) == 0)
4129 {
4130 idx = j;
4131 break;
4132 }
4133 }
4134
4135 key_name[0] = termcodes[idx].name[0];
4136 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 break;
4138 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004139
4140 /*
4141 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004142 * <Esc>[123;*X (modslen == slen - 3)
4143 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4144 * When there is a modifier the * matches a number.
4145 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004146 */
4147 if (termcodes[idx].modlen > 0)
4148 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004149 modslen = termcodes[idx].modlen;
4150 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004151 continue;
4152 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004153 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004154 {
4155 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004156
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004157 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004158 return -1; /* need to get more chars */
4159
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004160 if (tp[modslen] == termcodes[idx].code[slen - 1])
4161 slen = modslen + 1; /* no modifiers */
4162 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004163 continue; /* no match */
4164 else
4165 {
4166 /* Skip over the digits, the final char must
4167 * follow. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004168 for (j = slen - 2; j < len && isdigit(tp[j]); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004169 ;
4170 ++j;
4171 if (len < j) /* got a partial sequence */
4172 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004173 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4174 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004175
4176 /* Match! Convert modifier bits. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004177 n = atoi((char *)tp + slen - 2) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004178 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004179 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004180 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004181 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004182 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004183 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004184 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004185 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004186
4187 slen = j;
4188 }
4189 key_name[0] = termcodes[idx].name[0];
4190 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004191 break;
4192 }
4193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195 }
4196
4197#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004198 if (key_name[0] == NUL
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004199 /* Mouse codes of DEC, pterm, and URXVT start with <ESC>[. When
4200 * detecting the start of these mouse codes they might as well be
4201 * another key code or terminal response. */
4202# ifdef FEAT_MOUSE_DEC
4203 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004204# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004205# ifdef FEAT_MOUSE_PTERM
4206 || key_name[0] == KS_PTERM_MOUSE
4207# endif
4208# ifdef FEAT_MOUSE_URXVT
4209 || key_name[0] == KS_URXVT_MOUSE
4210# endif
4211 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004213 /* Check for some responses from the terminal starting with
4214 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004215 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004216 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01004217 * Also eat other possible responses to t_RV, rxvt returns
4218 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4219 * mrxvt has been reported to have "+" in the version. Assume
4220 * the escape sequence ends with a letter or one of "{|}~".
4221 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004222 * - Cursor position report: <Esc>[{row};{col}R
4223 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004224 * ambiguous-width character state.
4225 */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004226 p = tp[0] == CSI ? tp + 1 : tp + 2;
Bram Moolenaar9584b312013-03-13 19:29:28 +01004227 if ((*T_CRV != NUL || *T_U7 != NUL)
4228 && ((tp[0] == ESC && tp[1] == '[' && len >= 3)
Bram Moolenaar46a75612013-05-15 14:22:41 +02004229 || (tp[0] == CSI && len >= 2))
4230 && (VIM_ISDIGIT(*p) || *p == '>' || *p == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 {
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004232#ifdef FEAT_MBYTE
4233 int col;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004234 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 j = 0;
4237 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004238 for (i = 2 + (tp[0] != CSI); i < len
4239 && !(tp[i] >= '{' && tp[i] <= '~')
4240 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 if (tp[i] == ';' && ++j == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004242 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004243 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004244#ifdef FEAT_MBYTE
4245 row_char = tp[i - 1];
4246#endif
4247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004249 {
4250 LOG_TR("Not enough characters for CRV");
4251 return -1;
4252 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004253#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004254 if (extra > 0)
4255 col = atoi((char *)tp + extra);
4256 else
4257 col = 0;
4258
4259 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4260 * u7_status is not "sent", it may be from a previous Vim that
4261 * just exited. But not for <S-F3>, it sends something
4262 * similar, check for row and column to make sense. */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004263 if (j == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004264 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004265 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004266 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004267 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004268
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004269 LOG_TR("Received U7 status");
4270 u7_status = U7_GOT;
4271# ifdef FEAT_AUTOCMD
4272 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004273# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004274 if (col == 2)
4275 aw = "single";
4276 else if (col == 3)
4277 aw = "double";
4278 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4279 {
4280 /* Setting the option causes a screen redraw. Do
4281 * that right away if possible, keeping any
4282 * messages. */
4283 set_option_value((char_u *)"ambw", 0L,
4284 (char_u *)aw, 0);
4285# ifdef DEBUG_TERMRESPONSE
4286 {
4287 char buf[100];
4288 int r = redraw_asap(CLEAR);
4289
4290 sprintf(buf,
4291 "set 'ambiwidth', redraw_asap(): %d",
4292 r);
4293 log_tr(buf);
4294 }
4295# else
4296 redraw_asap(CLEAR);
4297# endif
4298 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004299 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004300 key_name[0] = (int)KS_EXTRA;
4301 key_name[1] = (int)KE_IGNORE;
4302 slen = i + 1;
4303 }
4304 else
4305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004307 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004309 LOG_TR("Received CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 crv_status = CRV_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004311# ifdef FEAT_AUTOCMD
4312 did_cursorhold = TRUE;
4313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314
4315 /* If this code starts with CSI, you can bet that the
4316 * terminal uses 8-bit codes. */
4317 if (tp[0] == CSI)
4318 switch_to_8bit();
4319
4320 /* rxvt sends its version number: "20703" is 2.7.3.
4321 * Ignore it for when the user has set 'term' to xterm,
4322 * even though it's an rxvt. */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004323 if (extra > 0)
4324 extra = atoi((char *)tp + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 if (extra > 20000)
4326 extra = 0;
4327
4328 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
4329 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004330 /* Only set 'ttymouse' automatically if it was not set
4331 * by the user already. */
4332 if (!option_was_set((char_u *)"ttym"))
4333 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004334# ifdef TTYM_SGR
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004335 if (extra >= 277)
4336 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004337 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004338 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004339# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004340 /* if xterm version >= 95 use mouse dragging */
4341 if (extra >= 95)
4342 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004344 }
4345
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 /* if xterm version >= 141 try to get termcap codes */
4347 if (extra >= 141)
4348 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004349 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 check_for_codes = TRUE;
4351 need_gather = TRUE;
4352 req_codes_from_term();
4353 }
4354 }
4355# ifdef FEAT_EVAL
4356 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4357# endif
4358# ifdef FEAT_AUTOCMD
4359 apply_autocmds(EVENT_TERMRESPONSE,
4360 NULL, NULL, FALSE, curbuf);
4361# endif
4362 key_name[0] = (int)KS_EXTRA;
4363 key_name[1] = (int)KE_IGNORE;
4364 slen = i + 1;
4365 }
4366 }
4367
4368 /* Check for '<Esc>P1+r<hex bytes><Esc>\'. A "0" instead of the
4369 * "1" means an invalid request. */
4370 else if (check_for_codes
4371 && ((tp[0] == ESC && tp[1] == 'P' && len >= 2)
4372 || tp[0] == DCS))
4373 {
4374 j = 1 + (tp[0] != DCS);
4375 for (i = j; i < len; ++i)
4376 if ((tp[i] == ESC && tp[i + 1] == '\\' && i + 1 < len)
4377 || tp[i] == STERM)
4378 {
4379 if (i - j >= 3 && tp[j + 1] == '+' && tp[j + 2] == 'r')
4380 got_code_from_term(tp + j, i);
4381 key_name[0] = (int)KS_EXTRA;
4382 key_name[1] = (int)KE_IGNORE;
4383 slen = i + 1 + (tp[i] == ESC);
4384 break;
4385 }
4386
4387 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004388 {
4389 LOG_TR("not enough characters for XT");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 return -1; /* not enough characters */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
4393 }
4394#endif
4395
4396 if (key_name[0] == NUL)
4397 continue; /* No match at this position, try next one */
4398
4399 /* We only get here when we have a complete termcode match */
4400
4401#ifdef FEAT_MOUSE
4402# ifdef FEAT_GUI
4403 /*
4404 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4405 * so that we know which window to scroll later.
4406 */
4407 if (gui.in_use
4408 && key_name[0] == (int)KS_EXTRA
4409 && (key_name[1] == (int)KE_X1MOUSE
4410 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004411 || key_name[1] == (int)KE_MOUSELEFT
4412 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 || key_name[1] == (int)KE_MOUSEDOWN
4414 || key_name[1] == (int)KE_MOUSEUP))
4415 {
4416 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4417 if (num_bytes == -1) /* not enough coordinates */
4418 return -1;
4419 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4420 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4421 slen += num_bytes;
4422 }
4423 else
4424# endif
4425 /*
4426 * If it is a mouse click, get the coordinates.
4427 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004428 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004430 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431# endif
4432# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004433 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434# endif
4435# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004436 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437# endif
4438# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004439 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004441# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004442 || key_name[0] == KS_URXVT_MOUSE
4443# endif
4444# ifdef FEAT_MOUSE_SGR
4445 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 )
4448 {
4449 is_click = is_drag = FALSE;
4450
Bram Moolenaar864207d2008-06-24 22:14:38 +00004451# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4452 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 if (key_name[0] == (int)KS_MOUSE)
4454 {
4455 /*
4456 * For xterm and MSDOS we get "<t_mouse>scr", where
4457 * s == encoded button state:
4458 * 0x20 = left button down
4459 * 0x21 = middle button down
4460 * 0x22 = right button down
4461 * 0x23 = any button release
4462 * 0x60 = button 4 down (scroll wheel down)
4463 * 0x61 = button 5 down (scroll wheel up)
4464 * add 0x04 for SHIFT
4465 * add 0x08 for ALT
4466 * add 0x10 for CTRL
4467 * add 0x20 for mouse drag (0x40 is drag with left button)
4468 * c == column + ' ' + 1 == column + 33
4469 * r == row + ' ' + 1 == row + 33
4470 *
4471 * The coordinates are passed on through global variables.
4472 * Ugly, but this avoids trouble with mouse clicks at an
4473 * unexpected moment and allows for mapping them.
4474 */
4475 for (;;)
4476 {
4477#ifdef FEAT_GUI
4478 if (gui.in_use)
4479 {
4480 /* GUI uses more bits for columns > 223 */
4481 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4482 if (num_bytes == -1) /* not enough coordinates */
4483 return -1;
4484 mouse_code = bytes[0];
4485 mouse_col = 128 * (bytes[1] - ' ' - 1)
4486 + bytes[2] - ' ' - 1;
4487 mouse_row = 128 * (bytes[3] - ' ' - 1)
4488 + bytes[4] - ' ' - 1;
4489 }
4490 else
4491#endif
4492 {
4493 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4494 if (num_bytes == -1) /* not enough coordinates */
4495 return -1;
4496 mouse_code = bytes[0];
4497 mouse_col = bytes[1] - ' ' - 1;
4498 mouse_row = bytes[2] - ' ' - 1;
4499 }
4500 slen += num_bytes;
4501
4502 /* If the following bytes is also a mouse code and it has
4503 * the same code, dump this one and get the next. This
4504 * makes dragging a whole lot faster. */
4505#ifdef FEAT_GUI
4506 if (gui.in_use)
4507 j = 3;
4508 else
4509#endif
4510 j = termcodes[idx].len;
4511 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4512 && tp[slen + j] == mouse_code
4513 && tp[slen + j + 1] != NUL
4514 && tp[slen + j + 2] != NUL
4515#ifdef FEAT_GUI
4516 && (!gui.in_use
4517 || (tp[slen + j + 3] != NUL
4518 && tp[slen + j + 4] != NUL))
4519#endif
4520 )
4521 slen += j;
4522 else
4523 break;
4524 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004527# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4528 if (key_name[0] == KS_URXVT_MOUSE
4529 || key_name[0] == KS_SGR_MOUSE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004530 {
4531 for (;;)
4532 {
4533 /* URXVT 1015 mouse reporting mode:
4534 * Almost identical to xterm mouse mode, except the values
4535 * are decimal instead of bytes.
4536 *
4537 * \033[%d;%d;%dM
4538 * ^-- row
4539 * ^----- column
4540 * ^-------- code
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004541 *
4542 * SGR 1006 mouse reporting mode:
4543 * Almost identical to xterm mouse mode, except the values
4544 * are decimal instead of bytes.
4545 *
4546 * \033[<%d;%d;%dM
4547 * ^-- row
4548 * ^----- column
4549 * ^-------- code
4550 *
4551 * \033[<%d;%d;%dm : mouse release event
4552 * ^-- row
4553 * ^----- column
4554 * ^-------- code
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004555 */
4556 p = tp + slen;
4557
4558 mouse_code = getdigits(&p);
4559 if (*p++ != ';')
4560 return -1;
4561
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004562 /* when mouse reporting is SGR, add 32 to mouse code */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004563 if (key_name[0] == KS_SGR_MOUSE)
4564 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004565
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004566 mouse_col = getdigits(&p) - 1;
4567 if (*p++ != ';')
4568 return -1;
4569
4570 mouse_row = getdigits(&p) - 1;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004571 if (key_name[0] == KS_SGR_MOUSE && *p == 'm')
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004572 mouse_code |= MOUSE_RELEASE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004573 else if (*p != 'M')
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004574 return -1;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004575 p++;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004576
4577 slen += (int)(p - (tp + slen));
4578
4579 /* skip this one if next one has same code (like xterm
4580 * case) */
4581 j = termcodes[idx].len;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004582 if (STRNCMP(tp, tp + slen, (size_t)j) == 0)
4583 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004584 int slen2;
4585 int cmd_complete = 0;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004586
4587 /* check if the command is complete by looking for the
4588 * 'M' */
4589 for (slen2 = slen; slen2 < len; slen2++)
4590 {
4591 if (tp[slen2] == 'M'
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004592 || (key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004593 && tp[slen2] == 'm'))
4594 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004595 cmd_complete = 1;
4596 break;
4597 }
4598 }
4599 p += j;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004600 if (cmd_complete && getdigits(&p) == mouse_code)
4601 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004602 slen += j; /* skip the \033[ */
4603 continue;
4604 }
4605 }
4606 break;
4607 }
4608 }
4609# endif
4610
4611 if (key_name[0] == (int)KS_MOUSE
4612#ifdef FEAT_MOUSE_URXVT
4613 || key_name[0] == (int)KS_URXVT_MOUSE
4614#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004615#ifdef FEAT_MOUSE_SGR
4616 || key_name[0] == KS_SGR_MOUSE
4617#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004618 )
4619 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620# if !defined(MSWIN) && !defined(MSDOS)
4621 /*
4622 * Handle mouse events.
4623 * Recognize the xterm mouse wheel, but not in the GUI, the
4624 * Linux console with GPM and the MS-DOS or Win32 console
4625 * (multi-clicks use >= 0x60).
4626 */
4627 if (mouse_code >= MOUSEWHEEL_LOW
4628# ifdef FEAT_GUI
4629 && !gui.in_use
4630# endif
4631# ifdef FEAT_MOUSE_GPM
4632 && gpm_flag == 0
4633# endif
4634 )
4635 {
4636 /* Keep the mouse_code before it's changed, so that we
4637 * remember that it was a mouse wheel click. */
4638 wheel_code = mouse_code;
4639 }
4640# ifdef FEAT_MOUSE_XTERM
4641 else if (held_button == MOUSE_RELEASE
4642# ifdef FEAT_GUI
4643 && !gui.in_use
4644# endif
4645 && (mouse_code == 0x23 || mouse_code == 0x24))
4646 {
4647 /* Apparently used by rxvt scroll wheel. */
4648 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4649 }
4650# endif
4651
4652# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4653 else if (use_xterm_mouse() > 1)
4654 {
4655 if (mouse_code & MOUSE_DRAG_XTERM)
4656 mouse_code |= MOUSE_DRAG;
4657 }
4658# endif
4659# ifdef FEAT_XCLIPBOARD
4660 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4661 {
4662 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4663 stop_xterm_trace();
4664 else
4665 start_xterm_trace(mouse_code);
4666 }
4667# endif
4668# endif
4669 }
4670# endif /* !UNIX || FEAT_MOUSE_XTERM */
4671# ifdef FEAT_MOUSE_NET
4672 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4673 {
4674 int mc, mr;
4675
4676 /* expect a rather limited sequence like: balancing {
4677 * \033}6,45\r
4678 * '6' is the row, 45 is the column
4679 */
4680 p = tp + slen;
4681 mr = getdigits(&p);
4682 if (*p++ != ',')
4683 return -1;
4684 mc = getdigits(&p);
4685 if (*p++ != '\r')
4686 return -1;
4687
4688 mouse_col = mc - 1;
4689 mouse_row = mr - 1;
4690 mouse_code = MOUSE_LEFT;
4691 slen += (int)(p - (tp + slen));
4692 }
4693# endif /* FEAT_MOUSE_NET */
4694# ifdef FEAT_MOUSE_JSB
4695 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4696 {
4697 int mult, val, iter, button, status;
4698
4699 /* JSBTERM Input Model
4700 * \033[0~zw uniq escape sequence
4701 * (L-x) Left button pressed - not pressed x not reporting
4702 * (M-x) Middle button pressed - not pressed x not reporting
4703 * (R-x) Right button pressed - not pressed x not reporting
4704 * (SDmdu) Single , Double click, m mouse move d button down
4705 * u button up
4706 * ### X cursor position padded to 3 digits
4707 * ### Y cursor position padded to 3 digits
4708 * (s-x) SHIFT key pressed - not pressed x not reporting
4709 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004710 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 */
4712
4713 p = tp + slen;
4714 button = mouse_code = 0;
4715 switch (*p++)
4716 {
4717 case 'L': button = 1; break;
4718 case '-': break;
4719 case 'x': break; /* ignore sequence */
4720 default: return -1; /* Unknown Result */
4721 }
4722 switch (*p++)
4723 {
4724 case 'M': button |= 2; break;
4725 case '-': break;
4726 case 'x': break; /* ignore sequence */
4727 default: return -1; /* Unknown Result */
4728 }
4729 switch (*p++)
4730 {
4731 case 'R': button |= 4; break;
4732 case '-': break;
4733 case 'x': break; /* ignore sequence */
4734 default: return -1; /* Unknown Result */
4735 }
4736 status = *p++;
4737 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4738 mult /= 10, p++)
4739 if (*p >= '0' && *p <= '9')
4740 val += (*p - '0') * mult;
4741 else
4742 return -1;
4743 mouse_col = val;
4744 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4745 mult /= 10, p++)
4746 if (*p >= '0' && *p <= '9')
4747 val += (*p - '0') * mult;
4748 else
4749 return -1;
4750 mouse_row = val;
4751 switch (*p++)
4752 {
4753 case 's': button |= 8; break; /* SHIFT key Pressed */
4754 case '-': break; /* Not Pressed */
4755 case 'x': break; /* Not Reporting */
4756 default: return -1; /* Unknown Result */
4757 }
4758 switch (*p++)
4759 {
4760 case 'c': button |= 16; break; /* CTRL key Pressed */
4761 case '-': break; /* Not Pressed */
4762 case 'x': break; /* Not Reporting */
4763 default: return -1; /* Unknown Result */
4764 }
4765 if (*p++ != '\033')
4766 return -1;
4767 if (*p++ != '\\')
4768 return -1;
4769 switch (status)
4770 {
4771 case 'D': /* Double Click */
4772 case 'S': /* Single Click */
4773 if (button & 1) mouse_code |= MOUSE_LEFT;
4774 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4775 if (button & 4) mouse_code |= MOUSE_RIGHT;
4776 if (button & 8) mouse_code |= MOUSE_SHIFT;
4777 if (button & 16) mouse_code |= MOUSE_CTRL;
4778 break;
4779 case 'm': /* Mouse move */
4780 if (button & 1) mouse_code |= MOUSE_LEFT;
4781 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4782 if (button & 4) mouse_code |= MOUSE_RIGHT;
4783 if (button & 8) mouse_code |= MOUSE_SHIFT;
4784 if (button & 16) mouse_code |= MOUSE_CTRL;
4785 if ((button & 7) != 0)
4786 {
4787 held_button = mouse_code;
4788 mouse_code |= MOUSE_DRAG;
4789 }
4790 is_drag = TRUE;
4791 showmode();
4792 break;
4793 case 'd': /* Button Down */
4794 if (button & 1) mouse_code |= MOUSE_LEFT;
4795 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4796 if (button & 4) mouse_code |= MOUSE_RIGHT;
4797 if (button & 8) mouse_code |= MOUSE_SHIFT;
4798 if (button & 16) mouse_code |= MOUSE_CTRL;
4799 break;
4800 case 'u': /* Button Up */
4801 if (button & 1)
4802 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4803 if (button & 2)
4804 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4805 if (button & 4)
4806 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4807 if (button & 8)
4808 mouse_code |= MOUSE_SHIFT;
4809 if (button & 16)
4810 mouse_code |= MOUSE_CTRL;
4811 break;
4812 default: return -1; /* Unknown Result */
4813 }
4814
4815 slen += (p - (tp + slen));
4816 }
4817# endif /* FEAT_MOUSE_JSB */
4818# ifdef FEAT_MOUSE_DEC
4819 if (key_name[0] == (int)KS_DEC_MOUSE)
4820 {
4821 /* The DEC Locator Input Model
4822 * Netterm delivers the code sequence:
4823 * \033[2;4;24;80&w (left button down)
4824 * \033[3;0;24;80&w (left button up)
4825 * \033[6;1;24;80&w (right button down)
4826 * \033[7;0;24;80&w (right button up)
4827 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4828 * Pe is the event code
4829 * Pb is the button code
4830 * Pr is the row coordinate
4831 * Pc is the column coordinate
4832 * Pp is the third coordinate (page number)
4833 * Pe, the event code indicates what event caused this report
4834 * The following event codes are defined:
4835 * 0 - request, the terminal received an explicit request
4836 * for a locator report, but the locator is unavailable
4837 * 1 - request, the terminal received an explicit request
4838 * for a locator report
4839 * 2 - left button down
4840 * 3 - left button up
4841 * 4 - middle button down
4842 * 5 - middle button up
4843 * 6 - right button down
4844 * 7 - right button up
4845 * 8 - fourth button down
4846 * 9 - fourth button up
4847 * 10 - locator outside filter rectangle
4848 * Pb, the button code, ASCII decimal 0-15 indicating which
4849 * buttons are down if any. The state of the four buttons
4850 * on the locator correspond to the low four bits of the
4851 * decimal value,
4852 * "1" means button depressed
4853 * 0 - no buttons down,
4854 * 1 - right,
4855 * 2 - middle,
4856 * 4 - left,
4857 * 8 - fourth
4858 * Pr is the row coordinate of the locator position in the page,
4859 * encoded as an ASCII decimal value.
4860 * If Pr is omitted, the locator position is undefined
4861 * (outside the terminal window for example).
4862 * Pc is the column coordinate of the locator position in the
4863 * page, encoded as an ASCII decimal value.
4864 * If Pc is omitted, the locator position is undefined
4865 * (outside the terminal window for example).
4866 * Pp is the page coordinate of the locator position
4867 * encoded as an ASCII decimal value.
4868 * The page coordinate may be omitted if the locator is on
4869 * page one (the default). We ignore it anyway.
4870 */
4871 int Pe, Pb, Pr, Pc;
4872
4873 p = tp + slen;
4874
4875 /* get event status */
4876 Pe = getdigits(&p);
4877 if (*p++ != ';')
4878 return -1;
4879
4880 /* get button status */
4881 Pb = getdigits(&p);
4882 if (*p++ != ';')
4883 return -1;
4884
4885 /* get row status */
4886 Pr = getdigits(&p);
4887 if (*p++ != ';')
4888 return -1;
4889
4890 /* get column status */
4891 Pc = getdigits(&p);
4892
4893 /* the page parameter is optional */
4894 if (*p == ';')
4895 {
4896 p++;
4897 (void)getdigits(&p);
4898 }
4899 if (*p++ != '&')
4900 return -1;
4901 if (*p++ != 'w')
4902 return -1;
4903
4904 mouse_code = 0;
4905 switch (Pe)
4906 {
4907 case 0: return -1; /* position request while unavailable */
4908 case 1: /* a response to a locator position request includes
4909 the status of all buttons */
4910 Pb &= 7; /* mask off and ignore fourth button */
4911 if (Pb & 4)
4912 mouse_code = MOUSE_LEFT;
4913 if (Pb & 2)
4914 mouse_code = MOUSE_MIDDLE;
4915 if (Pb & 1)
4916 mouse_code = MOUSE_RIGHT;
4917 if (Pb)
4918 {
4919 held_button = mouse_code;
4920 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004921 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 is_drag = TRUE;
4924 showmode();
4925 break;
4926 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004927 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 break;
4929 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
4930 break;
4931 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004932 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 break;
4934 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
4935 break;
4936 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004937 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 break;
4939 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
4940 break;
4941 case 8: return -1; /* fourth button down */
4942 case 9: return -1; /* fourth button up */
4943 case 10: return -1; /* mouse outside of filter rectangle */
4944 default: return -1; /* should never occur */
4945 }
4946
4947 mouse_col = Pc - 1;
4948 mouse_row = Pr - 1;
4949
4950 slen += (int)(p - (tp + slen));
4951 }
4952# endif /* FEAT_MOUSE_DEC */
4953# ifdef FEAT_MOUSE_PTERM
4954 if (key_name[0] == (int)KS_PTERM_MOUSE)
4955 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004956 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957
4958 p = tp + slen;
4959
4960 action = getdigits(&p);
4961 if (*p++ != ';')
4962 return -1;
4963
4964 mouse_row = getdigits(&p);
4965 if (*p++ != ';')
4966 return -1;
4967 mouse_col = getdigits(&p);
4968 if (*p++ != ';')
4969 return -1;
4970
4971 button = getdigits(&p);
4972 mouse_code = 0;
4973
4974 switch( button )
4975 {
4976 case 4: mouse_code = MOUSE_LEFT; break;
4977 case 1: mouse_code = MOUSE_RIGHT; break;
4978 case 2: mouse_code = MOUSE_MIDDLE; break;
4979 default: return -1;
4980 }
4981
4982 switch( action )
4983 {
4984 case 31: /* Initial press */
4985 if (*p++ != ';')
4986 return -1;
4987
4988 num_clicks = getdigits(&p); /* Not used */
4989 break;
4990
4991 case 32: /* Release */
4992 mouse_code |= MOUSE_RELEASE;
4993 break;
4994
4995 case 33: /* Drag */
4996 held_button = mouse_code;
4997 mouse_code |= MOUSE_DRAG;
4998 break;
4999
5000 default:
5001 return -1;
5002 }
5003
5004 if (*p++ != 't')
5005 return -1;
5006
5007 slen += (p - (tp + slen));
5008 }
5009# endif /* FEAT_MOUSE_PTERM */
5010
5011 /* Interpret the mouse code */
5012 current_button = (mouse_code & MOUSE_CLICK_MASK);
5013 if (current_button == MOUSE_RELEASE
5014# ifdef FEAT_MOUSE_XTERM
5015 && wheel_code == 0
5016# endif
5017 )
5018 {
5019 /*
5020 * If we get a mouse drag or release event when
5021 * there is no mouse button held down (held_button ==
5022 * MOUSE_RELEASE), produce a K_IGNORE below.
5023 * (can happen when you hold down two buttons
5024 * and then let them go, or click in the menu bar, but not
5025 * on a menu, and drag into the text).
5026 */
5027 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5028 is_drag = TRUE;
5029 current_button = held_button;
5030 }
5031 else if (wheel_code == 0)
5032 {
5033# ifdef CHECK_DOUBLE_CLICK
5034# ifdef FEAT_MOUSE_GPM
5035# ifdef FEAT_GUI
5036 /*
5037 * Only for Unix, when GUI or gpm is not active, we handle
5038 * multi-clicks here.
5039 */
5040 if (gpm_flag == 0 && !gui.in_use)
5041# else
5042 if (gpm_flag == 0)
5043# endif
5044# else
5045# ifdef FEAT_GUI
5046 if (!gui.in_use)
5047# endif
5048# endif
5049 {
5050 /*
5051 * Compute the time elapsed since the previous mouse click.
5052 */
5053 gettimeofday(&mouse_time, NULL);
5054 timediff = (mouse_time.tv_usec
5055 - orig_mouse_time.tv_usec) / 1000;
5056 if (timediff < 0)
5057 --orig_mouse_time.tv_sec;
5058 timediff += (mouse_time.tv_sec
5059 - orig_mouse_time.tv_sec) * 1000;
5060 orig_mouse_time = mouse_time;
5061 if (mouse_code == orig_mouse_code
5062 && timediff < p_mouset
5063 && orig_num_clicks != 4
5064 && orig_mouse_col == mouse_col
5065 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005066 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005068 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005070 )
5071#ifdef FEAT_WINDOWS
5072 /* Double click in tab pages line also works
5073 * when window contents changes. */
5074 || (mouse_row == 0 && firstwin->w_winrow > 0)
5075#endif
5076 )
5077 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 ++orig_num_clicks;
5079 else
5080 orig_num_clicks = 1;
5081 orig_mouse_col = mouse_col;
5082 orig_mouse_row = mouse_row;
5083 orig_topline = curwin->w_topline;
5084#ifdef FEAT_DIFF
5085 orig_topfill = curwin->w_topfill;
5086#endif
5087 }
5088# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5089 else
5090 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5091# endif
5092# else
5093 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5094# endif
5095 is_click = TRUE;
5096 orig_mouse_code = mouse_code;
5097 }
5098 if (!is_drag)
5099 held_button = mouse_code & MOUSE_CLICK_MASK;
5100
5101 /*
5102 * Translate the actual mouse event into a pseudo mouse event.
5103 * First work out what modifiers are to be used.
5104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 if (orig_mouse_code & MOUSE_SHIFT)
5106 modifiers |= MOD_MASK_SHIFT;
5107 if (orig_mouse_code & MOUSE_CTRL)
5108 modifiers |= MOD_MASK_CTRL;
5109 if (orig_mouse_code & MOUSE_ALT)
5110 modifiers |= MOD_MASK_ALT;
5111 if (orig_num_clicks == 2)
5112 modifiers |= MOD_MASK_2CLICK;
5113 else if (orig_num_clicks == 3)
5114 modifiers |= MOD_MASK_3CLICK;
5115 else if (orig_num_clicks == 4)
5116 modifiers |= MOD_MASK_4CLICK;
5117
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 /* Work out our pseudo mouse event */
5119 key_name[0] = (int)KS_EXTRA;
5120 if (wheel_code != 0)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005121 {
5122 if (wheel_code & MOUSE_CTRL)
5123 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005124 if (wheel_code & MOUSE_ALT)
5125 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 key_name[1] = (wheel_code & 1)
5127 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 else
5130 key_name[1] = get_pseudo_mouse_code(current_button,
5131 is_click, is_drag);
5132 }
5133#endif /* FEAT_MOUSE */
5134
5135#ifdef FEAT_GUI
5136 /*
5137 * If using the GUI, then we get menu and scrollbar events.
5138 *
5139 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5140 * four bytes which are to be taken as a pointer to the vimmenu_T
5141 * structure.
5142 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005143 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005144 * is one byte with the tab index.
5145 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5147 * by one byte representing the scrollbar number, and then four bytes
5148 * representing a long_u which is the new value of the scrollbar.
5149 *
5150 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5151 * KE_FILLER followed by four bytes representing a long_u which is the
5152 * new value of the scrollbar.
5153 */
5154# ifdef FEAT_MENU
5155 else if (key_name[0] == (int)KS_MENU)
5156 {
5157 long_u val;
5158
5159 num_bytes = get_long_from_buf(tp + slen, &val);
5160 if (num_bytes == -1)
5161 return -1;
5162 current_menu = (vimmenu_T *)val;
5163 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005164
5165 /* The menu may have been deleted right after it was used, check
5166 * for that. */
5167 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5168 {
5169 key_name[0] = KS_EXTRA;
5170 key_name[1] = (int)KE_IGNORE;
5171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005174# ifdef FEAT_GUI_TABLINE
5175 else if (key_name[0] == (int)KS_TABLINE)
5176 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005177 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005178 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5179 if (num_bytes == -1)
5180 return -1;
5181 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005182 if (current_tab == 255) /* -1 in a byte gives 255 */
5183 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005184 slen += num_bytes;
5185 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005186 else if (key_name[0] == (int)KS_TABMENU)
5187 {
5188 /* Selecting tabline tab or using its menu. */
5189 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5190 if (num_bytes == -1)
5191 return -1;
5192 current_tab = (int)bytes[0];
5193 current_tabmenu = (int)bytes[1];
5194 slen += num_bytes;
5195 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197# ifndef USE_ON_FLY_SCROLL
5198 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5199 {
5200 long_u val;
5201
5202 /* Get the last scrollbar event in the queue of the same type */
5203 j = 0;
5204 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5205 && tp[j + 2] != NUL; ++i)
5206 {
5207 j += 3;
5208 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5209 if (num_bytes == -1)
5210 break;
5211 if (i == 0)
5212 current_scrollbar = (int)bytes[0];
5213 else if (current_scrollbar != (int)bytes[0])
5214 break;
5215 j += num_bytes;
5216 num_bytes = get_long_from_buf(tp + j, &val);
5217 if (num_bytes == -1)
5218 break;
5219 scrollbar_value = val;
5220 j += num_bytes;
5221 slen = j;
5222 }
5223 if (i == 0) /* not enough characters to make one */
5224 return -1;
5225 }
5226 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5227 {
5228 long_u val;
5229
5230 /* Get the last horiz. scrollbar event in the queue */
5231 j = 0;
5232 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5233 && tp[j + 2] != NUL; ++i)
5234 {
5235 j += 3;
5236 num_bytes = get_long_from_buf(tp + j, &val);
5237 if (num_bytes == -1)
5238 break;
5239 scrollbar_value = val;
5240 j += num_bytes;
5241 slen = j;
5242 }
5243 if (i == 0) /* not enough characters to make one */
5244 return -1;
5245 }
5246# endif /* !USE_ON_FLY_SCROLL */
5247#endif /* FEAT_GUI */
5248
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005249 /*
5250 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5251 */
5252 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5253
5254 /*
5255 * Add any modifier codes to our string.
5256 */
5257 new_slen = 0; /* Length of what will replace the termcode */
5258 if (modifiers != 0)
5259 {
5260 /* Some keys have the modifier included. Need to handle that here
5261 * to make mappings work. */
5262 key = simplify_key(key, &modifiers);
5263 if (modifiers != 0)
5264 {
5265 string[new_slen++] = K_SPECIAL;
5266 string[new_slen++] = (int)KS_MODIFIER;
5267 string[new_slen++] = modifiers;
5268 }
5269 }
5270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005272 key_name[0] = KEY2TERMCAP0(key);
5273 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005275 {
5276 /* from ":set <M-b>=xx" */
5277#ifdef FEAT_MBYTE
5278 if (has_mbyte)
5279 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5280 else
5281#endif
5282 string[new_slen++] = key_name[1];
5283 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005284 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5285 && key_name[1] == KE_IGNORE)
5286 {
5287 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5288 * to indicate what happened. */
5289 retval = KEYLEN_REMOVED;
5290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 else
5292 {
5293 string[new_slen++] = K_SPECIAL;
5294 string[new_slen++] = key_name[0];
5295 string[new_slen++] = key_name[1];
5296 }
5297 string[new_slen] = NUL;
5298 extra = new_slen - slen;
5299 if (buf == NULL)
5300 {
5301 if (extra < 0)
5302 /* remove matched chars, taking care of noremap */
5303 del_typebuf(-extra, offset);
5304 else if (extra > 0)
5305 /* insert the extra space we need */
5306 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5307
5308 /*
5309 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5310 * typebuf.tb_buf[]!
5311 */
5312 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5313 (size_t)new_slen);
5314 }
5315 else
5316 {
5317 if (extra < 0)
5318 /* remove matched characters */
5319 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005320 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005322 {
5323 /* Insert the extra space we need. If there is insufficient
5324 * space return -1. */
5325 if (*buflen + extra + new_slen >= bufsize)
5326 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005328 (size_t)(*buflen - offset));
5329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005331 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005333 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335
Bram Moolenaar2951b772013-07-03 12:45:31 +02005336#ifdef FEAT_TERMRESPONSE
5337 LOG_TR("normal character");
5338#endif
5339
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 return 0; /* no match found */
5341}
5342
5343/*
5344 * Replace any terminal code strings in from[] with the equivalent internal
5345 * vim representation. This is used for the "from" and "to" part of a
5346 * mapping, and the "to" part of a menu command.
5347 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5348 * '<'.
5349 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5350 *
5351 * The replacement is done in result[] and finally copied into allocated
5352 * memory. If this all works well *bufp is set to the allocated memory and a
5353 * pointer to it is returned. If something fails *bufp is set to NULL and from
5354 * is returned.
5355 *
5356 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5357 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5358 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5359 * instead of a CTRL-V.
5360 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005361 char_u *
5362replace_termcodes(from, bufp, from_part, do_lt, special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 char_u *from;
5364 char_u **bufp;
5365 int from_part;
5366 int do_lt; /* also translate <lt> */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005367 int special; /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368{
5369 int i;
5370 int slen;
5371 int key;
5372 int dlen = 0;
5373 char_u *src;
5374 int do_backslash; /* backslash is a special character */
5375 int do_special; /* recognize <> key codes */
5376 int do_key_code; /* recognize raw key codes */
5377 char_u *result; /* buffer for resulting string */
5378
5379 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005380 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5382
5383 /*
5384 * Allocate space for the translation. Worst case a single character is
5385 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5386 */
5387 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5388 if (result == NULL) /* out of memory */
5389 {
5390 *bufp = NULL;
5391 return from;
5392 }
5393
5394 src = from;
5395
5396 /*
5397 * Check for #n at start only: function key n
5398 */
5399 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5400 {
5401 result[dlen++] = K_SPECIAL;
5402 result[dlen++] = 'k';
5403 if (src[1] == '0')
5404 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5405 else
5406 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5407 src += 2;
5408 }
5409
5410 /*
5411 * Copy each byte from *from to result[dlen]
5412 */
5413 while (*src != NUL)
5414 {
5415 /*
5416 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005417 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 */
5419 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5420 {
5421#ifdef FEAT_EVAL
5422 /*
5423 * Replace <SID> by K_SNR <script-nr> _.
5424 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5425 */
5426 if (STRNICMP(src, "<SID>", 5) == 0)
5427 {
5428 if (current_SID <= 0)
5429 EMSG(_(e_usingsid));
5430 else
5431 {
5432 src += 5;
5433 result[dlen++] = K_SPECIAL;
5434 result[dlen++] = (int)KS_EXTRA;
5435 result[dlen++] = (int)KE_SNR;
5436 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5437 dlen += (int)STRLEN(result + dlen);
5438 result[dlen++] = '_';
5439 continue;
5440 }
5441 }
5442#endif
5443
5444 slen = trans_special(&src, result + dlen, TRUE);
5445 if (slen)
5446 {
5447 dlen += slen;
5448 continue;
5449 }
5450 }
5451
5452 /*
5453 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5454 * Note that this is also checked after replacing the <> form.
5455 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5456 * it could be a character in the file.
5457 */
5458 if (do_key_code)
5459 {
5460 i = find_term_bykeys(src);
5461 if (i >= 0)
5462 {
5463 result[dlen++] = K_SPECIAL;
5464 result[dlen++] = termcodes[i].name[0];
5465 result[dlen++] = termcodes[i].name[1];
5466 src += termcodes[i].len;
5467 /* If terminal code matched, continue after it. */
5468 continue;
5469 }
5470 }
5471
5472#ifdef FEAT_EVAL
5473 if (do_special)
5474 {
5475 char_u *p, *s, len;
5476
5477 /*
5478 * Replace <Leader> by the value of "mapleader".
5479 * Replace <LocalLeader> by the value of "maplocalleader".
5480 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5481 */
5482 if (STRNICMP(src, "<Leader>", 8) == 0)
5483 {
5484 len = 8;
5485 p = get_var_value((char_u *)"g:mapleader");
5486 }
5487 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5488 {
5489 len = 13;
5490 p = get_var_value((char_u *)"g:maplocalleader");
5491 }
5492 else
5493 {
5494 len = 0;
5495 p = NULL;
5496 }
5497 if (len != 0)
5498 {
5499 /* Allow up to 8 * 6 characters for "mapleader". */
5500 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5501 s = (char_u *)"\\";
5502 else
5503 s = p;
5504 while (*s != NUL)
5505 result[dlen++] = *s++;
5506 src += len;
5507 continue;
5508 }
5509 }
5510#endif
5511
5512 /*
5513 * Remove CTRL-V and ignore the next character.
5514 * For "from" side the CTRL-V at the end is included, for the "to"
5515 * part it is removed.
5516 * If 'cpoptions' does not contain 'B', also accept a backslash.
5517 */
5518 key = *src;
5519 if (key == Ctrl_V || (do_backslash && key == '\\'))
5520 {
5521 ++src; /* skip CTRL-V or backslash */
5522 if (*src == NUL)
5523 {
5524 if (from_part)
5525 result[dlen++] = key;
5526 break;
5527 }
5528 }
5529
5530#ifdef FEAT_MBYTE
5531 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005532 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533#endif
5534 {
5535 /*
5536 * If the character is K_SPECIAL, replace it with K_SPECIAL
5537 * KS_SPECIAL KE_FILLER.
5538 * If compiled with the GUI replace CSI with K_CSI.
5539 */
5540 if (*src == K_SPECIAL)
5541 {
5542 result[dlen++] = K_SPECIAL;
5543 result[dlen++] = KS_SPECIAL;
5544 result[dlen++] = KE_FILLER;
5545 }
5546# ifdef FEAT_GUI
5547 else if (*src == CSI)
5548 {
5549 result[dlen++] = K_SPECIAL;
5550 result[dlen++] = KS_EXTRA;
5551 result[dlen++] = (int)KE_CSI;
5552 }
5553# endif
5554 else
5555 result[dlen++] = *src;
5556 ++src;
5557 }
5558 }
5559 result[dlen] = NUL;
5560
5561 /*
5562 * Copy the new string to allocated memory.
5563 * If this fails, just return from.
5564 */
5565 if ((*bufp = vim_strsave(result)) != NULL)
5566 from = *bufp;
5567 vim_free(result);
5568 return from;
5569}
5570
5571/*
5572 * Find a termcode with keys 'src' (must be NUL terminated).
5573 * Return the index in termcodes[], or -1 if not found.
5574 */
5575 int
5576find_term_bykeys(src)
5577 char_u *src;
5578{
5579 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005580 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581
5582 for (i = 0; i < tc_len; ++i)
5583 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005584 if (slen == termcodes[i].len
5585 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 return i;
5587 }
5588 return -1;
5589}
5590
5591/*
5592 * Gather the first characters in the terminal key codes into a string.
5593 * Used to speed up check_termcode().
5594 */
5595 static void
5596gather_termleader()
5597{
5598 int i;
5599 int len = 0;
5600
5601#ifdef FEAT_GUI
5602 if (gui.in_use)
5603 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5604#endif
5605#ifdef FEAT_TERMRESPONSE
5606 if (check_for_codes)
5607 termleader[len++] = DCS; /* the termcode response starts with DCS
5608 in 8-bit mode */
5609#endif
5610 termleader[len] = NUL;
5611
5612 for (i = 0; i < tc_len; ++i)
5613 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5614 {
5615 termleader[len++] = termcodes[i].code[0];
5616 termleader[len] = NUL;
5617 }
5618
5619 need_gather = FALSE;
5620}
5621
5622/*
5623 * Show all termcodes (for ":set termcap")
5624 * This code looks a lot like showoptions(), but is different.
5625 */
5626 void
5627show_termcodes()
5628{
5629 int col;
5630 int *items;
5631 int item_count;
5632 int run;
5633 int row, rows;
5634 int cols;
5635 int i;
5636 int len;
5637
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005638#define INC3 27 /* try to make three columns */
5639#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640#define GAP 2 /* spaces between columns */
5641
5642 if (tc_len == 0) /* no terminal codes (must be GUI) */
5643 return;
5644 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5645 if (items == NULL)
5646 return;
5647
5648 /* Highlight title */
5649 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5650
5651 /*
5652 * do the loop two times:
5653 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005654 * 2. display the medium items (medium length strings)
5655 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005657 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 {
5659 /*
5660 * collect the items in items[]
5661 */
5662 item_count = 0;
5663 for (i = 0; i < tc_len; i++)
5664 {
5665 len = show_one_termcode(termcodes[i].name,
5666 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005667 if (len <= INC3 - GAP ? run == 1
5668 : len <= INC2 - GAP ? run == 2
5669 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 items[item_count++] = i;
5671 }
5672
5673 /*
5674 * display the items
5675 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005676 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005678 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 if (cols == 0)
5680 cols = 1;
5681 rows = (item_count + cols - 1) / cols;
5682 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005683 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 rows = item_count;
5685 for (row = 0; row < rows && !got_int; ++row)
5686 {
5687 msg_putchar('\n'); /* go to next line */
5688 if (got_int) /* 'q' typed in more */
5689 break;
5690 col = 0;
5691 for (i = row; i < item_count; i += rows)
5692 {
5693 msg_col = col; /* make columns */
5694 show_one_termcode(termcodes[items[i]].name,
5695 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005696 if (run == 2)
5697 col += INC2;
5698 else
5699 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
5701 out_flush();
5702 ui_breakcheck();
5703 }
5704 }
5705 vim_free(items);
5706}
5707
5708/*
5709 * Show one termcode entry.
5710 * Output goes into IObuff[]
5711 */
5712 int
5713show_one_termcode(name, code, printit)
5714 char_u *name;
5715 char_u *code;
5716 int printit;
5717{
5718 char_u *p;
5719 int len;
5720
5721 if (name[0] > '~')
5722 {
5723 IObuff[0] = ' ';
5724 IObuff[1] = ' ';
5725 IObuff[2] = ' ';
5726 IObuff[3] = ' ';
5727 }
5728 else
5729 {
5730 IObuff[0] = 't';
5731 IObuff[1] = '_';
5732 IObuff[2] = name[0];
5733 IObuff[3] = name[1];
5734 }
5735 IObuff[4] = ' ';
5736
5737 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5738 if (p[1] != 't')
5739 STRCPY(IObuff + 5, p);
5740 else
5741 IObuff[5] = NUL;
5742 len = (int)STRLEN(IObuff);
5743 do
5744 IObuff[len++] = ' ';
5745 while (len < 17);
5746 IObuff[len] = NUL;
5747 if (code == NULL)
5748 len += 4;
5749 else
5750 len += vim_strsize(code);
5751
5752 if (printit)
5753 {
5754 msg_puts(IObuff);
5755 if (code == NULL)
5756 msg_puts((char_u *)"NULL");
5757 else
5758 msg_outtrans(code);
5759 }
5760 return len;
5761}
5762
5763#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5764/*
5765 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5766 * termcap codes from the terminal itself.
5767 * We get them one by one to avoid a very long response string.
5768 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005769static int xt_index_in = 0;
5770static int xt_index_out = 0;
5771
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 static void
5773req_codes_from_term()
5774{
5775 xt_index_out = 0;
5776 xt_index_in = 0;
5777 req_more_codes_from_term();
5778}
5779
5780 static void
5781req_more_codes_from_term()
5782{
5783 char buf[11];
5784 int old_idx = xt_index_out;
5785
5786 /* Don't do anything when going to exit. */
5787 if (exiting)
5788 return;
5789
5790 /* Send up to 10 more requests out than we received. Avoid sending too
5791 * many, there can be a buffer overflow somewhere. */
5792 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5793 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02005794# ifdef DEBUG_TERMRESPONSE
5795 char dbuf[100];
5796
5797 sprintf(dbuf, "Requesting XT %d: %s",
5798 xt_index_out, key_names[xt_index_out]);
5799 log_tr(dbuf);
5800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 sprintf(buf, "\033P+q%02x%02x\033\\",
5802 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5803 out_str_nf((char_u *)buf);
5804 ++xt_index_out;
5805 }
5806
5807 /* Send the codes out right away. */
5808 if (xt_index_out != old_idx)
5809 out_flush();
5810}
5811
5812/*
5813 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5814 * A "0" instead of the "1" indicates a code that isn't supported.
5815 * Both <name> and <string> are encoded in hex.
5816 * "code" points to the "0" or "1".
5817 */
5818 static void
5819got_code_from_term(code, len)
5820 char_u *code;
5821 int len;
5822{
5823#define XT_LEN 100
5824 char_u name[3];
5825 char_u str[XT_LEN];
5826 int i;
5827 int j = 0;
5828 int c;
5829
5830 /* A '1' means the code is supported, a '0' means it isn't.
5831 * When half the length is > XT_LEN we can't use it.
5832 * Our names are currently all 2 characters. */
5833 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5834 {
5835 /* Get the name from the response and find it in the table. */
5836 name[0] = hexhex2nr(code + 3);
5837 name[1] = hexhex2nr(code + 5);
5838 name[2] = NUL;
5839 for (i = 0; key_names[i] != NULL; ++i)
5840 {
5841 if (STRCMP(key_names[i], name) == 0)
5842 {
5843 xt_index_in = i;
5844 break;
5845 }
5846 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005847# ifdef DEBUG_TERMRESPONSE
5848 {
5849 char buf[100];
5850
5851 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
5852 log_tr(buf);
5853 }
5854# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 if (key_names[i] != NULL)
5856 {
5857 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5858 str[j++] = c;
5859 str[j] = NUL;
5860 if (name[0] == 'C' && name[1] == 'o')
5861 {
5862 /* Color count is not a key code. */
5863 i = atoi((char *)str);
5864 if (i != t_colors)
5865 {
5866 /* Nr of colors changed, initialize highlighting and
Bram Moolenaar238a5642006-02-21 22:12:05 +00005867 * redraw everything. This causes a redraw, which usually
5868 * clears the message. Try keeping the message if it
5869 * might work. */
5870 set_keep_msg_from_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 set_color_count(i);
5872 init_highlight(TRUE, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +02005873#ifdef DEBUG_TERMRESPONSE
5874 {
5875 char buf[100];
5876 int r = redraw_asap(CLEAR);
5877
5878 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
5879 log_tr(buf);
5880 }
5881#else
5882 redraw_asap(CLEAR);
5883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 }
5885 }
5886 else
5887 {
5888 /* First delete any existing entry with the same code. */
5889 i = find_term_bykeys(str);
5890 if (i >= 0)
5891 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005892 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 }
5894 }
5895 }
5896
5897 /* May request more codes now that we received one. */
5898 ++xt_index_in;
5899 req_more_codes_from_term();
5900}
5901
5902/*
5903 * Check if there are any unanswered requests and deal with them.
5904 * This is called before starting an external program or getting direct
5905 * keyboard input. We don't want responses to be send to that program or
5906 * handled as typed text.
5907 */
5908 static void
5909check_for_codes_from_term()
5910{
5911 int c;
5912
5913 /* If no codes requested or all are answered, no need to wait. */
5914 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5915 return;
5916
5917 /* Vgetc() will check for and handle any response.
5918 * Keep calling vpeekc() until we don't get any responses. */
5919 ++no_mapping;
5920 ++allow_keys;
5921 for (;;)
5922 {
5923 c = vpeekc();
5924 if (c == NUL) /* nothing available */
5925 break;
5926
5927 /* If a response is recognized it's replaced with K_IGNORE, must read
5928 * it from the input stream. If there is no K_IGNORE we can't do
5929 * anything, break here (there might be some responses further on, but
5930 * we don't want to throw away any typed chars). */
5931 if (c != K_SPECIAL && c != K_IGNORE)
5932 break;
5933 c = vgetc();
5934 if (c != K_IGNORE)
5935 {
5936 vungetc(c);
5937 break;
5938 }
5939 }
5940 --no_mapping;
5941 --allow_keys;
5942}
5943#endif
5944
5945#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5946/*
5947 * Translate an internal mapping/abbreviation representation into the
5948 * corresponding external one recognized by :map/:abbrev commands;
5949 * respects the current B/k/< settings of 'cpoption'.
5950 *
5951 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00005952 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 *
5954 * It uses a growarray to build the translation string since the
5955 * latter can be wider than the original description. The caller has to
5956 * free the string afterwards.
5957 *
5958 * Returns NULL when there is a problem.
5959 */
5960 char_u *
5961translate_mapping(str, expmap)
5962 char_u *str;
5963 int expmap; /* TRUE when expanding mappings on command-line */
5964{
5965 garray_T ga;
5966 int c;
5967 int modifiers;
5968 int cpo_bslash;
5969 int cpo_special;
5970 int cpo_keycode;
5971
5972 ga_init(&ga);
5973 ga.ga_itemsize = 1;
5974 ga.ga_growsize = 40;
5975
5976 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
5977 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
5978 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5979
5980 for (; *str; ++str)
5981 {
5982 c = *str;
5983 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5984 {
5985 modifiers = 0;
5986 if (str[1] == KS_MODIFIER)
5987 {
5988 str++;
5989 modifiers = *++str;
5990 c = *++str;
5991 }
5992 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
5993 {
5994 int i;
5995
5996 /* try to find special key in termcodes */
5997 for (i = 0; i < tc_len; ++i)
5998 if (termcodes[i].name[0] == str[1]
5999 && termcodes[i].name[1] == str[2])
6000 break;
6001 if (i < tc_len)
6002 {
6003 ga_concat(&ga, termcodes[i].code);
6004 str += 2;
6005 continue; /* for (str) */
6006 }
6007 }
6008 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
6009 {
6010 if (expmap && cpo_special)
6011 {
6012 ga_clear(&ga);
6013 return NULL;
6014 }
6015 c = TO_SPECIAL(str[1], str[2]);
6016 if (c == K_ZERO) /* display <Nul> as ^@ */
6017 c = NUL;
6018 str += 2;
6019 }
6020 if (IS_SPECIAL(c) || modifiers) /* special key */
6021 {
6022 if (expmap && cpo_special)
6023 {
6024 ga_clear(&ga);
6025 return NULL;
6026 }
6027 ga_concat(&ga, get_special_key_name(c, modifiers));
6028 continue; /* for (str) */
6029 }
6030 }
6031 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6032 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6033 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6034 if (c)
6035 ga_append(&ga, c);
6036 }
6037 ga_append(&ga, NUL);
6038 return (char_u *)(ga.ga_data);
6039}
6040#endif
6041
6042#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6043static char ksme_str[20];
6044static char ksmr_str[20];
6045static char ksmd_str[20];
6046
6047/*
6048 * For Win32 console: update termcap codes for existing console attributes.
6049 */
6050 void
6051update_tcap(attr)
6052 int attr;
6053{
6054 struct builtin_term *p;
6055
6056 p = find_builtin_term(DEFAULT_TERM);
6057 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6058 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6059 attr | 0x08); /* FOREGROUND_INTENSITY */
6060 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6061 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6062
6063 while (p->bt_string != NULL)
6064 {
6065 if (p->bt_entry == (int)KS_ME)
6066 p->bt_string = &ksme_str[0];
6067 else if (p->bt_entry == (int)KS_MR)
6068 p->bt_string = &ksmr_str[0];
6069 else if (p->bt_entry == (int)KS_MD)
6070 p->bt_string = &ksmd_str[0];
6071 ++p;
6072 }
6073}
6074#endif