blob: 0d797bcd668797403ee44bd9aa9d329d687f989b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 *
11 * term.c: functions for controlling the terminal
12 *
13 * primitive termcap support for Amiga, MSDOS, and Win32 included
14 *
15 * NOTE: padding and variable substitution is not performed,
16 * when compiling without HAVE_TGETENT, we use tputs() and tgoto() dummies.
17 */
18
19/*
20 * Some systems have a prototype for tgetstr() with (char *) instead of
21 * (char **). This define removes that prototype. We include our own prototype
22 * below.
23 */
24
25#define tgetstr tgetstr_defined_wrong
26#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
30# include <termios.h> /* seems to be required for some Linux */
31# endif
32# ifdef HAVE_TERMCAP_H
33# include <termcap.h>
34# endif
35
36/*
37 * A few linux systems define outfuntype in termcap.h to be used as the third
38 * argument for tputs().
39 */
40# ifdef VMS
41# define TPUTSFUNCAST
42# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
46# define TPUTSFUNCAST (int (*)())
47# endif
48# endif
49#endif
50
51#undef tgetstr
52
53/*
54 * Here are the builtin termcap entries. They are not stored as complete
Bram Moolenaare60acc12011-05-10 16:41:25 +020055 * structures with all entries, as such a structure is too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 *
57 * The entries are compact, therefore they normally are included even when
58 * HAVE_TGETENT is defined. When HAVE_TGETENT is defined, the builtin entries
59 * can be accessed with "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
60 *
61 * Each termcap is a list of builtin_term structures. It always starts with
62 * KS_NAME, which separates the entries. See parse_builtin_tcap() for all
63 * details.
64 * bt_entry is either a KS_xxx code (>= 0), or a K_xxx code.
65 *
66 * Entries marked with "guessed" may be wrong.
67 */
68struct builtin_term
69{
70 int bt_entry;
71 char *bt_string;
72};
73
74/* start of keys that are not directly used by Vim but can be mapped */
75#define BT_EXTRA_KEYS 0x101
76
77static struct builtin_term *find_builtin_term __ARGS((char_u *name));
78static void parse_builtin_tcap __ARGS((char_u *s));
79static void term_color __ARGS((char_u *s, int n));
80static void gather_termleader __ARGS((void));
81#ifdef FEAT_TERMRESPONSE
82static void req_codes_from_term __ARGS((void));
83static void req_more_codes_from_term __ARGS((void));
84static void got_code_from_term __ARGS((char_u *code, int len));
85static void check_for_codes_from_term __ARGS((void));
86#endif
87#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +000088 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
89 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000090static int get_bytes_from_buf __ARGS((char_u *, char_u *, int));
91#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000092static void del_termcode_idx __ARGS((int idx));
93static int term_is_builtin __ARGS((char_u *name));
94static int term_7to8bit __ARGS((char_u *p));
95#ifdef FEAT_TERMRESPONSE
96static void switch_to_8bit __ARGS((void));
97#endif
98
99#ifdef HAVE_TGETENT
100static char_u *tgetent_error __ARGS((char_u *, char_u *));
101
102/*
103 * Here is our own prototype for tgetstr(), any prototypes from the include
104 * files have been disabled by the define at the start of this file.
105 */
106char *tgetstr __ARGS((char *, char **));
107
108# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200109 /* Change this to "if 1" to debug what happens with termresponse. */
110# if 0
111# define DEBUG_TERMRESPONSE
112 static void log_tr(char *msg);
113# define LOG_TR(msg) log_tr(msg)
114# else
115# define LOG_TR(msg)
116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117/* Request Terminal Version status: */
118# define CRV_GET 1 /* send T_CRV when switched to RAW mode */
119# define CRV_SENT 2 /* did send T_CRV, waiting for answer */
120# define CRV_GOT 3 /* received T_CRV response */
121static int crv_status = CRV_GET;
Bram Moolenaar9584b312013-03-13 19:29:28 +0100122/* Request Cursor position report: */
123# define U7_GET 1 /* send T_U7 when switched to RAW mode */
124# define U7_SENT 2 /* did send T_U7, waiting for answer */
125# define U7_GOT 3 /* received T_U7 response */
126static int u7_status = U7_GET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127# endif
128
129/*
130 * Don't declare these variables if termcap.h contains them.
131 * Autoconf checks if these variables should be declared extern (not all
132 * systems have them).
133 * Some versions define ospeed to be speed_t, but that is incompatible with
134 * BSD, where ospeed is short and speed_t is long.
135 */
136# ifndef HAVE_OSPEED
137# ifdef OSPEED_EXTERN
138extern short ospeed;
139# else
140short ospeed;
141# endif
142# endif
143# ifndef HAVE_UP_BC_PC
144# ifdef UP_BC_PC_EXTERN
145extern char *UP, *BC, PC;
146# else
147char *UP, *BC, PC;
148# endif
149# endif
150
151# define TGETSTR(s, p) vim_tgetstr((s), (p))
152# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
153static char_u *vim_tgetstr __ARGS((char *s, char_u **pp));
154#endif /* HAVE_TGETENT */
155
156static int detected_8bit = FALSE; /* detected 8-bit terminal */
157
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000158static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159{
160
161#if defined(FEAT_GUI)
162/*
163 * GUI pseudo term-cap.
164 */
165 {(int)KS_NAME, "gui"},
166 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
167 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
168# ifdef TERMINFO
169 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
170# else
171 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
172# endif
173 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
174# ifdef TERMINFO
175 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
176 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
177# ifdef FEAT_VERTSPLIT
178 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
179# endif
180# else
181 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
182 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
183# ifdef FEAT_VERTSPLIT
184 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
185# endif
186# endif
187 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
188 /* attributes switched on with 'h', off with * 'H' */
189 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
190 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
191 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
192 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
193 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
194 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
195 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000196 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
197 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
199 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
200 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
201 {(int)KS_MS, "y"},
202 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100203 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 {(int)KS_LE, "\b"}, /* cursor-left = BS */
205 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
206# ifdef TERMINFO
207 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
208# else
209 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
210# endif
211 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000212 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#endif
214
215#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
217# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
218/*
219 * Amiga console window, default for Amiga
220 */
221 {(int)KS_NAME, "amiga"},
222 {(int)KS_CE, "\033[K"},
223 {(int)KS_CD, "\033[J"},
224 {(int)KS_AL, "\033[L"},
225# ifdef TERMINFO
226 {(int)KS_CAL, "\033[%p1%dL"},
227# else
228 {(int)KS_CAL, "\033[%dL"},
229# endif
230 {(int)KS_DL, "\033[M"},
231# ifdef TERMINFO
232 {(int)KS_CDL, "\033[%p1%dM"},
233# else
234 {(int)KS_CDL, "\033[%dM"},
235# endif
236 {(int)KS_CL, "\014"},
237 {(int)KS_VI, "\033[0 p"},
238 {(int)KS_VE, "\033[1 p"},
239 {(int)KS_ME, "\033[0m"},
240 {(int)KS_MR, "\033[7m"},
241 {(int)KS_MD, "\033[1m"},
242 {(int)KS_SE, "\033[0m"},
243 {(int)KS_SO, "\033[33m"},
244 {(int)KS_US, "\033[4m"},
245 {(int)KS_UE, "\033[0m"},
246 {(int)KS_CZH, "\033[3m"},
247 {(int)KS_CZR, "\033[0m"},
248#if defined(__MORPHOS__) || defined(__AROS__)
249 {(int)KS_CCO, "8"}, /* allow 8 colors */
250# ifdef TERMINFO
251 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
252 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
253# else
254 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
255 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
256# endif
257 {(int)KS_OP, "\033[m"}, /* reset colors */
258#endif
259 {(int)KS_MS, "y"},
260 {(int)KS_UT, "y"}, /* guessed */
261 {(int)KS_LE, "\b"},
262# ifdef TERMINFO
263 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
264# else
265 {(int)KS_CM, "\033[%i%d;%dH"},
266# endif
267#if defined(__MORPHOS__)
268 {(int)KS_SR, "\033M"},
269#endif
270# ifdef TERMINFO
271 {(int)KS_CRI, "\033[%p1%dC"},
272# else
273 {(int)KS_CRI, "\033[%dC"},
274# endif
275 {K_UP, "\233A"},
276 {K_DOWN, "\233B"},
277 {K_LEFT, "\233D"},
278 {K_RIGHT, "\233C"},
279 {K_S_UP, "\233T"},
280 {K_S_DOWN, "\233S"},
281 {K_S_LEFT, "\233 A"},
282 {K_S_RIGHT, "\233 @"},
283 {K_S_TAB, "\233Z"},
284 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
285 {K_F2, "\233\061~"},
286 {K_F3, "\233\062~"},
287 {K_F4, "\233\063~"},
288 {K_F5, "\233\064~"},
289 {K_F6, "\233\065~"},
290 {K_F7, "\233\066~"},
291 {K_F8, "\233\067~"},
292 {K_F9, "\233\070~"},
293 {K_F10, "\233\071~"},
294 {K_S_F1, "\233\061\060~"},
295 {K_S_F2, "\233\061\061~"},
296 {K_S_F3, "\233\061\062~"},
297 {K_S_F4, "\233\061\063~"},
298 {K_S_F5, "\233\061\064~"},
299 {K_S_F6, "\233\061\065~"},
300 {K_S_F7, "\233\061\066~"},
301 {K_S_F8, "\233\061\067~"},
302 {K_S_F9, "\233\061\070~"},
303 {K_S_F10, "\233\061\071~"},
304 {K_HELP, "\233?~"},
305 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
306 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
307 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
308 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
309 {K_END, "\233\064\065~"}, /* 101 key keyboard */
310
311 {BT_EXTRA_KEYS, ""},
312 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
313 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
314 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
315# endif
316
317# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
318/*
319 * almost standard ANSI terminal, default for bebox
320 */
321 {(int)KS_NAME, "beos-ansi"},
322 {(int)KS_CE, "\033[K"},
323 {(int)KS_CD, "\033[J"},
324 {(int)KS_AL, "\033[L"},
325# ifdef TERMINFO
326 {(int)KS_CAL, "\033[%p1%dL"},
327# else
328 {(int)KS_CAL, "\033[%dL"},
329# endif
330 {(int)KS_DL, "\033[M"},
331# ifdef TERMINFO
332 {(int)KS_CDL, "\033[%p1%dM"},
333# else
334 {(int)KS_CDL, "\033[%dM"},
335# endif
336#ifdef BEOS_PR_OR_BETTER
337# ifdef TERMINFO
338 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
339# else
340 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
341# endif
342#endif
343 {(int)KS_CL, "\033[H\033[2J"},
344#ifdef notyet
345 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
346 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
347#endif
348 {(int)KS_ME, "\033[m"}, /* normal mode */
349 {(int)KS_MR, "\033[7m"}, /* reverse */
350 {(int)KS_MD, "\033[1m"}, /* bold */
351 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
352 {(int)KS_SE, "\033[m"}, /* standout end */
353 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
354 {(int)KS_CZR, "\033[m"}, /* italic end */
355 {(int)KS_US, "\033[4m"}, /* underscore mode */
356 {(int)KS_UE, "\033[m"}, /* underscore end */
357 {(int)KS_CCO, "8"}, /* allow 8 colors */
358# ifdef TERMINFO
359 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
360 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
361# else
362 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
363 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
364# endif
365 {(int)KS_OP, "\033[m"}, /* reset colors */
366 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
367 {(int)KS_UT, "y"}, /* guessed */
368 {(int)KS_LE, "\b"},
369# ifdef TERMINFO
370 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
371# else
372 {(int)KS_CM, "\033[%i%d;%dH"},
373# endif
374 {(int)KS_SR, "\033M"},
375# ifdef TERMINFO
376 {(int)KS_CRI, "\033[%p1%dC"},
377# else
378 {(int)KS_CRI, "\033[%dC"},
379# endif
380#if defined(BEOS_DR8)
381 {(int)KS_DB, ""}, /* hack! see screen.c */
382#endif
383
384 {K_UP, "\033[A"},
385 {K_DOWN, "\033[B"},
386 {K_LEFT, "\033[D"},
387 {K_RIGHT, "\033[C"},
388# endif
389
390# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) || defined(__EMX__)
391/*
392 * standard ANSI terminal, default for unix
393 */
394 {(int)KS_NAME, "ansi"},
395 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
396 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
397# ifdef TERMINFO
398 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
399# else
400 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
401# endif
402 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
403# ifdef TERMINFO
404 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
405# else
406 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
407# endif
408 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
409 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
410 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
411 {(int)KS_MS, "y"},
412 {(int)KS_UT, "y"}, /* guessed */
413 {(int)KS_LE, "\b"},
414# ifdef TERMINFO
415 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
416# else
417 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
418# endif
419# ifdef TERMINFO
420 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
421# else
422 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
423# endif
424# endif
425
426# if defined(MSDOS) || defined(ALL_BUILTIN_TCAPS) || defined(__EMX__)
427/*
428 * These codes are valid when nansi.sys or equivalent has been installed.
429 * Function keys on a PC are preceded with a NUL. These are converted into
430 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
431 * CTRL-arrow is used instead of SHIFT-arrow.
432 */
433#ifdef __EMX__
434 {(int)KS_NAME, "os2ansi"},
435#else
436 {(int)KS_NAME, "pcansi"},
437 {(int)KS_DL, "\033[M"},
438 {(int)KS_AL, "\033[L"},
439#endif
440 {(int)KS_CE, "\033[K"},
441 {(int)KS_CL, "\033[2J"},
442 {(int)KS_ME, "\033[0m"},
443 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
444 {(int)KS_MD, "\033[1m"}, /* bold: white text */
445 {(int)KS_SE, "\033[0m"}, /* standout end */
446 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
447 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
448 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
449 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
450 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
451 {(int)KS_CCO, "8"}, /* allow 8 colors */
452# ifdef TERMINFO
453 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
454 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
455# else
456 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
457 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
458# endif
459 {(int)KS_OP, "\033[0m"}, /* reset colors */
460 {(int)KS_MS, "y"},
461 {(int)KS_UT, "y"}, /* guessed */
462 {(int)KS_LE, "\b"},
463# ifdef TERMINFO
464 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
465# else
466 {(int)KS_CM, "\033[%i%d;%dH"},
467# endif
468# ifdef TERMINFO
469 {(int)KS_CRI, "\033[%p1%dC"},
470# else
471 {(int)KS_CRI, "\033[%dC"},
472# endif
473 {K_UP, "\316H"},
474 {K_DOWN, "\316P"},
475 {K_LEFT, "\316K"},
476 {K_RIGHT, "\316M"},
477 {K_S_LEFT, "\316s"},
478 {K_S_RIGHT, "\316t"},
479 {K_F1, "\316;"},
480 {K_F2, "\316<"},
481 {K_F3, "\316="},
482 {K_F4, "\316>"},
483 {K_F5, "\316?"},
484 {K_F6, "\316@"},
485 {K_F7, "\316A"},
486 {K_F8, "\316B"},
487 {K_F9, "\316C"},
488 {K_F10, "\316D"},
489 {K_F11, "\316\205"}, /* guessed */
490 {K_F12, "\316\206"}, /* guessed */
491 {K_S_F1, "\316T"},
492 {K_S_F2, "\316U"},
493 {K_S_F3, "\316V"},
494 {K_S_F4, "\316W"},
495 {K_S_F5, "\316X"},
496 {K_S_F6, "\316Y"},
497 {K_S_F7, "\316Z"},
498 {K_S_F8, "\316["},
499 {K_S_F9, "\316\\"},
500 {K_S_F10, "\316]"},
501 {K_S_F11, "\316\207"}, /* guessed */
502 {K_S_F12, "\316\210"}, /* guessed */
503 {K_INS, "\316R"},
504 {K_DEL, "\316S"},
505 {K_HOME, "\316G"},
506 {K_END, "\316O"},
507 {K_PAGEDOWN, "\316Q"},
508 {K_PAGEUP, "\316I"},
509# endif
510
511# if defined(MSDOS)
512/*
513 * These codes are valid for the pc video. The entries that start with ESC |
514 * are translated into conio calls in os_msdos.c. Default for MSDOS.
515 */
516 {(int)KS_NAME, "pcterm"},
517 {(int)KS_CE, "\033|K"},
518 {(int)KS_AL, "\033|L"},
519 {(int)KS_DL, "\033|M"},
520# ifdef TERMINFO
521 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},
522# ifdef FEAT_VERTSPLIT
523 {(int)KS_CSV, "\033|%i%p1%d;%p2%dV"},
524# endif
525# else
526 {(int)KS_CS, "\033|%i%d;%dr"},
527# ifdef FEAT_VERTSPLIT
528 {(int)KS_CSV, "\033|%i%d;%dV"},
529# endif
530# endif
531 {(int)KS_CL, "\033|J"},
532 {(int)KS_ME, "\033|0m"}, /* normal */
533 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgrey */
534 {(int)KS_MD, "\033|15m"}, /* bold: white text */
535 {(int)KS_SE, "\033|0m"}, /* standout end */
536 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
537 {(int)KS_CZH, "\033|225m"}, /* italic mode: blue text on yellow */
538 {(int)KS_CZR, "\033|0m"}, /* italic mode end */
539 {(int)KS_US, "\033|67m"}, /* underscore mode: cyan text on red */
540 {(int)KS_UE, "\033|0m"}, /* underscore mode end */
541 {(int)KS_CCO, "16"}, /* allow 16 colors */
542# ifdef TERMINFO
543 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
544 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
545# else
546 {(int)KS_CAB, "\033|%db"}, /* set background color */
547 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
548# endif
549 {(int)KS_MS, "y"},
550 {(int)KS_UT, "y"},
551 {(int)KS_LE, "\b"},
552# ifdef TERMINFO
553 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},
554# else
555 {(int)KS_CM, "\033|%i%d;%dH"},
556# endif
557#ifdef DJGPP
558 {(int)KS_VB, "\033|B"}, /* visual bell */
559#endif
560 {K_UP, "\316H"},
561 {K_DOWN, "\316P"},
562 {K_LEFT, "\316K"},
563 {K_RIGHT, "\316M"},
564 {K_S_LEFT, "\316s"},
565 {K_S_RIGHT, "\316t"},
566 {K_S_TAB, "\316\017"},
567 {K_F1, "\316;"},
568 {K_F2, "\316<"},
569 {K_F3, "\316="},
570 {K_F4, "\316>"},
571 {K_F5, "\316?"},
572 {K_F6, "\316@"},
573 {K_F7, "\316A"},
574 {K_F8, "\316B"},
575 {K_F9, "\316C"},
576 {K_F10, "\316D"},
577 {K_F11, "\316\205"},
578 {K_F12, "\316\206"},
579 {K_S_F1, "\316T"},
580 {K_S_F2, "\316U"},
581 {K_S_F3, "\316V"},
582 {K_S_F4, "\316W"},
583 {K_S_F5, "\316X"},
584 {K_S_F6, "\316Y"},
585 {K_S_F7, "\316Z"},
586 {K_S_F8, "\316["},
587 {K_S_F9, "\316\\"},
588 {K_S_F10, "\316]"},
589 {K_S_F11, "\316\207"},
590 {K_S_F12, "\316\210"},
591 {K_INS, "\316R"},
592 {K_DEL, "\316S"},
593 {K_HOME, "\316G"},
594 {K_END, "\316O"},
595 {K_PAGEDOWN, "\316Q"},
596 {K_PAGEUP, "\316I"},
597 {K_KPLUS, "\316N"},
598 {K_KMINUS, "\316J"},
599 {K_KMULTIPLY, "\3167"},
600 {K_K0, "\316\332"},
601 {K_K1, "\316\336"},
602 {K_K2, "\316\342"},
603 {K_K3, "\316\346"},
604 {K_K4, "\316\352"},
605 {K_K5, "\316\356"},
606 {K_K6, "\316\362"},
607 {K_K7, "\316\366"},
608 {K_K8, "\316\372"},
609 {K_K9, "\316\376"},
610# endif
611
612# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS) || defined(__EMX__)
613/*
614 * These codes are valid for the Win32 Console . The entries that start with
615 * ESC | are translated into console calls in os_win32.c. The function keys
616 * are also translated in os_win32.c.
617 */
618 {(int)KS_NAME, "win32"},
619 {(int)KS_CE, "\033|K"}, /* clear to end of line */
620 {(int)KS_AL, "\033|L"}, /* add new blank line */
621# ifdef TERMINFO
622 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
623# else
624 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
625# endif
626 {(int)KS_DL, "\033|M"}, /* delete line */
627# ifdef TERMINFO
628 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
629# else
630 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
631# endif
632 {(int)KS_CL, "\033|J"}, /* clear screen */
633 {(int)KS_CD, "\033|j"}, /* clear to end of display */
634 {(int)KS_VI, "\033|v"}, /* cursor invisible */
635 {(int)KS_VE, "\033|V"}, /* cursor visible */
636
637 {(int)KS_ME, "\033|0m"}, /* normal */
638 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
639 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
640#if 1
641 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
642 {(int)KS_SE, "\033|0m"}, /* standout end */
643#else
644 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
645 {(int)KS_SE, "\033|f"}, /* standout end */
646#endif
647 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
648 {(int)KS_CZR, "\033|0m"}, /* italic end */
649 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
650 {(int)KS_UE, "\033|0m"}, /* underscore end */
651 {(int)KS_CCO, "16"}, /* allow 16 colors */
652# ifdef TERMINFO
653 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
654 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
655# else
656 {(int)KS_CAB, "\033|%db"}, /* set background color */
657 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
658# endif
659
660 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
661 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100662 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 {(int)KS_LE, "\b"},
664# ifdef TERMINFO
665 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
666# else
667 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
668# endif
669 {(int)KS_VB, "\033|B"}, /* visual bell */
670 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
671 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
672# ifdef TERMINFO
673 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
674# else
675 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
676# endif
677
678 {K_UP, "\316H"},
679 {K_DOWN, "\316P"},
680 {K_LEFT, "\316K"},
681 {K_RIGHT, "\316M"},
682 {K_S_UP, "\316\304"},
683 {K_S_DOWN, "\316\317"},
684 {K_S_LEFT, "\316\311"},
685 {K_C_LEFT, "\316s"},
686 {K_S_RIGHT, "\316\313"},
687 {K_C_RIGHT, "\316t"},
688 {K_S_TAB, "\316\017"},
689 {K_F1, "\316;"},
690 {K_F2, "\316<"},
691 {K_F3, "\316="},
692 {K_F4, "\316>"},
693 {K_F5, "\316?"},
694 {K_F6, "\316@"},
695 {K_F7, "\316A"},
696 {K_F8, "\316B"},
697 {K_F9, "\316C"},
698 {K_F10, "\316D"},
699 {K_F11, "\316\205"},
700 {K_F12, "\316\206"},
701 {K_S_F1, "\316T"},
702 {K_S_F2, "\316U"},
703 {K_S_F3, "\316V"},
704 {K_S_F4, "\316W"},
705 {K_S_F5, "\316X"},
706 {K_S_F6, "\316Y"},
707 {K_S_F7, "\316Z"},
708 {K_S_F8, "\316["},
709 {K_S_F9, "\316\\"},
710 {K_S_F10, "\316]"},
711 {K_S_F11, "\316\207"},
712 {K_S_F12, "\316\210"},
713 {K_INS, "\316R"},
714 {K_DEL, "\316S"},
715 {K_HOME, "\316G"},
716 {K_S_HOME, "\316\302"},
717 {K_C_HOME, "\316w"},
718 {K_END, "\316O"},
719 {K_S_END, "\316\315"},
720 {K_C_END, "\316u"},
721 {K_PAGEDOWN, "\316Q"},
722 {K_PAGEUP, "\316I"},
723 {K_KPLUS, "\316N"},
724 {K_KMINUS, "\316J"},
725 {K_KMULTIPLY, "\316\067"},
726 {K_K0, "\316\332"},
727 {K_K1, "\316\336"},
728 {K_K2, "\316\342"},
729 {K_K3, "\316\346"},
730 {K_K4, "\316\352"},
731 {K_K5, "\316\356"},
732 {K_K6, "\316\362"},
733 {K_K7, "\316\366"},
734 {K_K8, "\316\372"},
735 {K_K9, "\316\376"},
736# endif
737
738# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
739/*
740 * VT320 is working as an ANSI terminal compatible DEC terminal.
741 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
742 * Note: K_F1...K_F5 are for internal use, should not be defined.
743 * TODO:- rewrite ESC[ codes to CSI
744 * - keyboard languages (CSI ? 26 n)
745 */
746 {(int)KS_NAME, "vt320"},
747 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
748 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
749# ifdef TERMINFO
750 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
751# else
752 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
753# endif
754 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
755# ifdef TERMINFO
756 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
757# else
758 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
759# endif
760 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000761 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
762 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
764 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000765 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
766 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
767 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
768 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
769 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
770 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
771 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
772 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
773 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
774 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 {(int)KS_MS, "y"},
776 {(int)KS_UT, "y"},
Bram Moolenaar494838a2015-02-10 19:20:37 +0100777 {(int)KS_XN, "y"},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {(int)KS_LE, "\b"},
779# ifdef TERMINFO
780 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
781 ESC_STR "[%i%p1%d;%p2%dH")},
782# else
783 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
784# endif
785# ifdef TERMINFO
786 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
787# else
788 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
789# endif
790 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
791 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
792 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
793 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000794 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
795 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
796 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
797 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
798 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
800 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
801 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
802 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
803 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000804 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
806 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
807 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
808 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
809 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
810 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
811 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
812 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
813 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
814 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
815 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
816 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
817 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
818 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
819 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
820 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
821 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
822 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
823 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
824 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
825 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
826# endif
827
828# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
829/*
830 * Ordinary vt52
831 */
832 {(int)KS_NAME, "vt52"},
833 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
834 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
835 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
836 {(int)KS_LE, "\b"},
837# ifdef __MINT__
838 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
839 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
840 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
841 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
842 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
843 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
844 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
845 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
846 {K_UP, IF_EB("\033A", ESC_STR "A")},
847 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
848 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
849 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
850 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
851 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
852 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
853 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
854 {K_F1, IF_EB("\033P", ESC_STR "P")},
855 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
856 {K_F3, IF_EB("\033R", ESC_STR "R")},
857 {K_F4, IF_EB("\033S", ESC_STR "S")},
858 {K_F5, IF_EB("\033T", ESC_STR "T")},
859 {K_F6, IF_EB("\033U", ESC_STR "U")},
860 {K_F7, IF_EB("\033V", ESC_STR "V")},
861 {K_F8, IF_EB("\033W", ESC_STR "W")},
862 {K_F9, IF_EB("\033X", ESC_STR "X")},
863 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
864 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
865 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
866 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
867 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
868 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
869 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
870 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
871 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
872 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
873 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
874 {K_INS, IF_EB("\033I", ESC_STR "I")},
875 {K_HOME, IF_EB("\033E", ESC_STR "E")},
876 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
877 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
878# else
879 {(int)KS_AL, IF_EB("\033T", ESC_STR "T")},
880 {(int)KS_DL, IF_EB("\033U", ESC_STR "U")},
881 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
882 {(int)KS_ME, IF_EB("\033SO", ESC_STR "SO")},
883 {(int)KS_MR, IF_EB("\033S2", ESC_STR "S2")},
884 {(int)KS_MS, "y"},
885# endif
886# endif
887
888# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) || defined(__EMX__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {(int)KS_NAME, "xterm"},
890 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
891 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
892# ifdef TERMINFO
893 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
894# else
895 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
896# endif
897 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
898# ifdef TERMINFO
899 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
900# else
901 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
902# endif
903# ifdef TERMINFO
904 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
905 ESC_STR "[%i%p1%d;%p2%dr")},
906# else
907 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
908# endif
909 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
910 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
911 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
912 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
913 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
914 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
915 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
916 {(int)KS_MS, "y"},
917 {(int)KS_UT, "y"},
918 {(int)KS_LE, "\b"},
919# ifdef TERMINFO
920 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
921 ESC_STR "[%i%p1%d;%p2%dH")},
922# else
923 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
924# endif
925 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
926# ifdef TERMINFO
927 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
928# else
929 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
930# endif
931 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
932 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
933# ifdef FEAT_XTERM_SAVE
934 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
935 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
936 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
937# endif
938 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
939 {(int)KS_CIE, "\007"},
940 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
941 {(int)KS_FS, "\007"},
942# ifdef TERMINFO
943 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
944 ESC_STR "[8;%p1%d;%p2%dt")},
945 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
946 ESC_STR "[3;%p1%d;%p2%dt")},
947# else
948 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
949 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
950# endif
951 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100952 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000953
954 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
955 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
956 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
957 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
958 /* An extra set of cursor keys for vt100 mode */
959 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
960 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
961 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
962 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000964 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
965 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
966 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
967 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000968 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
969 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
970 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
971 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
972 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
973 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
974 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
975 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
976 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
977 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
978 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
979 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000981 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
982 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
983 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
984 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000985 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
986 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000987 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000988 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000989 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000990 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000991 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
992 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000993 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000994 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000995 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000996 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
997 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000998 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
999 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
1000 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
1001 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
1002 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
1003 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001004 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
1006 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001007 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
1008 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
1009 /* F14 and F15 are missing, because they send the same codes as the undo
1010 * and help key, although they don't work on all keyboards. */
1011 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
1012 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
1013 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1014 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1015 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1016
1017 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1018 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1019 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1020 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1021 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1022 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1023 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1024 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1025 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1026 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1027
1028 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1029 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1030 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1031 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1032 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1033 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1034 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035# endif
1036
1037# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1038/*
1039 * iris-ansi for Silicon Graphics machines.
1040 */
1041 {(int)KS_NAME, "iris-ansi"},
1042 {(int)KS_CE, "\033[K"},
1043 {(int)KS_CD, "\033[J"},
1044 {(int)KS_AL, "\033[L"},
1045# ifdef TERMINFO
1046 {(int)KS_CAL, "\033[%p1%dL"},
1047# else
1048 {(int)KS_CAL, "\033[%dL"},
1049# endif
1050 {(int)KS_DL, "\033[M"},
1051# ifdef TERMINFO
1052 {(int)KS_CDL, "\033[%p1%dM"},
1053# else
1054 {(int)KS_CDL, "\033[%dM"},
1055# endif
1056#if 0 /* The scroll region is not working as Vim expects. */
1057# ifdef TERMINFO
1058 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1059# else
1060 {(int)KS_CS, "\033[%i%d;%dr"},
1061# endif
1062#endif
1063 {(int)KS_CL, "\033[H\033[2J"},
1064 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1065 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1066 {(int)KS_TI, "\033[=6h"},
1067 {(int)KS_TE, "\033[=6l"},
1068 {(int)KS_SE, "\033[21;27m"},
1069 {(int)KS_SO, "\033[1;7m"},
1070 {(int)KS_ME, "\033[m"},
1071 {(int)KS_MR, "\033[7m"},
1072 {(int)KS_MD, "\033[1m"},
1073 {(int)KS_CCO, "8"}, /* allow 8 colors */
1074 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1075 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1076 {(int)KS_US, "\033[4m"}, /* underline on */
1077 {(int)KS_UE, "\033[24m"}, /* underline off */
1078# ifdef TERMINFO
1079 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1080 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1081 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1082 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1083# else
1084 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1085 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1086 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1087 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1088# endif
1089 {(int)KS_MS, "y"}, /* guessed */
1090 {(int)KS_UT, "y"}, /* guessed */
1091 {(int)KS_LE, "\b"},
1092# ifdef TERMINFO
1093 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1094# else
1095 {(int)KS_CM, "\033[%i%d;%dH"},
1096# endif
1097 {(int)KS_SR, "\033M"},
1098# ifdef TERMINFO
1099 {(int)KS_CRI, "\033[%p1%dC"},
1100# else
1101 {(int)KS_CRI, "\033[%dC"},
1102# endif
1103 {(int)KS_CIS, "\033P3.y"},
1104 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1105 {(int)KS_TS, "\033P1.y"},
1106 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1107# ifdef TERMINFO
1108 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1109 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1110# else
1111 {(int)KS_CWS, "\033[203;%d;%d/y"},
1112 {(int)KS_CWP, "\033[205;%d;%d/y"},
1113# endif
1114 {K_UP, "\033[A"},
1115 {K_DOWN, "\033[B"},
1116 {K_LEFT, "\033[D"},
1117 {K_RIGHT, "\033[C"},
1118 {K_S_UP, "\033[161q"},
1119 {K_S_DOWN, "\033[164q"},
1120 {K_S_LEFT, "\033[158q"},
1121 {K_S_RIGHT, "\033[167q"},
1122 {K_F1, "\033[001q"},
1123 {K_F2, "\033[002q"},
1124 {K_F3, "\033[003q"},
1125 {K_F4, "\033[004q"},
1126 {K_F5, "\033[005q"},
1127 {K_F6, "\033[006q"},
1128 {K_F7, "\033[007q"},
1129 {K_F8, "\033[008q"},
1130 {K_F9, "\033[009q"},
1131 {K_F10, "\033[010q"},
1132 {K_F11, "\033[011q"},
1133 {K_F12, "\033[012q"},
1134 {K_S_F1, "\033[013q"},
1135 {K_S_F2, "\033[014q"},
1136 {K_S_F3, "\033[015q"},
1137 {K_S_F4, "\033[016q"},
1138 {K_S_F5, "\033[017q"},
1139 {K_S_F6, "\033[018q"},
1140 {K_S_F7, "\033[019q"},
1141 {K_S_F8, "\033[020q"},
1142 {K_S_F9, "\033[021q"},
1143 {K_S_F10, "\033[022q"},
1144 {K_S_F11, "\033[023q"},
1145 {K_S_F12, "\033[024q"},
1146 {K_INS, "\033[139q"},
1147 {K_HOME, "\033[H"},
1148 {K_END, "\033[146q"},
1149 {K_PAGEUP, "\033[150q"},
1150 {K_PAGEDOWN, "\033[154q"},
1151# endif
1152
1153# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1154/*
1155 * for debugging
1156 */
1157 {(int)KS_NAME, "debug"},
1158 {(int)KS_CE, "[CE]"},
1159 {(int)KS_CD, "[CD]"},
1160 {(int)KS_AL, "[AL]"},
1161# ifdef TERMINFO
1162 {(int)KS_CAL, "[CAL%p1%d]"},
1163# else
1164 {(int)KS_CAL, "[CAL%d]"},
1165# endif
1166 {(int)KS_DL, "[DL]"},
1167# ifdef TERMINFO
1168 {(int)KS_CDL, "[CDL%p1%d]"},
1169# else
1170 {(int)KS_CDL, "[CDL%d]"},
1171# endif
1172# ifdef TERMINFO
1173 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1174# else
1175 {(int)KS_CS, "[%dCS%d]"},
1176# endif
1177# ifdef FEAT_VERTSPLIT
1178# ifdef TERMINFO
1179 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1180# else
1181 {(int)KS_CSV, "[%dCSV%d]"},
1182# endif
1183# endif
1184# ifdef TERMINFO
1185 {(int)KS_CAB, "[CAB%p1%d]"},
1186 {(int)KS_CAF, "[CAF%p1%d]"},
1187 {(int)KS_CSB, "[CSB%p1%d]"},
1188 {(int)KS_CSF, "[CSF%p1%d]"},
1189# else
1190 {(int)KS_CAB, "[CAB%d]"},
1191 {(int)KS_CAF, "[CAF%d]"},
1192 {(int)KS_CSB, "[CSB%d]"},
1193 {(int)KS_CSF, "[CSF%d]"},
1194# endif
1195 {(int)KS_OP, "[OP]"},
1196 {(int)KS_LE, "[LE]"},
1197 {(int)KS_CL, "[CL]"},
1198 {(int)KS_VI, "[VI]"},
1199 {(int)KS_VE, "[VE]"},
1200 {(int)KS_VS, "[VS]"},
1201 {(int)KS_ME, "[ME]"},
1202 {(int)KS_MR, "[MR]"},
1203 {(int)KS_MB, "[MB]"},
1204 {(int)KS_MD, "[MD]"},
1205 {(int)KS_SE, "[SE]"},
1206 {(int)KS_SO, "[SO]"},
1207 {(int)KS_UE, "[UE]"},
1208 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001209 {(int)KS_UCE, "[UCE]"},
1210 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {(int)KS_MS, "[MS]"},
1212 {(int)KS_UT, "[UT]"},
Bram Moolenaar494838a2015-02-10 19:20:37 +01001213 {(int)KS_XN, "[XN]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214# ifdef TERMINFO
1215 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1216# else
1217 {(int)KS_CM, "[%dCM%d]"},
1218# endif
1219 {(int)KS_SR, "[SR]"},
1220# ifdef TERMINFO
1221 {(int)KS_CRI, "[CRI%p1%d]"},
1222# else
1223 {(int)KS_CRI, "[CRI%d]"},
1224# endif
1225 {(int)KS_VB, "[VB]"},
1226 {(int)KS_KS, "[KS]"},
1227 {(int)KS_KE, "[KE]"},
1228 {(int)KS_TI, "[TI]"},
1229 {(int)KS_TE, "[TE]"},
1230 {(int)KS_CIS, "[CIS]"},
1231 {(int)KS_CIE, "[CIE]"},
1232 {(int)KS_TS, "[TS]"},
1233 {(int)KS_FS, "[FS]"},
1234# ifdef TERMINFO
1235 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1236 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1237# else
1238 {(int)KS_CWS, "[%dCWS%d]"},
1239 {(int)KS_CWP, "[%dCWP%d]"},
1240# endif
1241 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001242 {(int)KS_U7, "[U7]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {K_UP, "[KU]"},
1244 {K_DOWN, "[KD]"},
1245 {K_LEFT, "[KL]"},
1246 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001247 {K_XUP, "[xKU]"},
1248 {K_XDOWN, "[xKD]"},
1249 {K_XLEFT, "[xKL]"},
1250 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {K_S_UP, "[S-KU]"},
1252 {K_S_DOWN, "[S-KD]"},
1253 {K_S_LEFT, "[S-KL]"},
1254 {K_C_LEFT, "[C-KL]"},
1255 {K_S_RIGHT, "[S-KR]"},
1256 {K_C_RIGHT, "[C-KR]"},
1257 {K_F1, "[F1]"},
1258 {K_XF1, "[xF1]"},
1259 {K_F2, "[F2]"},
1260 {K_XF2, "[xF2]"},
1261 {K_F3, "[F3]"},
1262 {K_XF3, "[xF3]"},
1263 {K_F4, "[F4]"},
1264 {K_XF4, "[xF4]"},
1265 {K_F5, "[F5]"},
1266 {K_F6, "[F6]"},
1267 {K_F7, "[F7]"},
1268 {K_F8, "[F8]"},
1269 {K_F9, "[F9]"},
1270 {K_F10, "[F10]"},
1271 {K_F11, "[F11]"},
1272 {K_F12, "[F12]"},
1273 {K_S_F1, "[S-F1]"},
1274 {K_S_XF1, "[S-xF1]"},
1275 {K_S_F2, "[S-F2]"},
1276 {K_S_XF2, "[S-xF2]"},
1277 {K_S_F3, "[S-F3]"},
1278 {K_S_XF3, "[S-xF3]"},
1279 {K_S_F4, "[S-F4]"},
1280 {K_S_XF4, "[S-xF4]"},
1281 {K_S_F5, "[S-F5]"},
1282 {K_S_F6, "[S-F6]"},
1283 {K_S_F7, "[S-F7]"},
1284 {K_S_F8, "[S-F8]"},
1285 {K_S_F9, "[S-F9]"},
1286 {K_S_F10, "[S-F10]"},
1287 {K_S_F11, "[S-F11]"},
1288 {K_S_F12, "[S-F12]"},
1289 {K_HELP, "[HELP]"},
1290 {K_UNDO, "[UNDO]"},
1291 {K_BS, "[BS]"},
1292 {K_INS, "[INS]"},
1293 {K_KINS, "[KINS]"},
1294 {K_DEL, "[DEL]"},
1295 {K_KDEL, "[KDEL]"},
1296 {K_HOME, "[HOME]"},
1297 {K_S_HOME, "[C-HOME]"},
1298 {K_C_HOME, "[C-HOME]"},
1299 {K_KHOME, "[KHOME]"},
1300 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001301 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {K_END, "[END]"},
1303 {K_S_END, "[C-END]"},
1304 {K_C_END, "[C-END]"},
1305 {K_KEND, "[KEND]"},
1306 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001307 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 {K_PAGEUP, "[PAGEUP]"},
1309 {K_PAGEDOWN, "[PAGEDOWN]"},
1310 {K_KPAGEUP, "[KPAGEUP]"},
1311 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1312 {K_MOUSE, "[MOUSE]"},
1313 {K_KPLUS, "[KPLUS]"},
1314 {K_KMINUS, "[KMINUS]"},
1315 {K_KDIVIDE, "[KDIVIDE]"},
1316 {K_KMULTIPLY, "[KMULTIPLY]"},
1317 {K_KENTER, "[KENTER]"},
1318 {K_KPOINT, "[KPOINT]"},
1319 {K_K0, "[K0]"},
1320 {K_K1, "[K1]"},
1321 {K_K2, "[K2]"},
1322 {K_K3, "[K3]"},
1323 {K_K4, "[K4]"},
1324 {K_K5, "[K5]"},
1325 {K_K6, "[K6]"},
1326 {K_K7, "[K7]"},
1327 {K_K8, "[K8]"},
1328 {K_K9, "[K9]"},
1329# endif
1330
1331#endif /* NO_BUILTIN_TCAPS */
1332
1333/*
1334 * The most minimal terminal: only clear screen and cursor positioning
1335 * Always included.
1336 */
1337 {(int)KS_NAME, "dumb"},
1338 {(int)KS_CL, "\014"},
1339#ifdef TERMINFO
1340 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1341 ESC_STR "[%i%p1%d;%p2%dH")},
1342#else
1343 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1344#endif
1345
1346/*
1347 * end marker
1348 */
1349 {(int)KS_NAME, NULL}
1350
1351}; /* end of builtin_termcaps */
1352
1353/*
1354 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356#ifdef AMIGA
1357# define DEFAULT_TERM (char_u *)"amiga"
1358#endif
1359
1360#ifdef MSWIN
1361# define DEFAULT_TERM (char_u *)"win32"
1362#endif
1363
1364#ifdef MSDOS
1365# define DEFAULT_TERM (char_u *)"pcterm"
1366#endif
1367
1368#if defined(UNIX) && !defined(__MINT__)
1369# define DEFAULT_TERM (char_u *)"ansi"
1370#endif
1371
1372#ifdef __MINT__
1373# define DEFAULT_TERM (char_u *)"vt52"
1374#endif
1375
1376#ifdef __EMX__
1377# define DEFAULT_TERM (char_u *)"os2ansi"
1378#endif
1379
1380#ifdef VMS
1381# define DEFAULT_TERM (char_u *)"vt320"
1382#endif
1383
1384#ifdef __BEOS__
1385# undef DEFAULT_TERM
1386# define DEFAULT_TERM (char_u *)"beos-ansi"
1387#endif
1388
1389#ifndef DEFAULT_TERM
1390# define DEFAULT_TERM (char_u *)"dumb"
1391#endif
1392
1393/*
1394 * Term_strings contains currently used terminal output strings.
1395 * It is initialized with the default values by parse_builtin_tcap().
1396 * The values can be changed by setting the option with the same name.
1397 */
1398char_u *(term_strings[(int)KS_LAST + 1]);
1399
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001400static int need_gather = FALSE; /* need to fill termleader[] */
1401static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001403static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#endif
1405
1406 static struct builtin_term *
1407find_builtin_term(term)
1408 char_u *term;
1409{
1410 struct builtin_term *p;
1411
1412 p = builtin_termcaps;
1413 while (p->bt_string != NULL)
1414 {
1415 if (p->bt_entry == (int)KS_NAME)
1416 {
1417#ifdef UNIX
1418 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1419 return p;
1420 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1421 return p;
1422 else
1423#endif
1424#ifdef VMS
1425 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1426 return p;
1427 else
1428#endif
1429 if (STRCMP(term, p->bt_string) == 0)
1430 return p;
1431 }
1432 ++p;
1433 }
1434 return p;
1435}
1436
1437/*
1438 * Parsing of the builtin termcap entries.
1439 * Caller should check if 'name' is a valid builtin term.
1440 * The terminal's name is not set, as this is already done in termcapinit().
1441 */
1442 static void
1443parse_builtin_tcap(term)
1444 char_u *term;
1445{
1446 struct builtin_term *p;
1447 char_u name[2];
1448 int term_8bit;
1449
1450 p = find_builtin_term(term);
1451 term_8bit = term_is_8bit(term);
1452
1453 /* Do not parse if builtin term not found */
1454 if (p->bt_string == NULL)
1455 return;
1456
1457 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1458 {
1459 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1460 {
1461 /* Only set the value if it wasn't set yet. */
1462 if (term_strings[p->bt_entry] == NULL
1463 || term_strings[p->bt_entry] == empty_option)
1464 {
1465 /* 8bit terminal: use CSI instead of <Esc>[ */
1466 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1467 {
1468 char_u *s, *t;
1469
1470 s = vim_strsave((char_u *)p->bt_string);
1471 if (s != NULL)
1472 {
1473 for (t = s; *t; ++t)
1474 if (term_7to8bit(t))
1475 {
1476 *t = term_7to8bit(t);
1477 STRCPY(t + 1, t + 2);
1478 }
1479 term_strings[p->bt_entry] = s;
1480 set_term_option_alloced(&term_strings[p->bt_entry]);
1481 }
1482 }
1483 else
1484 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1485 }
1486 }
1487 else
1488 {
1489 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1490 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1491 if (find_termcode(name) == NULL)
1492 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1493 }
1494 }
1495}
1496#if defined(HAVE_TGETENT) || defined(FEAT_TERMRESPONSE)
1497static void set_color_count __ARGS((int nr));
1498
1499/*
1500 * Set number of colors.
1501 * Store it as a number in t_colors.
1502 * Store it as a string in T_CCO (using nr_colors[]).
1503 */
1504 static void
1505set_color_count(nr)
1506 int nr;
1507{
1508 char_u nr_colors[20]; /* string for number of colors */
1509
1510 t_colors = nr;
1511 if (t_colors > 1)
1512 sprintf((char *)nr_colors, "%d", t_colors);
1513 else
1514 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001515 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516}
1517#endif
1518
1519#ifdef HAVE_TGETENT
1520static char *(key_names[]) =
1521{
1522#ifdef FEAT_TERMRESPONSE
1523 /* Do this one first, it may cause a screen redraw. */
1524 "Co",
1525#endif
1526 "ku", "kd", "kr", "kl",
1527# ifdef ARCHIE
1528 "su", "sd", /* Termcap code made up! */
1529# endif
1530 "#2", "#4", "%i", "*7",
1531 "k1", "k2", "k3", "k4", "k5", "k6",
1532 "k7", "k8", "k9", "k;", "F1", "F2",
1533 "%1", "&8", "kb", "kI", "kD", "kh",
1534 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1535 NULL
1536};
1537#endif
1538
1539/*
1540 * Set terminal options for terminal "term".
1541 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1542 *
1543 * While doing this, until ttest(), some options may be NULL, be careful.
1544 */
1545 int
1546set_termname(term)
1547 char_u *term;
1548{
1549 struct builtin_term *termp;
1550#ifdef HAVE_TGETENT
1551 int builtin_first = p_tbi;
1552 int try;
1553 int termcap_cleared = FALSE;
1554#endif
1555 int width = 0, height = 0;
1556 char_u *error_msg = NULL;
1557 char_u *bs_p, *del_p;
1558
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001559 /* In silect mode (ex -s) we don't use the 'term' option. */
1560 if (silent_mode)
1561 return OK;
1562
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 detected_8bit = FALSE; /* reset 8-bit detection */
1564
1565 if (term_is_builtin(term))
1566 {
1567 term += 8;
1568#ifdef HAVE_TGETENT
1569 builtin_first = 1;
1570#endif
1571 }
1572
1573/*
1574 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1575 * If builtin_first is TRUE:
1576 * 0. try builtin termcap
1577 * 1. try external termcap
1578 * 2. if both fail default to a builtin terminal
1579 * If builtin_first is FALSE:
1580 * 1. try external termcap
1581 * 2. try builtin termcap, if both fail default to a builtin terminal
1582 */
1583#ifdef HAVE_TGETENT
1584 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1585 {
1586 /*
1587 * Use external termcap
1588 */
1589 if (try == 1)
1590 {
1591 char_u *p;
1592 static char_u tstrbuf[TBUFSZ];
1593 int i;
1594 char_u tbuf[TBUFSZ];
1595 char_u *tp;
1596 static struct {
1597 enum SpecialKey dest; /* index in term_strings[] */
1598 char *name; /* termcap name for string */
1599 } string_names[] =
1600 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1601 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1602 {KS_CL, "cl"}, {KS_CD, "cd"},
1603 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1604 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1605 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1606 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001607 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1608 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1610 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1611 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1612 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1613 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1614 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1615 {KS_TS, "ts"}, {KS_FS, "fs"},
1616 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001617 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001618 {KS_U7, "u7"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {(enum SpecialKey)0, NULL}
1620 };
1621
1622 /*
1623 * If the external termcap does not have a matching entry, try the
1624 * builtin ones.
1625 */
1626 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1627 {
1628 tp = tstrbuf;
1629 if (!termcap_cleared)
1630 {
1631 clear_termoptions(); /* clear old options */
1632 termcap_cleared = TRUE;
1633 }
1634
1635 /* get output strings */
1636 for (i = 0; string_names[i].name != NULL; ++i)
1637 {
1638 if (term_str(string_names[i].dest) == NULL
1639 || term_str(string_names[i].dest) == empty_option)
1640 term_str(string_names[i].dest) =
1641 TGETSTR(string_names[i].name, &tp);
1642 }
1643
1644 /* tgetflag() returns 1 if the flag is present, 0 if not and
1645 * possibly -1 if the flag doesn't exist. */
1646 if ((T_MS == NULL || T_MS == empty_option)
1647 && tgetflag("ms") > 0)
1648 T_MS = (char_u *)"y";
1649 if ((T_XS == NULL || T_XS == empty_option)
1650 && tgetflag("xs") > 0)
1651 T_XS = (char_u *)"y";
Bram Moolenaar494838a2015-02-10 19:20:37 +01001652 if ((T_XN == NULL || T_XN == empty_option)
1653 && tgetflag("xn") > 0)
1654 T_XN = (char_u *)"y";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if ((T_DB == NULL || T_DB == empty_option)
1656 && tgetflag("db") > 0)
1657 T_DB = (char_u *)"y";
1658 if ((T_DA == NULL || T_DA == empty_option)
1659 && tgetflag("da") > 0)
1660 T_DA = (char_u *)"y";
1661 if ((T_UT == NULL || T_UT == empty_option)
1662 && tgetflag("ut") > 0)
1663 T_UT = (char_u *)"y";
1664
1665
1666 /*
1667 * get key codes
1668 */
1669 for (i = 0; key_names[i] != NULL; ++i)
1670 {
1671 if (find_termcode((char_u *)key_names[i]) == NULL)
1672 {
1673 p = TGETSTR(key_names[i], &tp);
1674 /* if cursor-left == backspace, ignore it (televideo
1675 * 925) */
1676 if (p != NULL
1677 && (*p != Ctrl_H
1678 || key_names[i][0] != 'k'
1679 || key_names[i][1] != 'l'))
1680 add_termcode((char_u *)key_names[i], p, FALSE);
1681 }
1682 }
1683
1684 if (height == 0)
1685 height = tgetnum("li");
1686 if (width == 0)
1687 width = tgetnum("co");
1688
1689 /*
1690 * Get number of colors (if not done already).
1691 */
1692 if (term_str(KS_CCO) == NULL
1693 || term_str(KS_CCO) == empty_option)
1694 set_color_count(tgetnum("Co"));
1695
1696# ifndef hpux
1697 BC = (char *)TGETSTR("bc", &tp);
1698 UP = (char *)TGETSTR("up", &tp);
1699 p = TGETSTR("pc", &tp);
1700 if (p)
1701 PC = *p;
1702# endif /* hpux */
1703 }
1704 }
1705 else /* try == 0 || try == 2 */
1706#endif /* HAVE_TGETENT */
1707 /*
1708 * Use builtin termcap
1709 */
1710 {
1711#ifdef HAVE_TGETENT
1712 /*
1713 * If builtin termcap was already used, there is no need to search
1714 * for the builtin termcap again, quit now.
1715 */
1716 if (try == 2 && builtin_first && termcap_cleared)
1717 break;
1718#endif
1719 /*
1720 * search for 'term' in builtin_termcaps[]
1721 */
1722 termp = find_builtin_term(term);
1723 if (termp->bt_string == NULL) /* did not find it */
1724 {
1725#ifdef HAVE_TGETENT
1726 /*
1727 * If try == 0, first try the external termcap. If that is not
1728 * found we'll get back here with try == 2.
1729 * If termcap_cleared is set we used the external termcap,
1730 * don't complain about not finding the term in the builtin
1731 * termcap.
1732 */
1733 if (try == 0) /* try external one */
1734 continue;
1735 if (termcap_cleared) /* found in external termcap */
1736 break;
1737#endif
1738
1739 mch_errmsg("\r\n");
1740 if (error_msg != NULL)
1741 {
1742 mch_errmsg((char *)error_msg);
1743 mch_errmsg("\r\n");
1744 }
1745 mch_errmsg("'");
1746 mch_errmsg((char *)term);
1747 mch_errmsg(_("' not known. Available builtin terminals are:"));
1748 mch_errmsg("\r\n");
1749 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1750 ++termp)
1751 {
1752 if (termp->bt_entry == (int)KS_NAME)
1753 {
1754#ifdef HAVE_TGETENT
1755 mch_errmsg(" builtin_");
1756#else
1757 mch_errmsg(" ");
1758#endif
1759 mch_errmsg(termp->bt_string);
1760 mch_errmsg("\r\n");
1761 }
1762 }
1763 /* when user typed :set term=xxx, quit here */
1764 if (starting != NO_SCREEN)
1765 {
1766 screen_start(); /* don't know where cursor is now */
1767 wait_return(TRUE);
1768 return FAIL;
1769 }
1770 term = DEFAULT_TERM;
1771 mch_errmsg(_("defaulting to '"));
1772 mch_errmsg((char *)term);
1773 mch_errmsg("'\r\n");
1774 if (emsg_silent == 0)
1775 {
1776 screen_start(); /* don't know where cursor is now */
1777 out_flush();
1778 ui_delay(2000L, TRUE);
1779 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001780 set_string_option_direct((char_u *)"term", -1, term,
1781 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 display_errors();
1783 }
1784 out_flush();
1785#ifdef HAVE_TGETENT
1786 if (!termcap_cleared)
1787 {
1788#endif
1789 clear_termoptions(); /* clear old options */
1790#ifdef HAVE_TGETENT
1791 termcap_cleared = TRUE;
1792 }
1793#endif
1794 parse_builtin_tcap(term);
1795#ifdef FEAT_GUI
1796 if (term_is_gui(term))
1797 {
1798 out_flush();
1799 gui_init();
1800 /* If starting the GUI failed, don't do any of the other
1801 * things for this terminal */
1802 if (!gui.in_use)
1803 return FAIL;
1804#ifdef HAVE_TGETENT
1805 break; /* don't try using external termcap */
1806#endif
1807 }
1808#endif /* FEAT_GUI */
1809 }
1810#ifdef HAVE_TGETENT
1811 }
1812#endif
1813
1814/*
1815 * special: There is no info in the termcap about whether the cursor
1816 * positioning is relative to the start of the screen or to the start of the
1817 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1818 * relative.
1819 */
1820 if (STRCMP(term, "pcterm") == 0)
1821 T_CCS = (char_u *)"yes";
1822 else
1823 T_CCS = empty_option;
1824
1825#ifdef UNIX
1826/*
1827 * Any "stty" settings override the default for t_kb from the termcap.
1828 * This is in os_unix.c, because it depends a lot on the version of unix that
1829 * is being used.
1830 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1831 */
1832#ifdef FEAT_GUI
1833 if (!gui.in_use)
1834#endif
1835 get_stty();
1836#endif
1837
1838/*
1839 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1840 * didn't work, use the default CTRL-H
1841 * The default for t_kD is DEL, unless t_kb is DEL.
1842 * The vim_strsave'd strings are probably lost forever, well it's only two
1843 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1844 * directly.
1845 */
1846#ifdef FEAT_GUI
1847 if (!gui.in_use)
1848#endif
1849 {
1850 bs_p = find_termcode((char_u *)"kb");
1851 del_p = find_termcode((char_u *)"kD");
1852 if (bs_p == NULL || *bs_p == NUL)
1853 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1854 if ((del_p == NULL || *del_p == NUL) &&
1855 (bs_p == NULL || *bs_p != DEL))
1856 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1857 }
1858
1859#if defined(UNIX) || defined(VMS)
1860 term_is_xterm = vim_is_xterm(term);
1861#endif
1862
1863#ifdef FEAT_MOUSE
1864# if defined(UNIX) || defined(VMS)
1865# ifdef FEAT_MOUSE_TTY
1866 /*
1867 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1868 * The termcode for the mouse is added as a side effect in option.c.
1869 */
1870 {
1871 char_u *p;
1872
1873 p = (char_u *)"";
1874# ifdef FEAT_MOUSE_XTERM
1875# ifdef FEAT_CLIPBOARD
1876# ifdef FEAT_GUI
1877 if (!gui.in_use)
1878# endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001879# ifndef FEAT_CYGWIN_WIN32_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 clip_init(FALSE);
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882# endif
Bram Moolenaar864207d2008-06-24 22:14:38 +00001883 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 {
1885 if (use_xterm_mouse())
1886 p = NULL; /* keep existing value, might be "xterm2" */
1887 else
1888 p = (char_u *)"xterm";
1889 }
1890# endif
1891 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001894 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1895 * "xterm2" in check_termcode(). */
1896 reset_option_was_set((char_u *)"ttym");
1897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (p == NULL
1899# ifdef FEAT_GUI
1900 || gui.in_use
1901# endif
1902 )
1903 check_mouse_termcode(); /* set mouse termcode anyway */
1904 }
1905# endif
1906# else
1907 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1908# endif
1909#endif /* FEAT_MOUSE */
1910
1911#ifdef FEAT_SNIFF
1912 {
1913 char_u name[2];
1914
1915 name[0] = (int)KS_EXTRA;
1916 name[1] = (int)KE_SNIFF;
1917 add_termcode(name, (char_u *)"\233sniff", FALSE);
1918 }
1919#endif
1920
1921#ifdef USE_TERM_CONSOLE
1922 /* DEFAULT_TERM indicates that it is the machine console. */
1923 if (STRCMP(term, DEFAULT_TERM) != 0)
1924 term_console = FALSE;
1925 else
1926 {
1927 term_console = TRUE;
1928# ifdef AMIGA
1929 win_resize_on(); /* enable window resizing reports */
1930# endif
1931 }
1932#endif
1933
1934#if defined(UNIX) || defined(VMS)
1935 /*
1936 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1937 */
1938 if (vim_is_fastterm(term))
1939 p_tf = TRUE;
1940#endif
1941#ifdef USE_TERM_CONSOLE
1942 /*
1943 * 'ttyfast' is default on consoles
1944 */
1945 if (term_console)
1946 p_tf = TRUE;
1947#endif
1948
1949 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1950
1951 full_screen = TRUE; /* we can use termcap codes from now on */
1952 set_term_defaults(); /* use current values as defaults */
1953#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +02001954 LOG_TR("setting crv_status to CRV_GET");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 crv_status = CRV_GET; /* Get terminal version later */
1956#endif
1957
1958 /*
1959 * Initialize the terminal with the appropriate termcap codes.
1960 * Set the mouse and window title if possible.
1961 * Don't do this when starting, need to parse the .vimrc first, because it
1962 * may redefine t_TI etc.
1963 */
1964 if (starting != NO_SCREEN)
1965 {
1966 starttermcap(); /* may change terminal mode */
1967#ifdef FEAT_MOUSE
1968 setmouse(); /* may start using the mouse */
1969#endif
1970#ifdef FEAT_TITLE
1971 maketitle(); /* may display window title */
1972#endif
1973 }
1974
1975 /* display initial screen after ttest() checking. jw. */
1976 if (width <= 0 || height <= 0)
1977 {
1978 /* termcap failed to report size */
1979 /* set defaults, in case ui_get_shellsize() also fails */
1980 width = 80;
1981#if defined(MSDOS) || defined(WIN3264)
1982 height = 25; /* console is often 25 lines */
1983#else
1984 height = 24; /* most terminals are 24 lines */
1985#endif
1986 }
1987 set_shellsize(width, height, FALSE); /* may change Rows */
1988 if (starting != NO_SCREEN)
1989 {
1990 if (scroll_region)
1991 scroll_region_reset(); /* In case Rows changed */
1992 check_map_keycodes(); /* check mappings for terminal codes used */
1993
1994#ifdef FEAT_AUTOCMD
1995 {
1996 buf_T *old_curbuf;
1997
1998 /*
1999 * Execute the TermChanged autocommands for each buffer that is
2000 * loaded.
2001 */
2002 old_curbuf = curbuf;
2003 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
2004 {
2005 if (curbuf->b_ml.ml_mfp != NULL)
2006 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2007 curbuf);
2008 }
2009 if (buf_valid(old_curbuf))
2010 curbuf = old_curbuf;
2011 }
2012#endif
2013 }
2014
2015#ifdef FEAT_TERMRESPONSE
2016 may_req_termresponse();
2017#endif
2018
2019 return OK;
2020}
2021
2022#if defined(FEAT_MOUSE) || defined(PROTO)
2023
2024# ifdef FEAT_MOUSE_TTY
2025# define HMT_NORMAL 1
2026# define HMT_NETTERM 2
2027# define HMT_DEC 4
2028# define HMT_JSBTERM 8
2029# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002030# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002031# define HMT_SGR 64
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032static int has_mouse_termcode = 0;
2033# endif
2034
Bram Moolenaar864207d2008-06-24 22:14:38 +00002035# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 void
2037set_mouse_termcode(n, s)
2038 int n; /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2039 char_u *s;
2040{
2041 char_u name[2];
2042
2043 name[0] = n;
2044 name[1] = KE_FILLER;
2045 add_termcode(name, s, FALSE);
2046# ifdef FEAT_MOUSE_TTY
2047# ifdef FEAT_MOUSE_JSB
2048 if (n == KS_JSBTERM_MOUSE)
2049 has_mouse_termcode |= HMT_JSBTERM;
2050 else
2051# endif
2052# ifdef FEAT_MOUSE_NET
2053 if (n == KS_NETTERM_MOUSE)
2054 has_mouse_termcode |= HMT_NETTERM;
2055 else
2056# endif
2057# ifdef FEAT_MOUSE_DEC
2058 if (n == KS_DEC_MOUSE)
2059 has_mouse_termcode |= HMT_DEC;
2060 else
2061# endif
2062# ifdef FEAT_MOUSE_PTERM
2063 if (n == KS_PTERM_MOUSE)
2064 has_mouse_termcode |= HMT_PTERM;
2065 else
2066# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002067# ifdef FEAT_MOUSE_URXVT
2068 if (n == KS_URXVT_MOUSE)
2069 has_mouse_termcode |= HMT_URXVT;
2070 else
2071# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002072# ifdef FEAT_MOUSE_SGR
2073 if (n == KS_SGR_MOUSE)
2074 has_mouse_termcode |= HMT_SGR;
2075 else
2076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 has_mouse_termcode |= HMT_NORMAL;
2078# endif
2079}
2080# endif
2081
2082# if ((defined(UNIX) || defined(VMS) || defined(OS2)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002083 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 void
2085del_mouse_termcode(n)
2086 int n; /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2087{
2088 char_u name[2];
2089
2090 name[0] = n;
2091 name[1] = KE_FILLER;
2092 del_termcode(name);
2093# ifdef FEAT_MOUSE_TTY
2094# ifdef FEAT_MOUSE_JSB
2095 if (n == KS_JSBTERM_MOUSE)
2096 has_mouse_termcode &= ~HMT_JSBTERM;
2097 else
2098# endif
2099# ifdef FEAT_MOUSE_NET
2100 if (n == KS_NETTERM_MOUSE)
2101 has_mouse_termcode &= ~HMT_NETTERM;
2102 else
2103# endif
2104# ifdef FEAT_MOUSE_DEC
2105 if (n == KS_DEC_MOUSE)
2106 has_mouse_termcode &= ~HMT_DEC;
2107 else
2108# endif
2109# ifdef FEAT_MOUSE_PTERM
2110 if (n == KS_PTERM_MOUSE)
2111 has_mouse_termcode &= ~HMT_PTERM;
2112 else
2113# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002114# ifdef FEAT_MOUSE_URXVT
2115 if (n == KS_URXVT_MOUSE)
2116 has_mouse_termcode &= ~HMT_URXVT;
2117 else
2118# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002119# ifdef FEAT_MOUSE_SGR
2120 if (n == KS_SGR_MOUSE)
2121 has_mouse_termcode &= ~HMT_SGR;
2122 else
2123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 has_mouse_termcode &= ~HMT_NORMAL;
2125# endif
2126}
2127# endif
2128#endif
2129
2130#ifdef HAVE_TGETENT
2131/*
2132 * Call tgetent()
2133 * Return error message if it fails, NULL if it's OK.
2134 */
2135 static char_u *
2136tgetent_error(tbuf, term)
2137 char_u *tbuf;
2138 char_u *term;
2139{
2140 int i;
2141
2142 i = TGETENT(tbuf, term);
2143 if (i < 0 /* -1 is always an error */
2144# ifdef TGETENT_ZERO_ERR
2145 || i == 0 /* sometimes zero is also an error */
2146# endif
2147 )
2148 {
2149 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2150 * tgetent() with the always existing "dumb" entry to avoid a crash or
2151 * hang. */
2152 (void)TGETENT(tbuf, "dumb");
2153
2154 if (i < 0)
2155# ifdef TGETENT_ZERO_ERR
2156 return (char_u *)_("E557: Cannot open termcap file");
2157 if (i == 0)
2158# endif
2159#ifdef TERMINFO
2160 return (char_u *)_("E558: Terminal entry not found in terminfo");
2161#else
2162 return (char_u *)_("E559: Terminal entry not found in termcap");
2163#endif
2164 }
2165 return NULL;
2166}
2167
2168/*
2169 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2170 * Fix that here.
2171 */
2172 static char_u *
2173vim_tgetstr(s, pp)
2174 char *s;
2175 char_u **pp;
2176{
2177 char *p;
2178
2179 p = tgetstr(s, (char **)pp);
2180 if (p == (char *)-1)
2181 p = NULL;
2182 return (char_u *)p;
2183}
2184#endif /* HAVE_TGETENT */
2185
2186#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(__EMX__) || defined(VMS) || defined(MACOS_X))
2187/*
2188 * Get Columns and Rows from the termcap. Used after a window signal if the
2189 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2190 * and "li" entries never change. But on some systems this works.
2191 * Errors while getting the entries are ignored.
2192 */
2193 void
2194getlinecol(cp, rp)
2195 long *cp; /* pointer to columns */
2196 long *rp; /* pointer to rows */
2197{
2198 char_u tbuf[TBUFSZ];
2199
2200 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2201 {
2202 if (*cp == 0)
2203 *cp = tgetnum("co");
2204 if (*rp == 0)
2205 *rp = tgetnum("li");
2206 }
2207}
2208#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2209
2210/*
2211 * Get a string entry from the termcap and add it to the list of termcodes.
2212 * Used for <t_xx> special keys.
2213 * Give an error message for failure when not sourcing.
2214 * If force given, replace an existing entry.
2215 * Return FAIL if the entry was not found, OK if the entry was added.
2216 */
2217 int
2218add_termcap_entry(name, force)
2219 char_u *name;
2220 int force;
2221{
2222 char_u *term;
2223 int key;
2224 struct builtin_term *termp;
2225#ifdef HAVE_TGETENT
2226 char_u *string;
2227 int i;
2228 int builtin_first;
2229 char_u tbuf[TBUFSZ];
2230 char_u tstrbuf[TBUFSZ];
2231 char_u *tp = tstrbuf;
2232 char_u *error_msg = NULL;
2233#endif
2234
2235/*
2236 * If the GUI is running or will start in a moment, we only support the keys
2237 * that the GUI can produce.
2238 */
2239#ifdef FEAT_GUI
2240 if (gui.in_use || gui.starting)
2241 return gui_mch_haskey(name);
2242#endif
2243
2244 if (!force && find_termcode(name) != NULL) /* it's already there */
2245 return OK;
2246
2247 term = T_NAME;
2248 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2249 return FAIL;
2250
2251 if (term_is_builtin(term)) /* name starts with "builtin_" */
2252 {
2253 term += 8;
2254#ifdef HAVE_TGETENT
2255 builtin_first = TRUE;
2256#endif
2257 }
2258#ifdef HAVE_TGETENT
2259 else
2260 builtin_first = p_tbi;
2261#endif
2262
2263#ifdef HAVE_TGETENT
2264/*
2265 * We can get the entry from the builtin termcap and from the external one.
2266 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2267 * builtin termcap first.
2268 * If 'ttybuiltin' is off, try external termcap first.
2269 */
2270 for (i = 0; i < 2; ++i)
2271 {
2272 if (!builtin_first == i)
2273#endif
2274 /*
2275 * Search in builtin termcap
2276 */
2277 {
2278 termp = find_builtin_term(term);
2279 if (termp->bt_string != NULL) /* found it */
2280 {
2281 key = TERMCAP2KEY(name[0], name[1]);
2282 while (termp->bt_entry != (int)KS_NAME)
2283 {
2284 if ((int)termp->bt_entry == key)
2285 {
2286 add_termcode(name, (char_u *)termp->bt_string,
2287 term_is_8bit(term));
2288 return OK;
2289 }
2290 ++termp;
2291 }
2292 }
2293 }
2294#ifdef HAVE_TGETENT
2295 else
2296 /*
2297 * Search in external termcap
2298 */
2299 {
2300 error_msg = tgetent_error(tbuf, term);
2301 if (error_msg == NULL)
2302 {
2303 string = TGETSTR((char *)name, &tp);
2304 if (string != NULL && *string != NUL)
2305 {
2306 add_termcode(name, string, FALSE);
2307 return OK;
2308 }
2309 }
2310 }
2311 }
2312#endif
2313
2314 if (sourcing_name == NULL)
2315 {
2316#ifdef HAVE_TGETENT
2317 if (error_msg != NULL)
2318 EMSG(error_msg);
2319 else
2320#endif
2321 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2322 }
2323 return FAIL;
2324}
2325
2326 static int
2327term_is_builtin(name)
2328 char_u *name;
2329{
2330 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2331}
2332
2333/*
2334 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2335 * Assume that the terminal is using 8-bit controls when the name contains
2336 * "8bit", like in "xterm-8bit".
2337 */
2338 int
2339term_is_8bit(name)
2340 char_u *name;
2341{
2342 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2343}
2344
2345/*
2346 * Translate terminal control chars from 7-bit to 8-bit:
2347 * <Esc>[ -> CSI
2348 * <Esc>] -> <M-C-]>
2349 * <Esc>O -> <M-C-O>
2350 */
2351 static int
2352term_7to8bit(p)
2353 char_u *p;
2354{
2355 if (*p == ESC)
2356 {
2357 if (p[1] == '[')
2358 return CSI;
2359 if (p[1] == ']')
2360 return 0x9d;
2361 if (p[1] == 'O')
2362 return 0x8f;
2363 }
2364 return 0;
2365}
2366
2367#ifdef FEAT_GUI
2368 int
2369term_is_gui(name)
2370 char_u *name;
2371{
2372 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2373}
2374#endif
2375
2376#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2377
2378 char_u *
2379tltoa(i)
2380 unsigned long i;
2381{
2382 static char_u buf[16];
2383 char_u *p;
2384
2385 p = buf + 15;
2386 *p = '\0';
2387 do
2388 {
2389 --p;
2390 *p = (char_u) (i % 10 + '0');
2391 i /= 10;
2392 }
2393 while (i > 0 && p > buf);
2394 return p;
2395}
2396#endif
2397
2398#ifndef HAVE_TGETENT
2399
2400/*
2401 * minimal tgoto() implementation.
2402 * no padding and we only parse for %i %d and %+char
2403 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002404static char *tgoto __ARGS((char *, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002406 static char *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407tgoto(cm, x, y)
2408 char *cm;
2409 int x, y;
2410{
2411 static char buf[30];
2412 char *p, *s, *e;
2413
2414 if (!cm)
2415 return "OOPS";
2416 e = buf + 29;
2417 for (s = buf; s < e && *cm; cm++)
2418 {
2419 if (*cm != '%')
2420 {
2421 *s++ = *cm;
2422 continue;
2423 }
2424 switch (*++cm)
2425 {
2426 case 'd':
2427 p = (char *)tltoa((unsigned long)y);
2428 y = x;
2429 while (*p)
2430 *s++ = *p++;
2431 break;
2432 case 'i':
2433 x++;
2434 y++;
2435 break;
2436 case '+':
2437 *s++ = (char)(*++cm + y);
2438 y = x;
2439 break;
2440 case '%':
2441 *s++ = *cm;
2442 break;
2443 default:
2444 return "OOPS";
2445 }
2446 }
2447 *s = '\0';
2448 return buf;
2449}
2450
2451#endif /* HAVE_TGETENT */
2452
2453/*
2454 * Set the terminal name and initialize the terminal options.
2455 * If "name" is NULL or empty, get the terminal name from the environment.
2456 * If that fails, use the default terminal name.
2457 */
2458 void
2459termcapinit(name)
2460 char_u *name;
2461{
2462 char_u *term;
2463
2464 if (name != NULL && *name == NUL)
2465 name = NULL; /* empty name is equal to no name */
2466 term = name;
2467
2468#ifdef __BEOS__
2469 /*
2470 * TERM environment variable is normally set to 'ansi' on the Bebox;
2471 * Since the BeBox doesn't quite support full ANSI yet, we use our
2472 * own custom 'ansi-beos' termcap instead, unless the -T option has
2473 * been given on the command line.
2474 */
2475 if (term == NULL
2476 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2477 term = DEFAULT_TERM;
2478#endif
2479#ifndef MSWIN
2480 if (term == NULL)
2481 term = mch_getenv((char_u *)"TERM");
2482#endif
2483 if (term == NULL || *term == NUL)
2484 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002485 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
2487 /* Set the default terminal name. */
2488 set_string_default("term", term);
2489 set_string_default("ttytype", term);
2490
2491 /*
2492 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2493 */
2494 set_termname(T_NAME != NULL ? T_NAME : term);
2495}
2496
2497/*
2498 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2499 */
2500#ifdef DOS16
2501# define OUT_SIZE 255 /* only have 640K total... */
2502#else
2503# ifdef FEAT_GUI_W16
2504# define OUT_SIZE 1023 /* Save precious 1K near data */
2505# else
2506# define OUT_SIZE 2047
2507# endif
2508#endif
2509 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2510static char_u out_buf[OUT_SIZE + 1];
2511static int out_pos = 0; /* number of chars in out_buf */
2512
2513/*
2514 * out_flush(): flush the output buffer
2515 */
2516 void
2517out_flush()
2518{
2519 int len;
2520
2521 if (out_pos != 0)
2522 {
2523 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2524 len = out_pos;
2525 out_pos = 0;
2526 ui_write(out_buf, len);
2527 }
2528}
2529
2530#if defined(FEAT_MBYTE) || defined(PROTO)
2531/*
2532 * Sometimes a byte out of a multi-byte character is written with out_char().
2533 * To avoid flushing half of the character, call this function first.
2534 */
2535 void
2536out_flush_check()
2537{
2538 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2539 out_flush();
2540}
2541#endif
2542
2543#ifdef FEAT_GUI
2544/*
2545 * out_trash(): Throw away the contents of the output buffer
2546 */
2547 void
2548out_trash()
2549{
2550 out_pos = 0;
2551}
2552#endif
2553
2554/*
2555 * out_char(c): put a byte into the output buffer.
2556 * Flush it if it becomes full.
2557 * This should not be used for outputting text on the screen (use functions
2558 * like msg_puts() and screen_putchar() for that).
2559 */
2560 void
2561out_char(c)
2562 unsigned c;
2563{
2564#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2565 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2566 out_char('\r');
2567#endif
2568
2569 out_buf[out_pos++] = c;
2570
2571 /* For testing we flush each time. */
2572 if (out_pos >= OUT_SIZE || p_wd)
2573 out_flush();
2574}
2575
2576static void out_char_nf __ARGS((unsigned));
2577
2578/*
2579 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2580 */
2581 static void
2582out_char_nf(c)
2583 unsigned c;
2584{
2585#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2586 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2587 out_char_nf('\r');
2588#endif
2589
2590 out_buf[out_pos++] = c;
2591
2592 if (out_pos >= OUT_SIZE)
2593 out_flush();
2594}
2595
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002596#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2597 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598/*
2599 * A never-padding out_str.
2600 * use this whenever you don't want to run the string through tputs.
2601 * tputs above is harmless, but tputs from the termcap library
2602 * is likely to strip off leading digits, that it mistakes for padding
2603 * information, and "%i", "%d", etc.
2604 * This should only be used for writing terminal codes, not for outputting
2605 * normal text (use functions like msg_puts() and screen_putchar() for that).
2606 */
2607 void
2608out_str_nf(s)
2609 char_u *s;
2610{
2611 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2612 out_flush();
2613 while (*s)
2614 out_char_nf(*s++);
2615
2616 /* For testing we write one string at a time. */
2617 if (p_wd)
2618 out_flush();
2619}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
2622/*
2623 * out_str(s): Put a character string a byte at a time into the output buffer.
2624 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2625 * This should only be used for writing terminal codes, not for outputting
2626 * normal text (use functions like msg_puts() and screen_putchar() for that).
2627 */
2628 void
2629out_str(s)
2630 char_u *s;
2631{
2632 if (s != NULL && *s)
2633 {
2634#ifdef FEAT_GUI
2635 /* Don't use tputs() when GUI is used, ncurses crashes. */
2636 if (gui.in_use)
2637 {
2638 out_str_nf(s);
2639 return;
2640 }
2641#endif
2642 /* avoid terminal strings being split up */
2643 if (out_pos > OUT_SIZE - 20)
2644 out_flush();
2645#ifdef HAVE_TGETENT
2646 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2647#else
2648 while (*s)
2649 out_char_nf(*s++);
2650#endif
2651
2652 /* For testing we write one string at a time. */
2653 if (p_wd)
2654 out_flush();
2655 }
2656}
2657
2658/*
2659 * cursor positioning using termcap parser. (jw)
2660 */
2661 void
2662term_windgoto(row, col)
2663 int row;
2664 int col;
2665{
2666 OUT_STR(tgoto((char *)T_CM, col, row));
2667}
2668
2669 void
2670term_cursor_right(i)
2671 int i;
2672{
2673 OUT_STR(tgoto((char *)T_CRI, 0, i));
2674}
2675
2676 void
2677term_append_lines(line_count)
2678 int line_count;
2679{
2680 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2681}
2682
2683 void
2684term_delete_lines(line_count)
2685 int line_count;
2686{
2687 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2688}
2689
2690#if defined(HAVE_TGETENT) || defined(PROTO)
2691 void
2692term_set_winpos(x, y)
2693 int x;
2694 int y;
2695{
2696 /* Can't handle a negative value here */
2697 if (x < 0)
2698 x = 0;
2699 if (y < 0)
2700 y = 0;
2701 OUT_STR(tgoto((char *)T_CWP, y, x));
2702}
2703
2704 void
2705term_set_winsize(width, height)
2706 int width;
2707 int height;
2708{
2709 OUT_STR(tgoto((char *)T_CWS, height, width));
2710}
2711#endif
2712
2713 void
2714term_fg_color(n)
2715 int n;
2716{
2717 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2718 if (*T_CAF)
2719 term_color(T_CAF, n);
2720 else if (*T_CSF)
2721 term_color(T_CSF, n);
2722}
2723
2724 void
2725term_bg_color(n)
2726 int n;
2727{
2728 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2729 if (*T_CAB)
2730 term_color(T_CAB, n);
2731 else if (*T_CSB)
2732 term_color(T_CSB, n);
2733}
2734
2735 static void
2736term_color(s, n)
2737 char_u *s;
2738 int n;
2739{
2740 char buf[20];
2741 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2742
2743 /* Special handling of 16 colors, because termcap can't handle it */
2744 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2745 /* Also accept CSI instead of <Esc>[ */
2746 if (n >= 8 && t_colors >= 16
2747 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2748 && s[i] != NUL
2749 && (STRCMP(s + i + 1, "%p1%dm") == 0
2750 || STRCMP(s + i + 1, "%dm") == 0)
2751 && (s[i] == '3' || s[i] == '4'))
2752 {
2753 sprintf(buf,
2754#ifdef TERMINFO
2755 "%s%s%%p1%%dm",
2756#else
2757 "%s%s%%dm",
2758#endif
2759 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2760 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2761 : (n >= 16 ? "48;5;" : "10"));
2762 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2763 }
2764 else
2765 OUT_STR(tgoto((char *)s, 0, n));
2766}
2767
2768#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X))) || defined(PROTO)
2769/*
2770 * Generic function to set window title, using t_ts and t_fs.
2771 */
2772 void
2773term_settitle(title)
2774 char_u *title;
2775{
2776 /* t_ts takes one argument: column in status line */
2777 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2778 out_str_nf(title);
2779 out_str(T_FS); /* set title end */
2780 out_flush();
2781}
2782#endif
2783
2784/*
2785 * Make sure we have a valid set or terminal options.
2786 * Replace all entries that are NULL by empty_option
2787 */
2788 void
2789ttest(pairs)
2790 int pairs;
2791{
2792 check_options(); /* make sure no options are NULL */
2793
2794 /*
2795 * MUST have "cm": cursor motion.
2796 */
2797 if (*T_CM == NUL)
2798 EMSG(_("E437: terminal capability \"cm\" required"));
2799
2800 /*
2801 * if "cs" defined, use a scroll region, it's faster.
2802 */
2803 if (*T_CS != NUL)
2804 scroll_region = TRUE;
2805 else
2806 scroll_region = FALSE;
2807
2808 if (pairs)
2809 {
2810 /*
2811 * optional pairs
2812 */
2813 /* TP goes to normal mode for TI (invert) and TB (bold) */
2814 if (*T_ME == NUL)
2815 T_ME = T_MR = T_MD = T_MB = empty_option;
2816 if (*T_SO == NUL || *T_SE == NUL)
2817 T_SO = T_SE = empty_option;
2818 if (*T_US == NUL || *T_UE == NUL)
2819 T_US = T_UE = empty_option;
2820 if (*T_CZH == NUL || *T_CZR == NUL)
2821 T_CZH = T_CZR = empty_option;
2822
2823 /* T_VE is needed even though T_VI is not defined */
2824 if (*T_VE == NUL)
2825 T_VI = empty_option;
2826
2827 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2828 if (*T_ME == NUL)
2829 {
2830 T_ME = T_SE;
2831 T_MR = T_SO;
2832 T_MD = T_SO;
2833 }
2834
2835 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2836 if (*T_SO == NUL)
2837 {
2838 T_SE = T_ME;
2839 if (*T_MR == NUL)
2840 T_SO = T_MD;
2841 else
2842 T_SO = T_MR;
2843 }
2844
2845 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2846 if (*T_CZH == NUL)
2847 {
2848 T_CZR = T_ME;
2849 if (*T_MR == NUL)
2850 T_CZH = T_MD;
2851 else
2852 T_CZH = T_MR;
2853 }
2854
2855 /* "Sb" and "Sf" come in pairs */
2856 if (*T_CSB == NUL || *T_CSF == NUL)
2857 {
2858 T_CSB = empty_option;
2859 T_CSF = empty_option;
2860 }
2861
2862 /* "AB" and "AF" come in pairs */
2863 if (*T_CAB == NUL || *T_CAF == NUL)
2864 {
2865 T_CAB = empty_option;
2866 T_CAF = empty_option;
2867 }
2868
2869 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2870 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002871 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
2873 /* Set 'weirdinvert' according to value of 't_xs' */
2874 p_wiv = (*T_XS != NUL);
2875 }
2876 need_gather = TRUE;
2877
2878 /* Set t_colors to the value of t_Co. */
2879 t_colors = atoi((char *)T_CCO);
2880}
2881
2882#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2883 || defined(PROTO)
2884/*
2885 * Represent the given long_u as individual bytes, with the most significant
2886 * byte first, and store them in dst.
2887 */
2888 void
2889add_long_to_buf(val, dst)
2890 long_u val;
2891 char_u *dst;
2892{
2893 int i;
2894 int shift;
2895
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002896 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 {
2898 shift = 8 * (sizeof(long_u) - i);
2899 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2900 }
2901}
2902
2903static int get_long_from_buf __ARGS((char_u *buf, long_u *val));
2904
2905/*
2906 * Interpret the next string of bytes in buf as a long integer, with the most
2907 * significant byte first. Note that it is assumed that buf has been through
2908 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
2909 * Puts result in val, and returns the number of bytes read from buf
2910 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
2911 * were present.
2912 */
2913 static int
2914get_long_from_buf(buf, val)
2915 char_u *buf;
2916 long_u *val;
2917{
2918 int len;
2919 char_u bytes[sizeof(long_u)];
2920 int i;
2921 int shift;
2922
2923 *val = 0;
2924 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
2925 if (len != -1)
2926 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002927 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 {
2929 shift = 8 * (sizeof(long_u) - 1 - i);
2930 *val += (long_u)bytes[i] << shift;
2931 }
2932 }
2933 return len;
2934}
2935#endif
2936
2937#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002938 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
2939 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940/*
2941 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
2942 * that buf has been through inchar(). Returns the actual number of bytes used
2943 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
2944 * available.
2945 */
2946 static int
2947get_bytes_from_buf(buf, bytes, num_bytes)
2948 char_u *buf;
2949 char_u *bytes;
2950 int num_bytes;
2951{
2952 int len = 0;
2953 int i;
2954 char_u c;
2955
2956 for (i = 0; i < num_bytes; i++)
2957 {
2958 if ((c = buf[len++]) == NUL)
2959 return -1;
2960 if (c == K_SPECIAL)
2961 {
2962 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
2963 return -1;
2964 if (buf[len++] == (int)KS_ZERO)
2965 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02002966 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
2967 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
2968 if (buf[len++] == (int)KE_CSI)
2969 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00002971 else if (c == CSI && buf[len] == KS_EXTRA
2972 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002973 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
2974 * the start of a special key, see add_to_input_buf_csi(). */
2975 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 bytes[i] = c;
2977 }
2978 return len;
2979}
2980#endif
2981
2982/*
Bram Moolenaare057d402013-06-30 17:51:51 +02002983 * Check if the new shell size is valid, correct it if it's too small or way
2984 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 */
2986 void
2987check_shellsize()
2988{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 if (Rows < min_rows()) /* need room for one window and command line */
2990 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02002991 limit_screen_size();
2992}
2993
2994/*
2995 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
2996 */
2997 void
2998limit_screen_size()
2999{
3000 if (Columns < MIN_COLUMNS)
3001 Columns = MIN_COLUMNS;
3002 else if (Columns > 10000)
3003 Columns = 10000;
3004 if (Rows > 1000)
3005 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006}
3007
Bram Moolenaar86b68352004-12-27 21:59:20 +00003008/*
3009 * Invoked just before the screen structures are going to be (re)allocated.
3010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 void
3012win_new_shellsize()
3013{
3014 static int old_Rows = 0;
3015 static int old_Columns = 0;
3016
3017 if (old_Rows != Rows || old_Columns != Columns)
3018 ui_new_shellsize();
3019 if (old_Rows != Rows)
3020 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003021 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003022 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003023 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 old_Rows = Rows;
3025 shell_new_rows(); /* update window sizes */
3026 }
3027 if (old_Columns != Columns)
3028 {
3029 old_Columns = Columns;
3030#ifdef FEAT_VERTSPLIT
3031 shell_new_columns(); /* update window sizes */
3032#endif
3033 }
3034}
3035
3036/*
3037 * Call this function when the Vim shell has been resized in any way.
3038 * Will obtain the current size and redraw (also when size didn't change).
3039 */
3040 void
3041shell_resized()
3042{
3043 set_shellsize(0, 0, FALSE);
3044}
3045
3046/*
3047 * Check if the shell size changed. Handle a resize.
3048 * When the size didn't change, nothing happens.
3049 */
3050 void
3051shell_resized_check()
3052{
3053 int old_Rows = Rows;
3054 int old_Columns = Columns;
3055
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003056 if (!exiting
3057#ifdef FEAT_GUI
3058 /* Do not get the size when executing a shell command during
3059 * startup. */
3060 && !gui.starting
3061#endif
3062 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003063 {
3064 (void)ui_get_shellsize();
3065 check_shellsize();
3066 if (old_Rows != Rows || old_Columns != Columns)
3067 shell_resized();
3068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069}
3070
3071/*
3072 * Set size of the Vim shell.
3073 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3074 * window size (this is used for the :win command).
3075 * If 'mustset' is FALSE, we may try to get the real window size and if
3076 * it fails use 'width' and 'height'.
3077 */
3078 void
3079set_shellsize(width, height, mustset)
3080 int width, height;
3081 int mustset;
3082{
3083 static int busy = FALSE;
3084
3085 /*
3086 * Avoid recursiveness, can happen when setting the window size causes
3087 * another window-changed signal.
3088 */
3089 if (busy)
3090 return;
3091
3092 if (width < 0 || height < 0) /* just checking... */
3093 return;
3094
Bram Moolenaara971b822011-09-14 14:43:25 +02003095 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003097 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 State = SETWSIZE;
3099 return;
3100 }
3101
Bram Moolenaara971b822011-09-14 14:43:25 +02003102 /* curwin->w_buffer can be NULL when we are closing a window and the
3103 * buffer has already been closed and removing a scrollbar causes a resize
3104 * event. Don't resize then, it will happen after entering another buffer.
3105 */
3106 if (curwin->w_buffer == NULL)
3107 return;
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 ++busy;
3110
3111#ifdef AMIGA
3112 out_flush(); /* must do this before mch_get_shellsize() for
3113 some obscure reason */
3114#endif
3115
3116 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3117 {
3118 Rows = height;
3119 Columns = width;
3120 check_shellsize();
3121 ui_set_shellsize(mustset);
3122 }
3123 else
3124 check_shellsize();
3125
3126 /* The window layout used to be adjusted here, but it now happens in
3127 * screenalloc() (also invoked from screenclear()). That is because the
3128 * "busy" check above may skip this, but not screenalloc(). */
3129
3130 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3131 screenclear();
3132 else
3133 screen_start(); /* don't know where cursor is now */
3134
3135 if (starting != NO_SCREEN)
3136 {
3137#ifdef FEAT_TITLE
3138 maketitle();
3139#endif
3140 changed_line_abv_curs();
3141 invalidate_botline();
3142
3143 /*
3144 * We only redraw when it's needed:
3145 * - While at the more prompt or executing an external command, don't
3146 * redraw, but position the cursor.
3147 * - While editing the command line, only redraw that.
3148 * - in Ex mode, don't redraw anything.
3149 * - Otherwise, redraw right now, and position the cursor.
3150 * Always need to call update_screen() or screenalloc(), to make
3151 * sure Rows/Columns and the size of ScreenLines[] is correct!
3152 */
3153 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3154 || exmode_active)
3155 {
3156 screenalloc(FALSE);
3157 repeat_message();
3158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 else
3160 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003161#ifdef FEAT_SCROLLBIND
3162 if (curwin->w_p_scb)
3163 do_check_scrollbind(TRUE);
3164#endif
3165 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003166 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003167 update_screen(NOT_VALID);
3168 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003169 }
3170 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003171 {
3172 update_topline();
3173#if defined(FEAT_INS_EXPAND)
3174 if (pum_visible())
3175 {
3176 redraw_later(NOT_VALID);
3177 ins_compl_show_pum(); /* This includes the redraw. */
3178 }
3179 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003180#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003181 update_screen(NOT_VALID);
3182 if (redrawing())
3183 setcursor();
3184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 }
3186 cursor_on(); /* redrawing may have switched it off */
3187 }
3188 out_flush();
3189 --busy;
3190}
3191
3192/*
3193 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3194 * commands and Ex mode).
3195 */
3196 void
3197settmode(tmode)
3198 int tmode;
3199{
3200#ifdef FEAT_GUI
3201 /* don't set the term where gvim was started to any mode */
3202 if (gui.in_use)
3203 return;
3204#endif
3205
3206 if (full_screen)
3207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 /*
3209 * When returning after calling a shell we want to really set the
3210 * terminal to raw mode, even though we think it already is, because
3211 * the shell program may have reset the terminal mode.
3212 * When we think the terminal is normal, don't try to set it to
3213 * normal again, because that causes problems (logout!) on some
3214 * machines.
3215 */
3216 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3217 {
3218#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003219# ifdef FEAT_GUI
3220 if (!gui.in_use && !gui.starting)
3221# endif
3222 {
3223 /* May need to check for T_CRV response and termcodes, it
3224 * doesn't work in Cooked mode, an external program may get
3225 * them. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003226 if (tmode != TMODE_RAW && (crv_status == CRV_SENT
3227 || u7_status == U7_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003228 (void)vpeekc_nomap();
3229 check_for_codes_from_term();
3230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231#endif
3232#ifdef FEAT_MOUSE_TTY
3233 if (tmode != TMODE_RAW)
3234 mch_setmouse(FALSE); /* switch mouse off */
3235#endif
3236 out_flush();
3237 mch_settmode(tmode); /* machine specific function */
3238 cur_tmode = tmode;
3239#ifdef FEAT_MOUSE
3240 if (tmode == TMODE_RAW)
3241 setmouse(); /* may switch mouse on */
3242#endif
3243 out_flush();
3244 }
3245#ifdef FEAT_TERMRESPONSE
3246 may_req_termresponse();
3247#endif
3248 }
3249}
3250
3251 void
3252starttermcap()
3253{
3254 if (full_screen && !termcap_active)
3255 {
3256 out_str(T_TI); /* start termcap mode */
3257 out_str(T_KS); /* start "keypad transmit" mode */
3258 out_flush();
3259 termcap_active = TRUE;
3260 screen_start(); /* don't know where cursor is now */
3261#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003262# ifdef FEAT_GUI
3263 if (!gui.in_use && !gui.starting)
3264# endif
3265 {
3266 may_req_termresponse();
3267 /* Immediately check for a response. If t_Co changes, we don't
3268 * want to redraw with wrong colors first. */
Bram Moolenaar90013c62014-05-22 18:14:31 +02003269 if (crv_status == CRV_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003270 check_for_codes_from_term();
3271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#endif
3273 }
3274}
3275
3276 void
3277stoptermcap()
3278{
3279 screen_stop_highlight();
3280 reset_cterm_colors();
3281 if (termcap_active)
3282 {
3283#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003284# ifdef FEAT_GUI
3285 if (!gui.in_use && !gui.starting)
3286# endif
3287 {
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003288 /* May need to discard T_CRV or T_U7 response. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003289 if (crv_status == CRV_SENT || u7_status == U7_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003290 {
3291# ifdef UNIX
3292 /* Give the terminal a chance to respond. */
3293 mch_delay(100L, FALSE);
3294# endif
3295# ifdef TCIFLUSH
3296 /* Discard data received but not read. */
3297 if (exiting)
3298 tcflush(fileno(stdin), TCIFLUSH);
3299# endif
3300 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003301 /* Check for termcodes first, otherwise an external program may
3302 * get them. */
3303 check_for_codes_from_term();
3304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305#endif
3306 out_str(T_KE); /* stop "keypad transmit" mode */
3307 out_flush();
3308 termcap_active = FALSE;
3309 cursor_on(); /* just in case it is still off */
3310 out_str(T_TE); /* stop termcap mode */
3311 screen_start(); /* don't know where cursor is now */
3312 out_flush();
3313 }
3314}
3315
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003316#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317/*
3318 * Request version string (for xterm) when needed.
3319 * Only do this after switching to raw mode, otherwise the result will be
3320 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003321 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003322 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 * Only do this after termcap mode has been started, otherwise the codes for
3324 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003325 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3326 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003327 * On Unix only do it when both output and input are a tty (avoid writing
3328 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 * The result is caught in check_termcode().
3330 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003331 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332may_req_termresponse()
3333{
3334 if (crv_status == CRV_GET
3335 && cur_tmode == TMODE_RAW
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003336 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 && termcap_active
Bram Moolenaarebefac62005-12-28 22:39:57 +00003338 && p_ek
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003339# ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 && isatty(1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003341 && isatty(read_cmd_fd)
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003342# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 && *T_CRV != NUL)
3344 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02003345 LOG_TR("Sending CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 out_str(T_CRV);
3347 crv_status = CRV_SENT;
3348 /* check for the characters now, otherwise they might be eaten by
3349 * get_keystroke() */
3350 out_flush();
3351 (void)vpeekc_nomap();
3352 }
3353}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003354
3355# if defined(FEAT_MBYTE) || defined(PROTO)
3356/*
3357 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003358 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003359 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003360 * If the terminal treats \u25bd as single width, the position is (1, 1),
3361 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003362 * This function has the side effect that changes cursor position, so
3363 * it must be called immediately after entering termcap mode.
3364 */
3365 void
Bram Moolenaar386dcde2013-09-29 16:27:47 +02003366may_req_ambiguous_char_width()
Bram Moolenaar9584b312013-03-13 19:29:28 +01003367{
3368 if (u7_status == U7_GET
3369 && cur_tmode == TMODE_RAW
3370 && termcap_active
3371 && p_ek
3372# ifdef UNIX
3373 && isatty(1)
3374 && isatty(read_cmd_fd)
3375# endif
3376 && *T_U7 != NUL
3377 && !option_was_set((char_u *)"ambiwidth"))
3378 {
3379 char_u buf[16];
3380
Bram Moolenaar2951b772013-07-03 12:45:31 +02003381 LOG_TR("Sending U7 request");
3382 /* Do this in the second row. In the first row the returned sequence
3383 * may be CSI 1;2R, which is the same as <S-F3>. */
3384 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003385 buf[mb_char2bytes(0x25bd, buf)] = 0;
3386 out_str(buf);
3387 out_str(T_U7);
3388 u7_status = U7_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003389 out_flush();
3390 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003391 out_str((char_u *)" ");
3392 term_windgoto(0, 0);
3393 /* check for the characters now, otherwise they might be eaten by
3394 * get_keystroke() */
3395 out_flush();
3396 (void)vpeekc_nomap();
3397 }
3398}
3399# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003400
3401# ifdef DEBUG_TERMRESPONSE
3402 static void
3403log_tr(char *msg)
3404{
3405 static FILE *fd_tr = NULL;
3406 static proftime_T start;
3407 proftime_T now;
3408
3409 if (fd_tr == NULL)
3410 {
3411 fd_tr = fopen("termresponse.log", "w");
3412 profile_start(&start);
3413 }
3414 now = start;
3415 profile_end(&now);
3416 fprintf(fd_tr, "%s: %s %s\n",
3417 profile_msg(&now),
3418 must_redraw == NOT_VALID ? "NV"
3419 : must_redraw == CLEAR ? "CL" : " ",
3420 msg);
3421}
3422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423#endif
3424
3425/*
3426 * Return TRUE when saving and restoring the screen.
3427 */
3428 int
3429swapping_screen()
3430{
3431 return (full_screen && *T_TI != NUL);
3432}
3433
3434#ifdef FEAT_MOUSE
3435/*
3436 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3437 */
3438 void
3439setmouse()
3440{
3441# ifdef FEAT_MOUSE_TTY
3442 int checkfor;
3443# endif
3444
3445# ifdef FEAT_MOUSESHAPE
3446 update_mouseshape(-1);
3447# endif
3448
3449# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3450# ifdef FEAT_GUI
3451 /* In the GUI the mouse is always enabled. */
3452 if (gui.in_use)
3453 return;
3454# endif
3455 /* be quick when mouse is off */
3456 if (*p_mouse == NUL || has_mouse_termcode == 0)
3457 return;
3458
3459 /* don't switch mouse on when not in raw mode (Ex mode) */
3460 if (cur_tmode != TMODE_RAW)
3461 {
3462 mch_setmouse(FALSE);
3463 return;
3464 }
3465
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 if (VIsual_active)
3467 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003468 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 checkfor = MOUSE_RETURN;
3470 else if (State & INSERT)
3471 checkfor = MOUSE_INSERT;
3472 else if (State & CMDLINE)
3473 checkfor = MOUSE_COMMAND;
3474 else if (State == CONFIRM || State == EXTERNCMD)
3475 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3476 else
3477 checkfor = MOUSE_NORMAL; /* assume normal mode */
3478
3479 if (mouse_has(checkfor))
3480 mch_setmouse(TRUE);
3481 else
3482 mch_setmouse(FALSE);
3483# endif
3484}
3485
3486/*
3487 * Return TRUE if
3488 * - "c" is in 'mouse', or
3489 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3490 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3491 * normal editing mode (not at hit-return message).
3492 */
3493 int
3494mouse_has(c)
3495 int c;
3496{
3497 char_u *p;
3498
3499 for (p = p_mouse; *p; ++p)
3500 switch (*p)
3501 {
3502 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3503 return TRUE;
3504 break;
3505 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3506 return TRUE;
3507 break;
3508 default: if (c == *p) return TRUE; break;
3509 }
3510 return FALSE;
3511}
3512
3513/*
3514 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3515 */
3516 int
3517mouse_model_popup()
3518{
3519 return (p_mousem[0] == 'p');
3520}
3521#endif
3522
3523/*
3524 * By outputting the 'cursor very visible' termcap code, for some windowed
3525 * terminals this makes the screen scrolled to the correct position.
3526 * Used when starting Vim or returning from a shell.
3527 */
3528 void
3529scroll_start()
3530{
3531 if (*T_VS != NUL)
3532 {
3533 out_str(T_VS);
3534 out_str(T_VE);
3535 screen_start(); /* don't know where cursor is now */
3536 }
3537}
3538
3539static int cursor_is_off = FALSE;
3540
3541/*
3542 * Enable the cursor.
3543 */
3544 void
3545cursor_on()
3546{
3547 if (cursor_is_off)
3548 {
3549 out_str(T_VE);
3550 cursor_is_off = FALSE;
3551 }
3552}
3553
3554/*
3555 * Disable the cursor.
3556 */
3557 void
3558cursor_off()
3559{
3560 if (full_screen)
3561 {
3562 if (!cursor_is_off)
3563 out_str(T_VI); /* disable cursor */
3564 cursor_is_off = TRUE;
3565 }
3566}
3567
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003568#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569/*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003570 * Set cursor shape to match Insert mode.
3571 */
3572 void
3573term_cursor_shape()
3574{
3575 static int showing_insert_mode = MAYBE;
3576
3577 if (!full_screen || *T_CSI == NUL || *T_CEI == NUL)
3578 return;
3579
3580 if (State & INSERT)
3581 {
3582 if (showing_insert_mode != TRUE)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003583 out_str(T_CSI); /* Insert mode cursor */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003584 showing_insert_mode = TRUE;
3585 }
3586 else
3587 {
3588 if (showing_insert_mode != FALSE)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003589 out_str(T_CEI); /* non-Insert mode cursor */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003590 showing_insert_mode = FALSE;
3591 }
3592}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003593#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003594
3595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 * Set scrolling region for window 'wp'.
3597 * The region starts 'off' lines from the start of the window.
3598 * Also set the vertical scroll region for a vertically split window. Always
3599 * the full width of the window, excluding the vertical separator.
3600 */
3601 void
3602scroll_region_set(wp, off)
3603 win_T *wp;
3604 int off;
3605{
3606 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3607 W_WINROW(wp) + off));
3608#ifdef FEAT_VERTSPLIT
3609 if (*T_CSV != NUL && wp->w_width != Columns)
3610 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3611 W_WINCOL(wp)));
3612#endif
3613 screen_start(); /* don't know where cursor is now */
3614}
3615
3616/*
3617 * Reset scrolling region to the whole screen.
3618 */
3619 void
3620scroll_region_reset()
3621{
3622 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
3623#ifdef FEAT_VERTSPLIT
3624 if (*T_CSV != NUL)
3625 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3626#endif
3627 screen_start(); /* don't know where cursor is now */
3628}
3629
3630
3631/*
3632 * List of terminal codes that are currently recognized.
3633 */
3634
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003635static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636{
3637 char_u name[2]; /* termcap name of entry */
3638 char_u *code; /* terminal code (in allocated memory) */
3639 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003640 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641} *termcodes = NULL;
3642
3643static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3644static int tc_len = 0; /* current number of entries in termcodes[] */
3645
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003646static int termcode_star __ARGS((char_u *code, int len));
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 void
3649clear_termcodes()
3650{
3651 while (tc_len > 0)
3652 vim_free(termcodes[--tc_len].code);
3653 vim_free(termcodes);
3654 termcodes = NULL;
3655 tc_max_len = 0;
3656
3657#ifdef HAVE_TGETENT
3658 BC = (char *)empty_option;
3659 UP = (char *)empty_option;
3660 PC = NUL; /* set pad character to NUL */
3661 ospeed = 0;
3662#endif
3663
3664 need_gather = TRUE; /* need to fill termleader[] */
3665}
3666
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003667#define ATC_FROM_TERM 55
3668
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669/*
3670 * Add a new entry to the list of terminal codes.
3671 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003672 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3673 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 */
3675 void
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003676add_termcode(name, string, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 char_u *name;
3678 char_u *string;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003679 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 struct termcode *new_tc;
3682 int i, j;
3683 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003684 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
3686 if (string == NULL || *string == NUL)
3687 {
3688 del_termcode(name);
3689 return;
3690 }
3691
Bram Moolenaar45500912014-07-09 20:51:07 +02003692#if defined(WIN3264) && !defined(FEAT_GUI)
3693 s = vim_strnsave(string, (int)STRLEN(string) + 1);
3694#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 s = vim_strsave(string);
Bram Moolenaar45500912014-07-09 20:51:07 +02003696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (s == NULL)
3698 return;
3699
3700 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003701 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003703 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 s[0] = term_7to8bit(string);
3705 }
Bram Moolenaar45500912014-07-09 20:51:07 +02003706
3707#if defined(WIN3264) && !defined(FEAT_GUI)
3708 if (s[0] == K_NUL)
3709 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02003710 STRMOVE(s + 1, s);
3711 s[1] = 3;
Bram Moolenaar45500912014-07-09 20:51:07 +02003712 }
3713#endif
3714
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003715 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
3717 need_gather = TRUE; /* need to fill termleader[] */
3718
3719 /*
3720 * need to make space for more entries
3721 */
3722 if (tc_len == tc_max_len)
3723 {
3724 tc_max_len += 20;
3725 new_tc = (struct termcode *)alloc(
3726 (unsigned)(tc_max_len * sizeof(struct termcode)));
3727 if (new_tc == NULL)
3728 {
3729 tc_max_len -= 20;
3730 return;
3731 }
3732 for (i = 0; i < tc_len; ++i)
3733 new_tc[i] = termcodes[i];
3734 vim_free(termcodes);
3735 termcodes = new_tc;
3736 }
3737
3738 /*
3739 * Look for existing entry with the same name, it is replaced.
3740 * Look for an existing entry that is alphabetical higher, the new entry
3741 * is inserted in front of it.
3742 */
3743 for (i = 0; i < tc_len; ++i)
3744 {
3745 if (termcodes[i].name[0] < name[0])
3746 continue;
3747 if (termcodes[i].name[0] == name[0])
3748 {
3749 if (termcodes[i].name[1] < name[1])
3750 continue;
3751 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003752 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 */
3754 if (termcodes[i].name[1] == name[1])
3755 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003756 if (flags == ATC_FROM_TERM && (j = termcode_star(
3757 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003758 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003759 /* Don't replace ESC[123;*X or ESC O*X with another when
3760 * invoked from got_code_from_term(). */
3761 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003762 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003763 && s[len - 1]
3764 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003765 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003766 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003767 vim_free(s);
3768 return;
3769 }
3770 }
3771 else
3772 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003773 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003774 vim_free(termcodes[i].code);
3775 --tc_len;
3776 break;
3777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779 }
3780 /*
3781 * Found alphabetical larger entry, move rest to insert new entry
3782 */
3783 for (j = tc_len; j > i; --j)
3784 termcodes[j] = termcodes[j - 1];
3785 break;
3786 }
3787
3788 termcodes[i].name[0] = name[0];
3789 termcodes[i].name[1] = name[1];
3790 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003791 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003792
3793 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3794 * accept modifiers. */
3795 termcodes[i].modlen = 0;
3796 j = termcode_star(s, len);
3797 if (j > 0)
3798 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 ++tc_len;
3800}
3801
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003802/*
3803 * Check termcode "code[len]" for ending in ;*X, <Esc>O*X or <M-O>*X.
3804 * The "X" can be any character.
3805 * Return 0 if not found, 2 for ;*X and 1 for O*X and <M-O>*X.
3806 */
3807 static int
3808termcode_star(code, len)
3809 char_u *code;
3810 int len;
3811{
3812 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3813 if (len >= 3 && code[len - 2] == '*')
3814 {
3815 if (len >= 5 && code[len - 3] == ';')
3816 return 2;
3817 if ((len >= 4 && code[len - 3] == 'O') || code[len - 3] == 'O' + 128)
3818 return 1;
3819 }
3820 return 0;
3821}
3822
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 char_u *
3824find_termcode(name)
3825 char_u *name;
3826{
3827 int i;
3828
3829 for (i = 0; i < tc_len; ++i)
3830 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3831 return termcodes[i].code;
3832 return NULL;
3833}
3834
3835#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3836 char_u *
3837get_termcode(i)
3838 int i;
3839{
3840 if (i >= tc_len)
3841 return NULL;
3842 return &termcodes[i].name[0];
3843}
3844#endif
3845
3846 void
3847del_termcode(name)
3848 char_u *name;
3849{
3850 int i;
3851
3852 if (termcodes == NULL) /* nothing there yet */
3853 return;
3854
3855 need_gather = TRUE; /* need to fill termleader[] */
3856
3857 for (i = 0; i < tc_len; ++i)
3858 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3859 {
3860 del_termcode_idx(i);
3861 return;
3862 }
3863 /* not found. Give error message? */
3864}
3865
3866 static void
3867del_termcode_idx(idx)
3868 int idx;
3869{
3870 int i;
3871
3872 vim_free(termcodes[idx].code);
3873 --tc_len;
3874 for (i = idx; i < tc_len; ++i)
3875 termcodes[i] = termcodes[i + 1];
3876}
3877
3878#ifdef FEAT_TERMRESPONSE
3879/*
3880 * Called when detected that the terminal sends 8-bit codes.
3881 * Convert all 7-bit codes to their 8-bit equivalent.
3882 */
3883 static void
3884switch_to_8bit()
3885{
3886 int i;
3887 int c;
3888
3889 /* Only need to do something when not already using 8-bit codes. */
3890 if (!term_is_8bit(T_NAME))
3891 {
3892 for (i = 0; i < tc_len; ++i)
3893 {
3894 c = term_7to8bit(termcodes[i].code);
3895 if (c != 0)
3896 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003897 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 termcodes[i].code[0] = c;
3899 }
3900 }
3901 need_gather = TRUE; /* need to fill termleader[] */
3902 }
3903 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003904 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905}
3906#endif
3907
3908#ifdef CHECK_DOUBLE_CLICK
3909static linenr_T orig_topline = 0;
3910# ifdef FEAT_DIFF
3911static int orig_topfill = 0;
3912# endif
3913#endif
3914#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
3915/*
3916 * Checking for double clicks ourselves.
3917 * "orig_topline" is used to avoid detecting a double-click when the window
3918 * contents scrolled (e.g., when 'scrolloff' is non-zero).
3919 */
3920/*
3921 * Set orig_topline. Used when jumping to another window, so that a double
3922 * click still works.
3923 */
3924 void
3925set_mouse_topline(wp)
3926 win_T *wp;
3927{
3928 orig_topline = wp->w_topline;
3929# ifdef FEAT_DIFF
3930 orig_topfill = wp->w_topfill;
3931# endif
3932}
3933#endif
3934
3935/*
3936 * Check if typebuf.tb_buf[] contains a terminal key code.
3937 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
3938 * + max_offset].
3939 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003940 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 * With a match, the match is removed, the replacement code is inserted in
3942 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
3943 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003944 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
3945 * "buflen" is then the length of the string in buf[] and is updated for
3946 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 */
3948 int
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003949check_termcode(max_offset, buf, bufsize, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 int max_offset;
3951 char_u *buf;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003952 int bufsize;
3953 int *buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
3955 char_u *tp;
3956 char_u *p;
3957 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003958 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003960 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 int offset;
3962 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003963 int modifiers;
3964 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 int new_slen;
3966 int extra;
3967 char_u string[MAX_KEY_CODE_LEN + 1];
3968 int i, j;
3969 int idx = 0;
3970#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00003971# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
3972 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 char_u bytes[6];
3974 int num_bytes;
3975# endif
3976 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 int is_click, is_drag;
3978 int wheel_code = 0;
3979 int current_button;
3980 static int held_button = MOUSE_RELEASE;
3981 static int orig_num_clicks = 1;
3982 static int orig_mouse_code = 0x0;
3983# ifdef CHECK_DOUBLE_CLICK
3984 static int orig_mouse_col = 0;
3985 static int orig_mouse_row = 0;
3986 static struct timeval orig_mouse_time = {0, 0};
3987 /* time of previous mouse click */
3988 struct timeval mouse_time; /* time of current mouse click */
3989 long timediff; /* elapsed time in msec */
3990# endif
3991#endif
3992 int cpo_koffset;
3993#ifdef FEAT_MOUSE_GPM
3994 extern int gpm_flag; /* gpm library variable */
3995#endif
3996
3997 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
3998
3999 /*
4000 * Speed up the checks for terminal codes by gathering all first bytes
4001 * used in termleader[]. Often this is just a single <Esc>.
4002 */
4003 if (need_gather)
4004 gather_termleader();
4005
4006 /*
4007 * Check at several positions in typebuf.tb_buf[], to catch something like
4008 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4009 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004010 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 * This is used often, KEEP IT FAST!
4012 */
4013 for (offset = 0; offset < max_offset; ++offset)
4014 {
4015 if (buf == NULL)
4016 {
4017 if (offset >= typebuf.tb_len)
4018 break;
4019 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4020 len = typebuf.tb_len - offset; /* length of the input */
4021 }
4022 else
4023 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004024 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 break;
4026 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004027 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029
4030 /*
4031 * Don't check characters after K_SPECIAL, those are already
4032 * translated terminal chars (avoid translating ~@^Hx).
4033 */
4034 if (*tp == K_SPECIAL)
4035 {
4036 offset += 2; /* there are always 2 extra characters */
4037 continue;
4038 }
4039
4040 /*
4041 * Skip this position if the character does not appear as the first
4042 * character in term_strings. This speeds up a lot, since most
4043 * termcodes start with the same character (ESC or CSI).
4044 */
4045 i = *tp;
4046 for (p = termleader; *p && *p != i; ++p)
4047 ;
4048 if (*p == NUL)
4049 continue;
4050
4051 /*
4052 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4053 * are in Insert mode.
4054 */
4055 if (*tp == ESC && !p_ek && (State & INSERT))
4056 continue;
4057
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004059 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004060 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
4062#ifdef FEAT_GUI
4063 if (gui.in_use)
4064 {
4065 /*
4066 * GUI special key codes are all of the form [CSI xx].
4067 */
4068 if (*tp == CSI) /* Special key from GUI */
4069 {
4070 if (len < 3)
4071 return -1; /* Shouldn't happen */
4072 slen = 3;
4073 key_name[0] = tp[1];
4074 key_name[1] = tp[2];
4075 }
4076 }
4077 else
4078#endif /* FEAT_GUI */
4079 {
4080 for (idx = 0; idx < tc_len; ++idx)
4081 {
4082 /*
4083 * Ignore the entry if we are not at the start of
4084 * typebuf.tb_buf[]
4085 * and there are not enough characters to make a match.
4086 * But only when the 'K' flag is in 'cpoptions'.
4087 */
4088 slen = termcodes[idx].len;
4089 if (cpo_koffset && offset && len < slen)
4090 continue;
4091 if (STRNCMP(termcodes[idx].code, tp,
4092 (size_t)(slen > len ? len : slen)) == 0)
4093 {
4094 if (len < slen) /* got a partial sequence */
4095 return -1; /* need to get more chars */
4096
4097 /*
4098 * When found a keypad key, check if there is another key
4099 * that matches and use that one. This makes <Home> to be
4100 * found instead of <kHome> when they produce the same
4101 * key code.
4102 */
4103 if (termcodes[idx].name[0] == 'K'
4104 && VIM_ISDIGIT(termcodes[idx].name[1]))
4105 {
4106 for (j = idx + 1; j < tc_len; ++j)
4107 if (termcodes[j].len == slen &&
4108 STRNCMP(termcodes[idx].code,
4109 termcodes[j].code, slen) == 0)
4110 {
4111 idx = j;
4112 break;
4113 }
4114 }
4115
4116 key_name[0] = termcodes[idx].name[0];
4117 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 break;
4119 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004120
4121 /*
4122 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004123 * <Esc>[123;*X (modslen == slen - 3)
4124 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4125 * When there is a modifier the * matches a number.
4126 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004127 */
4128 if (termcodes[idx].modlen > 0)
4129 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004130 modslen = termcodes[idx].modlen;
4131 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004132 continue;
4133 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004134 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004135 {
4136 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004137
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004138 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004139 return -1; /* need to get more chars */
4140
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004141 if (tp[modslen] == termcodes[idx].code[slen - 1])
4142 slen = modslen + 1; /* no modifiers */
4143 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004144 continue; /* no match */
4145 else
4146 {
4147 /* Skip over the digits, the final char must
4148 * follow. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004149 for (j = slen - 2; j < len && isdigit(tp[j]); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004150 ;
4151 ++j;
4152 if (len < j) /* got a partial sequence */
4153 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004154 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4155 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004156
4157 /* Match! Convert modifier bits. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004158 n = atoi((char *)tp + slen - 2) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004159 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004160 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004161 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004162 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004163 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004164 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004165 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004166 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004167
4168 slen = j;
4169 }
4170 key_name[0] = termcodes[idx].name[0];
4171 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004172 break;
4173 }
4174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 }
4176 }
4177
4178#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004179 if (key_name[0] == NUL
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004180 /* Mouse codes of DEC, pterm, and URXVT start with <ESC>[. When
4181 * detecting the start of these mouse codes they might as well be
4182 * another key code or terminal response. */
4183# ifdef FEAT_MOUSE_DEC
4184 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004185# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004186# ifdef FEAT_MOUSE_PTERM
4187 || key_name[0] == KS_PTERM_MOUSE
4188# endif
4189# ifdef FEAT_MOUSE_URXVT
4190 || key_name[0] == KS_URXVT_MOUSE
4191# endif
4192 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004194 /* Check for some responses from the terminal starting with
4195 * "<Esc>[" or CSI:
Bram Moolenaar9584b312013-03-13 19:29:28 +01004196 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004197 * - Xterm version string: <Esc>[>{x};{vers};{y}c
Bram Moolenaar9584b312013-03-13 19:29:28 +01004198 * Also eat other possible responses to t_RV, rxvt returns
4199 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4200 * mrxvt has been reported to have "+" in the version. Assume
4201 * the escape sequence ends with a letter or one of "{|}~".
4202 *
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004203 * - Cursor position report: <Esc>[{row};{col}R
4204 * The final byte must be 'R'. It is used for checking the
Bram Moolenaar9584b312013-03-13 19:29:28 +01004205 * ambiguous-width character state.
4206 */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004207 p = tp[0] == CSI ? tp + 1 : tp + 2;
Bram Moolenaar9584b312013-03-13 19:29:28 +01004208 if ((*T_CRV != NUL || *T_U7 != NUL)
4209 && ((tp[0] == ESC && tp[1] == '[' && len >= 3)
Bram Moolenaar46a75612013-05-15 14:22:41 +02004210 || (tp[0] == CSI && len >= 2))
4211 && (VIM_ISDIGIT(*p) || *p == '>' || *p == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004213#ifdef FEAT_MBYTE
4214 int col;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004215 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 j = 0;
4218 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004219 for (i = 2 + (tp[0] != CSI); i < len
4220 && !(tp[i] >= '{' && tp[i] <= '~')
4221 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 if (tp[i] == ';' && ++j == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004223 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004224 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004225#ifdef FEAT_MBYTE
4226 row_char = tp[i - 1];
4227#endif
4228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004230 {
4231 LOG_TR("Not enough characters for CRV");
4232 return -1;
4233 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004234#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004235 if (extra > 0)
4236 col = atoi((char *)tp + extra);
4237 else
4238 col = 0;
4239
4240 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4241 * u7_status is not "sent", it may be from a previous Vim that
4242 * just exited. But not for <S-F3>, it sends something
4243 * similar, check for row and column to make sense. */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004244 if (j == 1 && tp[i] == 'R')
Bram Moolenaar9584b312013-03-13 19:29:28 +01004245 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004246 if (row_char == '2' && col >= 2)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004247 {
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004248 char *aw = NULL;
Bram Moolenaar2951b772013-07-03 12:45:31 +02004249
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004250 LOG_TR("Received U7 status");
4251 u7_status = U7_GOT;
4252# ifdef FEAT_AUTOCMD
4253 did_cursorhold = TRUE;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004254# endif
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004255 if (col == 2)
4256 aw = "single";
4257 else if (col == 3)
4258 aw = "double";
4259 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4260 {
4261 /* Setting the option causes a screen redraw. Do
4262 * that right away if possible, keeping any
4263 * messages. */
4264 set_option_value((char_u *)"ambw", 0L,
4265 (char_u *)aw, 0);
4266# ifdef DEBUG_TERMRESPONSE
4267 {
4268 char buf[100];
4269 int r = redraw_asap(CLEAR);
4270
4271 sprintf(buf,
4272 "set 'ambiwidth', redraw_asap(): %d",
4273 r);
4274 log_tr(buf);
4275 }
4276# else
4277 redraw_asap(CLEAR);
4278# endif
4279 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02004280 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004281 key_name[0] = (int)KS_EXTRA;
4282 key_name[1] = (int)KE_IGNORE;
4283 slen = i + 1;
4284 }
4285 else
4286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004288 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004290 LOG_TR("Received CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 crv_status = CRV_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004292# ifdef FEAT_AUTOCMD
4293 did_cursorhold = TRUE;
4294# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
4296 /* If this code starts with CSI, you can bet that the
4297 * terminal uses 8-bit codes. */
4298 if (tp[0] == CSI)
4299 switch_to_8bit();
4300
4301 /* rxvt sends its version number: "20703" is 2.7.3.
4302 * Ignore it for when the user has set 'term' to xterm,
4303 * even though it's an rxvt. */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004304 if (extra > 0)
4305 extra = atoi((char *)tp + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 if (extra > 20000)
4307 extra = 0;
4308
4309 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
4310 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004311 /* Only set 'ttymouse' automatically if it was not set
4312 * by the user already. */
4313 if (!option_was_set((char_u *)"ttym"))
4314 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004315# ifdef TTYM_SGR
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004316 if (extra >= 277)
4317 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004318 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004319 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004320# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004321 /* if xterm version >= 95 use mouse dragging */
4322 if (extra >= 95)
4323 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004325 }
4326
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 /* if xterm version >= 141 try to get termcap codes */
4328 if (extra >= 141)
4329 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004330 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 check_for_codes = TRUE;
4332 need_gather = TRUE;
4333 req_codes_from_term();
4334 }
4335 }
4336# ifdef FEAT_EVAL
4337 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4338# endif
4339# ifdef FEAT_AUTOCMD
4340 apply_autocmds(EVENT_TERMRESPONSE,
4341 NULL, NULL, FALSE, curbuf);
4342# endif
4343 key_name[0] = (int)KS_EXTRA;
4344 key_name[1] = (int)KE_IGNORE;
4345 slen = i + 1;
4346 }
4347 }
4348
4349 /* Check for '<Esc>P1+r<hex bytes><Esc>\'. A "0" instead of the
4350 * "1" means an invalid request. */
4351 else if (check_for_codes
4352 && ((tp[0] == ESC && tp[1] == 'P' && len >= 2)
4353 || tp[0] == DCS))
4354 {
4355 j = 1 + (tp[0] != DCS);
4356 for (i = j; i < len; ++i)
4357 if ((tp[i] == ESC && tp[i + 1] == '\\' && i + 1 < len)
4358 || tp[i] == STERM)
4359 {
4360 if (i - j >= 3 && tp[j + 1] == '+' && tp[j + 2] == 'r')
4361 got_code_from_term(tp + j, i);
4362 key_name[0] = (int)KS_EXTRA;
4363 key_name[1] = (int)KE_IGNORE;
4364 slen = i + 1 + (tp[i] == ESC);
4365 break;
4366 }
4367
4368 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004369 {
4370 LOG_TR("not enough characters for XT");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 return -1; /* not enough characters */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374 }
4375#endif
4376
4377 if (key_name[0] == NUL)
4378 continue; /* No match at this position, try next one */
4379
4380 /* We only get here when we have a complete termcode match */
4381
4382#ifdef FEAT_MOUSE
4383# ifdef FEAT_GUI
4384 /*
4385 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4386 * so that we know which window to scroll later.
4387 */
4388 if (gui.in_use
4389 && key_name[0] == (int)KS_EXTRA
4390 && (key_name[1] == (int)KE_X1MOUSE
4391 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004392 || key_name[1] == (int)KE_MOUSELEFT
4393 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 || key_name[1] == (int)KE_MOUSEDOWN
4395 || key_name[1] == (int)KE_MOUSEUP))
4396 {
4397 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4398 if (num_bytes == -1) /* not enough coordinates */
4399 return -1;
4400 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4401 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4402 slen += num_bytes;
4403 }
4404 else
4405# endif
4406 /*
4407 * If it is a mouse click, get the coordinates.
4408 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004409 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004411 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412# endif
4413# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004414 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415# endif
4416# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004417 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418# endif
4419# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004420 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004422# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004423 || key_name[0] == KS_URXVT_MOUSE
4424# endif
4425# ifdef FEAT_MOUSE_SGR
4426 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004427# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 )
4429 {
4430 is_click = is_drag = FALSE;
4431
Bram Moolenaar864207d2008-06-24 22:14:38 +00004432# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4433 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 if (key_name[0] == (int)KS_MOUSE)
4435 {
4436 /*
4437 * For xterm and MSDOS we get "<t_mouse>scr", where
4438 * s == encoded button state:
4439 * 0x20 = left button down
4440 * 0x21 = middle button down
4441 * 0x22 = right button down
4442 * 0x23 = any button release
4443 * 0x60 = button 4 down (scroll wheel down)
4444 * 0x61 = button 5 down (scroll wheel up)
4445 * add 0x04 for SHIFT
4446 * add 0x08 for ALT
4447 * add 0x10 for CTRL
4448 * add 0x20 for mouse drag (0x40 is drag with left button)
4449 * c == column + ' ' + 1 == column + 33
4450 * r == row + ' ' + 1 == row + 33
4451 *
4452 * The coordinates are passed on through global variables.
4453 * Ugly, but this avoids trouble with mouse clicks at an
4454 * unexpected moment and allows for mapping them.
4455 */
4456 for (;;)
4457 {
4458#ifdef FEAT_GUI
4459 if (gui.in_use)
4460 {
4461 /* GUI uses more bits for columns > 223 */
4462 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4463 if (num_bytes == -1) /* not enough coordinates */
4464 return -1;
4465 mouse_code = bytes[0];
4466 mouse_col = 128 * (bytes[1] - ' ' - 1)
4467 + bytes[2] - ' ' - 1;
4468 mouse_row = 128 * (bytes[3] - ' ' - 1)
4469 + bytes[4] - ' ' - 1;
4470 }
4471 else
4472#endif
4473 {
4474 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4475 if (num_bytes == -1) /* not enough coordinates */
4476 return -1;
4477 mouse_code = bytes[0];
4478 mouse_col = bytes[1] - ' ' - 1;
4479 mouse_row = bytes[2] - ' ' - 1;
4480 }
4481 slen += num_bytes;
4482
4483 /* If the following bytes is also a mouse code and it has
4484 * the same code, dump this one and get the next. This
4485 * makes dragging a whole lot faster. */
4486#ifdef FEAT_GUI
4487 if (gui.in_use)
4488 j = 3;
4489 else
4490#endif
4491 j = termcodes[idx].len;
4492 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4493 && tp[slen + j] == mouse_code
4494 && tp[slen + j + 1] != NUL
4495 && tp[slen + j + 2] != NUL
4496#ifdef FEAT_GUI
4497 && (!gui.in_use
4498 || (tp[slen + j + 3] != NUL
4499 && tp[slen + j + 4] != NUL))
4500#endif
4501 )
4502 slen += j;
4503 else
4504 break;
4505 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004508# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4509 if (key_name[0] == KS_URXVT_MOUSE
4510 || key_name[0] == KS_SGR_MOUSE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004511 {
4512 for (;;)
4513 {
4514 /* URXVT 1015 mouse reporting mode:
4515 * Almost identical to xterm mouse mode, except the values
4516 * are decimal instead of bytes.
4517 *
4518 * \033[%d;%d;%dM
4519 * ^-- row
4520 * ^----- column
4521 * ^-------- code
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004522 *
4523 * SGR 1006 mouse reporting mode:
4524 * Almost identical to xterm mouse mode, except the values
4525 * are decimal instead of bytes.
4526 *
4527 * \033[<%d;%d;%dM
4528 * ^-- row
4529 * ^----- column
4530 * ^-------- code
4531 *
4532 * \033[<%d;%d;%dm : mouse release event
4533 * ^-- row
4534 * ^----- column
4535 * ^-------- code
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004536 */
4537 p = tp + slen;
4538
4539 mouse_code = getdigits(&p);
4540 if (*p++ != ';')
4541 return -1;
4542
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004543 /* when mouse reporting is SGR, add 32 to mouse code */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004544 if (key_name[0] == KS_SGR_MOUSE)
4545 mouse_code += 32;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004546
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004547 mouse_col = getdigits(&p) - 1;
4548 if (*p++ != ';')
4549 return -1;
4550
4551 mouse_row = getdigits(&p) - 1;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004552 if (key_name[0] == KS_SGR_MOUSE && *p == 'm')
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004553 mouse_code |= MOUSE_RELEASE;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004554 else if (*p != 'M')
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004555 return -1;
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004556 p++;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004557
4558 slen += (int)(p - (tp + slen));
4559
4560 /* skip this one if next one has same code (like xterm
4561 * case) */
4562 j = termcodes[idx].len;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004563 if (STRNCMP(tp, tp + slen, (size_t)j) == 0)
4564 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004565 int slen2;
4566 int cmd_complete = 0;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004567
4568 /* check if the command is complete by looking for the
4569 * 'M' */
4570 for (slen2 = slen; slen2 < len; slen2++)
4571 {
4572 if (tp[slen2] == 'M'
Bram Moolenaar4e067c82014-07-30 17:21:58 +02004573 || (key_name[0] == KS_SGR_MOUSE
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004574 && tp[slen2] == 'm'))
4575 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004576 cmd_complete = 1;
4577 break;
4578 }
4579 }
4580 p += j;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004581 if (cmd_complete && getdigits(&p) == mouse_code)
4582 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004583 slen += j; /* skip the \033[ */
4584 continue;
4585 }
4586 }
4587 break;
4588 }
4589 }
4590# endif
4591
4592 if (key_name[0] == (int)KS_MOUSE
4593#ifdef FEAT_MOUSE_URXVT
4594 || key_name[0] == (int)KS_URXVT_MOUSE
4595#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004596#ifdef FEAT_MOUSE_SGR
4597 || key_name[0] == KS_SGR_MOUSE
4598#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004599 )
4600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601# if !defined(MSWIN) && !defined(MSDOS)
4602 /*
4603 * Handle mouse events.
4604 * Recognize the xterm mouse wheel, but not in the GUI, the
4605 * Linux console with GPM and the MS-DOS or Win32 console
4606 * (multi-clicks use >= 0x60).
4607 */
4608 if (mouse_code >= MOUSEWHEEL_LOW
4609# ifdef FEAT_GUI
4610 && !gui.in_use
4611# endif
4612# ifdef FEAT_MOUSE_GPM
4613 && gpm_flag == 0
4614# endif
4615 )
4616 {
4617 /* Keep the mouse_code before it's changed, so that we
4618 * remember that it was a mouse wheel click. */
4619 wheel_code = mouse_code;
4620 }
4621# ifdef FEAT_MOUSE_XTERM
4622 else if (held_button == MOUSE_RELEASE
4623# ifdef FEAT_GUI
4624 && !gui.in_use
4625# endif
4626 && (mouse_code == 0x23 || mouse_code == 0x24))
4627 {
4628 /* Apparently used by rxvt scroll wheel. */
4629 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4630 }
4631# endif
4632
4633# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4634 else if (use_xterm_mouse() > 1)
4635 {
4636 if (mouse_code & MOUSE_DRAG_XTERM)
4637 mouse_code |= MOUSE_DRAG;
4638 }
4639# endif
4640# ifdef FEAT_XCLIPBOARD
4641 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4642 {
4643 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4644 stop_xterm_trace();
4645 else
4646 start_xterm_trace(mouse_code);
4647 }
4648# endif
4649# endif
4650 }
4651# endif /* !UNIX || FEAT_MOUSE_XTERM */
4652# ifdef FEAT_MOUSE_NET
4653 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4654 {
4655 int mc, mr;
4656
4657 /* expect a rather limited sequence like: balancing {
4658 * \033}6,45\r
4659 * '6' is the row, 45 is the column
4660 */
4661 p = tp + slen;
4662 mr = getdigits(&p);
4663 if (*p++ != ',')
4664 return -1;
4665 mc = getdigits(&p);
4666 if (*p++ != '\r')
4667 return -1;
4668
4669 mouse_col = mc - 1;
4670 mouse_row = mr - 1;
4671 mouse_code = MOUSE_LEFT;
4672 slen += (int)(p - (tp + slen));
4673 }
4674# endif /* FEAT_MOUSE_NET */
4675# ifdef FEAT_MOUSE_JSB
4676 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4677 {
4678 int mult, val, iter, button, status;
4679
4680 /* JSBTERM Input Model
4681 * \033[0~zw uniq escape sequence
4682 * (L-x) Left button pressed - not pressed x not reporting
4683 * (M-x) Middle button pressed - not pressed x not reporting
4684 * (R-x) Right button pressed - not pressed x not reporting
4685 * (SDmdu) Single , Double click, m mouse move d button down
4686 * u button up
4687 * ### X cursor position padded to 3 digits
4688 * ### Y cursor position padded to 3 digits
4689 * (s-x) SHIFT key pressed - not pressed x not reporting
4690 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004691 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 */
4693
4694 p = tp + slen;
4695 button = mouse_code = 0;
4696 switch (*p++)
4697 {
4698 case 'L': button = 1; break;
4699 case '-': break;
4700 case 'x': break; /* ignore sequence */
4701 default: return -1; /* Unknown Result */
4702 }
4703 switch (*p++)
4704 {
4705 case 'M': button |= 2; break;
4706 case '-': break;
4707 case 'x': break; /* ignore sequence */
4708 default: return -1; /* Unknown Result */
4709 }
4710 switch (*p++)
4711 {
4712 case 'R': button |= 4; break;
4713 case '-': break;
4714 case 'x': break; /* ignore sequence */
4715 default: return -1; /* Unknown Result */
4716 }
4717 status = *p++;
4718 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4719 mult /= 10, p++)
4720 if (*p >= '0' && *p <= '9')
4721 val += (*p - '0') * mult;
4722 else
4723 return -1;
4724 mouse_col = val;
4725 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4726 mult /= 10, p++)
4727 if (*p >= '0' && *p <= '9')
4728 val += (*p - '0') * mult;
4729 else
4730 return -1;
4731 mouse_row = val;
4732 switch (*p++)
4733 {
4734 case 's': button |= 8; break; /* SHIFT key Pressed */
4735 case '-': break; /* Not Pressed */
4736 case 'x': break; /* Not Reporting */
4737 default: return -1; /* Unknown Result */
4738 }
4739 switch (*p++)
4740 {
4741 case 'c': button |= 16; break; /* CTRL key Pressed */
4742 case '-': break; /* Not Pressed */
4743 case 'x': break; /* Not Reporting */
4744 default: return -1; /* Unknown Result */
4745 }
4746 if (*p++ != '\033')
4747 return -1;
4748 if (*p++ != '\\')
4749 return -1;
4750 switch (status)
4751 {
4752 case 'D': /* Double Click */
4753 case 'S': /* Single Click */
4754 if (button & 1) mouse_code |= MOUSE_LEFT;
4755 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4756 if (button & 4) mouse_code |= MOUSE_RIGHT;
4757 if (button & 8) mouse_code |= MOUSE_SHIFT;
4758 if (button & 16) mouse_code |= MOUSE_CTRL;
4759 break;
4760 case 'm': /* Mouse move */
4761 if (button & 1) mouse_code |= MOUSE_LEFT;
4762 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4763 if (button & 4) mouse_code |= MOUSE_RIGHT;
4764 if (button & 8) mouse_code |= MOUSE_SHIFT;
4765 if (button & 16) mouse_code |= MOUSE_CTRL;
4766 if ((button & 7) != 0)
4767 {
4768 held_button = mouse_code;
4769 mouse_code |= MOUSE_DRAG;
4770 }
4771 is_drag = TRUE;
4772 showmode();
4773 break;
4774 case 'd': /* Button Down */
4775 if (button & 1) mouse_code |= MOUSE_LEFT;
4776 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4777 if (button & 4) mouse_code |= MOUSE_RIGHT;
4778 if (button & 8) mouse_code |= MOUSE_SHIFT;
4779 if (button & 16) mouse_code |= MOUSE_CTRL;
4780 break;
4781 case 'u': /* Button Up */
4782 if (button & 1)
4783 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4784 if (button & 2)
4785 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4786 if (button & 4)
4787 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4788 if (button & 8)
4789 mouse_code |= MOUSE_SHIFT;
4790 if (button & 16)
4791 mouse_code |= MOUSE_CTRL;
4792 break;
4793 default: return -1; /* Unknown Result */
4794 }
4795
4796 slen += (p - (tp + slen));
4797 }
4798# endif /* FEAT_MOUSE_JSB */
4799# ifdef FEAT_MOUSE_DEC
4800 if (key_name[0] == (int)KS_DEC_MOUSE)
4801 {
4802 /* The DEC Locator Input Model
4803 * Netterm delivers the code sequence:
4804 * \033[2;4;24;80&w (left button down)
4805 * \033[3;0;24;80&w (left button up)
4806 * \033[6;1;24;80&w (right button down)
4807 * \033[7;0;24;80&w (right button up)
4808 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4809 * Pe is the event code
4810 * Pb is the button code
4811 * Pr is the row coordinate
4812 * Pc is the column coordinate
4813 * Pp is the third coordinate (page number)
4814 * Pe, the event code indicates what event caused this report
4815 * The following event codes are defined:
4816 * 0 - request, the terminal received an explicit request
4817 * for a locator report, but the locator is unavailable
4818 * 1 - request, the terminal received an explicit request
4819 * for a locator report
4820 * 2 - left button down
4821 * 3 - left button up
4822 * 4 - middle button down
4823 * 5 - middle button up
4824 * 6 - right button down
4825 * 7 - right button up
4826 * 8 - fourth button down
4827 * 9 - fourth button up
4828 * 10 - locator outside filter rectangle
4829 * Pb, the button code, ASCII decimal 0-15 indicating which
4830 * buttons are down if any. The state of the four buttons
4831 * on the locator correspond to the low four bits of the
4832 * decimal value,
4833 * "1" means button depressed
4834 * 0 - no buttons down,
4835 * 1 - right,
4836 * 2 - middle,
4837 * 4 - left,
4838 * 8 - fourth
4839 * Pr is the row coordinate of the locator position in the page,
4840 * encoded as an ASCII decimal value.
4841 * If Pr is omitted, the locator position is undefined
4842 * (outside the terminal window for example).
4843 * Pc is the column coordinate of the locator position in the
4844 * page, encoded as an ASCII decimal value.
4845 * If Pc is omitted, the locator position is undefined
4846 * (outside the terminal window for example).
4847 * Pp is the page coordinate of the locator position
4848 * encoded as an ASCII decimal value.
4849 * The page coordinate may be omitted if the locator is on
4850 * page one (the default). We ignore it anyway.
4851 */
4852 int Pe, Pb, Pr, Pc;
4853
4854 p = tp + slen;
4855
4856 /* get event status */
4857 Pe = getdigits(&p);
4858 if (*p++ != ';')
4859 return -1;
4860
4861 /* get button status */
4862 Pb = getdigits(&p);
4863 if (*p++ != ';')
4864 return -1;
4865
4866 /* get row status */
4867 Pr = getdigits(&p);
4868 if (*p++ != ';')
4869 return -1;
4870
4871 /* get column status */
4872 Pc = getdigits(&p);
4873
4874 /* the page parameter is optional */
4875 if (*p == ';')
4876 {
4877 p++;
4878 (void)getdigits(&p);
4879 }
4880 if (*p++ != '&')
4881 return -1;
4882 if (*p++ != 'w')
4883 return -1;
4884
4885 mouse_code = 0;
4886 switch (Pe)
4887 {
4888 case 0: return -1; /* position request while unavailable */
4889 case 1: /* a response to a locator position request includes
4890 the status of all buttons */
4891 Pb &= 7; /* mask off and ignore fourth button */
4892 if (Pb & 4)
4893 mouse_code = MOUSE_LEFT;
4894 if (Pb & 2)
4895 mouse_code = MOUSE_MIDDLE;
4896 if (Pb & 1)
4897 mouse_code = MOUSE_RIGHT;
4898 if (Pb)
4899 {
4900 held_button = mouse_code;
4901 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004902 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904 is_drag = TRUE;
4905 showmode();
4906 break;
4907 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004908 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 break;
4910 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
4911 break;
4912 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004913 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 break;
4915 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
4916 break;
4917 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004918 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 break;
4920 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
4921 break;
4922 case 8: return -1; /* fourth button down */
4923 case 9: return -1; /* fourth button up */
4924 case 10: return -1; /* mouse outside of filter rectangle */
4925 default: return -1; /* should never occur */
4926 }
4927
4928 mouse_col = Pc - 1;
4929 mouse_row = Pr - 1;
4930
4931 slen += (int)(p - (tp + slen));
4932 }
4933# endif /* FEAT_MOUSE_DEC */
4934# ifdef FEAT_MOUSE_PTERM
4935 if (key_name[0] == (int)KS_PTERM_MOUSE)
4936 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004937 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
4939 p = tp + slen;
4940
4941 action = getdigits(&p);
4942 if (*p++ != ';')
4943 return -1;
4944
4945 mouse_row = getdigits(&p);
4946 if (*p++ != ';')
4947 return -1;
4948 mouse_col = getdigits(&p);
4949 if (*p++ != ';')
4950 return -1;
4951
4952 button = getdigits(&p);
4953 mouse_code = 0;
4954
4955 switch( button )
4956 {
4957 case 4: mouse_code = MOUSE_LEFT; break;
4958 case 1: mouse_code = MOUSE_RIGHT; break;
4959 case 2: mouse_code = MOUSE_MIDDLE; break;
4960 default: return -1;
4961 }
4962
4963 switch( action )
4964 {
4965 case 31: /* Initial press */
4966 if (*p++ != ';')
4967 return -1;
4968
4969 num_clicks = getdigits(&p); /* Not used */
4970 break;
4971
4972 case 32: /* Release */
4973 mouse_code |= MOUSE_RELEASE;
4974 break;
4975
4976 case 33: /* Drag */
4977 held_button = mouse_code;
4978 mouse_code |= MOUSE_DRAG;
4979 break;
4980
4981 default:
4982 return -1;
4983 }
4984
4985 if (*p++ != 't')
4986 return -1;
4987
4988 slen += (p - (tp + slen));
4989 }
4990# endif /* FEAT_MOUSE_PTERM */
4991
4992 /* Interpret the mouse code */
4993 current_button = (mouse_code & MOUSE_CLICK_MASK);
4994 if (current_button == MOUSE_RELEASE
4995# ifdef FEAT_MOUSE_XTERM
4996 && wheel_code == 0
4997# endif
4998 )
4999 {
5000 /*
5001 * If we get a mouse drag or release event when
5002 * there is no mouse button held down (held_button ==
5003 * MOUSE_RELEASE), produce a K_IGNORE below.
5004 * (can happen when you hold down two buttons
5005 * and then let them go, or click in the menu bar, but not
5006 * on a menu, and drag into the text).
5007 */
5008 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5009 is_drag = TRUE;
5010 current_button = held_button;
5011 }
5012 else if (wheel_code == 0)
5013 {
5014# ifdef CHECK_DOUBLE_CLICK
5015# ifdef FEAT_MOUSE_GPM
5016# ifdef FEAT_GUI
5017 /*
5018 * Only for Unix, when GUI or gpm is not active, we handle
5019 * multi-clicks here.
5020 */
5021 if (gpm_flag == 0 && !gui.in_use)
5022# else
5023 if (gpm_flag == 0)
5024# endif
5025# else
5026# ifdef FEAT_GUI
5027 if (!gui.in_use)
5028# endif
5029# endif
5030 {
5031 /*
5032 * Compute the time elapsed since the previous mouse click.
5033 */
5034 gettimeofday(&mouse_time, NULL);
5035 timediff = (mouse_time.tv_usec
5036 - orig_mouse_time.tv_usec) / 1000;
5037 if (timediff < 0)
5038 --orig_mouse_time.tv_sec;
5039 timediff += (mouse_time.tv_sec
5040 - orig_mouse_time.tv_sec) * 1000;
5041 orig_mouse_time = mouse_time;
5042 if (mouse_code == orig_mouse_code
5043 && timediff < p_mouset
5044 && orig_num_clicks != 4
5045 && orig_mouse_col == mouse_col
5046 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005047 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005049 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005051 )
5052#ifdef FEAT_WINDOWS
5053 /* Double click in tab pages line also works
5054 * when window contents changes. */
5055 || (mouse_row == 0 && firstwin->w_winrow > 0)
5056#endif
5057 )
5058 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 ++orig_num_clicks;
5060 else
5061 orig_num_clicks = 1;
5062 orig_mouse_col = mouse_col;
5063 orig_mouse_row = mouse_row;
5064 orig_topline = curwin->w_topline;
5065#ifdef FEAT_DIFF
5066 orig_topfill = curwin->w_topfill;
5067#endif
5068 }
5069# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5070 else
5071 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5072# endif
5073# else
5074 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5075# endif
5076 is_click = TRUE;
5077 orig_mouse_code = mouse_code;
5078 }
5079 if (!is_drag)
5080 held_button = mouse_code & MOUSE_CLICK_MASK;
5081
5082 /*
5083 * Translate the actual mouse event into a pseudo mouse event.
5084 * First work out what modifiers are to be used.
5085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (orig_mouse_code & MOUSE_SHIFT)
5087 modifiers |= MOD_MASK_SHIFT;
5088 if (orig_mouse_code & MOUSE_CTRL)
5089 modifiers |= MOD_MASK_CTRL;
5090 if (orig_mouse_code & MOUSE_ALT)
5091 modifiers |= MOD_MASK_ALT;
5092 if (orig_num_clicks == 2)
5093 modifiers |= MOD_MASK_2CLICK;
5094 else if (orig_num_clicks == 3)
5095 modifiers |= MOD_MASK_3CLICK;
5096 else if (orig_num_clicks == 4)
5097 modifiers |= MOD_MASK_4CLICK;
5098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 /* Work out our pseudo mouse event */
5100 key_name[0] = (int)KS_EXTRA;
5101 if (wheel_code != 0)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005102 {
5103 if (wheel_code & MOUSE_CTRL)
5104 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005105 if (wheel_code & MOUSE_ALT)
5106 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 key_name[1] = (wheel_code & 1)
5108 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 else
5111 key_name[1] = get_pseudo_mouse_code(current_button,
5112 is_click, is_drag);
5113 }
5114#endif /* FEAT_MOUSE */
5115
5116#ifdef FEAT_GUI
5117 /*
5118 * If using the GUI, then we get menu and scrollbar events.
5119 *
5120 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5121 * four bytes which are to be taken as a pointer to the vimmenu_T
5122 * structure.
5123 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005124 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005125 * is one byte with the tab index.
5126 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5128 * by one byte representing the scrollbar number, and then four bytes
5129 * representing a long_u which is the new value of the scrollbar.
5130 *
5131 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5132 * KE_FILLER followed by four bytes representing a long_u which is the
5133 * new value of the scrollbar.
5134 */
5135# ifdef FEAT_MENU
5136 else if (key_name[0] == (int)KS_MENU)
5137 {
5138 long_u val;
5139
5140 num_bytes = get_long_from_buf(tp + slen, &val);
5141 if (num_bytes == -1)
5142 return -1;
5143 current_menu = (vimmenu_T *)val;
5144 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005145
5146 /* The menu may have been deleted right after it was used, check
5147 * for that. */
5148 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5149 {
5150 key_name[0] = KS_EXTRA;
5151 key_name[1] = (int)KE_IGNORE;
5152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005155# ifdef FEAT_GUI_TABLINE
5156 else if (key_name[0] == (int)KS_TABLINE)
5157 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005158 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005159 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5160 if (num_bytes == -1)
5161 return -1;
5162 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005163 if (current_tab == 255) /* -1 in a byte gives 255 */
5164 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005165 slen += num_bytes;
5166 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005167 else if (key_name[0] == (int)KS_TABMENU)
5168 {
5169 /* Selecting tabline tab or using its menu. */
5170 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5171 if (num_bytes == -1)
5172 return -1;
5173 current_tab = (int)bytes[0];
5174 current_tabmenu = (int)bytes[1];
5175 slen += num_bytes;
5176 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005177# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178# ifndef USE_ON_FLY_SCROLL
5179 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5180 {
5181 long_u val;
5182
5183 /* Get the last scrollbar event in the queue of the same type */
5184 j = 0;
5185 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5186 && tp[j + 2] != NUL; ++i)
5187 {
5188 j += 3;
5189 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5190 if (num_bytes == -1)
5191 break;
5192 if (i == 0)
5193 current_scrollbar = (int)bytes[0];
5194 else if (current_scrollbar != (int)bytes[0])
5195 break;
5196 j += num_bytes;
5197 num_bytes = get_long_from_buf(tp + j, &val);
5198 if (num_bytes == -1)
5199 break;
5200 scrollbar_value = val;
5201 j += num_bytes;
5202 slen = j;
5203 }
5204 if (i == 0) /* not enough characters to make one */
5205 return -1;
5206 }
5207 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5208 {
5209 long_u val;
5210
5211 /* Get the last horiz. scrollbar event in the queue */
5212 j = 0;
5213 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5214 && tp[j + 2] != NUL; ++i)
5215 {
5216 j += 3;
5217 num_bytes = get_long_from_buf(tp + j, &val);
5218 if (num_bytes == -1)
5219 break;
5220 scrollbar_value = val;
5221 j += num_bytes;
5222 slen = j;
5223 }
5224 if (i == 0) /* not enough characters to make one */
5225 return -1;
5226 }
5227# endif /* !USE_ON_FLY_SCROLL */
5228#endif /* FEAT_GUI */
5229
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005230 /*
5231 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5232 */
5233 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5234
5235 /*
5236 * Add any modifier codes to our string.
5237 */
5238 new_slen = 0; /* Length of what will replace the termcode */
5239 if (modifiers != 0)
5240 {
5241 /* Some keys have the modifier included. Need to handle that here
5242 * to make mappings work. */
5243 key = simplify_key(key, &modifiers);
5244 if (modifiers != 0)
5245 {
5246 string[new_slen++] = K_SPECIAL;
5247 string[new_slen++] = (int)KS_MODIFIER;
5248 string[new_slen++] = modifiers;
5249 }
5250 }
5251
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005253 key_name[0] = KEY2TERMCAP0(key);
5254 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005256 {
5257 /* from ":set <M-b>=xx" */
5258#ifdef FEAT_MBYTE
5259 if (has_mbyte)
5260 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5261 else
5262#endif
5263 string[new_slen++] = key_name[1];
5264 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005265 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5266 && key_name[1] == KE_IGNORE)
5267 {
5268 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5269 * to indicate what happened. */
5270 retval = KEYLEN_REMOVED;
5271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 else
5273 {
5274 string[new_slen++] = K_SPECIAL;
5275 string[new_slen++] = key_name[0];
5276 string[new_slen++] = key_name[1];
5277 }
5278 string[new_slen] = NUL;
5279 extra = new_slen - slen;
5280 if (buf == NULL)
5281 {
5282 if (extra < 0)
5283 /* remove matched chars, taking care of noremap */
5284 del_typebuf(-extra, offset);
5285 else if (extra > 0)
5286 /* insert the extra space we need */
5287 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5288
5289 /*
5290 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5291 * typebuf.tb_buf[]!
5292 */
5293 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5294 (size_t)new_slen);
5295 }
5296 else
5297 {
5298 if (extra < 0)
5299 /* remove matched characters */
5300 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005301 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005303 {
5304 /* Insert the extra space we need. If there is insufficient
5305 * space return -1. */
5306 if (*buflen + extra + new_slen >= bufsize)
5307 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005309 (size_t)(*buflen - offset));
5310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005312 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005314 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 }
5316
Bram Moolenaar2951b772013-07-03 12:45:31 +02005317#ifdef FEAT_TERMRESPONSE
5318 LOG_TR("normal character");
5319#endif
5320
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 return 0; /* no match found */
5322}
5323
5324/*
5325 * Replace any terminal code strings in from[] with the equivalent internal
5326 * vim representation. This is used for the "from" and "to" part of a
5327 * mapping, and the "to" part of a menu command.
5328 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5329 * '<'.
5330 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5331 *
5332 * The replacement is done in result[] and finally copied into allocated
5333 * memory. If this all works well *bufp is set to the allocated memory and a
5334 * pointer to it is returned. If something fails *bufp is set to NULL and from
5335 * is returned.
5336 *
5337 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5338 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5339 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5340 * instead of a CTRL-V.
5341 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005342 char_u *
5343replace_termcodes(from, bufp, from_part, do_lt, special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 char_u *from;
5345 char_u **bufp;
5346 int from_part;
5347 int do_lt; /* also translate <lt> */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005348 int special; /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349{
5350 int i;
5351 int slen;
5352 int key;
5353 int dlen = 0;
5354 char_u *src;
5355 int do_backslash; /* backslash is a special character */
5356 int do_special; /* recognize <> key codes */
5357 int do_key_code; /* recognize raw key codes */
5358 char_u *result; /* buffer for resulting string */
5359
5360 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005361 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5363
5364 /*
5365 * Allocate space for the translation. Worst case a single character is
5366 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5367 */
5368 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5369 if (result == NULL) /* out of memory */
5370 {
5371 *bufp = NULL;
5372 return from;
5373 }
5374
5375 src = from;
5376
5377 /*
5378 * Check for #n at start only: function key n
5379 */
5380 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5381 {
5382 result[dlen++] = K_SPECIAL;
5383 result[dlen++] = 'k';
5384 if (src[1] == '0')
5385 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5386 else
5387 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5388 src += 2;
5389 }
5390
5391 /*
5392 * Copy each byte from *from to result[dlen]
5393 */
5394 while (*src != NUL)
5395 {
5396 /*
5397 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005398 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 */
5400 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5401 {
5402#ifdef FEAT_EVAL
5403 /*
5404 * Replace <SID> by K_SNR <script-nr> _.
5405 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5406 */
5407 if (STRNICMP(src, "<SID>", 5) == 0)
5408 {
5409 if (current_SID <= 0)
5410 EMSG(_(e_usingsid));
5411 else
5412 {
5413 src += 5;
5414 result[dlen++] = K_SPECIAL;
5415 result[dlen++] = (int)KS_EXTRA;
5416 result[dlen++] = (int)KE_SNR;
5417 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5418 dlen += (int)STRLEN(result + dlen);
5419 result[dlen++] = '_';
5420 continue;
5421 }
5422 }
5423#endif
5424
5425 slen = trans_special(&src, result + dlen, TRUE);
5426 if (slen)
5427 {
5428 dlen += slen;
5429 continue;
5430 }
5431 }
5432
5433 /*
5434 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5435 * Note that this is also checked after replacing the <> form.
5436 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5437 * it could be a character in the file.
5438 */
5439 if (do_key_code)
5440 {
5441 i = find_term_bykeys(src);
5442 if (i >= 0)
5443 {
5444 result[dlen++] = K_SPECIAL;
5445 result[dlen++] = termcodes[i].name[0];
5446 result[dlen++] = termcodes[i].name[1];
5447 src += termcodes[i].len;
5448 /* If terminal code matched, continue after it. */
5449 continue;
5450 }
5451 }
5452
5453#ifdef FEAT_EVAL
5454 if (do_special)
5455 {
5456 char_u *p, *s, len;
5457
5458 /*
5459 * Replace <Leader> by the value of "mapleader".
5460 * Replace <LocalLeader> by the value of "maplocalleader".
5461 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5462 */
5463 if (STRNICMP(src, "<Leader>", 8) == 0)
5464 {
5465 len = 8;
5466 p = get_var_value((char_u *)"g:mapleader");
5467 }
5468 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5469 {
5470 len = 13;
5471 p = get_var_value((char_u *)"g:maplocalleader");
5472 }
5473 else
5474 {
5475 len = 0;
5476 p = NULL;
5477 }
5478 if (len != 0)
5479 {
5480 /* Allow up to 8 * 6 characters for "mapleader". */
5481 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5482 s = (char_u *)"\\";
5483 else
5484 s = p;
5485 while (*s != NUL)
5486 result[dlen++] = *s++;
5487 src += len;
5488 continue;
5489 }
5490 }
5491#endif
5492
5493 /*
5494 * Remove CTRL-V and ignore the next character.
5495 * For "from" side the CTRL-V at the end is included, for the "to"
5496 * part it is removed.
5497 * If 'cpoptions' does not contain 'B', also accept a backslash.
5498 */
5499 key = *src;
5500 if (key == Ctrl_V || (do_backslash && key == '\\'))
5501 {
5502 ++src; /* skip CTRL-V or backslash */
5503 if (*src == NUL)
5504 {
5505 if (from_part)
5506 result[dlen++] = key;
5507 break;
5508 }
5509 }
5510
5511#ifdef FEAT_MBYTE
5512 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005513 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514#endif
5515 {
5516 /*
5517 * If the character is K_SPECIAL, replace it with K_SPECIAL
5518 * KS_SPECIAL KE_FILLER.
5519 * If compiled with the GUI replace CSI with K_CSI.
5520 */
5521 if (*src == K_SPECIAL)
5522 {
5523 result[dlen++] = K_SPECIAL;
5524 result[dlen++] = KS_SPECIAL;
5525 result[dlen++] = KE_FILLER;
5526 }
5527# ifdef FEAT_GUI
5528 else if (*src == CSI)
5529 {
5530 result[dlen++] = K_SPECIAL;
5531 result[dlen++] = KS_EXTRA;
5532 result[dlen++] = (int)KE_CSI;
5533 }
5534# endif
5535 else
5536 result[dlen++] = *src;
5537 ++src;
5538 }
5539 }
5540 result[dlen] = NUL;
5541
5542 /*
5543 * Copy the new string to allocated memory.
5544 * If this fails, just return from.
5545 */
5546 if ((*bufp = vim_strsave(result)) != NULL)
5547 from = *bufp;
5548 vim_free(result);
5549 return from;
5550}
5551
5552/*
5553 * Find a termcode with keys 'src' (must be NUL terminated).
5554 * Return the index in termcodes[], or -1 if not found.
5555 */
5556 int
5557find_term_bykeys(src)
5558 char_u *src;
5559{
5560 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005561 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
5563 for (i = 0; i < tc_len; ++i)
5564 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005565 if (slen == termcodes[i].len
5566 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 return i;
5568 }
5569 return -1;
5570}
5571
5572/*
5573 * Gather the first characters in the terminal key codes into a string.
5574 * Used to speed up check_termcode().
5575 */
5576 static void
5577gather_termleader()
5578{
5579 int i;
5580 int len = 0;
5581
5582#ifdef FEAT_GUI
5583 if (gui.in_use)
5584 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5585#endif
5586#ifdef FEAT_TERMRESPONSE
5587 if (check_for_codes)
5588 termleader[len++] = DCS; /* the termcode response starts with DCS
5589 in 8-bit mode */
5590#endif
5591 termleader[len] = NUL;
5592
5593 for (i = 0; i < tc_len; ++i)
5594 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5595 {
5596 termleader[len++] = termcodes[i].code[0];
5597 termleader[len] = NUL;
5598 }
5599
5600 need_gather = FALSE;
5601}
5602
5603/*
5604 * Show all termcodes (for ":set termcap")
5605 * This code looks a lot like showoptions(), but is different.
5606 */
5607 void
5608show_termcodes()
5609{
5610 int col;
5611 int *items;
5612 int item_count;
5613 int run;
5614 int row, rows;
5615 int cols;
5616 int i;
5617 int len;
5618
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005619#define INC3 27 /* try to make three columns */
5620#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621#define GAP 2 /* spaces between columns */
5622
5623 if (tc_len == 0) /* no terminal codes (must be GUI) */
5624 return;
5625 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5626 if (items == NULL)
5627 return;
5628
5629 /* Highlight title */
5630 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5631
5632 /*
5633 * do the loop two times:
5634 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005635 * 2. display the medium items (medium length strings)
5636 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005638 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 {
5640 /*
5641 * collect the items in items[]
5642 */
5643 item_count = 0;
5644 for (i = 0; i < tc_len; i++)
5645 {
5646 len = show_one_termcode(termcodes[i].name,
5647 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005648 if (len <= INC3 - GAP ? run == 1
5649 : len <= INC2 - GAP ? run == 2
5650 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 items[item_count++] = i;
5652 }
5653
5654 /*
5655 * display the items
5656 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005657 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005659 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 if (cols == 0)
5661 cols = 1;
5662 rows = (item_count + cols - 1) / cols;
5663 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005664 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 rows = item_count;
5666 for (row = 0; row < rows && !got_int; ++row)
5667 {
5668 msg_putchar('\n'); /* go to next line */
5669 if (got_int) /* 'q' typed in more */
5670 break;
5671 col = 0;
5672 for (i = row; i < item_count; i += rows)
5673 {
5674 msg_col = col; /* make columns */
5675 show_one_termcode(termcodes[items[i]].name,
5676 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005677 if (run == 2)
5678 col += INC2;
5679 else
5680 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682 out_flush();
5683 ui_breakcheck();
5684 }
5685 }
5686 vim_free(items);
5687}
5688
5689/*
5690 * Show one termcode entry.
5691 * Output goes into IObuff[]
5692 */
5693 int
5694show_one_termcode(name, code, printit)
5695 char_u *name;
5696 char_u *code;
5697 int printit;
5698{
5699 char_u *p;
5700 int len;
5701
5702 if (name[0] > '~')
5703 {
5704 IObuff[0] = ' ';
5705 IObuff[1] = ' ';
5706 IObuff[2] = ' ';
5707 IObuff[3] = ' ';
5708 }
5709 else
5710 {
5711 IObuff[0] = 't';
5712 IObuff[1] = '_';
5713 IObuff[2] = name[0];
5714 IObuff[3] = name[1];
5715 }
5716 IObuff[4] = ' ';
5717
5718 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5719 if (p[1] != 't')
5720 STRCPY(IObuff + 5, p);
5721 else
5722 IObuff[5] = NUL;
5723 len = (int)STRLEN(IObuff);
5724 do
5725 IObuff[len++] = ' ';
5726 while (len < 17);
5727 IObuff[len] = NUL;
5728 if (code == NULL)
5729 len += 4;
5730 else
5731 len += vim_strsize(code);
5732
5733 if (printit)
5734 {
5735 msg_puts(IObuff);
5736 if (code == NULL)
5737 msg_puts((char_u *)"NULL");
5738 else
5739 msg_outtrans(code);
5740 }
5741 return len;
5742}
5743
5744#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5745/*
5746 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5747 * termcap codes from the terminal itself.
5748 * We get them one by one to avoid a very long response string.
5749 */
Bram Moolenaar4e067c82014-07-30 17:21:58 +02005750static int xt_index_in = 0;
5751static int xt_index_out = 0;
5752
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 static void
5754req_codes_from_term()
5755{
5756 xt_index_out = 0;
5757 xt_index_in = 0;
5758 req_more_codes_from_term();
5759}
5760
5761 static void
5762req_more_codes_from_term()
5763{
5764 char buf[11];
5765 int old_idx = xt_index_out;
5766
5767 /* Don't do anything when going to exit. */
5768 if (exiting)
5769 return;
5770
5771 /* Send up to 10 more requests out than we received. Avoid sending too
5772 * many, there can be a buffer overflow somewhere. */
5773 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5774 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02005775# ifdef DEBUG_TERMRESPONSE
5776 char dbuf[100];
5777
5778 sprintf(dbuf, "Requesting XT %d: %s",
5779 xt_index_out, key_names[xt_index_out]);
5780 log_tr(dbuf);
5781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 sprintf(buf, "\033P+q%02x%02x\033\\",
5783 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5784 out_str_nf((char_u *)buf);
5785 ++xt_index_out;
5786 }
5787
5788 /* Send the codes out right away. */
5789 if (xt_index_out != old_idx)
5790 out_flush();
5791}
5792
5793/*
5794 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5795 * A "0" instead of the "1" indicates a code that isn't supported.
5796 * Both <name> and <string> are encoded in hex.
5797 * "code" points to the "0" or "1".
5798 */
5799 static void
5800got_code_from_term(code, len)
5801 char_u *code;
5802 int len;
5803{
5804#define XT_LEN 100
5805 char_u name[3];
5806 char_u str[XT_LEN];
5807 int i;
5808 int j = 0;
5809 int c;
5810
5811 /* A '1' means the code is supported, a '0' means it isn't.
5812 * When half the length is > XT_LEN we can't use it.
5813 * Our names are currently all 2 characters. */
5814 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5815 {
5816 /* Get the name from the response and find it in the table. */
5817 name[0] = hexhex2nr(code + 3);
5818 name[1] = hexhex2nr(code + 5);
5819 name[2] = NUL;
5820 for (i = 0; key_names[i] != NULL; ++i)
5821 {
5822 if (STRCMP(key_names[i], name) == 0)
5823 {
5824 xt_index_in = i;
5825 break;
5826 }
5827 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005828# ifdef DEBUG_TERMRESPONSE
5829 {
5830 char buf[100];
5831
5832 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
5833 log_tr(buf);
5834 }
5835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 if (key_names[i] != NULL)
5837 {
5838 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5839 str[j++] = c;
5840 str[j] = NUL;
5841 if (name[0] == 'C' && name[1] == 'o')
5842 {
5843 /* Color count is not a key code. */
5844 i = atoi((char *)str);
5845 if (i != t_colors)
5846 {
5847 /* Nr of colors changed, initialize highlighting and
Bram Moolenaar238a5642006-02-21 22:12:05 +00005848 * redraw everything. This causes a redraw, which usually
5849 * clears the message. Try keeping the message if it
5850 * might work. */
5851 set_keep_msg_from_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 set_color_count(i);
5853 init_highlight(TRUE, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +02005854#ifdef DEBUG_TERMRESPONSE
5855 {
5856 char buf[100];
5857 int r = redraw_asap(CLEAR);
5858
5859 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
5860 log_tr(buf);
5861 }
5862#else
5863 redraw_asap(CLEAR);
5864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 }
5866 }
5867 else
5868 {
5869 /* First delete any existing entry with the same code. */
5870 i = find_term_bykeys(str);
5871 if (i >= 0)
5872 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005873 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 }
5875 }
5876 }
5877
5878 /* May request more codes now that we received one. */
5879 ++xt_index_in;
5880 req_more_codes_from_term();
5881}
5882
5883/*
5884 * Check if there are any unanswered requests and deal with them.
5885 * This is called before starting an external program or getting direct
5886 * keyboard input. We don't want responses to be send to that program or
5887 * handled as typed text.
5888 */
5889 static void
5890check_for_codes_from_term()
5891{
5892 int c;
5893
5894 /* If no codes requested or all are answered, no need to wait. */
5895 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5896 return;
5897
5898 /* Vgetc() will check for and handle any response.
5899 * Keep calling vpeekc() until we don't get any responses. */
5900 ++no_mapping;
5901 ++allow_keys;
5902 for (;;)
5903 {
5904 c = vpeekc();
5905 if (c == NUL) /* nothing available */
5906 break;
5907
5908 /* If a response is recognized it's replaced with K_IGNORE, must read
5909 * it from the input stream. If there is no K_IGNORE we can't do
5910 * anything, break here (there might be some responses further on, but
5911 * we don't want to throw away any typed chars). */
5912 if (c != K_SPECIAL && c != K_IGNORE)
5913 break;
5914 c = vgetc();
5915 if (c != K_IGNORE)
5916 {
5917 vungetc(c);
5918 break;
5919 }
5920 }
5921 --no_mapping;
5922 --allow_keys;
5923}
5924#endif
5925
5926#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5927/*
5928 * Translate an internal mapping/abbreviation representation into the
5929 * corresponding external one recognized by :map/:abbrev commands;
5930 * respects the current B/k/< settings of 'cpoption'.
5931 *
5932 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00005933 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 *
5935 * It uses a growarray to build the translation string since the
5936 * latter can be wider than the original description. The caller has to
5937 * free the string afterwards.
5938 *
5939 * Returns NULL when there is a problem.
5940 */
5941 char_u *
5942translate_mapping(str, expmap)
5943 char_u *str;
5944 int expmap; /* TRUE when expanding mappings on command-line */
5945{
5946 garray_T ga;
5947 int c;
5948 int modifiers;
5949 int cpo_bslash;
5950 int cpo_special;
5951 int cpo_keycode;
5952
5953 ga_init(&ga);
5954 ga.ga_itemsize = 1;
5955 ga.ga_growsize = 40;
5956
5957 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
5958 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
5959 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5960
5961 for (; *str; ++str)
5962 {
5963 c = *str;
5964 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5965 {
5966 modifiers = 0;
5967 if (str[1] == KS_MODIFIER)
5968 {
5969 str++;
5970 modifiers = *++str;
5971 c = *++str;
5972 }
5973 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
5974 {
5975 int i;
5976
5977 /* try to find special key in termcodes */
5978 for (i = 0; i < tc_len; ++i)
5979 if (termcodes[i].name[0] == str[1]
5980 && termcodes[i].name[1] == str[2])
5981 break;
5982 if (i < tc_len)
5983 {
5984 ga_concat(&ga, termcodes[i].code);
5985 str += 2;
5986 continue; /* for (str) */
5987 }
5988 }
5989 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5990 {
5991 if (expmap && cpo_special)
5992 {
5993 ga_clear(&ga);
5994 return NULL;
5995 }
5996 c = TO_SPECIAL(str[1], str[2]);
5997 if (c == K_ZERO) /* display <Nul> as ^@ */
5998 c = NUL;
5999 str += 2;
6000 }
6001 if (IS_SPECIAL(c) || modifiers) /* special key */
6002 {
6003 if (expmap && cpo_special)
6004 {
6005 ga_clear(&ga);
6006 return NULL;
6007 }
6008 ga_concat(&ga, get_special_key_name(c, modifiers));
6009 continue; /* for (str) */
6010 }
6011 }
6012 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6013 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6014 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6015 if (c)
6016 ga_append(&ga, c);
6017 }
6018 ga_append(&ga, NUL);
6019 return (char_u *)(ga.ga_data);
6020}
6021#endif
6022
6023#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6024static char ksme_str[20];
6025static char ksmr_str[20];
6026static char ksmd_str[20];
6027
6028/*
6029 * For Win32 console: update termcap codes for existing console attributes.
6030 */
6031 void
6032update_tcap(attr)
6033 int attr;
6034{
6035 struct builtin_term *p;
6036
6037 p = find_builtin_term(DEFAULT_TERM);
6038 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6039 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6040 attr | 0x08); /* FOREGROUND_INTENSITY */
6041 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6042 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6043
6044 while (p->bt_string != NULL)
6045 {
6046 if (p->bt_entry == (int)KS_ME)
6047 p->bt_string = &ksme_str[0];
6048 else if (p->bt_entry == (int)KS_MR)
6049 p->bt_string = &ksmr_str[0];
6050 else if (p->bt_entry == (int)KS_MD)
6051 p->bt_string = &ksmd_str[0];
6052 ++p;
6053 }
6054}
6055#endif