blob: bd54421202c720a518996a43e591070f11ece8ac [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 Moolenaarcbc17d62014-05-22 21:22:19 +02003314#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
3315# if defined(UNIX) || defined(PROTO)
Bram Moolenaar90013c62014-05-22 18:14:31 +02003316/*
3317 * Return TRUE when the xterm version was requested or anything else that
3318 * would send an ESC sequence back to Vim.
3319 * If not sent yet, prevent it from being sent soon.
3320 * Used to check whether it is OK to enable checking for DEC mouse codes,
3321 * which conflict with may xterm ESC sequences.
3322 */
3323 int
3324did_request_esc_sequence()
3325{
3326 if (crv_status == CRV_GET)
3327 crv_status = 0;
3328 if (u7_status == U7_GET)
3329 u7_status = 0;
3330 return crv_status == CRV_SENT || u7_status == U7_SENT
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003331 || xt_index_out > xt_index_in;
Bram Moolenaar90013c62014-05-22 18:14:31 +02003332}
Bram Moolenaarcbc17d62014-05-22 21:22:19 +02003333# endif
Bram Moolenaar90013c62014-05-22 18:14:31 +02003334
3335
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336/*
3337 * Request version string (for xterm) when needed.
3338 * Only do this after switching to raw mode, otherwise the result will be
3339 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003340 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003341 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 * Only do this after termcap mode has been started, otherwise the codes for
3343 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003344 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3345 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003346 * On Unix only do it when both output and input are a tty (avoid writing
3347 * request to terminal while reading from a file).
Bram Moolenaar90013c62014-05-22 18:14:31 +02003348 * Do not do this when a mouse is being detected that starts with the same ESC
3349 * sequence as the termresponse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 * The result is caught in check_termcode().
3351 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003352 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353may_req_termresponse()
3354{
3355 if (crv_status == CRV_GET
3356 && cur_tmode == TMODE_RAW
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003357 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 && termcap_active
Bram Moolenaarebefac62005-12-28 22:39:57 +00003359 && p_ek
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003360# ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 && isatty(1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003362 && isatty(read_cmd_fd)
Bram Moolenaar90013c62014-05-22 18:14:31 +02003363 && !xterm_conflict_mouse
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 && *T_CRV != NUL)
3366 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02003367 LOG_TR("Sending CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 out_str(T_CRV);
3369 crv_status = CRV_SENT;
3370 /* check for the characters now, otherwise they might be eaten by
3371 * get_keystroke() */
3372 out_flush();
3373 (void)vpeekc_nomap();
3374 }
3375}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003376
3377# if defined(FEAT_MBYTE) || defined(PROTO)
3378/*
3379 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003380 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003381 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003382 * If the terminal treats \u25bd as single width, the position is (1, 1),
3383 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003384 * This function has the side effect that changes cursor position, so
3385 * it must be called immediately after entering termcap mode.
3386 */
3387 void
Bram Moolenaar386dcde2013-09-29 16:27:47 +02003388may_req_ambiguous_char_width()
Bram Moolenaar9584b312013-03-13 19:29:28 +01003389{
3390 if (u7_status == U7_GET
3391 && cur_tmode == TMODE_RAW
3392 && termcap_active
3393 && p_ek
3394# ifdef UNIX
3395 && isatty(1)
3396 && isatty(read_cmd_fd)
3397# endif
3398 && *T_U7 != NUL
3399 && !option_was_set((char_u *)"ambiwidth"))
3400 {
3401 char_u buf[16];
3402
Bram Moolenaar2951b772013-07-03 12:45:31 +02003403 LOG_TR("Sending U7 request");
3404 /* Do this in the second row. In the first row the returned sequence
3405 * may be CSI 1;2R, which is the same as <S-F3>. */
3406 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003407 buf[mb_char2bytes(0x25bd, buf)] = 0;
3408 out_str(buf);
3409 out_str(T_U7);
3410 u7_status = U7_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003411 out_flush();
3412 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003413 out_str((char_u *)" ");
3414 term_windgoto(0, 0);
3415 /* check for the characters now, otherwise they might be eaten by
3416 * get_keystroke() */
3417 out_flush();
3418 (void)vpeekc_nomap();
3419 }
3420}
3421# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003422
3423# ifdef DEBUG_TERMRESPONSE
3424 static void
3425log_tr(char *msg)
3426{
3427 static FILE *fd_tr = NULL;
3428 static proftime_T start;
3429 proftime_T now;
3430
3431 if (fd_tr == NULL)
3432 {
3433 fd_tr = fopen("termresponse.log", "w");
3434 profile_start(&start);
3435 }
3436 now = start;
3437 profile_end(&now);
3438 fprintf(fd_tr, "%s: %s %s\n",
3439 profile_msg(&now),
3440 must_redraw == NOT_VALID ? "NV"
3441 : must_redraw == CLEAR ? "CL" : " ",
3442 msg);
3443}
3444# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445#endif
3446
3447/*
3448 * Return TRUE when saving and restoring the screen.
3449 */
3450 int
3451swapping_screen()
3452{
3453 return (full_screen && *T_TI != NUL);
3454}
3455
3456#ifdef FEAT_MOUSE
3457/*
3458 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3459 */
3460 void
3461setmouse()
3462{
3463# ifdef FEAT_MOUSE_TTY
3464 int checkfor;
3465# endif
3466
3467# ifdef FEAT_MOUSESHAPE
3468 update_mouseshape(-1);
3469# endif
3470
3471# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3472# ifdef FEAT_GUI
3473 /* In the GUI the mouse is always enabled. */
3474 if (gui.in_use)
3475 return;
3476# endif
3477 /* be quick when mouse is off */
3478 if (*p_mouse == NUL || has_mouse_termcode == 0)
3479 return;
3480
3481 /* don't switch mouse on when not in raw mode (Ex mode) */
3482 if (cur_tmode != TMODE_RAW)
3483 {
3484 mch_setmouse(FALSE);
3485 return;
3486 }
3487
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (VIsual_active)
3489 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003490 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 checkfor = MOUSE_RETURN;
3492 else if (State & INSERT)
3493 checkfor = MOUSE_INSERT;
3494 else if (State & CMDLINE)
3495 checkfor = MOUSE_COMMAND;
3496 else if (State == CONFIRM || State == EXTERNCMD)
3497 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3498 else
3499 checkfor = MOUSE_NORMAL; /* assume normal mode */
3500
3501 if (mouse_has(checkfor))
3502 mch_setmouse(TRUE);
3503 else
3504 mch_setmouse(FALSE);
3505# endif
3506}
3507
3508/*
3509 * Return TRUE if
3510 * - "c" is in 'mouse', or
3511 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3512 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3513 * normal editing mode (not at hit-return message).
3514 */
3515 int
3516mouse_has(c)
3517 int c;
3518{
3519 char_u *p;
3520
3521 for (p = p_mouse; *p; ++p)
3522 switch (*p)
3523 {
3524 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3525 return TRUE;
3526 break;
3527 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3528 return TRUE;
3529 break;
3530 default: if (c == *p) return TRUE; break;
3531 }
3532 return FALSE;
3533}
3534
3535/*
3536 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3537 */
3538 int
3539mouse_model_popup()
3540{
3541 return (p_mousem[0] == 'p');
3542}
3543#endif
3544
3545/*
3546 * By outputting the 'cursor very visible' termcap code, for some windowed
3547 * terminals this makes the screen scrolled to the correct position.
3548 * Used when starting Vim or returning from a shell.
3549 */
3550 void
3551scroll_start()
3552{
3553 if (*T_VS != NUL)
3554 {
3555 out_str(T_VS);
3556 out_str(T_VE);
3557 screen_start(); /* don't know where cursor is now */
3558 }
3559}
3560
3561static int cursor_is_off = FALSE;
3562
3563/*
3564 * Enable the cursor.
3565 */
3566 void
3567cursor_on()
3568{
3569 if (cursor_is_off)
3570 {
3571 out_str(T_VE);
3572 cursor_is_off = FALSE;
3573 }
3574}
3575
3576/*
3577 * Disable the cursor.
3578 */
3579 void
3580cursor_off()
3581{
3582 if (full_screen)
3583 {
3584 if (!cursor_is_off)
3585 out_str(T_VI); /* disable cursor */
3586 cursor_is_off = TRUE;
3587 }
3588}
3589
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003590#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591/*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003592 * Set cursor shape to match Insert mode.
3593 */
3594 void
3595term_cursor_shape()
3596{
3597 static int showing_insert_mode = MAYBE;
3598
3599 if (!full_screen || *T_CSI == NUL || *T_CEI == NUL)
3600 return;
3601
3602 if (State & INSERT)
3603 {
3604 if (showing_insert_mode != TRUE)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003605 out_str(T_CSI); /* Insert mode cursor */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003606 showing_insert_mode = TRUE;
3607 }
3608 else
3609 {
3610 if (showing_insert_mode != FALSE)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003611 out_str(T_CEI); /* non-Insert mode cursor */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003612 showing_insert_mode = FALSE;
3613 }
3614}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003615#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003616
3617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 * Set scrolling region for window 'wp'.
3619 * The region starts 'off' lines from the start of the window.
3620 * Also set the vertical scroll region for a vertically split window. Always
3621 * the full width of the window, excluding the vertical separator.
3622 */
3623 void
3624scroll_region_set(wp, off)
3625 win_T *wp;
3626 int off;
3627{
3628 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3629 W_WINROW(wp) + off));
3630#ifdef FEAT_VERTSPLIT
3631 if (*T_CSV != NUL && wp->w_width != Columns)
3632 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3633 W_WINCOL(wp)));
3634#endif
3635 screen_start(); /* don't know where cursor is now */
3636}
3637
3638/*
3639 * Reset scrolling region to the whole screen.
3640 */
3641 void
3642scroll_region_reset()
3643{
3644 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
3645#ifdef FEAT_VERTSPLIT
3646 if (*T_CSV != NUL)
3647 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3648#endif
3649 screen_start(); /* don't know where cursor is now */
3650}
3651
3652
3653/*
3654 * List of terminal codes that are currently recognized.
3655 */
3656
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003657static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
3659 char_u name[2]; /* termcap name of entry */
3660 char_u *code; /* terminal code (in allocated memory) */
3661 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003662 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663} *termcodes = NULL;
3664
3665static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3666static int tc_len = 0; /* current number of entries in termcodes[] */
3667
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003668static int termcode_star __ARGS((char_u *code, int len));
3669
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 void
3671clear_termcodes()
3672{
3673 while (tc_len > 0)
3674 vim_free(termcodes[--tc_len].code);
3675 vim_free(termcodes);
3676 termcodes = NULL;
3677 tc_max_len = 0;
3678
3679#ifdef HAVE_TGETENT
3680 BC = (char *)empty_option;
3681 UP = (char *)empty_option;
3682 PC = NUL; /* set pad character to NUL */
3683 ospeed = 0;
3684#endif
3685
3686 need_gather = TRUE; /* need to fill termleader[] */
3687}
3688
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003689#define ATC_FROM_TERM 55
3690
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691/*
3692 * Add a new entry to the list of terminal codes.
3693 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003694 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3695 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 */
3697 void
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003698add_termcode(name, string, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 char_u *name;
3700 char_u *string;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003701 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702{
3703 struct termcode *new_tc;
3704 int i, j;
3705 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003706 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
3708 if (string == NULL || *string == NUL)
3709 {
3710 del_termcode(name);
3711 return;
3712 }
3713
3714 s = vim_strsave(string);
3715 if (s == NULL)
3716 return;
3717
3718 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003719 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003721 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 s[0] = term_7to8bit(string);
3723 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003724 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725
3726 need_gather = TRUE; /* need to fill termleader[] */
3727
3728 /*
3729 * need to make space for more entries
3730 */
3731 if (tc_len == tc_max_len)
3732 {
3733 tc_max_len += 20;
3734 new_tc = (struct termcode *)alloc(
3735 (unsigned)(tc_max_len * sizeof(struct termcode)));
3736 if (new_tc == NULL)
3737 {
3738 tc_max_len -= 20;
3739 return;
3740 }
3741 for (i = 0; i < tc_len; ++i)
3742 new_tc[i] = termcodes[i];
3743 vim_free(termcodes);
3744 termcodes = new_tc;
3745 }
3746
3747 /*
3748 * Look for existing entry with the same name, it is replaced.
3749 * Look for an existing entry that is alphabetical higher, the new entry
3750 * is inserted in front of it.
3751 */
3752 for (i = 0; i < tc_len; ++i)
3753 {
3754 if (termcodes[i].name[0] < name[0])
3755 continue;
3756 if (termcodes[i].name[0] == name[0])
3757 {
3758 if (termcodes[i].name[1] < name[1])
3759 continue;
3760 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003761 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 */
3763 if (termcodes[i].name[1] == name[1])
3764 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003765 if (flags == ATC_FROM_TERM && (j = termcode_star(
3766 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003767 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003768 /* Don't replace ESC[123;*X or ESC O*X with another when
3769 * invoked from got_code_from_term(). */
3770 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003771 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003772 && s[len - 1]
3773 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003774 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003775 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003776 vim_free(s);
3777 return;
3778 }
3779 }
3780 else
3781 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003782 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003783 vim_free(termcodes[i].code);
3784 --tc_len;
3785 break;
3786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
3788 }
3789 /*
3790 * Found alphabetical larger entry, move rest to insert new entry
3791 */
3792 for (j = tc_len; j > i; --j)
3793 termcodes[j] = termcodes[j - 1];
3794 break;
3795 }
3796
3797 termcodes[i].name[0] = name[0];
3798 termcodes[i].name[1] = name[1];
3799 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003800 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003801
3802 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3803 * accept modifiers. */
3804 termcodes[i].modlen = 0;
3805 j = termcode_star(s, len);
3806 if (j > 0)
3807 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 ++tc_len;
3809}
3810
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003811/*
3812 * Check termcode "code[len]" for ending in ;*X, <Esc>O*X or <M-O>*X.
3813 * The "X" can be any character.
3814 * Return 0 if not found, 2 for ;*X and 1 for O*X and <M-O>*X.
3815 */
3816 static int
3817termcode_star(code, len)
3818 char_u *code;
3819 int len;
3820{
3821 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3822 if (len >= 3 && code[len - 2] == '*')
3823 {
3824 if (len >= 5 && code[len - 3] == ';')
3825 return 2;
3826 if ((len >= 4 && code[len - 3] == 'O') || code[len - 3] == 'O' + 128)
3827 return 1;
3828 }
3829 return 0;
3830}
3831
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 char_u *
3833find_termcode(name)
3834 char_u *name;
3835{
3836 int i;
3837
3838 for (i = 0; i < tc_len; ++i)
3839 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3840 return termcodes[i].code;
3841 return NULL;
3842}
3843
3844#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3845 char_u *
3846get_termcode(i)
3847 int i;
3848{
3849 if (i >= tc_len)
3850 return NULL;
3851 return &termcodes[i].name[0];
3852}
3853#endif
3854
3855 void
3856del_termcode(name)
3857 char_u *name;
3858{
3859 int i;
3860
3861 if (termcodes == NULL) /* nothing there yet */
3862 return;
3863
3864 need_gather = TRUE; /* need to fill termleader[] */
3865
3866 for (i = 0; i < tc_len; ++i)
3867 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3868 {
3869 del_termcode_idx(i);
3870 return;
3871 }
3872 /* not found. Give error message? */
3873}
3874
3875 static void
3876del_termcode_idx(idx)
3877 int idx;
3878{
3879 int i;
3880
3881 vim_free(termcodes[idx].code);
3882 --tc_len;
3883 for (i = idx; i < tc_len; ++i)
3884 termcodes[i] = termcodes[i + 1];
3885}
3886
3887#ifdef FEAT_TERMRESPONSE
3888/*
3889 * Called when detected that the terminal sends 8-bit codes.
3890 * Convert all 7-bit codes to their 8-bit equivalent.
3891 */
3892 static void
3893switch_to_8bit()
3894{
3895 int i;
3896 int c;
3897
3898 /* Only need to do something when not already using 8-bit codes. */
3899 if (!term_is_8bit(T_NAME))
3900 {
3901 for (i = 0; i < tc_len; ++i)
3902 {
3903 c = term_7to8bit(termcodes[i].code);
3904 if (c != 0)
3905 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003906 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 termcodes[i].code[0] = c;
3908 }
3909 }
3910 need_gather = TRUE; /* need to fill termleader[] */
3911 }
3912 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003913 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914}
3915#endif
3916
3917#ifdef CHECK_DOUBLE_CLICK
3918static linenr_T orig_topline = 0;
3919# ifdef FEAT_DIFF
3920static int orig_topfill = 0;
3921# endif
3922#endif
3923#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
3924/*
3925 * Checking for double clicks ourselves.
3926 * "orig_topline" is used to avoid detecting a double-click when the window
3927 * contents scrolled (e.g., when 'scrolloff' is non-zero).
3928 */
3929/*
3930 * Set orig_topline. Used when jumping to another window, so that a double
3931 * click still works.
3932 */
3933 void
3934set_mouse_topline(wp)
3935 win_T *wp;
3936{
3937 orig_topline = wp->w_topline;
3938# ifdef FEAT_DIFF
3939 orig_topfill = wp->w_topfill;
3940# endif
3941}
3942#endif
3943
3944/*
3945 * Check if typebuf.tb_buf[] contains a terminal key code.
3946 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
3947 * + max_offset].
3948 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003949 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 * With a match, the match is removed, the replacement code is inserted in
3951 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
3952 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003953 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
3954 * "buflen" is then the length of the string in buf[] and is updated for
3955 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 */
3957 int
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003958check_termcode(max_offset, buf, bufsize, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 int max_offset;
3960 char_u *buf;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003961 int bufsize;
3962 int *buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963{
3964 char_u *tp;
3965 char_u *p;
3966 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003967 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003969 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 int offset;
3971 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003972 int modifiers;
3973 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 int new_slen;
3975 int extra;
3976 char_u string[MAX_KEY_CODE_LEN + 1];
3977 int i, j;
3978 int idx = 0;
3979#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00003980# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
3981 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 char_u bytes[6];
3983 int num_bytes;
3984# endif
3985 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 int is_click, is_drag;
3987 int wheel_code = 0;
3988 int current_button;
3989 static int held_button = MOUSE_RELEASE;
3990 static int orig_num_clicks = 1;
3991 static int orig_mouse_code = 0x0;
3992# ifdef CHECK_DOUBLE_CLICK
3993 static int orig_mouse_col = 0;
3994 static int orig_mouse_row = 0;
3995 static struct timeval orig_mouse_time = {0, 0};
3996 /* time of previous mouse click */
3997 struct timeval mouse_time; /* time of current mouse click */
3998 long timediff; /* elapsed time in msec */
3999# endif
4000#endif
4001 int cpo_koffset;
4002#ifdef FEAT_MOUSE_GPM
4003 extern int gpm_flag; /* gpm library variable */
4004#endif
4005
4006 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
4007
4008 /*
4009 * Speed up the checks for terminal codes by gathering all first bytes
4010 * used in termleader[]. Often this is just a single <Esc>.
4011 */
4012 if (need_gather)
4013 gather_termleader();
4014
4015 /*
4016 * Check at several positions in typebuf.tb_buf[], to catch something like
4017 * "x<Up>" that can be mapped. Stop at max_offset, because characters
4018 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004019 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 * This is used often, KEEP IT FAST!
4021 */
4022 for (offset = 0; offset < max_offset; ++offset)
4023 {
4024 if (buf == NULL)
4025 {
4026 if (offset >= typebuf.tb_len)
4027 break;
4028 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4029 len = typebuf.tb_len - offset; /* length of the input */
4030 }
4031 else
4032 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004033 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 break;
4035 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004036 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
4038
4039 /*
4040 * Don't check characters after K_SPECIAL, those are already
4041 * translated terminal chars (avoid translating ~@^Hx).
4042 */
4043 if (*tp == K_SPECIAL)
4044 {
4045 offset += 2; /* there are always 2 extra characters */
4046 continue;
4047 }
4048
4049 /*
4050 * Skip this position if the character does not appear as the first
4051 * character in term_strings. This speeds up a lot, since most
4052 * termcodes start with the same character (ESC or CSI).
4053 */
4054 i = *tp;
4055 for (p = termleader; *p && *p != i; ++p)
4056 ;
4057 if (*p == NUL)
4058 continue;
4059
4060 /*
4061 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4062 * are in Insert mode.
4063 */
4064 if (*tp == ESC && !p_ek && (State & INSERT))
4065 continue;
4066
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004068 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004069 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
4071#ifdef FEAT_GUI
4072 if (gui.in_use)
4073 {
4074 /*
4075 * GUI special key codes are all of the form [CSI xx].
4076 */
4077 if (*tp == CSI) /* Special key from GUI */
4078 {
4079 if (len < 3)
4080 return -1; /* Shouldn't happen */
4081 slen = 3;
4082 key_name[0] = tp[1];
4083 key_name[1] = tp[2];
4084 }
4085 }
4086 else
4087#endif /* FEAT_GUI */
4088 {
4089 for (idx = 0; idx < tc_len; ++idx)
4090 {
4091 /*
4092 * Ignore the entry if we are not at the start of
4093 * typebuf.tb_buf[]
4094 * and there are not enough characters to make a match.
4095 * But only when the 'K' flag is in 'cpoptions'.
4096 */
4097 slen = termcodes[idx].len;
4098 if (cpo_koffset && offset && len < slen)
4099 continue;
4100 if (STRNCMP(termcodes[idx].code, tp,
4101 (size_t)(slen > len ? len : slen)) == 0)
4102 {
4103 if (len < slen) /* got a partial sequence */
4104 return -1; /* need to get more chars */
4105
4106 /*
4107 * When found a keypad key, check if there is another key
4108 * that matches and use that one. This makes <Home> to be
4109 * found instead of <kHome> when they produce the same
4110 * key code.
4111 */
4112 if (termcodes[idx].name[0] == 'K'
4113 && VIM_ISDIGIT(termcodes[idx].name[1]))
4114 {
4115 for (j = idx + 1; j < tc_len; ++j)
4116 if (termcodes[j].len == slen &&
4117 STRNCMP(termcodes[idx].code,
4118 termcodes[j].code, slen) == 0)
4119 {
4120 idx = j;
4121 break;
4122 }
4123 }
4124
4125 key_name[0] = termcodes[idx].name[0];
4126 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 break;
4128 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004129
4130 /*
4131 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004132 * <Esc>[123;*X (modslen == slen - 3)
4133 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4134 * When there is a modifier the * matches a number.
4135 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004136 */
4137 if (termcodes[idx].modlen > 0)
4138 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004139 modslen = termcodes[idx].modlen;
4140 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004141 continue;
4142 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004143 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004144 {
4145 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004146
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004147 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004148 return -1; /* need to get more chars */
4149
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004150 if (tp[modslen] == termcodes[idx].code[slen - 1])
4151 slen = modslen + 1; /* no modifiers */
4152 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004153 continue; /* no match */
4154 else
4155 {
4156 /* Skip over the digits, the final char must
4157 * follow. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004158 for (j = slen - 2; j < len && isdigit(tp[j]); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004159 ;
4160 ++j;
4161 if (len < j) /* got a partial sequence */
4162 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004163 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4164 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004165
4166 /* Match! Convert modifier bits. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004167 n = atoi((char *)tp + slen - 2) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004168 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004169 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004170 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004171 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004172 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004173 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004174 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004175 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004176
4177 slen = j;
4178 }
4179 key_name[0] = termcodes[idx].name[0];
4180 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004181 break;
4182 }
4183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185 }
4186
4187#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004188 if (key_name[0] == NUL
4189 /* URXVT mouse uses <ESC>[#;#;#M, but we are matching <ESC>[ */
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004190 || key_name[0] == KS_URXVT_MOUSE
4191# ifdef FEAT_MBYTE
4192 || u7_status == U7_SENT
4193# endif
4194 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 {
Bram Moolenaar9584b312013-03-13 19:29:28 +01004196 /* Check for some responses from terminal start with "<Esc>[" or
4197 * CSI.
4198 *
4199 * - xterm version string: <Esc>[>{x};{vers};{y}c
4200 * Also eat other possible responses to t_RV, rxvt returns
4201 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4202 * mrxvt has been reported to have "+" in the version. Assume
4203 * the escape sequence ends with a letter or one of "{|}~".
4204 *
4205 * - cursor position report: <Esc>[{row};{col}R
4206 * The final byte is 'R'. now it is only used for checking for
4207 * ambiguous-width character state.
4208 */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004209 p = tp[0] == CSI ? tp + 1 : tp + 2;
Bram Moolenaar9584b312013-03-13 19:29:28 +01004210 if ((*T_CRV != NUL || *T_U7 != NUL)
4211 && ((tp[0] == ESC && tp[1] == '[' && len >= 3)
Bram Moolenaar46a75612013-05-15 14:22:41 +02004212 || (tp[0] == CSI && len >= 2))
4213 && (VIM_ISDIGIT(*p) || *p == '>' || *p == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 {
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004215#ifdef FEAT_MBYTE
4216 int col;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004217 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 j = 0;
4220 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004221 for (i = 2 + (tp[0] != CSI); i < len
4222 && !(tp[i] >= '{' && tp[i] <= '~')
4223 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 if (tp[i] == ';' && ++j == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004225 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004226 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004227#ifdef FEAT_MBYTE
4228 row_char = tp[i - 1];
4229#endif
4230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004232 {
4233 LOG_TR("Not enough characters for CRV");
4234 return -1;
4235 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004236#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004237 if (extra > 0)
4238 col = atoi((char *)tp + extra);
4239 else
4240 col = 0;
4241
4242 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4243 * u7_status is not "sent", it may be from a previous Vim that
4244 * just exited. But not for <S-F3>, it sends something
4245 * similar, check for row and column to make sense. */
4246 if (j == 1 && tp[i] == 'R' && row_char == '2' && col >= 2)
Bram Moolenaar9584b312013-03-13 19:29:28 +01004247 {
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004248 char *aw = NULL;
Bram Moolenaar9584b312013-03-13 19:29:28 +01004249
Bram Moolenaar2951b772013-07-03 12:45:31 +02004250 LOG_TR("Received U7 status");
Bram Moolenaar9584b312013-03-13 19:29:28 +01004251 u7_status = U7_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004252# ifdef FEAT_AUTOCMD
4253 did_cursorhold = TRUE;
4254# endif
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004255 if (col == 2)
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004256 aw = "single";
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004257 else if (col == 3)
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004258 aw = "double";
Bram Moolenaar2951b772013-07-03 12:45:31 +02004259 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4260 {
4261 /* Setting the option causes a screen redraw. Do that
4262 * right away if possible, keeping any messages. */
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004263 set_option_value((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004264# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +02004265 {
4266 char buf[100];
4267 int r = redraw_asap(CLEAR);
4268
4269 sprintf(buf, "set 'ambiwidth', redraw_asap(): %d",
4270 r);
4271 log_tr(buf);
4272 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004273# else
Bram Moolenaar2951b772013-07-03 12:45:31 +02004274 redraw_asap(CLEAR);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004275# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02004276 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004277 key_name[0] = (int)KS_EXTRA;
4278 key_name[1] = (int)KE_IGNORE;
4279 slen = i + 1;
4280 }
4281 else
4282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004284 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004286 LOG_TR("Received CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 crv_status = CRV_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004288# ifdef FEAT_AUTOCMD
4289 did_cursorhold = TRUE;
4290# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
4292 /* If this code starts with CSI, you can bet that the
4293 * terminal uses 8-bit codes. */
4294 if (tp[0] == CSI)
4295 switch_to_8bit();
4296
4297 /* rxvt sends its version number: "20703" is 2.7.3.
4298 * Ignore it for when the user has set 'term' to xterm,
4299 * even though it's an rxvt. */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004300 if (extra > 0)
4301 extra = atoi((char *)tp + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 if (extra > 20000)
4303 extra = 0;
4304
4305 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
4306 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004307 /* Only set 'ttymouse' automatically if it was not set
4308 * by the user already. */
4309 if (!option_was_set((char_u *)"ttym"))
4310 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004311# ifdef TTYM_SGR
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004312 if (extra >= 277)
4313 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004314 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004315 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004316# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004317 /* if xterm version >= 95 use mouse dragging */
4318 if (extra >= 95)
4319 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004321 }
4322
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 /* if xterm version >= 141 try to get termcap codes */
4324 if (extra >= 141)
4325 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004326 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 check_for_codes = TRUE;
4328 need_gather = TRUE;
4329 req_codes_from_term();
4330 }
4331 }
4332# ifdef FEAT_EVAL
4333 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4334# endif
4335# ifdef FEAT_AUTOCMD
4336 apply_autocmds(EVENT_TERMRESPONSE,
4337 NULL, NULL, FALSE, curbuf);
4338# endif
4339 key_name[0] = (int)KS_EXTRA;
4340 key_name[1] = (int)KE_IGNORE;
4341 slen = i + 1;
4342 }
4343 }
4344
4345 /* Check for '<Esc>P1+r<hex bytes><Esc>\'. A "0" instead of the
4346 * "1" means an invalid request. */
4347 else if (check_for_codes
4348 && ((tp[0] == ESC && tp[1] == 'P' && len >= 2)
4349 || tp[0] == DCS))
4350 {
4351 j = 1 + (tp[0] != DCS);
4352 for (i = j; i < len; ++i)
4353 if ((tp[i] == ESC && tp[i + 1] == '\\' && i + 1 < len)
4354 || tp[i] == STERM)
4355 {
4356 if (i - j >= 3 && tp[j + 1] == '+' && tp[j + 2] == 'r')
4357 got_code_from_term(tp + j, i);
4358 key_name[0] = (int)KS_EXTRA;
4359 key_name[1] = (int)KE_IGNORE;
4360 slen = i + 1 + (tp[i] == ESC);
4361 break;
4362 }
4363
4364 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004365 {
4366 LOG_TR("not enough characters for XT");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 return -1; /* not enough characters */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370 }
4371#endif
4372
4373 if (key_name[0] == NUL)
4374 continue; /* No match at this position, try next one */
4375
4376 /* We only get here when we have a complete termcode match */
4377
4378#ifdef FEAT_MOUSE
4379# ifdef FEAT_GUI
4380 /*
4381 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4382 * so that we know which window to scroll later.
4383 */
4384 if (gui.in_use
4385 && key_name[0] == (int)KS_EXTRA
4386 && (key_name[1] == (int)KE_X1MOUSE
4387 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004388 || key_name[1] == (int)KE_MOUSELEFT
4389 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 || key_name[1] == (int)KE_MOUSEDOWN
4391 || key_name[1] == (int)KE_MOUSEUP))
4392 {
4393 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4394 if (num_bytes == -1) /* not enough coordinates */
4395 return -1;
4396 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4397 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4398 slen += num_bytes;
4399 }
4400 else
4401# endif
4402 /*
4403 * If it is a mouse click, get the coordinates.
4404 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004405 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004407 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408# endif
4409# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004410 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411# endif
4412# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004413 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414# endif
4415# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004416 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004418# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004419 || key_name[0] == KS_URXVT_MOUSE
4420# endif
4421# ifdef FEAT_MOUSE_SGR
4422 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 )
4425 {
4426 is_click = is_drag = FALSE;
4427
Bram Moolenaar864207d2008-06-24 22:14:38 +00004428# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4429 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (key_name[0] == (int)KS_MOUSE)
4431 {
4432 /*
4433 * For xterm and MSDOS we get "<t_mouse>scr", where
4434 * s == encoded button state:
4435 * 0x20 = left button down
4436 * 0x21 = middle button down
4437 * 0x22 = right button down
4438 * 0x23 = any button release
4439 * 0x60 = button 4 down (scroll wheel down)
4440 * 0x61 = button 5 down (scroll wheel up)
4441 * add 0x04 for SHIFT
4442 * add 0x08 for ALT
4443 * add 0x10 for CTRL
4444 * add 0x20 for mouse drag (0x40 is drag with left button)
4445 * c == column + ' ' + 1 == column + 33
4446 * r == row + ' ' + 1 == row + 33
4447 *
4448 * The coordinates are passed on through global variables.
4449 * Ugly, but this avoids trouble with mouse clicks at an
4450 * unexpected moment and allows for mapping them.
4451 */
4452 for (;;)
4453 {
4454#ifdef FEAT_GUI
4455 if (gui.in_use)
4456 {
4457 /* GUI uses more bits for columns > 223 */
4458 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4459 if (num_bytes == -1) /* not enough coordinates */
4460 return -1;
4461 mouse_code = bytes[0];
4462 mouse_col = 128 * (bytes[1] - ' ' - 1)
4463 + bytes[2] - ' ' - 1;
4464 mouse_row = 128 * (bytes[3] - ' ' - 1)
4465 + bytes[4] - ' ' - 1;
4466 }
4467 else
4468#endif
4469 {
4470 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4471 if (num_bytes == -1) /* not enough coordinates */
4472 return -1;
4473 mouse_code = bytes[0];
4474 mouse_col = bytes[1] - ' ' - 1;
4475 mouse_row = bytes[2] - ' ' - 1;
4476 }
4477 slen += num_bytes;
4478
4479 /* If the following bytes is also a mouse code and it has
4480 * the same code, dump this one and get the next. This
4481 * makes dragging a whole lot faster. */
4482#ifdef FEAT_GUI
4483 if (gui.in_use)
4484 j = 3;
4485 else
4486#endif
4487 j = termcodes[idx].len;
4488 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4489 && tp[slen + j] == mouse_code
4490 && tp[slen + j + 1] != NUL
4491 && tp[slen + j + 2] != NUL
4492#ifdef FEAT_GUI
4493 && (!gui.in_use
4494 || (tp[slen + j + 3] != NUL
4495 && tp[slen + j + 4] != NUL))
4496#endif
4497 )
4498 slen += j;
4499 else
4500 break;
4501 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004504# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4505 if (key_name[0] == KS_URXVT_MOUSE
4506 || key_name[0] == KS_SGR_MOUSE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004507 {
4508 for (;;)
4509 {
4510 /* URXVT 1015 mouse reporting mode:
4511 * Almost identical to xterm mouse mode, except the values
4512 * are decimal instead of bytes.
4513 *
4514 * \033[%d;%d;%dM
4515 * ^-- row
4516 * ^----- column
4517 * ^-------- code
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004518 *
4519 * SGR 1006 mouse reporting mode:
4520 * Almost identical to xterm mouse mode, except the values
4521 * are decimal instead of bytes.
4522 *
4523 * \033[<%d;%d;%dM
4524 * ^-- row
4525 * ^----- column
4526 * ^-------- code
4527 *
4528 * \033[<%d;%d;%dm : mouse release event
4529 * ^-- row
4530 * ^----- column
4531 * ^-------- code
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004532 */
4533 p = tp + slen;
4534
4535 mouse_code = getdigits(&p);
4536 if (*p++ != ';')
4537 return -1;
4538
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004539 /* when mouse reporting is SGR, add 32 to mouse code */
4540 if (key_name[0] == KS_SGR_MOUSE)
4541 mouse_code += 32;
4542
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004543 mouse_col = getdigits(&p) - 1;
4544 if (*p++ != ';')
4545 return -1;
4546
4547 mouse_row = getdigits(&p) - 1;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004548 if (key_name[0] == KS_SGR_MOUSE && *p == 'm')
4549 mouse_code |= MOUSE_RELEASE;
4550 else if (*p != 'M')
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004551 return -1;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004552 p++;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004553
4554 slen += (int)(p - (tp + slen));
4555
4556 /* skip this one if next one has same code (like xterm
4557 * case) */
4558 j = termcodes[idx].len;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004559 if (STRNCMP(tp, tp + slen, (size_t)j) == 0)
4560 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004561 int slen2;
4562 int cmd_complete = 0;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004563
4564 /* check if the command is complete by looking for the
4565 * 'M' */
4566 for (slen2 = slen; slen2 < len; slen2++)
4567 {
4568 if (tp[slen2] == 'M'
4569 || (key_name[0] == KS_SGR_MOUSE
4570 && tp[slen2] == 'm'))
4571 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004572 cmd_complete = 1;
4573 break;
4574 }
4575 }
4576 p += j;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004577 if (cmd_complete && getdigits(&p) == mouse_code)
4578 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004579 slen += j; /* skip the \033[ */
4580 continue;
4581 }
4582 }
4583 break;
4584 }
4585 }
4586# endif
4587
4588 if (key_name[0] == (int)KS_MOUSE
4589#ifdef FEAT_MOUSE_URXVT
4590 || key_name[0] == (int)KS_URXVT_MOUSE
4591#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004592#ifdef FEAT_MOUSE_SGR
4593 || key_name[0] == KS_SGR_MOUSE
4594#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004595 )
4596 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597# if !defined(MSWIN) && !defined(MSDOS)
4598 /*
4599 * Handle mouse events.
4600 * Recognize the xterm mouse wheel, but not in the GUI, the
4601 * Linux console with GPM and the MS-DOS or Win32 console
4602 * (multi-clicks use >= 0x60).
4603 */
4604 if (mouse_code >= MOUSEWHEEL_LOW
4605# ifdef FEAT_GUI
4606 && !gui.in_use
4607# endif
4608# ifdef FEAT_MOUSE_GPM
4609 && gpm_flag == 0
4610# endif
4611 )
4612 {
4613 /* Keep the mouse_code before it's changed, so that we
4614 * remember that it was a mouse wheel click. */
4615 wheel_code = mouse_code;
4616 }
4617# ifdef FEAT_MOUSE_XTERM
4618 else if (held_button == MOUSE_RELEASE
4619# ifdef FEAT_GUI
4620 && !gui.in_use
4621# endif
4622 && (mouse_code == 0x23 || mouse_code == 0x24))
4623 {
4624 /* Apparently used by rxvt scroll wheel. */
4625 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4626 }
4627# endif
4628
4629# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4630 else if (use_xterm_mouse() > 1)
4631 {
4632 if (mouse_code & MOUSE_DRAG_XTERM)
4633 mouse_code |= MOUSE_DRAG;
4634 }
4635# endif
4636# ifdef FEAT_XCLIPBOARD
4637 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4638 {
4639 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4640 stop_xterm_trace();
4641 else
4642 start_xterm_trace(mouse_code);
4643 }
4644# endif
4645# endif
4646 }
4647# endif /* !UNIX || FEAT_MOUSE_XTERM */
4648# ifdef FEAT_MOUSE_NET
4649 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4650 {
4651 int mc, mr;
4652
4653 /* expect a rather limited sequence like: balancing {
4654 * \033}6,45\r
4655 * '6' is the row, 45 is the column
4656 */
4657 p = tp + slen;
4658 mr = getdigits(&p);
4659 if (*p++ != ',')
4660 return -1;
4661 mc = getdigits(&p);
4662 if (*p++ != '\r')
4663 return -1;
4664
4665 mouse_col = mc - 1;
4666 mouse_row = mr - 1;
4667 mouse_code = MOUSE_LEFT;
4668 slen += (int)(p - (tp + slen));
4669 }
4670# endif /* FEAT_MOUSE_NET */
4671# ifdef FEAT_MOUSE_JSB
4672 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4673 {
4674 int mult, val, iter, button, status;
4675
4676 /* JSBTERM Input Model
4677 * \033[0~zw uniq escape sequence
4678 * (L-x) Left button pressed - not pressed x not reporting
4679 * (M-x) Middle button pressed - not pressed x not reporting
4680 * (R-x) Right button pressed - not pressed x not reporting
4681 * (SDmdu) Single , Double click, m mouse move d button down
4682 * u button up
4683 * ### X cursor position padded to 3 digits
4684 * ### Y cursor position padded to 3 digits
4685 * (s-x) SHIFT key pressed - not pressed x not reporting
4686 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004687 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 */
4689
4690 p = tp + slen;
4691 button = mouse_code = 0;
4692 switch (*p++)
4693 {
4694 case 'L': button = 1; break;
4695 case '-': break;
4696 case 'x': break; /* ignore sequence */
4697 default: return -1; /* Unknown Result */
4698 }
4699 switch (*p++)
4700 {
4701 case 'M': button |= 2; break;
4702 case '-': break;
4703 case 'x': break; /* ignore sequence */
4704 default: return -1; /* Unknown Result */
4705 }
4706 switch (*p++)
4707 {
4708 case 'R': button |= 4; break;
4709 case '-': break;
4710 case 'x': break; /* ignore sequence */
4711 default: return -1; /* Unknown Result */
4712 }
4713 status = *p++;
4714 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4715 mult /= 10, p++)
4716 if (*p >= '0' && *p <= '9')
4717 val += (*p - '0') * mult;
4718 else
4719 return -1;
4720 mouse_col = val;
4721 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4722 mult /= 10, p++)
4723 if (*p >= '0' && *p <= '9')
4724 val += (*p - '0') * mult;
4725 else
4726 return -1;
4727 mouse_row = val;
4728 switch (*p++)
4729 {
4730 case 's': button |= 8; break; /* SHIFT key Pressed */
4731 case '-': break; /* Not Pressed */
4732 case 'x': break; /* Not Reporting */
4733 default: return -1; /* Unknown Result */
4734 }
4735 switch (*p++)
4736 {
4737 case 'c': button |= 16; break; /* CTRL key Pressed */
4738 case '-': break; /* Not Pressed */
4739 case 'x': break; /* Not Reporting */
4740 default: return -1; /* Unknown Result */
4741 }
4742 if (*p++ != '\033')
4743 return -1;
4744 if (*p++ != '\\')
4745 return -1;
4746 switch (status)
4747 {
4748 case 'D': /* Double Click */
4749 case 'S': /* Single Click */
4750 if (button & 1) mouse_code |= MOUSE_LEFT;
4751 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4752 if (button & 4) mouse_code |= MOUSE_RIGHT;
4753 if (button & 8) mouse_code |= MOUSE_SHIFT;
4754 if (button & 16) mouse_code |= MOUSE_CTRL;
4755 break;
4756 case 'm': /* Mouse move */
4757 if (button & 1) mouse_code |= MOUSE_LEFT;
4758 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4759 if (button & 4) mouse_code |= MOUSE_RIGHT;
4760 if (button & 8) mouse_code |= MOUSE_SHIFT;
4761 if (button & 16) mouse_code |= MOUSE_CTRL;
4762 if ((button & 7) != 0)
4763 {
4764 held_button = mouse_code;
4765 mouse_code |= MOUSE_DRAG;
4766 }
4767 is_drag = TRUE;
4768 showmode();
4769 break;
4770 case 'd': /* Button Down */
4771 if (button & 1) mouse_code |= MOUSE_LEFT;
4772 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4773 if (button & 4) mouse_code |= MOUSE_RIGHT;
4774 if (button & 8) mouse_code |= MOUSE_SHIFT;
4775 if (button & 16) mouse_code |= MOUSE_CTRL;
4776 break;
4777 case 'u': /* Button Up */
4778 if (button & 1)
4779 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4780 if (button & 2)
4781 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4782 if (button & 4)
4783 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4784 if (button & 8)
4785 mouse_code |= MOUSE_SHIFT;
4786 if (button & 16)
4787 mouse_code |= MOUSE_CTRL;
4788 break;
4789 default: return -1; /* Unknown Result */
4790 }
4791
4792 slen += (p - (tp + slen));
4793 }
4794# endif /* FEAT_MOUSE_JSB */
4795# ifdef FEAT_MOUSE_DEC
4796 if (key_name[0] == (int)KS_DEC_MOUSE)
4797 {
4798 /* The DEC Locator Input Model
4799 * Netterm delivers the code sequence:
4800 * \033[2;4;24;80&w (left button down)
4801 * \033[3;0;24;80&w (left button up)
4802 * \033[6;1;24;80&w (right button down)
4803 * \033[7;0;24;80&w (right button up)
4804 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4805 * Pe is the event code
4806 * Pb is the button code
4807 * Pr is the row coordinate
4808 * Pc is the column coordinate
4809 * Pp is the third coordinate (page number)
4810 * Pe, the event code indicates what event caused this report
4811 * The following event codes are defined:
4812 * 0 - request, the terminal received an explicit request
4813 * for a locator report, but the locator is unavailable
4814 * 1 - request, the terminal received an explicit request
4815 * for a locator report
4816 * 2 - left button down
4817 * 3 - left button up
4818 * 4 - middle button down
4819 * 5 - middle button up
4820 * 6 - right button down
4821 * 7 - right button up
4822 * 8 - fourth button down
4823 * 9 - fourth button up
4824 * 10 - locator outside filter rectangle
4825 * Pb, the button code, ASCII decimal 0-15 indicating which
4826 * buttons are down if any. The state of the four buttons
4827 * on the locator correspond to the low four bits of the
4828 * decimal value,
4829 * "1" means button depressed
4830 * 0 - no buttons down,
4831 * 1 - right,
4832 * 2 - middle,
4833 * 4 - left,
4834 * 8 - fourth
4835 * Pr is the row coordinate of the locator position in the page,
4836 * encoded as an ASCII decimal value.
4837 * If Pr is omitted, the locator position is undefined
4838 * (outside the terminal window for example).
4839 * Pc is the column coordinate of the locator position in the
4840 * page, encoded as an ASCII decimal value.
4841 * If Pc is omitted, the locator position is undefined
4842 * (outside the terminal window for example).
4843 * Pp is the page coordinate of the locator position
4844 * encoded as an ASCII decimal value.
4845 * The page coordinate may be omitted if the locator is on
4846 * page one (the default). We ignore it anyway.
4847 */
4848 int Pe, Pb, Pr, Pc;
4849
4850 p = tp + slen;
4851
4852 /* get event status */
4853 Pe = getdigits(&p);
4854 if (*p++ != ';')
4855 return -1;
4856
4857 /* get button status */
4858 Pb = getdigits(&p);
4859 if (*p++ != ';')
4860 return -1;
4861
4862 /* get row status */
4863 Pr = getdigits(&p);
4864 if (*p++ != ';')
4865 return -1;
4866
4867 /* get column status */
4868 Pc = getdigits(&p);
4869
4870 /* the page parameter is optional */
4871 if (*p == ';')
4872 {
4873 p++;
4874 (void)getdigits(&p);
4875 }
4876 if (*p++ != '&')
4877 return -1;
4878 if (*p++ != 'w')
4879 return -1;
4880
4881 mouse_code = 0;
4882 switch (Pe)
4883 {
4884 case 0: return -1; /* position request while unavailable */
4885 case 1: /* a response to a locator position request includes
4886 the status of all buttons */
4887 Pb &= 7; /* mask off and ignore fourth button */
4888 if (Pb & 4)
4889 mouse_code = MOUSE_LEFT;
4890 if (Pb & 2)
4891 mouse_code = MOUSE_MIDDLE;
4892 if (Pb & 1)
4893 mouse_code = MOUSE_RIGHT;
4894 if (Pb)
4895 {
4896 held_button = mouse_code;
4897 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004898 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900 is_drag = TRUE;
4901 showmode();
4902 break;
4903 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004904 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 break;
4906 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
4907 break;
4908 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004909 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 break;
4911 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
4912 break;
4913 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004914 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 break;
4916 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
4917 break;
4918 case 8: return -1; /* fourth button down */
4919 case 9: return -1; /* fourth button up */
4920 case 10: return -1; /* mouse outside of filter rectangle */
4921 default: return -1; /* should never occur */
4922 }
4923
4924 mouse_col = Pc - 1;
4925 mouse_row = Pr - 1;
4926
4927 slen += (int)(p - (tp + slen));
4928 }
4929# endif /* FEAT_MOUSE_DEC */
4930# ifdef FEAT_MOUSE_PTERM
4931 if (key_name[0] == (int)KS_PTERM_MOUSE)
4932 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004933 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934
4935 p = tp + slen;
4936
4937 action = getdigits(&p);
4938 if (*p++ != ';')
4939 return -1;
4940
4941 mouse_row = getdigits(&p);
4942 if (*p++ != ';')
4943 return -1;
4944 mouse_col = getdigits(&p);
4945 if (*p++ != ';')
4946 return -1;
4947
4948 button = getdigits(&p);
4949 mouse_code = 0;
4950
4951 switch( button )
4952 {
4953 case 4: mouse_code = MOUSE_LEFT; break;
4954 case 1: mouse_code = MOUSE_RIGHT; break;
4955 case 2: mouse_code = MOUSE_MIDDLE; break;
4956 default: return -1;
4957 }
4958
4959 switch( action )
4960 {
4961 case 31: /* Initial press */
4962 if (*p++ != ';')
4963 return -1;
4964
4965 num_clicks = getdigits(&p); /* Not used */
4966 break;
4967
4968 case 32: /* Release */
4969 mouse_code |= MOUSE_RELEASE;
4970 break;
4971
4972 case 33: /* Drag */
4973 held_button = mouse_code;
4974 mouse_code |= MOUSE_DRAG;
4975 break;
4976
4977 default:
4978 return -1;
4979 }
4980
4981 if (*p++ != 't')
4982 return -1;
4983
4984 slen += (p - (tp + slen));
4985 }
4986# endif /* FEAT_MOUSE_PTERM */
4987
4988 /* Interpret the mouse code */
4989 current_button = (mouse_code & MOUSE_CLICK_MASK);
4990 if (current_button == MOUSE_RELEASE
4991# ifdef FEAT_MOUSE_XTERM
4992 && wheel_code == 0
4993# endif
4994 )
4995 {
4996 /*
4997 * If we get a mouse drag or release event when
4998 * there is no mouse button held down (held_button ==
4999 * MOUSE_RELEASE), produce a K_IGNORE below.
5000 * (can happen when you hold down two buttons
5001 * and then let them go, or click in the menu bar, but not
5002 * on a menu, and drag into the text).
5003 */
5004 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
5005 is_drag = TRUE;
5006 current_button = held_button;
5007 }
5008 else if (wheel_code == 0)
5009 {
5010# ifdef CHECK_DOUBLE_CLICK
5011# ifdef FEAT_MOUSE_GPM
5012# ifdef FEAT_GUI
5013 /*
5014 * Only for Unix, when GUI or gpm is not active, we handle
5015 * multi-clicks here.
5016 */
5017 if (gpm_flag == 0 && !gui.in_use)
5018# else
5019 if (gpm_flag == 0)
5020# endif
5021# else
5022# ifdef FEAT_GUI
5023 if (!gui.in_use)
5024# endif
5025# endif
5026 {
5027 /*
5028 * Compute the time elapsed since the previous mouse click.
5029 */
5030 gettimeofday(&mouse_time, NULL);
5031 timediff = (mouse_time.tv_usec
5032 - orig_mouse_time.tv_usec) / 1000;
5033 if (timediff < 0)
5034 --orig_mouse_time.tv_sec;
5035 timediff += (mouse_time.tv_sec
5036 - orig_mouse_time.tv_sec) * 1000;
5037 orig_mouse_time = mouse_time;
5038 if (mouse_code == orig_mouse_code
5039 && timediff < p_mouset
5040 && orig_num_clicks != 4
5041 && orig_mouse_col == mouse_col
5042 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005043 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005045 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005047 )
5048#ifdef FEAT_WINDOWS
5049 /* Double click in tab pages line also works
5050 * when window contents changes. */
5051 || (mouse_row == 0 && firstwin->w_winrow > 0)
5052#endif
5053 )
5054 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 ++orig_num_clicks;
5056 else
5057 orig_num_clicks = 1;
5058 orig_mouse_col = mouse_col;
5059 orig_mouse_row = mouse_row;
5060 orig_topline = curwin->w_topline;
5061#ifdef FEAT_DIFF
5062 orig_topfill = curwin->w_topfill;
5063#endif
5064 }
5065# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5066 else
5067 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5068# endif
5069# else
5070 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5071# endif
5072 is_click = TRUE;
5073 orig_mouse_code = mouse_code;
5074 }
5075 if (!is_drag)
5076 held_button = mouse_code & MOUSE_CLICK_MASK;
5077
5078 /*
5079 * Translate the actual mouse event into a pseudo mouse event.
5080 * First work out what modifiers are to be used.
5081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 if (orig_mouse_code & MOUSE_SHIFT)
5083 modifiers |= MOD_MASK_SHIFT;
5084 if (orig_mouse_code & MOUSE_CTRL)
5085 modifiers |= MOD_MASK_CTRL;
5086 if (orig_mouse_code & MOUSE_ALT)
5087 modifiers |= MOD_MASK_ALT;
5088 if (orig_num_clicks == 2)
5089 modifiers |= MOD_MASK_2CLICK;
5090 else if (orig_num_clicks == 3)
5091 modifiers |= MOD_MASK_3CLICK;
5092 else if (orig_num_clicks == 4)
5093 modifiers |= MOD_MASK_4CLICK;
5094
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 /* Work out our pseudo mouse event */
5096 key_name[0] = (int)KS_EXTRA;
5097 if (wheel_code != 0)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005098 {
5099 if (wheel_code & MOUSE_CTRL)
5100 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005101 if (wheel_code & MOUSE_ALT)
5102 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 key_name[1] = (wheel_code & 1)
5104 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 else
5107 key_name[1] = get_pseudo_mouse_code(current_button,
5108 is_click, is_drag);
5109 }
5110#endif /* FEAT_MOUSE */
5111
5112#ifdef FEAT_GUI
5113 /*
5114 * If using the GUI, then we get menu and scrollbar events.
5115 *
5116 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5117 * four bytes which are to be taken as a pointer to the vimmenu_T
5118 * structure.
5119 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005120 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005121 * is one byte with the tab index.
5122 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5124 * by one byte representing the scrollbar number, and then four bytes
5125 * representing a long_u which is the new value of the scrollbar.
5126 *
5127 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5128 * KE_FILLER followed by four bytes representing a long_u which is the
5129 * new value of the scrollbar.
5130 */
5131# ifdef FEAT_MENU
5132 else if (key_name[0] == (int)KS_MENU)
5133 {
5134 long_u val;
5135
5136 num_bytes = get_long_from_buf(tp + slen, &val);
5137 if (num_bytes == -1)
5138 return -1;
5139 current_menu = (vimmenu_T *)val;
5140 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005141
5142 /* The menu may have been deleted right after it was used, check
5143 * for that. */
5144 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5145 {
5146 key_name[0] = KS_EXTRA;
5147 key_name[1] = (int)KE_IGNORE;
5148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005151# ifdef FEAT_GUI_TABLINE
5152 else if (key_name[0] == (int)KS_TABLINE)
5153 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005154 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005155 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5156 if (num_bytes == -1)
5157 return -1;
5158 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005159 if (current_tab == 255) /* -1 in a byte gives 255 */
5160 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005161 slen += num_bytes;
5162 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005163 else if (key_name[0] == (int)KS_TABMENU)
5164 {
5165 /* Selecting tabline tab or using its menu. */
5166 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5167 if (num_bytes == -1)
5168 return -1;
5169 current_tab = (int)bytes[0];
5170 current_tabmenu = (int)bytes[1];
5171 slen += num_bytes;
5172 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174# ifndef USE_ON_FLY_SCROLL
5175 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5176 {
5177 long_u val;
5178
5179 /* Get the last scrollbar event in the queue of the same type */
5180 j = 0;
5181 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5182 && tp[j + 2] != NUL; ++i)
5183 {
5184 j += 3;
5185 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5186 if (num_bytes == -1)
5187 break;
5188 if (i == 0)
5189 current_scrollbar = (int)bytes[0];
5190 else if (current_scrollbar != (int)bytes[0])
5191 break;
5192 j += num_bytes;
5193 num_bytes = get_long_from_buf(tp + j, &val);
5194 if (num_bytes == -1)
5195 break;
5196 scrollbar_value = val;
5197 j += num_bytes;
5198 slen = j;
5199 }
5200 if (i == 0) /* not enough characters to make one */
5201 return -1;
5202 }
5203 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5204 {
5205 long_u val;
5206
5207 /* Get the last horiz. scrollbar event in the queue */
5208 j = 0;
5209 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5210 && tp[j + 2] != NUL; ++i)
5211 {
5212 j += 3;
5213 num_bytes = get_long_from_buf(tp + j, &val);
5214 if (num_bytes == -1)
5215 break;
5216 scrollbar_value = val;
5217 j += num_bytes;
5218 slen = j;
5219 }
5220 if (i == 0) /* not enough characters to make one */
5221 return -1;
5222 }
5223# endif /* !USE_ON_FLY_SCROLL */
5224#endif /* FEAT_GUI */
5225
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005226 /*
5227 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5228 */
5229 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5230
5231 /*
5232 * Add any modifier codes to our string.
5233 */
5234 new_slen = 0; /* Length of what will replace the termcode */
5235 if (modifiers != 0)
5236 {
5237 /* Some keys have the modifier included. Need to handle that here
5238 * to make mappings work. */
5239 key = simplify_key(key, &modifiers);
5240 if (modifiers != 0)
5241 {
5242 string[new_slen++] = K_SPECIAL;
5243 string[new_slen++] = (int)KS_MODIFIER;
5244 string[new_slen++] = modifiers;
5245 }
5246 }
5247
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005249 key_name[0] = KEY2TERMCAP0(key);
5250 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005252 {
5253 /* from ":set <M-b>=xx" */
5254#ifdef FEAT_MBYTE
5255 if (has_mbyte)
5256 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5257 else
5258#endif
5259 string[new_slen++] = key_name[1];
5260 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005261 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5262 && key_name[1] == KE_IGNORE)
5263 {
5264 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5265 * to indicate what happened. */
5266 retval = KEYLEN_REMOVED;
5267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 else
5269 {
5270 string[new_slen++] = K_SPECIAL;
5271 string[new_slen++] = key_name[0];
5272 string[new_slen++] = key_name[1];
5273 }
5274 string[new_slen] = NUL;
5275 extra = new_slen - slen;
5276 if (buf == NULL)
5277 {
5278 if (extra < 0)
5279 /* remove matched chars, taking care of noremap */
5280 del_typebuf(-extra, offset);
5281 else if (extra > 0)
5282 /* insert the extra space we need */
5283 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5284
5285 /*
5286 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5287 * typebuf.tb_buf[]!
5288 */
5289 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5290 (size_t)new_slen);
5291 }
5292 else
5293 {
5294 if (extra < 0)
5295 /* remove matched characters */
5296 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005297 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005299 {
5300 /* Insert the extra space we need. If there is insufficient
5301 * space return -1. */
5302 if (*buflen + extra + new_slen >= bufsize)
5303 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005305 (size_t)(*buflen - offset));
5306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005308 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005310 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 }
5312
Bram Moolenaar2951b772013-07-03 12:45:31 +02005313#ifdef FEAT_TERMRESPONSE
5314 LOG_TR("normal character");
5315#endif
5316
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 return 0; /* no match found */
5318}
5319
5320/*
5321 * Replace any terminal code strings in from[] with the equivalent internal
5322 * vim representation. This is used for the "from" and "to" part of a
5323 * mapping, and the "to" part of a menu command.
5324 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5325 * '<'.
5326 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5327 *
5328 * The replacement is done in result[] and finally copied into allocated
5329 * memory. If this all works well *bufp is set to the allocated memory and a
5330 * pointer to it is returned. If something fails *bufp is set to NULL and from
5331 * is returned.
5332 *
5333 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5334 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5335 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5336 * instead of a CTRL-V.
5337 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005338 char_u *
5339replace_termcodes(from, bufp, from_part, do_lt, special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 char_u *from;
5341 char_u **bufp;
5342 int from_part;
5343 int do_lt; /* also translate <lt> */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005344 int special; /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345{
5346 int i;
5347 int slen;
5348 int key;
5349 int dlen = 0;
5350 char_u *src;
5351 int do_backslash; /* backslash is a special character */
5352 int do_special; /* recognize <> key codes */
5353 int do_key_code; /* recognize raw key codes */
5354 char_u *result; /* buffer for resulting string */
5355
5356 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005357 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5359
5360 /*
5361 * Allocate space for the translation. Worst case a single character is
5362 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5363 */
5364 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5365 if (result == NULL) /* out of memory */
5366 {
5367 *bufp = NULL;
5368 return from;
5369 }
5370
5371 src = from;
5372
5373 /*
5374 * Check for #n at start only: function key n
5375 */
5376 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5377 {
5378 result[dlen++] = K_SPECIAL;
5379 result[dlen++] = 'k';
5380 if (src[1] == '0')
5381 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5382 else
5383 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5384 src += 2;
5385 }
5386
5387 /*
5388 * Copy each byte from *from to result[dlen]
5389 */
5390 while (*src != NUL)
5391 {
5392 /*
5393 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005394 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 */
5396 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5397 {
5398#ifdef FEAT_EVAL
5399 /*
5400 * Replace <SID> by K_SNR <script-nr> _.
5401 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5402 */
5403 if (STRNICMP(src, "<SID>", 5) == 0)
5404 {
5405 if (current_SID <= 0)
5406 EMSG(_(e_usingsid));
5407 else
5408 {
5409 src += 5;
5410 result[dlen++] = K_SPECIAL;
5411 result[dlen++] = (int)KS_EXTRA;
5412 result[dlen++] = (int)KE_SNR;
5413 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5414 dlen += (int)STRLEN(result + dlen);
5415 result[dlen++] = '_';
5416 continue;
5417 }
5418 }
5419#endif
5420
5421 slen = trans_special(&src, result + dlen, TRUE);
5422 if (slen)
5423 {
5424 dlen += slen;
5425 continue;
5426 }
5427 }
5428
5429 /*
5430 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5431 * Note that this is also checked after replacing the <> form.
5432 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5433 * it could be a character in the file.
5434 */
5435 if (do_key_code)
5436 {
5437 i = find_term_bykeys(src);
5438 if (i >= 0)
5439 {
5440 result[dlen++] = K_SPECIAL;
5441 result[dlen++] = termcodes[i].name[0];
5442 result[dlen++] = termcodes[i].name[1];
5443 src += termcodes[i].len;
5444 /* If terminal code matched, continue after it. */
5445 continue;
5446 }
5447 }
5448
5449#ifdef FEAT_EVAL
5450 if (do_special)
5451 {
5452 char_u *p, *s, len;
5453
5454 /*
5455 * Replace <Leader> by the value of "mapleader".
5456 * Replace <LocalLeader> by the value of "maplocalleader".
5457 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5458 */
5459 if (STRNICMP(src, "<Leader>", 8) == 0)
5460 {
5461 len = 8;
5462 p = get_var_value((char_u *)"g:mapleader");
5463 }
5464 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5465 {
5466 len = 13;
5467 p = get_var_value((char_u *)"g:maplocalleader");
5468 }
5469 else
5470 {
5471 len = 0;
5472 p = NULL;
5473 }
5474 if (len != 0)
5475 {
5476 /* Allow up to 8 * 6 characters for "mapleader". */
5477 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5478 s = (char_u *)"\\";
5479 else
5480 s = p;
5481 while (*s != NUL)
5482 result[dlen++] = *s++;
5483 src += len;
5484 continue;
5485 }
5486 }
5487#endif
5488
5489 /*
5490 * Remove CTRL-V and ignore the next character.
5491 * For "from" side the CTRL-V at the end is included, for the "to"
5492 * part it is removed.
5493 * If 'cpoptions' does not contain 'B', also accept a backslash.
5494 */
5495 key = *src;
5496 if (key == Ctrl_V || (do_backslash && key == '\\'))
5497 {
5498 ++src; /* skip CTRL-V or backslash */
5499 if (*src == NUL)
5500 {
5501 if (from_part)
5502 result[dlen++] = key;
5503 break;
5504 }
5505 }
5506
5507#ifdef FEAT_MBYTE
5508 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005509 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510#endif
5511 {
5512 /*
5513 * If the character is K_SPECIAL, replace it with K_SPECIAL
5514 * KS_SPECIAL KE_FILLER.
5515 * If compiled with the GUI replace CSI with K_CSI.
5516 */
5517 if (*src == K_SPECIAL)
5518 {
5519 result[dlen++] = K_SPECIAL;
5520 result[dlen++] = KS_SPECIAL;
5521 result[dlen++] = KE_FILLER;
5522 }
5523# ifdef FEAT_GUI
5524 else if (*src == CSI)
5525 {
5526 result[dlen++] = K_SPECIAL;
5527 result[dlen++] = KS_EXTRA;
5528 result[dlen++] = (int)KE_CSI;
5529 }
5530# endif
5531 else
5532 result[dlen++] = *src;
5533 ++src;
5534 }
5535 }
5536 result[dlen] = NUL;
5537
5538 /*
5539 * Copy the new string to allocated memory.
5540 * If this fails, just return from.
5541 */
5542 if ((*bufp = vim_strsave(result)) != NULL)
5543 from = *bufp;
5544 vim_free(result);
5545 return from;
5546}
5547
5548/*
5549 * Find a termcode with keys 'src' (must be NUL terminated).
5550 * Return the index in termcodes[], or -1 if not found.
5551 */
5552 int
5553find_term_bykeys(src)
5554 char_u *src;
5555{
5556 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005557 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558
5559 for (i = 0; i < tc_len; ++i)
5560 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005561 if (slen == termcodes[i].len
5562 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 return i;
5564 }
5565 return -1;
5566}
5567
5568/*
5569 * Gather the first characters in the terminal key codes into a string.
5570 * Used to speed up check_termcode().
5571 */
5572 static void
5573gather_termleader()
5574{
5575 int i;
5576 int len = 0;
5577
5578#ifdef FEAT_GUI
5579 if (gui.in_use)
5580 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5581#endif
5582#ifdef FEAT_TERMRESPONSE
5583 if (check_for_codes)
5584 termleader[len++] = DCS; /* the termcode response starts with DCS
5585 in 8-bit mode */
5586#endif
5587 termleader[len] = NUL;
5588
5589 for (i = 0; i < tc_len; ++i)
5590 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5591 {
5592 termleader[len++] = termcodes[i].code[0];
5593 termleader[len] = NUL;
5594 }
5595
5596 need_gather = FALSE;
5597}
5598
5599/*
5600 * Show all termcodes (for ":set termcap")
5601 * This code looks a lot like showoptions(), but is different.
5602 */
5603 void
5604show_termcodes()
5605{
5606 int col;
5607 int *items;
5608 int item_count;
5609 int run;
5610 int row, rows;
5611 int cols;
5612 int i;
5613 int len;
5614
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005615#define INC3 27 /* try to make three columns */
5616#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617#define GAP 2 /* spaces between columns */
5618
5619 if (tc_len == 0) /* no terminal codes (must be GUI) */
5620 return;
5621 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5622 if (items == NULL)
5623 return;
5624
5625 /* Highlight title */
5626 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5627
5628 /*
5629 * do the loop two times:
5630 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005631 * 2. display the medium items (medium length strings)
5632 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005634 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 {
5636 /*
5637 * collect the items in items[]
5638 */
5639 item_count = 0;
5640 for (i = 0; i < tc_len; i++)
5641 {
5642 len = show_one_termcode(termcodes[i].name,
5643 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005644 if (len <= INC3 - GAP ? run == 1
5645 : len <= INC2 - GAP ? run == 2
5646 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 items[item_count++] = i;
5648 }
5649
5650 /*
5651 * display the items
5652 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005653 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005655 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 if (cols == 0)
5657 cols = 1;
5658 rows = (item_count + cols - 1) / cols;
5659 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005660 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 rows = item_count;
5662 for (row = 0; row < rows && !got_int; ++row)
5663 {
5664 msg_putchar('\n'); /* go to next line */
5665 if (got_int) /* 'q' typed in more */
5666 break;
5667 col = 0;
5668 for (i = row; i < item_count; i += rows)
5669 {
5670 msg_col = col; /* make columns */
5671 show_one_termcode(termcodes[items[i]].name,
5672 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005673 if (run == 2)
5674 col += INC2;
5675 else
5676 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
5678 out_flush();
5679 ui_breakcheck();
5680 }
5681 }
5682 vim_free(items);
5683}
5684
5685/*
5686 * Show one termcode entry.
5687 * Output goes into IObuff[]
5688 */
5689 int
5690show_one_termcode(name, code, printit)
5691 char_u *name;
5692 char_u *code;
5693 int printit;
5694{
5695 char_u *p;
5696 int len;
5697
5698 if (name[0] > '~')
5699 {
5700 IObuff[0] = ' ';
5701 IObuff[1] = ' ';
5702 IObuff[2] = ' ';
5703 IObuff[3] = ' ';
5704 }
5705 else
5706 {
5707 IObuff[0] = 't';
5708 IObuff[1] = '_';
5709 IObuff[2] = name[0];
5710 IObuff[3] = name[1];
5711 }
5712 IObuff[4] = ' ';
5713
5714 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5715 if (p[1] != 't')
5716 STRCPY(IObuff + 5, p);
5717 else
5718 IObuff[5] = NUL;
5719 len = (int)STRLEN(IObuff);
5720 do
5721 IObuff[len++] = ' ';
5722 while (len < 17);
5723 IObuff[len] = NUL;
5724 if (code == NULL)
5725 len += 4;
5726 else
5727 len += vim_strsize(code);
5728
5729 if (printit)
5730 {
5731 msg_puts(IObuff);
5732 if (code == NULL)
5733 msg_puts((char_u *)"NULL");
5734 else
5735 msg_outtrans(code);
5736 }
5737 return len;
5738}
5739
5740#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5741/*
5742 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5743 * termcap codes from the terminal itself.
5744 * We get them one by one to avoid a very long response string.
5745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 static void
5747req_codes_from_term()
5748{
5749 xt_index_out = 0;
5750 xt_index_in = 0;
5751 req_more_codes_from_term();
5752}
5753
5754 static void
5755req_more_codes_from_term()
5756{
5757 char buf[11];
5758 int old_idx = xt_index_out;
5759
5760 /* Don't do anything when going to exit. */
5761 if (exiting)
5762 return;
5763
5764 /* Send up to 10 more requests out than we received. Avoid sending too
5765 * many, there can be a buffer overflow somewhere. */
5766 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5767 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02005768# ifdef DEBUG_TERMRESPONSE
5769 char dbuf[100];
5770
5771 sprintf(dbuf, "Requesting XT %d: %s",
5772 xt_index_out, key_names[xt_index_out]);
5773 log_tr(dbuf);
5774# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 sprintf(buf, "\033P+q%02x%02x\033\\",
5776 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5777 out_str_nf((char_u *)buf);
5778 ++xt_index_out;
5779 }
5780
5781 /* Send the codes out right away. */
5782 if (xt_index_out != old_idx)
5783 out_flush();
5784}
5785
5786/*
5787 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5788 * A "0" instead of the "1" indicates a code that isn't supported.
5789 * Both <name> and <string> are encoded in hex.
5790 * "code" points to the "0" or "1".
5791 */
5792 static void
5793got_code_from_term(code, len)
5794 char_u *code;
5795 int len;
5796{
5797#define XT_LEN 100
5798 char_u name[3];
5799 char_u str[XT_LEN];
5800 int i;
5801 int j = 0;
5802 int c;
5803
5804 /* A '1' means the code is supported, a '0' means it isn't.
5805 * When half the length is > XT_LEN we can't use it.
5806 * Our names are currently all 2 characters. */
5807 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5808 {
5809 /* Get the name from the response and find it in the table. */
5810 name[0] = hexhex2nr(code + 3);
5811 name[1] = hexhex2nr(code + 5);
5812 name[2] = NUL;
5813 for (i = 0; key_names[i] != NULL; ++i)
5814 {
5815 if (STRCMP(key_names[i], name) == 0)
5816 {
5817 xt_index_in = i;
5818 break;
5819 }
5820 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005821# ifdef DEBUG_TERMRESPONSE
5822 {
5823 char buf[100];
5824
5825 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
5826 log_tr(buf);
5827 }
5828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 if (key_names[i] != NULL)
5830 {
5831 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5832 str[j++] = c;
5833 str[j] = NUL;
5834 if (name[0] == 'C' && name[1] == 'o')
5835 {
5836 /* Color count is not a key code. */
5837 i = atoi((char *)str);
5838 if (i != t_colors)
5839 {
5840 /* Nr of colors changed, initialize highlighting and
Bram Moolenaar238a5642006-02-21 22:12:05 +00005841 * redraw everything. This causes a redraw, which usually
5842 * clears the message. Try keeping the message if it
5843 * might work. */
5844 set_keep_msg_from_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 set_color_count(i);
5846 init_highlight(TRUE, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +02005847#ifdef DEBUG_TERMRESPONSE
5848 {
5849 char buf[100];
5850 int r = redraw_asap(CLEAR);
5851
5852 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
5853 log_tr(buf);
5854 }
5855#else
5856 redraw_asap(CLEAR);
5857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 }
5859 }
5860 else
5861 {
5862 /* First delete any existing entry with the same code. */
5863 i = find_term_bykeys(str);
5864 if (i >= 0)
5865 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005866 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 }
5868 }
5869 }
5870
5871 /* May request more codes now that we received one. */
5872 ++xt_index_in;
5873 req_more_codes_from_term();
5874}
5875
5876/*
5877 * Check if there are any unanswered requests and deal with them.
5878 * This is called before starting an external program or getting direct
5879 * keyboard input. We don't want responses to be send to that program or
5880 * handled as typed text.
5881 */
5882 static void
5883check_for_codes_from_term()
5884{
5885 int c;
5886
5887 /* If no codes requested or all are answered, no need to wait. */
5888 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5889 return;
5890
5891 /* Vgetc() will check for and handle any response.
5892 * Keep calling vpeekc() until we don't get any responses. */
5893 ++no_mapping;
5894 ++allow_keys;
5895 for (;;)
5896 {
5897 c = vpeekc();
5898 if (c == NUL) /* nothing available */
5899 break;
5900
5901 /* If a response is recognized it's replaced with K_IGNORE, must read
5902 * it from the input stream. If there is no K_IGNORE we can't do
5903 * anything, break here (there might be some responses further on, but
5904 * we don't want to throw away any typed chars). */
5905 if (c != K_SPECIAL && c != K_IGNORE)
5906 break;
5907 c = vgetc();
5908 if (c != K_IGNORE)
5909 {
5910 vungetc(c);
5911 break;
5912 }
5913 }
5914 --no_mapping;
5915 --allow_keys;
5916}
5917#endif
5918
5919#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5920/*
5921 * Translate an internal mapping/abbreviation representation into the
5922 * corresponding external one recognized by :map/:abbrev commands;
5923 * respects the current B/k/< settings of 'cpoption'.
5924 *
5925 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00005926 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 *
5928 * It uses a growarray to build the translation string since the
5929 * latter can be wider than the original description. The caller has to
5930 * free the string afterwards.
5931 *
5932 * Returns NULL when there is a problem.
5933 */
5934 char_u *
5935translate_mapping(str, expmap)
5936 char_u *str;
5937 int expmap; /* TRUE when expanding mappings on command-line */
5938{
5939 garray_T ga;
5940 int c;
5941 int modifiers;
5942 int cpo_bslash;
5943 int cpo_special;
5944 int cpo_keycode;
5945
5946 ga_init(&ga);
5947 ga.ga_itemsize = 1;
5948 ga.ga_growsize = 40;
5949
5950 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
5951 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
5952 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5953
5954 for (; *str; ++str)
5955 {
5956 c = *str;
5957 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5958 {
5959 modifiers = 0;
5960 if (str[1] == KS_MODIFIER)
5961 {
5962 str++;
5963 modifiers = *++str;
5964 c = *++str;
5965 }
5966 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
5967 {
5968 int i;
5969
5970 /* try to find special key in termcodes */
5971 for (i = 0; i < tc_len; ++i)
5972 if (termcodes[i].name[0] == str[1]
5973 && termcodes[i].name[1] == str[2])
5974 break;
5975 if (i < tc_len)
5976 {
5977 ga_concat(&ga, termcodes[i].code);
5978 str += 2;
5979 continue; /* for (str) */
5980 }
5981 }
5982 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5983 {
5984 if (expmap && cpo_special)
5985 {
5986 ga_clear(&ga);
5987 return NULL;
5988 }
5989 c = TO_SPECIAL(str[1], str[2]);
5990 if (c == K_ZERO) /* display <Nul> as ^@ */
5991 c = NUL;
5992 str += 2;
5993 }
5994 if (IS_SPECIAL(c) || modifiers) /* special key */
5995 {
5996 if (expmap && cpo_special)
5997 {
5998 ga_clear(&ga);
5999 return NULL;
6000 }
6001 ga_concat(&ga, get_special_key_name(c, modifiers));
6002 continue; /* for (str) */
6003 }
6004 }
6005 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6006 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6007 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6008 if (c)
6009 ga_append(&ga, c);
6010 }
6011 ga_append(&ga, NUL);
6012 return (char_u *)(ga.ga_data);
6013}
6014#endif
6015
6016#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
6017static char ksme_str[20];
6018static char ksmr_str[20];
6019static char ksmd_str[20];
6020
6021/*
6022 * For Win32 console: update termcap codes for existing console attributes.
6023 */
6024 void
6025update_tcap(attr)
6026 int attr;
6027{
6028 struct builtin_term *p;
6029
6030 p = find_builtin_term(DEFAULT_TERM);
6031 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6032 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6033 attr | 0x08); /* FOREGROUND_INTENSITY */
6034 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6035 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6036
6037 while (p->bt_string != NULL)
6038 {
6039 if (p->bt_entry == (int)KS_ME)
6040 p->bt_string = &ksme_str[0];
6041 else if (p->bt_entry == (int)KS_MR)
6042 p->bt_string = &ksmr_str[0];
6043 else if (p->bt_entry == (int)KS_MD)
6044 p->bt_string = &ksmd_str[0];
6045 ++p;
6046 }
6047}
6048#endif