blob: 5251d6b9a1df411d541399e577d558c0f1b843ba [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 *
11 * term.c: functions for controlling the terminal
12 *
13 * primitive termcap support for Amiga, MSDOS, and Win32 included
14 *
15 * NOTE: padding and variable substitution is not performed,
16 * when compiling without HAVE_TGETENT, we use tputs() and tgoto() dummies.
17 */
18
19/*
20 * Some systems have a prototype for tgetstr() with (char *) instead of
21 * (char **). This define removes that prototype. We include our own prototype
22 * below.
23 */
24
25#define tgetstr tgetstr_defined_wrong
26#include "vim.h"
27
28#ifdef HAVE_TGETENT
29# ifdef HAVE_TERMIOS_H
30# include <termios.h> /* seems to be required for some Linux */
31# endif
32# ifdef HAVE_TERMCAP_H
33# include <termcap.h>
34# endif
35
36/*
37 * A few linux systems define outfuntype in termcap.h to be used as the third
38 * argument for tputs().
39 */
40# ifdef VMS
41# define TPUTSFUNCAST
42# else
43# ifdef HAVE_OUTFUNTYPE
44# define TPUTSFUNCAST (outfuntype)
45# else
46# define TPUTSFUNCAST (int (*)())
47# endif
48# endif
49#endif
50
51#undef tgetstr
52
53/*
54 * Here are the builtin termcap entries. They are not stored as complete
Bram Moolenaare60acc12011-05-10 16:41:25 +020055 * structures with all entries, as such a structure is too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 *
57 * The entries are compact, therefore they normally are included even when
58 * HAVE_TGETENT is defined. When HAVE_TGETENT is defined, the builtin entries
59 * can be accessed with "builtin_amiga", "builtin_ansi", "builtin_debug", etc.
60 *
61 * Each termcap is a list of builtin_term structures. It always starts with
62 * KS_NAME, which separates the entries. See parse_builtin_tcap() for all
63 * details.
64 * bt_entry is either a KS_xxx code (>= 0), or a K_xxx code.
65 *
66 * Entries marked with "guessed" may be wrong.
67 */
68struct builtin_term
69{
70 int bt_entry;
71 char *bt_string;
72};
73
74/* start of keys that are not directly used by Vim but can be mapped */
75#define BT_EXTRA_KEYS 0x101
76
77static struct builtin_term *find_builtin_term __ARGS((char_u *name));
78static void parse_builtin_tcap __ARGS((char_u *s));
79static void term_color __ARGS((char_u *s, int n));
80static void gather_termleader __ARGS((void));
81#ifdef FEAT_TERMRESPONSE
82static void req_codes_from_term __ARGS((void));
83static void req_more_codes_from_term __ARGS((void));
84static void got_code_from_term __ARGS((char_u *code, int len));
85static void check_for_codes_from_term __ARGS((void));
86#endif
87#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +000088 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
89 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000090static int get_bytes_from_buf __ARGS((char_u *, char_u *, int));
91#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000092static void del_termcode_idx __ARGS((int idx));
93static int term_is_builtin __ARGS((char_u *name));
94static int term_7to8bit __ARGS((char_u *p));
95#ifdef FEAT_TERMRESPONSE
96static void switch_to_8bit __ARGS((void));
97#endif
98
99#ifdef HAVE_TGETENT
100static char_u *tgetent_error __ARGS((char_u *, char_u *));
101
102/*
103 * Here is our own prototype for tgetstr(), any prototypes from the include
104 * files have been disabled by the define at the start of this file.
105 */
106char *tgetstr __ARGS((char *, char **));
107
108# ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +0200109 /* Change this to "if 1" to debug what happens with termresponse. */
110# if 0
111# define DEBUG_TERMRESPONSE
112 static void log_tr(char *msg);
113# define LOG_TR(msg) log_tr(msg)
114# else
115# define LOG_TR(msg)
116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117/* Request Terminal Version status: */
118# define CRV_GET 1 /* send T_CRV when switched to RAW mode */
119# define CRV_SENT 2 /* did send T_CRV, waiting for answer */
120# define CRV_GOT 3 /* received T_CRV response */
121static int crv_status = CRV_GET;
Bram Moolenaar9584b312013-03-13 19:29:28 +0100122/* Request Cursor position report: */
123# define U7_GET 1 /* send T_U7 when switched to RAW mode */
124# define U7_SENT 2 /* did send T_U7, waiting for answer */
125# define U7_GOT 3 /* received T_U7 response */
126static int u7_status = U7_GET;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127# endif
128
129/*
130 * Don't declare these variables if termcap.h contains them.
131 * Autoconf checks if these variables should be declared extern (not all
132 * systems have them).
133 * Some versions define ospeed to be speed_t, but that is incompatible with
134 * BSD, where ospeed is short and speed_t is long.
135 */
136# ifndef HAVE_OSPEED
137# ifdef OSPEED_EXTERN
138extern short ospeed;
139# else
140short ospeed;
141# endif
142# endif
143# ifndef HAVE_UP_BC_PC
144# ifdef UP_BC_PC_EXTERN
145extern char *UP, *BC, PC;
146# else
147char *UP, *BC, PC;
148# endif
149# endif
150
151# define TGETSTR(s, p) vim_tgetstr((s), (p))
152# define TGETENT(b, t) tgetent((char *)(b), (char *)(t))
153static char_u *vim_tgetstr __ARGS((char *s, char_u **pp));
154#endif /* HAVE_TGETENT */
155
Bram Moolenaar90013c62014-05-22 18:14:31 +0200156#if defined(FEAT_TERMRESPONSE)
157static int xt_index_in = 0;
158static int xt_index_out = 0;
159#endif
160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161static int detected_8bit = FALSE; /* detected 8-bit terminal */
162
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000163static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164{
165
166#if defined(FEAT_GUI)
167/*
168 * GUI pseudo term-cap.
169 */
170 {(int)KS_NAME, "gui"},
171 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
172 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
173# ifdef TERMINFO
174 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
175# else
176 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
177# endif
178 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
179# ifdef TERMINFO
180 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
181 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
182# ifdef FEAT_VERTSPLIT
183 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
184# endif
185# else
186 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
187 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
188# ifdef FEAT_VERTSPLIT
189 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
190# endif
191# endif
192 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
193 /* attributes switched on with 'h', off with * 'H' */
194 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
195 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
196 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
197 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
198 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
199 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
200 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000201 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
202 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
204 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
205 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
206 {(int)KS_MS, "y"},
207 {(int)KS_UT, "y"},
208 {(int)KS_LE, "\b"}, /* cursor-left = BS */
209 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
210# ifdef TERMINFO
211 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
212# else
213 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
214# endif
215 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000216 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217#endif
218
219#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220
221# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
222/*
223 * Amiga console window, default for Amiga
224 */
225 {(int)KS_NAME, "amiga"},
226 {(int)KS_CE, "\033[K"},
227 {(int)KS_CD, "\033[J"},
228 {(int)KS_AL, "\033[L"},
229# ifdef TERMINFO
230 {(int)KS_CAL, "\033[%p1%dL"},
231# else
232 {(int)KS_CAL, "\033[%dL"},
233# endif
234 {(int)KS_DL, "\033[M"},
235# ifdef TERMINFO
236 {(int)KS_CDL, "\033[%p1%dM"},
237# else
238 {(int)KS_CDL, "\033[%dM"},
239# endif
240 {(int)KS_CL, "\014"},
241 {(int)KS_VI, "\033[0 p"},
242 {(int)KS_VE, "\033[1 p"},
243 {(int)KS_ME, "\033[0m"},
244 {(int)KS_MR, "\033[7m"},
245 {(int)KS_MD, "\033[1m"},
246 {(int)KS_SE, "\033[0m"},
247 {(int)KS_SO, "\033[33m"},
248 {(int)KS_US, "\033[4m"},
249 {(int)KS_UE, "\033[0m"},
250 {(int)KS_CZH, "\033[3m"},
251 {(int)KS_CZR, "\033[0m"},
252#if defined(__MORPHOS__) || defined(__AROS__)
253 {(int)KS_CCO, "8"}, /* allow 8 colors */
254# ifdef TERMINFO
255 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
256 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
257# else
258 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
259 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
260# endif
261 {(int)KS_OP, "\033[m"}, /* reset colors */
262#endif
263 {(int)KS_MS, "y"},
264 {(int)KS_UT, "y"}, /* guessed */
265 {(int)KS_LE, "\b"},
266# ifdef TERMINFO
267 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
268# else
269 {(int)KS_CM, "\033[%i%d;%dH"},
270# endif
271#if defined(__MORPHOS__)
272 {(int)KS_SR, "\033M"},
273#endif
274# ifdef TERMINFO
275 {(int)KS_CRI, "\033[%p1%dC"},
276# else
277 {(int)KS_CRI, "\033[%dC"},
278# endif
279 {K_UP, "\233A"},
280 {K_DOWN, "\233B"},
281 {K_LEFT, "\233D"},
282 {K_RIGHT, "\233C"},
283 {K_S_UP, "\233T"},
284 {K_S_DOWN, "\233S"},
285 {K_S_LEFT, "\233 A"},
286 {K_S_RIGHT, "\233 @"},
287 {K_S_TAB, "\233Z"},
288 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
289 {K_F2, "\233\061~"},
290 {K_F3, "\233\062~"},
291 {K_F4, "\233\063~"},
292 {K_F5, "\233\064~"},
293 {K_F6, "\233\065~"},
294 {K_F7, "\233\066~"},
295 {K_F8, "\233\067~"},
296 {K_F9, "\233\070~"},
297 {K_F10, "\233\071~"},
298 {K_S_F1, "\233\061\060~"},
299 {K_S_F2, "\233\061\061~"},
300 {K_S_F3, "\233\061\062~"},
301 {K_S_F4, "\233\061\063~"},
302 {K_S_F5, "\233\061\064~"},
303 {K_S_F6, "\233\061\065~"},
304 {K_S_F7, "\233\061\066~"},
305 {K_S_F8, "\233\061\067~"},
306 {K_S_F9, "\233\061\070~"},
307 {K_S_F10, "\233\061\071~"},
308 {K_HELP, "\233?~"},
309 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
310 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
311 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
312 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
313 {K_END, "\233\064\065~"}, /* 101 key keyboard */
314
315 {BT_EXTRA_KEYS, ""},
316 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
317 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
318 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
319# endif
320
321# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
322/*
323 * almost standard ANSI terminal, default for bebox
324 */
325 {(int)KS_NAME, "beos-ansi"},
326 {(int)KS_CE, "\033[K"},
327 {(int)KS_CD, "\033[J"},
328 {(int)KS_AL, "\033[L"},
329# ifdef TERMINFO
330 {(int)KS_CAL, "\033[%p1%dL"},
331# else
332 {(int)KS_CAL, "\033[%dL"},
333# endif
334 {(int)KS_DL, "\033[M"},
335# ifdef TERMINFO
336 {(int)KS_CDL, "\033[%p1%dM"},
337# else
338 {(int)KS_CDL, "\033[%dM"},
339# endif
340#ifdef BEOS_PR_OR_BETTER
341# ifdef TERMINFO
342 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
343# else
344 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
345# endif
346#endif
347 {(int)KS_CL, "\033[H\033[2J"},
348#ifdef notyet
349 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
350 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
351#endif
352 {(int)KS_ME, "\033[m"}, /* normal mode */
353 {(int)KS_MR, "\033[7m"}, /* reverse */
354 {(int)KS_MD, "\033[1m"}, /* bold */
355 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
356 {(int)KS_SE, "\033[m"}, /* standout end */
357 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
358 {(int)KS_CZR, "\033[m"}, /* italic end */
359 {(int)KS_US, "\033[4m"}, /* underscore mode */
360 {(int)KS_UE, "\033[m"}, /* underscore end */
361 {(int)KS_CCO, "8"}, /* allow 8 colors */
362# ifdef TERMINFO
363 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
364 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
365# else
366 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
367 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
368# endif
369 {(int)KS_OP, "\033[m"}, /* reset colors */
370 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
371 {(int)KS_UT, "y"}, /* guessed */
372 {(int)KS_LE, "\b"},
373# ifdef TERMINFO
374 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
375# else
376 {(int)KS_CM, "\033[%i%d;%dH"},
377# endif
378 {(int)KS_SR, "\033M"},
379# ifdef TERMINFO
380 {(int)KS_CRI, "\033[%p1%dC"},
381# else
382 {(int)KS_CRI, "\033[%dC"},
383# endif
384#if defined(BEOS_DR8)
385 {(int)KS_DB, ""}, /* hack! see screen.c */
386#endif
387
388 {K_UP, "\033[A"},
389 {K_DOWN, "\033[B"},
390 {K_LEFT, "\033[D"},
391 {K_RIGHT, "\033[C"},
392# endif
393
394# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) || defined(__EMX__)
395/*
396 * standard ANSI terminal, default for unix
397 */
398 {(int)KS_NAME, "ansi"},
399 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
400 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
401# ifdef TERMINFO
402 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
403# else
404 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
405# endif
406 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
407# ifdef TERMINFO
408 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
409# else
410 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
411# endif
412 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
413 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
414 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
415 {(int)KS_MS, "y"},
416 {(int)KS_UT, "y"}, /* guessed */
417 {(int)KS_LE, "\b"},
418# ifdef TERMINFO
419 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
420# else
421 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
422# endif
423# ifdef TERMINFO
424 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
425# else
426 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
427# endif
428# endif
429
430# if defined(MSDOS) || defined(ALL_BUILTIN_TCAPS) || defined(__EMX__)
431/*
432 * These codes are valid when nansi.sys or equivalent has been installed.
433 * Function keys on a PC are preceded with a NUL. These are converted into
434 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
435 * CTRL-arrow is used instead of SHIFT-arrow.
436 */
437#ifdef __EMX__
438 {(int)KS_NAME, "os2ansi"},
439#else
440 {(int)KS_NAME, "pcansi"},
441 {(int)KS_DL, "\033[M"},
442 {(int)KS_AL, "\033[L"},
443#endif
444 {(int)KS_CE, "\033[K"},
445 {(int)KS_CL, "\033[2J"},
446 {(int)KS_ME, "\033[0m"},
447 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
448 {(int)KS_MD, "\033[1m"}, /* bold: white text */
449 {(int)KS_SE, "\033[0m"}, /* standout end */
450 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
451 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
452 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
453 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
454 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
455 {(int)KS_CCO, "8"}, /* allow 8 colors */
456# ifdef TERMINFO
457 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
458 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
459# else
460 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
461 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
462# endif
463 {(int)KS_OP, "\033[0m"}, /* reset colors */
464 {(int)KS_MS, "y"},
465 {(int)KS_UT, "y"}, /* guessed */
466 {(int)KS_LE, "\b"},
467# ifdef TERMINFO
468 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
469# else
470 {(int)KS_CM, "\033[%i%d;%dH"},
471# endif
472# ifdef TERMINFO
473 {(int)KS_CRI, "\033[%p1%dC"},
474# else
475 {(int)KS_CRI, "\033[%dC"},
476# endif
477 {K_UP, "\316H"},
478 {K_DOWN, "\316P"},
479 {K_LEFT, "\316K"},
480 {K_RIGHT, "\316M"},
481 {K_S_LEFT, "\316s"},
482 {K_S_RIGHT, "\316t"},
483 {K_F1, "\316;"},
484 {K_F2, "\316<"},
485 {K_F3, "\316="},
486 {K_F4, "\316>"},
487 {K_F5, "\316?"},
488 {K_F6, "\316@"},
489 {K_F7, "\316A"},
490 {K_F8, "\316B"},
491 {K_F9, "\316C"},
492 {K_F10, "\316D"},
493 {K_F11, "\316\205"}, /* guessed */
494 {K_F12, "\316\206"}, /* guessed */
495 {K_S_F1, "\316T"},
496 {K_S_F2, "\316U"},
497 {K_S_F3, "\316V"},
498 {K_S_F4, "\316W"},
499 {K_S_F5, "\316X"},
500 {K_S_F6, "\316Y"},
501 {K_S_F7, "\316Z"},
502 {K_S_F8, "\316["},
503 {K_S_F9, "\316\\"},
504 {K_S_F10, "\316]"},
505 {K_S_F11, "\316\207"}, /* guessed */
506 {K_S_F12, "\316\210"}, /* guessed */
507 {K_INS, "\316R"},
508 {K_DEL, "\316S"},
509 {K_HOME, "\316G"},
510 {K_END, "\316O"},
511 {K_PAGEDOWN, "\316Q"},
512 {K_PAGEUP, "\316I"},
513# endif
514
515# if defined(MSDOS)
516/*
517 * These codes are valid for the pc video. The entries that start with ESC |
518 * are translated into conio calls in os_msdos.c. Default for MSDOS.
519 */
520 {(int)KS_NAME, "pcterm"},
521 {(int)KS_CE, "\033|K"},
522 {(int)KS_AL, "\033|L"},
523 {(int)KS_DL, "\033|M"},
524# ifdef TERMINFO
525 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},
526# ifdef FEAT_VERTSPLIT
527 {(int)KS_CSV, "\033|%i%p1%d;%p2%dV"},
528# endif
529# else
530 {(int)KS_CS, "\033|%i%d;%dr"},
531# ifdef FEAT_VERTSPLIT
532 {(int)KS_CSV, "\033|%i%d;%dV"},
533# endif
534# endif
535 {(int)KS_CL, "\033|J"},
536 {(int)KS_ME, "\033|0m"}, /* normal */
537 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgrey */
538 {(int)KS_MD, "\033|15m"}, /* bold: white text */
539 {(int)KS_SE, "\033|0m"}, /* standout end */
540 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
541 {(int)KS_CZH, "\033|225m"}, /* italic mode: blue text on yellow */
542 {(int)KS_CZR, "\033|0m"}, /* italic mode end */
543 {(int)KS_US, "\033|67m"}, /* underscore mode: cyan text on red */
544 {(int)KS_UE, "\033|0m"}, /* underscore mode end */
545 {(int)KS_CCO, "16"}, /* allow 16 colors */
546# ifdef TERMINFO
547 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
548 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
549# else
550 {(int)KS_CAB, "\033|%db"}, /* set background color */
551 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
552# endif
553 {(int)KS_MS, "y"},
554 {(int)KS_UT, "y"},
555 {(int)KS_LE, "\b"},
556# ifdef TERMINFO
557 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},
558# else
559 {(int)KS_CM, "\033|%i%d;%dH"},
560# endif
561#ifdef DJGPP
562 {(int)KS_VB, "\033|B"}, /* visual bell */
563#endif
564 {K_UP, "\316H"},
565 {K_DOWN, "\316P"},
566 {K_LEFT, "\316K"},
567 {K_RIGHT, "\316M"},
568 {K_S_LEFT, "\316s"},
569 {K_S_RIGHT, "\316t"},
570 {K_S_TAB, "\316\017"},
571 {K_F1, "\316;"},
572 {K_F2, "\316<"},
573 {K_F3, "\316="},
574 {K_F4, "\316>"},
575 {K_F5, "\316?"},
576 {K_F6, "\316@"},
577 {K_F7, "\316A"},
578 {K_F8, "\316B"},
579 {K_F9, "\316C"},
580 {K_F10, "\316D"},
581 {K_F11, "\316\205"},
582 {K_F12, "\316\206"},
583 {K_S_F1, "\316T"},
584 {K_S_F2, "\316U"},
585 {K_S_F3, "\316V"},
586 {K_S_F4, "\316W"},
587 {K_S_F5, "\316X"},
588 {K_S_F6, "\316Y"},
589 {K_S_F7, "\316Z"},
590 {K_S_F8, "\316["},
591 {K_S_F9, "\316\\"},
592 {K_S_F10, "\316]"},
593 {K_S_F11, "\316\207"},
594 {K_S_F12, "\316\210"},
595 {K_INS, "\316R"},
596 {K_DEL, "\316S"},
597 {K_HOME, "\316G"},
598 {K_END, "\316O"},
599 {K_PAGEDOWN, "\316Q"},
600 {K_PAGEUP, "\316I"},
601 {K_KPLUS, "\316N"},
602 {K_KMINUS, "\316J"},
603 {K_KMULTIPLY, "\3167"},
604 {K_K0, "\316\332"},
605 {K_K1, "\316\336"},
606 {K_K2, "\316\342"},
607 {K_K3, "\316\346"},
608 {K_K4, "\316\352"},
609 {K_K5, "\316\356"},
610 {K_K6, "\316\362"},
611 {K_K7, "\316\366"},
612 {K_K8, "\316\372"},
613 {K_K9, "\316\376"},
614# endif
615
616# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS) || defined(__EMX__)
617/*
618 * These codes are valid for the Win32 Console . The entries that start with
619 * ESC | are translated into console calls in os_win32.c. The function keys
620 * are also translated in os_win32.c.
621 */
622 {(int)KS_NAME, "win32"},
623 {(int)KS_CE, "\033|K"}, /* clear to end of line */
624 {(int)KS_AL, "\033|L"}, /* add new blank line */
625# ifdef TERMINFO
626 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
627# else
628 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
629# endif
630 {(int)KS_DL, "\033|M"}, /* delete line */
631# ifdef TERMINFO
632 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
633# else
634 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
635# endif
636 {(int)KS_CL, "\033|J"}, /* clear screen */
637 {(int)KS_CD, "\033|j"}, /* clear to end of display */
638 {(int)KS_VI, "\033|v"}, /* cursor invisible */
639 {(int)KS_VE, "\033|V"}, /* cursor visible */
640
641 {(int)KS_ME, "\033|0m"}, /* normal */
642 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
643 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
644#if 1
645 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
646 {(int)KS_SE, "\033|0m"}, /* standout end */
647#else
648 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
649 {(int)KS_SE, "\033|f"}, /* standout end */
650#endif
651 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
652 {(int)KS_CZR, "\033|0m"}, /* italic end */
653 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
654 {(int)KS_UE, "\033|0m"}, /* underscore end */
655 {(int)KS_CCO, "16"}, /* allow 16 colors */
656# ifdef TERMINFO
657 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
658 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
659# else
660 {(int)KS_CAB, "\033|%db"}, /* set background color */
661 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
662# endif
663
664 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
665 {(int)KS_UT, "y"},
666 {(int)KS_LE, "\b"},
667# ifdef TERMINFO
668 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
669# else
670 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
671# endif
672 {(int)KS_VB, "\033|B"}, /* visual bell */
673 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
674 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
675# ifdef TERMINFO
676 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
677# else
678 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
679# endif
680
681 {K_UP, "\316H"},
682 {K_DOWN, "\316P"},
683 {K_LEFT, "\316K"},
684 {K_RIGHT, "\316M"},
685 {K_S_UP, "\316\304"},
686 {K_S_DOWN, "\316\317"},
687 {K_S_LEFT, "\316\311"},
688 {K_C_LEFT, "\316s"},
689 {K_S_RIGHT, "\316\313"},
690 {K_C_RIGHT, "\316t"},
691 {K_S_TAB, "\316\017"},
692 {K_F1, "\316;"},
693 {K_F2, "\316<"},
694 {K_F3, "\316="},
695 {K_F4, "\316>"},
696 {K_F5, "\316?"},
697 {K_F6, "\316@"},
698 {K_F7, "\316A"},
699 {K_F8, "\316B"},
700 {K_F9, "\316C"},
701 {K_F10, "\316D"},
702 {K_F11, "\316\205"},
703 {K_F12, "\316\206"},
704 {K_S_F1, "\316T"},
705 {K_S_F2, "\316U"},
706 {K_S_F3, "\316V"},
707 {K_S_F4, "\316W"},
708 {K_S_F5, "\316X"},
709 {K_S_F6, "\316Y"},
710 {K_S_F7, "\316Z"},
711 {K_S_F8, "\316["},
712 {K_S_F9, "\316\\"},
713 {K_S_F10, "\316]"},
714 {K_S_F11, "\316\207"},
715 {K_S_F12, "\316\210"},
716 {K_INS, "\316R"},
717 {K_DEL, "\316S"},
718 {K_HOME, "\316G"},
719 {K_S_HOME, "\316\302"},
720 {K_C_HOME, "\316w"},
721 {K_END, "\316O"},
722 {K_S_END, "\316\315"},
723 {K_C_END, "\316u"},
724 {K_PAGEDOWN, "\316Q"},
725 {K_PAGEUP, "\316I"},
726 {K_KPLUS, "\316N"},
727 {K_KMINUS, "\316J"},
728 {K_KMULTIPLY, "\316\067"},
729 {K_K0, "\316\332"},
730 {K_K1, "\316\336"},
731 {K_K2, "\316\342"},
732 {K_K3, "\316\346"},
733 {K_K4, "\316\352"},
734 {K_K5, "\316\356"},
735 {K_K6, "\316\362"},
736 {K_K7, "\316\366"},
737 {K_K8, "\316\372"},
738 {K_K9, "\316\376"},
739# endif
740
741# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
742/*
743 * VT320 is working as an ANSI terminal compatible DEC terminal.
744 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
745 * Note: K_F1...K_F5 are for internal use, should not be defined.
746 * TODO:- rewrite ESC[ codes to CSI
747 * - keyboard languages (CSI ? 26 n)
748 */
749 {(int)KS_NAME, "vt320"},
750 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
751 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
752# ifdef TERMINFO
753 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
754# else
755 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
756# endif
757 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
758# ifdef TERMINFO
759 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
760# else
761 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
762# endif
763 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000764 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
765 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
767 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000768 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
769 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
770 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
771 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
772 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
773 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
774 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
775 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
776 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
777 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {(int)KS_MS, "y"},
779 {(int)KS_UT, "y"},
780 {(int)KS_LE, "\b"},
781# ifdef TERMINFO
782 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
783 ESC_STR "[%i%p1%d;%p2%dH")},
784# else
785 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
786# endif
787# ifdef TERMINFO
788 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
789# else
790 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
791# endif
792 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
793 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
794 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
795 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000796 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
797 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
798 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
799 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
800 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
802 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
803 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
804 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
805 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000806 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
808 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
809 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
810 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
811 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
812 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
813 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
814 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
815 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
816 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
817 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
818 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
819 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
820 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
821 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
822 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
823 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
824 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
825 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
826 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
827 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
828# endif
829
830# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
831/*
832 * Ordinary vt52
833 */
834 {(int)KS_NAME, "vt52"},
835 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
836 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
837 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
838 {(int)KS_LE, "\b"},
839# ifdef __MINT__
840 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
841 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
842 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
843 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
844 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
845 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
846 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
847 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
848 {K_UP, IF_EB("\033A", ESC_STR "A")},
849 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
850 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
851 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
852 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
853 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
854 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
855 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
856 {K_F1, IF_EB("\033P", ESC_STR "P")},
857 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
858 {K_F3, IF_EB("\033R", ESC_STR "R")},
859 {K_F4, IF_EB("\033S", ESC_STR "S")},
860 {K_F5, IF_EB("\033T", ESC_STR "T")},
861 {K_F6, IF_EB("\033U", ESC_STR "U")},
862 {K_F7, IF_EB("\033V", ESC_STR "V")},
863 {K_F8, IF_EB("\033W", ESC_STR "W")},
864 {K_F9, IF_EB("\033X", ESC_STR "X")},
865 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
866 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
867 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
868 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
869 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
870 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
871 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
872 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
873 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
874 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
875 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
876 {K_INS, IF_EB("\033I", ESC_STR "I")},
877 {K_HOME, IF_EB("\033E", ESC_STR "E")},
878 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
879 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
880# else
881 {(int)KS_AL, IF_EB("\033T", ESC_STR "T")},
882 {(int)KS_DL, IF_EB("\033U", ESC_STR "U")},
883 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
884 {(int)KS_ME, IF_EB("\033SO", ESC_STR "SO")},
885 {(int)KS_MR, IF_EB("\033S2", ESC_STR "S2")},
886 {(int)KS_MS, "y"},
887# endif
888# endif
889
890# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) || defined(__EMX__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {(int)KS_NAME, "xterm"},
892 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
893 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
894# ifdef TERMINFO
895 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
896# else
897 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
898# endif
899 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
900# ifdef TERMINFO
901 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
902# else
903 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
904# endif
905# ifdef TERMINFO
906 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
907 ESC_STR "[%i%p1%d;%p2%dr")},
908# else
909 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
910# endif
911 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
912 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
913 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
914 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
915 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
916 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
917 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
918 {(int)KS_MS, "y"},
919 {(int)KS_UT, "y"},
920 {(int)KS_LE, "\b"},
921# ifdef TERMINFO
922 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
923 ESC_STR "[%i%p1%d;%p2%dH")},
924# else
925 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
926# endif
927 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
928# ifdef TERMINFO
929 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
930# else
931 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
932# endif
933 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
934 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
935# ifdef FEAT_XTERM_SAVE
936 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
937 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
938 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
939# endif
940 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
941 {(int)KS_CIE, "\007"},
942 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
943 {(int)KS_FS, "\007"},
944# ifdef TERMINFO
945 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
946 ESC_STR "[8;%p1%d;%p2%dt")},
947 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
948 ESC_STR "[3;%p1%d;%p2%dt")},
949# else
950 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
951 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
952# endif
953 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100954 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000955
956 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
957 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
958 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
959 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
960 /* An extra set of cursor keys for vt100 mode */
961 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
962 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
963 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
964 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000966 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
967 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
968 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
969 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000970 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
971 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
972 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
973 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
974 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
975 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
976 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
977 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
978 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
979 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
980 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
981 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000983 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
984 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
985 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
986 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000987 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
988 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000989 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000990 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000991 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000992 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000993 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
994 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000995 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000996 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000997 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000998 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
999 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001000 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
1001 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
1002 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
1003 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
1004 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
1005 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001006 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
1008 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001009 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
1010 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
1011 /* F14 and F15 are missing, because they send the same codes as the undo
1012 * and help key, although they don't work on all keyboards. */
1013 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
1014 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
1015 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1016 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1017 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1018
1019 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1020 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1021 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1022 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1023 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1024 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1025 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1026 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1027 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1028 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1029
1030 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1031 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1032 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1033 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1034 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1035 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1036 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037# endif
1038
1039# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1040/*
1041 * iris-ansi for Silicon Graphics machines.
1042 */
1043 {(int)KS_NAME, "iris-ansi"},
1044 {(int)KS_CE, "\033[K"},
1045 {(int)KS_CD, "\033[J"},
1046 {(int)KS_AL, "\033[L"},
1047# ifdef TERMINFO
1048 {(int)KS_CAL, "\033[%p1%dL"},
1049# else
1050 {(int)KS_CAL, "\033[%dL"},
1051# endif
1052 {(int)KS_DL, "\033[M"},
1053# ifdef TERMINFO
1054 {(int)KS_CDL, "\033[%p1%dM"},
1055# else
1056 {(int)KS_CDL, "\033[%dM"},
1057# endif
1058#if 0 /* The scroll region is not working as Vim expects. */
1059# ifdef TERMINFO
1060 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1061# else
1062 {(int)KS_CS, "\033[%i%d;%dr"},
1063# endif
1064#endif
1065 {(int)KS_CL, "\033[H\033[2J"},
1066 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1067 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1068 {(int)KS_TI, "\033[=6h"},
1069 {(int)KS_TE, "\033[=6l"},
1070 {(int)KS_SE, "\033[21;27m"},
1071 {(int)KS_SO, "\033[1;7m"},
1072 {(int)KS_ME, "\033[m"},
1073 {(int)KS_MR, "\033[7m"},
1074 {(int)KS_MD, "\033[1m"},
1075 {(int)KS_CCO, "8"}, /* allow 8 colors */
1076 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1077 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1078 {(int)KS_US, "\033[4m"}, /* underline on */
1079 {(int)KS_UE, "\033[24m"}, /* underline off */
1080# ifdef TERMINFO
1081 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1082 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1083 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1084 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1085# else
1086 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1087 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1088 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1089 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1090# endif
1091 {(int)KS_MS, "y"}, /* guessed */
1092 {(int)KS_UT, "y"}, /* guessed */
1093 {(int)KS_LE, "\b"},
1094# ifdef TERMINFO
1095 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1096# else
1097 {(int)KS_CM, "\033[%i%d;%dH"},
1098# endif
1099 {(int)KS_SR, "\033M"},
1100# ifdef TERMINFO
1101 {(int)KS_CRI, "\033[%p1%dC"},
1102# else
1103 {(int)KS_CRI, "\033[%dC"},
1104# endif
1105 {(int)KS_CIS, "\033P3.y"},
1106 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1107 {(int)KS_TS, "\033P1.y"},
1108 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1109# ifdef TERMINFO
1110 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1111 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1112# else
1113 {(int)KS_CWS, "\033[203;%d;%d/y"},
1114 {(int)KS_CWP, "\033[205;%d;%d/y"},
1115# endif
1116 {K_UP, "\033[A"},
1117 {K_DOWN, "\033[B"},
1118 {K_LEFT, "\033[D"},
1119 {K_RIGHT, "\033[C"},
1120 {K_S_UP, "\033[161q"},
1121 {K_S_DOWN, "\033[164q"},
1122 {K_S_LEFT, "\033[158q"},
1123 {K_S_RIGHT, "\033[167q"},
1124 {K_F1, "\033[001q"},
1125 {K_F2, "\033[002q"},
1126 {K_F3, "\033[003q"},
1127 {K_F4, "\033[004q"},
1128 {K_F5, "\033[005q"},
1129 {K_F6, "\033[006q"},
1130 {K_F7, "\033[007q"},
1131 {K_F8, "\033[008q"},
1132 {K_F9, "\033[009q"},
1133 {K_F10, "\033[010q"},
1134 {K_F11, "\033[011q"},
1135 {K_F12, "\033[012q"},
1136 {K_S_F1, "\033[013q"},
1137 {K_S_F2, "\033[014q"},
1138 {K_S_F3, "\033[015q"},
1139 {K_S_F4, "\033[016q"},
1140 {K_S_F5, "\033[017q"},
1141 {K_S_F6, "\033[018q"},
1142 {K_S_F7, "\033[019q"},
1143 {K_S_F8, "\033[020q"},
1144 {K_S_F9, "\033[021q"},
1145 {K_S_F10, "\033[022q"},
1146 {K_S_F11, "\033[023q"},
1147 {K_S_F12, "\033[024q"},
1148 {K_INS, "\033[139q"},
1149 {K_HOME, "\033[H"},
1150 {K_END, "\033[146q"},
1151 {K_PAGEUP, "\033[150q"},
1152 {K_PAGEDOWN, "\033[154q"},
1153# endif
1154
1155# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1156/*
1157 * for debugging
1158 */
1159 {(int)KS_NAME, "debug"},
1160 {(int)KS_CE, "[CE]"},
1161 {(int)KS_CD, "[CD]"},
1162 {(int)KS_AL, "[AL]"},
1163# ifdef TERMINFO
1164 {(int)KS_CAL, "[CAL%p1%d]"},
1165# else
1166 {(int)KS_CAL, "[CAL%d]"},
1167# endif
1168 {(int)KS_DL, "[DL]"},
1169# ifdef TERMINFO
1170 {(int)KS_CDL, "[CDL%p1%d]"},
1171# else
1172 {(int)KS_CDL, "[CDL%d]"},
1173# endif
1174# ifdef TERMINFO
1175 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1176# else
1177 {(int)KS_CS, "[%dCS%d]"},
1178# endif
1179# ifdef FEAT_VERTSPLIT
1180# ifdef TERMINFO
1181 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1182# else
1183 {(int)KS_CSV, "[%dCSV%d]"},
1184# endif
1185# endif
1186# ifdef TERMINFO
1187 {(int)KS_CAB, "[CAB%p1%d]"},
1188 {(int)KS_CAF, "[CAF%p1%d]"},
1189 {(int)KS_CSB, "[CSB%p1%d]"},
1190 {(int)KS_CSF, "[CSF%p1%d]"},
1191# else
1192 {(int)KS_CAB, "[CAB%d]"},
1193 {(int)KS_CAF, "[CAF%d]"},
1194 {(int)KS_CSB, "[CSB%d]"},
1195 {(int)KS_CSF, "[CSF%d]"},
1196# endif
1197 {(int)KS_OP, "[OP]"},
1198 {(int)KS_LE, "[LE]"},
1199 {(int)KS_CL, "[CL]"},
1200 {(int)KS_VI, "[VI]"},
1201 {(int)KS_VE, "[VE]"},
1202 {(int)KS_VS, "[VS]"},
1203 {(int)KS_ME, "[ME]"},
1204 {(int)KS_MR, "[MR]"},
1205 {(int)KS_MB, "[MB]"},
1206 {(int)KS_MD, "[MD]"},
1207 {(int)KS_SE, "[SE]"},
1208 {(int)KS_SO, "[SO]"},
1209 {(int)KS_UE, "[UE]"},
1210 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001211 {(int)KS_UCE, "[UCE]"},
1212 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {(int)KS_MS, "[MS]"},
1214 {(int)KS_UT, "[UT]"},
1215# ifdef TERMINFO
1216 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1217# else
1218 {(int)KS_CM, "[%dCM%d]"},
1219# endif
1220 {(int)KS_SR, "[SR]"},
1221# ifdef TERMINFO
1222 {(int)KS_CRI, "[CRI%p1%d]"},
1223# else
1224 {(int)KS_CRI, "[CRI%d]"},
1225# endif
1226 {(int)KS_VB, "[VB]"},
1227 {(int)KS_KS, "[KS]"},
1228 {(int)KS_KE, "[KE]"},
1229 {(int)KS_TI, "[TI]"},
1230 {(int)KS_TE, "[TE]"},
1231 {(int)KS_CIS, "[CIS]"},
1232 {(int)KS_CIE, "[CIE]"},
1233 {(int)KS_TS, "[TS]"},
1234 {(int)KS_FS, "[FS]"},
1235# ifdef TERMINFO
1236 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1237 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1238# else
1239 {(int)KS_CWS, "[%dCWS%d]"},
1240 {(int)KS_CWP, "[%dCWP%d]"},
1241# endif
1242 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001243 {(int)KS_U7, "[U7]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 {K_UP, "[KU]"},
1245 {K_DOWN, "[KD]"},
1246 {K_LEFT, "[KL]"},
1247 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001248 {K_XUP, "[xKU]"},
1249 {K_XDOWN, "[xKD]"},
1250 {K_XLEFT, "[xKL]"},
1251 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {K_S_UP, "[S-KU]"},
1253 {K_S_DOWN, "[S-KD]"},
1254 {K_S_LEFT, "[S-KL]"},
1255 {K_C_LEFT, "[C-KL]"},
1256 {K_S_RIGHT, "[S-KR]"},
1257 {K_C_RIGHT, "[C-KR]"},
1258 {K_F1, "[F1]"},
1259 {K_XF1, "[xF1]"},
1260 {K_F2, "[F2]"},
1261 {K_XF2, "[xF2]"},
1262 {K_F3, "[F3]"},
1263 {K_XF3, "[xF3]"},
1264 {K_F4, "[F4]"},
1265 {K_XF4, "[xF4]"},
1266 {K_F5, "[F5]"},
1267 {K_F6, "[F6]"},
1268 {K_F7, "[F7]"},
1269 {K_F8, "[F8]"},
1270 {K_F9, "[F9]"},
1271 {K_F10, "[F10]"},
1272 {K_F11, "[F11]"},
1273 {K_F12, "[F12]"},
1274 {K_S_F1, "[S-F1]"},
1275 {K_S_XF1, "[S-xF1]"},
1276 {K_S_F2, "[S-F2]"},
1277 {K_S_XF2, "[S-xF2]"},
1278 {K_S_F3, "[S-F3]"},
1279 {K_S_XF3, "[S-xF3]"},
1280 {K_S_F4, "[S-F4]"},
1281 {K_S_XF4, "[S-xF4]"},
1282 {K_S_F5, "[S-F5]"},
1283 {K_S_F6, "[S-F6]"},
1284 {K_S_F7, "[S-F7]"},
1285 {K_S_F8, "[S-F8]"},
1286 {K_S_F9, "[S-F9]"},
1287 {K_S_F10, "[S-F10]"},
1288 {K_S_F11, "[S-F11]"},
1289 {K_S_F12, "[S-F12]"},
1290 {K_HELP, "[HELP]"},
1291 {K_UNDO, "[UNDO]"},
1292 {K_BS, "[BS]"},
1293 {K_INS, "[INS]"},
1294 {K_KINS, "[KINS]"},
1295 {K_DEL, "[DEL]"},
1296 {K_KDEL, "[KDEL]"},
1297 {K_HOME, "[HOME]"},
1298 {K_S_HOME, "[C-HOME]"},
1299 {K_C_HOME, "[C-HOME]"},
1300 {K_KHOME, "[KHOME]"},
1301 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001302 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 {K_END, "[END]"},
1304 {K_S_END, "[C-END]"},
1305 {K_C_END, "[C-END]"},
1306 {K_KEND, "[KEND]"},
1307 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001308 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {K_PAGEUP, "[PAGEUP]"},
1310 {K_PAGEDOWN, "[PAGEDOWN]"},
1311 {K_KPAGEUP, "[KPAGEUP]"},
1312 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1313 {K_MOUSE, "[MOUSE]"},
1314 {K_KPLUS, "[KPLUS]"},
1315 {K_KMINUS, "[KMINUS]"},
1316 {K_KDIVIDE, "[KDIVIDE]"},
1317 {K_KMULTIPLY, "[KMULTIPLY]"},
1318 {K_KENTER, "[KENTER]"},
1319 {K_KPOINT, "[KPOINT]"},
1320 {K_K0, "[K0]"},
1321 {K_K1, "[K1]"},
1322 {K_K2, "[K2]"},
1323 {K_K3, "[K3]"},
1324 {K_K4, "[K4]"},
1325 {K_K5, "[K5]"},
1326 {K_K6, "[K6]"},
1327 {K_K7, "[K7]"},
1328 {K_K8, "[K8]"},
1329 {K_K9, "[K9]"},
1330# endif
1331
1332#endif /* NO_BUILTIN_TCAPS */
1333
1334/*
1335 * The most minimal terminal: only clear screen and cursor positioning
1336 * Always included.
1337 */
1338 {(int)KS_NAME, "dumb"},
1339 {(int)KS_CL, "\014"},
1340#ifdef TERMINFO
1341 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1342 ESC_STR "[%i%p1%d;%p2%dH")},
1343#else
1344 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1345#endif
1346
1347/*
1348 * end marker
1349 */
1350 {(int)KS_NAME, NULL}
1351
1352}; /* end of builtin_termcaps */
1353
1354/*
1355 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357#ifdef AMIGA
1358# define DEFAULT_TERM (char_u *)"amiga"
1359#endif
1360
1361#ifdef MSWIN
1362# define DEFAULT_TERM (char_u *)"win32"
1363#endif
1364
1365#ifdef MSDOS
1366# define DEFAULT_TERM (char_u *)"pcterm"
1367#endif
1368
1369#if defined(UNIX) && !defined(__MINT__)
1370# define DEFAULT_TERM (char_u *)"ansi"
1371#endif
1372
1373#ifdef __MINT__
1374# define DEFAULT_TERM (char_u *)"vt52"
1375#endif
1376
1377#ifdef __EMX__
1378# define DEFAULT_TERM (char_u *)"os2ansi"
1379#endif
1380
1381#ifdef VMS
1382# define DEFAULT_TERM (char_u *)"vt320"
1383#endif
1384
1385#ifdef __BEOS__
1386# undef DEFAULT_TERM
1387# define DEFAULT_TERM (char_u *)"beos-ansi"
1388#endif
1389
1390#ifndef DEFAULT_TERM
1391# define DEFAULT_TERM (char_u *)"dumb"
1392#endif
1393
1394/*
1395 * Term_strings contains currently used terminal output strings.
1396 * It is initialized with the default values by parse_builtin_tcap().
1397 * The values can be changed by setting the option with the same name.
1398 */
1399char_u *(term_strings[(int)KS_LAST + 1]);
1400
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001401static int need_gather = FALSE; /* need to fill termleader[] */
1402static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001404static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405#endif
1406
1407 static struct builtin_term *
1408find_builtin_term(term)
1409 char_u *term;
1410{
1411 struct builtin_term *p;
1412
1413 p = builtin_termcaps;
1414 while (p->bt_string != NULL)
1415 {
1416 if (p->bt_entry == (int)KS_NAME)
1417 {
1418#ifdef UNIX
1419 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1420 return p;
1421 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1422 return p;
1423 else
1424#endif
1425#ifdef VMS
1426 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1427 return p;
1428 else
1429#endif
1430 if (STRCMP(term, p->bt_string) == 0)
1431 return p;
1432 }
1433 ++p;
1434 }
1435 return p;
1436}
1437
1438/*
1439 * Parsing of the builtin termcap entries.
1440 * Caller should check if 'name' is a valid builtin term.
1441 * The terminal's name is not set, as this is already done in termcapinit().
1442 */
1443 static void
1444parse_builtin_tcap(term)
1445 char_u *term;
1446{
1447 struct builtin_term *p;
1448 char_u name[2];
1449 int term_8bit;
1450
1451 p = find_builtin_term(term);
1452 term_8bit = term_is_8bit(term);
1453
1454 /* Do not parse if builtin term not found */
1455 if (p->bt_string == NULL)
1456 return;
1457
1458 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1459 {
1460 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1461 {
1462 /* Only set the value if it wasn't set yet. */
1463 if (term_strings[p->bt_entry] == NULL
1464 || term_strings[p->bt_entry] == empty_option)
1465 {
1466 /* 8bit terminal: use CSI instead of <Esc>[ */
1467 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1468 {
1469 char_u *s, *t;
1470
1471 s = vim_strsave((char_u *)p->bt_string);
1472 if (s != NULL)
1473 {
1474 for (t = s; *t; ++t)
1475 if (term_7to8bit(t))
1476 {
1477 *t = term_7to8bit(t);
1478 STRCPY(t + 1, t + 2);
1479 }
1480 term_strings[p->bt_entry] = s;
1481 set_term_option_alloced(&term_strings[p->bt_entry]);
1482 }
1483 }
1484 else
1485 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1486 }
1487 }
1488 else
1489 {
1490 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1491 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1492 if (find_termcode(name) == NULL)
1493 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1494 }
1495 }
1496}
1497#if defined(HAVE_TGETENT) || defined(FEAT_TERMRESPONSE)
1498static void set_color_count __ARGS((int nr));
1499
1500/*
1501 * Set number of colors.
1502 * Store it as a number in t_colors.
1503 * Store it as a string in T_CCO (using nr_colors[]).
1504 */
1505 static void
1506set_color_count(nr)
1507 int nr;
1508{
1509 char_u nr_colors[20]; /* string for number of colors */
1510
1511 t_colors = nr;
1512 if (t_colors > 1)
1513 sprintf((char *)nr_colors, "%d", t_colors);
1514 else
1515 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001516 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517}
1518#endif
1519
1520#ifdef HAVE_TGETENT
1521static char *(key_names[]) =
1522{
1523#ifdef FEAT_TERMRESPONSE
1524 /* Do this one first, it may cause a screen redraw. */
1525 "Co",
1526#endif
1527 "ku", "kd", "kr", "kl",
1528# ifdef ARCHIE
1529 "su", "sd", /* Termcap code made up! */
1530# endif
1531 "#2", "#4", "%i", "*7",
1532 "k1", "k2", "k3", "k4", "k5", "k6",
1533 "k7", "k8", "k9", "k;", "F1", "F2",
1534 "%1", "&8", "kb", "kI", "kD", "kh",
1535 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1536 NULL
1537};
1538#endif
1539
1540/*
1541 * Set terminal options for terminal "term".
1542 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1543 *
1544 * While doing this, until ttest(), some options may be NULL, be careful.
1545 */
1546 int
1547set_termname(term)
1548 char_u *term;
1549{
1550 struct builtin_term *termp;
1551#ifdef HAVE_TGETENT
1552 int builtin_first = p_tbi;
1553 int try;
1554 int termcap_cleared = FALSE;
1555#endif
1556 int width = 0, height = 0;
1557 char_u *error_msg = NULL;
1558 char_u *bs_p, *del_p;
1559
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001560 /* In silect mode (ex -s) we don't use the 'term' option. */
1561 if (silent_mode)
1562 return OK;
1563
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 detected_8bit = FALSE; /* reset 8-bit detection */
1565
1566 if (term_is_builtin(term))
1567 {
1568 term += 8;
1569#ifdef HAVE_TGETENT
1570 builtin_first = 1;
1571#endif
1572 }
1573
1574/*
1575 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1576 * If builtin_first is TRUE:
1577 * 0. try builtin termcap
1578 * 1. try external termcap
1579 * 2. if both fail default to a builtin terminal
1580 * If builtin_first is FALSE:
1581 * 1. try external termcap
1582 * 2. try builtin termcap, if both fail default to a builtin terminal
1583 */
1584#ifdef HAVE_TGETENT
1585 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1586 {
1587 /*
1588 * Use external termcap
1589 */
1590 if (try == 1)
1591 {
1592 char_u *p;
1593 static char_u tstrbuf[TBUFSZ];
1594 int i;
1595 char_u tbuf[TBUFSZ];
1596 char_u *tp;
1597 static struct {
1598 enum SpecialKey dest; /* index in term_strings[] */
1599 char *name; /* termcap name for string */
1600 } string_names[] =
1601 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1602 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1603 {KS_CL, "cl"}, {KS_CD, "cd"},
1604 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1605 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1606 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1607 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001608 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1609 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1611 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1612 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1613 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1614 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1615 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1616 {KS_TS, "ts"}, {KS_FS, "fs"},
1617 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001618 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001619 {KS_U7, "u7"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {(enum SpecialKey)0, NULL}
1621 };
1622
1623 /*
1624 * If the external termcap does not have a matching entry, try the
1625 * builtin ones.
1626 */
1627 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1628 {
1629 tp = tstrbuf;
1630 if (!termcap_cleared)
1631 {
1632 clear_termoptions(); /* clear old options */
1633 termcap_cleared = TRUE;
1634 }
1635
1636 /* get output strings */
1637 for (i = 0; string_names[i].name != NULL; ++i)
1638 {
1639 if (term_str(string_names[i].dest) == NULL
1640 || term_str(string_names[i].dest) == empty_option)
1641 term_str(string_names[i].dest) =
1642 TGETSTR(string_names[i].name, &tp);
1643 }
1644
1645 /* tgetflag() returns 1 if the flag is present, 0 if not and
1646 * possibly -1 if the flag doesn't exist. */
1647 if ((T_MS == NULL || T_MS == empty_option)
1648 && tgetflag("ms") > 0)
1649 T_MS = (char_u *)"y";
1650 if ((T_XS == NULL || T_XS == empty_option)
1651 && tgetflag("xs") > 0)
1652 T_XS = (char_u *)"y";
1653 if ((T_DB == NULL || T_DB == empty_option)
1654 && tgetflag("db") > 0)
1655 T_DB = (char_u *)"y";
1656 if ((T_DA == NULL || T_DA == empty_option)
1657 && tgetflag("da") > 0)
1658 T_DA = (char_u *)"y";
1659 if ((T_UT == NULL || T_UT == empty_option)
1660 && tgetflag("ut") > 0)
1661 T_UT = (char_u *)"y";
1662
1663
1664 /*
1665 * get key codes
1666 */
1667 for (i = 0; key_names[i] != NULL; ++i)
1668 {
1669 if (find_termcode((char_u *)key_names[i]) == NULL)
1670 {
1671 p = TGETSTR(key_names[i], &tp);
1672 /* if cursor-left == backspace, ignore it (televideo
1673 * 925) */
1674 if (p != NULL
1675 && (*p != Ctrl_H
1676 || key_names[i][0] != 'k'
1677 || key_names[i][1] != 'l'))
1678 add_termcode((char_u *)key_names[i], p, FALSE);
1679 }
1680 }
1681
1682 if (height == 0)
1683 height = tgetnum("li");
1684 if (width == 0)
1685 width = tgetnum("co");
1686
1687 /*
1688 * Get number of colors (if not done already).
1689 */
1690 if (term_str(KS_CCO) == NULL
1691 || term_str(KS_CCO) == empty_option)
1692 set_color_count(tgetnum("Co"));
1693
1694# ifndef hpux
1695 BC = (char *)TGETSTR("bc", &tp);
1696 UP = (char *)TGETSTR("up", &tp);
1697 p = TGETSTR("pc", &tp);
1698 if (p)
1699 PC = *p;
1700# endif /* hpux */
1701 }
1702 }
1703 else /* try == 0 || try == 2 */
1704#endif /* HAVE_TGETENT */
1705 /*
1706 * Use builtin termcap
1707 */
1708 {
1709#ifdef HAVE_TGETENT
1710 /*
1711 * If builtin termcap was already used, there is no need to search
1712 * for the builtin termcap again, quit now.
1713 */
1714 if (try == 2 && builtin_first && termcap_cleared)
1715 break;
1716#endif
1717 /*
1718 * search for 'term' in builtin_termcaps[]
1719 */
1720 termp = find_builtin_term(term);
1721 if (termp->bt_string == NULL) /* did not find it */
1722 {
1723#ifdef HAVE_TGETENT
1724 /*
1725 * If try == 0, first try the external termcap. If that is not
1726 * found we'll get back here with try == 2.
1727 * If termcap_cleared is set we used the external termcap,
1728 * don't complain about not finding the term in the builtin
1729 * termcap.
1730 */
1731 if (try == 0) /* try external one */
1732 continue;
1733 if (termcap_cleared) /* found in external termcap */
1734 break;
1735#endif
1736
1737 mch_errmsg("\r\n");
1738 if (error_msg != NULL)
1739 {
1740 mch_errmsg((char *)error_msg);
1741 mch_errmsg("\r\n");
1742 }
1743 mch_errmsg("'");
1744 mch_errmsg((char *)term);
1745 mch_errmsg(_("' not known. Available builtin terminals are:"));
1746 mch_errmsg("\r\n");
1747 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1748 ++termp)
1749 {
1750 if (termp->bt_entry == (int)KS_NAME)
1751 {
1752#ifdef HAVE_TGETENT
1753 mch_errmsg(" builtin_");
1754#else
1755 mch_errmsg(" ");
1756#endif
1757 mch_errmsg(termp->bt_string);
1758 mch_errmsg("\r\n");
1759 }
1760 }
1761 /* when user typed :set term=xxx, quit here */
1762 if (starting != NO_SCREEN)
1763 {
1764 screen_start(); /* don't know where cursor is now */
1765 wait_return(TRUE);
1766 return FAIL;
1767 }
1768 term = DEFAULT_TERM;
1769 mch_errmsg(_("defaulting to '"));
1770 mch_errmsg((char *)term);
1771 mch_errmsg("'\r\n");
1772 if (emsg_silent == 0)
1773 {
1774 screen_start(); /* don't know where cursor is now */
1775 out_flush();
1776 ui_delay(2000L, TRUE);
1777 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001778 set_string_option_direct((char_u *)"term", -1, term,
1779 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 display_errors();
1781 }
1782 out_flush();
1783#ifdef HAVE_TGETENT
1784 if (!termcap_cleared)
1785 {
1786#endif
1787 clear_termoptions(); /* clear old options */
1788#ifdef HAVE_TGETENT
1789 termcap_cleared = TRUE;
1790 }
1791#endif
1792 parse_builtin_tcap(term);
1793#ifdef FEAT_GUI
1794 if (term_is_gui(term))
1795 {
1796 out_flush();
1797 gui_init();
1798 /* If starting the GUI failed, don't do any of the other
1799 * things for this terminal */
1800 if (!gui.in_use)
1801 return FAIL;
1802#ifdef HAVE_TGETENT
1803 break; /* don't try using external termcap */
1804#endif
1805 }
1806#endif /* FEAT_GUI */
1807 }
1808#ifdef HAVE_TGETENT
1809 }
1810#endif
1811
1812/*
1813 * special: There is no info in the termcap about whether the cursor
1814 * positioning is relative to the start of the screen or to the start of the
1815 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1816 * relative.
1817 */
1818 if (STRCMP(term, "pcterm") == 0)
1819 T_CCS = (char_u *)"yes";
1820 else
1821 T_CCS = empty_option;
1822
1823#ifdef UNIX
1824/*
1825 * Any "stty" settings override the default for t_kb from the termcap.
1826 * This is in os_unix.c, because it depends a lot on the version of unix that
1827 * is being used.
1828 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1829 */
1830#ifdef FEAT_GUI
1831 if (!gui.in_use)
1832#endif
1833 get_stty();
1834#endif
1835
1836/*
1837 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1838 * didn't work, use the default CTRL-H
1839 * The default for t_kD is DEL, unless t_kb is DEL.
1840 * The vim_strsave'd strings are probably lost forever, well it's only two
1841 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1842 * directly.
1843 */
1844#ifdef FEAT_GUI
1845 if (!gui.in_use)
1846#endif
1847 {
1848 bs_p = find_termcode((char_u *)"kb");
1849 del_p = find_termcode((char_u *)"kD");
1850 if (bs_p == NULL || *bs_p == NUL)
1851 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1852 if ((del_p == NULL || *del_p == NUL) &&
1853 (bs_p == NULL || *bs_p != DEL))
1854 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1855 }
1856
1857#if defined(UNIX) || defined(VMS)
1858 term_is_xterm = vim_is_xterm(term);
1859#endif
1860
1861#ifdef FEAT_MOUSE
1862# if defined(UNIX) || defined(VMS)
1863# ifdef FEAT_MOUSE_TTY
1864 /*
1865 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1866 * The termcode for the mouse is added as a side effect in option.c.
1867 */
1868 {
1869 char_u *p;
1870
1871 p = (char_u *)"";
1872# ifdef FEAT_MOUSE_XTERM
1873# ifdef FEAT_CLIPBOARD
1874# ifdef FEAT_GUI
1875 if (!gui.in_use)
1876# endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001877# ifndef FEAT_CYGWIN_WIN32_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 clip_init(FALSE);
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880# endif
Bram Moolenaar864207d2008-06-24 22:14:38 +00001881 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 {
1883 if (use_xterm_mouse())
1884 p = NULL; /* keep existing value, might be "xterm2" */
1885 else
1886 p = (char_u *)"xterm";
1887 }
1888# endif
1889 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001892 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1893 * "xterm2" in check_termcode(). */
1894 reset_option_was_set((char_u *)"ttym");
1895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (p == NULL
1897# ifdef FEAT_GUI
1898 || gui.in_use
1899# endif
1900 )
1901 check_mouse_termcode(); /* set mouse termcode anyway */
1902 }
1903# endif
1904# else
1905 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1906# endif
1907#endif /* FEAT_MOUSE */
1908
1909#ifdef FEAT_SNIFF
1910 {
1911 char_u name[2];
1912
1913 name[0] = (int)KS_EXTRA;
1914 name[1] = (int)KE_SNIFF;
1915 add_termcode(name, (char_u *)"\233sniff", FALSE);
1916 }
1917#endif
1918
1919#ifdef USE_TERM_CONSOLE
1920 /* DEFAULT_TERM indicates that it is the machine console. */
1921 if (STRCMP(term, DEFAULT_TERM) != 0)
1922 term_console = FALSE;
1923 else
1924 {
1925 term_console = TRUE;
1926# ifdef AMIGA
1927 win_resize_on(); /* enable window resizing reports */
1928# endif
1929 }
1930#endif
1931
1932#if defined(UNIX) || defined(VMS)
1933 /*
1934 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1935 */
1936 if (vim_is_fastterm(term))
1937 p_tf = TRUE;
1938#endif
1939#ifdef USE_TERM_CONSOLE
1940 /*
1941 * 'ttyfast' is default on consoles
1942 */
1943 if (term_console)
1944 p_tf = TRUE;
1945#endif
1946
1947 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1948
1949 full_screen = TRUE; /* we can use termcap codes from now on */
1950 set_term_defaults(); /* use current values as defaults */
1951#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +02001952 LOG_TR("setting crv_status to CRV_GET");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 crv_status = CRV_GET; /* Get terminal version later */
1954#endif
1955
1956 /*
1957 * Initialize the terminal with the appropriate termcap codes.
1958 * Set the mouse and window title if possible.
1959 * Don't do this when starting, need to parse the .vimrc first, because it
1960 * may redefine t_TI etc.
1961 */
1962 if (starting != NO_SCREEN)
1963 {
1964 starttermcap(); /* may change terminal mode */
1965#ifdef FEAT_MOUSE
1966 setmouse(); /* may start using the mouse */
1967#endif
1968#ifdef FEAT_TITLE
1969 maketitle(); /* may display window title */
1970#endif
1971 }
1972
1973 /* display initial screen after ttest() checking. jw. */
1974 if (width <= 0 || height <= 0)
1975 {
1976 /* termcap failed to report size */
1977 /* set defaults, in case ui_get_shellsize() also fails */
1978 width = 80;
1979#if defined(MSDOS) || defined(WIN3264)
1980 height = 25; /* console is often 25 lines */
1981#else
1982 height = 24; /* most terminals are 24 lines */
1983#endif
1984 }
1985 set_shellsize(width, height, FALSE); /* may change Rows */
1986 if (starting != NO_SCREEN)
1987 {
1988 if (scroll_region)
1989 scroll_region_reset(); /* In case Rows changed */
1990 check_map_keycodes(); /* check mappings for terminal codes used */
1991
1992#ifdef FEAT_AUTOCMD
1993 {
1994 buf_T *old_curbuf;
1995
1996 /*
1997 * Execute the TermChanged autocommands for each buffer that is
1998 * loaded.
1999 */
2000 old_curbuf = curbuf;
2001 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
2002 {
2003 if (curbuf->b_ml.ml_mfp != NULL)
2004 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2005 curbuf);
2006 }
2007 if (buf_valid(old_curbuf))
2008 curbuf = old_curbuf;
2009 }
2010#endif
2011 }
2012
2013#ifdef FEAT_TERMRESPONSE
2014 may_req_termresponse();
2015#endif
2016
2017 return OK;
2018}
2019
2020#if defined(FEAT_MOUSE) || defined(PROTO)
2021
2022# ifdef FEAT_MOUSE_TTY
2023# define HMT_NORMAL 1
2024# define HMT_NETTERM 2
2025# define HMT_DEC 4
2026# define HMT_JSBTERM 8
2027# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002028# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002029# define HMT_SGR 64
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030static int has_mouse_termcode = 0;
2031# endif
2032
Bram Moolenaar864207d2008-06-24 22:14:38 +00002033# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 void
2035set_mouse_termcode(n, s)
2036 int n; /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2037 char_u *s;
2038{
2039 char_u name[2];
2040
2041 name[0] = n;
2042 name[1] = KE_FILLER;
2043 add_termcode(name, s, FALSE);
2044# ifdef FEAT_MOUSE_TTY
2045# ifdef FEAT_MOUSE_JSB
2046 if (n == KS_JSBTERM_MOUSE)
2047 has_mouse_termcode |= HMT_JSBTERM;
2048 else
2049# endif
2050# ifdef FEAT_MOUSE_NET
2051 if (n == KS_NETTERM_MOUSE)
2052 has_mouse_termcode |= HMT_NETTERM;
2053 else
2054# endif
2055# ifdef FEAT_MOUSE_DEC
2056 if (n == KS_DEC_MOUSE)
2057 has_mouse_termcode |= HMT_DEC;
2058 else
2059# endif
2060# ifdef FEAT_MOUSE_PTERM
2061 if (n == KS_PTERM_MOUSE)
2062 has_mouse_termcode |= HMT_PTERM;
2063 else
2064# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002065# ifdef FEAT_MOUSE_URXVT
2066 if (n == KS_URXVT_MOUSE)
2067 has_mouse_termcode |= HMT_URXVT;
2068 else
2069# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002070# ifdef FEAT_MOUSE_SGR
2071 if (n == KS_SGR_MOUSE)
2072 has_mouse_termcode |= HMT_SGR;
2073 else
2074# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 has_mouse_termcode |= HMT_NORMAL;
2076# endif
2077}
2078# endif
2079
2080# if ((defined(UNIX) || defined(VMS) || defined(OS2)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002081 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 void
2083del_mouse_termcode(n)
2084 int n; /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2085{
2086 char_u name[2];
2087
2088 name[0] = n;
2089 name[1] = KE_FILLER;
2090 del_termcode(name);
2091# ifdef FEAT_MOUSE_TTY
2092# ifdef FEAT_MOUSE_JSB
2093 if (n == KS_JSBTERM_MOUSE)
2094 has_mouse_termcode &= ~HMT_JSBTERM;
2095 else
2096# endif
2097# ifdef FEAT_MOUSE_NET
2098 if (n == KS_NETTERM_MOUSE)
2099 has_mouse_termcode &= ~HMT_NETTERM;
2100 else
2101# endif
2102# ifdef FEAT_MOUSE_DEC
2103 if (n == KS_DEC_MOUSE)
2104 has_mouse_termcode &= ~HMT_DEC;
2105 else
2106# endif
2107# ifdef FEAT_MOUSE_PTERM
2108 if (n == KS_PTERM_MOUSE)
2109 has_mouse_termcode &= ~HMT_PTERM;
2110 else
2111# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002112# ifdef FEAT_MOUSE_URXVT
2113 if (n == KS_URXVT_MOUSE)
2114 has_mouse_termcode &= ~HMT_URXVT;
2115 else
2116# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002117# ifdef FEAT_MOUSE_SGR
2118 if (n == KS_SGR_MOUSE)
2119 has_mouse_termcode &= ~HMT_SGR;
2120 else
2121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 has_mouse_termcode &= ~HMT_NORMAL;
2123# endif
2124}
2125# endif
2126#endif
2127
2128#ifdef HAVE_TGETENT
2129/*
2130 * Call tgetent()
2131 * Return error message if it fails, NULL if it's OK.
2132 */
2133 static char_u *
2134tgetent_error(tbuf, term)
2135 char_u *tbuf;
2136 char_u *term;
2137{
2138 int i;
2139
2140 i = TGETENT(tbuf, term);
2141 if (i < 0 /* -1 is always an error */
2142# ifdef TGETENT_ZERO_ERR
2143 || i == 0 /* sometimes zero is also an error */
2144# endif
2145 )
2146 {
2147 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2148 * tgetent() with the always existing "dumb" entry to avoid a crash or
2149 * hang. */
2150 (void)TGETENT(tbuf, "dumb");
2151
2152 if (i < 0)
2153# ifdef TGETENT_ZERO_ERR
2154 return (char_u *)_("E557: Cannot open termcap file");
2155 if (i == 0)
2156# endif
2157#ifdef TERMINFO
2158 return (char_u *)_("E558: Terminal entry not found in terminfo");
2159#else
2160 return (char_u *)_("E559: Terminal entry not found in termcap");
2161#endif
2162 }
2163 return NULL;
2164}
2165
2166/*
2167 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2168 * Fix that here.
2169 */
2170 static char_u *
2171vim_tgetstr(s, pp)
2172 char *s;
2173 char_u **pp;
2174{
2175 char *p;
2176
2177 p = tgetstr(s, (char **)pp);
2178 if (p == (char *)-1)
2179 p = NULL;
2180 return (char_u *)p;
2181}
2182#endif /* HAVE_TGETENT */
2183
2184#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(__EMX__) || defined(VMS) || defined(MACOS_X))
2185/*
2186 * Get Columns and Rows from the termcap. Used after a window signal if the
2187 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2188 * and "li" entries never change. But on some systems this works.
2189 * Errors while getting the entries are ignored.
2190 */
2191 void
2192getlinecol(cp, rp)
2193 long *cp; /* pointer to columns */
2194 long *rp; /* pointer to rows */
2195{
2196 char_u tbuf[TBUFSZ];
2197
2198 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2199 {
2200 if (*cp == 0)
2201 *cp = tgetnum("co");
2202 if (*rp == 0)
2203 *rp = tgetnum("li");
2204 }
2205}
2206#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2207
2208/*
2209 * Get a string entry from the termcap and add it to the list of termcodes.
2210 * Used for <t_xx> special keys.
2211 * Give an error message for failure when not sourcing.
2212 * If force given, replace an existing entry.
2213 * Return FAIL if the entry was not found, OK if the entry was added.
2214 */
2215 int
2216add_termcap_entry(name, force)
2217 char_u *name;
2218 int force;
2219{
2220 char_u *term;
2221 int key;
2222 struct builtin_term *termp;
2223#ifdef HAVE_TGETENT
2224 char_u *string;
2225 int i;
2226 int builtin_first;
2227 char_u tbuf[TBUFSZ];
2228 char_u tstrbuf[TBUFSZ];
2229 char_u *tp = tstrbuf;
2230 char_u *error_msg = NULL;
2231#endif
2232
2233/*
2234 * If the GUI is running or will start in a moment, we only support the keys
2235 * that the GUI can produce.
2236 */
2237#ifdef FEAT_GUI
2238 if (gui.in_use || gui.starting)
2239 return gui_mch_haskey(name);
2240#endif
2241
2242 if (!force && find_termcode(name) != NULL) /* it's already there */
2243 return OK;
2244
2245 term = T_NAME;
2246 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2247 return FAIL;
2248
2249 if (term_is_builtin(term)) /* name starts with "builtin_" */
2250 {
2251 term += 8;
2252#ifdef HAVE_TGETENT
2253 builtin_first = TRUE;
2254#endif
2255 }
2256#ifdef HAVE_TGETENT
2257 else
2258 builtin_first = p_tbi;
2259#endif
2260
2261#ifdef HAVE_TGETENT
2262/*
2263 * We can get the entry from the builtin termcap and from the external one.
2264 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2265 * builtin termcap first.
2266 * If 'ttybuiltin' is off, try external termcap first.
2267 */
2268 for (i = 0; i < 2; ++i)
2269 {
2270 if (!builtin_first == i)
2271#endif
2272 /*
2273 * Search in builtin termcap
2274 */
2275 {
2276 termp = find_builtin_term(term);
2277 if (termp->bt_string != NULL) /* found it */
2278 {
2279 key = TERMCAP2KEY(name[0], name[1]);
2280 while (termp->bt_entry != (int)KS_NAME)
2281 {
2282 if ((int)termp->bt_entry == key)
2283 {
2284 add_termcode(name, (char_u *)termp->bt_string,
2285 term_is_8bit(term));
2286 return OK;
2287 }
2288 ++termp;
2289 }
2290 }
2291 }
2292#ifdef HAVE_TGETENT
2293 else
2294 /*
2295 * Search in external termcap
2296 */
2297 {
2298 error_msg = tgetent_error(tbuf, term);
2299 if (error_msg == NULL)
2300 {
2301 string = TGETSTR((char *)name, &tp);
2302 if (string != NULL && *string != NUL)
2303 {
2304 add_termcode(name, string, FALSE);
2305 return OK;
2306 }
2307 }
2308 }
2309 }
2310#endif
2311
2312 if (sourcing_name == NULL)
2313 {
2314#ifdef HAVE_TGETENT
2315 if (error_msg != NULL)
2316 EMSG(error_msg);
2317 else
2318#endif
2319 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2320 }
2321 return FAIL;
2322}
2323
2324 static int
2325term_is_builtin(name)
2326 char_u *name;
2327{
2328 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2329}
2330
2331/*
2332 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2333 * Assume that the terminal is using 8-bit controls when the name contains
2334 * "8bit", like in "xterm-8bit".
2335 */
2336 int
2337term_is_8bit(name)
2338 char_u *name;
2339{
2340 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2341}
2342
2343/*
2344 * Translate terminal control chars from 7-bit to 8-bit:
2345 * <Esc>[ -> CSI
2346 * <Esc>] -> <M-C-]>
2347 * <Esc>O -> <M-C-O>
2348 */
2349 static int
2350term_7to8bit(p)
2351 char_u *p;
2352{
2353 if (*p == ESC)
2354 {
2355 if (p[1] == '[')
2356 return CSI;
2357 if (p[1] == ']')
2358 return 0x9d;
2359 if (p[1] == 'O')
2360 return 0x8f;
2361 }
2362 return 0;
2363}
2364
2365#ifdef FEAT_GUI
2366 int
2367term_is_gui(name)
2368 char_u *name;
2369{
2370 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2371}
2372#endif
2373
2374#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2375
2376 char_u *
2377tltoa(i)
2378 unsigned long i;
2379{
2380 static char_u buf[16];
2381 char_u *p;
2382
2383 p = buf + 15;
2384 *p = '\0';
2385 do
2386 {
2387 --p;
2388 *p = (char_u) (i % 10 + '0');
2389 i /= 10;
2390 }
2391 while (i > 0 && p > buf);
2392 return p;
2393}
2394#endif
2395
2396#ifndef HAVE_TGETENT
2397
2398/*
2399 * minimal tgoto() implementation.
2400 * no padding and we only parse for %i %d and %+char
2401 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002402static char *tgoto __ARGS((char *, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002404 static char *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405tgoto(cm, x, y)
2406 char *cm;
2407 int x, y;
2408{
2409 static char buf[30];
2410 char *p, *s, *e;
2411
2412 if (!cm)
2413 return "OOPS";
2414 e = buf + 29;
2415 for (s = buf; s < e && *cm; cm++)
2416 {
2417 if (*cm != '%')
2418 {
2419 *s++ = *cm;
2420 continue;
2421 }
2422 switch (*++cm)
2423 {
2424 case 'd':
2425 p = (char *)tltoa((unsigned long)y);
2426 y = x;
2427 while (*p)
2428 *s++ = *p++;
2429 break;
2430 case 'i':
2431 x++;
2432 y++;
2433 break;
2434 case '+':
2435 *s++ = (char)(*++cm + y);
2436 y = x;
2437 break;
2438 case '%':
2439 *s++ = *cm;
2440 break;
2441 default:
2442 return "OOPS";
2443 }
2444 }
2445 *s = '\0';
2446 return buf;
2447}
2448
2449#endif /* HAVE_TGETENT */
2450
2451/*
2452 * Set the terminal name and initialize the terminal options.
2453 * If "name" is NULL or empty, get the terminal name from the environment.
2454 * If that fails, use the default terminal name.
2455 */
2456 void
2457termcapinit(name)
2458 char_u *name;
2459{
2460 char_u *term;
2461
2462 if (name != NULL && *name == NUL)
2463 name = NULL; /* empty name is equal to no name */
2464 term = name;
2465
2466#ifdef __BEOS__
2467 /*
2468 * TERM environment variable is normally set to 'ansi' on the Bebox;
2469 * Since the BeBox doesn't quite support full ANSI yet, we use our
2470 * own custom 'ansi-beos' termcap instead, unless the -T option has
2471 * been given on the command line.
2472 */
2473 if (term == NULL
2474 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2475 term = DEFAULT_TERM;
2476#endif
2477#ifndef MSWIN
2478 if (term == NULL)
2479 term = mch_getenv((char_u *)"TERM");
2480#endif
2481 if (term == NULL || *term == NUL)
2482 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002483 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485 /* Set the default terminal name. */
2486 set_string_default("term", term);
2487 set_string_default("ttytype", term);
2488
2489 /*
2490 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2491 */
2492 set_termname(T_NAME != NULL ? T_NAME : term);
2493}
2494
2495/*
2496 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2497 */
2498#ifdef DOS16
2499# define OUT_SIZE 255 /* only have 640K total... */
2500#else
2501# ifdef FEAT_GUI_W16
2502# define OUT_SIZE 1023 /* Save precious 1K near data */
2503# else
2504# define OUT_SIZE 2047
2505# endif
2506#endif
2507 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2508static char_u out_buf[OUT_SIZE + 1];
2509static int out_pos = 0; /* number of chars in out_buf */
2510
2511/*
2512 * out_flush(): flush the output buffer
2513 */
2514 void
2515out_flush()
2516{
2517 int len;
2518
2519 if (out_pos != 0)
2520 {
2521 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2522 len = out_pos;
2523 out_pos = 0;
2524 ui_write(out_buf, len);
2525 }
2526}
2527
2528#if defined(FEAT_MBYTE) || defined(PROTO)
2529/*
2530 * Sometimes a byte out of a multi-byte character is written with out_char().
2531 * To avoid flushing half of the character, call this function first.
2532 */
2533 void
2534out_flush_check()
2535{
2536 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2537 out_flush();
2538}
2539#endif
2540
2541#ifdef FEAT_GUI
2542/*
2543 * out_trash(): Throw away the contents of the output buffer
2544 */
2545 void
2546out_trash()
2547{
2548 out_pos = 0;
2549}
2550#endif
2551
2552/*
2553 * out_char(c): put a byte into the output buffer.
2554 * Flush it if it becomes full.
2555 * This should not be used for outputting text on the screen (use functions
2556 * like msg_puts() and screen_putchar() for that).
2557 */
2558 void
2559out_char(c)
2560 unsigned c;
2561{
2562#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2563 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2564 out_char('\r');
2565#endif
2566
2567 out_buf[out_pos++] = c;
2568
2569 /* For testing we flush each time. */
2570 if (out_pos >= OUT_SIZE || p_wd)
2571 out_flush();
2572}
2573
2574static void out_char_nf __ARGS((unsigned));
2575
2576/*
2577 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2578 */
2579 static void
2580out_char_nf(c)
2581 unsigned c;
2582{
2583#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2584 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2585 out_char_nf('\r');
2586#endif
2587
2588 out_buf[out_pos++] = c;
2589
2590 if (out_pos >= OUT_SIZE)
2591 out_flush();
2592}
2593
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002594#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2595 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596/*
2597 * A never-padding out_str.
2598 * use this whenever you don't want to run the string through tputs.
2599 * tputs above is harmless, but tputs from the termcap library
2600 * is likely to strip off leading digits, that it mistakes for padding
2601 * information, and "%i", "%d", etc.
2602 * This should only be used for writing terminal codes, not for outputting
2603 * normal text (use functions like msg_puts() and screen_putchar() for that).
2604 */
2605 void
2606out_str_nf(s)
2607 char_u *s;
2608{
2609 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2610 out_flush();
2611 while (*s)
2612 out_char_nf(*s++);
2613
2614 /* For testing we write one string at a time. */
2615 if (p_wd)
2616 out_flush();
2617}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
2620/*
2621 * out_str(s): Put a character string a byte at a time into the output buffer.
2622 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2623 * This should only be used for writing terminal codes, not for outputting
2624 * normal text (use functions like msg_puts() and screen_putchar() for that).
2625 */
2626 void
2627out_str(s)
2628 char_u *s;
2629{
2630 if (s != NULL && *s)
2631 {
2632#ifdef FEAT_GUI
2633 /* Don't use tputs() when GUI is used, ncurses crashes. */
2634 if (gui.in_use)
2635 {
2636 out_str_nf(s);
2637 return;
2638 }
2639#endif
2640 /* avoid terminal strings being split up */
2641 if (out_pos > OUT_SIZE - 20)
2642 out_flush();
2643#ifdef HAVE_TGETENT
2644 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2645#else
2646 while (*s)
2647 out_char_nf(*s++);
2648#endif
2649
2650 /* For testing we write one string at a time. */
2651 if (p_wd)
2652 out_flush();
2653 }
2654}
2655
2656/*
2657 * cursor positioning using termcap parser. (jw)
2658 */
2659 void
2660term_windgoto(row, col)
2661 int row;
2662 int col;
2663{
2664 OUT_STR(tgoto((char *)T_CM, col, row));
2665}
2666
2667 void
2668term_cursor_right(i)
2669 int i;
2670{
2671 OUT_STR(tgoto((char *)T_CRI, 0, i));
2672}
2673
2674 void
2675term_append_lines(line_count)
2676 int line_count;
2677{
2678 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2679}
2680
2681 void
2682term_delete_lines(line_count)
2683 int line_count;
2684{
2685 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2686}
2687
2688#if defined(HAVE_TGETENT) || defined(PROTO)
2689 void
2690term_set_winpos(x, y)
2691 int x;
2692 int y;
2693{
2694 /* Can't handle a negative value here */
2695 if (x < 0)
2696 x = 0;
2697 if (y < 0)
2698 y = 0;
2699 OUT_STR(tgoto((char *)T_CWP, y, x));
2700}
2701
2702 void
2703term_set_winsize(width, height)
2704 int width;
2705 int height;
2706{
2707 OUT_STR(tgoto((char *)T_CWS, height, width));
2708}
2709#endif
2710
2711 void
2712term_fg_color(n)
2713 int n;
2714{
2715 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2716 if (*T_CAF)
2717 term_color(T_CAF, n);
2718 else if (*T_CSF)
2719 term_color(T_CSF, n);
2720}
2721
2722 void
2723term_bg_color(n)
2724 int n;
2725{
2726 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2727 if (*T_CAB)
2728 term_color(T_CAB, n);
2729 else if (*T_CSB)
2730 term_color(T_CSB, n);
2731}
2732
2733 static void
2734term_color(s, n)
2735 char_u *s;
2736 int n;
2737{
2738 char buf[20];
2739 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2740
2741 /* Special handling of 16 colors, because termcap can't handle it */
2742 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2743 /* Also accept CSI instead of <Esc>[ */
2744 if (n >= 8 && t_colors >= 16
2745 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2746 && s[i] != NUL
2747 && (STRCMP(s + i + 1, "%p1%dm") == 0
2748 || STRCMP(s + i + 1, "%dm") == 0)
2749 && (s[i] == '3' || s[i] == '4'))
2750 {
2751 sprintf(buf,
2752#ifdef TERMINFO
2753 "%s%s%%p1%%dm",
2754#else
2755 "%s%s%%dm",
2756#endif
2757 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2758 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2759 : (n >= 16 ? "48;5;" : "10"));
2760 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2761 }
2762 else
2763 OUT_STR(tgoto((char *)s, 0, n));
2764}
2765
2766#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X))) || defined(PROTO)
2767/*
2768 * Generic function to set window title, using t_ts and t_fs.
2769 */
2770 void
2771term_settitle(title)
2772 char_u *title;
2773{
2774 /* t_ts takes one argument: column in status line */
2775 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2776 out_str_nf(title);
2777 out_str(T_FS); /* set title end */
2778 out_flush();
2779}
2780#endif
2781
2782/*
2783 * Make sure we have a valid set or terminal options.
2784 * Replace all entries that are NULL by empty_option
2785 */
2786 void
2787ttest(pairs)
2788 int pairs;
2789{
2790 check_options(); /* make sure no options are NULL */
2791
2792 /*
2793 * MUST have "cm": cursor motion.
2794 */
2795 if (*T_CM == NUL)
2796 EMSG(_("E437: terminal capability \"cm\" required"));
2797
2798 /*
2799 * if "cs" defined, use a scroll region, it's faster.
2800 */
2801 if (*T_CS != NUL)
2802 scroll_region = TRUE;
2803 else
2804 scroll_region = FALSE;
2805
2806 if (pairs)
2807 {
2808 /*
2809 * optional pairs
2810 */
2811 /* TP goes to normal mode for TI (invert) and TB (bold) */
2812 if (*T_ME == NUL)
2813 T_ME = T_MR = T_MD = T_MB = empty_option;
2814 if (*T_SO == NUL || *T_SE == NUL)
2815 T_SO = T_SE = empty_option;
2816 if (*T_US == NUL || *T_UE == NUL)
2817 T_US = T_UE = empty_option;
2818 if (*T_CZH == NUL || *T_CZR == NUL)
2819 T_CZH = T_CZR = empty_option;
2820
2821 /* T_VE is needed even though T_VI is not defined */
2822 if (*T_VE == NUL)
2823 T_VI = empty_option;
2824
2825 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2826 if (*T_ME == NUL)
2827 {
2828 T_ME = T_SE;
2829 T_MR = T_SO;
2830 T_MD = T_SO;
2831 }
2832
2833 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2834 if (*T_SO == NUL)
2835 {
2836 T_SE = T_ME;
2837 if (*T_MR == NUL)
2838 T_SO = T_MD;
2839 else
2840 T_SO = T_MR;
2841 }
2842
2843 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2844 if (*T_CZH == NUL)
2845 {
2846 T_CZR = T_ME;
2847 if (*T_MR == NUL)
2848 T_CZH = T_MD;
2849 else
2850 T_CZH = T_MR;
2851 }
2852
2853 /* "Sb" and "Sf" come in pairs */
2854 if (*T_CSB == NUL || *T_CSF == NUL)
2855 {
2856 T_CSB = empty_option;
2857 T_CSF = empty_option;
2858 }
2859
2860 /* "AB" and "AF" come in pairs */
2861 if (*T_CAB == NUL || *T_CAF == NUL)
2862 {
2863 T_CAB = empty_option;
2864 T_CAF = empty_option;
2865 }
2866
2867 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2868 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002869 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
2871 /* Set 'weirdinvert' according to value of 't_xs' */
2872 p_wiv = (*T_XS != NUL);
2873 }
2874 need_gather = TRUE;
2875
2876 /* Set t_colors to the value of t_Co. */
2877 t_colors = atoi((char *)T_CCO);
2878}
2879
2880#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2881 || defined(PROTO)
2882/*
2883 * Represent the given long_u as individual bytes, with the most significant
2884 * byte first, and store them in dst.
2885 */
2886 void
2887add_long_to_buf(val, dst)
2888 long_u val;
2889 char_u *dst;
2890{
2891 int i;
2892 int shift;
2893
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002894 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
2896 shift = 8 * (sizeof(long_u) - i);
2897 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2898 }
2899}
2900
2901static int get_long_from_buf __ARGS((char_u *buf, long_u *val));
2902
2903/*
2904 * Interpret the next string of bytes in buf as a long integer, with the most
2905 * significant byte first. Note that it is assumed that buf has been through
2906 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
2907 * Puts result in val, and returns the number of bytes read from buf
2908 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
2909 * were present.
2910 */
2911 static int
2912get_long_from_buf(buf, val)
2913 char_u *buf;
2914 long_u *val;
2915{
2916 int len;
2917 char_u bytes[sizeof(long_u)];
2918 int i;
2919 int shift;
2920
2921 *val = 0;
2922 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
2923 if (len != -1)
2924 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002925 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 {
2927 shift = 8 * (sizeof(long_u) - 1 - i);
2928 *val += (long_u)bytes[i] << shift;
2929 }
2930 }
2931 return len;
2932}
2933#endif
2934
2935#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002936 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
2937 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938/*
2939 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
2940 * that buf has been through inchar(). Returns the actual number of bytes used
2941 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
2942 * available.
2943 */
2944 static int
2945get_bytes_from_buf(buf, bytes, num_bytes)
2946 char_u *buf;
2947 char_u *bytes;
2948 int num_bytes;
2949{
2950 int len = 0;
2951 int i;
2952 char_u c;
2953
2954 for (i = 0; i < num_bytes; i++)
2955 {
2956 if ((c = buf[len++]) == NUL)
2957 return -1;
2958 if (c == K_SPECIAL)
2959 {
2960 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
2961 return -1;
2962 if (buf[len++] == (int)KS_ZERO)
2963 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02002964 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
2965 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
2966 if (buf[len++] == (int)KE_CSI)
2967 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00002969 else if (c == CSI && buf[len] == KS_EXTRA
2970 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002971 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
2972 * the start of a special key, see add_to_input_buf_csi(). */
2973 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 bytes[i] = c;
2975 }
2976 return len;
2977}
2978#endif
2979
2980/*
Bram Moolenaare057d402013-06-30 17:51:51 +02002981 * Check if the new shell size is valid, correct it if it's too small or way
2982 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 */
2984 void
2985check_shellsize()
2986{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 if (Rows < min_rows()) /* need room for one window and command line */
2988 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02002989 limit_screen_size();
2990}
2991
2992/*
2993 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
2994 */
2995 void
2996limit_screen_size()
2997{
2998 if (Columns < MIN_COLUMNS)
2999 Columns = MIN_COLUMNS;
3000 else if (Columns > 10000)
3001 Columns = 10000;
3002 if (Rows > 1000)
3003 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004}
3005
Bram Moolenaar86b68352004-12-27 21:59:20 +00003006/*
3007 * Invoked just before the screen structures are going to be (re)allocated.
3008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 void
3010win_new_shellsize()
3011{
3012 static int old_Rows = 0;
3013 static int old_Columns = 0;
3014
3015 if (old_Rows != Rows || old_Columns != Columns)
3016 ui_new_shellsize();
3017 if (old_Rows != Rows)
3018 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003019 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003020 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003021 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 old_Rows = Rows;
3023 shell_new_rows(); /* update window sizes */
3024 }
3025 if (old_Columns != Columns)
3026 {
3027 old_Columns = Columns;
3028#ifdef FEAT_VERTSPLIT
3029 shell_new_columns(); /* update window sizes */
3030#endif
3031 }
3032}
3033
3034/*
3035 * Call this function when the Vim shell has been resized in any way.
3036 * Will obtain the current size and redraw (also when size didn't change).
3037 */
3038 void
3039shell_resized()
3040{
3041 set_shellsize(0, 0, FALSE);
3042}
3043
3044/*
3045 * Check if the shell size changed. Handle a resize.
3046 * When the size didn't change, nothing happens.
3047 */
3048 void
3049shell_resized_check()
3050{
3051 int old_Rows = Rows;
3052 int old_Columns = Columns;
3053
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003054 if (!exiting
3055#ifdef FEAT_GUI
3056 /* Do not get the size when executing a shell command during
3057 * startup. */
3058 && !gui.starting
3059#endif
3060 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003061 {
3062 (void)ui_get_shellsize();
3063 check_shellsize();
3064 if (old_Rows != Rows || old_Columns != Columns)
3065 shell_resized();
3066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067}
3068
3069/*
3070 * Set size of the Vim shell.
3071 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3072 * window size (this is used for the :win command).
3073 * If 'mustset' is FALSE, we may try to get the real window size and if
3074 * it fails use 'width' and 'height'.
3075 */
3076 void
3077set_shellsize(width, height, mustset)
3078 int width, height;
3079 int mustset;
3080{
3081 static int busy = FALSE;
3082
3083 /*
3084 * Avoid recursiveness, can happen when setting the window size causes
3085 * another window-changed signal.
3086 */
3087 if (busy)
3088 return;
3089
3090 if (width < 0 || height < 0) /* just checking... */
3091 return;
3092
Bram Moolenaara971b822011-09-14 14:43:25 +02003093 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003095 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 State = SETWSIZE;
3097 return;
3098 }
3099
Bram Moolenaara971b822011-09-14 14:43:25 +02003100 /* curwin->w_buffer can be NULL when we are closing a window and the
3101 * buffer has already been closed and removing a scrollbar causes a resize
3102 * event. Don't resize then, it will happen after entering another buffer.
3103 */
3104 if (curwin->w_buffer == NULL)
3105 return;
3106
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 ++busy;
3108
3109#ifdef AMIGA
3110 out_flush(); /* must do this before mch_get_shellsize() for
3111 some obscure reason */
3112#endif
3113
3114 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3115 {
3116 Rows = height;
3117 Columns = width;
3118 check_shellsize();
3119 ui_set_shellsize(mustset);
3120 }
3121 else
3122 check_shellsize();
3123
3124 /* The window layout used to be adjusted here, but it now happens in
3125 * screenalloc() (also invoked from screenclear()). That is because the
3126 * "busy" check above may skip this, but not screenalloc(). */
3127
3128 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3129 screenclear();
3130 else
3131 screen_start(); /* don't know where cursor is now */
3132
3133 if (starting != NO_SCREEN)
3134 {
3135#ifdef FEAT_TITLE
3136 maketitle();
3137#endif
3138 changed_line_abv_curs();
3139 invalidate_botline();
3140
3141 /*
3142 * We only redraw when it's needed:
3143 * - While at the more prompt or executing an external command, don't
3144 * redraw, but position the cursor.
3145 * - While editing the command line, only redraw that.
3146 * - in Ex mode, don't redraw anything.
3147 * - Otherwise, redraw right now, and position the cursor.
3148 * Always need to call update_screen() or screenalloc(), to make
3149 * sure Rows/Columns and the size of ScreenLines[] is correct!
3150 */
3151 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3152 || exmode_active)
3153 {
3154 screenalloc(FALSE);
3155 repeat_message();
3156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 else
3158 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003159#ifdef FEAT_SCROLLBIND
3160 if (curwin->w_p_scb)
3161 do_check_scrollbind(TRUE);
3162#endif
3163 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003164 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003165 update_screen(NOT_VALID);
3166 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003167 }
3168 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003169 {
3170 update_topline();
3171#if defined(FEAT_INS_EXPAND)
3172 if (pum_visible())
3173 {
3174 redraw_later(NOT_VALID);
3175 ins_compl_show_pum(); /* This includes the redraw. */
3176 }
3177 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003178#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003179 update_screen(NOT_VALID);
3180 if (redrawing())
3181 setcursor();
3182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 }
3184 cursor_on(); /* redrawing may have switched it off */
3185 }
3186 out_flush();
3187 --busy;
3188}
3189
3190/*
3191 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3192 * commands and Ex mode).
3193 */
3194 void
3195settmode(tmode)
3196 int tmode;
3197{
3198#ifdef FEAT_GUI
3199 /* don't set the term where gvim was started to any mode */
3200 if (gui.in_use)
3201 return;
3202#endif
3203
3204 if (full_screen)
3205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 /*
3207 * When returning after calling a shell we want to really set the
3208 * terminal to raw mode, even though we think it already is, because
3209 * the shell program may have reset the terminal mode.
3210 * When we think the terminal is normal, don't try to set it to
3211 * normal again, because that causes problems (logout!) on some
3212 * machines.
3213 */
3214 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3215 {
3216#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003217# ifdef FEAT_GUI
3218 if (!gui.in_use && !gui.starting)
3219# endif
3220 {
3221 /* May need to check for T_CRV response and termcodes, it
3222 * doesn't work in Cooked mode, an external program may get
3223 * them. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003224 if (tmode != TMODE_RAW && (crv_status == CRV_SENT
3225 || u7_status == U7_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003226 (void)vpeekc_nomap();
3227 check_for_codes_from_term();
3228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229#endif
3230#ifdef FEAT_MOUSE_TTY
3231 if (tmode != TMODE_RAW)
3232 mch_setmouse(FALSE); /* switch mouse off */
3233#endif
3234 out_flush();
3235 mch_settmode(tmode); /* machine specific function */
3236 cur_tmode = tmode;
3237#ifdef FEAT_MOUSE
3238 if (tmode == TMODE_RAW)
3239 setmouse(); /* may switch mouse on */
3240#endif
3241 out_flush();
3242 }
3243#ifdef FEAT_TERMRESPONSE
3244 may_req_termresponse();
3245#endif
3246 }
3247}
3248
3249 void
3250starttermcap()
3251{
3252 if (full_screen && !termcap_active)
3253 {
3254 out_str(T_TI); /* start termcap mode */
3255 out_str(T_KS); /* start "keypad transmit" mode */
3256 out_flush();
3257 termcap_active = TRUE;
3258 screen_start(); /* don't know where cursor is now */
3259#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003260# ifdef FEAT_GUI
3261 if (!gui.in_use && !gui.starting)
3262# endif
3263 {
3264 may_req_termresponse();
3265 /* Immediately check for a response. If t_Co changes, we don't
3266 * want to redraw with wrong colors first. */
Bram Moolenaar90013c62014-05-22 18:14:31 +02003267 if (crv_status == CRV_SENT)
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003268 check_for_codes_from_term();
3269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270#endif
3271 }
3272}
3273
3274 void
3275stoptermcap()
3276{
3277 screen_stop_highlight();
3278 reset_cterm_colors();
3279 if (termcap_active)
3280 {
3281#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003282# ifdef FEAT_GUI
3283 if (!gui.in_use && !gui.starting)
3284# endif
3285 {
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003286 /* May need to discard T_CRV or T_U7 response. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003287 if (crv_status == CRV_SENT || u7_status == U7_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003288 {
3289# ifdef UNIX
3290 /* Give the terminal a chance to respond. */
3291 mch_delay(100L, FALSE);
3292# endif
3293# ifdef TCIFLUSH
3294 /* Discard data received but not read. */
3295 if (exiting)
3296 tcflush(fileno(stdin), TCIFLUSH);
3297# endif
3298 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003299 /* Check for termcodes first, otherwise an external program may
3300 * get them. */
3301 check_for_codes_from_term();
3302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303#endif
3304 out_str(T_KE); /* stop "keypad transmit" mode */
3305 out_flush();
3306 termcap_active = FALSE;
3307 cursor_on(); /* just in case it is still off */
3308 out_str(T_TE); /* stop termcap mode */
3309 screen_start(); /* don't know where cursor is now */
3310 out_flush();
3311 }
3312}
3313
Bram Moolenaar90013c62014-05-22 18:14:31 +02003314#if defined(UNIX) || defined(PROTO)
3315/*
3316 * Return TRUE when the xterm version was requested or anything else that
3317 * would send an ESC sequence back to Vim.
3318 * If not sent yet, prevent it from being sent soon.
3319 * Used to check whether it is OK to enable checking for DEC mouse codes,
3320 * which conflict with may xterm ESC sequences.
3321 */
3322 int
3323did_request_esc_sequence()
3324{
3325 if (crv_status == CRV_GET)
3326 crv_status = 0;
3327 if (u7_status == U7_GET)
3328 u7_status = 0;
3329 return crv_status == CRV_SENT || u7_status == U7_SENT
3330# if defined(FEAT_TERMRESPONSE)
3331 || xt_index_out > xt_index_in
3332# endif
3333 ;
3334}
3335#endif
3336
3337
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003338#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339/*
3340 * Request version string (for xterm) when needed.
3341 * Only do this after switching to raw mode, otherwise the result will be
3342 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003343 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003344 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 * Only do this after termcap mode has been started, otherwise the codes for
3346 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003347 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3348 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003349 * On Unix only do it when both output and input are a tty (avoid writing
3350 * request to terminal while reading from a file).
Bram Moolenaar90013c62014-05-22 18:14:31 +02003351 * Do not do this when a mouse is being detected that starts with the same ESC
3352 * sequence as the termresponse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 * The result is caught in check_termcode().
3354 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003355 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356may_req_termresponse()
3357{
3358 if (crv_status == CRV_GET
3359 && cur_tmode == TMODE_RAW
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003360 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 && termcap_active
Bram Moolenaarebefac62005-12-28 22:39:57 +00003362 && p_ek
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003363# ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 && isatty(1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003365 && isatty(read_cmd_fd)
Bram Moolenaar90013c62014-05-22 18:14:31 +02003366 && !xterm_conflict_mouse
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 && *T_CRV != NUL)
3369 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02003370 LOG_TR("Sending CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 out_str(T_CRV);
3372 crv_status = CRV_SENT;
3373 /* check for the characters now, otherwise they might be eaten by
3374 * get_keystroke() */
3375 out_flush();
3376 (void)vpeekc_nomap();
3377 }
3378}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003379
3380# if defined(FEAT_MBYTE) || defined(PROTO)
3381/*
3382 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003383 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003384 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003385 * If the terminal treats \u25bd as single width, the position is (1, 1),
3386 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003387 * This function has the side effect that changes cursor position, so
3388 * it must be called immediately after entering termcap mode.
3389 */
3390 void
Bram Moolenaar386dcde2013-09-29 16:27:47 +02003391may_req_ambiguous_char_width()
Bram Moolenaar9584b312013-03-13 19:29:28 +01003392{
3393 if (u7_status == U7_GET
3394 && cur_tmode == TMODE_RAW
3395 && termcap_active
3396 && p_ek
3397# ifdef UNIX
3398 && isatty(1)
3399 && isatty(read_cmd_fd)
3400# endif
3401 && *T_U7 != NUL
3402 && !option_was_set((char_u *)"ambiwidth"))
3403 {
3404 char_u buf[16];
3405
Bram Moolenaar2951b772013-07-03 12:45:31 +02003406 LOG_TR("Sending U7 request");
3407 /* Do this in the second row. In the first row the returned sequence
3408 * may be CSI 1;2R, which is the same as <S-F3>. */
3409 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003410 buf[mb_char2bytes(0x25bd, buf)] = 0;
3411 out_str(buf);
3412 out_str(T_U7);
3413 u7_status = U7_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003414 out_flush();
3415 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003416 out_str((char_u *)" ");
3417 term_windgoto(0, 0);
3418 /* check for the characters now, otherwise they might be eaten by
3419 * get_keystroke() */
3420 out_flush();
3421 (void)vpeekc_nomap();
3422 }
3423}
3424# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003425
3426# ifdef DEBUG_TERMRESPONSE
3427 static void
3428log_tr(char *msg)
3429{
3430 static FILE *fd_tr = NULL;
3431 static proftime_T start;
3432 proftime_T now;
3433
3434 if (fd_tr == NULL)
3435 {
3436 fd_tr = fopen("termresponse.log", "w");
3437 profile_start(&start);
3438 }
3439 now = start;
3440 profile_end(&now);
3441 fprintf(fd_tr, "%s: %s %s\n",
3442 profile_msg(&now),
3443 must_redraw == NOT_VALID ? "NV"
3444 : must_redraw == CLEAR ? "CL" : " ",
3445 msg);
3446}
3447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448#endif
3449
3450/*
3451 * Return TRUE when saving and restoring the screen.
3452 */
3453 int
3454swapping_screen()
3455{
3456 return (full_screen && *T_TI != NUL);
3457}
3458
3459#ifdef FEAT_MOUSE
3460/*
3461 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3462 */
3463 void
3464setmouse()
3465{
3466# ifdef FEAT_MOUSE_TTY
3467 int checkfor;
3468# endif
3469
3470# ifdef FEAT_MOUSESHAPE
3471 update_mouseshape(-1);
3472# endif
3473
3474# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3475# ifdef FEAT_GUI
3476 /* In the GUI the mouse is always enabled. */
3477 if (gui.in_use)
3478 return;
3479# endif
3480 /* be quick when mouse is off */
3481 if (*p_mouse == NUL || has_mouse_termcode == 0)
3482 return;
3483
3484 /* don't switch mouse on when not in raw mode (Ex mode) */
3485 if (cur_tmode != TMODE_RAW)
3486 {
3487 mch_setmouse(FALSE);
3488 return;
3489 }
3490
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (VIsual_active)
3492 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003493 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 checkfor = MOUSE_RETURN;
3495 else if (State & INSERT)
3496 checkfor = MOUSE_INSERT;
3497 else if (State & CMDLINE)
3498 checkfor = MOUSE_COMMAND;
3499 else if (State == CONFIRM || State == EXTERNCMD)
3500 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3501 else
3502 checkfor = MOUSE_NORMAL; /* assume normal mode */
3503
3504 if (mouse_has(checkfor))
3505 mch_setmouse(TRUE);
3506 else
3507 mch_setmouse(FALSE);
3508# endif
3509}
3510
3511/*
3512 * Return TRUE if
3513 * - "c" is in 'mouse', or
3514 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3515 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3516 * normal editing mode (not at hit-return message).
3517 */
3518 int
3519mouse_has(c)
3520 int c;
3521{
3522 char_u *p;
3523
3524 for (p = p_mouse; *p; ++p)
3525 switch (*p)
3526 {
3527 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3528 return TRUE;
3529 break;
3530 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3531 return TRUE;
3532 break;
3533 default: if (c == *p) return TRUE; break;
3534 }
3535 return FALSE;
3536}
3537
3538/*
3539 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3540 */
3541 int
3542mouse_model_popup()
3543{
3544 return (p_mousem[0] == 'p');
3545}
3546#endif
3547
3548/*
3549 * By outputting the 'cursor very visible' termcap code, for some windowed
3550 * terminals this makes the screen scrolled to the correct position.
3551 * Used when starting Vim or returning from a shell.
3552 */
3553 void
3554scroll_start()
3555{
3556 if (*T_VS != NUL)
3557 {
3558 out_str(T_VS);
3559 out_str(T_VE);
3560 screen_start(); /* don't know where cursor is now */
3561 }
3562}
3563
3564static int cursor_is_off = FALSE;
3565
3566/*
3567 * Enable the cursor.
3568 */
3569 void
3570cursor_on()
3571{
3572 if (cursor_is_off)
3573 {
3574 out_str(T_VE);
3575 cursor_is_off = FALSE;
3576 }
3577}
3578
3579/*
3580 * Disable the cursor.
3581 */
3582 void
3583cursor_off()
3584{
3585 if (full_screen)
3586 {
3587 if (!cursor_is_off)
3588 out_str(T_VI); /* disable cursor */
3589 cursor_is_off = TRUE;
3590 }
3591}
3592
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003593#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594/*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003595 * Set cursor shape to match Insert mode.
3596 */
3597 void
3598term_cursor_shape()
3599{
3600 static int showing_insert_mode = MAYBE;
3601
3602 if (!full_screen || *T_CSI == NUL || *T_CEI == NUL)
3603 return;
3604
3605 if (State & INSERT)
3606 {
3607 if (showing_insert_mode != TRUE)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003608 out_str(T_CSI); /* Insert mode cursor */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003609 showing_insert_mode = TRUE;
3610 }
3611 else
3612 {
3613 if (showing_insert_mode != FALSE)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003614 out_str(T_CEI); /* non-Insert mode cursor */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003615 showing_insert_mode = FALSE;
3616 }
3617}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003618#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003619
3620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 * Set scrolling region for window 'wp'.
3622 * The region starts 'off' lines from the start of the window.
3623 * Also set the vertical scroll region for a vertically split window. Always
3624 * the full width of the window, excluding the vertical separator.
3625 */
3626 void
3627scroll_region_set(wp, off)
3628 win_T *wp;
3629 int off;
3630{
3631 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3632 W_WINROW(wp) + off));
3633#ifdef FEAT_VERTSPLIT
3634 if (*T_CSV != NUL && wp->w_width != Columns)
3635 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3636 W_WINCOL(wp)));
3637#endif
3638 screen_start(); /* don't know where cursor is now */
3639}
3640
3641/*
3642 * Reset scrolling region to the whole screen.
3643 */
3644 void
3645scroll_region_reset()
3646{
3647 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
3648#ifdef FEAT_VERTSPLIT
3649 if (*T_CSV != NUL)
3650 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3651#endif
3652 screen_start(); /* don't know where cursor is now */
3653}
3654
3655
3656/*
3657 * List of terminal codes that are currently recognized.
3658 */
3659
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003660static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661{
3662 char_u name[2]; /* termcap name of entry */
3663 char_u *code; /* terminal code (in allocated memory) */
3664 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003665 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666} *termcodes = NULL;
3667
3668static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3669static int tc_len = 0; /* current number of entries in termcodes[] */
3670
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003671static int termcode_star __ARGS((char_u *code, int len));
3672
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 void
3674clear_termcodes()
3675{
3676 while (tc_len > 0)
3677 vim_free(termcodes[--tc_len].code);
3678 vim_free(termcodes);
3679 termcodes = NULL;
3680 tc_max_len = 0;
3681
3682#ifdef HAVE_TGETENT
3683 BC = (char *)empty_option;
3684 UP = (char *)empty_option;
3685 PC = NUL; /* set pad character to NUL */
3686 ospeed = 0;
3687#endif
3688
3689 need_gather = TRUE; /* need to fill termleader[] */
3690}
3691
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003692#define ATC_FROM_TERM 55
3693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694/*
3695 * Add a new entry to the list of terminal codes.
3696 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003697 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3698 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 */
3700 void
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003701add_termcode(name, string, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 char_u *name;
3703 char_u *string;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003704 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705{
3706 struct termcode *new_tc;
3707 int i, j;
3708 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003709 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
3711 if (string == NULL || *string == NUL)
3712 {
3713 del_termcode(name);
3714 return;
3715 }
3716
3717 s = vim_strsave(string);
3718 if (s == NULL)
3719 return;
3720
3721 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003722 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003724 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 s[0] = term_7to8bit(string);
3726 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003727 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
3729 need_gather = TRUE; /* need to fill termleader[] */
3730
3731 /*
3732 * need to make space for more entries
3733 */
3734 if (tc_len == tc_max_len)
3735 {
3736 tc_max_len += 20;
3737 new_tc = (struct termcode *)alloc(
3738 (unsigned)(tc_max_len * sizeof(struct termcode)));
3739 if (new_tc == NULL)
3740 {
3741 tc_max_len -= 20;
3742 return;
3743 }
3744 for (i = 0; i < tc_len; ++i)
3745 new_tc[i] = termcodes[i];
3746 vim_free(termcodes);
3747 termcodes = new_tc;
3748 }
3749
3750 /*
3751 * Look for existing entry with the same name, it is replaced.
3752 * Look for an existing entry that is alphabetical higher, the new entry
3753 * is inserted in front of it.
3754 */
3755 for (i = 0; i < tc_len; ++i)
3756 {
3757 if (termcodes[i].name[0] < name[0])
3758 continue;
3759 if (termcodes[i].name[0] == name[0])
3760 {
3761 if (termcodes[i].name[1] < name[1])
3762 continue;
3763 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003764 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 */
3766 if (termcodes[i].name[1] == name[1])
3767 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003768 if (flags == ATC_FROM_TERM && (j = termcode_star(
3769 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003770 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003771 /* Don't replace ESC[123;*X or ESC O*X with another when
3772 * invoked from got_code_from_term(). */
3773 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003774 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003775 && s[len - 1]
3776 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003777 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003778 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003779 vim_free(s);
3780 return;
3781 }
3782 }
3783 else
3784 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003785 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003786 vim_free(termcodes[i].code);
3787 --tc_len;
3788 break;
3789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791 }
3792 /*
3793 * Found alphabetical larger entry, move rest to insert new entry
3794 */
3795 for (j = tc_len; j > i; --j)
3796 termcodes[j] = termcodes[j - 1];
3797 break;
3798 }
3799
3800 termcodes[i].name[0] = name[0];
3801 termcodes[i].name[1] = name[1];
3802 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003803 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003804
3805 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3806 * accept modifiers. */
3807 termcodes[i].modlen = 0;
3808 j = termcode_star(s, len);
3809 if (j > 0)
3810 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 ++tc_len;
3812}
3813
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003814/*
3815 * Check termcode "code[len]" for ending in ;*X, <Esc>O*X or <M-O>*X.
3816 * The "X" can be any character.
3817 * Return 0 if not found, 2 for ;*X and 1 for O*X and <M-O>*X.
3818 */
3819 static int
3820termcode_star(code, len)
3821 char_u *code;
3822 int len;
3823{
3824 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3825 if (len >= 3 && code[len - 2] == '*')
3826 {
3827 if (len >= 5 && code[len - 3] == ';')
3828 return 2;
3829 if ((len >= 4 && code[len - 3] == 'O') || code[len - 3] == 'O' + 128)
3830 return 1;
3831 }
3832 return 0;
3833}
3834
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 char_u *
3836find_termcode(name)
3837 char_u *name;
3838{
3839 int i;
3840
3841 for (i = 0; i < tc_len; ++i)
3842 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3843 return termcodes[i].code;
3844 return NULL;
3845}
3846
3847#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3848 char_u *
3849get_termcode(i)
3850 int i;
3851{
3852 if (i >= tc_len)
3853 return NULL;
3854 return &termcodes[i].name[0];
3855}
3856#endif
3857
3858 void
3859del_termcode(name)
3860 char_u *name;
3861{
3862 int i;
3863
3864 if (termcodes == NULL) /* nothing there yet */
3865 return;
3866
3867 need_gather = TRUE; /* need to fill termleader[] */
3868
3869 for (i = 0; i < tc_len; ++i)
3870 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3871 {
3872 del_termcode_idx(i);
3873 return;
3874 }
3875 /* not found. Give error message? */
3876}
3877
3878 static void
3879del_termcode_idx(idx)
3880 int idx;
3881{
3882 int i;
3883
3884 vim_free(termcodes[idx].code);
3885 --tc_len;
3886 for (i = idx; i < tc_len; ++i)
3887 termcodes[i] = termcodes[i + 1];
3888}
3889
3890#ifdef FEAT_TERMRESPONSE
3891/*
3892 * Called when detected that the terminal sends 8-bit codes.
3893 * Convert all 7-bit codes to their 8-bit equivalent.
3894 */
3895 static void
3896switch_to_8bit()
3897{
3898 int i;
3899 int c;
3900
3901 /* Only need to do something when not already using 8-bit codes. */
3902 if (!term_is_8bit(T_NAME))
3903 {
3904 for (i = 0; i < tc_len; ++i)
3905 {
3906 c = term_7to8bit(termcodes[i].code);
3907 if (c != 0)
3908 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003909 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 termcodes[i].code[0] = c;
3911 }
3912 }
3913 need_gather = TRUE; /* need to fill termleader[] */
3914 }
3915 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003916 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917}
3918#endif
3919
3920#ifdef CHECK_DOUBLE_CLICK
3921static linenr_T orig_topline = 0;
3922# ifdef FEAT_DIFF
3923static int orig_topfill = 0;
3924# endif
3925#endif
3926#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
3927/*
3928 * Checking for double clicks ourselves.
3929 * "orig_topline" is used to avoid detecting a double-click when the window
3930 * contents scrolled (e.g., when 'scrolloff' is non-zero).
3931 */
3932/*
3933 * Set orig_topline. Used when jumping to another window, so that a double
3934 * click still works.
3935 */
3936 void
3937set_mouse_topline(wp)
3938 win_T *wp;
3939{
3940 orig_topline = wp->w_topline;
3941# ifdef FEAT_DIFF
3942 orig_topfill = wp->w_topfill;
3943# endif
3944}
3945#endif
3946
3947/*
3948 * Check if typebuf.tb_buf[] contains a terminal key code.
3949 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
3950 * + max_offset].
3951 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003952 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 * With a match, the match is removed, the replacement code is inserted in
3954 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
3955 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003956 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
3957 * "buflen" is then the length of the string in buf[] and is updated for
3958 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 */
3960 int
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003961check_termcode(max_offset, buf, bufsize, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 int max_offset;
3963 char_u *buf;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003964 int bufsize;
3965 int *buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966{
3967 char_u *tp;
3968 char_u *p;
3969 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003970 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003972 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 int offset;
3974 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003975 int modifiers;
3976 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 int new_slen;
3978 int extra;
3979 char_u string[MAX_KEY_CODE_LEN + 1];
3980 int i, j;
3981 int idx = 0;
3982#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00003983# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
3984 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 char_u bytes[6];
3986 int num_bytes;
3987# endif
3988 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 int is_click, is_drag;
3990 int wheel_code = 0;
3991 int current_button;
3992 static int held_button = MOUSE_RELEASE;
3993 static int orig_num_clicks = 1;
3994 static int orig_mouse_code = 0x0;
3995# ifdef CHECK_DOUBLE_CLICK
3996 static int orig_mouse_col = 0;
3997 static int orig_mouse_row = 0;
3998 static struct timeval orig_mouse_time = {0, 0};
3999 /* time of previous mouse click */
4000 struct timeval mouse_time; /* time of current mouse click */
4001 long timediff; /* elapsed time in msec */
4002# endif
4003#endif
4004 int cpo_koffset;
4005#ifdef FEAT_MOUSE_GPM
4006 extern int gpm_flag; /* gpm library variable */
4007#endif
4008
4009 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4010
4011 /*
4012 * Speed up the checks for terminal codes by gathering all first bytes
4013 * used in termleader[]. Often this is just a single <Esc>.
4014 */
4015 if (need_gather)
4016 gather_termleader();
4017
4018 /*
4019 * Check at several positions in typebuf.tb_buf[], to catch something like
4020 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4021 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004022 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 * This is used often, KEEP IT FAST!
4024 */
4025 for (offset = 0; offset < max_offset; ++offset)
4026 {
4027 if (buf == NULL)
4028 {
4029 if (offset >= typebuf.tb_len)
4030 break;
4031 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4032 len = typebuf.tb_len - offset; /* length of the input */
4033 }
4034 else
4035 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004036 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 break;
4038 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004039 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 }
4041
4042 /*
4043 * Don't check characters after K_SPECIAL, those are already
4044 * translated terminal chars (avoid translating ~@^Hx).
4045 */
4046 if (*tp == K_SPECIAL)
4047 {
4048 offset += 2; /* there are always 2 extra characters */
4049 continue;
4050 }
4051
4052 /*
4053 * Skip this position if the character does not appear as the first
4054 * character in term_strings. This speeds up a lot, since most
4055 * termcodes start with the same character (ESC or CSI).
4056 */
4057 i = *tp;
4058 for (p = termleader; *p && *p != i; ++p)
4059 ;
4060 if (*p == NUL)
4061 continue;
4062
4063 /*
4064 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4065 * are in Insert mode.
4066 */
4067 if (*tp == ESC && !p_ek && (State & INSERT))
4068 continue;
4069
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004071 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004072 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073
4074#ifdef FEAT_GUI
4075 if (gui.in_use)
4076 {
4077 /*
4078 * GUI special key codes are all of the form [CSI xx].
4079 */
4080 if (*tp == CSI) /* Special key from GUI */
4081 {
4082 if (len < 3)
4083 return -1; /* Shouldn't happen */
4084 slen = 3;
4085 key_name[0] = tp[1];
4086 key_name[1] = tp[2];
4087 }
4088 }
4089 else
4090#endif /* FEAT_GUI */
4091 {
4092 for (idx = 0; idx < tc_len; ++idx)
4093 {
4094 /*
4095 * Ignore the entry if we are not at the start of
4096 * typebuf.tb_buf[]
4097 * and there are not enough characters to make a match.
4098 * But only when the 'K' flag is in 'cpoptions'.
4099 */
4100 slen = termcodes[idx].len;
4101 if (cpo_koffset && offset && len < slen)
4102 continue;
4103 if (STRNCMP(termcodes[idx].code, tp,
4104 (size_t)(slen > len ? len : slen)) == 0)
4105 {
4106 if (len < slen) /* got a partial sequence */
4107 return -1; /* need to get more chars */
4108
4109 /*
4110 * When found a keypad key, check if there is another key
4111 * that matches and use that one. This makes <Home> to be
4112 * found instead of <kHome> when they produce the same
4113 * key code.
4114 */
4115 if (termcodes[idx].name[0] == 'K'
4116 && VIM_ISDIGIT(termcodes[idx].name[1]))
4117 {
4118 for (j = idx + 1; j < tc_len; ++j)
4119 if (termcodes[j].len == slen &&
4120 STRNCMP(termcodes[idx].code,
4121 termcodes[j].code, slen) == 0)
4122 {
4123 idx = j;
4124 break;
4125 }
4126 }
4127
4128 key_name[0] = termcodes[idx].name[0];
4129 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 break;
4131 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004132
4133 /*
4134 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004135 * <Esc>[123;*X (modslen == slen - 3)
4136 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4137 * When there is a modifier the * matches a number.
4138 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004139 */
4140 if (termcodes[idx].modlen > 0)
4141 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004142 modslen = termcodes[idx].modlen;
4143 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004144 continue;
4145 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004146 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004147 {
4148 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004149
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004150 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004151 return -1; /* need to get more chars */
4152
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004153 if (tp[modslen] == termcodes[idx].code[slen - 1])
4154 slen = modslen + 1; /* no modifiers */
4155 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004156 continue; /* no match */
4157 else
4158 {
4159 /* Skip over the digits, the final char must
4160 * follow. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004161 for (j = slen - 2; j < len && isdigit(tp[j]); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004162 ;
4163 ++j;
4164 if (len < j) /* got a partial sequence */
4165 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004166 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4167 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004168
4169 /* Match! Convert modifier bits. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004170 n = atoi((char *)tp + slen - 2) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004171 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004172 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004173 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004174 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004175 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004176 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004177 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004178 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004179
4180 slen = j;
4181 }
4182 key_name[0] = termcodes[idx].name[0];
4183 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004184 break;
4185 }
4186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 }
4188 }
4189
4190#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004191 if (key_name[0] == NUL
4192 /* URXVT mouse uses <ESC>[#;#;#M, but we are matching <ESC>[ */
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004193 || key_name[0] == KS_URXVT_MOUSE
4194# ifdef FEAT_MBYTE
4195 || u7_status == U7_SENT
4196# endif
4197 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 {
Bram Moolenaar9584b312013-03-13 19:29:28 +01004199 /* Check for some responses from terminal start with "<Esc>[" or
4200 * CSI.
4201 *
4202 * - xterm version string: <Esc>[>{x};{vers};{y}c
4203 * Also eat other possible responses to t_RV, rxvt returns
4204 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4205 * mrxvt has been reported to have "+" in the version. Assume
4206 * the escape sequence ends with a letter or one of "{|}~".
4207 *
4208 * - cursor position report: <Esc>[{row};{col}R
4209 * The final byte is 'R'. now it is only used for checking for
4210 * ambiguous-width character state.
4211 */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004212 p = tp[0] == CSI ? tp + 1 : tp + 2;
Bram Moolenaar9584b312013-03-13 19:29:28 +01004213 if ((*T_CRV != NUL || *T_U7 != NUL)
4214 && ((tp[0] == ESC && tp[1] == '[' && len >= 3)
Bram Moolenaar46a75612013-05-15 14:22:41 +02004215 || (tp[0] == CSI && len >= 2))
4216 && (VIM_ISDIGIT(*p) || *p == '>' || *p == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 {
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004218#ifdef FEAT_MBYTE
4219 int col;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004220 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 j = 0;
4223 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004224 for (i = 2 + (tp[0] != CSI); i < len
4225 && !(tp[i] >= '{' && tp[i] <= '~')
4226 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 if (tp[i] == ';' && ++j == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004228 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004229 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004230#ifdef FEAT_MBYTE
4231 row_char = tp[i - 1];
4232#endif
4233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004235 {
4236 LOG_TR("Not enough characters for CRV");
4237 return -1;
4238 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004239#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004240 if (extra > 0)
4241 col = atoi((char *)tp + extra);
4242 else
4243 col = 0;
4244
4245 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4246 * u7_status is not "sent", it may be from a previous Vim that
4247 * just exited. But not for <S-F3>, it sends something
4248 * similar, check for row and column to make sense. */
4249 if (j == 1 && tp[i] == 'R' && row_char == '2' && col >= 2)
Bram Moolenaar9584b312013-03-13 19:29:28 +01004250 {
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004251 char *aw = NULL;
Bram Moolenaar9584b312013-03-13 19:29:28 +01004252
Bram Moolenaar2951b772013-07-03 12:45:31 +02004253 LOG_TR("Received U7 status");
Bram Moolenaar9584b312013-03-13 19:29:28 +01004254 u7_status = U7_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004255# ifdef FEAT_AUTOCMD
4256 did_cursorhold = TRUE;
4257# endif
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004258 if (col == 2)
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004259 aw = "single";
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004260 else if (col == 3)
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004261 aw = "double";
Bram Moolenaar2951b772013-07-03 12:45:31 +02004262 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4263 {
4264 /* Setting the option causes a screen redraw. Do that
4265 * right away if possible, keeping any messages. */
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004266 set_option_value((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004267# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +02004268 {
4269 char buf[100];
4270 int r = redraw_asap(CLEAR);
4271
4272 sprintf(buf, "set 'ambiwidth', redraw_asap(): %d",
4273 r);
4274 log_tr(buf);
4275 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004276# else
Bram Moolenaar2951b772013-07-03 12:45:31 +02004277 redraw_asap(CLEAR);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004278# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02004279 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004280 key_name[0] = (int)KS_EXTRA;
4281 key_name[1] = (int)KE_IGNORE;
4282 slen = i + 1;
4283 }
4284 else
4285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004287 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004289 LOG_TR("Received CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 crv_status = CRV_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004291# ifdef FEAT_AUTOCMD
4292 did_cursorhold = TRUE;
4293# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294
4295 /* If this code starts with CSI, you can bet that the
4296 * terminal uses 8-bit codes. */
4297 if (tp[0] == CSI)
4298 switch_to_8bit();
4299
4300 /* rxvt sends its version number: "20703" is 2.7.3.
4301 * Ignore it for when the user has set 'term' to xterm,
4302 * even though it's an rxvt. */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004303 if (extra > 0)
4304 extra = atoi((char *)tp + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 if (extra > 20000)
4306 extra = 0;
4307
4308 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
4309 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004310 /* Only set 'ttymouse' automatically if it was not set
4311 * by the user already. */
4312 if (!option_was_set((char_u *)"ttym"))
4313 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004314# ifdef TTYM_SGR
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004315 if (extra >= 277)
4316 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004317 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004318 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004319# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004320 /* if xterm version >= 95 use mouse dragging */
4321 if (extra >= 95)
4322 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004324 }
4325
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 /* if xterm version >= 141 try to get termcap codes */
4327 if (extra >= 141)
4328 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004329 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 check_for_codes = TRUE;
4331 need_gather = TRUE;
4332 req_codes_from_term();
4333 }
4334 }
4335# ifdef FEAT_EVAL
4336 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4337# endif
4338# ifdef FEAT_AUTOCMD
4339 apply_autocmds(EVENT_TERMRESPONSE,
4340 NULL, NULL, FALSE, curbuf);
4341# endif
4342 key_name[0] = (int)KS_EXTRA;
4343 key_name[1] = (int)KE_IGNORE;
4344 slen = i + 1;
4345 }
4346 }
4347
4348 /* Check for '<Esc>P1+r<hex bytes><Esc>\'. A "0" instead of the
4349 * "1" means an invalid request. */
4350 else if (check_for_codes
4351 && ((tp[0] == ESC && tp[1] == 'P' && len >= 2)
4352 || tp[0] == DCS))
4353 {
4354 j = 1 + (tp[0] != DCS);
4355 for (i = j; i < len; ++i)
4356 if ((tp[i] == ESC && tp[i + 1] == '\\' && i + 1 < len)
4357 || tp[i] == STERM)
4358 {
4359 if (i - j >= 3 && tp[j + 1] == '+' && tp[j + 2] == 'r')
4360 got_code_from_term(tp + j, i);
4361 key_name[0] = (int)KS_EXTRA;
4362 key_name[1] = (int)KE_IGNORE;
4363 slen = i + 1 + (tp[i] == ESC);
4364 break;
4365 }
4366
4367 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004368 {
4369 LOG_TR("not enough characters for XT");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 return -1; /* not enough characters */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
4373 }
4374#endif
4375
4376 if (key_name[0] == NUL)
4377 continue; /* No match at this position, try next one */
4378
4379 /* We only get here when we have a complete termcode match */
4380
4381#ifdef FEAT_MOUSE
4382# ifdef FEAT_GUI
4383 /*
4384 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4385 * so that we know which window to scroll later.
4386 */
4387 if (gui.in_use
4388 && key_name[0] == (int)KS_EXTRA
4389 && (key_name[1] == (int)KE_X1MOUSE
4390 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004391 || key_name[1] == (int)KE_MOUSELEFT
4392 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 || key_name[1] == (int)KE_MOUSEDOWN
4394 || key_name[1] == (int)KE_MOUSEUP))
4395 {
4396 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4397 if (num_bytes == -1) /* not enough coordinates */
4398 return -1;
4399 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4400 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4401 slen += num_bytes;
4402 }
4403 else
4404# endif
4405 /*
4406 * If it is a mouse click, get the coordinates.
4407 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004408 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004410 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411# endif
4412# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004413 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414# endif
4415# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004416 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417# endif
4418# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004419 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004421# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004422 || key_name[0] == KS_URXVT_MOUSE
4423# endif
4424# ifdef FEAT_MOUSE_SGR
4425 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004426# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 )
4428 {
4429 is_click = is_drag = FALSE;
4430
Bram Moolenaar864207d2008-06-24 22:14:38 +00004431# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4432 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 if (key_name[0] == (int)KS_MOUSE)
4434 {
4435 /*
4436 * For xterm and MSDOS we get "<t_mouse>scr", where
4437 * s == encoded button state:
4438 * 0x20 = left button down
4439 * 0x21 = middle button down
4440 * 0x22 = right button down
4441 * 0x23 = any button release
4442 * 0x60 = button 4 down (scroll wheel down)
4443 * 0x61 = button 5 down (scroll wheel up)
4444 * add 0x04 for SHIFT
4445 * add 0x08 for ALT
4446 * add 0x10 for CTRL
4447 * add 0x20 for mouse drag (0x40 is drag with left button)
4448 * c == column + ' ' + 1 == column + 33
4449 * r == row + ' ' + 1 == row + 33
4450 *
4451 * The coordinates are passed on through global variables.
4452 * Ugly, but this avoids trouble with mouse clicks at an
4453 * unexpected moment and allows for mapping them.
4454 */
4455 for (;;)
4456 {
4457#ifdef FEAT_GUI
4458 if (gui.in_use)
4459 {
4460 /* GUI uses more bits for columns > 223 */
4461 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4462 if (num_bytes == -1) /* not enough coordinates */
4463 return -1;
4464 mouse_code = bytes[0];
4465 mouse_col = 128 * (bytes[1] - ' ' - 1)
4466 + bytes[2] - ' ' - 1;
4467 mouse_row = 128 * (bytes[3] - ' ' - 1)
4468 + bytes[4] - ' ' - 1;
4469 }
4470 else
4471#endif
4472 {
4473 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4474 if (num_bytes == -1) /* not enough coordinates */
4475 return -1;
4476 mouse_code = bytes[0];
4477 mouse_col = bytes[1] - ' ' - 1;
4478 mouse_row = bytes[2] - ' ' - 1;
4479 }
4480 slen += num_bytes;
4481
4482 /* If the following bytes is also a mouse code and it has
4483 * the same code, dump this one and get the next. This
4484 * makes dragging a whole lot faster. */
4485#ifdef FEAT_GUI
4486 if (gui.in_use)
4487 j = 3;
4488 else
4489#endif
4490 j = termcodes[idx].len;
4491 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4492 && tp[slen + j] == mouse_code
4493 && tp[slen + j + 1] != NUL
4494 && tp[slen + j + 2] != NUL
4495#ifdef FEAT_GUI
4496 && (!gui.in_use
4497 || (tp[slen + j + 3] != NUL
4498 && tp[slen + j + 4] != NUL))
4499#endif
4500 )
4501 slen += j;
4502 else
4503 break;
4504 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004507# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4508 if (key_name[0] == KS_URXVT_MOUSE
4509 || key_name[0] == KS_SGR_MOUSE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004510 {
4511 for (;;)
4512 {
4513 /* URXVT 1015 mouse reporting mode:
4514 * Almost identical to xterm mouse mode, except the values
4515 * are decimal instead of bytes.
4516 *
4517 * \033[%d;%d;%dM
4518 * ^-- row
4519 * ^----- column
4520 * ^-------- code
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004521 *
4522 * SGR 1006 mouse reporting mode:
4523 * Almost identical to xterm mouse mode, except the values
4524 * are decimal instead of bytes.
4525 *
4526 * \033[<%d;%d;%dM
4527 * ^-- row
4528 * ^----- column
4529 * ^-------- code
4530 *
4531 * \033[<%d;%d;%dm : mouse release event
4532 * ^-- row
4533 * ^----- column
4534 * ^-------- code
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004535 */
4536 p = tp + slen;
4537
4538 mouse_code = getdigits(&p);
4539 if (*p++ != ';')
4540 return -1;
4541
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004542 /* when mouse reporting is SGR, add 32 to mouse code */
4543 if (key_name[0] == KS_SGR_MOUSE)
4544 mouse_code += 32;
4545
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004546 mouse_col = getdigits(&p) - 1;
4547 if (*p++ != ';')
4548 return -1;
4549
4550 mouse_row = getdigits(&p) - 1;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004551 if (key_name[0] == KS_SGR_MOUSE && *p == 'm')
4552 mouse_code |= MOUSE_RELEASE;
4553 else if (*p != 'M')
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004554 return -1;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004555 p++;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004556
4557 slen += (int)(p - (tp + slen));
4558
4559 /* skip this one if next one has same code (like xterm
4560 * case) */
4561 j = termcodes[idx].len;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004562 if (STRNCMP(tp, tp + slen, (size_t)j) == 0)
4563 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004564 int slen2;
4565 int cmd_complete = 0;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004566
4567 /* check if the command is complete by looking for the
4568 * 'M' */
4569 for (slen2 = slen; slen2 < len; slen2++)
4570 {
4571 if (tp[slen2] == 'M'
4572 || (key_name[0] == KS_SGR_MOUSE
4573 && tp[slen2] == 'm'))
4574 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004575 cmd_complete = 1;
4576 break;
4577 }
4578 }
4579 p += j;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004580 if (cmd_complete && getdigits(&p) == mouse_code)
4581 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004582 slen += j; /* skip the \033[ */
4583 continue;
4584 }
4585 }
4586 break;
4587 }
4588 }
4589# endif
4590
4591 if (key_name[0] == (int)KS_MOUSE
4592#ifdef FEAT_MOUSE_URXVT
4593 || key_name[0] == (int)KS_URXVT_MOUSE
4594#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004595#ifdef FEAT_MOUSE_SGR
4596 || key_name[0] == KS_SGR_MOUSE
4597#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004598 )
4599 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600# if !defined(MSWIN) && !defined(MSDOS)
4601 /*
4602 * Handle mouse events.
4603 * Recognize the xterm mouse wheel, but not in the GUI, the
4604 * Linux console with GPM and the MS-DOS or Win32 console
4605 * (multi-clicks use >= 0x60).
4606 */
4607 if (mouse_code >= MOUSEWHEEL_LOW
4608# ifdef FEAT_GUI
4609 && !gui.in_use
4610# endif
4611# ifdef FEAT_MOUSE_GPM
4612 && gpm_flag == 0
4613# endif
4614 )
4615 {
4616 /* Keep the mouse_code before it's changed, so that we
4617 * remember that it was a mouse wheel click. */
4618 wheel_code = mouse_code;
4619 }
4620# ifdef FEAT_MOUSE_XTERM
4621 else if (held_button == MOUSE_RELEASE
4622# ifdef FEAT_GUI
4623 && !gui.in_use
4624# endif
4625 && (mouse_code == 0x23 || mouse_code == 0x24))
4626 {
4627 /* Apparently used by rxvt scroll wheel. */
4628 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4629 }
4630# endif
4631
4632# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4633 else if (use_xterm_mouse() > 1)
4634 {
4635 if (mouse_code & MOUSE_DRAG_XTERM)
4636 mouse_code |= MOUSE_DRAG;
4637 }
4638# endif
4639# ifdef FEAT_XCLIPBOARD
4640 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4641 {
4642 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4643 stop_xterm_trace();
4644 else
4645 start_xterm_trace(mouse_code);
4646 }
4647# endif
4648# endif
4649 }
4650# endif /* !UNIX || FEAT_MOUSE_XTERM */
4651# ifdef FEAT_MOUSE_NET
4652 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4653 {
4654 int mc, mr;
4655
4656 /* expect a rather limited sequence like: balancing {
4657 * \033}6,45\r
4658 * '6' is the row, 45 is the column
4659 */
4660 p = tp + slen;
4661 mr = getdigits(&p);
4662 if (*p++ != ',')
4663 return -1;
4664 mc = getdigits(&p);
4665 if (*p++ != '\r')
4666 return -1;
4667
4668 mouse_col = mc - 1;
4669 mouse_row = mr - 1;
4670 mouse_code = MOUSE_LEFT;
4671 slen += (int)(p - (tp + slen));
4672 }
4673# endif /* FEAT_MOUSE_NET */
4674# ifdef FEAT_MOUSE_JSB
4675 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4676 {
4677 int mult, val, iter, button, status;
4678
4679 /* JSBTERM Input Model
4680 * \033[0~zw uniq escape sequence
4681 * (L-x) Left button pressed - not pressed x not reporting
4682 * (M-x) Middle button pressed - not pressed x not reporting
4683 * (R-x) Right button pressed - not pressed x not reporting
4684 * (SDmdu) Single , Double click, m mouse move d button down
4685 * u button up
4686 * ### X cursor position padded to 3 digits
4687 * ### Y cursor position padded to 3 digits
4688 * (s-x) SHIFT key pressed - not pressed x not reporting
4689 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004690 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 */
4692
4693 p = tp + slen;
4694 button = mouse_code = 0;
4695 switch (*p++)
4696 {
4697 case 'L': button = 1; break;
4698 case '-': break;
4699 case 'x': break; /* ignore sequence */
4700 default: return -1; /* Unknown Result */
4701 }
4702 switch (*p++)
4703 {
4704 case 'M': button |= 2; break;
4705 case '-': break;
4706 case 'x': break; /* ignore sequence */
4707 default: return -1; /* Unknown Result */
4708 }
4709 switch (*p++)
4710 {
4711 case 'R': button |= 4; break;
4712 case '-': break;
4713 case 'x': break; /* ignore sequence */
4714 default: return -1; /* Unknown Result */
4715 }
4716 status = *p++;
4717 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4718 mult /= 10, p++)
4719 if (*p >= '0' && *p <= '9')
4720 val += (*p - '0') * mult;
4721 else
4722 return -1;
4723 mouse_col = val;
4724 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4725 mult /= 10, p++)
4726 if (*p >= '0' && *p <= '9')
4727 val += (*p - '0') * mult;
4728 else
4729 return -1;
4730 mouse_row = val;
4731 switch (*p++)
4732 {
4733 case 's': button |= 8; break; /* SHIFT key Pressed */
4734 case '-': break; /* Not Pressed */
4735 case 'x': break; /* Not Reporting */
4736 default: return -1; /* Unknown Result */
4737 }
4738 switch (*p++)
4739 {
4740 case 'c': button |= 16; break; /* CTRL key Pressed */
4741 case '-': break; /* Not Pressed */
4742 case 'x': break; /* Not Reporting */
4743 default: return -1; /* Unknown Result */
4744 }
4745 if (*p++ != '\033')
4746 return -1;
4747 if (*p++ != '\\')
4748 return -1;
4749 switch (status)
4750 {
4751 case 'D': /* Double Click */
4752 case 'S': /* Single Click */
4753 if (button & 1) mouse_code |= MOUSE_LEFT;
4754 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4755 if (button & 4) mouse_code |= MOUSE_RIGHT;
4756 if (button & 8) mouse_code |= MOUSE_SHIFT;
4757 if (button & 16) mouse_code |= MOUSE_CTRL;
4758 break;
4759 case 'm': /* Mouse move */
4760 if (button & 1) mouse_code |= MOUSE_LEFT;
4761 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4762 if (button & 4) mouse_code |= MOUSE_RIGHT;
4763 if (button & 8) mouse_code |= MOUSE_SHIFT;
4764 if (button & 16) mouse_code |= MOUSE_CTRL;
4765 if ((button & 7) != 0)
4766 {
4767 held_button = mouse_code;
4768 mouse_code |= MOUSE_DRAG;
4769 }
4770 is_drag = TRUE;
4771 showmode();
4772 break;
4773 case 'd': /* Button Down */
4774 if (button & 1) mouse_code |= MOUSE_LEFT;
4775 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4776 if (button & 4) mouse_code |= MOUSE_RIGHT;
4777 if (button & 8) mouse_code |= MOUSE_SHIFT;
4778 if (button & 16) mouse_code |= MOUSE_CTRL;
4779 break;
4780 case 'u': /* Button Up */
4781 if (button & 1)
4782 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4783 if (button & 2)
4784 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4785 if (button & 4)
4786 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4787 if (button & 8)
4788 mouse_code |= MOUSE_SHIFT;
4789 if (button & 16)
4790 mouse_code |= MOUSE_CTRL;
4791 break;
4792 default: return -1; /* Unknown Result */
4793 }
4794
4795 slen += (p - (tp + slen));
4796 }
4797# endif /* FEAT_MOUSE_JSB */
4798# ifdef FEAT_MOUSE_DEC
4799 if (key_name[0] == (int)KS_DEC_MOUSE)
4800 {
4801 /* The DEC Locator Input Model
4802 * Netterm delivers the code sequence:
4803 * \033[2;4;24;80&w (left button down)
4804 * \033[3;0;24;80&w (left button up)
4805 * \033[6;1;24;80&w (right button down)
4806 * \033[7;0;24;80&w (right button up)
4807 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4808 * Pe is the event code
4809 * Pb is the button code
4810 * Pr is the row coordinate
4811 * Pc is the column coordinate
4812 * Pp is the third coordinate (page number)
4813 * Pe, the event code indicates what event caused this report
4814 * The following event codes are defined:
4815 * 0 - request, the terminal received an explicit request
4816 * for a locator report, but the locator is unavailable
4817 * 1 - request, the terminal received an explicit request
4818 * for a locator report
4819 * 2 - left button down
4820 * 3 - left button up
4821 * 4 - middle button down
4822 * 5 - middle button up
4823 * 6 - right button down
4824 * 7 - right button up
4825 * 8 - fourth button down
4826 * 9 - fourth button up
4827 * 10 - locator outside filter rectangle
4828 * Pb, the button code, ASCII decimal 0-15 indicating which
4829 * buttons are down if any. The state of the four buttons
4830 * on the locator correspond to the low four bits of the
4831 * decimal value,
4832 * "1" means button depressed
4833 * 0 - no buttons down,
4834 * 1 - right,
4835 * 2 - middle,
4836 * 4 - left,
4837 * 8 - fourth
4838 * Pr is the row coordinate of the locator position in the page,
4839 * encoded as an ASCII decimal value.
4840 * If Pr is omitted, the locator position is undefined
4841 * (outside the terminal window for example).
4842 * Pc is the column coordinate of the locator position in the
4843 * page, encoded as an ASCII decimal value.
4844 * If Pc is omitted, the locator position is undefined
4845 * (outside the terminal window for example).
4846 * Pp is the page coordinate of the locator position
4847 * encoded as an ASCII decimal value.
4848 * The page coordinate may be omitted if the locator is on
4849 * page one (the default). We ignore it anyway.
4850 */
4851 int Pe, Pb, Pr, Pc;
4852
4853 p = tp + slen;
4854
4855 /* get event status */
4856 Pe = getdigits(&p);
4857 if (*p++ != ';')
4858 return -1;
4859
4860 /* get button status */
4861 Pb = getdigits(&p);
4862 if (*p++ != ';')
4863 return -1;
4864
4865 /* get row status */
4866 Pr = getdigits(&p);
4867 if (*p++ != ';')
4868 return -1;
4869
4870 /* get column status */
4871 Pc = getdigits(&p);
4872
4873 /* the page parameter is optional */
4874 if (*p == ';')
4875 {
4876 p++;
4877 (void)getdigits(&p);
4878 }
4879 if (*p++ != '&')
4880 return -1;
4881 if (*p++ != 'w')
4882 return -1;
4883
4884 mouse_code = 0;
4885 switch (Pe)
4886 {
4887 case 0: return -1; /* position request while unavailable */
4888 case 1: /* a response to a locator position request includes
4889 the status of all buttons */
4890 Pb &= 7; /* mask off and ignore fourth button */
4891 if (Pb & 4)
4892 mouse_code = MOUSE_LEFT;
4893 if (Pb & 2)
4894 mouse_code = MOUSE_MIDDLE;
4895 if (Pb & 1)
4896 mouse_code = MOUSE_RIGHT;
4897 if (Pb)
4898 {
4899 held_button = mouse_code;
4900 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004901 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903 is_drag = TRUE;
4904 showmode();
4905 break;
4906 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004907 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 break;
4909 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
4910 break;
4911 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004912 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 break;
4914 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
4915 break;
4916 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004917 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 break;
4919 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
4920 break;
4921 case 8: return -1; /* fourth button down */
4922 case 9: return -1; /* fourth button up */
4923 case 10: return -1; /* mouse outside of filter rectangle */
4924 default: return -1; /* should never occur */
4925 }
4926
4927 mouse_col = Pc - 1;
4928 mouse_row = Pr - 1;
4929
4930 slen += (int)(p - (tp + slen));
4931 }
4932# endif /* FEAT_MOUSE_DEC */
4933# ifdef FEAT_MOUSE_PTERM
4934 if (key_name[0] == (int)KS_PTERM_MOUSE)
4935 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004936 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937
4938 p = tp + slen;
4939
4940 action = getdigits(&p);
4941 if (*p++ != ';')
4942 return -1;
4943
4944 mouse_row = getdigits(&p);
4945 if (*p++ != ';')
4946 return -1;
4947 mouse_col = getdigits(&p);
4948 if (*p++ != ';')
4949 return -1;
4950
4951 button = getdigits(&p);
4952 mouse_code = 0;
4953
4954 switch( button )
4955 {
4956 case 4: mouse_code = MOUSE_LEFT; break;
4957 case 1: mouse_code = MOUSE_RIGHT; break;
4958 case 2: mouse_code = MOUSE_MIDDLE; break;
4959 default: return -1;
4960 }
4961
4962 switch( action )
4963 {
4964 case 31: /* Initial press */
4965 if (*p++ != ';')
4966 return -1;
4967
4968 num_clicks = getdigits(&p); /* Not used */
4969 break;
4970
4971 case 32: /* Release */
4972 mouse_code |= MOUSE_RELEASE;
4973 break;
4974
4975 case 33: /* Drag */
4976 held_button = mouse_code;
4977 mouse_code |= MOUSE_DRAG;
4978 break;
4979
4980 default:
4981 return -1;
4982 }
4983
4984 if (*p++ != 't')
4985 return -1;
4986
4987 slen += (p - (tp + slen));
4988 }
4989# endif /* FEAT_MOUSE_PTERM */
4990
4991 /* Interpret the mouse code */
4992 current_button = (mouse_code & MOUSE_CLICK_MASK);
4993 if (current_button == MOUSE_RELEASE
4994# ifdef FEAT_MOUSE_XTERM
4995 && wheel_code == 0
4996# endif
4997 )
4998 {
4999 /*
5000 * If we get a mouse drag or release event when
5001 * there is no mouse button held down (held_button ==
5002 * MOUSE_RELEASE), produce a K_IGNORE below.
5003 * (can happen when you hold down two buttons
5004 * and then let them go, or click in the menu bar, but not
5005 * on a menu, and drag into the text).
5006 */
5007 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5008 is_drag = TRUE;
5009 current_button = held_button;
5010 }
5011 else if (wheel_code == 0)
5012 {
5013# ifdef CHECK_DOUBLE_CLICK
5014# ifdef FEAT_MOUSE_GPM
5015# ifdef FEAT_GUI
5016 /*
5017 * Only for Unix, when GUI or gpm is not active, we handle
5018 * multi-clicks here.
5019 */
5020 if (gpm_flag == 0 && !gui.in_use)
5021# else
5022 if (gpm_flag == 0)
5023# endif
5024# else
5025# ifdef FEAT_GUI
5026 if (!gui.in_use)
5027# endif
5028# endif
5029 {
5030 /*
5031 * Compute the time elapsed since the previous mouse click.
5032 */
5033 gettimeofday(&mouse_time, NULL);
5034 timediff = (mouse_time.tv_usec
5035 - orig_mouse_time.tv_usec) / 1000;
5036 if (timediff < 0)
5037 --orig_mouse_time.tv_sec;
5038 timediff += (mouse_time.tv_sec
5039 - orig_mouse_time.tv_sec) * 1000;
5040 orig_mouse_time = mouse_time;
5041 if (mouse_code == orig_mouse_code
5042 && timediff < p_mouset
5043 && orig_num_clicks != 4
5044 && orig_mouse_col == mouse_col
5045 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005046 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005048 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005050 )
5051#ifdef FEAT_WINDOWS
5052 /* Double click in tab pages line also works
5053 * when window contents changes. */
5054 || (mouse_row == 0 && firstwin->w_winrow > 0)
5055#endif
5056 )
5057 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 ++orig_num_clicks;
5059 else
5060 orig_num_clicks = 1;
5061 orig_mouse_col = mouse_col;
5062 orig_mouse_row = mouse_row;
5063 orig_topline = curwin->w_topline;
5064#ifdef FEAT_DIFF
5065 orig_topfill = curwin->w_topfill;
5066#endif
5067 }
5068# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5069 else
5070 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5071# endif
5072# else
5073 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5074# endif
5075 is_click = TRUE;
5076 orig_mouse_code = mouse_code;
5077 }
5078 if (!is_drag)
5079 held_button = mouse_code & MOUSE_CLICK_MASK;
5080
5081 /*
5082 * Translate the actual mouse event into a pseudo mouse event.
5083 * First work out what modifiers are to be used.
5084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 if (orig_mouse_code & MOUSE_SHIFT)
5086 modifiers |= MOD_MASK_SHIFT;
5087 if (orig_mouse_code & MOUSE_CTRL)
5088 modifiers |= MOD_MASK_CTRL;
5089 if (orig_mouse_code & MOUSE_ALT)
5090 modifiers |= MOD_MASK_ALT;
5091 if (orig_num_clicks == 2)
5092 modifiers |= MOD_MASK_2CLICK;
5093 else if (orig_num_clicks == 3)
5094 modifiers |= MOD_MASK_3CLICK;
5095 else if (orig_num_clicks == 4)
5096 modifiers |= MOD_MASK_4CLICK;
5097
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 /* Work out our pseudo mouse event */
5099 key_name[0] = (int)KS_EXTRA;
5100 if (wheel_code != 0)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005101 {
5102 if (wheel_code & MOUSE_CTRL)
5103 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005104 if (wheel_code & MOUSE_ALT)
5105 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 key_name[1] = (wheel_code & 1)
5107 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 else
5110 key_name[1] = get_pseudo_mouse_code(current_button,
5111 is_click, is_drag);
5112 }
5113#endif /* FEAT_MOUSE */
5114
5115#ifdef FEAT_GUI
5116 /*
5117 * If using the GUI, then we get menu and scrollbar events.
5118 *
5119 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5120 * four bytes which are to be taken as a pointer to the vimmenu_T
5121 * structure.
5122 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005123 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005124 * is one byte with the tab index.
5125 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5127 * by one byte representing the scrollbar number, and then four bytes
5128 * representing a long_u which is the new value of the scrollbar.
5129 *
5130 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5131 * KE_FILLER followed by four bytes representing a long_u which is the
5132 * new value of the scrollbar.
5133 */
5134# ifdef FEAT_MENU
5135 else if (key_name[0] == (int)KS_MENU)
5136 {
5137 long_u val;
5138
5139 num_bytes = get_long_from_buf(tp + slen, &val);
5140 if (num_bytes == -1)
5141 return -1;
5142 current_menu = (vimmenu_T *)val;
5143 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005144
5145 /* The menu may have been deleted right after it was used, check
5146 * for that. */
5147 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5148 {
5149 key_name[0] = KS_EXTRA;
5150 key_name[1] = (int)KE_IGNORE;
5151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
5153# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005154# ifdef FEAT_GUI_TABLINE
5155 else if (key_name[0] == (int)KS_TABLINE)
5156 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005157 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005158 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5159 if (num_bytes == -1)
5160 return -1;
5161 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005162 if (current_tab == 255) /* -1 in a byte gives 255 */
5163 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005164 slen += num_bytes;
5165 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005166 else if (key_name[0] == (int)KS_TABMENU)
5167 {
5168 /* Selecting tabline tab or using its menu. */
5169 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5170 if (num_bytes == -1)
5171 return -1;
5172 current_tab = (int)bytes[0];
5173 current_tabmenu = (int)bytes[1];
5174 slen += num_bytes;
5175 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005176# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177# ifndef USE_ON_FLY_SCROLL
5178 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5179 {
5180 long_u val;
5181
5182 /* Get the last scrollbar event in the queue of the same type */
5183 j = 0;
5184 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5185 && tp[j + 2] != NUL; ++i)
5186 {
5187 j += 3;
5188 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5189 if (num_bytes == -1)
5190 break;
5191 if (i == 0)
5192 current_scrollbar = (int)bytes[0];
5193 else if (current_scrollbar != (int)bytes[0])
5194 break;
5195 j += num_bytes;
5196 num_bytes = get_long_from_buf(tp + j, &val);
5197 if (num_bytes == -1)
5198 break;
5199 scrollbar_value = val;
5200 j += num_bytes;
5201 slen = j;
5202 }
5203 if (i == 0) /* not enough characters to make one */
5204 return -1;
5205 }
5206 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5207 {
5208 long_u val;
5209
5210 /* Get the last horiz. scrollbar event in the queue */
5211 j = 0;
5212 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5213 && tp[j + 2] != NUL; ++i)
5214 {
5215 j += 3;
5216 num_bytes = get_long_from_buf(tp + j, &val);
5217 if (num_bytes == -1)
5218 break;
5219 scrollbar_value = val;
5220 j += num_bytes;
5221 slen = j;
5222 }
5223 if (i == 0) /* not enough characters to make one */
5224 return -1;
5225 }
5226# endif /* !USE_ON_FLY_SCROLL */
5227#endif /* FEAT_GUI */
5228
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005229 /*
5230 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5231 */
5232 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5233
5234 /*
5235 * Add any modifier codes to our string.
5236 */
5237 new_slen = 0; /* Length of what will replace the termcode */
5238 if (modifiers != 0)
5239 {
5240 /* Some keys have the modifier included. Need to handle that here
5241 * to make mappings work. */
5242 key = simplify_key(key, &modifiers);
5243 if (modifiers != 0)
5244 {
5245 string[new_slen++] = K_SPECIAL;
5246 string[new_slen++] = (int)KS_MODIFIER;
5247 string[new_slen++] = modifiers;
5248 }
5249 }
5250
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005252 key_name[0] = KEY2TERMCAP0(key);
5253 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005255 {
5256 /* from ":set <M-b>=xx" */
5257#ifdef FEAT_MBYTE
5258 if (has_mbyte)
5259 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5260 else
5261#endif
5262 string[new_slen++] = key_name[1];
5263 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005264 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5265 && key_name[1] == KE_IGNORE)
5266 {
5267 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5268 * to indicate what happened. */
5269 retval = KEYLEN_REMOVED;
5270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 else
5272 {
5273 string[new_slen++] = K_SPECIAL;
5274 string[new_slen++] = key_name[0];
5275 string[new_slen++] = key_name[1];
5276 }
5277 string[new_slen] = NUL;
5278 extra = new_slen - slen;
5279 if (buf == NULL)
5280 {
5281 if (extra < 0)
5282 /* remove matched chars, taking care of noremap */
5283 del_typebuf(-extra, offset);
5284 else if (extra > 0)
5285 /* insert the extra space we need */
5286 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5287
5288 /*
5289 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5290 * typebuf.tb_buf[]!
5291 */
5292 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5293 (size_t)new_slen);
5294 }
5295 else
5296 {
5297 if (extra < 0)
5298 /* remove matched characters */
5299 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005300 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005302 {
5303 /* Insert the extra space we need. If there is insufficient
5304 * space return -1. */
5305 if (*buflen + extra + new_slen >= bufsize)
5306 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005308 (size_t)(*buflen - offset));
5309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005311 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005313 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
5315
Bram Moolenaar2951b772013-07-03 12:45:31 +02005316#ifdef FEAT_TERMRESPONSE
5317 LOG_TR("normal character");
5318#endif
5319
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 return 0; /* no match found */
5321}
5322
5323/*
5324 * Replace any terminal code strings in from[] with the equivalent internal
5325 * vim representation. This is used for the "from" and "to" part of a
5326 * mapping, and the "to" part of a menu command.
5327 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5328 * '<'.
5329 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5330 *
5331 * The replacement is done in result[] and finally copied into allocated
5332 * memory. If this all works well *bufp is set to the allocated memory and a
5333 * pointer to it is returned. If something fails *bufp is set to NULL and from
5334 * is returned.
5335 *
5336 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5337 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5338 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5339 * instead of a CTRL-V.
5340 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005341 char_u *
5342replace_termcodes(from, bufp, from_part, do_lt, special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 char_u *from;
5344 char_u **bufp;
5345 int from_part;
5346 int do_lt; /* also translate <lt> */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005347 int special; /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348{
5349 int i;
5350 int slen;
5351 int key;
5352 int dlen = 0;
5353 char_u *src;
5354 int do_backslash; /* backslash is a special character */
5355 int do_special; /* recognize <> key codes */
5356 int do_key_code; /* recognize raw key codes */
5357 char_u *result; /* buffer for resulting string */
5358
5359 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005360 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5362
5363 /*
5364 * Allocate space for the translation. Worst case a single character is
5365 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5366 */
5367 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5368 if (result == NULL) /* out of memory */
5369 {
5370 *bufp = NULL;
5371 return from;
5372 }
5373
5374 src = from;
5375
5376 /*
5377 * Check for #n at start only: function key n
5378 */
5379 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5380 {
5381 result[dlen++] = K_SPECIAL;
5382 result[dlen++] = 'k';
5383 if (src[1] == '0')
5384 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5385 else
5386 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5387 src += 2;
5388 }
5389
5390 /*
5391 * Copy each byte from *from to result[dlen]
5392 */
5393 while (*src != NUL)
5394 {
5395 /*
5396 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005397 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 */
5399 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5400 {
5401#ifdef FEAT_EVAL
5402 /*
5403 * Replace <SID> by K_SNR <script-nr> _.
5404 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5405 */
5406 if (STRNICMP(src, "<SID>", 5) == 0)
5407 {
5408 if (current_SID <= 0)
5409 EMSG(_(e_usingsid));
5410 else
5411 {
5412 src += 5;
5413 result[dlen++] = K_SPECIAL;
5414 result[dlen++] = (int)KS_EXTRA;
5415 result[dlen++] = (int)KE_SNR;
5416 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5417 dlen += (int)STRLEN(result + dlen);
5418 result[dlen++] = '_';
5419 continue;
5420 }
5421 }
5422#endif
5423
5424 slen = trans_special(&src, result + dlen, TRUE);
5425 if (slen)
5426 {
5427 dlen += slen;
5428 continue;
5429 }
5430 }
5431
5432 /*
5433 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5434 * Note that this is also checked after replacing the <> form.
5435 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5436 * it could be a character in the file.
5437 */
5438 if (do_key_code)
5439 {
5440 i = find_term_bykeys(src);
5441 if (i >= 0)
5442 {
5443 result[dlen++] = K_SPECIAL;
5444 result[dlen++] = termcodes[i].name[0];
5445 result[dlen++] = termcodes[i].name[1];
5446 src += termcodes[i].len;
5447 /* If terminal code matched, continue after it. */
5448 continue;
5449 }
5450 }
5451
5452#ifdef FEAT_EVAL
5453 if (do_special)
5454 {
5455 char_u *p, *s, len;
5456
5457 /*
5458 * Replace <Leader> by the value of "mapleader".
5459 * Replace <LocalLeader> by the value of "maplocalleader".
5460 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5461 */
5462 if (STRNICMP(src, "<Leader>", 8) == 0)
5463 {
5464 len = 8;
5465 p = get_var_value((char_u *)"g:mapleader");
5466 }
5467 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5468 {
5469 len = 13;
5470 p = get_var_value((char_u *)"g:maplocalleader");
5471 }
5472 else
5473 {
5474 len = 0;
5475 p = NULL;
5476 }
5477 if (len != 0)
5478 {
5479 /* Allow up to 8 * 6 characters for "mapleader". */
5480 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5481 s = (char_u *)"\\";
5482 else
5483 s = p;
5484 while (*s != NUL)
5485 result[dlen++] = *s++;
5486 src += len;
5487 continue;
5488 }
5489 }
5490#endif
5491
5492 /*
5493 * Remove CTRL-V and ignore the next character.
5494 * For "from" side the CTRL-V at the end is included, for the "to"
5495 * part it is removed.
5496 * If 'cpoptions' does not contain 'B', also accept a backslash.
5497 */
5498 key = *src;
5499 if (key == Ctrl_V || (do_backslash && key == '\\'))
5500 {
5501 ++src; /* skip CTRL-V or backslash */
5502 if (*src == NUL)
5503 {
5504 if (from_part)
5505 result[dlen++] = key;
5506 break;
5507 }
5508 }
5509
5510#ifdef FEAT_MBYTE
5511 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005512 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513#endif
5514 {
5515 /*
5516 * If the character is K_SPECIAL, replace it with K_SPECIAL
5517 * KS_SPECIAL KE_FILLER.
5518 * If compiled with the GUI replace CSI with K_CSI.
5519 */
5520 if (*src == K_SPECIAL)
5521 {
5522 result[dlen++] = K_SPECIAL;
5523 result[dlen++] = KS_SPECIAL;
5524 result[dlen++] = KE_FILLER;
5525 }
5526# ifdef FEAT_GUI
5527 else if (*src == CSI)
5528 {
5529 result[dlen++] = K_SPECIAL;
5530 result[dlen++] = KS_EXTRA;
5531 result[dlen++] = (int)KE_CSI;
5532 }
5533# endif
5534 else
5535 result[dlen++] = *src;
5536 ++src;
5537 }
5538 }
5539 result[dlen] = NUL;
5540
5541 /*
5542 * Copy the new string to allocated memory.
5543 * If this fails, just return from.
5544 */
5545 if ((*bufp = vim_strsave(result)) != NULL)
5546 from = *bufp;
5547 vim_free(result);
5548 return from;
5549}
5550
5551/*
5552 * Find a termcode with keys 'src' (must be NUL terminated).
5553 * Return the index in termcodes[], or -1 if not found.
5554 */
5555 int
5556find_term_bykeys(src)
5557 char_u *src;
5558{
5559 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005560 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
5562 for (i = 0; i < tc_len; ++i)
5563 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005564 if (slen == termcodes[i].len
5565 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 return i;
5567 }
5568 return -1;
5569}
5570
5571/*
5572 * Gather the first characters in the terminal key codes into a string.
5573 * Used to speed up check_termcode().
5574 */
5575 static void
5576gather_termleader()
5577{
5578 int i;
5579 int len = 0;
5580
5581#ifdef FEAT_GUI
5582 if (gui.in_use)
5583 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5584#endif
5585#ifdef FEAT_TERMRESPONSE
5586 if (check_for_codes)
5587 termleader[len++] = DCS; /* the termcode response starts with DCS
5588 in 8-bit mode */
5589#endif
5590 termleader[len] = NUL;
5591
5592 for (i = 0; i < tc_len; ++i)
5593 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5594 {
5595 termleader[len++] = termcodes[i].code[0];
5596 termleader[len] = NUL;
5597 }
5598
5599 need_gather = FALSE;
5600}
5601
5602/*
5603 * Show all termcodes (for ":set termcap")
5604 * This code looks a lot like showoptions(), but is different.
5605 */
5606 void
5607show_termcodes()
5608{
5609 int col;
5610 int *items;
5611 int item_count;
5612 int run;
5613 int row, rows;
5614 int cols;
5615 int i;
5616 int len;
5617
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005618#define INC3 27 /* try to make three columns */
5619#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620#define GAP 2 /* spaces between columns */
5621
5622 if (tc_len == 0) /* no terminal codes (must be GUI) */
5623 return;
5624 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5625 if (items == NULL)
5626 return;
5627
5628 /* Highlight title */
5629 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5630
5631 /*
5632 * do the loop two times:
5633 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005634 * 2. display the medium items (medium length strings)
5635 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005637 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {
5639 /*
5640 * collect the items in items[]
5641 */
5642 item_count = 0;
5643 for (i = 0; i < tc_len; i++)
5644 {
5645 len = show_one_termcode(termcodes[i].name,
5646 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005647 if (len <= INC3 - GAP ? run == 1
5648 : len <= INC2 - GAP ? run == 2
5649 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 items[item_count++] = i;
5651 }
5652
5653 /*
5654 * display the items
5655 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005656 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005658 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 if (cols == 0)
5660 cols = 1;
5661 rows = (item_count + cols - 1) / cols;
5662 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005663 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 rows = item_count;
5665 for (row = 0; row < rows && !got_int; ++row)
5666 {
5667 msg_putchar('\n'); /* go to next line */
5668 if (got_int) /* 'q' typed in more */
5669 break;
5670 col = 0;
5671 for (i = row; i < item_count; i += rows)
5672 {
5673 msg_col = col; /* make columns */
5674 show_one_termcode(termcodes[items[i]].name,
5675 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005676 if (run == 2)
5677 col += INC2;
5678 else
5679 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
5681 out_flush();
5682 ui_breakcheck();
5683 }
5684 }
5685 vim_free(items);
5686}
5687
5688/*
5689 * Show one termcode entry.
5690 * Output goes into IObuff[]
5691 */
5692 int
5693show_one_termcode(name, code, printit)
5694 char_u *name;
5695 char_u *code;
5696 int printit;
5697{
5698 char_u *p;
5699 int len;
5700
5701 if (name[0] > '~')
5702 {
5703 IObuff[0] = ' ';
5704 IObuff[1] = ' ';
5705 IObuff[2] = ' ';
5706 IObuff[3] = ' ';
5707 }
5708 else
5709 {
5710 IObuff[0] = 't';
5711 IObuff[1] = '_';
5712 IObuff[2] = name[0];
5713 IObuff[3] = name[1];
5714 }
5715 IObuff[4] = ' ';
5716
5717 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5718 if (p[1] != 't')
5719 STRCPY(IObuff + 5, p);
5720 else
5721 IObuff[5] = NUL;
5722 len = (int)STRLEN(IObuff);
5723 do
5724 IObuff[len++] = ' ';
5725 while (len < 17);
5726 IObuff[len] = NUL;
5727 if (code == NULL)
5728 len += 4;
5729 else
5730 len += vim_strsize(code);
5731
5732 if (printit)
5733 {
5734 msg_puts(IObuff);
5735 if (code == NULL)
5736 msg_puts((char_u *)"NULL");
5737 else
5738 msg_outtrans(code);
5739 }
5740 return len;
5741}
5742
5743#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5744/*
5745 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5746 * termcap codes from the terminal itself.
5747 * We get them one by one to avoid a very long response string.
5748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 static void
5750req_codes_from_term()
5751{
5752 xt_index_out = 0;
5753 xt_index_in = 0;
5754 req_more_codes_from_term();
5755}
5756
5757 static void
5758req_more_codes_from_term()
5759{
5760 char buf[11];
5761 int old_idx = xt_index_out;
5762
5763 /* Don't do anything when going to exit. */
5764 if (exiting)
5765 return;
5766
5767 /* Send up to 10 more requests out than we received. Avoid sending too
5768 * many, there can be a buffer overflow somewhere. */
5769 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5770 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02005771# ifdef DEBUG_TERMRESPONSE
5772 char dbuf[100];
5773
5774 sprintf(dbuf, "Requesting XT %d: %s",
5775 xt_index_out, key_names[xt_index_out]);
5776 log_tr(dbuf);
5777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 sprintf(buf, "\033P+q%02x%02x\033\\",
5779 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5780 out_str_nf((char_u *)buf);
5781 ++xt_index_out;
5782 }
5783
5784 /* Send the codes out right away. */
5785 if (xt_index_out != old_idx)
5786 out_flush();
5787}
5788
5789/*
5790 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5791 * A "0" instead of the "1" indicates a code that isn't supported.
5792 * Both <name> and <string> are encoded in hex.
5793 * "code" points to the "0" or "1".
5794 */
5795 static void
5796got_code_from_term(code, len)
5797 char_u *code;
5798 int len;
5799{
5800#define XT_LEN 100
5801 char_u name[3];
5802 char_u str[XT_LEN];
5803 int i;
5804 int j = 0;
5805 int c;
5806
5807 /* A '1' means the code is supported, a '0' means it isn't.
5808 * When half the length is > XT_LEN we can't use it.
5809 * Our names are currently all 2 characters. */
5810 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5811 {
5812 /* Get the name from the response and find it in the table. */
5813 name[0] = hexhex2nr(code + 3);
5814 name[1] = hexhex2nr(code + 5);
5815 name[2] = NUL;
5816 for (i = 0; key_names[i] != NULL; ++i)
5817 {
5818 if (STRCMP(key_names[i], name) == 0)
5819 {
5820 xt_index_in = i;
5821 break;
5822 }
5823 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005824# ifdef DEBUG_TERMRESPONSE
5825 {
5826 char buf[100];
5827
5828 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
5829 log_tr(buf);
5830 }
5831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 if (key_names[i] != NULL)
5833 {
5834 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5835 str[j++] = c;
5836 str[j] = NUL;
5837 if (name[0] == 'C' && name[1] == 'o')
5838 {
5839 /* Color count is not a key code. */
5840 i = atoi((char *)str);
5841 if (i != t_colors)
5842 {
5843 /* Nr of colors changed, initialize highlighting and
Bram Moolenaar238a5642006-02-21 22:12:05 +00005844 * redraw everything. This causes a redraw, which usually
5845 * clears the message. Try keeping the message if it
5846 * might work. */
5847 set_keep_msg_from_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 set_color_count(i);
5849 init_highlight(TRUE, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +02005850#ifdef DEBUG_TERMRESPONSE
5851 {
5852 char buf[100];
5853 int r = redraw_asap(CLEAR);
5854
5855 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
5856 log_tr(buf);
5857 }
5858#else
5859 redraw_asap(CLEAR);
5860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 }
5862 }
5863 else
5864 {
5865 /* First delete any existing entry with the same code. */
5866 i = find_term_bykeys(str);
5867 if (i >= 0)
5868 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005869 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 }
5871 }
5872 }
5873
5874 /* May request more codes now that we received one. */
5875 ++xt_index_in;
5876 req_more_codes_from_term();
5877}
5878
5879/*
5880 * Check if there are any unanswered requests and deal with them.
5881 * This is called before starting an external program or getting direct
5882 * keyboard input. We don't want responses to be send to that program or
5883 * handled as typed text.
5884 */
5885 static void
5886check_for_codes_from_term()
5887{
5888 int c;
5889
5890 /* If no codes requested or all are answered, no need to wait. */
5891 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5892 return;
5893
5894 /* Vgetc() will check for and handle any response.
5895 * Keep calling vpeekc() until we don't get any responses. */
5896 ++no_mapping;
5897 ++allow_keys;
5898 for (;;)
5899 {
5900 c = vpeekc();
5901 if (c == NUL) /* nothing available */
5902 break;
5903
5904 /* If a response is recognized it's replaced with K_IGNORE, must read
5905 * it from the input stream. If there is no K_IGNORE we can't do
5906 * anything, break here (there might be some responses further on, but
5907 * we don't want to throw away any typed chars). */
5908 if (c != K_SPECIAL && c != K_IGNORE)
5909 break;
5910 c = vgetc();
5911 if (c != K_IGNORE)
5912 {
5913 vungetc(c);
5914 break;
5915 }
5916 }
5917 --no_mapping;
5918 --allow_keys;
5919}
5920#endif
5921
5922#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5923/*
5924 * Translate an internal mapping/abbreviation representation into the
5925 * corresponding external one recognized by :map/:abbrev commands;
5926 * respects the current B/k/< settings of 'cpoption'.
5927 *
5928 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00005929 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 *
5931 * It uses a growarray to build the translation string since the
5932 * latter can be wider than the original description. The caller has to
5933 * free the string afterwards.
5934 *
5935 * Returns NULL when there is a problem.
5936 */
5937 char_u *
5938translate_mapping(str, expmap)
5939 char_u *str;
5940 int expmap; /* TRUE when expanding mappings on command-line */
5941{
5942 garray_T ga;
5943 int c;
5944 int modifiers;
5945 int cpo_bslash;
5946 int cpo_special;
5947 int cpo_keycode;
5948
5949 ga_init(&ga);
5950 ga.ga_itemsize = 1;
5951 ga.ga_growsize = 40;
5952
5953 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
5954 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
5955 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5956
5957 for (; *str; ++str)
5958 {
5959 c = *str;
5960 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5961 {
5962 modifiers = 0;
5963 if (str[1] == KS_MODIFIER)
5964 {
5965 str++;
5966 modifiers = *++str;
5967 c = *++str;
5968 }
5969 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
5970 {
5971 int i;
5972
5973 /* try to find special key in termcodes */
5974 for (i = 0; i < tc_len; ++i)
5975 if (termcodes[i].name[0] == str[1]
5976 && termcodes[i].name[1] == str[2])
5977 break;
5978 if (i < tc_len)
5979 {
5980 ga_concat(&ga, termcodes[i].code);
5981 str += 2;
5982 continue; /* for (str) */
5983 }
5984 }
5985 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5986 {
5987 if (expmap && cpo_special)
5988 {
5989 ga_clear(&ga);
5990 return NULL;
5991 }
5992 c = TO_SPECIAL(str[1], str[2]);
5993 if (c == K_ZERO) /* display <Nul> as ^@ */
5994 c = NUL;
5995 str += 2;
5996 }
5997 if (IS_SPECIAL(c) || modifiers) /* special key */
5998 {
5999 if (expmap && cpo_special)
6000 {
6001 ga_clear(&ga);
6002 return NULL;
6003 }
6004 ga_concat(&ga, get_special_key_name(c, modifiers));
6005 continue; /* for (str) */
6006 }
6007 }
6008 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6009 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6010 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6011 if (c)
6012 ga_append(&ga, c);
6013 }
6014 ga_append(&ga, NUL);
6015 return (char_u *)(ga.ga_data);
6016}
6017#endif
6018
6019#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6020static char ksme_str[20];
6021static char ksmr_str[20];
6022static char ksmd_str[20];
6023
6024/*
6025 * For Win32 console: update termcap codes for existing console attributes.
6026 */
6027 void
6028update_tcap(attr)
6029 int attr;
6030{
6031 struct builtin_term *p;
6032
6033 p = find_builtin_term(DEFAULT_TERM);
6034 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6035 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6036 attr | 0x08); /* FOREGROUND_INTENSITY */
6037 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6038 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6039
6040 while (p->bt_string != NULL)
6041 {
6042 if (p->bt_entry == (int)KS_ME)
6043 p->bt_string = &ksme_str[0];
6044 else if (p->bt_entry == (int)KS_MR)
6045 p->bt_string = &ksmr_str[0];
6046 else if (p->bt_entry == (int)KS_MD)
6047 p->bt_string = &ksmd_str[0];
6048 ++p;
6049 }
6050}
6051#endif