comparison src/term.c @ 7:3fc0f57ecb91 v7.0001

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