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