blob: c0d2496b8c222265f4b84037fc27d4e472fcf528 [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 Moolenaard6e7cc62008-09-14 12:42:29 +000034 expand_T *xpc; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000036 int xp_context; /* type of expansion */
37# ifdef FEAT_EVAL
38 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000039 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041};
42
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000043/* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49static int cmd_showtail; /* Only show path tail in lists ? */
50
51#ifdef FEAT_EVAL
52static int new_cmdpos; /* position set by set_cmdline_pos() */
53#endif
54
55#ifdef FEAT_CMDHIST
56typedef struct hist_entry
57{
58 int hisnum; /* identifying number */
59 char_u *hisstr; /* actual entry, separator char after the NUL */
60} histentry_T;
61
62static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
63static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
64static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
65 /* identifying (unique) number of newest history entry */
66static int hislen = 0; /* actual length of history tables */
67
68static int hist_char2type __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
70static int in_history __ARGS((int, char_u *, int));
71# ifdef FEAT_EVAL
72static int calc_hist_idx __ARGS((int histype, int num));
73# endif
74#endif
75
76#ifdef FEAT_RIGHTLEFT
77static int cmd_hkmap = 0; /* Hebrew mapping during command line */
78#endif
79
80#ifdef FEAT_FKMAP
81static int cmd_fkmap = 0; /* Farsi mapping during command line */
82#endif
83
84static int cmdline_charsize __ARGS((int idx));
85static void set_cmdspos __ARGS((void));
86static void set_cmdspos_cursor __ARGS((void));
87#ifdef FEAT_MBYTE
88static void correct_cmdspos __ARGS((int idx, int cells));
89#endif
90static void alloc_cmdbuff __ARGS((int len));
91static int realloc_cmdbuff __ARGS((int len));
92static void draw_cmdline __ARGS((int start, int len));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +000093static void save_cmdline __ARGS((struct cmdline_info *ccp));
94static void restore_cmdline __ARGS((struct cmdline_info *ccp));
Bram Moolenaar1769d5a2006-10-17 14:25:24 +000095static int cmdline_paste __ARGS((int regname, int literally, int remcr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
97static void redrawcmd_preedit __ARGS((void));
98#endif
99#ifdef FEAT_WILDMENU
100static void cmdline_del __ARGS((int from));
101#endif
102static void redrawcmdprompt __ARGS((void));
103static void cursorcmd __ARGS((void));
104static int ccheck_abbr __ARGS((int));
105static int nextwild __ARGS((expand_T *xp, int type, int options));
Bram Moolenaar45360022005-07-21 21:08:21 +0000106static void escape_fname __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static int showmatches __ARGS((expand_T *xp, int wildmenu));
108static void set_expand_context __ARGS((expand_T *xp));
109static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int));
110static int expand_showtail __ARGS((expand_T *xp));
111#ifdef FEAT_CMDL_COMPL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000112static int expand_shellcmd __ARGS((char_u *filepat, int *num_file, char_u ***file, int flagsarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname));
114# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
115static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000116static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117# endif
118#endif
119
120#ifdef FEAT_CMDWIN
121static int ex_window __ARGS((void));
122#endif
123
124/*
125 * getcmdline() - accept a command line starting with firstc.
126 *
127 * firstc == ':' get ":" command line.
128 * firstc == '/' or '?' get search pattern
129 * firstc == '=' get expression
130 * firstc == '@' get text for input() function
131 * firstc == '>' get text for debug mode
132 * firstc == NUL get text for :insert command
133 * firstc == -1 like NUL, and break on CTRL-C
134 *
135 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
136 * command line.
137 *
138 * Careful: getcmdline() can be called recursively!
139 *
140 * Return pointer to allocated string if there is a commandline, NULL
141 * otherwise.
142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 char_u *
144getcmdline(firstc, count, indent)
145 int firstc;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000146 long count UNUSED; /* only used for incremental search */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 int indent; /* indent for inside conditionals */
148{
149 int c;
150 int i;
151 int j;
152 int gotesc = FALSE; /* TRUE when <ESC> just typed */
153 int do_abbr; /* when TRUE check for abbr. */
154#ifdef FEAT_CMDHIST
155 char_u *lookfor = NULL; /* string to match */
156 int hiscnt; /* current history line in use */
157 int histype; /* history type to be used */
158#endif
159#ifdef FEAT_SEARCH_EXTRA
160 pos_T old_cursor;
161 colnr_T old_curswant;
162 colnr_T old_leftcol;
163 linenr_T old_topline;
164# ifdef FEAT_DIFF
165 int old_topfill;
166# endif
167 linenr_T old_botline;
168 int did_incsearch = FALSE;
169 int incsearch_postponed = FALSE;
170#endif
171 int did_wild_list = FALSE; /* did wild_list() recently */
172 int wim_index = 0; /* index in wim_flags[] */
173 int res;
174 int save_msg_scroll = msg_scroll;
175 int save_State = State; /* remember State when called */
176 int some_key_typed = FALSE; /* one of the keys was typed */
177#ifdef FEAT_MOUSE
178 /* mouse drag and release events are ignored, unless they are
179 * preceded with a mouse down event */
180 int ignore_drag_release = TRUE;
181#endif
182#ifdef FEAT_EVAL
183 int break_ctrl_c = FALSE;
184#endif
185 expand_T xpc;
186 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000187#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
188 /* Everything that may work recursively should save and restore the
189 * current command line in save_ccline. That includes update_screen(), a
190 * custom status line may invoke ":normal". */
191 struct cmdline_info save_ccline;
192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194#ifdef FEAT_SNIFF
195 want_sniff_request = 0;
196#endif
197#ifdef FEAT_EVAL
198 if (firstc == -1)
199 {
200 firstc = NUL;
201 break_ctrl_c = TRUE;
202 }
203#endif
204#ifdef FEAT_RIGHTLEFT
205 /* start without Hebrew mapping for a command line */
206 if (firstc == ':' || firstc == '=' || firstc == '>')
207 cmd_hkmap = 0;
208#endif
209
210 ccline.overstrike = FALSE; /* always start in insert mode */
211#ifdef FEAT_SEARCH_EXTRA
212 old_cursor = curwin->w_cursor; /* needs to be restored later */
213 old_curswant = curwin->w_curswant;
214 old_leftcol = curwin->w_leftcol;
215 old_topline = curwin->w_topline;
216# ifdef FEAT_DIFF
217 old_topfill = curwin->w_topfill;
218# endif
219 old_botline = curwin->w_botline;
220#endif
221
222 /*
223 * set some variables for redrawcmd()
224 */
225 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000226 ccline.cmdindent = (firstc > 0 ? indent : 0);
227
228 /* alloc initial ccline.cmdbuff */
229 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 if (ccline.cmdbuff == NULL)
231 return NULL; /* out of memory */
232 ccline.cmdlen = ccline.cmdpos = 0;
233 ccline.cmdbuff[0] = NUL;
234
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000235 /* autoindent for :insert and :append */
236 if (firstc <= 0)
237 {
238 copy_spaces(ccline.cmdbuff, indent);
239 ccline.cmdbuff[indent] = NUL;
240 ccline.cmdpos = indent;
241 ccline.cmdspos = indent;
242 ccline.cmdlen = indent;
243 }
244
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000246 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247
248#ifdef FEAT_RIGHTLEFT
249 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
250 && (firstc == '/' || firstc == '?'))
251 cmdmsg_rl = TRUE;
252 else
253 cmdmsg_rl = FALSE;
254#endif
255
256 redir_off = TRUE; /* don't redirect the typed command */
257 if (!cmd_silent)
258 {
259 i = msg_scrolled;
260 msg_scrolled = 0; /* avoid wait_return message */
261 gotocmdline(TRUE);
262 msg_scrolled += i;
263 redrawcmdprompt(); /* draw prompt or indent */
264 set_cmdspos();
265 }
266 xpc.xp_context = EXPAND_NOTHING;
267 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000268#ifndef BACKSLASH_IN_FILENAME
269 xpc.xp_shell = FALSE;
270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000272#if defined(FEAT_EVAL)
273 if (ccline.input_fn)
274 {
275 xpc.xp_context = ccline.xp_context;
276 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000277# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000278 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000279# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000280 }
281#endif
282
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 /*
284 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
285 * doing ":@0" when register 0 doesn't contain a CR.
286 */
287 msg_scroll = FALSE;
288
289 State = CMDLINE;
290
291 if (firstc == '/' || firstc == '?' || firstc == '@')
292 {
293 /* Use ":lmap" mappings for search pattern and input(). */
294 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
295 b_im_ptr = &curbuf->b_p_iminsert;
296 else
297 b_im_ptr = &curbuf->b_p_imsearch;
298 if (*b_im_ptr == B_IMODE_LMAP)
299 State |= LANGMAP;
300#ifdef USE_IM_CONTROL
301 im_set_active(*b_im_ptr == B_IMODE_IM);
302#endif
303 }
304#ifdef USE_IM_CONTROL
305 else if (p_imcmdline)
306 im_set_active(TRUE);
307#endif
308
309#ifdef FEAT_MOUSE
310 setmouse();
311#endif
312#ifdef CURSOR_SHAPE
313 ui_cursor_shape(); /* may show different cursor shape */
314#endif
315
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000316 /* When inside an autocommand for writing "exiting" may be set and
317 * terminal mode set to cooked. Need to set raw mode here then. */
318 settmode(TMODE_RAW);
319
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifdef FEAT_CMDHIST
321 init_history();
322 hiscnt = hislen; /* set hiscnt to impossible history value */
323 histype = hist_char2type(firstc);
324#endif
325
326#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000327 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#endif
329
330 /*
331 * Collect the command string, handling editing keys.
332 */
333 for (;;)
334 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000335 redir_off = TRUE; /* Don't redirect the typed command.
336 Repeated, because a ":redir" inside
337 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338#ifdef USE_ON_FLY_SCROLL
339 dont_scroll = FALSE; /* allow scrolling here */
340#endif
341 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
342
343 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000344
345 /* Get a character. Ignore K_IGNORE, it should not do anything, such
346 * as stop completion. */
347 do
348 {
349 c = safe_vgetc();
350 } while (c == K_IGNORE);
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 if (KeyTyped)
353 {
354 some_key_typed = TRUE;
355#ifdef FEAT_RIGHTLEFT
356 if (cmd_hkmap)
357 c = hkmap(c);
358# ifdef FEAT_FKMAP
359 if (cmd_fkmap)
360 c = cmdl_fkmap(c);
361# endif
362 if (cmdmsg_rl && !KeyStuffed)
363 {
364 /* Invert horizontal movements and operations. Only when
365 * typed by the user directly, not when the result of a
366 * mapping. */
367 switch (c)
368 {
369 case K_RIGHT: c = K_LEFT; break;
370 case K_S_RIGHT: c = K_S_LEFT; break;
371 case K_C_RIGHT: c = K_C_LEFT; break;
372 case K_LEFT: c = K_RIGHT; break;
373 case K_S_LEFT: c = K_S_RIGHT; break;
374 case K_C_LEFT: c = K_C_RIGHT; break;
375 }
376 }
377#endif
378 }
379
380 /*
381 * Ignore got_int when CTRL-C was typed here.
382 * Don't ignore it in :global, we really need to break then, e.g., for
383 * ":g/pat/normal /pat" (without the <CR>).
384 * Don't ignore it for the input() function.
385 */
386 if ((c == Ctrl_C
387#ifdef UNIX
388 || c == intr_char
389#endif
390 )
391#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
392 && firstc != '@'
393#endif
394#ifdef FEAT_EVAL
395 && !break_ctrl_c
396#endif
397 && !global_busy)
398 got_int = FALSE;
399
400#ifdef FEAT_CMDHIST
401 /* free old command line when finished moving around in the history
402 * list */
403 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000404 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000405 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 && c != K_PAGEDOWN && c != K_PAGEUP
407 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000408 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
410 {
411 vim_free(lookfor);
412 lookfor = NULL;
413 }
414#endif
415
416 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000417 * When there are matching completions to select <S-Tab> works like
418 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000420 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 c = Ctrl_P;
422
423#ifdef FEAT_WILDMENU
424 /* Special translations for 'wildmenu' */
425 if (did_wild_list && p_wmnu)
426 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000427 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000429 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 c = Ctrl_N;
431 }
432 /* Hitting CR after "emenu Name.": complete submenu */
433 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
434 && ccline.cmdpos > 1
435 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
436 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
437 && (c == '\n' || c == '\r' || c == K_KENTER))
438 c = K_DOWN;
439#endif
440
441 /* free expanded names when finished walking through matches */
442 if (xpc.xp_numfiles != -1
443 && !(c == p_wc && KeyTyped) && c != p_wcm
444 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
445 && c != Ctrl_L)
446 {
447 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
448 did_wild_list = FALSE;
449#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000450 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451#endif
452 xpc.xp_context = EXPAND_NOTHING;
453 wim_index = 0;
454#ifdef FEAT_WILDMENU
455 if (p_wmnu && wild_menu_showing != 0)
456 {
457 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000458 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000459
460 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000461 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
463 if (wild_menu_showing == WM_SCROLLED)
464 {
465 /* Entered command line, move it up */
466 cmdline_row--;
467 redrawcmd();
468 }
469 else if (save_p_ls != -1)
470 {
471 /* restore 'laststatus' and 'winminheight' */
472 p_ls = save_p_ls;
473 p_wmh = save_p_wmh;
474 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000475 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000477 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 redrawcmd();
479 save_p_ls = -1;
480 }
481 else
482 {
483# ifdef FEAT_VERTSPLIT
484 win_redraw_last_status(topframe);
485# else
486 lastwin->w_redr_status = TRUE;
487# endif
488 redraw_statuslines();
489 }
490 KeyTyped = skt;
491 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000492 if (ccline.input_fn)
493 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 }
495#endif
496 }
497
498#ifdef FEAT_WILDMENU
499 /* Special translations for 'wildmenu' */
500 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
501 {
502 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000503 if (c == K_DOWN && ccline.cmdpos > 0
504 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000506 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {
508 /* Hitting <Up>: Remove one submenu name in front of the
509 * cursor */
510 int found = FALSE;
511
512 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
513 i = 0;
514 while (--j > 0)
515 {
516 /* check for start of menu name */
517 if (ccline.cmdbuff[j] == ' '
518 && ccline.cmdbuff[j - 1] != '\\')
519 {
520 i = j + 1;
521 break;
522 }
523 /* check for start of submenu name */
524 if (ccline.cmdbuff[j] == '.'
525 && ccline.cmdbuff[j - 1] != '\\')
526 {
527 if (found)
528 {
529 i = j + 1;
530 break;
531 }
532 else
533 found = TRUE;
534 }
535 }
536 if (i > 0)
537 cmdline_del(i);
538 c = p_wc;
539 xpc.xp_context = EXPAND_NOTHING;
540 }
541 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000542 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000543 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000544 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 {
546 char_u upseg[5];
547
548 upseg[0] = PATHSEP;
549 upseg[1] = '.';
550 upseg[2] = '.';
551 upseg[3] = PATHSEP;
552 upseg[4] = NUL;
553
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000554 if (c == K_DOWN
555 && ccline.cmdpos > 0
556 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
557 && (ccline.cmdpos < 3
558 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
560 {
561 /* go down a directory */
562 c = p_wc;
563 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000564 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 {
566 /* If in a direct ancestor, strip off one ../ to go down */
567 int found = FALSE;
568
569 j = ccline.cmdpos;
570 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
571 while (--j > i)
572 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000573#ifdef FEAT_MBYTE
574 if (has_mbyte)
575 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (vim_ispathsep(ccline.cmdbuff[j]))
578 {
579 found = TRUE;
580 break;
581 }
582 }
583 if (found
584 && ccline.cmdbuff[j - 1] == '.'
585 && ccline.cmdbuff[j - 2] == '.'
586 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
587 {
588 cmdline_del(j - 2);
589 c = p_wc;
590 }
591 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000592 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 {
594 /* go up a directory */
595 int found = FALSE;
596
597 j = ccline.cmdpos - 1;
598 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
599 while (--j > i)
600 {
601#ifdef FEAT_MBYTE
602 if (has_mbyte)
603 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
604#endif
605 if (vim_ispathsep(ccline.cmdbuff[j])
606#ifdef BACKSLASH_IN_FILENAME
607 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
608 == NULL
609#endif
610 )
611 {
612 if (found)
613 {
614 i = j + 1;
615 break;
616 }
617 else
618 found = TRUE;
619 }
620 }
621
622 if (!found)
623 j = i;
624 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
625 j += 4;
626 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
627 && j == i)
628 j += 3;
629 else
630 j = 0;
631 if (j > 0)
632 {
633 /* TODO this is only for DOS/UNIX systems - need to put in
634 * machine-specific stuff here and in upseg init */
635 cmdline_del(j);
636 put_on_cmdline(upseg + 1, 3, FALSE);
637 }
638 else if (ccline.cmdpos > i)
639 cmdline_del(i);
640 c = p_wc;
641 }
642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644#endif /* FEAT_WILDMENU */
645
646 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
647 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
648 if (c == Ctrl_BSL)
649 {
650 ++no_mapping;
651 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000652 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 --no_mapping;
654 --allow_keys;
655 /* CTRL-\ e doesn't work when obtaining an expression. */
656 if (c != Ctrl_N && c != Ctrl_G
657 && (c != 'e' || ccline.cmdfirstc == '='))
658 {
659 vungetc(c);
660 c = Ctrl_BSL;
661 }
662#ifdef FEAT_EVAL
663 else if (c == 'e')
664 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200665 char_u *p = NULL;
666 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
668 /*
669 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000670 * Need to save and restore the current command line, to be
671 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 */
673 if (ccline.cmdpos == ccline.cmdlen)
674 new_cmdpos = 99999; /* keep it at the end */
675 else
676 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000677
678 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000680 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 if (c == '=')
682 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000683 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000684 * to avoid nasty things like going to another buffer when
685 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000686 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000687 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000689 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000690 restore_cmdline(&save_ccline);
691
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200692 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200694 len = (int)STRLEN(p);
695 if (realloc_cmdbuff(len + 1) == OK)
696 {
697 ccline.cmdlen = len;
698 STRCPY(ccline.cmdbuff, p);
699 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200701 /* Restore the cursor or use the position set with
702 * set_cmdline_pos(). */
703 if (new_cmdpos > ccline.cmdlen)
704 ccline.cmdpos = ccline.cmdlen;
705 else
706 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200708 KeyTyped = FALSE; /* Don't do p_wc completion. */
709 redrawcmd();
710 goto cmdline_changed;
711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 }
713 }
714 beep_flush();
715 c = ESC;
716 }
717#endif
718 else
719 {
720 if (c == Ctrl_G && p_im && restart_edit == 0)
721 restart_edit = 'a';
722 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
723 in history */
724 goto returncmd; /* back to Normal mode */
725 }
726 }
727
728#ifdef FEAT_CMDWIN
729 if (c == cedit_key || c == K_CMDWIN)
730 {
731 /*
732 * Open a window to edit the command line (and history).
733 */
734 c = ex_window();
735 some_key_typed = TRUE;
736 }
737# ifdef FEAT_DIGRAPHS
738 else
739# endif
740#endif
741#ifdef FEAT_DIGRAPHS
742 c = do_digraph(c);
743#endif
744
745 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
746 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
747 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000748 /* In Ex mode a backslash escapes a newline. */
749 if (exmode_active
750 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000751 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000752 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000753 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000755 if (c == K_KENTER)
756 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000758 else
759 {
760 gotesc = FALSE; /* Might have typed ESC previously, don't
761 truncate the cmdline now. */
762 if (ccheck_abbr(c + ABBR_OFF))
763 goto cmdline_changed;
764 if (!cmd_silent)
765 {
766 windgoto(msg_row, 0);
767 out_flush();
768 }
769 break;
770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 }
772
773 /*
774 * Completion for 'wildchar' or 'wildcharm' key.
775 * - hitting <ESC> twice means: abandon command line.
776 * - wildcard expansion is only done when the 'wildchar' key is really
777 * typed, not when it comes from a macro
778 */
779 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
780 {
781 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
782 {
783 /* if 'wildmode' contains "list" may still need to list */
784 if (xpc.xp_numfiles > 1
785 && !did_wild_list
786 && (wim_flags[wim_index] & WIM_LIST))
787 {
788 (void)showmatches(&xpc, FALSE);
789 redrawcmd();
790 did_wild_list = TRUE;
791 }
792 if (wim_flags[wim_index] & WIM_LONGEST)
793 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
794 else if (wim_flags[wim_index] & WIM_FULL)
795 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
796 else
797 res = OK; /* don't insert 'wildchar' now */
798 }
799 else /* typed p_wc first time */
800 {
801 wim_index = 0;
802 j = ccline.cmdpos;
803 /* if 'wildmode' first contains "longest", get longest
804 * common part */
805 if (wim_flags[0] & WIM_LONGEST)
806 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
807 else
808 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
809
810 /* if interrupted while completing, behave like it failed */
811 if (got_int)
812 {
813 (void)vpeekc(); /* remove <C-C> from input stream */
814 got_int = FALSE; /* don't abandon the command line */
815 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
816#ifdef FEAT_WILDMENU
817 xpc.xp_context = EXPAND_NOTHING;
818#endif
819 goto cmdline_changed;
820 }
821
822 /* when more than one match, and 'wildmode' first contains
823 * "list", or no change and 'wildmode' contains "longest,list",
824 * list all matches */
825 if (res == OK && xpc.xp_numfiles > 1)
826 {
827 /* a "longest" that didn't do anything is skipped (but not
828 * "list:longest") */
829 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
830 wim_index = 1;
831 if ((wim_flags[wim_index] & WIM_LIST)
832#ifdef FEAT_WILDMENU
833 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
834#endif
835 )
836 {
837 if (!(wim_flags[0] & WIM_LONGEST))
838 {
839#ifdef FEAT_WILDMENU
840 int p_wmnu_save = p_wmnu;
841 p_wmnu = 0;
842#endif
843 nextwild(&xpc, WILD_PREV, 0); /* remove match */
844#ifdef FEAT_WILDMENU
845 p_wmnu = p_wmnu_save;
846#endif
847 }
848#ifdef FEAT_WILDMENU
849 (void)showmatches(&xpc, p_wmnu
850 && ((wim_flags[wim_index] & WIM_LIST) == 0));
851#else
852 (void)showmatches(&xpc, FALSE);
853#endif
854 redrawcmd();
855 did_wild_list = TRUE;
856 if (wim_flags[wim_index] & WIM_LONGEST)
857 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
858 else if (wim_flags[wim_index] & WIM_FULL)
859 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
860 }
861 else
862 vim_beep();
863 }
864#ifdef FEAT_WILDMENU
865 else if (xpc.xp_numfiles == -1)
866 xpc.xp_context = EXPAND_NOTHING;
867#endif
868 }
869 if (wim_index < 3)
870 ++wim_index;
871 if (c == ESC)
872 gotesc = TRUE;
873 if (res == OK)
874 goto cmdline_changed;
875 }
876
877 gotesc = FALSE;
878
879 /* <S-Tab> goes to last match, in a clumsy way */
880 if (c == K_S_TAB && KeyTyped)
881 {
882 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
883 && nextwild(&xpc, WILD_PREV, 0) == OK
884 && nextwild(&xpc, WILD_PREV, 0) == OK)
885 goto cmdline_changed;
886 }
887
888 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
889 c = NL;
890
891 do_abbr = TRUE; /* default: check for abbreviation */
892
893 /*
894 * Big switch for a typed command line character.
895 */
896 switch (c)
897 {
898 case K_BS:
899 case Ctrl_H:
900 case K_DEL:
901 case K_KDEL:
902 case Ctrl_W:
903#ifdef FEAT_FKMAP
904 if (cmd_fkmap && c == K_BS)
905 c = K_DEL;
906#endif
907 if (c == K_KDEL)
908 c = K_DEL;
909
910 /*
911 * delete current character is the same as backspace on next
912 * character, except at end of line
913 */
914 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
915 ++ccline.cmdpos;
916#ifdef FEAT_MBYTE
917 if (has_mbyte && c == K_DEL)
918 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
919 ccline.cmdbuff + ccline.cmdpos);
920#endif
921 if (ccline.cmdpos > 0)
922 {
923 char_u *p;
924
925 j = ccline.cmdpos;
926 p = ccline.cmdbuff + j;
927#ifdef FEAT_MBYTE
928 if (has_mbyte)
929 {
930 p = mb_prevptr(ccline.cmdbuff, p);
931 if (c == Ctrl_W)
932 {
933 while (p > ccline.cmdbuff && vim_isspace(*p))
934 p = mb_prevptr(ccline.cmdbuff, p);
935 i = mb_get_class(p);
936 while (p > ccline.cmdbuff && mb_get_class(p) == i)
937 p = mb_prevptr(ccline.cmdbuff, p);
938 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000939 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 }
941 }
942 else
943#endif
944 if (c == Ctrl_W)
945 {
946 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
947 --p;
948 i = vim_iswordc(p[-1]);
949 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
950 && vim_iswordc(p[-1]) == i)
951 --p;
952 }
953 else
954 --p;
955 ccline.cmdpos = (int)(p - ccline.cmdbuff);
956 ccline.cmdlen -= j - ccline.cmdpos;
957 i = ccline.cmdpos;
958 while (i < ccline.cmdlen)
959 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
960
961 /* Truncate at the end, required for multi-byte chars. */
962 ccline.cmdbuff[ccline.cmdlen] = NUL;
963 redrawcmd();
964 }
965 else if (ccline.cmdlen == 0 && c != Ctrl_W
966 && ccline.cmdprompt == NULL && indent == 0)
967 {
968 /* In ex and debug mode it doesn't make sense to return. */
969 if (exmode_active
970#ifdef FEAT_EVAL
971 || ccline.cmdfirstc == '>'
972#endif
973 )
974 goto cmdline_not_changed;
975
976 vim_free(ccline.cmdbuff); /* no commandline to return */
977 ccline.cmdbuff = NULL;
978 if (!cmd_silent)
979 {
980#ifdef FEAT_RIGHTLEFT
981 if (cmdmsg_rl)
982 msg_col = Columns;
983 else
984#endif
985 msg_col = 0;
986 msg_putchar(' '); /* delete ':' */
987 }
988 redraw_cmdline = TRUE;
989 goto returncmd; /* back to cmd mode */
990 }
991 goto cmdline_changed;
992
993 case K_INS:
994 case K_KINS:
995#ifdef FEAT_FKMAP
996 /* if Farsi mode set, we are in reverse insert mode -
997 Do not change the mode */
998 if (cmd_fkmap)
999 beep_flush();
1000 else
1001#endif
1002 ccline.overstrike = !ccline.overstrike;
1003#ifdef CURSOR_SHAPE
1004 ui_cursor_shape(); /* may show different cursor shape */
1005#endif
1006 goto cmdline_not_changed;
1007
1008 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001009 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 {
1011 /* ":lmap" mappings exists, toggle use of mappings. */
1012 State ^= LANGMAP;
1013#ifdef USE_IM_CONTROL
1014 im_set_active(FALSE); /* Disable input method */
1015#endif
1016 if (b_im_ptr != NULL)
1017 {
1018 if (State & LANGMAP)
1019 *b_im_ptr = B_IMODE_LMAP;
1020 else
1021 *b_im_ptr = B_IMODE_NONE;
1022 }
1023 }
1024#ifdef USE_IM_CONTROL
1025 else
1026 {
1027 /* There are no ":lmap" mappings, toggle IM. When
1028 * 'imdisable' is set don't try getting the status, it's
1029 * always off. */
1030 if ((p_imdisable && b_im_ptr != NULL)
1031 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1032 {
1033 im_set_active(FALSE); /* Disable input method */
1034 if (b_im_ptr != NULL)
1035 *b_im_ptr = B_IMODE_NONE;
1036 }
1037 else
1038 {
1039 im_set_active(TRUE); /* Enable input method */
1040 if (b_im_ptr != NULL)
1041 *b_im_ptr = B_IMODE_IM;
1042 }
1043 }
1044#endif
1045 if (b_im_ptr != NULL)
1046 {
1047 if (b_im_ptr == &curbuf->b_p_iminsert)
1048 set_iminsert_global();
1049 else
1050 set_imsearch_global();
1051 }
1052#ifdef CURSOR_SHAPE
1053 ui_cursor_shape(); /* may show different cursor shape */
1054#endif
1055#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1056 /* Show/unshow value of 'keymap' in status lines later. */
1057 status_redraw_curbuf();
1058#endif
1059 goto cmdline_not_changed;
1060
1061/* case '@': only in very old vi */
1062 case Ctrl_U:
1063 /* delete all characters left of the cursor */
1064 j = ccline.cmdpos;
1065 ccline.cmdlen -= j;
1066 i = ccline.cmdpos = 0;
1067 while (i < ccline.cmdlen)
1068 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1069 /* Truncate at the end, required for multi-byte chars. */
1070 ccline.cmdbuff[ccline.cmdlen] = NUL;
1071 redrawcmd();
1072 goto cmdline_changed;
1073
1074#ifdef FEAT_CLIPBOARD
1075 case Ctrl_Y:
1076 /* Copy the modeless selection, if there is one. */
1077 if (clip_star.state != SELECT_CLEARED)
1078 {
1079 if (clip_star.state == SELECT_DONE)
1080 clip_copy_modeless_selection(TRUE);
1081 goto cmdline_not_changed;
1082 }
1083 break;
1084#endif
1085
1086 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1087 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001088 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001089 * ":normal" runs out of characters. */
1090 if (exmode_active
1091#ifdef FEAT_EX_EXTRA
1092 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1093#endif
1094 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 goto cmdline_not_changed;
1096
1097 gotesc = TRUE; /* will free ccline.cmdbuff after
1098 putting it in history */
1099 goto returncmd; /* back to cmd mode */
1100
1101 case Ctrl_R: /* insert register */
1102#ifdef USE_ON_FLY_SCROLL
1103 dont_scroll = TRUE; /* disallow scrolling here */
1104#endif
1105 putcmdline('"', TRUE);
1106 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001107 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 if (i == Ctrl_O)
1109 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1110 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001111 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 --no_mapping;
1113#ifdef FEAT_EVAL
1114 /*
1115 * Insert the result of an expression.
1116 * Need to save the current command line, to be able to enter
1117 * a new one...
1118 */
1119 new_cmdpos = -1;
1120 if (c == '=')
1121 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1123 {
1124 beep_flush();
1125 c = ESC;
1126 }
1127 else
1128 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001129 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001131 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 }
1133 }
1134#endif
1135 if (c != ESC) /* use ESC to cancel inserting register */
1136 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001137 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001138
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001139#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001140 /* When there was a serious error abort getting the
1141 * command line. */
1142 if (aborting())
1143 {
1144 gotesc = TRUE; /* will free ccline.cmdbuff after
1145 putting it in history */
1146 goto returncmd; /* back to cmd mode */
1147 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 KeyTyped = FALSE; /* Don't do p_wc completion. */
1150#ifdef FEAT_EVAL
1151 if (new_cmdpos >= 0)
1152 {
1153 /* set_cmdline_pos() was used */
1154 if (new_cmdpos > ccline.cmdlen)
1155 ccline.cmdpos = ccline.cmdlen;
1156 else
1157 ccline.cmdpos = new_cmdpos;
1158 }
1159#endif
1160 }
1161 redrawcmd();
1162 goto cmdline_changed;
1163
1164 case Ctrl_D:
1165 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1166 break; /* Use ^D as normal char instead */
1167
1168 redrawcmd();
1169 continue; /* don't do incremental search now */
1170
1171 case K_RIGHT:
1172 case K_S_RIGHT:
1173 case K_C_RIGHT:
1174 do
1175 {
1176 if (ccline.cmdpos >= ccline.cmdlen)
1177 break;
1178 i = cmdline_charsize(ccline.cmdpos);
1179 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1180 break;
1181 ccline.cmdspos += i;
1182#ifdef FEAT_MBYTE
1183 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001184 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 + ccline.cmdpos);
1186 else
1187#endif
1188 ++ccline.cmdpos;
1189 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001190 while ((c == K_S_RIGHT || c == K_C_RIGHT
1191 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1193#ifdef FEAT_MBYTE
1194 if (has_mbyte)
1195 set_cmdspos_cursor();
1196#endif
1197 goto cmdline_not_changed;
1198
1199 case K_LEFT:
1200 case K_S_LEFT:
1201 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001202 if (ccline.cmdpos == 0)
1203 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 do
1205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 --ccline.cmdpos;
1207#ifdef FEAT_MBYTE
1208 if (has_mbyte) /* move to first byte of char */
1209 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1210 ccline.cmdbuff + ccline.cmdpos);
1211#endif
1212 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1213 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001214 while (ccline.cmdpos > 0
1215 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001216 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1218#ifdef FEAT_MBYTE
1219 if (has_mbyte)
1220 set_cmdspos_cursor();
1221#endif
1222 goto cmdline_not_changed;
1223
1224 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001225 /* Ignore mouse event or ex_window() result. */
1226 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
Bram Moolenaar4770d092006-01-12 23:22:24 +00001228#ifdef FEAT_GUI_W32
1229 /* On Win32 ignore <M-F4>, we get it when closing the window was
1230 * cancelled. */
1231 case K_F4:
1232 if (mod_mask == MOD_MASK_ALT)
1233 {
1234 redrawcmd(); /* somehow the cmdline is cleared */
1235 goto cmdline_not_changed;
1236 }
1237 break;
1238#endif
1239
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240#ifdef FEAT_MOUSE
1241 case K_MIDDLEDRAG:
1242 case K_MIDDLERELEASE:
1243 goto cmdline_not_changed; /* Ignore mouse */
1244
1245 case K_MIDDLEMOUSE:
1246# ifdef FEAT_GUI
1247 /* When GUI is active, also paste when 'mouse' is empty */
1248 if (!gui.in_use)
1249# endif
1250 if (!mouse_has(MOUSE_COMMAND))
1251 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001252# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001254 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001256# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001257 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 redrawcmd();
1259 goto cmdline_changed;
1260
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001261# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001263 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 redrawcmd();
1265 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267
1268 case K_LEFTDRAG:
1269 case K_LEFTRELEASE:
1270 case K_RIGHTDRAG:
1271 case K_RIGHTRELEASE:
1272 /* Ignore drag and release events when the button-down wasn't
1273 * seen before. */
1274 if (ignore_drag_release)
1275 goto cmdline_not_changed;
1276 /* FALLTHROUGH */
1277 case K_LEFTMOUSE:
1278 case K_RIGHTMOUSE:
1279 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1280 ignore_drag_release = TRUE;
1281 else
1282 ignore_drag_release = FALSE;
1283# ifdef FEAT_GUI
1284 /* When GUI is active, also move when 'mouse' is empty */
1285 if (!gui.in_use)
1286# endif
1287 if (!mouse_has(MOUSE_COMMAND))
1288 goto cmdline_not_changed; /* Ignore mouse */
1289# ifdef FEAT_CLIPBOARD
1290 if (mouse_row < cmdline_row && clip_star.available)
1291 {
1292 int button, is_click, is_drag;
1293
1294 /*
1295 * Handle modeless selection.
1296 */
1297 button = get_mouse_button(KEY2TERMCAP1(c),
1298 &is_click, &is_drag);
1299 if (mouse_model_popup() && button == MOUSE_LEFT
1300 && (mod_mask & MOD_MASK_SHIFT))
1301 {
1302 /* Translate shift-left to right button. */
1303 button = MOUSE_RIGHT;
1304 mod_mask &= ~MOD_MASK_SHIFT;
1305 }
1306 clip_modeless(button, is_click, is_drag);
1307 goto cmdline_not_changed;
1308 }
1309# endif
1310
1311 set_cmdspos();
1312 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1313 ++ccline.cmdpos)
1314 {
1315 i = cmdline_charsize(ccline.cmdpos);
1316 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1317 && mouse_col < ccline.cmdspos % Columns + i)
1318 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001319# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 if (has_mbyte)
1321 {
1322 /* Count ">" for double-wide char that doesn't fit. */
1323 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001324 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 + ccline.cmdpos) - 1;
1326 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001327# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 ccline.cmdspos += i;
1329 }
1330 goto cmdline_not_changed;
1331
1332 /* Mouse scroll wheel: ignored here */
1333 case K_MOUSEDOWN:
1334 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001335 case K_MOUSELEFT:
1336 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 /* Alternate buttons ignored here */
1338 case K_X1MOUSE:
1339 case K_X1DRAG:
1340 case K_X1RELEASE:
1341 case K_X2MOUSE:
1342 case K_X2DRAG:
1343 case K_X2RELEASE:
1344 goto cmdline_not_changed;
1345
1346#endif /* FEAT_MOUSE */
1347
1348#ifdef FEAT_GUI
1349 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1350 case K_LEFTRELEASE_NM:
1351 goto cmdline_not_changed;
1352
1353 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001354 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 {
1356 gui_do_scroll();
1357 redrawcmd();
1358 }
1359 goto cmdline_not_changed;
1360
1361 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001362 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001364 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 redrawcmd();
1366 }
1367 goto cmdline_not_changed;
1368#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001369#ifdef FEAT_GUI_TABLINE
1370 case K_TABLINE:
1371 case K_TABMENU:
1372 /* Don't want to change any tabs here. Make sure the same tab
1373 * is still selected. */
1374 if (gui_use_tabline())
1375 gui_mch_set_curtab(tabpage_index(curtab));
1376 goto cmdline_not_changed;
1377#endif
1378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 case K_SELECT: /* end of Select mode mapping - ignore */
1380 goto cmdline_not_changed;
1381
1382 case Ctrl_B: /* begin of command line */
1383 case K_HOME:
1384 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 case K_S_HOME:
1386 case K_C_HOME:
1387 ccline.cmdpos = 0;
1388 set_cmdspos();
1389 goto cmdline_not_changed;
1390
1391 case Ctrl_E: /* end of command line */
1392 case K_END:
1393 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 case K_S_END:
1395 case K_C_END:
1396 ccline.cmdpos = ccline.cmdlen;
1397 set_cmdspos_cursor();
1398 goto cmdline_not_changed;
1399
1400 case Ctrl_A: /* all matches */
1401 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1402 break;
1403 goto cmdline_changed;
1404
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001405 case Ctrl_L:
1406#ifdef FEAT_SEARCH_EXTRA
1407 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1408 {
1409 /* Add a character from under the cursor for 'incsearch' */
1410 if (did_incsearch
1411 && !equalpos(curwin->w_cursor, old_cursor))
1412 {
1413 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001414 /* If 'ignorecase' and 'smartcase' are set and the
1415 * command line has no uppercase characters, convert
1416 * the character to lowercase */
1417 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1418 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001419 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001420 {
1421 if (c == firstc || vim_strchr((char_u *)(
1422 p_magic ? "\\^$.*[" : "\\^$"), c)
1423 != NULL)
1424 {
1425 /* put a backslash before special characters */
1426 stuffcharReadbuff(c);
1427 c = '\\';
1428 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001429 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001430 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001431 }
1432 goto cmdline_not_changed;
1433 }
1434#endif
1435
1436 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1438 break;
1439 goto cmdline_changed;
1440
1441 case Ctrl_N: /* next match */
1442 case Ctrl_P: /* previous match */
1443 if (xpc.xp_numfiles > 0)
1444 {
1445 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1446 == FAIL)
1447 break;
1448 goto cmdline_changed;
1449 }
1450
1451#ifdef FEAT_CMDHIST
1452 case K_UP:
1453 case K_DOWN:
1454 case K_S_UP:
1455 case K_S_DOWN:
1456 case K_PAGEUP:
1457 case K_KPAGEUP:
1458 case K_PAGEDOWN:
1459 case K_KPAGEDOWN:
1460 if (hislen == 0 || firstc == NUL) /* no history */
1461 goto cmdline_not_changed;
1462
1463 i = hiscnt;
1464
1465 /* save current command string so it can be restored later */
1466 if (lookfor == NULL)
1467 {
1468 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1469 goto cmdline_not_changed;
1470 lookfor[ccline.cmdpos] = NUL;
1471 }
1472
1473 j = (int)STRLEN(lookfor);
1474 for (;;)
1475 {
1476 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001477 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001478 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 {
1480 if (hiscnt == hislen) /* first time */
1481 hiscnt = hisidx[histype];
1482 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1483 hiscnt = hislen - 1;
1484 else if (hiscnt != hisidx[histype] + 1)
1485 --hiscnt;
1486 else /* at top of list */
1487 {
1488 hiscnt = i;
1489 break;
1490 }
1491 }
1492 else /* one step forwards */
1493 {
1494 /* on last entry, clear the line */
1495 if (hiscnt == hisidx[histype])
1496 {
1497 hiscnt = hislen;
1498 break;
1499 }
1500
1501 /* not on a history line, nothing to do */
1502 if (hiscnt == hislen)
1503 break;
1504 if (hiscnt == hislen - 1) /* wrap around */
1505 hiscnt = 0;
1506 else
1507 ++hiscnt;
1508 }
1509 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1510 {
1511 hiscnt = i;
1512 break;
1513 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001514 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001515 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 || STRNCMP(history[histype][hiscnt].hisstr,
1517 lookfor, (size_t)j) == 0)
1518 break;
1519 }
1520
1521 if (hiscnt != i) /* jumped to other entry */
1522 {
1523 char_u *p;
1524 int len;
1525 int old_firstc;
1526
1527 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001528 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 if (hiscnt == hislen)
1530 p = lookfor; /* back to the old one */
1531 else
1532 p = history[histype][hiscnt].hisstr;
1533
1534 if (histype == HIST_SEARCH
1535 && p != lookfor
1536 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1537 {
1538 /* Correct for the separator character used when
1539 * adding the history entry vs the one used now.
1540 * First loop: count length.
1541 * Second loop: copy the characters. */
1542 for (i = 0; i <= 1; ++i)
1543 {
1544 len = 0;
1545 for (j = 0; p[j] != NUL; ++j)
1546 {
1547 /* Replace old sep with new sep, unless it is
1548 * escaped. */
1549 if (p[j] == old_firstc
1550 && (j == 0 || p[j - 1] != '\\'))
1551 {
1552 if (i > 0)
1553 ccline.cmdbuff[len] = firstc;
1554 }
1555 else
1556 {
1557 /* Escape new sep, unless it is already
1558 * escaped. */
1559 if (p[j] == firstc
1560 && (j == 0 || p[j - 1] != '\\'))
1561 {
1562 if (i > 0)
1563 ccline.cmdbuff[len] = '\\';
1564 ++len;
1565 }
1566 if (i > 0)
1567 ccline.cmdbuff[len] = p[j];
1568 }
1569 ++len;
1570 }
1571 if (i == 0)
1572 {
1573 alloc_cmdbuff(len);
1574 if (ccline.cmdbuff == NULL)
1575 goto returncmd;
1576 }
1577 }
1578 ccline.cmdbuff[len] = NUL;
1579 }
1580 else
1581 {
1582 alloc_cmdbuff((int)STRLEN(p));
1583 if (ccline.cmdbuff == NULL)
1584 goto returncmd;
1585 STRCPY(ccline.cmdbuff, p);
1586 }
1587
1588 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1589 redrawcmd();
1590 goto cmdline_changed;
1591 }
1592 beep_flush();
1593 goto cmdline_not_changed;
1594#endif
1595
1596 case Ctrl_V:
1597 case Ctrl_Q:
1598#ifdef FEAT_MOUSE
1599 ignore_drag_release = TRUE;
1600#endif
1601 putcmdline('^', TRUE);
1602 c = get_literal(); /* get next (two) character(s) */
1603 do_abbr = FALSE; /* don't do abbreviation now */
1604#ifdef FEAT_MBYTE
1605 /* may need to remove ^ when composing char was typed */
1606 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1607 {
1608 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1609 msg_putchar(' ');
1610 cursorcmd();
1611 }
1612#endif
1613 break;
1614
1615#ifdef FEAT_DIGRAPHS
1616 case Ctrl_K:
1617#ifdef FEAT_MOUSE
1618 ignore_drag_release = TRUE;
1619#endif
1620 putcmdline('?', TRUE);
1621#ifdef USE_ON_FLY_SCROLL
1622 dont_scroll = TRUE; /* disallow scrolling here */
1623#endif
1624 c = get_digraph(TRUE);
1625 if (c != NUL)
1626 break;
1627
1628 redrawcmd();
1629 goto cmdline_not_changed;
1630#endif /* FEAT_DIGRAPHS */
1631
1632#ifdef FEAT_RIGHTLEFT
1633 case Ctrl__: /* CTRL-_: switch language mode */
1634 if (!p_ari)
1635 break;
1636#ifdef FEAT_FKMAP
1637 if (p_altkeymap)
1638 {
1639 cmd_fkmap = !cmd_fkmap;
1640 if (cmd_fkmap) /* in Farsi always in Insert mode */
1641 ccline.overstrike = FALSE;
1642 }
1643 else /* Hebrew is default */
1644#endif
1645 cmd_hkmap = !cmd_hkmap;
1646 goto cmdline_not_changed;
1647#endif
1648
1649 default:
1650#ifdef UNIX
1651 if (c == intr_char)
1652 {
1653 gotesc = TRUE; /* will free ccline.cmdbuff after
1654 putting it in history */
1655 goto returncmd; /* back to Normal mode */
1656 }
1657#endif
1658 /*
1659 * Normal character with no special meaning. Just set mod_mask
1660 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1661 * the string <S-Space>. This should only happen after ^V.
1662 */
1663 if (!IS_SPECIAL(c))
1664 mod_mask = 0x0;
1665 break;
1666 }
1667 /*
1668 * End of switch on command line character.
1669 * We come here if we have a normal character.
1670 */
1671
1672 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1673#ifdef FEAT_MBYTE
1674 /* Add ABBR_OFF for characters above 0x100, this is
1675 * what check_abbr() expects. */
1676 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1677#endif
1678 c))
1679 goto cmdline_changed;
1680
1681 /*
1682 * put the character in the command line
1683 */
1684 if (IS_SPECIAL(c) || mod_mask != 0)
1685 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1686 else
1687 {
1688#ifdef FEAT_MBYTE
1689 if (has_mbyte)
1690 {
1691 j = (*mb_char2bytes)(c, IObuff);
1692 IObuff[j] = NUL; /* exclude composing chars */
1693 put_on_cmdline(IObuff, j, TRUE);
1694 }
1695 else
1696#endif
1697 {
1698 IObuff[0] = c;
1699 put_on_cmdline(IObuff, 1, TRUE);
1700 }
1701 }
1702 goto cmdline_changed;
1703
1704/*
1705 * This part implements incremental searches for "/" and "?"
1706 * Jump to cmdline_not_changed when a character has been read but the command
1707 * line did not change. Then we only search and redraw if something changed in
1708 * the past.
1709 * Jump to cmdline_changed when the command line did change.
1710 * (Sorry for the goto's, I know it is ugly).
1711 */
1712cmdline_not_changed:
1713#ifdef FEAT_SEARCH_EXTRA
1714 if (!incsearch_postponed)
1715 continue;
1716#endif
1717
1718cmdline_changed:
1719#ifdef FEAT_SEARCH_EXTRA
1720 /*
1721 * 'incsearch' highlighting.
1722 */
1723 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1724 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001725 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001726#ifdef FEAT_RELTIME
1727 proftime_T tm;
1728#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001729
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 /* if there is a character waiting, search and redraw later */
1731 if (char_avail())
1732 {
1733 incsearch_postponed = TRUE;
1734 continue;
1735 }
1736 incsearch_postponed = FALSE;
1737 curwin->w_cursor = old_cursor; /* start at old position */
1738
1739 /* If there is no command line, don't do anything */
1740 if (ccline.cmdlen == 0)
1741 i = 0;
1742 else
1743 {
1744 cursor_off(); /* so the user knows we're busy */
1745 out_flush();
1746 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001747#ifdef FEAT_RELTIME
1748 /* Set the time limit to half a second. */
1749 profile_setlimit(500L, &tm);
1750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001752 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1753#ifdef FEAT_RELTIME
1754 &tm
1755#else
1756 NULL
1757#endif
1758 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 --emsg_off;
1760 /* if interrupted while searching, behave like it failed */
1761 if (got_int)
1762 {
1763 (void)vpeekc(); /* remove <C-C> from input stream */
1764 got_int = FALSE; /* don't abandon the command line */
1765 i = 0;
1766 }
1767 else if (char_avail())
1768 /* cancelled searching because a char was typed */
1769 incsearch_postponed = TRUE;
1770 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001771 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 highlight_match = TRUE; /* highlight position */
1773 else
1774 highlight_match = FALSE; /* remove highlight */
1775
1776 /* first restore the old curwin values, so the screen is
1777 * positioned in the same way as the actual search command */
1778 curwin->w_leftcol = old_leftcol;
1779 curwin->w_topline = old_topline;
1780# ifdef FEAT_DIFF
1781 curwin->w_topfill = old_topfill;
1782# endif
1783 curwin->w_botline = old_botline;
1784 changed_cline_bef_curs();
1785 update_topline();
1786
1787 if (i != 0)
1788 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001789 pos_T save_pos = curwin->w_cursor;
1790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001792 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 * moves the whole match onto the screen when 'nowrap' is set.
1794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 curwin->w_cursor.lnum += search_match_lines;
1796 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001797 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1798 {
1799 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1800 coladvance((colnr_T)MAXCOL);
1801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001803 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001804 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001806 else
1807 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001810# ifdef FEAT_WINDOWS
1811 /* May redraw the status line to show the cursor position. */
1812 if (p_ru && curwin->w_status_height > 0)
1813 curwin->w_redr_status = TRUE;
1814# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001816 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001817 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001818 restore_cmdline(&save_ccline);
1819
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001820 /* Leave it at the end to make CTRL-R CTRL-W work. */
1821 if (i != 0)
1822 curwin->w_cursor = end_pos;
1823
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 msg_starthere();
1825 redrawcmdline();
1826 did_incsearch = TRUE;
1827 }
1828#else /* FEAT_SEARCH_EXTRA */
1829 ;
1830#endif
1831
1832#ifdef FEAT_RIGHTLEFT
1833 if (cmdmsg_rl
1834# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001835 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836# endif
1837 )
1838 /* Always redraw the whole command line to fix shaping and
1839 * right-left typing. Not efficient, but it works. */
1840 redrawcmd();
1841#endif
1842 }
1843
1844returncmd:
1845
1846#ifdef FEAT_RIGHTLEFT
1847 cmdmsg_rl = FALSE;
1848#endif
1849
1850#ifdef FEAT_FKMAP
1851 cmd_fkmap = 0;
1852#endif
1853
1854 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001855 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
1857#ifdef FEAT_SEARCH_EXTRA
1858 if (did_incsearch)
1859 {
1860 curwin->w_cursor = old_cursor;
1861 curwin->w_curswant = old_curswant;
1862 curwin->w_leftcol = old_leftcol;
1863 curwin->w_topline = old_topline;
1864# ifdef FEAT_DIFF
1865 curwin->w_topfill = old_topfill;
1866# endif
1867 curwin->w_botline = old_botline;
1868 highlight_match = FALSE;
1869 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001870 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872#endif
1873
1874 if (ccline.cmdbuff != NULL)
1875 {
1876 /*
1877 * Put line in history buffer (":" and "=" only when it was typed).
1878 */
1879#ifdef FEAT_CMDHIST
1880 if (ccline.cmdlen && firstc != NUL
1881 && (some_key_typed || histype == HIST_SEARCH))
1882 {
1883 add_to_history(histype, ccline.cmdbuff, TRUE,
1884 histype == HIST_SEARCH ? firstc : NUL);
1885 if (firstc == ':')
1886 {
1887 vim_free(new_last_cmdline);
1888 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1889 }
1890 }
1891#endif
1892
1893 if (gotesc) /* abandon command line */
1894 {
1895 vim_free(ccline.cmdbuff);
1896 ccline.cmdbuff = NULL;
1897 if (msg_scrolled == 0)
1898 compute_cmdrow();
1899 MSG("");
1900 redraw_cmdline = TRUE;
1901 }
1902 }
1903
1904 /*
1905 * If the screen was shifted up, redraw the whole screen (later).
1906 * If the line is too long, clear it, so ruler and shown command do
1907 * not get printed in the middle of it.
1908 */
1909 msg_check();
1910 msg_scroll = save_msg_scroll;
1911 redir_off = FALSE;
1912
1913 /* When the command line was typed, no need for a wait-return prompt. */
1914 if (some_key_typed)
1915 need_wait_return = FALSE;
1916
1917 State = save_State;
1918#ifdef USE_IM_CONTROL
1919 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1920 im_save_status(b_im_ptr);
1921 im_set_active(FALSE);
1922#endif
1923#ifdef FEAT_MOUSE
1924 setmouse();
1925#endif
1926#ifdef CURSOR_SHAPE
1927 ui_cursor_shape(); /* may show different cursor shape */
1928#endif
1929
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001930 {
1931 char_u *p = ccline.cmdbuff;
1932
1933 /* Make ccline empty, getcmdline() may try to use it. */
1934 ccline.cmdbuff = NULL;
1935 return p;
1936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937}
1938
1939#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1940/*
1941 * Get a command line with a prompt.
1942 * This is prepared to be called recursively from getcmdline() (e.g. by
1943 * f_input() when evaluating an expression from CTRL-R =).
1944 * Returns the command line in allocated memory, or NULL.
1945 */
1946 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001947getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 int firstc;
1949 char_u *prompt; /* command line prompt */
1950 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001951 int xp_context; /* type of expansion */
1952 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953{
1954 char_u *s;
1955 struct cmdline_info save_ccline;
1956 int msg_col_save = msg_col;
1957
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001958 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 ccline.cmdprompt = prompt;
1960 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001961# ifdef FEAT_EVAL
1962 ccline.xp_context = xp_context;
1963 ccline.xp_arg = xp_arg;
1964 ccline.input_fn = (firstc == '@');
1965# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001967 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 /* Restore msg_col, the prompt from input() may have changed it. */
1969 msg_col = msg_col_save;
1970
1971 return s;
1972}
1973#endif
1974
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001975/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001976 * Return TRUE when the text must not be changed and we can't switch to
1977 * another window or buffer. Used when editing the command line, evaluating
1978 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001979 */
1980 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001981text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001982{
1983#ifdef FEAT_CMDWIN
1984 if (cmdwin_type != 0)
1985 return TRUE;
1986#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001987 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001988}
1989
1990/*
1991 * Give an error message for a command that isn't allowed while the cmdline
1992 * window is open or editing the cmdline in another way.
1993 */
1994 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001995text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001996{
1997#ifdef FEAT_CMDWIN
1998 if (cmdwin_type != 0)
1999 EMSG(_(e_cmdwin));
2000 else
2001#endif
2002 EMSG(_(e_secure));
2003}
2004
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002005#if defined(FEAT_AUTOCMD) || defined(PROTO)
2006/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002007 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2008 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002009 */
2010 int
2011curbuf_locked()
2012{
2013 if (curbuf_lock > 0)
2014 {
2015 EMSG(_("E788: Not allowed to edit another buffer now"));
2016 return TRUE;
2017 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002018 return allbuf_locked();
2019}
2020
2021/*
2022 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2023 * message.
2024 */
2025 int
2026allbuf_locked()
2027{
2028 if (allbuf_lock > 0)
2029 {
2030 EMSG(_("E811: Not allowed to change buffer information now"));
2031 return TRUE;
2032 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002033 return FALSE;
2034}
2035#endif
2036
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 static int
2038cmdline_charsize(idx)
2039 int idx;
2040{
2041#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2042 if (cmdline_star > 0) /* showing '*', always 1 position */
2043 return 1;
2044#endif
2045 return ptr2cells(ccline.cmdbuff + idx);
2046}
2047
2048/*
2049 * Compute the offset of the cursor on the command line for the prompt and
2050 * indent.
2051 */
2052 static void
2053set_cmdspos()
2054{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002055 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 ccline.cmdspos = 1 + ccline.cmdindent;
2057 else
2058 ccline.cmdspos = 0 + ccline.cmdindent;
2059}
2060
2061/*
2062 * Compute the screen position for the cursor on the command line.
2063 */
2064 static void
2065set_cmdspos_cursor()
2066{
2067 int i, m, c;
2068
2069 set_cmdspos();
2070 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002071 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002073 if (m < 0) /* overflow, Columns or Rows at weird value */
2074 m = MAXCOL;
2075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 else
2077 m = MAXCOL;
2078 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2079 {
2080 c = cmdline_charsize(i);
2081#ifdef FEAT_MBYTE
2082 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2083 if (has_mbyte)
2084 correct_cmdspos(i, c);
2085#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002086 /* If the cmdline doesn't fit, show cursor on last visible char.
2087 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 if ((ccline.cmdspos += c) >= m)
2089 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 ccline.cmdspos -= c;
2091 break;
2092 }
2093#ifdef FEAT_MBYTE
2094 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002095 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096#endif
2097 }
2098}
2099
2100#ifdef FEAT_MBYTE
2101/*
2102 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2103 * character that doesn't fit, so that a ">" must be displayed.
2104 */
2105 static void
2106correct_cmdspos(idx, cells)
2107 int idx;
2108 int cells;
2109{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002110 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2112 && ccline.cmdspos % Columns + cells > Columns)
2113 ccline.cmdspos++;
2114}
2115#endif
2116
2117/*
2118 * Get an Ex command line for the ":" command.
2119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002121getexline(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 int c; /* normally ':', NUL for ":append" */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002123 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 int indent; /* indent for inside conditionals */
2125{
2126 /* When executing a register, remove ':' that's in front of each line. */
2127 if (exec_from_reg && vpeekc() == ':')
2128 (void)vgetc();
2129 return getcmdline(c, 1L, indent);
2130}
2131
2132/*
2133 * Get an Ex command line for Ex mode.
2134 * In Ex mode we only use the OS supplied line editing features and no
2135 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002136 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002139getexmodeline(promptc, cookie, indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002140 int promptc; /* normally ':', NUL for ":append" and '?' for
2141 :s prompt */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002142 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 int indent; /* indent for inside conditionals */
2144{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002145 garray_T line_ga;
2146 char_u *pend;
2147 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002148 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002149 int escaped = FALSE; /* CTRL-V typed */
2150 int vcol = 0;
2151 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002152 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154 /* Switch cursor on now. This avoids that it happens after the "\n", which
2155 * confuses the system function that computes tabstops. */
2156 cursor_on();
2157
2158 /* always start in column 0; write a newline if necessary */
2159 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002160 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002162 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002164 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002165 if (p_prompt)
2166 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 while (indent-- > 0)
2168 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
2171
2172 ga_init2(&line_ga, 1, 30);
2173
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002174 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002175 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002176 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002177 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002178 while (indent >= 8)
2179 {
2180 ga_append(&line_ga, TAB);
2181 msg_puts((char_u *)" ");
2182 indent -= 8;
2183 }
2184 while (indent-- > 0)
2185 {
2186 ga_append(&line_ga, ' ');
2187 msg_putchar(' ');
2188 }
2189 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002190 ++no_mapping;
2191 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002192
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 /*
2194 * Get the line, one character at a time.
2195 */
2196 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002197 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
2199 if (ga_grow(&line_ga, 40) == FAIL)
2200 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002202 /* Get one character at a time. Don't use inchar(), it can't handle
2203 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002204 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002205 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206
2207 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002208 * Handle line editing.
2209 * Previously this was left to the system, putting the terminal in
2210 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002212 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002214 msg_putchar('\n');
2215 break;
2216 }
2217
2218 if (!escaped)
2219 {
2220 /* CR typed means "enter", which is NL */
2221 if (c1 == '\r')
2222 c1 = '\n';
2223
2224 if (c1 == BS || c1 == K_BS
2225 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002227 if (line_ga.ga_len > 0)
2228 {
2229 --line_ga.ga_len;
2230 goto redraw;
2231 }
2232 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 }
2234
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002235 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002237 msg_col = startcol;
2238 msg_clr_eos();
2239 line_ga.ga_len = 0;
2240 continue;
2241 }
2242
2243 if (c1 == Ctrl_T)
2244 {
2245 p = (char_u *)line_ga.ga_data;
2246 p[line_ga.ga_len] = NUL;
2247 indent = get_indent_str(p, 8);
2248 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2249add_indent:
2250 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002252 char_u *s = skipwhite(p);
2253
2254 ga_grow(&line_ga, 1);
2255 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2256 *s = ' ';
2257 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002259redraw:
2260 /* redraw the line */
2261 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002262 vcol = 0;
2263 for (p = (char_u *)line_ga.ga_data;
2264 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002266 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002268 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002270 msg_putchar(' ');
2271 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002273 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002275 msg_outtrans_len(p, 1);
2276 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 }
2278 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002279 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002280 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002281 continue;
2282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002284 if (c1 == Ctrl_D)
2285 {
2286 /* Delete one shiftwidth. */
2287 p = (char_u *)line_ga.ga_data;
2288 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002290 if (prev_char == '^')
2291 ex_keep_indent = TRUE;
2292 indent = 0;
2293 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295 else
2296 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002297 p[line_ga.ga_len] = NUL;
2298 indent = get_indent_str(p, 8);
2299 --indent;
2300 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002302 while (get_indent_str(p, 8) > indent)
2303 {
2304 char_u *s = skipwhite(p);
2305
2306 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2307 --line_ga.ga_len;
2308 }
2309 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002311
2312 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2313 {
2314 escaped = TRUE;
2315 continue;
2316 }
2317
2318 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2319 if (IS_SPECIAL(c1))
2320 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002322
2323 if (IS_SPECIAL(c1))
2324 c1 = '?';
2325 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002326 if (c1 == '\n')
2327 msg_putchar('\n');
2328 else if (c1 == TAB)
2329 {
2330 /* Don't use chartabsize(), 'ts' can be different */
2331 do
2332 {
2333 msg_putchar(' ');
2334 } while (++vcol % 8);
2335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002338 msg_outtrans_len(
2339 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2340 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002342 ++line_ga.ga_len;
2343 escaped = FALSE;
2344
2345 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002346 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002347
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002348 /* We are done when a NL is entered, but not when it comes after an
2349 * odd number of backslashes, that results in a NUL. */
2350 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002352 int bcount = 0;
2353
2354 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2355 ++bcount;
2356
2357 if (bcount > 0)
2358 {
2359 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2360 * "\NL", etc. */
2361 line_ga.ga_len -= (bcount + 1) / 2;
2362 pend -= (bcount + 1) / 2;
2363 pend[-1] = '\n';
2364 }
2365
2366 if ((bcount & 1) == 0)
2367 {
2368 --line_ga.ga_len;
2369 --pend;
2370 *pend = NUL;
2371 break;
2372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
2374 }
2375
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002376 --no_mapping;
2377 --allow_keys;
2378
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 /* make following messages go to the next line */
2380 msg_didout = FALSE;
2381 msg_col = 0;
2382 if (msg_row < Rows - 1)
2383 ++msg_row;
2384 emsg_on_display = FALSE; /* don't want ui_delay() */
2385
2386 if (got_int)
2387 ga_clear(&line_ga);
2388
2389 return (char_u *)line_ga.ga_data;
2390}
2391
Bram Moolenaare344bea2005-09-01 20:46:49 +00002392# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2393 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394/*
2395 * Return TRUE if ccline.overstrike is on.
2396 */
2397 int
2398cmdline_overstrike()
2399{
2400 return ccline.overstrike;
2401}
2402
2403/*
2404 * Return TRUE if the cursor is at the end of the cmdline.
2405 */
2406 int
2407cmdline_at_end()
2408{
2409 return (ccline.cmdpos >= ccline.cmdlen);
2410}
2411#endif
2412
Bram Moolenaar9372a112005-12-06 19:59:18 +00002413#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414/*
2415 * Return the virtual column number at the current cursor position.
2416 * This is used by the IM code to obtain the start of the preedit string.
2417 */
2418 colnr_T
2419cmdline_getvcol_cursor()
2420{
2421 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2422 return MAXCOL;
2423
2424# ifdef FEAT_MBYTE
2425 if (has_mbyte)
2426 {
2427 colnr_T col;
2428 int i = 0;
2429
2430 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002431 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432
2433 return col;
2434 }
2435 else
2436# endif
2437 return ccline.cmdpos;
2438}
2439#endif
2440
2441#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2442/*
2443 * If part of the command line is an IM preedit string, redraw it with
2444 * IM feedback attributes. The cursor position is restored after drawing.
2445 */
2446 static void
2447redrawcmd_preedit()
2448{
2449 if ((State & CMDLINE)
2450 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002451 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 && !p_imdisable
2453 && im_is_preediting())
2454 {
2455 int cmdpos = 0;
2456 int cmdspos;
2457 int old_row;
2458 int old_col;
2459 colnr_T col;
2460
2461 old_row = msg_row;
2462 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002463 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
2465# ifdef FEAT_MBYTE
2466 if (has_mbyte)
2467 {
2468 for (col = 0; col < preedit_start_col
2469 && cmdpos < ccline.cmdlen; ++col)
2470 {
2471 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002472 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 }
2474 }
2475 else
2476# endif
2477 {
2478 cmdspos += preedit_start_col;
2479 cmdpos += preedit_start_col;
2480 }
2481
2482 msg_row = cmdline_row + (cmdspos / (int)Columns);
2483 msg_col = cmdspos % (int)Columns;
2484 if (msg_row >= Rows)
2485 msg_row = Rows - 1;
2486
2487 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2488 {
2489 int char_len;
2490 int char_attr;
2491
2492 char_attr = im_get_feedback_attr(col);
2493 if (char_attr < 0)
2494 break; /* end of preedit string */
2495
2496# ifdef FEAT_MBYTE
2497 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002498 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 else
2500# endif
2501 char_len = 1;
2502
2503 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2504 cmdpos += char_len;
2505 }
2506
2507 msg_row = old_row;
2508 msg_col = old_col;
2509 }
2510}
2511#endif /* FEAT_XIM && FEAT_GUI_GTK */
2512
2513/*
2514 * Allocate a new command line buffer.
2515 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2516 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2517 */
2518 static void
2519alloc_cmdbuff(len)
2520 int len;
2521{
2522 /*
2523 * give some extra space to avoid having to allocate all the time
2524 */
2525 if (len < 80)
2526 len = 100;
2527 else
2528 len += 20;
2529
2530 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2531 ccline.cmdbufflen = len;
2532}
2533
2534/*
2535 * Re-allocate the command line to length len + something extra.
2536 * return FAIL for failure, OK otherwise
2537 */
2538 static int
2539realloc_cmdbuff(len)
2540 int len;
2541{
2542 char_u *p;
2543
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002544 if (len < ccline.cmdbufflen)
2545 return OK; /* no need to resize */
2546
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 p = ccline.cmdbuff;
2548 alloc_cmdbuff(len); /* will get some more */
2549 if (ccline.cmdbuff == NULL) /* out of memory */
2550 {
2551 ccline.cmdbuff = p; /* keep the old one */
2552 return FAIL;
2553 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002554 /* There isn't always a NUL after the command, but it may need to be
2555 * there, thus copy up to the NUL and add a NUL. */
2556 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2557 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002559
2560 if (ccline.xpc != NULL
2561 && ccline.xpc->xp_pattern != NULL
2562 && ccline.xpc->xp_context != EXPAND_NOTHING
2563 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2564 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002565 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002566
2567 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2568 * to point into the newly allocated memory. */
2569 if (i >= 0 && i <= ccline.cmdlen)
2570 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2571 }
2572
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 return OK;
2574}
2575
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002576#if defined(FEAT_ARABIC) || defined(PROTO)
2577static char_u *arshape_buf = NULL;
2578
2579# if defined(EXITFREE) || defined(PROTO)
2580 void
2581free_cmdline_buf()
2582{
2583 vim_free(arshape_buf);
2584}
2585# endif
2586#endif
2587
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588/*
2589 * Draw part of the cmdline at the current cursor position. But draw stars
2590 * when cmdline_star is TRUE.
2591 */
2592 static void
2593draw_cmdline(start, len)
2594 int start;
2595 int len;
2596{
2597#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2598 int i;
2599
2600 if (cmdline_star > 0)
2601 for (i = 0; i < len; ++i)
2602 {
2603 msg_putchar('*');
2604# ifdef FEAT_MBYTE
2605 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002606 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607# endif
2608 }
2609 else
2610#endif
2611#ifdef FEAT_ARABIC
2612 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2613 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 static int buflen = 0;
2615 char_u *p;
2616 int j;
2617 int newlen = 0;
2618 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002619 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 int prev_c = 0;
2621 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002622 int u8c;
2623 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625
2626 /*
2627 * Do arabic shaping into a temporary buffer. This is very
2628 * inefficient!
2629 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002630 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
2632 /* Re-allocate the buffer. We keep it around to avoid a lot of
2633 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002634 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002635 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002636 arshape_buf = alloc(buflen);
2637 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 return; /* out of memory */
2639 }
2640
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002641 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2642 {
2643 /* Prepend a space to draw the leading composing char on. */
2644 arshape_buf[0] = ' ';
2645 newlen = 1;
2646 }
2647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 for (j = start; j < start + len; j += mb_l)
2649 {
2650 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002651 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002652 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if (ARABIC_CHAR(u8c))
2654 {
2655 /* Do Arabic shaping. */
2656 if (cmdmsg_rl)
2657 {
2658 /* displaying from right to left */
2659 pc = prev_c;
2660 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002661 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (j + mb_l >= start + len)
2663 nc = NUL;
2664 else
2665 nc = utf_ptr2char(p + mb_l);
2666 }
2667 else
2668 {
2669 /* displaying from left to right */
2670 if (j + mb_l >= start + len)
2671 pc = NUL;
2672 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002673 {
2674 int pcc[MAX_MCO];
2675
2676 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002678 pc1 = pcc[0];
2679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 nc = prev_c;
2681 }
2682 prev_c = u8c;
2683
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002684 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002686 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002687 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002689 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2690 if (u8cc[1] != 0)
2691 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002692 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 }
2694 }
2695 else
2696 {
2697 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002698 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 newlen += mb_l;
2700 }
2701 }
2702
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002703 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 }
2705 else
2706#endif
2707 msg_outtrans_len(ccline.cmdbuff + start, len);
2708}
2709
2710/*
2711 * Put a character on the command line. Shifts the following text to the
2712 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2713 * "c" must be printable (fit in one display cell)!
2714 */
2715 void
2716putcmdline(c, shift)
2717 int c;
2718 int shift;
2719{
2720 if (cmd_silent)
2721 return;
2722 msg_no_more = TRUE;
2723 msg_putchar(c);
2724 if (shift)
2725 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2726 msg_no_more = FALSE;
2727 cursorcmd();
2728}
2729
2730/*
2731 * Undo a putcmdline(c, FALSE).
2732 */
2733 void
2734unputcmdline()
2735{
2736 if (cmd_silent)
2737 return;
2738 msg_no_more = TRUE;
2739 if (ccline.cmdlen == ccline.cmdpos)
2740 msg_putchar(' ');
2741 else
2742 draw_cmdline(ccline.cmdpos, 1);
2743 msg_no_more = FALSE;
2744 cursorcmd();
2745}
2746
2747/*
2748 * Put the given string, of the given length, onto the command line.
2749 * If len is -1, then STRLEN() is used to calculate the length.
2750 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2751 * part will be redrawn, otherwise it will not. If this function is called
2752 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2753 * called afterwards.
2754 */
2755 int
2756put_on_cmdline(str, len, redraw)
2757 char_u *str;
2758 int len;
2759 int redraw;
2760{
2761 int retval;
2762 int i;
2763 int m;
2764 int c;
2765
2766 if (len < 0)
2767 len = (int)STRLEN(str);
2768
2769 /* Check if ccline.cmdbuff needs to be longer */
2770 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002771 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 else
2773 retval = OK;
2774 if (retval == OK)
2775 {
2776 if (!ccline.overstrike)
2777 {
2778 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2779 ccline.cmdbuff + ccline.cmdpos,
2780 (size_t)(ccline.cmdlen - ccline.cmdpos));
2781 ccline.cmdlen += len;
2782 }
2783 else
2784 {
2785#ifdef FEAT_MBYTE
2786 if (has_mbyte)
2787 {
2788 /* Count nr of characters in the new string. */
2789 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002790 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 ++m;
2792 /* Count nr of bytes in cmdline that are overwritten by these
2793 * characters. */
2794 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002795 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 --m;
2797 if (i < ccline.cmdlen)
2798 {
2799 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2800 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2801 ccline.cmdlen += ccline.cmdpos + len - i;
2802 }
2803 else
2804 ccline.cmdlen = ccline.cmdpos + len;
2805 }
2806 else
2807#endif
2808 if (ccline.cmdpos + len > ccline.cmdlen)
2809 ccline.cmdlen = ccline.cmdpos + len;
2810 }
2811 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2812 ccline.cmdbuff[ccline.cmdlen] = NUL;
2813
2814#ifdef FEAT_MBYTE
2815 if (enc_utf8)
2816 {
2817 /* When the inserted text starts with a composing character,
2818 * backup to the character before it. There could be two of them.
2819 */
2820 i = 0;
2821 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2822 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2823 {
2824 i = (*mb_head_off)(ccline.cmdbuff,
2825 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2826 ccline.cmdpos -= i;
2827 len += i;
2828 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2829 }
2830# ifdef FEAT_ARABIC
2831 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2832 {
2833 /* Check the previous character for Arabic combining pair. */
2834 i = (*mb_head_off)(ccline.cmdbuff,
2835 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2836 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2837 + ccline.cmdpos - i), c))
2838 {
2839 ccline.cmdpos -= i;
2840 len += i;
2841 }
2842 else
2843 i = 0;
2844 }
2845# endif
2846 if (i != 0)
2847 {
2848 /* Also backup the cursor position. */
2849 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2850 ccline.cmdspos -= i;
2851 msg_col -= i;
2852 if (msg_col < 0)
2853 {
2854 msg_col += Columns;
2855 --msg_row;
2856 }
2857 }
2858 }
2859#endif
2860
2861 if (redraw && !cmd_silent)
2862 {
2863 msg_no_more = TRUE;
2864 i = cmdline_row;
2865 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2866 /* Avoid clearing the rest of the line too often. */
2867 if (cmdline_row != i || ccline.overstrike)
2868 msg_clr_eos();
2869 msg_no_more = FALSE;
2870 }
2871#ifdef FEAT_FKMAP
2872 /*
2873 * If we are in Farsi command mode, the character input must be in
2874 * Insert mode. So do not advance the cmdpos.
2875 */
2876 if (!cmd_fkmap)
2877#endif
2878 {
2879 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002880 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002882 if (m < 0) /* overflow, Columns or Rows at weird value */
2883 m = MAXCOL;
2884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 else
2886 m = MAXCOL;
2887 for (i = 0; i < len; ++i)
2888 {
2889 c = cmdline_charsize(ccline.cmdpos);
2890#ifdef FEAT_MBYTE
2891 /* count ">" for a double-wide char that doesn't fit. */
2892 if (has_mbyte)
2893 correct_cmdspos(ccline.cmdpos, c);
2894#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002895 /* Stop cursor at the end of the screen, but do increment the
2896 * insert position, so that entering a very long command
2897 * works, even though you can't see it. */
2898 if (ccline.cmdspos + c < m)
2899 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900#ifdef FEAT_MBYTE
2901 if (has_mbyte)
2902 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002903 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 if (c > len - i - 1)
2905 c = len - i - 1;
2906 ccline.cmdpos += c;
2907 i += c;
2908 }
2909#endif
2910 ++ccline.cmdpos;
2911 }
2912 }
2913 }
2914 if (redraw)
2915 msg_check();
2916 return retval;
2917}
2918
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002919static struct cmdline_info prev_ccline;
2920static int prev_ccline_used = FALSE;
2921
2922/*
2923 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2924 * and overwrite it. But get_cmdline_str() may need it, thus make it
2925 * available globally in prev_ccline.
2926 */
2927 static void
2928save_cmdline(ccp)
2929 struct cmdline_info *ccp;
2930{
2931 if (!prev_ccline_used)
2932 {
2933 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2934 prev_ccline_used = TRUE;
2935 }
2936 *ccp = prev_ccline;
2937 prev_ccline = ccline;
2938 ccline.cmdbuff = NULL;
2939 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002940 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002941}
2942
2943/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002944 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002945 */
2946 static void
2947restore_cmdline(ccp)
2948 struct cmdline_info *ccp;
2949{
2950 ccline = prev_ccline;
2951 prev_ccline = *ccp;
2952}
2953
Bram Moolenaar5a305422006-04-28 22:38:25 +00002954#if defined(FEAT_EVAL) || defined(PROTO)
2955/*
2956 * Save the command line into allocated memory. Returns a pointer to be
2957 * passed to restore_cmdline_alloc() later.
2958 * Returns NULL when failed.
2959 */
2960 char_u *
2961save_cmdline_alloc()
2962{
2963 struct cmdline_info *p;
2964
2965 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2966 if (p != NULL)
2967 save_cmdline(p);
2968 return (char_u *)p;
2969}
2970
2971/*
2972 * Restore the command line from the return value of save_cmdline_alloc().
2973 */
2974 void
2975restore_cmdline_alloc(p)
2976 char_u *p;
2977{
2978 if (p != NULL)
2979 {
2980 restore_cmdline((struct cmdline_info *)p);
2981 vim_free(p);
2982 }
2983}
2984#endif
2985
Bram Moolenaar8299df92004-07-10 09:47:34 +00002986/*
2987 * paste a yank register into the command line.
2988 * used by CTRL-R command in command-line mode
2989 * insert_reg() can't be used here, because special characters from the
2990 * register contents will be interpreted as commands.
2991 *
2992 * return FAIL for failure, OK otherwise
2993 */
2994 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002995cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00002996 int regname;
2997 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002998 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00002999{
3000 long i;
3001 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003002 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003003 int allocated;
3004 struct cmdline_info save_ccline;
3005
3006 /* check for valid regname; also accept special characters for CTRL-R in
3007 * the command line */
3008 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3009 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3010 return FAIL;
3011
3012 /* A register containing CTRL-R can cause an endless loop. Allow using
3013 * CTRL-C to break the loop. */
3014 line_breakcheck();
3015 if (got_int)
3016 return FAIL;
3017
3018#ifdef FEAT_CLIPBOARD
3019 regname = may_get_selection(regname);
3020#endif
3021
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003022 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003023 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003024 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003025 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003026 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003027 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003028 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003029
3030 if (i)
3031 {
3032 /* Got the value of a special register in "arg". */
3033 if (arg == NULL)
3034 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003035
3036 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3037 * part of the word. */
3038 p = arg;
3039 if (p_is && regname == Ctrl_W)
3040 {
3041 char_u *w;
3042 int len;
3043
3044 /* Locate start of last word in the cmd buffer. */
3045 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
3046 {
3047#ifdef FEAT_MBYTE
3048 if (has_mbyte)
3049 {
3050 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3051 if (!vim_iswordc(mb_ptr2char(w - len)))
3052 break;
3053 w -= len;
3054 }
3055 else
3056#endif
3057 {
3058 if (!vim_iswordc(w[-1]))
3059 break;
3060 --w;
3061 }
3062 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003063 len = (int)((ccline.cmdbuff + ccline.cmdlen) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003064 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3065 p += len;
3066 }
3067
3068 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003069 if (allocated)
3070 vim_free(arg);
3071 return OK;
3072 }
3073
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003074 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003075}
3076
3077/*
3078 * Put a string on the command line.
3079 * When "literally" is TRUE, insert literally.
3080 * When "literally" is FALSE, insert as typed, but don't leave the command
3081 * line.
3082 */
3083 void
3084cmdline_paste_str(s, literally)
3085 char_u *s;
3086 int literally;
3087{
3088 int c, cv;
3089
3090 if (literally)
3091 put_on_cmdline(s, -1, TRUE);
3092 else
3093 while (*s != NUL)
3094 {
3095 cv = *s;
3096 if (cv == Ctrl_V && s[1])
3097 ++s;
3098#ifdef FEAT_MBYTE
3099 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003100 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003101 else
3102#endif
3103 c = *s++;
3104 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3105#ifdef UNIX
3106 || c == intr_char
3107#endif
3108 || (c == Ctrl_BSL && *s == Ctrl_N))
3109 stuffcharReadbuff(Ctrl_V);
3110 stuffcharReadbuff(c);
3111 }
3112}
3113
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114#ifdef FEAT_WILDMENU
3115/*
3116 * Delete characters on the command line, from "from" to the current
3117 * position.
3118 */
3119 static void
3120cmdline_del(from)
3121 int from;
3122{
3123 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3124 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3125 ccline.cmdlen -= ccline.cmdpos - from;
3126 ccline.cmdpos = from;
3127}
3128#endif
3129
3130/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003131 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 * search
3133 */
3134 void
3135redrawcmdline()
3136{
3137 if (cmd_silent)
3138 return;
3139 need_wait_return = FALSE;
3140 compute_cmdrow();
3141 redrawcmd();
3142 cursorcmd();
3143}
3144
3145 static void
3146redrawcmdprompt()
3147{
3148 int i;
3149
3150 if (cmd_silent)
3151 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003152 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 msg_putchar(ccline.cmdfirstc);
3154 if (ccline.cmdprompt != NULL)
3155 {
3156 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3157 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3158 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003159 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 --ccline.cmdindent;
3161 }
3162 else
3163 for (i = ccline.cmdindent; i > 0; --i)
3164 msg_putchar(' ');
3165}
3166
3167/*
3168 * Redraw what is currently on the command line.
3169 */
3170 void
3171redrawcmd()
3172{
3173 if (cmd_silent)
3174 return;
3175
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003176 /* when 'incsearch' is set there may be no command line while redrawing */
3177 if (ccline.cmdbuff == NULL)
3178 {
3179 windgoto(cmdline_row, 0);
3180 msg_clr_eos();
3181 return;
3182 }
3183
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 msg_start();
3185 redrawcmdprompt();
3186
3187 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3188 msg_no_more = TRUE;
3189 draw_cmdline(0, ccline.cmdlen);
3190 msg_clr_eos();
3191 msg_no_more = FALSE;
3192
3193 set_cmdspos_cursor();
3194
3195 /*
3196 * An emsg() before may have set msg_scroll. This is used in normal mode,
3197 * in cmdline mode we can reset them now.
3198 */
3199 msg_scroll = FALSE; /* next message overwrites cmdline */
3200
3201 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3202 * in cmdline mode */
3203 skip_redraw = FALSE;
3204}
3205
3206 void
3207compute_cmdrow()
3208{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003209 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 cmdline_row = Rows - 1;
3211 else
3212 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3213 + W_STATUS_HEIGHT(lastwin);
3214}
3215
3216 static void
3217cursorcmd()
3218{
3219 if (cmd_silent)
3220 return;
3221
3222#ifdef FEAT_RIGHTLEFT
3223 if (cmdmsg_rl)
3224 {
3225 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3226 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3227 if (msg_row <= 0)
3228 msg_row = Rows - 1;
3229 }
3230 else
3231#endif
3232 {
3233 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3234 msg_col = ccline.cmdspos % (int)Columns;
3235 if (msg_row >= Rows)
3236 msg_row = Rows - 1;
3237 }
3238
3239 windgoto(msg_row, msg_col);
3240#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3241 redrawcmd_preedit();
3242#endif
3243#ifdef MCH_CURSOR_SHAPE
3244 mch_update_cursor();
3245#endif
3246}
3247
3248 void
3249gotocmdline(clr)
3250 int clr;
3251{
3252 msg_start();
3253#ifdef FEAT_RIGHTLEFT
3254 if (cmdmsg_rl)
3255 msg_col = Columns - 1;
3256 else
3257#endif
3258 msg_col = 0; /* always start in column 0 */
3259 if (clr) /* clear the bottom line(s) */
3260 msg_clr_eos(); /* will reset clear_cmdline */
3261 windgoto(cmdline_row, 0);
3262}
3263
3264/*
3265 * Check the word in front of the cursor for an abbreviation.
3266 * Called when the non-id character "c" has been entered.
3267 * When an abbreviation is recognized it is removed from the text with
3268 * backspaces and the replacement string is inserted, followed by "c".
3269 */
3270 static int
3271ccheck_abbr(c)
3272 int c;
3273{
3274 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3275 return FALSE;
3276
3277 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3278}
3279
3280/*
3281 * Return FAIL if this is not an appropriate context in which to do
3282 * completion of anything, return OK if it is (even if there are no matches).
3283 * For the caller, this means that the character is just passed through like a
3284 * normal character (instead of being expanded). This allows :s/^I^D etc.
3285 */
3286 static int
3287nextwild(xp, type, options)
3288 expand_T *xp;
3289 int type;
3290 int options; /* extra options for ExpandOne() */
3291{
3292 int i, j;
3293 char_u *p1;
3294 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 int difflen;
3296 int v;
3297
3298 if (xp->xp_numfiles == -1)
3299 {
3300 set_expand_context(xp);
3301 cmd_showtail = expand_showtail(xp);
3302 }
3303
3304 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3305 {
3306 beep_flush();
3307 return OK; /* Something illegal on command line */
3308 }
3309 if (xp->xp_context == EXPAND_NOTHING)
3310 {
3311 /* Caller can use the character as a normal char instead */
3312 return FAIL;
3313 }
3314
3315 MSG_PUTS("..."); /* show that we are busy */
3316 out_flush();
3317
3318 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003319 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
3321 if (type == WILD_NEXT || type == WILD_PREV)
3322 {
3323 /*
3324 * Get next/previous match for a previous expanded pattern.
3325 */
3326 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3327 }
3328 else
3329 {
3330 /*
3331 * Translate string into pattern and expand it.
3332 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003333 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3334 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 p2 = NULL;
3336 else
3337 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003338 p2 = ExpandOne(xp, p1,
3339 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3341 |options, type);
3342 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003343 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 if (p2 != NULL && type == WILD_LONGEST)
3345 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003346 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 if (ccline.cmdbuff[i + j] == '*'
3348 || ccline.cmdbuff[i + j] == '?')
3349 break;
3350 if ((int)STRLEN(p2) < j)
3351 {
3352 vim_free(p2);
3353 p2 = NULL;
3354 }
3355 }
3356 }
3357 }
3358
3359 if (p2 != NULL && !got_int)
3360 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003361 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003362 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003364 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 xp->xp_pattern = ccline.cmdbuff + i;
3366 }
3367 else
3368 v = OK;
3369 if (v == OK)
3370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003371 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3372 &ccline.cmdbuff[ccline.cmdpos],
3373 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3374 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 ccline.cmdlen += difflen;
3376 ccline.cmdpos += difflen;
3377 }
3378 }
3379 vim_free(p2);
3380
3381 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003382 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
3384 /* When expanding a ":map" command and no matches are found, assume that
3385 * the key is supposed to be inserted literally */
3386 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3387 return FAIL;
3388
3389 if (xp->xp_numfiles <= 0 && p2 == NULL)
3390 beep_flush();
3391 else if (xp->xp_numfiles == 1)
3392 /* free expanded pattern */
3393 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3394
3395 return OK;
3396}
3397
3398/*
3399 * Do wildcard expansion on the string 'str'.
3400 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003401 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 * Return NULL for failure.
3403 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003404 * "orig" is the originally expanded string, copied to allocated memory. It
3405 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3406 * WILD_PREV "orig" should be NULL.
3407 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003408 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3409 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 *
3411 * mode = WILD_FREE: just free previously expanded matches
3412 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3413 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3414 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3415 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3416 * mode = WILD_ALL: return all matches concatenated
3417 * mode = WILD_LONGEST: return longest matched part
3418 *
3419 * options = WILD_LIST_NOTFOUND: list entries without a match
3420 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3421 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3422 * options = WILD_NO_BEEP: Don't beep for multiple matches
3423 * options = WILD_ADD_SLASH: add a slash after directory names
3424 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3425 * options = WILD_SILENT: don't print warning messages
3426 * options = WILD_ESCAPE: put backslash before special chars
3427 *
3428 * The variables xp->xp_context and xp->xp_backslash must have been set!
3429 */
3430 char_u *
3431ExpandOne(xp, str, orig, options, mode)
3432 expand_T *xp;
3433 char_u *str;
3434 char_u *orig; /* allocated copy of original of expanded string */
3435 int options;
3436 int mode;
3437{
3438 char_u *ss = NULL;
3439 static int findex;
3440 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003441 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 int i;
3443 long_u len;
3444 int non_suf_match; /* number without matching suffix */
3445
3446 /*
3447 * first handle the case of using an old match
3448 */
3449 if (mode == WILD_NEXT || mode == WILD_PREV)
3450 {
3451 if (xp->xp_numfiles > 0)
3452 {
3453 if (mode == WILD_PREV)
3454 {
3455 if (findex == -1)
3456 findex = xp->xp_numfiles;
3457 --findex;
3458 }
3459 else /* mode == WILD_NEXT */
3460 ++findex;
3461
3462 /*
3463 * When wrapping around, return the original string, set findex to
3464 * -1.
3465 */
3466 if (findex < 0)
3467 {
3468 if (orig_save == NULL)
3469 findex = xp->xp_numfiles - 1;
3470 else
3471 findex = -1;
3472 }
3473 if (findex >= xp->xp_numfiles)
3474 {
3475 if (orig_save == NULL)
3476 findex = 0;
3477 else
3478 findex = -1;
3479 }
3480#ifdef FEAT_WILDMENU
3481 if (p_wmnu)
3482 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3483 findex, cmd_showtail);
3484#endif
3485 if (findex == -1)
3486 return vim_strsave(orig_save);
3487 return vim_strsave(xp->xp_files[findex]);
3488 }
3489 else
3490 return NULL;
3491 }
3492
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003493 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3495 {
3496 FreeWild(xp->xp_numfiles, xp->xp_files);
3497 xp->xp_numfiles = -1;
3498 vim_free(orig_save);
3499 orig_save = NULL;
3500 }
3501 findex = 0;
3502
3503 if (mode == WILD_FREE) /* only release file name */
3504 return NULL;
3505
3506 if (xp->xp_numfiles == -1)
3507 {
3508 vim_free(orig_save);
3509 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003510 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
3512 /*
3513 * Do the expansion.
3514 */
3515 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3516 options) == FAIL)
3517 {
3518#ifdef FNAME_ILLEGAL
3519 /* Illegal file name has been silently skipped. But when there
3520 * are wildcards, the real problem is that there was no match,
3521 * causing the pattern to be added, which has illegal characters.
3522 */
3523 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3524 EMSG2(_(e_nomatch2), str);
3525#endif
3526 }
3527 else if (xp->xp_numfiles == 0)
3528 {
3529 if (!(options & WILD_SILENT))
3530 EMSG2(_(e_nomatch2), str);
3531 }
3532 else
3533 {
3534 /* Escape the matches for use on the command line. */
3535 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3536
3537 /*
3538 * Check for matching suffixes in file names.
3539 */
3540 if (mode != WILD_ALL && mode != WILD_LONGEST)
3541 {
3542 if (xp->xp_numfiles)
3543 non_suf_match = xp->xp_numfiles;
3544 else
3545 non_suf_match = 1;
3546 if ((xp->xp_context == EXPAND_FILES
3547 || xp->xp_context == EXPAND_DIRECTORIES)
3548 && xp->xp_numfiles > 1)
3549 {
3550 /*
3551 * More than one match; check suffix.
3552 * The files will have been sorted on matching suffix in
3553 * expand_wildcards, only need to check the first two.
3554 */
3555 non_suf_match = 0;
3556 for (i = 0; i < 2; ++i)
3557 if (match_suffix(xp->xp_files[i]))
3558 ++non_suf_match;
3559 }
3560 if (non_suf_match != 1)
3561 {
3562 /* Can we ever get here unless it's while expanding
3563 * interactively? If not, we can get rid of this all
3564 * together. Don't really want to wait for this message
3565 * (and possibly have to hit return to continue!).
3566 */
3567 if (!(options & WILD_SILENT))
3568 EMSG(_(e_toomany));
3569 else if (!(options & WILD_NO_BEEP))
3570 beep_flush();
3571 }
3572 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3573 ss = vim_strsave(xp->xp_files[0]);
3574 }
3575 }
3576 }
3577
3578 /* Find longest common part */
3579 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3580 {
3581 for (len = 0; xp->xp_files[0][len]; ++len)
3582 {
3583 for (i = 0; i < xp->xp_numfiles; ++i)
3584 {
3585#ifdef CASE_INSENSITIVE_FILENAME
3586 if (xp->xp_context == EXPAND_DIRECTORIES
3587 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003588 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 || xp->xp_context == EXPAND_BUFFERS)
3590 {
3591 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3592 TOLOWER_LOC(xp->xp_files[0][len]))
3593 break;
3594 }
3595 else
3596#endif
3597 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3598 break;
3599 }
3600 if (i < xp->xp_numfiles)
3601 {
3602 if (!(options & WILD_NO_BEEP))
3603 vim_beep();
3604 break;
3605 }
3606 }
3607 ss = alloc((unsigned)len + 1);
3608 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003609 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 findex = -1; /* next p_wc gets first one */
3611 }
3612
3613 /* Concatenate all matching names */
3614 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3615 {
3616 len = 0;
3617 for (i = 0; i < xp->xp_numfiles; ++i)
3618 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3619 ss = lalloc(len, TRUE);
3620 if (ss != NULL)
3621 {
3622 *ss = NUL;
3623 for (i = 0; i < xp->xp_numfiles; ++i)
3624 {
3625 STRCAT(ss, xp->xp_files[i]);
3626 if (i != xp->xp_numfiles - 1)
3627 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3628 }
3629 }
3630 }
3631
3632 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3633 ExpandCleanup(xp);
3634
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003635 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003636 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003637 vim_free(orig);
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 return ss;
3640}
3641
3642/*
3643 * Prepare an expand structure for use.
3644 */
3645 void
3646ExpandInit(xp)
3647 expand_T *xp;
3648{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003649 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003650 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003652#ifndef BACKSLASH_IN_FILENAME
3653 xp->xp_shell = FALSE;
3654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 xp->xp_numfiles = -1;
3656 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003657#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3658 xp->xp_arg = NULL;
3659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660}
3661
3662/*
3663 * Cleanup an expand structure after use.
3664 */
3665 void
3666ExpandCleanup(xp)
3667 expand_T *xp;
3668{
3669 if (xp->xp_numfiles >= 0)
3670 {
3671 FreeWild(xp->xp_numfiles, xp->xp_files);
3672 xp->xp_numfiles = -1;
3673 }
3674}
3675
3676 void
3677ExpandEscape(xp, str, numfiles, files, options)
3678 expand_T *xp;
3679 char_u *str;
3680 int numfiles;
3681 char_u **files;
3682 int options;
3683{
3684 int i;
3685 char_u *p;
3686
3687 /*
3688 * May change home directory back to "~"
3689 */
3690 if (options & WILD_HOME_REPLACE)
3691 tilde_replace(str, numfiles, files);
3692
3693 if (options & WILD_ESCAPE)
3694 {
3695 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003696 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 || xp->xp_context == EXPAND_BUFFERS
3698 || xp->xp_context == EXPAND_DIRECTORIES)
3699 {
3700 /*
3701 * Insert a backslash into a file name before a space, \, %, #
3702 * and wildmatch characters, except '~'.
3703 */
3704 for (i = 0; i < numfiles; ++i)
3705 {
3706 /* for ":set path=" we need to escape spaces twice */
3707 if (xp->xp_backslash == XP_BS_THREE)
3708 {
3709 p = vim_strsave_escaped(files[i], (char_u *)" ");
3710 if (p != NULL)
3711 {
3712 vim_free(files[i]);
3713 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003714#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 p = vim_strsave_escaped(files[i], (char_u *)" ");
3716 if (p != NULL)
3717 {
3718 vim_free(files[i]);
3719 files[i] = p;
3720 }
3721#endif
3722 }
3723 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003724#ifdef BACKSLASH_IN_FILENAME
3725 p = vim_strsave_fnameescape(files[i], FALSE);
3726#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003727 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 if (p != NULL)
3730 {
3731 vim_free(files[i]);
3732 files[i] = p;
3733 }
3734
3735 /* If 'str' starts with "\~", replace "~" at start of
3736 * files[i] with "\~". */
3737 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003738 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
3740 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003741
3742 /* If the first file starts with a '+' escape it. Otherwise it
3743 * could be seen as "+cmd". */
3744 if (*files[0] == '+')
3745 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
3747 else if (xp->xp_context == EXPAND_TAGS)
3748 {
3749 /*
3750 * Insert a backslash before characters in a tag name that
3751 * would terminate the ":tag" command.
3752 */
3753 for (i = 0; i < numfiles; ++i)
3754 {
3755 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3756 if (p != NULL)
3757 {
3758 vim_free(files[i]);
3759 files[i] = p;
3760 }
3761 }
3762 }
3763 }
3764}
3765
3766/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003767 * Escape special characters in "fname" for when used as a file name argument
3768 * after a Vim command, or, when "shell" is non-zero, a shell command.
3769 * Returns the result in allocated memory.
3770 */
3771 char_u *
3772vim_strsave_fnameescape(fname, shell)
3773 char_u *fname;
3774 int shell;
3775{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003776 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003777#ifdef BACKSLASH_IN_FILENAME
3778 char_u buf[20];
3779 int j = 0;
3780
3781 /* Don't escape '[' and '{' if they are in 'isfname'. */
3782 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3783 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3784 buf[j++] = *p;
3785 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003786 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003787#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003788 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3789 if (shell && csh_like_shell() && p != NULL)
3790 {
3791 char_u *s;
3792
3793 /* For csh and similar shells need to put two backslashes before '!'.
3794 * One is taken by Vim, one by the shell. */
3795 s = vim_strsave_escaped(p, (char_u *)"!");
3796 vim_free(p);
3797 p = s;
3798 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003799#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003800
3801 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3802 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003803 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003804 escape_fname(&p);
3805
3806 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003807}
3808
3809/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003810 * Put a backslash before the file name in "pp", which is in allocated memory.
3811 */
3812 static void
3813escape_fname(pp)
3814 char_u **pp;
3815{
3816 char_u *p;
3817
3818 p = alloc((unsigned)(STRLEN(*pp) + 2));
3819 if (p != NULL)
3820 {
3821 p[0] = '\\';
3822 STRCPY(p + 1, *pp);
3823 vim_free(*pp);
3824 *pp = p;
3825 }
3826}
3827
3828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 * For each file name in files[num_files]:
3830 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3831 */
3832 void
3833tilde_replace(orig_pat, num_files, files)
3834 char_u *orig_pat;
3835 int num_files;
3836 char_u **files;
3837{
3838 int i;
3839 char_u *p;
3840
3841 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3842 {
3843 for (i = 0; i < num_files; ++i)
3844 {
3845 p = home_replace_save(NULL, files[i]);
3846 if (p != NULL)
3847 {
3848 vim_free(files[i]);
3849 files[i] = p;
3850 }
3851 }
3852 }
3853}
3854
3855/*
3856 * Show all matches for completion on the command line.
3857 * Returns EXPAND_NOTHING when the character that triggered expansion should
3858 * be inserted like a normal character.
3859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 static int
3861showmatches(xp, wildmenu)
3862 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003863 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864{
3865#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3866 int num_files;
3867 char_u **files_found;
3868 int i, j, k;
3869 int maxlen;
3870 int lines;
3871 int columns;
3872 char_u *p;
3873 int lastlen;
3874 int attr;
3875 int showtail;
3876
3877 if (xp->xp_numfiles == -1)
3878 {
3879 set_expand_context(xp);
3880 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3881 &num_files, &files_found);
3882 showtail = expand_showtail(xp);
3883 if (i != EXPAND_OK)
3884 return i;
3885
3886 }
3887 else
3888 {
3889 num_files = xp->xp_numfiles;
3890 files_found = xp->xp_files;
3891 showtail = cmd_showtail;
3892 }
3893
3894#ifdef FEAT_WILDMENU
3895 if (!wildmenu)
3896 {
3897#endif
3898 msg_didany = FALSE; /* lines_left will be set */
3899 msg_start(); /* prepare for paging */
3900 msg_putchar('\n');
3901 out_flush();
3902 cmdline_row = msg_row;
3903 msg_didany = FALSE; /* lines_left will be set again */
3904 msg_start(); /* prepare for paging */
3905#ifdef FEAT_WILDMENU
3906 }
3907#endif
3908
3909 if (got_int)
3910 got_int = FALSE; /* only int. the completion, not the cmd line */
3911#ifdef FEAT_WILDMENU
3912 else if (wildmenu)
3913 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3914#endif
3915 else
3916 {
3917 /* find the length of the longest file name */
3918 maxlen = 0;
3919 for (i = 0; i < num_files; ++i)
3920 {
3921 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003922 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 || xp->xp_context == EXPAND_BUFFERS))
3924 {
3925 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3926 j = vim_strsize(NameBuff);
3927 }
3928 else
3929 j = vim_strsize(L_SHOWFILE(i));
3930 if (j > maxlen)
3931 maxlen = j;
3932 }
3933
3934 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3935 lines = num_files;
3936 else
3937 {
3938 /* compute the number of columns and lines for the listing */
3939 maxlen += 2; /* two spaces between file names */
3940 columns = ((int)Columns + 2) / maxlen;
3941 if (columns < 1)
3942 columns = 1;
3943 lines = (num_files + columns - 1) / columns;
3944 }
3945
3946 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3947
3948 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3949 {
3950 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3951 msg_clr_eos();
3952 msg_advance(maxlen - 3);
3953 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3954 }
3955
3956 /* list the files line by line */
3957 for (i = 0; i < lines; ++i)
3958 {
3959 lastlen = 999;
3960 for (k = i; k < num_files; k += lines)
3961 {
3962 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3963 {
3964 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3965 p = files_found[k] + STRLEN(files_found[k]) + 1;
3966 msg_advance(maxlen + 1);
3967 msg_puts(p);
3968 msg_advance(maxlen + 3);
3969 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3970 break;
3971 }
3972 for (j = maxlen - lastlen; --j >= 0; )
3973 msg_putchar(' ');
3974 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003975 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 || xp->xp_context == EXPAND_BUFFERS)
3977 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01003978 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01003979 if (xp->xp_numfiles != -1)
3980 {
3981 char_u *halved_slash;
3982 char_u *exp_path;
3983
3984 /* Expansion was done before and special characters
3985 * were escaped, need to halve backslashes. Also
3986 * $HOME has been replaced with ~/. */
3987 exp_path = expand_env_save_opt(files_found[k], TRUE);
3988 halved_slash = backslash_halve_save(
3989 exp_path != NULL ? exp_path : files_found[k]);
3990 j = mch_isdir(halved_slash != NULL ? halved_slash
3991 : files_found[k]);
3992 vim_free(exp_path);
3993 vim_free(halved_slash);
3994 }
3995 else
3996 /* Expansion was done here, file names are literal. */
3997 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (showtail)
3999 p = L_SHOWFILE(k);
4000 else
4001 {
4002 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4003 TRUE);
4004 p = NameBuff;
4005 }
4006 }
4007 else
4008 {
4009 j = FALSE;
4010 p = L_SHOWFILE(k);
4011 }
4012 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4013 }
4014 if (msg_col > 0) /* when not wrapped around */
4015 {
4016 msg_clr_eos();
4017 msg_putchar('\n');
4018 }
4019 out_flush(); /* show one line at a time */
4020 if (got_int)
4021 {
4022 got_int = FALSE;
4023 break;
4024 }
4025 }
4026
4027 /*
4028 * we redraw the command below the lines that we have just listed
4029 * This is a bit tricky, but it saves a lot of screen updating.
4030 */
4031 cmdline_row = msg_row; /* will put it back later */
4032 }
4033
4034 if (xp->xp_numfiles == -1)
4035 FreeWild(num_files, files_found);
4036
4037 return EXPAND_OK;
4038}
4039
4040/*
4041 * Private gettail for showmatches() (and win_redr_status_matches()):
4042 * Find tail of file name path, but ignore trailing "/".
4043 */
4044 char_u *
4045sm_gettail(s)
4046 char_u *s;
4047{
4048 char_u *p;
4049 char_u *t = s;
4050 int had_sep = FALSE;
4051
4052 for (p = s; *p != NUL; )
4053 {
4054 if (vim_ispathsep(*p)
4055#ifdef BACKSLASH_IN_FILENAME
4056 && !rem_backslash(p)
4057#endif
4058 )
4059 had_sep = TRUE;
4060 else if (had_sep)
4061 {
4062 t = p;
4063 had_sep = FALSE;
4064 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004065 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067 return t;
4068}
4069
4070/*
4071 * Return TRUE if we only need to show the tail of completion matches.
4072 * When not completing file names or there is a wildcard in the path FALSE is
4073 * returned.
4074 */
4075 static int
4076expand_showtail(xp)
4077 expand_T *xp;
4078{
4079 char_u *s;
4080 char_u *end;
4081
4082 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004083 if (xp->xp_context != EXPAND_FILES
4084 && xp->xp_context != EXPAND_SHELLCMD
4085 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 return FALSE;
4087
4088 end = gettail(xp->xp_pattern);
4089 if (end == xp->xp_pattern) /* there is no path separator */
4090 return FALSE;
4091
4092 for (s = xp->xp_pattern; s < end; s++)
4093 {
4094 /* Skip escaped wildcards. Only when the backslash is not a path
4095 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4096 if (rem_backslash(s))
4097 ++s;
4098 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4099 return FALSE;
4100 }
4101 return TRUE;
4102}
4103
4104/*
4105 * Prepare a string for expansion.
4106 * When expanding file names: The string will be used with expand_wildcards().
4107 * Copy the file name into allocated memory and add a '*' at the end.
4108 * When expanding other names: The string will be used with regcomp(). Copy
4109 * the name into allocated memory and prepend "^".
4110 */
4111 char_u *
4112addstar(fname, len, context)
4113 char_u *fname;
4114 int len;
4115 int context; /* EXPAND_FILES etc. */
4116{
4117 char_u *retval;
4118 int i, j;
4119 int new_len;
4120 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004121 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004123 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004124 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004125 && context != EXPAND_SHELLCMD
4126 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 {
4128 /*
4129 * Matching will be done internally (on something other than files).
4130 * So we convert the file-matching-type wildcards into our kind for
4131 * use with vim_regcomp(). First work out how long it will be:
4132 */
4133
4134 /* For help tags the translation is done in find_help_tags().
4135 * For a tag pattern starting with "/" no translation is needed. */
4136 if (context == EXPAND_HELP
4137 || context == EXPAND_COLORS
4138 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004139 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004140 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 || (context == EXPAND_TAGS && fname[0] == '/'))
4142 retval = vim_strnsave(fname, len);
4143 else
4144 {
4145 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4146 for (i = 0; i < len; i++)
4147 {
4148 if (fname[i] == '*' || fname[i] == '~')
4149 new_len++; /* '*' needs to be replaced by ".*"
4150 '~' needs to be replaced by "\~" */
4151
4152 /* Buffer names are like file names. "." should be literal */
4153 if (context == EXPAND_BUFFERS && fname[i] == '.')
4154 new_len++; /* "." becomes "\." */
4155
4156 /* Custom expansion takes care of special things, match
4157 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004158 if ((context == EXPAND_USER_DEFINED
4159 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 new_len++; /* '\' becomes "\\" */
4161 }
4162 retval = alloc(new_len);
4163 if (retval != NULL)
4164 {
4165 retval[0] = '^';
4166 j = 1;
4167 for (i = 0; i < len; i++, j++)
4168 {
4169 /* Skip backslash. But why? At least keep it for custom
4170 * expansion. */
4171 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004172 && context != EXPAND_USER_LIST
4173 && fname[i] == '\\'
4174 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 break;
4176
4177 switch (fname[i])
4178 {
4179 case '*': retval[j++] = '.';
4180 break;
4181 case '~': retval[j++] = '\\';
4182 break;
4183 case '?': retval[j] = '.';
4184 continue;
4185 case '.': if (context == EXPAND_BUFFERS)
4186 retval[j++] = '\\';
4187 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004188 case '\\': if (context == EXPAND_USER_DEFINED
4189 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 retval[j++] = '\\';
4191 break;
4192 }
4193 retval[j] = fname[i];
4194 }
4195 retval[j] = NUL;
4196 }
4197 }
4198 }
4199 else
4200 {
4201 retval = alloc(len + 4);
4202 if (retval != NULL)
4203 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004204 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
4206 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004207 * Don't add a star to *, ~, ~user, $var or `cmd`.
4208 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 * ~ would be at the start of the file name, but not the tail.
4210 * $ could be anywhere in the tail.
4211 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004212 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 */
4214 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004215 ends_in_star = (len > 0 && retval[len - 1] == '*');
4216#ifndef BACKSLASH_IN_FILENAME
4217 for (i = len - 2; i >= 0; --i)
4218 {
4219 if (retval[i] != '\\')
4220 break;
4221 ends_in_star = !ends_in_star;
4222 }
4223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004225 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 && vim_strchr(tail, '$') == NULL
4227 && vim_strchr(retval, '`') == NULL)
4228 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004229 else if (len > 0 && retval[len - 1] == '$')
4230 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 retval[len] = NUL;
4232 }
4233 }
4234 return retval;
4235}
4236
4237/*
4238 * Must parse the command line so far to work out what context we are in.
4239 * Completion can then be done based on that context.
4240 * This routine sets the variables:
4241 * xp->xp_pattern The start of the pattern to be expanded within
4242 * the command line (ends at the cursor).
4243 * xp->xp_context The type of thing to expand. Will be one of:
4244 *
4245 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4246 * the command line, like an unknown command. Caller
4247 * should beep.
4248 * EXPAND_NOTHING Unrecognised context for completion, use char like
4249 * a normal char, rather than for completion. eg
4250 * :s/^I/
4251 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4252 * it.
4253 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4254 * EXPAND_FILES After command with XFILE set, or after setting
4255 * with P_EXPAND set. eg :e ^I, :w>>^I
4256 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4257 * when we know only directories are of interest. eg
4258 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004259 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4261 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4262 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4263 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4264 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4265 * EXPAND_EVENTS Complete event names
4266 * EXPAND_SYNTAX Complete :syntax command arguments
4267 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4268 * EXPAND_AUGROUP Complete autocommand group names
4269 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4270 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4271 * eg :unmap a^I , :cunab x^I
4272 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4273 * eg :call sub^I
4274 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4275 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4276 * names in expressions, eg :while s^I
4277 * EXPAND_ENV_VARS Complete environment variable names
4278 */
4279 static void
4280set_expand_context(xp)
4281 expand_T *xp;
4282{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004283 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 if (ccline.cmdfirstc != ':'
4285#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004286 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004287 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288#endif
4289 )
4290 {
4291 xp->xp_context = EXPAND_NOTHING;
4292 return;
4293 }
4294 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4295}
4296
4297 void
4298set_cmd_context(xp, str, len, col)
4299 expand_T *xp;
4300 char_u *str; /* start of command line */
4301 int len; /* length of command line (excl. NUL) */
4302 int col; /* position of cursor */
4303{
4304 int old_char = NUL;
4305 char_u *nextcomm;
4306
4307 /*
4308 * Avoid a UMR warning from Purify, only save the character if it has been
4309 * written before.
4310 */
4311 if (col < len)
4312 old_char = str[col];
4313 str[col] = NUL;
4314 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004315
4316#ifdef FEAT_EVAL
4317 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004318 {
4319# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004320 /* pass CMD_SIZE because there is no real command */
4321 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004322# endif
4323 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004324 else if (ccline.input_fn)
4325 {
4326 xp->xp_context = ccline.xp_context;
4327 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004328# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004329 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004330# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004331 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004332 else
4333#endif
4334 while (nextcomm != NULL)
4335 nextcomm = set_one_cmd_context(xp, nextcomm);
4336
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 str[col] = old_char;
4338}
4339
4340/*
4341 * Expand the command line "str" from context "xp".
4342 * "xp" must have been set by set_cmd_context().
4343 * xp->xp_pattern points into "str", to where the text that is to be expanded
4344 * starts.
4345 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4346 * cursor.
4347 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4348 * key that triggered expansion literally.
4349 * Returns EXPAND_OK otherwise.
4350 */
4351 int
4352expand_cmdline(xp, str, col, matchcount, matches)
4353 expand_T *xp;
4354 char_u *str; /* start of command line */
4355 int col; /* position of cursor */
4356 int *matchcount; /* return: nr of matches */
4357 char_u ***matches; /* return: array of pointers to matches */
4358{
4359 char_u *file_str = NULL;
4360
4361 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4362 {
4363 beep_flush();
4364 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4365 }
4366 if (xp->xp_context == EXPAND_NOTHING)
4367 {
4368 /* Caller can use the character as a normal char instead */
4369 return EXPAND_NOTHING;
4370 }
4371
4372 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004373 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4374 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 if (file_str == NULL)
4376 return EXPAND_UNSUCCESSFUL;
4377
4378 /* find all files that match the description */
4379 if (ExpandFromContext(xp, file_str, matchcount, matches,
4380 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4381 {
4382 *matchcount = 0;
4383 *matches = NULL;
4384 }
4385 vim_free(file_str);
4386
4387 return EXPAND_OK;
4388}
4389
4390#ifdef FEAT_MULTI_LANG
4391/*
4392 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4393 */
4394static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4395
4396 static void
4397cleanup_help_tags(num_file, file)
4398 int num_file;
4399 char_u **file;
4400{
4401 int i, j;
4402 int len;
4403
4404 for (i = 0; i < num_file; ++i)
4405 {
4406 len = (int)STRLEN(file[i]) - 3;
4407 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4408 {
4409 /* Sorting on priority means the same item in another language may
4410 * be anywhere. Search all items for a match up to the "@en". */
4411 for (j = 0; j < num_file; ++j)
4412 if (j != i
4413 && (int)STRLEN(file[j]) == len + 3
4414 && STRNCMP(file[i], file[j], len + 1) == 0)
4415 break;
4416 if (j == num_file)
4417 file[i][len] = NUL;
4418 }
4419 }
4420}
4421#endif
4422
4423/*
4424 * Do the expansion based on xp->xp_context and "pat".
4425 */
4426 static int
4427ExpandFromContext(xp, pat, num_file, file, options)
4428 expand_T *xp;
4429 char_u *pat;
4430 int *num_file;
4431 char_u ***file;
4432 int options;
4433{
4434#ifdef FEAT_CMDL_COMPL
4435 regmatch_T regmatch;
4436#endif
4437 int ret;
4438 int flags;
4439
4440 flags = EW_DIR; /* include directories */
4441 if (options & WILD_LIST_NOTFOUND)
4442 flags |= EW_NOTFOUND;
4443 if (options & WILD_ADD_SLASH)
4444 flags |= EW_ADDSLASH;
4445 if (options & WILD_KEEP_ALL)
4446 flags |= EW_KEEPALL;
4447 if (options & WILD_SILENT)
4448 flags |= EW_SILENT;
4449
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004450 if (xp->xp_context == EXPAND_FILES
4451 || xp->xp_context == EXPAND_DIRECTORIES
4452 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 {
4454 /*
4455 * Expand file or directory names.
4456 */
4457 int free_pat = FALSE;
4458 int i;
4459
4460 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4461 * space */
4462 if (xp->xp_backslash != XP_BS_NONE)
4463 {
4464 free_pat = TRUE;
4465 pat = vim_strsave(pat);
4466 for (i = 0; pat[i]; ++i)
4467 if (pat[i] == '\\')
4468 {
4469 if (xp->xp_backslash == XP_BS_THREE
4470 && pat[i + 1] == '\\'
4471 && pat[i + 2] == '\\'
4472 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004473 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 if (xp->xp_backslash == XP_BS_ONE
4475 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004476 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478 }
4479
4480 if (xp->xp_context == EXPAND_FILES)
4481 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004482 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4483 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 else
4485 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaard7834d32009-12-02 16:14:36 +00004486 /* Expand wildcards, supporting %:h and the like. */
4487 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 if (free_pat)
4489 vim_free(pat);
4490 return ret;
4491 }
4492
4493 *file = (char_u **)"";
4494 *num_file = 0;
4495 if (xp->xp_context == EXPAND_HELP)
4496 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004497 /* With an empty argument we would get all the help tags, which is
4498 * very slow. Get matches for "help" instead. */
4499 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4500 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 {
4502#ifdef FEAT_MULTI_LANG
4503 cleanup_help_tags(*num_file, *file);
4504#endif
4505 return OK;
4506 }
4507 return FAIL;
4508 }
4509
4510#ifndef FEAT_CMDL_COMPL
4511 return FAIL;
4512#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004513 if (xp->xp_context == EXPAND_SHELLCMD)
4514 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 if (xp->xp_context == EXPAND_OLD_SETTING)
4516 return ExpandOldSetting(num_file, file);
4517 if (xp->xp_context == EXPAND_BUFFERS)
4518 return ExpandBufnames(pat, num_file, file, options);
4519 if (xp->xp_context == EXPAND_TAGS
4520 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4521 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4522 if (xp->xp_context == EXPAND_COLORS)
4523 return ExpandRTDir(pat, num_file, file, "colors");
4524 if (xp->xp_context == EXPAND_COMPILER)
4525 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004526 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004527 return ExpandRTDir(pat, num_file, file, "syntax");
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004528 if (xp->xp_context == EXPAND_FILETYPE)
4529 return ExpandRTDir(pat, num_file, file, "{syntax,indent,ftplugin}");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004530# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4531 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004532 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004533# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
4535 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4536 if (regmatch.regprog == NULL)
4537 return FAIL;
4538
4539 /* set ignore-case according to p_ic, p_scs and pat */
4540 regmatch.rm_ic = ignorecase(pat);
4541
4542 if (xp->xp_context == EXPAND_SETTINGS
4543 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4544 ret = ExpandSettings(xp, &regmatch, num_file, file);
4545 else if (xp->xp_context == EXPAND_MAPPINGS)
4546 ret = ExpandMappings(&regmatch, num_file, file);
4547# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4548 else if (xp->xp_context == EXPAND_USER_DEFINED)
4549 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4550# endif
4551 else
4552 {
4553 static struct expgen
4554 {
4555 int context;
4556 char_u *((*func)__ARGS((expand_T *, int)));
4557 int ic;
4558 } tab[] =
4559 {
4560 {EXPAND_COMMANDS, get_command_name, FALSE},
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004561 {EXPAND_BEHAVE, get_behave_arg, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562#ifdef FEAT_USR_CMDS
4563 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4564 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4565 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4566 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4567#endif
4568#ifdef FEAT_EVAL
4569 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4570 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4571 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4572 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4573#endif
4574#ifdef FEAT_MENU
4575 {EXPAND_MENUS, get_menu_name, FALSE},
4576 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4577#endif
4578#ifdef FEAT_SYN_HL
4579 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4580#endif
4581 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4582#ifdef FEAT_AUTOCMD
4583 {EXPAND_EVENTS, get_event_name, TRUE},
4584 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4585#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004586#ifdef FEAT_CSCOPE
4587 {EXPAND_CSCOPE, get_cscope_name, TRUE},
4588#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004589#ifdef FEAT_SIGNS
4590 {EXPAND_SIGN, get_sign_name, TRUE},
4591#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004592#ifdef FEAT_PROFILE
4593 {EXPAND_PROFILE, get_profile_name, TRUE},
4594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4596 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4597 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4598#endif
4599 {EXPAND_ENV_VARS, get_env_name, TRUE},
4600 };
4601 int i;
4602
4603 /*
4604 * Find a context in the table and call the ExpandGeneric() with the
4605 * right function to do the expansion.
4606 */
4607 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004608 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 if (xp->xp_context == tab[i].context)
4610 {
4611 if (tab[i].ic)
4612 regmatch.rm_ic = TRUE;
4613 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4614 break;
4615 }
4616 }
4617
4618 vim_free(regmatch.regprog);
4619
4620 return ret;
4621#endif /* FEAT_CMDL_COMPL */
4622}
4623
4624#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4625/*
4626 * Expand a list of names.
4627 *
4628 * Generic function for command line completion. It calls a function to
4629 * obtain strings, one by one. The strings are matched against a regexp
4630 * program. Matching strings are copied into an array, which is returned.
4631 *
4632 * Returns OK when no problems encountered, FAIL for error (out of memory).
4633 */
4634 int
4635ExpandGeneric(xp, regmatch, num_file, file, func)
4636 expand_T *xp;
4637 regmatch_T *regmatch;
4638 int *num_file;
4639 char_u ***file;
4640 char_u *((*func)__ARGS((expand_T *, int)));
4641 /* returns a string from the list */
4642{
4643 int i;
4644 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004645 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 char_u *str;
4647
4648 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004649 * round == 0: count the number of matching names
4650 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004652 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
4654 for (i = 0; ; ++i)
4655 {
4656 str = (*func)(xp, i);
4657 if (str == NULL) /* end of list */
4658 break;
4659 if (*str == NUL) /* skip empty strings */
4660 continue;
4661
4662 if (vim_regexec(regmatch, str, (colnr_T)0))
4663 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004664 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
4666 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4667 (*file)[count] = str;
4668#ifdef FEAT_MENU
4669 if (func == get_menu_names && str != NULL)
4670 {
4671 /* test for separator added by get_menu_names() */
4672 str += STRLEN(str) - 1;
4673 if (*str == '\001')
4674 *str = '.';
4675 }
4676#endif
4677 }
4678 ++count;
4679 }
4680 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004681 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 {
4683 if (count == 0)
4684 return OK;
4685 *num_file = count;
4686 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4687 if (*file == NULL)
4688 {
4689 *file = (char_u **)"";
4690 return FAIL;
4691 }
4692 count = 0;
4693 }
4694 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004695
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004696 /* Sort the results. Keep menu's in the specified order. */
4697 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4698 sort_strings(*file, *num_file);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004699
Bram Moolenaar4f688582007-07-24 12:34:30 +00004700#ifdef FEAT_CMDL_COMPL
4701 /* Reset the variables used for special highlight names expansion, so that
4702 * they don't show up when getting normal highlight names by ID. */
4703 reset_expand_highlight();
4704#endif
4705
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 return OK;
4707}
4708
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004709/*
4710 * Complete a shell command.
4711 * Returns FAIL or OK;
4712 */
4713 static int
4714expand_shellcmd(filepat, num_file, file, flagsarg)
4715 char_u *filepat; /* pattern to match with command names */
4716 int *num_file; /* return: number of matches */
4717 char_u ***file; /* return: array with matches */
4718 int flagsarg; /* EW_ flags */
4719{
4720 char_u *pat;
4721 int i;
4722 char_u *path;
4723 int mustfree = FALSE;
4724 garray_T ga;
4725 char_u *buf = alloc(MAXPATHL);
4726 size_t l;
4727 char_u *s, *e;
4728 int flags = flagsarg;
4729 int ret;
4730
4731 if (buf == NULL)
4732 return FAIL;
4733
4734 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4735 * space */
4736 pat = vim_strsave(filepat);
4737 for (i = 0; pat[i]; ++i)
4738 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004739 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004740
4741 flags |= EW_FILE | EW_EXEC;
4742
4743 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004744 if (mch_isFullName(pat))
4745 path = (char_u *)" ";
4746 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004747 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4748 path = (char_u *)".";
4749 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004750 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004751 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004752 if (path == NULL)
4753 path = (char_u *)"";
4754 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004755
4756 /*
4757 * Go over all directories in $PATH. Expand matches in that directory and
4758 * collect them in "ga".
4759 */
4760 ga_init2(&ga, (int)sizeof(char *), 10);
4761 for (s = path; *s != NUL; s = e)
4762 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004763 if (*s == ' ')
4764 ++s; /* Skip space used for absolute path name. */
4765
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004766#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4767 e = vim_strchr(s, ';');
4768#else
4769 e = vim_strchr(s, ':');
4770#endif
4771 if (e == NULL)
4772 e = s + STRLEN(s);
4773
4774 l = e - s;
4775 if (l > MAXPATHL - 5)
4776 break;
4777 vim_strncpy(buf, s, l);
4778 add_pathsep(buf);
4779 l = STRLEN(buf);
4780 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4781
4782 /* Expand matches in one directory of $PATH. */
4783 ret = expand_wildcards(1, &buf, num_file, file, flags);
4784 if (ret == OK)
4785 {
4786 if (ga_grow(&ga, *num_file) == FAIL)
4787 FreeWild(*num_file, *file);
4788 else
4789 {
4790 for (i = 0; i < *num_file; ++i)
4791 {
4792 s = (*file)[i];
4793 if (STRLEN(s) > l)
4794 {
4795 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004796 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004797 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4798 }
4799 else
4800 vim_free(s);
4801 }
4802 vim_free(*file);
4803 }
4804 }
4805 if (*e != NUL)
4806 ++e;
4807 }
4808 *file = ga.ga_data;
4809 *num_file = ga.ga_len;
4810
4811 vim_free(buf);
4812 vim_free(pat);
4813 if (mustfree)
4814 vim_free(path);
4815 return OK;
4816}
4817
4818
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004820static 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));
4821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004823 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004824 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004826 static void *
4827call_user_expand_func(user_expand_func, xp, num_file, file)
4828 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 int *num_file;
4831 char_u ***file;
4832{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004833 char_u keep;
4834 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004837 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004838 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
4840 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004841 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 *num_file = 0;
4843 *file = NULL;
4844
Bram Moolenaar21669c02008-01-18 12:16:16 +00004845 if (ccline.cmdbuff == NULL)
4846 {
4847 /* Completion from Insert mode, pass fake arguments. */
4848 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004849 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004850 args[1] = xp->xp_pattern;
4851 }
4852 else
4853 {
4854 /* Completion on the command line, pass real arguments. */
4855 keep = ccline.cmdbuff[ccline.cmdlen];
4856 ccline.cmdbuff[ccline.cmdlen] = 0;
4857 sprintf((char *)num, "%d", ccline.cmdpos);
4858 args[1] = ccline.cmdbuff;
4859 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004860 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 args[2] = num;
4862
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004863 /* Save the cmdline, we don't know what the function may do. */
4864 save_ccline = ccline;
4865 ccline.cmdbuff = NULL;
4866 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004868
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004869 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004870
4871 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004873 if (ccline.cmdbuff != NULL)
4874 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004875
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004876 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004877 return ret;
4878}
4879
4880/*
4881 * Expand names with a function defined by the user.
4882 */
4883 static int
4884ExpandUserDefined(xp, regmatch, num_file, file)
4885 expand_T *xp;
4886 regmatch_T *regmatch;
4887 int *num_file;
4888 char_u ***file;
4889{
4890 char_u *retstr;
4891 char_u *s;
4892 char_u *e;
4893 char_u keep;
4894 garray_T ga;
4895
4896 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4897 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 return FAIL;
4899
4900 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004901 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 {
4903 e = vim_strchr(s, '\n');
4904 if (e == NULL)
4905 e = s + STRLEN(s);
4906 keep = *e;
4907 *e = 0;
4908
4909 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4910 {
4911 *e = keep;
4912 if (*e != NUL)
4913 ++e;
4914 continue;
4915 }
4916
4917 if (ga_grow(&ga, 1) == FAIL)
4918 break;
4919
4920 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4921 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
4923 *e = keep;
4924 if (*e != NUL)
4925 ++e;
4926 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004927 vim_free(retstr);
4928 *file = ga.ga_data;
4929 *num_file = ga.ga_len;
4930 return OK;
4931}
4932
4933/*
4934 * Expand names with a list returned by a function defined by the user.
4935 */
4936 static int
4937ExpandUserList(xp, num_file, file)
4938 expand_T *xp;
4939 int *num_file;
4940 char_u ***file;
4941{
4942 list_T *retlist;
4943 listitem_T *li;
4944 garray_T ga;
4945
4946 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4947 if (retlist == NULL)
4948 return FAIL;
4949
4950 ga_init2(&ga, (int)sizeof(char *), 3);
4951 /* Loop over the items in the list. */
4952 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4953 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00004954 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
4955 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004956
4957 if (ga_grow(&ga, 1) == FAIL)
4958 break;
4959
4960 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00004961 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004962 ++ga.ga_len;
4963 }
4964 list_unref(retlist);
4965
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 *file = ga.ga_data;
4967 *num_file = ga.ga_len;
4968 return OK;
4969}
4970#endif
4971
4972/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004973 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004974 * 'runtimepath'/{dirnames}/{pat}.vim
4975 * dirnames may contain one directory (ex: "colorscheme") or can be a glob
4976 * expression matching multiple directories (ex: "{syntax,ftplugin,indent}").
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 */
4978 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004979ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 char_u *pat;
4981 int *num_file;
4982 char_u ***file;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004983 char *dirnames;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984{
4985 char_u *all;
4986 char_u *s;
4987 char_u *e;
4988 garray_T ga;
4989
4990 *num_file = 0;
4991 *file = NULL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004992 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirnames) + 7));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 if (s == NULL)
4994 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004995 sprintf((char *)s, "%s/%s*.vim", dirnames, pat);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00004996 all = globpath(p_rtp, s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 vim_free(s);
4998 if (all == NULL)
4999 return FAIL;
5000
5001 ga_init2(&ga, (int)sizeof(char *), 3);
5002 for (s = all; *s != NUL; s = e)
5003 {
5004 e = vim_strchr(s, '\n');
5005 if (e == NULL)
5006 e = s + STRLEN(s);
5007 if (ga_grow(&ga, 1) == FAIL)
5008 break;
5009 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5010 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005011 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 if (*s == '\n' || vim_ispathsep(*s))
5013 break;
5014 ++s;
5015 ((char_u **)ga.ga_data)[ga.ga_len] =
5016 vim_strnsave(s, (int)(e - s - 4));
5017 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 if (*e != NUL)
5020 ++e;
5021 }
5022 vim_free(all);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005023
5024 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaardc685ab2010-08-13 21:16:49 +02005025 * directories in dirnames such as "{syntax,ftplugin,indent}". */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005026 remove_duplicates(&ga);
5027
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 *file = ga.ga_data;
5029 *num_file = ga.ga_len;
5030 return OK;
5031}
5032
5033#endif
5034
5035#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5036/*
5037 * Expand "file" for all comma-separated directories in "path".
5038 * Returns an allocated string with all matches concatenated, separated by
5039 * newlines. Returns NULL for an error or no matches.
5040 */
5041 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005042globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 char_u *path;
5044 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005045 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046{
5047 expand_T xpc;
5048 char_u *buf;
5049 garray_T ga;
5050 int i;
5051 int len;
5052 int num_p;
5053 char_u **p;
5054 char_u *cur = NULL;
5055
5056 buf = alloc(MAXPATHL);
5057 if (buf == NULL)
5058 return NULL;
5059
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005060 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005062
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 ga_init2(&ga, 1, 100);
5064
5065 /* Loop over all entries in {path}. */
5066 while (*path != NUL)
5067 {
5068 /* Copy one item of the path to buf[] and concatenate the file name. */
5069 copy_option_part(&path, buf, MAXPATHL, ",");
5070 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5071 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005072# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005073 /* Using the platform's path separator (\) makes vim incorrectly
5074 * treat it as an escape character, use '/' instead. */
5075 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5076 STRCAT(buf, "/");
5077# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005079# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005081 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5082 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005084 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005086 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087
5088 /* Concatenate new results to previous ones. */
5089 if (ga_grow(&ga, len) == OK)
5090 {
5091 cur = (char_u *)ga.ga_data + ga.ga_len;
5092 for (i = 0; i < num_p; ++i)
5093 {
5094 STRCPY(cur, p[i]);
5095 cur += STRLEN(p[i]);
5096 *cur++ = '\n';
5097 }
5098 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
5100 FreeWild(num_p, p);
5101 }
5102 }
5103 }
5104 if (cur != NULL)
5105 *--cur = 0; /* Replace trailing newline with NUL */
5106
5107 vim_free(buf);
5108 return (char_u *)ga.ga_data;
5109}
5110
5111#endif
5112
5113#if defined(FEAT_CMDHIST) || defined(PROTO)
5114
5115/*********************************
5116 * Command line history stuff *
5117 *********************************/
5118
5119/*
5120 * Translate a history character to the associated type number.
5121 */
5122 static int
5123hist_char2type(c)
5124 int c;
5125{
5126 if (c == ':')
5127 return HIST_CMD;
5128 if (c == '=')
5129 return HIST_EXPR;
5130 if (c == '@')
5131 return HIST_INPUT;
5132 if (c == '>')
5133 return HIST_DEBUG;
5134 return HIST_SEARCH; /* must be '?' or '/' */
5135}
5136
5137/*
5138 * Table of history names.
5139 * These names are used in :history and various hist...() functions.
5140 * It is sufficient to give the significant prefix of a history name.
5141 */
5142
5143static char *(history_names[]) =
5144{
5145 "cmd",
5146 "search",
5147 "expr",
5148 "input",
5149 "debug",
5150 NULL
5151};
5152
5153/*
5154 * init_history() - Initialize the command line history.
5155 * Also used to re-allocate the history when the size changes.
5156 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005157 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158init_history()
5159{
5160 int newlen; /* new length of history table */
5161 histentry_T *temp;
5162 int i;
5163 int j;
5164 int type;
5165
5166 /*
5167 * If size of history table changed, reallocate it
5168 */
5169 newlen = (int)p_hi;
5170 if (newlen != hislen) /* history length changed */
5171 {
5172 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5173 {
5174 if (newlen)
5175 {
5176 temp = (histentry_T *)lalloc(
5177 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5178 if (temp == NULL) /* out of memory! */
5179 {
5180 if (type == 0) /* first one: just keep the old length */
5181 {
5182 newlen = hislen;
5183 break;
5184 }
5185 /* Already changed one table, now we can only have zero
5186 * length for all tables. */
5187 newlen = 0;
5188 type = -1;
5189 continue;
5190 }
5191 }
5192 else
5193 temp = NULL;
5194 if (newlen == 0 || temp != NULL)
5195 {
5196 if (hisidx[type] < 0) /* there are no entries yet */
5197 {
5198 for (i = 0; i < newlen; ++i)
5199 {
5200 temp[i].hisnum = 0;
5201 temp[i].hisstr = NULL;
5202 }
5203 }
5204 else if (newlen > hislen) /* array becomes bigger */
5205 {
5206 for (i = 0; i <= hisidx[type]; ++i)
5207 temp[i] = history[type][i];
5208 j = i;
5209 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5210 {
5211 temp[i].hisnum = 0;
5212 temp[i].hisstr = NULL;
5213 }
5214 for ( ; j < hislen; ++i, ++j)
5215 temp[i] = history[type][j];
5216 }
5217 else /* array becomes smaller or 0 */
5218 {
5219 j = hisidx[type];
5220 for (i = newlen - 1; ; --i)
5221 {
5222 if (i >= 0) /* copy newest entries */
5223 temp[i] = history[type][j];
5224 else /* remove older entries */
5225 vim_free(history[type][j].hisstr);
5226 if (--j < 0)
5227 j = hislen - 1;
5228 if (j == hisidx[type])
5229 break;
5230 }
5231 hisidx[type] = newlen - 1;
5232 }
5233 vim_free(history[type]);
5234 history[type] = temp;
5235 }
5236 }
5237 hislen = newlen;
5238 }
5239}
5240
5241/*
5242 * Check if command line 'str' is already in history.
5243 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5244 */
5245 static int
5246in_history(type, str, move_to_front)
5247 int type;
5248 char_u *str;
5249 int move_to_front; /* Move the entry to the front if it exists */
5250{
5251 int i;
5252 int last_i = -1;
5253
5254 if (hisidx[type] < 0)
5255 return FALSE;
5256 i = hisidx[type];
5257 do
5258 {
5259 if (history[type][i].hisstr == NULL)
5260 return FALSE;
5261 if (STRCMP(str, history[type][i].hisstr) == 0)
5262 {
5263 if (!move_to_front)
5264 return TRUE;
5265 last_i = i;
5266 break;
5267 }
5268 if (--i < 0)
5269 i = hislen - 1;
5270 } while (i != hisidx[type]);
5271
5272 if (last_i >= 0)
5273 {
5274 str = history[type][i].hisstr;
5275 while (i != hisidx[type])
5276 {
5277 if (++i >= hislen)
5278 i = 0;
5279 history[type][last_i] = history[type][i];
5280 last_i = i;
5281 }
5282 history[type][i].hisstr = str;
5283 history[type][i].hisnum = ++hisnum[type];
5284 return TRUE;
5285 }
5286 return FALSE;
5287}
5288
5289/*
5290 * Convert history name (from table above) to its HIST_ equivalent.
5291 * When "name" is empty, return "cmd" history.
5292 * Returns -1 for unknown history name.
5293 */
5294 int
5295get_histtype(name)
5296 char_u *name;
5297{
5298 int i;
5299 int len = (int)STRLEN(name);
5300
5301 /* No argument: use current history. */
5302 if (len == 0)
5303 return hist_char2type(ccline.cmdfirstc);
5304
5305 for (i = 0; history_names[i] != NULL; ++i)
5306 if (STRNICMP(name, history_names[i], len) == 0)
5307 return i;
5308
5309 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5310 return hist_char2type(name[0]);
5311
5312 return -1;
5313}
5314
5315static int last_maptick = -1; /* last seen maptick */
5316
5317/*
5318 * Add the given string to the given history. If the string is already in the
5319 * history then it is moved to the front. "histype" may be one of he HIST_
5320 * values.
5321 */
5322 void
5323add_to_history(histype, new_entry, in_map, sep)
5324 int histype;
5325 char_u *new_entry;
5326 int in_map; /* consider maptick when inside a mapping */
5327 int sep; /* separator character used (search hist) */
5328{
5329 histentry_T *hisptr;
5330 int len;
5331
5332 if (hislen == 0) /* no history */
5333 return;
5334
5335 /*
5336 * Searches inside the same mapping overwrite each other, so that only
5337 * the last line is kept. Be careful not to remove a line that was moved
5338 * down, only lines that were added.
5339 */
5340 if (histype == HIST_SEARCH && in_map)
5341 {
5342 if (maptick == last_maptick)
5343 {
5344 /* Current line is from the same mapping, remove it */
5345 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5346 vim_free(hisptr->hisstr);
5347 hisptr->hisstr = NULL;
5348 hisptr->hisnum = 0;
5349 --hisnum[histype];
5350 if (--hisidx[HIST_SEARCH] < 0)
5351 hisidx[HIST_SEARCH] = hislen - 1;
5352 }
5353 last_maptick = -1;
5354 }
5355 if (!in_history(histype, new_entry, TRUE))
5356 {
5357 if (++hisidx[histype] == hislen)
5358 hisidx[histype] = 0;
5359 hisptr = &history[histype][hisidx[histype]];
5360 vim_free(hisptr->hisstr);
5361
5362 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005363 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5365 if (hisptr->hisstr != NULL)
5366 hisptr->hisstr[len + 1] = sep;
5367
5368 hisptr->hisnum = ++hisnum[histype];
5369 if (histype == HIST_SEARCH && in_map)
5370 last_maptick = maptick;
5371 }
5372}
5373
5374#if defined(FEAT_EVAL) || defined(PROTO)
5375
5376/*
5377 * Get identifier of newest history entry.
5378 * "histype" may be one of the HIST_ values.
5379 */
5380 int
5381get_history_idx(histype)
5382 int histype;
5383{
5384 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5385 || hisidx[histype] < 0)
5386 return -1;
5387
5388 return history[histype][hisidx[histype]].hisnum;
5389}
5390
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005391static struct cmdline_info *get_ccline_ptr __ARGS((void));
5392
5393/*
5394 * Get pointer to the command line info to use. cmdline_paste() may clear
5395 * ccline and put the previous value in prev_ccline.
5396 */
5397 static struct cmdline_info *
5398get_ccline_ptr()
5399{
5400 if ((State & CMDLINE) == 0)
5401 return NULL;
5402 if (ccline.cmdbuff != NULL)
5403 return &ccline;
5404 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5405 return &prev_ccline;
5406 return NULL;
5407}
5408
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409/*
5410 * Get the current command line in allocated memory.
5411 * Only works when the command line is being edited.
5412 * Returns NULL when something is wrong.
5413 */
5414 char_u *
5415get_cmdline_str()
5416{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005417 struct cmdline_info *p = get_ccline_ptr();
5418
5419 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005421 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422}
5423
5424/*
5425 * Get the current command line position, counted in bytes.
5426 * Zero is the first position.
5427 * Only works when the command line is being edited.
5428 * Returns -1 when something is wrong.
5429 */
5430 int
5431get_cmdline_pos()
5432{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005433 struct cmdline_info *p = get_ccline_ptr();
5434
5435 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005437 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438}
5439
5440/*
5441 * Set the command line byte position to "pos". Zero is the first position.
5442 * Only works when the command line is being edited.
5443 * Returns 1 when failed, 0 when OK.
5444 */
5445 int
5446set_cmdline_pos(pos)
5447 int pos;
5448{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005449 struct cmdline_info *p = get_ccline_ptr();
5450
5451 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 return 1;
5453
5454 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5455 * changed the command line. */
5456 if (pos < 0)
5457 new_cmdpos = 0;
5458 else
5459 new_cmdpos = pos;
5460 return 0;
5461}
5462
5463/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005464 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005465 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005466 * Only works when the command line is being edited.
5467 * Returns NUL when something is wrong.
5468 */
5469 int
5470get_cmdline_type()
5471{
5472 struct cmdline_info *p = get_ccline_ptr();
5473
5474 if (p == NULL)
5475 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005476 if (p->cmdfirstc == NUL)
5477 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005478 return p->cmdfirstc;
5479}
5480
5481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 * Calculate history index from a number:
5483 * num > 0: seen as identifying number of a history entry
5484 * num < 0: relative position in history wrt newest entry
5485 * "histype" may be one of the HIST_ values.
5486 */
5487 static int
5488calc_hist_idx(histype, num)
5489 int histype;
5490 int num;
5491{
5492 int i;
5493 histentry_T *hist;
5494 int wrapped = FALSE;
5495
5496 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5497 || (i = hisidx[histype]) < 0 || num == 0)
5498 return -1;
5499
5500 hist = history[histype];
5501 if (num > 0)
5502 {
5503 while (hist[i].hisnum > num)
5504 if (--i < 0)
5505 {
5506 if (wrapped)
5507 break;
5508 i += hislen;
5509 wrapped = TRUE;
5510 }
5511 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5512 return i;
5513 }
5514 else if (-num <= hislen)
5515 {
5516 i += num + 1;
5517 if (i < 0)
5518 i += hislen;
5519 if (hist[i].hisstr != NULL)
5520 return i;
5521 }
5522 return -1;
5523}
5524
5525/*
5526 * Get a history entry by its index.
5527 * "histype" may be one of the HIST_ values.
5528 */
5529 char_u *
5530get_history_entry(histype, idx)
5531 int histype;
5532 int idx;
5533{
5534 idx = calc_hist_idx(histype, idx);
5535 if (idx >= 0)
5536 return history[histype][idx].hisstr;
5537 else
5538 return (char_u *)"";
5539}
5540
5541/*
5542 * Clear all entries of a history.
5543 * "histype" may be one of the HIST_ values.
5544 */
5545 int
5546clr_history(histype)
5547 int histype;
5548{
5549 int i;
5550 histentry_T *hisptr;
5551
5552 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5553 {
5554 hisptr = history[histype];
5555 for (i = hislen; i--;)
5556 {
5557 vim_free(hisptr->hisstr);
5558 hisptr->hisnum = 0;
5559 hisptr++->hisstr = NULL;
5560 }
5561 hisidx[histype] = -1; /* mark history as cleared */
5562 hisnum[histype] = 0; /* reset identifier counter */
5563 return OK;
5564 }
5565 return FAIL;
5566}
5567
5568/*
5569 * Remove all entries matching {str} from a history.
5570 * "histype" may be one of the HIST_ values.
5571 */
5572 int
5573del_history_entry(histype, str)
5574 int histype;
5575 char_u *str;
5576{
5577 regmatch_T regmatch;
5578 histentry_T *hisptr;
5579 int idx;
5580 int i;
5581 int last;
5582 int found = FALSE;
5583
5584 regmatch.regprog = NULL;
5585 regmatch.rm_ic = FALSE; /* always match case */
5586 if (hislen != 0
5587 && histype >= 0
5588 && histype < HIST_COUNT
5589 && *str != NUL
5590 && (idx = hisidx[histype]) >= 0
5591 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5592 != NULL)
5593 {
5594 i = last = idx;
5595 do
5596 {
5597 hisptr = &history[histype][i];
5598 if (hisptr->hisstr == NULL)
5599 break;
5600 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5601 {
5602 found = TRUE;
5603 vim_free(hisptr->hisstr);
5604 hisptr->hisstr = NULL;
5605 hisptr->hisnum = 0;
5606 }
5607 else
5608 {
5609 if (i != last)
5610 {
5611 history[histype][last] = *hisptr;
5612 hisptr->hisstr = NULL;
5613 hisptr->hisnum = 0;
5614 }
5615 if (--last < 0)
5616 last += hislen;
5617 }
5618 if (--i < 0)
5619 i += hislen;
5620 } while (i != idx);
5621 if (history[histype][idx].hisstr == NULL)
5622 hisidx[histype] = -1;
5623 }
5624 vim_free(regmatch.regprog);
5625 return found;
5626}
5627
5628/*
5629 * Remove an indexed entry from a history.
5630 * "histype" may be one of the HIST_ values.
5631 */
5632 int
5633del_history_idx(histype, idx)
5634 int histype;
5635 int idx;
5636{
5637 int i, j;
5638
5639 i = calc_hist_idx(histype, idx);
5640 if (i < 0)
5641 return FALSE;
5642 idx = hisidx[histype];
5643 vim_free(history[histype][i].hisstr);
5644
5645 /* When deleting the last added search string in a mapping, reset
5646 * last_maptick, so that the last added search string isn't deleted again.
5647 */
5648 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5649 last_maptick = -1;
5650
5651 while (i != idx)
5652 {
5653 j = (i + 1) % hislen;
5654 history[histype][i] = history[histype][j];
5655 i = j;
5656 }
5657 history[histype][i].hisstr = NULL;
5658 history[histype][i].hisnum = 0;
5659 if (--i < 0)
5660 i += hislen;
5661 hisidx[histype] = i;
5662 return TRUE;
5663}
5664
5665#endif /* FEAT_EVAL */
5666
5667#if defined(FEAT_CRYPT) || defined(PROTO)
5668/*
5669 * Very specific function to remove the value in ":set key=val" from the
5670 * history.
5671 */
5672 void
5673remove_key_from_history()
5674{
5675 char_u *p;
5676 int i;
5677
5678 i = hisidx[HIST_CMD];
5679 if (i < 0)
5680 return;
5681 p = history[HIST_CMD][i].hisstr;
5682 if (p != NULL)
5683 for ( ; *p; ++p)
5684 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5685 {
5686 p = vim_strchr(p + 3, '=');
5687 if (p == NULL)
5688 break;
5689 ++p;
5690 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5691 if (p[i] == '\\' && p[i + 1])
5692 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005693 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 --p;
5695 }
5696}
5697#endif
5698
5699#endif /* FEAT_CMDHIST */
5700
5701#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5702/*
5703 * Get indices "num1,num2" that specify a range within a list (not a range of
5704 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5705 * Returns OK if parsed successfully, otherwise FAIL.
5706 */
5707 int
5708get_list_range(str, num1, num2)
5709 char_u **str;
5710 int *num1;
5711 int *num2;
5712{
5713 int len;
5714 int first = FALSE;
5715 long num;
5716
5717 *str = skipwhite(*str);
5718 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5719 {
5720 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5721 *str += len;
5722 *num1 = (int)num;
5723 first = TRUE;
5724 }
5725 *str = skipwhite(*str);
5726 if (**str == ',') /* parse "to" part of range */
5727 {
5728 *str = skipwhite(*str + 1);
5729 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5730 if (len > 0)
5731 {
5732 *num2 = (int)num;
5733 *str = skipwhite(*str + len);
5734 }
5735 else if (!first) /* no number given at all */
5736 return FAIL;
5737 }
5738 else if (first) /* only one number given */
5739 *num2 = *num1;
5740 return OK;
5741}
5742#endif
5743
5744#if defined(FEAT_CMDHIST) || defined(PROTO)
5745/*
5746 * :history command - print a history
5747 */
5748 void
5749ex_history(eap)
5750 exarg_T *eap;
5751{
5752 histentry_T *hist;
5753 int histype1 = HIST_CMD;
5754 int histype2 = HIST_CMD;
5755 int hisidx1 = 1;
5756 int hisidx2 = -1;
5757 int idx;
5758 int i, j, k;
5759 char_u *end;
5760 char_u *arg = eap->arg;
5761
5762 if (hislen == 0)
5763 {
5764 MSG(_("'history' option is zero"));
5765 return;
5766 }
5767
5768 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5769 {
5770 end = arg;
5771 while (ASCII_ISALPHA(*end)
5772 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5773 end++;
5774 i = *end;
5775 *end = NUL;
5776 histype1 = get_histtype(arg);
5777 if (histype1 == -1)
5778 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005779 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 {
5781 histype1 = 0;
5782 histype2 = HIST_COUNT-1;
5783 }
5784 else
5785 {
5786 *end = i;
5787 EMSG(_(e_trailing));
5788 return;
5789 }
5790 }
5791 else
5792 histype2 = histype1;
5793 *end = i;
5794 }
5795 else
5796 end = arg;
5797 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5798 {
5799 EMSG(_(e_trailing));
5800 return;
5801 }
5802
5803 for (; !got_int && histype1 <= histype2; ++histype1)
5804 {
5805 STRCPY(IObuff, "\n # ");
5806 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5807 MSG_PUTS_TITLE(IObuff);
5808 idx = hisidx[histype1];
5809 hist = history[histype1];
5810 j = hisidx1;
5811 k = hisidx2;
5812 if (j < 0)
5813 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5814 if (k < 0)
5815 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5816 if (idx >= 0 && j <= k)
5817 for (i = idx + 1; !got_int; ++i)
5818 {
5819 if (i == hislen)
5820 i = 0;
5821 if (hist[i].hisstr != NULL
5822 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5823 {
5824 msg_putchar('\n');
5825 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5826 hist[i].hisnum);
5827 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5828 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5829 (int)Columns - 10);
5830 else
5831 STRCAT(IObuff, hist[i].hisstr);
5832 msg_outtrans(IObuff);
5833 out_flush();
5834 }
5835 if (i == idx)
5836 break;
5837 }
5838 }
5839}
5840#endif
5841
5842#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5843static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5844static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5845static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5846static int viminfo_add_at_front = FALSE;
5847
5848static int hist_type2char __ARGS((int type, int use_question));
5849
5850/*
5851 * Translate a history type number to the associated character.
5852 */
5853 static int
5854hist_type2char(type, use_question)
5855 int type;
5856 int use_question; /* use '?' instead of '/' */
5857{
5858 if (type == HIST_CMD)
5859 return ':';
5860 if (type == HIST_SEARCH)
5861 {
5862 if (use_question)
5863 return '?';
5864 else
5865 return '/';
5866 }
5867 if (type == HIST_EXPR)
5868 return '=';
5869 return '@';
5870}
5871
5872/*
5873 * Prepare for reading the history from the viminfo file.
5874 * This allocates history arrays to store the read history lines.
5875 */
5876 void
5877prepare_viminfo_history(asklen)
5878 int asklen;
5879{
5880 int i;
5881 int num;
5882 int type;
5883 int len;
5884
5885 init_history();
5886 viminfo_add_at_front = (asklen != 0);
5887 if (asklen > hislen)
5888 asklen = hislen;
5889
5890 for (type = 0; type < HIST_COUNT; ++type)
5891 {
5892 /*
5893 * Count the number of empty spaces in the history list. If there are
5894 * more spaces available than we request, then fill them up.
5895 */
5896 for (i = 0, num = 0; i < hislen; i++)
5897 if (history[type][i].hisstr == NULL)
5898 num++;
5899 len = asklen;
5900 if (num > len)
5901 len = num;
5902 if (len <= 0)
5903 viminfo_history[type] = NULL;
5904 else
5905 viminfo_history[type] =
5906 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5907 if (viminfo_history[type] == NULL)
5908 len = 0;
5909 viminfo_hislen[type] = len;
5910 viminfo_hisidx[type] = 0;
5911 }
5912}
5913
5914/*
5915 * Accept a line from the viminfo, store it in the history array when it's
5916 * new.
5917 */
5918 int
5919read_viminfo_history(virp)
5920 vir_T *virp;
5921{
5922 int type;
5923 long_u len;
5924 char_u *val;
5925 char_u *p;
5926
5927 type = hist_char2type(virp->vir_line[0]);
5928 if (viminfo_hisidx[type] < viminfo_hislen[type])
5929 {
5930 val = viminfo_readstring(virp, 1, TRUE);
5931 if (val != NULL && *val != NUL)
5932 {
5933 if (!in_history(type, val + (type == HIST_SEARCH),
5934 viminfo_add_at_front))
5935 {
5936 /* Need to re-allocate to append the separator byte. */
5937 len = STRLEN(val);
5938 p = lalloc(len + 2, TRUE);
5939 if (p != NULL)
5940 {
5941 if (type == HIST_SEARCH)
5942 {
5943 /* Search entry: Move the separator from the first
5944 * column to after the NUL. */
5945 mch_memmove(p, val + 1, (size_t)len);
5946 p[len] = (*val == ' ' ? NUL : *val);
5947 }
5948 else
5949 {
5950 /* Not a search entry: No separator in the viminfo
5951 * file, add a NUL separator. */
5952 mch_memmove(p, val, (size_t)len + 1);
5953 p[len + 1] = NUL;
5954 }
5955 viminfo_history[type][viminfo_hisidx[type]++] = p;
5956 }
5957 }
5958 }
5959 vim_free(val);
5960 }
5961 return viminfo_readline(virp);
5962}
5963
5964 void
5965finish_viminfo_history()
5966{
5967 int idx;
5968 int i;
5969 int type;
5970
5971 for (type = 0; type < HIST_COUNT; ++type)
5972 {
5973 if (history[type] == NULL)
5974 return;
5975 idx = hisidx[type] + viminfo_hisidx[type];
5976 if (idx >= hislen)
5977 idx -= hislen;
5978 else if (idx < 0)
5979 idx = hislen - 1;
5980 if (viminfo_add_at_front)
5981 hisidx[type] = idx;
5982 else
5983 {
5984 if (hisidx[type] == -1)
5985 hisidx[type] = hislen - 1;
5986 do
5987 {
5988 if (history[type][idx].hisstr != NULL)
5989 break;
5990 if (++idx == hislen)
5991 idx = 0;
5992 } while (idx != hisidx[type]);
5993 if (idx != hisidx[type] && --idx < 0)
5994 idx = hislen - 1;
5995 }
5996 for (i = 0; i < viminfo_hisidx[type]; i++)
5997 {
5998 vim_free(history[type][idx].hisstr);
5999 history[type][idx].hisstr = viminfo_history[type][i];
6000 if (--idx < 0)
6001 idx = hislen - 1;
6002 }
6003 idx += 1;
6004 idx %= hislen;
6005 for (i = 0; i < viminfo_hisidx[type]; i++)
6006 {
6007 history[type][idx++].hisnum = ++hisnum[type];
6008 idx %= hislen;
6009 }
6010 vim_free(viminfo_history[type]);
6011 viminfo_history[type] = NULL;
6012 }
6013}
6014
6015 void
6016write_viminfo_history(fp)
6017 FILE *fp;
6018{
6019 int i;
6020 int type;
6021 int num_saved;
6022 char_u *p;
6023 int c;
6024
6025 init_history();
6026 if (hislen == 0)
6027 return;
6028 for (type = 0; type < HIST_COUNT; ++type)
6029 {
6030 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6031 if (num_saved == 0)
6032 continue;
6033 if (num_saved < 0) /* Use default */
6034 num_saved = hislen;
6035 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6036 type == HIST_CMD ? _("Command Line") :
6037 type == HIST_SEARCH ? _("Search String") :
6038 type == HIST_EXPR ? _("Expression") :
6039 _("Input Line"));
6040 if (num_saved > hislen)
6041 num_saved = hislen;
6042 i = hisidx[type];
6043 if (i >= 0)
6044 while (num_saved--)
6045 {
6046 p = history[type][i].hisstr;
6047 if (p != NULL)
6048 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006049 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 /* For the search history: put the separator in the second
6051 * column; use a space if there isn't one. */
6052 if (type == HIST_SEARCH)
6053 {
6054 c = p[STRLEN(p) + 1];
6055 putc(c == NUL ? ' ' : c, fp);
6056 }
6057 viminfo_writestring(fp, p);
6058 }
6059 if (--i < 0)
6060 i = hislen - 1;
6061 }
6062 }
6063}
6064#endif /* FEAT_VIMINFO */
6065
6066#if defined(FEAT_FKMAP) || defined(PROTO)
6067/*
6068 * Write a character at the current cursor+offset position.
6069 * It is directly written into the command buffer block.
6070 */
6071 void
6072cmd_pchar(c, offset)
6073 int c, offset;
6074{
6075 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6076 {
6077 EMSG(_("E198: cmd_pchar beyond the command length"));
6078 return;
6079 }
6080 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6081 ccline.cmdbuff[ccline.cmdlen] = NUL;
6082}
6083
6084 int
6085cmd_gchar(offset)
6086 int offset;
6087{
6088 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6089 {
6090 /* EMSG(_("cmd_gchar beyond the command length")); */
6091 return NUL;
6092 }
6093 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6094}
6095#endif
6096
6097#if defined(FEAT_CMDWIN) || defined(PROTO)
6098/*
6099 * Open a window on the current command line and history. Allow editing in
6100 * the window. Returns when the window is closed.
6101 * Returns:
6102 * CR if the command is to be executed
6103 * Ctrl_C if it is to be abandoned
6104 * K_IGNORE if editing continues
6105 */
6106 static int
6107ex_window()
6108{
6109 struct cmdline_info save_ccline;
6110 buf_T *old_curbuf = curbuf;
6111 win_T *old_curwin = curwin;
6112 buf_T *bp;
6113 win_T *wp;
6114 int i;
6115 linenr_T lnum;
6116 int histtype;
6117 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006118#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 int save_restart_edit = restart_edit;
6122 int save_State = State;
6123 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006124#ifdef FEAT_RIGHTLEFT
6125 int save_cmdmsg_rl = cmdmsg_rl;
6126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127
6128 /* Can't do this recursively. Can't do it when typing a password. */
6129 if (cmdwin_type != 0
6130# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6131 || cmdline_star > 0
6132# endif
6133 )
6134 {
6135 beep_flush();
6136 return K_IGNORE;
6137 }
6138
6139 /* Save current window sizes. */
6140 win_size_save(&winsizes);
6141
6142# ifdef FEAT_AUTOCMD
6143 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006144 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006146 /* don't use a new tab page */
6147 cmdmod.tab = 0;
6148
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 /* Create a window for the command-line buffer. */
6150 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6151 {
6152 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006153# ifdef FEAT_AUTOCMD
6154 unblock_autocmds();
6155# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 return K_IGNORE;
6157 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006158 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
6160 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006161 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006162 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6164 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6165 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006166#ifdef FEAT_FOLDING
6167 curwin->w_p_fen = FALSE;
6168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006170 curwin->w_p_rl = cmdmsg_rl;
6171 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006173 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174
6175# ifdef FEAT_AUTOCMD
6176 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006177 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178# endif
6179
Bram Moolenaar46152342005-09-07 21:18:43 +00006180 /* Showing the prompt may have set need_wait_return, reset it. */
6181 need_wait_return = FALSE;
6182
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006183 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6185 {
6186 if (p_wc == TAB)
6187 {
6188 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6189 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6190 }
6191 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6192 }
6193
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006194 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6195 * sets 'textwidth' to 78). */
6196 curbuf->b_p_tw = 0;
6197
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 /* Fill the buffer with the history. */
6199 init_history();
6200 if (hislen > 0)
6201 {
6202 i = hisidx[histtype];
6203 if (i >= 0)
6204 {
6205 lnum = 0;
6206 do
6207 {
6208 if (++i == hislen)
6209 i = 0;
6210 if (history[histtype][i].hisstr != NULL)
6211 ml_append(lnum++, history[histtype][i].hisstr,
6212 (colnr_T)0, FALSE);
6213 }
6214 while (i != hisidx[histtype]);
6215 }
6216 }
6217
6218 /* Replace the empty last line with the current command-line and put the
6219 * cursor there. */
6220 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6221 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6222 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006223 changed_line_abv_curs();
6224 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006225 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226
6227 /* Save the command line info, can be used recursively. */
6228 save_ccline = ccline;
6229 ccline.cmdbuff = NULL;
6230 ccline.cmdprompt = NULL;
6231
6232 /* No Ex mode here! */
6233 exmode_active = 0;
6234
6235 State = NORMAL;
6236# ifdef FEAT_MOUSE
6237 setmouse();
6238# endif
6239
6240# ifdef FEAT_AUTOCMD
6241 /* Trigger CmdwinEnter autocommands. */
6242 typestr[0] = cmdwin_type;
6243 typestr[1] = NUL;
6244 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006245 if (restart_edit != 0) /* autocmd with ":startinsert" */
6246 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247# endif
6248
6249 i = RedrawingDisabled;
6250 RedrawingDisabled = 0;
6251
6252 /*
6253 * Call the main loop until <CR> or CTRL-C is typed.
6254 */
6255 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006256 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
6258 RedrawingDisabled = i;
6259
6260# ifdef FEAT_AUTOCMD
6261 /* Trigger CmdwinLeave autocommands. */
6262 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6263# endif
6264
Bram Moolenaarccc18222007-05-10 18:25:20 +00006265 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 ccline = save_ccline;
6267 cmdwin_type = 0;
6268
6269 exmode_active = save_exmode;
6270
Bram Moolenaarf9821062008-06-20 16:31:07 +00006271 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 * this happens! */
6273 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6274 {
6275 cmdwin_result = Ctrl_C;
6276 EMSG(_("E199: Active window or buffer deleted"));
6277 }
6278 else
6279 {
6280# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6281 /* autocmds may abort script processing */
6282 if (aborting() && cmdwin_result != K_IGNORE)
6283 cmdwin_result = Ctrl_C;
6284# endif
6285 /* Set the new command line from the cmdline buffer. */
6286 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006287 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006289 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6290
6291 if (histtype == HIST_CMD)
6292 {
6293 /* Execute the command directly. */
6294 ccline.cmdbuff = vim_strsave((char_u *)p);
6295 cmdwin_result = CAR;
6296 }
6297 else
6298 {
6299 /* First need to cancel what we were doing. */
6300 ccline.cmdbuff = NULL;
6301 stuffcharReadbuff(':');
6302 stuffReadbuff((char_u *)p);
6303 stuffcharReadbuff(CAR);
6304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 }
6306 else if (cmdwin_result == K_XF2) /* :qa typed */
6307 {
6308 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6309 cmdwin_result = CAR;
6310 }
6311 else
6312 ccline.cmdbuff = vim_strsave(ml_get_curline());
6313 if (ccline.cmdbuff == NULL)
6314 cmdwin_result = Ctrl_C;
6315 else
6316 {
6317 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6318 ccline.cmdbufflen = ccline.cmdlen + 1;
6319 ccline.cmdpos = curwin->w_cursor.col;
6320 if (ccline.cmdpos > ccline.cmdlen)
6321 ccline.cmdpos = ccline.cmdlen;
6322 if (cmdwin_result == K_IGNORE)
6323 {
6324 set_cmdspos_cursor();
6325 redrawcmd();
6326 }
6327 }
6328
6329# ifdef FEAT_AUTOCMD
6330 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006331 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332# endif
6333 wp = curwin;
6334 bp = curbuf;
6335 win_goto(old_curwin);
6336 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006337
6338 /* win_close() may have already wiped the buffer when 'bh' is
6339 * set to 'wipe' */
6340 if (buf_valid(bp))
6341 close_buffer(NULL, bp, DOBUF_WIPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342
6343 /* Restore window sizes. */
6344 win_size_restore(&winsizes);
6345
6346# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006347 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348# endif
6349 }
6350
6351 ga_clear(&winsizes);
6352 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006353# ifdef FEAT_RIGHTLEFT
6354 cmdmsg_rl = save_cmdmsg_rl;
6355# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356
6357 State = save_State;
6358# ifdef FEAT_MOUSE
6359 setmouse();
6360# endif
6361
6362 return cmdwin_result;
6363}
6364#endif /* FEAT_CMDWIN */
6365
6366/*
6367 * Used for commands that either take a simple command string argument, or:
6368 * cmd << endmarker
6369 * {script}
6370 * endmarker
6371 * Returns a pointer to allocated memory with {script} or NULL.
6372 */
6373 char_u *
6374script_get(eap, cmd)
6375 exarg_T *eap;
6376 char_u *cmd;
6377{
6378 char_u *theline;
6379 char *end_pattern = NULL;
6380 char dot[] = ".";
6381 garray_T ga;
6382
6383 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6384 return NULL;
6385
6386 ga_init2(&ga, 1, 0x400);
6387
6388 if (cmd[2] != NUL)
6389 end_pattern = (char *)skipwhite(cmd + 2);
6390 else
6391 end_pattern = dot;
6392
6393 for (;;)
6394 {
6395 theline = eap->getline(
6396#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006397 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398#endif
6399 NUL, eap->cookie, 0);
6400
6401 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006402 {
6403 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
6407 ga_concat(&ga, theline);
6408 ga_append(&ga, '\n');
6409 vim_free(theline);
6410 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006411 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412
6413 return (char_u *)ga.ga_data;
6414}