blob: c6190bed6b34f93f4544c187776f0c6ebb3075c9 [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 */
2372 if (len * 2 > buflen)
2373 {
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 Moolenaar071d4272004-06-13 20:20:40 +00002377 buflen = len * 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
2383 for (j = start; j < start + len; j += mb_l)
2384 {
2385 p = ccline.cmdbuff + j;
2386 u8c = utfc_ptr2char_len(p, &u8c_c1, &u8c_c2, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002387 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 if (ARABIC_CHAR(u8c))
2389 {
2390 /* Do Arabic shaping. */
2391 if (cmdmsg_rl)
2392 {
2393 /* displaying from right to left */
2394 pc = prev_c;
2395 pc1 = prev_c1;
2396 prev_c1 = u8c_c1;
2397 if (j + mb_l >= start + len)
2398 nc = NUL;
2399 else
2400 nc = utf_ptr2char(p + mb_l);
2401 }
2402 else
2403 {
2404 /* displaying from left to right */
2405 if (j + mb_l >= start + len)
2406 pc = NUL;
2407 else
2408 pc = utfc_ptr2char_len(p + mb_l, &pc1, &dummy,
2409 start + len - j - mb_l);
2410 nc = prev_c;
2411 }
2412 prev_c = u8c;
2413
2414 u8c = arabic_shape(u8c, NULL, &u8c_c1, pc, pc1, nc);
2415
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002416 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (u8c_c1 != 0)
2418 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002419 newlen += (*mb_char2bytes)(u8c_c1, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 if (u8c_c2 != 0)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002421 newlen += (*mb_char2bytes)(u8c_c2,
2422 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 }
2424 }
2425 else
2426 {
2427 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002428 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 newlen += mb_l;
2430 }
2431 }
2432
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002433 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435 else
2436#endif
2437 msg_outtrans_len(ccline.cmdbuff + start, len);
2438}
2439
2440/*
2441 * Put a character on the command line. Shifts the following text to the
2442 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2443 * "c" must be printable (fit in one display cell)!
2444 */
2445 void
2446putcmdline(c, shift)
2447 int c;
2448 int shift;
2449{
2450 if (cmd_silent)
2451 return;
2452 msg_no_more = TRUE;
2453 msg_putchar(c);
2454 if (shift)
2455 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2456 msg_no_more = FALSE;
2457 cursorcmd();
2458}
2459
2460/*
2461 * Undo a putcmdline(c, FALSE).
2462 */
2463 void
2464unputcmdline()
2465{
2466 if (cmd_silent)
2467 return;
2468 msg_no_more = TRUE;
2469 if (ccline.cmdlen == ccline.cmdpos)
2470 msg_putchar(' ');
2471 else
2472 draw_cmdline(ccline.cmdpos, 1);
2473 msg_no_more = FALSE;
2474 cursorcmd();
2475}
2476
2477/*
2478 * Put the given string, of the given length, onto the command line.
2479 * If len is -1, then STRLEN() is used to calculate the length.
2480 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2481 * part will be redrawn, otherwise it will not. If this function is called
2482 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2483 * called afterwards.
2484 */
2485 int
2486put_on_cmdline(str, len, redraw)
2487 char_u *str;
2488 int len;
2489 int redraw;
2490{
2491 int retval;
2492 int i;
2493 int m;
2494 int c;
2495
2496 if (len < 0)
2497 len = (int)STRLEN(str);
2498
2499 /* Check if ccline.cmdbuff needs to be longer */
2500 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2501 retval = realloc_cmdbuff(ccline.cmdlen + len);
2502 else
2503 retval = OK;
2504 if (retval == OK)
2505 {
2506 if (!ccline.overstrike)
2507 {
2508 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2509 ccline.cmdbuff + ccline.cmdpos,
2510 (size_t)(ccline.cmdlen - ccline.cmdpos));
2511 ccline.cmdlen += len;
2512 }
2513 else
2514 {
2515#ifdef FEAT_MBYTE
2516 if (has_mbyte)
2517 {
2518 /* Count nr of characters in the new string. */
2519 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002520 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 ++m;
2522 /* Count nr of bytes in cmdline that are overwritten by these
2523 * characters. */
2524 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002525 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 --m;
2527 if (i < ccline.cmdlen)
2528 {
2529 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2530 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2531 ccline.cmdlen += ccline.cmdpos + len - i;
2532 }
2533 else
2534 ccline.cmdlen = ccline.cmdpos + len;
2535 }
2536 else
2537#endif
2538 if (ccline.cmdpos + len > ccline.cmdlen)
2539 ccline.cmdlen = ccline.cmdpos + len;
2540 }
2541 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2542 ccline.cmdbuff[ccline.cmdlen] = NUL;
2543
2544#ifdef FEAT_MBYTE
2545 if (enc_utf8)
2546 {
2547 /* When the inserted text starts with a composing character,
2548 * backup to the character before it. There could be two of them.
2549 */
2550 i = 0;
2551 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2552 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2553 {
2554 i = (*mb_head_off)(ccline.cmdbuff,
2555 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2556 ccline.cmdpos -= i;
2557 len += i;
2558 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2559 }
2560# ifdef FEAT_ARABIC
2561 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2562 {
2563 /* Check the previous character for Arabic combining pair. */
2564 i = (*mb_head_off)(ccline.cmdbuff,
2565 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2566 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2567 + ccline.cmdpos - i), c))
2568 {
2569 ccline.cmdpos -= i;
2570 len += i;
2571 }
2572 else
2573 i = 0;
2574 }
2575# endif
2576 if (i != 0)
2577 {
2578 /* Also backup the cursor position. */
2579 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2580 ccline.cmdspos -= i;
2581 msg_col -= i;
2582 if (msg_col < 0)
2583 {
2584 msg_col += Columns;
2585 --msg_row;
2586 }
2587 }
2588 }
2589#endif
2590
2591 if (redraw && !cmd_silent)
2592 {
2593 msg_no_more = TRUE;
2594 i = cmdline_row;
2595 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2596 /* Avoid clearing the rest of the line too often. */
2597 if (cmdline_row != i || ccline.overstrike)
2598 msg_clr_eos();
2599 msg_no_more = FALSE;
2600 }
2601#ifdef FEAT_FKMAP
2602 /*
2603 * If we are in Farsi command mode, the character input must be in
2604 * Insert mode. So do not advance the cmdpos.
2605 */
2606 if (!cmd_fkmap)
2607#endif
2608 {
2609 if (KeyTyped)
2610 m = Columns * Rows;
2611 else
2612 m = MAXCOL;
2613 for (i = 0; i < len; ++i)
2614 {
2615 c = cmdline_charsize(ccline.cmdpos);
2616#ifdef FEAT_MBYTE
2617 /* count ">" for a double-wide char that doesn't fit. */
2618 if (has_mbyte)
2619 correct_cmdspos(ccline.cmdpos, c);
2620#endif
2621 /* Stop cursor at the end of the screen */
2622 if (ccline.cmdspos + c >= m)
2623 break;
2624 ccline.cmdspos += c;
2625#ifdef FEAT_MBYTE
2626 if (has_mbyte)
2627 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002628 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 if (c > len - i - 1)
2630 c = len - i - 1;
2631 ccline.cmdpos += c;
2632 i += c;
2633 }
2634#endif
2635 ++ccline.cmdpos;
2636 }
2637 }
2638 }
2639 if (redraw)
2640 msg_check();
2641 return retval;
2642}
2643
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002644static struct cmdline_info prev_ccline;
2645static int prev_ccline_used = FALSE;
2646
2647/*
2648 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2649 * and overwrite it. But get_cmdline_str() may need it, thus make it
2650 * available globally in prev_ccline.
2651 */
2652 static void
2653save_cmdline(ccp)
2654 struct cmdline_info *ccp;
2655{
2656 if (!prev_ccline_used)
2657 {
2658 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2659 prev_ccline_used = TRUE;
2660 }
2661 *ccp = prev_ccline;
2662 prev_ccline = ccline;
2663 ccline.cmdbuff = NULL;
2664 ccline.cmdprompt = NULL;
2665}
2666
2667/*
2668 * Resture ccline after it has been saved with save_cmdline().
2669 */
2670 static void
2671restore_cmdline(ccp)
2672 struct cmdline_info *ccp;
2673{
2674 ccline = prev_ccline;
2675 prev_ccline = *ccp;
2676}
2677
Bram Moolenaar8299df92004-07-10 09:47:34 +00002678/*
2679 * paste a yank register into the command line.
2680 * used by CTRL-R command in command-line mode
2681 * insert_reg() can't be used here, because special characters from the
2682 * register contents will be interpreted as commands.
2683 *
2684 * return FAIL for failure, OK otherwise
2685 */
2686 static int
2687cmdline_paste(regname, literally)
2688 int regname;
2689 int literally; /* Insert text literally instead of "as typed" */
2690{
2691 long i;
2692 char_u *arg;
2693 int allocated;
2694 struct cmdline_info save_ccline;
2695
2696 /* check for valid regname; also accept special characters for CTRL-R in
2697 * the command line */
2698 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2699 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2700 return FAIL;
2701
2702 /* A register containing CTRL-R can cause an endless loop. Allow using
2703 * CTRL-C to break the loop. */
2704 line_breakcheck();
2705 if (got_int)
2706 return FAIL;
2707
2708#ifdef FEAT_CLIPBOARD
2709 regname = may_get_selection(regname);
2710#endif
2711
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002712 /* Need to save and restore ccline. */
2713 save_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002714 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002715 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002716
2717 if (i)
2718 {
2719 /* Got the value of a special register in "arg". */
2720 if (arg == NULL)
2721 return FAIL;
2722 cmdline_paste_str(arg, literally);
2723 if (allocated)
2724 vim_free(arg);
2725 return OK;
2726 }
2727
2728 return cmdline_paste_reg(regname, literally);
2729}
2730
2731/*
2732 * Put a string on the command line.
2733 * When "literally" is TRUE, insert literally.
2734 * When "literally" is FALSE, insert as typed, but don't leave the command
2735 * line.
2736 */
2737 void
2738cmdline_paste_str(s, literally)
2739 char_u *s;
2740 int literally;
2741{
2742 int c, cv;
2743
2744 if (literally)
2745 put_on_cmdline(s, -1, TRUE);
2746 else
2747 while (*s != NUL)
2748 {
2749 cv = *s;
2750 if (cv == Ctrl_V && s[1])
2751 ++s;
2752#ifdef FEAT_MBYTE
2753 if (has_mbyte)
2754 {
2755 c = mb_ptr2char(s);
2756 s += mb_char2len(c);
2757 }
2758 else
2759#endif
2760 c = *s++;
2761 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
2762#ifdef UNIX
2763 || c == intr_char
2764#endif
2765 || (c == Ctrl_BSL && *s == Ctrl_N))
2766 stuffcharReadbuff(Ctrl_V);
2767 stuffcharReadbuff(c);
2768 }
2769}
2770
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771#ifdef FEAT_WILDMENU
2772/*
2773 * Delete characters on the command line, from "from" to the current
2774 * position.
2775 */
2776 static void
2777cmdline_del(from)
2778 int from;
2779{
2780 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
2781 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
2782 ccline.cmdlen -= ccline.cmdpos - from;
2783 ccline.cmdpos = from;
2784}
2785#endif
2786
2787/*
2788 * this fuction is called when the screen size changes and with incremental
2789 * search
2790 */
2791 void
2792redrawcmdline()
2793{
2794 if (cmd_silent)
2795 return;
2796 need_wait_return = FALSE;
2797 compute_cmdrow();
2798 redrawcmd();
2799 cursorcmd();
2800}
2801
2802 static void
2803redrawcmdprompt()
2804{
2805 int i;
2806
2807 if (cmd_silent)
2808 return;
2809 if (ccline.cmdfirstc)
2810 msg_putchar(ccline.cmdfirstc);
2811 if (ccline.cmdprompt != NULL)
2812 {
2813 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
2814 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
2815 /* do the reverse of set_cmdspos() */
2816 if (ccline.cmdfirstc)
2817 --ccline.cmdindent;
2818 }
2819 else
2820 for (i = ccline.cmdindent; i > 0; --i)
2821 msg_putchar(' ');
2822}
2823
2824/*
2825 * Redraw what is currently on the command line.
2826 */
2827 void
2828redrawcmd()
2829{
2830 if (cmd_silent)
2831 return;
2832
2833 msg_start();
2834 redrawcmdprompt();
2835
2836 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
2837 msg_no_more = TRUE;
2838 draw_cmdline(0, ccline.cmdlen);
2839 msg_clr_eos();
2840 msg_no_more = FALSE;
2841
2842 set_cmdspos_cursor();
2843
2844 /*
2845 * An emsg() before may have set msg_scroll. This is used in normal mode,
2846 * in cmdline mode we can reset them now.
2847 */
2848 msg_scroll = FALSE; /* next message overwrites cmdline */
2849
2850 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
2851 * in cmdline mode */
2852 skip_redraw = FALSE;
2853}
2854
2855 void
2856compute_cmdrow()
2857{
2858 if (exmode_active || msg_scrolled)
2859 cmdline_row = Rows - 1;
2860 else
2861 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
2862 + W_STATUS_HEIGHT(lastwin);
2863}
2864
2865 static void
2866cursorcmd()
2867{
2868 if (cmd_silent)
2869 return;
2870
2871#ifdef FEAT_RIGHTLEFT
2872 if (cmdmsg_rl)
2873 {
2874 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
2875 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
2876 if (msg_row <= 0)
2877 msg_row = Rows - 1;
2878 }
2879 else
2880#endif
2881 {
2882 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
2883 msg_col = ccline.cmdspos % (int)Columns;
2884 if (msg_row >= Rows)
2885 msg_row = Rows - 1;
2886 }
2887
2888 windgoto(msg_row, msg_col);
2889#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2890 redrawcmd_preedit();
2891#endif
2892#ifdef MCH_CURSOR_SHAPE
2893 mch_update_cursor();
2894#endif
2895}
2896
2897 void
2898gotocmdline(clr)
2899 int clr;
2900{
2901 msg_start();
2902#ifdef FEAT_RIGHTLEFT
2903 if (cmdmsg_rl)
2904 msg_col = Columns - 1;
2905 else
2906#endif
2907 msg_col = 0; /* always start in column 0 */
2908 if (clr) /* clear the bottom line(s) */
2909 msg_clr_eos(); /* will reset clear_cmdline */
2910 windgoto(cmdline_row, 0);
2911}
2912
2913/*
2914 * Check the word in front of the cursor for an abbreviation.
2915 * Called when the non-id character "c" has been entered.
2916 * When an abbreviation is recognized it is removed from the text with
2917 * backspaces and the replacement string is inserted, followed by "c".
2918 */
2919 static int
2920ccheck_abbr(c)
2921 int c;
2922{
2923 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
2924 return FALSE;
2925
2926 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
2927}
2928
2929/*
2930 * Return FAIL if this is not an appropriate context in which to do
2931 * completion of anything, return OK if it is (even if there are no matches).
2932 * For the caller, this means that the character is just passed through like a
2933 * normal character (instead of being expanded). This allows :s/^I^D etc.
2934 */
2935 static int
2936nextwild(xp, type, options)
2937 expand_T *xp;
2938 int type;
2939 int options; /* extra options for ExpandOne() */
2940{
2941 int i, j;
2942 char_u *p1;
2943 char_u *p2;
2944 int oldlen;
2945 int difflen;
2946 int v;
2947
2948 if (xp->xp_numfiles == -1)
2949 {
2950 set_expand_context(xp);
2951 cmd_showtail = expand_showtail(xp);
2952 }
2953
2954 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2955 {
2956 beep_flush();
2957 return OK; /* Something illegal on command line */
2958 }
2959 if (xp->xp_context == EXPAND_NOTHING)
2960 {
2961 /* Caller can use the character as a normal char instead */
2962 return FAIL;
2963 }
2964
2965 MSG_PUTS("..."); /* show that we are busy */
2966 out_flush();
2967
2968 i = (int)(xp->xp_pattern - ccline.cmdbuff);
2969 oldlen = ccline.cmdpos - i;
2970
2971 if (type == WILD_NEXT || type == WILD_PREV)
2972 {
2973 /*
2974 * Get next/previous match for a previous expanded pattern.
2975 */
2976 p2 = ExpandOne(xp, NULL, NULL, 0, type);
2977 }
2978 else
2979 {
2980 /*
2981 * Translate string into pattern and expand it.
2982 */
2983 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
2984 p2 = NULL;
2985 else
2986 {
2987 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
2988 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
2989 |options, type);
2990 vim_free(p1);
2991 /* longest match: make sure it is not shorter (happens with :help */
2992 if (p2 != NULL && type == WILD_LONGEST)
2993 {
2994 for (j = 0; j < oldlen; ++j)
2995 if (ccline.cmdbuff[i + j] == '*'
2996 || ccline.cmdbuff[i + j] == '?')
2997 break;
2998 if ((int)STRLEN(p2) < j)
2999 {
3000 vim_free(p2);
3001 p2 = NULL;
3002 }
3003 }
3004 }
3005 }
3006
3007 if (p2 != NULL && !got_int)
3008 {
3009 difflen = (int)STRLEN(p2) - oldlen;
3010 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3011 {
3012 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3013 xp->xp_pattern = ccline.cmdbuff + i;
3014 }
3015 else
3016 v = OK;
3017 if (v == OK)
3018 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003019 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3020 &ccline.cmdbuff[ccline.cmdpos],
3021 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3022 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 ccline.cmdlen += difflen;
3024 ccline.cmdpos += difflen;
3025 }
3026 }
3027 vim_free(p2);
3028
3029 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003030 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
3032 /* When expanding a ":map" command and no matches are found, assume that
3033 * the key is supposed to be inserted literally */
3034 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3035 return FAIL;
3036
3037 if (xp->xp_numfiles <= 0 && p2 == NULL)
3038 beep_flush();
3039 else if (xp->xp_numfiles == 1)
3040 /* free expanded pattern */
3041 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3042
3043 return OK;
3044}
3045
3046/*
3047 * Do wildcard expansion on the string 'str'.
3048 * Chars that should not be expanded must be preceded with a backslash.
3049 * Return a pointer to alloced memory containing the new string.
3050 * Return NULL for failure.
3051 *
3052 * Results are cached in xp->xp_files and xp->xp_numfiles.
3053 *
3054 * mode = WILD_FREE: just free previously expanded matches
3055 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3056 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3057 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3058 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3059 * mode = WILD_ALL: return all matches concatenated
3060 * mode = WILD_LONGEST: return longest matched part
3061 *
3062 * options = WILD_LIST_NOTFOUND: list entries without a match
3063 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3064 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3065 * options = WILD_NO_BEEP: Don't beep for multiple matches
3066 * options = WILD_ADD_SLASH: add a slash after directory names
3067 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3068 * options = WILD_SILENT: don't print warning messages
3069 * options = WILD_ESCAPE: put backslash before special chars
3070 *
3071 * The variables xp->xp_context and xp->xp_backslash must have been set!
3072 */
3073 char_u *
3074ExpandOne(xp, str, orig, options, mode)
3075 expand_T *xp;
3076 char_u *str;
3077 char_u *orig; /* allocated copy of original of expanded string */
3078 int options;
3079 int mode;
3080{
3081 char_u *ss = NULL;
3082 static int findex;
3083 static char_u *orig_save = NULL; /* kept value of orig */
3084 int i;
3085 long_u len;
3086 int non_suf_match; /* number without matching suffix */
3087
3088 /*
3089 * first handle the case of using an old match
3090 */
3091 if (mode == WILD_NEXT || mode == WILD_PREV)
3092 {
3093 if (xp->xp_numfiles > 0)
3094 {
3095 if (mode == WILD_PREV)
3096 {
3097 if (findex == -1)
3098 findex = xp->xp_numfiles;
3099 --findex;
3100 }
3101 else /* mode == WILD_NEXT */
3102 ++findex;
3103
3104 /*
3105 * When wrapping around, return the original string, set findex to
3106 * -1.
3107 */
3108 if (findex < 0)
3109 {
3110 if (orig_save == NULL)
3111 findex = xp->xp_numfiles - 1;
3112 else
3113 findex = -1;
3114 }
3115 if (findex >= xp->xp_numfiles)
3116 {
3117 if (orig_save == NULL)
3118 findex = 0;
3119 else
3120 findex = -1;
3121 }
3122#ifdef FEAT_WILDMENU
3123 if (p_wmnu)
3124 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3125 findex, cmd_showtail);
3126#endif
3127 if (findex == -1)
3128 return vim_strsave(orig_save);
3129 return vim_strsave(xp->xp_files[findex]);
3130 }
3131 else
3132 return NULL;
3133 }
3134
3135/* free old names */
3136 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3137 {
3138 FreeWild(xp->xp_numfiles, xp->xp_files);
3139 xp->xp_numfiles = -1;
3140 vim_free(orig_save);
3141 orig_save = NULL;
3142 }
3143 findex = 0;
3144
3145 if (mode == WILD_FREE) /* only release file name */
3146 return NULL;
3147
3148 if (xp->xp_numfiles == -1)
3149 {
3150 vim_free(orig_save);
3151 orig_save = orig;
3152
3153 /*
3154 * Do the expansion.
3155 */
3156 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3157 options) == FAIL)
3158 {
3159#ifdef FNAME_ILLEGAL
3160 /* Illegal file name has been silently skipped. But when there
3161 * are wildcards, the real problem is that there was no match,
3162 * causing the pattern to be added, which has illegal characters.
3163 */
3164 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3165 EMSG2(_(e_nomatch2), str);
3166#endif
3167 }
3168 else if (xp->xp_numfiles == 0)
3169 {
3170 if (!(options & WILD_SILENT))
3171 EMSG2(_(e_nomatch2), str);
3172 }
3173 else
3174 {
3175 /* Escape the matches for use on the command line. */
3176 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3177
3178 /*
3179 * Check for matching suffixes in file names.
3180 */
3181 if (mode != WILD_ALL && mode != WILD_LONGEST)
3182 {
3183 if (xp->xp_numfiles)
3184 non_suf_match = xp->xp_numfiles;
3185 else
3186 non_suf_match = 1;
3187 if ((xp->xp_context == EXPAND_FILES
3188 || xp->xp_context == EXPAND_DIRECTORIES)
3189 && xp->xp_numfiles > 1)
3190 {
3191 /*
3192 * More than one match; check suffix.
3193 * The files will have been sorted on matching suffix in
3194 * expand_wildcards, only need to check the first two.
3195 */
3196 non_suf_match = 0;
3197 for (i = 0; i < 2; ++i)
3198 if (match_suffix(xp->xp_files[i]))
3199 ++non_suf_match;
3200 }
3201 if (non_suf_match != 1)
3202 {
3203 /* Can we ever get here unless it's while expanding
3204 * interactively? If not, we can get rid of this all
3205 * together. Don't really want to wait for this message
3206 * (and possibly have to hit return to continue!).
3207 */
3208 if (!(options & WILD_SILENT))
3209 EMSG(_(e_toomany));
3210 else if (!(options & WILD_NO_BEEP))
3211 beep_flush();
3212 }
3213 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3214 ss = vim_strsave(xp->xp_files[0]);
3215 }
3216 }
3217 }
3218
3219 /* Find longest common part */
3220 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3221 {
3222 for (len = 0; xp->xp_files[0][len]; ++len)
3223 {
3224 for (i = 0; i < xp->xp_numfiles; ++i)
3225 {
3226#ifdef CASE_INSENSITIVE_FILENAME
3227 if (xp->xp_context == EXPAND_DIRECTORIES
3228 || xp->xp_context == EXPAND_FILES
3229 || xp->xp_context == EXPAND_BUFFERS)
3230 {
3231 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3232 TOLOWER_LOC(xp->xp_files[0][len]))
3233 break;
3234 }
3235 else
3236#endif
3237 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3238 break;
3239 }
3240 if (i < xp->xp_numfiles)
3241 {
3242 if (!(options & WILD_NO_BEEP))
3243 vim_beep();
3244 break;
3245 }
3246 }
3247 ss = alloc((unsigned)len + 1);
3248 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003249 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 findex = -1; /* next p_wc gets first one */
3251 }
3252
3253 /* Concatenate all matching names */
3254 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3255 {
3256 len = 0;
3257 for (i = 0; i < xp->xp_numfiles; ++i)
3258 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3259 ss = lalloc(len, TRUE);
3260 if (ss != NULL)
3261 {
3262 *ss = NUL;
3263 for (i = 0; i < xp->xp_numfiles; ++i)
3264 {
3265 STRCAT(ss, xp->xp_files[i]);
3266 if (i != xp->xp_numfiles - 1)
3267 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3268 }
3269 }
3270 }
3271
3272 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3273 ExpandCleanup(xp);
3274
3275 return ss;
3276}
3277
3278/*
3279 * Prepare an expand structure for use.
3280 */
3281 void
3282ExpandInit(xp)
3283 expand_T *xp;
3284{
3285 xp->xp_backslash = XP_BS_NONE;
3286 xp->xp_numfiles = -1;
3287 xp->xp_files = NULL;
3288}
3289
3290/*
3291 * Cleanup an expand structure after use.
3292 */
3293 void
3294ExpandCleanup(xp)
3295 expand_T *xp;
3296{
3297 if (xp->xp_numfiles >= 0)
3298 {
3299 FreeWild(xp->xp_numfiles, xp->xp_files);
3300 xp->xp_numfiles = -1;
3301 }
3302}
3303
3304 void
3305ExpandEscape(xp, str, numfiles, files, options)
3306 expand_T *xp;
3307 char_u *str;
3308 int numfiles;
3309 char_u **files;
3310 int options;
3311{
3312 int i;
3313 char_u *p;
3314
3315 /*
3316 * May change home directory back to "~"
3317 */
3318 if (options & WILD_HOME_REPLACE)
3319 tilde_replace(str, numfiles, files);
3320
3321 if (options & WILD_ESCAPE)
3322 {
3323 if (xp->xp_context == EXPAND_FILES
3324 || xp->xp_context == EXPAND_BUFFERS
3325 || xp->xp_context == EXPAND_DIRECTORIES)
3326 {
3327 /*
3328 * Insert a backslash into a file name before a space, \, %, #
3329 * and wildmatch characters, except '~'.
3330 */
3331 for (i = 0; i < numfiles; ++i)
3332 {
3333 /* for ":set path=" we need to escape spaces twice */
3334 if (xp->xp_backslash == XP_BS_THREE)
3335 {
3336 p = vim_strsave_escaped(files[i], (char_u *)" ");
3337 if (p != NULL)
3338 {
3339 vim_free(files[i]);
3340 files[i] = p;
3341#if defined(BACKSLASH_IN_FILENAME) || defined(COLON_AS_PATHSEP)
3342 p = vim_strsave_escaped(files[i], (char_u *)" ");
3343 if (p != NULL)
3344 {
3345 vim_free(files[i]);
3346 files[i] = p;
3347 }
3348#endif
3349 }
3350 }
3351#ifdef BACKSLASH_IN_FILENAME
3352 {
3353 char_u buf[20];
3354 int j = 0;
3355
3356 /* Don't escape '[' and '{' if they are in 'isfname'. */
3357 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3358 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3359 buf[j++] = *p;
3360 buf[j] = NUL;
3361 p = vim_strsave_escaped(files[i], buf);
3362 }
3363#else
3364 p = vim_strsave_escaped(files[i], PATH_ESC_CHARS);
3365#endif
3366 if (p != NULL)
3367 {
3368 vim_free(files[i]);
3369 files[i] = p;
3370 }
3371
3372 /* If 'str' starts with "\~", replace "~" at start of
3373 * files[i] with "\~". */
3374 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003375 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003378
3379 /* If the first file starts with a '+' escape it. Otherwise it
3380 * could be seen as "+cmd". */
3381 if (*files[0] == '+')
3382 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384 else if (xp->xp_context == EXPAND_TAGS)
3385 {
3386 /*
3387 * Insert a backslash before characters in a tag name that
3388 * would terminate the ":tag" command.
3389 */
3390 for (i = 0; i < numfiles; ++i)
3391 {
3392 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3393 if (p != NULL)
3394 {
3395 vim_free(files[i]);
3396 files[i] = p;
3397 }
3398 }
3399 }
3400 }
3401}
3402
3403/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003404 * Put a backslash before the file name in "pp", which is in allocated memory.
3405 */
3406 static void
3407escape_fname(pp)
3408 char_u **pp;
3409{
3410 char_u *p;
3411
3412 p = alloc((unsigned)(STRLEN(*pp) + 2));
3413 if (p != NULL)
3414 {
3415 p[0] = '\\';
3416 STRCPY(p + 1, *pp);
3417 vim_free(*pp);
3418 *pp = p;
3419 }
3420}
3421
3422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 * For each file name in files[num_files]:
3424 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3425 */
3426 void
3427tilde_replace(orig_pat, num_files, files)
3428 char_u *orig_pat;
3429 int num_files;
3430 char_u **files;
3431{
3432 int i;
3433 char_u *p;
3434
3435 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3436 {
3437 for (i = 0; i < num_files; ++i)
3438 {
3439 p = home_replace_save(NULL, files[i]);
3440 if (p != NULL)
3441 {
3442 vim_free(files[i]);
3443 files[i] = p;
3444 }
3445 }
3446 }
3447}
3448
3449/*
3450 * Show all matches for completion on the command line.
3451 * Returns EXPAND_NOTHING when the character that triggered expansion should
3452 * be inserted like a normal character.
3453 */
3454/*ARGSUSED*/
3455 static int
3456showmatches(xp, wildmenu)
3457 expand_T *xp;
3458 int wildmenu;
3459{
3460#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3461 int num_files;
3462 char_u **files_found;
3463 int i, j, k;
3464 int maxlen;
3465 int lines;
3466 int columns;
3467 char_u *p;
3468 int lastlen;
3469 int attr;
3470 int showtail;
3471
3472 if (xp->xp_numfiles == -1)
3473 {
3474 set_expand_context(xp);
3475 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3476 &num_files, &files_found);
3477 showtail = expand_showtail(xp);
3478 if (i != EXPAND_OK)
3479 return i;
3480
3481 }
3482 else
3483 {
3484 num_files = xp->xp_numfiles;
3485 files_found = xp->xp_files;
3486 showtail = cmd_showtail;
3487 }
3488
3489#ifdef FEAT_WILDMENU
3490 if (!wildmenu)
3491 {
3492#endif
3493 msg_didany = FALSE; /* lines_left will be set */
3494 msg_start(); /* prepare for paging */
3495 msg_putchar('\n');
3496 out_flush();
3497 cmdline_row = msg_row;
3498 msg_didany = FALSE; /* lines_left will be set again */
3499 msg_start(); /* prepare for paging */
3500#ifdef FEAT_WILDMENU
3501 }
3502#endif
3503
3504 if (got_int)
3505 got_int = FALSE; /* only int. the completion, not the cmd line */
3506#ifdef FEAT_WILDMENU
3507 else if (wildmenu)
3508 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3509#endif
3510 else
3511 {
3512 /* find the length of the longest file name */
3513 maxlen = 0;
3514 for (i = 0; i < num_files; ++i)
3515 {
3516 if (!showtail && (xp->xp_context == EXPAND_FILES
3517 || xp->xp_context == EXPAND_BUFFERS))
3518 {
3519 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3520 j = vim_strsize(NameBuff);
3521 }
3522 else
3523 j = vim_strsize(L_SHOWFILE(i));
3524 if (j > maxlen)
3525 maxlen = j;
3526 }
3527
3528 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3529 lines = num_files;
3530 else
3531 {
3532 /* compute the number of columns and lines for the listing */
3533 maxlen += 2; /* two spaces between file names */
3534 columns = ((int)Columns + 2) / maxlen;
3535 if (columns < 1)
3536 columns = 1;
3537 lines = (num_files + columns - 1) / columns;
3538 }
3539
3540 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3541
3542 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3543 {
3544 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3545 msg_clr_eos();
3546 msg_advance(maxlen - 3);
3547 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3548 }
3549
3550 /* list the files line by line */
3551 for (i = 0; i < lines; ++i)
3552 {
3553 lastlen = 999;
3554 for (k = i; k < num_files; k += lines)
3555 {
3556 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3557 {
3558 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3559 p = files_found[k] + STRLEN(files_found[k]) + 1;
3560 msg_advance(maxlen + 1);
3561 msg_puts(p);
3562 msg_advance(maxlen + 3);
3563 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3564 break;
3565 }
3566 for (j = maxlen - lastlen; --j >= 0; )
3567 msg_putchar(' ');
3568 if (xp->xp_context == EXPAND_FILES
3569 || xp->xp_context == EXPAND_BUFFERS)
3570 {
3571 /* highlight directories */
3572 j = (mch_isdir(files_found[k]));
3573 if (showtail)
3574 p = L_SHOWFILE(k);
3575 else
3576 {
3577 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3578 TRUE);
3579 p = NameBuff;
3580 }
3581 }
3582 else
3583 {
3584 j = FALSE;
3585 p = L_SHOWFILE(k);
3586 }
3587 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3588 }
3589 if (msg_col > 0) /* when not wrapped around */
3590 {
3591 msg_clr_eos();
3592 msg_putchar('\n');
3593 }
3594 out_flush(); /* show one line at a time */
3595 if (got_int)
3596 {
3597 got_int = FALSE;
3598 break;
3599 }
3600 }
3601
3602 /*
3603 * we redraw the command below the lines that we have just listed
3604 * This is a bit tricky, but it saves a lot of screen updating.
3605 */
3606 cmdline_row = msg_row; /* will put it back later */
3607 }
3608
3609 if (xp->xp_numfiles == -1)
3610 FreeWild(num_files, files_found);
3611
3612 return EXPAND_OK;
3613}
3614
3615/*
3616 * Private gettail for showmatches() (and win_redr_status_matches()):
3617 * Find tail of file name path, but ignore trailing "/".
3618 */
3619 char_u *
3620sm_gettail(s)
3621 char_u *s;
3622{
3623 char_u *p;
3624 char_u *t = s;
3625 int had_sep = FALSE;
3626
3627 for (p = s; *p != NUL; )
3628 {
3629 if (vim_ispathsep(*p)
3630#ifdef BACKSLASH_IN_FILENAME
3631 && !rem_backslash(p)
3632#endif
3633 )
3634 had_sep = TRUE;
3635 else if (had_sep)
3636 {
3637 t = p;
3638 had_sep = FALSE;
3639 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003640 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 }
3642 return t;
3643}
3644
3645/*
3646 * Return TRUE if we only need to show the tail of completion matches.
3647 * When not completing file names or there is a wildcard in the path FALSE is
3648 * returned.
3649 */
3650 static int
3651expand_showtail(xp)
3652 expand_T *xp;
3653{
3654 char_u *s;
3655 char_u *end;
3656
3657 /* When not completing file names a "/" may mean something different. */
3658 if (xp->xp_context != EXPAND_FILES && xp->xp_context != EXPAND_DIRECTORIES)
3659 return FALSE;
3660
3661 end = gettail(xp->xp_pattern);
3662 if (end == xp->xp_pattern) /* there is no path separator */
3663 return FALSE;
3664
3665 for (s = xp->xp_pattern; s < end; s++)
3666 {
3667 /* Skip escaped wildcards. Only when the backslash is not a path
3668 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3669 if (rem_backslash(s))
3670 ++s;
3671 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3672 return FALSE;
3673 }
3674 return TRUE;
3675}
3676
3677/*
3678 * Prepare a string for expansion.
3679 * When expanding file names: The string will be used with expand_wildcards().
3680 * Copy the file name into allocated memory and add a '*' at the end.
3681 * When expanding other names: The string will be used with regcomp(). Copy
3682 * the name into allocated memory and prepend "^".
3683 */
3684 char_u *
3685addstar(fname, len, context)
3686 char_u *fname;
3687 int len;
3688 int context; /* EXPAND_FILES etc. */
3689{
3690 char_u *retval;
3691 int i, j;
3692 int new_len;
3693 char_u *tail;
3694
3695 if (context != EXPAND_FILES && context != EXPAND_DIRECTORIES)
3696 {
3697 /*
3698 * Matching will be done internally (on something other than files).
3699 * So we convert the file-matching-type wildcards into our kind for
3700 * use with vim_regcomp(). First work out how long it will be:
3701 */
3702
3703 /* For help tags the translation is done in find_help_tags().
3704 * For a tag pattern starting with "/" no translation is needed. */
3705 if (context == EXPAND_HELP
3706 || context == EXPAND_COLORS
3707 || context == EXPAND_COMPILER
3708 || (context == EXPAND_TAGS && fname[0] == '/'))
3709 retval = vim_strnsave(fname, len);
3710 else
3711 {
3712 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3713 for (i = 0; i < len; i++)
3714 {
3715 if (fname[i] == '*' || fname[i] == '~')
3716 new_len++; /* '*' needs to be replaced by ".*"
3717 '~' needs to be replaced by "\~" */
3718
3719 /* Buffer names are like file names. "." should be literal */
3720 if (context == EXPAND_BUFFERS && fname[i] == '.')
3721 new_len++; /* "." becomes "\." */
3722
3723 /* Custom expansion takes care of special things, match
3724 * backslashes literally (perhaps also for other types?) */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003725 if ((context == EXPAND_USER_DEFINED ||
3726 context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 new_len++; /* '\' becomes "\\" */
3728 }
3729 retval = alloc(new_len);
3730 if (retval != NULL)
3731 {
3732 retval[0] = '^';
3733 j = 1;
3734 for (i = 0; i < len; i++, j++)
3735 {
3736 /* Skip backslash. But why? At least keep it for custom
3737 * expansion. */
3738 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003739 && context != EXPAND_USER_LIST
3740 && fname[i] == '\\'
3741 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 break;
3743
3744 switch (fname[i])
3745 {
3746 case '*': retval[j++] = '.';
3747 break;
3748 case '~': retval[j++] = '\\';
3749 break;
3750 case '?': retval[j] = '.';
3751 continue;
3752 case '.': if (context == EXPAND_BUFFERS)
3753 retval[j++] = '\\';
3754 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003755 case '\\': if (context == EXPAND_USER_DEFINED
3756 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 retval[j++] = '\\';
3758 break;
3759 }
3760 retval[j] = fname[i];
3761 }
3762 retval[j] = NUL;
3763 }
3764 }
3765 }
3766 else
3767 {
3768 retval = alloc(len + 4);
3769 if (retval != NULL)
3770 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003771 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /*
3774 * Don't add a star to ~, ~user, $var or `cmd`.
3775 * ~ would be at the start of the file name, but not the tail.
3776 * $ could be anywhere in the tail.
3777 * ` could be anywhere in the file name.
3778 */
3779 tail = gettail(retval);
3780 if ((*retval != '~' || tail != retval)
3781 && vim_strchr(tail, '$') == NULL
3782 && vim_strchr(retval, '`') == NULL)
3783 retval[len++] = '*';
3784 retval[len] = NUL;
3785 }
3786 }
3787 return retval;
3788}
3789
3790/*
3791 * Must parse the command line so far to work out what context we are in.
3792 * Completion can then be done based on that context.
3793 * This routine sets the variables:
3794 * xp->xp_pattern The start of the pattern to be expanded within
3795 * the command line (ends at the cursor).
3796 * xp->xp_context The type of thing to expand. Will be one of:
3797 *
3798 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
3799 * the command line, like an unknown command. Caller
3800 * should beep.
3801 * EXPAND_NOTHING Unrecognised context for completion, use char like
3802 * a normal char, rather than for completion. eg
3803 * :s/^I/
3804 * EXPAND_COMMANDS Cursor is still touching the command, so complete
3805 * it.
3806 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
3807 * EXPAND_FILES After command with XFILE set, or after setting
3808 * with P_EXPAND set. eg :e ^I, :w>>^I
3809 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
3810 * when we know only directories are of interest. eg
3811 * :set dir=^I
3812 * EXPAND_SETTINGS Complete variable names. eg :set d^I
3813 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
3814 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
3815 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
3816 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
3817 * EXPAND_EVENTS Complete event names
3818 * EXPAND_SYNTAX Complete :syntax command arguments
3819 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
3820 * EXPAND_AUGROUP Complete autocommand group names
3821 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
3822 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
3823 * eg :unmap a^I , :cunab x^I
3824 * EXPAND_FUNCTIONS Complete internal or user defined function names,
3825 * eg :call sub^I
3826 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
3827 * EXPAND_EXPRESSION Complete internal or user defined function/variable
3828 * names in expressions, eg :while s^I
3829 * EXPAND_ENV_VARS Complete environment variable names
3830 */
3831 static void
3832set_expand_context(xp)
3833 expand_T *xp;
3834{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003835 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 if (ccline.cmdfirstc != ':'
3837#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003838 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839#endif
3840 )
3841 {
3842 xp->xp_context = EXPAND_NOTHING;
3843 return;
3844 }
3845 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
3846}
3847
3848 void
3849set_cmd_context(xp, str, len, col)
3850 expand_T *xp;
3851 char_u *str; /* start of command line */
3852 int len; /* length of command line (excl. NUL) */
3853 int col; /* position of cursor */
3854{
3855 int old_char = NUL;
3856 char_u *nextcomm;
3857
3858 /*
3859 * Avoid a UMR warning from Purify, only save the character if it has been
3860 * written before.
3861 */
3862 if (col < len)
3863 old_char = str[col];
3864 str[col] = NUL;
3865 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003866
3867#ifdef FEAT_EVAL
3868 if (ccline.cmdfirstc == '=')
3869 /* pass CMD_SIZE because there is no real command */
3870 set_context_for_expression(xp, str, CMD_SIZE);
3871 else
3872#endif
3873 while (nextcomm != NULL)
3874 nextcomm = set_one_cmd_context(xp, nextcomm);
3875
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 str[col] = old_char;
3877}
3878
3879/*
3880 * Expand the command line "str" from context "xp".
3881 * "xp" must have been set by set_cmd_context().
3882 * xp->xp_pattern points into "str", to where the text that is to be expanded
3883 * starts.
3884 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
3885 * cursor.
3886 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
3887 * key that triggered expansion literally.
3888 * Returns EXPAND_OK otherwise.
3889 */
3890 int
3891expand_cmdline(xp, str, col, matchcount, matches)
3892 expand_T *xp;
3893 char_u *str; /* start of command line */
3894 int col; /* position of cursor */
3895 int *matchcount; /* return: nr of matches */
3896 char_u ***matches; /* return: array of pointers to matches */
3897{
3898 char_u *file_str = NULL;
3899
3900 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3901 {
3902 beep_flush();
3903 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
3904 }
3905 if (xp->xp_context == EXPAND_NOTHING)
3906 {
3907 /* Caller can use the character as a normal char instead */
3908 return EXPAND_NOTHING;
3909 }
3910
3911 /* add star to file name, or convert to regexp if not exp. files. */
3912 file_str = addstar(xp->xp_pattern,
3913 (int)(str + col - xp->xp_pattern), xp->xp_context);
3914 if (file_str == NULL)
3915 return EXPAND_UNSUCCESSFUL;
3916
3917 /* find all files that match the description */
3918 if (ExpandFromContext(xp, file_str, matchcount, matches,
3919 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
3920 {
3921 *matchcount = 0;
3922 *matches = NULL;
3923 }
3924 vim_free(file_str);
3925
3926 return EXPAND_OK;
3927}
3928
3929#ifdef FEAT_MULTI_LANG
3930/*
3931 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
3932 */
3933static void cleanup_help_tags __ARGS((int num_file, char_u **file));
3934
3935 static void
3936cleanup_help_tags(num_file, file)
3937 int num_file;
3938 char_u **file;
3939{
3940 int i, j;
3941 int len;
3942
3943 for (i = 0; i < num_file; ++i)
3944 {
3945 len = (int)STRLEN(file[i]) - 3;
3946 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
3947 {
3948 /* Sorting on priority means the same item in another language may
3949 * be anywhere. Search all items for a match up to the "@en". */
3950 for (j = 0; j < num_file; ++j)
3951 if (j != i
3952 && (int)STRLEN(file[j]) == len + 3
3953 && STRNCMP(file[i], file[j], len + 1) == 0)
3954 break;
3955 if (j == num_file)
3956 file[i][len] = NUL;
3957 }
3958 }
3959}
3960#endif
3961
3962/*
3963 * Do the expansion based on xp->xp_context and "pat".
3964 */
3965 static int
3966ExpandFromContext(xp, pat, num_file, file, options)
3967 expand_T *xp;
3968 char_u *pat;
3969 int *num_file;
3970 char_u ***file;
3971 int options;
3972{
3973#ifdef FEAT_CMDL_COMPL
3974 regmatch_T regmatch;
3975#endif
3976 int ret;
3977 int flags;
3978
3979 flags = EW_DIR; /* include directories */
3980 if (options & WILD_LIST_NOTFOUND)
3981 flags |= EW_NOTFOUND;
3982 if (options & WILD_ADD_SLASH)
3983 flags |= EW_ADDSLASH;
3984 if (options & WILD_KEEP_ALL)
3985 flags |= EW_KEEPALL;
3986 if (options & WILD_SILENT)
3987 flags |= EW_SILENT;
3988
3989 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
3990 {
3991 /*
3992 * Expand file or directory names.
3993 */
3994 int free_pat = FALSE;
3995 int i;
3996
3997 /* for ":set path=" and ":set tags=" halve backslashes for escaped
3998 * space */
3999 if (xp->xp_backslash != XP_BS_NONE)
4000 {
4001 free_pat = TRUE;
4002 pat = vim_strsave(pat);
4003 for (i = 0; pat[i]; ++i)
4004 if (pat[i] == '\\')
4005 {
4006 if (xp->xp_backslash == XP_BS_THREE
4007 && pat[i + 1] == '\\'
4008 && pat[i + 2] == '\\'
4009 && pat[i + 3] == ' ')
4010 STRCPY(pat + i, pat + i + 3);
4011 if (xp->xp_backslash == XP_BS_ONE
4012 && pat[i + 1] == ' ')
4013 STRCPY(pat + i, pat + i + 1);
4014 }
4015 }
4016
4017 if (xp->xp_context == EXPAND_FILES)
4018 flags |= EW_FILE;
4019 else
4020 flags = (flags | EW_DIR) & ~EW_FILE;
4021 ret = expand_wildcards(1, &pat, num_file, file, flags);
4022 if (free_pat)
4023 vim_free(pat);
4024 return ret;
4025 }
4026
4027 *file = (char_u **)"";
4028 *num_file = 0;
4029 if (xp->xp_context == EXPAND_HELP)
4030 {
4031 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4032 {
4033#ifdef FEAT_MULTI_LANG
4034 cleanup_help_tags(*num_file, *file);
4035#endif
4036 return OK;
4037 }
4038 return FAIL;
4039 }
4040
4041#ifndef FEAT_CMDL_COMPL
4042 return FAIL;
4043#else
4044 if (xp->xp_context == EXPAND_OLD_SETTING)
4045 return ExpandOldSetting(num_file, file);
4046 if (xp->xp_context == EXPAND_BUFFERS)
4047 return ExpandBufnames(pat, num_file, file, options);
4048 if (xp->xp_context == EXPAND_TAGS
4049 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4050 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4051 if (xp->xp_context == EXPAND_COLORS)
4052 return ExpandRTDir(pat, num_file, file, "colors");
4053 if (xp->xp_context == EXPAND_COMPILER)
4054 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004055# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4056 if (xp->xp_context == EXPAND_USER_LIST)
4057 return ExpandUserList(xp, num_file, file);
4058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4061 if (regmatch.regprog == NULL)
4062 return FAIL;
4063
4064 /* set ignore-case according to p_ic, p_scs and pat */
4065 regmatch.rm_ic = ignorecase(pat);
4066
4067 if (xp->xp_context == EXPAND_SETTINGS
4068 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4069 ret = ExpandSettings(xp, &regmatch, num_file, file);
4070 else if (xp->xp_context == EXPAND_MAPPINGS)
4071 ret = ExpandMappings(&regmatch, num_file, file);
4072# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4073 else if (xp->xp_context == EXPAND_USER_DEFINED)
4074 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4075# endif
4076 else
4077 {
4078 static struct expgen
4079 {
4080 int context;
4081 char_u *((*func)__ARGS((expand_T *, int)));
4082 int ic;
4083 } tab[] =
4084 {
4085 {EXPAND_COMMANDS, get_command_name, FALSE},
4086#ifdef FEAT_USR_CMDS
4087 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4088 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4089 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4090 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4091#endif
4092#ifdef FEAT_EVAL
4093 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4094 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4095 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4096 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4097#endif
4098#ifdef FEAT_MENU
4099 {EXPAND_MENUS, get_menu_name, FALSE},
4100 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4101#endif
4102#ifdef FEAT_SYN_HL
4103 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4104#endif
4105 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4106#ifdef FEAT_AUTOCMD
4107 {EXPAND_EVENTS, get_event_name, TRUE},
4108 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4109#endif
4110#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4111 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4112 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4113#endif
4114 {EXPAND_ENV_VARS, get_env_name, TRUE},
4115 };
4116 int i;
4117
4118 /*
4119 * Find a context in the table and call the ExpandGeneric() with the
4120 * right function to do the expansion.
4121 */
4122 ret = FAIL;
4123 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4124 if (xp->xp_context == tab[i].context)
4125 {
4126 if (tab[i].ic)
4127 regmatch.rm_ic = TRUE;
4128 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4129 break;
4130 }
4131 }
4132
4133 vim_free(regmatch.regprog);
4134
4135 return ret;
4136#endif /* FEAT_CMDL_COMPL */
4137}
4138
4139#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4140/*
4141 * Expand a list of names.
4142 *
4143 * Generic function for command line completion. It calls a function to
4144 * obtain strings, one by one. The strings are matched against a regexp
4145 * program. Matching strings are copied into an array, which is returned.
4146 *
4147 * Returns OK when no problems encountered, FAIL for error (out of memory).
4148 */
4149 int
4150ExpandGeneric(xp, regmatch, num_file, file, func)
4151 expand_T *xp;
4152 regmatch_T *regmatch;
4153 int *num_file;
4154 char_u ***file;
4155 char_u *((*func)__ARGS((expand_T *, int)));
4156 /* returns a string from the list */
4157{
4158 int i;
4159 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004160 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 char_u *str;
4162
4163 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004164 * round == 0: count the number of matching names
4165 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004167 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
4169 for (i = 0; ; ++i)
4170 {
4171 str = (*func)(xp, i);
4172 if (str == NULL) /* end of list */
4173 break;
4174 if (*str == NUL) /* skip empty strings */
4175 continue;
4176
4177 if (vim_regexec(regmatch, str, (colnr_T)0))
4178 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004179 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
4181 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4182 (*file)[count] = str;
4183#ifdef FEAT_MENU
4184 if (func == get_menu_names && str != NULL)
4185 {
4186 /* test for separator added by get_menu_names() */
4187 str += STRLEN(str) - 1;
4188 if (*str == '\001')
4189 *str = '.';
4190 }
4191#endif
4192 }
4193 ++count;
4194 }
4195 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004196 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
4198 if (count == 0)
4199 return OK;
4200 *num_file = count;
4201 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4202 if (*file == NULL)
4203 {
4204 *file = (char_u **)"";
4205 return FAIL;
4206 }
4207 count = 0;
4208 }
4209 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004210
4211 /* Sort the results. */
4212 sort_strings(*file, *num_file);
4213
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return OK;
4215}
4216
4217# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004218static 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));
4219
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004221 * call "user_expand_func()" to invoke a user defined VimL function and return
4222 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004224 static void *
4225call_user_expand_func(user_expand_func, xp, num_file, file)
4226 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 int *num_file;
4229 char_u ***file;
4230{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004231 char_u keep;
4232 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004235 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004236 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
4238 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004239 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 *num_file = 0;
4241 *file = NULL;
4242
4243 keep = ccline.cmdbuff[ccline.cmdlen];
4244 ccline.cmdbuff[ccline.cmdlen] = 0;
4245 sprintf((char *)num, "%d", ccline.cmdpos);
4246 args[0] = xp->xp_pattern;
4247 args[1] = ccline.cmdbuff;
4248 args[2] = num;
4249
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004250 /* Save the cmdline, we don't know what the function may do. */
4251 save_ccline = ccline;
4252 ccline.cmdbuff = NULL;
4253 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004255
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004256 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004257
4258 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004260
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004262
4263 return ret;
4264}
4265
4266/*
4267 * Expand names with a function defined by the user.
4268 */
4269 static int
4270ExpandUserDefined(xp, regmatch, num_file, file)
4271 expand_T *xp;
4272 regmatch_T *regmatch;
4273 int *num_file;
4274 char_u ***file;
4275{
4276 char_u *retstr;
4277 char_u *s;
4278 char_u *e;
4279 char_u keep;
4280 garray_T ga;
4281
4282 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4283 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 return FAIL;
4285
4286 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004287 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
4289 e = vim_strchr(s, '\n');
4290 if (e == NULL)
4291 e = s + STRLEN(s);
4292 keep = *e;
4293 *e = 0;
4294
4295 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4296 {
4297 *e = keep;
4298 if (*e != NUL)
4299 ++e;
4300 continue;
4301 }
4302
4303 if (ga_grow(&ga, 1) == FAIL)
4304 break;
4305
4306 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4307 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
4309 *e = keep;
4310 if (*e != NUL)
4311 ++e;
4312 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004313 vim_free(retstr);
4314 *file = ga.ga_data;
4315 *num_file = ga.ga_len;
4316 return OK;
4317}
4318
4319/*
4320 * Expand names with a list returned by a function defined by the user.
4321 */
4322 static int
4323ExpandUserList(xp, num_file, file)
4324 expand_T *xp;
4325 int *num_file;
4326 char_u ***file;
4327{
4328 list_T *retlist;
4329 listitem_T *li;
4330 garray_T ga;
4331
4332 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4333 if (retlist == NULL)
4334 return FAIL;
4335
4336 ga_init2(&ga, (int)sizeof(char *), 3);
4337 /* Loop over the items in the list. */
4338 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4339 {
4340 if (li->li_tv.v_type != VAR_STRING)
4341 continue; /* Skip non-string items */
4342
4343 if (ga_grow(&ga, 1) == FAIL)
4344 break;
4345
4346 ((char_u **)ga.ga_data)[ga.ga_len] =
4347 vim_strsave(li->li_tv.vval.v_string);
4348 ++ga.ga_len;
4349 }
4350 list_unref(retlist);
4351
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 *file = ga.ga_data;
4353 *num_file = ga.ga_len;
4354 return OK;
4355}
4356#endif
4357
4358/*
4359 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4360 * or compiler names.
4361 */
4362 static int
4363ExpandRTDir(pat, num_file, file, dirname)
4364 char_u *pat;
4365 int *num_file;
4366 char_u ***file;
4367 char *dirname; /* "colors" or "compiler" */
4368{
4369 char_u *all;
4370 char_u *s;
4371 char_u *e;
4372 garray_T ga;
4373
4374 *num_file = 0;
4375 *file = NULL;
4376 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4377 if (s == NULL)
4378 return FAIL;
4379 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4380 all = globpath(p_rtp, s);
4381 vim_free(s);
4382 if (all == NULL)
4383 return FAIL;
4384
4385 ga_init2(&ga, (int)sizeof(char *), 3);
4386 for (s = all; *s != NUL; s = e)
4387 {
4388 e = vim_strchr(s, '\n');
4389 if (e == NULL)
4390 e = s + STRLEN(s);
4391 if (ga_grow(&ga, 1) == FAIL)
4392 break;
4393 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4394 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004395 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 if (*s == '\n' || vim_ispathsep(*s))
4397 break;
4398 ++s;
4399 ((char_u **)ga.ga_data)[ga.ga_len] =
4400 vim_strnsave(s, (int)(e - s - 4));
4401 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 }
4403 if (*e != NUL)
4404 ++e;
4405 }
4406 vim_free(all);
4407 *file = ga.ga_data;
4408 *num_file = ga.ga_len;
4409 return OK;
4410}
4411
4412#endif
4413
4414#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4415/*
4416 * Expand "file" for all comma-separated directories in "path".
4417 * Returns an allocated string with all matches concatenated, separated by
4418 * newlines. Returns NULL for an error or no matches.
4419 */
4420 char_u *
4421globpath(path, file)
4422 char_u *path;
4423 char_u *file;
4424{
4425 expand_T xpc;
4426 char_u *buf;
4427 garray_T ga;
4428 int i;
4429 int len;
4430 int num_p;
4431 char_u **p;
4432 char_u *cur = NULL;
4433
4434 buf = alloc(MAXPATHL);
4435 if (buf == NULL)
4436 return NULL;
4437
4438 xpc.xp_context = EXPAND_FILES;
4439 xpc.xp_backslash = XP_BS_NONE;
4440 ga_init2(&ga, 1, 100);
4441
4442 /* Loop over all entries in {path}. */
4443 while (*path != NUL)
4444 {
4445 /* Copy one item of the path to buf[] and concatenate the file name. */
4446 copy_option_part(&path, buf, MAXPATHL, ",");
4447 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4448 {
4449 add_pathsep(buf);
4450 STRCAT(buf, file);
4451 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4452 && num_p > 0)
4453 {
4454 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4455 for (len = 0, i = 0; i < num_p; ++i)
4456 len += (long_u)STRLEN(p[i]) + 1;
4457
4458 /* Concatenate new results to previous ones. */
4459 if (ga_grow(&ga, len) == OK)
4460 {
4461 cur = (char_u *)ga.ga_data + ga.ga_len;
4462 for (i = 0; i < num_p; ++i)
4463 {
4464 STRCPY(cur, p[i]);
4465 cur += STRLEN(p[i]);
4466 *cur++ = '\n';
4467 }
4468 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470 FreeWild(num_p, p);
4471 }
4472 }
4473 }
4474 if (cur != NULL)
4475 *--cur = 0; /* Replace trailing newline with NUL */
4476
4477 vim_free(buf);
4478 return (char_u *)ga.ga_data;
4479}
4480
4481#endif
4482
4483#if defined(FEAT_CMDHIST) || defined(PROTO)
4484
4485/*********************************
4486 * Command line history stuff *
4487 *********************************/
4488
4489/*
4490 * Translate a history character to the associated type number.
4491 */
4492 static int
4493hist_char2type(c)
4494 int c;
4495{
4496 if (c == ':')
4497 return HIST_CMD;
4498 if (c == '=')
4499 return HIST_EXPR;
4500 if (c == '@')
4501 return HIST_INPUT;
4502 if (c == '>')
4503 return HIST_DEBUG;
4504 return HIST_SEARCH; /* must be '?' or '/' */
4505}
4506
4507/*
4508 * Table of history names.
4509 * These names are used in :history and various hist...() functions.
4510 * It is sufficient to give the significant prefix of a history name.
4511 */
4512
4513static char *(history_names[]) =
4514{
4515 "cmd",
4516 "search",
4517 "expr",
4518 "input",
4519 "debug",
4520 NULL
4521};
4522
4523/*
4524 * init_history() - Initialize the command line history.
4525 * Also used to re-allocate the history when the size changes.
4526 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004527 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528init_history()
4529{
4530 int newlen; /* new length of history table */
4531 histentry_T *temp;
4532 int i;
4533 int j;
4534 int type;
4535
4536 /*
4537 * If size of history table changed, reallocate it
4538 */
4539 newlen = (int)p_hi;
4540 if (newlen != hislen) /* history length changed */
4541 {
4542 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4543 {
4544 if (newlen)
4545 {
4546 temp = (histentry_T *)lalloc(
4547 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4548 if (temp == NULL) /* out of memory! */
4549 {
4550 if (type == 0) /* first one: just keep the old length */
4551 {
4552 newlen = hislen;
4553 break;
4554 }
4555 /* Already changed one table, now we can only have zero
4556 * length for all tables. */
4557 newlen = 0;
4558 type = -1;
4559 continue;
4560 }
4561 }
4562 else
4563 temp = NULL;
4564 if (newlen == 0 || temp != NULL)
4565 {
4566 if (hisidx[type] < 0) /* there are no entries yet */
4567 {
4568 for (i = 0; i < newlen; ++i)
4569 {
4570 temp[i].hisnum = 0;
4571 temp[i].hisstr = NULL;
4572 }
4573 }
4574 else if (newlen > hislen) /* array becomes bigger */
4575 {
4576 for (i = 0; i <= hisidx[type]; ++i)
4577 temp[i] = history[type][i];
4578 j = i;
4579 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4580 {
4581 temp[i].hisnum = 0;
4582 temp[i].hisstr = NULL;
4583 }
4584 for ( ; j < hislen; ++i, ++j)
4585 temp[i] = history[type][j];
4586 }
4587 else /* array becomes smaller or 0 */
4588 {
4589 j = hisidx[type];
4590 for (i = newlen - 1; ; --i)
4591 {
4592 if (i >= 0) /* copy newest entries */
4593 temp[i] = history[type][j];
4594 else /* remove older entries */
4595 vim_free(history[type][j].hisstr);
4596 if (--j < 0)
4597 j = hislen - 1;
4598 if (j == hisidx[type])
4599 break;
4600 }
4601 hisidx[type] = newlen - 1;
4602 }
4603 vim_free(history[type]);
4604 history[type] = temp;
4605 }
4606 }
4607 hislen = newlen;
4608 }
4609}
4610
4611/*
4612 * Check if command line 'str' is already in history.
4613 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4614 */
4615 static int
4616in_history(type, str, move_to_front)
4617 int type;
4618 char_u *str;
4619 int move_to_front; /* Move the entry to the front if it exists */
4620{
4621 int i;
4622 int last_i = -1;
4623
4624 if (hisidx[type] < 0)
4625 return FALSE;
4626 i = hisidx[type];
4627 do
4628 {
4629 if (history[type][i].hisstr == NULL)
4630 return FALSE;
4631 if (STRCMP(str, history[type][i].hisstr) == 0)
4632 {
4633 if (!move_to_front)
4634 return TRUE;
4635 last_i = i;
4636 break;
4637 }
4638 if (--i < 0)
4639 i = hislen - 1;
4640 } while (i != hisidx[type]);
4641
4642 if (last_i >= 0)
4643 {
4644 str = history[type][i].hisstr;
4645 while (i != hisidx[type])
4646 {
4647 if (++i >= hislen)
4648 i = 0;
4649 history[type][last_i] = history[type][i];
4650 last_i = i;
4651 }
4652 history[type][i].hisstr = str;
4653 history[type][i].hisnum = ++hisnum[type];
4654 return TRUE;
4655 }
4656 return FALSE;
4657}
4658
4659/*
4660 * Convert history name (from table above) to its HIST_ equivalent.
4661 * When "name" is empty, return "cmd" history.
4662 * Returns -1 for unknown history name.
4663 */
4664 int
4665get_histtype(name)
4666 char_u *name;
4667{
4668 int i;
4669 int len = (int)STRLEN(name);
4670
4671 /* No argument: use current history. */
4672 if (len == 0)
4673 return hist_char2type(ccline.cmdfirstc);
4674
4675 for (i = 0; history_names[i] != NULL; ++i)
4676 if (STRNICMP(name, history_names[i], len) == 0)
4677 return i;
4678
4679 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
4680 return hist_char2type(name[0]);
4681
4682 return -1;
4683}
4684
4685static int last_maptick = -1; /* last seen maptick */
4686
4687/*
4688 * Add the given string to the given history. If the string is already in the
4689 * history then it is moved to the front. "histype" may be one of he HIST_
4690 * values.
4691 */
4692 void
4693add_to_history(histype, new_entry, in_map, sep)
4694 int histype;
4695 char_u *new_entry;
4696 int in_map; /* consider maptick when inside a mapping */
4697 int sep; /* separator character used (search hist) */
4698{
4699 histentry_T *hisptr;
4700 int len;
4701
4702 if (hislen == 0) /* no history */
4703 return;
4704
4705 /*
4706 * Searches inside the same mapping overwrite each other, so that only
4707 * the last line is kept. Be careful not to remove a line that was moved
4708 * down, only lines that were added.
4709 */
4710 if (histype == HIST_SEARCH && in_map)
4711 {
4712 if (maptick == last_maptick)
4713 {
4714 /* Current line is from the same mapping, remove it */
4715 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
4716 vim_free(hisptr->hisstr);
4717 hisptr->hisstr = NULL;
4718 hisptr->hisnum = 0;
4719 --hisnum[histype];
4720 if (--hisidx[HIST_SEARCH] < 0)
4721 hisidx[HIST_SEARCH] = hislen - 1;
4722 }
4723 last_maptick = -1;
4724 }
4725 if (!in_history(histype, new_entry, TRUE))
4726 {
4727 if (++hisidx[histype] == hislen)
4728 hisidx[histype] = 0;
4729 hisptr = &history[histype][hisidx[histype]];
4730 vim_free(hisptr->hisstr);
4731
4732 /* Store the separator after the NUL of the string. */
4733 len = STRLEN(new_entry);
4734 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
4735 if (hisptr->hisstr != NULL)
4736 hisptr->hisstr[len + 1] = sep;
4737
4738 hisptr->hisnum = ++hisnum[histype];
4739 if (histype == HIST_SEARCH && in_map)
4740 last_maptick = maptick;
4741 }
4742}
4743
4744#if defined(FEAT_EVAL) || defined(PROTO)
4745
4746/*
4747 * Get identifier of newest history entry.
4748 * "histype" may be one of the HIST_ values.
4749 */
4750 int
4751get_history_idx(histype)
4752 int histype;
4753{
4754 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4755 || hisidx[histype] < 0)
4756 return -1;
4757
4758 return history[histype][hisidx[histype]].hisnum;
4759}
4760
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004761static struct cmdline_info *get_ccline_ptr __ARGS((void));
4762
4763/*
4764 * Get pointer to the command line info to use. cmdline_paste() may clear
4765 * ccline and put the previous value in prev_ccline.
4766 */
4767 static struct cmdline_info *
4768get_ccline_ptr()
4769{
4770 if ((State & CMDLINE) == 0)
4771 return NULL;
4772 if (ccline.cmdbuff != NULL)
4773 return &ccline;
4774 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
4775 return &prev_ccline;
4776 return NULL;
4777}
4778
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779/*
4780 * Get the current command line in allocated memory.
4781 * Only works when the command line is being edited.
4782 * Returns NULL when something is wrong.
4783 */
4784 char_u *
4785get_cmdline_str()
4786{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004787 struct cmdline_info *p = get_ccline_ptr();
4788
4789 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004791 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792}
4793
4794/*
4795 * Get the current command line position, counted in bytes.
4796 * Zero is the first position.
4797 * Only works when the command line is being edited.
4798 * Returns -1 when something is wrong.
4799 */
4800 int
4801get_cmdline_pos()
4802{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004803 struct cmdline_info *p = get_ccline_ptr();
4804
4805 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004807 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808}
4809
4810/*
4811 * Set the command line byte position to "pos". Zero is the first position.
4812 * Only works when the command line is being edited.
4813 * Returns 1 when failed, 0 when OK.
4814 */
4815 int
4816set_cmdline_pos(pos)
4817 int pos;
4818{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004819 struct cmdline_info *p = get_ccline_ptr();
4820
4821 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 return 1;
4823
4824 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
4825 * changed the command line. */
4826 if (pos < 0)
4827 new_cmdpos = 0;
4828 else
4829 new_cmdpos = pos;
4830 return 0;
4831}
4832
4833/*
4834 * Calculate history index from a number:
4835 * num > 0: seen as identifying number of a history entry
4836 * num < 0: relative position in history wrt newest entry
4837 * "histype" may be one of the HIST_ values.
4838 */
4839 static int
4840calc_hist_idx(histype, num)
4841 int histype;
4842 int num;
4843{
4844 int i;
4845 histentry_T *hist;
4846 int wrapped = FALSE;
4847
4848 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4849 || (i = hisidx[histype]) < 0 || num == 0)
4850 return -1;
4851
4852 hist = history[histype];
4853 if (num > 0)
4854 {
4855 while (hist[i].hisnum > num)
4856 if (--i < 0)
4857 {
4858 if (wrapped)
4859 break;
4860 i += hislen;
4861 wrapped = TRUE;
4862 }
4863 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
4864 return i;
4865 }
4866 else if (-num <= hislen)
4867 {
4868 i += num + 1;
4869 if (i < 0)
4870 i += hislen;
4871 if (hist[i].hisstr != NULL)
4872 return i;
4873 }
4874 return -1;
4875}
4876
4877/*
4878 * Get a history entry by its index.
4879 * "histype" may be one of the HIST_ values.
4880 */
4881 char_u *
4882get_history_entry(histype, idx)
4883 int histype;
4884 int idx;
4885{
4886 idx = calc_hist_idx(histype, idx);
4887 if (idx >= 0)
4888 return history[histype][idx].hisstr;
4889 else
4890 return (char_u *)"";
4891}
4892
4893/*
4894 * Clear all entries of a history.
4895 * "histype" may be one of the HIST_ values.
4896 */
4897 int
4898clr_history(histype)
4899 int histype;
4900{
4901 int i;
4902 histentry_T *hisptr;
4903
4904 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
4905 {
4906 hisptr = history[histype];
4907 for (i = hislen; i--;)
4908 {
4909 vim_free(hisptr->hisstr);
4910 hisptr->hisnum = 0;
4911 hisptr++->hisstr = NULL;
4912 }
4913 hisidx[histype] = -1; /* mark history as cleared */
4914 hisnum[histype] = 0; /* reset identifier counter */
4915 return OK;
4916 }
4917 return FAIL;
4918}
4919
4920/*
4921 * Remove all entries matching {str} from a history.
4922 * "histype" may be one of the HIST_ values.
4923 */
4924 int
4925del_history_entry(histype, str)
4926 int histype;
4927 char_u *str;
4928{
4929 regmatch_T regmatch;
4930 histentry_T *hisptr;
4931 int idx;
4932 int i;
4933 int last;
4934 int found = FALSE;
4935
4936 regmatch.regprog = NULL;
4937 regmatch.rm_ic = FALSE; /* always match case */
4938 if (hislen != 0
4939 && histype >= 0
4940 && histype < HIST_COUNT
4941 && *str != NUL
4942 && (idx = hisidx[histype]) >= 0
4943 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
4944 != NULL)
4945 {
4946 i = last = idx;
4947 do
4948 {
4949 hisptr = &history[histype][i];
4950 if (hisptr->hisstr == NULL)
4951 break;
4952 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
4953 {
4954 found = TRUE;
4955 vim_free(hisptr->hisstr);
4956 hisptr->hisstr = NULL;
4957 hisptr->hisnum = 0;
4958 }
4959 else
4960 {
4961 if (i != last)
4962 {
4963 history[histype][last] = *hisptr;
4964 hisptr->hisstr = NULL;
4965 hisptr->hisnum = 0;
4966 }
4967 if (--last < 0)
4968 last += hislen;
4969 }
4970 if (--i < 0)
4971 i += hislen;
4972 } while (i != idx);
4973 if (history[histype][idx].hisstr == NULL)
4974 hisidx[histype] = -1;
4975 }
4976 vim_free(regmatch.regprog);
4977 return found;
4978}
4979
4980/*
4981 * Remove an indexed entry from a history.
4982 * "histype" may be one of the HIST_ values.
4983 */
4984 int
4985del_history_idx(histype, idx)
4986 int histype;
4987 int idx;
4988{
4989 int i, j;
4990
4991 i = calc_hist_idx(histype, idx);
4992 if (i < 0)
4993 return FALSE;
4994 idx = hisidx[histype];
4995 vim_free(history[histype][i].hisstr);
4996
4997 /* When deleting the last added search string in a mapping, reset
4998 * last_maptick, so that the last added search string isn't deleted again.
4999 */
5000 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5001 last_maptick = -1;
5002
5003 while (i != idx)
5004 {
5005 j = (i + 1) % hislen;
5006 history[histype][i] = history[histype][j];
5007 i = j;
5008 }
5009 history[histype][i].hisstr = NULL;
5010 history[histype][i].hisnum = 0;
5011 if (--i < 0)
5012 i += hislen;
5013 hisidx[histype] = i;
5014 return TRUE;
5015}
5016
5017#endif /* FEAT_EVAL */
5018
5019#if defined(FEAT_CRYPT) || defined(PROTO)
5020/*
5021 * Very specific function to remove the value in ":set key=val" from the
5022 * history.
5023 */
5024 void
5025remove_key_from_history()
5026{
5027 char_u *p;
5028 int i;
5029
5030 i = hisidx[HIST_CMD];
5031 if (i < 0)
5032 return;
5033 p = history[HIST_CMD][i].hisstr;
5034 if (p != NULL)
5035 for ( ; *p; ++p)
5036 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5037 {
5038 p = vim_strchr(p + 3, '=');
5039 if (p == NULL)
5040 break;
5041 ++p;
5042 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5043 if (p[i] == '\\' && p[i + 1])
5044 ++i;
5045 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5046 --p;
5047 }
5048}
5049#endif
5050
5051#endif /* FEAT_CMDHIST */
5052
5053#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5054/*
5055 * Get indices "num1,num2" that specify a range within a list (not a range of
5056 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5057 * Returns OK if parsed successfully, otherwise FAIL.
5058 */
5059 int
5060get_list_range(str, num1, num2)
5061 char_u **str;
5062 int *num1;
5063 int *num2;
5064{
5065 int len;
5066 int first = FALSE;
5067 long num;
5068
5069 *str = skipwhite(*str);
5070 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5071 {
5072 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5073 *str += len;
5074 *num1 = (int)num;
5075 first = TRUE;
5076 }
5077 *str = skipwhite(*str);
5078 if (**str == ',') /* parse "to" part of range */
5079 {
5080 *str = skipwhite(*str + 1);
5081 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5082 if (len > 0)
5083 {
5084 *num2 = (int)num;
5085 *str = skipwhite(*str + len);
5086 }
5087 else if (!first) /* no number given at all */
5088 return FAIL;
5089 }
5090 else if (first) /* only one number given */
5091 *num2 = *num1;
5092 return OK;
5093}
5094#endif
5095
5096#if defined(FEAT_CMDHIST) || defined(PROTO)
5097/*
5098 * :history command - print a history
5099 */
5100 void
5101ex_history(eap)
5102 exarg_T *eap;
5103{
5104 histentry_T *hist;
5105 int histype1 = HIST_CMD;
5106 int histype2 = HIST_CMD;
5107 int hisidx1 = 1;
5108 int hisidx2 = -1;
5109 int idx;
5110 int i, j, k;
5111 char_u *end;
5112 char_u *arg = eap->arg;
5113
5114 if (hislen == 0)
5115 {
5116 MSG(_("'history' option is zero"));
5117 return;
5118 }
5119
5120 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5121 {
5122 end = arg;
5123 while (ASCII_ISALPHA(*end)
5124 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5125 end++;
5126 i = *end;
5127 *end = NUL;
5128 histype1 = get_histtype(arg);
5129 if (histype1 == -1)
5130 {
5131 if (STRICMP(arg, "all") == 0)
5132 {
5133 histype1 = 0;
5134 histype2 = HIST_COUNT-1;
5135 }
5136 else
5137 {
5138 *end = i;
5139 EMSG(_(e_trailing));
5140 return;
5141 }
5142 }
5143 else
5144 histype2 = histype1;
5145 *end = i;
5146 }
5147 else
5148 end = arg;
5149 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5150 {
5151 EMSG(_(e_trailing));
5152 return;
5153 }
5154
5155 for (; !got_int && histype1 <= histype2; ++histype1)
5156 {
5157 STRCPY(IObuff, "\n # ");
5158 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5159 MSG_PUTS_TITLE(IObuff);
5160 idx = hisidx[histype1];
5161 hist = history[histype1];
5162 j = hisidx1;
5163 k = hisidx2;
5164 if (j < 0)
5165 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5166 if (k < 0)
5167 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5168 if (idx >= 0 && j <= k)
5169 for (i = idx + 1; !got_int; ++i)
5170 {
5171 if (i == hislen)
5172 i = 0;
5173 if (hist[i].hisstr != NULL
5174 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5175 {
5176 msg_putchar('\n');
5177 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5178 hist[i].hisnum);
5179 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5180 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5181 (int)Columns - 10);
5182 else
5183 STRCAT(IObuff, hist[i].hisstr);
5184 msg_outtrans(IObuff);
5185 out_flush();
5186 }
5187 if (i == idx)
5188 break;
5189 }
5190 }
5191}
5192#endif
5193
5194#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5195static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5196static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5197static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5198static int viminfo_add_at_front = FALSE;
5199
5200static int hist_type2char __ARGS((int type, int use_question));
5201
5202/*
5203 * Translate a history type number to the associated character.
5204 */
5205 static int
5206hist_type2char(type, use_question)
5207 int type;
5208 int use_question; /* use '?' instead of '/' */
5209{
5210 if (type == HIST_CMD)
5211 return ':';
5212 if (type == HIST_SEARCH)
5213 {
5214 if (use_question)
5215 return '?';
5216 else
5217 return '/';
5218 }
5219 if (type == HIST_EXPR)
5220 return '=';
5221 return '@';
5222}
5223
5224/*
5225 * Prepare for reading the history from the viminfo file.
5226 * This allocates history arrays to store the read history lines.
5227 */
5228 void
5229prepare_viminfo_history(asklen)
5230 int asklen;
5231{
5232 int i;
5233 int num;
5234 int type;
5235 int len;
5236
5237 init_history();
5238 viminfo_add_at_front = (asklen != 0);
5239 if (asklen > hislen)
5240 asklen = hislen;
5241
5242 for (type = 0; type < HIST_COUNT; ++type)
5243 {
5244 /*
5245 * Count the number of empty spaces in the history list. If there are
5246 * more spaces available than we request, then fill them up.
5247 */
5248 for (i = 0, num = 0; i < hislen; i++)
5249 if (history[type][i].hisstr == NULL)
5250 num++;
5251 len = asklen;
5252 if (num > len)
5253 len = num;
5254 if (len <= 0)
5255 viminfo_history[type] = NULL;
5256 else
5257 viminfo_history[type] =
5258 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5259 if (viminfo_history[type] == NULL)
5260 len = 0;
5261 viminfo_hislen[type] = len;
5262 viminfo_hisidx[type] = 0;
5263 }
5264}
5265
5266/*
5267 * Accept a line from the viminfo, store it in the history array when it's
5268 * new.
5269 */
5270 int
5271read_viminfo_history(virp)
5272 vir_T *virp;
5273{
5274 int type;
5275 long_u len;
5276 char_u *val;
5277 char_u *p;
5278
5279 type = hist_char2type(virp->vir_line[0]);
5280 if (viminfo_hisidx[type] < viminfo_hislen[type])
5281 {
5282 val = viminfo_readstring(virp, 1, TRUE);
5283 if (val != NULL && *val != NUL)
5284 {
5285 if (!in_history(type, val + (type == HIST_SEARCH),
5286 viminfo_add_at_front))
5287 {
5288 /* Need to re-allocate to append the separator byte. */
5289 len = STRLEN(val);
5290 p = lalloc(len + 2, TRUE);
5291 if (p != NULL)
5292 {
5293 if (type == HIST_SEARCH)
5294 {
5295 /* Search entry: Move the separator from the first
5296 * column to after the NUL. */
5297 mch_memmove(p, val + 1, (size_t)len);
5298 p[len] = (*val == ' ' ? NUL : *val);
5299 }
5300 else
5301 {
5302 /* Not a search entry: No separator in the viminfo
5303 * file, add a NUL separator. */
5304 mch_memmove(p, val, (size_t)len + 1);
5305 p[len + 1] = NUL;
5306 }
5307 viminfo_history[type][viminfo_hisidx[type]++] = p;
5308 }
5309 }
5310 }
5311 vim_free(val);
5312 }
5313 return viminfo_readline(virp);
5314}
5315
5316 void
5317finish_viminfo_history()
5318{
5319 int idx;
5320 int i;
5321 int type;
5322
5323 for (type = 0; type < HIST_COUNT; ++type)
5324 {
5325 if (history[type] == NULL)
5326 return;
5327 idx = hisidx[type] + viminfo_hisidx[type];
5328 if (idx >= hislen)
5329 idx -= hislen;
5330 else if (idx < 0)
5331 idx = hislen - 1;
5332 if (viminfo_add_at_front)
5333 hisidx[type] = idx;
5334 else
5335 {
5336 if (hisidx[type] == -1)
5337 hisidx[type] = hislen - 1;
5338 do
5339 {
5340 if (history[type][idx].hisstr != NULL)
5341 break;
5342 if (++idx == hislen)
5343 idx = 0;
5344 } while (idx != hisidx[type]);
5345 if (idx != hisidx[type] && --idx < 0)
5346 idx = hislen - 1;
5347 }
5348 for (i = 0; i < viminfo_hisidx[type]; i++)
5349 {
5350 vim_free(history[type][idx].hisstr);
5351 history[type][idx].hisstr = viminfo_history[type][i];
5352 if (--idx < 0)
5353 idx = hislen - 1;
5354 }
5355 idx += 1;
5356 idx %= hislen;
5357 for (i = 0; i < viminfo_hisidx[type]; i++)
5358 {
5359 history[type][idx++].hisnum = ++hisnum[type];
5360 idx %= hislen;
5361 }
5362 vim_free(viminfo_history[type]);
5363 viminfo_history[type] = NULL;
5364 }
5365}
5366
5367 void
5368write_viminfo_history(fp)
5369 FILE *fp;
5370{
5371 int i;
5372 int type;
5373 int num_saved;
5374 char_u *p;
5375 int c;
5376
5377 init_history();
5378 if (hislen == 0)
5379 return;
5380 for (type = 0; type < HIST_COUNT; ++type)
5381 {
5382 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5383 if (num_saved == 0)
5384 continue;
5385 if (num_saved < 0) /* Use default */
5386 num_saved = hislen;
5387 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5388 type == HIST_CMD ? _("Command Line") :
5389 type == HIST_SEARCH ? _("Search String") :
5390 type == HIST_EXPR ? _("Expression") :
5391 _("Input Line"));
5392 if (num_saved > hislen)
5393 num_saved = hislen;
5394 i = hisidx[type];
5395 if (i >= 0)
5396 while (num_saved--)
5397 {
5398 p = history[type][i].hisstr;
5399 if (p != NULL)
5400 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005401 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 /* For the search history: put the separator in the second
5403 * column; use a space if there isn't one. */
5404 if (type == HIST_SEARCH)
5405 {
5406 c = p[STRLEN(p) + 1];
5407 putc(c == NUL ? ' ' : c, fp);
5408 }
5409 viminfo_writestring(fp, p);
5410 }
5411 if (--i < 0)
5412 i = hislen - 1;
5413 }
5414 }
5415}
5416#endif /* FEAT_VIMINFO */
5417
5418#if defined(FEAT_FKMAP) || defined(PROTO)
5419/*
5420 * Write a character at the current cursor+offset position.
5421 * It is directly written into the command buffer block.
5422 */
5423 void
5424cmd_pchar(c, offset)
5425 int c, offset;
5426{
5427 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5428 {
5429 EMSG(_("E198: cmd_pchar beyond the command length"));
5430 return;
5431 }
5432 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5433 ccline.cmdbuff[ccline.cmdlen] = NUL;
5434}
5435
5436 int
5437cmd_gchar(offset)
5438 int offset;
5439{
5440 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5441 {
5442 /* EMSG(_("cmd_gchar beyond the command length")); */
5443 return NUL;
5444 }
5445 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5446}
5447#endif
5448
5449#if defined(FEAT_CMDWIN) || defined(PROTO)
5450/*
5451 * Open a window on the current command line and history. Allow editing in
5452 * the window. Returns when the window is closed.
5453 * Returns:
5454 * CR if the command is to be executed
5455 * Ctrl_C if it is to be abandoned
5456 * K_IGNORE if editing continues
5457 */
5458 static int
5459ex_window()
5460{
5461 struct cmdline_info save_ccline;
5462 buf_T *old_curbuf = curbuf;
5463 win_T *old_curwin = curwin;
5464 buf_T *bp;
5465 win_T *wp;
5466 int i;
5467 linenr_T lnum;
5468 int histtype;
5469 garray_T winsizes;
5470 char_u typestr[2];
5471 int save_restart_edit = restart_edit;
5472 int save_State = State;
5473 int save_exmode = exmode_active;
5474
5475 /* Can't do this recursively. Can't do it when typing a password. */
5476 if (cmdwin_type != 0
5477# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5478 || cmdline_star > 0
5479# endif
5480 )
5481 {
5482 beep_flush();
5483 return K_IGNORE;
5484 }
5485
5486 /* Save current window sizes. */
5487 win_size_save(&winsizes);
5488
5489# ifdef FEAT_AUTOCMD
5490 /* Don't execute autocommands while creating the window. */
5491 ++autocmd_block;
5492# endif
5493 /* Create a window for the command-line buffer. */
5494 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5495 {
5496 beep_flush();
5497 return K_IGNORE;
5498 }
5499 cmdwin_type = ccline.cmdfirstc;
5500 if (cmdwin_type == NUL)
5501 cmdwin_type = '-';
5502
5503 /* Create the command-line buffer empty. */
5504 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5505 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5506 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5507 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5508 curbuf->b_p_ma = TRUE;
5509# ifdef FEAT_RIGHTLEFT
5510 curwin->w_p_rl = FALSE;
5511# endif
5512# ifdef FEAT_SCROLLBIND
5513 curwin->w_p_scb = FALSE;
5514# endif
5515
5516# ifdef FEAT_AUTOCMD
5517 /* Do execute autocommands for setting the filetype (load syntax). */
5518 --autocmd_block;
5519# endif
5520
5521 histtype = hist_char2type(ccline.cmdfirstc);
5522 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5523 {
5524 if (p_wc == TAB)
5525 {
5526 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5527 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5528 }
5529 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5530 }
5531
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005532 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5533 * sets 'textwidth' to 78). */
5534 curbuf->b_p_tw = 0;
5535
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 /* Fill the buffer with the history. */
5537 init_history();
5538 if (hislen > 0)
5539 {
5540 i = hisidx[histtype];
5541 if (i >= 0)
5542 {
5543 lnum = 0;
5544 do
5545 {
5546 if (++i == hislen)
5547 i = 0;
5548 if (history[histtype][i].hisstr != NULL)
5549 ml_append(lnum++, history[histtype][i].hisstr,
5550 (colnr_T)0, FALSE);
5551 }
5552 while (i != hisidx[histtype]);
5553 }
5554 }
5555
5556 /* Replace the empty last line with the current command-line and put the
5557 * cursor there. */
5558 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5559 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5560 curwin->w_cursor.col = ccline.cmdpos;
5561 redraw_later(NOT_VALID);
5562
5563 /* Save the command line info, can be used recursively. */
5564 save_ccline = ccline;
5565 ccline.cmdbuff = NULL;
5566 ccline.cmdprompt = NULL;
5567
5568 /* No Ex mode here! */
5569 exmode_active = 0;
5570
5571 State = NORMAL;
5572# ifdef FEAT_MOUSE
5573 setmouse();
5574# endif
5575
5576# ifdef FEAT_AUTOCMD
5577 /* Trigger CmdwinEnter autocommands. */
5578 typestr[0] = cmdwin_type;
5579 typestr[1] = NUL;
5580 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
5581# endif
5582
5583 i = RedrawingDisabled;
5584 RedrawingDisabled = 0;
5585
5586 /*
5587 * Call the main loop until <CR> or CTRL-C is typed.
5588 */
5589 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005590 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591
5592 RedrawingDisabled = i;
5593
5594# ifdef FEAT_AUTOCMD
5595 /* Trigger CmdwinLeave autocommands. */
5596 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
5597# endif
5598
5599 /* Restore the comand line info. */
5600 ccline = save_ccline;
5601 cmdwin_type = 0;
5602
5603 exmode_active = save_exmode;
5604
5605 /* Safety check: The old window or buffer was deleted: It's a a bug when
5606 * this happens! */
5607 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
5608 {
5609 cmdwin_result = Ctrl_C;
5610 EMSG(_("E199: Active window or buffer deleted"));
5611 }
5612 else
5613 {
5614# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5615 /* autocmds may abort script processing */
5616 if (aborting() && cmdwin_result != K_IGNORE)
5617 cmdwin_result = Ctrl_C;
5618# endif
5619 /* Set the new command line from the cmdline buffer. */
5620 vim_free(ccline.cmdbuff);
5621 if (cmdwin_result == K_XF1) /* :qa! typed */
5622 {
5623 ccline.cmdbuff = vim_strsave((char_u *)"qa!");
5624 cmdwin_result = CAR;
5625 }
5626 else if (cmdwin_result == K_XF2) /* :qa typed */
5627 {
5628 ccline.cmdbuff = vim_strsave((char_u *)"qa");
5629 cmdwin_result = CAR;
5630 }
5631 else
5632 ccline.cmdbuff = vim_strsave(ml_get_curline());
5633 if (ccline.cmdbuff == NULL)
5634 cmdwin_result = Ctrl_C;
5635 else
5636 {
5637 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
5638 ccline.cmdbufflen = ccline.cmdlen + 1;
5639 ccline.cmdpos = curwin->w_cursor.col;
5640 if (ccline.cmdpos > ccline.cmdlen)
5641 ccline.cmdpos = ccline.cmdlen;
5642 if (cmdwin_result == K_IGNORE)
5643 {
5644 set_cmdspos_cursor();
5645 redrawcmd();
5646 }
5647 }
5648
5649# ifdef FEAT_AUTOCMD
5650 /* Don't execute autocommands while deleting the window. */
5651 ++autocmd_block;
5652# endif
5653 wp = curwin;
5654 bp = curbuf;
5655 win_goto(old_curwin);
5656 win_close(wp, TRUE);
5657 close_buffer(NULL, bp, DOBUF_WIPE);
5658
5659 /* Restore window sizes. */
5660 win_size_restore(&winsizes);
5661
5662# ifdef FEAT_AUTOCMD
5663 --autocmd_block;
5664# endif
5665 }
5666
5667 ga_clear(&winsizes);
5668 restart_edit = save_restart_edit;
5669
5670 State = save_State;
5671# ifdef FEAT_MOUSE
5672 setmouse();
5673# endif
5674
5675 return cmdwin_result;
5676}
5677#endif /* FEAT_CMDWIN */
5678
5679/*
5680 * Used for commands that either take a simple command string argument, or:
5681 * cmd << endmarker
5682 * {script}
5683 * endmarker
5684 * Returns a pointer to allocated memory with {script} or NULL.
5685 */
5686 char_u *
5687script_get(eap, cmd)
5688 exarg_T *eap;
5689 char_u *cmd;
5690{
5691 char_u *theline;
5692 char *end_pattern = NULL;
5693 char dot[] = ".";
5694 garray_T ga;
5695
5696 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
5697 return NULL;
5698
5699 ga_init2(&ga, 1, 0x400);
5700
5701 if (cmd[2] != NUL)
5702 end_pattern = (char *)skipwhite(cmd + 2);
5703 else
5704 end_pattern = dot;
5705
5706 for (;;)
5707 {
5708 theline = eap->getline(
5709#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00005710 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711#endif
5712 NUL, eap->cookie, 0);
5713
5714 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
5715 break;
5716
5717 ga_concat(&ga, theline);
5718 ga_append(&ga, '\n');
5719 vim_free(theline);
5720 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00005721 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
5723 return (char_u *)ga.ga_data;
5724}