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