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