blob: 7c9ea14ecad391f860d84f240b24149fe66345e9 [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 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
16/*
17 * Variables shared between getcmdline(), redrawcmdline() and others.
18 * These need to be saved when using CTRL-R |, that's why they are in a
19 * structure.
20 */
21struct cmdline_info
22{
23 char_u *cmdbuff; /* pointer to command line buffer */
24 int cmdbufflen; /* length of cmdbuff */
25 int cmdlen; /* number of chars in command line */
26 int cmdpos; /* current cursor position */
27 int cmdspos; /* cursor column on screen */
28 int cmdfirstc; /* ':', '/', '?', '=' or NUL */
29 int cmdindent; /* number of spaces before cmdline */
30 char_u *cmdprompt; /* message in front of cmdline */
31 int cmdattr; /* attributes for prompt */
32 int overstrike; /* Typing mode on the command line. Shared by
33 getcmdline() and put_on_cmdline(). */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000034 int xp_context; /* type of expansion */
35# ifdef FEAT_EVAL
36 char_u *xp_arg; /* user-defined expansion arg */
37 int input_fn; /* Invoked for input() function */
38# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000039};
40
41static struct cmdline_info ccline; /* current cmdline_info */
42
43static int cmd_showtail; /* Only show path tail in lists ? */
44
45#ifdef FEAT_EVAL
46static int new_cmdpos; /* position set by set_cmdline_pos() */
47#endif
48
49#ifdef FEAT_CMDHIST
50typedef struct hist_entry
51{
52 int hisnum; /* identifying number */
53 char_u *hisstr; /* actual entry, separator char after the NUL */
54} histentry_T;
55
56static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
57static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
58static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
59 /* identifying (unique) number of newest history entry */
60static int hislen = 0; /* actual length of history tables */
61
62static int hist_char2type __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
64static int in_history __ARGS((int, char_u *, int));
65# ifdef FEAT_EVAL
66static int calc_hist_idx __ARGS((int histype, int num));
67# endif
68#endif
69
70#ifdef FEAT_RIGHTLEFT
71static int cmd_hkmap = 0; /* Hebrew mapping during command line */
72#endif
73
74#ifdef FEAT_FKMAP
75static int cmd_fkmap = 0; /* Farsi mapping during command line */
76#endif
77
78static int cmdline_charsize __ARGS((int idx));
79static void set_cmdspos __ARGS((void));
80static void set_cmdspos_cursor __ARGS((void));
81#ifdef FEAT_MBYTE
82static void correct_cmdspos __ARGS((int idx, int cells));
83#endif
84static void alloc_cmdbuff __ARGS((int len));
85static int realloc_cmdbuff __ARGS((int len));
86static void draw_cmdline __ARGS((int start, int len));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +000087static void save_cmdline __ARGS((struct cmdline_info *ccp));
88static void restore_cmdline __ARGS((struct cmdline_info *ccp));
Bram Moolenaar8299df92004-07-10 09:47:34 +000089static int cmdline_paste __ARGS((int regname, int literally));
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
91static void redrawcmd_preedit __ARGS((void));
92#endif
93#ifdef FEAT_WILDMENU
94static void cmdline_del __ARGS((int from));
95#endif
96static void redrawcmdprompt __ARGS((void));
97static void cursorcmd __ARGS((void));
98static int ccheck_abbr __ARGS((int));
99static int nextwild __ARGS((expand_T *xp, int type, int options));
Bram Moolenaar45360022005-07-21 21:08:21 +0000100static void escape_fname __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101static int showmatches __ARGS((expand_T *xp, int wildmenu));
102static void set_expand_context __ARGS((expand_T *xp));
103static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int));
104static int expand_showtail __ARGS((expand_T *xp));
105#ifdef FEAT_CMDL_COMPL
106static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname));
107# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
108static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000109static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110# endif
111#endif
112
113#ifdef FEAT_CMDWIN
114static int ex_window __ARGS((void));
115#endif
116
117/*
118 * getcmdline() - accept a command line starting with firstc.
119 *
120 * firstc == ':' get ":" command line.
121 * firstc == '/' or '?' get search pattern
122 * firstc == '=' get expression
123 * firstc == '@' get text for input() function
124 * firstc == '>' get text for debug mode
125 * firstc == NUL get text for :insert command
126 * firstc == -1 like NUL, and break on CTRL-C
127 *
128 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
129 * command line.
130 *
131 * Careful: getcmdline() can be called recursively!
132 *
133 * Return pointer to allocated string if there is a commandline, NULL
134 * otherwise.
135 */
136/*ARGSUSED*/
137 char_u *
138getcmdline(firstc, count, indent)
139 int firstc;
140 long count; /* only used for incremental search */
141 int indent; /* indent for inside conditionals */
142{
143 int c;
144 int i;
145 int j;
146 int gotesc = FALSE; /* TRUE when <ESC> just typed */
147 int do_abbr; /* when TRUE check for abbr. */
148#ifdef FEAT_CMDHIST
149 char_u *lookfor = NULL; /* string to match */
150 int hiscnt; /* current history line in use */
151 int histype; /* history type to be used */
152#endif
153#ifdef FEAT_SEARCH_EXTRA
154 pos_T old_cursor;
155 colnr_T old_curswant;
156 colnr_T old_leftcol;
157 linenr_T old_topline;
158# ifdef FEAT_DIFF
159 int old_topfill;
160# endif
161 linenr_T old_botline;
162 int did_incsearch = FALSE;
163 int incsearch_postponed = FALSE;
164#endif
165 int did_wild_list = FALSE; /* did wild_list() recently */
166 int wim_index = 0; /* index in wim_flags[] */
167 int res;
168 int save_msg_scroll = msg_scroll;
169 int save_State = State; /* remember State when called */
170 int some_key_typed = FALSE; /* one of the keys was typed */
171#ifdef FEAT_MOUSE
172 /* mouse drag and release events are ignored, unless they are
173 * preceded with a mouse down event */
174 int ignore_drag_release = TRUE;
175#endif
176#ifdef FEAT_EVAL
177 int break_ctrl_c = FALSE;
178#endif
179 expand_T xpc;
180 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000181#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
182 /* Everything that may work recursively should save and restore the
183 * current command line in save_ccline. That includes update_screen(), a
184 * custom status line may invoke ":normal". */
185 struct cmdline_info save_ccline;
186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
188#ifdef FEAT_SNIFF
189 want_sniff_request = 0;
190#endif
191#ifdef FEAT_EVAL
192 if (firstc == -1)
193 {
194 firstc = NUL;
195 break_ctrl_c = TRUE;
196 }
197#endif
198#ifdef FEAT_RIGHTLEFT
199 /* start without Hebrew mapping for a command line */
200 if (firstc == ':' || firstc == '=' || firstc == '>')
201 cmd_hkmap = 0;
202#endif
203
204 ccline.overstrike = FALSE; /* always start in insert mode */
205#ifdef FEAT_SEARCH_EXTRA
206 old_cursor = curwin->w_cursor; /* needs to be restored later */
207 old_curswant = curwin->w_curswant;
208 old_leftcol = curwin->w_leftcol;
209 old_topline = curwin->w_topline;
210# ifdef FEAT_DIFF
211 old_topfill = curwin->w_topfill;
212# endif
213 old_botline = curwin->w_botline;
214#endif
215
216 /*
217 * set some variables for redrawcmd()
218 */
219 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000220 ccline.cmdindent = (firstc > 0 ? indent : 0);
221
222 /* alloc initial ccline.cmdbuff */
223 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 if (ccline.cmdbuff == NULL)
225 return NULL; /* out of memory */
226 ccline.cmdlen = ccline.cmdpos = 0;
227 ccline.cmdbuff[0] = NUL;
228
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000229 /* autoindent for :insert and :append */
230 if (firstc <= 0)
231 {
232 copy_spaces(ccline.cmdbuff, indent);
233 ccline.cmdbuff[indent] = NUL;
234 ccline.cmdpos = indent;
235 ccline.cmdspos = indent;
236 ccline.cmdlen = indent;
237 }
238
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 ExpandInit(&xpc);
240
241#ifdef FEAT_RIGHTLEFT
242 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
243 && (firstc == '/' || firstc == '?'))
244 cmdmsg_rl = TRUE;
245 else
246 cmdmsg_rl = FALSE;
247#endif
248
249 redir_off = TRUE; /* don't redirect the typed command */
250 if (!cmd_silent)
251 {
252 i = msg_scrolled;
253 msg_scrolled = 0; /* avoid wait_return message */
254 gotocmdline(TRUE);
255 msg_scrolled += i;
256 redrawcmdprompt(); /* draw prompt or indent */
257 set_cmdspos();
258 }
259 xpc.xp_context = EXPAND_NOTHING;
260 xpc.xp_backslash = XP_BS_NONE;
261
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000262#if defined(FEAT_EVAL)
263 if (ccline.input_fn)
264 {
265 xpc.xp_context = ccline.xp_context;
266 xpc.xp_pattern = ccline.cmdbuff;
267 xpc.xp_arg = ccline.xp_arg;
268 }
269#endif
270
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 /*
272 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
273 * doing ":@0" when register 0 doesn't contain a CR.
274 */
275 msg_scroll = FALSE;
276
277 State = CMDLINE;
278
279 if (firstc == '/' || firstc == '?' || firstc == '@')
280 {
281 /* Use ":lmap" mappings for search pattern and input(). */
282 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
283 b_im_ptr = &curbuf->b_p_iminsert;
284 else
285 b_im_ptr = &curbuf->b_p_imsearch;
286 if (*b_im_ptr == B_IMODE_LMAP)
287 State |= LANGMAP;
288#ifdef USE_IM_CONTROL
289 im_set_active(*b_im_ptr == B_IMODE_IM);
290#endif
291 }
292#ifdef USE_IM_CONTROL
293 else if (p_imcmdline)
294 im_set_active(TRUE);
295#endif
296
297#ifdef FEAT_MOUSE
298 setmouse();
299#endif
300#ifdef CURSOR_SHAPE
301 ui_cursor_shape(); /* may show different cursor shape */
302#endif
303
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000304 /* When inside an autocommand for writing "exiting" may be set and
305 * terminal mode set to cooked. Need to set raw mode here then. */
306 settmode(TMODE_RAW);
307
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308#ifdef FEAT_CMDHIST
309 init_history();
310 hiscnt = hislen; /* set hiscnt to impossible history value */
311 histype = hist_char2type(firstc);
312#endif
313
314#ifdef FEAT_DIGRAPHS
315 do_digraph(-1); /* init digraph typahead */
316#endif
317
318 /*
319 * Collect the command string, handling editing keys.
320 */
321 for (;;)
322 {
323#ifdef USE_ON_FLY_SCROLL
324 dont_scroll = FALSE; /* allow scrolling here */
325#endif
326 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
327
328 cursorcmd(); /* set the cursor on the right spot */
329 c = safe_vgetc();
330 if (KeyTyped)
331 {
332 some_key_typed = TRUE;
333#ifdef FEAT_RIGHTLEFT
334 if (cmd_hkmap)
335 c = hkmap(c);
336# ifdef FEAT_FKMAP
337 if (cmd_fkmap)
338 c = cmdl_fkmap(c);
339# endif
340 if (cmdmsg_rl && !KeyStuffed)
341 {
342 /* Invert horizontal movements and operations. Only when
343 * typed by the user directly, not when the result of a
344 * mapping. */
345 switch (c)
346 {
347 case K_RIGHT: c = K_LEFT; break;
348 case K_S_RIGHT: c = K_S_LEFT; break;
349 case K_C_RIGHT: c = K_C_LEFT; break;
350 case K_LEFT: c = K_RIGHT; break;
351 case K_S_LEFT: c = K_S_RIGHT; break;
352 case K_C_LEFT: c = K_C_RIGHT; break;
353 }
354 }
355#endif
356 }
357
358 /*
359 * Ignore got_int when CTRL-C was typed here.
360 * Don't ignore it in :global, we really need to break then, e.g., for
361 * ":g/pat/normal /pat" (without the <CR>).
362 * Don't ignore it for the input() function.
363 */
364 if ((c == Ctrl_C
365#ifdef UNIX
366 || c == intr_char
367#endif
368 )
369#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
370 && firstc != '@'
371#endif
372#ifdef FEAT_EVAL
373 && !break_ctrl_c
374#endif
375 && !global_busy)
376 got_int = FALSE;
377
378#ifdef FEAT_CMDHIST
379 /* free old command line when finished moving around in the history
380 * list */
381 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000382 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000383 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 && c != K_PAGEDOWN && c != K_PAGEUP
385 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000386 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
388 {
389 vim_free(lookfor);
390 lookfor = NULL;
391 }
392#endif
393
394 /*
395 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
396 */
397 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles != -1)
398 c = Ctrl_P;
399
400#ifdef FEAT_WILDMENU
401 /* Special translations for 'wildmenu' */
402 if (did_wild_list && p_wmnu)
403 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000404 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000406 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 c = Ctrl_N;
408 }
409 /* Hitting CR after "emenu Name.": complete submenu */
410 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
411 && ccline.cmdpos > 1
412 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
413 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
414 && (c == '\n' || c == '\r' || c == K_KENTER))
415 c = K_DOWN;
416#endif
417
418 /* free expanded names when finished walking through matches */
419 if (xpc.xp_numfiles != -1
420 && !(c == p_wc && KeyTyped) && c != p_wcm
421 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
422 && c != Ctrl_L)
423 {
424 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
425 did_wild_list = FALSE;
426#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000427 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428#endif
429 xpc.xp_context = EXPAND_NOTHING;
430 wim_index = 0;
431#ifdef FEAT_WILDMENU
432 if (p_wmnu && wild_menu_showing != 0)
433 {
434 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000435 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000436
437 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000438 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439
440 if (wild_menu_showing == WM_SCROLLED)
441 {
442 /* Entered command line, move it up */
443 cmdline_row--;
444 redrawcmd();
445 }
446 else if (save_p_ls != -1)
447 {
448 /* restore 'laststatus' and 'winminheight' */
449 p_ls = save_p_ls;
450 p_wmh = save_p_wmh;
451 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000452 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000454 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 redrawcmd();
456 save_p_ls = -1;
457 }
458 else
459 {
460# ifdef FEAT_VERTSPLIT
461 win_redraw_last_status(topframe);
462# else
463 lastwin->w_redr_status = TRUE;
464# endif
465 redraw_statuslines();
466 }
467 KeyTyped = skt;
468 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000469 if (ccline.input_fn)
470 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 }
472#endif
473 }
474
475#ifdef FEAT_WILDMENU
476 /* Special translations for 'wildmenu' */
477 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
478 {
479 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000480 if (ccline.cmdbuff[ccline.cmdpos - 1] == '.' && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000482 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 {
484 /* Hitting <Up>: Remove one submenu name in front of the
485 * cursor */
486 int found = FALSE;
487
488 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
489 i = 0;
490 while (--j > 0)
491 {
492 /* check for start of menu name */
493 if (ccline.cmdbuff[j] == ' '
494 && ccline.cmdbuff[j - 1] != '\\')
495 {
496 i = j + 1;
497 break;
498 }
499 /* check for start of submenu name */
500 if (ccline.cmdbuff[j] == '.'
501 && ccline.cmdbuff[j - 1] != '\\')
502 {
503 if (found)
504 {
505 i = j + 1;
506 break;
507 }
508 else
509 found = TRUE;
510 }
511 }
512 if (i > 0)
513 cmdline_del(i);
514 c = p_wc;
515 xpc.xp_context = EXPAND_NOTHING;
516 }
517 }
518 if (xpc.xp_context == EXPAND_FILES && p_wmnu)
519 {
520 char_u upseg[5];
521
522 upseg[0] = PATHSEP;
523 upseg[1] = '.';
524 upseg[2] = '.';
525 upseg[3] = PATHSEP;
526 upseg[4] = NUL;
527
528 if (ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000529 && c == K_DOWN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 && (ccline.cmdbuff[ccline.cmdpos - 2] != '.'
531 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
532 {
533 /* go down a directory */
534 c = p_wc;
535 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000536 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 {
538 /* If in a direct ancestor, strip off one ../ to go down */
539 int found = FALSE;
540
541 j = ccline.cmdpos;
542 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
543 while (--j > i)
544 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000545#ifdef FEAT_MBYTE
546 if (has_mbyte)
547 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 if (vim_ispathsep(ccline.cmdbuff[j]))
550 {
551 found = TRUE;
552 break;
553 }
554 }
555 if (found
556 && ccline.cmdbuff[j - 1] == '.'
557 && ccline.cmdbuff[j - 2] == '.'
558 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
559 {
560 cmdline_del(j - 2);
561 c = p_wc;
562 }
563 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000564 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 {
566 /* go up a directory */
567 int found = FALSE;
568
569 j = ccline.cmdpos - 1;
570 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
571 while (--j > i)
572 {
573#ifdef FEAT_MBYTE
574 if (has_mbyte)
575 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
576#endif
577 if (vim_ispathsep(ccline.cmdbuff[j])
578#ifdef BACKSLASH_IN_FILENAME
579 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
580 == NULL
581#endif
582 )
583 {
584 if (found)
585 {
586 i = j + 1;
587 break;
588 }
589 else
590 found = TRUE;
591 }
592 }
593
594 if (!found)
595 j = i;
596 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
597 j += 4;
598 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
599 && j == i)
600 j += 3;
601 else
602 j = 0;
603 if (j > 0)
604 {
605 /* TODO this is only for DOS/UNIX systems - need to put in
606 * machine-specific stuff here and in upseg init */
607 cmdline_del(j);
608 put_on_cmdline(upseg + 1, 3, FALSE);
609 }
610 else if (ccline.cmdpos > i)
611 cmdline_del(i);
612 c = p_wc;
613 }
614 }
615#if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
616 if (p_wmnu
617 && (xpc.xp_context == EXPAND_FILES
618 || xpc.xp_context == EXPAND_MENUNAMES)
619 && (c == K_UP || c == K_DOWN))
620 xpc.xp_context = EXPAND_NOTHING;
621#endif
622
623#endif /* FEAT_WILDMENU */
624
625 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
626 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
627 if (c == Ctrl_BSL)
628 {
629 ++no_mapping;
630 ++allow_keys;
631 c = safe_vgetc();
632 --no_mapping;
633 --allow_keys;
634 /* CTRL-\ e doesn't work when obtaining an expression. */
635 if (c != Ctrl_N && c != Ctrl_G
636 && (c != 'e' || ccline.cmdfirstc == '='))
637 {
638 vungetc(c);
639 c = Ctrl_BSL;
640 }
641#ifdef FEAT_EVAL
642 else if (c == 'e')
643 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000644 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646 /*
647 * Replace the command line with the result of an expression.
648 * Need to save the current command line, to be able to enter
649 * a new one...
650 */
651 if (ccline.cmdpos == ccline.cmdlen)
652 new_cmdpos = 99999; /* keep it at the end */
653 else
654 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000655
656 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000658 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 if (c == '=')
660 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000661 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 p = get_expr_line();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000663 restore_cmdline(&save_ccline);
664
665 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 {
667 ccline.cmdlen = STRLEN(p);
668 STRCPY(ccline.cmdbuff, p);
669 vim_free(p);
670
671 /* Restore the cursor or use the position set with
672 * set_cmdline_pos(). */
673 if (new_cmdpos > ccline.cmdlen)
674 ccline.cmdpos = ccline.cmdlen;
675 else
676 ccline.cmdpos = new_cmdpos;
677
678 KeyTyped = FALSE; /* Don't do p_wc completion. */
679 redrawcmd();
680 goto cmdline_changed;
681 }
682 }
683 beep_flush();
684 c = ESC;
685 }
686#endif
687 else
688 {
689 if (c == Ctrl_G && p_im && restart_edit == 0)
690 restart_edit = 'a';
691 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
692 in history */
693 goto returncmd; /* back to Normal mode */
694 }
695 }
696
697#ifdef FEAT_CMDWIN
698 if (c == cedit_key || c == K_CMDWIN)
699 {
700 /*
701 * Open a window to edit the command line (and history).
702 */
703 c = ex_window();
704 some_key_typed = TRUE;
705 }
706# ifdef FEAT_DIGRAPHS
707 else
708# endif
709#endif
710#ifdef FEAT_DIGRAPHS
711 c = do_digraph(c);
712#endif
713
714 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
715 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
716 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000717 /* In Ex mode a backslash escapes a newline. */
718 if (exmode_active
719 && c != ESC
720 && ccline.cmdpos > 0
721 && ccline.cmdpos == ccline.cmdlen
722 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000724 if (c == K_KENTER)
725 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000727 else
728 {
729 gotesc = FALSE; /* Might have typed ESC previously, don't
730 truncate the cmdline now. */
731 if (ccheck_abbr(c + ABBR_OFF))
732 goto cmdline_changed;
733 if (!cmd_silent)
734 {
735 windgoto(msg_row, 0);
736 out_flush();
737 }
738 break;
739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 }
741
742 /*
743 * Completion for 'wildchar' or 'wildcharm' key.
744 * - hitting <ESC> twice means: abandon command line.
745 * - wildcard expansion is only done when the 'wildchar' key is really
746 * typed, not when it comes from a macro
747 */
748 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
749 {
750 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
751 {
752 /* if 'wildmode' contains "list" may still need to list */
753 if (xpc.xp_numfiles > 1
754 && !did_wild_list
755 && (wim_flags[wim_index] & WIM_LIST))
756 {
757 (void)showmatches(&xpc, FALSE);
758 redrawcmd();
759 did_wild_list = TRUE;
760 }
761 if (wim_flags[wim_index] & WIM_LONGEST)
762 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
763 else if (wim_flags[wim_index] & WIM_FULL)
764 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
765 else
766 res = OK; /* don't insert 'wildchar' now */
767 }
768 else /* typed p_wc first time */
769 {
770 wim_index = 0;
771 j = ccline.cmdpos;
772 /* if 'wildmode' first contains "longest", get longest
773 * common part */
774 if (wim_flags[0] & WIM_LONGEST)
775 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
776 else
777 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
778
779 /* if interrupted while completing, behave like it failed */
780 if (got_int)
781 {
782 (void)vpeekc(); /* remove <C-C> from input stream */
783 got_int = FALSE; /* don't abandon the command line */
784 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
785#ifdef FEAT_WILDMENU
786 xpc.xp_context = EXPAND_NOTHING;
787#endif
788 goto cmdline_changed;
789 }
790
791 /* when more than one match, and 'wildmode' first contains
792 * "list", or no change and 'wildmode' contains "longest,list",
793 * list all matches */
794 if (res == OK && xpc.xp_numfiles > 1)
795 {
796 /* a "longest" that didn't do anything is skipped (but not
797 * "list:longest") */
798 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
799 wim_index = 1;
800 if ((wim_flags[wim_index] & WIM_LIST)
801#ifdef FEAT_WILDMENU
802 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
803#endif
804 )
805 {
806 if (!(wim_flags[0] & WIM_LONGEST))
807 {
808#ifdef FEAT_WILDMENU
809 int p_wmnu_save = p_wmnu;
810 p_wmnu = 0;
811#endif
812 nextwild(&xpc, WILD_PREV, 0); /* remove match */
813#ifdef FEAT_WILDMENU
814 p_wmnu = p_wmnu_save;
815#endif
816 }
817#ifdef FEAT_WILDMENU
818 (void)showmatches(&xpc, p_wmnu
819 && ((wim_flags[wim_index] & WIM_LIST) == 0));
820#else
821 (void)showmatches(&xpc, FALSE);
822#endif
823 redrawcmd();
824 did_wild_list = TRUE;
825 if (wim_flags[wim_index] & WIM_LONGEST)
826 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
827 else if (wim_flags[wim_index] & WIM_FULL)
828 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
829 }
830 else
831 vim_beep();
832 }
833#ifdef FEAT_WILDMENU
834 else if (xpc.xp_numfiles == -1)
835 xpc.xp_context = EXPAND_NOTHING;
836#endif
837 }
838 if (wim_index < 3)
839 ++wim_index;
840 if (c == ESC)
841 gotesc = TRUE;
842 if (res == OK)
843 goto cmdline_changed;
844 }
845
846 gotesc = FALSE;
847
848 /* <S-Tab> goes to last match, in a clumsy way */
849 if (c == K_S_TAB && KeyTyped)
850 {
851 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
852 && nextwild(&xpc, WILD_PREV, 0) == OK
853 && nextwild(&xpc, WILD_PREV, 0) == OK)
854 goto cmdline_changed;
855 }
856
857 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
858 c = NL;
859
860 do_abbr = TRUE; /* default: check for abbreviation */
861
862 /*
863 * Big switch for a typed command line character.
864 */
865 switch (c)
866 {
867 case K_BS:
868 case Ctrl_H:
869 case K_DEL:
870 case K_KDEL:
871 case Ctrl_W:
872#ifdef FEAT_FKMAP
873 if (cmd_fkmap && c == K_BS)
874 c = K_DEL;
875#endif
876 if (c == K_KDEL)
877 c = K_DEL;
878
879 /*
880 * delete current character is the same as backspace on next
881 * character, except at end of line
882 */
883 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
884 ++ccline.cmdpos;
885#ifdef FEAT_MBYTE
886 if (has_mbyte && c == K_DEL)
887 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
888 ccline.cmdbuff + ccline.cmdpos);
889#endif
890 if (ccline.cmdpos > 0)
891 {
892 char_u *p;
893
894 j = ccline.cmdpos;
895 p = ccline.cmdbuff + j;
896#ifdef FEAT_MBYTE
897 if (has_mbyte)
898 {
899 p = mb_prevptr(ccline.cmdbuff, p);
900 if (c == Ctrl_W)
901 {
902 while (p > ccline.cmdbuff && vim_isspace(*p))
903 p = mb_prevptr(ccline.cmdbuff, p);
904 i = mb_get_class(p);
905 while (p > ccline.cmdbuff && mb_get_class(p) == i)
906 p = mb_prevptr(ccline.cmdbuff, p);
907 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000908 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 }
910 }
911 else
912#endif
913 if (c == Ctrl_W)
914 {
915 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
916 --p;
917 i = vim_iswordc(p[-1]);
918 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
919 && vim_iswordc(p[-1]) == i)
920 --p;
921 }
922 else
923 --p;
924 ccline.cmdpos = (int)(p - ccline.cmdbuff);
925 ccline.cmdlen -= j - ccline.cmdpos;
926 i = ccline.cmdpos;
927 while (i < ccline.cmdlen)
928 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
929
930 /* Truncate at the end, required for multi-byte chars. */
931 ccline.cmdbuff[ccline.cmdlen] = NUL;
932 redrawcmd();
933 }
934 else if (ccline.cmdlen == 0 && c != Ctrl_W
935 && ccline.cmdprompt == NULL && indent == 0)
936 {
937 /* In ex and debug mode it doesn't make sense to return. */
938 if (exmode_active
939#ifdef FEAT_EVAL
940 || ccline.cmdfirstc == '>'
941#endif
942 )
943 goto cmdline_not_changed;
944
945 vim_free(ccline.cmdbuff); /* no commandline to return */
946 ccline.cmdbuff = NULL;
947 if (!cmd_silent)
948 {
949#ifdef FEAT_RIGHTLEFT
950 if (cmdmsg_rl)
951 msg_col = Columns;
952 else
953#endif
954 msg_col = 0;
955 msg_putchar(' '); /* delete ':' */
956 }
957 redraw_cmdline = TRUE;
958 goto returncmd; /* back to cmd mode */
959 }
960 goto cmdline_changed;
961
962 case K_INS:
963 case K_KINS:
964#ifdef FEAT_FKMAP
965 /* if Farsi mode set, we are in reverse insert mode -
966 Do not change the mode */
967 if (cmd_fkmap)
968 beep_flush();
969 else
970#endif
971 ccline.overstrike = !ccline.overstrike;
972#ifdef CURSOR_SHAPE
973 ui_cursor_shape(); /* may show different cursor shape */
974#endif
975 goto cmdline_not_changed;
976
977 case Ctrl_HAT:
978 if (map_to_exists_mode((char_u *)"", LANGMAP))
979 {
980 /* ":lmap" mappings exists, toggle use of mappings. */
981 State ^= LANGMAP;
982#ifdef USE_IM_CONTROL
983 im_set_active(FALSE); /* Disable input method */
984#endif
985 if (b_im_ptr != NULL)
986 {
987 if (State & LANGMAP)
988 *b_im_ptr = B_IMODE_LMAP;
989 else
990 *b_im_ptr = B_IMODE_NONE;
991 }
992 }
993#ifdef USE_IM_CONTROL
994 else
995 {
996 /* There are no ":lmap" mappings, toggle IM. When
997 * 'imdisable' is set don't try getting the status, it's
998 * always off. */
999 if ((p_imdisable && b_im_ptr != NULL)
1000 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1001 {
1002 im_set_active(FALSE); /* Disable input method */
1003 if (b_im_ptr != NULL)
1004 *b_im_ptr = B_IMODE_NONE;
1005 }
1006 else
1007 {
1008 im_set_active(TRUE); /* Enable input method */
1009 if (b_im_ptr != NULL)
1010 *b_im_ptr = B_IMODE_IM;
1011 }
1012 }
1013#endif
1014 if (b_im_ptr != NULL)
1015 {
1016 if (b_im_ptr == &curbuf->b_p_iminsert)
1017 set_iminsert_global();
1018 else
1019 set_imsearch_global();
1020 }
1021#ifdef CURSOR_SHAPE
1022 ui_cursor_shape(); /* may show different cursor shape */
1023#endif
1024#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1025 /* Show/unshow value of 'keymap' in status lines later. */
1026 status_redraw_curbuf();
1027#endif
1028 goto cmdline_not_changed;
1029
1030/* case '@': only in very old vi */
1031 case Ctrl_U:
1032 /* delete all characters left of the cursor */
1033 j = ccline.cmdpos;
1034 ccline.cmdlen -= j;
1035 i = ccline.cmdpos = 0;
1036 while (i < ccline.cmdlen)
1037 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1038 /* Truncate at the end, required for multi-byte chars. */
1039 ccline.cmdbuff[ccline.cmdlen] = NUL;
1040 redrawcmd();
1041 goto cmdline_changed;
1042
1043#ifdef FEAT_CLIPBOARD
1044 case Ctrl_Y:
1045 /* Copy the modeless selection, if there is one. */
1046 if (clip_star.state != SELECT_CLEARED)
1047 {
1048 if (clip_star.state == SELECT_DONE)
1049 clip_copy_modeless_selection(TRUE);
1050 goto cmdline_not_changed;
1051 }
1052 break;
1053#endif
1054
1055 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1056 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001057 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001058 * ":normal" runs out of characters. */
1059 if (exmode_active
1060#ifdef FEAT_EX_EXTRA
1061 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1062#endif
1063 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 goto cmdline_not_changed;
1065
1066 gotesc = TRUE; /* will free ccline.cmdbuff after
1067 putting it in history */
1068 goto returncmd; /* back to cmd mode */
1069
1070 case Ctrl_R: /* insert register */
1071#ifdef USE_ON_FLY_SCROLL
1072 dont_scroll = TRUE; /* disallow scrolling here */
1073#endif
1074 putcmdline('"', TRUE);
1075 ++no_mapping;
1076 i = c = safe_vgetc(); /* CTRL-R <char> */
1077 if (i == Ctrl_O)
1078 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1079 if (i == Ctrl_R)
1080 c = safe_vgetc(); /* CTRL-R CTRL-R <char> */
1081 --no_mapping;
1082#ifdef FEAT_EVAL
1083 /*
1084 * Insert the result of an expression.
1085 * Need to save the current command line, to be able to enter
1086 * a new one...
1087 */
1088 new_cmdpos = -1;
1089 if (c == '=')
1090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1092 {
1093 beep_flush();
1094 c = ESC;
1095 }
1096 else
1097 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001098 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001100 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 }
1102 }
1103#endif
1104 if (c != ESC) /* use ESC to cancel inserting register */
1105 {
1106 cmdline_paste(c, i == Ctrl_R);
1107 KeyTyped = FALSE; /* Don't do p_wc completion. */
1108#ifdef FEAT_EVAL
1109 if (new_cmdpos >= 0)
1110 {
1111 /* set_cmdline_pos() was used */
1112 if (new_cmdpos > ccline.cmdlen)
1113 ccline.cmdpos = ccline.cmdlen;
1114 else
1115 ccline.cmdpos = new_cmdpos;
1116 }
1117#endif
1118 }
1119 redrawcmd();
1120 goto cmdline_changed;
1121
1122 case Ctrl_D:
1123 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1124 break; /* Use ^D as normal char instead */
1125
1126 redrawcmd();
1127 continue; /* don't do incremental search now */
1128
1129 case K_RIGHT:
1130 case K_S_RIGHT:
1131 case K_C_RIGHT:
1132 do
1133 {
1134 if (ccline.cmdpos >= ccline.cmdlen)
1135 break;
1136 i = cmdline_charsize(ccline.cmdpos);
1137 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1138 break;
1139 ccline.cmdspos += i;
1140#ifdef FEAT_MBYTE
1141 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001142 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 + ccline.cmdpos);
1144 else
1145#endif
1146 ++ccline.cmdpos;
1147 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001148 while ((c == K_S_RIGHT || c == K_C_RIGHT
1149 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1151#ifdef FEAT_MBYTE
1152 if (has_mbyte)
1153 set_cmdspos_cursor();
1154#endif
1155 goto cmdline_not_changed;
1156
1157 case K_LEFT:
1158 case K_S_LEFT:
1159 case K_C_LEFT:
1160 do
1161 {
1162 if (ccline.cmdpos == 0)
1163 break;
1164 --ccline.cmdpos;
1165#ifdef FEAT_MBYTE
1166 if (has_mbyte) /* move to first byte of char */
1167 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1168 ccline.cmdbuff + ccline.cmdpos);
1169#endif
1170 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1171 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001172 while ((c == K_S_LEFT || c == K_C_LEFT
1173 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1175#ifdef FEAT_MBYTE
1176 if (has_mbyte)
1177 set_cmdspos_cursor();
1178#endif
1179 goto cmdline_not_changed;
1180
1181 case K_IGNORE:
1182 goto cmdline_not_changed; /* Ignore mouse */
1183
1184#ifdef FEAT_MOUSE
1185 case K_MIDDLEDRAG:
1186 case K_MIDDLERELEASE:
1187 goto cmdline_not_changed; /* Ignore mouse */
1188
1189 case K_MIDDLEMOUSE:
1190# ifdef FEAT_GUI
1191 /* When GUI is active, also paste when 'mouse' is empty */
1192 if (!gui.in_use)
1193# endif
1194 if (!mouse_has(MOUSE_COMMAND))
1195 goto cmdline_not_changed; /* Ignore mouse */
1196#ifdef FEAT_CLIPBOARD
1197 if (clip_star.available)
1198 cmdline_paste('*', TRUE);
1199 else
1200#endif
1201 cmdline_paste(0, TRUE);
1202 redrawcmd();
1203 goto cmdline_changed;
1204
1205#ifdef FEAT_DND
1206 case K_DROP:
1207 cmdline_paste('~', TRUE);
1208 redrawcmd();
1209 goto cmdline_changed;
1210#endif
1211
1212 case K_LEFTDRAG:
1213 case K_LEFTRELEASE:
1214 case K_RIGHTDRAG:
1215 case K_RIGHTRELEASE:
1216 /* Ignore drag and release events when the button-down wasn't
1217 * seen before. */
1218 if (ignore_drag_release)
1219 goto cmdline_not_changed;
1220 /* FALLTHROUGH */
1221 case K_LEFTMOUSE:
1222 case K_RIGHTMOUSE:
1223 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1224 ignore_drag_release = TRUE;
1225 else
1226 ignore_drag_release = FALSE;
1227# ifdef FEAT_GUI
1228 /* When GUI is active, also move when 'mouse' is empty */
1229 if (!gui.in_use)
1230# endif
1231 if (!mouse_has(MOUSE_COMMAND))
1232 goto cmdline_not_changed; /* Ignore mouse */
1233# ifdef FEAT_CLIPBOARD
1234 if (mouse_row < cmdline_row && clip_star.available)
1235 {
1236 int button, is_click, is_drag;
1237
1238 /*
1239 * Handle modeless selection.
1240 */
1241 button = get_mouse_button(KEY2TERMCAP1(c),
1242 &is_click, &is_drag);
1243 if (mouse_model_popup() && button == MOUSE_LEFT
1244 && (mod_mask & MOD_MASK_SHIFT))
1245 {
1246 /* Translate shift-left to right button. */
1247 button = MOUSE_RIGHT;
1248 mod_mask &= ~MOD_MASK_SHIFT;
1249 }
1250 clip_modeless(button, is_click, is_drag);
1251 goto cmdline_not_changed;
1252 }
1253# endif
1254
1255 set_cmdspos();
1256 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1257 ++ccline.cmdpos)
1258 {
1259 i = cmdline_charsize(ccline.cmdpos);
1260 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1261 && mouse_col < ccline.cmdspos % Columns + i)
1262 break;
1263#ifdef FEAT_MBYTE
1264 if (has_mbyte)
1265 {
1266 /* Count ">" for double-wide char that doesn't fit. */
1267 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001268 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 + ccline.cmdpos) - 1;
1270 }
1271#endif
1272 ccline.cmdspos += i;
1273 }
1274 goto cmdline_not_changed;
1275
1276 /* Mouse scroll wheel: ignored here */
1277 case K_MOUSEDOWN:
1278 case K_MOUSEUP:
1279 /* Alternate buttons ignored here */
1280 case K_X1MOUSE:
1281 case K_X1DRAG:
1282 case K_X1RELEASE:
1283 case K_X2MOUSE:
1284 case K_X2DRAG:
1285 case K_X2RELEASE:
1286 goto cmdline_not_changed;
1287
1288#endif /* FEAT_MOUSE */
1289
1290#ifdef FEAT_GUI
1291 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1292 case K_LEFTRELEASE_NM:
1293 goto cmdline_not_changed;
1294
1295 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001296 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 {
1298 gui_do_scroll();
1299 redrawcmd();
1300 }
1301 goto cmdline_not_changed;
1302
1303 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001304 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
1306 gui_do_horiz_scroll();
1307 redrawcmd();
1308 }
1309 goto cmdline_not_changed;
1310#endif
1311 case K_SELECT: /* end of Select mode mapping - ignore */
1312 goto cmdline_not_changed;
1313
1314 case Ctrl_B: /* begin of command line */
1315 case K_HOME:
1316 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 case K_S_HOME:
1318 case K_C_HOME:
1319 ccline.cmdpos = 0;
1320 set_cmdspos();
1321 goto cmdline_not_changed;
1322
1323 case Ctrl_E: /* end of command line */
1324 case K_END:
1325 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 case K_S_END:
1327 case K_C_END:
1328 ccline.cmdpos = ccline.cmdlen;
1329 set_cmdspos_cursor();
1330 goto cmdline_not_changed;
1331
1332 case Ctrl_A: /* all matches */
1333 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1334 break;
1335 goto cmdline_changed;
1336
1337 case Ctrl_L: /* longest common part */
1338 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1339 break;
1340 goto cmdline_changed;
1341
1342 case Ctrl_N: /* next match */
1343 case Ctrl_P: /* previous match */
1344 if (xpc.xp_numfiles > 0)
1345 {
1346 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1347 == FAIL)
1348 break;
1349 goto cmdline_changed;
1350 }
1351
1352#ifdef FEAT_CMDHIST
1353 case K_UP:
1354 case K_DOWN:
1355 case K_S_UP:
1356 case K_S_DOWN:
1357 case K_PAGEUP:
1358 case K_KPAGEUP:
1359 case K_PAGEDOWN:
1360 case K_KPAGEDOWN:
1361 if (hislen == 0 || firstc == NUL) /* no history */
1362 goto cmdline_not_changed;
1363
1364 i = hiscnt;
1365
1366 /* save current command string so it can be restored later */
1367 if (lookfor == NULL)
1368 {
1369 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1370 goto cmdline_not_changed;
1371 lookfor[ccline.cmdpos] = NUL;
1372 }
1373
1374 j = (int)STRLEN(lookfor);
1375 for (;;)
1376 {
1377 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001378 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001379 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
1381 if (hiscnt == hislen) /* first time */
1382 hiscnt = hisidx[histype];
1383 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1384 hiscnt = hislen - 1;
1385 else if (hiscnt != hisidx[histype] + 1)
1386 --hiscnt;
1387 else /* at top of list */
1388 {
1389 hiscnt = i;
1390 break;
1391 }
1392 }
1393 else /* one step forwards */
1394 {
1395 /* on last entry, clear the line */
1396 if (hiscnt == hisidx[histype])
1397 {
1398 hiscnt = hislen;
1399 break;
1400 }
1401
1402 /* not on a history line, nothing to do */
1403 if (hiscnt == hislen)
1404 break;
1405 if (hiscnt == hislen - 1) /* wrap around */
1406 hiscnt = 0;
1407 else
1408 ++hiscnt;
1409 }
1410 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1411 {
1412 hiscnt = i;
1413 break;
1414 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001415 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001416 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 || STRNCMP(history[histype][hiscnt].hisstr,
1418 lookfor, (size_t)j) == 0)
1419 break;
1420 }
1421
1422 if (hiscnt != i) /* jumped to other entry */
1423 {
1424 char_u *p;
1425 int len;
1426 int old_firstc;
1427
1428 vim_free(ccline.cmdbuff);
1429 if (hiscnt == hislen)
1430 p = lookfor; /* back to the old one */
1431 else
1432 p = history[histype][hiscnt].hisstr;
1433
1434 if (histype == HIST_SEARCH
1435 && p != lookfor
1436 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1437 {
1438 /* Correct for the separator character used when
1439 * adding the history entry vs the one used now.
1440 * First loop: count length.
1441 * Second loop: copy the characters. */
1442 for (i = 0; i <= 1; ++i)
1443 {
1444 len = 0;
1445 for (j = 0; p[j] != NUL; ++j)
1446 {
1447 /* Replace old sep with new sep, unless it is
1448 * escaped. */
1449 if (p[j] == old_firstc
1450 && (j == 0 || p[j - 1] != '\\'))
1451 {
1452 if (i > 0)
1453 ccline.cmdbuff[len] = firstc;
1454 }
1455 else
1456 {
1457 /* Escape new sep, unless it is already
1458 * escaped. */
1459 if (p[j] == firstc
1460 && (j == 0 || p[j - 1] != '\\'))
1461 {
1462 if (i > 0)
1463 ccline.cmdbuff[len] = '\\';
1464 ++len;
1465 }
1466 if (i > 0)
1467 ccline.cmdbuff[len] = p[j];
1468 }
1469 ++len;
1470 }
1471 if (i == 0)
1472 {
1473 alloc_cmdbuff(len);
1474 if (ccline.cmdbuff == NULL)
1475 goto returncmd;
1476 }
1477 }
1478 ccline.cmdbuff[len] = NUL;
1479 }
1480 else
1481 {
1482 alloc_cmdbuff((int)STRLEN(p));
1483 if (ccline.cmdbuff == NULL)
1484 goto returncmd;
1485 STRCPY(ccline.cmdbuff, p);
1486 }
1487
1488 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1489 redrawcmd();
1490 goto cmdline_changed;
1491 }
1492 beep_flush();
1493 goto cmdline_not_changed;
1494#endif
1495
1496 case Ctrl_V:
1497 case Ctrl_Q:
1498#ifdef FEAT_MOUSE
1499 ignore_drag_release = TRUE;
1500#endif
1501 putcmdline('^', TRUE);
1502 c = get_literal(); /* get next (two) character(s) */
1503 do_abbr = FALSE; /* don't do abbreviation now */
1504#ifdef FEAT_MBYTE
1505 /* may need to remove ^ when composing char was typed */
1506 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1507 {
1508 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1509 msg_putchar(' ');
1510 cursorcmd();
1511 }
1512#endif
1513 break;
1514
1515#ifdef FEAT_DIGRAPHS
1516 case Ctrl_K:
1517#ifdef FEAT_MOUSE
1518 ignore_drag_release = TRUE;
1519#endif
1520 putcmdline('?', TRUE);
1521#ifdef USE_ON_FLY_SCROLL
1522 dont_scroll = TRUE; /* disallow scrolling here */
1523#endif
1524 c = get_digraph(TRUE);
1525 if (c != NUL)
1526 break;
1527
1528 redrawcmd();
1529 goto cmdline_not_changed;
1530#endif /* FEAT_DIGRAPHS */
1531
1532#ifdef FEAT_RIGHTLEFT
1533 case Ctrl__: /* CTRL-_: switch language mode */
1534 if (!p_ari)
1535 break;
1536#ifdef FEAT_FKMAP
1537 if (p_altkeymap)
1538 {
1539 cmd_fkmap = !cmd_fkmap;
1540 if (cmd_fkmap) /* in Farsi always in Insert mode */
1541 ccline.overstrike = FALSE;
1542 }
1543 else /* Hebrew is default */
1544#endif
1545 cmd_hkmap = !cmd_hkmap;
1546 goto cmdline_not_changed;
1547#endif
1548
1549 default:
1550#ifdef UNIX
1551 if (c == intr_char)
1552 {
1553 gotesc = TRUE; /* will free ccline.cmdbuff after
1554 putting it in history */
1555 goto returncmd; /* back to Normal mode */
1556 }
1557#endif
1558 /*
1559 * Normal character with no special meaning. Just set mod_mask
1560 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1561 * the string <S-Space>. This should only happen after ^V.
1562 */
1563 if (!IS_SPECIAL(c))
1564 mod_mask = 0x0;
1565 break;
1566 }
1567 /*
1568 * End of switch on command line character.
1569 * We come here if we have a normal character.
1570 */
1571
1572 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1573#ifdef FEAT_MBYTE
1574 /* Add ABBR_OFF for characters above 0x100, this is
1575 * what check_abbr() expects. */
1576 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1577#endif
1578 c))
1579 goto cmdline_changed;
1580
1581 /*
1582 * put the character in the command line
1583 */
1584 if (IS_SPECIAL(c) || mod_mask != 0)
1585 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1586 else
1587 {
1588#ifdef FEAT_MBYTE
1589 if (has_mbyte)
1590 {
1591 j = (*mb_char2bytes)(c, IObuff);
1592 IObuff[j] = NUL; /* exclude composing chars */
1593 put_on_cmdline(IObuff, j, TRUE);
1594 }
1595 else
1596#endif
1597 {
1598 IObuff[0] = c;
1599 put_on_cmdline(IObuff, 1, TRUE);
1600 }
1601 }
1602 goto cmdline_changed;
1603
1604/*
1605 * This part implements incremental searches for "/" and "?"
1606 * Jump to cmdline_not_changed when a character has been read but the command
1607 * line did not change. Then we only search and redraw if something changed in
1608 * the past.
1609 * Jump to cmdline_changed when the command line did change.
1610 * (Sorry for the goto's, I know it is ugly).
1611 */
1612cmdline_not_changed:
1613#ifdef FEAT_SEARCH_EXTRA
1614 if (!incsearch_postponed)
1615 continue;
1616#endif
1617
1618cmdline_changed:
1619#ifdef FEAT_SEARCH_EXTRA
1620 /*
1621 * 'incsearch' highlighting.
1622 */
1623 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1624 {
1625 /* if there is a character waiting, search and redraw later */
1626 if (char_avail())
1627 {
1628 incsearch_postponed = TRUE;
1629 continue;
1630 }
1631 incsearch_postponed = FALSE;
1632 curwin->w_cursor = old_cursor; /* start at old position */
1633
1634 /* If there is no command line, don't do anything */
1635 if (ccline.cmdlen == 0)
1636 i = 0;
1637 else
1638 {
1639 cursor_off(); /* so the user knows we're busy */
1640 out_flush();
1641 ++emsg_off; /* So it doesn't beep if bad expr */
1642 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1643 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
1644 --emsg_off;
1645 /* if interrupted while searching, behave like it failed */
1646 if (got_int)
1647 {
1648 (void)vpeekc(); /* remove <C-C> from input stream */
1649 got_int = FALSE; /* don't abandon the command line */
1650 i = 0;
1651 }
1652 else if (char_avail())
1653 /* cancelled searching because a char was typed */
1654 incsearch_postponed = TRUE;
1655 }
1656 if (i)
1657 highlight_match = TRUE; /* highlight position */
1658 else
1659 highlight_match = FALSE; /* remove highlight */
1660
1661 /* first restore the old curwin values, so the screen is
1662 * positioned in the same way as the actual search command */
1663 curwin->w_leftcol = old_leftcol;
1664 curwin->w_topline = old_topline;
1665# ifdef FEAT_DIFF
1666 curwin->w_topfill = old_topfill;
1667# endif
1668 curwin->w_botline = old_botline;
1669 changed_cline_bef_curs();
1670 update_topline();
1671
1672 if (i != 0)
1673 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001674 pos_T save_pos = curwin->w_cursor;
1675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 /*
1677 * First move cursor to end of match, then to start. This
1678 * moves the whole match onto the screen when 'nowrap' is set.
1679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 curwin->w_cursor.lnum += search_match_lines;
1681 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001682 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1683 {
1684 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1685 coladvance((colnr_T)MAXCOL);
1686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 validate_cursor();
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001688 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 }
1690 validate_cursor();
1691
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001692 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 update_screen(NOT_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001694 restore_cmdline(&save_ccline);
1695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 msg_starthere();
1697 redrawcmdline();
1698 did_incsearch = TRUE;
1699 }
1700#else /* FEAT_SEARCH_EXTRA */
1701 ;
1702#endif
1703
1704#ifdef FEAT_RIGHTLEFT
1705 if (cmdmsg_rl
1706# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001707 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708# endif
1709 )
1710 /* Always redraw the whole command line to fix shaping and
1711 * right-left typing. Not efficient, but it works. */
1712 redrawcmd();
1713#endif
1714 }
1715
1716returncmd:
1717
1718#ifdef FEAT_RIGHTLEFT
1719 cmdmsg_rl = FALSE;
1720#endif
1721
1722#ifdef FEAT_FKMAP
1723 cmd_fkmap = 0;
1724#endif
1725
1726 ExpandCleanup(&xpc);
1727
1728#ifdef FEAT_SEARCH_EXTRA
1729 if (did_incsearch)
1730 {
1731 curwin->w_cursor = old_cursor;
1732 curwin->w_curswant = old_curswant;
1733 curwin->w_leftcol = old_leftcol;
1734 curwin->w_topline = old_topline;
1735# ifdef FEAT_DIFF
1736 curwin->w_topfill = old_topfill;
1737# endif
1738 curwin->w_botline = old_botline;
1739 highlight_match = FALSE;
1740 validate_cursor(); /* needed for TAB */
1741 redraw_later(NOT_VALID);
1742 }
1743#endif
1744
1745 if (ccline.cmdbuff != NULL)
1746 {
1747 /*
1748 * Put line in history buffer (":" and "=" only when it was typed).
1749 */
1750#ifdef FEAT_CMDHIST
1751 if (ccline.cmdlen && firstc != NUL
1752 && (some_key_typed || histype == HIST_SEARCH))
1753 {
1754 add_to_history(histype, ccline.cmdbuff, TRUE,
1755 histype == HIST_SEARCH ? firstc : NUL);
1756 if (firstc == ':')
1757 {
1758 vim_free(new_last_cmdline);
1759 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1760 }
1761 }
1762#endif
1763
1764 if (gotesc) /* abandon command line */
1765 {
1766 vim_free(ccline.cmdbuff);
1767 ccline.cmdbuff = NULL;
1768 if (msg_scrolled == 0)
1769 compute_cmdrow();
1770 MSG("");
1771 redraw_cmdline = TRUE;
1772 }
1773 }
1774
1775 /*
1776 * If the screen was shifted up, redraw the whole screen (later).
1777 * If the line is too long, clear it, so ruler and shown command do
1778 * not get printed in the middle of it.
1779 */
1780 msg_check();
1781 msg_scroll = save_msg_scroll;
1782 redir_off = FALSE;
1783
1784 /* When the command line was typed, no need for a wait-return prompt. */
1785 if (some_key_typed)
1786 need_wait_return = FALSE;
1787
1788 State = save_State;
1789#ifdef USE_IM_CONTROL
1790 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1791 im_save_status(b_im_ptr);
1792 im_set_active(FALSE);
1793#endif
1794#ifdef FEAT_MOUSE
1795 setmouse();
1796#endif
1797#ifdef CURSOR_SHAPE
1798 ui_cursor_shape(); /* may show different cursor shape */
1799#endif
1800
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001801 {
1802 char_u *p = ccline.cmdbuff;
1803
1804 /* Make ccline empty, getcmdline() may try to use it. */
1805 ccline.cmdbuff = NULL;
1806 return p;
1807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808}
1809
1810#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1811/*
1812 * Get a command line with a prompt.
1813 * This is prepared to be called recursively from getcmdline() (e.g. by
1814 * f_input() when evaluating an expression from CTRL-R =).
1815 * Returns the command line in allocated memory, or NULL.
1816 */
1817 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001818getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 int firstc;
1820 char_u *prompt; /* command line prompt */
1821 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001822 int xp_context; /* type of expansion */
1823 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
1825 char_u *s;
1826 struct cmdline_info save_ccline;
1827 int msg_col_save = msg_col;
1828
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001829 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 ccline.cmdprompt = prompt;
1831 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001832# ifdef FEAT_EVAL
1833 ccline.xp_context = xp_context;
1834 ccline.xp_arg = xp_arg;
1835 ccline.input_fn = (firstc == '@');
1836# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001838 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 /* Restore msg_col, the prompt from input() may have changed it. */
1840 msg_col = msg_col_save;
1841
1842 return s;
1843}
1844#endif
1845
1846 static int
1847cmdline_charsize(idx)
1848 int idx;
1849{
1850#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1851 if (cmdline_star > 0) /* showing '*', always 1 position */
1852 return 1;
1853#endif
1854 return ptr2cells(ccline.cmdbuff + idx);
1855}
1856
1857/*
1858 * Compute the offset of the cursor on the command line for the prompt and
1859 * indent.
1860 */
1861 static void
1862set_cmdspos()
1863{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001864 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 ccline.cmdspos = 1 + ccline.cmdindent;
1866 else
1867 ccline.cmdspos = 0 + ccline.cmdindent;
1868}
1869
1870/*
1871 * Compute the screen position for the cursor on the command line.
1872 */
1873 static void
1874set_cmdspos_cursor()
1875{
1876 int i, m, c;
1877
1878 set_cmdspos();
1879 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001880 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001882 if (m < 0) /* overflow, Columns or Rows at weird value */
1883 m = MAXCOL;
1884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 else
1886 m = MAXCOL;
1887 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
1888 {
1889 c = cmdline_charsize(i);
1890#ifdef FEAT_MBYTE
1891 /* Count ">" for double-wide multi-byte char that doesn't fit. */
1892 if (has_mbyte)
1893 correct_cmdspos(i, c);
1894#endif
1895 /* If the cmdline doesn't fit, put cursor on last visible char. */
1896 if ((ccline.cmdspos += c) >= m)
1897 {
1898 ccline.cmdpos = i - 1;
1899 ccline.cmdspos -= c;
1900 break;
1901 }
1902#ifdef FEAT_MBYTE
1903 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001904 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905#endif
1906 }
1907}
1908
1909#ifdef FEAT_MBYTE
1910/*
1911 * Check if the character at "idx", which is "cells" wide, is a multi-byte
1912 * character that doesn't fit, so that a ">" must be displayed.
1913 */
1914 static void
1915correct_cmdspos(idx, cells)
1916 int idx;
1917 int cells;
1918{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001919 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
1921 && ccline.cmdspos % Columns + cells > Columns)
1922 ccline.cmdspos++;
1923}
1924#endif
1925
1926/*
1927 * Get an Ex command line for the ":" command.
1928 */
1929/* ARGSUSED */
1930 char_u *
1931getexline(c, dummy, indent)
1932 int c; /* normally ':', NUL for ":append" */
1933 void *dummy; /* cookie not used */
1934 int indent; /* indent for inside conditionals */
1935{
1936 /* When executing a register, remove ':' that's in front of each line. */
1937 if (exec_from_reg && vpeekc() == ':')
1938 (void)vgetc();
1939 return getcmdline(c, 1L, indent);
1940}
1941
1942/*
1943 * Get an Ex command line for Ex mode.
1944 * In Ex mode we only use the OS supplied line editing features and no
1945 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001946 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 */
1948/* ARGSUSED */
1949 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001950getexmodeline(promptc, dummy, indent)
1951 int promptc; /* normally ':', NUL for ":append" and '?' for
1952 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 void *dummy; /* cookie not used */
1954 int indent; /* indent for inside conditionals */
1955{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001956 garray_T line_ga;
1957 char_u *pend;
1958 int startcol = 0;
1959 int c1;
1960 int escaped = FALSE; /* CTRL-V typed */
1961 int vcol = 0;
1962 char_u *p;
1963 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964
1965 /* Switch cursor on now. This avoids that it happens after the "\n", which
1966 * confuses the system function that computes tabstops. */
1967 cursor_on();
1968
1969 /* always start in column 0; write a newline if necessary */
1970 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001971 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001973 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001975 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001976 if (p_prompt)
1977 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 while (indent-- > 0)
1979 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982
1983 ga_init2(&line_ga, 1, 30);
1984
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001985 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001986 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001987 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001988 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001989 while (indent >= 8)
1990 {
1991 ga_append(&line_ga, TAB);
1992 msg_puts((char_u *)" ");
1993 indent -= 8;
1994 }
1995 while (indent-- > 0)
1996 {
1997 ga_append(&line_ga, ' ');
1998 msg_putchar(' ');
1999 }
2000 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002001 ++no_mapping;
2002 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002003
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 /*
2005 * Get the line, one character at a time.
2006 */
2007 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002008 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
2010 if (ga_grow(&line_ga, 40) == FAIL)
2011 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002012 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002014 /* Get one character at a time. Don't use inchar(), it can't handle
2015 * special characters. */
2016 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017
2018 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002019 * Handle line editing.
2020 * Previously this was left to the system, putting the terminal in
2021 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002023 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002025 msg_putchar('\n');
2026 break;
2027 }
2028
2029 if (!escaped)
2030 {
2031 /* CR typed means "enter", which is NL */
2032 if (c1 == '\r')
2033 c1 = '\n';
2034
2035 if (c1 == BS || c1 == K_BS
2036 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002038 if (line_ga.ga_len > 0)
2039 {
2040 --line_ga.ga_len;
2041 goto redraw;
2042 }
2043 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
2045
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002046 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002048 msg_col = startcol;
2049 msg_clr_eos();
2050 line_ga.ga_len = 0;
2051 continue;
2052 }
2053
2054 if (c1 == Ctrl_T)
2055 {
2056 p = (char_u *)line_ga.ga_data;
2057 p[line_ga.ga_len] = NUL;
2058 indent = get_indent_str(p, 8);
2059 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2060add_indent:
2061 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002063 char_u *s = skipwhite(p);
2064
2065 ga_grow(&line_ga, 1);
2066 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2067 *s = ' ';
2068 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002070redraw:
2071 /* redraw the line */
2072 msg_col = startcol;
2073 windgoto(msg_row, msg_col);
2074 vcol = 0;
2075 for (p = (char_u *)line_ga.ga_data;
2076 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002078 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002080 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002082 msg_putchar(' ');
2083 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002085 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002087 msg_outtrans_len(p, 1);
2088 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 }
2090 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002091 msg_clr_eos();
2092 continue;
2093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002095 if (c1 == Ctrl_D)
2096 {
2097 /* Delete one shiftwidth. */
2098 p = (char_u *)line_ga.ga_data;
2099 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002101 if (prev_char == '^')
2102 ex_keep_indent = TRUE;
2103 indent = 0;
2104 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
2106 else
2107 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002108 p[line_ga.ga_len] = NUL;
2109 indent = get_indent_str(p, 8);
2110 --indent;
2111 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002113 while (get_indent_str(p, 8) > indent)
2114 {
2115 char_u *s = skipwhite(p);
2116
2117 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2118 --line_ga.ga_len;
2119 }
2120 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002122
2123 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2124 {
2125 escaped = TRUE;
2126 continue;
2127 }
2128
2129 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2130 if (IS_SPECIAL(c1))
2131 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002133
2134 if (IS_SPECIAL(c1))
2135 c1 = '?';
2136 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2137 prev_char = c1;
2138 if (c1 == '\n')
2139 msg_putchar('\n');
2140 else if (c1 == TAB)
2141 {
2142 /* Don't use chartabsize(), 'ts' can be different */
2143 do
2144 {
2145 msg_putchar(' ');
2146 } while (++vcol % 8);
2147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002150 msg_outtrans_len(
2151 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2152 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002154 ++line_ga.ga_len;
2155 escaped = FALSE;
2156
2157 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002158 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002159
2160 /* we are done when a NL is entered, but not when it comes after a
2161 * backslash */
2162 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2163 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002166 --pend;
2167 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002168 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
2170 }
2171
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002172 --no_mapping;
2173 --allow_keys;
2174
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 /* make following messages go to the next line */
2176 msg_didout = FALSE;
2177 msg_col = 0;
2178 if (msg_row < Rows - 1)
2179 ++msg_row;
2180 emsg_on_display = FALSE; /* don't want ui_delay() */
2181
2182 if (got_int)
2183 ga_clear(&line_ga);
2184
2185 return (char_u *)line_ga.ga_data;
2186}
2187
Bram Moolenaare344bea2005-09-01 20:46:49 +00002188# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2189 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190/*
2191 * Return TRUE if ccline.overstrike is on.
2192 */
2193 int
2194cmdline_overstrike()
2195{
2196 return ccline.overstrike;
2197}
2198
2199/*
2200 * Return TRUE if the cursor is at the end of the cmdline.
2201 */
2202 int
2203cmdline_at_end()
2204{
2205 return (ccline.cmdpos >= ccline.cmdlen);
2206}
2207#endif
2208
Bram Moolenaar9372a112005-12-06 19:59:18 +00002209#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210/*
2211 * Return the virtual column number at the current cursor position.
2212 * This is used by the IM code to obtain the start of the preedit string.
2213 */
2214 colnr_T
2215cmdline_getvcol_cursor()
2216{
2217 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2218 return MAXCOL;
2219
2220# ifdef FEAT_MBYTE
2221 if (has_mbyte)
2222 {
2223 colnr_T col;
2224 int i = 0;
2225
2226 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002227 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
2229 return col;
2230 }
2231 else
2232# endif
2233 return ccline.cmdpos;
2234}
2235#endif
2236
2237#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2238/*
2239 * If part of the command line is an IM preedit string, redraw it with
2240 * IM feedback attributes. The cursor position is restored after drawing.
2241 */
2242 static void
2243redrawcmd_preedit()
2244{
2245 if ((State & CMDLINE)
2246 && xic != NULL
2247 && im_get_status()
2248 && !p_imdisable
2249 && im_is_preediting())
2250 {
2251 int cmdpos = 0;
2252 int cmdspos;
2253 int old_row;
2254 int old_col;
2255 colnr_T col;
2256
2257 old_row = msg_row;
2258 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002259 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260
2261# ifdef FEAT_MBYTE
2262 if (has_mbyte)
2263 {
2264 for (col = 0; col < preedit_start_col
2265 && cmdpos < ccline.cmdlen; ++col)
2266 {
2267 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002268 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 }
2270 }
2271 else
2272# endif
2273 {
2274 cmdspos += preedit_start_col;
2275 cmdpos += preedit_start_col;
2276 }
2277
2278 msg_row = cmdline_row + (cmdspos / (int)Columns);
2279 msg_col = cmdspos % (int)Columns;
2280 if (msg_row >= Rows)
2281 msg_row = Rows - 1;
2282
2283 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2284 {
2285 int char_len;
2286 int char_attr;
2287
2288 char_attr = im_get_feedback_attr(col);
2289 if (char_attr < 0)
2290 break; /* end of preedit string */
2291
2292# ifdef FEAT_MBYTE
2293 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002294 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 else
2296# endif
2297 char_len = 1;
2298
2299 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2300 cmdpos += char_len;
2301 }
2302
2303 msg_row = old_row;
2304 msg_col = old_col;
2305 }
2306}
2307#endif /* FEAT_XIM && FEAT_GUI_GTK */
2308
2309/*
2310 * Allocate a new command line buffer.
2311 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2312 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2313 */
2314 static void
2315alloc_cmdbuff(len)
2316 int len;
2317{
2318 /*
2319 * give some extra space to avoid having to allocate all the time
2320 */
2321 if (len < 80)
2322 len = 100;
2323 else
2324 len += 20;
2325
2326 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2327 ccline.cmdbufflen = len;
2328}
2329
2330/*
2331 * Re-allocate the command line to length len + something extra.
2332 * return FAIL for failure, OK otherwise
2333 */
2334 static int
2335realloc_cmdbuff(len)
2336 int len;
2337{
2338 char_u *p;
2339
2340 p = ccline.cmdbuff;
2341 alloc_cmdbuff(len); /* will get some more */
2342 if (ccline.cmdbuff == NULL) /* out of memory */
2343 {
2344 ccline.cmdbuff = p; /* keep the old one */
2345 return FAIL;
2346 }
2347 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2348 vim_free(p);
2349 return OK;
2350}
2351
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002352#if defined(FEAT_ARABIC) || defined(PROTO)
2353static char_u *arshape_buf = NULL;
2354
2355# if defined(EXITFREE) || defined(PROTO)
2356 void
2357free_cmdline_buf()
2358{
2359 vim_free(arshape_buf);
2360}
2361# endif
2362#endif
2363
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364/*
2365 * Draw part of the cmdline at the current cursor position. But draw stars
2366 * when cmdline_star is TRUE.
2367 */
2368 static void
2369draw_cmdline(start, len)
2370 int start;
2371 int len;
2372{
2373#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2374 int i;
2375
2376 if (cmdline_star > 0)
2377 for (i = 0; i < len; ++i)
2378 {
2379 msg_putchar('*');
2380# ifdef FEAT_MBYTE
2381 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002382 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383# endif
2384 }
2385 else
2386#endif
2387#ifdef FEAT_ARABIC
2388 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 static int buflen = 0;
2391 char_u *p;
2392 int j;
2393 int newlen = 0;
2394 int mb_l;
2395 int pc, pc1;
2396 int prev_c = 0;
2397 int prev_c1 = 0;
2398 int u8c, u8c_c1, u8c_c2;
2399 int nc = 0;
2400 int dummy;
2401
2402 /*
2403 * Do arabic shaping into a temporary buffer. This is very
2404 * inefficient!
2405 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002406 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
2408 /* Re-allocate the buffer. We keep it around to avoid a lot of
2409 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002410 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002411 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002412 arshape_buf = alloc(buflen);
2413 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 return; /* out of memory */
2415 }
2416
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002417 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2418 {
2419 /* Prepend a space to draw the leading composing char on. */
2420 arshape_buf[0] = ' ';
2421 newlen = 1;
2422 }
2423
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 for (j = start; j < start + len; j += mb_l)
2425 {
2426 p = ccline.cmdbuff + j;
2427 u8c = utfc_ptr2char_len(p, &u8c_c1, &u8c_c2, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002428 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 if (ARABIC_CHAR(u8c))
2430 {
2431 /* Do Arabic shaping. */
2432 if (cmdmsg_rl)
2433 {
2434 /* displaying from right to left */
2435 pc = prev_c;
2436 pc1 = prev_c1;
2437 prev_c1 = u8c_c1;
2438 if (j + mb_l >= start + len)
2439 nc = NUL;
2440 else
2441 nc = utf_ptr2char(p + mb_l);
2442 }
2443 else
2444 {
2445 /* displaying from left to right */
2446 if (j + mb_l >= start + len)
2447 pc = NUL;
2448 else
2449 pc = utfc_ptr2char_len(p + mb_l, &pc1, &dummy,
2450 start + len - j - mb_l);
2451 nc = prev_c;
2452 }
2453 prev_c = u8c;
2454
2455 u8c = arabic_shape(u8c, NULL, &u8c_c1, pc, pc1, nc);
2456
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002457 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 if (u8c_c1 != 0)
2459 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002460 newlen += (*mb_char2bytes)(u8c_c1, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 if (u8c_c2 != 0)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002462 newlen += (*mb_char2bytes)(u8c_c2,
2463 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
2465 }
2466 else
2467 {
2468 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002469 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 newlen += mb_l;
2471 }
2472 }
2473
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002474 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 }
2476 else
2477#endif
2478 msg_outtrans_len(ccline.cmdbuff + start, len);
2479}
2480
2481/*
2482 * Put a character on the command line. Shifts the following text to the
2483 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2484 * "c" must be printable (fit in one display cell)!
2485 */
2486 void
2487putcmdline(c, shift)
2488 int c;
2489 int shift;
2490{
2491 if (cmd_silent)
2492 return;
2493 msg_no_more = TRUE;
2494 msg_putchar(c);
2495 if (shift)
2496 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2497 msg_no_more = FALSE;
2498 cursorcmd();
2499}
2500
2501/*
2502 * Undo a putcmdline(c, FALSE).
2503 */
2504 void
2505unputcmdline()
2506{
2507 if (cmd_silent)
2508 return;
2509 msg_no_more = TRUE;
2510 if (ccline.cmdlen == ccline.cmdpos)
2511 msg_putchar(' ');
2512 else
2513 draw_cmdline(ccline.cmdpos, 1);
2514 msg_no_more = FALSE;
2515 cursorcmd();
2516}
2517
2518/*
2519 * Put the given string, of the given length, onto the command line.
2520 * If len is -1, then STRLEN() is used to calculate the length.
2521 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2522 * part will be redrawn, otherwise it will not. If this function is called
2523 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2524 * called afterwards.
2525 */
2526 int
2527put_on_cmdline(str, len, redraw)
2528 char_u *str;
2529 int len;
2530 int redraw;
2531{
2532 int retval;
2533 int i;
2534 int m;
2535 int c;
2536
2537 if (len < 0)
2538 len = (int)STRLEN(str);
2539
2540 /* Check if ccline.cmdbuff needs to be longer */
2541 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2542 retval = realloc_cmdbuff(ccline.cmdlen + len);
2543 else
2544 retval = OK;
2545 if (retval == OK)
2546 {
2547 if (!ccline.overstrike)
2548 {
2549 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2550 ccline.cmdbuff + ccline.cmdpos,
2551 (size_t)(ccline.cmdlen - ccline.cmdpos));
2552 ccline.cmdlen += len;
2553 }
2554 else
2555 {
2556#ifdef FEAT_MBYTE
2557 if (has_mbyte)
2558 {
2559 /* Count nr of characters in the new string. */
2560 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002561 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 ++m;
2563 /* Count nr of bytes in cmdline that are overwritten by these
2564 * characters. */
2565 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002566 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 --m;
2568 if (i < ccline.cmdlen)
2569 {
2570 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2571 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2572 ccline.cmdlen += ccline.cmdpos + len - i;
2573 }
2574 else
2575 ccline.cmdlen = ccline.cmdpos + len;
2576 }
2577 else
2578#endif
2579 if (ccline.cmdpos + len > ccline.cmdlen)
2580 ccline.cmdlen = ccline.cmdpos + len;
2581 }
2582 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2583 ccline.cmdbuff[ccline.cmdlen] = NUL;
2584
2585#ifdef FEAT_MBYTE
2586 if (enc_utf8)
2587 {
2588 /* When the inserted text starts with a composing character,
2589 * backup to the character before it. There could be two of them.
2590 */
2591 i = 0;
2592 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2593 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2594 {
2595 i = (*mb_head_off)(ccline.cmdbuff,
2596 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2597 ccline.cmdpos -= i;
2598 len += i;
2599 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2600 }
2601# ifdef FEAT_ARABIC
2602 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2603 {
2604 /* Check the previous character for Arabic combining pair. */
2605 i = (*mb_head_off)(ccline.cmdbuff,
2606 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2607 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2608 + ccline.cmdpos - i), c))
2609 {
2610 ccline.cmdpos -= i;
2611 len += i;
2612 }
2613 else
2614 i = 0;
2615 }
2616# endif
2617 if (i != 0)
2618 {
2619 /* Also backup the cursor position. */
2620 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2621 ccline.cmdspos -= i;
2622 msg_col -= i;
2623 if (msg_col < 0)
2624 {
2625 msg_col += Columns;
2626 --msg_row;
2627 }
2628 }
2629 }
2630#endif
2631
2632 if (redraw && !cmd_silent)
2633 {
2634 msg_no_more = TRUE;
2635 i = cmdline_row;
2636 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2637 /* Avoid clearing the rest of the line too often. */
2638 if (cmdline_row != i || ccline.overstrike)
2639 msg_clr_eos();
2640 msg_no_more = FALSE;
2641 }
2642#ifdef FEAT_FKMAP
2643 /*
2644 * If we are in Farsi command mode, the character input must be in
2645 * Insert mode. So do not advance the cmdpos.
2646 */
2647 if (!cmd_fkmap)
2648#endif
2649 {
2650 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002653 if (m < 0) /* overflow, Columns or Rows at weird value */
2654 m = MAXCOL;
2655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 else
2657 m = MAXCOL;
2658 for (i = 0; i < len; ++i)
2659 {
2660 c = cmdline_charsize(ccline.cmdpos);
2661#ifdef FEAT_MBYTE
2662 /* count ">" for a double-wide char that doesn't fit. */
2663 if (has_mbyte)
2664 correct_cmdspos(ccline.cmdpos, c);
2665#endif
2666 /* Stop cursor at the end of the screen */
2667 if (ccline.cmdspos + c >= m)
2668 break;
2669 ccline.cmdspos += c;
2670#ifdef FEAT_MBYTE
2671 if (has_mbyte)
2672 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002673 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 if (c > len - i - 1)
2675 c = len - i - 1;
2676 ccline.cmdpos += c;
2677 i += c;
2678 }
2679#endif
2680 ++ccline.cmdpos;
2681 }
2682 }
2683 }
2684 if (redraw)
2685 msg_check();
2686 return retval;
2687}
2688
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002689static struct cmdline_info prev_ccline;
2690static int prev_ccline_used = FALSE;
2691
2692/*
2693 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2694 * and overwrite it. But get_cmdline_str() may need it, thus make it
2695 * available globally in prev_ccline.
2696 */
2697 static void
2698save_cmdline(ccp)
2699 struct cmdline_info *ccp;
2700{
2701 if (!prev_ccline_used)
2702 {
2703 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2704 prev_ccline_used = TRUE;
2705 }
2706 *ccp = prev_ccline;
2707 prev_ccline = ccline;
2708 ccline.cmdbuff = NULL;
2709 ccline.cmdprompt = NULL;
2710}
2711
2712/*
2713 * Resture ccline after it has been saved with save_cmdline().
2714 */
2715 static void
2716restore_cmdline(ccp)
2717 struct cmdline_info *ccp;
2718{
2719 ccline = prev_ccline;
2720 prev_ccline = *ccp;
2721}
2722
Bram Moolenaar8299df92004-07-10 09:47:34 +00002723/*
2724 * paste a yank register into the command line.
2725 * used by CTRL-R command in command-line mode
2726 * insert_reg() can't be used here, because special characters from the
2727 * register contents will be interpreted as commands.
2728 *
2729 * return FAIL for failure, OK otherwise
2730 */
2731 static int
2732cmdline_paste(regname, literally)
2733 int regname;
2734 int literally; /* Insert text literally instead of "as typed" */
2735{
2736 long i;
2737 char_u *arg;
2738 int allocated;
2739 struct cmdline_info save_ccline;
2740
2741 /* check for valid regname; also accept special characters for CTRL-R in
2742 * the command line */
2743 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2744 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2745 return FAIL;
2746
2747 /* A register containing CTRL-R can cause an endless loop. Allow using
2748 * CTRL-C to break the loop. */
2749 line_breakcheck();
2750 if (got_int)
2751 return FAIL;
2752
2753#ifdef FEAT_CLIPBOARD
2754 regname = may_get_selection(regname);
2755#endif
2756
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002757 /* Need to save and restore ccline. */
2758 save_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002759 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002760 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002761
2762 if (i)
2763 {
2764 /* Got the value of a special register in "arg". */
2765 if (arg == NULL)
2766 return FAIL;
2767 cmdline_paste_str(arg, literally);
2768 if (allocated)
2769 vim_free(arg);
2770 return OK;
2771 }
2772
2773 return cmdline_paste_reg(regname, literally);
2774}
2775
2776/*
2777 * Put a string on the command line.
2778 * When "literally" is TRUE, insert literally.
2779 * When "literally" is FALSE, insert as typed, but don't leave the command
2780 * line.
2781 */
2782 void
2783cmdline_paste_str(s, literally)
2784 char_u *s;
2785 int literally;
2786{
2787 int c, cv;
2788
2789 if (literally)
2790 put_on_cmdline(s, -1, TRUE);
2791 else
2792 while (*s != NUL)
2793 {
2794 cv = *s;
2795 if (cv == Ctrl_V && s[1])
2796 ++s;
2797#ifdef FEAT_MBYTE
2798 if (has_mbyte)
2799 {
2800 c = mb_ptr2char(s);
2801 s += mb_char2len(c);
2802 }
2803 else
2804#endif
2805 c = *s++;
2806 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
2807#ifdef UNIX
2808 || c == intr_char
2809#endif
2810 || (c == Ctrl_BSL && *s == Ctrl_N))
2811 stuffcharReadbuff(Ctrl_V);
2812 stuffcharReadbuff(c);
2813 }
2814}
2815
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816#ifdef FEAT_WILDMENU
2817/*
2818 * Delete characters on the command line, from "from" to the current
2819 * position.
2820 */
2821 static void
2822cmdline_del(from)
2823 int from;
2824{
2825 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
2826 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
2827 ccline.cmdlen -= ccline.cmdpos - from;
2828 ccline.cmdpos = from;
2829}
2830#endif
2831
2832/*
2833 * this fuction is called when the screen size changes and with incremental
2834 * search
2835 */
2836 void
2837redrawcmdline()
2838{
2839 if (cmd_silent)
2840 return;
2841 need_wait_return = FALSE;
2842 compute_cmdrow();
2843 redrawcmd();
2844 cursorcmd();
2845}
2846
2847 static void
2848redrawcmdprompt()
2849{
2850 int i;
2851
2852 if (cmd_silent)
2853 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002854 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 msg_putchar(ccline.cmdfirstc);
2856 if (ccline.cmdprompt != NULL)
2857 {
2858 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
2859 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
2860 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002861 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 --ccline.cmdindent;
2863 }
2864 else
2865 for (i = ccline.cmdindent; i > 0; --i)
2866 msg_putchar(' ');
2867}
2868
2869/*
2870 * Redraw what is currently on the command line.
2871 */
2872 void
2873redrawcmd()
2874{
2875 if (cmd_silent)
2876 return;
2877
2878 msg_start();
2879 redrawcmdprompt();
2880
2881 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
2882 msg_no_more = TRUE;
2883 draw_cmdline(0, ccline.cmdlen);
2884 msg_clr_eos();
2885 msg_no_more = FALSE;
2886
2887 set_cmdspos_cursor();
2888
2889 /*
2890 * An emsg() before may have set msg_scroll. This is used in normal mode,
2891 * in cmdline mode we can reset them now.
2892 */
2893 msg_scroll = FALSE; /* next message overwrites cmdline */
2894
2895 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
2896 * in cmdline mode */
2897 skip_redraw = FALSE;
2898}
2899
2900 void
2901compute_cmdrow()
2902{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002903 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 cmdline_row = Rows - 1;
2905 else
2906 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
2907 + W_STATUS_HEIGHT(lastwin);
2908}
2909
2910 static void
2911cursorcmd()
2912{
2913 if (cmd_silent)
2914 return;
2915
2916#ifdef FEAT_RIGHTLEFT
2917 if (cmdmsg_rl)
2918 {
2919 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
2920 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
2921 if (msg_row <= 0)
2922 msg_row = Rows - 1;
2923 }
2924 else
2925#endif
2926 {
2927 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
2928 msg_col = ccline.cmdspos % (int)Columns;
2929 if (msg_row >= Rows)
2930 msg_row = Rows - 1;
2931 }
2932
2933 windgoto(msg_row, msg_col);
2934#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2935 redrawcmd_preedit();
2936#endif
2937#ifdef MCH_CURSOR_SHAPE
2938 mch_update_cursor();
2939#endif
2940}
2941
2942 void
2943gotocmdline(clr)
2944 int clr;
2945{
2946 msg_start();
2947#ifdef FEAT_RIGHTLEFT
2948 if (cmdmsg_rl)
2949 msg_col = Columns - 1;
2950 else
2951#endif
2952 msg_col = 0; /* always start in column 0 */
2953 if (clr) /* clear the bottom line(s) */
2954 msg_clr_eos(); /* will reset clear_cmdline */
2955 windgoto(cmdline_row, 0);
2956}
2957
2958/*
2959 * Check the word in front of the cursor for an abbreviation.
2960 * Called when the non-id character "c" has been entered.
2961 * When an abbreviation is recognized it is removed from the text with
2962 * backspaces and the replacement string is inserted, followed by "c".
2963 */
2964 static int
2965ccheck_abbr(c)
2966 int c;
2967{
2968 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
2969 return FALSE;
2970
2971 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
2972}
2973
2974/*
2975 * Return FAIL if this is not an appropriate context in which to do
2976 * completion of anything, return OK if it is (even if there are no matches).
2977 * For the caller, this means that the character is just passed through like a
2978 * normal character (instead of being expanded). This allows :s/^I^D etc.
2979 */
2980 static int
2981nextwild(xp, type, options)
2982 expand_T *xp;
2983 int type;
2984 int options; /* extra options for ExpandOne() */
2985{
2986 int i, j;
2987 char_u *p1;
2988 char_u *p2;
2989 int oldlen;
2990 int difflen;
2991 int v;
2992
2993 if (xp->xp_numfiles == -1)
2994 {
2995 set_expand_context(xp);
2996 cmd_showtail = expand_showtail(xp);
2997 }
2998
2999 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3000 {
3001 beep_flush();
3002 return OK; /* Something illegal on command line */
3003 }
3004 if (xp->xp_context == EXPAND_NOTHING)
3005 {
3006 /* Caller can use the character as a normal char instead */
3007 return FAIL;
3008 }
3009
3010 MSG_PUTS("..."); /* show that we are busy */
3011 out_flush();
3012
3013 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3014 oldlen = ccline.cmdpos - i;
3015
3016 if (type == WILD_NEXT || type == WILD_PREV)
3017 {
3018 /*
3019 * Get next/previous match for a previous expanded pattern.
3020 */
3021 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3022 }
3023 else
3024 {
3025 /*
3026 * Translate string into pattern and expand it.
3027 */
3028 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3029 p2 = NULL;
3030 else
3031 {
3032 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3033 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3034 |options, type);
3035 vim_free(p1);
3036 /* longest match: make sure it is not shorter (happens with :help */
3037 if (p2 != NULL && type == WILD_LONGEST)
3038 {
3039 for (j = 0; j < oldlen; ++j)
3040 if (ccline.cmdbuff[i + j] == '*'
3041 || ccline.cmdbuff[i + j] == '?')
3042 break;
3043 if ((int)STRLEN(p2) < j)
3044 {
3045 vim_free(p2);
3046 p2 = NULL;
3047 }
3048 }
3049 }
3050 }
3051
3052 if (p2 != NULL && !got_int)
3053 {
3054 difflen = (int)STRLEN(p2) - oldlen;
3055 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3056 {
3057 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3058 xp->xp_pattern = ccline.cmdbuff + i;
3059 }
3060 else
3061 v = OK;
3062 if (v == OK)
3063 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003064 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3065 &ccline.cmdbuff[ccline.cmdpos],
3066 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3067 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 ccline.cmdlen += difflen;
3069 ccline.cmdpos += difflen;
3070 }
3071 }
3072 vim_free(p2);
3073
3074 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003075 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
3077 /* When expanding a ":map" command and no matches are found, assume that
3078 * the key is supposed to be inserted literally */
3079 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3080 return FAIL;
3081
3082 if (xp->xp_numfiles <= 0 && p2 == NULL)
3083 beep_flush();
3084 else if (xp->xp_numfiles == 1)
3085 /* free expanded pattern */
3086 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3087
3088 return OK;
3089}
3090
3091/*
3092 * Do wildcard expansion on the string 'str'.
3093 * Chars that should not be expanded must be preceded with a backslash.
3094 * Return a pointer to alloced memory containing the new string.
3095 * Return NULL for failure.
3096 *
3097 * Results are cached in xp->xp_files and xp->xp_numfiles.
3098 *
3099 * mode = WILD_FREE: just free previously expanded matches
3100 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3101 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3102 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3103 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3104 * mode = WILD_ALL: return all matches concatenated
3105 * mode = WILD_LONGEST: return longest matched part
3106 *
3107 * options = WILD_LIST_NOTFOUND: list entries without a match
3108 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3109 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3110 * options = WILD_NO_BEEP: Don't beep for multiple matches
3111 * options = WILD_ADD_SLASH: add a slash after directory names
3112 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3113 * options = WILD_SILENT: don't print warning messages
3114 * options = WILD_ESCAPE: put backslash before special chars
3115 *
3116 * The variables xp->xp_context and xp->xp_backslash must have been set!
3117 */
3118 char_u *
3119ExpandOne(xp, str, orig, options, mode)
3120 expand_T *xp;
3121 char_u *str;
3122 char_u *orig; /* allocated copy of original of expanded string */
3123 int options;
3124 int mode;
3125{
3126 char_u *ss = NULL;
3127 static int findex;
3128 static char_u *orig_save = NULL; /* kept value of orig */
3129 int i;
3130 long_u len;
3131 int non_suf_match; /* number without matching suffix */
3132
3133 /*
3134 * first handle the case of using an old match
3135 */
3136 if (mode == WILD_NEXT || mode == WILD_PREV)
3137 {
3138 if (xp->xp_numfiles > 0)
3139 {
3140 if (mode == WILD_PREV)
3141 {
3142 if (findex == -1)
3143 findex = xp->xp_numfiles;
3144 --findex;
3145 }
3146 else /* mode == WILD_NEXT */
3147 ++findex;
3148
3149 /*
3150 * When wrapping around, return the original string, set findex to
3151 * -1.
3152 */
3153 if (findex < 0)
3154 {
3155 if (orig_save == NULL)
3156 findex = xp->xp_numfiles - 1;
3157 else
3158 findex = -1;
3159 }
3160 if (findex >= xp->xp_numfiles)
3161 {
3162 if (orig_save == NULL)
3163 findex = 0;
3164 else
3165 findex = -1;
3166 }
3167#ifdef FEAT_WILDMENU
3168 if (p_wmnu)
3169 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3170 findex, cmd_showtail);
3171#endif
3172 if (findex == -1)
3173 return vim_strsave(orig_save);
3174 return vim_strsave(xp->xp_files[findex]);
3175 }
3176 else
3177 return NULL;
3178 }
3179
3180/* free old names */
3181 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3182 {
3183 FreeWild(xp->xp_numfiles, xp->xp_files);
3184 xp->xp_numfiles = -1;
3185 vim_free(orig_save);
3186 orig_save = NULL;
3187 }
3188 findex = 0;
3189
3190 if (mode == WILD_FREE) /* only release file name */
3191 return NULL;
3192
3193 if (xp->xp_numfiles == -1)
3194 {
3195 vim_free(orig_save);
3196 orig_save = orig;
3197
3198 /*
3199 * Do the expansion.
3200 */
3201 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3202 options) == FAIL)
3203 {
3204#ifdef FNAME_ILLEGAL
3205 /* Illegal file name has been silently skipped. But when there
3206 * are wildcards, the real problem is that there was no match,
3207 * causing the pattern to be added, which has illegal characters.
3208 */
3209 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3210 EMSG2(_(e_nomatch2), str);
3211#endif
3212 }
3213 else if (xp->xp_numfiles == 0)
3214 {
3215 if (!(options & WILD_SILENT))
3216 EMSG2(_(e_nomatch2), str);
3217 }
3218 else
3219 {
3220 /* Escape the matches for use on the command line. */
3221 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3222
3223 /*
3224 * Check for matching suffixes in file names.
3225 */
3226 if (mode != WILD_ALL && mode != WILD_LONGEST)
3227 {
3228 if (xp->xp_numfiles)
3229 non_suf_match = xp->xp_numfiles;
3230 else
3231 non_suf_match = 1;
3232 if ((xp->xp_context == EXPAND_FILES
3233 || xp->xp_context == EXPAND_DIRECTORIES)
3234 && xp->xp_numfiles > 1)
3235 {
3236 /*
3237 * More than one match; check suffix.
3238 * The files will have been sorted on matching suffix in
3239 * expand_wildcards, only need to check the first two.
3240 */
3241 non_suf_match = 0;
3242 for (i = 0; i < 2; ++i)
3243 if (match_suffix(xp->xp_files[i]))
3244 ++non_suf_match;
3245 }
3246 if (non_suf_match != 1)
3247 {
3248 /* Can we ever get here unless it's while expanding
3249 * interactively? If not, we can get rid of this all
3250 * together. Don't really want to wait for this message
3251 * (and possibly have to hit return to continue!).
3252 */
3253 if (!(options & WILD_SILENT))
3254 EMSG(_(e_toomany));
3255 else if (!(options & WILD_NO_BEEP))
3256 beep_flush();
3257 }
3258 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3259 ss = vim_strsave(xp->xp_files[0]);
3260 }
3261 }
3262 }
3263
3264 /* Find longest common part */
3265 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3266 {
3267 for (len = 0; xp->xp_files[0][len]; ++len)
3268 {
3269 for (i = 0; i < xp->xp_numfiles; ++i)
3270 {
3271#ifdef CASE_INSENSITIVE_FILENAME
3272 if (xp->xp_context == EXPAND_DIRECTORIES
3273 || xp->xp_context == EXPAND_FILES
3274 || xp->xp_context == EXPAND_BUFFERS)
3275 {
3276 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3277 TOLOWER_LOC(xp->xp_files[0][len]))
3278 break;
3279 }
3280 else
3281#endif
3282 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3283 break;
3284 }
3285 if (i < xp->xp_numfiles)
3286 {
3287 if (!(options & WILD_NO_BEEP))
3288 vim_beep();
3289 break;
3290 }
3291 }
3292 ss = alloc((unsigned)len + 1);
3293 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003294 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 findex = -1; /* next p_wc gets first one */
3296 }
3297
3298 /* Concatenate all matching names */
3299 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3300 {
3301 len = 0;
3302 for (i = 0; i < xp->xp_numfiles; ++i)
3303 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3304 ss = lalloc(len, TRUE);
3305 if (ss != NULL)
3306 {
3307 *ss = NUL;
3308 for (i = 0; i < xp->xp_numfiles; ++i)
3309 {
3310 STRCAT(ss, xp->xp_files[i]);
3311 if (i != xp->xp_numfiles - 1)
3312 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3313 }
3314 }
3315 }
3316
3317 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3318 ExpandCleanup(xp);
3319
3320 return ss;
3321}
3322
3323/*
3324 * Prepare an expand structure for use.
3325 */
3326 void
3327ExpandInit(xp)
3328 expand_T *xp;
3329{
3330 xp->xp_backslash = XP_BS_NONE;
3331 xp->xp_numfiles = -1;
3332 xp->xp_files = NULL;
3333}
3334
3335/*
3336 * Cleanup an expand structure after use.
3337 */
3338 void
3339ExpandCleanup(xp)
3340 expand_T *xp;
3341{
3342 if (xp->xp_numfiles >= 0)
3343 {
3344 FreeWild(xp->xp_numfiles, xp->xp_files);
3345 xp->xp_numfiles = -1;
3346 }
3347}
3348
3349 void
3350ExpandEscape(xp, str, numfiles, files, options)
3351 expand_T *xp;
3352 char_u *str;
3353 int numfiles;
3354 char_u **files;
3355 int options;
3356{
3357 int i;
3358 char_u *p;
3359
3360 /*
3361 * May change home directory back to "~"
3362 */
3363 if (options & WILD_HOME_REPLACE)
3364 tilde_replace(str, numfiles, files);
3365
3366 if (options & WILD_ESCAPE)
3367 {
3368 if (xp->xp_context == EXPAND_FILES
3369 || xp->xp_context == EXPAND_BUFFERS
3370 || xp->xp_context == EXPAND_DIRECTORIES)
3371 {
3372 /*
3373 * Insert a backslash into a file name before a space, \, %, #
3374 * and wildmatch characters, except '~'.
3375 */
3376 for (i = 0; i < numfiles; ++i)
3377 {
3378 /* for ":set path=" we need to escape spaces twice */
3379 if (xp->xp_backslash == XP_BS_THREE)
3380 {
3381 p = vim_strsave_escaped(files[i], (char_u *)" ");
3382 if (p != NULL)
3383 {
3384 vim_free(files[i]);
3385 files[i] = p;
3386#if defined(BACKSLASH_IN_FILENAME) || defined(COLON_AS_PATHSEP)
3387 p = vim_strsave_escaped(files[i], (char_u *)" ");
3388 if (p != NULL)
3389 {
3390 vim_free(files[i]);
3391 files[i] = p;
3392 }
3393#endif
3394 }
3395 }
3396#ifdef BACKSLASH_IN_FILENAME
3397 {
3398 char_u buf[20];
3399 int j = 0;
3400
3401 /* Don't escape '[' and '{' if they are in 'isfname'. */
3402 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3403 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3404 buf[j++] = *p;
3405 buf[j] = NUL;
3406 p = vim_strsave_escaped(files[i], buf);
3407 }
3408#else
3409 p = vim_strsave_escaped(files[i], PATH_ESC_CHARS);
3410#endif
3411 if (p != NULL)
3412 {
3413 vim_free(files[i]);
3414 files[i] = p;
3415 }
3416
3417 /* If 'str' starts with "\~", replace "~" at start of
3418 * files[i] with "\~". */
3419 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003420 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 }
3422 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003423
3424 /* If the first file starts with a '+' escape it. Otherwise it
3425 * could be seen as "+cmd". */
3426 if (*files[0] == '+')
3427 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
3429 else if (xp->xp_context == EXPAND_TAGS)
3430 {
3431 /*
3432 * Insert a backslash before characters in a tag name that
3433 * would terminate the ":tag" command.
3434 */
3435 for (i = 0; i < numfiles; ++i)
3436 {
3437 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3438 if (p != NULL)
3439 {
3440 vim_free(files[i]);
3441 files[i] = p;
3442 }
3443 }
3444 }
3445 }
3446}
3447
3448/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003449 * Put a backslash before the file name in "pp", which is in allocated memory.
3450 */
3451 static void
3452escape_fname(pp)
3453 char_u **pp;
3454{
3455 char_u *p;
3456
3457 p = alloc((unsigned)(STRLEN(*pp) + 2));
3458 if (p != NULL)
3459 {
3460 p[0] = '\\';
3461 STRCPY(p + 1, *pp);
3462 vim_free(*pp);
3463 *pp = p;
3464 }
3465}
3466
3467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 * For each file name in files[num_files]:
3469 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3470 */
3471 void
3472tilde_replace(orig_pat, num_files, files)
3473 char_u *orig_pat;
3474 int num_files;
3475 char_u **files;
3476{
3477 int i;
3478 char_u *p;
3479
3480 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3481 {
3482 for (i = 0; i < num_files; ++i)
3483 {
3484 p = home_replace_save(NULL, files[i]);
3485 if (p != NULL)
3486 {
3487 vim_free(files[i]);
3488 files[i] = p;
3489 }
3490 }
3491 }
3492}
3493
3494/*
3495 * Show all matches for completion on the command line.
3496 * Returns EXPAND_NOTHING when the character that triggered expansion should
3497 * be inserted like a normal character.
3498 */
3499/*ARGSUSED*/
3500 static int
3501showmatches(xp, wildmenu)
3502 expand_T *xp;
3503 int wildmenu;
3504{
3505#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3506 int num_files;
3507 char_u **files_found;
3508 int i, j, k;
3509 int maxlen;
3510 int lines;
3511 int columns;
3512 char_u *p;
3513 int lastlen;
3514 int attr;
3515 int showtail;
3516
3517 if (xp->xp_numfiles == -1)
3518 {
3519 set_expand_context(xp);
3520 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3521 &num_files, &files_found);
3522 showtail = expand_showtail(xp);
3523 if (i != EXPAND_OK)
3524 return i;
3525
3526 }
3527 else
3528 {
3529 num_files = xp->xp_numfiles;
3530 files_found = xp->xp_files;
3531 showtail = cmd_showtail;
3532 }
3533
3534#ifdef FEAT_WILDMENU
3535 if (!wildmenu)
3536 {
3537#endif
3538 msg_didany = FALSE; /* lines_left will be set */
3539 msg_start(); /* prepare for paging */
3540 msg_putchar('\n');
3541 out_flush();
3542 cmdline_row = msg_row;
3543 msg_didany = FALSE; /* lines_left will be set again */
3544 msg_start(); /* prepare for paging */
3545#ifdef FEAT_WILDMENU
3546 }
3547#endif
3548
3549 if (got_int)
3550 got_int = FALSE; /* only int. the completion, not the cmd line */
3551#ifdef FEAT_WILDMENU
3552 else if (wildmenu)
3553 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3554#endif
3555 else
3556 {
3557 /* find the length of the longest file name */
3558 maxlen = 0;
3559 for (i = 0; i < num_files; ++i)
3560 {
3561 if (!showtail && (xp->xp_context == EXPAND_FILES
3562 || xp->xp_context == EXPAND_BUFFERS))
3563 {
3564 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3565 j = vim_strsize(NameBuff);
3566 }
3567 else
3568 j = vim_strsize(L_SHOWFILE(i));
3569 if (j > maxlen)
3570 maxlen = j;
3571 }
3572
3573 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3574 lines = num_files;
3575 else
3576 {
3577 /* compute the number of columns and lines for the listing */
3578 maxlen += 2; /* two spaces between file names */
3579 columns = ((int)Columns + 2) / maxlen;
3580 if (columns < 1)
3581 columns = 1;
3582 lines = (num_files + columns - 1) / columns;
3583 }
3584
3585 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3586
3587 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3588 {
3589 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3590 msg_clr_eos();
3591 msg_advance(maxlen - 3);
3592 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3593 }
3594
3595 /* list the files line by line */
3596 for (i = 0; i < lines; ++i)
3597 {
3598 lastlen = 999;
3599 for (k = i; k < num_files; k += lines)
3600 {
3601 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3602 {
3603 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3604 p = files_found[k] + STRLEN(files_found[k]) + 1;
3605 msg_advance(maxlen + 1);
3606 msg_puts(p);
3607 msg_advance(maxlen + 3);
3608 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3609 break;
3610 }
3611 for (j = maxlen - lastlen; --j >= 0; )
3612 msg_putchar(' ');
3613 if (xp->xp_context == EXPAND_FILES
3614 || xp->xp_context == EXPAND_BUFFERS)
3615 {
3616 /* highlight directories */
3617 j = (mch_isdir(files_found[k]));
3618 if (showtail)
3619 p = L_SHOWFILE(k);
3620 else
3621 {
3622 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3623 TRUE);
3624 p = NameBuff;
3625 }
3626 }
3627 else
3628 {
3629 j = FALSE;
3630 p = L_SHOWFILE(k);
3631 }
3632 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3633 }
3634 if (msg_col > 0) /* when not wrapped around */
3635 {
3636 msg_clr_eos();
3637 msg_putchar('\n');
3638 }
3639 out_flush(); /* show one line at a time */
3640 if (got_int)
3641 {
3642 got_int = FALSE;
3643 break;
3644 }
3645 }
3646
3647 /*
3648 * we redraw the command below the lines that we have just listed
3649 * This is a bit tricky, but it saves a lot of screen updating.
3650 */
3651 cmdline_row = msg_row; /* will put it back later */
3652 }
3653
3654 if (xp->xp_numfiles == -1)
3655 FreeWild(num_files, files_found);
3656
3657 return EXPAND_OK;
3658}
3659
3660/*
3661 * Private gettail for showmatches() (and win_redr_status_matches()):
3662 * Find tail of file name path, but ignore trailing "/".
3663 */
3664 char_u *
3665sm_gettail(s)
3666 char_u *s;
3667{
3668 char_u *p;
3669 char_u *t = s;
3670 int had_sep = FALSE;
3671
3672 for (p = s; *p != NUL; )
3673 {
3674 if (vim_ispathsep(*p)
3675#ifdef BACKSLASH_IN_FILENAME
3676 && !rem_backslash(p)
3677#endif
3678 )
3679 had_sep = TRUE;
3680 else if (had_sep)
3681 {
3682 t = p;
3683 had_sep = FALSE;
3684 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003685 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
3687 return t;
3688}
3689
3690/*
3691 * Return TRUE if we only need to show the tail of completion matches.
3692 * When not completing file names or there is a wildcard in the path FALSE is
3693 * returned.
3694 */
3695 static int
3696expand_showtail(xp)
3697 expand_T *xp;
3698{
3699 char_u *s;
3700 char_u *end;
3701
3702 /* When not completing file names a "/" may mean something different. */
3703 if (xp->xp_context != EXPAND_FILES && xp->xp_context != EXPAND_DIRECTORIES)
3704 return FALSE;
3705
3706 end = gettail(xp->xp_pattern);
3707 if (end == xp->xp_pattern) /* there is no path separator */
3708 return FALSE;
3709
3710 for (s = xp->xp_pattern; s < end; s++)
3711 {
3712 /* Skip escaped wildcards. Only when the backslash is not a path
3713 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3714 if (rem_backslash(s))
3715 ++s;
3716 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3717 return FALSE;
3718 }
3719 return TRUE;
3720}
3721
3722/*
3723 * Prepare a string for expansion.
3724 * When expanding file names: The string will be used with expand_wildcards().
3725 * Copy the file name into allocated memory and add a '*' at the end.
3726 * When expanding other names: The string will be used with regcomp(). Copy
3727 * the name into allocated memory and prepend "^".
3728 */
3729 char_u *
3730addstar(fname, len, context)
3731 char_u *fname;
3732 int len;
3733 int context; /* EXPAND_FILES etc. */
3734{
3735 char_u *retval;
3736 int i, j;
3737 int new_len;
3738 char_u *tail;
3739
3740 if (context != EXPAND_FILES && context != EXPAND_DIRECTORIES)
3741 {
3742 /*
3743 * Matching will be done internally (on something other than files).
3744 * So we convert the file-matching-type wildcards into our kind for
3745 * use with vim_regcomp(). First work out how long it will be:
3746 */
3747
3748 /* For help tags the translation is done in find_help_tags().
3749 * For a tag pattern starting with "/" no translation is needed. */
3750 if (context == EXPAND_HELP
3751 || context == EXPAND_COLORS
3752 || context == EXPAND_COMPILER
3753 || (context == EXPAND_TAGS && fname[0] == '/'))
3754 retval = vim_strnsave(fname, len);
3755 else
3756 {
3757 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3758 for (i = 0; i < len; i++)
3759 {
3760 if (fname[i] == '*' || fname[i] == '~')
3761 new_len++; /* '*' needs to be replaced by ".*"
3762 '~' needs to be replaced by "\~" */
3763
3764 /* Buffer names are like file names. "." should be literal */
3765 if (context == EXPAND_BUFFERS && fname[i] == '.')
3766 new_len++; /* "." becomes "\." */
3767
3768 /* Custom expansion takes care of special things, match
3769 * backslashes literally (perhaps also for other types?) */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003770 if ((context == EXPAND_USER_DEFINED ||
3771 context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 new_len++; /* '\' becomes "\\" */
3773 }
3774 retval = alloc(new_len);
3775 if (retval != NULL)
3776 {
3777 retval[0] = '^';
3778 j = 1;
3779 for (i = 0; i < len; i++, j++)
3780 {
3781 /* Skip backslash. But why? At least keep it for custom
3782 * expansion. */
3783 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003784 && context != EXPAND_USER_LIST
3785 && fname[i] == '\\'
3786 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 break;
3788
3789 switch (fname[i])
3790 {
3791 case '*': retval[j++] = '.';
3792 break;
3793 case '~': retval[j++] = '\\';
3794 break;
3795 case '?': retval[j] = '.';
3796 continue;
3797 case '.': if (context == EXPAND_BUFFERS)
3798 retval[j++] = '\\';
3799 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003800 case '\\': if (context == EXPAND_USER_DEFINED
3801 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 retval[j++] = '\\';
3803 break;
3804 }
3805 retval[j] = fname[i];
3806 }
3807 retval[j] = NUL;
3808 }
3809 }
3810 }
3811 else
3812 {
3813 retval = alloc(len + 4);
3814 if (retval != NULL)
3815 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003816 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817
3818 /*
3819 * Don't add a star to ~, ~user, $var or `cmd`.
3820 * ~ would be at the start of the file name, but not the tail.
3821 * $ could be anywhere in the tail.
3822 * ` could be anywhere in the file name.
3823 */
3824 tail = gettail(retval);
3825 if ((*retval != '~' || tail != retval)
3826 && vim_strchr(tail, '$') == NULL
3827 && vim_strchr(retval, '`') == NULL)
3828 retval[len++] = '*';
3829 retval[len] = NUL;
3830 }
3831 }
3832 return retval;
3833}
3834
3835/*
3836 * Must parse the command line so far to work out what context we are in.
3837 * Completion can then be done based on that context.
3838 * This routine sets the variables:
3839 * xp->xp_pattern The start of the pattern to be expanded within
3840 * the command line (ends at the cursor).
3841 * xp->xp_context The type of thing to expand. Will be one of:
3842 *
3843 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
3844 * the command line, like an unknown command. Caller
3845 * should beep.
3846 * EXPAND_NOTHING Unrecognised context for completion, use char like
3847 * a normal char, rather than for completion. eg
3848 * :s/^I/
3849 * EXPAND_COMMANDS Cursor is still touching the command, so complete
3850 * it.
3851 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
3852 * EXPAND_FILES After command with XFILE set, or after setting
3853 * with P_EXPAND set. eg :e ^I, :w>>^I
3854 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
3855 * when we know only directories are of interest. eg
3856 * :set dir=^I
3857 * EXPAND_SETTINGS Complete variable names. eg :set d^I
3858 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
3859 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
3860 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
3861 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
3862 * EXPAND_EVENTS Complete event names
3863 * EXPAND_SYNTAX Complete :syntax command arguments
3864 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
3865 * EXPAND_AUGROUP Complete autocommand group names
3866 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
3867 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
3868 * eg :unmap a^I , :cunab x^I
3869 * EXPAND_FUNCTIONS Complete internal or user defined function names,
3870 * eg :call sub^I
3871 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
3872 * EXPAND_EXPRESSION Complete internal or user defined function/variable
3873 * names in expressions, eg :while s^I
3874 * EXPAND_ENV_VARS Complete environment variable names
3875 */
3876 static void
3877set_expand_context(xp)
3878 expand_T *xp;
3879{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003880 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 if (ccline.cmdfirstc != ':'
3882#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003883 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003884 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885#endif
3886 )
3887 {
3888 xp->xp_context = EXPAND_NOTHING;
3889 return;
3890 }
3891 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
3892}
3893
3894 void
3895set_cmd_context(xp, str, len, col)
3896 expand_T *xp;
3897 char_u *str; /* start of command line */
3898 int len; /* length of command line (excl. NUL) */
3899 int col; /* position of cursor */
3900{
3901 int old_char = NUL;
3902 char_u *nextcomm;
3903
3904 /*
3905 * Avoid a UMR warning from Purify, only save the character if it has been
3906 * written before.
3907 */
3908 if (col < len)
3909 old_char = str[col];
3910 str[col] = NUL;
3911 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003912
3913#ifdef FEAT_EVAL
3914 if (ccline.cmdfirstc == '=')
3915 /* pass CMD_SIZE because there is no real command */
3916 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003917 else if (ccline.input_fn)
3918 {
3919 xp->xp_context = ccline.xp_context;
3920 xp->xp_pattern = ccline.cmdbuff;
3921 xp->xp_arg = ccline.xp_arg;
3922 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003923 else
3924#endif
3925 while (nextcomm != NULL)
3926 nextcomm = set_one_cmd_context(xp, nextcomm);
3927
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 str[col] = old_char;
3929}
3930
3931/*
3932 * Expand the command line "str" from context "xp".
3933 * "xp" must have been set by set_cmd_context().
3934 * xp->xp_pattern points into "str", to where the text that is to be expanded
3935 * starts.
3936 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
3937 * cursor.
3938 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
3939 * key that triggered expansion literally.
3940 * Returns EXPAND_OK otherwise.
3941 */
3942 int
3943expand_cmdline(xp, str, col, matchcount, matches)
3944 expand_T *xp;
3945 char_u *str; /* start of command line */
3946 int col; /* position of cursor */
3947 int *matchcount; /* return: nr of matches */
3948 char_u ***matches; /* return: array of pointers to matches */
3949{
3950 char_u *file_str = NULL;
3951
3952 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3953 {
3954 beep_flush();
3955 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
3956 }
3957 if (xp->xp_context == EXPAND_NOTHING)
3958 {
3959 /* Caller can use the character as a normal char instead */
3960 return EXPAND_NOTHING;
3961 }
3962
3963 /* add star to file name, or convert to regexp if not exp. files. */
3964 file_str = addstar(xp->xp_pattern,
3965 (int)(str + col - xp->xp_pattern), xp->xp_context);
3966 if (file_str == NULL)
3967 return EXPAND_UNSUCCESSFUL;
3968
3969 /* find all files that match the description */
3970 if (ExpandFromContext(xp, file_str, matchcount, matches,
3971 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
3972 {
3973 *matchcount = 0;
3974 *matches = NULL;
3975 }
3976 vim_free(file_str);
3977
3978 return EXPAND_OK;
3979}
3980
3981#ifdef FEAT_MULTI_LANG
3982/*
3983 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
3984 */
3985static void cleanup_help_tags __ARGS((int num_file, char_u **file));
3986
3987 static void
3988cleanup_help_tags(num_file, file)
3989 int num_file;
3990 char_u **file;
3991{
3992 int i, j;
3993 int len;
3994
3995 for (i = 0; i < num_file; ++i)
3996 {
3997 len = (int)STRLEN(file[i]) - 3;
3998 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
3999 {
4000 /* Sorting on priority means the same item in another language may
4001 * be anywhere. Search all items for a match up to the "@en". */
4002 for (j = 0; j < num_file; ++j)
4003 if (j != i
4004 && (int)STRLEN(file[j]) == len + 3
4005 && STRNCMP(file[i], file[j], len + 1) == 0)
4006 break;
4007 if (j == num_file)
4008 file[i][len] = NUL;
4009 }
4010 }
4011}
4012#endif
4013
4014/*
4015 * Do the expansion based on xp->xp_context and "pat".
4016 */
4017 static int
4018ExpandFromContext(xp, pat, num_file, file, options)
4019 expand_T *xp;
4020 char_u *pat;
4021 int *num_file;
4022 char_u ***file;
4023 int options;
4024{
4025#ifdef FEAT_CMDL_COMPL
4026 regmatch_T regmatch;
4027#endif
4028 int ret;
4029 int flags;
4030
4031 flags = EW_DIR; /* include directories */
4032 if (options & WILD_LIST_NOTFOUND)
4033 flags |= EW_NOTFOUND;
4034 if (options & WILD_ADD_SLASH)
4035 flags |= EW_ADDSLASH;
4036 if (options & WILD_KEEP_ALL)
4037 flags |= EW_KEEPALL;
4038 if (options & WILD_SILENT)
4039 flags |= EW_SILENT;
4040
4041 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4042 {
4043 /*
4044 * Expand file or directory names.
4045 */
4046 int free_pat = FALSE;
4047 int i;
4048
4049 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4050 * space */
4051 if (xp->xp_backslash != XP_BS_NONE)
4052 {
4053 free_pat = TRUE;
4054 pat = vim_strsave(pat);
4055 for (i = 0; pat[i]; ++i)
4056 if (pat[i] == '\\')
4057 {
4058 if (xp->xp_backslash == XP_BS_THREE
4059 && pat[i + 1] == '\\'
4060 && pat[i + 2] == '\\'
4061 && pat[i + 3] == ' ')
4062 STRCPY(pat + i, pat + i + 3);
4063 if (xp->xp_backslash == XP_BS_ONE
4064 && pat[i + 1] == ' ')
4065 STRCPY(pat + i, pat + i + 1);
4066 }
4067 }
4068
4069 if (xp->xp_context == EXPAND_FILES)
4070 flags |= EW_FILE;
4071 else
4072 flags = (flags | EW_DIR) & ~EW_FILE;
4073 ret = expand_wildcards(1, &pat, num_file, file, flags);
4074 if (free_pat)
4075 vim_free(pat);
4076 return ret;
4077 }
4078
4079 *file = (char_u **)"";
4080 *num_file = 0;
4081 if (xp->xp_context == EXPAND_HELP)
4082 {
4083 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4084 {
4085#ifdef FEAT_MULTI_LANG
4086 cleanup_help_tags(*num_file, *file);
4087#endif
4088 return OK;
4089 }
4090 return FAIL;
4091 }
4092
4093#ifndef FEAT_CMDL_COMPL
4094 return FAIL;
4095#else
4096 if (xp->xp_context == EXPAND_OLD_SETTING)
4097 return ExpandOldSetting(num_file, file);
4098 if (xp->xp_context == EXPAND_BUFFERS)
4099 return ExpandBufnames(pat, num_file, file, options);
4100 if (xp->xp_context == EXPAND_TAGS
4101 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4102 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4103 if (xp->xp_context == EXPAND_COLORS)
4104 return ExpandRTDir(pat, num_file, file, "colors");
4105 if (xp->xp_context == EXPAND_COMPILER)
4106 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004107# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4108 if (xp->xp_context == EXPAND_USER_LIST)
4109 return ExpandUserList(xp, num_file, file);
4110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
4112 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4113 if (regmatch.regprog == NULL)
4114 return FAIL;
4115
4116 /* set ignore-case according to p_ic, p_scs and pat */
4117 regmatch.rm_ic = ignorecase(pat);
4118
4119 if (xp->xp_context == EXPAND_SETTINGS
4120 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4121 ret = ExpandSettings(xp, &regmatch, num_file, file);
4122 else if (xp->xp_context == EXPAND_MAPPINGS)
4123 ret = ExpandMappings(&regmatch, num_file, file);
4124# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4125 else if (xp->xp_context == EXPAND_USER_DEFINED)
4126 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4127# endif
4128 else
4129 {
4130 static struct expgen
4131 {
4132 int context;
4133 char_u *((*func)__ARGS((expand_T *, int)));
4134 int ic;
4135 } tab[] =
4136 {
4137 {EXPAND_COMMANDS, get_command_name, FALSE},
4138#ifdef FEAT_USR_CMDS
4139 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4140 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4141 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4142 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4143#endif
4144#ifdef FEAT_EVAL
4145 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4146 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4147 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4148 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4149#endif
4150#ifdef FEAT_MENU
4151 {EXPAND_MENUS, get_menu_name, FALSE},
4152 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4153#endif
4154#ifdef FEAT_SYN_HL
4155 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4156#endif
4157 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4158#ifdef FEAT_AUTOCMD
4159 {EXPAND_EVENTS, get_event_name, TRUE},
4160 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4161#endif
4162#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4163 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4164 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4165#endif
4166 {EXPAND_ENV_VARS, get_env_name, TRUE},
4167 };
4168 int i;
4169
4170 /*
4171 * Find a context in the table and call the ExpandGeneric() with the
4172 * right function to do the expansion.
4173 */
4174 ret = FAIL;
4175 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4176 if (xp->xp_context == tab[i].context)
4177 {
4178 if (tab[i].ic)
4179 regmatch.rm_ic = TRUE;
4180 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4181 break;
4182 }
4183 }
4184
4185 vim_free(regmatch.regprog);
4186
4187 return ret;
4188#endif /* FEAT_CMDL_COMPL */
4189}
4190
4191#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4192/*
4193 * Expand a list of names.
4194 *
4195 * Generic function for command line completion. It calls a function to
4196 * obtain strings, one by one. The strings are matched against a regexp
4197 * program. Matching strings are copied into an array, which is returned.
4198 *
4199 * Returns OK when no problems encountered, FAIL for error (out of memory).
4200 */
4201 int
4202ExpandGeneric(xp, regmatch, num_file, file, func)
4203 expand_T *xp;
4204 regmatch_T *regmatch;
4205 int *num_file;
4206 char_u ***file;
4207 char_u *((*func)__ARGS((expand_T *, int)));
4208 /* returns a string from the list */
4209{
4210 int i;
4211 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004212 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 char_u *str;
4214
4215 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004216 * round == 0: count the number of matching names
4217 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004219 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
4221 for (i = 0; ; ++i)
4222 {
4223 str = (*func)(xp, i);
4224 if (str == NULL) /* end of list */
4225 break;
4226 if (*str == NUL) /* skip empty strings */
4227 continue;
4228
4229 if (vim_regexec(regmatch, str, (colnr_T)0))
4230 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004231 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
4233 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4234 (*file)[count] = str;
4235#ifdef FEAT_MENU
4236 if (func == get_menu_names && str != NULL)
4237 {
4238 /* test for separator added by get_menu_names() */
4239 str += STRLEN(str) - 1;
4240 if (*str == '\001')
4241 *str = '.';
4242 }
4243#endif
4244 }
4245 ++count;
4246 }
4247 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004248 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
4250 if (count == 0)
4251 return OK;
4252 *num_file = count;
4253 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4254 if (*file == NULL)
4255 {
4256 *file = (char_u **)"";
4257 return FAIL;
4258 }
4259 count = 0;
4260 }
4261 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004262
4263 /* Sort the results. */
4264 sort_strings(*file, *num_file);
4265
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 return OK;
4267}
4268
4269# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004270static void * call_user_expand_func __ARGS((void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int)), expand_T *xp, int *num_file, char_u ***file));
4271
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004273 * call "user_expand_func()" to invoke a user defined VimL function and return
4274 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004276 static void *
4277call_user_expand_func(user_expand_func, xp, num_file, file)
4278 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 int *num_file;
4281 char_u ***file;
4282{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004283 char_u keep;
4284 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004287 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004288 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
4290 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004291 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 *num_file = 0;
4293 *file = NULL;
4294
4295 keep = ccline.cmdbuff[ccline.cmdlen];
4296 ccline.cmdbuff[ccline.cmdlen] = 0;
4297 sprintf((char *)num, "%d", ccline.cmdpos);
4298 args[0] = xp->xp_pattern;
4299 args[1] = ccline.cmdbuff;
4300 args[2] = num;
4301
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004302 /* Save the cmdline, we don't know what the function may do. */
4303 save_ccline = ccline;
4304 ccline.cmdbuff = NULL;
4305 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004307
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004308 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004309
4310 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004312
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004314
4315 return ret;
4316}
4317
4318/*
4319 * Expand names with a function defined by the user.
4320 */
4321 static int
4322ExpandUserDefined(xp, regmatch, num_file, file)
4323 expand_T *xp;
4324 regmatch_T *regmatch;
4325 int *num_file;
4326 char_u ***file;
4327{
4328 char_u *retstr;
4329 char_u *s;
4330 char_u *e;
4331 char_u keep;
4332 garray_T ga;
4333
4334 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4335 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 return FAIL;
4337
4338 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004339 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 {
4341 e = vim_strchr(s, '\n');
4342 if (e == NULL)
4343 e = s + STRLEN(s);
4344 keep = *e;
4345 *e = 0;
4346
4347 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4348 {
4349 *e = keep;
4350 if (*e != NUL)
4351 ++e;
4352 continue;
4353 }
4354
4355 if (ga_grow(&ga, 1) == FAIL)
4356 break;
4357
4358 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4359 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
4361 *e = keep;
4362 if (*e != NUL)
4363 ++e;
4364 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004365 vim_free(retstr);
4366 *file = ga.ga_data;
4367 *num_file = ga.ga_len;
4368 return OK;
4369}
4370
4371/*
4372 * Expand names with a list returned by a function defined by the user.
4373 */
4374 static int
4375ExpandUserList(xp, num_file, file)
4376 expand_T *xp;
4377 int *num_file;
4378 char_u ***file;
4379{
4380 list_T *retlist;
4381 listitem_T *li;
4382 garray_T ga;
4383
4384 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4385 if (retlist == NULL)
4386 return FAIL;
4387
4388 ga_init2(&ga, (int)sizeof(char *), 3);
4389 /* Loop over the items in the list. */
4390 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4391 {
4392 if (li->li_tv.v_type != VAR_STRING)
4393 continue; /* Skip non-string items */
4394
4395 if (ga_grow(&ga, 1) == FAIL)
4396 break;
4397
4398 ((char_u **)ga.ga_data)[ga.ga_len] =
4399 vim_strsave(li->li_tv.vval.v_string);
4400 ++ga.ga_len;
4401 }
4402 list_unref(retlist);
4403
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 *file = ga.ga_data;
4405 *num_file = ga.ga_len;
4406 return OK;
4407}
4408#endif
4409
4410/*
4411 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4412 * or compiler names.
4413 */
4414 static int
4415ExpandRTDir(pat, num_file, file, dirname)
4416 char_u *pat;
4417 int *num_file;
4418 char_u ***file;
4419 char *dirname; /* "colors" or "compiler" */
4420{
4421 char_u *all;
4422 char_u *s;
4423 char_u *e;
4424 garray_T ga;
4425
4426 *num_file = 0;
4427 *file = NULL;
4428 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4429 if (s == NULL)
4430 return FAIL;
4431 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4432 all = globpath(p_rtp, s);
4433 vim_free(s);
4434 if (all == NULL)
4435 return FAIL;
4436
4437 ga_init2(&ga, (int)sizeof(char *), 3);
4438 for (s = all; *s != NUL; s = e)
4439 {
4440 e = vim_strchr(s, '\n');
4441 if (e == NULL)
4442 e = s + STRLEN(s);
4443 if (ga_grow(&ga, 1) == FAIL)
4444 break;
4445 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4446 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004447 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 if (*s == '\n' || vim_ispathsep(*s))
4449 break;
4450 ++s;
4451 ((char_u **)ga.ga_data)[ga.ga_len] =
4452 vim_strnsave(s, (int)(e - s - 4));
4453 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
4455 if (*e != NUL)
4456 ++e;
4457 }
4458 vim_free(all);
4459 *file = ga.ga_data;
4460 *num_file = ga.ga_len;
4461 return OK;
4462}
4463
4464#endif
4465
4466#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4467/*
4468 * Expand "file" for all comma-separated directories in "path".
4469 * Returns an allocated string with all matches concatenated, separated by
4470 * newlines. Returns NULL for an error or no matches.
4471 */
4472 char_u *
4473globpath(path, file)
4474 char_u *path;
4475 char_u *file;
4476{
4477 expand_T xpc;
4478 char_u *buf;
4479 garray_T ga;
4480 int i;
4481 int len;
4482 int num_p;
4483 char_u **p;
4484 char_u *cur = NULL;
4485
4486 buf = alloc(MAXPATHL);
4487 if (buf == NULL)
4488 return NULL;
4489
4490 xpc.xp_context = EXPAND_FILES;
4491 xpc.xp_backslash = XP_BS_NONE;
4492 ga_init2(&ga, 1, 100);
4493
4494 /* Loop over all entries in {path}. */
4495 while (*path != NUL)
4496 {
4497 /* Copy one item of the path to buf[] and concatenate the file name. */
4498 copy_option_part(&path, buf, MAXPATHL, ",");
4499 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4500 {
4501 add_pathsep(buf);
4502 STRCAT(buf, file);
4503 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4504 && num_p > 0)
4505 {
4506 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4507 for (len = 0, i = 0; i < num_p; ++i)
4508 len += (long_u)STRLEN(p[i]) + 1;
4509
4510 /* Concatenate new results to previous ones. */
4511 if (ga_grow(&ga, len) == OK)
4512 {
4513 cur = (char_u *)ga.ga_data + ga.ga_len;
4514 for (i = 0; i < num_p; ++i)
4515 {
4516 STRCPY(cur, p[i]);
4517 cur += STRLEN(p[i]);
4518 *cur++ = '\n';
4519 }
4520 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 }
4522 FreeWild(num_p, p);
4523 }
4524 }
4525 }
4526 if (cur != NULL)
4527 *--cur = 0; /* Replace trailing newline with NUL */
4528
4529 vim_free(buf);
4530 return (char_u *)ga.ga_data;
4531}
4532
4533#endif
4534
4535#if defined(FEAT_CMDHIST) || defined(PROTO)
4536
4537/*********************************
4538 * Command line history stuff *
4539 *********************************/
4540
4541/*
4542 * Translate a history character to the associated type number.
4543 */
4544 static int
4545hist_char2type(c)
4546 int c;
4547{
4548 if (c == ':')
4549 return HIST_CMD;
4550 if (c == '=')
4551 return HIST_EXPR;
4552 if (c == '@')
4553 return HIST_INPUT;
4554 if (c == '>')
4555 return HIST_DEBUG;
4556 return HIST_SEARCH; /* must be '?' or '/' */
4557}
4558
4559/*
4560 * Table of history names.
4561 * These names are used in :history and various hist...() functions.
4562 * It is sufficient to give the significant prefix of a history name.
4563 */
4564
4565static char *(history_names[]) =
4566{
4567 "cmd",
4568 "search",
4569 "expr",
4570 "input",
4571 "debug",
4572 NULL
4573};
4574
4575/*
4576 * init_history() - Initialize the command line history.
4577 * Also used to re-allocate the history when the size changes.
4578 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004579 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580init_history()
4581{
4582 int newlen; /* new length of history table */
4583 histentry_T *temp;
4584 int i;
4585 int j;
4586 int type;
4587
4588 /*
4589 * If size of history table changed, reallocate it
4590 */
4591 newlen = (int)p_hi;
4592 if (newlen != hislen) /* history length changed */
4593 {
4594 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4595 {
4596 if (newlen)
4597 {
4598 temp = (histentry_T *)lalloc(
4599 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4600 if (temp == NULL) /* out of memory! */
4601 {
4602 if (type == 0) /* first one: just keep the old length */
4603 {
4604 newlen = hislen;
4605 break;
4606 }
4607 /* Already changed one table, now we can only have zero
4608 * length for all tables. */
4609 newlen = 0;
4610 type = -1;
4611 continue;
4612 }
4613 }
4614 else
4615 temp = NULL;
4616 if (newlen == 0 || temp != NULL)
4617 {
4618 if (hisidx[type] < 0) /* there are no entries yet */
4619 {
4620 for (i = 0; i < newlen; ++i)
4621 {
4622 temp[i].hisnum = 0;
4623 temp[i].hisstr = NULL;
4624 }
4625 }
4626 else if (newlen > hislen) /* array becomes bigger */
4627 {
4628 for (i = 0; i <= hisidx[type]; ++i)
4629 temp[i] = history[type][i];
4630 j = i;
4631 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4632 {
4633 temp[i].hisnum = 0;
4634 temp[i].hisstr = NULL;
4635 }
4636 for ( ; j < hislen; ++i, ++j)
4637 temp[i] = history[type][j];
4638 }
4639 else /* array becomes smaller or 0 */
4640 {
4641 j = hisidx[type];
4642 for (i = newlen - 1; ; --i)
4643 {
4644 if (i >= 0) /* copy newest entries */
4645 temp[i] = history[type][j];
4646 else /* remove older entries */
4647 vim_free(history[type][j].hisstr);
4648 if (--j < 0)
4649 j = hislen - 1;
4650 if (j == hisidx[type])
4651 break;
4652 }
4653 hisidx[type] = newlen - 1;
4654 }
4655 vim_free(history[type]);
4656 history[type] = temp;
4657 }
4658 }
4659 hislen = newlen;
4660 }
4661}
4662
4663/*
4664 * Check if command line 'str' is already in history.
4665 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4666 */
4667 static int
4668in_history(type, str, move_to_front)
4669 int type;
4670 char_u *str;
4671 int move_to_front; /* Move the entry to the front if it exists */
4672{
4673 int i;
4674 int last_i = -1;
4675
4676 if (hisidx[type] < 0)
4677 return FALSE;
4678 i = hisidx[type];
4679 do
4680 {
4681 if (history[type][i].hisstr == NULL)
4682 return FALSE;
4683 if (STRCMP(str, history[type][i].hisstr) == 0)
4684 {
4685 if (!move_to_front)
4686 return TRUE;
4687 last_i = i;
4688 break;
4689 }
4690 if (--i < 0)
4691 i = hislen - 1;
4692 } while (i != hisidx[type]);
4693
4694 if (last_i >= 0)
4695 {
4696 str = history[type][i].hisstr;
4697 while (i != hisidx[type])
4698 {
4699 if (++i >= hislen)
4700 i = 0;
4701 history[type][last_i] = history[type][i];
4702 last_i = i;
4703 }
4704 history[type][i].hisstr = str;
4705 history[type][i].hisnum = ++hisnum[type];
4706 return TRUE;
4707 }
4708 return FALSE;
4709}
4710
4711/*
4712 * Convert history name (from table above) to its HIST_ equivalent.
4713 * When "name" is empty, return "cmd" history.
4714 * Returns -1 for unknown history name.
4715 */
4716 int
4717get_histtype(name)
4718 char_u *name;
4719{
4720 int i;
4721 int len = (int)STRLEN(name);
4722
4723 /* No argument: use current history. */
4724 if (len == 0)
4725 return hist_char2type(ccline.cmdfirstc);
4726
4727 for (i = 0; history_names[i] != NULL; ++i)
4728 if (STRNICMP(name, history_names[i], len) == 0)
4729 return i;
4730
4731 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
4732 return hist_char2type(name[0]);
4733
4734 return -1;
4735}
4736
4737static int last_maptick = -1; /* last seen maptick */
4738
4739/*
4740 * Add the given string to the given history. If the string is already in the
4741 * history then it is moved to the front. "histype" may be one of he HIST_
4742 * values.
4743 */
4744 void
4745add_to_history(histype, new_entry, in_map, sep)
4746 int histype;
4747 char_u *new_entry;
4748 int in_map; /* consider maptick when inside a mapping */
4749 int sep; /* separator character used (search hist) */
4750{
4751 histentry_T *hisptr;
4752 int len;
4753
4754 if (hislen == 0) /* no history */
4755 return;
4756
4757 /*
4758 * Searches inside the same mapping overwrite each other, so that only
4759 * the last line is kept. Be careful not to remove a line that was moved
4760 * down, only lines that were added.
4761 */
4762 if (histype == HIST_SEARCH && in_map)
4763 {
4764 if (maptick == last_maptick)
4765 {
4766 /* Current line is from the same mapping, remove it */
4767 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
4768 vim_free(hisptr->hisstr);
4769 hisptr->hisstr = NULL;
4770 hisptr->hisnum = 0;
4771 --hisnum[histype];
4772 if (--hisidx[HIST_SEARCH] < 0)
4773 hisidx[HIST_SEARCH] = hislen - 1;
4774 }
4775 last_maptick = -1;
4776 }
4777 if (!in_history(histype, new_entry, TRUE))
4778 {
4779 if (++hisidx[histype] == hislen)
4780 hisidx[histype] = 0;
4781 hisptr = &history[histype][hisidx[histype]];
4782 vim_free(hisptr->hisstr);
4783
4784 /* Store the separator after the NUL of the string. */
4785 len = STRLEN(new_entry);
4786 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
4787 if (hisptr->hisstr != NULL)
4788 hisptr->hisstr[len + 1] = sep;
4789
4790 hisptr->hisnum = ++hisnum[histype];
4791 if (histype == HIST_SEARCH && in_map)
4792 last_maptick = maptick;
4793 }
4794}
4795
4796#if defined(FEAT_EVAL) || defined(PROTO)
4797
4798/*
4799 * Get identifier of newest history entry.
4800 * "histype" may be one of the HIST_ values.
4801 */
4802 int
4803get_history_idx(histype)
4804 int histype;
4805{
4806 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4807 || hisidx[histype] < 0)
4808 return -1;
4809
4810 return history[histype][hisidx[histype]].hisnum;
4811}
4812
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004813static struct cmdline_info *get_ccline_ptr __ARGS((void));
4814
4815/*
4816 * Get pointer to the command line info to use. cmdline_paste() may clear
4817 * ccline and put the previous value in prev_ccline.
4818 */
4819 static struct cmdline_info *
4820get_ccline_ptr()
4821{
4822 if ((State & CMDLINE) == 0)
4823 return NULL;
4824 if (ccline.cmdbuff != NULL)
4825 return &ccline;
4826 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
4827 return &prev_ccline;
4828 return NULL;
4829}
4830
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831/*
4832 * Get the current command line in allocated memory.
4833 * Only works when the command line is being edited.
4834 * Returns NULL when something is wrong.
4835 */
4836 char_u *
4837get_cmdline_str()
4838{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004839 struct cmdline_info *p = get_ccline_ptr();
4840
4841 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004843 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844}
4845
4846/*
4847 * Get the current command line position, counted in bytes.
4848 * Zero is the first position.
4849 * Only works when the command line is being edited.
4850 * Returns -1 when something is wrong.
4851 */
4852 int
4853get_cmdline_pos()
4854{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004855 struct cmdline_info *p = get_ccline_ptr();
4856
4857 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004859 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860}
4861
4862/*
4863 * Set the command line byte position to "pos". Zero is the first position.
4864 * Only works when the command line is being edited.
4865 * Returns 1 when failed, 0 when OK.
4866 */
4867 int
4868set_cmdline_pos(pos)
4869 int pos;
4870{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004871 struct cmdline_info *p = get_ccline_ptr();
4872
4873 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 return 1;
4875
4876 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
4877 * changed the command line. */
4878 if (pos < 0)
4879 new_cmdpos = 0;
4880 else
4881 new_cmdpos = pos;
4882 return 0;
4883}
4884
4885/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004886 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00004887 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004888 * Only works when the command line is being edited.
4889 * Returns NUL when something is wrong.
4890 */
4891 int
4892get_cmdline_type()
4893{
4894 struct cmdline_info *p = get_ccline_ptr();
4895
4896 if (p == NULL)
4897 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00004898 if (p->cmdfirstc == NUL)
4899 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004900 return p->cmdfirstc;
4901}
4902
4903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 * Calculate history index from a number:
4905 * num > 0: seen as identifying number of a history entry
4906 * num < 0: relative position in history wrt newest entry
4907 * "histype" may be one of the HIST_ values.
4908 */
4909 static int
4910calc_hist_idx(histype, num)
4911 int histype;
4912 int num;
4913{
4914 int i;
4915 histentry_T *hist;
4916 int wrapped = FALSE;
4917
4918 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4919 || (i = hisidx[histype]) < 0 || num == 0)
4920 return -1;
4921
4922 hist = history[histype];
4923 if (num > 0)
4924 {
4925 while (hist[i].hisnum > num)
4926 if (--i < 0)
4927 {
4928 if (wrapped)
4929 break;
4930 i += hislen;
4931 wrapped = TRUE;
4932 }
4933 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
4934 return i;
4935 }
4936 else if (-num <= hislen)
4937 {
4938 i += num + 1;
4939 if (i < 0)
4940 i += hislen;
4941 if (hist[i].hisstr != NULL)
4942 return i;
4943 }
4944 return -1;
4945}
4946
4947/*
4948 * Get a history entry by its index.
4949 * "histype" may be one of the HIST_ values.
4950 */
4951 char_u *
4952get_history_entry(histype, idx)
4953 int histype;
4954 int idx;
4955{
4956 idx = calc_hist_idx(histype, idx);
4957 if (idx >= 0)
4958 return history[histype][idx].hisstr;
4959 else
4960 return (char_u *)"";
4961}
4962
4963/*
4964 * Clear all entries of a history.
4965 * "histype" may be one of the HIST_ values.
4966 */
4967 int
4968clr_history(histype)
4969 int histype;
4970{
4971 int i;
4972 histentry_T *hisptr;
4973
4974 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
4975 {
4976 hisptr = history[histype];
4977 for (i = hislen; i--;)
4978 {
4979 vim_free(hisptr->hisstr);
4980 hisptr->hisnum = 0;
4981 hisptr++->hisstr = NULL;
4982 }
4983 hisidx[histype] = -1; /* mark history as cleared */
4984 hisnum[histype] = 0; /* reset identifier counter */
4985 return OK;
4986 }
4987 return FAIL;
4988}
4989
4990/*
4991 * Remove all entries matching {str} from a history.
4992 * "histype" may be one of the HIST_ values.
4993 */
4994 int
4995del_history_entry(histype, str)
4996 int histype;
4997 char_u *str;
4998{
4999 regmatch_T regmatch;
5000 histentry_T *hisptr;
5001 int idx;
5002 int i;
5003 int last;
5004 int found = FALSE;
5005
5006 regmatch.regprog = NULL;
5007 regmatch.rm_ic = FALSE; /* always match case */
5008 if (hislen != 0
5009 && histype >= 0
5010 && histype < HIST_COUNT
5011 && *str != NUL
5012 && (idx = hisidx[histype]) >= 0
5013 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5014 != NULL)
5015 {
5016 i = last = idx;
5017 do
5018 {
5019 hisptr = &history[histype][i];
5020 if (hisptr->hisstr == NULL)
5021 break;
5022 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5023 {
5024 found = TRUE;
5025 vim_free(hisptr->hisstr);
5026 hisptr->hisstr = NULL;
5027 hisptr->hisnum = 0;
5028 }
5029 else
5030 {
5031 if (i != last)
5032 {
5033 history[histype][last] = *hisptr;
5034 hisptr->hisstr = NULL;
5035 hisptr->hisnum = 0;
5036 }
5037 if (--last < 0)
5038 last += hislen;
5039 }
5040 if (--i < 0)
5041 i += hislen;
5042 } while (i != idx);
5043 if (history[histype][idx].hisstr == NULL)
5044 hisidx[histype] = -1;
5045 }
5046 vim_free(regmatch.regprog);
5047 return found;
5048}
5049
5050/*
5051 * Remove an indexed entry from a history.
5052 * "histype" may be one of the HIST_ values.
5053 */
5054 int
5055del_history_idx(histype, idx)
5056 int histype;
5057 int idx;
5058{
5059 int i, j;
5060
5061 i = calc_hist_idx(histype, idx);
5062 if (i < 0)
5063 return FALSE;
5064 idx = hisidx[histype];
5065 vim_free(history[histype][i].hisstr);
5066
5067 /* When deleting the last added search string in a mapping, reset
5068 * last_maptick, so that the last added search string isn't deleted again.
5069 */
5070 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5071 last_maptick = -1;
5072
5073 while (i != idx)
5074 {
5075 j = (i + 1) % hislen;
5076 history[histype][i] = history[histype][j];
5077 i = j;
5078 }
5079 history[histype][i].hisstr = NULL;
5080 history[histype][i].hisnum = 0;
5081 if (--i < 0)
5082 i += hislen;
5083 hisidx[histype] = i;
5084 return TRUE;
5085}
5086
5087#endif /* FEAT_EVAL */
5088
5089#if defined(FEAT_CRYPT) || defined(PROTO)
5090/*
5091 * Very specific function to remove the value in ":set key=val" from the
5092 * history.
5093 */
5094 void
5095remove_key_from_history()
5096{
5097 char_u *p;
5098 int i;
5099
5100 i = hisidx[HIST_CMD];
5101 if (i < 0)
5102 return;
5103 p = history[HIST_CMD][i].hisstr;
5104 if (p != NULL)
5105 for ( ; *p; ++p)
5106 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5107 {
5108 p = vim_strchr(p + 3, '=');
5109 if (p == NULL)
5110 break;
5111 ++p;
5112 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5113 if (p[i] == '\\' && p[i + 1])
5114 ++i;
5115 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5116 --p;
5117 }
5118}
5119#endif
5120
5121#endif /* FEAT_CMDHIST */
5122
5123#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5124/*
5125 * Get indices "num1,num2" that specify a range within a list (not a range of
5126 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5127 * Returns OK if parsed successfully, otherwise FAIL.
5128 */
5129 int
5130get_list_range(str, num1, num2)
5131 char_u **str;
5132 int *num1;
5133 int *num2;
5134{
5135 int len;
5136 int first = FALSE;
5137 long num;
5138
5139 *str = skipwhite(*str);
5140 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5141 {
5142 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5143 *str += len;
5144 *num1 = (int)num;
5145 first = TRUE;
5146 }
5147 *str = skipwhite(*str);
5148 if (**str == ',') /* parse "to" part of range */
5149 {
5150 *str = skipwhite(*str + 1);
5151 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5152 if (len > 0)
5153 {
5154 *num2 = (int)num;
5155 *str = skipwhite(*str + len);
5156 }
5157 else if (!first) /* no number given at all */
5158 return FAIL;
5159 }
5160 else if (first) /* only one number given */
5161 *num2 = *num1;
5162 return OK;
5163}
5164#endif
5165
5166#if defined(FEAT_CMDHIST) || defined(PROTO)
5167/*
5168 * :history command - print a history
5169 */
5170 void
5171ex_history(eap)
5172 exarg_T *eap;
5173{
5174 histentry_T *hist;
5175 int histype1 = HIST_CMD;
5176 int histype2 = HIST_CMD;
5177 int hisidx1 = 1;
5178 int hisidx2 = -1;
5179 int idx;
5180 int i, j, k;
5181 char_u *end;
5182 char_u *arg = eap->arg;
5183
5184 if (hislen == 0)
5185 {
5186 MSG(_("'history' option is zero"));
5187 return;
5188 }
5189
5190 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5191 {
5192 end = arg;
5193 while (ASCII_ISALPHA(*end)
5194 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5195 end++;
5196 i = *end;
5197 *end = NUL;
5198 histype1 = get_histtype(arg);
5199 if (histype1 == -1)
5200 {
5201 if (STRICMP(arg, "all") == 0)
5202 {
5203 histype1 = 0;
5204 histype2 = HIST_COUNT-1;
5205 }
5206 else
5207 {
5208 *end = i;
5209 EMSG(_(e_trailing));
5210 return;
5211 }
5212 }
5213 else
5214 histype2 = histype1;
5215 *end = i;
5216 }
5217 else
5218 end = arg;
5219 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5220 {
5221 EMSG(_(e_trailing));
5222 return;
5223 }
5224
5225 for (; !got_int && histype1 <= histype2; ++histype1)
5226 {
5227 STRCPY(IObuff, "\n # ");
5228 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5229 MSG_PUTS_TITLE(IObuff);
5230 idx = hisidx[histype1];
5231 hist = history[histype1];
5232 j = hisidx1;
5233 k = hisidx2;
5234 if (j < 0)
5235 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5236 if (k < 0)
5237 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5238 if (idx >= 0 && j <= k)
5239 for (i = idx + 1; !got_int; ++i)
5240 {
5241 if (i == hislen)
5242 i = 0;
5243 if (hist[i].hisstr != NULL
5244 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5245 {
5246 msg_putchar('\n');
5247 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5248 hist[i].hisnum);
5249 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5250 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5251 (int)Columns - 10);
5252 else
5253 STRCAT(IObuff, hist[i].hisstr);
5254 msg_outtrans(IObuff);
5255 out_flush();
5256 }
5257 if (i == idx)
5258 break;
5259 }
5260 }
5261}
5262#endif
5263
5264#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5265static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5266static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5267static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5268static int viminfo_add_at_front = FALSE;
5269
5270static int hist_type2char __ARGS((int type, int use_question));
5271
5272/*
5273 * Translate a history type number to the associated character.
5274 */
5275 static int
5276hist_type2char(type, use_question)
5277 int type;
5278 int use_question; /* use '?' instead of '/' */
5279{
5280 if (type == HIST_CMD)
5281 return ':';
5282 if (type == HIST_SEARCH)
5283 {
5284 if (use_question)
5285 return '?';
5286 else
5287 return '/';
5288 }
5289 if (type == HIST_EXPR)
5290 return '=';
5291 return '@';
5292}
5293
5294/*
5295 * Prepare for reading the history from the viminfo file.
5296 * This allocates history arrays to store the read history lines.
5297 */
5298 void
5299prepare_viminfo_history(asklen)
5300 int asklen;
5301{
5302 int i;
5303 int num;
5304 int type;
5305 int len;
5306
5307 init_history();
5308 viminfo_add_at_front = (asklen != 0);
5309 if (asklen > hislen)
5310 asklen = hislen;
5311
5312 for (type = 0; type < HIST_COUNT; ++type)
5313 {
5314 /*
5315 * Count the number of empty spaces in the history list. If there are
5316 * more spaces available than we request, then fill them up.
5317 */
5318 for (i = 0, num = 0; i < hislen; i++)
5319 if (history[type][i].hisstr == NULL)
5320 num++;
5321 len = asklen;
5322 if (num > len)
5323 len = num;
5324 if (len <= 0)
5325 viminfo_history[type] = NULL;
5326 else
5327 viminfo_history[type] =
5328 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5329 if (viminfo_history[type] == NULL)
5330 len = 0;
5331 viminfo_hislen[type] = len;
5332 viminfo_hisidx[type] = 0;
5333 }
5334}
5335
5336/*
5337 * Accept a line from the viminfo, store it in the history array when it's
5338 * new.
5339 */
5340 int
5341read_viminfo_history(virp)
5342 vir_T *virp;
5343{
5344 int type;
5345 long_u len;
5346 char_u *val;
5347 char_u *p;
5348
5349 type = hist_char2type(virp->vir_line[0]);
5350 if (viminfo_hisidx[type] < viminfo_hislen[type])
5351 {
5352 val = viminfo_readstring(virp, 1, TRUE);
5353 if (val != NULL && *val != NUL)
5354 {
5355 if (!in_history(type, val + (type == HIST_SEARCH),
5356 viminfo_add_at_front))
5357 {
5358 /* Need to re-allocate to append the separator byte. */
5359 len = STRLEN(val);
5360 p = lalloc(len + 2, TRUE);
5361 if (p != NULL)
5362 {
5363 if (type == HIST_SEARCH)
5364 {
5365 /* Search entry: Move the separator from the first
5366 * column to after the NUL. */
5367 mch_memmove(p, val + 1, (size_t)len);
5368 p[len] = (*val == ' ' ? NUL : *val);
5369 }
5370 else
5371 {
5372 /* Not a search entry: No separator in the viminfo
5373 * file, add a NUL separator. */
5374 mch_memmove(p, val, (size_t)len + 1);
5375 p[len + 1] = NUL;
5376 }
5377 viminfo_history[type][viminfo_hisidx[type]++] = p;
5378 }
5379 }
5380 }
5381 vim_free(val);
5382 }
5383 return viminfo_readline(virp);
5384}
5385
5386 void
5387finish_viminfo_history()
5388{
5389 int idx;
5390 int i;
5391 int type;
5392
5393 for (type = 0; type < HIST_COUNT; ++type)
5394 {
5395 if (history[type] == NULL)
5396 return;
5397 idx = hisidx[type] + viminfo_hisidx[type];
5398 if (idx >= hislen)
5399 idx -= hislen;
5400 else if (idx < 0)
5401 idx = hislen - 1;
5402 if (viminfo_add_at_front)
5403 hisidx[type] = idx;
5404 else
5405 {
5406 if (hisidx[type] == -1)
5407 hisidx[type] = hislen - 1;
5408 do
5409 {
5410 if (history[type][idx].hisstr != NULL)
5411 break;
5412 if (++idx == hislen)
5413 idx = 0;
5414 } while (idx != hisidx[type]);
5415 if (idx != hisidx[type] && --idx < 0)
5416 idx = hislen - 1;
5417 }
5418 for (i = 0; i < viminfo_hisidx[type]; i++)
5419 {
5420 vim_free(history[type][idx].hisstr);
5421 history[type][idx].hisstr = viminfo_history[type][i];
5422 if (--idx < 0)
5423 idx = hislen - 1;
5424 }
5425 idx += 1;
5426 idx %= hislen;
5427 for (i = 0; i < viminfo_hisidx[type]; i++)
5428 {
5429 history[type][idx++].hisnum = ++hisnum[type];
5430 idx %= hislen;
5431 }
5432 vim_free(viminfo_history[type]);
5433 viminfo_history[type] = NULL;
5434 }
5435}
5436
5437 void
5438write_viminfo_history(fp)
5439 FILE *fp;
5440{
5441 int i;
5442 int type;
5443 int num_saved;
5444 char_u *p;
5445 int c;
5446
5447 init_history();
5448 if (hislen == 0)
5449 return;
5450 for (type = 0; type < HIST_COUNT; ++type)
5451 {
5452 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5453 if (num_saved == 0)
5454 continue;
5455 if (num_saved < 0) /* Use default */
5456 num_saved = hislen;
5457 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5458 type == HIST_CMD ? _("Command Line") :
5459 type == HIST_SEARCH ? _("Search String") :
5460 type == HIST_EXPR ? _("Expression") :
5461 _("Input Line"));
5462 if (num_saved > hislen)
5463 num_saved = hislen;
5464 i = hisidx[type];
5465 if (i >= 0)
5466 while (num_saved--)
5467 {
5468 p = history[type][i].hisstr;
5469 if (p != NULL)
5470 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005471 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 /* For the search history: put the separator in the second
5473 * column; use a space if there isn't one. */
5474 if (type == HIST_SEARCH)
5475 {
5476 c = p[STRLEN(p) + 1];
5477 putc(c == NUL ? ' ' : c, fp);
5478 }
5479 viminfo_writestring(fp, p);
5480 }
5481 if (--i < 0)
5482 i = hislen - 1;
5483 }
5484 }
5485}
5486#endif /* FEAT_VIMINFO */
5487
5488#if defined(FEAT_FKMAP) || defined(PROTO)
5489/*
5490 * Write a character at the current cursor+offset position.
5491 * It is directly written into the command buffer block.
5492 */
5493 void
5494cmd_pchar(c, offset)
5495 int c, offset;
5496{
5497 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5498 {
5499 EMSG(_("E198: cmd_pchar beyond the command length"));
5500 return;
5501 }
5502 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5503 ccline.cmdbuff[ccline.cmdlen] = NUL;
5504}
5505
5506 int
5507cmd_gchar(offset)
5508 int offset;
5509{
5510 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5511 {
5512 /* EMSG(_("cmd_gchar beyond the command length")); */
5513 return NUL;
5514 }
5515 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5516}
5517#endif
5518
5519#if defined(FEAT_CMDWIN) || defined(PROTO)
5520/*
5521 * Open a window on the current command line and history. Allow editing in
5522 * the window. Returns when the window is closed.
5523 * Returns:
5524 * CR if the command is to be executed
5525 * Ctrl_C if it is to be abandoned
5526 * K_IGNORE if editing continues
5527 */
5528 static int
5529ex_window()
5530{
5531 struct cmdline_info save_ccline;
5532 buf_T *old_curbuf = curbuf;
5533 win_T *old_curwin = curwin;
5534 buf_T *bp;
5535 win_T *wp;
5536 int i;
5537 linenr_T lnum;
5538 int histtype;
5539 garray_T winsizes;
5540 char_u typestr[2];
5541 int save_restart_edit = restart_edit;
5542 int save_State = State;
5543 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005544#ifdef FEAT_RIGHTLEFT
5545 int save_cmdmsg_rl = cmdmsg_rl;
5546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
5548 /* Can't do this recursively. Can't do it when typing a password. */
5549 if (cmdwin_type != 0
5550# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5551 || cmdline_star > 0
5552# endif
5553 )
5554 {
5555 beep_flush();
5556 return K_IGNORE;
5557 }
5558
5559 /* Save current window sizes. */
5560 win_size_save(&winsizes);
5561
5562# ifdef FEAT_AUTOCMD
5563 /* Don't execute autocommands while creating the window. */
5564 ++autocmd_block;
5565# endif
5566 /* Create a window for the command-line buffer. */
5567 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5568 {
5569 beep_flush();
5570 return K_IGNORE;
5571 }
5572 cmdwin_type = ccline.cmdfirstc;
5573 if (cmdwin_type == NUL)
5574 cmdwin_type = '-';
5575
5576 /* Create the command-line buffer empty. */
5577 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5578 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5579 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5580 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5581 curbuf->b_p_ma = TRUE;
5582# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005583 curwin->w_p_rl = cmdmsg_rl;
5584 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585# endif
5586# ifdef FEAT_SCROLLBIND
5587 curwin->w_p_scb = FALSE;
5588# endif
5589
5590# ifdef FEAT_AUTOCMD
5591 /* Do execute autocommands for setting the filetype (load syntax). */
5592 --autocmd_block;
5593# endif
5594
Bram Moolenaar46152342005-09-07 21:18:43 +00005595 /* Showing the prompt may have set need_wait_return, reset it. */
5596 need_wait_return = FALSE;
5597
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 histtype = hist_char2type(ccline.cmdfirstc);
5599 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5600 {
5601 if (p_wc == TAB)
5602 {
5603 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5604 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5605 }
5606 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5607 }
5608
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005609 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5610 * sets 'textwidth' to 78). */
5611 curbuf->b_p_tw = 0;
5612
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 /* Fill the buffer with the history. */
5614 init_history();
5615 if (hislen > 0)
5616 {
5617 i = hisidx[histtype];
5618 if (i >= 0)
5619 {
5620 lnum = 0;
5621 do
5622 {
5623 if (++i == hislen)
5624 i = 0;
5625 if (history[histtype][i].hisstr != NULL)
5626 ml_append(lnum++, history[histtype][i].hisstr,
5627 (colnr_T)0, FALSE);
5628 }
5629 while (i != hisidx[histtype]);
5630 }
5631 }
5632
5633 /* Replace the empty last line with the current command-line and put the
5634 * cursor there. */
5635 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5636 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5637 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00005638 changed_line_abv_curs();
5639 invalidate_botline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 redraw_later(NOT_VALID);
5641
5642 /* Save the command line info, can be used recursively. */
5643 save_ccline = ccline;
5644 ccline.cmdbuff = NULL;
5645 ccline.cmdprompt = NULL;
5646
5647 /* No Ex mode here! */
5648 exmode_active = 0;
5649
5650 State = NORMAL;
5651# ifdef FEAT_MOUSE
5652 setmouse();
5653# endif
5654
5655# ifdef FEAT_AUTOCMD
5656 /* Trigger CmdwinEnter autocommands. */
5657 typestr[0] = cmdwin_type;
5658 typestr[1] = NUL;
5659 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
5660# endif
5661
5662 i = RedrawingDisabled;
5663 RedrawingDisabled = 0;
5664
5665 /*
5666 * Call the main loop until <CR> or CTRL-C is typed.
5667 */
5668 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005669 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
5671 RedrawingDisabled = i;
5672
5673# ifdef FEAT_AUTOCMD
5674 /* Trigger CmdwinLeave autocommands. */
5675 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
5676# endif
5677
5678 /* Restore the comand line info. */
5679 ccline = save_ccline;
5680 cmdwin_type = 0;
5681
5682 exmode_active = save_exmode;
5683
5684 /* Safety check: The old window or buffer was deleted: It's a a bug when
5685 * this happens! */
5686 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
5687 {
5688 cmdwin_result = Ctrl_C;
5689 EMSG(_("E199: Active window or buffer deleted"));
5690 }
5691 else
5692 {
5693# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5694 /* autocmds may abort script processing */
5695 if (aborting() && cmdwin_result != K_IGNORE)
5696 cmdwin_result = Ctrl_C;
5697# endif
5698 /* Set the new command line from the cmdline buffer. */
5699 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00005700 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
Bram Moolenaar46152342005-09-07 21:18:43 +00005702 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
5703
5704 if (histtype == HIST_CMD)
5705 {
5706 /* Execute the command directly. */
5707 ccline.cmdbuff = vim_strsave((char_u *)p);
5708 cmdwin_result = CAR;
5709 }
5710 else
5711 {
5712 /* First need to cancel what we were doing. */
5713 ccline.cmdbuff = NULL;
5714 stuffcharReadbuff(':');
5715 stuffReadbuff((char_u *)p);
5716 stuffcharReadbuff(CAR);
5717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 }
5719 else if (cmdwin_result == K_XF2) /* :qa typed */
5720 {
5721 ccline.cmdbuff = vim_strsave((char_u *)"qa");
5722 cmdwin_result = CAR;
5723 }
5724 else
5725 ccline.cmdbuff = vim_strsave(ml_get_curline());
5726 if (ccline.cmdbuff == NULL)
5727 cmdwin_result = Ctrl_C;
5728 else
5729 {
5730 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
5731 ccline.cmdbufflen = ccline.cmdlen + 1;
5732 ccline.cmdpos = curwin->w_cursor.col;
5733 if (ccline.cmdpos > ccline.cmdlen)
5734 ccline.cmdpos = ccline.cmdlen;
5735 if (cmdwin_result == K_IGNORE)
5736 {
5737 set_cmdspos_cursor();
5738 redrawcmd();
5739 }
5740 }
5741
5742# ifdef FEAT_AUTOCMD
5743 /* Don't execute autocommands while deleting the window. */
5744 ++autocmd_block;
5745# endif
5746 wp = curwin;
5747 bp = curbuf;
5748 win_goto(old_curwin);
5749 win_close(wp, TRUE);
5750 close_buffer(NULL, bp, DOBUF_WIPE);
5751
5752 /* Restore window sizes. */
5753 win_size_restore(&winsizes);
5754
5755# ifdef FEAT_AUTOCMD
5756 --autocmd_block;
5757# endif
5758 }
5759
5760 ga_clear(&winsizes);
5761 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00005762# ifdef FEAT_RIGHTLEFT
5763 cmdmsg_rl = save_cmdmsg_rl;
5764# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
5766 State = save_State;
5767# ifdef FEAT_MOUSE
5768 setmouse();
5769# endif
5770
5771 return cmdwin_result;
5772}
5773#endif /* FEAT_CMDWIN */
5774
5775/*
5776 * Used for commands that either take a simple command string argument, or:
5777 * cmd << endmarker
5778 * {script}
5779 * endmarker
5780 * Returns a pointer to allocated memory with {script} or NULL.
5781 */
5782 char_u *
5783script_get(eap, cmd)
5784 exarg_T *eap;
5785 char_u *cmd;
5786{
5787 char_u *theline;
5788 char *end_pattern = NULL;
5789 char dot[] = ".";
5790 garray_T ga;
5791
5792 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
5793 return NULL;
5794
5795 ga_init2(&ga, 1, 0x400);
5796
5797 if (cmd[2] != NUL)
5798 end_pattern = (char *)skipwhite(cmd + 2);
5799 else
5800 end_pattern = dot;
5801
5802 for (;;)
5803 {
5804 theline = eap->getline(
5805#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00005806 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807#endif
5808 NUL, eap->cookie, 0);
5809
5810 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
5811 break;
5812
5813 ga_concat(&ga, theline);
5814 ga_append(&ga, '\n');
5815 vim_free(theline);
5816 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00005817 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818
5819 return (char_u *)ga.ga_data;
5820}