blob: 41974de41b788c87491f79049f0e4fd875f4750a [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
156static int detected_8bit = FALSE; /* detected 8-bit terminal */
157
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000158static struct builtin_term builtin_termcaps[] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159{
160
161#if defined(FEAT_GUI)
162/*
163 * GUI pseudo term-cap.
164 */
165 {(int)KS_NAME, "gui"},
166 {(int)KS_CE, IF_EB("\033|$", ESC_STR "|$")},
167 {(int)KS_AL, IF_EB("\033|i", ESC_STR "|i")},
168# ifdef TERMINFO
169 {(int)KS_CAL, IF_EB("\033|%p1%dI", ESC_STR "|%p1%dI")},
170# else
171 {(int)KS_CAL, IF_EB("\033|%dI", ESC_STR "|%dI")},
172# endif
173 {(int)KS_DL, IF_EB("\033|d", ESC_STR "|d")},
174# ifdef TERMINFO
175 {(int)KS_CDL, IF_EB("\033|%p1%dD", ESC_STR "|%p1%dD")},
176 {(int)KS_CS, IF_EB("\033|%p1%d;%p2%dR", ESC_STR "|%p1%d;%p2%dR")},
177# ifdef FEAT_VERTSPLIT
178 {(int)KS_CSV, IF_EB("\033|%p1%d;%p2%dV", ESC_STR "|%p1%d;%p2%dV")},
179# endif
180# else
181 {(int)KS_CDL, IF_EB("\033|%dD", ESC_STR "|%dD")},
182 {(int)KS_CS, IF_EB("\033|%d;%dR", ESC_STR "|%d;%dR")},
183# ifdef FEAT_VERTSPLIT
184 {(int)KS_CSV, IF_EB("\033|%d;%dV", ESC_STR "|%d;%dV")},
185# endif
186# endif
187 {(int)KS_CL, IF_EB("\033|C", ESC_STR "|C")},
188 /* attributes switched on with 'h', off with * 'H' */
189 {(int)KS_ME, IF_EB("\033|31H", ESC_STR "|31H")}, /* HL_ALL */
190 {(int)KS_MR, IF_EB("\033|1h", ESC_STR "|1h")}, /* HL_INVERSE */
191 {(int)KS_MD, IF_EB("\033|2h", ESC_STR "|2h")}, /* HL_BOLD */
192 {(int)KS_SE, IF_EB("\033|16H", ESC_STR "|16H")}, /* HL_STANDOUT */
193 {(int)KS_SO, IF_EB("\033|16h", ESC_STR "|16h")}, /* HL_STANDOUT */
194 {(int)KS_UE, IF_EB("\033|8H", ESC_STR "|8H")}, /* HL_UNDERLINE */
195 {(int)KS_US, IF_EB("\033|8h", ESC_STR "|8h")}, /* HL_UNDERLINE */
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000196 {(int)KS_UCE, IF_EB("\033|8C", ESC_STR "|8C")}, /* HL_UNDERCURL */
197 {(int)KS_UCS, IF_EB("\033|8c", ESC_STR "|8c")}, /* HL_UNDERCURL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {(int)KS_CZR, IF_EB("\033|4H", ESC_STR "|4H")}, /* HL_ITALIC */
199 {(int)KS_CZH, IF_EB("\033|4h", ESC_STR "|4h")}, /* HL_ITALIC */
200 {(int)KS_VB, IF_EB("\033|f", ESC_STR "|f")},
201 {(int)KS_MS, "y"},
202 {(int)KS_UT, "y"},
203 {(int)KS_LE, "\b"}, /* cursor-left = BS */
204 {(int)KS_ND, "\014"}, /* cursor-right = CTRL-L */
205# ifdef TERMINFO
206 {(int)KS_CM, IF_EB("\033|%p1%d;%p2%dM", ESC_STR "|%p1%d;%p2%dM")},
207# else
208 {(int)KS_CM, IF_EB("\033|%d;%dM", ESC_STR "|%d;%dM")},
209# endif
210 /* there are no key sequences here, the GUI sequences are recognized
Bram Moolenaare2cc9702005-03-15 22:43:58 +0000211 * in check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#endif
213
214#ifndef NO_BUILTIN_TCAPS
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215
216# if defined(AMIGA) || defined(ALL_BUILTIN_TCAPS)
217/*
218 * Amiga console window, default for Amiga
219 */
220 {(int)KS_NAME, "amiga"},
221 {(int)KS_CE, "\033[K"},
222 {(int)KS_CD, "\033[J"},
223 {(int)KS_AL, "\033[L"},
224# ifdef TERMINFO
225 {(int)KS_CAL, "\033[%p1%dL"},
226# else
227 {(int)KS_CAL, "\033[%dL"},
228# endif
229 {(int)KS_DL, "\033[M"},
230# ifdef TERMINFO
231 {(int)KS_CDL, "\033[%p1%dM"},
232# else
233 {(int)KS_CDL, "\033[%dM"},
234# endif
235 {(int)KS_CL, "\014"},
236 {(int)KS_VI, "\033[0 p"},
237 {(int)KS_VE, "\033[1 p"},
238 {(int)KS_ME, "\033[0m"},
239 {(int)KS_MR, "\033[7m"},
240 {(int)KS_MD, "\033[1m"},
241 {(int)KS_SE, "\033[0m"},
242 {(int)KS_SO, "\033[33m"},
243 {(int)KS_US, "\033[4m"},
244 {(int)KS_UE, "\033[0m"},
245 {(int)KS_CZH, "\033[3m"},
246 {(int)KS_CZR, "\033[0m"},
247#if defined(__MORPHOS__) || defined(__AROS__)
248 {(int)KS_CCO, "8"}, /* allow 8 colors */
249# ifdef TERMINFO
250 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
251 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
252# else
253 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
254 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
255# endif
256 {(int)KS_OP, "\033[m"}, /* reset colors */
257#endif
258 {(int)KS_MS, "y"},
259 {(int)KS_UT, "y"}, /* guessed */
260 {(int)KS_LE, "\b"},
261# ifdef TERMINFO
262 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
263# else
264 {(int)KS_CM, "\033[%i%d;%dH"},
265# endif
266#if defined(__MORPHOS__)
267 {(int)KS_SR, "\033M"},
268#endif
269# ifdef TERMINFO
270 {(int)KS_CRI, "\033[%p1%dC"},
271# else
272 {(int)KS_CRI, "\033[%dC"},
273# endif
274 {K_UP, "\233A"},
275 {K_DOWN, "\233B"},
276 {K_LEFT, "\233D"},
277 {K_RIGHT, "\233C"},
278 {K_S_UP, "\233T"},
279 {K_S_DOWN, "\233S"},
280 {K_S_LEFT, "\233 A"},
281 {K_S_RIGHT, "\233 @"},
282 {K_S_TAB, "\233Z"},
283 {K_F1, "\233\060~"},/* some compilers don't dig "\2330" */
284 {K_F2, "\233\061~"},
285 {K_F3, "\233\062~"},
286 {K_F4, "\233\063~"},
287 {K_F5, "\233\064~"},
288 {K_F6, "\233\065~"},
289 {K_F7, "\233\066~"},
290 {K_F8, "\233\067~"},
291 {K_F9, "\233\070~"},
292 {K_F10, "\233\071~"},
293 {K_S_F1, "\233\061\060~"},
294 {K_S_F2, "\233\061\061~"},
295 {K_S_F3, "\233\061\062~"},
296 {K_S_F4, "\233\061\063~"},
297 {K_S_F5, "\233\061\064~"},
298 {K_S_F6, "\233\061\065~"},
299 {K_S_F7, "\233\061\066~"},
300 {K_S_F8, "\233\061\067~"},
301 {K_S_F9, "\233\061\070~"},
302 {K_S_F10, "\233\061\071~"},
303 {K_HELP, "\233?~"},
304 {K_INS, "\233\064\060~"}, /* 101 key keyboard */
305 {K_PAGEUP, "\233\064\061~"}, /* 101 key keyboard */
306 {K_PAGEDOWN, "\233\064\062~"}, /* 101 key keyboard */
307 {K_HOME, "\233\064\064~"}, /* 101 key keyboard */
308 {K_END, "\233\064\065~"}, /* 101 key keyboard */
309
310 {BT_EXTRA_KEYS, ""},
311 {TERMCAP2KEY('#', '2'), "\233\065\064~"}, /* shifted home key */
312 {TERMCAP2KEY('#', '3'), "\233\065\060~"}, /* shifted insert key */
313 {TERMCAP2KEY('*', '7'), "\233\065\065~"}, /* shifted end key */
314# endif
315
316# if defined(__BEOS__) || defined(ALL_BUILTIN_TCAPS)
317/*
318 * almost standard ANSI terminal, default for bebox
319 */
320 {(int)KS_NAME, "beos-ansi"},
321 {(int)KS_CE, "\033[K"},
322 {(int)KS_CD, "\033[J"},
323 {(int)KS_AL, "\033[L"},
324# ifdef TERMINFO
325 {(int)KS_CAL, "\033[%p1%dL"},
326# else
327 {(int)KS_CAL, "\033[%dL"},
328# endif
329 {(int)KS_DL, "\033[M"},
330# ifdef TERMINFO
331 {(int)KS_CDL, "\033[%p1%dM"},
332# else
333 {(int)KS_CDL, "\033[%dM"},
334# endif
335#ifdef BEOS_PR_OR_BETTER
336# ifdef TERMINFO
337 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
338# else
339 {(int)KS_CS, "\033[%i%d;%dr"}, /* scroll region */
340# endif
341#endif
342 {(int)KS_CL, "\033[H\033[2J"},
343#ifdef notyet
344 {(int)KS_VI, "[VI]"}, /* cursor invisible, VT320: CSI ? 25 l */
345 {(int)KS_VE, "[VE]"}, /* cursor visible, VT320: CSI ? 25 h */
346#endif
347 {(int)KS_ME, "\033[m"}, /* normal mode */
348 {(int)KS_MR, "\033[7m"}, /* reverse */
349 {(int)KS_MD, "\033[1m"}, /* bold */
350 {(int)KS_SO, "\033[31m"}, /* standout mode: red */
351 {(int)KS_SE, "\033[m"}, /* standout end */
352 {(int)KS_CZH, "\033[35m"}, /* italic: purple */
353 {(int)KS_CZR, "\033[m"}, /* italic end */
354 {(int)KS_US, "\033[4m"}, /* underscore mode */
355 {(int)KS_UE, "\033[m"}, /* underscore end */
356 {(int)KS_CCO, "8"}, /* allow 8 colors */
357# ifdef TERMINFO
358 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
359 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
360# else
361 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
362 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
363# endif
364 {(int)KS_OP, "\033[m"}, /* reset colors */
365 {(int)KS_MS, "y"}, /* safe to move cur in reverse mode */
366 {(int)KS_UT, "y"}, /* guessed */
367 {(int)KS_LE, "\b"},
368# ifdef TERMINFO
369 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
370# else
371 {(int)KS_CM, "\033[%i%d;%dH"},
372# endif
373 {(int)KS_SR, "\033M"},
374# ifdef TERMINFO
375 {(int)KS_CRI, "\033[%p1%dC"},
376# else
377 {(int)KS_CRI, "\033[%dC"},
378# endif
379#if defined(BEOS_DR8)
380 {(int)KS_DB, ""}, /* hack! see screen.c */
381#endif
382
383 {K_UP, "\033[A"},
384 {K_DOWN, "\033[B"},
385 {K_LEFT, "\033[D"},
386 {K_RIGHT, "\033[C"},
387# endif
388
389# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) || defined(__EMX__)
390/*
391 * standard ANSI terminal, default for unix
392 */
393 {(int)KS_NAME, "ansi"},
394 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
395 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
396# ifdef TERMINFO
397 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
398# else
399 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
400# endif
401 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
402# ifdef TERMINFO
403 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
404# else
405 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
406# endif
407 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
408 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
409 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
410 {(int)KS_MS, "y"},
411 {(int)KS_UT, "y"}, /* guessed */
412 {(int)KS_LE, "\b"},
413# ifdef TERMINFO
414 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH", ESC_STR "[%i%p1%d;%p2%dH")},
415# else
416 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
417# endif
418# ifdef TERMINFO
419 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
420# else
421 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
422# endif
423# endif
424
425# if defined(MSDOS) || defined(ALL_BUILTIN_TCAPS) || defined(__EMX__)
426/*
427 * These codes are valid when nansi.sys or equivalent has been installed.
428 * Function keys on a PC are preceded with a NUL. These are converted into
429 * K_NUL '\316' in mch_inchar(), because we cannot handle NULs in key codes.
430 * CTRL-arrow is used instead of SHIFT-arrow.
431 */
432#ifdef __EMX__
433 {(int)KS_NAME, "os2ansi"},
434#else
435 {(int)KS_NAME, "pcansi"},
436 {(int)KS_DL, "\033[M"},
437 {(int)KS_AL, "\033[L"},
438#endif
439 {(int)KS_CE, "\033[K"},
440 {(int)KS_CL, "\033[2J"},
441 {(int)KS_ME, "\033[0m"},
442 {(int)KS_MR, "\033[5m"}, /* reverse: black on lightgrey */
443 {(int)KS_MD, "\033[1m"}, /* bold: white text */
444 {(int)KS_SE, "\033[0m"}, /* standout end */
445 {(int)KS_SO, "\033[31m"}, /* standout: white on blue */
446 {(int)KS_CZH, "\033[34;43m"}, /* italic mode: blue text on yellow */
447 {(int)KS_CZR, "\033[0m"}, /* italic mode end */
448 {(int)KS_US, "\033[36;41m"}, /* underscore mode: cyan text on red */
449 {(int)KS_UE, "\033[0m"}, /* underscore mode end */
450 {(int)KS_CCO, "8"}, /* allow 8 colors */
451# ifdef TERMINFO
452 {(int)KS_CAB, "\033[4%p1%dm"},/* set background color */
453 {(int)KS_CAF, "\033[3%p1%dm"},/* set foreground color */
454# else
455 {(int)KS_CAB, "\033[4%dm"}, /* set background color */
456 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color */
457# endif
458 {(int)KS_OP, "\033[0m"}, /* reset colors */
459 {(int)KS_MS, "y"},
460 {(int)KS_UT, "y"}, /* guessed */
461 {(int)KS_LE, "\b"},
462# ifdef TERMINFO
463 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
464# else
465 {(int)KS_CM, "\033[%i%d;%dH"},
466# endif
467# ifdef TERMINFO
468 {(int)KS_CRI, "\033[%p1%dC"},
469# else
470 {(int)KS_CRI, "\033[%dC"},
471# endif
472 {K_UP, "\316H"},
473 {K_DOWN, "\316P"},
474 {K_LEFT, "\316K"},
475 {K_RIGHT, "\316M"},
476 {K_S_LEFT, "\316s"},
477 {K_S_RIGHT, "\316t"},
478 {K_F1, "\316;"},
479 {K_F2, "\316<"},
480 {K_F3, "\316="},
481 {K_F4, "\316>"},
482 {K_F5, "\316?"},
483 {K_F6, "\316@"},
484 {K_F7, "\316A"},
485 {K_F8, "\316B"},
486 {K_F9, "\316C"},
487 {K_F10, "\316D"},
488 {K_F11, "\316\205"}, /* guessed */
489 {K_F12, "\316\206"}, /* guessed */
490 {K_S_F1, "\316T"},
491 {K_S_F2, "\316U"},
492 {K_S_F3, "\316V"},
493 {K_S_F4, "\316W"},
494 {K_S_F5, "\316X"},
495 {K_S_F6, "\316Y"},
496 {K_S_F7, "\316Z"},
497 {K_S_F8, "\316["},
498 {K_S_F9, "\316\\"},
499 {K_S_F10, "\316]"},
500 {K_S_F11, "\316\207"}, /* guessed */
501 {K_S_F12, "\316\210"}, /* guessed */
502 {K_INS, "\316R"},
503 {K_DEL, "\316S"},
504 {K_HOME, "\316G"},
505 {K_END, "\316O"},
506 {K_PAGEDOWN, "\316Q"},
507 {K_PAGEUP, "\316I"},
508# endif
509
510# if defined(MSDOS)
511/*
512 * These codes are valid for the pc video. The entries that start with ESC |
513 * are translated into conio calls in os_msdos.c. Default for MSDOS.
514 */
515 {(int)KS_NAME, "pcterm"},
516 {(int)KS_CE, "\033|K"},
517 {(int)KS_AL, "\033|L"},
518 {(int)KS_DL, "\033|M"},
519# ifdef TERMINFO
520 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},
521# ifdef FEAT_VERTSPLIT
522 {(int)KS_CSV, "\033|%i%p1%d;%p2%dV"},
523# endif
524# else
525 {(int)KS_CS, "\033|%i%d;%dr"},
526# ifdef FEAT_VERTSPLIT
527 {(int)KS_CSV, "\033|%i%d;%dV"},
528# endif
529# endif
530 {(int)KS_CL, "\033|J"},
531 {(int)KS_ME, "\033|0m"}, /* normal */
532 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgrey */
533 {(int)KS_MD, "\033|15m"}, /* bold: white text */
534 {(int)KS_SE, "\033|0m"}, /* standout end */
535 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
536 {(int)KS_CZH, "\033|225m"}, /* italic mode: blue text on yellow */
537 {(int)KS_CZR, "\033|0m"}, /* italic mode end */
538 {(int)KS_US, "\033|67m"}, /* underscore mode: cyan text on red */
539 {(int)KS_UE, "\033|0m"}, /* underscore mode end */
540 {(int)KS_CCO, "16"}, /* allow 16 colors */
541# ifdef TERMINFO
542 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
543 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
544# else
545 {(int)KS_CAB, "\033|%db"}, /* set background color */
546 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
547# endif
548 {(int)KS_MS, "y"},
549 {(int)KS_UT, "y"},
550 {(int)KS_LE, "\b"},
551# ifdef TERMINFO
552 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},
553# else
554 {(int)KS_CM, "\033|%i%d;%dH"},
555# endif
556#ifdef DJGPP
557 {(int)KS_VB, "\033|B"}, /* visual bell */
558#endif
559 {K_UP, "\316H"},
560 {K_DOWN, "\316P"},
561 {K_LEFT, "\316K"},
562 {K_RIGHT, "\316M"},
563 {K_S_LEFT, "\316s"},
564 {K_S_RIGHT, "\316t"},
565 {K_S_TAB, "\316\017"},
566 {K_F1, "\316;"},
567 {K_F2, "\316<"},
568 {K_F3, "\316="},
569 {K_F4, "\316>"},
570 {K_F5, "\316?"},
571 {K_F6, "\316@"},
572 {K_F7, "\316A"},
573 {K_F8, "\316B"},
574 {K_F9, "\316C"},
575 {K_F10, "\316D"},
576 {K_F11, "\316\205"},
577 {K_F12, "\316\206"},
578 {K_S_F1, "\316T"},
579 {K_S_F2, "\316U"},
580 {K_S_F3, "\316V"},
581 {K_S_F4, "\316W"},
582 {K_S_F5, "\316X"},
583 {K_S_F6, "\316Y"},
584 {K_S_F7, "\316Z"},
585 {K_S_F8, "\316["},
586 {K_S_F9, "\316\\"},
587 {K_S_F10, "\316]"},
588 {K_S_F11, "\316\207"},
589 {K_S_F12, "\316\210"},
590 {K_INS, "\316R"},
591 {K_DEL, "\316S"},
592 {K_HOME, "\316G"},
593 {K_END, "\316O"},
594 {K_PAGEDOWN, "\316Q"},
595 {K_PAGEUP, "\316I"},
596 {K_KPLUS, "\316N"},
597 {K_KMINUS, "\316J"},
598 {K_KMULTIPLY, "\3167"},
599 {K_K0, "\316\332"},
600 {K_K1, "\316\336"},
601 {K_K2, "\316\342"},
602 {K_K3, "\316\346"},
603 {K_K4, "\316\352"},
604 {K_K5, "\316\356"},
605 {K_K6, "\316\362"},
606 {K_K7, "\316\366"},
607 {K_K8, "\316\372"},
608 {K_K9, "\316\376"},
609# endif
610
611# if defined(WIN3264) || defined(ALL_BUILTIN_TCAPS) || defined(__EMX__)
612/*
613 * These codes are valid for the Win32 Console . The entries that start with
614 * ESC | are translated into console calls in os_win32.c. The function keys
615 * are also translated in os_win32.c.
616 */
617 {(int)KS_NAME, "win32"},
618 {(int)KS_CE, "\033|K"}, /* clear to end of line */
619 {(int)KS_AL, "\033|L"}, /* add new blank line */
620# ifdef TERMINFO
621 {(int)KS_CAL, "\033|%p1%dL"}, /* add number of new blank lines */
622# else
623 {(int)KS_CAL, "\033|%dL"}, /* add number of new blank lines */
624# endif
625 {(int)KS_DL, "\033|M"}, /* delete line */
626# ifdef TERMINFO
627 {(int)KS_CDL, "\033|%p1%dM"}, /* delete number of lines */
628# else
629 {(int)KS_CDL, "\033|%dM"}, /* delete number of lines */
630# endif
631 {(int)KS_CL, "\033|J"}, /* clear screen */
632 {(int)KS_CD, "\033|j"}, /* clear to end of display */
633 {(int)KS_VI, "\033|v"}, /* cursor invisible */
634 {(int)KS_VE, "\033|V"}, /* cursor visible */
635
636 {(int)KS_ME, "\033|0m"}, /* normal */
637 {(int)KS_MR, "\033|112m"}, /* reverse: black on lightgray */
638 {(int)KS_MD, "\033|15m"}, /* bold: white on black */
639#if 1
640 {(int)KS_SO, "\033|31m"}, /* standout: white on blue */
641 {(int)KS_SE, "\033|0m"}, /* standout end */
642#else
643 {(int)KS_SO, "\033|F"}, /* standout: high intensity */
644 {(int)KS_SE, "\033|f"}, /* standout end */
645#endif
646 {(int)KS_CZH, "\033|225m"}, /* italic: blue text on yellow */
647 {(int)KS_CZR, "\033|0m"}, /* italic end */
648 {(int)KS_US, "\033|67m"}, /* underscore: cyan text on red */
649 {(int)KS_UE, "\033|0m"}, /* underscore end */
650 {(int)KS_CCO, "16"}, /* allow 16 colors */
651# ifdef TERMINFO
652 {(int)KS_CAB, "\033|%p1%db"}, /* set background color */
653 {(int)KS_CAF, "\033|%p1%df"}, /* set foreground color */
654# else
655 {(int)KS_CAB, "\033|%db"}, /* set background color */
656 {(int)KS_CAF, "\033|%df"}, /* set foreground color */
657# endif
658
659 {(int)KS_MS, "y"}, /* save to move cur in reverse mode */
660 {(int)KS_UT, "y"},
661 {(int)KS_LE, "\b"},
662# ifdef TERMINFO
663 {(int)KS_CM, "\033|%i%p1%d;%p2%dH"},/* cursor motion */
664# else
665 {(int)KS_CM, "\033|%i%d;%dH"},/* cursor motion */
666# endif
667 {(int)KS_VB, "\033|B"}, /* visual bell */
668 {(int)KS_TI, "\033|S"}, /* put terminal in termcap mode */
669 {(int)KS_TE, "\033|E"}, /* out of termcap mode */
670# ifdef TERMINFO
671 {(int)KS_CS, "\033|%i%p1%d;%p2%dr"},/* scroll region */
672# else
673 {(int)KS_CS, "\033|%i%d;%dr"},/* scroll region */
674# endif
675
676 {K_UP, "\316H"},
677 {K_DOWN, "\316P"},
678 {K_LEFT, "\316K"},
679 {K_RIGHT, "\316M"},
680 {K_S_UP, "\316\304"},
681 {K_S_DOWN, "\316\317"},
682 {K_S_LEFT, "\316\311"},
683 {K_C_LEFT, "\316s"},
684 {K_S_RIGHT, "\316\313"},
685 {K_C_RIGHT, "\316t"},
686 {K_S_TAB, "\316\017"},
687 {K_F1, "\316;"},
688 {K_F2, "\316<"},
689 {K_F3, "\316="},
690 {K_F4, "\316>"},
691 {K_F5, "\316?"},
692 {K_F6, "\316@"},
693 {K_F7, "\316A"},
694 {K_F8, "\316B"},
695 {K_F9, "\316C"},
696 {K_F10, "\316D"},
697 {K_F11, "\316\205"},
698 {K_F12, "\316\206"},
699 {K_S_F1, "\316T"},
700 {K_S_F2, "\316U"},
701 {K_S_F3, "\316V"},
702 {K_S_F4, "\316W"},
703 {K_S_F5, "\316X"},
704 {K_S_F6, "\316Y"},
705 {K_S_F7, "\316Z"},
706 {K_S_F8, "\316["},
707 {K_S_F9, "\316\\"},
708 {K_S_F10, "\316]"},
709 {K_S_F11, "\316\207"},
710 {K_S_F12, "\316\210"},
711 {K_INS, "\316R"},
712 {K_DEL, "\316S"},
713 {K_HOME, "\316G"},
714 {K_S_HOME, "\316\302"},
715 {K_C_HOME, "\316w"},
716 {K_END, "\316O"},
717 {K_S_END, "\316\315"},
718 {K_C_END, "\316u"},
719 {K_PAGEDOWN, "\316Q"},
720 {K_PAGEUP, "\316I"},
721 {K_KPLUS, "\316N"},
722 {K_KMINUS, "\316J"},
723 {K_KMULTIPLY, "\316\067"},
724 {K_K0, "\316\332"},
725 {K_K1, "\316\336"},
726 {K_K2, "\316\342"},
727 {K_K3, "\316\346"},
728 {K_K4, "\316\352"},
729 {K_K5, "\316\356"},
730 {K_K6, "\316\362"},
731 {K_K7, "\316\366"},
732 {K_K8, "\316\372"},
733 {K_K9, "\316\376"},
734# endif
735
736# if defined(VMS) || defined(ALL_BUILTIN_TCAPS)
737/*
738 * VT320 is working as an ANSI terminal compatible DEC terminal.
739 * (it covers VT1x0, VT2x0 and VT3x0 up to VT320 on VMS as well)
740 * Note: K_F1...K_F5 are for internal use, should not be defined.
741 * TODO:- rewrite ESC[ codes to CSI
742 * - keyboard languages (CSI ? 26 n)
743 */
744 {(int)KS_NAME, "vt320"},
745 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
746 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
747# ifdef TERMINFO
748 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
749# else
750 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
751# endif
752 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
753# ifdef TERMINFO
754 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
755# else
756 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
757# endif
758 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000759 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
760 {(int)KS_CCO, "8"}, /* allow 8 colors */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 {(int)KS_ME, IF_EB("\033[0m", ESC_STR "[0m")},
762 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000763 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")}, /* bold mode */
764 {(int)KS_SE, IF_EB("\033[22m", ESC_STR "[22m")},/* normal mode */
765 {(int)KS_UE, IF_EB("\033[24m", ESC_STR "[24m")},/* exit underscore mode */
766 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")}, /* underscore mode */
767 {(int)KS_CZH, IF_EB("\033[34;43m", ESC_STR "[34;43m")}, /* italic mode: blue text on yellow */
768 {(int)KS_CZR, IF_EB("\033[0m", ESC_STR "[0m")}, /* italic mode end */
769 {(int)KS_CAB, IF_EB("\033[4%dm", ESC_STR "[4%dm")}, /* set background color (ANSI) */
770 {(int)KS_CAF, IF_EB("\033[3%dm", ESC_STR "[3%dm")}, /* set foreground color (ANSI) */
771 {(int)KS_CSB, IF_EB("\033[102;%dm", ESC_STR "[102;%dm")}, /* set screen background color */
772 {(int)KS_CSF, IF_EB("\033[101;%dm", ESC_STR "[101;%dm")}, /* set screen foreground color */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 {(int)KS_MS, "y"},
774 {(int)KS_UT, "y"},
775 {(int)KS_LE, "\b"},
776# ifdef TERMINFO
777 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
778 ESC_STR "[%i%p1%d;%p2%dH")},
779# else
780 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
781# endif
782# ifdef TERMINFO
783 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
784# else
785 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
786# endif
787 {K_UP, IF_EB("\033[A", ESC_STR "[A")},
788 {K_DOWN, IF_EB("\033[B", ESC_STR "[B")},
789 {K_RIGHT, IF_EB("\033[C", ESC_STR "[C")},
790 {K_LEFT, IF_EB("\033[D", ESC_STR "[D")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000791 {K_F1, IF_EB("\033[11~", ESC_STR "[11~")},
792 {K_F2, IF_EB("\033[12~", ESC_STR "[12~")},
793 {K_F3, IF_EB("\033[13~", ESC_STR "[13~")},
794 {K_F4, IF_EB("\033[14~", ESC_STR "[14~")},
795 {K_F5, IF_EB("\033[15~", ESC_STR "[15~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {K_F6, IF_EB("\033[17~", ESC_STR "[17~")},
797 {K_F7, IF_EB("\033[18~", ESC_STR "[18~")},
798 {K_F8, IF_EB("\033[19~", ESC_STR "[19~")},
799 {K_F9, IF_EB("\033[20~", ESC_STR "[20~")},
800 {K_F10, IF_EB("\033[21~", ESC_STR "[21~")},
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000801 {K_F11, IF_EB("\033[23~", ESC_STR "[23~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {K_F12, IF_EB("\033[24~", ESC_STR "[24~")},
803 {K_F13, IF_EB("\033[25~", ESC_STR "[25~")},
804 {K_F14, IF_EB("\033[26~", ESC_STR "[26~")},
805 {K_F15, IF_EB("\033[28~", ESC_STR "[28~")}, /* Help */
806 {K_F16, IF_EB("\033[29~", ESC_STR "[29~")}, /* Select */
807 {K_F17, IF_EB("\033[31~", ESC_STR "[31~")},
808 {K_F18, IF_EB("\033[32~", ESC_STR "[32~")},
809 {K_F19, IF_EB("\033[33~", ESC_STR "[33~")},
810 {K_F20, IF_EB("\033[34~", ESC_STR "[34~")},
811 {K_INS, IF_EB("\033[2~", ESC_STR "[2~")},
812 {K_DEL, IF_EB("\033[3~", ESC_STR "[3~")},
813 {K_HOME, IF_EB("\033[1~", ESC_STR "[1~")},
814 {K_END, IF_EB("\033[4~", ESC_STR "[4~")},
815 {K_PAGEUP, IF_EB("\033[5~", ESC_STR "[5~")},
816 {K_PAGEDOWN, IF_EB("\033[6~", ESC_STR "[6~")},
817 {K_KPLUS, IF_EB("\033Ok", ESC_STR "Ok")}, /* keypad plus */
818 {K_KMINUS, IF_EB("\033Om", ESC_STR "Om")}, /* keypad minus */
819 {K_KDIVIDE, IF_EB("\033Oo", ESC_STR "Oo")}, /* keypad / */
820 {K_KMULTIPLY, IF_EB("\033Oj", ESC_STR "Oj")}, /* keypad * */
821 {K_KENTER, IF_EB("\033OM", ESC_STR "OM")}, /* keypad Enter */
822 {K_BS, "\x7f"}, /* for some reason 0177 doesn't work */
823# endif
824
825# if defined(ALL_BUILTIN_TCAPS) || defined(__MINT__)
826/*
827 * Ordinary vt52
828 */
829 {(int)KS_NAME, "vt52"},
830 {(int)KS_CE, IF_EB("\033K", ESC_STR "K")},
831 {(int)KS_CD, IF_EB("\033J", ESC_STR "J")},
832 {(int)KS_CM, IF_EB("\033Y%+ %+ ", ESC_STR "Y%+ %+ ")},
833 {(int)KS_LE, "\b"},
834# ifdef __MINT__
835 {(int)KS_AL, IF_EB("\033L", ESC_STR "L")},
836 {(int)KS_DL, IF_EB("\033M", ESC_STR "M")},
837 {(int)KS_CL, IF_EB("\033E", ESC_STR "E")},
838 {(int)KS_SR, IF_EB("\033I", ESC_STR "I")},
839 {(int)KS_VE, IF_EB("\033e", ESC_STR "e")},
840 {(int)KS_VI, IF_EB("\033f", ESC_STR "f")},
841 {(int)KS_SO, IF_EB("\033p", ESC_STR "p")},
842 {(int)KS_SE, IF_EB("\033q", ESC_STR "q")},
843 {K_UP, IF_EB("\033A", ESC_STR "A")},
844 {K_DOWN, IF_EB("\033B", ESC_STR "B")},
845 {K_LEFT, IF_EB("\033D", ESC_STR "D")},
846 {K_RIGHT, IF_EB("\033C", ESC_STR "C")},
847 {K_S_UP, IF_EB("\033a", ESC_STR "a")},
848 {K_S_DOWN, IF_EB("\033b", ESC_STR "b")},
849 {K_S_LEFT, IF_EB("\033d", ESC_STR "d")},
850 {K_S_RIGHT, IF_EB("\033c", ESC_STR "c")},
851 {K_F1, IF_EB("\033P", ESC_STR "P")},
852 {K_F2, IF_EB("\033Q", ESC_STR "Q")},
853 {K_F3, IF_EB("\033R", ESC_STR "R")},
854 {K_F4, IF_EB("\033S", ESC_STR "S")},
855 {K_F5, IF_EB("\033T", ESC_STR "T")},
856 {K_F6, IF_EB("\033U", ESC_STR "U")},
857 {K_F7, IF_EB("\033V", ESC_STR "V")},
858 {K_F8, IF_EB("\033W", ESC_STR "W")},
859 {K_F9, IF_EB("\033X", ESC_STR "X")},
860 {K_F10, IF_EB("\033Y", ESC_STR "Y")},
861 {K_S_F1, IF_EB("\033p", ESC_STR "p")},
862 {K_S_F2, IF_EB("\033q", ESC_STR "q")},
863 {K_S_F3, IF_EB("\033r", ESC_STR "r")},
864 {K_S_F4, IF_EB("\033s", ESC_STR "s")},
865 {K_S_F5, IF_EB("\033t", ESC_STR "t")},
866 {K_S_F6, IF_EB("\033u", ESC_STR "u")},
867 {K_S_F7, IF_EB("\033v", ESC_STR "v")},
868 {K_S_F8, IF_EB("\033w", ESC_STR "w")},
869 {K_S_F9, IF_EB("\033x", ESC_STR "x")},
870 {K_S_F10, IF_EB("\033y", ESC_STR "y")},
871 {K_INS, IF_EB("\033I", ESC_STR "I")},
872 {K_HOME, IF_EB("\033E", ESC_STR "E")},
873 {K_PAGEDOWN, IF_EB("\033b", ESC_STR "b")},
874 {K_PAGEUP, IF_EB("\033a", ESC_STR "a")},
875# else
876 {(int)KS_AL, IF_EB("\033T", ESC_STR "T")},
877 {(int)KS_DL, IF_EB("\033U", ESC_STR "U")},
878 {(int)KS_CL, IF_EB("\033H\033J", ESC_STR "H" ESC_STR_nc "J")},
879 {(int)KS_ME, IF_EB("\033SO", ESC_STR "SO")},
880 {(int)KS_MR, IF_EB("\033S2", ESC_STR "S2")},
881 {(int)KS_MS, "y"},
882# endif
883# endif
884
885# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS) || defined(SOME_BUILTIN_TCAPS) || defined(__EMX__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {(int)KS_NAME, "xterm"},
887 {(int)KS_CE, IF_EB("\033[K", ESC_STR "[K")},
888 {(int)KS_AL, IF_EB("\033[L", ESC_STR "[L")},
889# ifdef TERMINFO
890 {(int)KS_CAL, IF_EB("\033[%p1%dL", ESC_STR "[%p1%dL")},
891# else
892 {(int)KS_CAL, IF_EB("\033[%dL", ESC_STR "[%dL")},
893# endif
894 {(int)KS_DL, IF_EB("\033[M", ESC_STR "[M")},
895# ifdef TERMINFO
896 {(int)KS_CDL, IF_EB("\033[%p1%dM", ESC_STR "[%p1%dM")},
897# else
898 {(int)KS_CDL, IF_EB("\033[%dM", ESC_STR "[%dM")},
899# endif
900# ifdef TERMINFO
901 {(int)KS_CS, IF_EB("\033[%i%p1%d;%p2%dr",
902 ESC_STR "[%i%p1%d;%p2%dr")},
903# else
904 {(int)KS_CS, IF_EB("\033[%i%d;%dr", ESC_STR "[%i%d;%dr")},
905# endif
906 {(int)KS_CL, IF_EB("\033[H\033[2J", ESC_STR "[H" ESC_STR_nc "[2J")},
907 {(int)KS_CD, IF_EB("\033[J", ESC_STR "[J")},
908 {(int)KS_ME, IF_EB("\033[m", ESC_STR "[m")},
909 {(int)KS_MR, IF_EB("\033[7m", ESC_STR "[7m")},
910 {(int)KS_MD, IF_EB("\033[1m", ESC_STR "[1m")},
911 {(int)KS_UE, IF_EB("\033[m", ESC_STR "[m")},
912 {(int)KS_US, IF_EB("\033[4m", ESC_STR "[4m")},
913 {(int)KS_MS, "y"},
914 {(int)KS_UT, "y"},
915 {(int)KS_LE, "\b"},
916# ifdef TERMINFO
917 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
918 ESC_STR "[%i%p1%d;%p2%dH")},
919# else
920 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
921# endif
922 {(int)KS_SR, IF_EB("\033M", ESC_STR "M")},
923# ifdef TERMINFO
924 {(int)KS_CRI, IF_EB("\033[%p1%dC", ESC_STR "[%p1%dC")},
925# else
926 {(int)KS_CRI, IF_EB("\033[%dC", ESC_STR "[%dC")},
927# endif
928 {(int)KS_KS, IF_EB("\033[?1h\033=", ESC_STR "[?1h" ESC_STR_nc "=")},
929 {(int)KS_KE, IF_EB("\033[?1l\033>", ESC_STR "[?1l" ESC_STR_nc ">")},
930# ifdef FEAT_XTERM_SAVE
931 {(int)KS_TI, IF_EB("\0337\033[?47h", ESC_STR "7" ESC_STR_nc "[?47h")},
932 {(int)KS_TE, IF_EB("\033[2J\033[?47l\0338",
933 ESC_STR "[2J" ESC_STR_nc "[?47l" ESC_STR_nc "8")},
934# endif
935 {(int)KS_CIS, IF_EB("\033]1;", ESC_STR "]1;")},
936 {(int)KS_CIE, "\007"},
937 {(int)KS_TS, IF_EB("\033]2;", ESC_STR "]2;")},
938 {(int)KS_FS, "\007"},
939# ifdef TERMINFO
940 {(int)KS_CWS, IF_EB("\033[8;%p1%d;%p2%dt",
941 ESC_STR "[8;%p1%d;%p2%dt")},
942 {(int)KS_CWP, IF_EB("\033[3;%p1%d;%p2%dt",
943 ESC_STR "[3;%p1%d;%p2%dt")},
944# else
945 {(int)KS_CWS, IF_EB("\033[8;%d;%dt", ESC_STR "[8;%d;%dt")},
946 {(int)KS_CWP, IF_EB("\033[3;%d;%dt", ESC_STR "[3;%d;%dt")},
947# endif
948 {(int)KS_CRV, IF_EB("\033[>c", ESC_STR "[>c")},
Bram Moolenaar9584b312013-03-13 19:29:28 +0100949 {(int)KS_U7, IF_EB("\033[6n", ESC_STR "[6n")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000950
951 {K_UP, IF_EB("\033O*A", ESC_STR "O*A")},
952 {K_DOWN, IF_EB("\033O*B", ESC_STR "O*B")},
953 {K_RIGHT, IF_EB("\033O*C", ESC_STR "O*C")},
954 {K_LEFT, IF_EB("\033O*D", ESC_STR "O*D")},
955 /* An extra set of cursor keys for vt100 mode */
956 {K_XUP, IF_EB("\033[1;*A", ESC_STR "[1;*A")},
957 {K_XDOWN, IF_EB("\033[1;*B", ESC_STR "[1;*B")},
958 {K_XRIGHT, IF_EB("\033[1;*C", ESC_STR "[1;*C")},
959 {K_XLEFT, IF_EB("\033[1;*D", ESC_STR "[1;*D")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 /* An extra set of function keys for vt100 mode */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000961 {K_XF1, IF_EB("\033O*P", ESC_STR "O*P")},
962 {K_XF2, IF_EB("\033O*Q", ESC_STR "O*Q")},
963 {K_XF3, IF_EB("\033O*R", ESC_STR "O*R")},
964 {K_XF4, IF_EB("\033O*S", ESC_STR "O*S")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000965 {K_F1, IF_EB("\033[11;*~", ESC_STR "[11;*~")},
966 {K_F2, IF_EB("\033[12;*~", ESC_STR "[12;*~")},
967 {K_F3, IF_EB("\033[13;*~", ESC_STR "[13;*~")},
968 {K_F4, IF_EB("\033[14;*~", ESC_STR "[14;*~")},
969 {K_F5, IF_EB("\033[15;*~", ESC_STR "[15;*~")},
970 {K_F6, IF_EB("\033[17;*~", ESC_STR "[17;*~")},
971 {K_F7, IF_EB("\033[18;*~", ESC_STR "[18;*~")},
972 {K_F8, IF_EB("\033[19;*~", ESC_STR "[19;*~")},
973 {K_F9, IF_EB("\033[20;*~", ESC_STR "[20;*~")},
974 {K_F10, IF_EB("\033[21;*~", ESC_STR "[21;*~")},
975 {K_F11, IF_EB("\033[23;*~", ESC_STR "[23;*~")},
976 {K_F12, IF_EB("\033[24;*~", ESC_STR "[24;*~")},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 {K_S_TAB, IF_EB("\033[Z", ESC_STR "[Z")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000978 {K_HELP, IF_EB("\033[28;*~", ESC_STR "[28;*~")},
979 {K_UNDO, IF_EB("\033[26;*~", ESC_STR "[26;*~")},
980 {K_INS, IF_EB("\033[2;*~", ESC_STR "[2;*~")},
981 {K_HOME, IF_EB("\033[1;*H", ESC_STR "[1;*H")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000982 /* {K_S_HOME, IF_EB("\033O2H", ESC_STR "O2H")}, */
983 /* {K_C_HOME, IF_EB("\033O5H", ESC_STR "O5H")}, */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000984 {K_KHOME, IF_EB("\033[1;*~", ESC_STR "[1;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000985 {K_XHOME, IF_EB("\033O*H", ESC_STR "O*H")}, /* other Home */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000986 {K_ZHOME, IF_EB("\033[7;*~", ESC_STR "[7;*~")}, /* other Home */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000987 {K_END, IF_EB("\033[1;*F", ESC_STR "[1;*F")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000988 /* {K_S_END, IF_EB("\033O2F", ESC_STR "O2F")}, */
989 /* {K_C_END, IF_EB("\033O5F", ESC_STR "O5F")}, */
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000990 {K_KEND, IF_EB("\033[4;*~", ESC_STR "[4;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000991 {K_XEND, IF_EB("\033O*F", ESC_STR "O*F")}, /* other End */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000992 {K_ZEND, IF_EB("\033[8;*~", ESC_STR "[8;*~")},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000993 {K_PAGEUP, IF_EB("\033[5;*~", ESC_STR "[5;*~")},
994 {K_PAGEDOWN, IF_EB("\033[6;*~", ESC_STR "[6;*~")},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000995 {K_KPLUS, IF_EB("\033O*k", ESC_STR "O*k")}, /* keypad plus */
996 {K_KMINUS, IF_EB("\033O*m", ESC_STR "O*m")}, /* keypad minus */
997 {K_KDIVIDE, IF_EB("\033O*o", ESC_STR "O*o")}, /* keypad / */
998 {K_KMULTIPLY, IF_EB("\033O*j", ESC_STR "O*j")}, /* keypad * */
999 {K_KENTER, IF_EB("\033O*M", ESC_STR "O*M")}, /* keypad Enter */
1000 {K_KPOINT, IF_EB("\033O*n", ESC_STR "O*n")}, /* keypad . */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001001 {K_KDEL, IF_EB("\033[3;*~", ESC_STR "[3;*~")}, /* keypad Del */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002
1003 {BT_EXTRA_KEYS, ""},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001004 {TERMCAP2KEY('k', '0'), IF_EB("\033[10;*~", ESC_STR "[10;*~")}, /* F0 */
1005 {TERMCAP2KEY('F', '3'), IF_EB("\033[25;*~", ESC_STR "[25;*~")}, /* F13 */
1006 /* F14 and F15 are missing, because they send the same codes as the undo
1007 * and help key, although they don't work on all keyboards. */
1008 {TERMCAP2KEY('F', '6'), IF_EB("\033[29;*~", ESC_STR "[29;*~")}, /* F16 */
1009 {TERMCAP2KEY('F', '7'), IF_EB("\033[31;*~", ESC_STR "[31;*~")}, /* F17 */
1010 {TERMCAP2KEY('F', '8'), IF_EB("\033[32;*~", ESC_STR "[32;*~")}, /* F18 */
1011 {TERMCAP2KEY('F', '9'), IF_EB("\033[33;*~", ESC_STR "[33;*~")}, /* F19 */
1012 {TERMCAP2KEY('F', 'A'), IF_EB("\033[34;*~", ESC_STR "[34;*~")}, /* F20 */
1013
1014 {TERMCAP2KEY('F', 'B'), IF_EB("\033[42;*~", ESC_STR "[42;*~")}, /* F21 */
1015 {TERMCAP2KEY('F', 'C'), IF_EB("\033[43;*~", ESC_STR "[43;*~")}, /* F22 */
1016 {TERMCAP2KEY('F', 'D'), IF_EB("\033[44;*~", ESC_STR "[44;*~")}, /* F23 */
1017 {TERMCAP2KEY('F', 'E'), IF_EB("\033[45;*~", ESC_STR "[45;*~")}, /* F24 */
1018 {TERMCAP2KEY('F', 'F'), IF_EB("\033[46;*~", ESC_STR "[46;*~")}, /* F25 */
1019 {TERMCAP2KEY('F', 'G'), IF_EB("\033[47;*~", ESC_STR "[47;*~")}, /* F26 */
1020 {TERMCAP2KEY('F', 'H'), IF_EB("\033[48;*~", ESC_STR "[48;*~")}, /* F27 */
1021 {TERMCAP2KEY('F', 'I'), IF_EB("\033[49;*~", ESC_STR "[49;*~")}, /* F28 */
1022 {TERMCAP2KEY('F', 'J'), IF_EB("\033[50;*~", ESC_STR "[50;*~")}, /* F29 */
1023 {TERMCAP2KEY('F', 'K'), IF_EB("\033[51;*~", ESC_STR "[51;*~")}, /* F30 */
1024
1025 {TERMCAP2KEY('F', 'L'), IF_EB("\033[52;*~", ESC_STR "[52;*~")}, /* F31 */
1026 {TERMCAP2KEY('F', 'M'), IF_EB("\033[53;*~", ESC_STR "[53;*~")}, /* F32 */
1027 {TERMCAP2KEY('F', 'N'), IF_EB("\033[54;*~", ESC_STR "[54;*~")}, /* F33 */
1028 {TERMCAP2KEY('F', 'O'), IF_EB("\033[55;*~", ESC_STR "[55;*~")}, /* F34 */
1029 {TERMCAP2KEY('F', 'P'), IF_EB("\033[56;*~", ESC_STR "[56;*~")}, /* F35 */
1030 {TERMCAP2KEY('F', 'Q'), IF_EB("\033[57;*~", ESC_STR "[57;*~")}, /* F36 */
1031 {TERMCAP2KEY('F', 'R'), IF_EB("\033[58;*~", ESC_STR "[58;*~")}, /* F37 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032# endif
1033
1034# if defined(UNIX) || defined(ALL_BUILTIN_TCAPS)
1035/*
1036 * iris-ansi for Silicon Graphics machines.
1037 */
1038 {(int)KS_NAME, "iris-ansi"},
1039 {(int)KS_CE, "\033[K"},
1040 {(int)KS_CD, "\033[J"},
1041 {(int)KS_AL, "\033[L"},
1042# ifdef TERMINFO
1043 {(int)KS_CAL, "\033[%p1%dL"},
1044# else
1045 {(int)KS_CAL, "\033[%dL"},
1046# endif
1047 {(int)KS_DL, "\033[M"},
1048# ifdef TERMINFO
1049 {(int)KS_CDL, "\033[%p1%dM"},
1050# else
1051 {(int)KS_CDL, "\033[%dM"},
1052# endif
1053#if 0 /* The scroll region is not working as Vim expects. */
1054# ifdef TERMINFO
1055 {(int)KS_CS, "\033[%i%p1%d;%p2%dr"},
1056# else
1057 {(int)KS_CS, "\033[%i%d;%dr"},
1058# endif
1059#endif
1060 {(int)KS_CL, "\033[H\033[2J"},
1061 {(int)KS_VE, "\033[9/y\033[12/y"}, /* These aren't documented */
1062 {(int)KS_VS, "\033[10/y\033[=1h\033[=2l"}, /* These aren't documented */
1063 {(int)KS_TI, "\033[=6h"},
1064 {(int)KS_TE, "\033[=6l"},
1065 {(int)KS_SE, "\033[21;27m"},
1066 {(int)KS_SO, "\033[1;7m"},
1067 {(int)KS_ME, "\033[m"},
1068 {(int)KS_MR, "\033[7m"},
1069 {(int)KS_MD, "\033[1m"},
1070 {(int)KS_CCO, "8"}, /* allow 8 colors */
1071 {(int)KS_CZH, "\033[3m"}, /* italic mode on */
1072 {(int)KS_CZR, "\033[23m"}, /* italic mode off */
1073 {(int)KS_US, "\033[4m"}, /* underline on */
1074 {(int)KS_UE, "\033[24m"}, /* underline off */
1075# ifdef TERMINFO
1076 {(int)KS_CAB, "\033[4%p1%dm"}, /* set background color (ANSI) */
1077 {(int)KS_CAF, "\033[3%p1%dm"}, /* set foreground color (ANSI) */
1078 {(int)KS_CSB, "\033[102;%p1%dm"}, /* set screen background color */
1079 {(int)KS_CSF, "\033[101;%p1%dm"}, /* set screen foreground color */
1080# else
1081 {(int)KS_CAB, "\033[4%dm"}, /* set background color (ANSI) */
1082 {(int)KS_CAF, "\033[3%dm"}, /* set foreground color (ANSI) */
1083 {(int)KS_CSB, "\033[102;%dm"}, /* set screen background color */
1084 {(int)KS_CSF, "\033[101;%dm"}, /* set screen foreground color */
1085# endif
1086 {(int)KS_MS, "y"}, /* guessed */
1087 {(int)KS_UT, "y"}, /* guessed */
1088 {(int)KS_LE, "\b"},
1089# ifdef TERMINFO
1090 {(int)KS_CM, "\033[%i%p1%d;%p2%dH"},
1091# else
1092 {(int)KS_CM, "\033[%i%d;%dH"},
1093# endif
1094 {(int)KS_SR, "\033M"},
1095# ifdef TERMINFO
1096 {(int)KS_CRI, "\033[%p1%dC"},
1097# else
1098 {(int)KS_CRI, "\033[%dC"},
1099# endif
1100 {(int)KS_CIS, "\033P3.y"},
1101 {(int)KS_CIE, "\234"}, /* ST "String Terminator" */
1102 {(int)KS_TS, "\033P1.y"},
1103 {(int)KS_FS, "\234"}, /* ST "String Terminator" */
1104# ifdef TERMINFO
1105 {(int)KS_CWS, "\033[203;%p1%d;%p2%d/y"},
1106 {(int)KS_CWP, "\033[205;%p1%d;%p2%d/y"},
1107# else
1108 {(int)KS_CWS, "\033[203;%d;%d/y"},
1109 {(int)KS_CWP, "\033[205;%d;%d/y"},
1110# endif
1111 {K_UP, "\033[A"},
1112 {K_DOWN, "\033[B"},
1113 {K_LEFT, "\033[D"},
1114 {K_RIGHT, "\033[C"},
1115 {K_S_UP, "\033[161q"},
1116 {K_S_DOWN, "\033[164q"},
1117 {K_S_LEFT, "\033[158q"},
1118 {K_S_RIGHT, "\033[167q"},
1119 {K_F1, "\033[001q"},
1120 {K_F2, "\033[002q"},
1121 {K_F3, "\033[003q"},
1122 {K_F4, "\033[004q"},
1123 {K_F5, "\033[005q"},
1124 {K_F6, "\033[006q"},
1125 {K_F7, "\033[007q"},
1126 {K_F8, "\033[008q"},
1127 {K_F9, "\033[009q"},
1128 {K_F10, "\033[010q"},
1129 {K_F11, "\033[011q"},
1130 {K_F12, "\033[012q"},
1131 {K_S_F1, "\033[013q"},
1132 {K_S_F2, "\033[014q"},
1133 {K_S_F3, "\033[015q"},
1134 {K_S_F4, "\033[016q"},
1135 {K_S_F5, "\033[017q"},
1136 {K_S_F6, "\033[018q"},
1137 {K_S_F7, "\033[019q"},
1138 {K_S_F8, "\033[020q"},
1139 {K_S_F9, "\033[021q"},
1140 {K_S_F10, "\033[022q"},
1141 {K_S_F11, "\033[023q"},
1142 {K_S_F12, "\033[024q"},
1143 {K_INS, "\033[139q"},
1144 {K_HOME, "\033[H"},
1145 {K_END, "\033[146q"},
1146 {K_PAGEUP, "\033[150q"},
1147 {K_PAGEDOWN, "\033[154q"},
1148# endif
1149
1150# if defined(DEBUG) || defined(ALL_BUILTIN_TCAPS)
1151/*
1152 * for debugging
1153 */
1154 {(int)KS_NAME, "debug"},
1155 {(int)KS_CE, "[CE]"},
1156 {(int)KS_CD, "[CD]"},
1157 {(int)KS_AL, "[AL]"},
1158# ifdef TERMINFO
1159 {(int)KS_CAL, "[CAL%p1%d]"},
1160# else
1161 {(int)KS_CAL, "[CAL%d]"},
1162# endif
1163 {(int)KS_DL, "[DL]"},
1164# ifdef TERMINFO
1165 {(int)KS_CDL, "[CDL%p1%d]"},
1166# else
1167 {(int)KS_CDL, "[CDL%d]"},
1168# endif
1169# ifdef TERMINFO
1170 {(int)KS_CS, "[%p1%dCS%p2%d]"},
1171# else
1172 {(int)KS_CS, "[%dCS%d]"},
1173# endif
1174# ifdef FEAT_VERTSPLIT
1175# ifdef TERMINFO
1176 {(int)KS_CSV, "[%p1%dCSV%p2%d]"},
1177# else
1178 {(int)KS_CSV, "[%dCSV%d]"},
1179# endif
1180# endif
1181# ifdef TERMINFO
1182 {(int)KS_CAB, "[CAB%p1%d]"},
1183 {(int)KS_CAF, "[CAF%p1%d]"},
1184 {(int)KS_CSB, "[CSB%p1%d]"},
1185 {(int)KS_CSF, "[CSF%p1%d]"},
1186# else
1187 {(int)KS_CAB, "[CAB%d]"},
1188 {(int)KS_CAF, "[CAF%d]"},
1189 {(int)KS_CSB, "[CSB%d]"},
1190 {(int)KS_CSF, "[CSF%d]"},
1191# endif
1192 {(int)KS_OP, "[OP]"},
1193 {(int)KS_LE, "[LE]"},
1194 {(int)KS_CL, "[CL]"},
1195 {(int)KS_VI, "[VI]"},
1196 {(int)KS_VE, "[VE]"},
1197 {(int)KS_VS, "[VS]"},
1198 {(int)KS_ME, "[ME]"},
1199 {(int)KS_MR, "[MR]"},
1200 {(int)KS_MB, "[MB]"},
1201 {(int)KS_MD, "[MD]"},
1202 {(int)KS_SE, "[SE]"},
1203 {(int)KS_SO, "[SO]"},
1204 {(int)KS_UE, "[UE]"},
1205 {(int)KS_US, "[US]"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001206 {(int)KS_UCE, "[UCE]"},
1207 {(int)KS_UCS, "[UCS]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {(int)KS_MS, "[MS]"},
1209 {(int)KS_UT, "[UT]"},
1210# ifdef TERMINFO
1211 {(int)KS_CM, "[%p1%dCM%p2%d]"},
1212# else
1213 {(int)KS_CM, "[%dCM%d]"},
1214# endif
1215 {(int)KS_SR, "[SR]"},
1216# ifdef TERMINFO
1217 {(int)KS_CRI, "[CRI%p1%d]"},
1218# else
1219 {(int)KS_CRI, "[CRI%d]"},
1220# endif
1221 {(int)KS_VB, "[VB]"},
1222 {(int)KS_KS, "[KS]"},
1223 {(int)KS_KE, "[KE]"},
1224 {(int)KS_TI, "[TI]"},
1225 {(int)KS_TE, "[TE]"},
1226 {(int)KS_CIS, "[CIS]"},
1227 {(int)KS_CIE, "[CIE]"},
1228 {(int)KS_TS, "[TS]"},
1229 {(int)KS_FS, "[FS]"},
1230# ifdef TERMINFO
1231 {(int)KS_CWS, "[%p1%dCWS%p2%d]"},
1232 {(int)KS_CWP, "[%p1%dCWP%p2%d]"},
1233# else
1234 {(int)KS_CWS, "[%dCWS%d]"},
1235 {(int)KS_CWP, "[%dCWP%d]"},
1236# endif
1237 {(int)KS_CRV, "[CRV]"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001238 {(int)KS_U7, "[U7]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {K_UP, "[KU]"},
1240 {K_DOWN, "[KD]"},
1241 {K_LEFT, "[KL]"},
1242 {K_RIGHT, "[KR]"},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001243 {K_XUP, "[xKU]"},
1244 {K_XDOWN, "[xKD]"},
1245 {K_XLEFT, "[xKL]"},
1246 {K_XRIGHT, "[xKR]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 {K_S_UP, "[S-KU]"},
1248 {K_S_DOWN, "[S-KD]"},
1249 {K_S_LEFT, "[S-KL]"},
1250 {K_C_LEFT, "[C-KL]"},
1251 {K_S_RIGHT, "[S-KR]"},
1252 {K_C_RIGHT, "[C-KR]"},
1253 {K_F1, "[F1]"},
1254 {K_XF1, "[xF1]"},
1255 {K_F2, "[F2]"},
1256 {K_XF2, "[xF2]"},
1257 {K_F3, "[F3]"},
1258 {K_XF3, "[xF3]"},
1259 {K_F4, "[F4]"},
1260 {K_XF4, "[xF4]"},
1261 {K_F5, "[F5]"},
1262 {K_F6, "[F6]"},
1263 {K_F7, "[F7]"},
1264 {K_F8, "[F8]"},
1265 {K_F9, "[F9]"},
1266 {K_F10, "[F10]"},
1267 {K_F11, "[F11]"},
1268 {K_F12, "[F12]"},
1269 {K_S_F1, "[S-F1]"},
1270 {K_S_XF1, "[S-xF1]"},
1271 {K_S_F2, "[S-F2]"},
1272 {K_S_XF2, "[S-xF2]"},
1273 {K_S_F3, "[S-F3]"},
1274 {K_S_XF3, "[S-xF3]"},
1275 {K_S_F4, "[S-F4]"},
1276 {K_S_XF4, "[S-xF4]"},
1277 {K_S_F5, "[S-F5]"},
1278 {K_S_F6, "[S-F6]"},
1279 {K_S_F7, "[S-F7]"},
1280 {K_S_F8, "[S-F8]"},
1281 {K_S_F9, "[S-F9]"},
1282 {K_S_F10, "[S-F10]"},
1283 {K_S_F11, "[S-F11]"},
1284 {K_S_F12, "[S-F12]"},
1285 {K_HELP, "[HELP]"},
1286 {K_UNDO, "[UNDO]"},
1287 {K_BS, "[BS]"},
1288 {K_INS, "[INS]"},
1289 {K_KINS, "[KINS]"},
1290 {K_DEL, "[DEL]"},
1291 {K_KDEL, "[KDEL]"},
1292 {K_HOME, "[HOME]"},
1293 {K_S_HOME, "[C-HOME]"},
1294 {K_C_HOME, "[C-HOME]"},
1295 {K_KHOME, "[KHOME]"},
1296 {K_XHOME, "[XHOME]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001297 {K_ZHOME, "[ZHOME]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {K_END, "[END]"},
1299 {K_S_END, "[C-END]"},
1300 {K_C_END, "[C-END]"},
1301 {K_KEND, "[KEND]"},
1302 {K_XEND, "[XEND]"},
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001303 {K_ZEND, "[ZEND]"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {K_PAGEUP, "[PAGEUP]"},
1305 {K_PAGEDOWN, "[PAGEDOWN]"},
1306 {K_KPAGEUP, "[KPAGEUP]"},
1307 {K_KPAGEDOWN, "[KPAGEDOWN]"},
1308 {K_MOUSE, "[MOUSE]"},
1309 {K_KPLUS, "[KPLUS]"},
1310 {K_KMINUS, "[KMINUS]"},
1311 {K_KDIVIDE, "[KDIVIDE]"},
1312 {K_KMULTIPLY, "[KMULTIPLY]"},
1313 {K_KENTER, "[KENTER]"},
1314 {K_KPOINT, "[KPOINT]"},
1315 {K_K0, "[K0]"},
1316 {K_K1, "[K1]"},
1317 {K_K2, "[K2]"},
1318 {K_K3, "[K3]"},
1319 {K_K4, "[K4]"},
1320 {K_K5, "[K5]"},
1321 {K_K6, "[K6]"},
1322 {K_K7, "[K7]"},
1323 {K_K8, "[K8]"},
1324 {K_K9, "[K9]"},
1325# endif
1326
1327#endif /* NO_BUILTIN_TCAPS */
1328
1329/*
1330 * The most minimal terminal: only clear screen and cursor positioning
1331 * Always included.
1332 */
1333 {(int)KS_NAME, "dumb"},
1334 {(int)KS_CL, "\014"},
1335#ifdef TERMINFO
1336 {(int)KS_CM, IF_EB("\033[%i%p1%d;%p2%dH",
1337 ESC_STR "[%i%p1%d;%p2%dH")},
1338#else
1339 {(int)KS_CM, IF_EB("\033[%i%d;%dH", ESC_STR "[%i%d;%dH")},
1340#endif
1341
1342/*
1343 * end marker
1344 */
1345 {(int)KS_NAME, NULL}
1346
1347}; /* end of builtin_termcaps */
1348
1349/*
1350 * DEFAULT_TERM is used, when no terminal is specified with -T option or $TERM.
1351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352#ifdef AMIGA
1353# define DEFAULT_TERM (char_u *)"amiga"
1354#endif
1355
1356#ifdef MSWIN
1357# define DEFAULT_TERM (char_u *)"win32"
1358#endif
1359
1360#ifdef MSDOS
1361# define DEFAULT_TERM (char_u *)"pcterm"
1362#endif
1363
1364#if defined(UNIX) && !defined(__MINT__)
1365# define DEFAULT_TERM (char_u *)"ansi"
1366#endif
1367
1368#ifdef __MINT__
1369# define DEFAULT_TERM (char_u *)"vt52"
1370#endif
1371
1372#ifdef __EMX__
1373# define DEFAULT_TERM (char_u *)"os2ansi"
1374#endif
1375
1376#ifdef VMS
1377# define DEFAULT_TERM (char_u *)"vt320"
1378#endif
1379
1380#ifdef __BEOS__
1381# undef DEFAULT_TERM
1382# define DEFAULT_TERM (char_u *)"beos-ansi"
1383#endif
1384
1385#ifndef DEFAULT_TERM
1386# define DEFAULT_TERM (char_u *)"dumb"
1387#endif
1388
1389/*
1390 * Term_strings contains currently used terminal output strings.
1391 * It is initialized with the default values by parse_builtin_tcap().
1392 * The values can be changed by setting the option with the same name.
1393 */
1394char_u *(term_strings[(int)KS_LAST + 1]);
1395
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001396static int need_gather = FALSE; /* need to fill termleader[] */
1397static char_u termleader[256 + 1]; /* for check_termcode() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#ifdef FEAT_TERMRESPONSE
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00001399static int check_for_codes = FALSE; /* check for key code response */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400#endif
1401
1402 static struct builtin_term *
1403find_builtin_term(term)
1404 char_u *term;
1405{
1406 struct builtin_term *p;
1407
1408 p = builtin_termcaps;
1409 while (p->bt_string != NULL)
1410 {
1411 if (p->bt_entry == (int)KS_NAME)
1412 {
1413#ifdef UNIX
1414 if (STRCMP(p->bt_string, "iris-ansi") == 0 && vim_is_iris(term))
1415 return p;
1416 else if (STRCMP(p->bt_string, "xterm") == 0 && vim_is_xterm(term))
1417 return p;
1418 else
1419#endif
1420#ifdef VMS
1421 if (STRCMP(p->bt_string, "vt320") == 0 && vim_is_vt300(term))
1422 return p;
1423 else
1424#endif
1425 if (STRCMP(term, p->bt_string) == 0)
1426 return p;
1427 }
1428 ++p;
1429 }
1430 return p;
1431}
1432
1433/*
1434 * Parsing of the builtin termcap entries.
1435 * Caller should check if 'name' is a valid builtin term.
1436 * The terminal's name is not set, as this is already done in termcapinit().
1437 */
1438 static void
1439parse_builtin_tcap(term)
1440 char_u *term;
1441{
1442 struct builtin_term *p;
1443 char_u name[2];
1444 int term_8bit;
1445
1446 p = find_builtin_term(term);
1447 term_8bit = term_is_8bit(term);
1448
1449 /* Do not parse if builtin term not found */
1450 if (p->bt_string == NULL)
1451 return;
1452
1453 for (++p; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
1454 {
1455 if ((int)p->bt_entry >= 0) /* KS_xx entry */
1456 {
1457 /* Only set the value if it wasn't set yet. */
1458 if (term_strings[p->bt_entry] == NULL
1459 || term_strings[p->bt_entry] == empty_option)
1460 {
1461 /* 8bit terminal: use CSI instead of <Esc>[ */
1462 if (term_8bit && term_7to8bit((char_u *)p->bt_string) != 0)
1463 {
1464 char_u *s, *t;
1465
1466 s = vim_strsave((char_u *)p->bt_string);
1467 if (s != NULL)
1468 {
1469 for (t = s; *t; ++t)
1470 if (term_7to8bit(t))
1471 {
1472 *t = term_7to8bit(t);
1473 STRCPY(t + 1, t + 2);
1474 }
1475 term_strings[p->bt_entry] = s;
1476 set_term_option_alloced(&term_strings[p->bt_entry]);
1477 }
1478 }
1479 else
1480 term_strings[p->bt_entry] = (char_u *)p->bt_string;
1481 }
1482 }
1483 else
1484 {
1485 name[0] = KEY2TERMCAP0((int)p->bt_entry);
1486 name[1] = KEY2TERMCAP1((int)p->bt_entry);
1487 if (find_termcode(name) == NULL)
1488 add_termcode(name, (char_u *)p->bt_string, term_8bit);
1489 }
1490 }
1491}
1492#if defined(HAVE_TGETENT) || defined(FEAT_TERMRESPONSE)
1493static void set_color_count __ARGS((int nr));
1494
1495/*
1496 * Set number of colors.
1497 * Store it as a number in t_colors.
1498 * Store it as a string in T_CCO (using nr_colors[]).
1499 */
1500 static void
1501set_color_count(nr)
1502 int nr;
1503{
1504 char_u nr_colors[20]; /* string for number of colors */
1505
1506 t_colors = nr;
1507 if (t_colors > 1)
1508 sprintf((char *)nr_colors, "%d", t_colors);
1509 else
1510 *nr_colors = NUL;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001511 set_string_option_direct((char_u *)"t_Co", -1, nr_colors, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512}
1513#endif
1514
1515#ifdef HAVE_TGETENT
1516static char *(key_names[]) =
1517{
1518#ifdef FEAT_TERMRESPONSE
1519 /* Do this one first, it may cause a screen redraw. */
1520 "Co",
1521#endif
1522 "ku", "kd", "kr", "kl",
1523# ifdef ARCHIE
1524 "su", "sd", /* Termcap code made up! */
1525# endif
1526 "#2", "#4", "%i", "*7",
1527 "k1", "k2", "k3", "k4", "k5", "k6",
1528 "k7", "k8", "k9", "k;", "F1", "F2",
1529 "%1", "&8", "kb", "kI", "kD", "kh",
1530 "@7", "kP", "kN", "K1", "K3", "K4", "K5", "kB",
1531 NULL
1532};
1533#endif
1534
1535/*
1536 * Set terminal options for terminal "term".
1537 * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
1538 *
1539 * While doing this, until ttest(), some options may be NULL, be careful.
1540 */
1541 int
1542set_termname(term)
1543 char_u *term;
1544{
1545 struct builtin_term *termp;
1546#ifdef HAVE_TGETENT
1547 int builtin_first = p_tbi;
1548 int try;
1549 int termcap_cleared = FALSE;
1550#endif
1551 int width = 0, height = 0;
1552 char_u *error_msg = NULL;
1553 char_u *bs_p, *del_p;
1554
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001555 /* In silect mode (ex -s) we don't use the 'term' option. */
1556 if (silent_mode)
1557 return OK;
1558
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 detected_8bit = FALSE; /* reset 8-bit detection */
1560
1561 if (term_is_builtin(term))
1562 {
1563 term += 8;
1564#ifdef HAVE_TGETENT
1565 builtin_first = 1;
1566#endif
1567 }
1568
1569/*
1570 * If HAVE_TGETENT is not defined, only the builtin termcap is used, otherwise:
1571 * If builtin_first is TRUE:
1572 * 0. try builtin termcap
1573 * 1. try external termcap
1574 * 2. if both fail default to a builtin terminal
1575 * If builtin_first is FALSE:
1576 * 1. try external termcap
1577 * 2. try builtin termcap, if both fail default to a builtin terminal
1578 */
1579#ifdef HAVE_TGETENT
1580 for (try = builtin_first ? 0 : 1; try < 3; ++try)
1581 {
1582 /*
1583 * Use external termcap
1584 */
1585 if (try == 1)
1586 {
1587 char_u *p;
1588 static char_u tstrbuf[TBUFSZ];
1589 int i;
1590 char_u tbuf[TBUFSZ];
1591 char_u *tp;
1592 static struct {
1593 enum SpecialKey dest; /* index in term_strings[] */
1594 char *name; /* termcap name for string */
1595 } string_names[] =
1596 { {KS_CE, "ce"}, {KS_AL, "al"}, {KS_CAL,"AL"},
1597 {KS_DL, "dl"}, {KS_CDL,"DL"}, {KS_CS, "cs"},
1598 {KS_CL, "cl"}, {KS_CD, "cd"},
1599 {KS_VI, "vi"}, {KS_VE, "ve"}, {KS_MB, "mb"},
1600 {KS_VS, "vs"}, {KS_ME, "me"}, {KS_MR, "mr"},
1601 {KS_MD, "md"}, {KS_SE, "se"}, {KS_SO, "so"},
1602 {KS_CZH,"ZH"}, {KS_CZR,"ZR"}, {KS_UE, "ue"},
Bram Moolenaare2cc9702005-03-15 22:43:58 +00001603 {KS_US, "us"}, {KS_UCE, "Ce"}, {KS_UCS, "Cs"},
1604 {KS_CM, "cm"}, {KS_SR, "sr"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 {KS_CRI,"RI"}, {KS_VB, "vb"}, {KS_KS, "ks"},
1606 {KS_KE, "ke"}, {KS_TI, "ti"}, {KS_TE, "te"},
1607 {KS_BC, "bc"}, {KS_CSB,"Sb"}, {KS_CSF,"Sf"},
1608 {KS_CAB,"AB"}, {KS_CAF,"AF"}, {KS_LE, "le"},
1609 {KS_ND, "nd"}, {KS_OP, "op"}, {KS_CRV, "RV"},
1610 {KS_CIS, "IS"}, {KS_CIE, "IE"},
1611 {KS_TS, "ts"}, {KS_FS, "fs"},
1612 {KS_CWP, "WP"}, {KS_CWS, "WS"},
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001613 {KS_CSI, "SI"}, {KS_CEI, "EI"},
Bram Moolenaar9584b312013-03-13 19:29:28 +01001614 {KS_U7, "u7"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {(enum SpecialKey)0, NULL}
1616 };
1617
1618 /*
1619 * If the external termcap does not have a matching entry, try the
1620 * builtin ones.
1621 */
1622 if ((error_msg = tgetent_error(tbuf, term)) == NULL)
1623 {
1624 tp = tstrbuf;
1625 if (!termcap_cleared)
1626 {
1627 clear_termoptions(); /* clear old options */
1628 termcap_cleared = TRUE;
1629 }
1630
1631 /* get output strings */
1632 for (i = 0; string_names[i].name != NULL; ++i)
1633 {
1634 if (term_str(string_names[i].dest) == NULL
1635 || term_str(string_names[i].dest) == empty_option)
1636 term_str(string_names[i].dest) =
1637 TGETSTR(string_names[i].name, &tp);
1638 }
1639
1640 /* tgetflag() returns 1 if the flag is present, 0 if not and
1641 * possibly -1 if the flag doesn't exist. */
1642 if ((T_MS == NULL || T_MS == empty_option)
1643 && tgetflag("ms") > 0)
1644 T_MS = (char_u *)"y";
1645 if ((T_XS == NULL || T_XS == empty_option)
1646 && tgetflag("xs") > 0)
1647 T_XS = (char_u *)"y";
1648 if ((T_DB == NULL || T_DB == empty_option)
1649 && tgetflag("db") > 0)
1650 T_DB = (char_u *)"y";
1651 if ((T_DA == NULL || T_DA == empty_option)
1652 && tgetflag("da") > 0)
1653 T_DA = (char_u *)"y";
1654 if ((T_UT == NULL || T_UT == empty_option)
1655 && tgetflag("ut") > 0)
1656 T_UT = (char_u *)"y";
1657
1658
1659 /*
1660 * get key codes
1661 */
1662 for (i = 0; key_names[i] != NULL; ++i)
1663 {
1664 if (find_termcode((char_u *)key_names[i]) == NULL)
1665 {
1666 p = TGETSTR(key_names[i], &tp);
1667 /* if cursor-left == backspace, ignore it (televideo
1668 * 925) */
1669 if (p != NULL
1670 && (*p != Ctrl_H
1671 || key_names[i][0] != 'k'
1672 || key_names[i][1] != 'l'))
1673 add_termcode((char_u *)key_names[i], p, FALSE);
1674 }
1675 }
1676
1677 if (height == 0)
1678 height = tgetnum("li");
1679 if (width == 0)
1680 width = tgetnum("co");
1681
1682 /*
1683 * Get number of colors (if not done already).
1684 */
1685 if (term_str(KS_CCO) == NULL
1686 || term_str(KS_CCO) == empty_option)
1687 set_color_count(tgetnum("Co"));
1688
1689# ifndef hpux
1690 BC = (char *)TGETSTR("bc", &tp);
1691 UP = (char *)TGETSTR("up", &tp);
1692 p = TGETSTR("pc", &tp);
1693 if (p)
1694 PC = *p;
1695# endif /* hpux */
1696 }
1697 }
1698 else /* try == 0 || try == 2 */
1699#endif /* HAVE_TGETENT */
1700 /*
1701 * Use builtin termcap
1702 */
1703 {
1704#ifdef HAVE_TGETENT
1705 /*
1706 * If builtin termcap was already used, there is no need to search
1707 * for the builtin termcap again, quit now.
1708 */
1709 if (try == 2 && builtin_first && termcap_cleared)
1710 break;
1711#endif
1712 /*
1713 * search for 'term' in builtin_termcaps[]
1714 */
1715 termp = find_builtin_term(term);
1716 if (termp->bt_string == NULL) /* did not find it */
1717 {
1718#ifdef HAVE_TGETENT
1719 /*
1720 * If try == 0, first try the external termcap. If that is not
1721 * found we'll get back here with try == 2.
1722 * If termcap_cleared is set we used the external termcap,
1723 * don't complain about not finding the term in the builtin
1724 * termcap.
1725 */
1726 if (try == 0) /* try external one */
1727 continue;
1728 if (termcap_cleared) /* found in external termcap */
1729 break;
1730#endif
1731
1732 mch_errmsg("\r\n");
1733 if (error_msg != NULL)
1734 {
1735 mch_errmsg((char *)error_msg);
1736 mch_errmsg("\r\n");
1737 }
1738 mch_errmsg("'");
1739 mch_errmsg((char *)term);
1740 mch_errmsg(_("' not known. Available builtin terminals are:"));
1741 mch_errmsg("\r\n");
1742 for (termp = &(builtin_termcaps[0]); termp->bt_string != NULL;
1743 ++termp)
1744 {
1745 if (termp->bt_entry == (int)KS_NAME)
1746 {
1747#ifdef HAVE_TGETENT
1748 mch_errmsg(" builtin_");
1749#else
1750 mch_errmsg(" ");
1751#endif
1752 mch_errmsg(termp->bt_string);
1753 mch_errmsg("\r\n");
1754 }
1755 }
1756 /* when user typed :set term=xxx, quit here */
1757 if (starting != NO_SCREEN)
1758 {
1759 screen_start(); /* don't know where cursor is now */
1760 wait_return(TRUE);
1761 return FAIL;
1762 }
1763 term = DEFAULT_TERM;
1764 mch_errmsg(_("defaulting to '"));
1765 mch_errmsg((char *)term);
1766 mch_errmsg("'\r\n");
1767 if (emsg_silent == 0)
1768 {
1769 screen_start(); /* don't know where cursor is now */
1770 out_flush();
1771 ui_delay(2000L, TRUE);
1772 }
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001773 set_string_option_direct((char_u *)"term", -1, term,
1774 OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 display_errors();
1776 }
1777 out_flush();
1778#ifdef HAVE_TGETENT
1779 if (!termcap_cleared)
1780 {
1781#endif
1782 clear_termoptions(); /* clear old options */
1783#ifdef HAVE_TGETENT
1784 termcap_cleared = TRUE;
1785 }
1786#endif
1787 parse_builtin_tcap(term);
1788#ifdef FEAT_GUI
1789 if (term_is_gui(term))
1790 {
1791 out_flush();
1792 gui_init();
1793 /* If starting the GUI failed, don't do any of the other
1794 * things for this terminal */
1795 if (!gui.in_use)
1796 return FAIL;
1797#ifdef HAVE_TGETENT
1798 break; /* don't try using external termcap */
1799#endif
1800 }
1801#endif /* FEAT_GUI */
1802 }
1803#ifdef HAVE_TGETENT
1804 }
1805#endif
1806
1807/*
1808 * special: There is no info in the termcap about whether the cursor
1809 * positioning is relative to the start of the screen or to the start of the
1810 * scrolling region. We just guess here. Only msdos pcterm is known to do it
1811 * relative.
1812 */
1813 if (STRCMP(term, "pcterm") == 0)
1814 T_CCS = (char_u *)"yes";
1815 else
1816 T_CCS = empty_option;
1817
1818#ifdef UNIX
1819/*
1820 * Any "stty" settings override the default for t_kb from the termcap.
1821 * This is in os_unix.c, because it depends a lot on the version of unix that
1822 * is being used.
1823 * Don't do this when the GUI is active, it uses "t_kb" and "t_kD" directly.
1824 */
1825#ifdef FEAT_GUI
1826 if (!gui.in_use)
1827#endif
1828 get_stty();
1829#endif
1830
1831/*
1832 * If the termcap has no entry for 'bs' and/or 'del' and the ioctl() also
1833 * didn't work, use the default CTRL-H
1834 * The default for t_kD is DEL, unless t_kb is DEL.
1835 * The vim_strsave'd strings are probably lost forever, well it's only two
1836 * bytes. Don't do this when the GUI is active, it uses "t_kb" and "t_kD"
1837 * directly.
1838 */
1839#ifdef FEAT_GUI
1840 if (!gui.in_use)
1841#endif
1842 {
1843 bs_p = find_termcode((char_u *)"kb");
1844 del_p = find_termcode((char_u *)"kD");
1845 if (bs_p == NULL || *bs_p == NUL)
1846 add_termcode((char_u *)"kb", (bs_p = (char_u *)CTRL_H_STR), FALSE);
1847 if ((del_p == NULL || *del_p == NUL) &&
1848 (bs_p == NULL || *bs_p != DEL))
1849 add_termcode((char_u *)"kD", (char_u *)DEL_STR, FALSE);
1850 }
1851
1852#if defined(UNIX) || defined(VMS)
1853 term_is_xterm = vim_is_xterm(term);
1854#endif
1855
1856#ifdef FEAT_MOUSE
1857# if defined(UNIX) || defined(VMS)
1858# ifdef FEAT_MOUSE_TTY
1859 /*
1860 * For Unix, set the 'ttymouse' option to the type of mouse to be used.
1861 * The termcode for the mouse is added as a side effect in option.c.
1862 */
1863 {
1864 char_u *p;
1865
1866 p = (char_u *)"";
1867# ifdef FEAT_MOUSE_XTERM
1868# ifdef FEAT_CLIPBOARD
1869# ifdef FEAT_GUI
1870 if (!gui.in_use)
1871# endif
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001872# ifndef FEAT_CYGWIN_WIN32_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 clip_init(FALSE);
Bram Moolenaar693e40c2013-02-26 14:56:42 +01001874# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875# endif
Bram Moolenaar864207d2008-06-24 22:14:38 +00001876 if (use_xterm_like_mouse(term))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
1878 if (use_xterm_mouse())
1879 p = NULL; /* keep existing value, might be "xterm2" */
1880 else
1881 p = (char_u *)"xterm";
1882 }
1883# endif
1884 if (p != NULL)
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001885 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 set_option_value((char_u *)"ttym", 0L, p, 0);
Bram Moolenaar15d55de2012-12-05 14:43:02 +01001887 /* Reset the WAS_SET flag, 'ttymouse' can be set to "sgr" or
1888 * "xterm2" in check_termcode(). */
1889 reset_option_was_set((char_u *)"ttym");
1890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (p == NULL
1892# ifdef FEAT_GUI
1893 || gui.in_use
1894# endif
1895 )
1896 check_mouse_termcode(); /* set mouse termcode anyway */
1897 }
1898# endif
1899# else
1900 set_mouse_termcode(KS_MOUSE, (char_u *)"\233M");
1901# endif
1902#endif /* FEAT_MOUSE */
1903
1904#ifdef FEAT_SNIFF
1905 {
1906 char_u name[2];
1907
1908 name[0] = (int)KS_EXTRA;
1909 name[1] = (int)KE_SNIFF;
1910 add_termcode(name, (char_u *)"\233sniff", FALSE);
1911 }
1912#endif
1913
1914#ifdef USE_TERM_CONSOLE
1915 /* DEFAULT_TERM indicates that it is the machine console. */
1916 if (STRCMP(term, DEFAULT_TERM) != 0)
1917 term_console = FALSE;
1918 else
1919 {
1920 term_console = TRUE;
1921# ifdef AMIGA
1922 win_resize_on(); /* enable window resizing reports */
1923# endif
1924 }
1925#endif
1926
1927#if defined(UNIX) || defined(VMS)
1928 /*
1929 * 'ttyfast' is default on for xterm, iris-ansi and a few others.
1930 */
1931 if (vim_is_fastterm(term))
1932 p_tf = TRUE;
1933#endif
1934#ifdef USE_TERM_CONSOLE
1935 /*
1936 * 'ttyfast' is default on consoles
1937 */
1938 if (term_console)
1939 p_tf = TRUE;
1940#endif
1941
1942 ttest(TRUE); /* make sure we have a valid set of terminal codes */
1943
1944 full_screen = TRUE; /* we can use termcap codes from now on */
1945 set_term_defaults(); /* use current values as defaults */
1946#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +02001947 LOG_TR("setting crv_status to CRV_GET");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 crv_status = CRV_GET; /* Get terminal version later */
1949#endif
1950
1951 /*
1952 * Initialize the terminal with the appropriate termcap codes.
1953 * Set the mouse and window title if possible.
1954 * Don't do this when starting, need to parse the .vimrc first, because it
1955 * may redefine t_TI etc.
1956 */
1957 if (starting != NO_SCREEN)
1958 {
1959 starttermcap(); /* may change terminal mode */
1960#ifdef FEAT_MOUSE
1961 setmouse(); /* may start using the mouse */
1962#endif
1963#ifdef FEAT_TITLE
1964 maketitle(); /* may display window title */
1965#endif
1966 }
1967
1968 /* display initial screen after ttest() checking. jw. */
1969 if (width <= 0 || height <= 0)
1970 {
1971 /* termcap failed to report size */
1972 /* set defaults, in case ui_get_shellsize() also fails */
1973 width = 80;
1974#if defined(MSDOS) || defined(WIN3264)
1975 height = 25; /* console is often 25 lines */
1976#else
1977 height = 24; /* most terminals are 24 lines */
1978#endif
1979 }
1980 set_shellsize(width, height, FALSE); /* may change Rows */
1981 if (starting != NO_SCREEN)
1982 {
1983 if (scroll_region)
1984 scroll_region_reset(); /* In case Rows changed */
1985 check_map_keycodes(); /* check mappings for terminal codes used */
1986
1987#ifdef FEAT_AUTOCMD
1988 {
1989 buf_T *old_curbuf;
1990
1991 /*
1992 * Execute the TermChanged autocommands for each buffer that is
1993 * loaded.
1994 */
1995 old_curbuf = curbuf;
1996 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
1997 {
1998 if (curbuf->b_ml.ml_mfp != NULL)
1999 apply_autocmds(EVENT_TERMCHANGED, NULL, NULL, FALSE,
2000 curbuf);
2001 }
2002 if (buf_valid(old_curbuf))
2003 curbuf = old_curbuf;
2004 }
2005#endif
2006 }
2007
2008#ifdef FEAT_TERMRESPONSE
2009 may_req_termresponse();
2010#endif
2011
2012 return OK;
2013}
2014
2015#if defined(FEAT_MOUSE) || defined(PROTO)
2016
2017# ifdef FEAT_MOUSE_TTY
2018# define HMT_NORMAL 1
2019# define HMT_NETTERM 2
2020# define HMT_DEC 4
2021# define HMT_JSBTERM 8
2022# define HMT_PTERM 16
Bram Moolenaarb491c032011-11-30 14:47:15 +01002023# define HMT_URXVT 32
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002024# define HMT_SGR 64
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025static int has_mouse_termcode = 0;
2026# endif
2027
Bram Moolenaar864207d2008-06-24 22:14:38 +00002028# if (!defined(UNIX) || defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 void
2030set_mouse_termcode(n, s)
2031 int n; /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2032 char_u *s;
2033{
2034 char_u name[2];
2035
2036 name[0] = n;
2037 name[1] = KE_FILLER;
2038 add_termcode(name, s, FALSE);
2039# ifdef FEAT_MOUSE_TTY
2040# ifdef FEAT_MOUSE_JSB
2041 if (n == KS_JSBTERM_MOUSE)
2042 has_mouse_termcode |= HMT_JSBTERM;
2043 else
2044# endif
2045# ifdef FEAT_MOUSE_NET
2046 if (n == KS_NETTERM_MOUSE)
2047 has_mouse_termcode |= HMT_NETTERM;
2048 else
2049# endif
2050# ifdef FEAT_MOUSE_DEC
2051 if (n == KS_DEC_MOUSE)
2052 has_mouse_termcode |= HMT_DEC;
2053 else
2054# endif
2055# ifdef FEAT_MOUSE_PTERM
2056 if (n == KS_PTERM_MOUSE)
2057 has_mouse_termcode |= HMT_PTERM;
2058 else
2059# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002060# ifdef FEAT_MOUSE_URXVT
2061 if (n == KS_URXVT_MOUSE)
2062 has_mouse_termcode |= HMT_URXVT;
2063 else
2064# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002065# ifdef FEAT_MOUSE_SGR
2066 if (n == KS_SGR_MOUSE)
2067 has_mouse_termcode |= HMT_SGR;
2068 else
2069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 has_mouse_termcode |= HMT_NORMAL;
2071# endif
2072}
2073# endif
2074
2075# if ((defined(UNIX) || defined(VMS) || defined(OS2)) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002076 && defined(FEAT_MOUSE_TTY)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 void
2078del_mouse_termcode(n)
2079 int n; /* KS_MOUSE, KS_NETTERM_MOUSE or KS_DEC_MOUSE */
2080{
2081 char_u name[2];
2082
2083 name[0] = n;
2084 name[1] = KE_FILLER;
2085 del_termcode(name);
2086# ifdef FEAT_MOUSE_TTY
2087# ifdef FEAT_MOUSE_JSB
2088 if (n == KS_JSBTERM_MOUSE)
2089 has_mouse_termcode &= ~HMT_JSBTERM;
2090 else
2091# endif
2092# ifdef FEAT_MOUSE_NET
2093 if (n == KS_NETTERM_MOUSE)
2094 has_mouse_termcode &= ~HMT_NETTERM;
2095 else
2096# endif
2097# ifdef FEAT_MOUSE_DEC
2098 if (n == KS_DEC_MOUSE)
2099 has_mouse_termcode &= ~HMT_DEC;
2100 else
2101# endif
2102# ifdef FEAT_MOUSE_PTERM
2103 if (n == KS_PTERM_MOUSE)
2104 has_mouse_termcode &= ~HMT_PTERM;
2105 else
2106# endif
Bram Moolenaarb491c032011-11-30 14:47:15 +01002107# ifdef FEAT_MOUSE_URXVT
2108 if (n == KS_URXVT_MOUSE)
2109 has_mouse_termcode &= ~HMT_URXVT;
2110 else
2111# endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02002112# ifdef FEAT_MOUSE_SGR
2113 if (n == KS_SGR_MOUSE)
2114 has_mouse_termcode &= ~HMT_SGR;
2115 else
2116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 has_mouse_termcode &= ~HMT_NORMAL;
2118# endif
2119}
2120# endif
2121#endif
2122
2123#ifdef HAVE_TGETENT
2124/*
2125 * Call tgetent()
2126 * Return error message if it fails, NULL if it's OK.
2127 */
2128 static char_u *
2129tgetent_error(tbuf, term)
2130 char_u *tbuf;
2131 char_u *term;
2132{
2133 int i;
2134
2135 i = TGETENT(tbuf, term);
2136 if (i < 0 /* -1 is always an error */
2137# ifdef TGETENT_ZERO_ERR
2138 || i == 0 /* sometimes zero is also an error */
2139# endif
2140 )
2141 {
2142 /* On FreeBSD tputs() gets a SEGV after a tgetent() which fails. Call
2143 * tgetent() with the always existing "dumb" entry to avoid a crash or
2144 * hang. */
2145 (void)TGETENT(tbuf, "dumb");
2146
2147 if (i < 0)
2148# ifdef TGETENT_ZERO_ERR
2149 return (char_u *)_("E557: Cannot open termcap file");
2150 if (i == 0)
2151# endif
2152#ifdef TERMINFO
2153 return (char_u *)_("E558: Terminal entry not found in terminfo");
2154#else
2155 return (char_u *)_("E559: Terminal entry not found in termcap");
2156#endif
2157 }
2158 return NULL;
2159}
2160
2161/*
2162 * Some versions of tgetstr() have been reported to return -1 instead of NULL.
2163 * Fix that here.
2164 */
2165 static char_u *
2166vim_tgetstr(s, pp)
2167 char *s;
2168 char_u **pp;
2169{
2170 char *p;
2171
2172 p = tgetstr(s, (char **)pp);
2173 if (p == (char *)-1)
2174 p = NULL;
2175 return (char_u *)p;
2176}
2177#endif /* HAVE_TGETENT */
2178
2179#if defined(HAVE_TGETENT) && (defined(UNIX) || defined(__EMX__) || defined(VMS) || defined(MACOS_X))
2180/*
2181 * Get Columns and Rows from the termcap. Used after a window signal if the
2182 * ioctl() fails. It doesn't make sense to call tgetent each time if the "co"
2183 * and "li" entries never change. But on some systems this works.
2184 * Errors while getting the entries are ignored.
2185 */
2186 void
2187getlinecol(cp, rp)
2188 long *cp; /* pointer to columns */
2189 long *rp; /* pointer to rows */
2190{
2191 char_u tbuf[TBUFSZ];
2192
2193 if (T_NAME != NULL && *T_NAME != NUL && tgetent_error(tbuf, T_NAME) == NULL)
2194 {
2195 if (*cp == 0)
2196 *cp = tgetnum("co");
2197 if (*rp == 0)
2198 *rp = tgetnum("li");
2199 }
2200}
2201#endif /* defined(HAVE_TGETENT) && defined(UNIX) */
2202
2203/*
2204 * Get a string entry from the termcap and add it to the list of termcodes.
2205 * Used for <t_xx> special keys.
2206 * Give an error message for failure when not sourcing.
2207 * If force given, replace an existing entry.
2208 * Return FAIL if the entry was not found, OK if the entry was added.
2209 */
2210 int
2211add_termcap_entry(name, force)
2212 char_u *name;
2213 int force;
2214{
2215 char_u *term;
2216 int key;
2217 struct builtin_term *termp;
2218#ifdef HAVE_TGETENT
2219 char_u *string;
2220 int i;
2221 int builtin_first;
2222 char_u tbuf[TBUFSZ];
2223 char_u tstrbuf[TBUFSZ];
2224 char_u *tp = tstrbuf;
2225 char_u *error_msg = NULL;
2226#endif
2227
2228/*
2229 * If the GUI is running or will start in a moment, we only support the keys
2230 * that the GUI can produce.
2231 */
2232#ifdef FEAT_GUI
2233 if (gui.in_use || gui.starting)
2234 return gui_mch_haskey(name);
2235#endif
2236
2237 if (!force && find_termcode(name) != NULL) /* it's already there */
2238 return OK;
2239
2240 term = T_NAME;
2241 if (term == NULL || *term == NUL) /* 'term' not defined yet */
2242 return FAIL;
2243
2244 if (term_is_builtin(term)) /* name starts with "builtin_" */
2245 {
2246 term += 8;
2247#ifdef HAVE_TGETENT
2248 builtin_first = TRUE;
2249#endif
2250 }
2251#ifdef HAVE_TGETENT
2252 else
2253 builtin_first = p_tbi;
2254#endif
2255
2256#ifdef HAVE_TGETENT
2257/*
2258 * We can get the entry from the builtin termcap and from the external one.
2259 * If 'ttybuiltin' is on or the terminal name starts with "builtin_", try
2260 * builtin termcap first.
2261 * If 'ttybuiltin' is off, try external termcap first.
2262 */
2263 for (i = 0; i < 2; ++i)
2264 {
2265 if (!builtin_first == i)
2266#endif
2267 /*
2268 * Search in builtin termcap
2269 */
2270 {
2271 termp = find_builtin_term(term);
2272 if (termp->bt_string != NULL) /* found it */
2273 {
2274 key = TERMCAP2KEY(name[0], name[1]);
2275 while (termp->bt_entry != (int)KS_NAME)
2276 {
2277 if ((int)termp->bt_entry == key)
2278 {
2279 add_termcode(name, (char_u *)termp->bt_string,
2280 term_is_8bit(term));
2281 return OK;
2282 }
2283 ++termp;
2284 }
2285 }
2286 }
2287#ifdef HAVE_TGETENT
2288 else
2289 /*
2290 * Search in external termcap
2291 */
2292 {
2293 error_msg = tgetent_error(tbuf, term);
2294 if (error_msg == NULL)
2295 {
2296 string = TGETSTR((char *)name, &tp);
2297 if (string != NULL && *string != NUL)
2298 {
2299 add_termcode(name, string, FALSE);
2300 return OK;
2301 }
2302 }
2303 }
2304 }
2305#endif
2306
2307 if (sourcing_name == NULL)
2308 {
2309#ifdef HAVE_TGETENT
2310 if (error_msg != NULL)
2311 EMSG(error_msg);
2312 else
2313#endif
2314 EMSG2(_("E436: No \"%s\" entry in termcap"), name);
2315 }
2316 return FAIL;
2317}
2318
2319 static int
2320term_is_builtin(name)
2321 char_u *name;
2322{
2323 return (STRNCMP(name, "builtin_", (size_t)8) == 0);
2324}
2325
2326/*
2327 * Return TRUE if terminal "name" uses CSI instead of <Esc>[.
2328 * Assume that the terminal is using 8-bit controls when the name contains
2329 * "8bit", like in "xterm-8bit".
2330 */
2331 int
2332term_is_8bit(name)
2333 char_u *name;
2334{
2335 return (detected_8bit || strstr((char *)name, "8bit") != NULL);
2336}
2337
2338/*
2339 * Translate terminal control chars from 7-bit to 8-bit:
2340 * <Esc>[ -> CSI
2341 * <Esc>] -> <M-C-]>
2342 * <Esc>O -> <M-C-O>
2343 */
2344 static int
2345term_7to8bit(p)
2346 char_u *p;
2347{
2348 if (*p == ESC)
2349 {
2350 if (p[1] == '[')
2351 return CSI;
2352 if (p[1] == ']')
2353 return 0x9d;
2354 if (p[1] == 'O')
2355 return 0x8f;
2356 }
2357 return 0;
2358}
2359
2360#ifdef FEAT_GUI
2361 int
2362term_is_gui(name)
2363 char_u *name;
2364{
2365 return (STRCMP(name, "builtin_gui") == 0 || STRCMP(name, "gui") == 0);
2366}
2367#endif
2368
2369#if !defined(HAVE_TGETENT) || defined(AMIGA) || defined(PROTO)
2370
2371 char_u *
2372tltoa(i)
2373 unsigned long i;
2374{
2375 static char_u buf[16];
2376 char_u *p;
2377
2378 p = buf + 15;
2379 *p = '\0';
2380 do
2381 {
2382 --p;
2383 *p = (char_u) (i % 10 + '0');
2384 i /= 10;
2385 }
2386 while (i > 0 && p > buf);
2387 return p;
2388}
2389#endif
2390
2391#ifndef HAVE_TGETENT
2392
2393/*
2394 * minimal tgoto() implementation.
2395 * no padding and we only parse for %i %d and %+char
2396 */
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002397static char *tgoto __ARGS((char *, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00002399 static char *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400tgoto(cm, x, y)
2401 char *cm;
2402 int x, y;
2403{
2404 static char buf[30];
2405 char *p, *s, *e;
2406
2407 if (!cm)
2408 return "OOPS";
2409 e = buf + 29;
2410 for (s = buf; s < e && *cm; cm++)
2411 {
2412 if (*cm != '%')
2413 {
2414 *s++ = *cm;
2415 continue;
2416 }
2417 switch (*++cm)
2418 {
2419 case 'd':
2420 p = (char *)tltoa((unsigned long)y);
2421 y = x;
2422 while (*p)
2423 *s++ = *p++;
2424 break;
2425 case 'i':
2426 x++;
2427 y++;
2428 break;
2429 case '+':
2430 *s++ = (char)(*++cm + y);
2431 y = x;
2432 break;
2433 case '%':
2434 *s++ = *cm;
2435 break;
2436 default:
2437 return "OOPS";
2438 }
2439 }
2440 *s = '\0';
2441 return buf;
2442}
2443
2444#endif /* HAVE_TGETENT */
2445
2446/*
2447 * Set the terminal name and initialize the terminal options.
2448 * If "name" is NULL or empty, get the terminal name from the environment.
2449 * If that fails, use the default terminal name.
2450 */
2451 void
2452termcapinit(name)
2453 char_u *name;
2454{
2455 char_u *term;
2456
2457 if (name != NULL && *name == NUL)
2458 name = NULL; /* empty name is equal to no name */
2459 term = name;
2460
2461#ifdef __BEOS__
2462 /*
2463 * TERM environment variable is normally set to 'ansi' on the Bebox;
2464 * Since the BeBox doesn't quite support full ANSI yet, we use our
2465 * own custom 'ansi-beos' termcap instead, unless the -T option has
2466 * been given on the command line.
2467 */
2468 if (term == NULL
2469 && strcmp((char *)mch_getenv((char_u *)"TERM"), "ansi") == 0)
2470 term = DEFAULT_TERM;
2471#endif
2472#ifndef MSWIN
2473 if (term == NULL)
2474 term = mch_getenv((char_u *)"TERM");
2475#endif
2476 if (term == NULL || *term == NUL)
2477 term = DEFAULT_TERM;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002478 set_string_option_direct((char_u *)"term", -1, term, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 /* Set the default terminal name. */
2481 set_string_default("term", term);
2482 set_string_default("ttytype", term);
2483
2484 /*
2485 * Avoid using "term" here, because the next mch_getenv() may overwrite it.
2486 */
2487 set_termname(T_NAME != NULL ? T_NAME : term);
2488}
2489
2490/*
2491 * the number of calls to ui_write is reduced by using the buffer "out_buf"
2492 */
2493#ifdef DOS16
2494# define OUT_SIZE 255 /* only have 640K total... */
2495#else
2496# ifdef FEAT_GUI_W16
2497# define OUT_SIZE 1023 /* Save precious 1K near data */
2498# else
2499# define OUT_SIZE 2047
2500# endif
2501#endif
2502 /* Add one to allow mch_write() in os_win32.c to append a NUL */
2503static char_u out_buf[OUT_SIZE + 1];
2504static int out_pos = 0; /* number of chars in out_buf */
2505
2506/*
2507 * out_flush(): flush the output buffer
2508 */
2509 void
2510out_flush()
2511{
2512 int len;
2513
2514 if (out_pos != 0)
2515 {
2516 /* set out_pos to 0 before ui_write, to avoid recursiveness */
2517 len = out_pos;
2518 out_pos = 0;
2519 ui_write(out_buf, len);
2520 }
2521}
2522
2523#if defined(FEAT_MBYTE) || defined(PROTO)
2524/*
2525 * Sometimes a byte out of a multi-byte character is written with out_char().
2526 * To avoid flushing half of the character, call this function first.
2527 */
2528 void
2529out_flush_check()
2530{
2531 if (enc_dbcs != 0 && out_pos >= OUT_SIZE - MB_MAXBYTES)
2532 out_flush();
2533}
2534#endif
2535
2536#ifdef FEAT_GUI
2537/*
2538 * out_trash(): Throw away the contents of the output buffer
2539 */
2540 void
2541out_trash()
2542{
2543 out_pos = 0;
2544}
2545#endif
2546
2547/*
2548 * out_char(c): put a byte into the output buffer.
2549 * Flush it if it becomes full.
2550 * This should not be used for outputting text on the screen (use functions
2551 * like msg_puts() and screen_putchar() for that).
2552 */
2553 void
2554out_char(c)
2555 unsigned c;
2556{
2557#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2558 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2559 out_char('\r');
2560#endif
2561
2562 out_buf[out_pos++] = c;
2563
2564 /* For testing we flush each time. */
2565 if (out_pos >= OUT_SIZE || p_wd)
2566 out_flush();
2567}
2568
2569static void out_char_nf __ARGS((unsigned));
2570
2571/*
2572 * out_char_nf(c): like out_char(), but don't flush when p_wd is set
2573 */
2574 static void
2575out_char_nf(c)
2576 unsigned c;
2577{
2578#if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(MACOS_X_UNIX)
2579 if (c == '\n') /* turn LF into CR-LF (CRMOD doesn't seem to do this) */
2580 out_char_nf('\r');
2581#endif
2582
2583 out_buf[out_pos++] = c;
2584
2585 if (out_pos >= OUT_SIZE)
2586 out_flush();
2587}
2588
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002589#if defined(FEAT_TITLE) || defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI) \
2590 || defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591/*
2592 * A never-padding out_str.
2593 * use this whenever you don't want to run the string through tputs.
2594 * tputs above is harmless, but tputs from the termcap library
2595 * is likely to strip off leading digits, that it mistakes for padding
2596 * information, and "%i", "%d", etc.
2597 * This should only be used for writing terminal codes, not for outputting
2598 * normal text (use functions like msg_puts() and screen_putchar() for that).
2599 */
2600 void
2601out_str_nf(s)
2602 char_u *s;
2603{
2604 if (out_pos > OUT_SIZE - 20) /* avoid terminal strings being split up */
2605 out_flush();
2606 while (*s)
2607 out_char_nf(*s++);
2608
2609 /* For testing we write one string at a time. */
2610 if (p_wd)
2611 out_flush();
2612}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02002613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614
2615/*
2616 * out_str(s): Put a character string a byte at a time into the output buffer.
2617 * If HAVE_TGETENT is defined use the termcap parser. (jw)
2618 * This should only be used for writing terminal codes, not for outputting
2619 * normal text (use functions like msg_puts() and screen_putchar() for that).
2620 */
2621 void
2622out_str(s)
2623 char_u *s;
2624{
2625 if (s != NULL && *s)
2626 {
2627#ifdef FEAT_GUI
2628 /* Don't use tputs() when GUI is used, ncurses crashes. */
2629 if (gui.in_use)
2630 {
2631 out_str_nf(s);
2632 return;
2633 }
2634#endif
2635 /* avoid terminal strings being split up */
2636 if (out_pos > OUT_SIZE - 20)
2637 out_flush();
2638#ifdef HAVE_TGETENT
2639 tputs((char *)s, 1, TPUTSFUNCAST out_char_nf);
2640#else
2641 while (*s)
2642 out_char_nf(*s++);
2643#endif
2644
2645 /* For testing we write one string at a time. */
2646 if (p_wd)
2647 out_flush();
2648 }
2649}
2650
2651/*
2652 * cursor positioning using termcap parser. (jw)
2653 */
2654 void
2655term_windgoto(row, col)
2656 int row;
2657 int col;
2658{
2659 OUT_STR(tgoto((char *)T_CM, col, row));
2660}
2661
2662 void
2663term_cursor_right(i)
2664 int i;
2665{
2666 OUT_STR(tgoto((char *)T_CRI, 0, i));
2667}
2668
2669 void
2670term_append_lines(line_count)
2671 int line_count;
2672{
2673 OUT_STR(tgoto((char *)T_CAL, 0, line_count));
2674}
2675
2676 void
2677term_delete_lines(line_count)
2678 int line_count;
2679{
2680 OUT_STR(tgoto((char *)T_CDL, 0, line_count));
2681}
2682
2683#if defined(HAVE_TGETENT) || defined(PROTO)
2684 void
2685term_set_winpos(x, y)
2686 int x;
2687 int y;
2688{
2689 /* Can't handle a negative value here */
2690 if (x < 0)
2691 x = 0;
2692 if (y < 0)
2693 y = 0;
2694 OUT_STR(tgoto((char *)T_CWP, y, x));
2695}
2696
2697 void
2698term_set_winsize(width, height)
2699 int width;
2700 int height;
2701{
2702 OUT_STR(tgoto((char *)T_CWS, height, width));
2703}
2704#endif
2705
2706 void
2707term_fg_color(n)
2708 int n;
2709{
2710 /* Use "AF" termcap entry if present, "Sf" entry otherwise */
2711 if (*T_CAF)
2712 term_color(T_CAF, n);
2713 else if (*T_CSF)
2714 term_color(T_CSF, n);
2715}
2716
2717 void
2718term_bg_color(n)
2719 int n;
2720{
2721 /* Use "AB" termcap entry if present, "Sb" entry otherwise */
2722 if (*T_CAB)
2723 term_color(T_CAB, n);
2724 else if (*T_CSB)
2725 term_color(T_CSB, n);
2726}
2727
2728 static void
2729term_color(s, n)
2730 char_u *s;
2731 int n;
2732{
2733 char buf[20];
2734 int i = 2; /* index in s[] just after <Esc>[ or CSI */
2735
2736 /* Special handling of 16 colors, because termcap can't handle it */
2737 /* Also accept "\e[3%dm" for TERMINFO, it is sometimes used */
2738 /* Also accept CSI instead of <Esc>[ */
2739 if (n >= 8 && t_colors >= 16
2740 && ((s[0] == ESC && s[1] == '[') || (s[0] == CSI && (i = 1) == 1))
2741 && s[i] != NUL
2742 && (STRCMP(s + i + 1, "%p1%dm") == 0
2743 || STRCMP(s + i + 1, "%dm") == 0)
2744 && (s[i] == '3' || s[i] == '4'))
2745 {
2746 sprintf(buf,
2747#ifdef TERMINFO
2748 "%s%s%%p1%%dm",
2749#else
2750 "%s%s%%dm",
2751#endif
2752 i == 2 ? IF_EB("\033[", ESC_STR "[") : "\233",
2753 s[i] == '3' ? (n >= 16 ? "38;5;" : "9")
2754 : (n >= 16 ? "48;5;" : "10"));
2755 OUT_STR(tgoto(buf, 0, n >= 16 ? n : n - 8));
2756 }
2757 else
2758 OUT_STR(tgoto((char *)s, 0, n));
2759}
2760
2761#if (defined(FEAT_TITLE) && (defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X))) || defined(PROTO)
2762/*
2763 * Generic function to set window title, using t_ts and t_fs.
2764 */
2765 void
2766term_settitle(title)
2767 char_u *title;
2768{
2769 /* t_ts takes one argument: column in status line */
2770 OUT_STR(tgoto((char *)T_TS, 0, 0)); /* set title start */
2771 out_str_nf(title);
2772 out_str(T_FS); /* set title end */
2773 out_flush();
2774}
2775#endif
2776
2777/*
2778 * Make sure we have a valid set or terminal options.
2779 * Replace all entries that are NULL by empty_option
2780 */
2781 void
2782ttest(pairs)
2783 int pairs;
2784{
2785 check_options(); /* make sure no options are NULL */
2786
2787 /*
2788 * MUST have "cm": cursor motion.
2789 */
2790 if (*T_CM == NUL)
2791 EMSG(_("E437: terminal capability \"cm\" required"));
2792
2793 /*
2794 * if "cs" defined, use a scroll region, it's faster.
2795 */
2796 if (*T_CS != NUL)
2797 scroll_region = TRUE;
2798 else
2799 scroll_region = FALSE;
2800
2801 if (pairs)
2802 {
2803 /*
2804 * optional pairs
2805 */
2806 /* TP goes to normal mode for TI (invert) and TB (bold) */
2807 if (*T_ME == NUL)
2808 T_ME = T_MR = T_MD = T_MB = empty_option;
2809 if (*T_SO == NUL || *T_SE == NUL)
2810 T_SO = T_SE = empty_option;
2811 if (*T_US == NUL || *T_UE == NUL)
2812 T_US = T_UE = empty_option;
2813 if (*T_CZH == NUL || *T_CZR == NUL)
2814 T_CZH = T_CZR = empty_option;
2815
2816 /* T_VE is needed even though T_VI is not defined */
2817 if (*T_VE == NUL)
2818 T_VI = empty_option;
2819
2820 /* if 'mr' or 'me' is not defined use 'so' and 'se' */
2821 if (*T_ME == NUL)
2822 {
2823 T_ME = T_SE;
2824 T_MR = T_SO;
2825 T_MD = T_SO;
2826 }
2827
2828 /* if 'so' or 'se' is not defined use 'mr' and 'me' */
2829 if (*T_SO == NUL)
2830 {
2831 T_SE = T_ME;
2832 if (*T_MR == NUL)
2833 T_SO = T_MD;
2834 else
2835 T_SO = T_MR;
2836 }
2837
2838 /* if 'ZH' or 'ZR' is not defined use 'mr' and 'me' */
2839 if (*T_CZH == NUL)
2840 {
2841 T_CZR = T_ME;
2842 if (*T_MR == NUL)
2843 T_CZH = T_MD;
2844 else
2845 T_CZH = T_MR;
2846 }
2847
2848 /* "Sb" and "Sf" come in pairs */
2849 if (*T_CSB == NUL || *T_CSF == NUL)
2850 {
2851 T_CSB = empty_option;
2852 T_CSF = empty_option;
2853 }
2854
2855 /* "AB" and "AF" come in pairs */
2856 if (*T_CAB == NUL || *T_CAF == NUL)
2857 {
2858 T_CAB = empty_option;
2859 T_CAF = empty_option;
2860 }
2861
2862 /* if 'Sb' and 'AB' are not defined, reset "Co" */
2863 if (*T_CSB == NUL && *T_CAB == NUL)
Bram Moolenaar363cb672009-07-22 12:28:17 +00002864 free_one_termoption(T_CCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
2866 /* Set 'weirdinvert' according to value of 't_xs' */
2867 p_wiv = (*T_XS != NUL);
2868 }
2869 need_gather = TRUE;
2870
2871 /* Set t_colors to the value of t_Co. */
2872 t_colors = atoi((char *)T_CCO);
2873}
2874
2875#if (defined(FEAT_GUI) && (defined(FEAT_MENU) || !defined(USE_ON_FLY_SCROLL))) \
2876 || defined(PROTO)
2877/*
2878 * Represent the given long_u as individual bytes, with the most significant
2879 * byte first, and store them in dst.
2880 */
2881 void
2882add_long_to_buf(val, dst)
2883 long_u val;
2884 char_u *dst;
2885{
2886 int i;
2887 int shift;
2888
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002889 for (i = 1; i <= (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 {
2891 shift = 8 * (sizeof(long_u) - i);
2892 dst[i - 1] = (char_u) ((val >> shift) & 0xff);
2893 }
2894}
2895
2896static int get_long_from_buf __ARGS((char_u *buf, long_u *val));
2897
2898/*
2899 * Interpret the next string of bytes in buf as a long integer, with the most
2900 * significant byte first. Note that it is assumed that buf has been through
2901 * inchar(), so that NUL and K_SPECIAL will be represented as three bytes each.
2902 * Puts result in val, and returns the number of bytes read from buf
2903 * (between sizeof(long_u) and 2 * sizeof(long_u)), or -1 if not enough bytes
2904 * were present.
2905 */
2906 static int
2907get_long_from_buf(buf, val)
2908 char_u *buf;
2909 long_u *val;
2910{
2911 int len;
2912 char_u bytes[sizeof(long_u)];
2913 int i;
2914 int shift;
2915
2916 *val = 0;
2917 len = get_bytes_from_buf(buf, bytes, (int)sizeof(long_u));
2918 if (len != -1)
2919 {
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002920 for (i = 0; i < (int)sizeof(long_u); i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
2922 shift = 8 * (sizeof(long_u) - 1 - i);
2923 *val += (long_u)bytes[i] << shift;
2924 }
2925 }
2926 return len;
2927}
2928#endif
2929
2930#if defined(FEAT_GUI) \
Bram Moolenaar864207d2008-06-24 22:14:38 +00002931 || (defined(FEAT_MOUSE) && (!defined(UNIX) || defined(FEAT_MOUSE_XTERM) \
2932 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933/*
2934 * Read the next num_bytes bytes from buf, and store them in bytes. Assume
2935 * that buf has been through inchar(). Returns the actual number of bytes used
2936 * from buf (between num_bytes and num_bytes*2), or -1 if not enough bytes were
2937 * available.
2938 */
2939 static int
2940get_bytes_from_buf(buf, bytes, num_bytes)
2941 char_u *buf;
2942 char_u *bytes;
2943 int num_bytes;
2944{
2945 int len = 0;
2946 int i;
2947 char_u c;
2948
2949 for (i = 0; i < num_bytes; i++)
2950 {
2951 if ((c = buf[len++]) == NUL)
2952 return -1;
2953 if (c == K_SPECIAL)
2954 {
2955 if (buf[len] == NUL || buf[len + 1] == NUL) /* cannot happen? */
2956 return -1;
2957 if (buf[len++] == (int)KS_ZERO)
2958 c = NUL;
Bram Moolenaar0e710d62013-07-01 20:06:19 +02002959 /* else it should be KS_SPECIAL; when followed by KE_FILLER c is
2960 * K_SPECIAL, or followed by KE_CSI and c must be CSI. */
2961 if (buf[len++] == (int)KE_CSI)
2962 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
Bram Moolenaar8d1ab512007-05-06 13:49:21 +00002964 else if (c == CSI && buf[len] == KS_EXTRA
2965 && buf[len + 1] == (int)KE_CSI)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002966 /* CSI is stored as CSI KS_SPECIAL KE_CSI to avoid confusion with
2967 * the start of a special key, see add_to_input_buf_csi(). */
2968 len += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 bytes[i] = c;
2970 }
2971 return len;
2972}
2973#endif
2974
2975/*
Bram Moolenaare057d402013-06-30 17:51:51 +02002976 * Check if the new shell size is valid, correct it if it's too small or way
2977 * too big.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 */
2979 void
2980check_shellsize()
2981{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 if (Rows < min_rows()) /* need room for one window and command line */
2983 Rows = min_rows();
Bram Moolenaare057d402013-06-30 17:51:51 +02002984 limit_screen_size();
2985}
2986
2987/*
2988 * Limit Rows and Columns to avoid an overflow in Rows * Columns.
2989 */
2990 void
2991limit_screen_size()
2992{
2993 if (Columns < MIN_COLUMNS)
2994 Columns = MIN_COLUMNS;
2995 else if (Columns > 10000)
2996 Columns = 10000;
2997 if (Rows > 1000)
2998 Rows = 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999}
3000
Bram Moolenaar86b68352004-12-27 21:59:20 +00003001/*
3002 * Invoked just before the screen structures are going to be (re)allocated.
3003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 void
3005win_new_shellsize()
3006{
3007 static int old_Rows = 0;
3008 static int old_Columns = 0;
3009
3010 if (old_Rows != Rows || old_Columns != Columns)
3011 ui_new_shellsize();
3012 if (old_Rows != Rows)
3013 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003014 /* if 'window' uses the whole screen, keep it using that */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003015 if (p_window == old_Rows - 1 || old_Rows == 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003016 p_window = Rows - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 old_Rows = Rows;
3018 shell_new_rows(); /* update window sizes */
3019 }
3020 if (old_Columns != Columns)
3021 {
3022 old_Columns = Columns;
3023#ifdef FEAT_VERTSPLIT
3024 shell_new_columns(); /* update window sizes */
3025#endif
3026 }
3027}
3028
3029/*
3030 * Call this function when the Vim shell has been resized in any way.
3031 * Will obtain the current size and redraw (also when size didn't change).
3032 */
3033 void
3034shell_resized()
3035{
3036 set_shellsize(0, 0, FALSE);
3037}
3038
3039/*
3040 * Check if the shell size changed. Handle a resize.
3041 * When the size didn't change, nothing happens.
3042 */
3043 void
3044shell_resized_check()
3045{
3046 int old_Rows = Rows;
3047 int old_Columns = Columns;
3048
Bram Moolenaar3633dc02012-08-29 16:26:04 +02003049 if (!exiting
3050#ifdef FEAT_GUI
3051 /* Do not get the size when executing a shell command during
3052 * startup. */
3053 && !gui.starting
3054#endif
3055 )
Bram Moolenaar99808352010-12-30 14:47:36 +01003056 {
3057 (void)ui_get_shellsize();
3058 check_shellsize();
3059 if (old_Rows != Rows || old_Columns != Columns)
3060 shell_resized();
3061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062}
3063
3064/*
3065 * Set size of the Vim shell.
3066 * If 'mustset' is TRUE, we must set Rows and Columns, do not get the real
3067 * window size (this is used for the :win command).
3068 * If 'mustset' is FALSE, we may try to get the real window size and if
3069 * it fails use 'width' and 'height'.
3070 */
3071 void
3072set_shellsize(width, height, mustset)
3073 int width, height;
3074 int mustset;
3075{
3076 static int busy = FALSE;
3077
3078 /*
3079 * Avoid recursiveness, can happen when setting the window size causes
3080 * another window-changed signal.
3081 */
3082 if (busy)
3083 return;
3084
3085 if (width < 0 || height < 0) /* just checking... */
3086 return;
3087
Bram Moolenaara971b822011-09-14 14:43:25 +02003088 if (State == HITRETURN || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 {
Bram Moolenaara971b822011-09-14 14:43:25 +02003090 /* postpone the resizing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 State = SETWSIZE;
3092 return;
3093 }
3094
Bram Moolenaara971b822011-09-14 14:43:25 +02003095 /* curwin->w_buffer can be NULL when we are closing a window and the
3096 * buffer has already been closed and removing a scrollbar causes a resize
3097 * event. Don't resize then, it will happen after entering another buffer.
3098 */
3099 if (curwin->w_buffer == NULL)
3100 return;
3101
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 ++busy;
3103
3104#ifdef AMIGA
3105 out_flush(); /* must do this before mch_get_shellsize() for
3106 some obscure reason */
3107#endif
3108
3109 if (mustset || (ui_get_shellsize() == FAIL && height != 0))
3110 {
3111 Rows = height;
3112 Columns = width;
3113 check_shellsize();
3114 ui_set_shellsize(mustset);
3115 }
3116 else
3117 check_shellsize();
3118
3119 /* The window layout used to be adjusted here, but it now happens in
3120 * screenalloc() (also invoked from screenclear()). That is because the
3121 * "busy" check above may skip this, but not screenalloc(). */
3122
3123 if (State != ASKMORE && State != EXTERNCMD && State != CONFIRM)
3124 screenclear();
3125 else
3126 screen_start(); /* don't know where cursor is now */
3127
3128 if (starting != NO_SCREEN)
3129 {
3130#ifdef FEAT_TITLE
3131 maketitle();
3132#endif
3133 changed_line_abv_curs();
3134 invalidate_botline();
3135
3136 /*
3137 * We only redraw when it's needed:
3138 * - While at the more prompt or executing an external command, don't
3139 * redraw, but position the cursor.
3140 * - While editing the command line, only redraw that.
3141 * - in Ex mode, don't redraw anything.
3142 * - Otherwise, redraw right now, and position the cursor.
3143 * Always need to call update_screen() or screenalloc(), to make
3144 * sure Rows/Columns and the size of ScreenLines[] is correct!
3145 */
3146 if (State == ASKMORE || State == EXTERNCMD || State == CONFIRM
3147 || exmode_active)
3148 {
3149 screenalloc(FALSE);
3150 repeat_message();
3151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 else
3153 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003154#ifdef FEAT_SCROLLBIND
3155 if (curwin->w_p_scb)
3156 do_check_scrollbind(TRUE);
3157#endif
3158 if (State & CMDLINE)
Bram Moolenaar280f1262006-01-30 00:14:18 +00003159 {
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003160 update_screen(NOT_VALID);
3161 redrawcmdline();
Bram Moolenaar280f1262006-01-30 00:14:18 +00003162 }
3163 else
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003164 {
3165 update_topline();
3166#if defined(FEAT_INS_EXPAND)
3167 if (pum_visible())
3168 {
3169 redraw_later(NOT_VALID);
3170 ins_compl_show_pum(); /* This includes the redraw. */
3171 }
3172 else
Bram Moolenaar280f1262006-01-30 00:14:18 +00003173#endif
Bram Moolenaar09ef47a2006-10-24 19:36:02 +00003174 update_screen(NOT_VALID);
3175 if (redrawing())
3176 setcursor();
3177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 }
3179 cursor_on(); /* redrawing may have switched it off */
3180 }
3181 out_flush();
3182 --busy;
3183}
3184
3185/*
3186 * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
3187 * commands and Ex mode).
3188 */
3189 void
3190settmode(tmode)
3191 int tmode;
3192{
3193#ifdef FEAT_GUI
3194 /* don't set the term where gvim was started to any mode */
3195 if (gui.in_use)
3196 return;
3197#endif
3198
3199 if (full_screen)
3200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 /*
3202 * When returning after calling a shell we want to really set the
3203 * terminal to raw mode, even though we think it already is, because
3204 * the shell program may have reset the terminal mode.
3205 * When we think the terminal is normal, don't try to set it to
3206 * normal again, because that causes problems (logout!) on some
3207 * machines.
3208 */
3209 if (tmode != TMODE_COOK || cur_tmode != TMODE_COOK)
3210 {
3211#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003212# ifdef FEAT_GUI
3213 if (!gui.in_use && !gui.starting)
3214# endif
3215 {
3216 /* May need to check for T_CRV response and termcodes, it
3217 * doesn't work in Cooked mode, an external program may get
3218 * them. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003219 if (tmode != TMODE_RAW && (crv_status == CRV_SENT
3220 || u7_status == U7_SENT))
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003221 (void)vpeekc_nomap();
3222 check_for_codes_from_term();
3223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224#endif
3225#ifdef FEAT_MOUSE_TTY
3226 if (tmode != TMODE_RAW)
3227 mch_setmouse(FALSE); /* switch mouse off */
3228#endif
3229 out_flush();
3230 mch_settmode(tmode); /* machine specific function */
3231 cur_tmode = tmode;
3232#ifdef FEAT_MOUSE
3233 if (tmode == TMODE_RAW)
3234 setmouse(); /* may switch mouse on */
3235#endif
3236 out_flush();
3237 }
3238#ifdef FEAT_TERMRESPONSE
3239 may_req_termresponse();
3240#endif
3241 }
3242}
3243
3244 void
3245starttermcap()
3246{
3247 if (full_screen && !termcap_active)
3248 {
3249 out_str(T_TI); /* start termcap mode */
3250 out_str(T_KS); /* start "keypad transmit" mode */
3251 out_flush();
3252 termcap_active = TRUE;
3253 screen_start(); /* don't know where cursor is now */
3254#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003255# ifdef FEAT_GUI
3256 if (!gui.in_use && !gui.starting)
3257# endif
3258 {
3259 may_req_termresponse();
3260 /* Immediately check for a response. If t_Co changes, we don't
3261 * want to redraw with wrong colors first. */
3262 if (crv_status != CRV_GET)
3263 check_for_codes_from_term();
3264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265#endif
3266 }
3267}
3268
3269 void
3270stoptermcap()
3271{
3272 screen_stop_highlight();
3273 reset_cterm_colors();
3274 if (termcap_active)
3275 {
3276#ifdef FEAT_TERMRESPONSE
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003277# ifdef FEAT_GUI
3278 if (!gui.in_use && !gui.starting)
3279# endif
3280 {
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003281 /* May need to discard T_CRV or T_U7 response. */
Bram Moolenaar9584b312013-03-13 19:29:28 +01003282 if (crv_status == CRV_SENT || u7_status == U7_SENT)
Bram Moolenaar29607ac2013-05-13 20:26:53 +02003283 {
3284# ifdef UNIX
3285 /* Give the terminal a chance to respond. */
3286 mch_delay(100L, FALSE);
3287# endif
3288# ifdef TCIFLUSH
3289 /* Discard data received but not read. */
3290 if (exiting)
3291 tcflush(fileno(stdin), TCIFLUSH);
3292# endif
3293 }
Bram Moolenaar2bf6a2d2008-07-29 10:22:12 +00003294 /* Check for termcodes first, otherwise an external program may
3295 * get them. */
3296 check_for_codes_from_term();
3297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298#endif
3299 out_str(T_KE); /* stop "keypad transmit" mode */
3300 out_flush();
3301 termcap_active = FALSE;
3302 cursor_on(); /* just in case it is still off */
3303 out_str(T_TE); /* stop termcap mode */
3304 screen_start(); /* don't know where cursor is now */
3305 out_flush();
3306 }
3307}
3308
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003309#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310/*
3311 * Request version string (for xterm) when needed.
3312 * Only do this after switching to raw mode, otherwise the result will be
3313 * echoed.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003314 * Only do this after startup has finished, to avoid that the response comes
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00003315 * while executing "-c !cmd" or even after "-c quit".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 * Only do this after termcap mode has been started, otherwise the codes for
3317 * the cursor keys may be wrong.
Bram Moolenaarebefac62005-12-28 22:39:57 +00003318 * Only do this when 'esckeys' is on, otherwise the response causes trouble in
3319 * Insert mode.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003320 * On Unix only do it when both output and input are a tty (avoid writing
3321 * request to terminal while reading from a file).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 * The result is caught in check_termcode().
3323 */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003324 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325may_req_termresponse()
3326{
3327 if (crv_status == CRV_GET
3328 && cur_tmode == TMODE_RAW
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003329 && starting == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 && termcap_active
Bram Moolenaarebefac62005-12-28 22:39:57 +00003331 && p_ek
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003332# ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 && isatty(1)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003334 && isatty(read_cmd_fd)
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 && *T_CRV != NUL)
3337 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02003338 LOG_TR("Sending CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 out_str(T_CRV);
3340 crv_status = CRV_SENT;
3341 /* check for the characters now, otherwise they might be eaten by
3342 * get_keystroke() */
3343 out_flush();
3344 (void)vpeekc_nomap();
3345 }
3346}
Bram Moolenaar9584b312013-03-13 19:29:28 +01003347
3348# if defined(FEAT_MBYTE) || defined(PROTO)
3349/*
3350 * Check how the terminal treats ambiguous character width (UAX #11).
Bram Moolenaar2951b772013-07-03 12:45:31 +02003351 * First, we move the cursor to (1, 0) and print a test ambiguous character
Bram Moolenaar9584b312013-03-13 19:29:28 +01003352 * \u25bd (WHITE DOWN-POINTING TRIANGLE) and query current cursor position.
Bram Moolenaar2951b772013-07-03 12:45:31 +02003353 * If the terminal treats \u25bd as single width, the position is (1, 1),
3354 * or if it is treated as double width, that will be (1, 2).
Bram Moolenaar9584b312013-03-13 19:29:28 +01003355 * This function has the side effect that changes cursor position, so
3356 * it must be called immediately after entering termcap mode.
3357 */
3358 void
Bram Moolenaar386dcde2013-09-29 16:27:47 +02003359may_req_ambiguous_char_width()
Bram Moolenaar9584b312013-03-13 19:29:28 +01003360{
3361 if (u7_status == U7_GET
3362 && cur_tmode == TMODE_RAW
3363 && termcap_active
3364 && p_ek
3365# ifdef UNIX
3366 && isatty(1)
3367 && isatty(read_cmd_fd)
3368# endif
3369 && *T_U7 != NUL
3370 && !option_was_set((char_u *)"ambiwidth"))
3371 {
3372 char_u buf[16];
3373
Bram Moolenaar2951b772013-07-03 12:45:31 +02003374 LOG_TR("Sending U7 request");
3375 /* Do this in the second row. In the first row the returned sequence
3376 * may be CSI 1;2R, which is the same as <S-F3>. */
3377 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003378 buf[mb_char2bytes(0x25bd, buf)] = 0;
3379 out_str(buf);
3380 out_str(T_U7);
3381 u7_status = U7_SENT;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01003382 out_flush();
3383 term_windgoto(1, 0);
Bram Moolenaar9584b312013-03-13 19:29:28 +01003384 out_str((char_u *)" ");
3385 term_windgoto(0, 0);
3386 /* check for the characters now, otherwise they might be eaten by
3387 * get_keystroke() */
3388 out_flush();
3389 (void)vpeekc_nomap();
3390 }
3391}
3392# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02003393
3394# ifdef DEBUG_TERMRESPONSE
3395 static void
3396log_tr(char *msg)
3397{
3398 static FILE *fd_tr = NULL;
3399 static proftime_T start;
3400 proftime_T now;
3401
3402 if (fd_tr == NULL)
3403 {
3404 fd_tr = fopen("termresponse.log", "w");
3405 profile_start(&start);
3406 }
3407 now = start;
3408 profile_end(&now);
3409 fprintf(fd_tr, "%s: %s %s\n",
3410 profile_msg(&now),
3411 must_redraw == NOT_VALID ? "NV"
3412 : must_redraw == CLEAR ? "CL" : " ",
3413 msg);
3414}
3415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#endif
3417
3418/*
3419 * Return TRUE when saving and restoring the screen.
3420 */
3421 int
3422swapping_screen()
3423{
3424 return (full_screen && *T_TI != NUL);
3425}
3426
3427#ifdef FEAT_MOUSE
3428/*
3429 * setmouse() - switch mouse on/off depending on current mode and 'mouse'
3430 */
3431 void
3432setmouse()
3433{
3434# ifdef FEAT_MOUSE_TTY
3435 int checkfor;
3436# endif
3437
3438# ifdef FEAT_MOUSESHAPE
3439 update_mouseshape(-1);
3440# endif
3441
3442# ifdef FEAT_MOUSE_TTY /* Should be outside proc, but may break MOUSESHAPE */
3443# ifdef FEAT_GUI
3444 /* In the GUI the mouse is always enabled. */
3445 if (gui.in_use)
3446 return;
3447# endif
3448 /* be quick when mouse is off */
3449 if (*p_mouse == NUL || has_mouse_termcode == 0)
3450 return;
3451
3452 /* don't switch mouse on when not in raw mode (Ex mode) */
3453 if (cur_tmode != TMODE_RAW)
3454 {
3455 mch_setmouse(FALSE);
3456 return;
3457 }
3458
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 if (VIsual_active)
3460 checkfor = MOUSE_VISUAL;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003461 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 checkfor = MOUSE_RETURN;
3463 else if (State & INSERT)
3464 checkfor = MOUSE_INSERT;
3465 else if (State & CMDLINE)
3466 checkfor = MOUSE_COMMAND;
3467 else if (State == CONFIRM || State == EXTERNCMD)
3468 checkfor = ' '; /* don't use mouse for ":confirm" or ":!cmd" */
3469 else
3470 checkfor = MOUSE_NORMAL; /* assume normal mode */
3471
3472 if (mouse_has(checkfor))
3473 mch_setmouse(TRUE);
3474 else
3475 mch_setmouse(FALSE);
3476# endif
3477}
3478
3479/*
3480 * Return TRUE if
3481 * - "c" is in 'mouse', or
3482 * - 'a' is in 'mouse' and "c" is in MOUSE_A, or
3483 * - the current buffer is a help file and 'h' is in 'mouse' and we are in a
3484 * normal editing mode (not at hit-return message).
3485 */
3486 int
3487mouse_has(c)
3488 int c;
3489{
3490 char_u *p;
3491
3492 for (p = p_mouse; *p; ++p)
3493 switch (*p)
3494 {
3495 case 'a': if (vim_strchr((char_u *)MOUSE_A, c) != NULL)
3496 return TRUE;
3497 break;
3498 case MOUSE_HELP: if (c != MOUSE_RETURN && curbuf->b_help)
3499 return TRUE;
3500 break;
3501 default: if (c == *p) return TRUE; break;
3502 }
3503 return FALSE;
3504}
3505
3506/*
3507 * Return TRUE when 'mousemodel' is set to "popup" or "popup_setpos".
3508 */
3509 int
3510mouse_model_popup()
3511{
3512 return (p_mousem[0] == 'p');
3513}
3514#endif
3515
3516/*
3517 * By outputting the 'cursor very visible' termcap code, for some windowed
3518 * terminals this makes the screen scrolled to the correct position.
3519 * Used when starting Vim or returning from a shell.
3520 */
3521 void
3522scroll_start()
3523{
3524 if (*T_VS != NUL)
3525 {
3526 out_str(T_VS);
3527 out_str(T_VE);
3528 screen_start(); /* don't know where cursor is now */
3529 }
3530}
3531
3532static int cursor_is_off = FALSE;
3533
3534/*
3535 * Enable the cursor.
3536 */
3537 void
3538cursor_on()
3539{
3540 if (cursor_is_off)
3541 {
3542 out_str(T_VE);
3543 cursor_is_off = FALSE;
3544 }
3545}
3546
3547/*
3548 * Disable the cursor.
3549 */
3550 void
3551cursor_off()
3552{
3553 if (full_screen)
3554 {
3555 if (!cursor_is_off)
3556 out_str(T_VI); /* disable cursor */
3557 cursor_is_off = TRUE;
3558 }
3559}
3560
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003561#if defined(CURSOR_SHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562/*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003563 * Set cursor shape to match Insert mode.
3564 */
3565 void
3566term_cursor_shape()
3567{
3568 static int showing_insert_mode = MAYBE;
3569
3570 if (!full_screen || *T_CSI == NUL || *T_CEI == NUL)
3571 return;
3572
3573 if (State & INSERT)
3574 {
3575 if (showing_insert_mode != TRUE)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003576 out_str(T_CSI); /* Insert mode cursor */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003577 showing_insert_mode = TRUE;
3578 }
3579 else
3580 {
3581 if (showing_insert_mode != FALSE)
Bram Moolenaar17c7c012006-01-26 22:25:15 +00003582 out_str(T_CEI); /* non-Insert mode cursor */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003583 showing_insert_mode = FALSE;
3584 }
3585}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003586#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003587
3588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 * Set scrolling region for window 'wp'.
3590 * The region starts 'off' lines from the start of the window.
3591 * Also set the vertical scroll region for a vertically split window. Always
3592 * the full width of the window, excluding the vertical separator.
3593 */
3594 void
3595scroll_region_set(wp, off)
3596 win_T *wp;
3597 int off;
3598{
3599 OUT_STR(tgoto((char *)T_CS, W_WINROW(wp) + wp->w_height - 1,
3600 W_WINROW(wp) + off));
3601#ifdef FEAT_VERTSPLIT
3602 if (*T_CSV != NUL && wp->w_width != Columns)
3603 OUT_STR(tgoto((char *)T_CSV, W_WINCOL(wp) + wp->w_width - 1,
3604 W_WINCOL(wp)));
3605#endif
3606 screen_start(); /* don't know where cursor is now */
3607}
3608
3609/*
3610 * Reset scrolling region to the whole screen.
3611 */
3612 void
3613scroll_region_reset()
3614{
3615 OUT_STR(tgoto((char *)T_CS, (int)Rows - 1, 0));
3616#ifdef FEAT_VERTSPLIT
3617 if (*T_CSV != NUL)
3618 OUT_STR(tgoto((char *)T_CSV, (int)Columns - 1, 0));
3619#endif
3620 screen_start(); /* don't know where cursor is now */
3621}
3622
3623
3624/*
3625 * List of terminal codes that are currently recognized.
3626 */
3627
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00003628static struct termcode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629{
3630 char_u name[2]; /* termcap name of entry */
3631 char_u *code; /* terminal code (in allocated memory) */
3632 int len; /* STRLEN(code) */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003633 int modlen; /* length of part before ";*~". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634} *termcodes = NULL;
3635
3636static int tc_max_len = 0; /* number of entries that termcodes[] can hold */
3637static int tc_len = 0; /* current number of entries in termcodes[] */
3638
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003639static int termcode_star __ARGS((char_u *code, int len));
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 void
3642clear_termcodes()
3643{
3644 while (tc_len > 0)
3645 vim_free(termcodes[--tc_len].code);
3646 vim_free(termcodes);
3647 termcodes = NULL;
3648 tc_max_len = 0;
3649
3650#ifdef HAVE_TGETENT
3651 BC = (char *)empty_option;
3652 UP = (char *)empty_option;
3653 PC = NUL; /* set pad character to NUL */
3654 ospeed = 0;
3655#endif
3656
3657 need_gather = TRUE; /* need to fill termleader[] */
3658}
3659
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003660#define ATC_FROM_TERM 55
3661
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662/*
3663 * Add a new entry to the list of terminal codes.
3664 * The list is kept alphabetical for ":set termcap"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003665 * "flags" is TRUE when replacing 7-bit by 8-bit controls is desired.
3666 * "flags" can also be ATC_FROM_TERM for got_code_from_term().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 */
3668 void
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003669add_termcode(name, string, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 char_u *name;
3671 char_u *string;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003672 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
3674 struct termcode *new_tc;
3675 int i, j;
3676 char_u *s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003677 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
3679 if (string == NULL || *string == NUL)
3680 {
3681 del_termcode(name);
3682 return;
3683 }
3684
3685 s = vim_strsave(string);
3686 if (s == NULL)
3687 return;
3688
3689 /* Change leading <Esc>[ to CSI, change <Esc>O to <M-O>. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003690 if (flags != 0 && flags != ATC_FROM_TERM && term_7to8bit(string) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003692 STRMOVE(s, s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 s[0] = term_7to8bit(string);
3694 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003695 len = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696
3697 need_gather = TRUE; /* need to fill termleader[] */
3698
3699 /*
3700 * need to make space for more entries
3701 */
3702 if (tc_len == tc_max_len)
3703 {
3704 tc_max_len += 20;
3705 new_tc = (struct termcode *)alloc(
3706 (unsigned)(tc_max_len * sizeof(struct termcode)));
3707 if (new_tc == NULL)
3708 {
3709 tc_max_len -= 20;
3710 return;
3711 }
3712 for (i = 0; i < tc_len; ++i)
3713 new_tc[i] = termcodes[i];
3714 vim_free(termcodes);
3715 termcodes = new_tc;
3716 }
3717
3718 /*
3719 * Look for existing entry with the same name, it is replaced.
3720 * Look for an existing entry that is alphabetical higher, the new entry
3721 * is inserted in front of it.
3722 */
3723 for (i = 0; i < tc_len; ++i)
3724 {
3725 if (termcodes[i].name[0] < name[0])
3726 continue;
3727 if (termcodes[i].name[0] == name[0])
3728 {
3729 if (termcodes[i].name[1] < name[1])
3730 continue;
3731 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003732 * Exact match: May replace old code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 */
3734 if (termcodes[i].name[1] == name[1])
3735 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003736 if (flags == ATC_FROM_TERM && (j = termcode_star(
3737 termcodes[i].code, termcodes[i].len)) > 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003738 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003739 /* Don't replace ESC[123;*X or ESC O*X with another when
3740 * invoked from got_code_from_term(). */
3741 if (len == termcodes[i].len - j
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003742 && STRNCMP(s, termcodes[i].code, len - 1) == 0
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003743 && s[len - 1]
3744 == termcodes[i].code[termcodes[i].len - 1])
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003745 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003746 /* They are equal but for the ";*": don't add it. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003747 vim_free(s);
3748 return;
3749 }
3750 }
3751 else
3752 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003753 /* Replace old code. */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003754 vim_free(termcodes[i].code);
3755 --tc_len;
3756 break;
3757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 }
3759 }
3760 /*
3761 * Found alphabetical larger entry, move rest to insert new entry
3762 */
3763 for (j = tc_len; j > i; --j)
3764 termcodes[j] = termcodes[j - 1];
3765 break;
3766 }
3767
3768 termcodes[i].name[0] = name[0];
3769 termcodes[i].name[1] = name[1];
3770 termcodes[i].code = s;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003771 termcodes[i].len = len;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003772
3773 /* For xterm we recognize special codes like "ESC[42;*X" and "ESC O*X" that
3774 * accept modifiers. */
3775 termcodes[i].modlen = 0;
3776 j = termcode_star(s, len);
3777 if (j > 0)
3778 termcodes[i].modlen = len - 1 - j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 ++tc_len;
3780}
3781
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003782/*
3783 * Check termcode "code[len]" for ending in ;*X, <Esc>O*X or <M-O>*X.
3784 * The "X" can be any character.
3785 * Return 0 if not found, 2 for ;*X and 1 for O*X and <M-O>*X.
3786 */
3787 static int
3788termcode_star(code, len)
3789 char_u *code;
3790 int len;
3791{
3792 /* Shortest is <M-O>*X. With ; shortest is <CSI>1;*X */
3793 if (len >= 3 && code[len - 2] == '*')
3794 {
3795 if (len >= 5 && code[len - 3] == ';')
3796 return 2;
3797 if ((len >= 4 && code[len - 3] == 'O') || code[len - 3] == 'O' + 128)
3798 return 1;
3799 }
3800 return 0;
3801}
3802
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 char_u *
3804find_termcode(name)
3805 char_u *name;
3806{
3807 int i;
3808
3809 for (i = 0; i < tc_len; ++i)
3810 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3811 return termcodes[i].code;
3812 return NULL;
3813}
3814
3815#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3816 char_u *
3817get_termcode(i)
3818 int i;
3819{
3820 if (i >= tc_len)
3821 return NULL;
3822 return &termcodes[i].name[0];
3823}
3824#endif
3825
3826 void
3827del_termcode(name)
3828 char_u *name;
3829{
3830 int i;
3831
3832 if (termcodes == NULL) /* nothing there yet */
3833 return;
3834
3835 need_gather = TRUE; /* need to fill termleader[] */
3836
3837 for (i = 0; i < tc_len; ++i)
3838 if (termcodes[i].name[0] == name[0] && termcodes[i].name[1] == name[1])
3839 {
3840 del_termcode_idx(i);
3841 return;
3842 }
3843 /* not found. Give error message? */
3844}
3845
3846 static void
3847del_termcode_idx(idx)
3848 int idx;
3849{
3850 int i;
3851
3852 vim_free(termcodes[idx].code);
3853 --tc_len;
3854 for (i = idx; i < tc_len; ++i)
3855 termcodes[i] = termcodes[i + 1];
3856}
3857
3858#ifdef FEAT_TERMRESPONSE
3859/*
3860 * Called when detected that the terminal sends 8-bit codes.
3861 * Convert all 7-bit codes to their 8-bit equivalent.
3862 */
3863 static void
3864switch_to_8bit()
3865{
3866 int i;
3867 int c;
3868
3869 /* Only need to do something when not already using 8-bit codes. */
3870 if (!term_is_8bit(T_NAME))
3871 {
3872 for (i = 0; i < tc_len; ++i)
3873 {
3874 c = term_7to8bit(termcodes[i].code);
3875 if (c != 0)
3876 {
Bram Moolenaar864207d2008-06-24 22:14:38 +00003877 STRMOVE(termcodes[i].code + 1, termcodes[i].code + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 termcodes[i].code[0] = c;
3879 }
3880 }
3881 need_gather = TRUE; /* need to fill termleader[] */
3882 }
3883 detected_8bit = TRUE;
Bram Moolenaar2951b772013-07-03 12:45:31 +02003884 LOG_TR("Switching to 8 bit");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885}
3886#endif
3887
3888#ifdef CHECK_DOUBLE_CLICK
3889static linenr_T orig_topline = 0;
3890# ifdef FEAT_DIFF
3891static int orig_topfill = 0;
3892# endif
3893#endif
3894#if (defined(FEAT_WINDOWS) && defined(CHECK_DOUBLE_CLICK)) || defined(PROTO)
3895/*
3896 * Checking for double clicks ourselves.
3897 * "orig_topline" is used to avoid detecting a double-click when the window
3898 * contents scrolled (e.g., when 'scrolloff' is non-zero).
3899 */
3900/*
3901 * Set orig_topline. Used when jumping to another window, so that a double
3902 * click still works.
3903 */
3904 void
3905set_mouse_topline(wp)
3906 win_T *wp;
3907{
3908 orig_topline = wp->w_topline;
3909# ifdef FEAT_DIFF
3910 orig_topfill = wp->w_topfill;
3911# endif
3912}
3913#endif
3914
3915/*
3916 * Check if typebuf.tb_buf[] contains a terminal key code.
3917 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
3918 * + max_offset].
3919 * Return 0 for no match, -1 for partial match, > 0 for full match.
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003920 * Return KEYLEN_REMOVED when a key code was deleted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 * With a match, the match is removed, the replacement code is inserted in
3922 * typebuf.tb_buf[] and the number of characters in typebuf.tb_buf[] is
3923 * returned.
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003924 * When "buf" is not NULL, buf[bufsize] is used instead of typebuf.tb_buf[].
3925 * "buflen" is then the length of the string in buf[] and is updated for
3926 * inserts and deletes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 */
3928 int
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003929check_termcode(max_offset, buf, bufsize, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 int max_offset;
3931 char_u *buf;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003932 int bufsize;
3933 int *buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934{
3935 char_u *tp;
3936 char_u *p;
3937 int slen = 0; /* init for GCC */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003938 int modslen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 int len;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003940 int retval = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 int offset;
3942 char_u key_name[2];
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003943 int modifiers;
3944 int key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 int new_slen;
3946 int extra;
3947 char_u string[MAX_KEY_CODE_LEN + 1];
3948 int i, j;
3949 int idx = 0;
3950#ifdef FEAT_MOUSE
Bram Moolenaar864207d2008-06-24 22:14:38 +00003951# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
3952 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 char_u bytes[6];
3954 int num_bytes;
3955# endif
3956 int mouse_code = 0; /* init for GCC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 int is_click, is_drag;
3958 int wheel_code = 0;
3959 int current_button;
3960 static int held_button = MOUSE_RELEASE;
3961 static int orig_num_clicks = 1;
3962 static int orig_mouse_code = 0x0;
3963# ifdef CHECK_DOUBLE_CLICK
3964 static int orig_mouse_col = 0;
3965 static int orig_mouse_row = 0;
3966 static struct timeval orig_mouse_time = {0, 0};
3967 /* time of previous mouse click */
3968 struct timeval mouse_time; /* time of current mouse click */
3969 long timediff; /* elapsed time in msec */
3970# endif
3971#endif
3972 int cpo_koffset;
3973#ifdef FEAT_MOUSE_GPM
3974 extern int gpm_flag; /* gpm library variable */
3975#endif
3976
3977 cpo_koffset = (vim_strchr(p_cpo, CPO_KOFFSET) != NULL);
3978
3979 /*
3980 * Speed up the checks for terminal codes by gathering all first bytes
3981 * used in termleader[]. Often this is just a single <Esc>.
3982 */
3983 if (need_gather)
3984 gather_termleader();
3985
3986 /*
3987 * Check at several positions in typebuf.tb_buf[], to catch something like
3988 * "x<Up>" that can be mapped. Stop at max_offset, because characters
3989 * after that cannot be used for mapping, and with @r commands
Bram Moolenaare533bbe2013-03-16 14:33:36 +01003990 * typebuf.tb_buf[] can become very long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 * This is used often, KEEP IT FAST!
3992 */
3993 for (offset = 0; offset < max_offset; ++offset)
3994 {
3995 if (buf == NULL)
3996 {
3997 if (offset >= typebuf.tb_len)
3998 break;
3999 tp = typebuf.tb_buf + typebuf.tb_off + offset;
4000 len = typebuf.tb_len - offset; /* length of the input */
4001 }
4002 else
4003 {
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004004 if (offset >= *buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 break;
4006 tp = buf + offset;
Bram Moolenaara8c8a682012-02-05 22:05:48 +01004007 len = *buflen - offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009
4010 /*
4011 * Don't check characters after K_SPECIAL, those are already
4012 * translated terminal chars (avoid translating ~@^Hx).
4013 */
4014 if (*tp == K_SPECIAL)
4015 {
4016 offset += 2; /* there are always 2 extra characters */
4017 continue;
4018 }
4019
4020 /*
4021 * Skip this position if the character does not appear as the first
4022 * character in term_strings. This speeds up a lot, since most
4023 * termcodes start with the same character (ESC or CSI).
4024 */
4025 i = *tp;
4026 for (p = termleader; *p && *p != i; ++p)
4027 ;
4028 if (*p == NUL)
4029 continue;
4030
4031 /*
4032 * Skip this position if p_ek is not set and tp[0] is an ESC and we
4033 * are in Insert mode.
4034 */
4035 if (*tp == ESC && !p_ek && (State & INSERT))
4036 continue;
4037
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 key_name[0] = NUL; /* no key name found yet */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004039 key_name[1] = NUL; /* no key name found yet */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004040 modifiers = 0; /* no modifiers yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041
4042#ifdef FEAT_GUI
4043 if (gui.in_use)
4044 {
4045 /*
4046 * GUI special key codes are all of the form [CSI xx].
4047 */
4048 if (*tp == CSI) /* Special key from GUI */
4049 {
4050 if (len < 3)
4051 return -1; /* Shouldn't happen */
4052 slen = 3;
4053 key_name[0] = tp[1];
4054 key_name[1] = tp[2];
4055 }
4056 }
4057 else
4058#endif /* FEAT_GUI */
4059 {
4060 for (idx = 0; idx < tc_len; ++idx)
4061 {
4062 /*
4063 * Ignore the entry if we are not at the start of
4064 * typebuf.tb_buf[]
4065 * and there are not enough characters to make a match.
4066 * But only when the 'K' flag is in 'cpoptions'.
4067 */
4068 slen = termcodes[idx].len;
4069 if (cpo_koffset && offset && len < slen)
4070 continue;
4071 if (STRNCMP(termcodes[idx].code, tp,
4072 (size_t)(slen > len ? len : slen)) == 0)
4073 {
4074 if (len < slen) /* got a partial sequence */
4075 return -1; /* need to get more chars */
4076
4077 /*
4078 * When found a keypad key, check if there is another key
4079 * that matches and use that one. This makes <Home> to be
4080 * found instead of <kHome> when they produce the same
4081 * key code.
4082 */
4083 if (termcodes[idx].name[0] == 'K'
4084 && VIM_ISDIGIT(termcodes[idx].name[1]))
4085 {
4086 for (j = idx + 1; j < tc_len; ++j)
4087 if (termcodes[j].len == slen &&
4088 STRNCMP(termcodes[idx].code,
4089 termcodes[j].code, slen) == 0)
4090 {
4091 idx = j;
4092 break;
4093 }
4094 }
4095
4096 key_name[0] = termcodes[idx].name[0];
4097 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 break;
4099 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004100
4101 /*
4102 * Check for code with modifier, like xterm uses:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004103 * <Esc>[123;*X (modslen == slen - 3)
4104 * Also <Esc>O*X and <M-O>*X (modslen == slen - 2).
4105 * When there is a modifier the * matches a number.
4106 * When there is no modifier the ;* or * is omitted.
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004107 */
4108 if (termcodes[idx].modlen > 0)
4109 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004110 modslen = termcodes[idx].modlen;
4111 if (cpo_koffset && offset && len < modslen)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004112 continue;
4113 if (STRNCMP(termcodes[idx].code, tp,
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004114 (size_t)(modslen > len ? len : modslen)) == 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004115 {
4116 int n;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004117
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004118 if (len <= modslen) /* got a partial sequence */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004119 return -1; /* need to get more chars */
4120
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004121 if (tp[modslen] == termcodes[idx].code[slen - 1])
4122 slen = modslen + 1; /* no modifiers */
4123 else if (tp[modslen] != ';' && modslen == slen - 3)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004124 continue; /* no match */
4125 else
4126 {
4127 /* Skip over the digits, the final char must
4128 * follow. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004129 for (j = slen - 2; j < len && isdigit(tp[j]); ++j)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004130 ;
4131 ++j;
4132 if (len < j) /* got a partial sequence */
4133 return -1; /* need to get more chars */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004134 if (tp[j - 1] != termcodes[idx].code[slen - 1])
4135 continue; /* no match */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004136
4137 /* Match! Convert modifier bits. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004138 n = atoi((char *)tp + slen - 2) - 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004139 if (n & 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004140 modifiers |= MOD_MASK_SHIFT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004141 if (n & 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004142 modifiers |= MOD_MASK_ALT;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004143 if (n & 4)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004144 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004145 if (n & 8)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004146 modifiers |= MOD_MASK_META;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004147
4148 slen = j;
4149 }
4150 key_name[0] = termcodes[idx].name[0];
4151 key_name[1] = termcodes[idx].name[1];
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004152 break;
4153 }
4154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156 }
4157
4158#ifdef FEAT_TERMRESPONSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004159 if (key_name[0] == NUL
4160 /* URXVT mouse uses <ESC>[#;#;#M, but we are matching <ESC>[ */
Bram Moolenaare533bbe2013-03-16 14:33:36 +01004161 || key_name[0] == KS_URXVT_MOUSE
4162# ifdef FEAT_MBYTE
4163 || u7_status == U7_SENT
4164# endif
4165 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 {
Bram Moolenaar9584b312013-03-13 19:29:28 +01004167 /* Check for some responses from terminal start with "<Esc>[" or
4168 * CSI.
4169 *
4170 * - xterm version string: <Esc>[>{x};{vers};{y}c
4171 * Also eat other possible responses to t_RV, rxvt returns
4172 * "<Esc>[?1;2c". Also accept CSI instead of <Esc>[.
4173 * mrxvt has been reported to have "+" in the version. Assume
4174 * the escape sequence ends with a letter or one of "{|}~".
4175 *
4176 * - cursor position report: <Esc>[{row};{col}R
4177 * The final byte is 'R'. now it is only used for checking for
4178 * ambiguous-width character state.
4179 */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004180 p = tp[0] == CSI ? tp + 1 : tp + 2;
Bram Moolenaar9584b312013-03-13 19:29:28 +01004181 if ((*T_CRV != NUL || *T_U7 != NUL)
4182 && ((tp[0] == ESC && tp[1] == '[' && len >= 3)
Bram Moolenaar46a75612013-05-15 14:22:41 +02004183 || (tp[0] == CSI && len >= 2))
4184 && (VIM_ISDIGIT(*p) || *p == '>' || *p == '?'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 {
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004186#ifdef FEAT_MBYTE
4187 int col;
Bram Moolenaarc41053062014-03-25 13:46:26 +01004188 int row_char = NUL;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 j = 0;
4191 extra = 0;
Bram Moolenaarc9dd5bc2008-02-27 15:14:04 +00004192 for (i = 2 + (tp[0] != CSI); i < len
4193 && !(tp[i] >= '{' && tp[i] <= '~')
4194 && !ASCII_ISALPHA(tp[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 if (tp[i] == ';' && ++j == 1)
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004196 {
Bram Moolenaar46a75612013-05-15 14:22:41 +02004197 extra = i + 1;
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004198#ifdef FEAT_MBYTE
4199 row_char = tp[i - 1];
4200#endif
4201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004203 {
4204 LOG_TR("Not enough characters for CRV");
4205 return -1;
4206 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004207#ifdef FEAT_MBYTE
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004208 if (extra > 0)
4209 col = atoi((char *)tp + extra);
4210 else
4211 col = 0;
4212
4213 /* Eat it when it has 2 arguments and ends in 'R'. Also when
4214 * u7_status is not "sent", it may be from a previous Vim that
4215 * just exited. But not for <S-F3>, it sends something
4216 * similar, check for row and column to make sense. */
4217 if (j == 1 && tp[i] == 'R' && row_char == '2' && col >= 2)
Bram Moolenaar9584b312013-03-13 19:29:28 +01004218 {
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004219 char *aw = NULL;
Bram Moolenaar9584b312013-03-13 19:29:28 +01004220
Bram Moolenaar2951b772013-07-03 12:45:31 +02004221 LOG_TR("Received U7 status");
Bram Moolenaar9584b312013-03-13 19:29:28 +01004222 u7_status = U7_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004223# ifdef FEAT_AUTOCMD
4224 did_cursorhold = TRUE;
4225# endif
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004226 if (col == 2)
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004227 aw = "single";
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004228 else if (col == 3)
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004229 aw = "double";
Bram Moolenaar2951b772013-07-03 12:45:31 +02004230 if (aw != NULL && STRCMP(aw, p_ambw) != 0)
4231 {
4232 /* Setting the option causes a screen redraw. Do that
4233 * right away if possible, keeping any messages. */
Bram Moolenaar2a66a072013-04-06 14:30:40 +02004234 set_option_value((char_u *)"ambw", 0L, (char_u *)aw, 0);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004235# ifdef DEBUG_TERMRESPONSE
Bram Moolenaar2951b772013-07-03 12:45:31 +02004236 {
4237 char buf[100];
4238 int r = redraw_asap(CLEAR);
4239
4240 sprintf(buf, "set 'ambiwidth', redraw_asap(): %d",
4241 r);
4242 log_tr(buf);
4243 }
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004244# else
Bram Moolenaar2951b772013-07-03 12:45:31 +02004245 redraw_asap(CLEAR);
Bram Moolenaar9c8c8c52014-03-19 14:01:57 +01004246# endif
Bram Moolenaar2951b772013-07-03 12:45:31 +02004247 }
Bram Moolenaar9584b312013-03-13 19:29:28 +01004248 key_name[0] = (int)KS_EXTRA;
4249 key_name[1] = (int)KE_IGNORE;
4250 slen = i + 1;
4251 }
4252 else
4253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 /* eat it when at least one digit and ending in 'c' */
Bram Moolenaar9584b312013-03-13 19:29:28 +01004255 if (*T_CRV != NUL && i > 2 + (tp[0] != CSI) && tp[i] == 'c')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004257 LOG_TR("Received CRV");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 crv_status = CRV_GOT;
Bram Moolenaar68879252013-04-05 19:50:17 +02004259# ifdef FEAT_AUTOCMD
4260 did_cursorhold = TRUE;
4261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
4263 /* If this code starts with CSI, you can bet that the
4264 * terminal uses 8-bit codes. */
4265 if (tp[0] == CSI)
4266 switch_to_8bit();
4267
4268 /* rxvt sends its version number: "20703" is 2.7.3.
4269 * Ignore it for when the user has set 'term' to xterm,
4270 * even though it's an rxvt. */
Bram Moolenaar46a75612013-05-15 14:22:41 +02004271 if (extra > 0)
4272 extra = atoi((char *)tp + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 if (extra > 20000)
4274 extra = 0;
4275
4276 if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
4277 {
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004278 /* Only set 'ttymouse' automatically if it was not set
4279 * by the user already. */
4280 if (!option_was_set((char_u *)"ttym"))
4281 {
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004282# ifdef TTYM_SGR
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004283 if (extra >= 277)
4284 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004285 (char_u *)"sgr", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004286 else
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004287# endif
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004288 /* if xterm version >= 95 use mouse dragging */
4289 if (extra >= 95)
4290 set_option_value((char_u *)"ttym", 0L,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 (char_u *)"xterm2", 0);
Bram Moolenaarbffa06d2012-10-21 02:10:24 +02004292 }
4293
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 /* if xterm version >= 141 try to get termcap codes */
4295 if (extra >= 141)
4296 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02004297 LOG_TR("Enable checking for XT codes");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 check_for_codes = TRUE;
4299 need_gather = TRUE;
4300 req_codes_from_term();
4301 }
4302 }
4303# ifdef FEAT_EVAL
4304 set_vim_var_string(VV_TERMRESPONSE, tp, i + 1);
4305# endif
4306# ifdef FEAT_AUTOCMD
4307 apply_autocmds(EVENT_TERMRESPONSE,
4308 NULL, NULL, FALSE, curbuf);
4309# endif
4310 key_name[0] = (int)KS_EXTRA;
4311 key_name[1] = (int)KE_IGNORE;
4312 slen = i + 1;
4313 }
4314 }
4315
4316 /* Check for '<Esc>P1+r<hex bytes><Esc>\'. A "0" instead of the
4317 * "1" means an invalid request. */
4318 else if (check_for_codes
4319 && ((tp[0] == ESC && tp[1] == 'P' && len >= 2)
4320 || tp[0] == DCS))
4321 {
4322 j = 1 + (tp[0] != DCS);
4323 for (i = j; i < len; ++i)
4324 if ((tp[i] == ESC && tp[i + 1] == '\\' && i + 1 < len)
4325 || tp[i] == STERM)
4326 {
4327 if (i - j >= 3 && tp[j + 1] == '+' && tp[j + 2] == 'r')
4328 got_code_from_term(tp + j, i);
4329 key_name[0] = (int)KS_EXTRA;
4330 key_name[1] = (int)KE_IGNORE;
4331 slen = i + 1 + (tp[i] == ESC);
4332 break;
4333 }
4334
4335 if (i == len)
Bram Moolenaar2951b772013-07-03 12:45:31 +02004336 {
4337 LOG_TR("not enough characters for XT");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 return -1; /* not enough characters */
Bram Moolenaar2951b772013-07-03 12:45:31 +02004339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 }
4341 }
4342#endif
4343
4344 if (key_name[0] == NUL)
4345 continue; /* No match at this position, try next one */
4346
4347 /* We only get here when we have a complete termcode match */
4348
4349#ifdef FEAT_MOUSE
4350# ifdef FEAT_GUI
4351 /*
4352 * Only in the GUI: Fetch the pointer coordinates of the scroll event
4353 * so that we know which window to scroll later.
4354 */
4355 if (gui.in_use
4356 && key_name[0] == (int)KS_EXTRA
4357 && (key_name[1] == (int)KE_X1MOUSE
4358 || key_name[1] == (int)KE_X2MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004359 || key_name[1] == (int)KE_MOUSELEFT
4360 || key_name[1] == (int)KE_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 || key_name[1] == (int)KE_MOUSEDOWN
4362 || key_name[1] == (int)KE_MOUSEUP))
4363 {
4364 num_bytes = get_bytes_from_buf(tp + slen, bytes, 4);
4365 if (num_bytes == -1) /* not enough coordinates */
4366 return -1;
4367 mouse_col = 128 * (bytes[0] - ' ' - 1) + bytes[1] - ' ' - 1;
4368 mouse_row = 128 * (bytes[2] - ' ' - 1) + bytes[3] - ' ' - 1;
4369 slen += num_bytes;
4370 }
4371 else
4372# endif
4373 /*
4374 * If it is a mouse click, get the coordinates.
4375 */
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004376 if (key_name[0] == KS_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377# ifdef FEAT_MOUSE_JSB
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004378 || key_name[0] == KS_JSBTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379# endif
4380# ifdef FEAT_MOUSE_NET
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004381 || key_name[0] == KS_NETTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382# endif
4383# ifdef FEAT_MOUSE_DEC
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004384 || key_name[0] == KS_DEC_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385# endif
4386# ifdef FEAT_MOUSE_PTERM
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004387 || key_name[0] == KS_PTERM_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388# endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004389# ifdef FEAT_MOUSE_URXVT
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004390 || key_name[0] == KS_URXVT_MOUSE
4391# endif
4392# ifdef FEAT_MOUSE_SGR
4393 || key_name[0] == KS_SGR_MOUSE
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 )
4396 {
4397 is_click = is_drag = FALSE;
4398
Bram Moolenaar864207d2008-06-24 22:14:38 +00004399# if !defined(UNIX) || defined(FEAT_MOUSE_XTERM) || defined(FEAT_GUI) \
4400 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 if (key_name[0] == (int)KS_MOUSE)
4402 {
4403 /*
4404 * For xterm and MSDOS we get "<t_mouse>scr", where
4405 * s == encoded button state:
4406 * 0x20 = left button down
4407 * 0x21 = middle button down
4408 * 0x22 = right button down
4409 * 0x23 = any button release
4410 * 0x60 = button 4 down (scroll wheel down)
4411 * 0x61 = button 5 down (scroll wheel up)
4412 * add 0x04 for SHIFT
4413 * add 0x08 for ALT
4414 * add 0x10 for CTRL
4415 * add 0x20 for mouse drag (0x40 is drag with left button)
4416 * c == column + ' ' + 1 == column + 33
4417 * r == row + ' ' + 1 == row + 33
4418 *
4419 * The coordinates are passed on through global variables.
4420 * Ugly, but this avoids trouble with mouse clicks at an
4421 * unexpected moment and allows for mapping them.
4422 */
4423 for (;;)
4424 {
4425#ifdef FEAT_GUI
4426 if (gui.in_use)
4427 {
4428 /* GUI uses more bits for columns > 223 */
4429 num_bytes = get_bytes_from_buf(tp + slen, bytes, 5);
4430 if (num_bytes == -1) /* not enough coordinates */
4431 return -1;
4432 mouse_code = bytes[0];
4433 mouse_col = 128 * (bytes[1] - ' ' - 1)
4434 + bytes[2] - ' ' - 1;
4435 mouse_row = 128 * (bytes[3] - ' ' - 1)
4436 + bytes[4] - ' ' - 1;
4437 }
4438 else
4439#endif
4440 {
4441 num_bytes = get_bytes_from_buf(tp + slen, bytes, 3);
4442 if (num_bytes == -1) /* not enough coordinates */
4443 return -1;
4444 mouse_code = bytes[0];
4445 mouse_col = bytes[1] - ' ' - 1;
4446 mouse_row = bytes[2] - ' ' - 1;
4447 }
4448 slen += num_bytes;
4449
4450 /* If the following bytes is also a mouse code and it has
4451 * the same code, dump this one and get the next. This
4452 * makes dragging a whole lot faster. */
4453#ifdef FEAT_GUI
4454 if (gui.in_use)
4455 j = 3;
4456 else
4457#endif
4458 j = termcodes[idx].len;
4459 if (STRNCMP(tp, tp + slen, (size_t)j) == 0
4460 && tp[slen + j] == mouse_code
4461 && tp[slen + j + 1] != NUL
4462 && tp[slen + j + 2] != NUL
4463#ifdef FEAT_GUI
4464 && (!gui.in_use
4465 || (tp[slen + j + 3] != NUL
4466 && tp[slen + j + 4] != NUL))
4467#endif
4468 )
4469 slen += j;
4470 else
4471 break;
4472 }
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004475# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
4476 if (key_name[0] == KS_URXVT_MOUSE
4477 || key_name[0] == KS_SGR_MOUSE)
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004478 {
4479 for (;;)
4480 {
4481 /* URXVT 1015 mouse reporting mode:
4482 * Almost identical to xterm mouse mode, except the values
4483 * are decimal instead of bytes.
4484 *
4485 * \033[%d;%d;%dM
4486 * ^-- row
4487 * ^----- column
4488 * ^-------- code
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004489 *
4490 * SGR 1006 mouse reporting mode:
4491 * Almost identical to xterm mouse mode, except the values
4492 * are decimal instead of bytes.
4493 *
4494 * \033[<%d;%d;%dM
4495 * ^-- row
4496 * ^----- column
4497 * ^-------- code
4498 *
4499 * \033[<%d;%d;%dm : mouse release event
4500 * ^-- row
4501 * ^----- column
4502 * ^-------- code
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004503 */
4504 p = tp + slen;
4505
4506 mouse_code = getdigits(&p);
4507 if (*p++ != ';')
4508 return -1;
4509
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004510 /* when mouse reporting is SGR, add 32 to mouse code */
4511 if (key_name[0] == KS_SGR_MOUSE)
4512 mouse_code += 32;
4513
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004514 mouse_col = getdigits(&p) - 1;
4515 if (*p++ != ';')
4516 return -1;
4517
4518 mouse_row = getdigits(&p) - 1;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004519 if (key_name[0] == KS_SGR_MOUSE && *p == 'm')
4520 mouse_code |= MOUSE_RELEASE;
4521 else if (*p != 'M')
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004522 return -1;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004523 p++;
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004524
4525 slen += (int)(p - (tp + slen));
4526
4527 /* skip this one if next one has same code (like xterm
4528 * case) */
4529 j = termcodes[idx].len;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004530 if (STRNCMP(tp, tp + slen, (size_t)j) == 0)
4531 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004532 int slen2;
4533 int cmd_complete = 0;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004534
4535 /* check if the command is complete by looking for the
4536 * 'M' */
4537 for (slen2 = slen; slen2 < len; slen2++)
4538 {
4539 if (tp[slen2] == 'M'
4540 || (key_name[0] == KS_SGR_MOUSE
4541 && tp[slen2] == 'm'))
4542 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004543 cmd_complete = 1;
4544 break;
4545 }
4546 }
4547 p += j;
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004548 if (cmd_complete && getdigits(&p) == mouse_code)
4549 {
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004550 slen += j; /* skip the \033[ */
4551 continue;
4552 }
4553 }
4554 break;
4555 }
4556 }
4557# endif
4558
4559 if (key_name[0] == (int)KS_MOUSE
4560#ifdef FEAT_MOUSE_URXVT
4561 || key_name[0] == (int)KS_URXVT_MOUSE
4562#endif
Bram Moolenaar2b9578f2012-08-15 16:21:32 +02004563#ifdef FEAT_MOUSE_SGR
4564 || key_name[0] == KS_SGR_MOUSE
4565#endif
Bram Moolenaar1dff76b2011-10-26 23:48:20 +02004566 )
4567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568# if !defined(MSWIN) && !defined(MSDOS)
4569 /*
4570 * Handle mouse events.
4571 * Recognize the xterm mouse wheel, but not in the GUI, the
4572 * Linux console with GPM and the MS-DOS or Win32 console
4573 * (multi-clicks use >= 0x60).
4574 */
4575 if (mouse_code >= MOUSEWHEEL_LOW
4576# ifdef FEAT_GUI
4577 && !gui.in_use
4578# endif
4579# ifdef FEAT_MOUSE_GPM
4580 && gpm_flag == 0
4581# endif
4582 )
4583 {
4584 /* Keep the mouse_code before it's changed, so that we
4585 * remember that it was a mouse wheel click. */
4586 wheel_code = mouse_code;
4587 }
4588# ifdef FEAT_MOUSE_XTERM
4589 else if (held_button == MOUSE_RELEASE
4590# ifdef FEAT_GUI
4591 && !gui.in_use
4592# endif
4593 && (mouse_code == 0x23 || mouse_code == 0x24))
4594 {
4595 /* Apparently used by rxvt scroll wheel. */
4596 wheel_code = mouse_code - 0x23 + MOUSEWHEEL_LOW;
4597 }
4598# endif
4599
4600# if defined(UNIX) && defined(FEAT_MOUSE_TTY)
4601 else if (use_xterm_mouse() > 1)
4602 {
4603 if (mouse_code & MOUSE_DRAG_XTERM)
4604 mouse_code |= MOUSE_DRAG;
4605 }
4606# endif
4607# ifdef FEAT_XCLIPBOARD
4608 else if (!(mouse_code & MOUSE_DRAG & ~MOUSE_CLICK_MASK))
4609 {
4610 if ((mouse_code & MOUSE_RELEASE) == MOUSE_RELEASE)
4611 stop_xterm_trace();
4612 else
4613 start_xterm_trace(mouse_code);
4614 }
4615# endif
4616# endif
4617 }
4618# endif /* !UNIX || FEAT_MOUSE_XTERM */
4619# ifdef FEAT_MOUSE_NET
4620 if (key_name[0] == (int)KS_NETTERM_MOUSE)
4621 {
4622 int mc, mr;
4623
4624 /* expect a rather limited sequence like: balancing {
4625 * \033}6,45\r
4626 * '6' is the row, 45 is the column
4627 */
4628 p = tp + slen;
4629 mr = getdigits(&p);
4630 if (*p++ != ',')
4631 return -1;
4632 mc = getdigits(&p);
4633 if (*p++ != '\r')
4634 return -1;
4635
4636 mouse_col = mc - 1;
4637 mouse_row = mr - 1;
4638 mouse_code = MOUSE_LEFT;
4639 slen += (int)(p - (tp + slen));
4640 }
4641# endif /* FEAT_MOUSE_NET */
4642# ifdef FEAT_MOUSE_JSB
4643 if (key_name[0] == (int)KS_JSBTERM_MOUSE)
4644 {
4645 int mult, val, iter, button, status;
4646
4647 /* JSBTERM Input Model
4648 * \033[0~zw uniq escape sequence
4649 * (L-x) Left button pressed - not pressed x not reporting
4650 * (M-x) Middle button pressed - not pressed x not reporting
4651 * (R-x) Right button pressed - not pressed x not reporting
4652 * (SDmdu) Single , Double click, m mouse move d button down
4653 * u button up
4654 * ### X cursor position padded to 3 digits
4655 * ### Y cursor position padded to 3 digits
4656 * (s-x) SHIFT key pressed - not pressed x not reporting
4657 * (c-x) CTRL key pressed - not pressed x not reporting
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004658 * \033\\ terminating sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 */
4660
4661 p = tp + slen;
4662 button = mouse_code = 0;
4663 switch (*p++)
4664 {
4665 case 'L': button = 1; break;
4666 case '-': break;
4667 case 'x': break; /* ignore sequence */
4668 default: return -1; /* Unknown Result */
4669 }
4670 switch (*p++)
4671 {
4672 case 'M': button |= 2; break;
4673 case '-': break;
4674 case 'x': break; /* ignore sequence */
4675 default: return -1; /* Unknown Result */
4676 }
4677 switch (*p++)
4678 {
4679 case 'R': button |= 4; break;
4680 case '-': break;
4681 case 'x': break; /* ignore sequence */
4682 default: return -1; /* Unknown Result */
4683 }
4684 status = *p++;
4685 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4686 mult /= 10, p++)
4687 if (*p >= '0' && *p <= '9')
4688 val += (*p - '0') * mult;
4689 else
4690 return -1;
4691 mouse_col = val;
4692 for (val = 0, mult = 100, iter = 0; iter < 3; iter++,
4693 mult /= 10, p++)
4694 if (*p >= '0' && *p <= '9')
4695 val += (*p - '0') * mult;
4696 else
4697 return -1;
4698 mouse_row = val;
4699 switch (*p++)
4700 {
4701 case 's': button |= 8; break; /* SHIFT key Pressed */
4702 case '-': break; /* Not Pressed */
4703 case 'x': break; /* Not Reporting */
4704 default: return -1; /* Unknown Result */
4705 }
4706 switch (*p++)
4707 {
4708 case 'c': button |= 16; break; /* CTRL key Pressed */
4709 case '-': break; /* Not Pressed */
4710 case 'x': break; /* Not Reporting */
4711 default: return -1; /* Unknown Result */
4712 }
4713 if (*p++ != '\033')
4714 return -1;
4715 if (*p++ != '\\')
4716 return -1;
4717 switch (status)
4718 {
4719 case 'D': /* Double Click */
4720 case 'S': /* Single Click */
4721 if (button & 1) mouse_code |= MOUSE_LEFT;
4722 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4723 if (button & 4) mouse_code |= MOUSE_RIGHT;
4724 if (button & 8) mouse_code |= MOUSE_SHIFT;
4725 if (button & 16) mouse_code |= MOUSE_CTRL;
4726 break;
4727 case 'm': /* Mouse move */
4728 if (button & 1) mouse_code |= MOUSE_LEFT;
4729 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4730 if (button & 4) mouse_code |= MOUSE_RIGHT;
4731 if (button & 8) mouse_code |= MOUSE_SHIFT;
4732 if (button & 16) mouse_code |= MOUSE_CTRL;
4733 if ((button & 7) != 0)
4734 {
4735 held_button = mouse_code;
4736 mouse_code |= MOUSE_DRAG;
4737 }
4738 is_drag = TRUE;
4739 showmode();
4740 break;
4741 case 'd': /* Button Down */
4742 if (button & 1) mouse_code |= MOUSE_LEFT;
4743 if (button & 2) mouse_code |= MOUSE_MIDDLE;
4744 if (button & 4) mouse_code |= MOUSE_RIGHT;
4745 if (button & 8) mouse_code |= MOUSE_SHIFT;
4746 if (button & 16) mouse_code |= MOUSE_CTRL;
4747 break;
4748 case 'u': /* Button Up */
4749 if (button & 1)
4750 mouse_code |= MOUSE_LEFT | MOUSE_RELEASE;
4751 if (button & 2)
4752 mouse_code |= MOUSE_MIDDLE | MOUSE_RELEASE;
4753 if (button & 4)
4754 mouse_code |= MOUSE_RIGHT | MOUSE_RELEASE;
4755 if (button & 8)
4756 mouse_code |= MOUSE_SHIFT;
4757 if (button & 16)
4758 mouse_code |= MOUSE_CTRL;
4759 break;
4760 default: return -1; /* Unknown Result */
4761 }
4762
4763 slen += (p - (tp + slen));
4764 }
4765# endif /* FEAT_MOUSE_JSB */
4766# ifdef FEAT_MOUSE_DEC
4767 if (key_name[0] == (int)KS_DEC_MOUSE)
4768 {
4769 /* The DEC Locator Input Model
4770 * Netterm delivers the code sequence:
4771 * \033[2;4;24;80&w (left button down)
4772 * \033[3;0;24;80&w (left button up)
4773 * \033[6;1;24;80&w (right button down)
4774 * \033[7;0;24;80&w (right button up)
4775 * CSI Pe ; Pb ; Pr ; Pc ; Pp & w
4776 * Pe is the event code
4777 * Pb is the button code
4778 * Pr is the row coordinate
4779 * Pc is the column coordinate
4780 * Pp is the third coordinate (page number)
4781 * Pe, the event code indicates what event caused this report
4782 * The following event codes are defined:
4783 * 0 - request, the terminal received an explicit request
4784 * for a locator report, but the locator is unavailable
4785 * 1 - request, the terminal received an explicit request
4786 * for a locator report
4787 * 2 - left button down
4788 * 3 - left button up
4789 * 4 - middle button down
4790 * 5 - middle button up
4791 * 6 - right button down
4792 * 7 - right button up
4793 * 8 - fourth button down
4794 * 9 - fourth button up
4795 * 10 - locator outside filter rectangle
4796 * Pb, the button code, ASCII decimal 0-15 indicating which
4797 * buttons are down if any. The state of the four buttons
4798 * on the locator correspond to the low four bits of the
4799 * decimal value,
4800 * "1" means button depressed
4801 * 0 - no buttons down,
4802 * 1 - right,
4803 * 2 - middle,
4804 * 4 - left,
4805 * 8 - fourth
4806 * Pr is the row coordinate of the locator position in the page,
4807 * encoded as an ASCII decimal value.
4808 * If Pr is omitted, the locator position is undefined
4809 * (outside the terminal window for example).
4810 * Pc is the column coordinate of the locator position in the
4811 * page, encoded as an ASCII decimal value.
4812 * If Pc is omitted, the locator position is undefined
4813 * (outside the terminal window for example).
4814 * Pp is the page coordinate of the locator position
4815 * encoded as an ASCII decimal value.
4816 * The page coordinate may be omitted if the locator is on
4817 * page one (the default). We ignore it anyway.
4818 */
4819 int Pe, Pb, Pr, Pc;
4820
4821 p = tp + slen;
4822
4823 /* get event status */
4824 Pe = getdigits(&p);
4825 if (*p++ != ';')
4826 return -1;
4827
4828 /* get button status */
4829 Pb = getdigits(&p);
4830 if (*p++ != ';')
4831 return -1;
4832
4833 /* get row status */
4834 Pr = getdigits(&p);
4835 if (*p++ != ';')
4836 return -1;
4837
4838 /* get column status */
4839 Pc = getdigits(&p);
4840
4841 /* the page parameter is optional */
4842 if (*p == ';')
4843 {
4844 p++;
4845 (void)getdigits(&p);
4846 }
4847 if (*p++ != '&')
4848 return -1;
4849 if (*p++ != 'w')
4850 return -1;
4851
4852 mouse_code = 0;
4853 switch (Pe)
4854 {
4855 case 0: return -1; /* position request while unavailable */
4856 case 1: /* a response to a locator position request includes
4857 the status of all buttons */
4858 Pb &= 7; /* mask off and ignore fourth button */
4859 if (Pb & 4)
4860 mouse_code = MOUSE_LEFT;
4861 if (Pb & 2)
4862 mouse_code = MOUSE_MIDDLE;
4863 if (Pb & 1)
4864 mouse_code = MOUSE_RIGHT;
4865 if (Pb)
4866 {
4867 held_button = mouse_code;
4868 mouse_code |= MOUSE_DRAG;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004869 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871 is_drag = TRUE;
4872 showmode();
4873 break;
4874 case 2: mouse_code = MOUSE_LEFT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004875 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 break;
4877 case 3: mouse_code = MOUSE_RELEASE | MOUSE_LEFT;
4878 break;
4879 case 4: mouse_code = MOUSE_MIDDLE;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004880 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 break;
4882 case 5: mouse_code = MOUSE_RELEASE | MOUSE_MIDDLE;
4883 break;
4884 case 6: mouse_code = MOUSE_RIGHT;
Bram Moolenaar6bb68362005-03-22 23:03:44 +00004885 WantQueryMouse = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 break;
4887 case 7: mouse_code = MOUSE_RELEASE | MOUSE_RIGHT;
4888 break;
4889 case 8: return -1; /* fourth button down */
4890 case 9: return -1; /* fourth button up */
4891 case 10: return -1; /* mouse outside of filter rectangle */
4892 default: return -1; /* should never occur */
4893 }
4894
4895 mouse_col = Pc - 1;
4896 mouse_row = Pr - 1;
4897
4898 slen += (int)(p - (tp + slen));
4899 }
4900# endif /* FEAT_MOUSE_DEC */
4901# ifdef FEAT_MOUSE_PTERM
4902 if (key_name[0] == (int)KS_PTERM_MOUSE)
4903 {
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +02004904 int button, num_clicks, action;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905
4906 p = tp + slen;
4907
4908 action = getdigits(&p);
4909 if (*p++ != ';')
4910 return -1;
4911
4912 mouse_row = getdigits(&p);
4913 if (*p++ != ';')
4914 return -1;
4915 mouse_col = getdigits(&p);
4916 if (*p++ != ';')
4917 return -1;
4918
4919 button = getdigits(&p);
4920 mouse_code = 0;
4921
4922 switch( button )
4923 {
4924 case 4: mouse_code = MOUSE_LEFT; break;
4925 case 1: mouse_code = MOUSE_RIGHT; break;
4926 case 2: mouse_code = MOUSE_MIDDLE; break;
4927 default: return -1;
4928 }
4929
4930 switch( action )
4931 {
4932 case 31: /* Initial press */
4933 if (*p++ != ';')
4934 return -1;
4935
4936 num_clicks = getdigits(&p); /* Not used */
4937 break;
4938
4939 case 32: /* Release */
4940 mouse_code |= MOUSE_RELEASE;
4941 break;
4942
4943 case 33: /* Drag */
4944 held_button = mouse_code;
4945 mouse_code |= MOUSE_DRAG;
4946 break;
4947
4948 default:
4949 return -1;
4950 }
4951
4952 if (*p++ != 't')
4953 return -1;
4954
4955 slen += (p - (tp + slen));
4956 }
4957# endif /* FEAT_MOUSE_PTERM */
4958
4959 /* Interpret the mouse code */
4960 current_button = (mouse_code & MOUSE_CLICK_MASK);
4961 if (current_button == MOUSE_RELEASE
4962# ifdef FEAT_MOUSE_XTERM
4963 && wheel_code == 0
4964# endif
4965 )
4966 {
4967 /*
4968 * If we get a mouse drag or release event when
4969 * there is no mouse button held down (held_button ==
4970 * MOUSE_RELEASE), produce a K_IGNORE below.
4971 * (can happen when you hold down two buttons
4972 * and then let them go, or click in the menu bar, but not
4973 * on a menu, and drag into the text).
4974 */
4975 if ((mouse_code & MOUSE_DRAG) == MOUSE_DRAG)
4976 is_drag = TRUE;
4977 current_button = held_button;
4978 }
4979 else if (wheel_code == 0)
4980 {
4981# ifdef CHECK_DOUBLE_CLICK
4982# ifdef FEAT_MOUSE_GPM
4983# ifdef FEAT_GUI
4984 /*
4985 * Only for Unix, when GUI or gpm is not active, we handle
4986 * multi-clicks here.
4987 */
4988 if (gpm_flag == 0 && !gui.in_use)
4989# else
4990 if (gpm_flag == 0)
4991# endif
4992# else
4993# ifdef FEAT_GUI
4994 if (!gui.in_use)
4995# endif
4996# endif
4997 {
4998 /*
4999 * Compute the time elapsed since the previous mouse click.
5000 */
5001 gettimeofday(&mouse_time, NULL);
5002 timediff = (mouse_time.tv_usec
5003 - orig_mouse_time.tv_usec) / 1000;
5004 if (timediff < 0)
5005 --orig_mouse_time.tv_sec;
5006 timediff += (mouse_time.tv_sec
5007 - orig_mouse_time.tv_sec) * 1000;
5008 orig_mouse_time = mouse_time;
5009 if (mouse_code == orig_mouse_code
5010 && timediff < p_mouset
5011 && orig_num_clicks != 4
5012 && orig_mouse_col == mouse_col
5013 && orig_mouse_row == mouse_row
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005014 && ((orig_topline == curwin->w_topline
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015#ifdef FEAT_DIFF
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005016 && orig_topfill == curwin->w_topfill
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017#endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005018 )
5019#ifdef FEAT_WINDOWS
5020 /* Double click in tab pages line also works
5021 * when window contents changes. */
5022 || (mouse_row == 0 && firstwin->w_winrow > 0)
5023#endif
5024 )
5025 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 ++orig_num_clicks;
5027 else
5028 orig_num_clicks = 1;
5029 orig_mouse_col = mouse_col;
5030 orig_mouse_row = mouse_row;
5031 orig_topline = curwin->w_topline;
5032#ifdef FEAT_DIFF
5033 orig_topfill = curwin->w_topfill;
5034#endif
5035 }
5036# if defined(FEAT_GUI) || defined(FEAT_MOUSE_GPM)
5037 else
5038 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5039# endif
5040# else
5041 orig_num_clicks = NUM_MOUSE_CLICKS(mouse_code);
5042# endif
5043 is_click = TRUE;
5044 orig_mouse_code = mouse_code;
5045 }
5046 if (!is_drag)
5047 held_button = mouse_code & MOUSE_CLICK_MASK;
5048
5049 /*
5050 * Translate the actual mouse event into a pseudo mouse event.
5051 * First work out what modifiers are to be used.
5052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 if (orig_mouse_code & MOUSE_SHIFT)
5054 modifiers |= MOD_MASK_SHIFT;
5055 if (orig_mouse_code & MOUSE_CTRL)
5056 modifiers |= MOD_MASK_CTRL;
5057 if (orig_mouse_code & MOUSE_ALT)
5058 modifiers |= MOD_MASK_ALT;
5059 if (orig_num_clicks == 2)
5060 modifiers |= MOD_MASK_2CLICK;
5061 else if (orig_num_clicks == 3)
5062 modifiers |= MOD_MASK_3CLICK;
5063 else if (orig_num_clicks == 4)
5064 modifiers |= MOD_MASK_4CLICK;
5065
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 /* Work out our pseudo mouse event */
5067 key_name[0] = (int)KS_EXTRA;
5068 if (wheel_code != 0)
Bram Moolenaar5074e302010-07-18 14:26:11 +02005069 {
5070 if (wheel_code & MOUSE_CTRL)
5071 modifiers |= MOD_MASK_CTRL;
Bram Moolenaar01a8f382010-07-18 23:32:13 +02005072 if (wheel_code & MOUSE_ALT)
5073 modifiers |= MOD_MASK_ALT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 key_name[1] = (wheel_code & 1)
5075 ? (int)KE_MOUSEUP : (int)KE_MOUSEDOWN;
Bram Moolenaar5074e302010-07-18 14:26:11 +02005076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 else
5078 key_name[1] = get_pseudo_mouse_code(current_button,
5079 is_click, is_drag);
5080 }
5081#endif /* FEAT_MOUSE */
5082
5083#ifdef FEAT_GUI
5084 /*
5085 * If using the GUI, then we get menu and scrollbar events.
5086 *
5087 * A menu event is encoded as K_SPECIAL, KS_MENU, KE_FILLER followed by
5088 * four bytes which are to be taken as a pointer to the vimmenu_T
5089 * structure.
5090 *
Bram Moolenaarcf0dfa22007-05-10 18:52:16 +00005091 * A tab line event is encoded as K_SPECIAL KS_TABLINE nr, where "nr"
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005092 * is one byte with the tab index.
5093 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 * A scrollbar event is K_SPECIAL, KS_VER_SCROLLBAR, KE_FILLER followed
5095 * by one byte representing the scrollbar number, and then four bytes
5096 * representing a long_u which is the new value of the scrollbar.
5097 *
5098 * A horizontal scrollbar event is K_SPECIAL, KS_HOR_SCROLLBAR,
5099 * KE_FILLER followed by four bytes representing a long_u which is the
5100 * new value of the scrollbar.
5101 */
5102# ifdef FEAT_MENU
5103 else if (key_name[0] == (int)KS_MENU)
5104 {
5105 long_u val;
5106
5107 num_bytes = get_long_from_buf(tp + slen, &val);
5108 if (num_bytes == -1)
5109 return -1;
5110 current_menu = (vimmenu_T *)val;
5111 slen += num_bytes;
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00005112
5113 /* The menu may have been deleted right after it was used, check
5114 * for that. */
5115 if (check_menu_pointer(root_menu, current_menu) == FAIL)
5116 {
5117 key_name[0] = KS_EXTRA;
5118 key_name[1] = (int)KE_IGNORE;
5119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005122# ifdef FEAT_GUI_TABLINE
5123 else if (key_name[0] == (int)KS_TABLINE)
5124 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005125 /* Selecting tabline tab or using its menu. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005126 num_bytes = get_bytes_from_buf(tp + slen, bytes, 1);
5127 if (num_bytes == -1)
5128 return -1;
5129 current_tab = (int)bytes[0];
Bram Moolenaar07354542007-09-15 12:07:46 +00005130 if (current_tab == 255) /* -1 in a byte gives 255 */
5131 current_tab = -1;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005132 slen += num_bytes;
5133 }
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005134 else if (key_name[0] == (int)KS_TABMENU)
5135 {
5136 /* Selecting tabline tab or using its menu. */
5137 num_bytes = get_bytes_from_buf(tp + slen, bytes, 2);
5138 if (num_bytes == -1)
5139 return -1;
5140 current_tab = (int)bytes[0];
5141 current_tabmenu = (int)bytes[1];
5142 slen += num_bytes;
5143 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005144# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145# ifndef USE_ON_FLY_SCROLL
5146 else if (key_name[0] == (int)KS_VER_SCROLLBAR)
5147 {
5148 long_u val;
5149
5150 /* Get the last scrollbar event in the queue of the same type */
5151 j = 0;
5152 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_VER_SCROLLBAR
5153 && tp[j + 2] != NUL; ++i)
5154 {
5155 j += 3;
5156 num_bytes = get_bytes_from_buf(tp + j, bytes, 1);
5157 if (num_bytes == -1)
5158 break;
5159 if (i == 0)
5160 current_scrollbar = (int)bytes[0];
5161 else if (current_scrollbar != (int)bytes[0])
5162 break;
5163 j += num_bytes;
5164 num_bytes = get_long_from_buf(tp + j, &val);
5165 if (num_bytes == -1)
5166 break;
5167 scrollbar_value = val;
5168 j += num_bytes;
5169 slen = j;
5170 }
5171 if (i == 0) /* not enough characters to make one */
5172 return -1;
5173 }
5174 else if (key_name[0] == (int)KS_HOR_SCROLLBAR)
5175 {
5176 long_u val;
5177
5178 /* Get the last horiz. scrollbar event in the queue */
5179 j = 0;
5180 for (i = 0; tp[j] == CSI && tp[j + 1] == KS_HOR_SCROLLBAR
5181 && tp[j + 2] != NUL; ++i)
5182 {
5183 j += 3;
5184 num_bytes = get_long_from_buf(tp + j, &val);
5185 if (num_bytes == -1)
5186 break;
5187 scrollbar_value = val;
5188 j += num_bytes;
5189 slen = j;
5190 }
5191 if (i == 0) /* not enough characters to make one */
5192 return -1;
5193 }
5194# endif /* !USE_ON_FLY_SCROLL */
5195#endif /* FEAT_GUI */
5196
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005197 /*
5198 * Change <xHome> to <Home>, <xUp> to <Up>, etc.
5199 */
5200 key = handle_x_keys(TERMCAP2KEY(key_name[0], key_name[1]));
5201
5202 /*
5203 * Add any modifier codes to our string.
5204 */
5205 new_slen = 0; /* Length of what will replace the termcode */
5206 if (modifiers != 0)
5207 {
5208 /* Some keys have the modifier included. Need to handle that here
5209 * to make mappings work. */
5210 key = simplify_key(key, &modifiers);
5211 if (modifiers != 0)
5212 {
5213 string[new_slen++] = K_SPECIAL;
5214 string[new_slen++] = (int)KS_MODIFIER;
5215 string[new_slen++] = modifiers;
5216 }
5217 }
5218
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 /* Finally, add the special key code to our string */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005220 key_name[0] = KEY2TERMCAP0(key);
5221 key_name[1] = KEY2TERMCAP1(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 if (key_name[0] == KS_KEY)
Bram Moolenaar6768a332009-01-22 17:33:49 +00005223 {
5224 /* from ":set <M-b>=xx" */
5225#ifdef FEAT_MBYTE
5226 if (has_mbyte)
5227 new_slen += (*mb_char2bytes)(key_name[1], string + new_slen);
5228 else
5229#endif
5230 string[new_slen++] = key_name[1];
5231 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005232 else if (new_slen == 0 && key_name[0] == KS_EXTRA
5233 && key_name[1] == KE_IGNORE)
5234 {
5235 /* Do not put K_IGNORE into the buffer, do return KEYLEN_REMOVED
5236 * to indicate what happened. */
5237 retval = KEYLEN_REMOVED;
5238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 else
5240 {
5241 string[new_slen++] = K_SPECIAL;
5242 string[new_slen++] = key_name[0];
5243 string[new_slen++] = key_name[1];
5244 }
5245 string[new_slen] = NUL;
5246 extra = new_slen - slen;
5247 if (buf == NULL)
5248 {
5249 if (extra < 0)
5250 /* remove matched chars, taking care of noremap */
5251 del_typebuf(-extra, offset);
5252 else if (extra > 0)
5253 /* insert the extra space we need */
5254 ins_typebuf(string + slen, REMAP_YES, offset, FALSE, FALSE);
5255
5256 /*
5257 * Careful: del_typebuf() and ins_typebuf() may have reallocated
5258 * typebuf.tb_buf[]!
5259 */
5260 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, string,
5261 (size_t)new_slen);
5262 }
5263 else
5264 {
5265 if (extra < 0)
5266 /* remove matched characters */
5267 mch_memmove(buf + offset, buf + offset - extra,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005268 (size_t)(*buflen + offset + extra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 else if (extra > 0)
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005270 {
5271 /* Insert the extra space we need. If there is insufficient
5272 * space return -1. */
5273 if (*buflen + extra + new_slen >= bufsize)
5274 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 mch_memmove(buf + offset + extra, buf + offset,
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005276 (size_t)(*buflen - offset));
5277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 mch_memmove(buf + offset, string, (size_t)new_slen);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01005279 *buflen = *buflen + extra + new_slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01005281 return retval == 0 ? (len + extra + offset) : retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 }
5283
Bram Moolenaar2951b772013-07-03 12:45:31 +02005284#ifdef FEAT_TERMRESPONSE
5285 LOG_TR("normal character");
5286#endif
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 return 0; /* no match found */
5289}
5290
5291/*
5292 * Replace any terminal code strings in from[] with the equivalent internal
5293 * vim representation. This is used for the "from" and "to" part of a
5294 * mapping, and the "to" part of a menu command.
5295 * Any strings like "<C-UP>" are also replaced, unless 'cpoptions' contains
5296 * '<'.
5297 * K_SPECIAL by itself is replaced by K_SPECIAL KS_SPECIAL KE_FILLER.
5298 *
5299 * The replacement is done in result[] and finally copied into allocated
5300 * memory. If this all works well *bufp is set to the allocated memory and a
5301 * pointer to it is returned. If something fails *bufp is set to NULL and from
5302 * is returned.
5303 *
5304 * CTRL-V characters are removed. When "from_part" is TRUE, a trailing CTRL-V
5305 * is included, otherwise it is removed (for ":map xx ^V", maps xx to
5306 * nothing). When 'cpoptions' does not contain 'B', a backslash can be used
5307 * instead of a CTRL-V.
5308 */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005309 char_u *
5310replace_termcodes(from, bufp, from_part, do_lt, special)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 char_u *from;
5312 char_u **bufp;
5313 int from_part;
5314 int do_lt; /* also translate <lt> */
Bram Moolenaar9c102382006-05-03 21:26:49 +00005315 int special; /* always accept <key> notation */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
5317 int i;
5318 int slen;
5319 int key;
5320 int dlen = 0;
5321 char_u *src;
5322 int do_backslash; /* backslash is a special character */
5323 int do_special; /* recognize <> key codes */
5324 int do_key_code; /* recognize raw key codes */
5325 char_u *result; /* buffer for resulting string */
5326
5327 do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
Bram Moolenaar9c102382006-05-03 21:26:49 +00005328 do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL) || special;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5330
5331 /*
5332 * Allocate space for the translation. Worst case a single character is
5333 * replaced by 6 bytes (shifted special key), plus a NUL at the end.
5334 */
5335 result = alloc((unsigned)STRLEN(from) * 6 + 1);
5336 if (result == NULL) /* out of memory */
5337 {
5338 *bufp = NULL;
5339 return from;
5340 }
5341
5342 src = from;
5343
5344 /*
5345 * Check for #n at start only: function key n
5346 */
5347 if (from_part && src[0] == '#' && VIM_ISDIGIT(src[1])) /* function key */
5348 {
5349 result[dlen++] = K_SPECIAL;
5350 result[dlen++] = 'k';
5351 if (src[1] == '0')
5352 result[dlen++] = ';'; /* #0 is F10 is "k;" */
5353 else
5354 result[dlen++] = src[1]; /* #3 is F3 is "k3" */
5355 src += 2;
5356 }
5357
5358 /*
5359 * Copy each byte from *from to result[dlen]
5360 */
5361 while (*src != NUL)
5362 {
5363 /*
5364 * If 'cpoptions' does not contain '<', check for special key codes,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005365 * like "<C-S-LeftMouse>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 */
5367 if (do_special && (do_lt || STRNCMP(src, "<lt>", 4) != 0))
5368 {
5369#ifdef FEAT_EVAL
5370 /*
5371 * Replace <SID> by K_SNR <script-nr> _.
5372 * (room: 5 * 6 = 30 bytes; needed: 3 + <nr> + 1 <= 14)
5373 */
5374 if (STRNICMP(src, "<SID>", 5) == 0)
5375 {
5376 if (current_SID <= 0)
5377 EMSG(_(e_usingsid));
5378 else
5379 {
5380 src += 5;
5381 result[dlen++] = K_SPECIAL;
5382 result[dlen++] = (int)KS_EXTRA;
5383 result[dlen++] = (int)KE_SNR;
5384 sprintf((char *)result + dlen, "%ld", (long)current_SID);
5385 dlen += (int)STRLEN(result + dlen);
5386 result[dlen++] = '_';
5387 continue;
5388 }
5389 }
5390#endif
5391
5392 slen = trans_special(&src, result + dlen, TRUE);
5393 if (slen)
5394 {
5395 dlen += slen;
5396 continue;
5397 }
5398 }
5399
5400 /*
5401 * If 'cpoptions' does not contain 'k', see if it's an actual key-code.
5402 * Note that this is also checked after replacing the <> form.
5403 * Single character codes are NOT replaced (e.g. ^H or DEL), because
5404 * it could be a character in the file.
5405 */
5406 if (do_key_code)
5407 {
5408 i = find_term_bykeys(src);
5409 if (i >= 0)
5410 {
5411 result[dlen++] = K_SPECIAL;
5412 result[dlen++] = termcodes[i].name[0];
5413 result[dlen++] = termcodes[i].name[1];
5414 src += termcodes[i].len;
5415 /* If terminal code matched, continue after it. */
5416 continue;
5417 }
5418 }
5419
5420#ifdef FEAT_EVAL
5421 if (do_special)
5422 {
5423 char_u *p, *s, len;
5424
5425 /*
5426 * Replace <Leader> by the value of "mapleader".
5427 * Replace <LocalLeader> by the value of "maplocalleader".
5428 * If "mapleader" or "maplocalleader" isn't set use a backslash.
5429 */
5430 if (STRNICMP(src, "<Leader>", 8) == 0)
5431 {
5432 len = 8;
5433 p = get_var_value((char_u *)"g:mapleader");
5434 }
5435 else if (STRNICMP(src, "<LocalLeader>", 13) == 0)
5436 {
5437 len = 13;
5438 p = get_var_value((char_u *)"g:maplocalleader");
5439 }
5440 else
5441 {
5442 len = 0;
5443 p = NULL;
5444 }
5445 if (len != 0)
5446 {
5447 /* Allow up to 8 * 6 characters for "mapleader". */
5448 if (p == NULL || *p == NUL || STRLEN(p) > 8 * 6)
5449 s = (char_u *)"\\";
5450 else
5451 s = p;
5452 while (*s != NUL)
5453 result[dlen++] = *s++;
5454 src += len;
5455 continue;
5456 }
5457 }
5458#endif
5459
5460 /*
5461 * Remove CTRL-V and ignore the next character.
5462 * For "from" side the CTRL-V at the end is included, for the "to"
5463 * part it is removed.
5464 * If 'cpoptions' does not contain 'B', also accept a backslash.
5465 */
5466 key = *src;
5467 if (key == Ctrl_V || (do_backslash && key == '\\'))
5468 {
5469 ++src; /* skip CTRL-V or backslash */
5470 if (*src == NUL)
5471 {
5472 if (from_part)
5473 result[dlen++] = key;
5474 break;
5475 }
5476 }
5477
5478#ifdef FEAT_MBYTE
5479 /* skip multibyte char correctly */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005480 for (i = (*mb_ptr2len)(src); i > 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481#endif
5482 {
5483 /*
5484 * If the character is K_SPECIAL, replace it with K_SPECIAL
5485 * KS_SPECIAL KE_FILLER.
5486 * If compiled with the GUI replace CSI with K_CSI.
5487 */
5488 if (*src == K_SPECIAL)
5489 {
5490 result[dlen++] = K_SPECIAL;
5491 result[dlen++] = KS_SPECIAL;
5492 result[dlen++] = KE_FILLER;
5493 }
5494# ifdef FEAT_GUI
5495 else if (*src == CSI)
5496 {
5497 result[dlen++] = K_SPECIAL;
5498 result[dlen++] = KS_EXTRA;
5499 result[dlen++] = (int)KE_CSI;
5500 }
5501# endif
5502 else
5503 result[dlen++] = *src;
5504 ++src;
5505 }
5506 }
5507 result[dlen] = NUL;
5508
5509 /*
5510 * Copy the new string to allocated memory.
5511 * If this fails, just return from.
5512 */
5513 if ((*bufp = vim_strsave(result)) != NULL)
5514 from = *bufp;
5515 vim_free(result);
5516 return from;
5517}
5518
5519/*
5520 * Find a termcode with keys 'src' (must be NUL terminated).
5521 * Return the index in termcodes[], or -1 if not found.
5522 */
5523 int
5524find_term_bykeys(src)
5525 char_u *src;
5526{
5527 int i;
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005528 int slen = (int)STRLEN(src);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
5530 for (i = 0; i < tc_len; ++i)
5531 {
Bram Moolenaar5af7d712012-01-20 17:15:51 +01005532 if (slen == termcodes[i].len
5533 && STRNCMP(termcodes[i].code, src, (size_t)slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 return i;
5535 }
5536 return -1;
5537}
5538
5539/*
5540 * Gather the first characters in the terminal key codes into a string.
5541 * Used to speed up check_termcode().
5542 */
5543 static void
5544gather_termleader()
5545{
5546 int i;
5547 int len = 0;
5548
5549#ifdef FEAT_GUI
5550 if (gui.in_use)
5551 termleader[len++] = CSI; /* the GUI codes are not in termcodes[] */
5552#endif
5553#ifdef FEAT_TERMRESPONSE
5554 if (check_for_codes)
5555 termleader[len++] = DCS; /* the termcode response starts with DCS
5556 in 8-bit mode */
5557#endif
5558 termleader[len] = NUL;
5559
5560 for (i = 0; i < tc_len; ++i)
5561 if (vim_strchr(termleader, termcodes[i].code[0]) == NULL)
5562 {
5563 termleader[len++] = termcodes[i].code[0];
5564 termleader[len] = NUL;
5565 }
5566
5567 need_gather = FALSE;
5568}
5569
5570/*
5571 * Show all termcodes (for ":set termcap")
5572 * This code looks a lot like showoptions(), but is different.
5573 */
5574 void
5575show_termcodes()
5576{
5577 int col;
5578 int *items;
5579 int item_count;
5580 int run;
5581 int row, rows;
5582 int cols;
5583 int i;
5584 int len;
5585
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005586#define INC3 27 /* try to make three columns */
5587#define INC2 40 /* try to make two columns */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588#define GAP 2 /* spaces between columns */
5589
5590 if (tc_len == 0) /* no terminal codes (must be GUI) */
5591 return;
5592 items = (int *)alloc((unsigned)(sizeof(int) * tc_len));
5593 if (items == NULL)
5594 return;
5595
5596 /* Highlight title */
5597 MSG_PUTS_TITLE(_("\n--- Terminal keys ---"));
5598
5599 /*
5600 * do the loop two times:
5601 * 1. display the short items (non-strings and short strings)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005602 * 2. display the medium items (medium length strings)
5603 * 3. display the long items (remaining strings)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005605 for (run = 1; run <= 3 && !got_int; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
5607 /*
5608 * collect the items in items[]
5609 */
5610 item_count = 0;
5611 for (i = 0; i < tc_len; i++)
5612 {
5613 len = show_one_termcode(termcodes[i].name,
5614 termcodes[i].code, FALSE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005615 if (len <= INC3 - GAP ? run == 1
5616 : len <= INC2 - GAP ? run == 2
5617 : run == 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 items[item_count++] = i;
5619 }
5620
5621 /*
5622 * display the items
5623 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005624 if (run <= 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005626 cols = (Columns + GAP) / (run == 1 ? INC3 : INC2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 if (cols == 0)
5628 cols = 1;
5629 rows = (item_count + cols - 1) / cols;
5630 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005631 else /* run == 3 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 rows = item_count;
5633 for (row = 0; row < rows && !got_int; ++row)
5634 {
5635 msg_putchar('\n'); /* go to next line */
5636 if (got_int) /* 'q' typed in more */
5637 break;
5638 col = 0;
5639 for (i = row; i < item_count; i += rows)
5640 {
5641 msg_col = col; /* make columns */
5642 show_one_termcode(termcodes[items[i]].name,
5643 termcodes[items[i]].code, TRUE);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005644 if (run == 2)
5645 col += INC2;
5646 else
5647 col += INC3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
5649 out_flush();
5650 ui_breakcheck();
5651 }
5652 }
5653 vim_free(items);
5654}
5655
5656/*
5657 * Show one termcode entry.
5658 * Output goes into IObuff[]
5659 */
5660 int
5661show_one_termcode(name, code, printit)
5662 char_u *name;
5663 char_u *code;
5664 int printit;
5665{
5666 char_u *p;
5667 int len;
5668
5669 if (name[0] > '~')
5670 {
5671 IObuff[0] = ' ';
5672 IObuff[1] = ' ';
5673 IObuff[2] = ' ';
5674 IObuff[3] = ' ';
5675 }
5676 else
5677 {
5678 IObuff[0] = 't';
5679 IObuff[1] = '_';
5680 IObuff[2] = name[0];
5681 IObuff[3] = name[1];
5682 }
5683 IObuff[4] = ' ';
5684
5685 p = get_special_key_name(TERMCAP2KEY(name[0], name[1]), 0);
5686 if (p[1] != 't')
5687 STRCPY(IObuff + 5, p);
5688 else
5689 IObuff[5] = NUL;
5690 len = (int)STRLEN(IObuff);
5691 do
5692 IObuff[len++] = ' ';
5693 while (len < 17);
5694 IObuff[len] = NUL;
5695 if (code == NULL)
5696 len += 4;
5697 else
5698 len += vim_strsize(code);
5699
5700 if (printit)
5701 {
5702 msg_puts(IObuff);
5703 if (code == NULL)
5704 msg_puts((char_u *)"NULL");
5705 else
5706 msg_outtrans(code);
5707 }
5708 return len;
5709}
5710
5711#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
5712/*
5713 * For Xterm >= 140 compiled with OPT_TCAP_QUERY: Obtain the actually used
5714 * termcap codes from the terminal itself.
5715 * We get them one by one to avoid a very long response string.
5716 */
5717static int xt_index_in = 0;
5718static int xt_index_out = 0;
5719
5720 static void
5721req_codes_from_term()
5722{
5723 xt_index_out = 0;
5724 xt_index_in = 0;
5725 req_more_codes_from_term();
5726}
5727
5728 static void
5729req_more_codes_from_term()
5730{
5731 char buf[11];
5732 int old_idx = xt_index_out;
5733
5734 /* Don't do anything when going to exit. */
5735 if (exiting)
5736 return;
5737
5738 /* Send up to 10 more requests out than we received. Avoid sending too
5739 * many, there can be a buffer overflow somewhere. */
5740 while (xt_index_out < xt_index_in + 10 && key_names[xt_index_out] != NULL)
5741 {
Bram Moolenaar2951b772013-07-03 12:45:31 +02005742# ifdef DEBUG_TERMRESPONSE
5743 char dbuf[100];
5744
5745 sprintf(dbuf, "Requesting XT %d: %s",
5746 xt_index_out, key_names[xt_index_out]);
5747 log_tr(dbuf);
5748# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 sprintf(buf, "\033P+q%02x%02x\033\\",
5750 key_names[xt_index_out][0], key_names[xt_index_out][1]);
5751 out_str_nf((char_u *)buf);
5752 ++xt_index_out;
5753 }
5754
5755 /* Send the codes out right away. */
5756 if (xt_index_out != old_idx)
5757 out_flush();
5758}
5759
5760/*
5761 * Decode key code response from xterm: '<Esc>P1+r<name>=<string><Esc>\'.
5762 * A "0" instead of the "1" indicates a code that isn't supported.
5763 * Both <name> and <string> are encoded in hex.
5764 * "code" points to the "0" or "1".
5765 */
5766 static void
5767got_code_from_term(code, len)
5768 char_u *code;
5769 int len;
5770{
5771#define XT_LEN 100
5772 char_u name[3];
5773 char_u str[XT_LEN];
5774 int i;
5775 int j = 0;
5776 int c;
5777
5778 /* A '1' means the code is supported, a '0' means it isn't.
5779 * When half the length is > XT_LEN we can't use it.
5780 * Our names are currently all 2 characters. */
5781 if (code[0] == '1' && code[7] == '=' && len / 2 < XT_LEN)
5782 {
5783 /* Get the name from the response and find it in the table. */
5784 name[0] = hexhex2nr(code + 3);
5785 name[1] = hexhex2nr(code + 5);
5786 name[2] = NUL;
5787 for (i = 0; key_names[i] != NULL; ++i)
5788 {
5789 if (STRCMP(key_names[i], name) == 0)
5790 {
5791 xt_index_in = i;
5792 break;
5793 }
5794 }
Bram Moolenaar2951b772013-07-03 12:45:31 +02005795# ifdef DEBUG_TERMRESPONSE
5796 {
5797 char buf[100];
5798
5799 sprintf(buf, "Received XT %d: %s", xt_index_in, (char *)name);
5800 log_tr(buf);
5801 }
5802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 if (key_names[i] != NULL)
5804 {
5805 for (i = 8; (c = hexhex2nr(code + i)) >= 0; i += 2)
5806 str[j++] = c;
5807 str[j] = NUL;
5808 if (name[0] == 'C' && name[1] == 'o')
5809 {
5810 /* Color count is not a key code. */
5811 i = atoi((char *)str);
5812 if (i != t_colors)
5813 {
5814 /* Nr of colors changed, initialize highlighting and
Bram Moolenaar238a5642006-02-21 22:12:05 +00005815 * redraw everything. This causes a redraw, which usually
5816 * clears the message. Try keeping the message if it
5817 * might work. */
5818 set_keep_msg_from_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 set_color_count(i);
5820 init_highlight(TRUE, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +02005821#ifdef DEBUG_TERMRESPONSE
5822 {
5823 char buf[100];
5824 int r = redraw_asap(CLEAR);
5825
5826 sprintf(buf, "Received t_Co, redraw_asap(): %d", r);
5827 log_tr(buf);
5828 }
5829#else
5830 redraw_asap(CLEAR);
5831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
5833 }
5834 else
5835 {
5836 /* First delete any existing entry with the same code. */
5837 i = find_term_bykeys(str);
5838 if (i >= 0)
5839 del_termcode_idx(i);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005840 add_termcode(name, str, ATC_FROM_TERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
5842 }
5843 }
5844
5845 /* May request more codes now that we received one. */
5846 ++xt_index_in;
5847 req_more_codes_from_term();
5848}
5849
5850/*
5851 * Check if there are any unanswered requests and deal with them.
5852 * This is called before starting an external program or getting direct
5853 * keyboard input. We don't want responses to be send to that program or
5854 * handled as typed text.
5855 */
5856 static void
5857check_for_codes_from_term()
5858{
5859 int c;
5860
5861 /* If no codes requested or all are answered, no need to wait. */
5862 if (xt_index_out == 0 || xt_index_out == xt_index_in)
5863 return;
5864
5865 /* Vgetc() will check for and handle any response.
5866 * Keep calling vpeekc() until we don't get any responses. */
5867 ++no_mapping;
5868 ++allow_keys;
5869 for (;;)
5870 {
5871 c = vpeekc();
5872 if (c == NUL) /* nothing available */
5873 break;
5874
5875 /* If a response is recognized it's replaced with K_IGNORE, must read
5876 * it from the input stream. If there is no K_IGNORE we can't do
5877 * anything, break here (there might be some responses further on, but
5878 * we don't want to throw away any typed chars). */
5879 if (c != K_SPECIAL && c != K_IGNORE)
5880 break;
5881 c = vgetc();
5882 if (c != K_IGNORE)
5883 {
5884 vungetc(c);
5885 break;
5886 }
5887 }
5888 --no_mapping;
5889 --allow_keys;
5890}
5891#endif
5892
5893#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5894/*
5895 * Translate an internal mapping/abbreviation representation into the
5896 * corresponding external one recognized by :map/:abbrev commands;
5897 * respects the current B/k/< settings of 'cpoption'.
5898 *
5899 * This function is called when expanding mappings/abbreviations on the
Bram Moolenaarbfa28242009-06-16 12:31:33 +00005900 * command-line, and for building the "Ambiguous mapping..." error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 *
5902 * It uses a growarray to build the translation string since the
5903 * latter can be wider than the original description. The caller has to
5904 * free the string afterwards.
5905 *
5906 * Returns NULL when there is a problem.
5907 */
5908 char_u *
5909translate_mapping(str, expmap)
5910 char_u *str;
5911 int expmap; /* TRUE when expanding mappings on command-line */
5912{
5913 garray_T ga;
5914 int c;
5915 int modifiers;
5916 int cpo_bslash;
5917 int cpo_special;
5918 int cpo_keycode;
5919
5920 ga_init(&ga);
5921 ga.ga_itemsize = 1;
5922 ga.ga_growsize = 40;
5923
5924 cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
5925 cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
5926 cpo_keycode = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
5927
5928 for (; *str; ++str)
5929 {
5930 c = *str;
5931 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5932 {
5933 modifiers = 0;
5934 if (str[1] == KS_MODIFIER)
5935 {
5936 str++;
5937 modifiers = *++str;
5938 c = *++str;
5939 }
5940 if (cpo_special && cpo_keycode && c == K_SPECIAL && !modifiers)
5941 {
5942 int i;
5943
5944 /* try to find special key in termcodes */
5945 for (i = 0; i < tc_len; ++i)
5946 if (termcodes[i].name[0] == str[1]
5947 && termcodes[i].name[1] == str[2])
5948 break;
5949 if (i < tc_len)
5950 {
5951 ga_concat(&ga, termcodes[i].code);
5952 str += 2;
5953 continue; /* for (str) */
5954 }
5955 }
5956 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5957 {
5958 if (expmap && cpo_special)
5959 {
5960 ga_clear(&ga);
5961 return NULL;
5962 }
5963 c = TO_SPECIAL(str[1], str[2]);
5964 if (c == K_ZERO) /* display <Nul> as ^@ */
5965 c = NUL;
5966 str += 2;
5967 }
5968 if (IS_SPECIAL(c) || modifiers) /* special key */
5969 {
5970 if (expmap && cpo_special)
5971 {
5972 ga_clear(&ga);
5973 return NULL;
5974 }
5975 ga_concat(&ga, get_special_key_name(c, modifiers));
5976 continue; /* for (str) */
5977 }
5978 }
5979 if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
5980 || (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
5981 ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
5982 if (c)
5983 ga_append(&ga, c);
5984 }
5985 ga_append(&ga, NUL);
5986 return (char_u *)(ga.ga_data);
5987}
5988#endif
5989
5990#if (defined(WIN3264) && !defined(FEAT_GUI)) || defined(PROTO)
5991static char ksme_str[20];
5992static char ksmr_str[20];
5993static char ksmd_str[20];
5994
5995/*
5996 * For Win32 console: update termcap codes for existing console attributes.
5997 */
5998 void
5999update_tcap(attr)
6000 int attr;
6001{
6002 struct builtin_term *p;
6003
6004 p = find_builtin_term(DEFAULT_TERM);
6005 sprintf(ksme_str, IF_EB("\033|%dm", ESC_STR "|%dm"), attr);
6006 sprintf(ksmd_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6007 attr | 0x08); /* FOREGROUND_INTENSITY */
6008 sprintf(ksmr_str, IF_EB("\033|%dm", ESC_STR "|%dm"),
6009 ((attr & 0x0F) << 4) | ((attr & 0xF0) >> 4));
6010
6011 while (p->bt_string != NULL)
6012 {
6013 if (p->bt_entry == (int)KS_ME)
6014 p->bt_string = &ksme_str[0];
6015 else if (p->bt_entry == (int)KS_MR)
6016 p->bt_string = &ksmr_str[0];
6017 else if (p->bt_entry == (int)KS_MD)
6018 p->bt_string = &ksmd_str[0];
6019 ++p;
6020 }
6021}
6022#endif