blob: 12c506ab27bc195e09b641899161f46bfb02f05b [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
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001593 /* In silect mode (ex -s) we don't use the 'term' option. */
1594 if (silent_mode)
1595 return OK;
1596
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 detected_8bit = FALSE; /* reset 8-bit detection */
1598
1599 if (term_is_builtin(term))
1600 {
1601 term += 8;
1602#ifdef HAVE_TGETENT
1603 builtin_first = 1;
1604#endif
1605 }
1606
1607/*
1608 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1609 * If builtin_first is TRUE:
1610 * 0. try builtin termcap
1611 * 1. try external termcap
1612 * 2. if both fail default to a builtin terminal
1613 * If builtin_first is FALSE:
1614 * 1. try external termcap
1615 * 2. try builtin termcap, if both fail default to a builtin terminal
1616 */
1617#ifdef HAVE_TGETENT
1618 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1619 {
1620 /*
1621 * Use external termcap
1622 */
1623 if (try == 1)
1624 {
1625 char_u *p;
1626 static char_u tstrbuf[TBUFSZ];
1627 int i;
1628 char_u tbuf[TBUFSZ];
1629 char_u *tp;
1630 static struct {
1631 enum SpecialKey dest; /* index in term_strings[] */
1632 char *name; /* termcap name for string */
1633 } string_names[] =
1634 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1635 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1636 {KS_CL, "cl"}, {KS_CD, "cd"},
1637 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1638 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1639 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1640 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
1641 {KS_US, "us"}, {KS_CM, "cm"}, {KS_SR, "sr"},
1642 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1643 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1644 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1645 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1646 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1647 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1648 {KS_TS, "ts"}, {KS_FS, "fs"},
1649 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001650 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 {(enum SpecialKey)0, NULL}
1652 };
1653
1654 /*
1655 * If the external termcap does not have a matching entry, try the
1656 * builtin ones.
1657 */
1658 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1659 {
1660 tp = tstrbuf;
1661 if (!termcap_cleared)
1662 {
1663 clear_termoptions(); /* clear old options */
1664 termcap_cleared = TRUE;
1665 }
1666
1667 /* get output strings */
1668 for (i = 0; string_names[i].name != NULL; ++i)
1669 {
1670 if (term_str(string_names[i].dest) == NULL
1671 || term_str(string_names[i].dest) == empty_option)
1672 term_str(string_names[i].dest) =
1673 TGETSTR(string_names[i].name, &tp);
1674 }
1675
1676 /* tgetflag() returns 1 if the flag is present, 0 if not and
1677 * possibly -1 if the flag doesn't exist. */
1678 if ((T_MS == NULL || T_MS == empty_option)
1679 && tgetflag("ms") > 0)
1680 T_MS = (char_u *)"y";
1681 if ((T_XS == NULL || T_XS == empty_option)
1682 && tgetflag("xs") > 0)
1683 T_XS = (char_u *)"y";
1684 if ((T_DB == NULL || T_DB == empty_option)
1685 && tgetflag("db") > 0)
1686 T_DB = (char_u *)"y";
1687 if ((T_DA == NULL || T_DA == empty_option)
1688 && tgetflag("da") > 0)
1689 T_DA = (char_u *)"y";
1690 if ((T_UT == NULL || T_UT == empty_option)
1691 && tgetflag("ut") > 0)
1692 T_UT = (char_u *)"y";
1693
1694
1695 /*
1696 * get key codes
1697 */
1698 for (i = 0; key_names[i] != NULL; ++i)
1699 {
1700 if (find_termcode((char_u *)key_names[i]) == NULL)
1701 {
1702 p = TGETSTR(key_names[i], &tp);
1703 /* if cursor-left == backspace, ignore it (televideo
1704 * 925) */
1705 if (p != NULL
1706 && (*p != Ctrl_H
1707 || key_names[i][0] != 'k'
1708 || key_names[i][1] != 'l'))
1709 add_termcode((char_u *)key_names[i], p, FALSE);
1710 }
1711 }
1712
1713 if (height == 0)
1714 height = tgetnum("li");
1715 if (width == 0)
1716 width = tgetnum("co");
1717
1718 /*
1719 * Get number of colors (if not done already).
1720 */
1721 if (term_str(KS_CCO) == NULL
1722 || term_str(KS_CCO) == empty_option)
1723 set_color_count(tgetnum("Co"));
1724
1725# ifndef hpux
1726 BC = (char *)TGETSTR("bc", &tp);
1727 UP = (char *)TGETSTR("up", &tp);
1728 p = TGETSTR("pc", &tp);
1729 if (p)
1730 PC = *p;
1731# endif /* hpux */
1732 }
1733 }
1734 else /* try == 0 || try == 2 */
1735#endif /* HAVE_TGETENT */
1736 /*
1737 * Use builtin termcap
1738 */
1739 {
1740#ifdef HAVE_TGETENT
1741 /*
1742 * If builtin termcap was already used, there is no need to search
1743 * for the builtin termcap again, quit now.
1744 */
1745 if (try == 2 && builtin_first && termcap_cleared)
1746 break;
1747#endif
1748 /*
1749 * search for 'term' in builtin_termcaps[]
1750 */
1751 termp = find_builtin_term(term);
1752 if (termp->bt_string == NULL) /* did not find it */
1753 {
1754#ifdef HAVE_TGETENT
1755 /*
1756 * If try == 0, first try the external termcap. If that is not
1757 * found we'll get back here with try == 2.
1758 * If termcap_cleared is set we used the external termcap,
1759 * don't complain about not finding the term in the builtin
1760 * termcap.
1761 */
1762 if (try == 0) /* try external one */
1763 continue;
1764 if (termcap_cleared) /* found in external termcap */
1765 break;
1766#endif
1767
1768 mch_errmsg("\r\n");
1769 if (error_msg != NULL)
1770 {
1771 mch_errmsg((char *)error_msg);
1772 mch_errmsg("\r\n");
1773 }
1774 mch_errmsg("'");
1775 mch_errmsg((char *)term);
1776 mch_errmsg(_("' not known. Available builtin terminals are:"));
1777 mch_errmsg("\r\n");
1778 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1779 ++termp)
1780 {
1781 if (termp->bt_entry == (int)KS_NAME)
1782 {
1783#ifdef HAVE_TGETENT
1784 mch_errmsg(" builtin_");
1785#else
1786 mch_errmsg(" ");
1787#endif
1788 mch_errmsg(termp->bt_string);
1789 mch_errmsg("\r\n");
1790 }
1791 }
1792 /* when user typed :set term=xxx, quit here */
1793 if (starting != NO_SCREEN)
1794 {
1795 screen_start(); /* don't know where cursor is now */
1796 wait_return(TRUE);
1797 return FAIL;
1798 }
1799 term = DEFAULT_TERM;
1800 mch_errmsg(_("defaulting to '"));
1801 mch_errmsg((char *)term);
1802 mch_errmsg("'\r\n");
1803 if (emsg_silent == 0)
1804 {
1805 screen_start(); /* don't know where cursor is now */
1806 out_flush();
1807 ui_delay(2000L, TRUE);
1808 }
1809 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE);
1810 display_errors();
1811 }
1812 out_flush();
1813#ifdef HAVE_TGETENT
1814 if (!termcap_cleared)
1815 {
1816#endif
1817 clear_termoptions(); /* clear old options */
1818#ifdef HAVE_TGETENT
1819 termcap_cleared = TRUE;
1820 }
1821#endif
1822 parse_builtin_tcap(term);
1823#ifdef FEAT_GUI
1824 if (term_is_gui(term))
1825 {
1826 out_flush();
1827 gui_init();
1828 /* If starting the GUI failed, don't do any of the other
1829 * things for this terminal */
1830 if (!gui.in_use)
1831 return FAIL;
1832#ifdef HAVE_TGETENT
1833 break; /* don't try using external termcap */
1834#endif
1835 }
1836#endif /* FEAT_GUI */
1837 }
1838#ifdef HAVE_TGETENT
1839 }
1840#endif
1841
1842/*
1843 * special: There is no info in the termcap about whether the cursor
1844 * positioning is relative to the start of the screen or to the start of the
1845 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1846 * relative.
1847 */
1848 if (STRCMP(term, "pcterm") == 0)
1849 T_CCS = (char_u *)"yes";
1850 else
1851 T_CCS = empty_option;
1852
1853#ifdef UNIX
1854/*
1855 * Any "stty" settings override the default for t_kb from the termcap.
1856 * This is in os_unix.c, because it depends a lot on the version of unix that
1857 * is being used.
1858 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1859 */
1860#ifdef FEAT_GUI
1861 if (!gui.in_use)
1862#endif
1863 get_stty();
1864#endif
1865
1866/*
1867 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1868 * didn't work, use the default CTRL-H
1869 * The default for t_kD is DEL, unless t_kb is DEL.
1870 * The vim_strsave'd strings are probably lost forever, well it's only two
1871 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1872 * directly.
1873 */
1874#ifdef FEAT_GUI
1875 if (!gui.in_use)
1876#endif
1877 {
1878 bs_p = find_termcode((char_u *)"kb");
1879 del_p = find_termcode((char_u *)"kD");
1880 if (bs_p == NULL || *bs_p == NUL)
1881 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1882 if ((del_p == NULL || *del_p == NUL) &&
1883 (bs_p == NULL || *bs_p != DEL))
1884 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1885 }
1886
1887#if defined(UNIX) || defined(VMS)
1888 term_is_xterm = vim_is_xterm(term);
1889#endif
1890
1891#ifdef FEAT_MOUSE
1892# if defined(UNIX) || defined(VMS)
1893# ifdef FEAT_MOUSE_TTY
1894 /*
1895 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1896 * The termcode for the mouse is added as a side effect in option.c.
1897 */
1898 {
1899 char_u *p;
1900
1901 p = (char_u *)"";
1902# ifdef FEAT_MOUSE_XTERM
1903# ifdef FEAT_CLIPBOARD
1904# ifdef FEAT_GUI
1905 if (!gui.in_use)
1906# endif
1907 clip_init(FALSE);
1908# endif
1909 if (term_is_xterm)
1910 {
1911 if (use_xterm_mouse())
1912 p = NULL; /* keep existing value, might be "xterm2" */
1913 else
1914 p = (char_u *)"xterm";
1915 }
1916# endif
1917 if (p != NULL)
1918 set_option_value((char_u *)"ttym", 0L, p, 0);
1919 if (p == NULL
1920# ifdef FEAT_GUI
1921 || gui.in_use
1922# endif
1923 )
1924 check_mouse_termcode(); /* set mouse termcode anyway */
1925 }
1926# endif
1927# else
1928 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1929# endif
1930#endif /* FEAT_MOUSE */
1931
1932#ifdef FEAT_SNIFF
1933 {
1934 char_u name[2];
1935
1936 name[0] = (int)KS_EXTRA;
1937 name[1] = (int)KE_SNIFF;
1938 add_termcode(name, (char_u *)"\233sniff", FALSE);
1939 }
1940#endif
1941
1942#ifdef USE_TERM_CONSOLE
1943 /* DEFAULT_TERM indicates that it is the machine console. */
1944 if (STRCMP(term, DEFAULT_TERM) != 0)
1945 term_console = FALSE;
1946 else
1947 {
1948 term_console = TRUE;
1949# ifdef AMIGA
1950 win_resize_on(); /* enable window resizing reports */
1951# endif
1952 }
1953#endif
1954
1955#if defined(UNIX) || defined(VMS)
1956 /*
1957 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1958 */
1959 if (vim_is_fastterm(term))
1960 p_tf = TRUE;
1961#endif
1962#ifdef USE_TERM_CONSOLE
1963 /*
1964 * 'ttyfast' is default on consoles
1965 */
1966 if (term_console)
1967 p_tf = TRUE;
1968#endif
1969
1970 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1971
1972 full_screen = TRUE; /* we can use termcap codes from now on */
1973 set_term_defaults(); /* use current values as defaults */
1974#ifdef FEAT_TERMRESPONSE
1975 crv_status = CRV_GET; /* Get terminal version later */
1976#endif
1977
1978 /*
1979 * Initialize the terminal with the appropriate termcap codes.
1980 * Set the mouse and window title if possible.
1981 * Don't do this when starting, need to parse the .vimrc first, because it
1982 * may redefine t_TI etc.
1983 */
1984 if (starting != NO_SCREEN)
1985 {
1986 starttermcap(); /* may change terminal mode */
1987#ifdef FEAT_MOUSE
1988 setmouse(); /* may start using the mouse */
1989#endif
1990#ifdef FEAT_TITLE
1991 maketitle(); /* may display window title */
1992#endif
1993 }
1994
1995 /* display initial screen after ttest() checking. jw. */
1996 if (width <= 0 || height <= 0)
1997 {
1998 /* termcap failed to report size */
1999 /* set defaults, in case ui_get_shellsize() also fails */
2000 width = 80;
2001#if defined(MSDOS) || defined(WIN3264)
2002 height = 25; /* console is often 25 lines */
2003#else
2004 height = 24; /* most terminals are 24 lines */
2005#endif
2006 }
2007 set_shellsize(width, height, FALSE); /* may change Rows */
2008 if (starting != NO_SCREEN)
2009 {
2010 if (scroll_region)
2011 scroll_region_reset(); /* In case Rows changed */
2012 check_map_keycodes(); /* check mappings for terminal codes used */
2013
2014#ifdef FEAT_AUTOCMD
2015 {
2016 buf_T *old_curbuf;
2017
2018 /*
2019 * Execute the TermChanged autocommands for each buffer that is
2020 * loaded.
2021 */
2022 old_curbuf = curbuf;
2023 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
2024 {
2025 if (curbuf->b_ml.ml_mfp != NULL)
2026 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2027 curbuf);
2028 }
2029 if (buf_valid(old_curbuf))
2030 curbuf = old_curbuf;
2031 }
2032#endif
2033 }
2034
2035#ifdef FEAT_TERMRESPONSE
2036 may_req_termresponse();
2037#endif
2038
2039 return OK;
2040}
2041
2042#if defined(FEAT_MOUSE) || defined(PROTO)
2043
2044# ifdef FEAT_MOUSE_TTY
2045# define HMT_NORMAL 1
2046# define HMT_NETTERM 2
2047# define HMT_DEC 4
2048# define HMT_JSBTERM 8
2049# define HMT_PTERM 16
2050static int has_mouse_termcode = 0;
2051# endif
2052
2053# if (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_MOUSE_NET) \
2054 || defined(FEAT_MOUSE_DEC)) || defined(FEAT_MOUSE_JSB) \
2055 || defined(FEAT_MOUSE_PTERM) || defined(PROTO)
2056 void
2057set_mouse_termcode(n, s)
2058 int n; /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2059 char_u *s;
2060{
2061 char_u name[2];
2062
2063 name[0] = n;
2064 name[1] = KE_FILLER;
2065 add_termcode(name, s, FALSE);
2066# ifdef FEAT_MOUSE_TTY
2067# ifdef FEAT_MOUSE_JSB
2068 if (n == KS_JSBTERM_MOUSE)
2069 has_mouse_termcode |= HMT_JSBTERM;
2070 else
2071# endif
2072# ifdef FEAT_MOUSE_NET
2073 if (n == KS_NETTERM_MOUSE)
2074 has_mouse_termcode |= HMT_NETTERM;
2075 else
2076# endif
2077# ifdef FEAT_MOUSE_DEC
2078 if (n == KS_DEC_MOUSE)
2079 has_mouse_termcode |= HMT_DEC;
2080 else
2081# endif
2082# ifdef FEAT_MOUSE_PTERM
2083 if (n == KS_PTERM_MOUSE)
2084 has_mouse_termcode |= HMT_PTERM;
2085 else
2086# endif
2087 has_mouse_termcode |= HMT_NORMAL;
2088# endif
2089}
2090# endif
2091
2092# if ((defined(UNIX) || defined(VMS) || defined(OS2)) \
2093 && (defined(FEAT_MOUSE_XTERM) || defined(FEAT_MOUSE_DEC) \
2094 || defined(FEAT_MOUSE_GPM) || defined(FEAT_MOUSE_PTERM))) \
2095 || defined(PROTO)
2096 void
2097del_mouse_termcode(n)
2098 int n; /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2099{
2100 char_u name[2];
2101
2102 name[0] = n;
2103 name[1] = KE_FILLER;
2104 del_termcode(name);
2105# ifdef FEAT_MOUSE_TTY
2106# ifdef FEAT_MOUSE_JSB
2107 if (n == KS_JSBTERM_MOUSE)
2108 has_mouse_termcode &= ~HMT_JSBTERM;
2109 else
2110# endif
2111# ifdef FEAT_MOUSE_NET
2112 if (n == KS_NETTERM_MOUSE)
2113 has_mouse_termcode &= ~HMT_NETTERM;
2114 else
2115# endif
2116# ifdef FEAT_MOUSE_DEC
2117 if (n == KS_DEC_MOUSE)
2118 has_mouse_termcode &= ~HMT_DEC;
2119 else
2120# endif
2121# ifdef FEAT_MOUSE_PTERM
2122 if (n == KS_PTERM_MOUSE)
2123 has_mouse_termcode &= ~HMT_PTERM;
2124 else
2125# endif
2126 has_mouse_termcode &= ~HMT_NORMAL;
2127# endif
2128}
2129# endif
2130#endif
2131
2132#ifdef HAVE_TGETENT
2133/*
2134 * Call tgetent()
2135 * Return error message if it fails, NULL if it's OK.
2136 */
2137 static char_u *
2138tgetent_error(tbuf, term)
2139 char_u *tbuf;
2140 char_u *term;
2141{
2142 int i;
2143
2144 i = TGETENT(tbuf, term);
2145 if (i < 0 /* -1 is always an error */
2146# ifdef TGETENT_ZERO_ERR
2147 || i == 0 /* sometimes zero is also an error */
2148# endif
2149 )
2150 {
2151 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2152 * tgetent() with the always existing "dumb" entry to avoid a crash or
2153 * hang. */
2154 (void)TGETENT(tbuf, "dumb");
2155
2156 if (i < 0)
2157# ifdef TGETENT_ZERO_ERR
2158 return (char_u *)_("E557: Cannot open termcap file");
2159 if (i == 0)
2160# endif
2161#ifdef TERMINFO
2162 return (char_u *)_("E558: Terminal entry not found in terminfo");
2163#else
2164 return (char_u *)_("E559: Terminal entry not found in termcap");
2165#endif
2166 }
2167 return NULL;
2168}
2169
2170/*
2171 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2172 * Fix that here.
2173 */
2174 static char_u *
2175vim_tgetstr(s, pp)
2176 char *s;
2177 char_u **pp;
2178{
2179 char *p;
2180
2181 p = tgetstr(s, (char **)pp);
2182 if (p == (char *)-1)
2183 p = NULL;
2184 return (char_u *)p;
2185}
2186#endif /* HAVE_TGETENT */
2187
2188#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(__EMX__) || defined(VMS) || defined(MACOS_X))
2189/*
2190 * Get Columns and Rows from the termcap. Used after a window signal if the
2191 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2192 * and "li" entries never change. But on some systems this works.
2193 * Errors while getting the entries are ignored.
2194 */
2195 void
2196getlinecol(cp, rp)
2197 long *cp; /* pointer to columns */
2198 long *rp; /* pointer to rows */
2199{
2200 char_u tbuf[TBUFSZ];
2201
2202 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2203 {
2204 if (*cp == 0)
2205 *cp = tgetnum("co");
2206 if (*rp == 0)
2207 *rp = tgetnum("li");
2208 }
2209}
2210#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2211
2212/*
2213 * Get a string entry from the termcap and add it to the list of termcodes.
2214 * Used for <t_xx> special keys.
2215 * Give an error message for failure when not sourcing.
2216 * If force given, replace an existing entry.
2217 * Return FAIL if the entry was not found, OK if the entry was added.
2218 */
2219 int
2220add_termcap_entry(name, force)
2221 char_u *name;
2222 int force;
2223{
2224 char_u *term;
2225 int key;
2226 struct builtin_term *termp;
2227#ifdef HAVE_TGETENT
2228 char_u *string;
2229 int i;
2230 int builtin_first;
2231 char_u tbuf[TBUFSZ];
2232 char_u tstrbuf[TBUFSZ];
2233 char_u *tp = tstrbuf;
2234 char_u *error_msg = NULL;
2235#endif
2236
2237/*
2238 * If the GUI is running or will start in a moment, we only support the keys
2239 * that the GUI can produce.
2240 */
2241#ifdef FEAT_GUI
2242 if (gui.in_use || gui.starting)
2243 return gui_mch_haskey(name);
2244#endif
2245
2246 if (!force && find_termcode(name) != NULL) /* it's already there */
2247 return OK;
2248
2249 term = T_NAME;
2250 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2251 return FAIL;
2252
2253 if (term_is_builtin(term)) /* name starts with "builtin_" */
2254 {
2255 term += 8;
2256#ifdef HAVE_TGETENT
2257 builtin_first = TRUE;
2258#endif
2259 }
2260#ifdef HAVE_TGETENT
2261 else
2262 builtin_first = p_tbi;
2263#endif
2264
2265#ifdef HAVE_TGETENT
2266/*
2267 * We can get the entry from the builtin termcap and from the external one.
2268 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2269 * builtin termcap first.
2270 * If 'ttybuiltin' is off, try external termcap first.
2271 */
2272 for (i = 0; i < 2; ++i)
2273 {
2274 if (!builtin_first == i)
2275#endif
2276 /*
2277 * Search in builtin termcap
2278 */
2279 {
2280 termp = find_builtin_term(term);
2281 if (termp->bt_string != NULL) /* found it */
2282 {
2283 key = TERMCAP2KEY(name[0], name[1]);
2284 while (termp->bt_entry != (int)KS_NAME)
2285 {
2286 if ((int)termp->bt_entry == key)
2287 {
2288 add_termcode(name, (char_u *)termp->bt_string,
2289 term_is_8bit(term));
2290 return OK;
2291 }
2292 ++termp;
2293 }
2294 }
2295 }
2296#ifdef HAVE_TGETENT
2297 else
2298 /*
2299 * Search in external termcap
2300 */
2301 {
2302 error_msg = tgetent_error(tbuf, term);
2303 if (error_msg == NULL)
2304 {
2305 string = TGETSTR((char *)name, &tp);
2306 if (string != NULL && *string != NUL)
2307 {
2308 add_termcode(name, string, FALSE);
2309 return OK;
2310 }
2311 }
2312 }
2313 }
2314#endif
2315
2316 if (sourcing_name == NULL)
2317 {
2318#ifdef HAVE_TGETENT
2319 if (error_msg != NULL)
2320 EMSG(error_msg);
2321 else
2322#endif
2323 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2324 }
2325 return FAIL;
2326}
2327
2328 static int
2329term_is_builtin(name)
2330 char_u *name;
2331{
2332 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2333}
2334
2335/*
2336 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2337 * Assume that the terminal is using 8-bit controls when the name contains
2338 * "8bit", like in "xterm-8bit".
2339 */
2340 int
2341term_is_8bit(name)
2342 char_u *name;
2343{
2344 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2345}
2346
2347/*
2348 * Translate terminal control chars from 7-bit to 8-bit:
2349 * <Esc>[ -> CSI
2350 * <Esc>] -> <M-C-]>
2351 * <Esc>O -> <M-C-O>
2352 */
2353 static int
2354term_7to8bit(p)
2355 char_u *p;
2356{
2357 if (*p == ESC)
2358 {
2359 if (p[1] == '[')
2360 return CSI;
2361 if (p[1] == ']')
2362 return 0x9d;
2363 if (p[1] == 'O')
2364 return 0x8f;
2365 }
2366 return 0;
2367}
2368
2369#ifdef FEAT_GUI
2370 int
2371term_is_gui(name)
2372 char_u *name;
2373{
2374 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2375}
2376#endif
2377
2378#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2379
2380 char_u *
2381tltoa(i)
2382 unsigned long i;
2383{
2384 static char_u buf[16];
2385 char_u *p;
2386
2387 p = buf + 15;
2388 *p = '\0';
2389 do
2390 {
2391 --p;
2392 *p = (char_u) (i % 10 + '0');
2393 i /= 10;
2394 }
2395 while (i > 0 && p > buf);
2396 return p;
2397}
2398#endif
2399
2400#ifndef HAVE_TGETENT
2401
2402/*
2403 * minimal tgoto() implementation.
2404 * no padding and we only parse for %i %d and %+char
2405 */
2406char *tgoto __ARGS((char *, int, int));
2407
2408 char *
2409tgoto(cm, x, y)
2410 char *cm;
2411 int x, y;
2412{
2413 static char buf[30];
2414 char *p, *s, *e;
2415
2416 if (!cm)
2417 return "OOPS";
2418 e = buf + 29;
2419 for (s = buf; s < e && *cm; cm++)
2420 {
2421 if (*cm != '%')
2422 {
2423 *s++ = *cm;
2424 continue;
2425 }
2426 switch (*++cm)
2427 {
2428 case 'd':
2429 p = (char *)tltoa((unsigned long)y);
2430 y = x;
2431 while (*p)
2432 *s++ = *p++;
2433 break;
2434 case 'i':
2435 x++;
2436 y++;
2437 break;
2438 case '+':
2439 *s++ = (char)(*++cm + y);
2440 y = x;
2441 break;
2442 case '%':
2443 *s++ = *cm;
2444 break;
2445 default:
2446 return "OOPS";
2447 }
2448 }
2449 *s = '\0';
2450 return buf;
2451}
2452
2453#endif /* HAVE_TGETENT */
2454
2455/*
2456 * Set the terminal name and initialize the terminal options.
2457 * If "name" is NULL or empty, get the terminal name from the environment.
2458 * If that fails, use the default terminal name.
2459 */
2460 void
2461termcapinit(name)
2462 char_u *name;
2463{
2464 char_u *term;
2465
2466 if (name != NULL && *name == NUL)
2467 name = NULL; /* empty name is equal to no name */
2468 term = name;
2469
2470#ifdef __BEOS__
2471 /*
2472 * TERM environment variable is normally set to 'ansi' on the Bebox;
2473 * Since the BeBox doesn't quite support full ANSI yet, we use our
2474 * own custom 'ansi-beos' termcap instead, unless the -T option has
2475 * been given on the command line.
2476 */
2477 if (term == NULL
2478 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2479 term = DEFAULT_TERM;
2480#endif
2481#ifndef MSWIN
2482 if (term == NULL)
2483 term = mch_getenv((char_u *)"TERM");
2484#endif
2485 if (term == NULL || *term == NUL)
2486 term = DEFAULT_TERM;
2487 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE);
2488
2489 /* Set the default terminal name. */
2490 set_string_default("term", term);
2491 set_string_default("ttytype", term);
2492
2493 /*
2494 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2495 */
2496 set_termname(T_NAME != NULL ? T_NAME : term);
2497}
2498
2499/*
2500 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2501 */
2502#ifdef DOS16
2503# define OUT_SIZE 255 /* only have 640K total... */
2504#else
2505# ifdef FEAT_GUI_W16
2506# define OUT_SIZE 1023 /* Save precious 1K near data */
2507# else
2508# define OUT_SIZE 2047
2509# endif
2510#endif
2511 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2512static char_u out_buf[OUT_SIZE + 1];
2513static int out_pos = 0; /* number of chars in out_buf */
2514
2515/*
2516 * out_flush(): flush the output buffer
2517 */
2518 void
2519out_flush()
2520{
2521 int len;
2522
2523 if (out_pos != 0)
2524 {
2525 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2526 len = out_pos;
2527 out_pos = 0;
2528 ui_write(out_buf, len);
2529 }
2530}
2531
2532#if defined(FEAT_MBYTE) || defined(PROTO)
2533/*
2534 * Sometimes a byte out of a multi-byte character is written with out_char().
2535 * To avoid flushing half of the character, call this function first.
2536 */
2537 void
2538out_flush_check()
2539{
2540 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2541 out_flush();
2542}
2543#endif
2544
2545#ifdef FEAT_GUI
2546/*
2547 * out_trash(): Throw away the contents of the output buffer
2548 */
2549 void
2550out_trash()
2551{
2552 out_pos = 0;
2553}
2554#endif
2555
2556/*
2557 * out_char(c): put a byte into the output buffer.
2558 * Flush it if it becomes full.
2559 * This should not be used for outputting text on the screen (use functions
2560 * like msg_puts() and screen_putchar() for that).
2561 */
2562 void
2563out_char(c)
2564 unsigned c;
2565{
2566#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2567 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2568 out_char('\r');
2569#endif
2570
2571 out_buf[out_pos++] = c;
2572
2573 /* For testing we flush each time. */
2574 if (out_pos >= OUT_SIZE || p_wd)
2575 out_flush();
2576}
2577
2578static void out_char_nf __ARGS((unsigned));
2579
2580/*
2581 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2582 */
2583 static void
2584out_char_nf(c)
2585 unsigned c;
2586{
2587#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2588 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2589 out_char_nf('\r');
2590#endif
2591
2592 out_buf[out_pos++] = c;
2593
2594 if (out_pos >= OUT_SIZE)
2595 out_flush();
2596}
2597
2598/*
2599 * A never-padding out_str.
2600 * use this whenever you don't want to run the string through tputs.
2601 * tputs above is harmless, but tputs from the termcap library
2602 * is likely to strip off leading digits, that it mistakes for padding
2603 * information, and "%i", "%d", etc.
2604 * This should only be used for writing terminal codes, not for outputting
2605 * normal text (use functions like msg_puts() and screen_putchar() for that).
2606 */
2607 void
2608out_str_nf(s)
2609 char_u *s;
2610{
2611 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2612 out_flush();
2613 while (*s)
2614 out_char_nf(*s++);
2615
2616 /* For testing we write one string at a time. */
2617 if (p_wd)
2618 out_flush();
2619}
2620
2621/*
2622 * out_str(s): Put a character string a byte at a time into the output buffer.
2623 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2624 * This should only be used for writing terminal codes, not for outputting
2625 * normal text (use functions like msg_puts() and screen_putchar() for that).
2626 */
2627 void
2628out_str(s)
2629 char_u *s;
2630{
2631 if (s != NULL && *s)
2632 {
2633#ifdef FEAT_GUI
2634 /* Don't use tputs() when GUI is used, ncurses crashes. */
2635 if (gui.in_use)
2636 {
2637 out_str_nf(s);
2638 return;
2639 }
2640#endif
2641 /* avoid terminal strings being split up */
2642 if (out_pos > OUT_SIZE - 20)
2643 out_flush();
2644#ifdef HAVE_TGETENT
2645 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2646#else
2647 while (*s)
2648 out_char_nf(*s++);
2649#endif
2650
2651 /* For testing we write one string at a time. */
2652 if (p_wd)
2653 out_flush();
2654 }
2655}
2656
2657/*
2658 * cursor positioning using termcap parser. (jw)
2659 */
2660 void
2661term_windgoto(row, col)
2662 int row;
2663 int col;
2664{
2665 OUT_STR(tgoto((char *)T_CM, col, row));
2666}
2667
2668 void
2669term_cursor_right(i)
2670 int i;
2671{
2672 OUT_STR(tgoto((char *)T_CRI, 0, i));
2673}
2674
2675 void
2676term_append_lines(line_count)
2677 int line_count;
2678{
2679 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2680}
2681
2682 void
2683term_delete_lines(line_count)
2684 int line_count;
2685{
2686 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2687}
2688
2689#if defined(HAVE_TGETENT) || defined(PROTO)
2690 void
2691term_set_winpos(x, y)
2692 int x;
2693 int y;
2694{
2695 /* Can't handle a negative value here */
2696 if (x < 0)
2697 x = 0;
2698 if (y < 0)
2699 y = 0;
2700 OUT_STR(tgoto((char *)T_CWP, y, x));
2701}
2702
2703 void
2704term_set_winsize(width, height)
2705 int width;
2706 int height;
2707{
2708 OUT_STR(tgoto((char *)T_CWS, height, width));
2709}
2710#endif
2711
2712 void
2713term_fg_color(n)
2714 int n;
2715{
2716 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2717 if (*T_CAF)
2718 term_color(T_CAF, n);
2719 else if (*T_CSF)
2720 term_color(T_CSF, n);
2721}
2722
2723 void
2724term_bg_color(n)
2725 int n;
2726{
2727 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2728 if (*T_CAB)
2729 term_color(T_CAB, n);
2730 else if (*T_CSB)
2731 term_color(T_CSB, n);
2732}
2733
2734 static void
2735term_color(s, n)
2736 char_u *s;
2737 int n;
2738{
2739 char buf[20];
2740 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2741
2742 /* Special handling of 16 colors, because termcap can't handle it */
2743 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2744 /* Also accept CSI instead of <Esc>[ */
2745 if (n >= 8 && t_colors >= 16
2746 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2747 && s[i] != NUL
2748 && (STRCMP(s + i + 1, "%p1%dm") == 0
2749 || STRCMP(s + i + 1, "%dm") == 0)
2750 && (s[i] == '3' || s[i] == '4'))
2751 {
2752 sprintf(buf,
2753#ifdef TERMINFO
2754 "%s%s%%p1%%dm",
2755#else
2756 "%s%s%%dm",
2757#endif
2758 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2759 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2760 : (n >= 16 ? "48;5;" : "10"));
2761 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2762 }
2763 else
2764 OUT_STR(tgoto((char *)s, 0, n));
2765}
2766
2767#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X))) || defined(PROTO)
2768/*
2769 * Generic function to set window title, using t_ts and t_fs.
2770 */
2771 void
2772term_settitle(title)
2773 char_u *title;
2774{
2775 /* t_ts takes one argument: column in status line */
2776 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2777 out_str_nf(title);
2778 out_str(T_FS); /* set title end */
2779 out_flush();
2780}
2781#endif
2782
2783/*
2784 * Make sure we have a valid set or terminal options.
2785 * Replace all entries that are NULL by empty_option
2786 */
2787 void
2788ttest(pairs)
2789 int pairs;
2790{
2791 check_options(); /* make sure no options are NULL */
2792
2793 /*
2794 * MUST have "cm": cursor motion.
2795 */
2796 if (*T_CM == NUL)
2797 EMSG(_("E437: terminal capability \"cm\" required"));
2798
2799 /*
2800 * if "cs" defined, use a scroll region, it's faster.
2801 */
2802 if (*T_CS != NUL)
2803 scroll_region = TRUE;
2804 else
2805 scroll_region = FALSE;
2806
2807 if (pairs)
2808 {
2809 /*
2810 * optional pairs
2811 */
2812 /* TP goes to normal mode for TI (invert) and TB (bold) */
2813 if (*T_ME == NUL)
2814 T_ME = T_MR = T_MD = T_MB = empty_option;
2815 if (*T_SO == NUL || *T_SE == NUL)
2816 T_SO = T_SE = empty_option;
2817 if (*T_US == NUL || *T_UE == NUL)
2818 T_US = T_UE = empty_option;
2819 if (*T_CZH == NUL || *T_CZR == NUL)
2820 T_CZH = T_CZR = empty_option;
2821
2822 /* T_VE is needed even though T_VI is not defined */
2823 if (*T_VE == NUL)
2824 T_VI = empty_option;
2825
2826 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2827 if (*T_ME == NUL)
2828 {
2829 T_ME = T_SE;
2830 T_MR = T_SO;
2831 T_MD = T_SO;
2832 }
2833
2834 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2835 if (*T_SO == NUL)
2836 {
2837 T_SE = T_ME;
2838 if (*T_MR == NUL)
2839 T_SO = T_MD;
2840 else
2841 T_SO = T_MR;
2842 }
2843
2844 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2845 if (*T_CZH == NUL)
2846 {
2847 T_CZR = T_ME;
2848 if (*T_MR == NUL)
2849 T_CZH = T_MD;
2850 else
2851 T_CZH = T_MR;
2852 }
2853
2854 /* "Sb" and "Sf" come in pairs */
2855 if (*T_CSB == NUL || *T_CSF == NUL)
2856 {
2857 T_CSB = empty_option;
2858 T_CSF = empty_option;
2859 }
2860
2861 /* "AB" and "AF" come in pairs */
2862 if (*T_CAB == NUL || *T_CAF == NUL)
2863 {
2864 T_CAB = empty_option;
2865 T_CAF = empty_option;
2866 }
2867
2868 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2869 if (*T_CSB == NUL && *T_CAB == NUL)
2870 T_CCO = empty_option;
2871
2872 /* Set 'weirdinvert' according to value of 't_xs' */
2873 p_wiv = (*T_XS != NUL);
2874 }
2875 need_gather = TRUE;
2876
2877 /* Set t_colors to the value of t_Co. */
2878 t_colors = atoi((char *)T_CCO);
2879}
2880
2881#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2882 || defined(PROTO)
2883/*
2884 * Represent the given long_u as individual bytes, with the most significant
2885 * byte first, and store them in dst.
2886 */
2887 void
2888add_long_to_buf(val, dst)
2889 long_u val;
2890 char_u *dst;
2891{
2892 int i;
2893 int shift;
2894
2895 for (i = 1; i <= sizeof(long_u); i++)
2896 {
2897 shift = 8 * (sizeof(long_u) - i);
2898 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2899 }
2900}
2901
2902static int get_long_from_buf __ARGS((char_u *buf, long_u *val));
2903
2904/*
2905 * Interpret the next string of bytes in buf as a long integer, with the most
2906 * significant byte first. Note that it is assumed that buf has been through
2907 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
2908 * Puts result in val, and returns the number of bytes read from buf
2909 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
2910 * were present.
2911 */
2912 static int
2913get_long_from_buf(buf, val)
2914 char_u *buf;
2915 long_u *val;
2916{
2917 int len;
2918 char_u bytes[sizeof(long_u)];
2919 int i;
2920 int shift;
2921
2922 *val = 0;
2923 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
2924 if (len != -1)
2925 {
2926 for (i = 0; i < sizeof(long_u); i++)
2927 {
2928 shift = 8 * (sizeof(long_u) - 1 - i);
2929 *val += (long_u)bytes[i] << shift;
2930 }
2931 }
2932 return len;
2933}
2934#endif
2935
2936#if defined(FEAT_GUI) \
2937 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM)))
2938/*
2939 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
2940 * that buf has been through inchar(). Returns the actual number of bytes used
2941 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
2942 * available.
2943 */
2944 static int
2945get_bytes_from_buf(buf, bytes, num_bytes)
2946 char_u *buf;
2947 char_u *bytes;
2948 int num_bytes;
2949{
2950 int len = 0;
2951 int i;
2952 char_u c;
2953
2954 for (i = 0; i < num_bytes; i++)
2955 {
2956 if ((c = buf[len++]) == NUL)
2957 return -1;
2958 if (c == K_SPECIAL)
2959 {
2960 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
2961 return -1;
2962 if (buf[len++] == (int)KS_ZERO)
2963 c = NUL;
2964 ++len; /* skip KE_FILLER */
2965 /* else it should be KS_SPECIAL, and c already equals K_SPECIAL */
2966 }
2967 bytes[i] = c;
2968 }
2969 return len;
2970}
2971#endif
2972
2973/*
2974 * Check if the new shell size is valid, correct it if it's too small.
2975 */
2976 void
2977check_shellsize()
2978{
2979 if (Columns < MIN_COLUMNS)
2980 Columns = MIN_COLUMNS;
2981 if (Rows < min_rows()) /* need room for one window and command line */
2982 Rows = min_rows();
2983}
2984
Bram Moolenaar86b68352004-12-27 21:59:20 +00002985/*
2986 * Invoked just before the screen structures are going to be (re)allocated.
2987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 void
2989win_new_shellsize()
2990{
2991 static int old_Rows = 0;
2992 static int old_Columns = 0;
2993
2994 if (old_Rows != Rows || old_Columns != Columns)
2995 ui_new_shellsize();
2996 if (old_Rows != Rows)
2997 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002998 /* if 'window' uses the whole screen, keep it using that */
2999 if (p_window == old_Rows - 1)
3000 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 old_Rows = Rows;
3002 shell_new_rows(); /* update window sizes */
3003 }
3004 if (old_Columns != Columns)
3005 {
3006 old_Columns = Columns;
3007#ifdef FEAT_VERTSPLIT
3008 shell_new_columns(); /* update window sizes */
3009#endif
3010 }
3011}
3012
3013/*
3014 * Call this function when the Vim shell has been resized in any way.
3015 * Will obtain the current size and redraw (also when size didn't change).
3016 */
3017 void
3018shell_resized()
3019{
3020 set_shellsize(0, 0, FALSE);
3021}
3022
3023/*
3024 * Check if the shell size changed. Handle a resize.
3025 * When the size didn't change, nothing happens.
3026 */
3027 void
3028shell_resized_check()
3029{
3030 int old_Rows = Rows;
3031 int old_Columns = Columns;
3032
3033 (void)ui_get_shellsize();
3034 check_shellsize();
3035 if (old_Rows != Rows || old_Columns != Columns)
3036 shell_resized();
3037}
3038
3039/*
3040 * Set size of the Vim shell.
3041 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3042 * window size (this is used for the :win command).
3043 * If 'mustset' is FALSE, we may try to get the real window size and if
3044 * it fails use 'width' and 'height'.
3045 */
3046 void
3047set_shellsize(width, height, mustset)
3048 int width, height;
3049 int mustset;
3050{
3051 static int busy = FALSE;
3052
3053 /*
3054 * Avoid recursiveness, can happen when setting the window size causes
3055 * another window-changed signal.
3056 */
3057 if (busy)
3058 return;
3059
3060 if (width < 0 || height < 0) /* just checking... */
3061 return;
3062
3063 if (State == HITRETURN || State == SETWSIZE) /* postpone the resizing */
3064 {
3065 State = SETWSIZE;
3066 return;
3067 }
3068
3069 ++busy;
3070
3071#ifdef AMIGA
3072 out_flush(); /* must do this before mch_get_shellsize() for
3073 some obscure reason */
3074#endif
3075
3076 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3077 {
3078 Rows = height;
3079 Columns = width;
3080 check_shellsize();
3081 ui_set_shellsize(mustset);
3082 }
3083 else
3084 check_shellsize();
3085
3086 /* The window layout used to be adjusted here, but it now happens in
3087 * screenalloc() (also invoked from screenclear()). That is because the
3088 * "busy" check above may skip this, but not screenalloc(). */
3089
3090 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3091 screenclear();
3092 else
3093 screen_start(); /* don't know where cursor is now */
3094
3095 if (starting != NO_SCREEN)
3096 {
3097#ifdef FEAT_TITLE
3098 maketitle();
3099#endif
3100 changed_line_abv_curs();
3101 invalidate_botline();
3102
3103 /*
3104 * We only redraw when it's needed:
3105 * - While at the more prompt or executing an external command, don't
3106 * redraw, but position the cursor.
3107 * - While editing the command line, only redraw that.
3108 * - in Ex mode, don't redraw anything.
3109 * - Otherwise, redraw right now, and position the cursor.
3110 * Always need to call update_screen() or screenalloc(), to make
3111 * sure Rows/Columns and the size of ScreenLines[] is correct!
3112 */
3113 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3114 || exmode_active)
3115 {
3116 screenalloc(FALSE);
3117 repeat_message();
3118 }
3119 else if (State & CMDLINE)
3120 {
3121 update_screen(NOT_VALID);
3122 redrawcmdline();
3123 }
3124 else
3125 {
3126 update_topline();
3127 update_screen(NOT_VALID);
3128 if (redrawing())
3129 setcursor();
3130 }
3131 cursor_on(); /* redrawing may have switched it off */
3132 }
3133 out_flush();
3134 --busy;
3135}
3136
3137/*
3138 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3139 * commands and Ex mode).
3140 */
3141 void
3142settmode(tmode)
3143 int tmode;
3144{
3145#ifdef FEAT_GUI
3146 /* don't set the term where gvim was started to any mode */
3147 if (gui.in_use)
3148 return;
3149#endif
3150
3151 if (full_screen)
3152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 /*
3154 * When returning after calling a shell we want to really set the
3155 * terminal to raw mode, even though we think it already is, because
3156 * the shell program may have reset the terminal mode.
3157 * When we think the terminal is normal, don't try to set it to
3158 * normal again, because that causes problems (logout!) on some
3159 * machines.
3160 */
3161 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3162 {
3163#ifdef FEAT_TERMRESPONSE
3164 /* May need to check for T_CRV response and termcodes, it doesn't
3165 * work in Cooked mode, an external program may get them. */
3166 if (tmode != TMODE_RAW && crv_status == CRV_SENT)
3167 (void)vpeekc_nomap();
3168 check_for_codes_from_term();
3169#endif
3170#ifdef FEAT_MOUSE_TTY
3171 if (tmode != TMODE_RAW)
3172 mch_setmouse(FALSE); /* switch mouse off */
3173#endif
3174 out_flush();
3175 mch_settmode(tmode); /* machine specific function */
3176 cur_tmode = tmode;
3177#ifdef FEAT_MOUSE
3178 if (tmode == TMODE_RAW)
3179 setmouse(); /* may switch mouse on */
3180#endif
3181 out_flush();
3182 }
3183#ifdef FEAT_TERMRESPONSE
3184 may_req_termresponse();
3185#endif
3186 }
3187}
3188
3189 void
3190starttermcap()
3191{
3192 if (full_screen && !termcap_active)
3193 {
3194 out_str(T_TI); /* start termcap mode */
3195 out_str(T_KS); /* start "keypad transmit" mode */
3196 out_flush();
3197 termcap_active = TRUE;
3198 screen_start(); /* don't know where cursor is now */
3199#ifdef FEAT_TERMRESPONSE
3200 may_req_termresponse();
3201 /* Immediately check for a response. If t_Co changes, we don't want
3202 * to redraw with wrong colors first. */
3203 check_for_codes_from_term();
3204#endif
3205 }
3206}
3207
3208 void
3209stoptermcap()
3210{
3211 screen_stop_highlight();
3212 reset_cterm_colors();
3213 if (termcap_active)
3214 {
3215#ifdef FEAT_TERMRESPONSE
3216 /* May need to check for T_CRV response. */
3217 if (crv_status == CRV_SENT)
3218 (void)vpeekc_nomap();
3219 /* Check for termcodes first, otherwise an external program may get
3220 * them. */
3221 check_for_codes_from_term();
3222#endif
3223 out_str(T_KE); /* stop "keypad transmit" mode */
3224 out_flush();
3225 termcap_active = FALSE;
3226 cursor_on(); /* just in case it is still off */
3227 out_str(T_TE); /* stop termcap mode */
3228 screen_start(); /* don't know where cursor is now */
3229 out_flush();
3230 }
3231}
3232
3233#ifdef FEAT_TERMRESPONSE
3234/*
3235 * Request version string (for xterm) when needed.
3236 * Only do this after switching to raw mode, otherwise the result will be
3237 * echoed.
3238 * Only do this after termcap mode has been started, otherwise the codes for
3239 * the cursor keys may be wrong.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003240 * On Unix only do it when both output and input are a tty (avoid writing
3241 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 * The result is caught in check_termcode().
3243 */
3244 static void
3245may_req_termresponse()
3246{
3247 if (crv_status == CRV_GET
3248 && cur_tmode == TMODE_RAW
3249 && termcap_active
3250#ifdef UNIX
3251 && isatty(1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003252 && isatty(read_cmd_fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253#endif
3254 && *T_CRV != NUL)
3255 {
3256 out_str(T_CRV);
3257 crv_status = CRV_SENT;
3258 /* check for the characters now, otherwise they might be eaten by
3259 * get_keystroke() */
3260 out_flush();
3261 (void)vpeekc_nomap();
3262 }
3263}
3264#endif
3265
3266/*
3267 * Return TRUE when saving and restoring the screen.
3268 */
3269 int
3270swapping_screen()
3271{
3272 return (full_screen && *T_TI != NUL);
3273}
3274
3275#ifdef FEAT_MOUSE
3276/*
3277 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3278 */
3279 void
3280setmouse()
3281{
3282# ifdef FEAT_MOUSE_TTY
3283 int checkfor;
3284# endif
3285
3286# ifdef FEAT_MOUSESHAPE
3287 update_mouseshape(-1);
3288# endif
3289
3290# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3291# ifdef FEAT_GUI
3292 /* In the GUI the mouse is always enabled. */
3293 if (gui.in_use)
3294 return;
3295# endif
3296 /* be quick when mouse is off */
3297 if (*p_mouse == NUL || has_mouse_termcode == 0)
3298 return;
3299
3300 /* don't switch mouse on when not in raw mode (Ex mode) */
3301 if (cur_tmode != TMODE_RAW)
3302 {
3303 mch_setmouse(FALSE);
3304 return;
3305 }
3306
3307# ifdef FEAT_VISUAL
3308 if (VIsual_active)
3309 checkfor = MOUSE_VISUAL;
3310 else
3311# endif
3312 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
3313 checkfor = MOUSE_RETURN;
3314 else if (State & INSERT)
3315 checkfor = MOUSE_INSERT;
3316 else if (State & CMDLINE)
3317 checkfor = MOUSE_COMMAND;
3318 else if (State == CONFIRM || State == EXTERNCMD)
3319 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3320 else
3321 checkfor = MOUSE_NORMAL; /* assume normal mode */
3322
3323 if (mouse_has(checkfor))
3324 mch_setmouse(TRUE);
3325 else
3326 mch_setmouse(FALSE);
3327# endif
3328}
3329
3330/*
3331 * Return TRUE if
3332 * - "c" is in 'mouse', or
3333 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3334 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3335 * normal editing mode (not at hit-return message).
3336 */
3337 int
3338mouse_has(c)
3339 int c;
3340{
3341 char_u *p;
3342
3343 for (p = p_mouse; *p; ++p)
3344 switch (*p)
3345 {
3346 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3347 return TRUE;
3348 break;
3349 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3350 return TRUE;
3351 break;
3352 default: if (c == *p) return TRUE; break;
3353 }
3354 return FALSE;
3355}
3356
3357/*
3358 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3359 */
3360 int
3361mouse_model_popup()
3362{
3363 return (p_mousem[0] == 'p');
3364}
3365#endif
3366
3367/*
3368 * By outputting the 'cursor very visible' termcap code, for some windowed
3369 * terminals this makes the screen scrolled to the correct position.
3370 * Used when starting Vim or returning from a shell.
3371 */
3372 void
3373scroll_start()
3374{
3375 if (*T_VS != NUL)
3376 {
3377 out_str(T_VS);
3378 out_str(T_VE);
3379 screen_start(); /* don't know where cursor is now */
3380 }
3381}
3382
3383static int cursor_is_off = FALSE;
3384
3385/*
3386 * Enable the cursor.
3387 */
3388 void
3389cursor_on()
3390{
3391 if (cursor_is_off)
3392 {
3393 out_str(T_VE);
3394 cursor_is_off = FALSE;
3395 }
3396}
3397
3398/*
3399 * Disable the cursor.
3400 */
3401 void
3402cursor_off()
3403{
3404 if (full_screen)
3405 {
3406 if (!cursor_is_off)
3407 out_str(T_VI); /* disable cursor */
3408 cursor_is_off = TRUE;
3409 }
3410}
3411
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003412#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413/*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003414 * Set cursor shape to match Insert mode.
3415 */
3416 void
3417term_cursor_shape()
3418{
3419 static int showing_insert_mode = MAYBE;
3420
3421 if (!full_screen || *T_CSI == NUL || *T_CEI == NUL)
3422 return;
3423
3424 if (State & INSERT)
3425 {
3426 if (showing_insert_mode != TRUE)
3427 out_str(T_CSI); /* disable cursor */
3428 showing_insert_mode = TRUE;
3429 }
3430 else
3431 {
3432 if (showing_insert_mode != FALSE)
3433 out_str(T_CEI); /* disable cursor */
3434 showing_insert_mode = FALSE;
3435 }
3436}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003437#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003438
3439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 * Set scrolling region for window 'wp'.
3441 * The region starts 'off' lines from the start of the window.
3442 * Also set the vertical scroll region for a vertically split window. Always
3443 * the full width of the window, excluding the vertical separator.
3444 */
3445 void
3446scroll_region_set(wp, off)
3447 win_T *wp;
3448 int off;
3449{
3450 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3451 W_WINROW(wp) + off));
3452#ifdef FEAT_VERTSPLIT
3453 if (*T_CSV != NUL && wp->w_width != Columns)
3454 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3455 W_WINCOL(wp)));
3456#endif
3457 screen_start(); /* don't know where cursor is now */
3458}
3459
3460/*
3461 * Reset scrolling region to the whole screen.
3462 */
3463 void
3464scroll_region_reset()
3465{
3466 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
3467#ifdef FEAT_VERTSPLIT
3468 if (*T_CSV != NUL)
3469 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3470#endif
3471 screen_start(); /* don't know where cursor is now */
3472}
3473
3474
3475/*
3476 * List of terminal codes that are currently recognized.
3477 */
3478
3479struct termcode
3480{
3481 char_u name[2]; /* termcap name of entry */
3482 char_u *code; /* terminal code (in allocated memory) */
3483 int len; /* STRLEN(code) */
3484} *termcodes = NULL;
3485
3486static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3487static int tc_len = 0; /* current number of entries in termcodes[] */
3488
3489 void
3490clear_termcodes()
3491{
3492 while (tc_len > 0)
3493 vim_free(termcodes[--tc_len].code);
3494 vim_free(termcodes);
3495 termcodes = NULL;
3496 tc_max_len = 0;
3497
3498#ifdef HAVE_TGETENT
3499 BC = (char *)empty_option;
3500 UP = (char *)empty_option;
3501 PC = NUL; /* set pad character to NUL */
3502 ospeed = 0;
3503#endif
3504
3505 need_gather = TRUE; /* need to fill termleader[] */
3506}
3507
3508/*
3509 * Add a new entry to the list of terminal codes.
3510 * The list is kept alphabetical for ":set termcap"
3511 */
3512 void
3513add_termcode(name, string, use_8bit)
3514 char_u *name;
3515 char_u *string;
3516 int use_8bit; /* replace 7-bit control by 8-bit one */
3517{
3518 struct termcode *new_tc;
3519 int i, j;
3520 char_u *s;
3521
3522 if (string == NULL || *string == NUL)
3523 {
3524 del_termcode(name);
3525 return;
3526 }
3527
3528 s = vim_strsave(string);
3529 if (s == NULL)
3530 return;
3531
3532 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
3533 if (use_8bit && term_7to8bit(string) != 0)
3534 {
3535 mch_memmove(s, s + 1, STRLEN(s));
3536 s[0] = term_7to8bit(string);
3537 }
3538
3539 need_gather = TRUE; /* need to fill termleader[] */
3540
3541 /*
3542 * need to make space for more entries
3543 */
3544 if (tc_len == tc_max_len)
3545 {
3546 tc_max_len += 20;
3547 new_tc = (struct termcode *)alloc(
3548 (unsigned)(tc_max_len * sizeof(struct termcode)));
3549 if (new_tc == NULL)
3550 {
3551 tc_max_len -= 20;
3552 return;
3553 }
3554 for (i = 0; i < tc_len; ++i)
3555 new_tc[i] = termcodes[i];
3556 vim_free(termcodes);
3557 termcodes = new_tc;
3558 }
3559
3560 /*
3561 * Look for existing entry with the same name, it is replaced.
3562 * Look for an existing entry that is alphabetical higher, the new entry
3563 * is inserted in front of it.
3564 */
3565 for (i = 0; i < tc_len; ++i)
3566 {
3567 if (termcodes[i].name[0] < name[0])
3568 continue;
3569 if (termcodes[i].name[0] == name[0])
3570 {
3571 if (termcodes[i].name[1] < name[1])
3572 continue;
3573 /*
3574 * Exact match: Replace old code.
3575 */
3576 if (termcodes[i].name[1] == name[1])
3577 {
3578 vim_free(termcodes[i].code);
3579 --tc_len;
3580 break;
3581 }
3582 }
3583 /*
3584 * Found alphabetical larger entry, move rest to insert new entry
3585 */
3586 for (j = tc_len; j > i; --j)
3587 termcodes[j] = termcodes[j - 1];
3588 break;
3589 }
3590
3591 termcodes[i].name[0] = name[0];
3592 termcodes[i].name[1] = name[1];
3593 termcodes[i].code = s;
3594 termcodes[i].len = (int)STRLEN(s);
3595 ++tc_len;
3596}
3597
3598 char_u *
3599find_termcode(name)
3600 char_u *name;
3601{
3602 int i;
3603
3604 for (i = 0; i < tc_len; ++i)
3605 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3606 return termcodes[i].code;
3607 return NULL;
3608}
3609
3610#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3611 char_u *
3612get_termcode(i)
3613 int i;
3614{
3615 if (i >= tc_len)
3616 return NULL;
3617 return &termcodes[i].name[0];
3618}
3619#endif
3620
3621 void
3622del_termcode(name)
3623 char_u *name;
3624{
3625 int i;
3626
3627 if (termcodes == NULL) /* nothing there yet */
3628 return;
3629
3630 need_gather = TRUE; /* need to fill termleader[] */
3631
3632 for (i = 0; i < tc_len; ++i)
3633 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3634 {
3635 del_termcode_idx(i);
3636 return;
3637 }
3638 /* not found. Give error message? */
3639}
3640
3641 static void
3642del_termcode_idx(idx)
3643 int idx;
3644{
3645 int i;
3646
3647 vim_free(termcodes[idx].code);
3648 --tc_len;
3649 for (i = idx; i < tc_len; ++i)
3650 termcodes[i] = termcodes[i + 1];
3651}
3652
3653#ifdef FEAT_TERMRESPONSE
3654/*
3655 * Called when detected that the terminal sends 8-bit codes.
3656 * Convert all 7-bit codes to their 8-bit equivalent.
3657 */
3658 static void
3659switch_to_8bit()
3660{
3661 int i;
3662 int c;
3663
3664 /* Only need to do something when not already using 8-bit codes. */
3665 if (!term_is_8bit(T_NAME))
3666 {
3667 for (i = 0; i < tc_len; ++i)
3668 {
3669 c = term_7to8bit(termcodes[i].code);
3670 if (c != 0)
3671 {
3672 mch_memmove(termcodes[i].code + 1, termcodes[i].code + 2,
3673 STRLEN(termcodes[i].code + 1));
3674 termcodes[i].code[0] = c;
3675 }
3676 }
3677 need_gather = TRUE; /* need to fill termleader[] */
3678 }
3679 detected_8bit = TRUE;
3680}
3681#endif
3682
3683#ifdef CHECK_DOUBLE_CLICK
3684static linenr_T orig_topline = 0;
3685# ifdef FEAT_DIFF
3686static int orig_topfill = 0;
3687# endif
3688#endif
3689#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
3690/*
3691 * Checking for double clicks ourselves.
3692 * "orig_topline" is used to avoid detecting a double-click when the window
3693 * contents scrolled (e.g., when 'scrolloff' is non-zero).
3694 */
3695/*
3696 * Set orig_topline. Used when jumping to another window, so that a double
3697 * click still works.
3698 */
3699 void
3700set_mouse_topline(wp)
3701 win_T *wp;
3702{
3703 orig_topline = wp->w_topline;
3704# ifdef FEAT_DIFF
3705 orig_topfill = wp->w_topfill;
3706# endif
3707}
3708#endif
3709
3710/*
3711 * Check if typebuf.tb_buf[] contains a terminal key code.
3712 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
3713 * + max_offset].
3714 * Return 0 for no match, -1 for partial match, > 0 for full match.
3715 * With a match, the match is removed, the replacement code is inserted in
3716 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
3717 * returned.
3718 * When "buf" is not NULL, it is used instead of typebuf.tb_buf[]. "buflen" is
3719 * then the length of the string in buf[].
3720 */
3721 int
3722check_termcode(max_offset, buf, buflen)
3723 int max_offset;
3724 char_u *buf;
3725 int buflen;
3726{
3727 char_u *tp;
3728 char_u *p;
3729 int slen = 0; /* init for GCC */
3730 int len;
3731 int offset;
3732 char_u key_name[2];
3733 int new_slen;
3734 int extra;
3735 char_u string[MAX_KEY_CODE_LEN + 1];
3736 int i, j;
3737 int idx = 0;
3738#ifdef FEAT_MOUSE
3739# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI)
3740 char_u bytes[6];
3741 int num_bytes;
3742# endif
3743 int mouse_code = 0; /* init for GCC */
3744 int modifiers;
3745 int is_click, is_drag;
3746 int wheel_code = 0;
3747 int current_button;
3748 static int held_button = MOUSE_RELEASE;
3749 static int orig_num_clicks = 1;
3750 static int orig_mouse_code = 0x0;
3751# ifdef CHECK_DOUBLE_CLICK
3752 static int orig_mouse_col = 0;
3753 static int orig_mouse_row = 0;
3754 static struct timeval orig_mouse_time = {0, 0};
3755 /* time of previous mouse click */
3756 struct timeval mouse_time; /* time of current mouse click */
3757 long timediff; /* elapsed time in msec */
3758# endif
3759#endif
3760 int cpo_koffset;
3761#ifdef FEAT_MOUSE_GPM
3762 extern int gpm_flag; /* gpm library variable */
3763#endif
3764
3765 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
3766
3767 /*
3768 * Speed up the checks for terminal codes by gathering all first bytes
3769 * used in termleader[]. Often this is just a single <Esc>.
3770 */
3771 if (need_gather)
3772 gather_termleader();
3773
3774 /*
3775 * Check at several positions in typebuf.tb_buf[], to catch something like
3776 * "x<Up>" that can be mapped. Stop at max_offset, because characters
3777 * after that cannot be used for mapping, and with @r commands
3778 * typebuf.tb_buf[]
3779 * can become very long.
3780 * This is used often, KEEP IT FAST!
3781 */
3782 for (offset = 0; offset < max_offset; ++offset)
3783 {
3784 if (buf == NULL)
3785 {
3786 if (offset >= typebuf.tb_len)
3787 break;
3788 tp = typebuf.tb_buf + typebuf.tb_off + offset;
3789 len = typebuf.tb_len - offset; /* length of the input */
3790 }
3791 else
3792 {
3793 if (offset >= buflen)
3794 break;
3795 tp = buf + offset;
3796 len = buflen - offset;
3797 }
3798
3799 /*
3800 * Don't check characters after K_SPECIAL, those are already
3801 * translated terminal chars (avoid translating ~@^Hx).
3802 */
3803 if (*tp == K_SPECIAL)
3804 {
3805 offset += 2; /* there are always 2 extra characters */
3806 continue;
3807 }
3808
3809 /*
3810 * Skip this position if the character does not appear as the first
3811 * character in term_strings. This speeds up a lot, since most
3812 * termcodes start with the same character (ESC or CSI).
3813 */
3814 i = *tp;
3815 for (p = termleader; *p && *p != i; ++p)
3816 ;
3817 if (*p == NUL)
3818 continue;
3819
3820 /*
3821 * Skip this position if p_ek is not set and tp[0] is an ESC and we
3822 * are in Insert mode.
3823 */
3824 if (*tp == ESC && !p_ek && (State & INSERT))
3825 continue;
3826
3827 new_slen = 0; /* Length of what will replace the termcode */
3828 key_name[0] = NUL; /* no key name found yet */
3829
3830#ifdef FEAT_GUI
3831 if (gui.in_use)
3832 {
3833 /*
3834 * GUI special key codes are all of the form [CSI xx].
3835 */
3836 if (*tp == CSI) /* Special key from GUI */
3837 {
3838 if (len < 3)
3839 return -1; /* Shouldn't happen */
3840 slen = 3;
3841 key_name[0] = tp[1];
3842 key_name[1] = tp[2];
3843 }
3844 }
3845 else
3846#endif /* FEAT_GUI */
3847 {
3848 for (idx = 0; idx < tc_len; ++idx)
3849 {
3850 /*
3851 * Ignore the entry if we are not at the start of
3852 * typebuf.tb_buf[]
3853 * and there are not enough characters to make a match.
3854 * But only when the 'K' flag is in 'cpoptions'.
3855 */
3856 slen = termcodes[idx].len;
3857 if (cpo_koffset && offset && len < slen)
3858 continue;
3859 if (STRNCMP(termcodes[idx].code, tp,
3860 (size_t)(slen > len ? len : slen)) == 0)
3861 {
3862 if (len < slen) /* got a partial sequence */
3863 return -1; /* need to get more chars */
3864
3865 /*
3866 * When found a keypad key, check if there is another key
3867 * that matches and use that one. This makes <Home> to be
3868 * found instead of <kHome> when they produce the same
3869 * key code.
3870 */
3871 if (termcodes[idx].name[0] == 'K'
3872 && VIM_ISDIGIT(termcodes[idx].name[1]))
3873 {
3874 for (j = idx + 1; j < tc_len; ++j)
3875 if (termcodes[j].len == slen &&
3876 STRNCMP(termcodes[idx].code,
3877 termcodes[j].code, slen) == 0)
3878 {
3879 idx = j;
3880 break;
3881 }
3882 }
3883
3884 key_name[0] = termcodes[idx].name[0];
3885 key_name[1] = termcodes[idx].name[1];
3886
3887 break;
3888 }
3889 }
3890 }
3891
3892#ifdef FEAT_TERMRESPONSE
3893 if (key_name[0] == NUL)
3894 {
3895 /* Check for xterm version string: "<Esc>[>{x};{vers};{y}c". Also
3896 * eat other possible responses to t_RV, rxvt returns
3897 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[. */
3898 if (*T_CRV != NUL && ((tp[0] == ESC && tp[1] == '[' && len >= 3)
3899 || (tp[0] == CSI && len >= 2)))
3900 {
3901 j = 0;
3902 extra = 0;
3903 for (i = 2 + (tp[0] != CSI);
3904 i < len && (VIM_ISDIGIT(tp[i])
3905 || tp[i] == ';' || tp[i] == '.'); ++i)
3906 if (tp[i] == ';' && ++j == 1)
3907 extra = atoi((char *)tp + i + 1);
3908 if (i == len)
3909 return -1; /* not enough characters */
3910
3911 /* eat it when at least one digit and ending in 'c' */
3912 if (i > 2 + (tp[0] != CSI) && tp[i] == 'c')
3913 {
3914 crv_status = CRV_GOT;
3915
3916 /* If this code starts with CSI, you can bet that the
3917 * terminal uses 8-bit codes. */
3918 if (tp[0] == CSI)
3919 switch_to_8bit();
3920
3921 /* rxvt sends its version number: "20703" is 2.7.3.
3922 * Ignore it for when the user has set 'term' to xterm,
3923 * even though it's an rxvt. */
3924 if (extra > 20000)
3925 extra = 0;
3926
3927 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
3928 {
3929 /* if xterm version >= 95 use mouse dragging */
3930 if (extra >= 95)
3931 set_option_value((char_u *)"ttym", 0L,
3932 (char_u *)"xterm2", 0);
3933 /* if xterm version >= 141 try to get termcap codes */
3934 if (extra >= 141)
3935 {
3936 check_for_codes = TRUE;
3937 need_gather = TRUE;
3938 req_codes_from_term();
3939 }
3940 }
3941# ifdef FEAT_EVAL
3942 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
3943# endif
3944# ifdef FEAT_AUTOCMD
3945 apply_autocmds(EVENT_TERMRESPONSE,
3946 NULL, NULL, FALSE, curbuf);
3947# endif
3948 key_name[0] = (int)KS_EXTRA;
3949 key_name[1] = (int)KE_IGNORE;
3950 slen = i + 1;
3951 }
3952 }
3953
3954 /* Check for '<Esc>P1+r<hex bytes><Esc>\'. A "0" instead of the
3955 * "1" means an invalid request. */
3956 else if (check_for_codes
3957 && ((tp[0] == ESC && tp[1] == 'P' && len >= 2)
3958 || tp[0] == DCS))
3959 {
3960 j = 1 + (tp[0] != DCS);
3961 for (i = j; i < len; ++i)
3962 if ((tp[i] == ESC && tp[i + 1] == '\\' && i + 1 < len)
3963 || tp[i] == STERM)
3964 {
3965 if (i - j >= 3 && tp[j + 1] == '+' && tp[j + 2] == 'r')
3966 got_code_from_term(tp + j, i);
3967 key_name[0] = (int)KS_EXTRA;
3968 key_name[1] = (int)KE_IGNORE;
3969 slen = i + 1 + (tp[i] == ESC);
3970 break;
3971 }
3972
3973 if (i == len)
3974 return -1; /* not enough characters */
3975 }
3976 }
3977#endif
3978
3979 if (key_name[0] == NUL)
3980 continue; /* No match at this position, try next one */
3981
3982 /* We only get here when we have a complete termcode match */
3983
3984#ifdef FEAT_MOUSE
3985# ifdef FEAT_GUI
3986 /*
3987 * Only in the GUI: Fetch the pointer coordinates of the scroll event
3988 * so that we know which window to scroll later.
3989 */
3990 if (gui.in_use
3991 && key_name[0] == (int)KS_EXTRA
3992 && (key_name[1] == (int)KE_X1MOUSE
3993 || key_name[1] == (int)KE_X2MOUSE
3994 || key_name[1] == (int)KE_MOUSEDOWN
3995 || key_name[1] == (int)KE_MOUSEUP))
3996 {
3997 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
3998 if (num_bytes == -1) /* not enough coordinates */
3999 return -1;
4000 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4001 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4002 slen += num_bytes;
4003 }
4004 else
4005# endif
4006 /*
4007 * If it is a mouse click, get the coordinates.
4008 */
4009 if (key_name[0] == (int)KS_MOUSE
4010# ifdef FEAT_MOUSE_JSB
4011 || key_name[0] == (int)KS_JSBTERM_MOUSE
4012# endif
4013# ifdef FEAT_MOUSE_NET
4014 || key_name[0] == (int)KS_NETTERM_MOUSE
4015# endif
4016# ifdef FEAT_MOUSE_DEC
4017 || key_name[0] == (int)KS_DEC_MOUSE
4018# endif
4019# ifdef FEAT_MOUSE_PTERM
4020 || key_name[0] == (int)KS_PTERM_MOUSE
4021# endif
4022 )
4023 {
4024 is_click = is_drag = FALSE;
4025
4026# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI)
4027 if (key_name[0] == (int)KS_MOUSE)
4028 {
4029 /*
4030 * For xterm and MSDOS we get "<t_mouse>scr", where
4031 * s == encoded button state:
4032 * 0x20 = left button down
4033 * 0x21 = middle button down
4034 * 0x22 = right button down
4035 * 0x23 = any button release
4036 * 0x60 = button 4 down (scroll wheel down)
4037 * 0x61 = button 5 down (scroll wheel up)
4038 * add 0x04 for SHIFT
4039 * add 0x08 for ALT
4040 * add 0x10 for CTRL
4041 * add 0x20 for mouse drag (0x40 is drag with left button)
4042 * c == column + ' ' + 1 == column + 33
4043 * r == row + ' ' + 1 == row + 33
4044 *
4045 * The coordinates are passed on through global variables.
4046 * Ugly, but this avoids trouble with mouse clicks at an
4047 * unexpected moment and allows for mapping them.
4048 */
4049 for (;;)
4050 {
4051#ifdef FEAT_GUI
4052 if (gui.in_use)
4053 {
4054 /* GUI uses more bits for columns > 223 */
4055 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4056 if (num_bytes == -1) /* not enough coordinates */
4057 return -1;
4058 mouse_code = bytes[0];
4059 mouse_col = 128 * (bytes[1] - ' ' - 1)
4060 + bytes[2] - ' ' - 1;
4061 mouse_row = 128 * (bytes[3] - ' ' - 1)
4062 + bytes[4] - ' ' - 1;
4063 }
4064 else
4065#endif
4066 {
4067 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4068 if (num_bytes == -1) /* not enough coordinates */
4069 return -1;
4070 mouse_code = bytes[0];
4071 mouse_col = bytes[1] - ' ' - 1;
4072 mouse_row = bytes[2] - ' ' - 1;
4073 }
4074 slen += num_bytes;
4075
4076 /* If the following bytes is also a mouse code and it has
4077 * the same code, dump this one and get the next. This
4078 * makes dragging a whole lot faster. */
4079#ifdef FEAT_GUI
4080 if (gui.in_use)
4081 j = 3;
4082 else
4083#endif
4084 j = termcodes[idx].len;
4085 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4086 && tp[slen + j] == mouse_code
4087 && tp[slen + j + 1] != NUL
4088 && tp[slen + j + 2] != NUL
4089#ifdef FEAT_GUI
4090 && (!gui.in_use
4091 || (tp[slen + j + 3] != NUL
4092 && tp[slen + j + 4] != NUL))
4093#endif
4094 )
4095 slen += j;
4096 else
4097 break;
4098 }
4099
4100# if !defined(MSWIN) && !defined(MSDOS)
4101 /*
4102 * Handle mouse events.
4103 * Recognize the xterm mouse wheel, but not in the GUI, the
4104 * Linux console with GPM and the MS-DOS or Win32 console
4105 * (multi-clicks use >= 0x60).
4106 */
4107 if (mouse_code >= MOUSEWHEEL_LOW
4108# ifdef FEAT_GUI
4109 && !gui.in_use
4110# endif
4111# ifdef FEAT_MOUSE_GPM
4112 && gpm_flag == 0
4113# endif
4114 )
4115 {
4116 /* Keep the mouse_code before it's changed, so that we
4117 * remember that it was a mouse wheel click. */
4118 wheel_code = mouse_code;
4119 }
4120# ifdef FEAT_MOUSE_XTERM
4121 else if (held_button == MOUSE_RELEASE
4122# ifdef FEAT_GUI
4123 && !gui.in_use
4124# endif
4125 && (mouse_code == 0x23 || mouse_code == 0x24))
4126 {
4127 /* Apparently used by rxvt scroll wheel. */
4128 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4129 }
4130# endif
4131
4132# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4133 else if (use_xterm_mouse() > 1)
4134 {
4135 if (mouse_code & MOUSE_DRAG_XTERM)
4136 mouse_code |= MOUSE_DRAG;
4137 }
4138# endif
4139# ifdef FEAT_XCLIPBOARD
4140 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4141 {
4142 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4143 stop_xterm_trace();
4144 else
4145 start_xterm_trace(mouse_code);
4146 }
4147# endif
4148# endif
4149 }
4150# endif /* !UNIX || FEAT_MOUSE_XTERM */
4151# ifdef FEAT_MOUSE_NET
4152 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4153 {
4154 int mc, mr;
4155
4156 /* expect a rather limited sequence like: balancing {
4157 * \033}6,45\r
4158 * '6' is the row, 45 is the column
4159 */
4160 p = tp + slen;
4161 mr = getdigits(&p);
4162 if (*p++ != ',')
4163 return -1;
4164 mc = getdigits(&p);
4165 if (*p++ != '\r')
4166 return -1;
4167
4168 mouse_col = mc - 1;
4169 mouse_row = mr - 1;
4170 mouse_code = MOUSE_LEFT;
4171 slen += (int)(p - (tp + slen));
4172 }
4173# endif /* FEAT_MOUSE_NET */
4174# ifdef FEAT_MOUSE_JSB
4175 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4176 {
4177 int mult, val, iter, button, status;
4178
4179 /* JSBTERM Input Model
4180 * \033[0~zw uniq escape sequence
4181 * (L-x) Left button pressed - not pressed x not reporting
4182 * (M-x) Middle button pressed - not pressed x not reporting
4183 * (R-x) Right button pressed - not pressed x not reporting
4184 * (SDmdu) Single , Double click, m mouse move d button down
4185 * u button up
4186 * ### X cursor position padded to 3 digits
4187 * ### Y cursor position padded to 3 digits
4188 * (s-x) SHIFT key pressed - not pressed x not reporting
4189 * (c-x) CTRL key pressed - not pressed x not reporting
4190 * \033\\ terminateing sequence
4191 */
4192
4193 p = tp + slen;
4194 button = mouse_code = 0;
4195 switch (*p++)
4196 {
4197 case 'L': button = 1; break;
4198 case '-': break;
4199 case 'x': break; /* ignore sequence */
4200 default: return -1; /* Unknown Result */
4201 }
4202 switch (*p++)
4203 {
4204 case 'M': button |= 2; break;
4205 case '-': break;
4206 case 'x': break; /* ignore sequence */
4207 default: return -1; /* Unknown Result */
4208 }
4209 switch (*p++)
4210 {
4211 case 'R': button |= 4; break;
4212 case '-': break;
4213 case 'x': break; /* ignore sequence */
4214 default: return -1; /* Unknown Result */
4215 }
4216 status = *p++;
4217 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4218 mult /= 10, p++)
4219 if (*p >= '0' && *p <= '9')
4220 val += (*p - '0') * mult;
4221 else
4222 return -1;
4223 mouse_col = val;
4224 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4225 mult /= 10, p++)
4226 if (*p >= '0' && *p <= '9')
4227 val += (*p - '0') * mult;
4228 else
4229 return -1;
4230 mouse_row = val;
4231 switch (*p++)
4232 {
4233 case 's': button |= 8; break; /* SHIFT key Pressed */
4234 case '-': break; /* Not Pressed */
4235 case 'x': break; /* Not Reporting */
4236 default: return -1; /* Unknown Result */
4237 }
4238 switch (*p++)
4239 {
4240 case 'c': button |= 16; break; /* CTRL key Pressed */
4241 case '-': break; /* Not Pressed */
4242 case 'x': break; /* Not Reporting */
4243 default: return -1; /* Unknown Result */
4244 }
4245 if (*p++ != '\033')
4246 return -1;
4247 if (*p++ != '\\')
4248 return -1;
4249 switch (status)
4250 {
4251 case 'D': /* Double Click */
4252 case 'S': /* Single Click */
4253 if (button & 1) mouse_code |= MOUSE_LEFT;
4254 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4255 if (button & 4) mouse_code |= MOUSE_RIGHT;
4256 if (button & 8) mouse_code |= MOUSE_SHIFT;
4257 if (button & 16) mouse_code |= MOUSE_CTRL;
4258 break;
4259 case 'm': /* Mouse move */
4260 if (button & 1) mouse_code |= MOUSE_LEFT;
4261 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4262 if (button & 4) mouse_code |= MOUSE_RIGHT;
4263 if (button & 8) mouse_code |= MOUSE_SHIFT;
4264 if (button & 16) mouse_code |= MOUSE_CTRL;
4265 if ((button & 7) != 0)
4266 {
4267 held_button = mouse_code;
4268 mouse_code |= MOUSE_DRAG;
4269 }
4270 is_drag = TRUE;
4271 showmode();
4272 break;
4273 case 'd': /* Button Down */
4274 if (button & 1) mouse_code |= MOUSE_LEFT;
4275 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4276 if (button & 4) mouse_code |= MOUSE_RIGHT;
4277 if (button & 8) mouse_code |= MOUSE_SHIFT;
4278 if (button & 16) mouse_code |= MOUSE_CTRL;
4279 break;
4280 case 'u': /* Button Up */
4281 if (button & 1)
4282 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4283 if (button & 2)
4284 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4285 if (button & 4)
4286 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4287 if (button & 8)
4288 mouse_code |= MOUSE_SHIFT;
4289 if (button & 16)
4290 mouse_code |= MOUSE_CTRL;
4291 break;
4292 default: return -1; /* Unknown Result */
4293 }
4294
4295 slen += (p - (tp + slen));
4296 }
4297# endif /* FEAT_MOUSE_JSB */
4298# ifdef FEAT_MOUSE_DEC
4299 if (key_name[0] == (int)KS_DEC_MOUSE)
4300 {
4301 /* The DEC Locator Input Model
4302 * Netterm delivers the code sequence:
4303 * \033[2;4;24;80&w (left button down)
4304 * \033[3;0;24;80&w (left button up)
4305 * \033[6;1;24;80&w (right button down)
4306 * \033[7;0;24;80&w (right button up)
4307 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4308 * Pe is the event code
4309 * Pb is the button code
4310 * Pr is the row coordinate
4311 * Pc is the column coordinate
4312 * Pp is the third coordinate (page number)
4313 * Pe, the event code indicates what event caused this report
4314 * The following event codes are defined:
4315 * 0 - request, the terminal received an explicit request
4316 * for a locator report, but the locator is unavailable
4317 * 1 - request, the terminal received an explicit request
4318 * for a locator report
4319 * 2 - left button down
4320 * 3 - left button up
4321 * 4 - middle button down
4322 * 5 - middle button up
4323 * 6 - right button down
4324 * 7 - right button up
4325 * 8 - fourth button down
4326 * 9 - fourth button up
4327 * 10 - locator outside filter rectangle
4328 * Pb, the button code, ASCII decimal 0-15 indicating which
4329 * buttons are down if any. The state of the four buttons
4330 * on the locator correspond to the low four bits of the
4331 * decimal value,
4332 * "1" means button depressed
4333 * 0 - no buttons down,
4334 * 1 - right,
4335 * 2 - middle,
4336 * 4 - left,
4337 * 8 - fourth
4338 * Pr is the row coordinate of the locator position in the page,
4339 * encoded as an ASCII decimal value.
4340 * If Pr is omitted, the locator position is undefined
4341 * (outside the terminal window for example).
4342 * Pc is the column coordinate of the locator position in the
4343 * page, encoded as an ASCII decimal value.
4344 * If Pc is omitted, the locator position is undefined
4345 * (outside the terminal window for example).
4346 * Pp is the page coordinate of the locator position
4347 * encoded as an ASCII decimal value.
4348 * The page coordinate may be omitted if the locator is on
4349 * page one (the default). We ignore it anyway.
4350 */
4351 int Pe, Pb, Pr, Pc;
4352
4353 p = tp + slen;
4354
4355 /* get event status */
4356 Pe = getdigits(&p);
4357 if (*p++ != ';')
4358 return -1;
4359
4360 /* get button status */
4361 Pb = getdigits(&p);
4362 if (*p++ != ';')
4363 return -1;
4364
4365 /* get row status */
4366 Pr = getdigits(&p);
4367 if (*p++ != ';')
4368 return -1;
4369
4370 /* get column status */
4371 Pc = getdigits(&p);
4372
4373 /* the page parameter is optional */
4374 if (*p == ';')
4375 {
4376 p++;
4377 (void)getdigits(&p);
4378 }
4379 if (*p++ != '&')
4380 return -1;
4381 if (*p++ != 'w')
4382 return -1;
4383
4384 mouse_code = 0;
4385 switch (Pe)
4386 {
4387 case 0: return -1; /* position request while unavailable */
4388 case 1: /* a response to a locator position request includes
4389 the status of all buttons */
4390 Pb &= 7; /* mask off and ignore fourth button */
4391 if (Pb & 4)
4392 mouse_code = MOUSE_LEFT;
4393 if (Pb & 2)
4394 mouse_code = MOUSE_MIDDLE;
4395 if (Pb & 1)
4396 mouse_code = MOUSE_RIGHT;
4397 if (Pb)
4398 {
4399 held_button = mouse_code;
4400 mouse_code |= MOUSE_DRAG;
4401 WantQueryMouse = 1;
4402 }
4403 is_drag = TRUE;
4404 showmode();
4405 break;
4406 case 2: mouse_code = MOUSE_LEFT;
4407 WantQueryMouse = 1;
4408 break;
4409 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
4410 break;
4411 case 4: mouse_code = MOUSE_MIDDLE;
4412 WantQueryMouse = 1;
4413 break;
4414 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
4415 break;
4416 case 6: mouse_code = MOUSE_RIGHT;
4417 WantQueryMouse = 1;
4418 break;
4419 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
4420 break;
4421 case 8: return -1; /* fourth button down */
4422 case 9: return -1; /* fourth button up */
4423 case 10: return -1; /* mouse outside of filter rectangle */
4424 default: return -1; /* should never occur */
4425 }
4426
4427 mouse_col = Pc - 1;
4428 mouse_row = Pr - 1;
4429
4430 slen += (int)(p - (tp + slen));
4431 }
4432# endif /* FEAT_MOUSE_DEC */
4433# ifdef FEAT_MOUSE_PTERM
4434 if (key_name[0] == (int)KS_PTERM_MOUSE)
4435 {
4436 int button, num_clicks, action, mc, mr;
4437
4438 p = tp + slen;
4439
4440 action = getdigits(&p);
4441 if (*p++ != ';')
4442 return -1;
4443
4444 mouse_row = getdigits(&p);
4445 if (*p++ != ';')
4446 return -1;
4447 mouse_col = getdigits(&p);
4448 if (*p++ != ';')
4449 return -1;
4450
4451 button = getdigits(&p);
4452 mouse_code = 0;
4453
4454 switch( button )
4455 {
4456 case 4: mouse_code = MOUSE_LEFT; break;
4457 case 1: mouse_code = MOUSE_RIGHT; break;
4458 case 2: mouse_code = MOUSE_MIDDLE; break;
4459 default: return -1;
4460 }
4461
4462 switch( action )
4463 {
4464 case 31: /* Initial press */
4465 if (*p++ != ';')
4466 return -1;
4467
4468 num_clicks = getdigits(&p); /* Not used */
4469 break;
4470
4471 case 32: /* Release */
4472 mouse_code |= MOUSE_RELEASE;
4473 break;
4474
4475 case 33: /* Drag */
4476 held_button = mouse_code;
4477 mouse_code |= MOUSE_DRAG;
4478 break;
4479
4480 default:
4481 return -1;
4482 }
4483
4484 if (*p++ != 't')
4485 return -1;
4486
4487 slen += (p - (tp + slen));
4488 }
4489# endif /* FEAT_MOUSE_PTERM */
4490
4491 /* Interpret the mouse code */
4492 current_button = (mouse_code & MOUSE_CLICK_MASK);
4493 if (current_button == MOUSE_RELEASE
4494# ifdef FEAT_MOUSE_XTERM
4495 && wheel_code == 0
4496# endif
4497 )
4498 {
4499 /*
4500 * If we get a mouse drag or release event when
4501 * there is no mouse button held down (held_button ==
4502 * MOUSE_RELEASE), produce a K_IGNORE below.
4503 * (can happen when you hold down two buttons
4504 * and then let them go, or click in the menu bar, but not
4505 * on a menu, and drag into the text).
4506 */
4507 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
4508 is_drag = TRUE;
4509 current_button = held_button;
4510 }
4511 else if (wheel_code == 0)
4512 {
4513# ifdef CHECK_DOUBLE_CLICK
4514# ifdef FEAT_MOUSE_GPM
4515# ifdef FEAT_GUI
4516 /*
4517 * Only for Unix, when GUI or gpm is not active, we handle
4518 * multi-clicks here.
4519 */
4520 if (gpm_flag == 0 && !gui.in_use)
4521# else
4522 if (gpm_flag == 0)
4523# endif
4524# else
4525# ifdef FEAT_GUI
4526 if (!gui.in_use)
4527# endif
4528# endif
4529 {
4530 /*
4531 * Compute the time elapsed since the previous mouse click.
4532 */
4533 gettimeofday(&mouse_time, NULL);
4534 timediff = (mouse_time.tv_usec
4535 - orig_mouse_time.tv_usec) / 1000;
4536 if (timediff < 0)
4537 --orig_mouse_time.tv_sec;
4538 timediff += (mouse_time.tv_sec
4539 - orig_mouse_time.tv_sec) * 1000;
4540 orig_mouse_time = mouse_time;
4541 if (mouse_code == orig_mouse_code
4542 && timediff < p_mouset
4543 && orig_num_clicks != 4
4544 && orig_mouse_col == mouse_col
4545 && orig_mouse_row == mouse_row
4546#ifdef FEAT_DIFF
4547 && orig_topfill == curwin->w_topfill
4548#endif
4549 && orig_topline == curwin->w_topline)
4550 ++orig_num_clicks;
4551 else
4552 orig_num_clicks = 1;
4553 orig_mouse_col = mouse_col;
4554 orig_mouse_row = mouse_row;
4555 orig_topline = curwin->w_topline;
4556#ifdef FEAT_DIFF
4557 orig_topfill = curwin->w_topfill;
4558#endif
4559 }
4560# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
4561 else
4562 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
4563# endif
4564# else
4565 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
4566# endif
4567 is_click = TRUE;
4568 orig_mouse_code = mouse_code;
4569 }
4570 if (!is_drag)
4571 held_button = mouse_code & MOUSE_CLICK_MASK;
4572
4573 /*
4574 * Translate the actual mouse event into a pseudo mouse event.
4575 * First work out what modifiers are to be used.
4576 */
4577 modifiers = 0x0;
4578 if (orig_mouse_code & MOUSE_SHIFT)
4579 modifiers |= MOD_MASK_SHIFT;
4580 if (orig_mouse_code & MOUSE_CTRL)
4581 modifiers |= MOD_MASK_CTRL;
4582 if (orig_mouse_code & MOUSE_ALT)
4583 modifiers |= MOD_MASK_ALT;
4584 if (orig_num_clicks == 2)
4585 modifiers |= MOD_MASK_2CLICK;
4586 else if (orig_num_clicks == 3)
4587 modifiers |= MOD_MASK_3CLICK;
4588 else if (orig_num_clicks == 4)
4589 modifiers |= MOD_MASK_4CLICK;
4590
4591 /* Add the modifier codes to our string */
4592 if (modifiers != 0)
4593 {
4594 string[new_slen++] = K_SPECIAL;
4595 string[new_slen++] = (int)KS_MODIFIER;
4596 string[new_slen++] = modifiers;
4597 }
4598
4599 /* Work out our pseudo mouse event */
4600 key_name[0] = (int)KS_EXTRA;
4601 if (wheel_code != 0)
4602 key_name[1] = (wheel_code & 1)
4603 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
4604 else
4605 key_name[1] = get_pseudo_mouse_code(current_button,
4606 is_click, is_drag);
4607 }
4608#endif /* FEAT_MOUSE */
4609
4610#ifdef FEAT_GUI
4611 /*
4612 * If using the GUI, then we get menu and scrollbar events.
4613 *
4614 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
4615 * four bytes which are to be taken as a pointer to the vimmenu_T
4616 * structure.
4617 *
4618 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
4619 * by one byte representing the scrollbar number, and then four bytes
4620 * representing a long_u which is the new value of the scrollbar.
4621 *
4622 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
4623 * KE_FILLER followed by four bytes representing a long_u which is the
4624 * new value of the scrollbar.
4625 */
4626# ifdef FEAT_MENU
4627 else if (key_name[0] == (int)KS_MENU)
4628 {
4629 long_u val;
4630
4631 num_bytes = get_long_from_buf(tp + slen, &val);
4632 if (num_bytes == -1)
4633 return -1;
4634 current_menu = (vimmenu_T *)val;
4635 slen += num_bytes;
4636 }
4637# endif
4638# ifndef USE_ON_FLY_SCROLL
4639 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
4640 {
4641 long_u val;
4642
4643 /* Get the last scrollbar event in the queue of the same type */
4644 j = 0;
4645 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
4646 && tp[j + 2] != NUL; ++i)
4647 {
4648 j += 3;
4649 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
4650 if (num_bytes == -1)
4651 break;
4652 if (i == 0)
4653 current_scrollbar = (int)bytes[0];
4654 else if (current_scrollbar != (int)bytes[0])
4655 break;
4656 j += num_bytes;
4657 num_bytes = get_long_from_buf(tp + j, &val);
4658 if (num_bytes == -1)
4659 break;
4660 scrollbar_value = val;
4661 j += num_bytes;
4662 slen = j;
4663 }
4664 if (i == 0) /* not enough characters to make one */
4665 return -1;
4666 }
4667 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
4668 {
4669 long_u val;
4670
4671 /* Get the last horiz. scrollbar event in the queue */
4672 j = 0;
4673 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
4674 && tp[j + 2] != NUL; ++i)
4675 {
4676 j += 3;
4677 num_bytes = get_long_from_buf(tp + j, &val);
4678 if (num_bytes == -1)
4679 break;
4680 scrollbar_value = val;
4681 j += num_bytes;
4682 slen = j;
4683 }
4684 if (i == 0) /* not enough characters to make one */
4685 return -1;
4686 }
4687# endif /* !USE_ON_FLY_SCROLL */
4688#endif /* FEAT_GUI */
4689
4690 /* Finally, add the special key code to our string */
4691 if (key_name[0] == KS_KEY)
4692 string[new_slen++] = key_name[1]; /* from ":set <M-b>=xx" */
4693 else
4694 {
4695 string[new_slen++] = K_SPECIAL;
4696 string[new_slen++] = key_name[0];
4697 string[new_slen++] = key_name[1];
4698 }
4699 string[new_slen] = NUL;
4700 extra = new_slen - slen;
4701 if (buf == NULL)
4702 {
4703 if (extra < 0)
4704 /* remove matched chars, taking care of noremap */
4705 del_typebuf(-extra, offset);
4706 else if (extra > 0)
4707 /* insert the extra space we need */
4708 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
4709
4710 /*
4711 * Careful: del_typebuf() and ins_typebuf() may have reallocated
4712 * typebuf.tb_buf[]!
4713 */
4714 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
4715 (size_t)new_slen);
4716 }
4717 else
4718 {
4719 if (extra < 0)
4720 /* remove matched characters */
4721 mch_memmove(buf + offset, buf + offset - extra,
4722 (size_t)(buflen + offset + extra));
4723 else if (extra > 0)
4724 /* insert the extra space we need */
4725 mch_memmove(buf + offset + extra, buf + offset,
4726 (size_t)(buflen - offset));
4727 mch_memmove(buf + offset, string, (size_t)new_slen);
4728 }
4729 return (len + extra + offset);
4730 }
4731
4732 return 0; /* no match found */
4733}
4734
4735/*
4736 * Replace any terminal code strings in from[] with the equivalent internal
4737 * vim representation. This is used for the "from" and "to" part of a
4738 * mapping, and the "to" part of a menu command.
4739 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
4740 * '<'.
4741 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
4742 *
4743 * The replacement is done in result[] and finally copied into allocated
4744 * memory. If this all works well *bufp is set to the allocated memory and a
4745 * pointer to it is returned. If something fails *bufp is set to NULL and from
4746 * is returned.
4747 *
4748 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
4749 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
4750 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
4751 * instead of a CTRL-V.
4752 */
4753 char_u *
4754replace_termcodes(from, bufp, from_part, do_lt)
4755 char_u *from;
4756 char_u **bufp;
4757 int from_part;
4758 int do_lt; /* also translate <lt> */
4759{
4760 int i;
4761 int slen;
4762 int key;
4763 int dlen = 0;
4764 char_u *src;
4765 int do_backslash; /* backslash is a special character */
4766 int do_special; /* recognize <> key codes */
4767 int do_key_code; /* recognize raw key codes */
4768 char_u *result; /* buffer for resulting string */
4769
4770 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
4771 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL);
4772 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
4773
4774 /*
4775 * Allocate space for the translation. Worst case a single character is
4776 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
4777 */
4778 result = alloc((unsigned)STRLEN(from) * 6 + 1);
4779 if (result == NULL) /* out of memory */
4780 {
4781 *bufp = NULL;
4782 return from;
4783 }
4784
4785 src = from;
4786
4787 /*
4788 * Check for #n at start only: function key n
4789 */
4790 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
4791 {
4792 result[dlen++] = K_SPECIAL;
4793 result[dlen++] = 'k';
4794 if (src[1] == '0')
4795 result[dlen++] = ';'; /* #0 is F10 is "k;" */
4796 else
4797 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
4798 src += 2;
4799 }
4800
4801 /*
4802 * Copy each byte from *from to result[dlen]
4803 */
4804 while (*src != NUL)
4805 {
4806 /*
4807 * If 'cpoptions' does not contain '<', check for special key codes,
4808 * like "<C-S-MouseLeft>"
4809 */
4810 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
4811 {
4812#ifdef FEAT_EVAL
4813 /*
4814 * Replace <SID> by K_SNR <script-nr> _.
4815 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
4816 */
4817 if (STRNICMP(src, "<SID>", 5) == 0)
4818 {
4819 if (current_SID <= 0)
4820 EMSG(_(e_usingsid));
4821 else
4822 {
4823 src += 5;
4824 result[dlen++] = K_SPECIAL;
4825 result[dlen++] = (int)KS_EXTRA;
4826 result[dlen++] = (int)KE_SNR;
4827 sprintf((char *)result + dlen, "%ld", (long)current_SID);
4828 dlen += (int)STRLEN(result + dlen);
4829 result[dlen++] = '_';
4830 continue;
4831 }
4832 }
4833#endif
4834
4835 slen = trans_special(&src, result + dlen, TRUE);
4836 if (slen)
4837 {
4838 dlen += slen;
4839 continue;
4840 }
4841 }
4842
4843 /*
4844 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
4845 * Note that this is also checked after replacing the <> form.
4846 * Single character codes are NOT replaced (e.g. ^H or DEL), because
4847 * it could be a character in the file.
4848 */
4849 if (do_key_code)
4850 {
4851 i = find_term_bykeys(src);
4852 if (i >= 0)
4853 {
4854 result[dlen++] = K_SPECIAL;
4855 result[dlen++] = termcodes[i].name[0];
4856 result[dlen++] = termcodes[i].name[1];
4857 src += termcodes[i].len;
4858 /* If terminal code matched, continue after it. */
4859 continue;
4860 }
4861 }
4862
4863#ifdef FEAT_EVAL
4864 if (do_special)
4865 {
4866 char_u *p, *s, len;
4867
4868 /*
4869 * Replace <Leader> by the value of "mapleader".
4870 * Replace <LocalLeader> by the value of "maplocalleader".
4871 * If "mapleader" or "maplocalleader" isn't set use a backslash.
4872 */
4873 if (STRNICMP(src, "<Leader>", 8) == 0)
4874 {
4875 len = 8;
4876 p = get_var_value((char_u *)"g:mapleader");
4877 }
4878 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
4879 {
4880 len = 13;
4881 p = get_var_value((char_u *)"g:maplocalleader");
4882 }
4883 else
4884 {
4885 len = 0;
4886 p = NULL;
4887 }
4888 if (len != 0)
4889 {
4890 /* Allow up to 8 * 6 characters for "mapleader". */
4891 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
4892 s = (char_u *)"\\";
4893 else
4894 s = p;
4895 while (*s != NUL)
4896 result[dlen++] = *s++;
4897 src += len;
4898 continue;
4899 }
4900 }
4901#endif
4902
4903 /*
4904 * Remove CTRL-V and ignore the next character.
4905 * For "from" side the CTRL-V at the end is included, for the "to"
4906 * part it is removed.
4907 * If 'cpoptions' does not contain 'B', also accept a backslash.
4908 */
4909 key = *src;
4910 if (key == Ctrl_V || (do_backslash && key == '\\'))
4911 {
4912 ++src; /* skip CTRL-V or backslash */
4913 if (*src == NUL)
4914 {
4915 if (from_part)
4916 result[dlen++] = key;
4917 break;
4918 }
4919 }
4920
4921#ifdef FEAT_MBYTE
4922 /* skip multibyte char correctly */
4923 for (i = (*mb_ptr2len_check)(src); i > 0; --i)
4924#endif
4925 {
4926 /*
4927 * If the character is K_SPECIAL, replace it with K_SPECIAL
4928 * KS_SPECIAL KE_FILLER.
4929 * If compiled with the GUI replace CSI with K_CSI.
4930 */
4931 if (*src == K_SPECIAL)
4932 {
4933 result[dlen++] = K_SPECIAL;
4934 result[dlen++] = KS_SPECIAL;
4935 result[dlen++] = KE_FILLER;
4936 }
4937# ifdef FEAT_GUI
4938 else if (*src == CSI)
4939 {
4940 result[dlen++] = K_SPECIAL;
4941 result[dlen++] = KS_EXTRA;
4942 result[dlen++] = (int)KE_CSI;
4943 }
4944# endif
4945 else
4946 result[dlen++] = *src;
4947 ++src;
4948 }
4949 }
4950 result[dlen] = NUL;
4951
4952 /*
4953 * Copy the new string to allocated memory.
4954 * If this fails, just return from.
4955 */
4956 if ((*bufp = vim_strsave(result)) != NULL)
4957 from = *bufp;
4958 vim_free(result);
4959 return from;
4960}
4961
4962/*
4963 * Find a termcode with keys 'src' (must be NUL terminated).
4964 * Return the index in termcodes[], or -1 if not found.
4965 */
4966 int
4967find_term_bykeys(src)
4968 char_u *src;
4969{
4970 int i;
4971 int slen;
4972
4973 for (i = 0; i < tc_len; ++i)
4974 {
4975 slen = termcodes[i].len;
4976 if (slen > 1 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
4977 return i;
4978 }
4979 return -1;
4980}
4981
4982/*
4983 * Gather the first characters in the terminal key codes into a string.
4984 * Used to speed up check_termcode().
4985 */
4986 static void
4987gather_termleader()
4988{
4989 int i;
4990 int len = 0;
4991
4992#ifdef FEAT_GUI
4993 if (gui.in_use)
4994 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
4995#endif
4996#ifdef FEAT_TERMRESPONSE
4997 if (check_for_codes)
4998 termleader[len++] = DCS; /* the termcode response starts with DCS
4999 in 8-bit mode */
5000#endif
5001 termleader[len] = NUL;
5002
5003 for (i = 0; i < tc_len; ++i)
5004 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5005 {
5006 termleader[len++] = termcodes[i].code[0];
5007 termleader[len] = NUL;
5008 }
5009
5010 need_gather = FALSE;
5011}
5012
5013/*
5014 * Show all termcodes (for ":set termcap")
5015 * This code looks a lot like showoptions(), but is different.
5016 */
5017 void
5018show_termcodes()
5019{
5020 int col;
5021 int *items;
5022 int item_count;
5023 int run;
5024 int row, rows;
5025 int cols;
5026 int i;
5027 int len;
5028
5029#define INC 27 /* try to make three columns */
5030#define GAP 2 /* spaces between columns */
5031
5032 if (tc_len == 0) /* no terminal codes (must be GUI) */
5033 return;
5034 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5035 if (items == NULL)
5036 return;
5037
5038 /* Highlight title */
5039 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5040
5041 /*
5042 * do the loop two times:
5043 * 1. display the short items (non-strings and short strings)
5044 * 2. display the long items (strings)
5045 */
5046 for (run = 1; run <= 2 && !got_int; ++run)
5047 {
5048 /*
5049 * collect the items in items[]
5050 */
5051 item_count = 0;
5052 for (i = 0; i < tc_len; i++)
5053 {
5054 len = show_one_termcode(termcodes[i].name,
5055 termcodes[i].code, FALSE);
5056 if ((len <= INC - GAP && run == 1) || (len > INC - GAP && run == 2))
5057 items[item_count++] = i;
5058 }
5059
5060 /*
5061 * display the items
5062 */
5063 if (run == 1)
5064 {
5065 cols = (Columns + GAP) / INC;
5066 if (cols == 0)
5067 cols = 1;
5068 rows = (item_count + cols - 1) / cols;
5069 }
5070 else /* run == 2 */
5071 rows = item_count;
5072 for (row = 0; row < rows && !got_int; ++row)
5073 {
5074 msg_putchar('\n'); /* go to next line */
5075 if (got_int) /* 'q' typed in more */
5076 break;
5077 col = 0;
5078 for (i = row; i < item_count; i += rows)
5079 {
5080 msg_col = col; /* make columns */
5081 show_one_termcode(termcodes[items[i]].name,
5082 termcodes[items[i]].code, TRUE);
5083 col += INC;
5084 }
5085 out_flush();
5086 ui_breakcheck();
5087 }
5088 }
5089 vim_free(items);
5090}
5091
5092/*
5093 * Show one termcode entry.
5094 * Output goes into IObuff[]
5095 */
5096 int
5097show_one_termcode(name, code, printit)
5098 char_u *name;
5099 char_u *code;
5100 int printit;
5101{
5102 char_u *p;
5103 int len;
5104
5105 if (name[0] > '~')
5106 {
5107 IObuff[0] = ' ';
5108 IObuff[1] = ' ';
5109 IObuff[2] = ' ';
5110 IObuff[3] = ' ';
5111 }
5112 else
5113 {
5114 IObuff[0] = 't';
5115 IObuff[1] = '_';
5116 IObuff[2] = name[0];
5117 IObuff[3] = name[1];
5118 }
5119 IObuff[4] = ' ';
5120
5121 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5122 if (p[1] != 't')
5123 STRCPY(IObuff + 5, p);
5124 else
5125 IObuff[5] = NUL;
5126 len = (int)STRLEN(IObuff);
5127 do
5128 IObuff[len++] = ' ';
5129 while (len < 17);
5130 IObuff[len] = NUL;
5131 if (code == NULL)
5132 len += 4;
5133 else
5134 len += vim_strsize(code);
5135
5136 if (printit)
5137 {
5138 msg_puts(IObuff);
5139 if (code == NULL)
5140 msg_puts((char_u *)"NULL");
5141 else
5142 msg_outtrans(code);
5143 }
5144 return len;
5145}
5146
5147#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5148/*
5149 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5150 * termcap codes from the terminal itself.
5151 * We get them one by one to avoid a very long response string.
5152 */
5153static int xt_index_in = 0;
5154static int xt_index_out = 0;
5155
5156 static void
5157req_codes_from_term()
5158{
5159 xt_index_out = 0;
5160 xt_index_in = 0;
5161 req_more_codes_from_term();
5162}
5163
5164 static void
5165req_more_codes_from_term()
5166{
5167 char buf[11];
5168 int old_idx = xt_index_out;
5169
5170 /* Don't do anything when going to exit. */
5171 if (exiting)
5172 return;
5173
5174 /* Send up to 10 more requests out than we received. Avoid sending too
5175 * many, there can be a buffer overflow somewhere. */
5176 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5177 {
5178 sprintf(buf, "\033P+q%02x%02x\033\\",
5179 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5180 out_str_nf((char_u *)buf);
5181 ++xt_index_out;
5182 }
5183
5184 /* Send the codes out right away. */
5185 if (xt_index_out != old_idx)
5186 out_flush();
5187}
5188
5189/*
5190 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5191 * A "0" instead of the "1" indicates a code that isn't supported.
5192 * Both <name> and <string> are encoded in hex.
5193 * "code" points to the "0" or "1".
5194 */
5195 static void
5196got_code_from_term(code, len)
5197 char_u *code;
5198 int len;
5199{
5200#define XT_LEN 100
5201 char_u name[3];
5202 char_u str[XT_LEN];
5203 int i;
5204 int j = 0;
5205 int c;
5206
5207 /* A '1' means the code is supported, a '0' means it isn't.
5208 * When half the length is > XT_LEN we can't use it.
5209 * Our names are currently all 2 characters. */
5210 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5211 {
5212 /* Get the name from the response and find it in the table. */
5213 name[0] = hexhex2nr(code + 3);
5214 name[1] = hexhex2nr(code + 5);
5215 name[2] = NUL;
5216 for (i = 0; key_names[i] != NULL; ++i)
5217 {
5218 if (STRCMP(key_names[i], name) == 0)
5219 {
5220 xt_index_in = i;
5221 break;
5222 }
5223 }
5224 if (key_names[i] != NULL)
5225 {
5226 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5227 str[j++] = c;
5228 str[j] = NUL;
5229 if (name[0] == 'C' && name[1] == 'o')
5230 {
5231 /* Color count is not a key code. */
5232 i = atoi((char *)str);
5233 if (i != t_colors)
5234 {
5235 /* Nr of colors changed, initialize highlighting and
5236 * redraw everything. */
5237 set_color_count(i);
5238 init_highlight(TRUE, FALSE);
5239 redraw_later(CLEAR);
5240 }
5241 }
5242 else
5243 {
5244 /* First delete any existing entry with the same code. */
5245 i = find_term_bykeys(str);
5246 if (i >= 0)
5247 del_termcode_idx(i);
5248 add_termcode(name, str, FALSE);
5249 }
5250 }
5251 }
5252
5253 /* May request more codes now that we received one. */
5254 ++xt_index_in;
5255 req_more_codes_from_term();
5256}
5257
5258/*
5259 * Check if there are any unanswered requests and deal with them.
5260 * This is called before starting an external program or getting direct
5261 * keyboard input. We don't want responses to be send to that program or
5262 * handled as typed text.
5263 */
5264 static void
5265check_for_codes_from_term()
5266{
5267 int c;
5268
5269 /* If no codes requested or all are answered, no need to wait. */
5270 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5271 return;
5272
5273 /* Vgetc() will check for and handle any response.
5274 * Keep calling vpeekc() until we don't get any responses. */
5275 ++no_mapping;
5276 ++allow_keys;
5277 for (;;)
5278 {
5279 c = vpeekc();
5280 if (c == NUL) /* nothing available */
5281 break;
5282
5283 /* If a response is recognized it's replaced with K_IGNORE, must read
5284 * it from the input stream. If there is no K_IGNORE we can't do
5285 * anything, break here (there might be some responses further on, but
5286 * we don't want to throw away any typed chars). */
5287 if (c != K_SPECIAL && c != K_IGNORE)
5288 break;
5289 c = vgetc();
5290 if (c != K_IGNORE)
5291 {
5292 vungetc(c);
5293 break;
5294 }
5295 }
5296 --no_mapping;
5297 --allow_keys;
5298}
5299#endif
5300
5301#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5302/*
5303 * Translate an internal mapping/abbreviation representation into the
5304 * corresponding external one recognized by :map/:abbrev commands;
5305 * respects the current B/k/< settings of 'cpoption'.
5306 *
5307 * This function is called when expanding mappings/abbreviations on the
5308 * command-line, and for building the "Ambiguous mapping..." error messæge.
5309 *
5310 * It uses a growarray to build the translation string since the
5311 * latter can be wider than the original description. The caller has to
5312 * free the string afterwards.
5313 *
5314 * Returns NULL when there is a problem.
5315 */
5316 char_u *
5317translate_mapping(str, expmap)
5318 char_u *str;
5319 int expmap; /* TRUE when expanding mappings on command-line */
5320{
5321 garray_T ga;
5322 int c;
5323 int modifiers;
5324 int cpo_bslash;
5325 int cpo_special;
5326 int cpo_keycode;
5327
5328 ga_init(&ga);
5329 ga.ga_itemsize = 1;
5330 ga.ga_growsize = 40;
5331
5332 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
5333 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
5334 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5335
5336 for (; *str; ++str)
5337 {
5338 c = *str;
5339 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5340 {
5341 modifiers = 0;
5342 if (str[1] == KS_MODIFIER)
5343 {
5344 str++;
5345 modifiers = *++str;
5346 c = *++str;
5347 }
5348 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
5349 {
5350 int i;
5351
5352 /* try to find special key in termcodes */
5353 for (i = 0; i < tc_len; ++i)
5354 if (termcodes[i].name[0] == str[1]
5355 && termcodes[i].name[1] == str[2])
5356 break;
5357 if (i < tc_len)
5358 {
5359 ga_concat(&ga, termcodes[i].code);
5360 str += 2;
5361 continue; /* for (str) */
5362 }
5363 }
5364 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5365 {
5366 if (expmap && cpo_special)
5367 {
5368 ga_clear(&ga);
5369 return NULL;
5370 }
5371 c = TO_SPECIAL(str[1], str[2]);
5372 if (c == K_ZERO) /* display <Nul> as ^@ */
5373 c = NUL;
5374 str += 2;
5375 }
5376 if (IS_SPECIAL(c) || modifiers) /* special key */
5377 {
5378 if (expmap && cpo_special)
5379 {
5380 ga_clear(&ga);
5381 return NULL;
5382 }
5383 ga_concat(&ga, get_special_key_name(c, modifiers));
5384 continue; /* for (str) */
5385 }
5386 }
5387 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
5388 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
5389 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
5390 if (c)
5391 ga_append(&ga, c);
5392 }
5393 ga_append(&ga, NUL);
5394 return (char_u *)(ga.ga_data);
5395}
5396#endif
5397
5398#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
5399static char ksme_str[20];
5400static char ksmr_str[20];
5401static char ksmd_str[20];
5402
5403/*
5404 * For Win32 console: update termcap codes for existing console attributes.
5405 */
5406 void
5407update_tcap(attr)
5408 int attr;
5409{
5410 struct builtin_term *p;
5411
5412 p = find_builtin_term(DEFAULT_TERM);
5413 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
5414 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
5415 attr | 0x08); /* FOREGROUND_INTENSITY */
5416 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
5417 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
5418
5419 while (p->bt_string != NULL)
5420 {
5421 if (p->bt_entry == (int)KS_ME)
5422 p->bt_string = &ksme_str[0];
5423 else if (p->bt_entry == (int)KS_MR)
5424 p->bt_string = &ksmr_str[0];
5425 else if (p->bt_entry == (int)KS_MD)
5426 p->bt_string = &ksmd_str[0];
5427 ++p;
5428 }
5429}
5430#endif