blob: a471c1e0c3bf530dbfed6562217c31e5510b40d1 [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
Bram Moolenaar86b68352004-12-27 21:59:20 +00002981/*
2982 * Invoked just before the screen structures are going to be (re)allocated.
2983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 void
2985win_new_shellsize()
2986{
2987 static int old_Rows = 0;
2988 static int old_Columns = 0;
2989
2990 if (old_Rows != Rows || old_Columns != Columns)
2991 ui_new_shellsize();
2992 if (old_Rows != Rows)
2993 {
2994 old_Rows = Rows;
2995 shell_new_rows(); /* update window sizes */
2996 }
2997 if (old_Columns != Columns)
2998 {
2999 old_Columns = Columns;
3000#ifdef FEAT_VERTSPLIT
3001 shell_new_columns(); /* update window sizes */
3002#endif
3003 }
3004}
3005
3006/*
3007 * Call this function when the Vim shell has been resized in any way.
3008 * Will obtain the current size and redraw (also when size didn't change).
3009 */
3010 void
3011shell_resized()
3012{
3013 set_shellsize(0, 0, FALSE);
3014}
3015
3016/*
3017 * Check if the shell size changed. Handle a resize.
3018 * When the size didn't change, nothing happens.
3019 */
3020 void
3021shell_resized_check()
3022{
3023 int old_Rows = Rows;
3024 int old_Columns = Columns;
3025
3026 (void)ui_get_shellsize();
3027 check_shellsize();
3028 if (old_Rows != Rows || old_Columns != Columns)
3029 shell_resized();
3030}
3031
3032/*
3033 * Set size of the Vim shell.
3034 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3035 * window size (this is used for the :win command).
3036 * If 'mustset' is FALSE, we may try to get the real window size and if
3037 * it fails use 'width' and 'height'.
3038 */
3039 void
3040set_shellsize(width, height, mustset)
3041 int width, height;
3042 int mustset;
3043{
3044 static int busy = FALSE;
3045
3046 /*
3047 * Avoid recursiveness, can happen when setting the window size causes
3048 * another window-changed signal.
3049 */
3050 if (busy)
3051 return;
3052
3053 if (width < 0 || height < 0) /* just checking... */
3054 return;
3055
3056 if (State == HITRETURN || State == SETWSIZE) /* postpone the resizing */
3057 {
3058 State = SETWSIZE;
3059 return;
3060 }
3061
3062 ++busy;
3063
3064#ifdef AMIGA
3065 out_flush(); /* must do this before mch_get_shellsize() for
3066 some obscure reason */
3067#endif
3068
3069 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3070 {
3071 Rows = height;
3072 Columns = width;
3073 check_shellsize();
3074 ui_set_shellsize(mustset);
3075 }
3076 else
3077 check_shellsize();
3078
3079 /* The window layout used to be adjusted here, but it now happens in
3080 * screenalloc() (also invoked from screenclear()). That is because the
3081 * "busy" check above may skip this, but not screenalloc(). */
3082
3083 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3084 screenclear();
3085 else
3086 screen_start(); /* don't know where cursor is now */
3087
3088 if (starting != NO_SCREEN)
3089 {
3090#ifdef FEAT_TITLE
3091 maketitle();
3092#endif
3093 changed_line_abv_curs();
3094 invalidate_botline();
3095
3096 /*
3097 * We only redraw when it's needed:
3098 * - While at the more prompt or executing an external command, don't
3099 * redraw, but position the cursor.
3100 * - While editing the command line, only redraw that.
3101 * - in Ex mode, don't redraw anything.
3102 * - Otherwise, redraw right now, and position the cursor.
3103 * Always need to call update_screen() or screenalloc(), to make
3104 * sure Rows/Columns and the size of ScreenLines[] is correct!
3105 */
3106 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3107 || exmode_active)
3108 {
3109 screenalloc(FALSE);
3110 repeat_message();
3111 }
3112 else if (State & CMDLINE)
3113 {
3114 update_screen(NOT_VALID);
3115 redrawcmdline();
3116 }
3117 else
3118 {
3119 update_topline();
3120 update_screen(NOT_VALID);
3121 if (redrawing())
3122 setcursor();
3123 }
3124 cursor_on(); /* redrawing may have switched it off */
3125 }
3126 out_flush();
3127 --busy;
3128}
3129
3130/*
3131 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3132 * commands and Ex mode).
3133 */
3134 void
3135settmode(tmode)
3136 int tmode;
3137{
3138#ifdef FEAT_GUI
3139 /* don't set the term where gvim was started to any mode */
3140 if (gui.in_use)
3141 return;
3142#endif
3143
3144 if (full_screen)
3145 {
3146 /* In Ex mode, never set to RAW */
3147 if (exmode_active == EXMODE_NORMAL)
3148 tmode = TMODE_COOK;
3149
3150 /*
3151 * When returning after calling a shell we want to really set the
3152 * terminal to raw mode, even though we think it already is, because
3153 * the shell program may have reset the terminal mode.
3154 * When we think the terminal is normal, don't try to set it to
3155 * normal again, because that causes problems (logout!) on some
3156 * machines.
3157 */
3158 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3159 {
3160#ifdef FEAT_TERMRESPONSE
3161 /* May need to check for T_CRV response and termcodes, it doesn't
3162 * work in Cooked mode, an external program may get them. */
3163 if (tmode != TMODE_RAW && crv_status == CRV_SENT)
3164 (void)vpeekc_nomap();
3165 check_for_codes_from_term();
3166#endif
3167#ifdef FEAT_MOUSE_TTY
3168 if (tmode != TMODE_RAW)
3169 mch_setmouse(FALSE); /* switch mouse off */
3170#endif
3171 out_flush();
3172 mch_settmode(tmode); /* machine specific function */
3173 cur_tmode = tmode;
3174#ifdef FEAT_MOUSE
3175 if (tmode == TMODE_RAW)
3176 setmouse(); /* may switch mouse on */
3177#endif
3178 out_flush();
3179 }
3180#ifdef FEAT_TERMRESPONSE
3181 may_req_termresponse();
3182#endif
3183 }
3184}
3185
3186 void
3187starttermcap()
3188{
3189 if (full_screen && !termcap_active)
3190 {
3191 out_str(T_TI); /* start termcap mode */
3192 out_str(T_KS); /* start "keypad transmit" mode */
3193 out_flush();
3194 termcap_active = TRUE;
3195 screen_start(); /* don't know where cursor is now */
3196#ifdef FEAT_TERMRESPONSE
3197 may_req_termresponse();
3198 /* Immediately check for a response. If t_Co changes, we don't want
3199 * to redraw with wrong colors first. */
3200 check_for_codes_from_term();
3201#endif
3202 }
3203}
3204
3205 void
3206stoptermcap()
3207{
3208 screen_stop_highlight();
3209 reset_cterm_colors();
3210 if (termcap_active)
3211 {
3212#ifdef FEAT_TERMRESPONSE
3213 /* May need to check for T_CRV response. */
3214 if (crv_status == CRV_SENT)
3215 (void)vpeekc_nomap();
3216 /* Check for termcodes first, otherwise an external program may get
3217 * them. */
3218 check_for_codes_from_term();
3219#endif
3220 out_str(T_KE); /* stop "keypad transmit" mode */
3221 out_flush();
3222 termcap_active = FALSE;
3223 cursor_on(); /* just in case it is still off */
3224 out_str(T_TE); /* stop termcap mode */
3225 screen_start(); /* don't know where cursor is now */
3226 out_flush();
3227 }
3228}
3229
3230#ifdef FEAT_TERMRESPONSE
3231/*
3232 * Request version string (for xterm) when needed.
3233 * Only do this after switching to raw mode, otherwise the result will be
3234 * echoed.
3235 * Only do this after termcap mode has been started, otherwise the codes for
3236 * the cursor keys may be wrong.
3237 * The result is caught in check_termcode().
3238 */
3239 static void
3240may_req_termresponse()
3241{
3242 if (crv_status == CRV_GET
3243 && cur_tmode == TMODE_RAW
3244 && termcap_active
3245#ifdef UNIX
3246 && isatty(1)
3247#endif
3248 && *T_CRV != NUL)
3249 {
3250 out_str(T_CRV);
3251 crv_status = CRV_SENT;
3252 /* check for the characters now, otherwise they might be eaten by
3253 * get_keystroke() */
3254 out_flush();
3255 (void)vpeekc_nomap();
3256 }
3257}
3258#endif
3259
3260/*
3261 * Return TRUE when saving and restoring the screen.
3262 */
3263 int
3264swapping_screen()
3265{
3266 return (full_screen && *T_TI != NUL);
3267}
3268
3269#ifdef FEAT_MOUSE
3270/*
3271 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3272 */
3273 void
3274setmouse()
3275{
3276# ifdef FEAT_MOUSE_TTY
3277 int checkfor;
3278# endif
3279
3280# ifdef FEAT_MOUSESHAPE
3281 update_mouseshape(-1);
3282# endif
3283
3284# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3285# ifdef FEAT_GUI
3286 /* In the GUI the mouse is always enabled. */
3287 if (gui.in_use)
3288 return;
3289# endif
3290 /* be quick when mouse is off */
3291 if (*p_mouse == NUL || has_mouse_termcode == 0)
3292 return;
3293
3294 /* don't switch mouse on when not in raw mode (Ex mode) */
3295 if (cur_tmode != TMODE_RAW)
3296 {
3297 mch_setmouse(FALSE);
3298 return;
3299 }
3300
3301# ifdef FEAT_VISUAL
3302 if (VIsual_active)
3303 checkfor = MOUSE_VISUAL;
3304 else
3305# endif
3306 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
3307 checkfor = MOUSE_RETURN;
3308 else if (State & INSERT)
3309 checkfor = MOUSE_INSERT;
3310 else if (State & CMDLINE)
3311 checkfor = MOUSE_COMMAND;
3312 else if (State == CONFIRM || State == EXTERNCMD)
3313 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3314 else
3315 checkfor = MOUSE_NORMAL; /* assume normal mode */
3316
3317 if (mouse_has(checkfor))
3318 mch_setmouse(TRUE);
3319 else
3320 mch_setmouse(FALSE);
3321# endif
3322}
3323
3324/*
3325 * Return TRUE if
3326 * - "c" is in 'mouse', or
3327 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3328 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3329 * normal editing mode (not at hit-return message).
3330 */
3331 int
3332mouse_has(c)
3333 int c;
3334{
3335 char_u *p;
3336
3337 for (p = p_mouse; *p; ++p)
3338 switch (*p)
3339 {
3340 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3341 return TRUE;
3342 break;
3343 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3344 return TRUE;
3345 break;
3346 default: if (c == *p) return TRUE; break;
3347 }
3348 return FALSE;
3349}
3350
3351/*
3352 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3353 */
3354 int
3355mouse_model_popup()
3356{
3357 return (p_mousem[0] == 'p');
3358}
3359#endif
3360
3361/*
3362 * By outputting the 'cursor very visible' termcap code, for some windowed
3363 * terminals this makes the screen scrolled to the correct position.
3364 * Used when starting Vim or returning from a shell.
3365 */
3366 void
3367scroll_start()
3368{
3369 if (*T_VS != NUL)
3370 {
3371 out_str(T_VS);
3372 out_str(T_VE);
3373 screen_start(); /* don't know where cursor is now */
3374 }
3375}
3376
3377static int cursor_is_off = FALSE;
3378
3379/*
3380 * Enable the cursor.
3381 */
3382 void
3383cursor_on()
3384{
3385 if (cursor_is_off)
3386 {
3387 out_str(T_VE);
3388 cursor_is_off = FALSE;
3389 }
3390}
3391
3392/*
3393 * Disable the cursor.
3394 */
3395 void
3396cursor_off()
3397{
3398 if (full_screen)
3399 {
3400 if (!cursor_is_off)
3401 out_str(T_VI); /* disable cursor */
3402 cursor_is_off = TRUE;
3403 }
3404}
3405
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003406#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407/*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003408 * Set cursor shape to match Insert mode.
3409 */
3410 void
3411term_cursor_shape()
3412{
3413 static int showing_insert_mode = MAYBE;
3414
3415 if (!full_screen || *T_CSI == NUL || *T_CEI == NUL)
3416 return;
3417
3418 if (State & INSERT)
3419 {
3420 if (showing_insert_mode != TRUE)
3421 out_str(T_CSI); /* disable cursor */
3422 showing_insert_mode = TRUE;
3423 }
3424 else
3425 {
3426 if (showing_insert_mode != FALSE)
3427 out_str(T_CEI); /* disable cursor */
3428 showing_insert_mode = FALSE;
3429 }
3430}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003431#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003432
3433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 * Set scrolling region for window 'wp'.
3435 * The region starts 'off' lines from the start of the window.
3436 * Also set the vertical scroll region for a vertically split window. Always
3437 * the full width of the window, excluding the vertical separator.
3438 */
3439 void
3440scroll_region_set(wp, off)
3441 win_T *wp;
3442 int off;
3443{
3444 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3445 W_WINROW(wp) + off));
3446#ifdef FEAT_VERTSPLIT
3447 if (*T_CSV != NUL && wp->w_width != Columns)
3448 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3449 W_WINCOL(wp)));
3450#endif
3451 screen_start(); /* don't know where cursor is now */
3452}
3453
3454/*
3455 * Reset scrolling region to the whole screen.
3456 */
3457 void
3458scroll_region_reset()
3459{
3460 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
3461#ifdef FEAT_VERTSPLIT
3462 if (*T_CSV != NUL)
3463 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3464#endif
3465 screen_start(); /* don't know where cursor is now */
3466}
3467
3468
3469/*
3470 * List of terminal codes that are currently recognized.
3471 */
3472
3473struct termcode
3474{
3475 char_u name[2]; /* termcap name of entry */
3476 char_u *code; /* terminal code (in allocated memory) */
3477 int len; /* STRLEN(code) */
3478} *termcodes = NULL;
3479
3480static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3481static int tc_len = 0; /* current number of entries in termcodes[] */
3482
3483 void
3484clear_termcodes()
3485{
3486 while (tc_len > 0)
3487 vim_free(termcodes[--tc_len].code);
3488 vim_free(termcodes);
3489 termcodes = NULL;
3490 tc_max_len = 0;
3491
3492#ifdef HAVE_TGETENT
3493 BC = (char *)empty_option;
3494 UP = (char *)empty_option;
3495 PC = NUL; /* set pad character to NUL */
3496 ospeed = 0;
3497#endif
3498
3499 need_gather = TRUE; /* need to fill termleader[] */
3500}
3501
3502/*
3503 * Add a new entry to the list of terminal codes.
3504 * The list is kept alphabetical for ":set termcap"
3505 */
3506 void
3507add_termcode(name, string, use_8bit)
3508 char_u *name;
3509 char_u *string;
3510 int use_8bit; /* replace 7-bit control by 8-bit one */
3511{
3512 struct termcode *new_tc;
3513 int i, j;
3514 char_u *s;
3515
3516 if (string == NULL || *string == NUL)
3517 {
3518 del_termcode(name);
3519 return;
3520 }
3521
3522 s = vim_strsave(string);
3523 if (s == NULL)
3524 return;
3525
3526 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
3527 if (use_8bit && term_7to8bit(string) != 0)
3528 {
3529 mch_memmove(s, s + 1, STRLEN(s));
3530 s[0] = term_7to8bit(string);
3531 }
3532
3533 need_gather = TRUE; /* need to fill termleader[] */
3534
3535 /*
3536 * need to make space for more entries
3537 */
3538 if (tc_len == tc_max_len)
3539 {
3540 tc_max_len += 20;
3541 new_tc = (struct termcode *)alloc(
3542 (unsigned)(tc_max_len * sizeof(struct termcode)));
3543 if (new_tc == NULL)
3544 {
3545 tc_max_len -= 20;
3546 return;
3547 }
3548 for (i = 0; i < tc_len; ++i)
3549 new_tc[i] = termcodes[i];
3550 vim_free(termcodes);
3551 termcodes = new_tc;
3552 }
3553
3554 /*
3555 * Look for existing entry with the same name, it is replaced.
3556 * Look for an existing entry that is alphabetical higher, the new entry
3557 * is inserted in front of it.
3558 */
3559 for (i = 0; i < tc_len; ++i)
3560 {
3561 if (termcodes[i].name[0] < name[0])
3562 continue;
3563 if (termcodes[i].name[0] == name[0])
3564 {
3565 if (termcodes[i].name[1] < name[1])
3566 continue;
3567 /*
3568 * Exact match: Replace old code.
3569 */
3570 if (termcodes[i].name[1] == name[1])
3571 {
3572 vim_free(termcodes[i].code);
3573 --tc_len;
3574 break;
3575 }
3576 }
3577 /*
3578 * Found alphabetical larger entry, move rest to insert new entry
3579 */
3580 for (j = tc_len; j > i; --j)
3581 termcodes[j] = termcodes[j - 1];
3582 break;
3583 }
3584
3585 termcodes[i].name[0] = name[0];
3586 termcodes[i].name[1] = name[1];
3587 termcodes[i].code = s;
3588 termcodes[i].len = (int)STRLEN(s);
3589 ++tc_len;
3590}
3591
3592 char_u *
3593find_termcode(name)
3594 char_u *name;
3595{
3596 int i;
3597
3598 for (i = 0; i < tc_len; ++i)
3599 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3600 return termcodes[i].code;
3601 return NULL;
3602}
3603
3604#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3605 char_u *
3606get_termcode(i)
3607 int i;
3608{
3609 if (i >= tc_len)
3610 return NULL;
3611 return &termcodes[i].name[0];
3612}
3613#endif
3614
3615 void
3616del_termcode(name)
3617 char_u *name;
3618{
3619 int i;
3620
3621 if (termcodes == NULL) /* nothing there yet */
3622 return;
3623
3624 need_gather = TRUE; /* need to fill termleader[] */
3625
3626 for (i = 0; i < tc_len; ++i)
3627 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3628 {
3629 del_termcode_idx(i);
3630 return;
3631 }
3632 /* not found. Give error message? */
3633}
3634
3635 static void
3636del_termcode_idx(idx)
3637 int idx;
3638{
3639 int i;
3640
3641 vim_free(termcodes[idx].code);
3642 --tc_len;
3643 for (i = idx; i < tc_len; ++i)
3644 termcodes[i] = termcodes[i + 1];
3645}
3646
3647#ifdef FEAT_TERMRESPONSE
3648/*
3649 * Called when detected that the terminal sends 8-bit codes.
3650 * Convert all 7-bit codes to their 8-bit equivalent.
3651 */
3652 static void
3653switch_to_8bit()
3654{
3655 int i;
3656 int c;
3657
3658 /* Only need to do something when not already using 8-bit codes. */
3659 if (!term_is_8bit(T_NAME))
3660 {
3661 for (i = 0; i < tc_len; ++i)
3662 {
3663 c = term_7to8bit(termcodes[i].code);
3664 if (c != 0)
3665 {
3666 mch_memmove(termcodes[i].code + 1, termcodes[i].code + 2,
3667 STRLEN(termcodes[i].code + 1));
3668 termcodes[i].code[0] = c;
3669 }
3670 }
3671 need_gather = TRUE; /* need to fill termleader[] */
3672 }
3673 detected_8bit = TRUE;
3674}
3675#endif
3676
3677#ifdef CHECK_DOUBLE_CLICK
3678static linenr_T orig_topline = 0;
3679# ifdef FEAT_DIFF
3680static int orig_topfill = 0;
3681# endif
3682#endif
3683#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
3684/*
3685 * Checking for double clicks ourselves.
3686 * "orig_topline" is used to avoid detecting a double-click when the window
3687 * contents scrolled (e.g., when 'scrolloff' is non-zero).
3688 */
3689/*
3690 * Set orig_topline. Used when jumping to another window, so that a double
3691 * click still works.
3692 */
3693 void
3694set_mouse_topline(wp)
3695 win_T *wp;
3696{
3697 orig_topline = wp->w_topline;
3698# ifdef FEAT_DIFF
3699 orig_topfill = wp->w_topfill;
3700# endif
3701}
3702#endif
3703
3704/*
3705 * Check if typebuf.tb_buf[] contains a terminal key code.
3706 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
3707 * + max_offset].
3708 * Return 0 for no match, -1 for partial match, > 0 for full match.
3709 * With a match, the match is removed, the replacement code is inserted in
3710 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
3711 * returned.
3712 * When "buf" is not NULL, it is used instead of typebuf.tb_buf[]. "buflen" is
3713 * then the length of the string in buf[].
3714 */
3715 int
3716check_termcode(max_offset, buf, buflen)
3717 int max_offset;
3718 char_u *buf;
3719 int buflen;
3720{
3721 char_u *tp;
3722 char_u *p;
3723 int slen = 0; /* init for GCC */
3724 int len;
3725 int offset;
3726 char_u key_name[2];
3727 int new_slen;
3728 int extra;
3729 char_u string[MAX_KEY_CODE_LEN + 1];
3730 int i, j;
3731 int idx = 0;
3732#ifdef FEAT_MOUSE
3733# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI)
3734 char_u bytes[6];
3735 int num_bytes;
3736# endif
3737 int mouse_code = 0; /* init for GCC */
3738 int modifiers;
3739 int is_click, is_drag;
3740 int wheel_code = 0;
3741 int current_button;
3742 static int held_button = MOUSE_RELEASE;
3743 static int orig_num_clicks = 1;
3744 static int orig_mouse_code = 0x0;
3745# ifdef CHECK_DOUBLE_CLICK
3746 static int orig_mouse_col = 0;
3747 static int orig_mouse_row = 0;
3748 static struct timeval orig_mouse_time = {0, 0};
3749 /* time of previous mouse click */
3750 struct timeval mouse_time; /* time of current mouse click */
3751 long timediff; /* elapsed time in msec */
3752# endif
3753#endif
3754 int cpo_koffset;
3755#ifdef FEAT_MOUSE_GPM
3756 extern int gpm_flag; /* gpm library variable */
3757#endif
3758
3759 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
3760
3761 /*
3762 * Speed up the checks for terminal codes by gathering all first bytes
3763 * used in termleader[]. Often this is just a single <Esc>.
3764 */
3765 if (need_gather)
3766 gather_termleader();
3767
3768 /*
3769 * Check at several positions in typebuf.tb_buf[], to catch something like
3770 * "x<Up>" that can be mapped. Stop at max_offset, because characters
3771 * after that cannot be used for mapping, and with @r commands
3772 * typebuf.tb_buf[]
3773 * can become very long.
3774 * This is used often, KEEP IT FAST!
3775 */
3776 for (offset = 0; offset < max_offset; ++offset)
3777 {
3778 if (buf == NULL)
3779 {
3780 if (offset >= typebuf.tb_len)
3781 break;
3782 tp = typebuf.tb_buf + typebuf.tb_off + offset;
3783 len = typebuf.tb_len - offset; /* length of the input */
3784 }
3785 else
3786 {
3787 if (offset >= buflen)
3788 break;
3789 tp = buf + offset;
3790 len = buflen - offset;
3791 }
3792
3793 /*
3794 * Don't check characters after K_SPECIAL, those are already
3795 * translated terminal chars (avoid translating ~@^Hx).
3796 */
3797 if (*tp == K_SPECIAL)
3798 {
3799 offset += 2; /* there are always 2 extra characters */
3800 continue;
3801 }
3802
3803 /*
3804 * Skip this position if the character does not appear as the first
3805 * character in term_strings. This speeds up a lot, since most
3806 * termcodes start with the same character (ESC or CSI).
3807 */
3808 i = *tp;
3809 for (p = termleader; *p && *p != i; ++p)
3810 ;
3811 if (*p == NUL)
3812 continue;
3813
3814 /*
3815 * Skip this position if p_ek is not set and tp[0] is an ESC and we
3816 * are in Insert mode.
3817 */
3818 if (*tp == ESC && !p_ek && (State & INSERT))
3819 continue;
3820
3821 new_slen = 0; /* Length of what will replace the termcode */
3822 key_name[0] = NUL; /* no key name found yet */
3823
3824#ifdef FEAT_GUI
3825 if (gui.in_use)
3826 {
3827 /*
3828 * GUI special key codes are all of the form [CSI xx].
3829 */
3830 if (*tp == CSI) /* Special key from GUI */
3831 {
3832 if (len < 3)
3833 return -1; /* Shouldn't happen */
3834 slen = 3;
3835 key_name[0] = tp[1];
3836 key_name[1] = tp[2];
3837 }
3838 }
3839 else
3840#endif /* FEAT_GUI */
3841 {
3842 for (idx = 0; idx < tc_len; ++idx)
3843 {
3844 /*
3845 * Ignore the entry if we are not at the start of
3846 * typebuf.tb_buf[]
3847 * and there are not enough characters to make a match.
3848 * But only when the 'K' flag is in 'cpoptions'.
3849 */
3850 slen = termcodes[idx].len;
3851 if (cpo_koffset && offset && len < slen)
3852 continue;
3853 if (STRNCMP(termcodes[idx].code, tp,
3854 (size_t)(slen > len ? len : slen)) == 0)
3855 {
3856 if (len < slen) /* got a partial sequence */
3857 return -1; /* need to get more chars */
3858
3859 /*
3860 * When found a keypad key, check if there is another key
3861 * that matches and use that one. This makes <Home> to be
3862 * found instead of <kHome> when they produce the same
3863 * key code.
3864 */
3865 if (termcodes[idx].name[0] == 'K'
3866 && VIM_ISDIGIT(termcodes[idx].name[1]))
3867 {
3868 for (j = idx + 1; j < tc_len; ++j)
3869 if (termcodes[j].len == slen &&
3870 STRNCMP(termcodes[idx].code,
3871 termcodes[j].code, slen) == 0)
3872 {
3873 idx = j;
3874 break;
3875 }
3876 }
3877
3878 key_name[0] = termcodes[idx].name[0];
3879 key_name[1] = termcodes[idx].name[1];
3880
3881 break;
3882 }
3883 }
3884 }
3885
3886#ifdef FEAT_TERMRESPONSE
3887 if (key_name[0] == NUL)
3888 {
3889 /* Check for xterm version string: "<Esc>[>{x};{vers};{y}c". Also
3890 * eat other possible responses to t_RV, rxvt returns
3891 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[. */
3892 if (*T_CRV != NUL && ((tp[0] == ESC && tp[1] == '[' && len >= 3)
3893 || (tp[0] == CSI && len >= 2)))
3894 {
3895 j = 0;
3896 extra = 0;
3897 for (i = 2 + (tp[0] != CSI);
3898 i < len && (VIM_ISDIGIT(tp[i])
3899 || tp[i] == ';' || tp[i] == '.'); ++i)
3900 if (tp[i] == ';' && ++j == 1)
3901 extra = atoi((char *)tp + i + 1);
3902 if (i == len)
3903 return -1; /* not enough characters */
3904
3905 /* eat it when at least one digit and ending in 'c' */
3906 if (i > 2 + (tp[0] != CSI) && tp[i] == 'c')
3907 {
3908 crv_status = CRV_GOT;
3909
3910 /* If this code starts with CSI, you can bet that the
3911 * terminal uses 8-bit codes. */
3912 if (tp[0] == CSI)
3913 switch_to_8bit();
3914
3915 /* rxvt sends its version number: "20703" is 2.7.3.
3916 * Ignore it for when the user has set 'term' to xterm,
3917 * even though it's an rxvt. */
3918 if (extra > 20000)
3919 extra = 0;
3920
3921 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
3922 {
3923 /* if xterm version >= 95 use mouse dragging */
3924 if (extra >= 95)
3925 set_option_value((char_u *)"ttym", 0L,
3926 (char_u *)"xterm2", 0);
3927 /* if xterm version >= 141 try to get termcap codes */
3928 if (extra >= 141)
3929 {
3930 check_for_codes = TRUE;
3931 need_gather = TRUE;
3932 req_codes_from_term();
3933 }
3934 }
3935# ifdef FEAT_EVAL
3936 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
3937# endif
3938# ifdef FEAT_AUTOCMD
3939 apply_autocmds(EVENT_TERMRESPONSE,
3940 NULL, NULL, FALSE, curbuf);
3941# endif
3942 key_name[0] = (int)KS_EXTRA;
3943 key_name[1] = (int)KE_IGNORE;
3944 slen = i + 1;
3945 }
3946 }
3947
3948 /* Check for '<Esc>P1+r<hex bytes><Esc>\'. A "0" instead of the
3949 * "1" means an invalid request. */
3950 else if (check_for_codes
3951 && ((tp[0] == ESC && tp[1] == 'P' && len >= 2)
3952 || tp[0] == DCS))
3953 {
3954 j = 1 + (tp[0] != DCS);
3955 for (i = j; i < len; ++i)
3956 if ((tp[i] == ESC && tp[i + 1] == '\\' && i + 1 < len)
3957 || tp[i] == STERM)
3958 {
3959 if (i - j >= 3 && tp[j + 1] == '+' && tp[j + 2] == 'r')
3960 got_code_from_term(tp + j, i);
3961 key_name[0] = (int)KS_EXTRA;
3962 key_name[1] = (int)KE_IGNORE;
3963 slen = i + 1 + (tp[i] == ESC);
3964 break;
3965 }
3966
3967 if (i == len)
3968 return -1; /* not enough characters */
3969 }
3970 }
3971#endif
3972
3973 if (key_name[0] == NUL)
3974 continue; /* No match at this position, try next one */
3975
3976 /* We only get here when we have a complete termcode match */
3977
3978#ifdef FEAT_MOUSE
3979# ifdef FEAT_GUI
3980 /*
3981 * Only in the GUI: Fetch the pointer coordinates of the scroll event
3982 * so that we know which window to scroll later.
3983 */
3984 if (gui.in_use
3985 && key_name[0] == (int)KS_EXTRA
3986 && (key_name[1] == (int)KE_X1MOUSE
3987 || key_name[1] == (int)KE_X2MOUSE
3988 || key_name[1] == (int)KE_MOUSEDOWN
3989 || key_name[1] == (int)KE_MOUSEUP))
3990 {
3991 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
3992 if (num_bytes == -1) /* not enough coordinates */
3993 return -1;
3994 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
3995 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
3996 slen += num_bytes;
3997 }
3998 else
3999# endif
4000 /*
4001 * If it is a mouse click, get the coordinates.
4002 */
4003 if (key_name[0] == (int)KS_MOUSE
4004# ifdef FEAT_MOUSE_JSB
4005 || key_name[0] == (int)KS_JSBTERM_MOUSE
4006# endif
4007# ifdef FEAT_MOUSE_NET
4008 || key_name[0] == (int)KS_NETTERM_MOUSE
4009# endif
4010# ifdef FEAT_MOUSE_DEC
4011 || key_name[0] == (int)KS_DEC_MOUSE
4012# endif
4013# ifdef FEAT_MOUSE_PTERM
4014 || key_name[0] == (int)KS_PTERM_MOUSE
4015# endif
4016 )
4017 {
4018 is_click = is_drag = FALSE;
4019
4020# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI)
4021 if (key_name[0] == (int)KS_MOUSE)
4022 {
4023 /*
4024 * For xterm and MSDOS we get "<t_mouse>scr", where
4025 * s == encoded button state:
4026 * 0x20 = left button down
4027 * 0x21 = middle button down
4028 * 0x22 = right button down
4029 * 0x23 = any button release
4030 * 0x60 = button 4 down (scroll wheel down)
4031 * 0x61 = button 5 down (scroll wheel up)
4032 * add 0x04 for SHIFT
4033 * add 0x08 for ALT
4034 * add 0x10 for CTRL
4035 * add 0x20 for mouse drag (0x40 is drag with left button)
4036 * c == column + ' ' + 1 == column + 33
4037 * r == row + ' ' + 1 == row + 33
4038 *
4039 * The coordinates are passed on through global variables.
4040 * Ugly, but this avoids trouble with mouse clicks at an
4041 * unexpected moment and allows for mapping them.
4042 */
4043 for (;;)
4044 {
4045#ifdef FEAT_GUI
4046 if (gui.in_use)
4047 {
4048 /* GUI uses more bits for columns > 223 */
4049 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4050 if (num_bytes == -1) /* not enough coordinates */
4051 return -1;
4052 mouse_code = bytes[0];
4053 mouse_col = 128 * (bytes[1] - ' ' - 1)
4054 + bytes[2] - ' ' - 1;
4055 mouse_row = 128 * (bytes[3] - ' ' - 1)
4056 + bytes[4] - ' ' - 1;
4057 }
4058 else
4059#endif
4060 {
4061 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4062 if (num_bytes == -1) /* not enough coordinates */
4063 return -1;
4064 mouse_code = bytes[0];
4065 mouse_col = bytes[1] - ' ' - 1;
4066 mouse_row = bytes[2] - ' ' - 1;
4067 }
4068 slen += num_bytes;
4069
4070 /* If the following bytes is also a mouse code and it has
4071 * the same code, dump this one and get the next. This
4072 * makes dragging a whole lot faster. */
4073#ifdef FEAT_GUI
4074 if (gui.in_use)
4075 j = 3;
4076 else
4077#endif
4078 j = termcodes[idx].len;
4079 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4080 && tp[slen + j] == mouse_code
4081 && tp[slen + j + 1] != NUL
4082 && tp[slen + j + 2] != NUL
4083#ifdef FEAT_GUI
4084 && (!gui.in_use
4085 || (tp[slen + j + 3] != NUL
4086 && tp[slen + j + 4] != NUL))
4087#endif
4088 )
4089 slen += j;
4090 else
4091 break;
4092 }
4093
4094# if !defined(MSWIN) && !defined(MSDOS)
4095 /*
4096 * Handle mouse events.
4097 * Recognize the xterm mouse wheel, but not in the GUI, the
4098 * Linux console with GPM and the MS-DOS or Win32 console
4099 * (multi-clicks use >= 0x60).
4100 */
4101 if (mouse_code >= MOUSEWHEEL_LOW
4102# ifdef FEAT_GUI
4103 && !gui.in_use
4104# endif
4105# ifdef FEAT_MOUSE_GPM
4106 && gpm_flag == 0
4107# endif
4108 )
4109 {
4110 /* Keep the mouse_code before it's changed, so that we
4111 * remember that it was a mouse wheel click. */
4112 wheel_code = mouse_code;
4113 }
4114# ifdef FEAT_MOUSE_XTERM
4115 else if (held_button == MOUSE_RELEASE
4116# ifdef FEAT_GUI
4117 && !gui.in_use
4118# endif
4119 && (mouse_code == 0x23 || mouse_code == 0x24))
4120 {
4121 /* Apparently used by rxvt scroll wheel. */
4122 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4123 }
4124# endif
4125
4126# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4127 else if (use_xterm_mouse() > 1)
4128 {
4129 if (mouse_code & MOUSE_DRAG_XTERM)
4130 mouse_code |= MOUSE_DRAG;
4131 }
4132# endif
4133# ifdef FEAT_XCLIPBOARD
4134 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4135 {
4136 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4137 stop_xterm_trace();
4138 else
4139 start_xterm_trace(mouse_code);
4140 }
4141# endif
4142# endif
4143 }
4144# endif /* !UNIX || FEAT_MOUSE_XTERM */
4145# ifdef FEAT_MOUSE_NET
4146 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4147 {
4148 int mc, mr;
4149
4150 /* expect a rather limited sequence like: balancing {
4151 * \033}6,45\r
4152 * '6' is the row, 45 is the column
4153 */
4154 p = tp + slen;
4155 mr = getdigits(&p);
4156 if (*p++ != ',')
4157 return -1;
4158 mc = getdigits(&p);
4159 if (*p++ != '\r')
4160 return -1;
4161
4162 mouse_col = mc - 1;
4163 mouse_row = mr - 1;
4164 mouse_code = MOUSE_LEFT;
4165 slen += (int)(p - (tp + slen));
4166 }
4167# endif /* FEAT_MOUSE_NET */
4168# ifdef FEAT_MOUSE_JSB
4169 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4170 {
4171 int mult, val, iter, button, status;
4172
4173 /* JSBTERM Input Model
4174 * \033[0~zw uniq escape sequence
4175 * (L-x) Left button pressed - not pressed x not reporting
4176 * (M-x) Middle button pressed - not pressed x not reporting
4177 * (R-x) Right button pressed - not pressed x not reporting
4178 * (SDmdu) Single , Double click, m mouse move d button down
4179 * u button up
4180 * ### X cursor position padded to 3 digits
4181 * ### Y cursor position padded to 3 digits
4182 * (s-x) SHIFT key pressed - not pressed x not reporting
4183 * (c-x) CTRL key pressed - not pressed x not reporting
4184 * \033\\ terminateing sequence
4185 */
4186
4187 p = tp + slen;
4188 button = mouse_code = 0;
4189 switch (*p++)
4190 {
4191 case 'L': button = 1; break;
4192 case '-': break;
4193 case 'x': break; /* ignore sequence */
4194 default: return -1; /* Unknown Result */
4195 }
4196 switch (*p++)
4197 {
4198 case 'M': button |= 2; break;
4199 case '-': break;
4200 case 'x': break; /* ignore sequence */
4201 default: return -1; /* Unknown Result */
4202 }
4203 switch (*p++)
4204 {
4205 case 'R': button |= 4; break;
4206 case '-': break;
4207 case 'x': break; /* ignore sequence */
4208 default: return -1; /* Unknown Result */
4209 }
4210 status = *p++;
4211 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4212 mult /= 10, p++)
4213 if (*p >= '0' && *p <= '9')
4214 val += (*p - '0') * mult;
4215 else
4216 return -1;
4217 mouse_col = val;
4218 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4219 mult /= 10, p++)
4220 if (*p >= '0' && *p <= '9')
4221 val += (*p - '0') * mult;
4222 else
4223 return -1;
4224 mouse_row = val;
4225 switch (*p++)
4226 {
4227 case 's': button |= 8; break; /* SHIFT key Pressed */
4228 case '-': break; /* Not Pressed */
4229 case 'x': break; /* Not Reporting */
4230 default: return -1; /* Unknown Result */
4231 }
4232 switch (*p++)
4233 {
4234 case 'c': button |= 16; break; /* CTRL key Pressed */
4235 case '-': break; /* Not Pressed */
4236 case 'x': break; /* Not Reporting */
4237 default: return -1; /* Unknown Result */
4238 }
4239 if (*p++ != '\033')
4240 return -1;
4241 if (*p++ != '\\')
4242 return -1;
4243 switch (status)
4244 {
4245 case 'D': /* Double Click */
4246 case 'S': /* Single Click */
4247 if (button & 1) mouse_code |= MOUSE_LEFT;
4248 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4249 if (button & 4) mouse_code |= MOUSE_RIGHT;
4250 if (button & 8) mouse_code |= MOUSE_SHIFT;
4251 if (button & 16) mouse_code |= MOUSE_CTRL;
4252 break;
4253 case 'm': /* Mouse move */
4254 if (button & 1) mouse_code |= MOUSE_LEFT;
4255 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4256 if (button & 4) mouse_code |= MOUSE_RIGHT;
4257 if (button & 8) mouse_code |= MOUSE_SHIFT;
4258 if (button & 16) mouse_code |= MOUSE_CTRL;
4259 if ((button & 7) != 0)
4260 {
4261 held_button = mouse_code;
4262 mouse_code |= MOUSE_DRAG;
4263 }
4264 is_drag = TRUE;
4265 showmode();
4266 break;
4267 case 'd': /* Button Down */
4268 if (button & 1) mouse_code |= MOUSE_LEFT;
4269 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4270 if (button & 4) mouse_code |= MOUSE_RIGHT;
4271 if (button & 8) mouse_code |= MOUSE_SHIFT;
4272 if (button & 16) mouse_code |= MOUSE_CTRL;
4273 break;
4274 case 'u': /* Button Up */
4275 if (button & 1)
4276 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4277 if (button & 2)
4278 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4279 if (button & 4)
4280 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4281 if (button & 8)
4282 mouse_code |= MOUSE_SHIFT;
4283 if (button & 16)
4284 mouse_code |= MOUSE_CTRL;
4285 break;
4286 default: return -1; /* Unknown Result */
4287 }
4288
4289 slen += (p - (tp + slen));
4290 }
4291# endif /* FEAT_MOUSE_JSB */
4292# ifdef FEAT_MOUSE_DEC
4293 if (key_name[0] == (int)KS_DEC_MOUSE)
4294 {
4295 /* The DEC Locator Input Model
4296 * Netterm delivers the code sequence:
4297 * \033[2;4;24;80&w (left button down)
4298 * \033[3;0;24;80&w (left button up)
4299 * \033[6;1;24;80&w (right button down)
4300 * \033[7;0;24;80&w (right button up)
4301 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4302 * Pe is the event code
4303 * Pb is the button code
4304 * Pr is the row coordinate
4305 * Pc is the column coordinate
4306 * Pp is the third coordinate (page number)
4307 * Pe, the event code indicates what event caused this report
4308 * The following event codes are defined:
4309 * 0 - request, the terminal received an explicit request
4310 * for a locator report, but the locator is unavailable
4311 * 1 - request, the terminal received an explicit request
4312 * for a locator report
4313 * 2 - left button down
4314 * 3 - left button up
4315 * 4 - middle button down
4316 * 5 - middle button up
4317 * 6 - right button down
4318 * 7 - right button up
4319 * 8 - fourth button down
4320 * 9 - fourth button up
4321 * 10 - locator outside filter rectangle
4322 * Pb, the button code, ASCII decimal 0-15 indicating which
4323 * buttons are down if any. The state of the four buttons
4324 * on the locator correspond to the low four bits of the
4325 * decimal value,
4326 * "1" means button depressed
4327 * 0 - no buttons down,
4328 * 1 - right,
4329 * 2 - middle,
4330 * 4 - left,
4331 * 8 - fourth
4332 * Pr is the row coordinate of the locator position in the page,
4333 * encoded as an ASCII decimal value.
4334 * If Pr is omitted, the locator position is undefined
4335 * (outside the terminal window for example).
4336 * Pc is the column coordinate of the locator position in the
4337 * page, encoded as an ASCII decimal value.
4338 * If Pc is omitted, the locator position is undefined
4339 * (outside the terminal window for example).
4340 * Pp is the page coordinate of the locator position
4341 * encoded as an ASCII decimal value.
4342 * The page coordinate may be omitted if the locator is on
4343 * page one (the default). We ignore it anyway.
4344 */
4345 int Pe, Pb, Pr, Pc;
4346
4347 p = tp + slen;
4348
4349 /* get event status */
4350 Pe = getdigits(&p);
4351 if (*p++ != ';')
4352 return -1;
4353
4354 /* get button status */
4355 Pb = getdigits(&p);
4356 if (*p++ != ';')
4357 return -1;
4358
4359 /* get row status */
4360 Pr = getdigits(&p);
4361 if (*p++ != ';')
4362 return -1;
4363
4364 /* get column status */
4365 Pc = getdigits(&p);
4366
4367 /* the page parameter is optional */
4368 if (*p == ';')
4369 {
4370 p++;
4371 (void)getdigits(&p);
4372 }
4373 if (*p++ != '&')
4374 return -1;
4375 if (*p++ != 'w')
4376 return -1;
4377
4378 mouse_code = 0;
4379 switch (Pe)
4380 {
4381 case 0: return -1; /* position request while unavailable */
4382 case 1: /* a response to a locator position request includes
4383 the status of all buttons */
4384 Pb &= 7; /* mask off and ignore fourth button */
4385 if (Pb & 4)
4386 mouse_code = MOUSE_LEFT;
4387 if (Pb & 2)
4388 mouse_code = MOUSE_MIDDLE;
4389 if (Pb & 1)
4390 mouse_code = MOUSE_RIGHT;
4391 if (Pb)
4392 {
4393 held_button = mouse_code;
4394 mouse_code |= MOUSE_DRAG;
4395 WantQueryMouse = 1;
4396 }
4397 is_drag = TRUE;
4398 showmode();
4399 break;
4400 case 2: mouse_code = MOUSE_LEFT;
4401 WantQueryMouse = 1;
4402 break;
4403 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
4404 break;
4405 case 4: mouse_code = MOUSE_MIDDLE;
4406 WantQueryMouse = 1;
4407 break;
4408 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
4409 break;
4410 case 6: mouse_code = MOUSE_RIGHT;
4411 WantQueryMouse = 1;
4412 break;
4413 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
4414 break;
4415 case 8: return -1; /* fourth button down */
4416 case 9: return -1; /* fourth button up */
4417 case 10: return -1; /* mouse outside of filter rectangle */
4418 default: return -1; /* should never occur */
4419 }
4420
4421 mouse_col = Pc - 1;
4422 mouse_row = Pr - 1;
4423
4424 slen += (int)(p - (tp + slen));
4425 }
4426# endif /* FEAT_MOUSE_DEC */
4427# ifdef FEAT_MOUSE_PTERM
4428 if (key_name[0] == (int)KS_PTERM_MOUSE)
4429 {
4430 int button, num_clicks, action, mc, mr;
4431
4432 p = tp + slen;
4433
4434 action = getdigits(&p);
4435 if (*p++ != ';')
4436 return -1;
4437
4438 mouse_row = getdigits(&p);
4439 if (*p++ != ';')
4440 return -1;
4441 mouse_col = getdigits(&p);
4442 if (*p++ != ';')
4443 return -1;
4444
4445 button = getdigits(&p);
4446 mouse_code = 0;
4447
4448 switch( button )
4449 {
4450 case 4: mouse_code = MOUSE_LEFT; break;
4451 case 1: mouse_code = MOUSE_RIGHT; break;
4452 case 2: mouse_code = MOUSE_MIDDLE; break;
4453 default: return -1;
4454 }
4455
4456 switch( action )
4457 {
4458 case 31: /* Initial press */
4459 if (*p++ != ';')
4460 return -1;
4461
4462 num_clicks = getdigits(&p); /* Not used */
4463 break;
4464
4465 case 32: /* Release */
4466 mouse_code |= MOUSE_RELEASE;
4467 break;
4468
4469 case 33: /* Drag */
4470 held_button = mouse_code;
4471 mouse_code |= MOUSE_DRAG;
4472 break;
4473
4474 default:
4475 return -1;
4476 }
4477
4478 if (*p++ != 't')
4479 return -1;
4480
4481 slen += (p - (tp + slen));
4482 }
4483# endif /* FEAT_MOUSE_PTERM */
4484
4485 /* Interpret the mouse code */
4486 current_button = (mouse_code & MOUSE_CLICK_MASK);
4487 if (current_button == MOUSE_RELEASE
4488# ifdef FEAT_MOUSE_XTERM
4489 && wheel_code == 0
4490# endif
4491 )
4492 {
4493 /*
4494 * If we get a mouse drag or release event when
4495 * there is no mouse button held down (held_button ==
4496 * MOUSE_RELEASE), produce a K_IGNORE below.
4497 * (can happen when you hold down two buttons
4498 * and then let them go, or click in the menu bar, but not
4499 * on a menu, and drag into the text).
4500 */
4501 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
4502 is_drag = TRUE;
4503 current_button = held_button;
4504 }
4505 else if (wheel_code == 0)
4506 {
4507# ifdef CHECK_DOUBLE_CLICK
4508# ifdef FEAT_MOUSE_GPM
4509# ifdef FEAT_GUI
4510 /*
4511 * Only for Unix, when GUI or gpm is not active, we handle
4512 * multi-clicks here.
4513 */
4514 if (gpm_flag == 0 && !gui.in_use)
4515# else
4516 if (gpm_flag == 0)
4517# endif
4518# else
4519# ifdef FEAT_GUI
4520 if (!gui.in_use)
4521# endif
4522# endif
4523 {
4524 /*
4525 * Compute the time elapsed since the previous mouse click.
4526 */
4527 gettimeofday(&mouse_time, NULL);
4528 timediff = (mouse_time.tv_usec
4529 - orig_mouse_time.tv_usec) / 1000;
4530 if (timediff < 0)
4531 --orig_mouse_time.tv_sec;
4532 timediff += (mouse_time.tv_sec
4533 - orig_mouse_time.tv_sec) * 1000;
4534 orig_mouse_time = mouse_time;
4535 if (mouse_code == orig_mouse_code
4536 && timediff < p_mouset
4537 && orig_num_clicks != 4
4538 && orig_mouse_col == mouse_col
4539 && orig_mouse_row == mouse_row
4540#ifdef FEAT_DIFF
4541 && orig_topfill == curwin->w_topfill
4542#endif
4543 && orig_topline == curwin->w_topline)
4544 ++orig_num_clicks;
4545 else
4546 orig_num_clicks = 1;
4547 orig_mouse_col = mouse_col;
4548 orig_mouse_row = mouse_row;
4549 orig_topline = curwin->w_topline;
4550#ifdef FEAT_DIFF
4551 orig_topfill = curwin->w_topfill;
4552#endif
4553 }
4554# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
4555 else
4556 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
4557# endif
4558# else
4559 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
4560# endif
4561 is_click = TRUE;
4562 orig_mouse_code = mouse_code;
4563 }
4564 if (!is_drag)
4565 held_button = mouse_code & MOUSE_CLICK_MASK;
4566
4567 /*
4568 * Translate the actual mouse event into a pseudo mouse event.
4569 * First work out what modifiers are to be used.
4570 */
4571 modifiers = 0x0;
4572 if (orig_mouse_code & MOUSE_SHIFT)
4573 modifiers |= MOD_MASK_SHIFT;
4574 if (orig_mouse_code & MOUSE_CTRL)
4575 modifiers |= MOD_MASK_CTRL;
4576 if (orig_mouse_code & MOUSE_ALT)
4577 modifiers |= MOD_MASK_ALT;
4578 if (orig_num_clicks == 2)
4579 modifiers |= MOD_MASK_2CLICK;
4580 else if (orig_num_clicks == 3)
4581 modifiers |= MOD_MASK_3CLICK;
4582 else if (orig_num_clicks == 4)
4583 modifiers |= MOD_MASK_4CLICK;
4584
4585 /* Add the modifier codes to our string */
4586 if (modifiers != 0)
4587 {
4588 string[new_slen++] = K_SPECIAL;
4589 string[new_slen++] = (int)KS_MODIFIER;
4590 string[new_slen++] = modifiers;
4591 }
4592
4593 /* Work out our pseudo mouse event */
4594 key_name[0] = (int)KS_EXTRA;
4595 if (wheel_code != 0)
4596 key_name[1] = (wheel_code & 1)
4597 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
4598 else
4599 key_name[1] = get_pseudo_mouse_code(current_button,
4600 is_click, is_drag);
4601 }
4602#endif /* FEAT_MOUSE */
4603
4604#ifdef FEAT_GUI
4605 /*
4606 * If using the GUI, then we get menu and scrollbar events.
4607 *
4608 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
4609 * four bytes which are to be taken as a pointer to the vimmenu_T
4610 * structure.
4611 *
4612 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
4613 * by one byte representing the scrollbar number, and then four bytes
4614 * representing a long_u which is the new value of the scrollbar.
4615 *
4616 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
4617 * KE_FILLER followed by four bytes representing a long_u which is the
4618 * new value of the scrollbar.
4619 */
4620# ifdef FEAT_MENU
4621 else if (key_name[0] == (int)KS_MENU)
4622 {
4623 long_u val;
4624
4625 num_bytes = get_long_from_buf(tp + slen, &val);
4626 if (num_bytes == -1)
4627 return -1;
4628 current_menu = (vimmenu_T *)val;
4629 slen += num_bytes;
4630 }
4631# endif
4632# ifndef USE_ON_FLY_SCROLL
4633 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
4634 {
4635 long_u val;
4636
4637 /* Get the last scrollbar event in the queue of the same type */
4638 j = 0;
4639 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
4640 && tp[j + 2] != NUL; ++i)
4641 {
4642 j += 3;
4643 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
4644 if (num_bytes == -1)
4645 break;
4646 if (i == 0)
4647 current_scrollbar = (int)bytes[0];
4648 else if (current_scrollbar != (int)bytes[0])
4649 break;
4650 j += num_bytes;
4651 num_bytes = get_long_from_buf(tp + j, &val);
4652 if (num_bytes == -1)
4653 break;
4654 scrollbar_value = val;
4655 j += num_bytes;
4656 slen = j;
4657 }
4658 if (i == 0) /* not enough characters to make one */
4659 return -1;
4660 }
4661 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
4662 {
4663 long_u val;
4664
4665 /* Get the last horiz. scrollbar event in the queue */
4666 j = 0;
4667 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
4668 && tp[j + 2] != NUL; ++i)
4669 {
4670 j += 3;
4671 num_bytes = get_long_from_buf(tp + j, &val);
4672 if (num_bytes == -1)
4673 break;
4674 scrollbar_value = val;
4675 j += num_bytes;
4676 slen = j;
4677 }
4678 if (i == 0) /* not enough characters to make one */
4679 return -1;
4680 }
4681# endif /* !USE_ON_FLY_SCROLL */
4682#endif /* FEAT_GUI */
4683
4684 /* Finally, add the special key code to our string */
4685 if (key_name[0] == KS_KEY)
4686 string[new_slen++] = key_name[1]; /* from ":set <M-b>=xx" */
4687 else
4688 {
4689 string[new_slen++] = K_SPECIAL;
4690 string[new_slen++] = key_name[0];
4691 string[new_slen++] = key_name[1];
4692 }
4693 string[new_slen] = NUL;
4694 extra = new_slen - slen;
4695 if (buf == NULL)
4696 {
4697 if (extra < 0)
4698 /* remove matched chars, taking care of noremap */
4699 del_typebuf(-extra, offset);
4700 else if (extra > 0)
4701 /* insert the extra space we need */
4702 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
4703
4704 /*
4705 * Careful: del_typebuf() and ins_typebuf() may have reallocated
4706 * typebuf.tb_buf[]!
4707 */
4708 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4709 (size_t)new_slen);
4710 }
4711 else
4712 {
4713 if (extra < 0)
4714 /* remove matched characters */
4715 mch_memmove(buf + offset, buf + offset - extra,
4716 (size_t)(buflen + offset + extra));
4717 else if (extra > 0)
4718 /* insert the extra space we need */
4719 mch_memmove(buf + offset + extra, buf + offset,
4720 (size_t)(buflen - offset));
4721 mch_memmove(buf + offset, string, (size_t)new_slen);
4722 }
4723 return (len + extra + offset);
4724 }
4725
4726 return 0; /* no match found */
4727}
4728
4729/*
4730 * Replace any terminal code strings in from[] with the equivalent internal
4731 * vim representation. This is used for the "from" and "to" part of a
4732 * mapping, and the "to" part of a menu command.
4733 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
4734 * '<'.
4735 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
4736 *
4737 * The replacement is done in result[] and finally copied into allocated
4738 * memory. If this all works well *bufp is set to the allocated memory and a
4739 * pointer to it is returned. If something fails *bufp is set to NULL and from
4740 * is returned.
4741 *
4742 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
4743 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
4744 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
4745 * instead of a CTRL-V.
4746 */
4747 char_u *
4748replace_termcodes(from, bufp, from_part, do_lt)
4749 char_u *from;
4750 char_u **bufp;
4751 int from_part;
4752 int do_lt; /* also translate <lt> */
4753{
4754 int i;
4755 int slen;
4756 int key;
4757 int dlen = 0;
4758 char_u *src;
4759 int do_backslash; /* backslash is a special character */
4760 int do_special; /* recognize <> key codes */
4761 int do_key_code; /* recognize raw key codes */
4762 char_u *result; /* buffer for resulting string */
4763
4764 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
4765 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL);
4766 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
4767
4768 /*
4769 * Allocate space for the translation. Worst case a single character is
4770 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
4771 */
4772 result = alloc((unsigned)STRLEN(from) * 6 + 1);
4773 if (result == NULL) /* out of memory */
4774 {
4775 *bufp = NULL;
4776 return from;
4777 }
4778
4779 src = from;
4780
4781 /*
4782 * Check for #n at start only: function key n
4783 */
4784 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
4785 {
4786 result[dlen++] = K_SPECIAL;
4787 result[dlen++] = 'k';
4788 if (src[1] == '0')
4789 result[dlen++] = ';'; /* #0 is F10 is "k;" */
4790 else
4791 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
4792 src += 2;
4793 }
4794
4795 /*
4796 * Copy each byte from *from to result[dlen]
4797 */
4798 while (*src != NUL)
4799 {
4800 /*
4801 * If 'cpoptions' does not contain '<', check for special key codes,
4802 * like "<C-S-MouseLeft>"
4803 */
4804 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
4805 {
4806#ifdef FEAT_EVAL
4807 /*
4808 * Replace <SID> by K_SNR <script-nr> _.
4809 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
4810 */
4811 if (STRNICMP(src, "<SID>", 5) == 0)
4812 {
4813 if (current_SID <= 0)
4814 EMSG(_(e_usingsid));
4815 else
4816 {
4817 src += 5;
4818 result[dlen++] = K_SPECIAL;
4819 result[dlen++] = (int)KS_EXTRA;
4820 result[dlen++] = (int)KE_SNR;
4821 sprintf((char *)result + dlen, "%ld", (long)current_SID);
4822 dlen += (int)STRLEN(result + dlen);
4823 result[dlen++] = '_';
4824 continue;
4825 }
4826 }
4827#endif
4828
4829 slen = trans_special(&src, result + dlen, TRUE);
4830 if (slen)
4831 {
4832 dlen += slen;
4833 continue;
4834 }
4835 }
4836
4837 /*
4838 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
4839 * Note that this is also checked after replacing the <> form.
4840 * Single character codes are NOT replaced (e.g. ^H or DEL), because
4841 * it could be a character in the file.
4842 */
4843 if (do_key_code)
4844 {
4845 i = find_term_bykeys(src);
4846 if (i >= 0)
4847 {
4848 result[dlen++] = K_SPECIAL;
4849 result[dlen++] = termcodes[i].name[0];
4850 result[dlen++] = termcodes[i].name[1];
4851 src += termcodes[i].len;
4852 /* If terminal code matched, continue after it. */
4853 continue;
4854 }
4855 }
4856
4857#ifdef FEAT_EVAL
4858 if (do_special)
4859 {
4860 char_u *p, *s, len;
4861
4862 /*
4863 * Replace <Leader> by the value of "mapleader".
4864 * Replace <LocalLeader> by the value of "maplocalleader".
4865 * If "mapleader" or "maplocalleader" isn't set use a backslash.
4866 */
4867 if (STRNICMP(src, "<Leader>", 8) == 0)
4868 {
4869 len = 8;
4870 p = get_var_value((char_u *)"g:mapleader");
4871 }
4872 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
4873 {
4874 len = 13;
4875 p = get_var_value((char_u *)"g:maplocalleader");
4876 }
4877 else
4878 {
4879 len = 0;
4880 p = NULL;
4881 }
4882 if (len != 0)
4883 {
4884 /* Allow up to 8 * 6 characters for "mapleader". */
4885 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
4886 s = (char_u *)"\\";
4887 else
4888 s = p;
4889 while (*s != NUL)
4890 result[dlen++] = *s++;
4891 src += len;
4892 continue;
4893 }
4894 }
4895#endif
4896
4897 /*
4898 * Remove CTRL-V and ignore the next character.
4899 * For "from" side the CTRL-V at the end is included, for the "to"
4900 * part it is removed.
4901 * If 'cpoptions' does not contain 'B', also accept a backslash.
4902 */
4903 key = *src;
4904 if (key == Ctrl_V || (do_backslash && key == '\\'))
4905 {
4906 ++src; /* skip CTRL-V or backslash */
4907 if (*src == NUL)
4908 {
4909 if (from_part)
4910 result[dlen++] = key;
4911 break;
4912 }
4913 }
4914
4915#ifdef FEAT_MBYTE
4916 /* skip multibyte char correctly */
4917 for (i = (*mb_ptr2len_check)(src); i > 0; --i)
4918#endif
4919 {
4920 /*
4921 * If the character is K_SPECIAL, replace it with K_SPECIAL
4922 * KS_SPECIAL KE_FILLER.
4923 * If compiled with the GUI replace CSI with K_CSI.
4924 */
4925 if (*src == K_SPECIAL)
4926 {
4927 result[dlen++] = K_SPECIAL;
4928 result[dlen++] = KS_SPECIAL;
4929 result[dlen++] = KE_FILLER;
4930 }
4931# ifdef FEAT_GUI
4932 else if (*src == CSI)
4933 {
4934 result[dlen++] = K_SPECIAL;
4935 result[dlen++] = KS_EXTRA;
4936 result[dlen++] = (int)KE_CSI;
4937 }
4938# endif
4939 else
4940 result[dlen++] = *src;
4941 ++src;
4942 }
4943 }
4944 result[dlen] = NUL;
4945
4946 /*
4947 * Copy the new string to allocated memory.
4948 * If this fails, just return from.
4949 */
4950 if ((*bufp = vim_strsave(result)) != NULL)
4951 from = *bufp;
4952 vim_free(result);
4953 return from;
4954}
4955
4956/*
4957 * Find a termcode with keys 'src' (must be NUL terminated).
4958 * Return the index in termcodes[], or -1 if not found.
4959 */
4960 int
4961find_term_bykeys(src)
4962 char_u *src;
4963{
4964 int i;
4965 int slen;
4966
4967 for (i = 0; i < tc_len; ++i)
4968 {
4969 slen = termcodes[i].len;
4970 if (slen > 1 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
4971 return i;
4972 }
4973 return -1;
4974}
4975
4976/*
4977 * Gather the first characters in the terminal key codes into a string.
4978 * Used to speed up check_termcode().
4979 */
4980 static void
4981gather_termleader()
4982{
4983 int i;
4984 int len = 0;
4985
4986#ifdef FEAT_GUI
4987 if (gui.in_use)
4988 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
4989#endif
4990#ifdef FEAT_TERMRESPONSE
4991 if (check_for_codes)
4992 termleader[len++] = DCS; /* the termcode response starts with DCS
4993 in 8-bit mode */
4994#endif
4995 termleader[len] = NUL;
4996
4997 for (i = 0; i < tc_len; ++i)
4998 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
4999 {
5000 termleader[len++] = termcodes[i].code[0];
5001 termleader[len] = NUL;
5002 }
5003
5004 need_gather = FALSE;
5005}
5006
5007/*
5008 * Show all termcodes (for ":set termcap")
5009 * This code looks a lot like showoptions(), but is different.
5010 */
5011 void
5012show_termcodes()
5013{
5014 int col;
5015 int *items;
5016 int item_count;
5017 int run;
5018 int row, rows;
5019 int cols;
5020 int i;
5021 int len;
5022
5023#define INC 27 /* try to make three columns */
5024#define GAP 2 /* spaces between columns */
5025
5026 if (tc_len == 0) /* no terminal codes (must be GUI) */
5027 return;
5028 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5029 if (items == NULL)
5030 return;
5031
5032 /* Highlight title */
5033 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5034
5035 /*
5036 * do the loop two times:
5037 * 1. display the short items (non-strings and short strings)
5038 * 2. display the long items (strings)
5039 */
5040 for (run = 1; run <= 2 && !got_int; ++run)
5041 {
5042 /*
5043 * collect the items in items[]
5044 */
5045 item_count = 0;
5046 for (i = 0; i < tc_len; i++)
5047 {
5048 len = show_one_termcode(termcodes[i].name,
5049 termcodes[i].code, FALSE);
5050 if ((len <= INC - GAP && run == 1) || (len > INC - GAP && run == 2))
5051 items[item_count++] = i;
5052 }
5053
5054 /*
5055 * display the items
5056 */
5057 if (run == 1)
5058 {
5059 cols = (Columns + GAP) / INC;
5060 if (cols == 0)
5061 cols = 1;
5062 rows = (item_count + cols - 1) / cols;
5063 }
5064 else /* run == 2 */
5065 rows = item_count;
5066 for (row = 0; row < rows && !got_int; ++row)
5067 {
5068 msg_putchar('\n'); /* go to next line */
5069 if (got_int) /* 'q' typed in more */
5070 break;
5071 col = 0;
5072 for (i = row; i < item_count; i += rows)
5073 {
5074 msg_col = col; /* make columns */
5075 show_one_termcode(termcodes[items[i]].name,
5076 termcodes[items[i]].code, TRUE);
5077 col += INC;
5078 }
5079 out_flush();
5080 ui_breakcheck();
5081 }
5082 }
5083 vim_free(items);
5084}
5085
5086/*
5087 * Show one termcode entry.
5088 * Output goes into IObuff[]
5089 */
5090 int
5091show_one_termcode(name, code, printit)
5092 char_u *name;
5093 char_u *code;
5094 int printit;
5095{
5096 char_u *p;
5097 int len;
5098
5099 if (name[0] > '~')
5100 {
5101 IObuff[0] = ' ';
5102 IObuff[1] = ' ';
5103 IObuff[2] = ' ';
5104 IObuff[3] = ' ';
5105 }
5106 else
5107 {
5108 IObuff[0] = 't';
5109 IObuff[1] = '_';
5110 IObuff[2] = name[0];
5111 IObuff[3] = name[1];
5112 }
5113 IObuff[4] = ' ';
5114
5115 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5116 if (p[1] != 't')
5117 STRCPY(IObuff + 5, p);
5118 else
5119 IObuff[5] = NUL;
5120 len = (int)STRLEN(IObuff);
5121 do
5122 IObuff[len++] = ' ';
5123 while (len < 17);
5124 IObuff[len] = NUL;
5125 if (code == NULL)
5126 len += 4;
5127 else
5128 len += vim_strsize(code);
5129
5130 if (printit)
5131 {
5132 msg_puts(IObuff);
5133 if (code == NULL)
5134 msg_puts((char_u *)"NULL");
5135 else
5136 msg_outtrans(code);
5137 }
5138 return len;
5139}
5140
5141#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5142/*
5143 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5144 * termcap codes from the terminal itself.
5145 * We get them one by one to avoid a very long response string.
5146 */
5147static int xt_index_in = 0;
5148static int xt_index_out = 0;
5149
5150 static void
5151req_codes_from_term()
5152{
5153 xt_index_out = 0;
5154 xt_index_in = 0;
5155 req_more_codes_from_term();
5156}
5157
5158 static void
5159req_more_codes_from_term()
5160{
5161 char buf[11];
5162 int old_idx = xt_index_out;
5163
5164 /* Don't do anything when going to exit. */
5165 if (exiting)
5166 return;
5167
5168 /* Send up to 10 more requests out than we received. Avoid sending too
5169 * many, there can be a buffer overflow somewhere. */
5170 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5171 {
5172 sprintf(buf, "\033P+q%02x%02x\033\\",
5173 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5174 out_str_nf((char_u *)buf);
5175 ++xt_index_out;
5176 }
5177
5178 /* Send the codes out right away. */
5179 if (xt_index_out != old_idx)
5180 out_flush();
5181}
5182
5183/*
5184 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5185 * A "0" instead of the "1" indicates a code that isn't supported.
5186 * Both <name> and <string> are encoded in hex.
5187 * "code" points to the "0" or "1".
5188 */
5189 static void
5190got_code_from_term(code, len)
5191 char_u *code;
5192 int len;
5193{
5194#define XT_LEN 100
5195 char_u name[3];
5196 char_u str[XT_LEN];
5197 int i;
5198 int j = 0;
5199 int c;
5200
5201 /* A '1' means the code is supported, a '0' means it isn't.
5202 * When half the length is > XT_LEN we can't use it.
5203 * Our names are currently all 2 characters. */
5204 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5205 {
5206 /* Get the name from the response and find it in the table. */
5207 name[0] = hexhex2nr(code + 3);
5208 name[1] = hexhex2nr(code + 5);
5209 name[2] = NUL;
5210 for (i = 0; key_names[i] != NULL; ++i)
5211 {
5212 if (STRCMP(key_names[i], name) == 0)
5213 {
5214 xt_index_in = i;
5215 break;
5216 }
5217 }
5218 if (key_names[i] != NULL)
5219 {
5220 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5221 str[j++] = c;
5222 str[j] = NUL;
5223 if (name[0] == 'C' && name[1] == 'o')
5224 {
5225 /* Color count is not a key code. */
5226 i = atoi((char *)str);
5227 if (i != t_colors)
5228 {
5229 /* Nr of colors changed, initialize highlighting and
5230 * redraw everything. */
5231 set_color_count(i);
5232 init_highlight(TRUE, FALSE);
5233 redraw_later(CLEAR);
5234 }
5235 }
5236 else
5237 {
5238 /* First delete any existing entry with the same code. */
5239 i = find_term_bykeys(str);
5240 if (i >= 0)
5241 del_termcode_idx(i);
5242 add_termcode(name, str, FALSE);
5243 }
5244 }
5245 }
5246
5247 /* May request more codes now that we received one. */
5248 ++xt_index_in;
5249 req_more_codes_from_term();
5250}
5251
5252/*
5253 * Check if there are any unanswered requests and deal with them.
5254 * This is called before starting an external program or getting direct
5255 * keyboard input. We don't want responses to be send to that program or
5256 * handled as typed text.
5257 */
5258 static void
5259check_for_codes_from_term()
5260{
5261 int c;
5262
5263 /* If no codes requested or all are answered, no need to wait. */
5264 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5265 return;
5266
5267 /* Vgetc() will check for and handle any response.
5268 * Keep calling vpeekc() until we don't get any responses. */
5269 ++no_mapping;
5270 ++allow_keys;
5271 for (;;)
5272 {
5273 c = vpeekc();
5274 if (c == NUL) /* nothing available */
5275 break;
5276
5277 /* If a response is recognized it's replaced with K_IGNORE, must read
5278 * it from the input stream. If there is no K_IGNORE we can't do
5279 * anything, break here (there might be some responses further on, but
5280 * we don't want to throw away any typed chars). */
5281 if (c != K_SPECIAL && c != K_IGNORE)
5282 break;
5283 c = vgetc();
5284 if (c != K_IGNORE)
5285 {
5286 vungetc(c);
5287 break;
5288 }
5289 }
5290 --no_mapping;
5291 --allow_keys;
5292}
5293#endif
5294
5295#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5296/*
5297 * Translate an internal mapping/abbreviation representation into the
5298 * corresponding external one recognized by :map/:abbrev commands;
5299 * respects the current B/k/< settings of 'cpoption'.
5300 *
5301 * This function is called when expanding mappings/abbreviations on the
5302 * command-line, and for building the "Ambiguous mapping..." error messæge.
5303 *
5304 * It uses a growarray to build the translation string since the
5305 * latter can be wider than the original description. The caller has to
5306 * free the string afterwards.
5307 *
5308 * Returns NULL when there is a problem.
5309 */
5310 char_u *
5311translate_mapping(str, expmap)
5312 char_u *str;
5313 int expmap; /* TRUE when expanding mappings on command-line */
5314{
5315 garray_T ga;
5316 int c;
5317 int modifiers;
5318 int cpo_bslash;
5319 int cpo_special;
5320 int cpo_keycode;
5321
5322 ga_init(&ga);
5323 ga.ga_itemsize = 1;
5324 ga.ga_growsize = 40;
5325
5326 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
5327 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
5328 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5329
5330 for (; *str; ++str)
5331 {
5332 c = *str;
5333 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5334 {
5335 modifiers = 0;
5336 if (str[1] == KS_MODIFIER)
5337 {
5338 str++;
5339 modifiers = *++str;
5340 c = *++str;
5341 }
5342 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
5343 {
5344 int i;
5345
5346 /* try to find special key in termcodes */
5347 for (i = 0; i < tc_len; ++i)
5348 if (termcodes[i].name[0] == str[1]
5349 && termcodes[i].name[1] == str[2])
5350 break;
5351 if (i < tc_len)
5352 {
5353 ga_concat(&ga, termcodes[i].code);
5354 str += 2;
5355 continue; /* for (str) */
5356 }
5357 }
5358 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5359 {
5360 if (expmap && cpo_special)
5361 {
5362 ga_clear(&ga);
5363 return NULL;
5364 }
5365 c = TO_SPECIAL(str[1], str[2]);
5366 if (c == K_ZERO) /* display <Nul> as ^@ */
5367 c = NUL;
5368 str += 2;
5369 }
5370 if (IS_SPECIAL(c) || modifiers) /* special key */
5371 {
5372 if (expmap && cpo_special)
5373 {
5374 ga_clear(&ga);
5375 return NULL;
5376 }
5377 ga_concat(&ga, get_special_key_name(c, modifiers));
5378 continue; /* for (str) */
5379 }
5380 }
5381 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
5382 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
5383 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
5384 if (c)
5385 ga_append(&ga, c);
5386 }
5387 ga_append(&ga, NUL);
5388 return (char_u *)(ga.ga_data);
5389}
5390#endif
5391
5392#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
5393static char ksme_str[20];
5394static char ksmr_str[20];
5395static char ksmd_str[20];
5396
5397/*
5398 * For Win32 console: update termcap codes for existing console attributes.
5399 */
5400 void
5401update_tcap(attr)
5402 int attr;
5403{
5404 struct builtin_term *p;
5405
5406 p = find_builtin_term(DEFAULT_TERM);
5407 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
5408 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
5409 attr | 0x08); /* FOREGROUND_INTENSITY */
5410 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
5411 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
5412
5413 while (p->bt_string != NULL)
5414 {
5415 if (p->bt_entry == (int)KS_ME)
5416 p->bt_string = &ksme_str[0];
5417 else if (p->bt_entry == (int)KS_MR)
5418 p->bt_string = &ksmr_str[0];
5419 else if (p->bt_entry == (int)KS_MD)
5420 p->bt_string = &ksmd_str[0];
5421 ++p;
5422 }
5423}
5424#endif