blob: a7de0503e2eec61b7a6059aefdceb7845023a1da [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
16/*
17 * Variables shared between getcmdline(), redrawcmdline() and others.
18 * These need to be saved when using CTRL-R |, that's why they are in a
19 * structure.
20 */
21struct cmdline_info
22{
23 char_u *cmdbuff; /* pointer to command line buffer */
24 int cmdbufflen; /* length of cmdbuff */
25 int cmdlen; /* number of chars in command line */
26 int cmdpos; /* current cursor position */
27 int cmdspos; /* cursor column on screen */
28 int cmdfirstc; /* ':', '/', '?', '=' or NUL */
29 int cmdindent; /* number of spaces before cmdline */
30 char_u *cmdprompt; /* message in front of cmdline */
31 int cmdattr; /* attributes for prompt */
32 int overstrike; /* Typing mode on the command line. Shared by
33 getcmdline() and put_on_cmdline(). */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000034 int xp_context; /* type of expansion */
35# ifdef FEAT_EVAL
36 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000037 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000039};
40
41static struct cmdline_info ccline; /* current cmdline_info */
42
43static int cmd_showtail; /* Only show path tail in lists ? */
44
45#ifdef FEAT_EVAL
46static int new_cmdpos; /* position set by set_cmdline_pos() */
47#endif
48
49#ifdef FEAT_CMDHIST
50typedef struct hist_entry
51{
52 int hisnum; /* identifying number */
53 char_u *hisstr; /* actual entry, separator char after the NUL */
54} histentry_T;
55
56static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
57static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
58static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
59 /* identifying (unique) number of newest history entry */
60static int hislen = 0; /* actual length of history tables */
61
62static int hist_char2type __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
64static int in_history __ARGS((int, char_u *, int));
65# ifdef FEAT_EVAL
66static int calc_hist_idx __ARGS((int histype, int num));
67# endif
68#endif
69
70#ifdef FEAT_RIGHTLEFT
71static int cmd_hkmap = 0; /* Hebrew mapping during command line */
72#endif
73
74#ifdef FEAT_FKMAP
75static int cmd_fkmap = 0; /* Farsi mapping during command line */
76#endif
77
78static int cmdline_charsize __ARGS((int idx));
79static void set_cmdspos __ARGS((void));
80static void set_cmdspos_cursor __ARGS((void));
81#ifdef FEAT_MBYTE
82static void correct_cmdspos __ARGS((int idx, int cells));
83#endif
84static void alloc_cmdbuff __ARGS((int len));
85static int realloc_cmdbuff __ARGS((int len));
86static void draw_cmdline __ARGS((int start, int len));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +000087static void save_cmdline __ARGS((struct cmdline_info *ccp));
88static void restore_cmdline __ARGS((struct cmdline_info *ccp));
Bram Moolenaar1769d5a2006-10-17 14:25:24 +000089static int cmdline_paste __ARGS((int regname, int literally, int remcr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
91static void redrawcmd_preedit __ARGS((void));
92#endif
93#ifdef FEAT_WILDMENU
94static void cmdline_del __ARGS((int from));
95#endif
96static void redrawcmdprompt __ARGS((void));
97static void cursorcmd __ARGS((void));
98static int ccheck_abbr __ARGS((int));
99static int nextwild __ARGS((expand_T *xp, int type, int options));
Bram Moolenaar45360022005-07-21 21:08:21 +0000100static void escape_fname __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101static int showmatches __ARGS((expand_T *xp, int wildmenu));
102static void set_expand_context __ARGS((expand_T *xp));
103static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int));
104static int expand_showtail __ARGS((expand_T *xp));
105#ifdef FEAT_CMDL_COMPL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000106static int expand_shellcmd __ARGS((char_u *filepat, int *num_file, char_u ***file, int flagsarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname));
108# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
109static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000110static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111# endif
112#endif
113
114#ifdef FEAT_CMDWIN
115static int ex_window __ARGS((void));
116#endif
117
118/*
119 * getcmdline() - accept a command line starting with firstc.
120 *
121 * firstc == ':' get ":" command line.
122 * firstc == '/' or '?' get search pattern
123 * firstc == '=' get expression
124 * firstc == '@' get text for input() function
125 * firstc == '>' get text for debug mode
126 * firstc == NUL get text for :insert command
127 * firstc == -1 like NUL, and break on CTRL-C
128 *
129 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
130 * command line.
131 *
132 * Careful: getcmdline() can be called recursively!
133 *
134 * Return pointer to allocated string if there is a commandline, NULL
135 * otherwise.
136 */
137/*ARGSUSED*/
138 char_u *
139getcmdline(firstc, count, indent)
140 int firstc;
141 long count; /* only used for incremental search */
142 int indent; /* indent for inside conditionals */
143{
144 int c;
145 int i;
146 int j;
147 int gotesc = FALSE; /* TRUE when <ESC> just typed */
148 int do_abbr; /* when TRUE check for abbr. */
149#ifdef FEAT_CMDHIST
150 char_u *lookfor = NULL; /* string to match */
151 int hiscnt; /* current history line in use */
152 int histype; /* history type to be used */
153#endif
154#ifdef FEAT_SEARCH_EXTRA
155 pos_T old_cursor;
156 colnr_T old_curswant;
157 colnr_T old_leftcol;
158 linenr_T old_topline;
159# ifdef FEAT_DIFF
160 int old_topfill;
161# endif
162 linenr_T old_botline;
163 int did_incsearch = FALSE;
164 int incsearch_postponed = FALSE;
165#endif
166 int did_wild_list = FALSE; /* did wild_list() recently */
167 int wim_index = 0; /* index in wim_flags[] */
168 int res;
169 int save_msg_scroll = msg_scroll;
170 int save_State = State; /* remember State when called */
171 int some_key_typed = FALSE; /* one of the keys was typed */
172#ifdef FEAT_MOUSE
173 /* mouse drag and release events are ignored, unless they are
174 * preceded with a mouse down event */
175 int ignore_drag_release = TRUE;
176#endif
177#ifdef FEAT_EVAL
178 int break_ctrl_c = FALSE;
179#endif
180 expand_T xpc;
181 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000182#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
183 /* Everything that may work recursively should save and restore the
184 * current command line in save_ccline. That includes update_screen(), a
185 * custom status line may invoke ":normal". */
186 struct cmdline_info save_ccline;
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
189#ifdef FEAT_SNIFF
190 want_sniff_request = 0;
191#endif
192#ifdef FEAT_EVAL
193 if (firstc == -1)
194 {
195 firstc = NUL;
196 break_ctrl_c = TRUE;
197 }
198#endif
199#ifdef FEAT_RIGHTLEFT
200 /* start without Hebrew mapping for a command line */
201 if (firstc == ':' || firstc == '=' || firstc == '>')
202 cmd_hkmap = 0;
203#endif
204
205 ccline.overstrike = FALSE; /* always start in insert mode */
206#ifdef FEAT_SEARCH_EXTRA
207 old_cursor = curwin->w_cursor; /* needs to be restored later */
208 old_curswant = curwin->w_curswant;
209 old_leftcol = curwin->w_leftcol;
210 old_topline = curwin->w_topline;
211# ifdef FEAT_DIFF
212 old_topfill = curwin->w_topfill;
213# endif
214 old_botline = curwin->w_botline;
215#endif
216
217 /*
218 * set some variables for redrawcmd()
219 */
220 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000221 ccline.cmdindent = (firstc > 0 ? indent : 0);
222
223 /* alloc initial ccline.cmdbuff */
224 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 if (ccline.cmdbuff == NULL)
226 return NULL; /* out of memory */
227 ccline.cmdlen = ccline.cmdpos = 0;
228 ccline.cmdbuff[0] = NUL;
229
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000230 /* autoindent for :insert and :append */
231 if (firstc <= 0)
232 {
233 copy_spaces(ccline.cmdbuff, indent);
234 ccline.cmdbuff[indent] = NUL;
235 ccline.cmdpos = indent;
236 ccline.cmdspos = indent;
237 ccline.cmdlen = indent;
238 }
239
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 ExpandInit(&xpc);
241
242#ifdef FEAT_RIGHTLEFT
243 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
244 && (firstc == '/' || firstc == '?'))
245 cmdmsg_rl = TRUE;
246 else
247 cmdmsg_rl = FALSE;
248#endif
249
250 redir_off = TRUE; /* don't redirect the typed command */
251 if (!cmd_silent)
252 {
253 i = msg_scrolled;
254 msg_scrolled = 0; /* avoid wait_return message */
255 gotocmdline(TRUE);
256 msg_scrolled += i;
257 redrawcmdprompt(); /* draw prompt or indent */
258 set_cmdspos();
259 }
260 xpc.xp_context = EXPAND_NOTHING;
261 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000262#ifndef BACKSLASH_IN_FILENAME
263 xpc.xp_shell = FALSE;
264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000266#if defined(FEAT_EVAL)
267 if (ccline.input_fn)
268 {
269 xpc.xp_context = ccline.xp_context;
270 xpc.xp_pattern = ccline.cmdbuff;
271 xpc.xp_arg = ccline.xp_arg;
272 }
273#endif
274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 /*
276 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
277 * doing ":@0" when register 0 doesn't contain a CR.
278 */
279 msg_scroll = FALSE;
280
281 State = CMDLINE;
282
283 if (firstc == '/' || firstc == '?' || firstc == '@')
284 {
285 /* Use ":lmap" mappings for search pattern and input(). */
286 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
287 b_im_ptr = &curbuf->b_p_iminsert;
288 else
289 b_im_ptr = &curbuf->b_p_imsearch;
290 if (*b_im_ptr == B_IMODE_LMAP)
291 State |= LANGMAP;
292#ifdef USE_IM_CONTROL
293 im_set_active(*b_im_ptr == B_IMODE_IM);
294#endif
295 }
296#ifdef USE_IM_CONTROL
297 else if (p_imcmdline)
298 im_set_active(TRUE);
299#endif
300
301#ifdef FEAT_MOUSE
302 setmouse();
303#endif
304#ifdef CURSOR_SHAPE
305 ui_cursor_shape(); /* may show different cursor shape */
306#endif
307
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000308 /* When inside an autocommand for writing "exiting" may be set and
309 * terminal mode set to cooked. Need to set raw mode here then. */
310 settmode(TMODE_RAW);
311
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#ifdef FEAT_CMDHIST
313 init_history();
314 hiscnt = hislen; /* set hiscnt to impossible history value */
315 histype = hist_char2type(firstc);
316#endif
317
318#ifdef FEAT_DIGRAPHS
319 do_digraph(-1); /* init digraph typahead */
320#endif
321
322 /*
323 * Collect the command string, handling editing keys.
324 */
325 for (;;)
326 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000327 redir_off = TRUE; /* Don't redirect the typed command.
328 Repeated, because a ":redir" inside
329 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#ifdef USE_ON_FLY_SCROLL
331 dont_scroll = FALSE; /* allow scrolling here */
332#endif
333 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
334
335 cursorcmd(); /* set the cursor on the right spot */
336 c = safe_vgetc();
337 if (KeyTyped)
338 {
339 some_key_typed = TRUE;
340#ifdef FEAT_RIGHTLEFT
341 if (cmd_hkmap)
342 c = hkmap(c);
343# ifdef FEAT_FKMAP
344 if (cmd_fkmap)
345 c = cmdl_fkmap(c);
346# endif
347 if (cmdmsg_rl && !KeyStuffed)
348 {
349 /* Invert horizontal movements and operations. Only when
350 * typed by the user directly, not when the result of a
351 * mapping. */
352 switch (c)
353 {
354 case K_RIGHT: c = K_LEFT; break;
355 case K_S_RIGHT: c = K_S_LEFT; break;
356 case K_C_RIGHT: c = K_C_LEFT; break;
357 case K_LEFT: c = K_RIGHT; break;
358 case K_S_LEFT: c = K_S_RIGHT; break;
359 case K_C_LEFT: c = K_C_RIGHT; break;
360 }
361 }
362#endif
363 }
364
365 /*
366 * Ignore got_int when CTRL-C was typed here.
367 * Don't ignore it in :global, we really need to break then, e.g., for
368 * ":g/pat/normal /pat" (without the <CR>).
369 * Don't ignore it for the input() function.
370 */
371 if ((c == Ctrl_C
372#ifdef UNIX
373 || c == intr_char
374#endif
375 )
376#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
377 && firstc != '@'
378#endif
379#ifdef FEAT_EVAL
380 && !break_ctrl_c
381#endif
382 && !global_busy)
383 got_int = FALSE;
384
385#ifdef FEAT_CMDHIST
386 /* free old command line when finished moving around in the history
387 * list */
388 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000389 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000390 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 && c != K_PAGEDOWN && c != K_PAGEUP
392 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000393 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
395 {
396 vim_free(lookfor);
397 lookfor = NULL;
398 }
399#endif
400
401 /*
402 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
403 */
404 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles != -1)
405 c = Ctrl_P;
406
407#ifdef FEAT_WILDMENU
408 /* Special translations for 'wildmenu' */
409 if (did_wild_list && p_wmnu)
410 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000411 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000413 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 c = Ctrl_N;
415 }
416 /* Hitting CR after "emenu Name.": complete submenu */
417 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
418 && ccline.cmdpos > 1
419 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
420 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
421 && (c == '\n' || c == '\r' || c == K_KENTER))
422 c = K_DOWN;
423#endif
424
425 /* free expanded names when finished walking through matches */
426 if (xpc.xp_numfiles != -1
427 && !(c == p_wc && KeyTyped) && c != p_wcm
428 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
429 && c != Ctrl_L)
430 {
431 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
432 did_wild_list = FALSE;
433#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000434 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435#endif
436 xpc.xp_context = EXPAND_NOTHING;
437 wim_index = 0;
438#ifdef FEAT_WILDMENU
439 if (p_wmnu && wild_menu_showing != 0)
440 {
441 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000442 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000443
444 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000445 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
447 if (wild_menu_showing == WM_SCROLLED)
448 {
449 /* Entered command line, move it up */
450 cmdline_row--;
451 redrawcmd();
452 }
453 else if (save_p_ls != -1)
454 {
455 /* restore 'laststatus' and 'winminheight' */
456 p_ls = save_p_ls;
457 p_wmh = save_p_wmh;
458 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000459 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000461 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 redrawcmd();
463 save_p_ls = -1;
464 }
465 else
466 {
467# ifdef FEAT_VERTSPLIT
468 win_redraw_last_status(topframe);
469# else
470 lastwin->w_redr_status = TRUE;
471# endif
472 redraw_statuslines();
473 }
474 KeyTyped = skt;
475 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000476 if (ccline.input_fn)
477 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 }
479#endif
480 }
481
482#ifdef FEAT_WILDMENU
483 /* Special translations for 'wildmenu' */
484 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
485 {
486 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000487 if (ccline.cmdbuff[ccline.cmdpos - 1] == '.' && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000489 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 {
491 /* Hitting <Up>: Remove one submenu name in front of the
492 * cursor */
493 int found = FALSE;
494
495 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
496 i = 0;
497 while (--j > 0)
498 {
499 /* check for start of menu name */
500 if (ccline.cmdbuff[j] == ' '
501 && ccline.cmdbuff[j - 1] != '\\')
502 {
503 i = j + 1;
504 break;
505 }
506 /* check for start of submenu name */
507 if (ccline.cmdbuff[j] == '.'
508 && ccline.cmdbuff[j - 1] != '\\')
509 {
510 if (found)
511 {
512 i = j + 1;
513 break;
514 }
515 else
516 found = TRUE;
517 }
518 }
519 if (i > 0)
520 cmdline_del(i);
521 c = p_wc;
522 xpc.xp_context = EXPAND_NOTHING;
523 }
524 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000525 if ((xpc.xp_context == EXPAND_FILES
526 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {
528 char_u upseg[5];
529
530 upseg[0] = PATHSEP;
531 upseg[1] = '.';
532 upseg[2] = '.';
533 upseg[3] = PATHSEP;
534 upseg[4] = NUL;
535
536 if (ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000537 && c == K_DOWN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 && (ccline.cmdbuff[ccline.cmdpos - 2] != '.'
539 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
540 {
541 /* go down a directory */
542 c = p_wc;
543 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000544 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 {
546 /* If in a direct ancestor, strip off one ../ to go down */
547 int found = FALSE;
548
549 j = ccline.cmdpos;
550 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
551 while (--j > i)
552 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000553#ifdef FEAT_MBYTE
554 if (has_mbyte)
555 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 if (vim_ispathsep(ccline.cmdbuff[j]))
558 {
559 found = TRUE;
560 break;
561 }
562 }
563 if (found
564 && ccline.cmdbuff[j - 1] == '.'
565 && ccline.cmdbuff[j - 2] == '.'
566 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
567 {
568 cmdline_del(j - 2);
569 c = p_wc;
570 }
571 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000572 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
574 /* go up a directory */
575 int found = FALSE;
576
577 j = ccline.cmdpos - 1;
578 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
579 while (--j > i)
580 {
581#ifdef FEAT_MBYTE
582 if (has_mbyte)
583 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
584#endif
585 if (vim_ispathsep(ccline.cmdbuff[j])
586#ifdef BACKSLASH_IN_FILENAME
587 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
588 == NULL
589#endif
590 )
591 {
592 if (found)
593 {
594 i = j + 1;
595 break;
596 }
597 else
598 found = TRUE;
599 }
600 }
601
602 if (!found)
603 j = i;
604 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
605 j += 4;
606 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
607 && j == i)
608 j += 3;
609 else
610 j = 0;
611 if (j > 0)
612 {
613 /* TODO this is only for DOS/UNIX systems - need to put in
614 * machine-specific stuff here and in upseg init */
615 cmdline_del(j);
616 put_on_cmdline(upseg + 1, 3, FALSE);
617 }
618 else if (ccline.cmdpos > i)
619 cmdline_del(i);
620 c = p_wc;
621 }
622 }
623#if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
624 if (p_wmnu
625 && (xpc.xp_context == EXPAND_FILES
626 || xpc.xp_context == EXPAND_MENUNAMES)
627 && (c == K_UP || c == K_DOWN))
628 xpc.xp_context = EXPAND_NOTHING;
629#endif
630
631#endif /* FEAT_WILDMENU */
632
633 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
634 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
635 if (c == Ctrl_BSL)
636 {
637 ++no_mapping;
638 ++allow_keys;
639 c = safe_vgetc();
640 --no_mapping;
641 --allow_keys;
642 /* CTRL-\ e doesn't work when obtaining an expression. */
643 if (c != Ctrl_N && c != Ctrl_G
644 && (c != 'e' || ccline.cmdfirstc == '='))
645 {
646 vungetc(c);
647 c = Ctrl_BSL;
648 }
649#ifdef FEAT_EVAL
650 else if (c == 'e')
651 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000652 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
654 /*
655 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000656 * Need to save and restore the current command line, to be
657 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 */
659 if (ccline.cmdpos == ccline.cmdlen)
660 new_cmdpos = 99999; /* keep it at the end */
661 else
662 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000663
664 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000666 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 if (c == '=')
668 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000669 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000670 * to avoid nasty things like going to another buffer when
671 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000672 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000673 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000675 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000676 restore_cmdline(&save_ccline);
677
678 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000680 ccline.cmdlen = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 STRCPY(ccline.cmdbuff, p);
682 vim_free(p);
683
684 /* Restore the cursor or use the position set with
685 * set_cmdline_pos(). */
686 if (new_cmdpos > ccline.cmdlen)
687 ccline.cmdpos = ccline.cmdlen;
688 else
689 ccline.cmdpos = new_cmdpos;
690
691 KeyTyped = FALSE; /* Don't do p_wc completion. */
692 redrawcmd();
693 goto cmdline_changed;
694 }
695 }
696 beep_flush();
697 c = ESC;
698 }
699#endif
700 else
701 {
702 if (c == Ctrl_G && p_im && restart_edit == 0)
703 restart_edit = 'a';
704 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
705 in history */
706 goto returncmd; /* back to Normal mode */
707 }
708 }
709
710#ifdef FEAT_CMDWIN
711 if (c == cedit_key || c == K_CMDWIN)
712 {
713 /*
714 * Open a window to edit the command line (and history).
715 */
716 c = ex_window();
717 some_key_typed = TRUE;
718 }
719# ifdef FEAT_DIGRAPHS
720 else
721# endif
722#endif
723#ifdef FEAT_DIGRAPHS
724 c = do_digraph(c);
725#endif
726
727 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
728 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
729 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000730 /* In Ex mode a backslash escapes a newline. */
731 if (exmode_active
732 && c != ESC
733 && ccline.cmdpos > 0
734 && ccline.cmdpos == ccline.cmdlen
735 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000737 if (c == K_KENTER)
738 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000740 else
741 {
742 gotesc = FALSE; /* Might have typed ESC previously, don't
743 truncate the cmdline now. */
744 if (ccheck_abbr(c + ABBR_OFF))
745 goto cmdline_changed;
746 if (!cmd_silent)
747 {
748 windgoto(msg_row, 0);
749 out_flush();
750 }
751 break;
752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 }
754
755 /*
756 * Completion for 'wildchar' or 'wildcharm' key.
757 * - hitting <ESC> twice means: abandon command line.
758 * - wildcard expansion is only done when the 'wildchar' key is really
759 * typed, not when it comes from a macro
760 */
761 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
762 {
763 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
764 {
765 /* if 'wildmode' contains "list" may still need to list */
766 if (xpc.xp_numfiles > 1
767 && !did_wild_list
768 && (wim_flags[wim_index] & WIM_LIST))
769 {
770 (void)showmatches(&xpc, FALSE);
771 redrawcmd();
772 did_wild_list = TRUE;
773 }
774 if (wim_flags[wim_index] & WIM_LONGEST)
775 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
776 else if (wim_flags[wim_index] & WIM_FULL)
777 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
778 else
779 res = OK; /* don't insert 'wildchar' now */
780 }
781 else /* typed p_wc first time */
782 {
783 wim_index = 0;
784 j = ccline.cmdpos;
785 /* if 'wildmode' first contains "longest", get longest
786 * common part */
787 if (wim_flags[0] & WIM_LONGEST)
788 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
789 else
790 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
791
792 /* if interrupted while completing, behave like it failed */
793 if (got_int)
794 {
795 (void)vpeekc(); /* remove <C-C> from input stream */
796 got_int = FALSE; /* don't abandon the command line */
797 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
798#ifdef FEAT_WILDMENU
799 xpc.xp_context = EXPAND_NOTHING;
800#endif
801 goto cmdline_changed;
802 }
803
804 /* when more than one match, and 'wildmode' first contains
805 * "list", or no change and 'wildmode' contains "longest,list",
806 * list all matches */
807 if (res == OK && xpc.xp_numfiles > 1)
808 {
809 /* a "longest" that didn't do anything is skipped (but not
810 * "list:longest") */
811 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
812 wim_index = 1;
813 if ((wim_flags[wim_index] & WIM_LIST)
814#ifdef FEAT_WILDMENU
815 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
816#endif
817 )
818 {
819 if (!(wim_flags[0] & WIM_LONGEST))
820 {
821#ifdef FEAT_WILDMENU
822 int p_wmnu_save = p_wmnu;
823 p_wmnu = 0;
824#endif
825 nextwild(&xpc, WILD_PREV, 0); /* remove match */
826#ifdef FEAT_WILDMENU
827 p_wmnu = p_wmnu_save;
828#endif
829 }
830#ifdef FEAT_WILDMENU
831 (void)showmatches(&xpc, p_wmnu
832 && ((wim_flags[wim_index] & WIM_LIST) == 0));
833#else
834 (void)showmatches(&xpc, FALSE);
835#endif
836 redrawcmd();
837 did_wild_list = TRUE;
838 if (wim_flags[wim_index] & WIM_LONGEST)
839 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
840 else if (wim_flags[wim_index] & WIM_FULL)
841 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
842 }
843 else
844 vim_beep();
845 }
846#ifdef FEAT_WILDMENU
847 else if (xpc.xp_numfiles == -1)
848 xpc.xp_context = EXPAND_NOTHING;
849#endif
850 }
851 if (wim_index < 3)
852 ++wim_index;
853 if (c == ESC)
854 gotesc = TRUE;
855 if (res == OK)
856 goto cmdline_changed;
857 }
858
859 gotesc = FALSE;
860
861 /* <S-Tab> goes to last match, in a clumsy way */
862 if (c == K_S_TAB && KeyTyped)
863 {
864 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
865 && nextwild(&xpc, WILD_PREV, 0) == OK
866 && nextwild(&xpc, WILD_PREV, 0) == OK)
867 goto cmdline_changed;
868 }
869
870 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
871 c = NL;
872
873 do_abbr = TRUE; /* default: check for abbreviation */
874
875 /*
876 * Big switch for a typed command line character.
877 */
878 switch (c)
879 {
880 case K_BS:
881 case Ctrl_H:
882 case K_DEL:
883 case K_KDEL:
884 case Ctrl_W:
885#ifdef FEAT_FKMAP
886 if (cmd_fkmap && c == K_BS)
887 c = K_DEL;
888#endif
889 if (c == K_KDEL)
890 c = K_DEL;
891
892 /*
893 * delete current character is the same as backspace on next
894 * character, except at end of line
895 */
896 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
897 ++ccline.cmdpos;
898#ifdef FEAT_MBYTE
899 if (has_mbyte && c == K_DEL)
900 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
901 ccline.cmdbuff + ccline.cmdpos);
902#endif
903 if (ccline.cmdpos > 0)
904 {
905 char_u *p;
906
907 j = ccline.cmdpos;
908 p = ccline.cmdbuff + j;
909#ifdef FEAT_MBYTE
910 if (has_mbyte)
911 {
912 p = mb_prevptr(ccline.cmdbuff, p);
913 if (c == Ctrl_W)
914 {
915 while (p > ccline.cmdbuff && vim_isspace(*p))
916 p = mb_prevptr(ccline.cmdbuff, p);
917 i = mb_get_class(p);
918 while (p > ccline.cmdbuff && mb_get_class(p) == i)
919 p = mb_prevptr(ccline.cmdbuff, p);
920 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000921 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 }
923 }
924 else
925#endif
926 if (c == Ctrl_W)
927 {
928 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
929 --p;
930 i = vim_iswordc(p[-1]);
931 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
932 && vim_iswordc(p[-1]) == i)
933 --p;
934 }
935 else
936 --p;
937 ccline.cmdpos = (int)(p - ccline.cmdbuff);
938 ccline.cmdlen -= j - ccline.cmdpos;
939 i = ccline.cmdpos;
940 while (i < ccline.cmdlen)
941 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
942
943 /* Truncate at the end, required for multi-byte chars. */
944 ccline.cmdbuff[ccline.cmdlen] = NUL;
945 redrawcmd();
946 }
947 else if (ccline.cmdlen == 0 && c != Ctrl_W
948 && ccline.cmdprompt == NULL && indent == 0)
949 {
950 /* In ex and debug mode it doesn't make sense to return. */
951 if (exmode_active
952#ifdef FEAT_EVAL
953 || ccline.cmdfirstc == '>'
954#endif
955 )
956 goto cmdline_not_changed;
957
958 vim_free(ccline.cmdbuff); /* no commandline to return */
959 ccline.cmdbuff = NULL;
960 if (!cmd_silent)
961 {
962#ifdef FEAT_RIGHTLEFT
963 if (cmdmsg_rl)
964 msg_col = Columns;
965 else
966#endif
967 msg_col = 0;
968 msg_putchar(' '); /* delete ':' */
969 }
970 redraw_cmdline = TRUE;
971 goto returncmd; /* back to cmd mode */
972 }
973 goto cmdline_changed;
974
975 case K_INS:
976 case K_KINS:
977#ifdef FEAT_FKMAP
978 /* if Farsi mode set, we are in reverse insert mode -
979 Do not change the mode */
980 if (cmd_fkmap)
981 beep_flush();
982 else
983#endif
984 ccline.overstrike = !ccline.overstrike;
985#ifdef CURSOR_SHAPE
986 ui_cursor_shape(); /* may show different cursor shape */
987#endif
988 goto cmdline_not_changed;
989
990 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000991 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 {
993 /* ":lmap" mappings exists, toggle use of mappings. */
994 State ^= LANGMAP;
995#ifdef USE_IM_CONTROL
996 im_set_active(FALSE); /* Disable input method */
997#endif
998 if (b_im_ptr != NULL)
999 {
1000 if (State & LANGMAP)
1001 *b_im_ptr = B_IMODE_LMAP;
1002 else
1003 *b_im_ptr = B_IMODE_NONE;
1004 }
1005 }
1006#ifdef USE_IM_CONTROL
1007 else
1008 {
1009 /* There are no ":lmap" mappings, toggle IM. When
1010 * 'imdisable' is set don't try getting the status, it's
1011 * always off. */
1012 if ((p_imdisable && b_im_ptr != NULL)
1013 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1014 {
1015 im_set_active(FALSE); /* Disable input method */
1016 if (b_im_ptr != NULL)
1017 *b_im_ptr = B_IMODE_NONE;
1018 }
1019 else
1020 {
1021 im_set_active(TRUE); /* Enable input method */
1022 if (b_im_ptr != NULL)
1023 *b_im_ptr = B_IMODE_IM;
1024 }
1025 }
1026#endif
1027 if (b_im_ptr != NULL)
1028 {
1029 if (b_im_ptr == &curbuf->b_p_iminsert)
1030 set_iminsert_global();
1031 else
1032 set_imsearch_global();
1033 }
1034#ifdef CURSOR_SHAPE
1035 ui_cursor_shape(); /* may show different cursor shape */
1036#endif
1037#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1038 /* Show/unshow value of 'keymap' in status lines later. */
1039 status_redraw_curbuf();
1040#endif
1041 goto cmdline_not_changed;
1042
1043/* case '@': only in very old vi */
1044 case Ctrl_U:
1045 /* delete all characters left of the cursor */
1046 j = ccline.cmdpos;
1047 ccline.cmdlen -= j;
1048 i = ccline.cmdpos = 0;
1049 while (i < ccline.cmdlen)
1050 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1051 /* Truncate at the end, required for multi-byte chars. */
1052 ccline.cmdbuff[ccline.cmdlen] = NUL;
1053 redrawcmd();
1054 goto cmdline_changed;
1055
1056#ifdef FEAT_CLIPBOARD
1057 case Ctrl_Y:
1058 /* Copy the modeless selection, if there is one. */
1059 if (clip_star.state != SELECT_CLEARED)
1060 {
1061 if (clip_star.state == SELECT_DONE)
1062 clip_copy_modeless_selection(TRUE);
1063 goto cmdline_not_changed;
1064 }
1065 break;
1066#endif
1067
1068 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1069 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001070 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001071 * ":normal" runs out of characters. */
1072 if (exmode_active
1073#ifdef FEAT_EX_EXTRA
1074 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1075#endif
1076 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 goto cmdline_not_changed;
1078
1079 gotesc = TRUE; /* will free ccline.cmdbuff after
1080 putting it in history */
1081 goto returncmd; /* back to cmd mode */
1082
1083 case Ctrl_R: /* insert register */
1084#ifdef USE_ON_FLY_SCROLL
1085 dont_scroll = TRUE; /* disallow scrolling here */
1086#endif
1087 putcmdline('"', TRUE);
1088 ++no_mapping;
1089 i = c = safe_vgetc(); /* CTRL-R <char> */
1090 if (i == Ctrl_O)
1091 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1092 if (i == Ctrl_R)
1093 c = safe_vgetc(); /* CTRL-R CTRL-R <char> */
1094 --no_mapping;
1095#ifdef FEAT_EVAL
1096 /*
1097 * Insert the result of an expression.
1098 * Need to save the current command line, to be able to enter
1099 * a new one...
1100 */
1101 new_cmdpos = -1;
1102 if (c == '=')
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1105 {
1106 beep_flush();
1107 c = ESC;
1108 }
1109 else
1110 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001111 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001113 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 }
1115 }
1116#endif
1117 if (c != ESC) /* use ESC to cancel inserting register */
1118 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001119 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001120
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001121#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001122 /* When there was a serious error abort getting the
1123 * command line. */
1124 if (aborting())
1125 {
1126 gotesc = TRUE; /* will free ccline.cmdbuff after
1127 putting it in history */
1128 goto returncmd; /* back to cmd mode */
1129 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 KeyTyped = FALSE; /* Don't do p_wc completion. */
1132#ifdef FEAT_EVAL
1133 if (new_cmdpos >= 0)
1134 {
1135 /* set_cmdline_pos() was used */
1136 if (new_cmdpos > ccline.cmdlen)
1137 ccline.cmdpos = ccline.cmdlen;
1138 else
1139 ccline.cmdpos = new_cmdpos;
1140 }
1141#endif
1142 }
1143 redrawcmd();
1144 goto cmdline_changed;
1145
1146 case Ctrl_D:
1147 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1148 break; /* Use ^D as normal char instead */
1149
1150 redrawcmd();
1151 continue; /* don't do incremental search now */
1152
1153 case K_RIGHT:
1154 case K_S_RIGHT:
1155 case K_C_RIGHT:
1156 do
1157 {
1158 if (ccline.cmdpos >= ccline.cmdlen)
1159 break;
1160 i = cmdline_charsize(ccline.cmdpos);
1161 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1162 break;
1163 ccline.cmdspos += i;
1164#ifdef FEAT_MBYTE
1165 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001166 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 + ccline.cmdpos);
1168 else
1169#endif
1170 ++ccline.cmdpos;
1171 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001172 while ((c == K_S_RIGHT || c == K_C_RIGHT
1173 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1175#ifdef FEAT_MBYTE
1176 if (has_mbyte)
1177 set_cmdspos_cursor();
1178#endif
1179 goto cmdline_not_changed;
1180
1181 case K_LEFT:
1182 case K_S_LEFT:
1183 case K_C_LEFT:
1184 do
1185 {
1186 if (ccline.cmdpos == 0)
1187 break;
1188 --ccline.cmdpos;
1189#ifdef FEAT_MBYTE
1190 if (has_mbyte) /* move to first byte of char */
1191 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1192 ccline.cmdbuff + ccline.cmdpos);
1193#endif
1194 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1195 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001196 while ((c == K_S_LEFT || c == K_C_LEFT
1197 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1199#ifdef FEAT_MBYTE
1200 if (has_mbyte)
1201 set_cmdspos_cursor();
1202#endif
1203 goto cmdline_not_changed;
1204
1205 case K_IGNORE:
1206 goto cmdline_not_changed; /* Ignore mouse */
1207
Bram Moolenaar4770d092006-01-12 23:22:24 +00001208#ifdef FEAT_GUI_W32
1209 /* On Win32 ignore <M-F4>, we get it when closing the window was
1210 * cancelled. */
1211 case K_F4:
1212 if (mod_mask == MOD_MASK_ALT)
1213 {
1214 redrawcmd(); /* somehow the cmdline is cleared */
1215 goto cmdline_not_changed;
1216 }
1217 break;
1218#endif
1219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#ifdef FEAT_MOUSE
1221 case K_MIDDLEDRAG:
1222 case K_MIDDLERELEASE:
1223 goto cmdline_not_changed; /* Ignore mouse */
1224
1225 case K_MIDDLEMOUSE:
1226# ifdef FEAT_GUI
1227 /* When GUI is active, also paste when 'mouse' is empty */
1228 if (!gui.in_use)
1229# endif
1230 if (!mouse_has(MOUSE_COMMAND))
1231 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001232# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001234 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001236# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001237 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 redrawcmd();
1239 goto cmdline_changed;
1240
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001241# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001243 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 redrawcmd();
1245 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001246# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
1248 case K_LEFTDRAG:
1249 case K_LEFTRELEASE:
1250 case K_RIGHTDRAG:
1251 case K_RIGHTRELEASE:
1252 /* Ignore drag and release events when the button-down wasn't
1253 * seen before. */
1254 if (ignore_drag_release)
1255 goto cmdline_not_changed;
1256 /* FALLTHROUGH */
1257 case K_LEFTMOUSE:
1258 case K_RIGHTMOUSE:
1259 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1260 ignore_drag_release = TRUE;
1261 else
1262 ignore_drag_release = FALSE;
1263# ifdef FEAT_GUI
1264 /* When GUI is active, also move when 'mouse' is empty */
1265 if (!gui.in_use)
1266# endif
1267 if (!mouse_has(MOUSE_COMMAND))
1268 goto cmdline_not_changed; /* Ignore mouse */
1269# ifdef FEAT_CLIPBOARD
1270 if (mouse_row < cmdline_row && clip_star.available)
1271 {
1272 int button, is_click, is_drag;
1273
1274 /*
1275 * Handle modeless selection.
1276 */
1277 button = get_mouse_button(KEY2TERMCAP1(c),
1278 &is_click, &is_drag);
1279 if (mouse_model_popup() && button == MOUSE_LEFT
1280 && (mod_mask & MOD_MASK_SHIFT))
1281 {
1282 /* Translate shift-left to right button. */
1283 button = MOUSE_RIGHT;
1284 mod_mask &= ~MOD_MASK_SHIFT;
1285 }
1286 clip_modeless(button, is_click, is_drag);
1287 goto cmdline_not_changed;
1288 }
1289# endif
1290
1291 set_cmdspos();
1292 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1293 ++ccline.cmdpos)
1294 {
1295 i = cmdline_charsize(ccline.cmdpos);
1296 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1297 && mouse_col < ccline.cmdspos % Columns + i)
1298 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001299# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (has_mbyte)
1301 {
1302 /* Count ">" for double-wide char that doesn't fit. */
1303 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001304 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 + ccline.cmdpos) - 1;
1306 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 ccline.cmdspos += i;
1309 }
1310 goto cmdline_not_changed;
1311
1312 /* Mouse scroll wheel: ignored here */
1313 case K_MOUSEDOWN:
1314 case K_MOUSEUP:
1315 /* Alternate buttons ignored here */
1316 case K_X1MOUSE:
1317 case K_X1DRAG:
1318 case K_X1RELEASE:
1319 case K_X2MOUSE:
1320 case K_X2DRAG:
1321 case K_X2RELEASE:
1322 goto cmdline_not_changed;
1323
1324#endif /* FEAT_MOUSE */
1325
1326#ifdef FEAT_GUI
1327 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1328 case K_LEFTRELEASE_NM:
1329 goto cmdline_not_changed;
1330
1331 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001332 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 {
1334 gui_do_scroll();
1335 redrawcmd();
1336 }
1337 goto cmdline_not_changed;
1338
1339 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001340 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 {
1342 gui_do_horiz_scroll();
1343 redrawcmd();
1344 }
1345 goto cmdline_not_changed;
1346#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001347#ifdef FEAT_GUI_TABLINE
1348 case K_TABLINE:
1349 case K_TABMENU:
1350 /* Don't want to change any tabs here. Make sure the same tab
1351 * is still selected. */
1352 if (gui_use_tabline())
1353 gui_mch_set_curtab(tabpage_index(curtab));
1354 goto cmdline_not_changed;
1355#endif
1356
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 case K_SELECT: /* end of Select mode mapping - ignore */
1358 goto cmdline_not_changed;
1359
1360 case Ctrl_B: /* begin of command line */
1361 case K_HOME:
1362 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 case K_S_HOME:
1364 case K_C_HOME:
1365 ccline.cmdpos = 0;
1366 set_cmdspos();
1367 goto cmdline_not_changed;
1368
1369 case Ctrl_E: /* end of command line */
1370 case K_END:
1371 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 case K_S_END:
1373 case K_C_END:
1374 ccline.cmdpos = ccline.cmdlen;
1375 set_cmdspos_cursor();
1376 goto cmdline_not_changed;
1377
1378 case Ctrl_A: /* all matches */
1379 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1380 break;
1381 goto cmdline_changed;
1382
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001383 case Ctrl_L:
1384#ifdef FEAT_SEARCH_EXTRA
1385 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1386 {
1387 /* Add a character from under the cursor for 'incsearch' */
1388 if (did_incsearch
1389 && !equalpos(curwin->w_cursor, old_cursor))
1390 {
1391 c = gchar_cursor();
1392 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001393 {
1394 if (c == firstc || vim_strchr((char_u *)(
1395 p_magic ? "\\^$.*[" : "\\^$"), c)
1396 != NULL)
1397 {
1398 /* put a backslash before special characters */
1399 stuffcharReadbuff(c);
1400 c = '\\';
1401 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001402 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001403 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001404 }
1405 goto cmdline_not_changed;
1406 }
1407#endif
1408
1409 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1411 break;
1412 goto cmdline_changed;
1413
1414 case Ctrl_N: /* next match */
1415 case Ctrl_P: /* previous match */
1416 if (xpc.xp_numfiles > 0)
1417 {
1418 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1419 == FAIL)
1420 break;
1421 goto cmdline_changed;
1422 }
1423
1424#ifdef FEAT_CMDHIST
1425 case K_UP:
1426 case K_DOWN:
1427 case K_S_UP:
1428 case K_S_DOWN:
1429 case K_PAGEUP:
1430 case K_KPAGEUP:
1431 case K_PAGEDOWN:
1432 case K_KPAGEDOWN:
1433 if (hislen == 0 || firstc == NUL) /* no history */
1434 goto cmdline_not_changed;
1435
1436 i = hiscnt;
1437
1438 /* save current command string so it can be restored later */
1439 if (lookfor == NULL)
1440 {
1441 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1442 goto cmdline_not_changed;
1443 lookfor[ccline.cmdpos] = NUL;
1444 }
1445
1446 j = (int)STRLEN(lookfor);
1447 for (;;)
1448 {
1449 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001450 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001451 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 {
1453 if (hiscnt == hislen) /* first time */
1454 hiscnt = hisidx[histype];
1455 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1456 hiscnt = hislen - 1;
1457 else if (hiscnt != hisidx[histype] + 1)
1458 --hiscnt;
1459 else /* at top of list */
1460 {
1461 hiscnt = i;
1462 break;
1463 }
1464 }
1465 else /* one step forwards */
1466 {
1467 /* on last entry, clear the line */
1468 if (hiscnt == hisidx[histype])
1469 {
1470 hiscnt = hislen;
1471 break;
1472 }
1473
1474 /* not on a history line, nothing to do */
1475 if (hiscnt == hislen)
1476 break;
1477 if (hiscnt == hislen - 1) /* wrap around */
1478 hiscnt = 0;
1479 else
1480 ++hiscnt;
1481 }
1482 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1483 {
1484 hiscnt = i;
1485 break;
1486 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001487 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001488 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 || STRNCMP(history[histype][hiscnt].hisstr,
1490 lookfor, (size_t)j) == 0)
1491 break;
1492 }
1493
1494 if (hiscnt != i) /* jumped to other entry */
1495 {
1496 char_u *p;
1497 int len;
1498 int old_firstc;
1499
1500 vim_free(ccline.cmdbuff);
1501 if (hiscnt == hislen)
1502 p = lookfor; /* back to the old one */
1503 else
1504 p = history[histype][hiscnt].hisstr;
1505
1506 if (histype == HIST_SEARCH
1507 && p != lookfor
1508 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1509 {
1510 /* Correct for the separator character used when
1511 * adding the history entry vs the one used now.
1512 * First loop: count length.
1513 * Second loop: copy the characters. */
1514 for (i = 0; i <= 1; ++i)
1515 {
1516 len = 0;
1517 for (j = 0; p[j] != NUL; ++j)
1518 {
1519 /* Replace old sep with new sep, unless it is
1520 * escaped. */
1521 if (p[j] == old_firstc
1522 && (j == 0 || p[j - 1] != '\\'))
1523 {
1524 if (i > 0)
1525 ccline.cmdbuff[len] = firstc;
1526 }
1527 else
1528 {
1529 /* Escape new sep, unless it is already
1530 * escaped. */
1531 if (p[j] == firstc
1532 && (j == 0 || p[j - 1] != '\\'))
1533 {
1534 if (i > 0)
1535 ccline.cmdbuff[len] = '\\';
1536 ++len;
1537 }
1538 if (i > 0)
1539 ccline.cmdbuff[len] = p[j];
1540 }
1541 ++len;
1542 }
1543 if (i == 0)
1544 {
1545 alloc_cmdbuff(len);
1546 if (ccline.cmdbuff == NULL)
1547 goto returncmd;
1548 }
1549 }
1550 ccline.cmdbuff[len] = NUL;
1551 }
1552 else
1553 {
1554 alloc_cmdbuff((int)STRLEN(p));
1555 if (ccline.cmdbuff == NULL)
1556 goto returncmd;
1557 STRCPY(ccline.cmdbuff, p);
1558 }
1559
1560 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1561 redrawcmd();
1562 goto cmdline_changed;
1563 }
1564 beep_flush();
1565 goto cmdline_not_changed;
1566#endif
1567
1568 case Ctrl_V:
1569 case Ctrl_Q:
1570#ifdef FEAT_MOUSE
1571 ignore_drag_release = TRUE;
1572#endif
1573 putcmdline('^', TRUE);
1574 c = get_literal(); /* get next (two) character(s) */
1575 do_abbr = FALSE; /* don't do abbreviation now */
1576#ifdef FEAT_MBYTE
1577 /* may need to remove ^ when composing char was typed */
1578 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1579 {
1580 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1581 msg_putchar(' ');
1582 cursorcmd();
1583 }
1584#endif
1585 break;
1586
1587#ifdef FEAT_DIGRAPHS
1588 case Ctrl_K:
1589#ifdef FEAT_MOUSE
1590 ignore_drag_release = TRUE;
1591#endif
1592 putcmdline('?', TRUE);
1593#ifdef USE_ON_FLY_SCROLL
1594 dont_scroll = TRUE; /* disallow scrolling here */
1595#endif
1596 c = get_digraph(TRUE);
1597 if (c != NUL)
1598 break;
1599
1600 redrawcmd();
1601 goto cmdline_not_changed;
1602#endif /* FEAT_DIGRAPHS */
1603
1604#ifdef FEAT_RIGHTLEFT
1605 case Ctrl__: /* CTRL-_: switch language mode */
1606 if (!p_ari)
1607 break;
1608#ifdef FEAT_FKMAP
1609 if (p_altkeymap)
1610 {
1611 cmd_fkmap = !cmd_fkmap;
1612 if (cmd_fkmap) /* in Farsi always in Insert mode */
1613 ccline.overstrike = FALSE;
1614 }
1615 else /* Hebrew is default */
1616#endif
1617 cmd_hkmap = !cmd_hkmap;
1618 goto cmdline_not_changed;
1619#endif
1620
1621 default:
1622#ifdef UNIX
1623 if (c == intr_char)
1624 {
1625 gotesc = TRUE; /* will free ccline.cmdbuff after
1626 putting it in history */
1627 goto returncmd; /* back to Normal mode */
1628 }
1629#endif
1630 /*
1631 * Normal character with no special meaning. Just set mod_mask
1632 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1633 * the string <S-Space>. This should only happen after ^V.
1634 */
1635 if (!IS_SPECIAL(c))
1636 mod_mask = 0x0;
1637 break;
1638 }
1639 /*
1640 * End of switch on command line character.
1641 * We come here if we have a normal character.
1642 */
1643
1644 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1645#ifdef FEAT_MBYTE
1646 /* Add ABBR_OFF for characters above 0x100, this is
1647 * what check_abbr() expects. */
1648 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1649#endif
1650 c))
1651 goto cmdline_changed;
1652
1653 /*
1654 * put the character in the command line
1655 */
1656 if (IS_SPECIAL(c) || mod_mask != 0)
1657 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1658 else
1659 {
1660#ifdef FEAT_MBYTE
1661 if (has_mbyte)
1662 {
1663 j = (*mb_char2bytes)(c, IObuff);
1664 IObuff[j] = NUL; /* exclude composing chars */
1665 put_on_cmdline(IObuff, j, TRUE);
1666 }
1667 else
1668#endif
1669 {
1670 IObuff[0] = c;
1671 put_on_cmdline(IObuff, 1, TRUE);
1672 }
1673 }
1674 goto cmdline_changed;
1675
1676/*
1677 * This part implements incremental searches for "/" and "?"
1678 * Jump to cmdline_not_changed when a character has been read but the command
1679 * line did not change. Then we only search and redraw if something changed in
1680 * the past.
1681 * Jump to cmdline_changed when the command line did change.
1682 * (Sorry for the goto's, I know it is ugly).
1683 */
1684cmdline_not_changed:
1685#ifdef FEAT_SEARCH_EXTRA
1686 if (!incsearch_postponed)
1687 continue;
1688#endif
1689
1690cmdline_changed:
1691#ifdef FEAT_SEARCH_EXTRA
1692 /*
1693 * 'incsearch' highlighting.
1694 */
1695 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1696 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001697 pos_T end_pos;
1698
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 /* if there is a character waiting, search and redraw later */
1700 if (char_avail())
1701 {
1702 incsearch_postponed = TRUE;
1703 continue;
1704 }
1705 incsearch_postponed = FALSE;
1706 curwin->w_cursor = old_cursor; /* start at old position */
1707
1708 /* If there is no command line, don't do anything */
1709 if (ccline.cmdlen == 0)
1710 i = 0;
1711 else
1712 {
1713 cursor_off(); /* so the user knows we're busy */
1714 out_flush();
1715 ++emsg_off; /* So it doesn't beep if bad expr */
1716 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1717 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
1718 --emsg_off;
1719 /* if interrupted while searching, behave like it failed */
1720 if (got_int)
1721 {
1722 (void)vpeekc(); /* remove <C-C> from input stream */
1723 got_int = FALSE; /* don't abandon the command line */
1724 i = 0;
1725 }
1726 else if (char_avail())
1727 /* cancelled searching because a char was typed */
1728 incsearch_postponed = TRUE;
1729 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001730 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 highlight_match = TRUE; /* highlight position */
1732 else
1733 highlight_match = FALSE; /* remove highlight */
1734
1735 /* first restore the old curwin values, so the screen is
1736 * positioned in the same way as the actual search command */
1737 curwin->w_leftcol = old_leftcol;
1738 curwin->w_topline = old_topline;
1739# ifdef FEAT_DIFF
1740 curwin->w_topfill = old_topfill;
1741# endif
1742 curwin->w_botline = old_botline;
1743 changed_cline_bef_curs();
1744 update_topline();
1745
1746 if (i != 0)
1747 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001748 pos_T save_pos = curwin->w_cursor;
1749
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001751 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 * moves the whole match onto the screen when 'nowrap' is set.
1753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 curwin->w_cursor.lnum += search_match_lines;
1755 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001756 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1757 {
1758 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1759 coladvance((colnr_T)MAXCOL);
1760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001762 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001763 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001765 else
1766 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001769# ifdef FEAT_WINDOWS
1770 /* May redraw the status line to show the cursor position. */
1771 if (p_ru && curwin->w_status_height > 0)
1772 curwin->w_redr_status = TRUE;
1773# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001775 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001776 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001777 restore_cmdline(&save_ccline);
1778
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001779 /* Leave it at the end to make CTRL-R CTRL-W work. */
1780 if (i != 0)
1781 curwin->w_cursor = end_pos;
1782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 msg_starthere();
1784 redrawcmdline();
1785 did_incsearch = TRUE;
1786 }
1787#else /* FEAT_SEARCH_EXTRA */
1788 ;
1789#endif
1790
1791#ifdef FEAT_RIGHTLEFT
1792 if (cmdmsg_rl
1793# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001794 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795# endif
1796 )
1797 /* Always redraw the whole command line to fix shaping and
1798 * right-left typing. Not efficient, but it works. */
1799 redrawcmd();
1800#endif
1801 }
1802
1803returncmd:
1804
1805#ifdef FEAT_RIGHTLEFT
1806 cmdmsg_rl = FALSE;
1807#endif
1808
1809#ifdef FEAT_FKMAP
1810 cmd_fkmap = 0;
1811#endif
1812
1813 ExpandCleanup(&xpc);
1814
1815#ifdef FEAT_SEARCH_EXTRA
1816 if (did_incsearch)
1817 {
1818 curwin->w_cursor = old_cursor;
1819 curwin->w_curswant = old_curswant;
1820 curwin->w_leftcol = old_leftcol;
1821 curwin->w_topline = old_topline;
1822# ifdef FEAT_DIFF
1823 curwin->w_topfill = old_topfill;
1824# endif
1825 curwin->w_botline = old_botline;
1826 highlight_match = FALSE;
1827 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001828 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 }
1830#endif
1831
1832 if (ccline.cmdbuff != NULL)
1833 {
1834 /*
1835 * Put line in history buffer (":" and "=" only when it was typed).
1836 */
1837#ifdef FEAT_CMDHIST
1838 if (ccline.cmdlen && firstc != NUL
1839 && (some_key_typed || histype == HIST_SEARCH))
1840 {
1841 add_to_history(histype, ccline.cmdbuff, TRUE,
1842 histype == HIST_SEARCH ? firstc : NUL);
1843 if (firstc == ':')
1844 {
1845 vim_free(new_last_cmdline);
1846 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1847 }
1848 }
1849#endif
1850
1851 if (gotesc) /* abandon command line */
1852 {
1853 vim_free(ccline.cmdbuff);
1854 ccline.cmdbuff = NULL;
1855 if (msg_scrolled == 0)
1856 compute_cmdrow();
1857 MSG("");
1858 redraw_cmdline = TRUE;
1859 }
1860 }
1861
1862 /*
1863 * If the screen was shifted up, redraw the whole screen (later).
1864 * If the line is too long, clear it, so ruler and shown command do
1865 * not get printed in the middle of it.
1866 */
1867 msg_check();
1868 msg_scroll = save_msg_scroll;
1869 redir_off = FALSE;
1870
1871 /* When the command line was typed, no need for a wait-return prompt. */
1872 if (some_key_typed)
1873 need_wait_return = FALSE;
1874
1875 State = save_State;
1876#ifdef USE_IM_CONTROL
1877 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1878 im_save_status(b_im_ptr);
1879 im_set_active(FALSE);
1880#endif
1881#ifdef FEAT_MOUSE
1882 setmouse();
1883#endif
1884#ifdef CURSOR_SHAPE
1885 ui_cursor_shape(); /* may show different cursor shape */
1886#endif
1887
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001888 {
1889 char_u *p = ccline.cmdbuff;
1890
1891 /* Make ccline empty, getcmdline() may try to use it. */
1892 ccline.cmdbuff = NULL;
1893 return p;
1894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895}
1896
1897#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1898/*
1899 * Get a command line with a prompt.
1900 * This is prepared to be called recursively from getcmdline() (e.g. by
1901 * f_input() when evaluating an expression from CTRL-R =).
1902 * Returns the command line in allocated memory, or NULL.
1903 */
1904 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001905getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 int firstc;
1907 char_u *prompt; /* command line prompt */
1908 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001909 int xp_context; /* type of expansion */
1910 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911{
1912 char_u *s;
1913 struct cmdline_info save_ccline;
1914 int msg_col_save = msg_col;
1915
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001916 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 ccline.cmdprompt = prompt;
1918 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001919# ifdef FEAT_EVAL
1920 ccline.xp_context = xp_context;
1921 ccline.xp_arg = xp_arg;
1922 ccline.input_fn = (firstc == '@');
1923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001925 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 /* Restore msg_col, the prompt from input() may have changed it. */
1927 msg_col = msg_col_save;
1928
1929 return s;
1930}
1931#endif
1932
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001933/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001934 * Return TRUE when the text must not be changed and we can't switch to
1935 * another window or buffer. Used when editing the command line, evaluating
1936 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001937 */
1938 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001939text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001940{
1941#ifdef FEAT_CMDWIN
1942 if (cmdwin_type != 0)
1943 return TRUE;
1944#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001945 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001946}
1947
1948/*
1949 * Give an error message for a command that isn't allowed while the cmdline
1950 * window is open or editing the cmdline in another way.
1951 */
1952 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001953text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001954{
1955#ifdef FEAT_CMDWIN
1956 if (cmdwin_type != 0)
1957 EMSG(_(e_cmdwin));
1958 else
1959#endif
1960 EMSG(_(e_secure));
1961}
1962
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001963#if defined(FEAT_AUTOCMD) || defined(PROTO)
1964/*
1965 * Check if "curbuf_lock" is set and return TRUE when it is and give an error
1966 * message.
1967 */
1968 int
1969curbuf_locked()
1970{
1971 if (curbuf_lock > 0)
1972 {
1973 EMSG(_("E788: Not allowed to edit another buffer now"));
1974 return TRUE;
1975 }
1976 return FALSE;
1977}
1978#endif
1979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 static int
1981cmdline_charsize(idx)
1982 int idx;
1983{
1984#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1985 if (cmdline_star > 0) /* showing '*', always 1 position */
1986 return 1;
1987#endif
1988 return ptr2cells(ccline.cmdbuff + idx);
1989}
1990
1991/*
1992 * Compute the offset of the cursor on the command line for the prompt and
1993 * indent.
1994 */
1995 static void
1996set_cmdspos()
1997{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001998 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 ccline.cmdspos = 1 + ccline.cmdindent;
2000 else
2001 ccline.cmdspos = 0 + ccline.cmdindent;
2002}
2003
2004/*
2005 * Compute the screen position for the cursor on the command line.
2006 */
2007 static void
2008set_cmdspos_cursor()
2009{
2010 int i, m, c;
2011
2012 set_cmdspos();
2013 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002016 if (m < 0) /* overflow, Columns or Rows at weird value */
2017 m = MAXCOL;
2018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 else
2020 m = MAXCOL;
2021 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2022 {
2023 c = cmdline_charsize(i);
2024#ifdef FEAT_MBYTE
2025 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2026 if (has_mbyte)
2027 correct_cmdspos(i, c);
2028#endif
2029 /* If the cmdline doesn't fit, put cursor on last visible char. */
2030 if ((ccline.cmdspos += c) >= m)
2031 {
2032 ccline.cmdpos = i - 1;
2033 ccline.cmdspos -= c;
2034 break;
2035 }
2036#ifdef FEAT_MBYTE
2037 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002038 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039#endif
2040 }
2041}
2042
2043#ifdef FEAT_MBYTE
2044/*
2045 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2046 * character that doesn't fit, so that a ">" must be displayed.
2047 */
2048 static void
2049correct_cmdspos(idx, cells)
2050 int idx;
2051 int cells;
2052{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002053 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2055 && ccline.cmdspos % Columns + cells > Columns)
2056 ccline.cmdspos++;
2057}
2058#endif
2059
2060/*
2061 * Get an Ex command line for the ":" command.
2062 */
2063/* ARGSUSED */
2064 char_u *
2065getexline(c, dummy, indent)
2066 int c; /* normally ':', NUL for ":append" */
2067 void *dummy; /* cookie not used */
2068 int indent; /* indent for inside conditionals */
2069{
2070 /* When executing a register, remove ':' that's in front of each line. */
2071 if (exec_from_reg && vpeekc() == ':')
2072 (void)vgetc();
2073 return getcmdline(c, 1L, indent);
2074}
2075
2076/*
2077 * Get an Ex command line for Ex mode.
2078 * In Ex mode we only use the OS supplied line editing features and no
2079 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002080 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 */
2082/* ARGSUSED */
2083 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002084getexmodeline(promptc, dummy, indent)
2085 int promptc; /* normally ':', NUL for ":append" and '?' for
2086 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 void *dummy; /* cookie not used */
2088 int indent; /* indent for inside conditionals */
2089{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002090 garray_T line_ga;
2091 char_u *pend;
2092 int startcol = 0;
2093 int c1;
2094 int escaped = FALSE; /* CTRL-V typed */
2095 int vcol = 0;
2096 char_u *p;
2097 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098
2099 /* Switch cursor on now. This avoids that it happens after the "\n", which
2100 * confuses the system function that computes tabstops. */
2101 cursor_on();
2102
2103 /* always start in column 0; write a newline if necessary */
2104 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002105 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002107 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002109 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002110 if (p_prompt)
2111 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 while (indent-- > 0)
2113 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 }
2116
2117 ga_init2(&line_ga, 1, 30);
2118
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002119 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002120 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002121 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002122 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002123 while (indent >= 8)
2124 {
2125 ga_append(&line_ga, TAB);
2126 msg_puts((char_u *)" ");
2127 indent -= 8;
2128 }
2129 while (indent-- > 0)
2130 {
2131 ga_append(&line_ga, ' ');
2132 msg_putchar(' ');
2133 }
2134 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002135 ++no_mapping;
2136 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002137
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 /*
2139 * Get the line, one character at a time.
2140 */
2141 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002142 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
2144 if (ga_grow(&line_ga, 40) == FAIL)
2145 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002146 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002148 /* Get one character at a time. Don't use inchar(), it can't handle
2149 * special characters. */
2150 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
2152 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002153 * Handle line editing.
2154 * Previously this was left to the system, putting the terminal in
2155 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002157 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002159 msg_putchar('\n');
2160 break;
2161 }
2162
2163 if (!escaped)
2164 {
2165 /* CR typed means "enter", which is NL */
2166 if (c1 == '\r')
2167 c1 = '\n';
2168
2169 if (c1 == BS || c1 == K_BS
2170 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002172 if (line_ga.ga_len > 0)
2173 {
2174 --line_ga.ga_len;
2175 goto redraw;
2176 }
2177 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
2179
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002180 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002182 msg_col = startcol;
2183 msg_clr_eos();
2184 line_ga.ga_len = 0;
2185 continue;
2186 }
2187
2188 if (c1 == Ctrl_T)
2189 {
2190 p = (char_u *)line_ga.ga_data;
2191 p[line_ga.ga_len] = NUL;
2192 indent = get_indent_str(p, 8);
2193 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2194add_indent:
2195 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002197 char_u *s = skipwhite(p);
2198
2199 ga_grow(&line_ga, 1);
2200 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2201 *s = ' ';
2202 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002204redraw:
2205 /* redraw the line */
2206 msg_col = startcol;
2207 windgoto(msg_row, msg_col);
2208 vcol = 0;
2209 for (p = (char_u *)line_ga.ga_data;
2210 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002212 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002214 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002216 msg_putchar(' ');
2217 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002219 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002221 msg_outtrans_len(p, 1);
2222 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 }
2224 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002225 msg_clr_eos();
2226 continue;
2227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002229 if (c1 == Ctrl_D)
2230 {
2231 /* Delete one shiftwidth. */
2232 p = (char_u *)line_ga.ga_data;
2233 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002235 if (prev_char == '^')
2236 ex_keep_indent = TRUE;
2237 indent = 0;
2238 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 }
2240 else
2241 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002242 p[line_ga.ga_len] = NUL;
2243 indent = get_indent_str(p, 8);
2244 --indent;
2245 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002247 while (get_indent_str(p, 8) > indent)
2248 {
2249 char_u *s = skipwhite(p);
2250
2251 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2252 --line_ga.ga_len;
2253 }
2254 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002256
2257 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2258 {
2259 escaped = TRUE;
2260 continue;
2261 }
2262
2263 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2264 if (IS_SPECIAL(c1))
2265 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002267
2268 if (IS_SPECIAL(c1))
2269 c1 = '?';
2270 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2271 prev_char = c1;
2272 if (c1 == '\n')
2273 msg_putchar('\n');
2274 else if (c1 == TAB)
2275 {
2276 /* Don't use chartabsize(), 'ts' can be different */
2277 do
2278 {
2279 msg_putchar(' ');
2280 } while (++vcol % 8);
2281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002284 msg_outtrans_len(
2285 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2286 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002288 ++line_ga.ga_len;
2289 escaped = FALSE;
2290
2291 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002292 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002293
2294 /* we are done when a NL is entered, but not when it comes after a
2295 * backslash */
2296 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2297 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002300 --pend;
2301 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002302 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
2304 }
2305
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002306 --no_mapping;
2307 --allow_keys;
2308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 /* make following messages go to the next line */
2310 msg_didout = FALSE;
2311 msg_col = 0;
2312 if (msg_row < Rows - 1)
2313 ++msg_row;
2314 emsg_on_display = FALSE; /* don't want ui_delay() */
2315
2316 if (got_int)
2317 ga_clear(&line_ga);
2318
2319 return (char_u *)line_ga.ga_data;
2320}
2321
Bram Moolenaare344bea2005-09-01 20:46:49 +00002322# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2323 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324/*
2325 * Return TRUE if ccline.overstrike is on.
2326 */
2327 int
2328cmdline_overstrike()
2329{
2330 return ccline.overstrike;
2331}
2332
2333/*
2334 * Return TRUE if the cursor is at the end of the cmdline.
2335 */
2336 int
2337cmdline_at_end()
2338{
2339 return (ccline.cmdpos >= ccline.cmdlen);
2340}
2341#endif
2342
Bram Moolenaar9372a112005-12-06 19:59:18 +00002343#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344/*
2345 * Return the virtual column number at the current cursor position.
2346 * This is used by the IM code to obtain the start of the preedit string.
2347 */
2348 colnr_T
2349cmdline_getvcol_cursor()
2350{
2351 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2352 return MAXCOL;
2353
2354# ifdef FEAT_MBYTE
2355 if (has_mbyte)
2356 {
2357 colnr_T col;
2358 int i = 0;
2359
2360 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002361 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
2363 return col;
2364 }
2365 else
2366# endif
2367 return ccline.cmdpos;
2368}
2369#endif
2370
2371#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2372/*
2373 * If part of the command line is an IM preedit string, redraw it with
2374 * IM feedback attributes. The cursor position is restored after drawing.
2375 */
2376 static void
2377redrawcmd_preedit()
2378{
2379 if ((State & CMDLINE)
2380 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002381 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 && !p_imdisable
2383 && im_is_preediting())
2384 {
2385 int cmdpos = 0;
2386 int cmdspos;
2387 int old_row;
2388 int old_col;
2389 colnr_T col;
2390
2391 old_row = msg_row;
2392 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002393 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
2395# ifdef FEAT_MBYTE
2396 if (has_mbyte)
2397 {
2398 for (col = 0; col < preedit_start_col
2399 && cmdpos < ccline.cmdlen; ++col)
2400 {
2401 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002402 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404 }
2405 else
2406# endif
2407 {
2408 cmdspos += preedit_start_col;
2409 cmdpos += preedit_start_col;
2410 }
2411
2412 msg_row = cmdline_row + (cmdspos / (int)Columns);
2413 msg_col = cmdspos % (int)Columns;
2414 if (msg_row >= Rows)
2415 msg_row = Rows - 1;
2416
2417 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2418 {
2419 int char_len;
2420 int char_attr;
2421
2422 char_attr = im_get_feedback_attr(col);
2423 if (char_attr < 0)
2424 break; /* end of preedit string */
2425
2426# ifdef FEAT_MBYTE
2427 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002428 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 else
2430# endif
2431 char_len = 1;
2432
2433 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2434 cmdpos += char_len;
2435 }
2436
2437 msg_row = old_row;
2438 msg_col = old_col;
2439 }
2440}
2441#endif /* FEAT_XIM && FEAT_GUI_GTK */
2442
2443/*
2444 * Allocate a new command line buffer.
2445 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2446 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2447 */
2448 static void
2449alloc_cmdbuff(len)
2450 int len;
2451{
2452 /*
2453 * give some extra space to avoid having to allocate all the time
2454 */
2455 if (len < 80)
2456 len = 100;
2457 else
2458 len += 20;
2459
2460 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2461 ccline.cmdbufflen = len;
2462}
2463
2464/*
2465 * Re-allocate the command line to length len + something extra.
2466 * return FAIL for failure, OK otherwise
2467 */
2468 static int
2469realloc_cmdbuff(len)
2470 int len;
2471{
2472 char_u *p;
2473
2474 p = ccline.cmdbuff;
2475 alloc_cmdbuff(len); /* will get some more */
2476 if (ccline.cmdbuff == NULL) /* out of memory */
2477 {
2478 ccline.cmdbuff = p; /* keep the old one */
2479 return FAIL;
2480 }
2481 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2482 vim_free(p);
2483 return OK;
2484}
2485
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002486#if defined(FEAT_ARABIC) || defined(PROTO)
2487static char_u *arshape_buf = NULL;
2488
2489# if defined(EXITFREE) || defined(PROTO)
2490 void
2491free_cmdline_buf()
2492{
2493 vim_free(arshape_buf);
2494}
2495# endif
2496#endif
2497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498/*
2499 * Draw part of the cmdline at the current cursor position. But draw stars
2500 * when cmdline_star is TRUE.
2501 */
2502 static void
2503draw_cmdline(start, len)
2504 int start;
2505 int len;
2506{
2507#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2508 int i;
2509
2510 if (cmdline_star > 0)
2511 for (i = 0; i < len; ++i)
2512 {
2513 msg_putchar('*');
2514# ifdef FEAT_MBYTE
2515 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002516 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517# endif
2518 }
2519 else
2520#endif
2521#ifdef FEAT_ARABIC
2522 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 static int buflen = 0;
2525 char_u *p;
2526 int j;
2527 int newlen = 0;
2528 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002529 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 int prev_c = 0;
2531 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002532 int u8c;
2533 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
2536 /*
2537 * Do arabic shaping into a temporary buffer. This is very
2538 * inefficient!
2539 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002540 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
2542 /* Re-allocate the buffer. We keep it around to avoid a lot of
2543 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002544 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002545 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002546 arshape_buf = alloc(buflen);
2547 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 return; /* out of memory */
2549 }
2550
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002551 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2552 {
2553 /* Prepend a space to draw the leading composing char on. */
2554 arshape_buf[0] = ' ';
2555 newlen = 1;
2556 }
2557
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 for (j = start; j < start + len; j += mb_l)
2559 {
2560 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002561 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002562 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (ARABIC_CHAR(u8c))
2564 {
2565 /* Do Arabic shaping. */
2566 if (cmdmsg_rl)
2567 {
2568 /* displaying from right to left */
2569 pc = prev_c;
2570 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002571 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 if (j + mb_l >= start + len)
2573 nc = NUL;
2574 else
2575 nc = utf_ptr2char(p + mb_l);
2576 }
2577 else
2578 {
2579 /* displaying from left to right */
2580 if (j + mb_l >= start + len)
2581 pc = NUL;
2582 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002583 {
2584 int pcc[MAX_MCO];
2585
2586 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002588 pc1 = pcc[0];
2589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 nc = prev_c;
2591 }
2592 prev_c = u8c;
2593
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002594 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002596 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002597 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002599 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2600 if (u8cc[1] != 0)
2601 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002602 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
2604 }
2605 else
2606 {
2607 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002608 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 newlen += mb_l;
2610 }
2611 }
2612
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002613 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 }
2615 else
2616#endif
2617 msg_outtrans_len(ccline.cmdbuff + start, len);
2618}
2619
2620/*
2621 * Put a character on the command line. Shifts the following text to the
2622 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2623 * "c" must be printable (fit in one display cell)!
2624 */
2625 void
2626putcmdline(c, shift)
2627 int c;
2628 int shift;
2629{
2630 if (cmd_silent)
2631 return;
2632 msg_no_more = TRUE;
2633 msg_putchar(c);
2634 if (shift)
2635 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2636 msg_no_more = FALSE;
2637 cursorcmd();
2638}
2639
2640/*
2641 * Undo a putcmdline(c, FALSE).
2642 */
2643 void
2644unputcmdline()
2645{
2646 if (cmd_silent)
2647 return;
2648 msg_no_more = TRUE;
2649 if (ccline.cmdlen == ccline.cmdpos)
2650 msg_putchar(' ');
2651 else
2652 draw_cmdline(ccline.cmdpos, 1);
2653 msg_no_more = FALSE;
2654 cursorcmd();
2655}
2656
2657/*
2658 * Put the given string, of the given length, onto the command line.
2659 * If len is -1, then STRLEN() is used to calculate the length.
2660 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2661 * part will be redrawn, otherwise it will not. If this function is called
2662 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2663 * called afterwards.
2664 */
2665 int
2666put_on_cmdline(str, len, redraw)
2667 char_u *str;
2668 int len;
2669 int redraw;
2670{
2671 int retval;
2672 int i;
2673 int m;
2674 int c;
2675
2676 if (len < 0)
2677 len = (int)STRLEN(str);
2678
2679 /* Check if ccline.cmdbuff needs to be longer */
2680 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2681 retval = realloc_cmdbuff(ccline.cmdlen + len);
2682 else
2683 retval = OK;
2684 if (retval == OK)
2685 {
2686 if (!ccline.overstrike)
2687 {
2688 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2689 ccline.cmdbuff + ccline.cmdpos,
2690 (size_t)(ccline.cmdlen - ccline.cmdpos));
2691 ccline.cmdlen += len;
2692 }
2693 else
2694 {
2695#ifdef FEAT_MBYTE
2696 if (has_mbyte)
2697 {
2698 /* Count nr of characters in the new string. */
2699 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002700 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 ++m;
2702 /* Count nr of bytes in cmdline that are overwritten by these
2703 * characters. */
2704 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002705 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 --m;
2707 if (i < ccline.cmdlen)
2708 {
2709 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2710 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2711 ccline.cmdlen += ccline.cmdpos + len - i;
2712 }
2713 else
2714 ccline.cmdlen = ccline.cmdpos + len;
2715 }
2716 else
2717#endif
2718 if (ccline.cmdpos + len > ccline.cmdlen)
2719 ccline.cmdlen = ccline.cmdpos + len;
2720 }
2721 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2722 ccline.cmdbuff[ccline.cmdlen] = NUL;
2723
2724#ifdef FEAT_MBYTE
2725 if (enc_utf8)
2726 {
2727 /* When the inserted text starts with a composing character,
2728 * backup to the character before it. There could be two of them.
2729 */
2730 i = 0;
2731 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2732 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2733 {
2734 i = (*mb_head_off)(ccline.cmdbuff,
2735 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2736 ccline.cmdpos -= i;
2737 len += i;
2738 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2739 }
2740# ifdef FEAT_ARABIC
2741 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2742 {
2743 /* Check the previous character for Arabic combining pair. */
2744 i = (*mb_head_off)(ccline.cmdbuff,
2745 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2746 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2747 + ccline.cmdpos - i), c))
2748 {
2749 ccline.cmdpos -= i;
2750 len += i;
2751 }
2752 else
2753 i = 0;
2754 }
2755# endif
2756 if (i != 0)
2757 {
2758 /* Also backup the cursor position. */
2759 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2760 ccline.cmdspos -= i;
2761 msg_col -= i;
2762 if (msg_col < 0)
2763 {
2764 msg_col += Columns;
2765 --msg_row;
2766 }
2767 }
2768 }
2769#endif
2770
2771 if (redraw && !cmd_silent)
2772 {
2773 msg_no_more = TRUE;
2774 i = cmdline_row;
2775 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2776 /* Avoid clearing the rest of the line too often. */
2777 if (cmdline_row != i || ccline.overstrike)
2778 msg_clr_eos();
2779 msg_no_more = FALSE;
2780 }
2781#ifdef FEAT_FKMAP
2782 /*
2783 * If we are in Farsi command mode, the character input must be in
2784 * Insert mode. So do not advance the cmdpos.
2785 */
2786 if (!cmd_fkmap)
2787#endif
2788 {
2789 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002790 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002792 if (m < 0) /* overflow, Columns or Rows at weird value */
2793 m = MAXCOL;
2794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 else
2796 m = MAXCOL;
2797 for (i = 0; i < len; ++i)
2798 {
2799 c = cmdline_charsize(ccline.cmdpos);
2800#ifdef FEAT_MBYTE
2801 /* count ">" for a double-wide char that doesn't fit. */
2802 if (has_mbyte)
2803 correct_cmdspos(ccline.cmdpos, c);
2804#endif
2805 /* Stop cursor at the end of the screen */
2806 if (ccline.cmdspos + c >= m)
2807 break;
2808 ccline.cmdspos += c;
2809#ifdef FEAT_MBYTE
2810 if (has_mbyte)
2811 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002812 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 if (c > len - i - 1)
2814 c = len - i - 1;
2815 ccline.cmdpos += c;
2816 i += c;
2817 }
2818#endif
2819 ++ccline.cmdpos;
2820 }
2821 }
2822 }
2823 if (redraw)
2824 msg_check();
2825 return retval;
2826}
2827
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002828static struct cmdline_info prev_ccline;
2829static int prev_ccline_used = FALSE;
2830
2831/*
2832 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2833 * and overwrite it. But get_cmdline_str() may need it, thus make it
2834 * available globally in prev_ccline.
2835 */
2836 static void
2837save_cmdline(ccp)
2838 struct cmdline_info *ccp;
2839{
2840 if (!prev_ccline_used)
2841 {
2842 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2843 prev_ccline_used = TRUE;
2844 }
2845 *ccp = prev_ccline;
2846 prev_ccline = ccline;
2847 ccline.cmdbuff = NULL;
2848 ccline.cmdprompt = NULL;
2849}
2850
2851/*
2852 * Resture ccline after it has been saved with save_cmdline().
2853 */
2854 static void
2855restore_cmdline(ccp)
2856 struct cmdline_info *ccp;
2857{
2858 ccline = prev_ccline;
2859 prev_ccline = *ccp;
2860}
2861
Bram Moolenaar5a305422006-04-28 22:38:25 +00002862#if defined(FEAT_EVAL) || defined(PROTO)
2863/*
2864 * Save the command line into allocated memory. Returns a pointer to be
2865 * passed to restore_cmdline_alloc() later.
2866 * Returns NULL when failed.
2867 */
2868 char_u *
2869save_cmdline_alloc()
2870{
2871 struct cmdline_info *p;
2872
2873 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2874 if (p != NULL)
2875 save_cmdline(p);
2876 return (char_u *)p;
2877}
2878
2879/*
2880 * Restore the command line from the return value of save_cmdline_alloc().
2881 */
2882 void
2883restore_cmdline_alloc(p)
2884 char_u *p;
2885{
2886 if (p != NULL)
2887 {
2888 restore_cmdline((struct cmdline_info *)p);
2889 vim_free(p);
2890 }
2891}
2892#endif
2893
Bram Moolenaar8299df92004-07-10 09:47:34 +00002894/*
2895 * paste a yank register into the command line.
2896 * used by CTRL-R command in command-line mode
2897 * insert_reg() can't be used here, because special characters from the
2898 * register contents will be interpreted as commands.
2899 *
2900 * return FAIL for failure, OK otherwise
2901 */
2902 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002903cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00002904 int regname;
2905 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002906 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00002907{
2908 long i;
2909 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002910 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002911 int allocated;
2912 struct cmdline_info save_ccline;
2913
2914 /* check for valid regname; also accept special characters for CTRL-R in
2915 * the command line */
2916 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2917 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2918 return FAIL;
2919
2920 /* A register containing CTRL-R can cause an endless loop. Allow using
2921 * CTRL-C to break the loop. */
2922 line_breakcheck();
2923 if (got_int)
2924 return FAIL;
2925
2926#ifdef FEAT_CLIPBOARD
2927 regname = may_get_selection(regname);
2928#endif
2929
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002930 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002931 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002932 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002933 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002934 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002935 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002936 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002937
2938 if (i)
2939 {
2940 /* Got the value of a special register in "arg". */
2941 if (arg == NULL)
2942 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002943
2944 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
2945 * part of the word. */
2946 p = arg;
2947 if (p_is && regname == Ctrl_W)
2948 {
2949 char_u *w;
2950 int len;
2951
2952 /* Locate start of last word in the cmd buffer. */
2953 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
2954 {
2955#ifdef FEAT_MBYTE
2956 if (has_mbyte)
2957 {
2958 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
2959 if (!vim_iswordc(mb_ptr2char(w - len)))
2960 break;
2961 w -= len;
2962 }
2963 else
2964#endif
2965 {
2966 if (!vim_iswordc(w[-1]))
2967 break;
2968 --w;
2969 }
2970 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002971 len = (int)((ccline.cmdbuff + ccline.cmdlen) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002972 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
2973 p += len;
2974 }
2975
2976 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002977 if (allocated)
2978 vim_free(arg);
2979 return OK;
2980 }
2981
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002982 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002983}
2984
2985/*
2986 * Put a string on the command line.
2987 * When "literally" is TRUE, insert literally.
2988 * When "literally" is FALSE, insert as typed, but don't leave the command
2989 * line.
2990 */
2991 void
2992cmdline_paste_str(s, literally)
2993 char_u *s;
2994 int literally;
2995{
2996 int c, cv;
2997
2998 if (literally)
2999 put_on_cmdline(s, -1, TRUE);
3000 else
3001 while (*s != NUL)
3002 {
3003 cv = *s;
3004 if (cv == Ctrl_V && s[1])
3005 ++s;
3006#ifdef FEAT_MBYTE
3007 if (has_mbyte)
3008 {
3009 c = mb_ptr2char(s);
3010 s += mb_char2len(c);
3011 }
3012 else
3013#endif
3014 c = *s++;
3015 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3016#ifdef UNIX
3017 || c == intr_char
3018#endif
3019 || (c == Ctrl_BSL && *s == Ctrl_N))
3020 stuffcharReadbuff(Ctrl_V);
3021 stuffcharReadbuff(c);
3022 }
3023}
3024
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025#ifdef FEAT_WILDMENU
3026/*
3027 * Delete characters on the command line, from "from" to the current
3028 * position.
3029 */
3030 static void
3031cmdline_del(from)
3032 int from;
3033{
3034 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3035 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3036 ccline.cmdlen -= ccline.cmdpos - from;
3037 ccline.cmdpos = from;
3038}
3039#endif
3040
3041/*
3042 * this fuction is called when the screen size changes and with incremental
3043 * search
3044 */
3045 void
3046redrawcmdline()
3047{
3048 if (cmd_silent)
3049 return;
3050 need_wait_return = FALSE;
3051 compute_cmdrow();
3052 redrawcmd();
3053 cursorcmd();
3054}
3055
3056 static void
3057redrawcmdprompt()
3058{
3059 int i;
3060
3061 if (cmd_silent)
3062 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003063 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 msg_putchar(ccline.cmdfirstc);
3065 if (ccline.cmdprompt != NULL)
3066 {
3067 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3068 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3069 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003070 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 --ccline.cmdindent;
3072 }
3073 else
3074 for (i = ccline.cmdindent; i > 0; --i)
3075 msg_putchar(' ');
3076}
3077
3078/*
3079 * Redraw what is currently on the command line.
3080 */
3081 void
3082redrawcmd()
3083{
3084 if (cmd_silent)
3085 return;
3086
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003087 /* when 'incsearch' is set there may be no command line while redrawing */
3088 if (ccline.cmdbuff == NULL)
3089 {
3090 windgoto(cmdline_row, 0);
3091 msg_clr_eos();
3092 return;
3093 }
3094
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 msg_start();
3096 redrawcmdprompt();
3097
3098 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3099 msg_no_more = TRUE;
3100 draw_cmdline(0, ccline.cmdlen);
3101 msg_clr_eos();
3102 msg_no_more = FALSE;
3103
3104 set_cmdspos_cursor();
3105
3106 /*
3107 * An emsg() before may have set msg_scroll. This is used in normal mode,
3108 * in cmdline mode we can reset them now.
3109 */
3110 msg_scroll = FALSE; /* next message overwrites cmdline */
3111
3112 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3113 * in cmdline mode */
3114 skip_redraw = FALSE;
3115}
3116
3117 void
3118compute_cmdrow()
3119{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003120 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 cmdline_row = Rows - 1;
3122 else
3123 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3124 + W_STATUS_HEIGHT(lastwin);
3125}
3126
3127 static void
3128cursorcmd()
3129{
3130 if (cmd_silent)
3131 return;
3132
3133#ifdef FEAT_RIGHTLEFT
3134 if (cmdmsg_rl)
3135 {
3136 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3137 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3138 if (msg_row <= 0)
3139 msg_row = Rows - 1;
3140 }
3141 else
3142#endif
3143 {
3144 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3145 msg_col = ccline.cmdspos % (int)Columns;
3146 if (msg_row >= Rows)
3147 msg_row = Rows - 1;
3148 }
3149
3150 windgoto(msg_row, msg_col);
3151#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3152 redrawcmd_preedit();
3153#endif
3154#ifdef MCH_CURSOR_SHAPE
3155 mch_update_cursor();
3156#endif
3157}
3158
3159 void
3160gotocmdline(clr)
3161 int clr;
3162{
3163 msg_start();
3164#ifdef FEAT_RIGHTLEFT
3165 if (cmdmsg_rl)
3166 msg_col = Columns - 1;
3167 else
3168#endif
3169 msg_col = 0; /* always start in column 0 */
3170 if (clr) /* clear the bottom line(s) */
3171 msg_clr_eos(); /* will reset clear_cmdline */
3172 windgoto(cmdline_row, 0);
3173}
3174
3175/*
3176 * Check the word in front of the cursor for an abbreviation.
3177 * Called when the non-id character "c" has been entered.
3178 * When an abbreviation is recognized it is removed from the text with
3179 * backspaces and the replacement string is inserted, followed by "c".
3180 */
3181 static int
3182ccheck_abbr(c)
3183 int c;
3184{
3185 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3186 return FALSE;
3187
3188 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3189}
3190
3191/*
3192 * Return FAIL if this is not an appropriate context in which to do
3193 * completion of anything, return OK if it is (even if there are no matches).
3194 * For the caller, this means that the character is just passed through like a
3195 * normal character (instead of being expanded). This allows :s/^I^D etc.
3196 */
3197 static int
3198nextwild(xp, type, options)
3199 expand_T *xp;
3200 int type;
3201 int options; /* extra options for ExpandOne() */
3202{
3203 int i, j;
3204 char_u *p1;
3205 char_u *p2;
3206 int oldlen;
3207 int difflen;
3208 int v;
3209
3210 if (xp->xp_numfiles == -1)
3211 {
3212 set_expand_context(xp);
3213 cmd_showtail = expand_showtail(xp);
3214 }
3215
3216 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3217 {
3218 beep_flush();
3219 return OK; /* Something illegal on command line */
3220 }
3221 if (xp->xp_context == EXPAND_NOTHING)
3222 {
3223 /* Caller can use the character as a normal char instead */
3224 return FAIL;
3225 }
3226
3227 MSG_PUTS("..."); /* show that we are busy */
3228 out_flush();
3229
3230 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3231 oldlen = ccline.cmdpos - i;
3232
3233 if (type == WILD_NEXT || type == WILD_PREV)
3234 {
3235 /*
3236 * Get next/previous match for a previous expanded pattern.
3237 */
3238 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3239 }
3240 else
3241 {
3242 /*
3243 * Translate string into pattern and expand it.
3244 */
3245 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3246 p2 = NULL;
3247 else
3248 {
3249 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3250 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3251 |options, type);
3252 vim_free(p1);
3253 /* longest match: make sure it is not shorter (happens with :help */
3254 if (p2 != NULL && type == WILD_LONGEST)
3255 {
3256 for (j = 0; j < oldlen; ++j)
3257 if (ccline.cmdbuff[i + j] == '*'
3258 || ccline.cmdbuff[i + j] == '?')
3259 break;
3260 if ((int)STRLEN(p2) < j)
3261 {
3262 vim_free(p2);
3263 p2 = NULL;
3264 }
3265 }
3266 }
3267 }
3268
3269 if (p2 != NULL && !got_int)
3270 {
3271 difflen = (int)STRLEN(p2) - oldlen;
3272 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3273 {
3274 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3275 xp->xp_pattern = ccline.cmdbuff + i;
3276 }
3277 else
3278 v = OK;
3279 if (v == OK)
3280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003281 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3282 &ccline.cmdbuff[ccline.cmdpos],
3283 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3284 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 ccline.cmdlen += difflen;
3286 ccline.cmdpos += difflen;
3287 }
3288 }
3289 vim_free(p2);
3290
3291 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003292 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
3294 /* When expanding a ":map" command and no matches are found, assume that
3295 * the key is supposed to be inserted literally */
3296 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3297 return FAIL;
3298
3299 if (xp->xp_numfiles <= 0 && p2 == NULL)
3300 beep_flush();
3301 else if (xp->xp_numfiles == 1)
3302 /* free expanded pattern */
3303 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3304
3305 return OK;
3306}
3307
3308/*
3309 * Do wildcard expansion on the string 'str'.
3310 * Chars that should not be expanded must be preceded with a backslash.
3311 * Return a pointer to alloced memory containing the new string.
3312 * Return NULL for failure.
3313 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003314 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3315 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 *
3317 * mode = WILD_FREE: just free previously expanded matches
3318 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3319 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3320 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3321 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3322 * mode = WILD_ALL: return all matches concatenated
3323 * mode = WILD_LONGEST: return longest matched part
3324 *
3325 * options = WILD_LIST_NOTFOUND: list entries without a match
3326 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3327 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3328 * options = WILD_NO_BEEP: Don't beep for multiple matches
3329 * options = WILD_ADD_SLASH: add a slash after directory names
3330 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3331 * options = WILD_SILENT: don't print warning messages
3332 * options = WILD_ESCAPE: put backslash before special chars
3333 *
3334 * The variables xp->xp_context and xp->xp_backslash must have been set!
3335 */
3336 char_u *
3337ExpandOne(xp, str, orig, options, mode)
3338 expand_T *xp;
3339 char_u *str;
3340 char_u *orig; /* allocated copy of original of expanded string */
3341 int options;
3342 int mode;
3343{
3344 char_u *ss = NULL;
3345 static int findex;
3346 static char_u *orig_save = NULL; /* kept value of orig */
3347 int i;
3348 long_u len;
3349 int non_suf_match; /* number without matching suffix */
3350
3351 /*
3352 * first handle the case of using an old match
3353 */
3354 if (mode == WILD_NEXT || mode == WILD_PREV)
3355 {
3356 if (xp->xp_numfiles > 0)
3357 {
3358 if (mode == WILD_PREV)
3359 {
3360 if (findex == -1)
3361 findex = xp->xp_numfiles;
3362 --findex;
3363 }
3364 else /* mode == WILD_NEXT */
3365 ++findex;
3366
3367 /*
3368 * When wrapping around, return the original string, set findex to
3369 * -1.
3370 */
3371 if (findex < 0)
3372 {
3373 if (orig_save == NULL)
3374 findex = xp->xp_numfiles - 1;
3375 else
3376 findex = -1;
3377 }
3378 if (findex >= xp->xp_numfiles)
3379 {
3380 if (orig_save == NULL)
3381 findex = 0;
3382 else
3383 findex = -1;
3384 }
3385#ifdef FEAT_WILDMENU
3386 if (p_wmnu)
3387 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3388 findex, cmd_showtail);
3389#endif
3390 if (findex == -1)
3391 return vim_strsave(orig_save);
3392 return vim_strsave(xp->xp_files[findex]);
3393 }
3394 else
3395 return NULL;
3396 }
3397
3398/* free old names */
3399 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3400 {
3401 FreeWild(xp->xp_numfiles, xp->xp_files);
3402 xp->xp_numfiles = -1;
3403 vim_free(orig_save);
3404 orig_save = NULL;
3405 }
3406 findex = 0;
3407
3408 if (mode == WILD_FREE) /* only release file name */
3409 return NULL;
3410
3411 if (xp->xp_numfiles == -1)
3412 {
3413 vim_free(orig_save);
3414 orig_save = orig;
3415
3416 /*
3417 * Do the expansion.
3418 */
3419 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3420 options) == FAIL)
3421 {
3422#ifdef FNAME_ILLEGAL
3423 /* Illegal file name has been silently skipped. But when there
3424 * are wildcards, the real problem is that there was no match,
3425 * causing the pattern to be added, which has illegal characters.
3426 */
3427 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3428 EMSG2(_(e_nomatch2), str);
3429#endif
3430 }
3431 else if (xp->xp_numfiles == 0)
3432 {
3433 if (!(options & WILD_SILENT))
3434 EMSG2(_(e_nomatch2), str);
3435 }
3436 else
3437 {
3438 /* Escape the matches for use on the command line. */
3439 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3440
3441 /*
3442 * Check for matching suffixes in file names.
3443 */
3444 if (mode != WILD_ALL && mode != WILD_LONGEST)
3445 {
3446 if (xp->xp_numfiles)
3447 non_suf_match = xp->xp_numfiles;
3448 else
3449 non_suf_match = 1;
3450 if ((xp->xp_context == EXPAND_FILES
3451 || xp->xp_context == EXPAND_DIRECTORIES)
3452 && xp->xp_numfiles > 1)
3453 {
3454 /*
3455 * More than one match; check suffix.
3456 * The files will have been sorted on matching suffix in
3457 * expand_wildcards, only need to check the first two.
3458 */
3459 non_suf_match = 0;
3460 for (i = 0; i < 2; ++i)
3461 if (match_suffix(xp->xp_files[i]))
3462 ++non_suf_match;
3463 }
3464 if (non_suf_match != 1)
3465 {
3466 /* Can we ever get here unless it's while expanding
3467 * interactively? If not, we can get rid of this all
3468 * together. Don't really want to wait for this message
3469 * (and possibly have to hit return to continue!).
3470 */
3471 if (!(options & WILD_SILENT))
3472 EMSG(_(e_toomany));
3473 else if (!(options & WILD_NO_BEEP))
3474 beep_flush();
3475 }
3476 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3477 ss = vim_strsave(xp->xp_files[0]);
3478 }
3479 }
3480 }
3481
3482 /* Find longest common part */
3483 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3484 {
3485 for (len = 0; xp->xp_files[0][len]; ++len)
3486 {
3487 for (i = 0; i < xp->xp_numfiles; ++i)
3488 {
3489#ifdef CASE_INSENSITIVE_FILENAME
3490 if (xp->xp_context == EXPAND_DIRECTORIES
3491 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003492 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 || xp->xp_context == EXPAND_BUFFERS)
3494 {
3495 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3496 TOLOWER_LOC(xp->xp_files[0][len]))
3497 break;
3498 }
3499 else
3500#endif
3501 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3502 break;
3503 }
3504 if (i < xp->xp_numfiles)
3505 {
3506 if (!(options & WILD_NO_BEEP))
3507 vim_beep();
3508 break;
3509 }
3510 }
3511 ss = alloc((unsigned)len + 1);
3512 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003513 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 findex = -1; /* next p_wc gets first one */
3515 }
3516
3517 /* Concatenate all matching names */
3518 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3519 {
3520 len = 0;
3521 for (i = 0; i < xp->xp_numfiles; ++i)
3522 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3523 ss = lalloc(len, TRUE);
3524 if (ss != NULL)
3525 {
3526 *ss = NUL;
3527 for (i = 0; i < xp->xp_numfiles; ++i)
3528 {
3529 STRCAT(ss, xp->xp_files[i]);
3530 if (i != xp->xp_numfiles - 1)
3531 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3532 }
3533 }
3534 }
3535
3536 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3537 ExpandCleanup(xp);
3538
3539 return ss;
3540}
3541
3542/*
3543 * Prepare an expand structure for use.
3544 */
3545 void
3546ExpandInit(xp)
3547 expand_T *xp;
3548{
3549 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003550#ifndef BACKSLASH_IN_FILENAME
3551 xp->xp_shell = FALSE;
3552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 xp->xp_numfiles = -1;
3554 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003555#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3556 xp->xp_arg = NULL;
3557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558}
3559
3560/*
3561 * Cleanup an expand structure after use.
3562 */
3563 void
3564ExpandCleanup(xp)
3565 expand_T *xp;
3566{
3567 if (xp->xp_numfiles >= 0)
3568 {
3569 FreeWild(xp->xp_numfiles, xp->xp_files);
3570 xp->xp_numfiles = -1;
3571 }
3572}
3573
3574 void
3575ExpandEscape(xp, str, numfiles, files, options)
3576 expand_T *xp;
3577 char_u *str;
3578 int numfiles;
3579 char_u **files;
3580 int options;
3581{
3582 int i;
3583 char_u *p;
3584
3585 /*
3586 * May change home directory back to "~"
3587 */
3588 if (options & WILD_HOME_REPLACE)
3589 tilde_replace(str, numfiles, files);
3590
3591 if (options & WILD_ESCAPE)
3592 {
3593 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003594 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 || xp->xp_context == EXPAND_BUFFERS
3596 || xp->xp_context == EXPAND_DIRECTORIES)
3597 {
3598 /*
3599 * Insert a backslash into a file name before a space, \, %, #
3600 * and wildmatch characters, except '~'.
3601 */
3602 for (i = 0; i < numfiles; ++i)
3603 {
3604 /* for ":set path=" we need to escape spaces twice */
3605 if (xp->xp_backslash == XP_BS_THREE)
3606 {
3607 p = vim_strsave_escaped(files[i], (char_u *)" ");
3608 if (p != NULL)
3609 {
3610 vim_free(files[i]);
3611 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003612#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 p = vim_strsave_escaped(files[i], (char_u *)" ");
3614 if (p != NULL)
3615 {
3616 vim_free(files[i]);
3617 files[i] = p;
3618 }
3619#endif
3620 }
3621 }
3622#ifdef BACKSLASH_IN_FILENAME
3623 {
3624 char_u buf[20];
3625 int j = 0;
3626
3627 /* Don't escape '[' and '{' if they are in 'isfname'. */
3628 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3629 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3630 buf[j++] = *p;
3631 buf[j] = NUL;
3632 p = vim_strsave_escaped(files[i], buf);
3633 }
3634#else
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003635 p = vim_strsave_escaped(files[i],
3636 xp->xp_shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637#endif
3638 if (p != NULL)
3639 {
3640 vim_free(files[i]);
3641 files[i] = p;
3642 }
3643
3644 /* If 'str' starts with "\~", replace "~" at start of
3645 * files[i] with "\~". */
3646 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003647 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 }
3649 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003650
3651 /* If the first file starts with a '+' escape it. Otherwise it
3652 * could be seen as "+cmd". */
3653 if (*files[0] == '+')
3654 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 }
3656 else if (xp->xp_context == EXPAND_TAGS)
3657 {
3658 /*
3659 * Insert a backslash before characters in a tag name that
3660 * would terminate the ":tag" command.
3661 */
3662 for (i = 0; i < numfiles; ++i)
3663 {
3664 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3665 if (p != NULL)
3666 {
3667 vim_free(files[i]);
3668 files[i] = p;
3669 }
3670 }
3671 }
3672 }
3673}
3674
3675/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003676 * Put a backslash before the file name in "pp", which is in allocated memory.
3677 */
3678 static void
3679escape_fname(pp)
3680 char_u **pp;
3681{
3682 char_u *p;
3683
3684 p = alloc((unsigned)(STRLEN(*pp) + 2));
3685 if (p != NULL)
3686 {
3687 p[0] = '\\';
3688 STRCPY(p + 1, *pp);
3689 vim_free(*pp);
3690 *pp = p;
3691 }
3692}
3693
3694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 * For each file name in files[num_files]:
3696 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3697 */
3698 void
3699tilde_replace(orig_pat, num_files, files)
3700 char_u *orig_pat;
3701 int num_files;
3702 char_u **files;
3703{
3704 int i;
3705 char_u *p;
3706
3707 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3708 {
3709 for (i = 0; i < num_files; ++i)
3710 {
3711 p = home_replace_save(NULL, files[i]);
3712 if (p != NULL)
3713 {
3714 vim_free(files[i]);
3715 files[i] = p;
3716 }
3717 }
3718 }
3719}
3720
3721/*
3722 * Show all matches for completion on the command line.
3723 * Returns EXPAND_NOTHING when the character that triggered expansion should
3724 * be inserted like a normal character.
3725 */
3726/*ARGSUSED*/
3727 static int
3728showmatches(xp, wildmenu)
3729 expand_T *xp;
3730 int wildmenu;
3731{
3732#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3733 int num_files;
3734 char_u **files_found;
3735 int i, j, k;
3736 int maxlen;
3737 int lines;
3738 int columns;
3739 char_u *p;
3740 int lastlen;
3741 int attr;
3742 int showtail;
3743
3744 if (xp->xp_numfiles == -1)
3745 {
3746 set_expand_context(xp);
3747 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3748 &num_files, &files_found);
3749 showtail = expand_showtail(xp);
3750 if (i != EXPAND_OK)
3751 return i;
3752
3753 }
3754 else
3755 {
3756 num_files = xp->xp_numfiles;
3757 files_found = xp->xp_files;
3758 showtail = cmd_showtail;
3759 }
3760
3761#ifdef FEAT_WILDMENU
3762 if (!wildmenu)
3763 {
3764#endif
3765 msg_didany = FALSE; /* lines_left will be set */
3766 msg_start(); /* prepare for paging */
3767 msg_putchar('\n');
3768 out_flush();
3769 cmdline_row = msg_row;
3770 msg_didany = FALSE; /* lines_left will be set again */
3771 msg_start(); /* prepare for paging */
3772#ifdef FEAT_WILDMENU
3773 }
3774#endif
3775
3776 if (got_int)
3777 got_int = FALSE; /* only int. the completion, not the cmd line */
3778#ifdef FEAT_WILDMENU
3779 else if (wildmenu)
3780 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3781#endif
3782 else
3783 {
3784 /* find the length of the longest file name */
3785 maxlen = 0;
3786 for (i = 0; i < num_files; ++i)
3787 {
3788 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003789 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 || xp->xp_context == EXPAND_BUFFERS))
3791 {
3792 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3793 j = vim_strsize(NameBuff);
3794 }
3795 else
3796 j = vim_strsize(L_SHOWFILE(i));
3797 if (j > maxlen)
3798 maxlen = j;
3799 }
3800
3801 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3802 lines = num_files;
3803 else
3804 {
3805 /* compute the number of columns and lines for the listing */
3806 maxlen += 2; /* two spaces between file names */
3807 columns = ((int)Columns + 2) / maxlen;
3808 if (columns < 1)
3809 columns = 1;
3810 lines = (num_files + columns - 1) / columns;
3811 }
3812
3813 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3814
3815 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3816 {
3817 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3818 msg_clr_eos();
3819 msg_advance(maxlen - 3);
3820 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3821 }
3822
3823 /* list the files line by line */
3824 for (i = 0; i < lines; ++i)
3825 {
3826 lastlen = 999;
3827 for (k = i; k < num_files; k += lines)
3828 {
3829 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3830 {
3831 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3832 p = files_found[k] + STRLEN(files_found[k]) + 1;
3833 msg_advance(maxlen + 1);
3834 msg_puts(p);
3835 msg_advance(maxlen + 3);
3836 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3837 break;
3838 }
3839 for (j = maxlen - lastlen; --j >= 0; )
3840 msg_putchar(' ');
3841 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003842 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 || xp->xp_context == EXPAND_BUFFERS)
3844 {
3845 /* highlight directories */
3846 j = (mch_isdir(files_found[k]));
3847 if (showtail)
3848 p = L_SHOWFILE(k);
3849 else
3850 {
3851 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3852 TRUE);
3853 p = NameBuff;
3854 }
3855 }
3856 else
3857 {
3858 j = FALSE;
3859 p = L_SHOWFILE(k);
3860 }
3861 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3862 }
3863 if (msg_col > 0) /* when not wrapped around */
3864 {
3865 msg_clr_eos();
3866 msg_putchar('\n');
3867 }
3868 out_flush(); /* show one line at a time */
3869 if (got_int)
3870 {
3871 got_int = FALSE;
3872 break;
3873 }
3874 }
3875
3876 /*
3877 * we redraw the command below the lines that we have just listed
3878 * This is a bit tricky, but it saves a lot of screen updating.
3879 */
3880 cmdline_row = msg_row; /* will put it back later */
3881 }
3882
3883 if (xp->xp_numfiles == -1)
3884 FreeWild(num_files, files_found);
3885
3886 return EXPAND_OK;
3887}
3888
3889/*
3890 * Private gettail for showmatches() (and win_redr_status_matches()):
3891 * Find tail of file name path, but ignore trailing "/".
3892 */
3893 char_u *
3894sm_gettail(s)
3895 char_u *s;
3896{
3897 char_u *p;
3898 char_u *t = s;
3899 int had_sep = FALSE;
3900
3901 for (p = s; *p != NUL; )
3902 {
3903 if (vim_ispathsep(*p)
3904#ifdef BACKSLASH_IN_FILENAME
3905 && !rem_backslash(p)
3906#endif
3907 )
3908 had_sep = TRUE;
3909 else if (had_sep)
3910 {
3911 t = p;
3912 had_sep = FALSE;
3913 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003914 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916 return t;
3917}
3918
3919/*
3920 * Return TRUE if we only need to show the tail of completion matches.
3921 * When not completing file names or there is a wildcard in the path FALSE is
3922 * returned.
3923 */
3924 static int
3925expand_showtail(xp)
3926 expand_T *xp;
3927{
3928 char_u *s;
3929 char_u *end;
3930
3931 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003932 if (xp->xp_context != EXPAND_FILES
3933 && xp->xp_context != EXPAND_SHELLCMD
3934 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 return FALSE;
3936
3937 end = gettail(xp->xp_pattern);
3938 if (end == xp->xp_pattern) /* there is no path separator */
3939 return FALSE;
3940
3941 for (s = xp->xp_pattern; s < end; s++)
3942 {
3943 /* Skip escaped wildcards. Only when the backslash is not a path
3944 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3945 if (rem_backslash(s))
3946 ++s;
3947 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3948 return FALSE;
3949 }
3950 return TRUE;
3951}
3952
3953/*
3954 * Prepare a string for expansion.
3955 * When expanding file names: The string will be used with expand_wildcards().
3956 * Copy the file name into allocated memory and add a '*' at the end.
3957 * When expanding other names: The string will be used with regcomp(). Copy
3958 * the name into allocated memory and prepend "^".
3959 */
3960 char_u *
3961addstar(fname, len, context)
3962 char_u *fname;
3963 int len;
3964 int context; /* EXPAND_FILES etc. */
3965{
3966 char_u *retval;
3967 int i, j;
3968 int new_len;
3969 char_u *tail;
3970
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003971 if (context != EXPAND_FILES
3972 && context != EXPAND_SHELLCMD
3973 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 {
3975 /*
3976 * Matching will be done internally (on something other than files).
3977 * So we convert the file-matching-type wildcards into our kind for
3978 * use with vim_regcomp(). First work out how long it will be:
3979 */
3980
3981 /* For help tags the translation is done in find_help_tags().
3982 * For a tag pattern starting with "/" no translation is needed. */
3983 if (context == EXPAND_HELP
3984 || context == EXPAND_COLORS
3985 || context == EXPAND_COMPILER
3986 || (context == EXPAND_TAGS && fname[0] == '/'))
3987 retval = vim_strnsave(fname, len);
3988 else
3989 {
3990 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3991 for (i = 0; i < len; i++)
3992 {
3993 if (fname[i] == '*' || fname[i] == '~')
3994 new_len++; /* '*' needs to be replaced by ".*"
3995 '~' needs to be replaced by "\~" */
3996
3997 /* Buffer names are like file names. "." should be literal */
3998 if (context == EXPAND_BUFFERS && fname[i] == '.')
3999 new_len++; /* "." becomes "\." */
4000
4001 /* Custom expansion takes care of special things, match
4002 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004003 if ((context == EXPAND_USER_DEFINED
4004 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 new_len++; /* '\' becomes "\\" */
4006 }
4007 retval = alloc(new_len);
4008 if (retval != NULL)
4009 {
4010 retval[0] = '^';
4011 j = 1;
4012 for (i = 0; i < len; i++, j++)
4013 {
4014 /* Skip backslash. But why? At least keep it for custom
4015 * expansion. */
4016 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004017 && context != EXPAND_USER_LIST
4018 && fname[i] == '\\'
4019 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 break;
4021
4022 switch (fname[i])
4023 {
4024 case '*': retval[j++] = '.';
4025 break;
4026 case '~': retval[j++] = '\\';
4027 break;
4028 case '?': retval[j] = '.';
4029 continue;
4030 case '.': if (context == EXPAND_BUFFERS)
4031 retval[j++] = '\\';
4032 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004033 case '\\': if (context == EXPAND_USER_DEFINED
4034 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 retval[j++] = '\\';
4036 break;
4037 }
4038 retval[j] = fname[i];
4039 }
4040 retval[j] = NUL;
4041 }
4042 }
4043 }
4044 else
4045 {
4046 retval = alloc(len + 4);
4047 if (retval != NULL)
4048 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004049 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050
4051 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004052 * Don't add a star to *, ~, ~user, $var or `cmd`.
4053 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 * ~ would be at the start of the file name, but not the tail.
4055 * $ could be anywhere in the tail.
4056 * ` could be anywhere in the file name.
4057 */
4058 tail = gettail(retval);
4059 if ((*retval != '~' || tail != retval)
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004060 && (len == 0 || retval[len - 1] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 && vim_strchr(tail, '$') == NULL
4062 && vim_strchr(retval, '`') == NULL)
4063 retval[len++] = '*';
4064 retval[len] = NUL;
4065 }
4066 }
4067 return retval;
4068}
4069
4070/*
4071 * Must parse the command line so far to work out what context we are in.
4072 * Completion can then be done based on that context.
4073 * This routine sets the variables:
4074 * xp->xp_pattern The start of the pattern to be expanded within
4075 * the command line (ends at the cursor).
4076 * xp->xp_context The type of thing to expand. Will be one of:
4077 *
4078 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4079 * the command line, like an unknown command. Caller
4080 * should beep.
4081 * EXPAND_NOTHING Unrecognised context for completion, use char like
4082 * a normal char, rather than for completion. eg
4083 * :s/^I/
4084 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4085 * it.
4086 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4087 * EXPAND_FILES After command with XFILE set, or after setting
4088 * with P_EXPAND set. eg :e ^I, :w>>^I
4089 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4090 * when we know only directories are of interest. eg
4091 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004092 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4094 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4095 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4096 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4097 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4098 * EXPAND_EVENTS Complete event names
4099 * EXPAND_SYNTAX Complete :syntax command arguments
4100 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4101 * EXPAND_AUGROUP Complete autocommand group names
4102 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4103 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4104 * eg :unmap a^I , :cunab x^I
4105 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4106 * eg :call sub^I
4107 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4108 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4109 * names in expressions, eg :while s^I
4110 * EXPAND_ENV_VARS Complete environment variable names
4111 */
4112 static void
4113set_expand_context(xp)
4114 expand_T *xp;
4115{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004116 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 if (ccline.cmdfirstc != ':'
4118#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004119 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004120 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121#endif
4122 )
4123 {
4124 xp->xp_context = EXPAND_NOTHING;
4125 return;
4126 }
4127 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4128}
4129
4130 void
4131set_cmd_context(xp, str, len, col)
4132 expand_T *xp;
4133 char_u *str; /* start of command line */
4134 int len; /* length of command line (excl. NUL) */
4135 int col; /* position of cursor */
4136{
4137 int old_char = NUL;
4138 char_u *nextcomm;
4139
4140 /*
4141 * Avoid a UMR warning from Purify, only save the character if it has been
4142 * written before.
4143 */
4144 if (col < len)
4145 old_char = str[col];
4146 str[col] = NUL;
4147 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004148
4149#ifdef FEAT_EVAL
4150 if (ccline.cmdfirstc == '=')
4151 /* pass CMD_SIZE because there is no real command */
4152 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004153 else if (ccline.input_fn)
4154 {
4155 xp->xp_context = ccline.xp_context;
4156 xp->xp_pattern = ccline.cmdbuff;
4157 xp->xp_arg = ccline.xp_arg;
4158 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004159 else
4160#endif
4161 while (nextcomm != NULL)
4162 nextcomm = set_one_cmd_context(xp, nextcomm);
4163
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 str[col] = old_char;
4165}
4166
4167/*
4168 * Expand the command line "str" from context "xp".
4169 * "xp" must have been set by set_cmd_context().
4170 * xp->xp_pattern points into "str", to where the text that is to be expanded
4171 * starts.
4172 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4173 * cursor.
4174 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4175 * key that triggered expansion literally.
4176 * Returns EXPAND_OK otherwise.
4177 */
4178 int
4179expand_cmdline(xp, str, col, matchcount, matches)
4180 expand_T *xp;
4181 char_u *str; /* start of command line */
4182 int col; /* position of cursor */
4183 int *matchcount; /* return: nr of matches */
4184 char_u ***matches; /* return: array of pointers to matches */
4185{
4186 char_u *file_str = NULL;
4187
4188 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4189 {
4190 beep_flush();
4191 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4192 }
4193 if (xp->xp_context == EXPAND_NOTHING)
4194 {
4195 /* Caller can use the character as a normal char instead */
4196 return EXPAND_NOTHING;
4197 }
4198
4199 /* add star to file name, or convert to regexp if not exp. files. */
4200 file_str = addstar(xp->xp_pattern,
4201 (int)(str + col - xp->xp_pattern), xp->xp_context);
4202 if (file_str == NULL)
4203 return EXPAND_UNSUCCESSFUL;
4204
4205 /* find all files that match the description */
4206 if (ExpandFromContext(xp, file_str, matchcount, matches,
4207 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4208 {
4209 *matchcount = 0;
4210 *matches = NULL;
4211 }
4212 vim_free(file_str);
4213
4214 return EXPAND_OK;
4215}
4216
4217#ifdef FEAT_MULTI_LANG
4218/*
4219 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4220 */
4221static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4222
4223 static void
4224cleanup_help_tags(num_file, file)
4225 int num_file;
4226 char_u **file;
4227{
4228 int i, j;
4229 int len;
4230
4231 for (i = 0; i < num_file; ++i)
4232 {
4233 len = (int)STRLEN(file[i]) - 3;
4234 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4235 {
4236 /* Sorting on priority means the same item in another language may
4237 * be anywhere. Search all items for a match up to the "@en". */
4238 for (j = 0; j < num_file; ++j)
4239 if (j != i
4240 && (int)STRLEN(file[j]) == len + 3
4241 && STRNCMP(file[i], file[j], len + 1) == 0)
4242 break;
4243 if (j == num_file)
4244 file[i][len] = NUL;
4245 }
4246 }
4247}
4248#endif
4249
4250/*
4251 * Do the expansion based on xp->xp_context and "pat".
4252 */
4253 static int
4254ExpandFromContext(xp, pat, num_file, file, options)
4255 expand_T *xp;
4256 char_u *pat;
4257 int *num_file;
4258 char_u ***file;
4259 int options;
4260{
4261#ifdef FEAT_CMDL_COMPL
4262 regmatch_T regmatch;
4263#endif
4264 int ret;
4265 int flags;
4266
4267 flags = EW_DIR; /* include directories */
4268 if (options & WILD_LIST_NOTFOUND)
4269 flags |= EW_NOTFOUND;
4270 if (options & WILD_ADD_SLASH)
4271 flags |= EW_ADDSLASH;
4272 if (options & WILD_KEEP_ALL)
4273 flags |= EW_KEEPALL;
4274 if (options & WILD_SILENT)
4275 flags |= EW_SILENT;
4276
4277 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4278 {
4279 /*
4280 * Expand file or directory names.
4281 */
4282 int free_pat = FALSE;
4283 int i;
4284
4285 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4286 * space */
4287 if (xp->xp_backslash != XP_BS_NONE)
4288 {
4289 free_pat = TRUE;
4290 pat = vim_strsave(pat);
4291 for (i = 0; pat[i]; ++i)
4292 if (pat[i] == '\\')
4293 {
4294 if (xp->xp_backslash == XP_BS_THREE
4295 && pat[i + 1] == '\\'
4296 && pat[i + 2] == '\\'
4297 && pat[i + 3] == ' ')
4298 STRCPY(pat + i, pat + i + 3);
4299 if (xp->xp_backslash == XP_BS_ONE
4300 && pat[i + 1] == ' ')
4301 STRCPY(pat + i, pat + i + 1);
4302 }
4303 }
4304
4305 if (xp->xp_context == EXPAND_FILES)
4306 flags |= EW_FILE;
4307 else
4308 flags = (flags | EW_DIR) & ~EW_FILE;
4309 ret = expand_wildcards(1, &pat, num_file, file, flags);
4310 if (free_pat)
4311 vim_free(pat);
4312 return ret;
4313 }
4314
4315 *file = (char_u **)"";
4316 *num_file = 0;
4317 if (xp->xp_context == EXPAND_HELP)
4318 {
4319 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4320 {
4321#ifdef FEAT_MULTI_LANG
4322 cleanup_help_tags(*num_file, *file);
4323#endif
4324 return OK;
4325 }
4326 return FAIL;
4327 }
4328
4329#ifndef FEAT_CMDL_COMPL
4330 return FAIL;
4331#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004332 if (xp->xp_context == EXPAND_SHELLCMD)
4333 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 if (xp->xp_context == EXPAND_OLD_SETTING)
4335 return ExpandOldSetting(num_file, file);
4336 if (xp->xp_context == EXPAND_BUFFERS)
4337 return ExpandBufnames(pat, num_file, file, options);
4338 if (xp->xp_context == EXPAND_TAGS
4339 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4340 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4341 if (xp->xp_context == EXPAND_COLORS)
4342 return ExpandRTDir(pat, num_file, file, "colors");
4343 if (xp->xp_context == EXPAND_COMPILER)
4344 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004345# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4346 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004347 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349
4350 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4351 if (regmatch.regprog == NULL)
4352 return FAIL;
4353
4354 /* set ignore-case according to p_ic, p_scs and pat */
4355 regmatch.rm_ic = ignorecase(pat);
4356
4357 if (xp->xp_context == EXPAND_SETTINGS
4358 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4359 ret = ExpandSettings(xp, &regmatch, num_file, file);
4360 else if (xp->xp_context == EXPAND_MAPPINGS)
4361 ret = ExpandMappings(&regmatch, num_file, file);
4362# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4363 else if (xp->xp_context == EXPAND_USER_DEFINED)
4364 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4365# endif
4366 else
4367 {
4368 static struct expgen
4369 {
4370 int context;
4371 char_u *((*func)__ARGS((expand_T *, int)));
4372 int ic;
4373 } tab[] =
4374 {
4375 {EXPAND_COMMANDS, get_command_name, FALSE},
4376#ifdef FEAT_USR_CMDS
4377 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4378 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4379 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4380 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4381#endif
4382#ifdef FEAT_EVAL
4383 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4384 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4385 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4386 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4387#endif
4388#ifdef FEAT_MENU
4389 {EXPAND_MENUS, get_menu_name, FALSE},
4390 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4391#endif
4392#ifdef FEAT_SYN_HL
4393 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4394#endif
4395 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4396#ifdef FEAT_AUTOCMD
4397 {EXPAND_EVENTS, get_event_name, TRUE},
4398 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4399#endif
4400#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4401 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4402 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4403#endif
4404 {EXPAND_ENV_VARS, get_env_name, TRUE},
4405 };
4406 int i;
4407
4408 /*
4409 * Find a context in the table and call the ExpandGeneric() with the
4410 * right function to do the expansion.
4411 */
4412 ret = FAIL;
4413 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4414 if (xp->xp_context == tab[i].context)
4415 {
4416 if (tab[i].ic)
4417 regmatch.rm_ic = TRUE;
4418 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4419 break;
4420 }
4421 }
4422
4423 vim_free(regmatch.regprog);
4424
4425 return ret;
4426#endif /* FEAT_CMDL_COMPL */
4427}
4428
4429#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4430/*
4431 * Expand a list of names.
4432 *
4433 * Generic function for command line completion. It calls a function to
4434 * obtain strings, one by one. The strings are matched against a regexp
4435 * program. Matching strings are copied into an array, which is returned.
4436 *
4437 * Returns OK when no problems encountered, FAIL for error (out of memory).
4438 */
4439 int
4440ExpandGeneric(xp, regmatch, num_file, file, func)
4441 expand_T *xp;
4442 regmatch_T *regmatch;
4443 int *num_file;
4444 char_u ***file;
4445 char_u *((*func)__ARGS((expand_T *, int)));
4446 /* returns a string from the list */
4447{
4448 int i;
4449 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004450 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 char_u *str;
4452
4453 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004454 * round == 0: count the number of matching names
4455 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004457 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 {
4459 for (i = 0; ; ++i)
4460 {
4461 str = (*func)(xp, i);
4462 if (str == NULL) /* end of list */
4463 break;
4464 if (*str == NUL) /* skip empty strings */
4465 continue;
4466
4467 if (vim_regexec(regmatch, str, (colnr_T)0))
4468 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004469 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 {
4471 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4472 (*file)[count] = str;
4473#ifdef FEAT_MENU
4474 if (func == get_menu_names && str != NULL)
4475 {
4476 /* test for separator added by get_menu_names() */
4477 str += STRLEN(str) - 1;
4478 if (*str == '\001')
4479 *str = '.';
4480 }
4481#endif
4482 }
4483 ++count;
4484 }
4485 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004486 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
4488 if (count == 0)
4489 return OK;
4490 *num_file = count;
4491 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4492 if (*file == NULL)
4493 {
4494 *file = (char_u **)"";
4495 return FAIL;
4496 }
4497 count = 0;
4498 }
4499 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004500
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004501 /* Sort the results. Keep menu's in the specified order. */
4502 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4503 sort_strings(*file, *num_file);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004504
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 return OK;
4506}
4507
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004508/*
4509 * Complete a shell command.
4510 * Returns FAIL or OK;
4511 */
4512 static int
4513expand_shellcmd(filepat, num_file, file, flagsarg)
4514 char_u *filepat; /* pattern to match with command names */
4515 int *num_file; /* return: number of matches */
4516 char_u ***file; /* return: array with matches */
4517 int flagsarg; /* EW_ flags */
4518{
4519 char_u *pat;
4520 int i;
4521 char_u *path;
4522 int mustfree = FALSE;
4523 garray_T ga;
4524 char_u *buf = alloc(MAXPATHL);
4525 size_t l;
4526 char_u *s, *e;
4527 int flags = flagsarg;
4528 int ret;
4529
4530 if (buf == NULL)
4531 return FAIL;
4532
4533 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4534 * space */
4535 pat = vim_strsave(filepat);
4536 for (i = 0; pat[i]; ++i)
4537 if (pat[i] == '\\' && pat[i + 1] == ' ')
4538 STRCPY(pat + i, pat + i + 1);
4539
4540 flags |= EW_FILE | EW_EXEC;
4541
4542 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004543 if (mch_isFullName(pat))
4544 path = (char_u *)" ";
4545 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004546 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4547 path = (char_u *)".";
4548 else
4549 path = vim_getenv((char_u *)"PATH", &mustfree);
4550
4551 /*
4552 * Go over all directories in $PATH. Expand matches in that directory and
4553 * collect them in "ga".
4554 */
4555 ga_init2(&ga, (int)sizeof(char *), 10);
4556 for (s = path; *s != NUL; s = e)
4557 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004558 if (*s == ' ')
4559 ++s; /* Skip space used for absolute path name. */
4560
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004561#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4562 e = vim_strchr(s, ';');
4563#else
4564 e = vim_strchr(s, ':');
4565#endif
4566 if (e == NULL)
4567 e = s + STRLEN(s);
4568
4569 l = e - s;
4570 if (l > MAXPATHL - 5)
4571 break;
4572 vim_strncpy(buf, s, l);
4573 add_pathsep(buf);
4574 l = STRLEN(buf);
4575 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4576
4577 /* Expand matches in one directory of $PATH. */
4578 ret = expand_wildcards(1, &buf, num_file, file, flags);
4579 if (ret == OK)
4580 {
4581 if (ga_grow(&ga, *num_file) == FAIL)
4582 FreeWild(*num_file, *file);
4583 else
4584 {
4585 for (i = 0; i < *num_file; ++i)
4586 {
4587 s = (*file)[i];
4588 if (STRLEN(s) > l)
4589 {
4590 /* Remove the path again. */
4591 mch_memmove(s, s + l, STRLEN(s + l) + 1);
4592 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4593 }
4594 else
4595 vim_free(s);
4596 }
4597 vim_free(*file);
4598 }
4599 }
4600 if (*e != NUL)
4601 ++e;
4602 }
4603 *file = ga.ga_data;
4604 *num_file = ga.ga_len;
4605
4606 vim_free(buf);
4607 vim_free(pat);
4608 if (mustfree)
4609 vim_free(path);
4610 return OK;
4611}
4612
4613
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004615static 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));
4616
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004618 * call "user_expand_func()" to invoke a user defined VimL function and return
4619 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004621 static void *
4622call_user_expand_func(user_expand_func, xp, num_file, file)
4623 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 int *num_file;
4626 char_u ***file;
4627{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004628 char_u keep;
4629 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004632 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004633 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634
4635 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004636 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 *num_file = 0;
4638 *file = NULL;
4639
4640 keep = ccline.cmdbuff[ccline.cmdlen];
4641 ccline.cmdbuff[ccline.cmdlen] = 0;
4642 sprintf((char *)num, "%d", ccline.cmdpos);
4643 args[0] = xp->xp_pattern;
4644 args[1] = ccline.cmdbuff;
4645 args[2] = num;
4646
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004647 /* Save the cmdline, we don't know what the function may do. */
4648 save_ccline = ccline;
4649 ccline.cmdbuff = NULL;
4650 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004652
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004653 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004654
4655 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004657
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004659
4660 return ret;
4661}
4662
4663/*
4664 * Expand names with a function defined by the user.
4665 */
4666 static int
4667ExpandUserDefined(xp, regmatch, num_file, file)
4668 expand_T *xp;
4669 regmatch_T *regmatch;
4670 int *num_file;
4671 char_u ***file;
4672{
4673 char_u *retstr;
4674 char_u *s;
4675 char_u *e;
4676 char_u keep;
4677 garray_T ga;
4678
4679 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4680 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 return FAIL;
4682
4683 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004684 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 {
4686 e = vim_strchr(s, '\n');
4687 if (e == NULL)
4688 e = s + STRLEN(s);
4689 keep = *e;
4690 *e = 0;
4691
4692 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4693 {
4694 *e = keep;
4695 if (*e != NUL)
4696 ++e;
4697 continue;
4698 }
4699
4700 if (ga_grow(&ga, 1) == FAIL)
4701 break;
4702
4703 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4704 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705
4706 *e = keep;
4707 if (*e != NUL)
4708 ++e;
4709 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004710 vim_free(retstr);
4711 *file = ga.ga_data;
4712 *num_file = ga.ga_len;
4713 return OK;
4714}
4715
4716/*
4717 * Expand names with a list returned by a function defined by the user.
4718 */
4719 static int
4720ExpandUserList(xp, num_file, file)
4721 expand_T *xp;
4722 int *num_file;
4723 char_u ***file;
4724{
4725 list_T *retlist;
4726 listitem_T *li;
4727 garray_T ga;
4728
4729 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4730 if (retlist == NULL)
4731 return FAIL;
4732
4733 ga_init2(&ga, (int)sizeof(char *), 3);
4734 /* Loop over the items in the list. */
4735 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4736 {
4737 if (li->li_tv.v_type != VAR_STRING)
4738 continue; /* Skip non-string items */
4739
4740 if (ga_grow(&ga, 1) == FAIL)
4741 break;
4742
4743 ((char_u **)ga.ga_data)[ga.ga_len] =
4744 vim_strsave(li->li_tv.vval.v_string);
4745 ++ga.ga_len;
4746 }
4747 list_unref(retlist);
4748
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 *file = ga.ga_data;
4750 *num_file = ga.ga_len;
4751 return OK;
4752}
4753#endif
4754
4755/*
4756 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4757 * or compiler names.
4758 */
4759 static int
4760ExpandRTDir(pat, num_file, file, dirname)
4761 char_u *pat;
4762 int *num_file;
4763 char_u ***file;
4764 char *dirname; /* "colors" or "compiler" */
4765{
4766 char_u *all;
4767 char_u *s;
4768 char_u *e;
4769 garray_T ga;
4770
4771 *num_file = 0;
4772 *file = NULL;
4773 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4774 if (s == NULL)
4775 return FAIL;
4776 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4777 all = globpath(p_rtp, s);
4778 vim_free(s);
4779 if (all == NULL)
4780 return FAIL;
4781
4782 ga_init2(&ga, (int)sizeof(char *), 3);
4783 for (s = all; *s != NUL; s = e)
4784 {
4785 e = vim_strchr(s, '\n');
4786 if (e == NULL)
4787 e = s + STRLEN(s);
4788 if (ga_grow(&ga, 1) == FAIL)
4789 break;
4790 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4791 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004792 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 if (*s == '\n' || vim_ispathsep(*s))
4794 break;
4795 ++s;
4796 ((char_u **)ga.ga_data)[ga.ga_len] =
4797 vim_strnsave(s, (int)(e - s - 4));
4798 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 }
4800 if (*e != NUL)
4801 ++e;
4802 }
4803 vim_free(all);
4804 *file = ga.ga_data;
4805 *num_file = ga.ga_len;
4806 return OK;
4807}
4808
4809#endif
4810
4811#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4812/*
4813 * Expand "file" for all comma-separated directories in "path".
4814 * Returns an allocated string with all matches concatenated, separated by
4815 * newlines. Returns NULL for an error or no matches.
4816 */
4817 char_u *
4818globpath(path, file)
4819 char_u *path;
4820 char_u *file;
4821{
4822 expand_T xpc;
4823 char_u *buf;
4824 garray_T ga;
4825 int i;
4826 int len;
4827 int num_p;
4828 char_u **p;
4829 char_u *cur = NULL;
4830
4831 buf = alloc(MAXPATHL);
4832 if (buf == NULL)
4833 return NULL;
4834
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004835 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004837
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 ga_init2(&ga, 1, 100);
4839
4840 /* Loop over all entries in {path}. */
4841 while (*path != NUL)
4842 {
4843 /* Copy one item of the path to buf[] and concatenate the file name. */
4844 copy_option_part(&path, buf, MAXPATHL, ",");
4845 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4846 {
4847 add_pathsep(buf);
4848 STRCAT(buf, file);
4849 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4850 && num_p > 0)
4851 {
4852 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4853 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004854 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
4856 /* Concatenate new results to previous ones. */
4857 if (ga_grow(&ga, len) == OK)
4858 {
4859 cur = (char_u *)ga.ga_data + ga.ga_len;
4860 for (i = 0; i < num_p; ++i)
4861 {
4862 STRCPY(cur, p[i]);
4863 cur += STRLEN(p[i]);
4864 *cur++ = '\n';
4865 }
4866 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868 FreeWild(num_p, p);
4869 }
4870 }
4871 }
4872 if (cur != NULL)
4873 *--cur = 0; /* Replace trailing newline with NUL */
4874
4875 vim_free(buf);
4876 return (char_u *)ga.ga_data;
4877}
4878
4879#endif
4880
4881#if defined(FEAT_CMDHIST) || defined(PROTO)
4882
4883/*********************************
4884 * Command line history stuff *
4885 *********************************/
4886
4887/*
4888 * Translate a history character to the associated type number.
4889 */
4890 static int
4891hist_char2type(c)
4892 int c;
4893{
4894 if (c == ':')
4895 return HIST_CMD;
4896 if (c == '=')
4897 return HIST_EXPR;
4898 if (c == '@')
4899 return HIST_INPUT;
4900 if (c == '>')
4901 return HIST_DEBUG;
4902 return HIST_SEARCH; /* must be '?' or '/' */
4903}
4904
4905/*
4906 * Table of history names.
4907 * These names are used in :history and various hist...() functions.
4908 * It is sufficient to give the significant prefix of a history name.
4909 */
4910
4911static char *(history_names[]) =
4912{
4913 "cmd",
4914 "search",
4915 "expr",
4916 "input",
4917 "debug",
4918 NULL
4919};
4920
4921/*
4922 * init_history() - Initialize the command line history.
4923 * Also used to re-allocate the history when the size changes.
4924 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004925 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926init_history()
4927{
4928 int newlen; /* new length of history table */
4929 histentry_T *temp;
4930 int i;
4931 int j;
4932 int type;
4933
4934 /*
4935 * If size of history table changed, reallocate it
4936 */
4937 newlen = (int)p_hi;
4938 if (newlen != hislen) /* history length changed */
4939 {
4940 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4941 {
4942 if (newlen)
4943 {
4944 temp = (histentry_T *)lalloc(
4945 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4946 if (temp == NULL) /* out of memory! */
4947 {
4948 if (type == 0) /* first one: just keep the old length */
4949 {
4950 newlen = hislen;
4951 break;
4952 }
4953 /* Already changed one table, now we can only have zero
4954 * length for all tables. */
4955 newlen = 0;
4956 type = -1;
4957 continue;
4958 }
4959 }
4960 else
4961 temp = NULL;
4962 if (newlen == 0 || temp != NULL)
4963 {
4964 if (hisidx[type] < 0) /* there are no entries yet */
4965 {
4966 for (i = 0; i < newlen; ++i)
4967 {
4968 temp[i].hisnum = 0;
4969 temp[i].hisstr = NULL;
4970 }
4971 }
4972 else if (newlen > hislen) /* array becomes bigger */
4973 {
4974 for (i = 0; i <= hisidx[type]; ++i)
4975 temp[i] = history[type][i];
4976 j = i;
4977 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4978 {
4979 temp[i].hisnum = 0;
4980 temp[i].hisstr = NULL;
4981 }
4982 for ( ; j < hislen; ++i, ++j)
4983 temp[i] = history[type][j];
4984 }
4985 else /* array becomes smaller or 0 */
4986 {
4987 j = hisidx[type];
4988 for (i = newlen - 1; ; --i)
4989 {
4990 if (i >= 0) /* copy newest entries */
4991 temp[i] = history[type][j];
4992 else /* remove older entries */
4993 vim_free(history[type][j].hisstr);
4994 if (--j < 0)
4995 j = hislen - 1;
4996 if (j == hisidx[type])
4997 break;
4998 }
4999 hisidx[type] = newlen - 1;
5000 }
5001 vim_free(history[type]);
5002 history[type] = temp;
5003 }
5004 }
5005 hislen = newlen;
5006 }
5007}
5008
5009/*
5010 * Check if command line 'str' is already in history.
5011 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5012 */
5013 static int
5014in_history(type, str, move_to_front)
5015 int type;
5016 char_u *str;
5017 int move_to_front; /* Move the entry to the front if it exists */
5018{
5019 int i;
5020 int last_i = -1;
5021
5022 if (hisidx[type] < 0)
5023 return FALSE;
5024 i = hisidx[type];
5025 do
5026 {
5027 if (history[type][i].hisstr == NULL)
5028 return FALSE;
5029 if (STRCMP(str, history[type][i].hisstr) == 0)
5030 {
5031 if (!move_to_front)
5032 return TRUE;
5033 last_i = i;
5034 break;
5035 }
5036 if (--i < 0)
5037 i = hislen - 1;
5038 } while (i != hisidx[type]);
5039
5040 if (last_i >= 0)
5041 {
5042 str = history[type][i].hisstr;
5043 while (i != hisidx[type])
5044 {
5045 if (++i >= hislen)
5046 i = 0;
5047 history[type][last_i] = history[type][i];
5048 last_i = i;
5049 }
5050 history[type][i].hisstr = str;
5051 history[type][i].hisnum = ++hisnum[type];
5052 return TRUE;
5053 }
5054 return FALSE;
5055}
5056
5057/*
5058 * Convert history name (from table above) to its HIST_ equivalent.
5059 * When "name" is empty, return "cmd" history.
5060 * Returns -1 for unknown history name.
5061 */
5062 int
5063get_histtype(name)
5064 char_u *name;
5065{
5066 int i;
5067 int len = (int)STRLEN(name);
5068
5069 /* No argument: use current history. */
5070 if (len == 0)
5071 return hist_char2type(ccline.cmdfirstc);
5072
5073 for (i = 0; history_names[i] != NULL; ++i)
5074 if (STRNICMP(name, history_names[i], len) == 0)
5075 return i;
5076
5077 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5078 return hist_char2type(name[0]);
5079
5080 return -1;
5081}
5082
5083static int last_maptick = -1; /* last seen maptick */
5084
5085/*
5086 * Add the given string to the given history. If the string is already in the
5087 * history then it is moved to the front. "histype" may be one of he HIST_
5088 * values.
5089 */
5090 void
5091add_to_history(histype, new_entry, in_map, sep)
5092 int histype;
5093 char_u *new_entry;
5094 int in_map; /* consider maptick when inside a mapping */
5095 int sep; /* separator character used (search hist) */
5096{
5097 histentry_T *hisptr;
5098 int len;
5099
5100 if (hislen == 0) /* no history */
5101 return;
5102
5103 /*
5104 * Searches inside the same mapping overwrite each other, so that only
5105 * the last line is kept. Be careful not to remove a line that was moved
5106 * down, only lines that were added.
5107 */
5108 if (histype == HIST_SEARCH && in_map)
5109 {
5110 if (maptick == last_maptick)
5111 {
5112 /* Current line is from the same mapping, remove it */
5113 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5114 vim_free(hisptr->hisstr);
5115 hisptr->hisstr = NULL;
5116 hisptr->hisnum = 0;
5117 --hisnum[histype];
5118 if (--hisidx[HIST_SEARCH] < 0)
5119 hisidx[HIST_SEARCH] = hislen - 1;
5120 }
5121 last_maptick = -1;
5122 }
5123 if (!in_history(histype, new_entry, TRUE))
5124 {
5125 if (++hisidx[histype] == hislen)
5126 hisidx[histype] = 0;
5127 hisptr = &history[histype][hisidx[histype]];
5128 vim_free(hisptr->hisstr);
5129
5130 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005131 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5133 if (hisptr->hisstr != NULL)
5134 hisptr->hisstr[len + 1] = sep;
5135
5136 hisptr->hisnum = ++hisnum[histype];
5137 if (histype == HIST_SEARCH && in_map)
5138 last_maptick = maptick;
5139 }
5140}
5141
5142#if defined(FEAT_EVAL) || defined(PROTO)
5143
5144/*
5145 * Get identifier of newest history entry.
5146 * "histype" may be one of the HIST_ values.
5147 */
5148 int
5149get_history_idx(histype)
5150 int histype;
5151{
5152 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5153 || hisidx[histype] < 0)
5154 return -1;
5155
5156 return history[histype][hisidx[histype]].hisnum;
5157}
5158
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005159static struct cmdline_info *get_ccline_ptr __ARGS((void));
5160
5161/*
5162 * Get pointer to the command line info to use. cmdline_paste() may clear
5163 * ccline and put the previous value in prev_ccline.
5164 */
5165 static struct cmdline_info *
5166get_ccline_ptr()
5167{
5168 if ((State & CMDLINE) == 0)
5169 return NULL;
5170 if (ccline.cmdbuff != NULL)
5171 return &ccline;
5172 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5173 return &prev_ccline;
5174 return NULL;
5175}
5176
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177/*
5178 * Get the current command line in allocated memory.
5179 * Only works when the command line is being edited.
5180 * Returns NULL when something is wrong.
5181 */
5182 char_u *
5183get_cmdline_str()
5184{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005185 struct cmdline_info *p = get_ccline_ptr();
5186
5187 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005189 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190}
5191
5192/*
5193 * Get the current command line position, counted in bytes.
5194 * Zero is the first position.
5195 * Only works when the command line is being edited.
5196 * Returns -1 when something is wrong.
5197 */
5198 int
5199get_cmdline_pos()
5200{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005201 struct cmdline_info *p = get_ccline_ptr();
5202
5203 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005205 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206}
5207
5208/*
5209 * Set the command line byte position to "pos". Zero is the first position.
5210 * Only works when the command line is being edited.
5211 * Returns 1 when failed, 0 when OK.
5212 */
5213 int
5214set_cmdline_pos(pos)
5215 int pos;
5216{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005217 struct cmdline_info *p = get_ccline_ptr();
5218
5219 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 return 1;
5221
5222 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5223 * changed the command line. */
5224 if (pos < 0)
5225 new_cmdpos = 0;
5226 else
5227 new_cmdpos = pos;
5228 return 0;
5229}
5230
5231/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005232 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005233 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005234 * Only works when the command line is being edited.
5235 * Returns NUL when something is wrong.
5236 */
5237 int
5238get_cmdline_type()
5239{
5240 struct cmdline_info *p = get_ccline_ptr();
5241
5242 if (p == NULL)
5243 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005244 if (p->cmdfirstc == NUL)
5245 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005246 return p->cmdfirstc;
5247}
5248
5249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 * Calculate history index from a number:
5251 * num > 0: seen as identifying number of a history entry
5252 * num < 0: relative position in history wrt newest entry
5253 * "histype" may be one of the HIST_ values.
5254 */
5255 static int
5256calc_hist_idx(histype, num)
5257 int histype;
5258 int num;
5259{
5260 int i;
5261 histentry_T *hist;
5262 int wrapped = FALSE;
5263
5264 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5265 || (i = hisidx[histype]) < 0 || num == 0)
5266 return -1;
5267
5268 hist = history[histype];
5269 if (num > 0)
5270 {
5271 while (hist[i].hisnum > num)
5272 if (--i < 0)
5273 {
5274 if (wrapped)
5275 break;
5276 i += hislen;
5277 wrapped = TRUE;
5278 }
5279 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5280 return i;
5281 }
5282 else if (-num <= hislen)
5283 {
5284 i += num + 1;
5285 if (i < 0)
5286 i += hislen;
5287 if (hist[i].hisstr != NULL)
5288 return i;
5289 }
5290 return -1;
5291}
5292
5293/*
5294 * Get a history entry by its index.
5295 * "histype" may be one of the HIST_ values.
5296 */
5297 char_u *
5298get_history_entry(histype, idx)
5299 int histype;
5300 int idx;
5301{
5302 idx = calc_hist_idx(histype, idx);
5303 if (idx >= 0)
5304 return history[histype][idx].hisstr;
5305 else
5306 return (char_u *)"";
5307}
5308
5309/*
5310 * Clear all entries of a history.
5311 * "histype" may be one of the HIST_ values.
5312 */
5313 int
5314clr_history(histype)
5315 int histype;
5316{
5317 int i;
5318 histentry_T *hisptr;
5319
5320 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5321 {
5322 hisptr = history[histype];
5323 for (i = hislen; i--;)
5324 {
5325 vim_free(hisptr->hisstr);
5326 hisptr->hisnum = 0;
5327 hisptr++->hisstr = NULL;
5328 }
5329 hisidx[histype] = -1; /* mark history as cleared */
5330 hisnum[histype] = 0; /* reset identifier counter */
5331 return OK;
5332 }
5333 return FAIL;
5334}
5335
5336/*
5337 * Remove all entries matching {str} from a history.
5338 * "histype" may be one of the HIST_ values.
5339 */
5340 int
5341del_history_entry(histype, str)
5342 int histype;
5343 char_u *str;
5344{
5345 regmatch_T regmatch;
5346 histentry_T *hisptr;
5347 int idx;
5348 int i;
5349 int last;
5350 int found = FALSE;
5351
5352 regmatch.regprog = NULL;
5353 regmatch.rm_ic = FALSE; /* always match case */
5354 if (hislen != 0
5355 && histype >= 0
5356 && histype < HIST_COUNT
5357 && *str != NUL
5358 && (idx = hisidx[histype]) >= 0
5359 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5360 != NULL)
5361 {
5362 i = last = idx;
5363 do
5364 {
5365 hisptr = &history[histype][i];
5366 if (hisptr->hisstr == NULL)
5367 break;
5368 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5369 {
5370 found = TRUE;
5371 vim_free(hisptr->hisstr);
5372 hisptr->hisstr = NULL;
5373 hisptr->hisnum = 0;
5374 }
5375 else
5376 {
5377 if (i != last)
5378 {
5379 history[histype][last] = *hisptr;
5380 hisptr->hisstr = NULL;
5381 hisptr->hisnum = 0;
5382 }
5383 if (--last < 0)
5384 last += hislen;
5385 }
5386 if (--i < 0)
5387 i += hislen;
5388 } while (i != idx);
5389 if (history[histype][idx].hisstr == NULL)
5390 hisidx[histype] = -1;
5391 }
5392 vim_free(regmatch.regprog);
5393 return found;
5394}
5395
5396/*
5397 * Remove an indexed entry from a history.
5398 * "histype" may be one of the HIST_ values.
5399 */
5400 int
5401del_history_idx(histype, idx)
5402 int histype;
5403 int idx;
5404{
5405 int i, j;
5406
5407 i = calc_hist_idx(histype, idx);
5408 if (i < 0)
5409 return FALSE;
5410 idx = hisidx[histype];
5411 vim_free(history[histype][i].hisstr);
5412
5413 /* When deleting the last added search string in a mapping, reset
5414 * last_maptick, so that the last added search string isn't deleted again.
5415 */
5416 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5417 last_maptick = -1;
5418
5419 while (i != idx)
5420 {
5421 j = (i + 1) % hislen;
5422 history[histype][i] = history[histype][j];
5423 i = j;
5424 }
5425 history[histype][i].hisstr = NULL;
5426 history[histype][i].hisnum = 0;
5427 if (--i < 0)
5428 i += hislen;
5429 hisidx[histype] = i;
5430 return TRUE;
5431}
5432
5433#endif /* FEAT_EVAL */
5434
5435#if defined(FEAT_CRYPT) || defined(PROTO)
5436/*
5437 * Very specific function to remove the value in ":set key=val" from the
5438 * history.
5439 */
5440 void
5441remove_key_from_history()
5442{
5443 char_u *p;
5444 int i;
5445
5446 i = hisidx[HIST_CMD];
5447 if (i < 0)
5448 return;
5449 p = history[HIST_CMD][i].hisstr;
5450 if (p != NULL)
5451 for ( ; *p; ++p)
5452 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5453 {
5454 p = vim_strchr(p + 3, '=');
5455 if (p == NULL)
5456 break;
5457 ++p;
5458 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5459 if (p[i] == '\\' && p[i + 1])
5460 ++i;
5461 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5462 --p;
5463 }
5464}
5465#endif
5466
5467#endif /* FEAT_CMDHIST */
5468
5469#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5470/*
5471 * Get indices "num1,num2" that specify a range within a list (not a range of
5472 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5473 * Returns OK if parsed successfully, otherwise FAIL.
5474 */
5475 int
5476get_list_range(str, num1, num2)
5477 char_u **str;
5478 int *num1;
5479 int *num2;
5480{
5481 int len;
5482 int first = FALSE;
5483 long num;
5484
5485 *str = skipwhite(*str);
5486 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5487 {
5488 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5489 *str += len;
5490 *num1 = (int)num;
5491 first = TRUE;
5492 }
5493 *str = skipwhite(*str);
5494 if (**str == ',') /* parse "to" part of range */
5495 {
5496 *str = skipwhite(*str + 1);
5497 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5498 if (len > 0)
5499 {
5500 *num2 = (int)num;
5501 *str = skipwhite(*str + len);
5502 }
5503 else if (!first) /* no number given at all */
5504 return FAIL;
5505 }
5506 else if (first) /* only one number given */
5507 *num2 = *num1;
5508 return OK;
5509}
5510#endif
5511
5512#if defined(FEAT_CMDHIST) || defined(PROTO)
5513/*
5514 * :history command - print a history
5515 */
5516 void
5517ex_history(eap)
5518 exarg_T *eap;
5519{
5520 histentry_T *hist;
5521 int histype1 = HIST_CMD;
5522 int histype2 = HIST_CMD;
5523 int hisidx1 = 1;
5524 int hisidx2 = -1;
5525 int idx;
5526 int i, j, k;
5527 char_u *end;
5528 char_u *arg = eap->arg;
5529
5530 if (hislen == 0)
5531 {
5532 MSG(_("'history' option is zero"));
5533 return;
5534 }
5535
5536 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5537 {
5538 end = arg;
5539 while (ASCII_ISALPHA(*end)
5540 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5541 end++;
5542 i = *end;
5543 *end = NUL;
5544 histype1 = get_histtype(arg);
5545 if (histype1 == -1)
5546 {
5547 if (STRICMP(arg, "all") == 0)
5548 {
5549 histype1 = 0;
5550 histype2 = HIST_COUNT-1;
5551 }
5552 else
5553 {
5554 *end = i;
5555 EMSG(_(e_trailing));
5556 return;
5557 }
5558 }
5559 else
5560 histype2 = histype1;
5561 *end = i;
5562 }
5563 else
5564 end = arg;
5565 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5566 {
5567 EMSG(_(e_trailing));
5568 return;
5569 }
5570
5571 for (; !got_int && histype1 <= histype2; ++histype1)
5572 {
5573 STRCPY(IObuff, "\n # ");
5574 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5575 MSG_PUTS_TITLE(IObuff);
5576 idx = hisidx[histype1];
5577 hist = history[histype1];
5578 j = hisidx1;
5579 k = hisidx2;
5580 if (j < 0)
5581 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5582 if (k < 0)
5583 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5584 if (idx >= 0 && j <= k)
5585 for (i = idx + 1; !got_int; ++i)
5586 {
5587 if (i == hislen)
5588 i = 0;
5589 if (hist[i].hisstr != NULL
5590 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5591 {
5592 msg_putchar('\n');
5593 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5594 hist[i].hisnum);
5595 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5596 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5597 (int)Columns - 10);
5598 else
5599 STRCAT(IObuff, hist[i].hisstr);
5600 msg_outtrans(IObuff);
5601 out_flush();
5602 }
5603 if (i == idx)
5604 break;
5605 }
5606 }
5607}
5608#endif
5609
5610#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5611static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5612static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5613static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5614static int viminfo_add_at_front = FALSE;
5615
5616static int hist_type2char __ARGS((int type, int use_question));
5617
5618/*
5619 * Translate a history type number to the associated character.
5620 */
5621 static int
5622hist_type2char(type, use_question)
5623 int type;
5624 int use_question; /* use '?' instead of '/' */
5625{
5626 if (type == HIST_CMD)
5627 return ':';
5628 if (type == HIST_SEARCH)
5629 {
5630 if (use_question)
5631 return '?';
5632 else
5633 return '/';
5634 }
5635 if (type == HIST_EXPR)
5636 return '=';
5637 return '@';
5638}
5639
5640/*
5641 * Prepare for reading the history from the viminfo file.
5642 * This allocates history arrays to store the read history lines.
5643 */
5644 void
5645prepare_viminfo_history(asklen)
5646 int asklen;
5647{
5648 int i;
5649 int num;
5650 int type;
5651 int len;
5652
5653 init_history();
5654 viminfo_add_at_front = (asklen != 0);
5655 if (asklen > hislen)
5656 asklen = hislen;
5657
5658 for (type = 0; type < HIST_COUNT; ++type)
5659 {
5660 /*
5661 * Count the number of empty spaces in the history list. If there are
5662 * more spaces available than we request, then fill them up.
5663 */
5664 for (i = 0, num = 0; i < hislen; i++)
5665 if (history[type][i].hisstr == NULL)
5666 num++;
5667 len = asklen;
5668 if (num > len)
5669 len = num;
5670 if (len <= 0)
5671 viminfo_history[type] = NULL;
5672 else
5673 viminfo_history[type] =
5674 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5675 if (viminfo_history[type] == NULL)
5676 len = 0;
5677 viminfo_hislen[type] = len;
5678 viminfo_hisidx[type] = 0;
5679 }
5680}
5681
5682/*
5683 * Accept a line from the viminfo, store it in the history array when it's
5684 * new.
5685 */
5686 int
5687read_viminfo_history(virp)
5688 vir_T *virp;
5689{
5690 int type;
5691 long_u len;
5692 char_u *val;
5693 char_u *p;
5694
5695 type = hist_char2type(virp->vir_line[0]);
5696 if (viminfo_hisidx[type] < viminfo_hislen[type])
5697 {
5698 val = viminfo_readstring(virp, 1, TRUE);
5699 if (val != NULL && *val != NUL)
5700 {
5701 if (!in_history(type, val + (type == HIST_SEARCH),
5702 viminfo_add_at_front))
5703 {
5704 /* Need to re-allocate to append the separator byte. */
5705 len = STRLEN(val);
5706 p = lalloc(len + 2, TRUE);
5707 if (p != NULL)
5708 {
5709 if (type == HIST_SEARCH)
5710 {
5711 /* Search entry: Move the separator from the first
5712 * column to after the NUL. */
5713 mch_memmove(p, val + 1, (size_t)len);
5714 p[len] = (*val == ' ' ? NUL : *val);
5715 }
5716 else
5717 {
5718 /* Not a search entry: No separator in the viminfo
5719 * file, add a NUL separator. */
5720 mch_memmove(p, val, (size_t)len + 1);
5721 p[len + 1] = NUL;
5722 }
5723 viminfo_history[type][viminfo_hisidx[type]++] = p;
5724 }
5725 }
5726 }
5727 vim_free(val);
5728 }
5729 return viminfo_readline(virp);
5730}
5731
5732 void
5733finish_viminfo_history()
5734{
5735 int idx;
5736 int i;
5737 int type;
5738
5739 for (type = 0; type < HIST_COUNT; ++type)
5740 {
5741 if (history[type] == NULL)
5742 return;
5743 idx = hisidx[type] + viminfo_hisidx[type];
5744 if (idx >= hislen)
5745 idx -= hislen;
5746 else if (idx < 0)
5747 idx = hislen - 1;
5748 if (viminfo_add_at_front)
5749 hisidx[type] = idx;
5750 else
5751 {
5752 if (hisidx[type] == -1)
5753 hisidx[type] = hislen - 1;
5754 do
5755 {
5756 if (history[type][idx].hisstr != NULL)
5757 break;
5758 if (++idx == hislen)
5759 idx = 0;
5760 } while (idx != hisidx[type]);
5761 if (idx != hisidx[type] && --idx < 0)
5762 idx = hislen - 1;
5763 }
5764 for (i = 0; i < viminfo_hisidx[type]; i++)
5765 {
5766 vim_free(history[type][idx].hisstr);
5767 history[type][idx].hisstr = viminfo_history[type][i];
5768 if (--idx < 0)
5769 idx = hislen - 1;
5770 }
5771 idx += 1;
5772 idx %= hislen;
5773 for (i = 0; i < viminfo_hisidx[type]; i++)
5774 {
5775 history[type][idx++].hisnum = ++hisnum[type];
5776 idx %= hislen;
5777 }
5778 vim_free(viminfo_history[type]);
5779 viminfo_history[type] = NULL;
5780 }
5781}
5782
5783 void
5784write_viminfo_history(fp)
5785 FILE *fp;
5786{
5787 int i;
5788 int type;
5789 int num_saved;
5790 char_u *p;
5791 int c;
5792
5793 init_history();
5794 if (hislen == 0)
5795 return;
5796 for (type = 0; type < HIST_COUNT; ++type)
5797 {
5798 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5799 if (num_saved == 0)
5800 continue;
5801 if (num_saved < 0) /* Use default */
5802 num_saved = hislen;
5803 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5804 type == HIST_CMD ? _("Command Line") :
5805 type == HIST_SEARCH ? _("Search String") :
5806 type == HIST_EXPR ? _("Expression") :
5807 _("Input Line"));
5808 if (num_saved > hislen)
5809 num_saved = hislen;
5810 i = hisidx[type];
5811 if (i >= 0)
5812 while (num_saved--)
5813 {
5814 p = history[type][i].hisstr;
5815 if (p != NULL)
5816 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005817 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 /* For the search history: put the separator in the second
5819 * column; use a space if there isn't one. */
5820 if (type == HIST_SEARCH)
5821 {
5822 c = p[STRLEN(p) + 1];
5823 putc(c == NUL ? ' ' : c, fp);
5824 }
5825 viminfo_writestring(fp, p);
5826 }
5827 if (--i < 0)
5828 i = hislen - 1;
5829 }
5830 }
5831}
5832#endif /* FEAT_VIMINFO */
5833
5834#if defined(FEAT_FKMAP) || defined(PROTO)
5835/*
5836 * Write a character at the current cursor+offset position.
5837 * It is directly written into the command buffer block.
5838 */
5839 void
5840cmd_pchar(c, offset)
5841 int c, offset;
5842{
5843 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5844 {
5845 EMSG(_("E198: cmd_pchar beyond the command length"));
5846 return;
5847 }
5848 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5849 ccline.cmdbuff[ccline.cmdlen] = NUL;
5850}
5851
5852 int
5853cmd_gchar(offset)
5854 int offset;
5855{
5856 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5857 {
5858 /* EMSG(_("cmd_gchar beyond the command length")); */
5859 return NUL;
5860 }
5861 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5862}
5863#endif
5864
5865#if defined(FEAT_CMDWIN) || defined(PROTO)
5866/*
5867 * Open a window on the current command line and history. Allow editing in
5868 * the window. Returns when the window is closed.
5869 * Returns:
5870 * CR if the command is to be executed
5871 * Ctrl_C if it is to be abandoned
5872 * K_IGNORE if editing continues
5873 */
5874 static int
5875ex_window()
5876{
5877 struct cmdline_info save_ccline;
5878 buf_T *old_curbuf = curbuf;
5879 win_T *old_curwin = curwin;
5880 buf_T *bp;
5881 win_T *wp;
5882 int i;
5883 linenr_T lnum;
5884 int histtype;
5885 garray_T winsizes;
5886 char_u typestr[2];
5887 int save_restart_edit = restart_edit;
5888 int save_State = State;
5889 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005890#ifdef FEAT_RIGHTLEFT
5891 int save_cmdmsg_rl = cmdmsg_rl;
5892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
5894 /* Can't do this recursively. Can't do it when typing a password. */
5895 if (cmdwin_type != 0
5896# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5897 || cmdline_star > 0
5898# endif
5899 )
5900 {
5901 beep_flush();
5902 return K_IGNORE;
5903 }
5904
5905 /* Save current window sizes. */
5906 win_size_save(&winsizes);
5907
5908# ifdef FEAT_AUTOCMD
5909 /* Don't execute autocommands while creating the window. */
5910 ++autocmd_block;
5911# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005912 /* don't use a new tab page */
5913 cmdmod.tab = 0;
5914
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 /* Create a window for the command-line buffer. */
5916 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5917 {
5918 beep_flush();
5919 return K_IGNORE;
5920 }
5921 cmdwin_type = ccline.cmdfirstc;
5922 if (cmdwin_type == NUL)
5923 cmdwin_type = '-';
5924
5925 /* Create the command-line buffer empty. */
5926 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5927 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5928 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5929 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5930 curbuf->b_p_ma = TRUE;
5931# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005932 curwin->w_p_rl = cmdmsg_rl;
5933 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934# endif
5935# ifdef FEAT_SCROLLBIND
5936 curwin->w_p_scb = FALSE;
5937# endif
5938
5939# ifdef FEAT_AUTOCMD
5940 /* Do execute autocommands for setting the filetype (load syntax). */
5941 --autocmd_block;
5942# endif
5943
Bram Moolenaar46152342005-09-07 21:18:43 +00005944 /* Showing the prompt may have set need_wait_return, reset it. */
5945 need_wait_return = FALSE;
5946
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 histtype = hist_char2type(ccline.cmdfirstc);
5948 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5949 {
5950 if (p_wc == TAB)
5951 {
5952 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5953 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5954 }
5955 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5956 }
5957
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005958 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5959 * sets 'textwidth' to 78). */
5960 curbuf->b_p_tw = 0;
5961
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 /* Fill the buffer with the history. */
5963 init_history();
5964 if (hislen > 0)
5965 {
5966 i = hisidx[histtype];
5967 if (i >= 0)
5968 {
5969 lnum = 0;
5970 do
5971 {
5972 if (++i == hislen)
5973 i = 0;
5974 if (history[histtype][i].hisstr != NULL)
5975 ml_append(lnum++, history[histtype][i].hisstr,
5976 (colnr_T)0, FALSE);
5977 }
5978 while (i != hisidx[histtype]);
5979 }
5980 }
5981
5982 /* Replace the empty last line with the current command-line and put the
5983 * cursor there. */
5984 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5985 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5986 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00005987 changed_line_abv_curs();
5988 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005989 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990
5991 /* Save the command line info, can be used recursively. */
5992 save_ccline = ccline;
5993 ccline.cmdbuff = NULL;
5994 ccline.cmdprompt = NULL;
5995
5996 /* No Ex mode here! */
5997 exmode_active = 0;
5998
5999 State = NORMAL;
6000# ifdef FEAT_MOUSE
6001 setmouse();
6002# endif
6003
6004# ifdef FEAT_AUTOCMD
6005 /* Trigger CmdwinEnter autocommands. */
6006 typestr[0] = cmdwin_type;
6007 typestr[1] = NUL;
6008 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006009 if (restart_edit != 0) /* autocmd with ":startinsert" */
6010 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011# endif
6012
6013 i = RedrawingDisabled;
6014 RedrawingDisabled = 0;
6015
6016 /*
6017 * Call the main loop until <CR> or CTRL-C is typed.
6018 */
6019 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006020 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021
6022 RedrawingDisabled = i;
6023
6024# ifdef FEAT_AUTOCMD
6025 /* Trigger CmdwinLeave autocommands. */
6026 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6027# endif
6028
6029 /* Restore the comand line info. */
6030 ccline = save_ccline;
6031 cmdwin_type = 0;
6032
6033 exmode_active = save_exmode;
6034
6035 /* Safety check: The old window or buffer was deleted: It's a a bug when
6036 * this happens! */
6037 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6038 {
6039 cmdwin_result = Ctrl_C;
6040 EMSG(_("E199: Active window or buffer deleted"));
6041 }
6042 else
6043 {
6044# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6045 /* autocmds may abort script processing */
6046 if (aborting() && cmdwin_result != K_IGNORE)
6047 cmdwin_result = Ctrl_C;
6048# endif
6049 /* Set the new command line from the cmdline buffer. */
6050 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006051 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006053 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6054
6055 if (histtype == HIST_CMD)
6056 {
6057 /* Execute the command directly. */
6058 ccline.cmdbuff = vim_strsave((char_u *)p);
6059 cmdwin_result = CAR;
6060 }
6061 else
6062 {
6063 /* First need to cancel what we were doing. */
6064 ccline.cmdbuff = NULL;
6065 stuffcharReadbuff(':');
6066 stuffReadbuff((char_u *)p);
6067 stuffcharReadbuff(CAR);
6068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 }
6070 else if (cmdwin_result == K_XF2) /* :qa typed */
6071 {
6072 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6073 cmdwin_result = CAR;
6074 }
6075 else
6076 ccline.cmdbuff = vim_strsave(ml_get_curline());
6077 if (ccline.cmdbuff == NULL)
6078 cmdwin_result = Ctrl_C;
6079 else
6080 {
6081 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6082 ccline.cmdbufflen = ccline.cmdlen + 1;
6083 ccline.cmdpos = curwin->w_cursor.col;
6084 if (ccline.cmdpos > ccline.cmdlen)
6085 ccline.cmdpos = ccline.cmdlen;
6086 if (cmdwin_result == K_IGNORE)
6087 {
6088 set_cmdspos_cursor();
6089 redrawcmd();
6090 }
6091 }
6092
6093# ifdef FEAT_AUTOCMD
6094 /* Don't execute autocommands while deleting the window. */
6095 ++autocmd_block;
6096# endif
6097 wp = curwin;
6098 bp = curbuf;
6099 win_goto(old_curwin);
6100 win_close(wp, TRUE);
6101 close_buffer(NULL, bp, DOBUF_WIPE);
6102
6103 /* Restore window sizes. */
6104 win_size_restore(&winsizes);
6105
6106# ifdef FEAT_AUTOCMD
6107 --autocmd_block;
6108# endif
6109 }
6110
6111 ga_clear(&winsizes);
6112 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006113# ifdef FEAT_RIGHTLEFT
6114 cmdmsg_rl = save_cmdmsg_rl;
6115# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116
6117 State = save_State;
6118# ifdef FEAT_MOUSE
6119 setmouse();
6120# endif
6121
6122 return cmdwin_result;
6123}
6124#endif /* FEAT_CMDWIN */
6125
6126/*
6127 * Used for commands that either take a simple command string argument, or:
6128 * cmd << endmarker
6129 * {script}
6130 * endmarker
6131 * Returns a pointer to allocated memory with {script} or NULL.
6132 */
6133 char_u *
6134script_get(eap, cmd)
6135 exarg_T *eap;
6136 char_u *cmd;
6137{
6138 char_u *theline;
6139 char *end_pattern = NULL;
6140 char dot[] = ".";
6141 garray_T ga;
6142
6143 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6144 return NULL;
6145
6146 ga_init2(&ga, 1, 0x400);
6147
6148 if (cmd[2] != NUL)
6149 end_pattern = (char *)skipwhite(cmd + 2);
6150 else
6151 end_pattern = dot;
6152
6153 for (;;)
6154 {
6155 theline = eap->getline(
6156#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006157 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158#endif
6159 NUL, eap->cookie, 0);
6160
6161 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
6162 break;
6163
6164 ga_concat(&ga, theline);
6165 ga_append(&ga, '\n');
6166 vim_free(theline);
6167 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006168 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169
6170 return (char_u *)ga.ga_data;
6171}