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