blob: ddbac97a49120d86924a7261c2dd9d1e5199d6cf [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
109/* Request Terminal Version status: */
110# define CRV_GET 1 /* send T_CRV when switched to RAW mode */
111# define CRV_SENT 2 /* did send T_CRV, waiting for answer */
112# define CRV_GOT 3 /* received T_CRV response */
113static int crv_status = CRV_GET;
Bram Moolenaar9584b312013-03-13 19:29:28 +0100114/* Request Cursor position report: */
115# define U7_GET 1 /* send T_U7 when switched to RAW mode */
116# define U7_SENT 2 /* did send T_U7, waiting for answer */
117# define U7_GOT 3 /* received T_U7 response */
118static int u7_status = U7_GET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119# endif
120
121/*
122 * Don't declare these variables if termcap.h contains them.
123 * Autoconf checks if these variables should be declared extern (not all
124 * systems have them).
125 * Some versions define ospeed to be speed_t, but that is incompatible with
126 * BSD, where ospeed is short and speed_t is long.
127 */
128# ifndef HAVE_OSPEED
129# ifdef OSPEED_EXTERN
130extern short ospeed;
131# else
132short ospeed;
133# endif
134# endif
135# ifndef HAVE_UP_BC_PC
136# ifdef UP_BC_PC_EXTERN
137extern char *UP, *BC, PC;
138# else
139char *UP, *BC, PC;
140# endif
141# endif
142
143# define TGETSTR(s, p) vim_tgetstr((s), (p))
144# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
145static char_u *vim_tgetstr __ARGS((char *s, char_u **pp));
146#endif /* HAVE_TGETENT */
147
148static int detected_8bit = FALSE; /* detected 8-bit terminal */
149
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000150static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151{
152
153#if defined(FEAT_GUI)
154/*
155 * GUI pseudo term-cap.
156 */
157 {(int)KS_NAME, "gui"},
158 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
159 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
160# ifdef TERMINFO
161 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
162# else
163 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
164# endif
165 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
166# ifdef TERMINFO
167 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
168 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
169# ifdef FEAT_VERTSPLIT
170 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
171# endif
172# else
173 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
174 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
175# ifdef FEAT_VERTSPLIT
176 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
177# endif
178# endif
179 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
180 /* attributes switched on with 'h', off with * 'H' */
181 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
182 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
183 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
184 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
185 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
186 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
187 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000188 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
189 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
191 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
192 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
193 {(int)KS_MS, "y"},
194 {(int)KS_UT, "y"},
195 {(int)KS_LE, "\b"}, /* cursor-left = BS */
196 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
197# ifdef TERMINFO
198 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
199# else
200 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
201# endif
202 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000203 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#endif
205
206#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207
208# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
209/*
210 * Amiga console window, default for Amiga
211 */
212 {(int)KS_NAME, "amiga"},
213 {(int)KS_CE, "\033[K"},
214 {(int)KS_CD, "\033[J"},
215 {(int)KS_AL, "\033[L"},
216# ifdef TERMINFO
217 {(int)KS_CAL, "\033[%p1%dL"},
218# else
219 {(int)KS_CAL, "\033[%dL"},
220# endif
221 {(int)KS_DL, "\033[M"},
222# ifdef TERMINFO
223 {(int)KS_CDL, "\033[%p1%dM"},
224# else
225 {(int)KS_CDL, "\033[%dM"},
226# endif
227 {(int)KS_CL, "\014"},
228 {(int)KS_VI, "\033[0 p"},
229 {(int)KS_VE, "\033[1 p"},
230 {(int)KS_ME, "\033[0m"},
231 {(int)KS_MR, "\033[7m"},
232 {(int)KS_MD, "\033[1m"},
233 {(int)KS_SE, "\033[0m"},
234 {(int)KS_SO, "\033[33m"},
235 {(int)KS_US, "\033[4m"},
236 {(int)KS_UE, "\033[0m"},
237 {(int)KS_CZH, "\033[3m"},
238 {(int)KS_CZR, "\033[0m"},
239#if defined(__MORPHOS__) || defined(__AROS__)
240 {(int)KS_CCO, "8"}, /* allow 8 colors */
241# ifdef TERMINFO
242 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
243 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
244# else
245 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
246 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
247# endif
248 {(int)KS_OP, "\033[m"}, /* reset colors */
249#endif
250 {(int)KS_MS, "y"},
251 {(int)KS_UT, "y"}, /* guessed */
252 {(int)KS_LE, "\b"},
253# ifdef TERMINFO
254 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
255# else
256 {(int)KS_CM, "\033[%i%d;%dH"},
257# endif
258#if defined(__MORPHOS__)
259 {(int)KS_SR, "\033M"},
260#endif
261# ifdef TERMINFO
262 {(int)KS_CRI, "\033[%p1%dC"},
263# else
264 {(int)KS_CRI, "\033[%dC"},
265# endif
266 {K_UP, "\233A"},
267 {K_DOWN, "\233B"},
268 {K_LEFT, "\233D"},
269 {K_RIGHT, "\233C"},
270 {K_S_UP, "\233T"},
271 {K_S_DOWN, "\233S"},
272 {K_S_LEFT, "\233 A"},
273 {K_S_RIGHT, "\233 @"},
274 {K_S_TAB, "\233Z"},
275 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
276 {K_F2, "\233\061~"},
277 {K_F3, "\233\062~"},
278 {K_F4, "\233\063~"},
279 {K_F5, "\233\064~"},
280 {K_F6, "\233\065~"},
281 {K_F7, "\233\066~"},
282 {K_F8, "\233\067~"},
283 {K_F9, "\233\070~"},
284 {K_F10, "\233\071~"},
285 {K_S_F1, "\233\061\060~"},
286 {K_S_F2, "\233\061\061~"},
287 {K_S_F3, "\233\061\062~"},
288 {K_S_F4, "\233\061\063~"},
289 {K_S_F5, "\233\061\064~"},
290 {K_S_F6, "\233\061\065~"},
291 {K_S_F7, "\233\061\066~"},
292 {K_S_F8, "\233\061\067~"},
293 {K_S_F9, "\233\061\070~"},
294 {K_S_F10, "\233\061\071~"},
295 {K_HELP, "\233?~"},
296 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
297 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
298 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
299 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
300 {K_END, "\233\064\065~"}, /* 101 key keyboard */
301
302 {BT_EXTRA_KEYS, ""},
303 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
304 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
305 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
306# endif
307
308# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
309/*
310 * almost standard ANSI terminal, default for bebox
311 */
312 {(int)KS_NAME, "beos-ansi"},
313 {(int)KS_CE, "\033[K"},
314 {(int)KS_CD, "\033[J"},
315 {(int)KS_AL, "\033[L"},
316# ifdef TERMINFO
317 {(int)KS_CAL, "\033[%p1%dL"},
318# else
319 {(int)KS_CAL, "\033[%dL"},
320# endif
321 {(int)KS_DL, "\033[M"},
322# ifdef TERMINFO
323 {(int)KS_CDL, "\033[%p1%dM"},
324# else
325 {(int)KS_CDL, "\033[%dM"},
326# endif
327#ifdef BEOS_PR_OR_BETTER
328# ifdef TERMINFO
329 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
330# else
331 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
332# endif
333#endif
334 {(int)KS_CL, "\033[H\033[2J"},
335#ifdef notyet
336 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
337 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
338#endif
339 {(int)KS_ME, "\033[m"}, /* normal mode */
340 {(int)KS_MR, "\033[7m"}, /* reverse */
341 {(int)KS_MD, "\033[1m"}, /* bold */
342 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
343 {(int)KS_SE, "\033[m"}, /* standout end */
344 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
345 {(int)KS_CZR, "\033[m"}, /* italic end */
346 {(int)KS_US, "\033[4m"}, /* underscore mode */
347 {(int)KS_UE, "\033[m"}, /* underscore end */
348 {(int)KS_CCO, "8"}, /* allow 8 colors */
349# ifdef TERMINFO
350 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
351 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
352# else
353 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
354 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
355# endif
356 {(int)KS_OP, "\033[m"}, /* reset colors */
357 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
358 {(int)KS_UT, "y"}, /* guessed */
359 {(int)KS_LE, "\b"},
360# ifdef TERMINFO
361 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
362# else
363 {(int)KS_CM, "\033[%i%d;%dH"},
364# endif
365 {(int)KS_SR, "\033M"},
366# ifdef TERMINFO
367 {(int)KS_CRI, "\033[%p1%dC"},
368# else
369 {(int)KS_CRI, "\033[%dC"},
370# endif
371#if defined(BEOS_DR8)
372 {(int)KS_DB, ""}, /* hack! see screen.c */
373#endif
374
375 {K_UP, "\033[A"},
376 {K_DOWN, "\033[B"},
377 {K_LEFT, "\033[D"},
378 {K_RIGHT, "\033[C"},
379# endif
380
381# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) || defined(__EMX__)
382/*
383 * standard ANSI terminal, default for unix
384 */
385 {(int)KS_NAME, "ansi"},
386 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
387 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
388# ifdef TERMINFO
389 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
390# else
391 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
392# endif
393 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
394# ifdef TERMINFO
395 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
396# else
397 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
398# endif
399 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
400 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
401 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
402 {(int)KS_MS, "y"},
403 {(int)KS_UT, "y"}, /* guessed */
404 {(int)KS_LE, "\b"},
405# ifdef TERMINFO
406 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
407# else
408 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
409# endif
410# ifdef TERMINFO
411 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
412# else
413 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
414# endif
415# endif
416
417# if defined(MSDOS) || defined(ALL_BUILTIN_TCAPS) || defined(__EMX__)
418/*
419 * These codes are valid when nansi.sys or equivalent has been installed.
420 * Function keys on a PC are preceded with a NUL. These are converted into
421 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
422 * CTRL-arrow is used instead of SHIFT-arrow.
423 */
424#ifdef __EMX__
425 {(int)KS_NAME, "os2ansi"},
426#else
427 {(int)KS_NAME, "pcansi"},
428 {(int)KS_DL, "\033[M"},
429 {(int)KS_AL, "\033[L"},
430#endif
431 {(int)KS_CE, "\033[K"},
432 {(int)KS_CL, "\033[2J"},
433 {(int)KS_ME, "\033[0m"},
434 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
435 {(int)KS_MD, "\033[1m"}, /* bold: white text */
436 {(int)KS_SE, "\033[0m"}, /* standout end */
437 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
438 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
439 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
440 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
441 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
442 {(int)KS_CCO, "8"}, /* allow 8 colors */
443# ifdef TERMINFO
444 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
445 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
446# else
447 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
448 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
449# endif
450 {(int)KS_OP, "\033[0m"}, /* reset colors */
451 {(int)KS_MS, "y"},
452 {(int)KS_UT, "y"}, /* guessed */
453 {(int)KS_LE, "\b"},
454# ifdef TERMINFO
455 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
456# else
457 {(int)KS_CM, "\033[%i%d;%dH"},
458# endif
459# ifdef TERMINFO
460 {(int)KS_CRI, "\033[%p1%dC"},
461# else
462 {(int)KS_CRI, "\033[%dC"},
463# endif
464 {K_UP, "\316H"},
465 {K_DOWN, "\316P"},
466 {K_LEFT, "\316K"},
467 {K_RIGHT, "\316M"},
468 {K_S_LEFT, "\316s"},
469 {K_S_RIGHT, "\316t"},
470 {K_F1, "\316;"},
471 {K_F2, "\316<"},
472 {K_F3, "\316="},
473 {K_F4, "\316>"},
474 {K_F5, "\316?"},
475 {K_F6, "\316@"},
476 {K_F7, "\316A"},
477 {K_F8, "\316B"},
478 {K_F9, "\316C"},
479 {K_F10, "\316D"},
480 {K_F11, "\316\205"}, /* guessed */
481 {K_F12, "\316\206"}, /* guessed */
482 {K_S_F1, "\316T"},
483 {K_S_F2, "\316U"},
484 {K_S_F3, "\316V"},
485 {K_S_F4, "\316W"},
486 {K_S_F5, "\316X"},
487 {K_S_F6, "\316Y"},
488 {K_S_F7, "\316Z"},
489 {K_S_F8, "\316["},
490 {K_S_F9, "\316\\"},
491 {K_S_F10, "\316]"},
492 {K_S_F11, "\316\207"}, /* guessed */
493 {K_S_F12, "\316\210"}, /* guessed */
494 {K_INS, "\316R"},
495 {K_DEL, "\316S"},
496 {K_HOME, "\316G"},
497 {K_END, "\316O"},
498 {K_PAGEDOWN, "\316Q"},
499 {K_PAGEUP, "\316I"},
500# endif
501
502# if defined(MSDOS)
503/*
504 * These codes are valid for the pc video. The entries that start with ESC |
505 * are translated into conio calls in os_msdos.c. Default for MSDOS.
506 */
507 {(int)KS_NAME, "pcterm"},
508 {(int)KS_CE, "\033|K"},
509 {(int)KS_AL, "\033|L"},
510 {(int)KS_DL, "\033|M"},
511# ifdef TERMINFO
512 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},
513# ifdef FEAT_VERTSPLIT
514 {(int)KS_CSV, "\033|%i%p1%d;%p2%dV"},
515# endif
516# else
517 {(int)KS_CS, "\033|%i%d;%dr"},
518# ifdef FEAT_VERTSPLIT
519 {(int)KS_CSV, "\033|%i%d;%dV"},
520# endif
521# endif
522 {(int)KS_CL, "\033|J"},
523 {(int)KS_ME, "\033|0m"}, /* normal */
524 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgrey */
525 {(int)KS_MD, "\033|15m"}, /* bold: white text */
526 {(int)KS_SE, "\033|0m"}, /* standout end */
527 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
528 {(int)KS_CZH, "\033|225m"}, /* italic mode: blue text on yellow */
529 {(int)KS_CZR, "\033|0m"}, /* italic mode end */
530 {(int)KS_US, "\033|67m"}, /* underscore mode: cyan text on red */
531 {(int)KS_UE, "\033|0m"}, /* underscore mode end */
532 {(int)KS_CCO, "16"}, /* allow 16 colors */
533# ifdef TERMINFO
534 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
535 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
536# else
537 {(int)KS_CAB, "\033|%db"}, /* set background color */
538 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
539# endif
540 {(int)KS_MS, "y"},
541 {(int)KS_UT, "y"},
542 {(int)KS_LE, "\b"},
543# ifdef TERMINFO
544 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},
545# else
546 {(int)KS_CM, "\033|%i%d;%dH"},
547# endif
548#ifdef DJGPP
549 {(int)KS_VB, "\033|B"}, /* visual bell */
550#endif
551 {K_UP, "\316H"},
552 {K_DOWN, "\316P"},
553 {K_LEFT, "\316K"},
554 {K_RIGHT, "\316M"},
555 {K_S_LEFT, "\316s"},
556 {K_S_RIGHT, "\316t"},
557 {K_S_TAB, "\316\017"},
558 {K_F1, "\316;"},
559 {K_F2, "\316<"},
560 {K_F3, "\316="},
561 {K_F4, "\316>"},
562 {K_F5, "\316?"},
563 {K_F6, "\316@"},
564 {K_F7, "\316A"},
565 {K_F8, "\316B"},
566 {K_F9, "\316C"},
567 {K_F10, "\316D"},
568 {K_F11, "\316\205"},
569 {K_F12, "\316\206"},
570 {K_S_F1, "\316T"},
571 {K_S_F2, "\316U"},
572 {K_S_F3, "\316V"},
573 {K_S_F4, "\316W"},
574 {K_S_F5, "\316X"},
575 {K_S_F6, "\316Y"},
576 {K_S_F7, "\316Z"},
577 {K_S_F8, "\316["},
578 {K_S_F9, "\316\\"},
579 {K_S_F10, "\316]"},
580 {K_S_F11, "\316\207"},
581 {K_S_F12, "\316\210"},
582 {K_INS, "\316R"},
583 {K_DEL, "\316S"},
584 {K_HOME, "\316G"},
585 {K_END, "\316O"},
586 {K_PAGEDOWN, "\316Q"},
587 {K_PAGEUP, "\316I"},
588 {K_KPLUS, "\316N"},
589 {K_KMINUS, "\316J"},
590 {K_KMULTIPLY, "\3167"},
591 {K_K0, "\316\332"},
592 {K_K1, "\316\336"},
593 {K_K2, "\316\342"},
594 {K_K3, "\316\346"},
595 {K_K4, "\316\352"},
596 {K_K5, "\316\356"},
597 {K_K6, "\316\362"},
598 {K_K7, "\316\366"},
599 {K_K8, "\316\372"},
600 {K_K9, "\316\376"},
601# endif
602
603# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS) || defined(__EMX__)
604/*
605 * These codes are valid for the Win32 Console . The entries that start with
606 * ESC | are translated into console calls in os_win32.c. The function keys
607 * are also translated in os_win32.c.
608 */
609 {(int)KS_NAME, "win32"},
610 {(int)KS_CE, "\033|K"}, /* clear to end of line */
611 {(int)KS_AL, "\033|L"}, /* add new blank line */
612# ifdef TERMINFO
613 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
614# else
615 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
616# endif
617 {(int)KS_DL, "\033|M"}, /* delete line */
618# ifdef TERMINFO
619 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
620# else
621 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
622# endif
623 {(int)KS_CL, "\033|J"}, /* clear screen */
624 {(int)KS_CD, "\033|j"}, /* clear to end of display */
625 {(int)KS_VI, "\033|v"}, /* cursor invisible */
626 {(int)KS_VE, "\033|V"}, /* cursor visible */
627
628 {(int)KS_ME, "\033|0m"}, /* normal */
629 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
630 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
631#if 1
632 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
633 {(int)KS_SE, "\033|0m"}, /* standout end */
634#else
635 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
636 {(int)KS_SE, "\033|f"}, /* standout end */
637#endif
638 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
639 {(int)KS_CZR, "\033|0m"}, /* italic end */
640 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
641 {(int)KS_UE, "\033|0m"}, /* underscore end */
642 {(int)KS_CCO, "16"}, /* allow 16 colors */
643# ifdef TERMINFO
644 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
645 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
646# else
647 {(int)KS_CAB, "\033|%db"}, /* set background color */
648 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
649# endif
650
651 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
652 {(int)KS_UT, "y"},
653 {(int)KS_LE, "\b"},
654# ifdef TERMINFO
655 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
656# else
657 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
658# endif
659 {(int)KS_VB, "\033|B"}, /* visual bell */
660 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
661 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
662# ifdef TERMINFO
663 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
664# else
665 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
666# endif
667
668 {K_UP, "\316H"},
669 {K_DOWN, "\316P"},
670 {K_LEFT, "\316K"},
671 {K_RIGHT, "\316M"},
672 {K_S_UP, "\316\304"},
673 {K_S_DOWN, "\316\317"},
674 {K_S_LEFT, "\316\311"},
675 {K_C_LEFT, "\316s"},
676 {K_S_RIGHT, "\316\313"},
677 {K_C_RIGHT, "\316t"},
678 {K_S_TAB, "\316\017"},
679 {K_F1, "\316;"},
680 {K_F2, "\316<"},
681 {K_F3, "\316="},
682 {K_F4, "\316>"},
683 {K_F5, "\316?"},
684 {K_F6, "\316@"},
685 {K_F7, "\316A"},
686 {K_F8, "\316B"},
687 {K_F9, "\316C"},
688 {K_F10, "\316D"},
689 {K_F11, "\316\205"},
690 {K_F12, "\316\206"},
691 {K_S_F1, "\316T"},
692 {K_S_F2, "\316U"},
693 {K_S_F3, "\316V"},
694 {K_S_F4, "\316W"},
695 {K_S_F5, "\316X"},
696 {K_S_F6, "\316Y"},
697 {K_S_F7, "\316Z"},
698 {K_S_F8, "\316["},
699 {K_S_F9, "\316\\"},
700 {K_S_F10, "\316]"},
701 {K_S_F11, "\316\207"},
702 {K_S_F12, "\316\210"},
703 {K_INS, "\316R"},
704 {K_DEL, "\316S"},
705 {K_HOME, "\316G"},
706 {K_S_HOME, "\316\302"},
707 {K_C_HOME, "\316w"},
708 {K_END, "\316O"},
709 {K_S_END, "\316\315"},
710 {K_C_END, "\316u"},
711 {K_PAGEDOWN, "\316Q"},
712 {K_PAGEUP, "\316I"},
713 {K_KPLUS, "\316N"},
714 {K_KMINUS, "\316J"},
715 {K_KMULTIPLY, "\316\067"},
716 {K_K0, "\316\332"},
717 {K_K1, "\316\336"},
718 {K_K2, "\316\342"},
719 {K_K3, "\316\346"},
720 {K_K4, "\316\352"},
721 {K_K5, "\316\356"},
722 {K_K6, "\316\362"},
723 {K_K7, "\316\366"},
724 {K_K8, "\316\372"},
725 {K_K9, "\316\376"},
726# endif
727
728# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
729/*
730 * VT320 is working as an ANSI terminal compatible DEC terminal.
731 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
732 * Note: K_F1...K_F5 are for internal use, should not be defined.
733 * TODO:- rewrite ESC[ codes to CSI
734 * - keyboard languages (CSI ? 26 n)
735 */
736 {(int)KS_NAME, "vt320"},
737 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
738 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
739# ifdef TERMINFO
740 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
741# else
742 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
743# endif
744 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
745# ifdef TERMINFO
746 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
747# else
748 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
749# endif
750 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000751 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
752 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
754 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000755 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
756 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
757 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
758 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
759 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
760 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
761 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
762 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
763 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
764 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 {(int)KS_MS, "y"},
766 {(int)KS_UT, "y"},
767 {(int)KS_LE, "\b"},
768# ifdef TERMINFO
769 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
770 ESC_STR "[%i%p1%d;%p2%dH")},
771# else
772 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
773# endif
774# ifdef TERMINFO
775 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
776# else
777 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
778# endif
779 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
780 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
781 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
782 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000783 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
784 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
785 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
786 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
787 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
789 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
790 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
791 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
792 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000793 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
795 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
796 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
797 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
798 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
799 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
800 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
801 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
802 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
803 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
804 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
805 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
806 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
807 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
808 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
809 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
810 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
811 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
812 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
813 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
814 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
815# endif
816
817# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
818/*
819 * Ordinary vt52
820 */
821 {(int)KS_NAME, "vt52"},
822 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
823 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
824 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
825 {(int)KS_LE, "\b"},
826# ifdef __MINT__
827 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
828 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
829 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
830 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
831 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
832 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
833 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
834 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
835 {K_UP, IF_EB("\033A", ESC_STR "A")},
836 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
837 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
838 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
839 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
840 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
841 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
842 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
843 {K_F1, IF_EB("\033P", ESC_STR "P")},
844 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
845 {K_F3, IF_EB("\033R", ESC_STR "R")},
846 {K_F4, IF_EB("\033S", ESC_STR "S")},
847 {K_F5, IF_EB("\033T", ESC_STR "T")},
848 {K_F6, IF_EB("\033U", ESC_STR "U")},
849 {K_F7, IF_EB("\033V", ESC_STR "V")},
850 {K_F8, IF_EB("\033W", ESC_STR "W")},
851 {K_F9, IF_EB("\033X", ESC_STR "X")},
852 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
853 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
854 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
855 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
856 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
857 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
858 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
859 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
860 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
861 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
862 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
863 {K_INS, IF_EB("\033I", ESC_STR "I")},
864 {K_HOME, IF_EB("\033E", ESC_STR "E")},
865 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
866 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
867# else
868 {(int)KS_AL, IF_EB("\033T", ESC_STR "T")},
869 {(int)KS_DL, IF_EB("\033U", ESC_STR "U")},
870 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
871 {(int)KS_ME, IF_EB("\033SO", ESC_STR "SO")},
872 {(int)KS_MR, IF_EB("\033S2", ESC_STR "S2")},
873 {(int)KS_MS, "y"},
874# endif
875# endif
876
877# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) || defined(__EMX__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 {(int)KS_NAME, "xterm"},
879 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
880 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
881# ifdef TERMINFO
882 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
883# else
884 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
885# endif
886 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
887# ifdef TERMINFO
888 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
889# else
890 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
891# endif
892# ifdef TERMINFO
893 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
894 ESC_STR "[%i%p1%d;%p2%dr")},
895# else
896 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
897# endif
898 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
899 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
900 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
901 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
902 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
903 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
904 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
905 {(int)KS_MS, "y"},
906 {(int)KS_UT, "y"},
907 {(int)KS_LE, "\b"},
908# ifdef TERMINFO
909 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
910 ESC_STR "[%i%p1%d;%p2%dH")},
911# else
912 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
913# endif
914 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
915# ifdef TERMINFO
916 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
917# else
918 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
919# endif
920 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
921 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
922# ifdef FEAT_XTERM_SAVE
923 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
924 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
925 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
926# endif
927 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
928 {(int)KS_CIE, "\007"},
929 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
930 {(int)KS_FS, "\007"},
931# ifdef TERMINFO
932 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
933 ESC_STR "[8;%p1%d;%p2%dt")},
934 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
935 ESC_STR "[3;%p1%d;%p2%dt")},
936# else
937 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
938 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
939# endif
940 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100941 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000942
943 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
944 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
945 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
946 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
947 /* An extra set of cursor keys for vt100 mode */
948 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
949 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
950 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
951 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000953 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
954 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
955 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
956 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000957 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
958 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
959 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
960 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
961 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
962 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
963 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
964 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
965 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
966 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
967 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
968 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000970 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
971 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
972 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
973 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000974 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
975 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000976 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000977 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000978 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000979 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000980 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
981 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000982 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000983 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000984 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000985 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
986 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000987 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
988 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
989 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
990 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
991 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
992 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000993 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
995 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000996 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
997 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
998 /* F14 and F15 are missing, because they send the same codes as the undo
999 * and help key, although they don't work on all keyboards. */
1000 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
1001 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
1002 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1003 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1004 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1005
1006 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1007 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1008 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1009 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1010 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1011 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1012 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1013 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1014 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1015 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1016
1017 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1018 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1019 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1020 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1021 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1022 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1023 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024# endif
1025
1026# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1027/*
1028 * iris-ansi for Silicon Graphics machines.
1029 */
1030 {(int)KS_NAME, "iris-ansi"},
1031 {(int)KS_CE, "\033[K"},
1032 {(int)KS_CD, "\033[J"},
1033 {(int)KS_AL, "\033[L"},
1034# ifdef TERMINFO
1035 {(int)KS_CAL, "\033[%p1%dL"},
1036# else
1037 {(int)KS_CAL, "\033[%dL"},
1038# endif
1039 {(int)KS_DL, "\033[M"},
1040# ifdef TERMINFO
1041 {(int)KS_CDL, "\033[%p1%dM"},
1042# else
1043 {(int)KS_CDL, "\033[%dM"},
1044# endif
1045#if 0 /* The scroll region is not working as Vim expects. */
1046# ifdef TERMINFO
1047 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1048# else
1049 {(int)KS_CS, "\033[%i%d;%dr"},
1050# endif
1051#endif
1052 {(int)KS_CL, "\033[H\033[2J"},
1053 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1054 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1055 {(int)KS_TI, "\033[=6h"},
1056 {(int)KS_TE, "\033[=6l"},
1057 {(int)KS_SE, "\033[21;27m"},
1058 {(int)KS_SO, "\033[1;7m"},
1059 {(int)KS_ME, "\033[m"},
1060 {(int)KS_MR, "\033[7m"},
1061 {(int)KS_MD, "\033[1m"},
1062 {(int)KS_CCO, "8"}, /* allow 8 colors */
1063 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1064 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1065 {(int)KS_US, "\033[4m"}, /* underline on */
1066 {(int)KS_UE, "\033[24m"}, /* underline off */
1067# ifdef TERMINFO
1068 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1069 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1070 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1071 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1072# else
1073 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1074 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1075 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1076 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1077# endif
1078 {(int)KS_MS, "y"}, /* guessed */
1079 {(int)KS_UT, "y"}, /* guessed */
1080 {(int)KS_LE, "\b"},
1081# ifdef TERMINFO
1082 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1083# else
1084 {(int)KS_CM, "\033[%i%d;%dH"},
1085# endif
1086 {(int)KS_SR, "\033M"},
1087# ifdef TERMINFO
1088 {(int)KS_CRI, "\033[%p1%dC"},
1089# else
1090 {(int)KS_CRI, "\033[%dC"},
1091# endif
1092 {(int)KS_CIS, "\033P3.y"},
1093 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1094 {(int)KS_TS, "\033P1.y"},
1095 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1096# ifdef TERMINFO
1097 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1098 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1099# else
1100 {(int)KS_CWS, "\033[203;%d;%d/y"},
1101 {(int)KS_CWP, "\033[205;%d;%d/y"},
1102# endif
1103 {K_UP, "\033[A"},
1104 {K_DOWN, "\033[B"},
1105 {K_LEFT, "\033[D"},
1106 {K_RIGHT, "\033[C"},
1107 {K_S_UP, "\033[161q"},
1108 {K_S_DOWN, "\033[164q"},
1109 {K_S_LEFT, "\033[158q"},
1110 {K_S_RIGHT, "\033[167q"},
1111 {K_F1, "\033[001q"},
1112 {K_F2, "\033[002q"},
1113 {K_F3, "\033[003q"},
1114 {K_F4, "\033[004q"},
1115 {K_F5, "\033[005q"},
1116 {K_F6, "\033[006q"},
1117 {K_F7, "\033[007q"},
1118 {K_F8, "\033[008q"},
1119 {K_F9, "\033[009q"},
1120 {K_F10, "\033[010q"},
1121 {K_F11, "\033[011q"},
1122 {K_F12, "\033[012q"},
1123 {K_S_F1, "\033[013q"},
1124 {K_S_F2, "\033[014q"},
1125 {K_S_F3, "\033[015q"},
1126 {K_S_F4, "\033[016q"},
1127 {K_S_F5, "\033[017q"},
1128 {K_S_F6, "\033[018q"},
1129 {K_S_F7, "\033[019q"},
1130 {K_S_F8, "\033[020q"},
1131 {K_S_F9, "\033[021q"},
1132 {K_S_F10, "\033[022q"},
1133 {K_S_F11, "\033[023q"},
1134 {K_S_F12, "\033[024q"},
1135 {K_INS, "\033[139q"},
1136 {K_HOME, "\033[H"},
1137 {K_END, "\033[146q"},
1138 {K_PAGEUP, "\033[150q"},
1139 {K_PAGEDOWN, "\033[154q"},
1140# endif
1141
1142# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1143/*
1144 * for debugging
1145 */
1146 {(int)KS_NAME, "debug"},
1147 {(int)KS_CE, "[CE]"},
1148 {(int)KS_CD, "[CD]"},
1149 {(int)KS_AL, "[AL]"},
1150# ifdef TERMINFO
1151 {(int)KS_CAL, "[CAL%p1%d]"},
1152# else
1153 {(int)KS_CAL, "[CAL%d]"},
1154# endif
1155 {(int)KS_DL, "[DL]"},
1156# ifdef TERMINFO
1157 {(int)KS_CDL, "[CDL%p1%d]"},
1158# else
1159 {(int)KS_CDL, "[CDL%d]"},
1160# endif
1161# ifdef TERMINFO
1162 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1163# else
1164 {(int)KS_CS, "[%dCS%d]"},
1165# endif
1166# ifdef FEAT_VERTSPLIT
1167# ifdef TERMINFO
1168 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1169# else
1170 {(int)KS_CSV, "[%dCSV%d]"},
1171# endif
1172# endif
1173# ifdef TERMINFO
1174 {(int)KS_CAB, "[CAB%p1%d]"},
1175 {(int)KS_CAF, "[CAF%p1%d]"},
1176 {(int)KS_CSB, "[CSB%p1%d]"},
1177 {(int)KS_CSF, "[CSF%p1%d]"},
1178# else
1179 {(int)KS_CAB, "[CAB%d]"},
1180 {(int)KS_CAF, "[CAF%d]"},
1181 {(int)KS_CSB, "[CSB%d]"},
1182 {(int)KS_CSF, "[CSF%d]"},
1183# endif
1184 {(int)KS_OP, "[OP]"},
1185 {(int)KS_LE, "[LE]"},
1186 {(int)KS_CL, "[CL]"},
1187 {(int)KS_VI, "[VI]"},
1188 {(int)KS_VE, "[VE]"},
1189 {(int)KS_VS, "[VS]"},
1190 {(int)KS_ME, "[ME]"},
1191 {(int)KS_MR, "[MR]"},
1192 {(int)KS_MB, "[MB]"},
1193 {(int)KS_MD, "[MD]"},
1194 {(int)KS_SE, "[SE]"},
1195 {(int)KS_SO, "[SO]"},
1196 {(int)KS_UE, "[UE]"},
1197 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001198 {(int)KS_UCE, "[UCE]"},
1199 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 {(int)KS_MS, "[MS]"},
1201 {(int)KS_UT, "[UT]"},
1202# ifdef TERMINFO
1203 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1204# else
1205 {(int)KS_CM, "[%dCM%d]"},
1206# endif
1207 {(int)KS_SR, "[SR]"},
1208# ifdef TERMINFO
1209 {(int)KS_CRI, "[CRI%p1%d]"},
1210# else
1211 {(int)KS_CRI, "[CRI%d]"},
1212# endif
1213 {(int)KS_VB, "[VB]"},
1214 {(int)KS_KS, "[KS]"},
1215 {(int)KS_KE, "[KE]"},
1216 {(int)KS_TI, "[TI]"},
1217 {(int)KS_TE, "[TE]"},
1218 {(int)KS_CIS, "[CIS]"},
1219 {(int)KS_CIE, "[CIE]"},
1220 {(int)KS_TS, "[TS]"},
1221 {(int)KS_FS, "[FS]"},
1222# ifdef TERMINFO
1223 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1224 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1225# else
1226 {(int)KS_CWS, "[%dCWS%d]"},
1227 {(int)KS_CWP, "[%dCWP%d]"},
1228# endif
1229 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001230 {(int)KS_U7, "[U7]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {K_UP, "[KU]"},
1232 {K_DOWN, "[KD]"},
1233 {K_LEFT, "[KL]"},
1234 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001235 {K_XUP, "[xKU]"},
1236 {K_XDOWN, "[xKD]"},
1237 {K_XLEFT, "[xKL]"},
1238 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {K_S_UP, "[S-KU]"},
1240 {K_S_DOWN, "[S-KD]"},
1241 {K_S_LEFT, "[S-KL]"},
1242 {K_C_LEFT, "[C-KL]"},
1243 {K_S_RIGHT, "[S-KR]"},
1244 {K_C_RIGHT, "[C-KR]"},
1245 {K_F1, "[F1]"},
1246 {K_XF1, "[xF1]"},
1247 {K_F2, "[F2]"},
1248 {K_XF2, "[xF2]"},
1249 {K_F3, "[F3]"},
1250 {K_XF3, "[xF3]"},
1251 {K_F4, "[F4]"},
1252 {K_XF4, "[xF4]"},
1253 {K_F5, "[F5]"},
1254 {K_F6, "[F6]"},
1255 {K_F7, "[F7]"},
1256 {K_F8, "[F8]"},
1257 {K_F9, "[F9]"},
1258 {K_F10, "[F10]"},
1259 {K_F11, "[F11]"},
1260 {K_F12, "[F12]"},
1261 {K_S_F1, "[S-F1]"},
1262 {K_S_XF1, "[S-xF1]"},
1263 {K_S_F2, "[S-F2]"},
1264 {K_S_XF2, "[S-xF2]"},
1265 {K_S_F3, "[S-F3]"},
1266 {K_S_XF3, "[S-xF3]"},
1267 {K_S_F4, "[S-F4]"},
1268 {K_S_XF4, "[S-xF4]"},
1269 {K_S_F5, "[S-F5]"},
1270 {K_S_F6, "[S-F6]"},
1271 {K_S_F7, "[S-F7]"},
1272 {K_S_F8, "[S-F8]"},
1273 {K_S_F9, "[S-F9]"},
1274 {K_S_F10, "[S-F10]"},
1275 {K_S_F11, "[S-F11]"},
1276 {K_S_F12, "[S-F12]"},
1277 {K_HELP, "[HELP]"},
1278 {K_UNDO, "[UNDO]"},
1279 {K_BS, "[BS]"},
1280 {K_INS, "[INS]"},
1281 {K_KINS, "[KINS]"},
1282 {K_DEL, "[DEL]"},
1283 {K_KDEL, "[KDEL]"},
1284 {K_HOME, "[HOME]"},
1285 {K_S_HOME, "[C-HOME]"},
1286 {K_C_HOME, "[C-HOME]"},
1287 {K_KHOME, "[KHOME]"},
1288 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001289 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {K_END, "[END]"},
1291 {K_S_END, "[C-END]"},
1292 {K_C_END, "[C-END]"},
1293 {K_KEND, "[KEND]"},
1294 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001295 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {K_PAGEUP, "[PAGEUP]"},
1297 {K_PAGEDOWN, "[PAGEDOWN]"},
1298 {K_KPAGEUP, "[KPAGEUP]"},
1299 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1300 {K_MOUSE, "[MOUSE]"},
1301 {K_KPLUS, "[KPLUS]"},
1302 {K_KMINUS, "[KMINUS]"},
1303 {K_KDIVIDE, "[KDIVIDE]"},
1304 {K_KMULTIPLY, "[KMULTIPLY]"},
1305 {K_KENTER, "[KENTER]"},
1306 {K_KPOINT, "[KPOINT]"},
1307 {K_K0, "[K0]"},
1308 {K_K1, "[K1]"},
1309 {K_K2, "[K2]"},
1310 {K_K3, "[K3]"},
1311 {K_K4, "[K4]"},
1312 {K_K5, "[K5]"},
1313 {K_K6, "[K6]"},
1314 {K_K7, "[K7]"},
1315 {K_K8, "[K8]"},
1316 {K_K9, "[K9]"},
1317# endif
1318
1319#endif /* NO_BUILTIN_TCAPS */
1320
1321/*
1322 * The most minimal terminal: only clear screen and cursor positioning
1323 * Always included.
1324 */
1325 {(int)KS_NAME, "dumb"},
1326 {(int)KS_CL, "\014"},
1327#ifdef TERMINFO
1328 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1329 ESC_STR "[%i%p1%d;%p2%dH")},
1330#else
1331 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1332#endif
1333
1334/*
1335 * end marker
1336 */
1337 {(int)KS_NAME, NULL}
1338
1339}; /* end of builtin_termcaps */
1340
1341/*
1342 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344#ifdef AMIGA
1345# define DEFAULT_TERM (char_u *)"amiga"
1346#endif
1347
1348#ifdef MSWIN
1349# define DEFAULT_TERM (char_u *)"win32"
1350#endif
1351
1352#ifdef MSDOS
1353# define DEFAULT_TERM (char_u *)"pcterm"
1354#endif
1355
1356#if defined(UNIX) && !defined(__MINT__)
1357# define DEFAULT_TERM (char_u *)"ansi"
1358#endif
1359
1360#ifdef __MINT__
1361# define DEFAULT_TERM (char_u *)"vt52"
1362#endif
1363
1364#ifdef __EMX__
1365# define DEFAULT_TERM (char_u *)"os2ansi"
1366#endif
1367
1368#ifdef VMS
1369# define DEFAULT_TERM (char_u *)"vt320"
1370#endif
1371
1372#ifdef __BEOS__
1373# undef DEFAULT_TERM
1374# define DEFAULT_TERM (char_u *)"beos-ansi"
1375#endif
1376
1377#ifndef DEFAULT_TERM
1378# define DEFAULT_TERM (char_u *)"dumb"
1379#endif
1380
1381/*
1382 * Term_strings contains currently used terminal output strings.
1383 * It is initialized with the default values by parse_builtin_tcap().
1384 * The values can be changed by setting the option with the same name.
1385 */
1386char_u *(term_strings[(int)KS_LAST + 1]);
1387
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001388static int need_gather = FALSE; /* need to fill termleader[] */
1389static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001391static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#endif
1393
1394 static struct builtin_term *
1395find_builtin_term(term)
1396 char_u *term;
1397{
1398 struct builtin_term *p;
1399
1400 p = builtin_termcaps;
1401 while (p->bt_string != NULL)
1402 {
1403 if (p->bt_entry == (int)KS_NAME)
1404 {
1405#ifdef UNIX
1406 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1407 return p;
1408 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1409 return p;
1410 else
1411#endif
1412#ifdef VMS
1413 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1414 return p;
1415 else
1416#endif
1417 if (STRCMP(term, p->bt_string) == 0)
1418 return p;
1419 }
1420 ++p;
1421 }
1422 return p;
1423}
1424
1425/*
1426 * Parsing of the builtin termcap entries.
1427 * Caller should check if 'name' is a valid builtin term.
1428 * The terminal's name is not set, as this is already done in termcapinit().
1429 */
1430 static void
1431parse_builtin_tcap(term)
1432 char_u *term;
1433{
1434 struct builtin_term *p;
1435 char_u name[2];
1436 int term_8bit;
1437
1438 p = find_builtin_term(term);
1439 term_8bit = term_is_8bit(term);
1440
1441 /* Do not parse if builtin term not found */
1442 if (p->bt_string == NULL)
1443 return;
1444
1445 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1446 {
1447 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1448 {
1449 /* Only set the value if it wasn't set yet. */
1450 if (term_strings[p->bt_entry] == NULL
1451 || term_strings[p->bt_entry] == empty_option)
1452 {
1453 /* 8bit terminal: use CSI instead of <Esc>[ */
1454 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1455 {
1456 char_u *s, *t;
1457
1458 s = vim_strsave((char_u *)p->bt_string);
1459 if (s != NULL)
1460 {
1461 for (t = s; *t; ++t)
1462 if (term_7to8bit(t))
1463 {
1464 *t = term_7to8bit(t);
1465 STRCPY(t + 1, t + 2);
1466 }
1467 term_strings[p->bt_entry] = s;
1468 set_term_option_alloced(&term_strings[p->bt_entry]);
1469 }
1470 }
1471 else
1472 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1473 }
1474 }
1475 else
1476 {
1477 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1478 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1479 if (find_termcode(name) == NULL)
1480 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1481 }
1482 }
1483}
1484#if defined(HAVE_TGETENT) || defined(FEAT_TERMRESPONSE)
1485static void set_color_count __ARGS((int nr));
1486
1487/*
1488 * Set number of colors.
1489 * Store it as a number in t_colors.
1490 * Store it as a string in T_CCO (using nr_colors[]).
1491 */
1492 static void
1493set_color_count(nr)
1494 int nr;
1495{
1496 char_u nr_colors[20]; /* string for number of colors */
1497
1498 t_colors = nr;
1499 if (t_colors > 1)
1500 sprintf((char *)nr_colors, "%d", t_colors);
1501 else
1502 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001503 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504}
1505#endif
1506
1507#ifdef HAVE_TGETENT
1508static char *(key_names[]) =
1509{
1510#ifdef FEAT_TERMRESPONSE
1511 /* Do this one first, it may cause a screen redraw. */
1512 "Co",
1513#endif
1514 "ku", "kd", "kr", "kl",
1515# ifdef ARCHIE
1516 "su", "sd", /* Termcap code made up! */
1517# endif
1518 "#2", "#4", "%i", "*7",
1519 "k1", "k2", "k3", "k4", "k5", "k6",
1520 "k7", "k8", "k9", "k;", "F1", "F2",
1521 "%1", "&8", "kb", "kI", "kD", "kh",
1522 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1523 NULL
1524};
1525#endif
1526
1527/*
1528 * Set terminal options for terminal "term".
1529 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1530 *
1531 * While doing this, until ttest(), some options may be NULL, be careful.
1532 */
1533 int
1534set_termname(term)
1535 char_u *term;
1536{
1537 struct builtin_term *termp;
1538#ifdef HAVE_TGETENT
1539 int builtin_first = p_tbi;
1540 int try;
1541 int termcap_cleared = FALSE;
1542#endif
1543 int width = 0, height = 0;
1544 char_u *error_msg = NULL;
1545 char_u *bs_p, *del_p;
1546
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001547 /* In silect mode (ex -s) we don't use the 'term' option. */
1548 if (silent_mode)
1549 return OK;
1550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 detected_8bit = FALSE; /* reset 8-bit detection */
1552
1553 if (term_is_builtin(term))
1554 {
1555 term += 8;
1556#ifdef HAVE_TGETENT
1557 builtin_first = 1;
1558#endif
1559 }
1560
1561/*
1562 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1563 * If builtin_first is TRUE:
1564 * 0. try builtin termcap
1565 * 1. try external termcap
1566 * 2. if both fail default to a builtin terminal
1567 * If builtin_first is FALSE:
1568 * 1. try external termcap
1569 * 2. try builtin termcap, if both fail default to a builtin terminal
1570 */
1571#ifdef HAVE_TGETENT
1572 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1573 {
1574 /*
1575 * Use external termcap
1576 */
1577 if (try == 1)
1578 {
1579 char_u *p;
1580 static char_u tstrbuf[TBUFSZ];
1581 int i;
1582 char_u tbuf[TBUFSZ];
1583 char_u *tp;
1584 static struct {
1585 enum SpecialKey dest; /* index in term_strings[] */
1586 char *name; /* termcap name for string */
1587 } string_names[] =
1588 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1589 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1590 {KS_CL, "cl"}, {KS_CD, "cd"},
1591 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1592 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1593 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1594 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001595 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1596 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1598 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1599 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1600 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1601 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1602 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1603 {KS_TS, "ts"}, {KS_FS, "fs"},
1604 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001605 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001606 {KS_U7, "u7"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {(enum SpecialKey)0, NULL}
1608 };
1609
1610 /*
1611 * If the external termcap does not have a matching entry, try the
1612 * builtin ones.
1613 */
1614 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1615 {
1616 tp = tstrbuf;
1617 if (!termcap_cleared)
1618 {
1619 clear_termoptions(); /* clear old options */
1620 termcap_cleared = TRUE;
1621 }
1622
1623 /* get output strings */
1624 for (i = 0; string_names[i].name != NULL; ++i)
1625 {
1626 if (term_str(string_names[i].dest) == NULL
1627 || term_str(string_names[i].dest) == empty_option)
1628 term_str(string_names[i].dest) =
1629 TGETSTR(string_names[i].name, &tp);
1630 }
1631
1632 /* tgetflag() returns 1 if the flag is present, 0 if not and
1633 * possibly -1 if the flag doesn't exist. */
1634 if ((T_MS == NULL || T_MS == empty_option)
1635 && tgetflag("ms") > 0)
1636 T_MS = (char_u *)"y";
1637 if ((T_XS == NULL || T_XS == empty_option)
1638 && tgetflag("xs") > 0)
1639 T_XS = (char_u *)"y";
1640 if ((T_DB == NULL || T_DB == empty_option)
1641 && tgetflag("db") > 0)
1642 T_DB = (char_u *)"y";
1643 if ((T_DA == NULL || T_DA == empty_option)
1644 && tgetflag("da") > 0)
1645 T_DA = (char_u *)"y";
1646 if ((T_UT == NULL || T_UT == empty_option)
1647 && tgetflag("ut") > 0)
1648 T_UT = (char_u *)"y";
1649
1650
1651 /*
1652 * get key codes
1653 */
1654 for (i = 0; key_names[i] != NULL; ++i)
1655 {
1656 if (find_termcode((char_u *)key_names[i]) == NULL)
1657 {
1658 p = TGETSTR(key_names[i], &tp);
1659 /* if cursor-left == backspace, ignore it (televideo
1660 * 925) */
1661 if (p != NULL
1662 && (*p != Ctrl_H
1663 || key_names[i][0] != 'k'
1664 || key_names[i][1] != 'l'))
1665 add_termcode((char_u *)key_names[i], p, FALSE);
1666 }
1667 }
1668
1669 if (height == 0)
1670 height = tgetnum("li");
1671 if (width == 0)
1672 width = tgetnum("co");
1673
1674 /*
1675 * Get number of colors (if not done already).
1676 */
1677 if (term_str(KS_CCO) == NULL
1678 || term_str(KS_CCO) == empty_option)
1679 set_color_count(tgetnum("Co"));
1680
1681# ifndef hpux
1682 BC = (char *)TGETSTR("bc", &tp);
1683 UP = (char *)TGETSTR("up", &tp);
1684 p = TGETSTR("pc", &tp);
1685 if (p)
1686 PC = *p;
1687# endif /* hpux */
1688 }
1689 }
1690 else /* try == 0 || try == 2 */
1691#endif /* HAVE_TGETENT */
1692 /*
1693 * Use builtin termcap
1694 */
1695 {
1696#ifdef HAVE_TGETENT
1697 /*
1698 * If builtin termcap was already used, there is no need to search
1699 * for the builtin termcap again, quit now.
1700 */
1701 if (try == 2 && builtin_first && termcap_cleared)
1702 break;
1703#endif
1704 /*
1705 * search for 'term' in builtin_termcaps[]
1706 */
1707 termp = find_builtin_term(term);
1708 if (termp->bt_string == NULL) /* did not find it */
1709 {
1710#ifdef HAVE_TGETENT
1711 /*
1712 * If try == 0, first try the external termcap. If that is not
1713 * found we'll get back here with try == 2.
1714 * If termcap_cleared is set we used the external termcap,
1715 * don't complain about not finding the term in the builtin
1716 * termcap.
1717 */
1718 if (try == 0) /* try external one */
1719 continue;
1720 if (termcap_cleared) /* found in external termcap */
1721 break;
1722#endif
1723
1724 mch_errmsg("\r\n");
1725 if (error_msg != NULL)
1726 {
1727 mch_errmsg((char *)error_msg);
1728 mch_errmsg("\r\n");
1729 }
1730 mch_errmsg("'");
1731 mch_errmsg((char *)term);
1732 mch_errmsg(_("' not known. Available builtin terminals are:"));
1733 mch_errmsg("\r\n");
1734 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1735 ++termp)
1736 {
1737 if (termp->bt_entry == (int)KS_NAME)
1738 {
1739#ifdef HAVE_TGETENT
1740 mch_errmsg(" builtin_");
1741#else
1742 mch_errmsg(" ");
1743#endif
1744 mch_errmsg(termp->bt_string);
1745 mch_errmsg("\r\n");
1746 }
1747 }
1748 /* when user typed :set term=xxx, quit here */
1749 if (starting != NO_SCREEN)
1750 {
1751 screen_start(); /* don't know where cursor is now */
1752 wait_return(TRUE);
1753 return FAIL;
1754 }
1755 term = DEFAULT_TERM;
1756 mch_errmsg(_("defaulting to '"));
1757 mch_errmsg((char *)term);
1758 mch_errmsg("'\r\n");
1759 if (emsg_silent == 0)
1760 {
1761 screen_start(); /* don't know where cursor is now */
1762 out_flush();
1763 ui_delay(2000L, TRUE);
1764 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001765 set_string_option_direct((char_u *)"term", -1, term,
1766 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 display_errors();
1768 }
1769 out_flush();
1770#ifdef HAVE_TGETENT
1771 if (!termcap_cleared)
1772 {
1773#endif
1774 clear_termoptions(); /* clear old options */
1775#ifdef HAVE_TGETENT
1776 termcap_cleared = TRUE;
1777 }
1778#endif
1779 parse_builtin_tcap(term);
1780#ifdef FEAT_GUI
1781 if (term_is_gui(term))
1782 {
1783 out_flush();
1784 gui_init();
1785 /* If starting the GUI failed, don't do any of the other
1786 * things for this terminal */
1787 if (!gui.in_use)
1788 return FAIL;
1789#ifdef HAVE_TGETENT
1790 break; /* don't try using external termcap */
1791#endif
1792 }
1793#endif /* FEAT_GUI */
1794 }
1795#ifdef HAVE_TGETENT
1796 }
1797#endif
1798
1799/*
1800 * special: There is no info in the termcap about whether the cursor
1801 * positioning is relative to the start of the screen or to the start of the
1802 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1803 * relative.
1804 */
1805 if (STRCMP(term, "pcterm") == 0)
1806 T_CCS = (char_u *)"yes";
1807 else
1808 T_CCS = empty_option;
1809
1810#ifdef UNIX
1811/*
1812 * Any "stty" settings override the default for t_kb from the termcap.
1813 * This is in os_unix.c, because it depends a lot on the version of unix that
1814 * is being used.
1815 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1816 */
1817#ifdef FEAT_GUI
1818 if (!gui.in_use)
1819#endif
1820 get_stty();
1821#endif
1822
1823/*
1824 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1825 * didn't work, use the default CTRL-H
1826 * The default for t_kD is DEL, unless t_kb is DEL.
1827 * The vim_strsave'd strings are probably lost forever, well it's only two
1828 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1829 * directly.
1830 */
1831#ifdef FEAT_GUI
1832 if (!gui.in_use)
1833#endif
1834 {
1835 bs_p = find_termcode((char_u *)"kb");
1836 del_p = find_termcode((char_u *)"kD");
1837 if (bs_p == NULL || *bs_p == NUL)
1838 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1839 if ((del_p == NULL || *del_p == NUL) &&
1840 (bs_p == NULL || *bs_p != DEL))
1841 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1842 }
1843
1844#if defined(UNIX) || defined(VMS)
1845 term_is_xterm = vim_is_xterm(term);
1846#endif
1847
1848#ifdef FEAT_MOUSE
1849# if defined(UNIX) || defined(VMS)
1850# ifdef FEAT_MOUSE_TTY
1851 /*
1852 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1853 * The termcode for the mouse is added as a side effect in option.c.
1854 */
1855 {
1856 char_u *p;
1857
1858 p = (char_u *)"";
1859# ifdef FEAT_MOUSE_XTERM
1860# ifdef FEAT_CLIPBOARD
1861# ifdef FEAT_GUI
1862 if (!gui.in_use)
1863# endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001864# ifndef FEAT_CYGWIN_WIN32_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 clip_init(FALSE);
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867# endif
Bram Moolenaar864207d2008-06-24 22:14:38 +00001868 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 {
1870 if (use_xterm_mouse())
1871 p = NULL; /* keep existing value, might be "xterm2" */
1872 else
1873 p = (char_u *)"xterm";
1874 }
1875# endif
1876 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001877 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001879 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1880 * "xterm2" in check_termcode(). */
1881 reset_option_was_set((char_u *)"ttym");
1882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (p == NULL
1884# ifdef FEAT_GUI
1885 || gui.in_use
1886# endif
1887 )
1888 check_mouse_termcode(); /* set mouse termcode anyway */
1889 }
1890# endif
1891# else
1892 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1893# endif
1894#endif /* FEAT_MOUSE */
1895
1896#ifdef FEAT_SNIFF
1897 {
1898 char_u name[2];
1899
1900 name[0] = (int)KS_EXTRA;
1901 name[1] = (int)KE_SNIFF;
1902 add_termcode(name, (char_u *)"\233sniff", FALSE);
1903 }
1904#endif
1905
1906#ifdef USE_TERM_CONSOLE
1907 /* DEFAULT_TERM indicates that it is the machine console. */
1908 if (STRCMP(term, DEFAULT_TERM) != 0)
1909 term_console = FALSE;
1910 else
1911 {
1912 term_console = TRUE;
1913# ifdef AMIGA
1914 win_resize_on(); /* enable window resizing reports */
1915# endif
1916 }
1917#endif
1918
1919#if defined(UNIX) || defined(VMS)
1920 /*
1921 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1922 */
1923 if (vim_is_fastterm(term))
1924 p_tf = TRUE;
1925#endif
1926#ifdef USE_TERM_CONSOLE
1927 /*
1928 * 'ttyfast' is default on consoles
1929 */
1930 if (term_console)
1931 p_tf = TRUE;
1932#endif
1933
1934 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1935
1936 full_screen = TRUE; /* we can use termcap codes from now on */
1937 set_term_defaults(); /* use current values as defaults */
1938#ifdef FEAT_TERMRESPONSE
1939 crv_status = CRV_GET; /* Get terminal version later */
1940#endif
1941
1942 /*
1943 * Initialize the terminal with the appropriate termcap codes.
1944 * Set the mouse and window title if possible.
1945 * Don't do this when starting, need to parse the .vimrc first, because it
1946 * may redefine t_TI etc.
1947 */
1948 if (starting != NO_SCREEN)
1949 {
1950 starttermcap(); /* may change terminal mode */
1951#ifdef FEAT_MOUSE
1952 setmouse(); /* may start using the mouse */
1953#endif
1954#ifdef FEAT_TITLE
1955 maketitle(); /* may display window title */
1956#endif
1957 }
1958
1959 /* display initial screen after ttest() checking. jw. */
1960 if (width <= 0 || height <= 0)
1961 {
1962 /* termcap failed to report size */
1963 /* set defaults, in case ui_get_shellsize() also fails */
1964 width = 80;
1965#if defined(MSDOS) || defined(WIN3264)
1966 height = 25; /* console is often 25 lines */
1967#else
1968 height = 24; /* most terminals are 24 lines */
1969#endif
1970 }
1971 set_shellsize(width, height, FALSE); /* may change Rows */
1972 if (starting != NO_SCREEN)
1973 {
1974 if (scroll_region)
1975 scroll_region_reset(); /* In case Rows changed */
1976 check_map_keycodes(); /* check mappings for terminal codes used */
1977
1978#ifdef FEAT_AUTOCMD
1979 {
1980 buf_T *old_curbuf;
1981
1982 /*
1983 * Execute the TermChanged autocommands for each buffer that is
1984 * loaded.
1985 */
1986 old_curbuf = curbuf;
1987 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
1988 {
1989 if (curbuf->b_ml.ml_mfp != NULL)
1990 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
1991 curbuf);
1992 }
1993 if (buf_valid(old_curbuf))
1994 curbuf = old_curbuf;
1995 }
1996#endif
1997 }
1998
1999#ifdef FEAT_TERMRESPONSE
2000 may_req_termresponse();
2001#endif
2002
2003 return OK;
2004}
2005
2006#if defined(FEAT_MOUSE) || defined(PROTO)
2007
2008# ifdef FEAT_MOUSE_TTY
2009# define HMT_NORMAL 1
2010# define HMT_NETTERM 2
2011# define HMT_DEC 4
2012# define HMT_JSBTERM 8
2013# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002014# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002015# define HMT_SGR 64
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016static int has_mouse_termcode = 0;
2017# endif
2018
Bram Moolenaar864207d2008-06-24 22:14:38 +00002019# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 void
2021set_mouse_termcode(n, s)
2022 int n; /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2023 char_u *s;
2024{
2025 char_u name[2];
2026
2027 name[0] = n;
2028 name[1] = KE_FILLER;
2029 add_termcode(name, s, FALSE);
2030# ifdef FEAT_MOUSE_TTY
2031# ifdef FEAT_MOUSE_JSB
2032 if (n == KS_JSBTERM_MOUSE)
2033 has_mouse_termcode |= HMT_JSBTERM;
2034 else
2035# endif
2036# ifdef FEAT_MOUSE_NET
2037 if (n == KS_NETTERM_MOUSE)
2038 has_mouse_termcode |= HMT_NETTERM;
2039 else
2040# endif
2041# ifdef FEAT_MOUSE_DEC
2042 if (n == KS_DEC_MOUSE)
2043 has_mouse_termcode |= HMT_DEC;
2044 else
2045# endif
2046# ifdef FEAT_MOUSE_PTERM
2047 if (n == KS_PTERM_MOUSE)
2048 has_mouse_termcode |= HMT_PTERM;
2049 else
2050# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002051# ifdef FEAT_MOUSE_URXVT
2052 if (n == KS_URXVT_MOUSE)
2053 has_mouse_termcode |= HMT_URXVT;
2054 else
2055# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002056# ifdef FEAT_MOUSE_SGR
2057 if (n == KS_SGR_MOUSE)
2058 has_mouse_termcode |= HMT_SGR;
2059 else
2060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 has_mouse_termcode |= HMT_NORMAL;
2062# endif
2063}
2064# endif
2065
2066# if ((defined(UNIX) || defined(VMS) || defined(OS2)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002067 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 void
2069del_mouse_termcode(n)
2070 int n; /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2071{
2072 char_u name[2];
2073
2074 name[0] = n;
2075 name[1] = KE_FILLER;
2076 del_termcode(name);
2077# ifdef FEAT_MOUSE_TTY
2078# ifdef FEAT_MOUSE_JSB
2079 if (n == KS_JSBTERM_MOUSE)
2080 has_mouse_termcode &= ~HMT_JSBTERM;
2081 else
2082# endif
2083# ifdef FEAT_MOUSE_NET
2084 if (n == KS_NETTERM_MOUSE)
2085 has_mouse_termcode &= ~HMT_NETTERM;
2086 else
2087# endif
2088# ifdef FEAT_MOUSE_DEC
2089 if (n == KS_DEC_MOUSE)
2090 has_mouse_termcode &= ~HMT_DEC;
2091 else
2092# endif
2093# ifdef FEAT_MOUSE_PTERM
2094 if (n == KS_PTERM_MOUSE)
2095 has_mouse_termcode &= ~HMT_PTERM;
2096 else
2097# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002098# ifdef FEAT_MOUSE_URXVT
2099 if (n == KS_URXVT_MOUSE)
2100 has_mouse_termcode &= ~HMT_URXVT;
2101 else
2102# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002103# ifdef FEAT_MOUSE_SGR
2104 if (n == KS_SGR_MOUSE)
2105 has_mouse_termcode &= ~HMT_SGR;
2106 else
2107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 has_mouse_termcode &= ~HMT_NORMAL;
2109# endif
2110}
2111# endif
2112#endif
2113
2114#ifdef HAVE_TGETENT
2115/*
2116 * Call tgetent()
2117 * Return error message if it fails, NULL if it's OK.
2118 */
2119 static char_u *
2120tgetent_error(tbuf, term)
2121 char_u *tbuf;
2122 char_u *term;
2123{
2124 int i;
2125
2126 i = TGETENT(tbuf, term);
2127 if (i < 0 /* -1 is always an error */
2128# ifdef TGETENT_ZERO_ERR
2129 || i == 0 /* sometimes zero is also an error */
2130# endif
2131 )
2132 {
2133 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2134 * tgetent() with the always existing "dumb" entry to avoid a crash or
2135 * hang. */
2136 (void)TGETENT(tbuf, "dumb");
2137
2138 if (i < 0)
2139# ifdef TGETENT_ZERO_ERR
2140 return (char_u *)_("E557: Cannot open termcap file");
2141 if (i == 0)
2142# endif
2143#ifdef TERMINFO
2144 return (char_u *)_("E558: Terminal entry not found in terminfo");
2145#else
2146 return (char_u *)_("E559: Terminal entry not found in termcap");
2147#endif
2148 }
2149 return NULL;
2150}
2151
2152/*
2153 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2154 * Fix that here.
2155 */
2156 static char_u *
2157vim_tgetstr(s, pp)
2158 char *s;
2159 char_u **pp;
2160{
2161 char *p;
2162
2163 p = tgetstr(s, (char **)pp);
2164 if (p == (char *)-1)
2165 p = NULL;
2166 return (char_u *)p;
2167}
2168#endif /* HAVE_TGETENT */
2169
2170#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(__EMX__) || defined(VMS) || defined(MACOS_X))
2171/*
2172 * Get Columns and Rows from the termcap. Used after a window signal if the
2173 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2174 * and "li" entries never change. But on some systems this works.
2175 * Errors while getting the entries are ignored.
2176 */
2177 void
2178getlinecol(cp, rp)
2179 long *cp; /* pointer to columns */
2180 long *rp; /* pointer to rows */
2181{
2182 char_u tbuf[TBUFSZ];
2183
2184 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2185 {
2186 if (*cp == 0)
2187 *cp = tgetnum("co");
2188 if (*rp == 0)
2189 *rp = tgetnum("li");
2190 }
2191}
2192#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2193
2194/*
2195 * Get a string entry from the termcap and add it to the list of termcodes.
2196 * Used for <t_xx> special keys.
2197 * Give an error message for failure when not sourcing.
2198 * If force given, replace an existing entry.
2199 * Return FAIL if the entry was not found, OK if the entry was added.
2200 */
2201 int
2202add_termcap_entry(name, force)
2203 char_u *name;
2204 int force;
2205{
2206 char_u *term;
2207 int key;
2208 struct builtin_term *termp;
2209#ifdef HAVE_TGETENT
2210 char_u *string;
2211 int i;
2212 int builtin_first;
2213 char_u tbuf[TBUFSZ];
2214 char_u tstrbuf[TBUFSZ];
2215 char_u *tp = tstrbuf;
2216 char_u *error_msg = NULL;
2217#endif
2218
2219/*
2220 * If the GUI is running or will start in a moment, we only support the keys
2221 * that the GUI can produce.
2222 */
2223#ifdef FEAT_GUI
2224 if (gui.in_use || gui.starting)
2225 return gui_mch_haskey(name);
2226#endif
2227
2228 if (!force && find_termcode(name) != NULL) /* it's already there */
2229 return OK;
2230
2231 term = T_NAME;
2232 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2233 return FAIL;
2234
2235 if (term_is_builtin(term)) /* name starts with "builtin_" */
2236 {
2237 term += 8;
2238#ifdef HAVE_TGETENT
2239 builtin_first = TRUE;
2240#endif
2241 }
2242#ifdef HAVE_TGETENT
2243 else
2244 builtin_first = p_tbi;
2245#endif
2246
2247#ifdef HAVE_TGETENT
2248/*
2249 * We can get the entry from the builtin termcap and from the external one.
2250 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2251 * builtin termcap first.
2252 * If 'ttybuiltin' is off, try external termcap first.
2253 */
2254 for (i = 0; i < 2; ++i)
2255 {
2256 if (!builtin_first == i)
2257#endif
2258 /*
2259 * Search in builtin termcap
2260 */
2261 {
2262 termp = find_builtin_term(term);
2263 if (termp->bt_string != NULL) /* found it */
2264 {
2265 key = TERMCAP2KEY(name[0], name[1]);
2266 while (termp->bt_entry != (int)KS_NAME)
2267 {
2268 if ((int)termp->bt_entry == key)
2269 {
2270 add_termcode(name, (char_u *)termp->bt_string,
2271 term_is_8bit(term));
2272 return OK;
2273 }
2274 ++termp;
2275 }
2276 }
2277 }
2278#ifdef HAVE_TGETENT
2279 else
2280 /*
2281 * Search in external termcap
2282 */
2283 {
2284 error_msg = tgetent_error(tbuf, term);
2285 if (error_msg == NULL)
2286 {
2287 string = TGETSTR((char *)name, &tp);
2288 if (string != NULL && *string != NUL)
2289 {
2290 add_termcode(name, string, FALSE);
2291 return OK;
2292 }
2293 }
2294 }
2295 }
2296#endif
2297
2298 if (sourcing_name == NULL)
2299 {
2300#ifdef HAVE_TGETENT
2301 if (error_msg != NULL)
2302 EMSG(error_msg);
2303 else
2304#endif
2305 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2306 }
2307 return FAIL;
2308}
2309
2310 static int
2311term_is_builtin(name)
2312 char_u *name;
2313{
2314 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2315}
2316
2317/*
2318 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2319 * Assume that the terminal is using 8-bit controls when the name contains
2320 * "8bit", like in "xterm-8bit".
2321 */
2322 int
2323term_is_8bit(name)
2324 char_u *name;
2325{
2326 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2327}
2328
2329/*
2330 * Translate terminal control chars from 7-bit to 8-bit:
2331 * <Esc>[ -> CSI
2332 * <Esc>] -> <M-C-]>
2333 * <Esc>O -> <M-C-O>
2334 */
2335 static int
2336term_7to8bit(p)
2337 char_u *p;
2338{
2339 if (*p == ESC)
2340 {
2341 if (p[1] == '[')
2342 return CSI;
2343 if (p[1] == ']')
2344 return 0x9d;
2345 if (p[1] == 'O')
2346 return 0x8f;
2347 }
2348 return 0;
2349}
2350
2351#ifdef FEAT_GUI
2352 int
2353term_is_gui(name)
2354 char_u *name;
2355{
2356 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2357}
2358#endif
2359
2360#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2361
2362 char_u *
2363tltoa(i)
2364 unsigned long i;
2365{
2366 static char_u buf[16];
2367 char_u *p;
2368
2369 p = buf + 15;
2370 *p = '\0';
2371 do
2372 {
2373 --p;
2374 *p = (char_u) (i % 10 + '0');
2375 i /= 10;
2376 }
2377 while (i > 0 && p > buf);
2378 return p;
2379}
2380#endif
2381
2382#ifndef HAVE_TGETENT
2383
2384/*
2385 * minimal tgoto() implementation.
2386 * no padding and we only parse for %i %d and %+char
2387 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002388static char *tgoto __ARGS((char *, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002390 static char *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391tgoto(cm, x, y)
2392 char *cm;
2393 int x, y;
2394{
2395 static char buf[30];
2396 char *p, *s, *e;
2397
2398 if (!cm)
2399 return "OOPS";
2400 e = buf + 29;
2401 for (s = buf; s < e && *cm; cm++)
2402 {
2403 if (*cm != '%')
2404 {
2405 *s++ = *cm;
2406 continue;
2407 }
2408 switch (*++cm)
2409 {
2410 case 'd':
2411 p = (char *)tltoa((unsigned long)y);
2412 y = x;
2413 while (*p)
2414 *s++ = *p++;
2415 break;
2416 case 'i':
2417 x++;
2418 y++;
2419 break;
2420 case '+':
2421 *s++ = (char)(*++cm + y);
2422 y = x;
2423 break;
2424 case '%':
2425 *s++ = *cm;
2426 break;
2427 default:
2428 return "OOPS";
2429 }
2430 }
2431 *s = '\0';
2432 return buf;
2433}
2434
2435#endif /* HAVE_TGETENT */
2436
2437/*
2438 * Set the terminal name and initialize the terminal options.
2439 * If "name" is NULL or empty, get the terminal name from the environment.
2440 * If that fails, use the default terminal name.
2441 */
2442 void
2443termcapinit(name)
2444 char_u *name;
2445{
2446 char_u *term;
2447
2448 if (name != NULL && *name == NUL)
2449 name = NULL; /* empty name is equal to no name */
2450 term = name;
2451
2452#ifdef __BEOS__
2453 /*
2454 * TERM environment variable is normally set to 'ansi' on the Bebox;
2455 * Since the BeBox doesn't quite support full ANSI yet, we use our
2456 * own custom 'ansi-beos' termcap instead, unless the -T option has
2457 * been given on the command line.
2458 */
2459 if (term == NULL
2460 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2461 term = DEFAULT_TERM;
2462#endif
2463#ifndef MSWIN
2464 if (term == NULL)
2465 term = mch_getenv((char_u *)"TERM");
2466#endif
2467 if (term == NULL || *term == NUL)
2468 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002469 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
2471 /* Set the default terminal name. */
2472 set_string_default("term", term);
2473 set_string_default("ttytype", term);
2474
2475 /*
2476 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2477 */
2478 set_termname(T_NAME != NULL ? T_NAME : term);
2479}
2480
2481/*
2482 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2483 */
2484#ifdef DOS16
2485# define OUT_SIZE 255 /* only have 640K total... */
2486#else
2487# ifdef FEAT_GUI_W16
2488# define OUT_SIZE 1023 /* Save precious 1K near data */
2489# else
2490# define OUT_SIZE 2047
2491# endif
2492#endif
2493 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2494static char_u out_buf[OUT_SIZE + 1];
2495static int out_pos = 0; /* number of chars in out_buf */
2496
2497/*
2498 * out_flush(): flush the output buffer
2499 */
2500 void
2501out_flush()
2502{
2503 int len;
2504
2505 if (out_pos != 0)
2506 {
2507 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2508 len = out_pos;
2509 out_pos = 0;
2510 ui_write(out_buf, len);
2511 }
2512}
2513
2514#if defined(FEAT_MBYTE) || defined(PROTO)
2515/*
2516 * Sometimes a byte out of a multi-byte character is written with out_char().
2517 * To avoid flushing half of the character, call this function first.
2518 */
2519 void
2520out_flush_check()
2521{
2522 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2523 out_flush();
2524}
2525#endif
2526
2527#ifdef FEAT_GUI
2528/*
2529 * out_trash(): Throw away the contents of the output buffer
2530 */
2531 void
2532out_trash()
2533{
2534 out_pos = 0;
2535}
2536#endif
2537
2538/*
2539 * out_char(c): put a byte into the output buffer.
2540 * Flush it if it becomes full.
2541 * This should not be used for outputting text on the screen (use functions
2542 * like msg_puts() and screen_putchar() for that).
2543 */
2544 void
2545out_char(c)
2546 unsigned c;
2547{
2548#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2549 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2550 out_char('\r');
2551#endif
2552
2553 out_buf[out_pos++] = c;
2554
2555 /* For testing we flush each time. */
2556 if (out_pos >= OUT_SIZE || p_wd)
2557 out_flush();
2558}
2559
2560static void out_char_nf __ARGS((unsigned));
2561
2562/*
2563 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2564 */
2565 static void
2566out_char_nf(c)
2567 unsigned c;
2568{
2569#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2570 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2571 out_char_nf('\r');
2572#endif
2573
2574 out_buf[out_pos++] = c;
2575
2576 if (out_pos >= OUT_SIZE)
2577 out_flush();
2578}
2579
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002580#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2581 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582/*
2583 * A never-padding out_str.
2584 * use this whenever you don't want to run the string through tputs.
2585 * tputs above is harmless, but tputs from the termcap library
2586 * is likely to strip off leading digits, that it mistakes for padding
2587 * information, and "%i", "%d", etc.
2588 * This should only be used for writing terminal codes, not for outputting
2589 * normal text (use functions like msg_puts() and screen_putchar() for that).
2590 */
2591 void
2592out_str_nf(s)
2593 char_u *s;
2594{
2595 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2596 out_flush();
2597 while (*s)
2598 out_char_nf(*s++);
2599
2600 /* For testing we write one string at a time. */
2601 if (p_wd)
2602 out_flush();
2603}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605
2606/*
2607 * out_str(s): Put a character string a byte at a time into the output buffer.
2608 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2609 * This should only be used for writing terminal codes, not for outputting
2610 * normal text (use functions like msg_puts() and screen_putchar() for that).
2611 */
2612 void
2613out_str(s)
2614 char_u *s;
2615{
2616 if (s != NULL && *s)
2617 {
2618#ifdef FEAT_GUI
2619 /* Don't use tputs() when GUI is used, ncurses crashes. */
2620 if (gui.in_use)
2621 {
2622 out_str_nf(s);
2623 return;
2624 }
2625#endif
2626 /* avoid terminal strings being split up */
2627 if (out_pos > OUT_SIZE - 20)
2628 out_flush();
2629#ifdef HAVE_TGETENT
2630 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2631#else
2632 while (*s)
2633 out_char_nf(*s++);
2634#endif
2635
2636 /* For testing we write one string at a time. */
2637 if (p_wd)
2638 out_flush();
2639 }
2640}
2641
2642/*
2643 * cursor positioning using termcap parser. (jw)
2644 */
2645 void
2646term_windgoto(row, col)
2647 int row;
2648 int col;
2649{
2650 OUT_STR(tgoto((char *)T_CM, col, row));
2651}
2652
2653 void
2654term_cursor_right(i)
2655 int i;
2656{
2657 OUT_STR(tgoto((char *)T_CRI, 0, i));
2658}
2659
2660 void
2661term_append_lines(line_count)
2662 int line_count;
2663{
2664 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2665}
2666
2667 void
2668term_delete_lines(line_count)
2669 int line_count;
2670{
2671 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2672}
2673
2674#if defined(HAVE_TGETENT) || defined(PROTO)
2675 void
2676term_set_winpos(x, y)
2677 int x;
2678 int y;
2679{
2680 /* Can't handle a negative value here */
2681 if (x < 0)
2682 x = 0;
2683 if (y < 0)
2684 y = 0;
2685 OUT_STR(tgoto((char *)T_CWP, y, x));
2686}
2687
2688 void
2689term_set_winsize(width, height)
2690 int width;
2691 int height;
2692{
2693 OUT_STR(tgoto((char *)T_CWS, height, width));
2694}
2695#endif
2696
2697 void
2698term_fg_color(n)
2699 int n;
2700{
2701 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2702 if (*T_CAF)
2703 term_color(T_CAF, n);
2704 else if (*T_CSF)
2705 term_color(T_CSF, n);
2706}
2707
2708 void
2709term_bg_color(n)
2710 int n;
2711{
2712 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2713 if (*T_CAB)
2714 term_color(T_CAB, n);
2715 else if (*T_CSB)
2716 term_color(T_CSB, n);
2717}
2718
2719 static void
2720term_color(s, n)
2721 char_u *s;
2722 int n;
2723{
2724 char buf[20];
2725 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2726
2727 /* Special handling of 16 colors, because termcap can't handle it */
2728 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2729 /* Also accept CSI instead of <Esc>[ */
2730 if (n >= 8 && t_colors >= 16
2731 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2732 && s[i] != NUL
2733 && (STRCMP(s + i + 1, "%p1%dm") == 0
2734 || STRCMP(s + i + 1, "%dm") == 0)
2735 && (s[i] == '3' || s[i] == '4'))
2736 {
2737 sprintf(buf,
2738#ifdef TERMINFO
2739 "%s%s%%p1%%dm",
2740#else
2741 "%s%s%%dm",
2742#endif
2743 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2744 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2745 : (n >= 16 ? "48;5;" : "10"));
2746 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2747 }
2748 else
2749 OUT_STR(tgoto((char *)s, 0, n));
2750}
2751
2752#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X))) || defined(PROTO)
2753/*
2754 * Generic function to set window title, using t_ts and t_fs.
2755 */
2756 void
2757term_settitle(title)
2758 char_u *title;
2759{
2760 /* t_ts takes one argument: column in status line */
2761 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2762 out_str_nf(title);
2763 out_str(T_FS); /* set title end */
2764 out_flush();
2765}
2766#endif
2767
2768/*
2769 * Make sure we have a valid set or terminal options.
2770 * Replace all entries that are NULL by empty_option
2771 */
2772 void
2773ttest(pairs)
2774 int pairs;
2775{
2776 check_options(); /* make sure no options are NULL */
2777
2778 /*
2779 * MUST have "cm": cursor motion.
2780 */
2781 if (*T_CM == NUL)
2782 EMSG(_("E437: terminal capability \"cm\" required"));
2783
2784 /*
2785 * if "cs" defined, use a scroll region, it's faster.
2786 */
2787 if (*T_CS != NUL)
2788 scroll_region = TRUE;
2789 else
2790 scroll_region = FALSE;
2791
2792 if (pairs)
2793 {
2794 /*
2795 * optional pairs
2796 */
2797 /* TP goes to normal mode for TI (invert) and TB (bold) */
2798 if (*T_ME == NUL)
2799 T_ME = T_MR = T_MD = T_MB = empty_option;
2800 if (*T_SO == NUL || *T_SE == NUL)
2801 T_SO = T_SE = empty_option;
2802 if (*T_US == NUL || *T_UE == NUL)
2803 T_US = T_UE = empty_option;
2804 if (*T_CZH == NUL || *T_CZR == NUL)
2805 T_CZH = T_CZR = empty_option;
2806
2807 /* T_VE is needed even though T_VI is not defined */
2808 if (*T_VE == NUL)
2809 T_VI = empty_option;
2810
2811 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2812 if (*T_ME == NUL)
2813 {
2814 T_ME = T_SE;
2815 T_MR = T_SO;
2816 T_MD = T_SO;
2817 }
2818
2819 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2820 if (*T_SO == NUL)
2821 {
2822 T_SE = T_ME;
2823 if (*T_MR == NUL)
2824 T_SO = T_MD;
2825 else
2826 T_SO = T_MR;
2827 }
2828
2829 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2830 if (*T_CZH == NUL)
2831 {
2832 T_CZR = T_ME;
2833 if (*T_MR == NUL)
2834 T_CZH = T_MD;
2835 else
2836 T_CZH = T_MR;
2837 }
2838
2839 /* "Sb" and "Sf" come in pairs */
2840 if (*T_CSB == NUL || *T_CSF == NUL)
2841 {
2842 T_CSB = empty_option;
2843 T_CSF = empty_option;
2844 }
2845
2846 /* "AB" and "AF" come in pairs */
2847 if (*T_CAB == NUL || *T_CAF == NUL)
2848 {
2849 T_CAB = empty_option;
2850 T_CAF = empty_option;
2851 }
2852
2853 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2854 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002855 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
2857 /* Set 'weirdinvert' according to value of 't_xs' */
2858 p_wiv = (*T_XS != NUL);
2859 }
2860 need_gather = TRUE;
2861
2862 /* Set t_colors to the value of t_Co. */
2863 t_colors = atoi((char *)T_CCO);
2864}
2865
2866#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2867 || defined(PROTO)
2868/*
2869 * Represent the given long_u as individual bytes, with the most significant
2870 * byte first, and store them in dst.
2871 */
2872 void
2873add_long_to_buf(val, dst)
2874 long_u val;
2875 char_u *dst;
2876{
2877 int i;
2878 int shift;
2879
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002880 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 {
2882 shift = 8 * (sizeof(long_u) - i);
2883 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2884 }
2885}
2886
2887static int get_long_from_buf __ARGS((char_u *buf, long_u *val));
2888
2889/*
2890 * Interpret the next string of bytes in buf as a long integer, with the most
2891 * significant byte first. Note that it is assumed that buf has been through
2892 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
2893 * Puts result in val, and returns the number of bytes read from buf
2894 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
2895 * were present.
2896 */
2897 static int
2898get_long_from_buf(buf, val)
2899 char_u *buf;
2900 long_u *val;
2901{
2902 int len;
2903 char_u bytes[sizeof(long_u)];
2904 int i;
2905 int shift;
2906
2907 *val = 0;
2908 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
2909 if (len != -1)
2910 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002911 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 {
2913 shift = 8 * (sizeof(long_u) - 1 - i);
2914 *val += (long_u)bytes[i] << shift;
2915 }
2916 }
2917 return len;
2918}
2919#endif
2920
2921#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002922 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
2923 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924/*
2925 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
2926 * that buf has been through inchar(). Returns the actual number of bytes used
2927 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
2928 * available.
2929 */
2930 static int
2931get_bytes_from_buf(buf, bytes, num_bytes)
2932 char_u *buf;
2933 char_u *bytes;
2934 int num_bytes;
2935{
2936 int len = 0;
2937 int i;
2938 char_u c;
2939
2940 for (i = 0; i < num_bytes; i++)
2941 {
2942 if ((c = buf[len++]) == NUL)
2943 return -1;
2944 if (c == K_SPECIAL)
2945 {
2946 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
2947 return -1;
2948 if (buf[len++] == (int)KS_ZERO)
2949 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02002950 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
2951 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
2952 if (buf[len++] == (int)KE_CSI)
2953 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00002955 else if (c == CSI && buf[len] == KS_EXTRA
2956 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002957 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
2958 * the start of a special key, see add_to_input_buf_csi(). */
2959 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 bytes[i] = c;
2961 }
2962 return len;
2963}
2964#endif
2965
2966/*
Bram Moolenaare057d402013-06-30 17:51:51 +02002967 * Check if the new shell size is valid, correct it if it's too small or way
2968 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 */
2970 void
2971check_shellsize()
2972{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 if (Rows < min_rows()) /* need room for one window and command line */
2974 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02002975 limit_screen_size();
2976}
2977
2978/*
2979 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
2980 */
2981 void
2982limit_screen_size()
2983{
2984 if (Columns < MIN_COLUMNS)
2985 Columns = MIN_COLUMNS;
2986 else if (Columns > 10000)
2987 Columns = 10000;
2988 if (Rows > 1000)
2989 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990}
2991
Bram Moolenaar86b68352004-12-27 21:59:20 +00002992/*
2993 * Invoked just before the screen structures are going to be (re)allocated.
2994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 void
2996win_new_shellsize()
2997{
2998 static int old_Rows = 0;
2999 static int old_Columns = 0;
3000
3001 if (old_Rows != Rows || old_Columns != Columns)
3002 ui_new_shellsize();
3003 if (old_Rows != Rows)
3004 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003005 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003006 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003007 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 old_Rows = Rows;
3009 shell_new_rows(); /* update window sizes */
3010 }
3011 if (old_Columns != Columns)
3012 {
3013 old_Columns = Columns;
3014#ifdef FEAT_VERTSPLIT
3015 shell_new_columns(); /* update window sizes */
3016#endif
3017 }
3018}
3019
3020/*
3021 * Call this function when the Vim shell has been resized in any way.
3022 * Will obtain the current size and redraw (also when size didn't change).
3023 */
3024 void
3025shell_resized()
3026{
3027 set_shellsize(0, 0, FALSE);
3028}
3029
3030/*
3031 * Check if the shell size changed. Handle a resize.
3032 * When the size didn't change, nothing happens.
3033 */
3034 void
3035shell_resized_check()
3036{
3037 int old_Rows = Rows;
3038 int old_Columns = Columns;
3039
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003040 if (!exiting
3041#ifdef FEAT_GUI
3042 /* Do not get the size when executing a shell command during
3043 * startup. */
3044 && !gui.starting
3045#endif
3046 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003047 {
3048 (void)ui_get_shellsize();
3049 check_shellsize();
3050 if (old_Rows != Rows || old_Columns != Columns)
3051 shell_resized();
3052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053}
3054
3055/*
3056 * Set size of the Vim shell.
3057 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3058 * window size (this is used for the :win command).
3059 * If 'mustset' is FALSE, we may try to get the real window size and if
3060 * it fails use 'width' and 'height'.
3061 */
3062 void
3063set_shellsize(width, height, mustset)
3064 int width, height;
3065 int mustset;
3066{
3067 static int busy = FALSE;
3068
3069 /*
3070 * Avoid recursiveness, can happen when setting the window size causes
3071 * another window-changed signal.
3072 */
3073 if (busy)
3074 return;
3075
3076 if (width < 0 || height < 0) /* just checking... */
3077 return;
3078
Bram Moolenaara971b822011-09-14 14:43:25 +02003079 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003081 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 State = SETWSIZE;
3083 return;
3084 }
3085
Bram Moolenaara971b822011-09-14 14:43:25 +02003086 /* curwin->w_buffer can be NULL when we are closing a window and the
3087 * buffer has already been closed and removing a scrollbar causes a resize
3088 * event. Don't resize then, it will happen after entering another buffer.
3089 */
3090 if (curwin->w_buffer == NULL)
3091 return;
3092
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 ++busy;
3094
3095#ifdef AMIGA
3096 out_flush(); /* must do this before mch_get_shellsize() for
3097 some obscure reason */
3098#endif
3099
3100 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3101 {
3102 Rows = height;
3103 Columns = width;
3104 check_shellsize();
3105 ui_set_shellsize(mustset);
3106 }
3107 else
3108 check_shellsize();
3109
3110 /* The window layout used to be adjusted here, but it now happens in
3111 * screenalloc() (also invoked from screenclear()). That is because the
3112 * "busy" check above may skip this, but not screenalloc(). */
3113
3114 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3115 screenclear();
3116 else
3117 screen_start(); /* don't know where cursor is now */
3118
3119 if (starting != NO_SCREEN)
3120 {
3121#ifdef FEAT_TITLE
3122 maketitle();
3123#endif
3124 changed_line_abv_curs();
3125 invalidate_botline();
3126
3127 /*
3128 * We only redraw when it's needed:
3129 * - While at the more prompt or executing an external command, don't
3130 * redraw, but position the cursor.
3131 * - While editing the command line, only redraw that.
3132 * - in Ex mode, don't redraw anything.
3133 * - Otherwise, redraw right now, and position the cursor.
3134 * Always need to call update_screen() or screenalloc(), to make
3135 * sure Rows/Columns and the size of ScreenLines[] is correct!
3136 */
3137 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3138 || exmode_active)
3139 {
3140 screenalloc(FALSE);
3141 repeat_message();
3142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 else
3144 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003145#ifdef FEAT_SCROLLBIND
3146 if (curwin->w_p_scb)
3147 do_check_scrollbind(TRUE);
3148#endif
3149 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003150 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003151 update_screen(NOT_VALID);
3152 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003153 }
3154 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003155 {
3156 update_topline();
3157#if defined(FEAT_INS_EXPAND)
3158 if (pum_visible())
3159 {
3160 redraw_later(NOT_VALID);
3161 ins_compl_show_pum(); /* This includes the redraw. */
3162 }
3163 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003164#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003165 update_screen(NOT_VALID);
3166 if (redrawing())
3167 setcursor();
3168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
3170 cursor_on(); /* redrawing may have switched it off */
3171 }
3172 out_flush();
3173 --busy;
3174}
3175
3176/*
3177 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3178 * commands and Ex mode).
3179 */
3180 void
3181settmode(tmode)
3182 int tmode;
3183{
3184#ifdef FEAT_GUI
3185 /* don't set the term where gvim was started to any mode */
3186 if (gui.in_use)
3187 return;
3188#endif
3189
3190 if (full_screen)
3191 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 /*
3193 * When returning after calling a shell we want to really set the
3194 * terminal to raw mode, even though we think it already is, because
3195 * the shell program may have reset the terminal mode.
3196 * When we think the terminal is normal, don't try to set it to
3197 * normal again, because that causes problems (logout!) on some
3198 * machines.
3199 */
3200 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3201 {
3202#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003203# ifdef FEAT_GUI
3204 if (!gui.in_use && !gui.starting)
3205# endif
3206 {
3207 /* May need to check for T_CRV response and termcodes, it
3208 * doesn't work in Cooked mode, an external program may get
3209 * them. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003210 if (tmode != TMODE_RAW && (crv_status == CRV_SENT
3211 || u7_status == U7_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003212 (void)vpeekc_nomap();
3213 check_for_codes_from_term();
3214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#endif
3216#ifdef FEAT_MOUSE_TTY
3217 if (tmode != TMODE_RAW)
3218 mch_setmouse(FALSE); /* switch mouse off */
3219#endif
3220 out_flush();
3221 mch_settmode(tmode); /* machine specific function */
3222 cur_tmode = tmode;
3223#ifdef FEAT_MOUSE
3224 if (tmode == TMODE_RAW)
3225 setmouse(); /* may switch mouse on */
3226#endif
3227 out_flush();
3228 }
3229#ifdef FEAT_TERMRESPONSE
3230 may_req_termresponse();
3231#endif
3232 }
3233}
3234
3235 void
3236starttermcap()
3237{
3238 if (full_screen && !termcap_active)
3239 {
3240 out_str(T_TI); /* start termcap mode */
3241 out_str(T_KS); /* start "keypad transmit" mode */
3242 out_flush();
3243 termcap_active = TRUE;
3244 screen_start(); /* don't know where cursor is now */
3245#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003246# ifdef FEAT_GUI
3247 if (!gui.in_use && !gui.starting)
3248# endif
3249 {
3250 may_req_termresponse();
3251 /* Immediately check for a response. If t_Co changes, we don't
3252 * want to redraw with wrong colors first. */
3253 if (crv_status != CRV_GET)
3254 check_for_codes_from_term();
3255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256#endif
3257 }
3258}
3259
3260 void
3261stoptermcap()
3262{
3263 screen_stop_highlight();
3264 reset_cterm_colors();
3265 if (termcap_active)
3266 {
3267#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003268# ifdef FEAT_GUI
3269 if (!gui.in_use && !gui.starting)
3270# endif
3271 {
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003272 /* May need to discard T_CRV or T_U7 response. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003273 if (crv_status == CRV_SENT || u7_status == U7_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003274 {
3275# ifdef UNIX
3276 /* Give the terminal a chance to respond. */
3277 mch_delay(100L, FALSE);
3278# endif
3279# ifdef TCIFLUSH
3280 /* Discard data received but not read. */
3281 if (exiting)
3282 tcflush(fileno(stdin), TCIFLUSH);
3283# endif
3284 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003285 /* Check for termcodes first, otherwise an external program may
3286 * get them. */
3287 check_for_codes_from_term();
3288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289#endif
3290 out_str(T_KE); /* stop "keypad transmit" mode */
3291 out_flush();
3292 termcap_active = FALSE;
3293 cursor_on(); /* just in case it is still off */
3294 out_str(T_TE); /* stop termcap mode */
3295 screen_start(); /* don't know where cursor is now */
3296 out_flush();
3297 }
3298}
3299
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003300#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301/*
3302 * Request version string (for xterm) when needed.
3303 * Only do this after switching to raw mode, otherwise the result will be
3304 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003305 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003306 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 * Only do this after termcap mode has been started, otherwise the codes for
3308 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003309 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3310 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003311 * On Unix only do it when both output and input are a tty (avoid writing
3312 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 * The result is caught in check_termcode().
3314 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003315 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316may_req_termresponse()
3317{
3318 if (crv_status == CRV_GET
3319 && cur_tmode == TMODE_RAW
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003320 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 && termcap_active
Bram Moolenaarebefac62005-12-28 22:39:57 +00003322 && p_ek
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003323# ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 && isatty(1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003325 && isatty(read_cmd_fd)
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003326# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 && *T_CRV != NUL)
3328 {
3329 out_str(T_CRV);
3330 crv_status = CRV_SENT;
3331 /* check for the characters now, otherwise they might be eaten by
3332 * get_keystroke() */
3333 out_flush();
3334 (void)vpeekc_nomap();
3335 }
3336}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003337
3338# if defined(FEAT_MBYTE) || defined(PROTO)
3339/*
3340 * Check how the terminal treats ambiguous character width (UAX #11).
3341 * First, we move the cursor to (0, 0) and print a test ambiguous character
3342 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
3343 * If the terminal treats \u25bd as single width, the position is (0, 1),
3344 * or if it is treated as double width, that will be (0, 2).
3345 * This function has the side effect that changes cursor position, so
3346 * it must be called immediately after entering termcap mode.
3347 */
3348 void
3349may_req_ambiguous_character_width()
3350{
3351 if (u7_status == U7_GET
3352 && cur_tmode == TMODE_RAW
3353 && termcap_active
3354 && p_ek
3355# ifdef UNIX
3356 && isatty(1)
3357 && isatty(read_cmd_fd)
3358# endif
3359 && *T_U7 != NUL
3360 && !option_was_set((char_u *)"ambiwidth"))
3361 {
3362 char_u buf[16];
3363
3364 term_windgoto(0, 0);
3365 buf[mb_char2bytes(0x25bd, buf)] = 0;
3366 out_str(buf);
3367 out_str(T_U7);
3368 u7_status = U7_SENT;
3369 term_windgoto(0, 0);
3370 out_str((char_u *)" ");
3371 term_windgoto(0, 0);
3372 /* check for the characters now, otherwise they might be eaten by
3373 * get_keystroke() */
3374 out_flush();
3375 (void)vpeekc_nomap();
3376 }
3377}
3378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379#endif
3380
3381/*
3382 * Return TRUE when saving and restoring the screen.
3383 */
3384 int
3385swapping_screen()
3386{
3387 return (full_screen && *T_TI != NUL);
3388}
3389
3390#ifdef FEAT_MOUSE
3391/*
3392 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3393 */
3394 void
3395setmouse()
3396{
3397# ifdef FEAT_MOUSE_TTY
3398 int checkfor;
3399# endif
3400
3401# ifdef FEAT_MOUSESHAPE
3402 update_mouseshape(-1);
3403# endif
3404
3405# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3406# ifdef FEAT_GUI
3407 /* In the GUI the mouse is always enabled. */
3408 if (gui.in_use)
3409 return;
3410# endif
3411 /* be quick when mouse is off */
3412 if (*p_mouse == NUL || has_mouse_termcode == 0)
3413 return;
3414
3415 /* don't switch mouse on when not in raw mode (Ex mode) */
3416 if (cur_tmode != TMODE_RAW)
3417 {
3418 mch_setmouse(FALSE);
3419 return;
3420 }
3421
3422# ifdef FEAT_VISUAL
3423 if (VIsual_active)
3424 checkfor = MOUSE_VISUAL;
3425 else
3426# endif
3427 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
3428 checkfor = MOUSE_RETURN;
3429 else if (State & INSERT)
3430 checkfor = MOUSE_INSERT;
3431 else if (State & CMDLINE)
3432 checkfor = MOUSE_COMMAND;
3433 else if (State == CONFIRM || State == EXTERNCMD)
3434 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3435 else
3436 checkfor = MOUSE_NORMAL; /* assume normal mode */
3437
3438 if (mouse_has(checkfor))
3439 mch_setmouse(TRUE);
3440 else
3441 mch_setmouse(FALSE);
3442# endif
3443}
3444
3445/*
3446 * Return TRUE if
3447 * - "c" is in 'mouse', or
3448 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3449 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3450 * normal editing mode (not at hit-return message).
3451 */
3452 int
3453mouse_has(c)
3454 int c;
3455{
3456 char_u *p;
3457
3458 for (p = p_mouse; *p; ++p)
3459 switch (*p)
3460 {
3461 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3462 return TRUE;
3463 break;
3464 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3465 return TRUE;
3466 break;
3467 default: if (c == *p) return TRUE; break;
3468 }
3469 return FALSE;
3470}
3471
3472/*
3473 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3474 */
3475 int
3476mouse_model_popup()
3477{
3478 return (p_mousem[0] == 'p');
3479}
3480#endif
3481
3482/*
3483 * By outputting the 'cursor very visible' termcap code, for some windowed
3484 * terminals this makes the screen scrolled to the correct position.
3485 * Used when starting Vim or returning from a shell.
3486 */
3487 void
3488scroll_start()
3489{
3490 if (*T_VS != NUL)
3491 {
3492 out_str(T_VS);
3493 out_str(T_VE);
3494 screen_start(); /* don't know where cursor is now */
3495 }
3496}
3497
3498static int cursor_is_off = FALSE;
3499
3500/*
3501 * Enable the cursor.
3502 */
3503 void
3504cursor_on()
3505{
3506 if (cursor_is_off)
3507 {
3508 out_str(T_VE);
3509 cursor_is_off = FALSE;
3510 }
3511}
3512
3513/*
3514 * Disable the cursor.
3515 */
3516 void
3517cursor_off()
3518{
3519 if (full_screen)
3520 {
3521 if (!cursor_is_off)
3522 out_str(T_VI); /* disable cursor */
3523 cursor_is_off = TRUE;
3524 }
3525}
3526
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003527#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528/*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003529 * Set cursor shape to match Insert mode.
3530 */
3531 void
3532term_cursor_shape()
3533{
3534 static int showing_insert_mode = MAYBE;
3535
3536 if (!full_screen || *T_CSI == NUL || *T_CEI == NUL)
3537 return;
3538
3539 if (State & INSERT)
3540 {
3541 if (showing_insert_mode != TRUE)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003542 out_str(T_CSI); /* Insert mode cursor */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003543 showing_insert_mode = TRUE;
3544 }
3545 else
3546 {
3547 if (showing_insert_mode != FALSE)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003548 out_str(T_CEI); /* non-Insert mode cursor */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003549 showing_insert_mode = FALSE;
3550 }
3551}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003552#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003553
3554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 * Set scrolling region for window 'wp'.
3556 * The region starts 'off' lines from the start of the window.
3557 * Also set the vertical scroll region for a vertically split window. Always
3558 * the full width of the window, excluding the vertical separator.
3559 */
3560 void
3561scroll_region_set(wp, off)
3562 win_T *wp;
3563 int off;
3564{
3565 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3566 W_WINROW(wp) + off));
3567#ifdef FEAT_VERTSPLIT
3568 if (*T_CSV != NUL && wp->w_width != Columns)
3569 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3570 W_WINCOL(wp)));
3571#endif
3572 screen_start(); /* don't know where cursor is now */
3573}
3574
3575/*
3576 * Reset scrolling region to the whole screen.
3577 */
3578 void
3579scroll_region_reset()
3580{
3581 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
3582#ifdef FEAT_VERTSPLIT
3583 if (*T_CSV != NUL)
3584 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3585#endif
3586 screen_start(); /* don't know where cursor is now */
3587}
3588
3589
3590/*
3591 * List of terminal codes that are currently recognized.
3592 */
3593
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003594static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595{
3596 char_u name[2]; /* termcap name of entry */
3597 char_u *code; /* terminal code (in allocated memory) */
3598 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003599 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600} *termcodes = NULL;
3601
3602static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3603static int tc_len = 0; /* current number of entries in termcodes[] */
3604
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003605static int termcode_star __ARGS((char_u *code, int len));
3606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 void
3608clear_termcodes()
3609{
3610 while (tc_len > 0)
3611 vim_free(termcodes[--tc_len].code);
3612 vim_free(termcodes);
3613 termcodes = NULL;
3614 tc_max_len = 0;
3615
3616#ifdef HAVE_TGETENT
3617 BC = (char *)empty_option;
3618 UP = (char *)empty_option;
3619 PC = NUL; /* set pad character to NUL */
3620 ospeed = 0;
3621#endif
3622
3623 need_gather = TRUE; /* need to fill termleader[] */
3624}
3625
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003626#define ATC_FROM_TERM 55
3627
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628/*
3629 * Add a new entry to the list of terminal codes.
3630 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003631 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3632 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 */
3634 void
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003635add_termcode(name, string, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 char_u *name;
3637 char_u *string;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003638 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
3640 struct termcode *new_tc;
3641 int i, j;
3642 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003643 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 if (string == NULL || *string == NUL)
3646 {
3647 del_termcode(name);
3648 return;
3649 }
3650
3651 s = vim_strsave(string);
3652 if (s == NULL)
3653 return;
3654
3655 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003656 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003658 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 s[0] = term_7to8bit(string);
3660 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003661 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
3663 need_gather = TRUE; /* need to fill termleader[] */
3664
3665 /*
3666 * need to make space for more entries
3667 */
3668 if (tc_len == tc_max_len)
3669 {
3670 tc_max_len += 20;
3671 new_tc = (struct termcode *)alloc(
3672 (unsigned)(tc_max_len * sizeof(struct termcode)));
3673 if (new_tc == NULL)
3674 {
3675 tc_max_len -= 20;
3676 return;
3677 }
3678 for (i = 0; i < tc_len; ++i)
3679 new_tc[i] = termcodes[i];
3680 vim_free(termcodes);
3681 termcodes = new_tc;
3682 }
3683
3684 /*
3685 * Look for existing entry with the same name, it is replaced.
3686 * Look for an existing entry that is alphabetical higher, the new entry
3687 * is inserted in front of it.
3688 */
3689 for (i = 0; i < tc_len; ++i)
3690 {
3691 if (termcodes[i].name[0] < name[0])
3692 continue;
3693 if (termcodes[i].name[0] == name[0])
3694 {
3695 if (termcodes[i].name[1] < name[1])
3696 continue;
3697 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003698 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 */
3700 if (termcodes[i].name[1] == name[1])
3701 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003702 if (flags == ATC_FROM_TERM && (j = termcode_star(
3703 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003704 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003705 /* Don't replace ESC[123;*X or ESC O*X with another when
3706 * invoked from got_code_from_term(). */
3707 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003708 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003709 && s[len - 1]
3710 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003711 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003712 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003713 vim_free(s);
3714 return;
3715 }
3716 }
3717 else
3718 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003719 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003720 vim_free(termcodes[i].code);
3721 --tc_len;
3722 break;
3723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
3725 }
3726 /*
3727 * Found alphabetical larger entry, move rest to insert new entry
3728 */
3729 for (j = tc_len; j > i; --j)
3730 termcodes[j] = termcodes[j - 1];
3731 break;
3732 }
3733
3734 termcodes[i].name[0] = name[0];
3735 termcodes[i].name[1] = name[1];
3736 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003737 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003738
3739 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3740 * accept modifiers. */
3741 termcodes[i].modlen = 0;
3742 j = termcode_star(s, len);
3743 if (j > 0)
3744 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 ++tc_len;
3746}
3747
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003748/*
3749 * Check termcode "code[len]" for ending in ;*X, <Esc>O*X or <M-O>*X.
3750 * The "X" can be any character.
3751 * Return 0 if not found, 2 for ;*X and 1 for O*X and <M-O>*X.
3752 */
3753 static int
3754termcode_star(code, len)
3755 char_u *code;
3756 int len;
3757{
3758 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3759 if (len >= 3 && code[len - 2] == '*')
3760 {
3761 if (len >= 5 && code[len - 3] == ';')
3762 return 2;
3763 if ((len >= 4 && code[len - 3] == 'O') || code[len - 3] == 'O' + 128)
3764 return 1;
3765 }
3766 return 0;
3767}
3768
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 char_u *
3770find_termcode(name)
3771 char_u *name;
3772{
3773 int i;
3774
3775 for (i = 0; i < tc_len; ++i)
3776 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3777 return termcodes[i].code;
3778 return NULL;
3779}
3780
3781#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3782 char_u *
3783get_termcode(i)
3784 int i;
3785{
3786 if (i >= tc_len)
3787 return NULL;
3788 return &termcodes[i].name[0];
3789}
3790#endif
3791
3792 void
3793del_termcode(name)
3794 char_u *name;
3795{
3796 int i;
3797
3798 if (termcodes == NULL) /* nothing there yet */
3799 return;
3800
3801 need_gather = TRUE; /* need to fill termleader[] */
3802
3803 for (i = 0; i < tc_len; ++i)
3804 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3805 {
3806 del_termcode_idx(i);
3807 return;
3808 }
3809 /* not found. Give error message? */
3810}
3811
3812 static void
3813del_termcode_idx(idx)
3814 int idx;
3815{
3816 int i;
3817
3818 vim_free(termcodes[idx].code);
3819 --tc_len;
3820 for (i = idx; i < tc_len; ++i)
3821 termcodes[i] = termcodes[i + 1];
3822}
3823
3824#ifdef FEAT_TERMRESPONSE
3825/*
3826 * Called when detected that the terminal sends 8-bit codes.
3827 * Convert all 7-bit codes to their 8-bit equivalent.
3828 */
3829 static void
3830switch_to_8bit()
3831{
3832 int i;
3833 int c;
3834
3835 /* Only need to do something when not already using 8-bit codes. */
3836 if (!term_is_8bit(T_NAME))
3837 {
3838 for (i = 0; i < tc_len; ++i)
3839 {
3840 c = term_7to8bit(termcodes[i].code);
3841 if (c != 0)
3842 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003843 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 termcodes[i].code[0] = c;
3845 }
3846 }
3847 need_gather = TRUE; /* need to fill termleader[] */
3848 }
3849 detected_8bit = TRUE;
3850}
3851#endif
3852
3853#ifdef CHECK_DOUBLE_CLICK
3854static linenr_T orig_topline = 0;
3855# ifdef FEAT_DIFF
3856static int orig_topfill = 0;
3857# endif
3858#endif
3859#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
3860/*
3861 * Checking for double clicks ourselves.
3862 * "orig_topline" is used to avoid detecting a double-click when the window
3863 * contents scrolled (e.g., when 'scrolloff' is non-zero).
3864 */
3865/*
3866 * Set orig_topline. Used when jumping to another window, so that a double
3867 * click still works.
3868 */
3869 void
3870set_mouse_topline(wp)
3871 win_T *wp;
3872{
3873 orig_topline = wp->w_topline;
3874# ifdef FEAT_DIFF
3875 orig_topfill = wp->w_topfill;
3876# endif
3877}
3878#endif
3879
3880/*
3881 * Check if typebuf.tb_buf[] contains a terminal key code.
3882 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
3883 * + max_offset].
3884 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003885 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 * With a match, the match is removed, the replacement code is inserted in
3887 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
3888 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003889 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
3890 * "buflen" is then the length of the string in buf[] and is updated for
3891 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 */
3893 int
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003894check_termcode(max_offset, buf, bufsize, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 int max_offset;
3896 char_u *buf;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003897 int bufsize;
3898 int *buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899{
3900 char_u *tp;
3901 char_u *p;
3902 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003903 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003905 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 int offset;
3907 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003908 int modifiers;
3909 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 int new_slen;
3911 int extra;
3912 char_u string[MAX_KEY_CODE_LEN + 1];
3913 int i, j;
3914 int idx = 0;
3915#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00003916# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
3917 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 char_u bytes[6];
3919 int num_bytes;
3920# endif
3921 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 int is_click, is_drag;
3923 int wheel_code = 0;
3924 int current_button;
3925 static int held_button = MOUSE_RELEASE;
3926 static int orig_num_clicks = 1;
3927 static int orig_mouse_code = 0x0;
3928# ifdef CHECK_DOUBLE_CLICK
3929 static int orig_mouse_col = 0;
3930 static int orig_mouse_row = 0;
3931 static struct timeval orig_mouse_time = {0, 0};
3932 /* time of previous mouse click */
3933 struct timeval mouse_time; /* time of current mouse click */
3934 long timediff; /* elapsed time in msec */
3935# endif
3936#endif
3937 int cpo_koffset;
3938#ifdef FEAT_MOUSE_GPM
3939 extern int gpm_flag; /* gpm library variable */
3940#endif
3941
3942 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
3943
3944 /*
3945 * Speed up the checks for terminal codes by gathering all first bytes
3946 * used in termleader[]. Often this is just a single <Esc>.
3947 */
3948 if (need_gather)
3949 gather_termleader();
3950
3951 /*
3952 * Check at several positions in typebuf.tb_buf[], to catch something like
3953 * "x<Up>" that can be mapped. Stop at max_offset, because characters
3954 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01003955 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 * This is used often, KEEP IT FAST!
3957 */
3958 for (offset = 0; offset < max_offset; ++offset)
3959 {
3960 if (buf == NULL)
3961 {
3962 if (offset >= typebuf.tb_len)
3963 break;
3964 tp = typebuf.tb_buf + typebuf.tb_off + offset;
3965 len = typebuf.tb_len - offset; /* length of the input */
3966 }
3967 else
3968 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003969 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 break;
3971 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003972 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
3974
3975 /*
3976 * Don't check characters after K_SPECIAL, those are already
3977 * translated terminal chars (avoid translating ~@^Hx).
3978 */
3979 if (*tp == K_SPECIAL)
3980 {
3981 offset += 2; /* there are always 2 extra characters */
3982 continue;
3983 }
3984
3985 /*
3986 * Skip this position if the character does not appear as the first
3987 * character in term_strings. This speeds up a lot, since most
3988 * termcodes start with the same character (ESC or CSI).
3989 */
3990 i = *tp;
3991 for (p = termleader; *p && *p != i; ++p)
3992 ;
3993 if (*p == NUL)
3994 continue;
3995
3996 /*
3997 * Skip this position if p_ek is not set and tp[0] is an ESC and we
3998 * are in Insert mode.
3999 */
4000 if (*tp == ESC && !p_ek && (State & INSERT))
4001 continue;
4002
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004004 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004005 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
4007#ifdef FEAT_GUI
4008 if (gui.in_use)
4009 {
4010 /*
4011 * GUI special key codes are all of the form [CSI xx].
4012 */
4013 if (*tp == CSI) /* Special key from GUI */
4014 {
4015 if (len < 3)
4016 return -1; /* Shouldn't happen */
4017 slen = 3;
4018 key_name[0] = tp[1];
4019 key_name[1] = tp[2];
4020 }
4021 }
4022 else
4023#endif /* FEAT_GUI */
4024 {
4025 for (idx = 0; idx < tc_len; ++idx)
4026 {
4027 /*
4028 * Ignore the entry if we are not at the start of
4029 * typebuf.tb_buf[]
4030 * and there are not enough characters to make a match.
4031 * But only when the 'K' flag is in 'cpoptions'.
4032 */
4033 slen = termcodes[idx].len;
4034 if (cpo_koffset && offset && len < slen)
4035 continue;
4036 if (STRNCMP(termcodes[idx].code, tp,
4037 (size_t)(slen > len ? len : slen)) == 0)
4038 {
4039 if (len < slen) /* got a partial sequence */
4040 return -1; /* need to get more chars */
4041
4042 /*
4043 * When found a keypad key, check if there is another key
4044 * that matches and use that one. This makes <Home> to be
4045 * found instead of <kHome> when they produce the same
4046 * key code.
4047 */
4048 if (termcodes[idx].name[0] == 'K'
4049 && VIM_ISDIGIT(termcodes[idx].name[1]))
4050 {
4051 for (j = idx + 1; j < tc_len; ++j)
4052 if (termcodes[j].len == slen &&
4053 STRNCMP(termcodes[idx].code,
4054 termcodes[j].code, slen) == 0)
4055 {
4056 idx = j;
4057 break;
4058 }
4059 }
4060
4061 key_name[0] = termcodes[idx].name[0];
4062 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 break;
4064 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004065
4066 /*
4067 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004068 * <Esc>[123;*X (modslen == slen - 3)
4069 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4070 * When there is a modifier the * matches a number.
4071 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004072 */
4073 if (termcodes[idx].modlen > 0)
4074 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004075 modslen = termcodes[idx].modlen;
4076 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004077 continue;
4078 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004079 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004080 {
4081 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004082
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004083 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004084 return -1; /* need to get more chars */
4085
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004086 if (tp[modslen] == termcodes[idx].code[slen - 1])
4087 slen = modslen + 1; /* no modifiers */
4088 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004089 continue; /* no match */
4090 else
4091 {
4092 /* Skip over the digits, the final char must
4093 * follow. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004094 for (j = slen - 2; j < len && isdigit(tp[j]); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004095 ;
4096 ++j;
4097 if (len < j) /* got a partial sequence */
4098 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004099 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4100 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004101
4102 /* Match! Convert modifier bits. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004103 n = atoi((char *)tp + slen - 2) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004104 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004105 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004106 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004107 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004108 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004109 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004110 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004111 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004112
4113 slen = j;
4114 }
4115 key_name[0] = termcodes[idx].name[0];
4116 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004117 break;
4118 }
4119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
4121 }
4122
4123#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004124 if (key_name[0] == NUL
4125 /* URXVT mouse uses <ESC>[#;#;#M, but we are matching <ESC>[ */
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004126 || key_name[0] == KS_URXVT_MOUSE
4127# ifdef FEAT_MBYTE
4128 || u7_status == U7_SENT
4129# endif
4130 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
Bram Moolenaar9584b312013-03-13 19:29:28 +01004132 /* Check for some responses from terminal start with "<Esc>[" or
4133 * CSI.
4134 *
4135 * - xterm version string: <Esc>[>{x};{vers};{y}c
4136 * Also eat other possible responses to t_RV, rxvt returns
4137 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4138 * mrxvt has been reported to have "+" in the version. Assume
4139 * the escape sequence ends with a letter or one of "{|}~".
4140 *
4141 * - cursor position report: <Esc>[{row};{col}R
4142 * The final byte is 'R'. now it is only used for checking for
4143 * ambiguous-width character state.
4144 */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004145 p = tp[0] == CSI ? tp + 1 : tp + 2;
Bram Moolenaar9584b312013-03-13 19:29:28 +01004146 if ((*T_CRV != NUL || *T_U7 != NUL)
4147 && ((tp[0] == ESC && tp[1] == '[' && len >= 3)
Bram Moolenaar46a75612013-05-15 14:22:41 +02004148 || (tp[0] == CSI && len >= 2))
4149 && (VIM_ISDIGIT(*p) || *p == '>' || *p == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 {
4151 j = 0;
4152 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004153 for (i = 2 + (tp[0] != CSI); i < len
4154 && !(tp[i] >= '{' && tp[i] <= '~')
4155 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 if (tp[i] == ';' && ++j == 1)
Bram Moolenaar46a75612013-05-15 14:22:41 +02004157 extra = i + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 if (i == len)
4159 return -1; /* not enough characters */
4160
Bram Moolenaar9584b312013-03-13 19:29:28 +01004161#ifdef FEAT_MBYTE
4162 /* eat it when it has 2 arguments and ends in 'R' */
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004163 if (j == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004164 {
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004165 char *aw = NULL;
Bram Moolenaar9584b312013-03-13 19:29:28 +01004166
4167 u7_status = U7_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004168# ifdef FEAT_AUTOCMD
4169 did_cursorhold = TRUE;
4170# endif
Bram Moolenaar46a75612013-05-15 14:22:41 +02004171 if (extra > 0)
4172 extra = atoi((char *)tp + extra);
Bram Moolenaar9584b312013-03-13 19:29:28 +01004173 if (extra == 2)
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004174 aw = "single";
Bram Moolenaar9584b312013-03-13 19:29:28 +01004175 else if (extra == 3)
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004176 aw = "double";
4177 if (aw != NULL)
4178 set_option_value((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01004179 key_name[0] = (int)KS_EXTRA;
4180 key_name[1] = (int)KE_IGNORE;
4181 slen = i + 1;
4182 }
4183 else
4184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004186 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {
4188 crv_status = CRV_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004189# ifdef FEAT_AUTOCMD
4190 did_cursorhold = TRUE;
4191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192
4193 /* If this code starts with CSI, you can bet that the
4194 * terminal uses 8-bit codes. */
4195 if (tp[0] == CSI)
4196 switch_to_8bit();
4197
4198 /* rxvt sends its version number: "20703" is 2.7.3.
4199 * Ignore it for when the user has set 'term' to xterm,
4200 * even though it's an rxvt. */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004201 if (extra > 0)
4202 extra = atoi((char *)tp + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 if (extra > 20000)
4204 extra = 0;
4205
4206 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
4207 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004208 /* Only set 'ttymouse' automatically if it was not set
4209 * by the user already. */
4210 if (!option_was_set((char_u *)"ttym"))
4211 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004212# ifdef TTYM_SGR
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004213 if (extra >= 277)
4214 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004215 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004216 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004217# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004218 /* if xterm version >= 95 use mouse dragging */
4219 if (extra >= 95)
4220 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004222 }
4223
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 /* if xterm version >= 141 try to get termcap codes */
4225 if (extra >= 141)
4226 {
4227 check_for_codes = TRUE;
4228 need_gather = TRUE;
4229 req_codes_from_term();
4230 }
4231 }
4232# ifdef FEAT_EVAL
4233 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4234# endif
4235# ifdef FEAT_AUTOCMD
4236 apply_autocmds(EVENT_TERMRESPONSE,
4237 NULL, NULL, FALSE, curbuf);
4238# endif
4239 key_name[0] = (int)KS_EXTRA;
4240 key_name[1] = (int)KE_IGNORE;
4241 slen = i + 1;
4242 }
4243 }
4244
4245 /* Check for '<Esc>P1+r<hex bytes><Esc>\'. A "0" instead of the
4246 * "1" means an invalid request. */
4247 else if (check_for_codes
4248 && ((tp[0] == ESC && tp[1] == 'P' && len >= 2)
4249 || tp[0] == DCS))
4250 {
4251 j = 1 + (tp[0] != DCS);
4252 for (i = j; i < len; ++i)
4253 if ((tp[i] == ESC && tp[i + 1] == '\\' && i + 1 < len)
4254 || tp[i] == STERM)
4255 {
4256 if (i - j >= 3 && tp[j + 1] == '+' && tp[j + 2] == 'r')
4257 got_code_from_term(tp + j, i);
4258 key_name[0] = (int)KS_EXTRA;
4259 key_name[1] = (int)KE_IGNORE;
4260 slen = i + 1 + (tp[i] == ESC);
4261 break;
4262 }
4263
4264 if (i == len)
4265 return -1; /* not enough characters */
4266 }
4267 }
4268#endif
4269
4270 if (key_name[0] == NUL)
4271 continue; /* No match at this position, try next one */
4272
4273 /* We only get here when we have a complete termcode match */
4274
4275#ifdef FEAT_MOUSE
4276# ifdef FEAT_GUI
4277 /*
4278 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4279 * so that we know which window to scroll later.
4280 */
4281 if (gui.in_use
4282 && key_name[0] == (int)KS_EXTRA
4283 && (key_name[1] == (int)KE_X1MOUSE
4284 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004285 || key_name[1] == (int)KE_MOUSELEFT
4286 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 || key_name[1] == (int)KE_MOUSEDOWN
4288 || key_name[1] == (int)KE_MOUSEUP))
4289 {
4290 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4291 if (num_bytes == -1) /* not enough coordinates */
4292 return -1;
4293 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4294 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4295 slen += num_bytes;
4296 }
4297 else
4298# endif
4299 /*
4300 * If it is a mouse click, get the coordinates.
4301 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004302 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004304 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305# endif
4306# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004307 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308# endif
4309# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004310 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311# endif
4312# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004313 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004315# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004316 || key_name[0] == KS_URXVT_MOUSE
4317# endif
4318# ifdef FEAT_MOUSE_SGR
4319 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004320# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 )
4322 {
4323 is_click = is_drag = FALSE;
4324
Bram Moolenaar864207d2008-06-24 22:14:38 +00004325# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4326 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 if (key_name[0] == (int)KS_MOUSE)
4328 {
4329 /*
4330 * For xterm and MSDOS we get "<t_mouse>scr", where
4331 * s == encoded button state:
4332 * 0x20 = left button down
4333 * 0x21 = middle button down
4334 * 0x22 = right button down
4335 * 0x23 = any button release
4336 * 0x60 = button 4 down (scroll wheel down)
4337 * 0x61 = button 5 down (scroll wheel up)
4338 * add 0x04 for SHIFT
4339 * add 0x08 for ALT
4340 * add 0x10 for CTRL
4341 * add 0x20 for mouse drag (0x40 is drag with left button)
4342 * c == column + ' ' + 1 == column + 33
4343 * r == row + ' ' + 1 == row + 33
4344 *
4345 * The coordinates are passed on through global variables.
4346 * Ugly, but this avoids trouble with mouse clicks at an
4347 * unexpected moment and allows for mapping them.
4348 */
4349 for (;;)
4350 {
4351#ifdef FEAT_GUI
4352 if (gui.in_use)
4353 {
4354 /* GUI uses more bits for columns > 223 */
4355 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4356 if (num_bytes == -1) /* not enough coordinates */
4357 return -1;
4358 mouse_code = bytes[0];
4359 mouse_col = 128 * (bytes[1] - ' ' - 1)
4360 + bytes[2] - ' ' - 1;
4361 mouse_row = 128 * (bytes[3] - ' ' - 1)
4362 + bytes[4] - ' ' - 1;
4363 }
4364 else
4365#endif
4366 {
4367 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4368 if (num_bytes == -1) /* not enough coordinates */
4369 return -1;
4370 mouse_code = bytes[0];
4371 mouse_col = bytes[1] - ' ' - 1;
4372 mouse_row = bytes[2] - ' ' - 1;
4373 }
4374 slen += num_bytes;
4375
4376 /* If the following bytes is also a mouse code and it has
4377 * the same code, dump this one and get the next. This
4378 * makes dragging a whole lot faster. */
4379#ifdef FEAT_GUI
4380 if (gui.in_use)
4381 j = 3;
4382 else
4383#endif
4384 j = termcodes[idx].len;
4385 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4386 && tp[slen + j] == mouse_code
4387 && tp[slen + j + 1] != NUL
4388 && tp[slen + j + 2] != NUL
4389#ifdef FEAT_GUI
4390 && (!gui.in_use
4391 || (tp[slen + j + 3] != NUL
4392 && tp[slen + j + 4] != NUL))
4393#endif
4394 )
4395 slen += j;
4396 else
4397 break;
4398 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004401# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4402 if (key_name[0] == KS_URXVT_MOUSE
4403 || key_name[0] == KS_SGR_MOUSE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004404 {
4405 for (;;)
4406 {
4407 /* URXVT 1015 mouse reporting mode:
4408 * Almost identical to xterm mouse mode, except the values
4409 * are decimal instead of bytes.
4410 *
4411 * \033[%d;%d;%dM
4412 * ^-- row
4413 * ^----- column
4414 * ^-------- code
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004415 *
4416 * SGR 1006 mouse reporting mode:
4417 * Almost identical to xterm mouse mode, except the values
4418 * are decimal instead of bytes.
4419 *
4420 * \033[<%d;%d;%dM
4421 * ^-- row
4422 * ^----- column
4423 * ^-------- code
4424 *
4425 * \033[<%d;%d;%dm : mouse release event
4426 * ^-- row
4427 * ^----- column
4428 * ^-------- code
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004429 */
4430 p = tp + slen;
4431
4432 mouse_code = getdigits(&p);
4433 if (*p++ != ';')
4434 return -1;
4435
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004436 /* when mouse reporting is SGR, add 32 to mouse code */
4437 if (key_name[0] == KS_SGR_MOUSE)
4438 mouse_code += 32;
4439
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004440 mouse_col = getdigits(&p) - 1;
4441 if (*p++ != ';')
4442 return -1;
4443
4444 mouse_row = getdigits(&p) - 1;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004445 if (key_name[0] == KS_SGR_MOUSE && *p == 'm')
4446 mouse_code |= MOUSE_RELEASE;
4447 else if (*p != 'M')
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004448 return -1;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004449 p++;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004450
4451 slen += (int)(p - (tp + slen));
4452
4453 /* skip this one if next one has same code (like xterm
4454 * case) */
4455 j = termcodes[idx].len;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004456 if (STRNCMP(tp, tp + slen, (size_t)j) == 0)
4457 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004458 int slen2;
4459 int cmd_complete = 0;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004460
4461 /* check if the command is complete by looking for the
4462 * 'M' */
4463 for (slen2 = slen; slen2 < len; slen2++)
4464 {
4465 if (tp[slen2] == 'M'
4466 || (key_name[0] == KS_SGR_MOUSE
4467 && tp[slen2] == 'm'))
4468 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004469 cmd_complete = 1;
4470 break;
4471 }
4472 }
4473 p += j;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004474 if (cmd_complete && getdigits(&p) == mouse_code)
4475 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004476 slen += j; /* skip the \033[ */
4477 continue;
4478 }
4479 }
4480 break;
4481 }
4482 }
4483# endif
4484
4485 if (key_name[0] == (int)KS_MOUSE
4486#ifdef FEAT_MOUSE_URXVT
4487 || key_name[0] == (int)KS_URXVT_MOUSE
4488#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004489#ifdef FEAT_MOUSE_SGR
4490 || key_name[0] == KS_SGR_MOUSE
4491#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004492 )
4493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494# if !defined(MSWIN) && !defined(MSDOS)
4495 /*
4496 * Handle mouse events.
4497 * Recognize the xterm mouse wheel, but not in the GUI, the
4498 * Linux console with GPM and the MS-DOS or Win32 console
4499 * (multi-clicks use >= 0x60).
4500 */
4501 if (mouse_code >= MOUSEWHEEL_LOW
4502# ifdef FEAT_GUI
4503 && !gui.in_use
4504# endif
4505# ifdef FEAT_MOUSE_GPM
4506 && gpm_flag == 0
4507# endif
4508 )
4509 {
4510 /* Keep the mouse_code before it's changed, so that we
4511 * remember that it was a mouse wheel click. */
4512 wheel_code = mouse_code;
4513 }
4514# ifdef FEAT_MOUSE_XTERM
4515 else if (held_button == MOUSE_RELEASE
4516# ifdef FEAT_GUI
4517 && !gui.in_use
4518# endif
4519 && (mouse_code == 0x23 || mouse_code == 0x24))
4520 {
4521 /* Apparently used by rxvt scroll wheel. */
4522 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4523 }
4524# endif
4525
4526# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4527 else if (use_xterm_mouse() > 1)
4528 {
4529 if (mouse_code & MOUSE_DRAG_XTERM)
4530 mouse_code |= MOUSE_DRAG;
4531 }
4532# endif
4533# ifdef FEAT_XCLIPBOARD
4534 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4535 {
4536 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4537 stop_xterm_trace();
4538 else
4539 start_xterm_trace(mouse_code);
4540 }
4541# endif
4542# endif
4543 }
4544# endif /* !UNIX || FEAT_MOUSE_XTERM */
4545# ifdef FEAT_MOUSE_NET
4546 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4547 {
4548 int mc, mr;
4549
4550 /* expect a rather limited sequence like: balancing {
4551 * \033}6,45\r
4552 * '6' is the row, 45 is the column
4553 */
4554 p = tp + slen;
4555 mr = getdigits(&p);
4556 if (*p++ != ',')
4557 return -1;
4558 mc = getdigits(&p);
4559 if (*p++ != '\r')
4560 return -1;
4561
4562 mouse_col = mc - 1;
4563 mouse_row = mr - 1;
4564 mouse_code = MOUSE_LEFT;
4565 slen += (int)(p - (tp + slen));
4566 }
4567# endif /* FEAT_MOUSE_NET */
4568# ifdef FEAT_MOUSE_JSB
4569 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4570 {
4571 int mult, val, iter, button, status;
4572
4573 /* JSBTERM Input Model
4574 * \033[0~zw uniq escape sequence
4575 * (L-x) Left button pressed - not pressed x not reporting
4576 * (M-x) Middle button pressed - not pressed x not reporting
4577 * (R-x) Right button pressed - not pressed x not reporting
4578 * (SDmdu) Single , Double click, m mouse move d button down
4579 * u button up
4580 * ### X cursor position padded to 3 digits
4581 * ### Y cursor position padded to 3 digits
4582 * (s-x) SHIFT key pressed - not pressed x not reporting
4583 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004584 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 */
4586
4587 p = tp + slen;
4588 button = mouse_code = 0;
4589 switch (*p++)
4590 {
4591 case 'L': button = 1; break;
4592 case '-': break;
4593 case 'x': break; /* ignore sequence */
4594 default: return -1; /* Unknown Result */
4595 }
4596 switch (*p++)
4597 {
4598 case 'M': button |= 2; break;
4599 case '-': break;
4600 case 'x': break; /* ignore sequence */
4601 default: return -1; /* Unknown Result */
4602 }
4603 switch (*p++)
4604 {
4605 case 'R': button |= 4; break;
4606 case '-': break;
4607 case 'x': break; /* ignore sequence */
4608 default: return -1; /* Unknown Result */
4609 }
4610 status = *p++;
4611 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4612 mult /= 10, p++)
4613 if (*p >= '0' && *p <= '9')
4614 val += (*p - '0') * mult;
4615 else
4616 return -1;
4617 mouse_col = val;
4618 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4619 mult /= 10, p++)
4620 if (*p >= '0' && *p <= '9')
4621 val += (*p - '0') * mult;
4622 else
4623 return -1;
4624 mouse_row = val;
4625 switch (*p++)
4626 {
4627 case 's': button |= 8; break; /* SHIFT key Pressed */
4628 case '-': break; /* Not Pressed */
4629 case 'x': break; /* Not Reporting */
4630 default: return -1; /* Unknown Result */
4631 }
4632 switch (*p++)
4633 {
4634 case 'c': button |= 16; break; /* CTRL key Pressed */
4635 case '-': break; /* Not Pressed */
4636 case 'x': break; /* Not Reporting */
4637 default: return -1; /* Unknown Result */
4638 }
4639 if (*p++ != '\033')
4640 return -1;
4641 if (*p++ != '\\')
4642 return -1;
4643 switch (status)
4644 {
4645 case 'D': /* Double Click */
4646 case 'S': /* Single Click */
4647 if (button & 1) mouse_code |= MOUSE_LEFT;
4648 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4649 if (button & 4) mouse_code |= MOUSE_RIGHT;
4650 if (button & 8) mouse_code |= MOUSE_SHIFT;
4651 if (button & 16) mouse_code |= MOUSE_CTRL;
4652 break;
4653 case 'm': /* Mouse move */
4654 if (button & 1) mouse_code |= MOUSE_LEFT;
4655 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4656 if (button & 4) mouse_code |= MOUSE_RIGHT;
4657 if (button & 8) mouse_code |= MOUSE_SHIFT;
4658 if (button & 16) mouse_code |= MOUSE_CTRL;
4659 if ((button & 7) != 0)
4660 {
4661 held_button = mouse_code;
4662 mouse_code |= MOUSE_DRAG;
4663 }
4664 is_drag = TRUE;
4665 showmode();
4666 break;
4667 case 'd': /* Button Down */
4668 if (button & 1) mouse_code |= MOUSE_LEFT;
4669 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4670 if (button & 4) mouse_code |= MOUSE_RIGHT;
4671 if (button & 8) mouse_code |= MOUSE_SHIFT;
4672 if (button & 16) mouse_code |= MOUSE_CTRL;
4673 break;
4674 case 'u': /* Button Up */
4675 if (button & 1)
4676 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4677 if (button & 2)
4678 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4679 if (button & 4)
4680 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4681 if (button & 8)
4682 mouse_code |= MOUSE_SHIFT;
4683 if (button & 16)
4684 mouse_code |= MOUSE_CTRL;
4685 break;
4686 default: return -1; /* Unknown Result */
4687 }
4688
4689 slen += (p - (tp + slen));
4690 }
4691# endif /* FEAT_MOUSE_JSB */
4692# ifdef FEAT_MOUSE_DEC
4693 if (key_name[0] == (int)KS_DEC_MOUSE)
4694 {
4695 /* The DEC Locator Input Model
4696 * Netterm delivers the code sequence:
4697 * \033[2;4;24;80&w (left button down)
4698 * \033[3;0;24;80&w (left button up)
4699 * \033[6;1;24;80&w (right button down)
4700 * \033[7;0;24;80&w (right button up)
4701 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4702 * Pe is the event code
4703 * Pb is the button code
4704 * Pr is the row coordinate
4705 * Pc is the column coordinate
4706 * Pp is the third coordinate (page number)
4707 * Pe, the event code indicates what event caused this report
4708 * The following event codes are defined:
4709 * 0 - request, the terminal received an explicit request
4710 * for a locator report, but the locator is unavailable
4711 * 1 - request, the terminal received an explicit request
4712 * for a locator report
4713 * 2 - left button down
4714 * 3 - left button up
4715 * 4 - middle button down
4716 * 5 - middle button up
4717 * 6 - right button down
4718 * 7 - right button up
4719 * 8 - fourth button down
4720 * 9 - fourth button up
4721 * 10 - locator outside filter rectangle
4722 * Pb, the button code, ASCII decimal 0-15 indicating which
4723 * buttons are down if any. The state of the four buttons
4724 * on the locator correspond to the low four bits of the
4725 * decimal value,
4726 * "1" means button depressed
4727 * 0 - no buttons down,
4728 * 1 - right,
4729 * 2 - middle,
4730 * 4 - left,
4731 * 8 - fourth
4732 * Pr is the row coordinate of the locator position in the page,
4733 * encoded as an ASCII decimal value.
4734 * If Pr is omitted, the locator position is undefined
4735 * (outside the terminal window for example).
4736 * Pc is the column coordinate of the locator position in the
4737 * page, encoded as an ASCII decimal value.
4738 * If Pc is omitted, the locator position is undefined
4739 * (outside the terminal window for example).
4740 * Pp is the page coordinate of the locator position
4741 * encoded as an ASCII decimal value.
4742 * The page coordinate may be omitted if the locator is on
4743 * page one (the default). We ignore it anyway.
4744 */
4745 int Pe, Pb, Pr, Pc;
4746
4747 p = tp + slen;
4748
4749 /* get event status */
4750 Pe = getdigits(&p);
4751 if (*p++ != ';')
4752 return -1;
4753
4754 /* get button status */
4755 Pb = getdigits(&p);
4756 if (*p++ != ';')
4757 return -1;
4758
4759 /* get row status */
4760 Pr = getdigits(&p);
4761 if (*p++ != ';')
4762 return -1;
4763
4764 /* get column status */
4765 Pc = getdigits(&p);
4766
4767 /* the page parameter is optional */
4768 if (*p == ';')
4769 {
4770 p++;
4771 (void)getdigits(&p);
4772 }
4773 if (*p++ != '&')
4774 return -1;
4775 if (*p++ != 'w')
4776 return -1;
4777
4778 mouse_code = 0;
4779 switch (Pe)
4780 {
4781 case 0: return -1; /* position request while unavailable */
4782 case 1: /* a response to a locator position request includes
4783 the status of all buttons */
4784 Pb &= 7; /* mask off and ignore fourth button */
4785 if (Pb & 4)
4786 mouse_code = MOUSE_LEFT;
4787 if (Pb & 2)
4788 mouse_code = MOUSE_MIDDLE;
4789 if (Pb & 1)
4790 mouse_code = MOUSE_RIGHT;
4791 if (Pb)
4792 {
4793 held_button = mouse_code;
4794 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004795 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797 is_drag = TRUE;
4798 showmode();
4799 break;
4800 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004801 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 break;
4803 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
4804 break;
4805 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004806 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 break;
4808 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
4809 break;
4810 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004811 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 break;
4813 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
4814 break;
4815 case 8: return -1; /* fourth button down */
4816 case 9: return -1; /* fourth button up */
4817 case 10: return -1; /* mouse outside of filter rectangle */
4818 default: return -1; /* should never occur */
4819 }
4820
4821 mouse_col = Pc - 1;
4822 mouse_row = Pr - 1;
4823
4824 slen += (int)(p - (tp + slen));
4825 }
4826# endif /* FEAT_MOUSE_DEC */
4827# ifdef FEAT_MOUSE_PTERM
4828 if (key_name[0] == (int)KS_PTERM_MOUSE)
4829 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004830 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
4832 p = tp + slen;
4833
4834 action = getdigits(&p);
4835 if (*p++ != ';')
4836 return -1;
4837
4838 mouse_row = getdigits(&p);
4839 if (*p++ != ';')
4840 return -1;
4841 mouse_col = getdigits(&p);
4842 if (*p++ != ';')
4843 return -1;
4844
4845 button = getdigits(&p);
4846 mouse_code = 0;
4847
4848 switch( button )
4849 {
4850 case 4: mouse_code = MOUSE_LEFT; break;
4851 case 1: mouse_code = MOUSE_RIGHT; break;
4852 case 2: mouse_code = MOUSE_MIDDLE; break;
4853 default: return -1;
4854 }
4855
4856 switch( action )
4857 {
4858 case 31: /* Initial press */
4859 if (*p++ != ';')
4860 return -1;
4861
4862 num_clicks = getdigits(&p); /* Not used */
4863 break;
4864
4865 case 32: /* Release */
4866 mouse_code |= MOUSE_RELEASE;
4867 break;
4868
4869 case 33: /* Drag */
4870 held_button = mouse_code;
4871 mouse_code |= MOUSE_DRAG;
4872 break;
4873
4874 default:
4875 return -1;
4876 }
4877
4878 if (*p++ != 't')
4879 return -1;
4880
4881 slen += (p - (tp + slen));
4882 }
4883# endif /* FEAT_MOUSE_PTERM */
4884
4885 /* Interpret the mouse code */
4886 current_button = (mouse_code & MOUSE_CLICK_MASK);
4887 if (current_button == MOUSE_RELEASE
4888# ifdef FEAT_MOUSE_XTERM
4889 && wheel_code == 0
4890# endif
4891 )
4892 {
4893 /*
4894 * If we get a mouse drag or release event when
4895 * there is no mouse button held down (held_button ==
4896 * MOUSE_RELEASE), produce a K_IGNORE below.
4897 * (can happen when you hold down two buttons
4898 * and then let them go, or click in the menu bar, but not
4899 * on a menu, and drag into the text).
4900 */
4901 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
4902 is_drag = TRUE;
4903 current_button = held_button;
4904 }
4905 else if (wheel_code == 0)
4906 {
4907# ifdef CHECK_DOUBLE_CLICK
4908# ifdef FEAT_MOUSE_GPM
4909# ifdef FEAT_GUI
4910 /*
4911 * Only for Unix, when GUI or gpm is not active, we handle
4912 * multi-clicks here.
4913 */
4914 if (gpm_flag == 0 && !gui.in_use)
4915# else
4916 if (gpm_flag == 0)
4917# endif
4918# else
4919# ifdef FEAT_GUI
4920 if (!gui.in_use)
4921# endif
4922# endif
4923 {
4924 /*
4925 * Compute the time elapsed since the previous mouse click.
4926 */
4927 gettimeofday(&mouse_time, NULL);
4928 timediff = (mouse_time.tv_usec
4929 - orig_mouse_time.tv_usec) / 1000;
4930 if (timediff < 0)
4931 --orig_mouse_time.tv_sec;
4932 timediff += (mouse_time.tv_sec
4933 - orig_mouse_time.tv_sec) * 1000;
4934 orig_mouse_time = mouse_time;
4935 if (mouse_code == orig_mouse_code
4936 && timediff < p_mouset
4937 && orig_num_clicks != 4
4938 && orig_mouse_col == mouse_col
4939 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004940 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004942 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004944 )
4945#ifdef FEAT_WINDOWS
4946 /* Double click in tab pages line also works
4947 * when window contents changes. */
4948 || (mouse_row == 0 && firstwin->w_winrow > 0)
4949#endif
4950 )
4951 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 ++orig_num_clicks;
4953 else
4954 orig_num_clicks = 1;
4955 orig_mouse_col = mouse_col;
4956 orig_mouse_row = mouse_row;
4957 orig_topline = curwin->w_topline;
4958#ifdef FEAT_DIFF
4959 orig_topfill = curwin->w_topfill;
4960#endif
4961 }
4962# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
4963 else
4964 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
4965# endif
4966# else
4967 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
4968# endif
4969 is_click = TRUE;
4970 orig_mouse_code = mouse_code;
4971 }
4972 if (!is_drag)
4973 held_button = mouse_code & MOUSE_CLICK_MASK;
4974
4975 /*
4976 * Translate the actual mouse event into a pseudo mouse event.
4977 * First work out what modifiers are to be used.
4978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 if (orig_mouse_code & MOUSE_SHIFT)
4980 modifiers |= MOD_MASK_SHIFT;
4981 if (orig_mouse_code & MOUSE_CTRL)
4982 modifiers |= MOD_MASK_CTRL;
4983 if (orig_mouse_code & MOUSE_ALT)
4984 modifiers |= MOD_MASK_ALT;
4985 if (orig_num_clicks == 2)
4986 modifiers |= MOD_MASK_2CLICK;
4987 else if (orig_num_clicks == 3)
4988 modifiers |= MOD_MASK_3CLICK;
4989 else if (orig_num_clicks == 4)
4990 modifiers |= MOD_MASK_4CLICK;
4991
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 /* Work out our pseudo mouse event */
4993 key_name[0] = (int)KS_EXTRA;
4994 if (wheel_code != 0)
Bram Moolenaar5074e302010-07-18 14:26:11 +02004995 {
4996 if (wheel_code & MOUSE_CTRL)
4997 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02004998 if (wheel_code & MOUSE_ALT)
4999 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 key_name[1] = (wheel_code & 1)
5001 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 else
5004 key_name[1] = get_pseudo_mouse_code(current_button,
5005 is_click, is_drag);
5006 }
5007#endif /* FEAT_MOUSE */
5008
5009#ifdef FEAT_GUI
5010 /*
5011 * If using the GUI, then we get menu and scrollbar events.
5012 *
5013 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5014 * four bytes which are to be taken as a pointer to the vimmenu_T
5015 * structure.
5016 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005017 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005018 * is one byte with the tab index.
5019 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5021 * by one byte representing the scrollbar number, and then four bytes
5022 * representing a long_u which is the new value of the scrollbar.
5023 *
5024 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5025 * KE_FILLER followed by four bytes representing a long_u which is the
5026 * new value of the scrollbar.
5027 */
5028# ifdef FEAT_MENU
5029 else if (key_name[0] == (int)KS_MENU)
5030 {
5031 long_u val;
5032
5033 num_bytes = get_long_from_buf(tp + slen, &val);
5034 if (num_bytes == -1)
5035 return -1;
5036 current_menu = (vimmenu_T *)val;
5037 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005038
5039 /* The menu may have been deleted right after it was used, check
5040 * for that. */
5041 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5042 {
5043 key_name[0] = KS_EXTRA;
5044 key_name[1] = (int)KE_IGNORE;
5045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
5047# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005048# ifdef FEAT_GUI_TABLINE
5049 else if (key_name[0] == (int)KS_TABLINE)
5050 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005051 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005052 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5053 if (num_bytes == -1)
5054 return -1;
5055 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005056 if (current_tab == 255) /* -1 in a byte gives 255 */
5057 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005058 slen += num_bytes;
5059 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005060 else if (key_name[0] == (int)KS_TABMENU)
5061 {
5062 /* Selecting tabline tab or using its menu. */
5063 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5064 if (num_bytes == -1)
5065 return -1;
5066 current_tab = (int)bytes[0];
5067 current_tabmenu = (int)bytes[1];
5068 slen += num_bytes;
5069 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071# ifndef USE_ON_FLY_SCROLL
5072 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5073 {
5074 long_u val;
5075
5076 /* Get the last scrollbar event in the queue of the same type */
5077 j = 0;
5078 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5079 && tp[j + 2] != NUL; ++i)
5080 {
5081 j += 3;
5082 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5083 if (num_bytes == -1)
5084 break;
5085 if (i == 0)
5086 current_scrollbar = (int)bytes[0];
5087 else if (current_scrollbar != (int)bytes[0])
5088 break;
5089 j += num_bytes;
5090 num_bytes = get_long_from_buf(tp + j, &val);
5091 if (num_bytes == -1)
5092 break;
5093 scrollbar_value = val;
5094 j += num_bytes;
5095 slen = j;
5096 }
5097 if (i == 0) /* not enough characters to make one */
5098 return -1;
5099 }
5100 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5101 {
5102 long_u val;
5103
5104 /* Get the last horiz. scrollbar event in the queue */
5105 j = 0;
5106 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5107 && tp[j + 2] != NUL; ++i)
5108 {
5109 j += 3;
5110 num_bytes = get_long_from_buf(tp + j, &val);
5111 if (num_bytes == -1)
5112 break;
5113 scrollbar_value = val;
5114 j += num_bytes;
5115 slen = j;
5116 }
5117 if (i == 0) /* not enough characters to make one */
5118 return -1;
5119 }
5120# endif /* !USE_ON_FLY_SCROLL */
5121#endif /* FEAT_GUI */
5122
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005123 /*
5124 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5125 */
5126 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5127
5128 /*
5129 * Add any modifier codes to our string.
5130 */
5131 new_slen = 0; /* Length of what will replace the termcode */
5132 if (modifiers != 0)
5133 {
5134 /* Some keys have the modifier included. Need to handle that here
5135 * to make mappings work. */
5136 key = simplify_key(key, &modifiers);
5137 if (modifiers != 0)
5138 {
5139 string[new_slen++] = K_SPECIAL;
5140 string[new_slen++] = (int)KS_MODIFIER;
5141 string[new_slen++] = modifiers;
5142 }
5143 }
5144
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005146 key_name[0] = KEY2TERMCAP0(key);
5147 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005149 {
5150 /* from ":set <M-b>=xx" */
5151#ifdef FEAT_MBYTE
5152 if (has_mbyte)
5153 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5154 else
5155#endif
5156 string[new_slen++] = key_name[1];
5157 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005158 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5159 && key_name[1] == KE_IGNORE)
5160 {
5161 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5162 * to indicate what happened. */
5163 retval = KEYLEN_REMOVED;
5164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 else
5166 {
5167 string[new_slen++] = K_SPECIAL;
5168 string[new_slen++] = key_name[0];
5169 string[new_slen++] = key_name[1];
5170 }
5171 string[new_slen] = NUL;
5172 extra = new_slen - slen;
5173 if (buf == NULL)
5174 {
5175 if (extra < 0)
5176 /* remove matched chars, taking care of noremap */
5177 del_typebuf(-extra, offset);
5178 else if (extra > 0)
5179 /* insert the extra space we need */
5180 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5181
5182 /*
5183 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5184 * typebuf.tb_buf[]!
5185 */
5186 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5187 (size_t)new_slen);
5188 }
5189 else
5190 {
5191 if (extra < 0)
5192 /* remove matched characters */
5193 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005194 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005196 {
5197 /* Insert the extra space we need. If there is insufficient
5198 * space return -1. */
5199 if (*buflen + extra + new_slen >= bufsize)
5200 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005202 (size_t)(*buflen - offset));
5203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005205 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005207 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 }
5209
5210 return 0; /* no match found */
5211}
5212
5213/*
5214 * Replace any terminal code strings in from[] with the equivalent internal
5215 * vim representation. This is used for the "from" and "to" part of a
5216 * mapping, and the "to" part of a menu command.
5217 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5218 * '<'.
5219 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5220 *
5221 * The replacement is done in result[] and finally copied into allocated
5222 * memory. If this all works well *bufp is set to the allocated memory and a
5223 * pointer to it is returned. If something fails *bufp is set to NULL and from
5224 * is returned.
5225 *
5226 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5227 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5228 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5229 * instead of a CTRL-V.
5230 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005231 char_u *
5232replace_termcodes(from, bufp, from_part, do_lt, special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 char_u *from;
5234 char_u **bufp;
5235 int from_part;
5236 int do_lt; /* also translate <lt> */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005237 int special; /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238{
5239 int i;
5240 int slen;
5241 int key;
5242 int dlen = 0;
5243 char_u *src;
5244 int do_backslash; /* backslash is a special character */
5245 int do_special; /* recognize <> key codes */
5246 int do_key_code; /* recognize raw key codes */
5247 char_u *result; /* buffer for resulting string */
5248
5249 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005250 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5252
5253 /*
5254 * Allocate space for the translation. Worst case a single character is
5255 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5256 */
5257 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5258 if (result == NULL) /* out of memory */
5259 {
5260 *bufp = NULL;
5261 return from;
5262 }
5263
5264 src = from;
5265
5266 /*
5267 * Check for #n at start only: function key n
5268 */
5269 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5270 {
5271 result[dlen++] = K_SPECIAL;
5272 result[dlen++] = 'k';
5273 if (src[1] == '0')
5274 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5275 else
5276 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5277 src += 2;
5278 }
5279
5280 /*
5281 * Copy each byte from *from to result[dlen]
5282 */
5283 while (*src != NUL)
5284 {
5285 /*
5286 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005287 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 */
5289 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5290 {
5291#ifdef FEAT_EVAL
5292 /*
5293 * Replace <SID> by K_SNR <script-nr> _.
5294 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5295 */
5296 if (STRNICMP(src, "<SID>", 5) == 0)
5297 {
5298 if (current_SID <= 0)
5299 EMSG(_(e_usingsid));
5300 else
5301 {
5302 src += 5;
5303 result[dlen++] = K_SPECIAL;
5304 result[dlen++] = (int)KS_EXTRA;
5305 result[dlen++] = (int)KE_SNR;
5306 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5307 dlen += (int)STRLEN(result + dlen);
5308 result[dlen++] = '_';
5309 continue;
5310 }
5311 }
5312#endif
5313
5314 slen = trans_special(&src, result + dlen, TRUE);
5315 if (slen)
5316 {
5317 dlen += slen;
5318 continue;
5319 }
5320 }
5321
5322 /*
5323 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5324 * Note that this is also checked after replacing the <> form.
5325 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5326 * it could be a character in the file.
5327 */
5328 if (do_key_code)
5329 {
5330 i = find_term_bykeys(src);
5331 if (i >= 0)
5332 {
5333 result[dlen++] = K_SPECIAL;
5334 result[dlen++] = termcodes[i].name[0];
5335 result[dlen++] = termcodes[i].name[1];
5336 src += termcodes[i].len;
5337 /* If terminal code matched, continue after it. */
5338 continue;
5339 }
5340 }
5341
5342#ifdef FEAT_EVAL
5343 if (do_special)
5344 {
5345 char_u *p, *s, len;
5346
5347 /*
5348 * Replace <Leader> by the value of "mapleader".
5349 * Replace <LocalLeader> by the value of "maplocalleader".
5350 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5351 */
5352 if (STRNICMP(src, "<Leader>", 8) == 0)
5353 {
5354 len = 8;
5355 p = get_var_value((char_u *)"g:mapleader");
5356 }
5357 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5358 {
5359 len = 13;
5360 p = get_var_value((char_u *)"g:maplocalleader");
5361 }
5362 else
5363 {
5364 len = 0;
5365 p = NULL;
5366 }
5367 if (len != 0)
5368 {
5369 /* Allow up to 8 * 6 characters for "mapleader". */
5370 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5371 s = (char_u *)"\\";
5372 else
5373 s = p;
5374 while (*s != NUL)
5375 result[dlen++] = *s++;
5376 src += len;
5377 continue;
5378 }
5379 }
5380#endif
5381
5382 /*
5383 * Remove CTRL-V and ignore the next character.
5384 * For "from" side the CTRL-V at the end is included, for the "to"
5385 * part it is removed.
5386 * If 'cpoptions' does not contain 'B', also accept a backslash.
5387 */
5388 key = *src;
5389 if (key == Ctrl_V || (do_backslash && key == '\\'))
5390 {
5391 ++src; /* skip CTRL-V or backslash */
5392 if (*src == NUL)
5393 {
5394 if (from_part)
5395 result[dlen++] = key;
5396 break;
5397 }
5398 }
5399
5400#ifdef FEAT_MBYTE
5401 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005402 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403#endif
5404 {
5405 /*
5406 * If the character is K_SPECIAL, replace it with K_SPECIAL
5407 * KS_SPECIAL KE_FILLER.
5408 * If compiled with the GUI replace CSI with K_CSI.
5409 */
5410 if (*src == K_SPECIAL)
5411 {
5412 result[dlen++] = K_SPECIAL;
5413 result[dlen++] = KS_SPECIAL;
5414 result[dlen++] = KE_FILLER;
5415 }
5416# ifdef FEAT_GUI
5417 else if (*src == CSI)
5418 {
5419 result[dlen++] = K_SPECIAL;
5420 result[dlen++] = KS_EXTRA;
5421 result[dlen++] = (int)KE_CSI;
5422 }
5423# endif
5424 else
5425 result[dlen++] = *src;
5426 ++src;
5427 }
5428 }
5429 result[dlen] = NUL;
5430
5431 /*
5432 * Copy the new string to allocated memory.
5433 * If this fails, just return from.
5434 */
5435 if ((*bufp = vim_strsave(result)) != NULL)
5436 from = *bufp;
5437 vim_free(result);
5438 return from;
5439}
5440
5441/*
5442 * Find a termcode with keys 'src' (must be NUL terminated).
5443 * Return the index in termcodes[], or -1 if not found.
5444 */
5445 int
5446find_term_bykeys(src)
5447 char_u *src;
5448{
5449 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005450 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451
5452 for (i = 0; i < tc_len; ++i)
5453 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005454 if (slen == termcodes[i].len
5455 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 return i;
5457 }
5458 return -1;
5459}
5460
5461/*
5462 * Gather the first characters in the terminal key codes into a string.
5463 * Used to speed up check_termcode().
5464 */
5465 static void
5466gather_termleader()
5467{
5468 int i;
5469 int len = 0;
5470
5471#ifdef FEAT_GUI
5472 if (gui.in_use)
5473 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5474#endif
5475#ifdef FEAT_TERMRESPONSE
5476 if (check_for_codes)
5477 termleader[len++] = DCS; /* the termcode response starts with DCS
5478 in 8-bit mode */
5479#endif
5480 termleader[len] = NUL;
5481
5482 for (i = 0; i < tc_len; ++i)
5483 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5484 {
5485 termleader[len++] = termcodes[i].code[0];
5486 termleader[len] = NUL;
5487 }
5488
5489 need_gather = FALSE;
5490}
5491
5492/*
5493 * Show all termcodes (for ":set termcap")
5494 * This code looks a lot like showoptions(), but is different.
5495 */
5496 void
5497show_termcodes()
5498{
5499 int col;
5500 int *items;
5501 int item_count;
5502 int run;
5503 int row, rows;
5504 int cols;
5505 int i;
5506 int len;
5507
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005508#define INC3 27 /* try to make three columns */
5509#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510#define GAP 2 /* spaces between columns */
5511
5512 if (tc_len == 0) /* no terminal codes (must be GUI) */
5513 return;
5514 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5515 if (items == NULL)
5516 return;
5517
5518 /* Highlight title */
5519 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5520
5521 /*
5522 * do the loop two times:
5523 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005524 * 2. display the medium items (medium length strings)
5525 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005527 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 {
5529 /*
5530 * collect the items in items[]
5531 */
5532 item_count = 0;
5533 for (i = 0; i < tc_len; i++)
5534 {
5535 len = show_one_termcode(termcodes[i].name,
5536 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005537 if (len <= INC3 - GAP ? run == 1
5538 : len <= INC2 - GAP ? run == 2
5539 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 items[item_count++] = i;
5541 }
5542
5543 /*
5544 * display the items
5545 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005546 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005548 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 if (cols == 0)
5550 cols = 1;
5551 rows = (item_count + cols - 1) / cols;
5552 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005553 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 rows = item_count;
5555 for (row = 0; row < rows && !got_int; ++row)
5556 {
5557 msg_putchar('\n'); /* go to next line */
5558 if (got_int) /* 'q' typed in more */
5559 break;
5560 col = 0;
5561 for (i = row; i < item_count; i += rows)
5562 {
5563 msg_col = col; /* make columns */
5564 show_one_termcode(termcodes[items[i]].name,
5565 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005566 if (run == 2)
5567 col += INC2;
5568 else
5569 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571 out_flush();
5572 ui_breakcheck();
5573 }
5574 }
5575 vim_free(items);
5576}
5577
5578/*
5579 * Show one termcode entry.
5580 * Output goes into IObuff[]
5581 */
5582 int
5583show_one_termcode(name, code, printit)
5584 char_u *name;
5585 char_u *code;
5586 int printit;
5587{
5588 char_u *p;
5589 int len;
5590
5591 if (name[0] > '~')
5592 {
5593 IObuff[0] = ' ';
5594 IObuff[1] = ' ';
5595 IObuff[2] = ' ';
5596 IObuff[3] = ' ';
5597 }
5598 else
5599 {
5600 IObuff[0] = 't';
5601 IObuff[1] = '_';
5602 IObuff[2] = name[0];
5603 IObuff[3] = name[1];
5604 }
5605 IObuff[4] = ' ';
5606
5607 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5608 if (p[1] != 't')
5609 STRCPY(IObuff + 5, p);
5610 else
5611 IObuff[5] = NUL;
5612 len = (int)STRLEN(IObuff);
5613 do
5614 IObuff[len++] = ' ';
5615 while (len < 17);
5616 IObuff[len] = NUL;
5617 if (code == NULL)
5618 len += 4;
5619 else
5620 len += vim_strsize(code);
5621
5622 if (printit)
5623 {
5624 msg_puts(IObuff);
5625 if (code == NULL)
5626 msg_puts((char_u *)"NULL");
5627 else
5628 msg_outtrans(code);
5629 }
5630 return len;
5631}
5632
5633#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5634/*
5635 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5636 * termcap codes from the terminal itself.
5637 * We get them one by one to avoid a very long response string.
5638 */
5639static int xt_index_in = 0;
5640static int xt_index_out = 0;
5641
5642 static void
5643req_codes_from_term()
5644{
5645 xt_index_out = 0;
5646 xt_index_in = 0;
5647 req_more_codes_from_term();
5648}
5649
5650 static void
5651req_more_codes_from_term()
5652{
5653 char buf[11];
5654 int old_idx = xt_index_out;
5655
5656 /* Don't do anything when going to exit. */
5657 if (exiting)
5658 return;
5659
5660 /* Send up to 10 more requests out than we received. Avoid sending too
5661 * many, there can be a buffer overflow somewhere. */
5662 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5663 {
5664 sprintf(buf, "\033P+q%02x%02x\033\\",
5665 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5666 out_str_nf((char_u *)buf);
5667 ++xt_index_out;
5668 }
5669
5670 /* Send the codes out right away. */
5671 if (xt_index_out != old_idx)
5672 out_flush();
5673}
5674
5675/*
5676 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5677 * A "0" instead of the "1" indicates a code that isn't supported.
5678 * Both <name> and <string> are encoded in hex.
5679 * "code" points to the "0" or "1".
5680 */
5681 static void
5682got_code_from_term(code, len)
5683 char_u *code;
5684 int len;
5685{
5686#define XT_LEN 100
5687 char_u name[3];
5688 char_u str[XT_LEN];
5689 int i;
5690 int j = 0;
5691 int c;
5692
5693 /* A '1' means the code is supported, a '0' means it isn't.
5694 * When half the length is > XT_LEN we can't use it.
5695 * Our names are currently all 2 characters. */
5696 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5697 {
5698 /* Get the name from the response and find it in the table. */
5699 name[0] = hexhex2nr(code + 3);
5700 name[1] = hexhex2nr(code + 5);
5701 name[2] = NUL;
5702 for (i = 0; key_names[i] != NULL; ++i)
5703 {
5704 if (STRCMP(key_names[i], name) == 0)
5705 {
5706 xt_index_in = i;
5707 break;
5708 }
5709 }
5710 if (key_names[i] != NULL)
5711 {
5712 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5713 str[j++] = c;
5714 str[j] = NUL;
5715 if (name[0] == 'C' && name[1] == 'o')
5716 {
5717 /* Color count is not a key code. */
5718 i = atoi((char *)str);
5719 if (i != t_colors)
5720 {
5721 /* Nr of colors changed, initialize highlighting and
Bram Moolenaar238a5642006-02-21 22:12:05 +00005722 * redraw everything. This causes a redraw, which usually
5723 * clears the message. Try keeping the message if it
5724 * might work. */
5725 set_keep_msg_from_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 set_color_count(i);
5727 init_highlight(TRUE, FALSE);
5728 redraw_later(CLEAR);
5729 }
5730 }
5731 else
5732 {
5733 /* First delete any existing entry with the same code. */
5734 i = find_term_bykeys(str);
5735 if (i >= 0)
5736 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005737 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739 }
5740 }
5741
5742 /* May request more codes now that we received one. */
5743 ++xt_index_in;
5744 req_more_codes_from_term();
5745}
5746
5747/*
5748 * Check if there are any unanswered requests and deal with them.
5749 * This is called before starting an external program or getting direct
5750 * keyboard input. We don't want responses to be send to that program or
5751 * handled as typed text.
5752 */
5753 static void
5754check_for_codes_from_term()
5755{
5756 int c;
5757
5758 /* If no codes requested or all are answered, no need to wait. */
5759 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5760 return;
5761
5762 /* Vgetc() will check for and handle any response.
5763 * Keep calling vpeekc() until we don't get any responses. */
5764 ++no_mapping;
5765 ++allow_keys;
5766 for (;;)
5767 {
5768 c = vpeekc();
5769 if (c == NUL) /* nothing available */
5770 break;
5771
5772 /* If a response is recognized it's replaced with K_IGNORE, must read
5773 * it from the input stream. If there is no K_IGNORE we can't do
5774 * anything, break here (there might be some responses further on, but
5775 * we don't want to throw away any typed chars). */
5776 if (c != K_SPECIAL && c != K_IGNORE)
5777 break;
5778 c = vgetc();
5779 if (c != K_IGNORE)
5780 {
5781 vungetc(c);
5782 break;
5783 }
5784 }
5785 --no_mapping;
5786 --allow_keys;
5787}
5788#endif
5789
5790#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5791/*
5792 * Translate an internal mapping/abbreviation representation into the
5793 * corresponding external one recognized by :map/:abbrev commands;
5794 * respects the current B/k/< settings of 'cpoption'.
5795 *
5796 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00005797 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 *
5799 * It uses a growarray to build the translation string since the
5800 * latter can be wider than the original description. The caller has to
5801 * free the string afterwards.
5802 *
5803 * Returns NULL when there is a problem.
5804 */
5805 char_u *
5806translate_mapping(str, expmap)
5807 char_u *str;
5808 int expmap; /* TRUE when expanding mappings on command-line */
5809{
5810 garray_T ga;
5811 int c;
5812 int modifiers;
5813 int cpo_bslash;
5814 int cpo_special;
5815 int cpo_keycode;
5816
5817 ga_init(&ga);
5818 ga.ga_itemsize = 1;
5819 ga.ga_growsize = 40;
5820
5821 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
5822 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
5823 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5824
5825 for (; *str; ++str)
5826 {
5827 c = *str;
5828 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5829 {
5830 modifiers = 0;
5831 if (str[1] == KS_MODIFIER)
5832 {
5833 str++;
5834 modifiers = *++str;
5835 c = *++str;
5836 }
5837 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
5838 {
5839 int i;
5840
5841 /* try to find special key in termcodes */
5842 for (i = 0; i < tc_len; ++i)
5843 if (termcodes[i].name[0] == str[1]
5844 && termcodes[i].name[1] == str[2])
5845 break;
5846 if (i < tc_len)
5847 {
5848 ga_concat(&ga, termcodes[i].code);
5849 str += 2;
5850 continue; /* for (str) */
5851 }
5852 }
5853 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5854 {
5855 if (expmap && cpo_special)
5856 {
5857 ga_clear(&ga);
5858 return NULL;
5859 }
5860 c = TO_SPECIAL(str[1], str[2]);
5861 if (c == K_ZERO) /* display <Nul> as ^@ */
5862 c = NUL;
5863 str += 2;
5864 }
5865 if (IS_SPECIAL(c) || modifiers) /* special key */
5866 {
5867 if (expmap && cpo_special)
5868 {
5869 ga_clear(&ga);
5870 return NULL;
5871 }
5872 ga_concat(&ga, get_special_key_name(c, modifiers));
5873 continue; /* for (str) */
5874 }
5875 }
5876 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
5877 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
5878 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
5879 if (c)
5880 ga_append(&ga, c);
5881 }
5882 ga_append(&ga, NUL);
5883 return (char_u *)(ga.ga_data);
5884}
5885#endif
5886
5887#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
5888static char ksme_str[20];
5889static char ksmr_str[20];
5890static char ksmd_str[20];
5891
5892/*
5893 * For Win32 console: update termcap codes for existing console attributes.
5894 */
5895 void
5896update_tcap(attr)
5897 int attr;
5898{
5899 struct builtin_term *p;
5900
5901 p = find_builtin_term(DEFAULT_TERM);
5902 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
5903 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
5904 attr | 0x08); /* FOREGROUND_INTENSITY */
5905 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
5906 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
5907
5908 while (p->bt_string != NULL)
5909 {
5910 if (p->bt_entry == (int)KS_ME)
5911 p->bt_string = &ksme_str[0];
5912 else if (p->bt_entry == (int)KS_MR)
5913 p->bt_string = &ksmr_str[0];
5914 else if (p->bt_entry == (int)KS_MD)
5915 p->bt_string = &ksmd_str[0];
5916 ++p;
5917 }
5918}
5919#endif