blob: 699e356f92853b880bbbc486083f078d82f06f68 [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. */
3262 if (crv_status != CRV_GET)
3263 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 Moolenaara40ceaf2006-01-13 22:35:40 +00003309#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
3459# ifdef FEAT_VISUAL
3460 if (VIsual_active)
3461 checkfor = MOUSE_VISUAL;
3462 else
3463# endif
3464 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
3465 checkfor = MOUSE_RETURN;
3466 else if (State & INSERT)
3467 checkfor = MOUSE_INSERT;
3468 else if (State & CMDLINE)
3469 checkfor = MOUSE_COMMAND;
3470 else if (State == CONFIRM || State == EXTERNCMD)
3471 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3472 else
3473 checkfor = MOUSE_NORMAL; /* assume normal mode */
3474
3475 if (mouse_has(checkfor))
3476 mch_setmouse(TRUE);
3477 else
3478 mch_setmouse(FALSE);
3479# endif
3480}
3481
3482/*
3483 * Return TRUE if
3484 * - "c" is in 'mouse', or
3485 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3486 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3487 * normal editing mode (not at hit-return message).
3488 */
3489 int
3490mouse_has(c)
3491 int c;
3492{
3493 char_u *p;
3494
3495 for (p = p_mouse; *p; ++p)
3496 switch (*p)
3497 {
3498 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3499 return TRUE;
3500 break;
3501 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3502 return TRUE;
3503 break;
3504 default: if (c == *p) return TRUE; break;
3505 }
3506 return FALSE;
3507}
3508
3509/*
3510 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3511 */
3512 int
3513mouse_model_popup()
3514{
3515 return (p_mousem[0] == 'p');
3516}
3517#endif
3518
3519/*
3520 * By outputting the 'cursor very visible' termcap code, for some windowed
3521 * terminals this makes the screen scrolled to the correct position.
3522 * Used when starting Vim or returning from a shell.
3523 */
3524 void
3525scroll_start()
3526{
3527 if (*T_VS != NUL)
3528 {
3529 out_str(T_VS);
3530 out_str(T_VE);
3531 screen_start(); /* don't know where cursor is now */
3532 }
3533}
3534
3535static int cursor_is_off = FALSE;
3536
3537/*
3538 * Enable the cursor.
3539 */
3540 void
3541cursor_on()
3542{
3543 if (cursor_is_off)
3544 {
3545 out_str(T_VE);
3546 cursor_is_off = FALSE;
3547 }
3548}
3549
3550/*
3551 * Disable the cursor.
3552 */
3553 void
3554cursor_off()
3555{
3556 if (full_screen)
3557 {
3558 if (!cursor_is_off)
3559 out_str(T_VI); /* disable cursor */
3560 cursor_is_off = TRUE;
3561 }
3562}
3563
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003564#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565/*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003566 * Set cursor shape to match Insert mode.
3567 */
3568 void
3569term_cursor_shape()
3570{
3571 static int showing_insert_mode = MAYBE;
3572
3573 if (!full_screen || *T_CSI == NUL || *T_CEI == NUL)
3574 return;
3575
3576 if (State & INSERT)
3577 {
3578 if (showing_insert_mode != TRUE)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003579 out_str(T_CSI); /* Insert mode cursor */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003580 showing_insert_mode = TRUE;
3581 }
3582 else
3583 {
3584 if (showing_insert_mode != FALSE)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003585 out_str(T_CEI); /* non-Insert mode cursor */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003586 showing_insert_mode = FALSE;
3587 }
3588}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003589#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003590
3591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 * Set scrolling region for window 'wp'.
3593 * The region starts 'off' lines from the start of the window.
3594 * Also set the vertical scroll region for a vertically split window. Always
3595 * the full width of the window, excluding the vertical separator.
3596 */
3597 void
3598scroll_region_set(wp, off)
3599 win_T *wp;
3600 int off;
3601{
3602 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3603 W_WINROW(wp) + off));
3604#ifdef FEAT_VERTSPLIT
3605 if (*T_CSV != NUL && wp->w_width != Columns)
3606 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3607 W_WINCOL(wp)));
3608#endif
3609 screen_start(); /* don't know where cursor is now */
3610}
3611
3612/*
3613 * Reset scrolling region to the whole screen.
3614 */
3615 void
3616scroll_region_reset()
3617{
3618 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
3619#ifdef FEAT_VERTSPLIT
3620 if (*T_CSV != NUL)
3621 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3622#endif
3623 screen_start(); /* don't know where cursor is now */
3624}
3625
3626
3627/*
3628 * List of terminal codes that are currently recognized.
3629 */
3630
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003631static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632{
3633 char_u name[2]; /* termcap name of entry */
3634 char_u *code; /* terminal code (in allocated memory) */
3635 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003636 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637} *termcodes = NULL;
3638
3639static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3640static int tc_len = 0; /* current number of entries in termcodes[] */
3641
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003642static int termcode_star __ARGS((char_u *code, int len));
3643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 void
3645clear_termcodes()
3646{
3647 while (tc_len > 0)
3648 vim_free(termcodes[--tc_len].code);
3649 vim_free(termcodes);
3650 termcodes = NULL;
3651 tc_max_len = 0;
3652
3653#ifdef HAVE_TGETENT
3654 BC = (char *)empty_option;
3655 UP = (char *)empty_option;
3656 PC = NUL; /* set pad character to NUL */
3657 ospeed = 0;
3658#endif
3659
3660 need_gather = TRUE; /* need to fill termleader[] */
3661}
3662
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003663#define ATC_FROM_TERM 55
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665/*
3666 * Add a new entry to the list of terminal codes.
3667 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003668 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3669 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 */
3671 void
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003672add_termcode(name, string, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 char_u *name;
3674 char_u *string;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003675 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
3677 struct termcode *new_tc;
3678 int i, j;
3679 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003680 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
3682 if (string == NULL || *string == NUL)
3683 {
3684 del_termcode(name);
3685 return;
3686 }
3687
3688 s = vim_strsave(string);
3689 if (s == NULL)
3690 return;
3691
3692 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003693 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003695 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 s[0] = term_7to8bit(string);
3697 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003698 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
3700 need_gather = TRUE; /* need to fill termleader[] */
3701
3702 /*
3703 * need to make space for more entries
3704 */
3705 if (tc_len == tc_max_len)
3706 {
3707 tc_max_len += 20;
3708 new_tc = (struct termcode *)alloc(
3709 (unsigned)(tc_max_len * sizeof(struct termcode)));
3710 if (new_tc == NULL)
3711 {
3712 tc_max_len -= 20;
3713 return;
3714 }
3715 for (i = 0; i < tc_len; ++i)
3716 new_tc[i] = termcodes[i];
3717 vim_free(termcodes);
3718 termcodes = new_tc;
3719 }
3720
3721 /*
3722 * Look for existing entry with the same name, it is replaced.
3723 * Look for an existing entry that is alphabetical higher, the new entry
3724 * is inserted in front of it.
3725 */
3726 for (i = 0; i < tc_len; ++i)
3727 {
3728 if (termcodes[i].name[0] < name[0])
3729 continue;
3730 if (termcodes[i].name[0] == name[0])
3731 {
3732 if (termcodes[i].name[1] < name[1])
3733 continue;
3734 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003735 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 */
3737 if (termcodes[i].name[1] == name[1])
3738 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003739 if (flags == ATC_FROM_TERM && (j = termcode_star(
3740 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003741 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003742 /* Don't replace ESC[123;*X or ESC O*X with another when
3743 * invoked from got_code_from_term(). */
3744 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003745 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003746 && s[len - 1]
3747 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003748 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003749 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003750 vim_free(s);
3751 return;
3752 }
3753 }
3754 else
3755 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003756 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003757 vim_free(termcodes[i].code);
3758 --tc_len;
3759 break;
3760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762 }
3763 /*
3764 * Found alphabetical larger entry, move rest to insert new entry
3765 */
3766 for (j = tc_len; j > i; --j)
3767 termcodes[j] = termcodes[j - 1];
3768 break;
3769 }
3770
3771 termcodes[i].name[0] = name[0];
3772 termcodes[i].name[1] = name[1];
3773 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003774 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003775
3776 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3777 * accept modifiers. */
3778 termcodes[i].modlen = 0;
3779 j = termcode_star(s, len);
3780 if (j > 0)
3781 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 ++tc_len;
3783}
3784
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003785/*
3786 * Check termcode "code[len]" for ending in ;*X, <Esc>O*X or <M-O>*X.
3787 * The "X" can be any character.
3788 * Return 0 if not found, 2 for ;*X and 1 for O*X and <M-O>*X.
3789 */
3790 static int
3791termcode_star(code, len)
3792 char_u *code;
3793 int len;
3794{
3795 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3796 if (len >= 3 && code[len - 2] == '*')
3797 {
3798 if (len >= 5 && code[len - 3] == ';')
3799 return 2;
3800 if ((len >= 4 && code[len - 3] == 'O') || code[len - 3] == 'O' + 128)
3801 return 1;
3802 }
3803 return 0;
3804}
3805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 char_u *
3807find_termcode(name)
3808 char_u *name;
3809{
3810 int i;
3811
3812 for (i = 0; i < tc_len; ++i)
3813 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3814 return termcodes[i].code;
3815 return NULL;
3816}
3817
3818#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3819 char_u *
3820get_termcode(i)
3821 int i;
3822{
3823 if (i >= tc_len)
3824 return NULL;
3825 return &termcodes[i].name[0];
3826}
3827#endif
3828
3829 void
3830del_termcode(name)
3831 char_u *name;
3832{
3833 int i;
3834
3835 if (termcodes == NULL) /* nothing there yet */
3836 return;
3837
3838 need_gather = TRUE; /* need to fill termleader[] */
3839
3840 for (i = 0; i < tc_len; ++i)
3841 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3842 {
3843 del_termcode_idx(i);
3844 return;
3845 }
3846 /* not found. Give error message? */
3847}
3848
3849 static void
3850del_termcode_idx(idx)
3851 int idx;
3852{
3853 int i;
3854
3855 vim_free(termcodes[idx].code);
3856 --tc_len;
3857 for (i = idx; i < tc_len; ++i)
3858 termcodes[i] = termcodes[i + 1];
3859}
3860
3861#ifdef FEAT_TERMRESPONSE
3862/*
3863 * Called when detected that the terminal sends 8-bit codes.
3864 * Convert all 7-bit codes to their 8-bit equivalent.
3865 */
3866 static void
3867switch_to_8bit()
3868{
3869 int i;
3870 int c;
3871
3872 /* Only need to do something when not already using 8-bit codes. */
3873 if (!term_is_8bit(T_NAME))
3874 {
3875 for (i = 0; i < tc_len; ++i)
3876 {
3877 c = term_7to8bit(termcodes[i].code);
3878 if (c != 0)
3879 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003880 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 termcodes[i].code[0] = c;
3882 }
3883 }
3884 need_gather = TRUE; /* need to fill termleader[] */
3885 }
3886 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003887 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888}
3889#endif
3890
3891#ifdef CHECK_DOUBLE_CLICK
3892static linenr_T orig_topline = 0;
3893# ifdef FEAT_DIFF
3894static int orig_topfill = 0;
3895# endif
3896#endif
3897#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
3898/*
3899 * Checking for double clicks ourselves.
3900 * "orig_topline" is used to avoid detecting a double-click when the window
3901 * contents scrolled (e.g., when 'scrolloff' is non-zero).
3902 */
3903/*
3904 * Set orig_topline. Used when jumping to another window, so that a double
3905 * click still works.
3906 */
3907 void
3908set_mouse_topline(wp)
3909 win_T *wp;
3910{
3911 orig_topline = wp->w_topline;
3912# ifdef FEAT_DIFF
3913 orig_topfill = wp->w_topfill;
3914# endif
3915}
3916#endif
3917
3918/*
3919 * Check if typebuf.tb_buf[] contains a terminal key code.
3920 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
3921 * + max_offset].
3922 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003923 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 * With a match, the match is removed, the replacement code is inserted in
3925 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
3926 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003927 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
3928 * "buflen" is then the length of the string in buf[] and is updated for
3929 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 */
3931 int
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003932check_termcode(max_offset, buf, bufsize, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 int max_offset;
3934 char_u *buf;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003935 int bufsize;
3936 int *buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
3938 char_u *tp;
3939 char_u *p;
3940 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003941 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003943 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 int offset;
3945 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003946 int modifiers;
3947 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 int new_slen;
3949 int extra;
3950 char_u string[MAX_KEY_CODE_LEN + 1];
3951 int i, j;
3952 int idx = 0;
3953#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00003954# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
3955 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 char_u bytes[6];
3957 int num_bytes;
3958# endif
3959 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 int is_click, is_drag;
3961 int wheel_code = 0;
3962 int current_button;
3963 static int held_button = MOUSE_RELEASE;
3964 static int orig_num_clicks = 1;
3965 static int orig_mouse_code = 0x0;
3966# ifdef CHECK_DOUBLE_CLICK
3967 static int orig_mouse_col = 0;
3968 static int orig_mouse_row = 0;
3969 static struct timeval orig_mouse_time = {0, 0};
3970 /* time of previous mouse click */
3971 struct timeval mouse_time; /* time of current mouse click */
3972 long timediff; /* elapsed time in msec */
3973# endif
3974#endif
3975 int cpo_koffset;
3976#ifdef FEAT_MOUSE_GPM
3977 extern int gpm_flag; /* gpm library variable */
3978#endif
3979
3980 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
3981
3982 /*
3983 * Speed up the checks for terminal codes by gathering all first bytes
3984 * used in termleader[]. Often this is just a single <Esc>.
3985 */
3986 if (need_gather)
3987 gather_termleader();
3988
3989 /*
3990 * Check at several positions in typebuf.tb_buf[], to catch something like
3991 * "x<Up>" that can be mapped. Stop at max_offset, because characters
3992 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01003993 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 * This is used often, KEEP IT FAST!
3995 */
3996 for (offset = 0; offset < max_offset; ++offset)
3997 {
3998 if (buf == NULL)
3999 {
4000 if (offset >= typebuf.tb_len)
4001 break;
4002 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4003 len = typebuf.tb_len - offset; /* length of the input */
4004 }
4005 else
4006 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004007 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 break;
4009 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004010 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 }
4012
4013 /*
4014 * Don't check characters after K_SPECIAL, those are already
4015 * translated terminal chars (avoid translating ~@^Hx).
4016 */
4017 if (*tp == K_SPECIAL)
4018 {
4019 offset += 2; /* there are always 2 extra characters */
4020 continue;
4021 }
4022
4023 /*
4024 * Skip this position if the character does not appear as the first
4025 * character in term_strings. This speeds up a lot, since most
4026 * termcodes start with the same character (ESC or CSI).
4027 */
4028 i = *tp;
4029 for (p = termleader; *p && *p != i; ++p)
4030 ;
4031 if (*p == NUL)
4032 continue;
4033
4034 /*
4035 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4036 * are in Insert mode.
4037 */
4038 if (*tp == ESC && !p_ek && (State & INSERT))
4039 continue;
4040
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004042 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004043 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
4045#ifdef FEAT_GUI
4046 if (gui.in_use)
4047 {
4048 /*
4049 * GUI special key codes are all of the form [CSI xx].
4050 */
4051 if (*tp == CSI) /* Special key from GUI */
4052 {
4053 if (len < 3)
4054 return -1; /* Shouldn't happen */
4055 slen = 3;
4056 key_name[0] = tp[1];
4057 key_name[1] = tp[2];
4058 }
4059 }
4060 else
4061#endif /* FEAT_GUI */
4062 {
4063 for (idx = 0; idx < tc_len; ++idx)
4064 {
4065 /*
4066 * Ignore the entry if we are not at the start of
4067 * typebuf.tb_buf[]
4068 * and there are not enough characters to make a match.
4069 * But only when the 'K' flag is in 'cpoptions'.
4070 */
4071 slen = termcodes[idx].len;
4072 if (cpo_koffset && offset && len < slen)
4073 continue;
4074 if (STRNCMP(termcodes[idx].code, tp,
4075 (size_t)(slen > len ? len : slen)) == 0)
4076 {
4077 if (len < slen) /* got a partial sequence */
4078 return -1; /* need to get more chars */
4079
4080 /*
4081 * When found a keypad key, check if there is another key
4082 * that matches and use that one. This makes <Home> to be
4083 * found instead of <kHome> when they produce the same
4084 * key code.
4085 */
4086 if (termcodes[idx].name[0] == 'K'
4087 && VIM_ISDIGIT(termcodes[idx].name[1]))
4088 {
4089 for (j = idx + 1; j < tc_len; ++j)
4090 if (termcodes[j].len == slen &&
4091 STRNCMP(termcodes[idx].code,
4092 termcodes[j].code, slen) == 0)
4093 {
4094 idx = j;
4095 break;
4096 }
4097 }
4098
4099 key_name[0] = termcodes[idx].name[0];
4100 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 break;
4102 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004103
4104 /*
4105 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004106 * <Esc>[123;*X (modslen == slen - 3)
4107 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4108 * When there is a modifier the * matches a number.
4109 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004110 */
4111 if (termcodes[idx].modlen > 0)
4112 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004113 modslen = termcodes[idx].modlen;
4114 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004115 continue;
4116 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004117 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004118 {
4119 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004120
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004121 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004122 return -1; /* need to get more chars */
4123
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004124 if (tp[modslen] == termcodes[idx].code[slen - 1])
4125 slen = modslen + 1; /* no modifiers */
4126 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004127 continue; /* no match */
4128 else
4129 {
4130 /* Skip over the digits, the final char must
4131 * follow. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004132 for (j = slen - 2; j < len && isdigit(tp[j]); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004133 ;
4134 ++j;
4135 if (len < j) /* got a partial sequence */
4136 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004137 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4138 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004139
4140 /* Match! Convert modifier bits. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004141 n = atoi((char *)tp + slen - 2) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004142 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004143 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004144 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004145 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004146 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004147 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004148 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004149 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004150
4151 slen = j;
4152 }
4153 key_name[0] = termcodes[idx].name[0];
4154 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004155 break;
4156 }
4157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
4159 }
4160
4161#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004162 if (key_name[0] == NUL
4163 /* URXVT mouse uses <ESC>[#;#;#M, but we are matching <ESC>[ */
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004164 || key_name[0] == KS_URXVT_MOUSE
4165# ifdef FEAT_MBYTE
4166 || u7_status == U7_SENT
4167# endif
4168 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
Bram Moolenaar9584b312013-03-13 19:29:28 +01004170 /* Check for some responses from terminal start with "<Esc>[" or
4171 * CSI.
4172 *
4173 * - xterm version string: <Esc>[>{x};{vers};{y}c
4174 * Also eat other possible responses to t_RV, rxvt returns
4175 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4176 * mrxvt has been reported to have "+" in the version. Assume
4177 * the escape sequence ends with a letter or one of "{|}~".
4178 *
4179 * - cursor position report: <Esc>[{row};{col}R
4180 * The final byte is 'R'. now it is only used for checking for
4181 * ambiguous-width character state.
4182 */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004183 p = tp[0] == CSI ? tp + 1 : tp + 2;
Bram Moolenaar9584b312013-03-13 19:29:28 +01004184 if ((*T_CRV != NUL || *T_U7 != NUL)
4185 && ((tp[0] == ESC && tp[1] == '[' && len >= 3)
Bram Moolenaar46a75612013-05-15 14:22:41 +02004186 || (tp[0] == CSI && len >= 2))
4187 && (VIM_ISDIGIT(*p) || *p == '>' || *p == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004189#ifdef FEAT_MBYTE
4190 int col;
4191 int row_char;
4192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 j = 0;
4194 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004195 for (i = 2 + (tp[0] != CSI); i < len
4196 && !(tp[i] >= '{' && tp[i] <= '~')
4197 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 if (tp[i] == ';' && ++j == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004199 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004200 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004201#ifdef FEAT_MBYTE
4202 row_char = tp[i - 1];
4203#endif
4204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004206 {
4207 LOG_TR("Not enough characters for CRV");
4208 return -1;
4209 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004210#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004211 if (extra > 0)
4212 col = atoi((char *)tp + extra);
4213 else
4214 col = 0;
4215
4216 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4217 * u7_status is not "sent", it may be from a previous Vim that
4218 * just exited. But not for <S-F3>, it sends something
4219 * similar, check for row and column to make sense. */
4220 if (j == 1 && tp[i] == 'R' && row_char == '2' && col >= 2)
Bram Moolenaar9584b312013-03-13 19:29:28 +01004221 {
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004222 char *aw = NULL;
Bram Moolenaar9584b312013-03-13 19:29:28 +01004223
Bram Moolenaar2951b772013-07-03 12:45:31 +02004224 LOG_TR("Received U7 status");
Bram Moolenaar9584b312013-03-13 19:29:28 +01004225 u7_status = U7_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004226# ifdef FEAT_AUTOCMD
4227 did_cursorhold = TRUE;
4228# endif
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004229 if (col == 2)
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004230 aw = "single";
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004231 else if (col == 3)
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004232 aw = "double";
Bram Moolenaar2951b772013-07-03 12:45:31 +02004233 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4234 {
4235 /* Setting the option causes a screen redraw. Do that
4236 * right away if possible, keeping any messages. */
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004237 set_option_value((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004238# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +02004239 {
4240 char buf[100];
4241 int r = redraw_asap(CLEAR);
4242
4243 sprintf(buf, "set 'ambiwidth', redraw_asap(): %d",
4244 r);
4245 log_tr(buf);
4246 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004247# else
Bram Moolenaar2951b772013-07-03 12:45:31 +02004248 redraw_asap(CLEAR);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004249# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02004250 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004251 key_name[0] = (int)KS_EXTRA;
4252 key_name[1] = (int)KE_IGNORE;
4253 slen = i + 1;
4254 }
4255 else
4256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004258 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004260 LOG_TR("Received CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 crv_status = CRV_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004262# ifdef FEAT_AUTOCMD
4263 did_cursorhold = TRUE;
4264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265
4266 /* If this code starts with CSI, you can bet that the
4267 * terminal uses 8-bit codes. */
4268 if (tp[0] == CSI)
4269 switch_to_8bit();
4270
4271 /* rxvt sends its version number: "20703" is 2.7.3.
4272 * Ignore it for when the user has set 'term' to xterm,
4273 * even though it's an rxvt. */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004274 if (extra > 0)
4275 extra = atoi((char *)tp + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 if (extra > 20000)
4277 extra = 0;
4278
4279 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
4280 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004281 /* Only set 'ttymouse' automatically if it was not set
4282 * by the user already. */
4283 if (!option_was_set((char_u *)"ttym"))
4284 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004285# ifdef TTYM_SGR
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004286 if (extra >= 277)
4287 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004288 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004289 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004290# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004291 /* if xterm version >= 95 use mouse dragging */
4292 if (extra >= 95)
4293 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004295 }
4296
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 /* if xterm version >= 141 try to get termcap codes */
4298 if (extra >= 141)
4299 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004300 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 check_for_codes = TRUE;
4302 need_gather = TRUE;
4303 req_codes_from_term();
4304 }
4305 }
4306# ifdef FEAT_EVAL
4307 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4308# endif
4309# ifdef FEAT_AUTOCMD
4310 apply_autocmds(EVENT_TERMRESPONSE,
4311 NULL, NULL, FALSE, curbuf);
4312# endif
4313 key_name[0] = (int)KS_EXTRA;
4314 key_name[1] = (int)KE_IGNORE;
4315 slen = i + 1;
4316 }
4317 }
4318
4319 /* Check for '<Esc>P1+r<hex bytes><Esc>\'. A "0" instead of the
4320 * "1" means an invalid request. */
4321 else if (check_for_codes
4322 && ((tp[0] == ESC && tp[1] == 'P' && len >= 2)
4323 || tp[0] == DCS))
4324 {
4325 j = 1 + (tp[0] != DCS);
4326 for (i = j; i < len; ++i)
4327 if ((tp[i] == ESC && tp[i + 1] == '\\' && i + 1 < len)
4328 || tp[i] == STERM)
4329 {
4330 if (i - j >= 3 && tp[j + 1] == '+' && tp[j + 2] == 'r')
4331 got_code_from_term(tp + j, i);
4332 key_name[0] = (int)KS_EXTRA;
4333 key_name[1] = (int)KE_IGNORE;
4334 slen = i + 1 + (tp[i] == ESC);
4335 break;
4336 }
4337
4338 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004339 {
4340 LOG_TR("not enough characters for XT");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 return -1; /* not enough characters */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344 }
4345#endif
4346
4347 if (key_name[0] == NUL)
4348 continue; /* No match at this position, try next one */
4349
4350 /* We only get here when we have a complete termcode match */
4351
4352#ifdef FEAT_MOUSE
4353# ifdef FEAT_GUI
4354 /*
4355 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4356 * so that we know which window to scroll later.
4357 */
4358 if (gui.in_use
4359 && key_name[0] == (int)KS_EXTRA
4360 && (key_name[1] == (int)KE_X1MOUSE
4361 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004362 || key_name[1] == (int)KE_MOUSELEFT
4363 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 || key_name[1] == (int)KE_MOUSEDOWN
4365 || key_name[1] == (int)KE_MOUSEUP))
4366 {
4367 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4368 if (num_bytes == -1) /* not enough coordinates */
4369 return -1;
4370 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4371 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4372 slen += num_bytes;
4373 }
4374 else
4375# endif
4376 /*
4377 * If it is a mouse click, get the coordinates.
4378 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004379 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004381 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382# endif
4383# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004384 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385# endif
4386# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004387 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388# endif
4389# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004390 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004392# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004393 || key_name[0] == KS_URXVT_MOUSE
4394# endif
4395# ifdef FEAT_MOUSE_SGR
4396 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004397# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 )
4399 {
4400 is_click = is_drag = FALSE;
4401
Bram Moolenaar864207d2008-06-24 22:14:38 +00004402# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4403 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 if (key_name[0] == (int)KS_MOUSE)
4405 {
4406 /*
4407 * For xterm and MSDOS we get "<t_mouse>scr", where
4408 * s == encoded button state:
4409 * 0x20 = left button down
4410 * 0x21 = middle button down
4411 * 0x22 = right button down
4412 * 0x23 = any button release
4413 * 0x60 = button 4 down (scroll wheel down)
4414 * 0x61 = button 5 down (scroll wheel up)
4415 * add 0x04 for SHIFT
4416 * add 0x08 for ALT
4417 * add 0x10 for CTRL
4418 * add 0x20 for mouse drag (0x40 is drag with left button)
4419 * c == column + ' ' + 1 == column + 33
4420 * r == row + ' ' + 1 == row + 33
4421 *
4422 * The coordinates are passed on through global variables.
4423 * Ugly, but this avoids trouble with mouse clicks at an
4424 * unexpected moment and allows for mapping them.
4425 */
4426 for (;;)
4427 {
4428#ifdef FEAT_GUI
4429 if (gui.in_use)
4430 {
4431 /* GUI uses more bits for columns > 223 */
4432 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4433 if (num_bytes == -1) /* not enough coordinates */
4434 return -1;
4435 mouse_code = bytes[0];
4436 mouse_col = 128 * (bytes[1] - ' ' - 1)
4437 + bytes[2] - ' ' - 1;
4438 mouse_row = 128 * (bytes[3] - ' ' - 1)
4439 + bytes[4] - ' ' - 1;
4440 }
4441 else
4442#endif
4443 {
4444 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4445 if (num_bytes == -1) /* not enough coordinates */
4446 return -1;
4447 mouse_code = bytes[0];
4448 mouse_col = bytes[1] - ' ' - 1;
4449 mouse_row = bytes[2] - ' ' - 1;
4450 }
4451 slen += num_bytes;
4452
4453 /* If the following bytes is also a mouse code and it has
4454 * the same code, dump this one and get the next. This
4455 * makes dragging a whole lot faster. */
4456#ifdef FEAT_GUI
4457 if (gui.in_use)
4458 j = 3;
4459 else
4460#endif
4461 j = termcodes[idx].len;
4462 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4463 && tp[slen + j] == mouse_code
4464 && tp[slen + j + 1] != NUL
4465 && tp[slen + j + 2] != NUL
4466#ifdef FEAT_GUI
4467 && (!gui.in_use
4468 || (tp[slen + j + 3] != NUL
4469 && tp[slen + j + 4] != NUL))
4470#endif
4471 )
4472 slen += j;
4473 else
4474 break;
4475 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004478# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4479 if (key_name[0] == KS_URXVT_MOUSE
4480 || key_name[0] == KS_SGR_MOUSE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004481 {
4482 for (;;)
4483 {
4484 /* URXVT 1015 mouse reporting mode:
4485 * Almost identical to xterm mouse mode, except the values
4486 * are decimal instead of bytes.
4487 *
4488 * \033[%d;%d;%dM
4489 * ^-- row
4490 * ^----- column
4491 * ^-------- code
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004492 *
4493 * SGR 1006 mouse reporting mode:
4494 * Almost identical to xterm mouse mode, except the values
4495 * are decimal instead of bytes.
4496 *
4497 * \033[<%d;%d;%dM
4498 * ^-- row
4499 * ^----- column
4500 * ^-------- code
4501 *
4502 * \033[<%d;%d;%dm : mouse release event
4503 * ^-- row
4504 * ^----- column
4505 * ^-------- code
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004506 */
4507 p = tp + slen;
4508
4509 mouse_code = getdigits(&p);
4510 if (*p++ != ';')
4511 return -1;
4512
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004513 /* when mouse reporting is SGR, add 32 to mouse code */
4514 if (key_name[0] == KS_SGR_MOUSE)
4515 mouse_code += 32;
4516
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004517 mouse_col = getdigits(&p) - 1;
4518 if (*p++ != ';')
4519 return -1;
4520
4521 mouse_row = getdigits(&p) - 1;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004522 if (key_name[0] == KS_SGR_MOUSE && *p == 'm')
4523 mouse_code |= MOUSE_RELEASE;
4524 else if (*p != 'M')
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004525 return -1;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004526 p++;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004527
4528 slen += (int)(p - (tp + slen));
4529
4530 /* skip this one if next one has same code (like xterm
4531 * case) */
4532 j = termcodes[idx].len;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004533 if (STRNCMP(tp, tp + slen, (size_t)j) == 0)
4534 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004535 int slen2;
4536 int cmd_complete = 0;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004537
4538 /* check if the command is complete by looking for the
4539 * 'M' */
4540 for (slen2 = slen; slen2 < len; slen2++)
4541 {
4542 if (tp[slen2] == 'M'
4543 || (key_name[0] == KS_SGR_MOUSE
4544 && tp[slen2] == 'm'))
4545 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004546 cmd_complete = 1;
4547 break;
4548 }
4549 }
4550 p += j;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004551 if (cmd_complete && getdigits(&p) == mouse_code)
4552 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004553 slen += j; /* skip the \033[ */
4554 continue;
4555 }
4556 }
4557 break;
4558 }
4559 }
4560# endif
4561
4562 if (key_name[0] == (int)KS_MOUSE
4563#ifdef FEAT_MOUSE_URXVT
4564 || key_name[0] == (int)KS_URXVT_MOUSE
4565#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004566#ifdef FEAT_MOUSE_SGR
4567 || key_name[0] == KS_SGR_MOUSE
4568#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004569 )
4570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571# if !defined(MSWIN) && !defined(MSDOS)
4572 /*
4573 * Handle mouse events.
4574 * Recognize the xterm mouse wheel, but not in the GUI, the
4575 * Linux console with GPM and the MS-DOS or Win32 console
4576 * (multi-clicks use >= 0x60).
4577 */
4578 if (mouse_code >= MOUSEWHEEL_LOW
4579# ifdef FEAT_GUI
4580 && !gui.in_use
4581# endif
4582# ifdef FEAT_MOUSE_GPM
4583 && gpm_flag == 0
4584# endif
4585 )
4586 {
4587 /* Keep the mouse_code before it's changed, so that we
4588 * remember that it was a mouse wheel click. */
4589 wheel_code = mouse_code;
4590 }
4591# ifdef FEAT_MOUSE_XTERM
4592 else if (held_button == MOUSE_RELEASE
4593# ifdef FEAT_GUI
4594 && !gui.in_use
4595# endif
4596 && (mouse_code == 0x23 || mouse_code == 0x24))
4597 {
4598 /* Apparently used by rxvt scroll wheel. */
4599 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4600 }
4601# endif
4602
4603# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4604 else if (use_xterm_mouse() > 1)
4605 {
4606 if (mouse_code & MOUSE_DRAG_XTERM)
4607 mouse_code |= MOUSE_DRAG;
4608 }
4609# endif
4610# ifdef FEAT_XCLIPBOARD
4611 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4612 {
4613 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4614 stop_xterm_trace();
4615 else
4616 start_xterm_trace(mouse_code);
4617 }
4618# endif
4619# endif
4620 }
4621# endif /* !UNIX || FEAT_MOUSE_XTERM */
4622# ifdef FEAT_MOUSE_NET
4623 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4624 {
4625 int mc, mr;
4626
4627 /* expect a rather limited sequence like: balancing {
4628 * \033}6,45\r
4629 * '6' is the row, 45 is the column
4630 */
4631 p = tp + slen;
4632 mr = getdigits(&p);
4633 if (*p++ != ',')
4634 return -1;
4635 mc = getdigits(&p);
4636 if (*p++ != '\r')
4637 return -1;
4638
4639 mouse_col = mc - 1;
4640 mouse_row = mr - 1;
4641 mouse_code = MOUSE_LEFT;
4642 slen += (int)(p - (tp + slen));
4643 }
4644# endif /* FEAT_MOUSE_NET */
4645# ifdef FEAT_MOUSE_JSB
4646 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4647 {
4648 int mult, val, iter, button, status;
4649
4650 /* JSBTERM Input Model
4651 * \033[0~zw uniq escape sequence
4652 * (L-x) Left button pressed - not pressed x not reporting
4653 * (M-x) Middle button pressed - not pressed x not reporting
4654 * (R-x) Right button pressed - not pressed x not reporting
4655 * (SDmdu) Single , Double click, m mouse move d button down
4656 * u button up
4657 * ### X cursor position padded to 3 digits
4658 * ### Y cursor position padded to 3 digits
4659 * (s-x) SHIFT key pressed - not pressed x not reporting
4660 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004661 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 */
4663
4664 p = tp + slen;
4665 button = mouse_code = 0;
4666 switch (*p++)
4667 {
4668 case 'L': button = 1; break;
4669 case '-': break;
4670 case 'x': break; /* ignore sequence */
4671 default: return -1; /* Unknown Result */
4672 }
4673 switch (*p++)
4674 {
4675 case 'M': button |= 2; break;
4676 case '-': break;
4677 case 'x': break; /* ignore sequence */
4678 default: return -1; /* Unknown Result */
4679 }
4680 switch (*p++)
4681 {
4682 case 'R': button |= 4; break;
4683 case '-': break;
4684 case 'x': break; /* ignore sequence */
4685 default: return -1; /* Unknown Result */
4686 }
4687 status = *p++;
4688 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4689 mult /= 10, p++)
4690 if (*p >= '0' && *p <= '9')
4691 val += (*p - '0') * mult;
4692 else
4693 return -1;
4694 mouse_col = val;
4695 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4696 mult /= 10, p++)
4697 if (*p >= '0' && *p <= '9')
4698 val += (*p - '0') * mult;
4699 else
4700 return -1;
4701 mouse_row = val;
4702 switch (*p++)
4703 {
4704 case 's': button |= 8; break; /* SHIFT key Pressed */
4705 case '-': break; /* Not Pressed */
4706 case 'x': break; /* Not Reporting */
4707 default: return -1; /* Unknown Result */
4708 }
4709 switch (*p++)
4710 {
4711 case 'c': button |= 16; break; /* CTRL key Pressed */
4712 case '-': break; /* Not Pressed */
4713 case 'x': break; /* Not Reporting */
4714 default: return -1; /* Unknown Result */
4715 }
4716 if (*p++ != '\033')
4717 return -1;
4718 if (*p++ != '\\')
4719 return -1;
4720 switch (status)
4721 {
4722 case 'D': /* Double Click */
4723 case 'S': /* Single Click */
4724 if (button & 1) mouse_code |= MOUSE_LEFT;
4725 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4726 if (button & 4) mouse_code |= MOUSE_RIGHT;
4727 if (button & 8) mouse_code |= MOUSE_SHIFT;
4728 if (button & 16) mouse_code |= MOUSE_CTRL;
4729 break;
4730 case 'm': /* Mouse move */
4731 if (button & 1) mouse_code |= MOUSE_LEFT;
4732 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4733 if (button & 4) mouse_code |= MOUSE_RIGHT;
4734 if (button & 8) mouse_code |= MOUSE_SHIFT;
4735 if (button & 16) mouse_code |= MOUSE_CTRL;
4736 if ((button & 7) != 0)
4737 {
4738 held_button = mouse_code;
4739 mouse_code |= MOUSE_DRAG;
4740 }
4741 is_drag = TRUE;
4742 showmode();
4743 break;
4744 case 'd': /* Button Down */
4745 if (button & 1) mouse_code |= MOUSE_LEFT;
4746 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4747 if (button & 4) mouse_code |= MOUSE_RIGHT;
4748 if (button & 8) mouse_code |= MOUSE_SHIFT;
4749 if (button & 16) mouse_code |= MOUSE_CTRL;
4750 break;
4751 case 'u': /* Button Up */
4752 if (button & 1)
4753 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4754 if (button & 2)
4755 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4756 if (button & 4)
4757 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4758 if (button & 8)
4759 mouse_code |= MOUSE_SHIFT;
4760 if (button & 16)
4761 mouse_code |= MOUSE_CTRL;
4762 break;
4763 default: return -1; /* Unknown Result */
4764 }
4765
4766 slen += (p - (tp + slen));
4767 }
4768# endif /* FEAT_MOUSE_JSB */
4769# ifdef FEAT_MOUSE_DEC
4770 if (key_name[0] == (int)KS_DEC_MOUSE)
4771 {
4772 /* The DEC Locator Input Model
4773 * Netterm delivers the code sequence:
4774 * \033[2;4;24;80&w (left button down)
4775 * \033[3;0;24;80&w (left button up)
4776 * \033[6;1;24;80&w (right button down)
4777 * \033[7;0;24;80&w (right button up)
4778 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4779 * Pe is the event code
4780 * Pb is the button code
4781 * Pr is the row coordinate
4782 * Pc is the column coordinate
4783 * Pp is the third coordinate (page number)
4784 * Pe, the event code indicates what event caused this report
4785 * The following event codes are defined:
4786 * 0 - request, the terminal received an explicit request
4787 * for a locator report, but the locator is unavailable
4788 * 1 - request, the terminal received an explicit request
4789 * for a locator report
4790 * 2 - left button down
4791 * 3 - left button up
4792 * 4 - middle button down
4793 * 5 - middle button up
4794 * 6 - right button down
4795 * 7 - right button up
4796 * 8 - fourth button down
4797 * 9 - fourth button up
4798 * 10 - locator outside filter rectangle
4799 * Pb, the button code, ASCII decimal 0-15 indicating which
4800 * buttons are down if any. The state of the four buttons
4801 * on the locator correspond to the low four bits of the
4802 * decimal value,
4803 * "1" means button depressed
4804 * 0 - no buttons down,
4805 * 1 - right,
4806 * 2 - middle,
4807 * 4 - left,
4808 * 8 - fourth
4809 * Pr is the row coordinate of the locator position in the page,
4810 * encoded as an ASCII decimal value.
4811 * If Pr is omitted, the locator position is undefined
4812 * (outside the terminal window for example).
4813 * Pc is the column coordinate of the locator position in the
4814 * page, encoded as an ASCII decimal value.
4815 * If Pc is omitted, the locator position is undefined
4816 * (outside the terminal window for example).
4817 * Pp is the page coordinate of the locator position
4818 * encoded as an ASCII decimal value.
4819 * The page coordinate may be omitted if the locator is on
4820 * page one (the default). We ignore it anyway.
4821 */
4822 int Pe, Pb, Pr, Pc;
4823
4824 p = tp + slen;
4825
4826 /* get event status */
4827 Pe = getdigits(&p);
4828 if (*p++ != ';')
4829 return -1;
4830
4831 /* get button status */
4832 Pb = getdigits(&p);
4833 if (*p++ != ';')
4834 return -1;
4835
4836 /* get row status */
4837 Pr = getdigits(&p);
4838 if (*p++ != ';')
4839 return -1;
4840
4841 /* get column status */
4842 Pc = getdigits(&p);
4843
4844 /* the page parameter is optional */
4845 if (*p == ';')
4846 {
4847 p++;
4848 (void)getdigits(&p);
4849 }
4850 if (*p++ != '&')
4851 return -1;
4852 if (*p++ != 'w')
4853 return -1;
4854
4855 mouse_code = 0;
4856 switch (Pe)
4857 {
4858 case 0: return -1; /* position request while unavailable */
4859 case 1: /* a response to a locator position request includes
4860 the status of all buttons */
4861 Pb &= 7; /* mask off and ignore fourth button */
4862 if (Pb & 4)
4863 mouse_code = MOUSE_LEFT;
4864 if (Pb & 2)
4865 mouse_code = MOUSE_MIDDLE;
4866 if (Pb & 1)
4867 mouse_code = MOUSE_RIGHT;
4868 if (Pb)
4869 {
4870 held_button = mouse_code;
4871 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004872 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874 is_drag = TRUE;
4875 showmode();
4876 break;
4877 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004878 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 break;
4880 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
4881 break;
4882 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004883 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 break;
4885 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
4886 break;
4887 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004888 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 break;
4890 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
4891 break;
4892 case 8: return -1; /* fourth button down */
4893 case 9: return -1; /* fourth button up */
4894 case 10: return -1; /* mouse outside of filter rectangle */
4895 default: return -1; /* should never occur */
4896 }
4897
4898 mouse_col = Pc - 1;
4899 mouse_row = Pr - 1;
4900
4901 slen += (int)(p - (tp + slen));
4902 }
4903# endif /* FEAT_MOUSE_DEC */
4904# ifdef FEAT_MOUSE_PTERM
4905 if (key_name[0] == (int)KS_PTERM_MOUSE)
4906 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004907 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908
4909 p = tp + slen;
4910
4911 action = getdigits(&p);
4912 if (*p++ != ';')
4913 return -1;
4914
4915 mouse_row = getdigits(&p);
4916 if (*p++ != ';')
4917 return -1;
4918 mouse_col = getdigits(&p);
4919 if (*p++ != ';')
4920 return -1;
4921
4922 button = getdigits(&p);
4923 mouse_code = 0;
4924
4925 switch( button )
4926 {
4927 case 4: mouse_code = MOUSE_LEFT; break;
4928 case 1: mouse_code = MOUSE_RIGHT; break;
4929 case 2: mouse_code = MOUSE_MIDDLE; break;
4930 default: return -1;
4931 }
4932
4933 switch( action )
4934 {
4935 case 31: /* Initial press */
4936 if (*p++ != ';')
4937 return -1;
4938
4939 num_clicks = getdigits(&p); /* Not used */
4940 break;
4941
4942 case 32: /* Release */
4943 mouse_code |= MOUSE_RELEASE;
4944 break;
4945
4946 case 33: /* Drag */
4947 held_button = mouse_code;
4948 mouse_code |= MOUSE_DRAG;
4949 break;
4950
4951 default:
4952 return -1;
4953 }
4954
4955 if (*p++ != 't')
4956 return -1;
4957
4958 slen += (p - (tp + slen));
4959 }
4960# endif /* FEAT_MOUSE_PTERM */
4961
4962 /* Interpret the mouse code */
4963 current_button = (mouse_code & MOUSE_CLICK_MASK);
4964 if (current_button == MOUSE_RELEASE
4965# ifdef FEAT_MOUSE_XTERM
4966 && wheel_code == 0
4967# endif
4968 )
4969 {
4970 /*
4971 * If we get a mouse drag or release event when
4972 * there is no mouse button held down (held_button ==
4973 * MOUSE_RELEASE), produce a K_IGNORE below.
4974 * (can happen when you hold down two buttons
4975 * and then let them go, or click in the menu bar, but not
4976 * on a menu, and drag into the text).
4977 */
4978 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
4979 is_drag = TRUE;
4980 current_button = held_button;
4981 }
4982 else if (wheel_code == 0)
4983 {
4984# ifdef CHECK_DOUBLE_CLICK
4985# ifdef FEAT_MOUSE_GPM
4986# ifdef FEAT_GUI
4987 /*
4988 * Only for Unix, when GUI or gpm is not active, we handle
4989 * multi-clicks here.
4990 */
4991 if (gpm_flag == 0 && !gui.in_use)
4992# else
4993 if (gpm_flag == 0)
4994# endif
4995# else
4996# ifdef FEAT_GUI
4997 if (!gui.in_use)
4998# endif
4999# endif
5000 {
5001 /*
5002 * Compute the time elapsed since the previous mouse click.
5003 */
5004 gettimeofday(&mouse_time, NULL);
5005 timediff = (mouse_time.tv_usec
5006 - orig_mouse_time.tv_usec) / 1000;
5007 if (timediff < 0)
5008 --orig_mouse_time.tv_sec;
5009 timediff += (mouse_time.tv_sec
5010 - orig_mouse_time.tv_sec) * 1000;
5011 orig_mouse_time = mouse_time;
5012 if (mouse_code == orig_mouse_code
5013 && timediff < p_mouset
5014 && orig_num_clicks != 4
5015 && orig_mouse_col == mouse_col
5016 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005017 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005019 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005021 )
5022#ifdef FEAT_WINDOWS
5023 /* Double click in tab pages line also works
5024 * when window contents changes. */
5025 || (mouse_row == 0 && firstwin->w_winrow > 0)
5026#endif
5027 )
5028 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 ++orig_num_clicks;
5030 else
5031 orig_num_clicks = 1;
5032 orig_mouse_col = mouse_col;
5033 orig_mouse_row = mouse_row;
5034 orig_topline = curwin->w_topline;
5035#ifdef FEAT_DIFF
5036 orig_topfill = curwin->w_topfill;
5037#endif
5038 }
5039# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5040 else
5041 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5042# endif
5043# else
5044 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5045# endif
5046 is_click = TRUE;
5047 orig_mouse_code = mouse_code;
5048 }
5049 if (!is_drag)
5050 held_button = mouse_code & MOUSE_CLICK_MASK;
5051
5052 /*
5053 * Translate the actual mouse event into a pseudo mouse event.
5054 * First work out what modifiers are to be used.
5055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 if (orig_mouse_code & MOUSE_SHIFT)
5057 modifiers |= MOD_MASK_SHIFT;
5058 if (orig_mouse_code & MOUSE_CTRL)
5059 modifiers |= MOD_MASK_CTRL;
5060 if (orig_mouse_code & MOUSE_ALT)
5061 modifiers |= MOD_MASK_ALT;
5062 if (orig_num_clicks == 2)
5063 modifiers |= MOD_MASK_2CLICK;
5064 else if (orig_num_clicks == 3)
5065 modifiers |= MOD_MASK_3CLICK;
5066 else if (orig_num_clicks == 4)
5067 modifiers |= MOD_MASK_4CLICK;
5068
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 /* Work out our pseudo mouse event */
5070 key_name[0] = (int)KS_EXTRA;
5071 if (wheel_code != 0)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005072 {
5073 if (wheel_code & MOUSE_CTRL)
5074 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005075 if (wheel_code & MOUSE_ALT)
5076 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 key_name[1] = (wheel_code & 1)
5078 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 else
5081 key_name[1] = get_pseudo_mouse_code(current_button,
5082 is_click, is_drag);
5083 }
5084#endif /* FEAT_MOUSE */
5085
5086#ifdef FEAT_GUI
5087 /*
5088 * If using the GUI, then we get menu and scrollbar events.
5089 *
5090 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5091 * four bytes which are to be taken as a pointer to the vimmenu_T
5092 * structure.
5093 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005094 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005095 * is one byte with the tab index.
5096 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5098 * by one byte representing the scrollbar number, and then four bytes
5099 * representing a long_u which is the new value of the scrollbar.
5100 *
5101 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5102 * KE_FILLER followed by four bytes representing a long_u which is the
5103 * new value of the scrollbar.
5104 */
5105# ifdef FEAT_MENU
5106 else if (key_name[0] == (int)KS_MENU)
5107 {
5108 long_u val;
5109
5110 num_bytes = get_long_from_buf(tp + slen, &val);
5111 if (num_bytes == -1)
5112 return -1;
5113 current_menu = (vimmenu_T *)val;
5114 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005115
5116 /* The menu may have been deleted right after it was used, check
5117 * for that. */
5118 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5119 {
5120 key_name[0] = KS_EXTRA;
5121 key_name[1] = (int)KE_IGNORE;
5122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005125# ifdef FEAT_GUI_TABLINE
5126 else if (key_name[0] == (int)KS_TABLINE)
5127 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005128 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005129 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5130 if (num_bytes == -1)
5131 return -1;
5132 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005133 if (current_tab == 255) /* -1 in a byte gives 255 */
5134 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005135 slen += num_bytes;
5136 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005137 else if (key_name[0] == (int)KS_TABMENU)
5138 {
5139 /* Selecting tabline tab or using its menu. */
5140 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5141 if (num_bytes == -1)
5142 return -1;
5143 current_tab = (int)bytes[0];
5144 current_tabmenu = (int)bytes[1];
5145 slen += num_bytes;
5146 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148# ifndef USE_ON_FLY_SCROLL
5149 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5150 {
5151 long_u val;
5152
5153 /* Get the last scrollbar event in the queue of the same type */
5154 j = 0;
5155 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5156 && tp[j + 2] != NUL; ++i)
5157 {
5158 j += 3;
5159 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5160 if (num_bytes == -1)
5161 break;
5162 if (i == 0)
5163 current_scrollbar = (int)bytes[0];
5164 else if (current_scrollbar != (int)bytes[0])
5165 break;
5166 j += num_bytes;
5167 num_bytes = get_long_from_buf(tp + j, &val);
5168 if (num_bytes == -1)
5169 break;
5170 scrollbar_value = val;
5171 j += num_bytes;
5172 slen = j;
5173 }
5174 if (i == 0) /* not enough characters to make one */
5175 return -1;
5176 }
5177 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5178 {
5179 long_u val;
5180
5181 /* Get the last horiz. scrollbar event in the queue */
5182 j = 0;
5183 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5184 && tp[j + 2] != NUL; ++i)
5185 {
5186 j += 3;
5187 num_bytes = get_long_from_buf(tp + j, &val);
5188 if (num_bytes == -1)
5189 break;
5190 scrollbar_value = val;
5191 j += num_bytes;
5192 slen = j;
5193 }
5194 if (i == 0) /* not enough characters to make one */
5195 return -1;
5196 }
5197# endif /* !USE_ON_FLY_SCROLL */
5198#endif /* FEAT_GUI */
5199
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005200 /*
5201 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5202 */
5203 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5204
5205 /*
5206 * Add any modifier codes to our string.
5207 */
5208 new_slen = 0; /* Length of what will replace the termcode */
5209 if (modifiers != 0)
5210 {
5211 /* Some keys have the modifier included. Need to handle that here
5212 * to make mappings work. */
5213 key = simplify_key(key, &modifiers);
5214 if (modifiers != 0)
5215 {
5216 string[new_slen++] = K_SPECIAL;
5217 string[new_slen++] = (int)KS_MODIFIER;
5218 string[new_slen++] = modifiers;
5219 }
5220 }
5221
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005223 key_name[0] = KEY2TERMCAP0(key);
5224 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005226 {
5227 /* from ":set <M-b>=xx" */
5228#ifdef FEAT_MBYTE
5229 if (has_mbyte)
5230 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5231 else
5232#endif
5233 string[new_slen++] = key_name[1];
5234 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005235 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5236 && key_name[1] == KE_IGNORE)
5237 {
5238 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5239 * to indicate what happened. */
5240 retval = KEYLEN_REMOVED;
5241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 else
5243 {
5244 string[new_slen++] = K_SPECIAL;
5245 string[new_slen++] = key_name[0];
5246 string[new_slen++] = key_name[1];
5247 }
5248 string[new_slen] = NUL;
5249 extra = new_slen - slen;
5250 if (buf == NULL)
5251 {
5252 if (extra < 0)
5253 /* remove matched chars, taking care of noremap */
5254 del_typebuf(-extra, offset);
5255 else if (extra > 0)
5256 /* insert the extra space we need */
5257 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5258
5259 /*
5260 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5261 * typebuf.tb_buf[]!
5262 */
5263 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5264 (size_t)new_slen);
5265 }
5266 else
5267 {
5268 if (extra < 0)
5269 /* remove matched characters */
5270 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005271 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005273 {
5274 /* Insert the extra space we need. If there is insufficient
5275 * space return -1. */
5276 if (*buflen + extra + new_slen >= bufsize)
5277 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005279 (size_t)(*buflen - offset));
5280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005282 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005284 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286
Bram Moolenaar2951b772013-07-03 12:45:31 +02005287#ifdef FEAT_TERMRESPONSE
5288 LOG_TR("normal character");
5289#endif
5290
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 return 0; /* no match found */
5292}
5293
5294/*
5295 * Replace any terminal code strings in from[] with the equivalent internal
5296 * vim representation. This is used for the "from" and "to" part of a
5297 * mapping, and the "to" part of a menu command.
5298 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5299 * '<'.
5300 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5301 *
5302 * The replacement is done in result[] and finally copied into allocated
5303 * memory. If this all works well *bufp is set to the allocated memory and a
5304 * pointer to it is returned. If something fails *bufp is set to NULL and from
5305 * is returned.
5306 *
5307 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5308 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5309 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5310 * instead of a CTRL-V.
5311 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005312 char_u *
5313replace_termcodes(from, bufp, from_part, do_lt, special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 char_u *from;
5315 char_u **bufp;
5316 int from_part;
5317 int do_lt; /* also translate <lt> */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005318 int special; /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
5320 int i;
5321 int slen;
5322 int key;
5323 int dlen = 0;
5324 char_u *src;
5325 int do_backslash; /* backslash is a special character */
5326 int do_special; /* recognize <> key codes */
5327 int do_key_code; /* recognize raw key codes */
5328 char_u *result; /* buffer for resulting string */
5329
5330 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005331 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5333
5334 /*
5335 * Allocate space for the translation. Worst case a single character is
5336 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5337 */
5338 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5339 if (result == NULL) /* out of memory */
5340 {
5341 *bufp = NULL;
5342 return from;
5343 }
5344
5345 src = from;
5346
5347 /*
5348 * Check for #n at start only: function key n
5349 */
5350 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5351 {
5352 result[dlen++] = K_SPECIAL;
5353 result[dlen++] = 'k';
5354 if (src[1] == '0')
5355 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5356 else
5357 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5358 src += 2;
5359 }
5360
5361 /*
5362 * Copy each byte from *from to result[dlen]
5363 */
5364 while (*src != NUL)
5365 {
5366 /*
5367 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005368 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 */
5370 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5371 {
5372#ifdef FEAT_EVAL
5373 /*
5374 * Replace <SID> by K_SNR <script-nr> _.
5375 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5376 */
5377 if (STRNICMP(src, "<SID>", 5) == 0)
5378 {
5379 if (current_SID <= 0)
5380 EMSG(_(e_usingsid));
5381 else
5382 {
5383 src += 5;
5384 result[dlen++] = K_SPECIAL;
5385 result[dlen++] = (int)KS_EXTRA;
5386 result[dlen++] = (int)KE_SNR;
5387 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5388 dlen += (int)STRLEN(result + dlen);
5389 result[dlen++] = '_';
5390 continue;
5391 }
5392 }
5393#endif
5394
5395 slen = trans_special(&src, result + dlen, TRUE);
5396 if (slen)
5397 {
5398 dlen += slen;
5399 continue;
5400 }
5401 }
5402
5403 /*
5404 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5405 * Note that this is also checked after replacing the <> form.
5406 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5407 * it could be a character in the file.
5408 */
5409 if (do_key_code)
5410 {
5411 i = find_term_bykeys(src);
5412 if (i >= 0)
5413 {
5414 result[dlen++] = K_SPECIAL;
5415 result[dlen++] = termcodes[i].name[0];
5416 result[dlen++] = termcodes[i].name[1];
5417 src += termcodes[i].len;
5418 /* If terminal code matched, continue after it. */
5419 continue;
5420 }
5421 }
5422
5423#ifdef FEAT_EVAL
5424 if (do_special)
5425 {
5426 char_u *p, *s, len;
5427
5428 /*
5429 * Replace <Leader> by the value of "mapleader".
5430 * Replace <LocalLeader> by the value of "maplocalleader".
5431 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5432 */
5433 if (STRNICMP(src, "<Leader>", 8) == 0)
5434 {
5435 len = 8;
5436 p = get_var_value((char_u *)"g:mapleader");
5437 }
5438 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5439 {
5440 len = 13;
5441 p = get_var_value((char_u *)"g:maplocalleader");
5442 }
5443 else
5444 {
5445 len = 0;
5446 p = NULL;
5447 }
5448 if (len != 0)
5449 {
5450 /* Allow up to 8 * 6 characters for "mapleader". */
5451 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5452 s = (char_u *)"\\";
5453 else
5454 s = p;
5455 while (*s != NUL)
5456 result[dlen++] = *s++;
5457 src += len;
5458 continue;
5459 }
5460 }
5461#endif
5462
5463 /*
5464 * Remove CTRL-V and ignore the next character.
5465 * For "from" side the CTRL-V at the end is included, for the "to"
5466 * part it is removed.
5467 * If 'cpoptions' does not contain 'B', also accept a backslash.
5468 */
5469 key = *src;
5470 if (key == Ctrl_V || (do_backslash && key == '\\'))
5471 {
5472 ++src; /* skip CTRL-V or backslash */
5473 if (*src == NUL)
5474 {
5475 if (from_part)
5476 result[dlen++] = key;
5477 break;
5478 }
5479 }
5480
5481#ifdef FEAT_MBYTE
5482 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005483 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484#endif
5485 {
5486 /*
5487 * If the character is K_SPECIAL, replace it with K_SPECIAL
5488 * KS_SPECIAL KE_FILLER.
5489 * If compiled with the GUI replace CSI with K_CSI.
5490 */
5491 if (*src == K_SPECIAL)
5492 {
5493 result[dlen++] = K_SPECIAL;
5494 result[dlen++] = KS_SPECIAL;
5495 result[dlen++] = KE_FILLER;
5496 }
5497# ifdef FEAT_GUI
5498 else if (*src == CSI)
5499 {
5500 result[dlen++] = K_SPECIAL;
5501 result[dlen++] = KS_EXTRA;
5502 result[dlen++] = (int)KE_CSI;
5503 }
5504# endif
5505 else
5506 result[dlen++] = *src;
5507 ++src;
5508 }
5509 }
5510 result[dlen] = NUL;
5511
5512 /*
5513 * Copy the new string to allocated memory.
5514 * If this fails, just return from.
5515 */
5516 if ((*bufp = vim_strsave(result)) != NULL)
5517 from = *bufp;
5518 vim_free(result);
5519 return from;
5520}
5521
5522/*
5523 * Find a termcode with keys 'src' (must be NUL terminated).
5524 * Return the index in termcodes[], or -1 if not found.
5525 */
5526 int
5527find_term_bykeys(src)
5528 char_u *src;
5529{
5530 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005531 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
5533 for (i = 0; i < tc_len; ++i)
5534 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005535 if (slen == termcodes[i].len
5536 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 return i;
5538 }
5539 return -1;
5540}
5541
5542/*
5543 * Gather the first characters in the terminal key codes into a string.
5544 * Used to speed up check_termcode().
5545 */
5546 static void
5547gather_termleader()
5548{
5549 int i;
5550 int len = 0;
5551
5552#ifdef FEAT_GUI
5553 if (gui.in_use)
5554 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5555#endif
5556#ifdef FEAT_TERMRESPONSE
5557 if (check_for_codes)
5558 termleader[len++] = DCS; /* the termcode response starts with DCS
5559 in 8-bit mode */
5560#endif
5561 termleader[len] = NUL;
5562
5563 for (i = 0; i < tc_len; ++i)
5564 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5565 {
5566 termleader[len++] = termcodes[i].code[0];
5567 termleader[len] = NUL;
5568 }
5569
5570 need_gather = FALSE;
5571}
5572
5573/*
5574 * Show all termcodes (for ":set termcap")
5575 * This code looks a lot like showoptions(), but is different.
5576 */
5577 void
5578show_termcodes()
5579{
5580 int col;
5581 int *items;
5582 int item_count;
5583 int run;
5584 int row, rows;
5585 int cols;
5586 int i;
5587 int len;
5588
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005589#define INC3 27 /* try to make three columns */
5590#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591#define GAP 2 /* spaces between columns */
5592
5593 if (tc_len == 0) /* no terminal codes (must be GUI) */
5594 return;
5595 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5596 if (items == NULL)
5597 return;
5598
5599 /* Highlight title */
5600 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5601
5602 /*
5603 * do the loop two times:
5604 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005605 * 2. display the medium items (medium length strings)
5606 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005608 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 {
5610 /*
5611 * collect the items in items[]
5612 */
5613 item_count = 0;
5614 for (i = 0; i < tc_len; i++)
5615 {
5616 len = show_one_termcode(termcodes[i].name,
5617 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005618 if (len <= INC3 - GAP ? run == 1
5619 : len <= INC2 - GAP ? run == 2
5620 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 items[item_count++] = i;
5622 }
5623
5624 /*
5625 * display the items
5626 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005627 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005629 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 if (cols == 0)
5631 cols = 1;
5632 rows = (item_count + cols - 1) / cols;
5633 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005634 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 rows = item_count;
5636 for (row = 0; row < rows && !got_int; ++row)
5637 {
5638 msg_putchar('\n'); /* go to next line */
5639 if (got_int) /* 'q' typed in more */
5640 break;
5641 col = 0;
5642 for (i = row; i < item_count; i += rows)
5643 {
5644 msg_col = col; /* make columns */
5645 show_one_termcode(termcodes[items[i]].name,
5646 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005647 if (run == 2)
5648 col += INC2;
5649 else
5650 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
5652 out_flush();
5653 ui_breakcheck();
5654 }
5655 }
5656 vim_free(items);
5657}
5658
5659/*
5660 * Show one termcode entry.
5661 * Output goes into IObuff[]
5662 */
5663 int
5664show_one_termcode(name, code, printit)
5665 char_u *name;
5666 char_u *code;
5667 int printit;
5668{
5669 char_u *p;
5670 int len;
5671
5672 if (name[0] > '~')
5673 {
5674 IObuff[0] = ' ';
5675 IObuff[1] = ' ';
5676 IObuff[2] = ' ';
5677 IObuff[3] = ' ';
5678 }
5679 else
5680 {
5681 IObuff[0] = 't';
5682 IObuff[1] = '_';
5683 IObuff[2] = name[0];
5684 IObuff[3] = name[1];
5685 }
5686 IObuff[4] = ' ';
5687
5688 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5689 if (p[1] != 't')
5690 STRCPY(IObuff + 5, p);
5691 else
5692 IObuff[5] = NUL;
5693 len = (int)STRLEN(IObuff);
5694 do
5695 IObuff[len++] = ' ';
5696 while (len < 17);
5697 IObuff[len] = NUL;
5698 if (code == NULL)
5699 len += 4;
5700 else
5701 len += vim_strsize(code);
5702
5703 if (printit)
5704 {
5705 msg_puts(IObuff);
5706 if (code == NULL)
5707 msg_puts((char_u *)"NULL");
5708 else
5709 msg_outtrans(code);
5710 }
5711 return len;
5712}
5713
5714#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5715/*
5716 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5717 * termcap codes from the terminal itself.
5718 * We get them one by one to avoid a very long response string.
5719 */
5720static int xt_index_in = 0;
5721static int xt_index_out = 0;
5722
5723 static void
5724req_codes_from_term()
5725{
5726 xt_index_out = 0;
5727 xt_index_in = 0;
5728 req_more_codes_from_term();
5729}
5730
5731 static void
5732req_more_codes_from_term()
5733{
5734 char buf[11];
5735 int old_idx = xt_index_out;
5736
5737 /* Don't do anything when going to exit. */
5738 if (exiting)
5739 return;
5740
5741 /* Send up to 10 more requests out than we received. Avoid sending too
5742 * many, there can be a buffer overflow somewhere. */
5743 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5744 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02005745# ifdef DEBUG_TERMRESPONSE
5746 char dbuf[100];
5747
5748 sprintf(dbuf, "Requesting XT %d: %s",
5749 xt_index_out, key_names[xt_index_out]);
5750 log_tr(dbuf);
5751# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 sprintf(buf, "\033P+q%02x%02x\033\\",
5753 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5754 out_str_nf((char_u *)buf);
5755 ++xt_index_out;
5756 }
5757
5758 /* Send the codes out right away. */
5759 if (xt_index_out != old_idx)
5760 out_flush();
5761}
5762
5763/*
5764 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5765 * A "0" instead of the "1" indicates a code that isn't supported.
5766 * Both <name> and <string> are encoded in hex.
5767 * "code" points to the "0" or "1".
5768 */
5769 static void
5770got_code_from_term(code, len)
5771 char_u *code;
5772 int len;
5773{
5774#define XT_LEN 100
5775 char_u name[3];
5776 char_u str[XT_LEN];
5777 int i;
5778 int j = 0;
5779 int c;
5780
5781 /* A '1' means the code is supported, a '0' means it isn't.
5782 * When half the length is > XT_LEN we can't use it.
5783 * Our names are currently all 2 characters. */
5784 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5785 {
5786 /* Get the name from the response and find it in the table. */
5787 name[0] = hexhex2nr(code + 3);
5788 name[1] = hexhex2nr(code + 5);
5789 name[2] = NUL;
5790 for (i = 0; key_names[i] != NULL; ++i)
5791 {
5792 if (STRCMP(key_names[i], name) == 0)
5793 {
5794 xt_index_in = i;
5795 break;
5796 }
5797 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005798# ifdef DEBUG_TERMRESPONSE
5799 {
5800 char buf[100];
5801
5802 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
5803 log_tr(buf);
5804 }
5805# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 if (key_names[i] != NULL)
5807 {
5808 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5809 str[j++] = c;
5810 str[j] = NUL;
5811 if (name[0] == 'C' && name[1] == 'o')
5812 {
5813 /* Color count is not a key code. */
5814 i = atoi((char *)str);
5815 if (i != t_colors)
5816 {
5817 /* Nr of colors changed, initialize highlighting and
Bram Moolenaar238a5642006-02-21 22:12:05 +00005818 * redraw everything. This causes a redraw, which usually
5819 * clears the message. Try keeping the message if it
5820 * might work. */
5821 set_keep_msg_from_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 set_color_count(i);
5823 init_highlight(TRUE, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +02005824#ifdef DEBUG_TERMRESPONSE
5825 {
5826 char buf[100];
5827 int r = redraw_asap(CLEAR);
5828
5829 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
5830 log_tr(buf);
5831 }
5832#else
5833 redraw_asap(CLEAR);
5834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 }
5836 }
5837 else
5838 {
5839 /* First delete any existing entry with the same code. */
5840 i = find_term_bykeys(str);
5841 if (i >= 0)
5842 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005843 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 }
5845 }
5846 }
5847
5848 /* May request more codes now that we received one. */
5849 ++xt_index_in;
5850 req_more_codes_from_term();
5851}
5852
5853/*
5854 * Check if there are any unanswered requests and deal with them.
5855 * This is called before starting an external program or getting direct
5856 * keyboard input. We don't want responses to be send to that program or
5857 * handled as typed text.
5858 */
5859 static void
5860check_for_codes_from_term()
5861{
5862 int c;
5863
5864 /* If no codes requested or all are answered, no need to wait. */
5865 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5866 return;
5867
5868 /* Vgetc() will check for and handle any response.
5869 * Keep calling vpeekc() until we don't get any responses. */
5870 ++no_mapping;
5871 ++allow_keys;
5872 for (;;)
5873 {
5874 c = vpeekc();
5875 if (c == NUL) /* nothing available */
5876 break;
5877
5878 /* If a response is recognized it's replaced with K_IGNORE, must read
5879 * it from the input stream. If there is no K_IGNORE we can't do
5880 * anything, break here (there might be some responses further on, but
5881 * we don't want to throw away any typed chars). */
5882 if (c != K_SPECIAL && c != K_IGNORE)
5883 break;
5884 c = vgetc();
5885 if (c != K_IGNORE)
5886 {
5887 vungetc(c);
5888 break;
5889 }
5890 }
5891 --no_mapping;
5892 --allow_keys;
5893}
5894#endif
5895
5896#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5897/*
5898 * Translate an internal mapping/abbreviation representation into the
5899 * corresponding external one recognized by :map/:abbrev commands;
5900 * respects the current B/k/< settings of 'cpoption'.
5901 *
5902 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00005903 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 *
5905 * It uses a growarray to build the translation string since the
5906 * latter can be wider than the original description. The caller has to
5907 * free the string afterwards.
5908 *
5909 * Returns NULL when there is a problem.
5910 */
5911 char_u *
5912translate_mapping(str, expmap)
5913 char_u *str;
5914 int expmap; /* TRUE when expanding mappings on command-line */
5915{
5916 garray_T ga;
5917 int c;
5918 int modifiers;
5919 int cpo_bslash;
5920 int cpo_special;
5921 int cpo_keycode;
5922
5923 ga_init(&ga);
5924 ga.ga_itemsize = 1;
5925 ga.ga_growsize = 40;
5926
5927 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
5928 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
5929 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5930
5931 for (; *str; ++str)
5932 {
5933 c = *str;
5934 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5935 {
5936 modifiers = 0;
5937 if (str[1] == KS_MODIFIER)
5938 {
5939 str++;
5940 modifiers = *++str;
5941 c = *++str;
5942 }
5943 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
5944 {
5945 int i;
5946
5947 /* try to find special key in termcodes */
5948 for (i = 0; i < tc_len; ++i)
5949 if (termcodes[i].name[0] == str[1]
5950 && termcodes[i].name[1] == str[2])
5951 break;
5952 if (i < tc_len)
5953 {
5954 ga_concat(&ga, termcodes[i].code);
5955 str += 2;
5956 continue; /* for (str) */
5957 }
5958 }
5959 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5960 {
5961 if (expmap && cpo_special)
5962 {
5963 ga_clear(&ga);
5964 return NULL;
5965 }
5966 c = TO_SPECIAL(str[1], str[2]);
5967 if (c == K_ZERO) /* display <Nul> as ^@ */
5968 c = NUL;
5969 str += 2;
5970 }
5971 if (IS_SPECIAL(c) || modifiers) /* special key */
5972 {
5973 if (expmap && cpo_special)
5974 {
5975 ga_clear(&ga);
5976 return NULL;
5977 }
5978 ga_concat(&ga, get_special_key_name(c, modifiers));
5979 continue; /* for (str) */
5980 }
5981 }
5982 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
5983 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
5984 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
5985 if (c)
5986 ga_append(&ga, c);
5987 }
5988 ga_append(&ga, NUL);
5989 return (char_u *)(ga.ga_data);
5990}
5991#endif
5992
5993#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
5994static char ksme_str[20];
5995static char ksmr_str[20];
5996static char ksmd_str[20];
5997
5998/*
5999 * For Win32 console: update termcap codes for existing console attributes.
6000 */
6001 void
6002update_tcap(attr)
6003 int attr;
6004{
6005 struct builtin_term *p;
6006
6007 p = find_builtin_term(DEFAULT_TERM);
6008 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6009 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6010 attr | 0x08); /* FOREGROUND_INTENSITY */
6011 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6012 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6013
6014 while (p->bt_string != NULL)
6015 {
6016 if (p->bt_entry == (int)KS_ME)
6017 p->bt_string = &ksme_str[0];
6018 else if (p->bt_entry == (int)KS_MR)
6019 p->bt_string = &ksmr_str[0];
6020 else if (p->bt_entry == (int)KS_MD)
6021 p->bt_string = &ksmd_str[0];
6022 ++p;
6023 }
6024}
6025#endif