blob: 97c122da7669000a30d969bb3ae929955fdd5e75 [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
Bram Moolenaar4c402232011-07-27 17:58:46 +020070static int in_history __ARGS((int, char_u *, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000071# 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 Moolenaar0c7437a2011-06-26 19:40:23 +0200113static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname[]));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114# 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();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100715 got_int = FALSE; /* don't abandon the command line */
716 did_emsg = FALSE;
717 emsg_on_display = FALSE;
718 redrawcmd();
719 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 }
721#endif
722 else
723 {
724 if (c == Ctrl_G && p_im && restart_edit == 0)
725 restart_edit = 'a';
726 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
727 in history */
728 goto returncmd; /* back to Normal mode */
729 }
730 }
731
732#ifdef FEAT_CMDWIN
733 if (c == cedit_key || c == K_CMDWIN)
734 {
735 /*
736 * Open a window to edit the command line (and history).
737 */
738 c = ex_window();
739 some_key_typed = TRUE;
740 }
741# ifdef FEAT_DIGRAPHS
742 else
743# endif
744#endif
745#ifdef FEAT_DIGRAPHS
746 c = do_digraph(c);
747#endif
748
749 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
750 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
751 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000752 /* In Ex mode a backslash escapes a newline. */
753 if (exmode_active
754 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000755 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000756 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000757 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000759 if (c == K_KENTER)
760 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000762 else
763 {
764 gotesc = FALSE; /* Might have typed ESC previously, don't
765 truncate the cmdline now. */
766 if (ccheck_abbr(c + ABBR_OFF))
767 goto cmdline_changed;
768 if (!cmd_silent)
769 {
770 windgoto(msg_row, 0);
771 out_flush();
772 }
773 break;
774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 }
776
777 /*
778 * Completion for 'wildchar' or 'wildcharm' key.
779 * - hitting <ESC> twice means: abandon command line.
780 * - wildcard expansion is only done when the 'wildchar' key is really
781 * typed, not when it comes from a macro
782 */
783 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
784 {
785 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
786 {
787 /* if 'wildmode' contains "list" may still need to list */
788 if (xpc.xp_numfiles > 1
789 && !did_wild_list
790 && (wim_flags[wim_index] & WIM_LIST))
791 {
792 (void)showmatches(&xpc, FALSE);
793 redrawcmd();
794 did_wild_list = TRUE;
795 }
796 if (wim_flags[wim_index] & WIM_LONGEST)
797 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
798 else if (wim_flags[wim_index] & WIM_FULL)
799 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
800 else
801 res = OK; /* don't insert 'wildchar' now */
802 }
803 else /* typed p_wc first time */
804 {
805 wim_index = 0;
806 j = ccline.cmdpos;
807 /* if 'wildmode' first contains "longest", get longest
808 * common part */
809 if (wim_flags[0] & WIM_LONGEST)
810 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
811 else
812 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
813
814 /* if interrupted while completing, behave like it failed */
815 if (got_int)
816 {
817 (void)vpeekc(); /* remove <C-C> from input stream */
818 got_int = FALSE; /* don't abandon the command line */
819 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
820#ifdef FEAT_WILDMENU
821 xpc.xp_context = EXPAND_NOTHING;
822#endif
823 goto cmdline_changed;
824 }
825
826 /* when more than one match, and 'wildmode' first contains
827 * "list", or no change and 'wildmode' contains "longest,list",
828 * list all matches */
829 if (res == OK && xpc.xp_numfiles > 1)
830 {
831 /* a "longest" that didn't do anything is skipped (but not
832 * "list:longest") */
833 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
834 wim_index = 1;
835 if ((wim_flags[wim_index] & WIM_LIST)
836#ifdef FEAT_WILDMENU
837 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
838#endif
839 )
840 {
841 if (!(wim_flags[0] & WIM_LONGEST))
842 {
843#ifdef FEAT_WILDMENU
844 int p_wmnu_save = p_wmnu;
845 p_wmnu = 0;
846#endif
847 nextwild(&xpc, WILD_PREV, 0); /* remove match */
848#ifdef FEAT_WILDMENU
849 p_wmnu = p_wmnu_save;
850#endif
851 }
852#ifdef FEAT_WILDMENU
853 (void)showmatches(&xpc, p_wmnu
854 && ((wim_flags[wim_index] & WIM_LIST) == 0));
855#else
856 (void)showmatches(&xpc, FALSE);
857#endif
858 redrawcmd();
859 did_wild_list = TRUE;
860 if (wim_flags[wim_index] & WIM_LONGEST)
861 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
862 else if (wim_flags[wim_index] & WIM_FULL)
863 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
864 }
865 else
866 vim_beep();
867 }
868#ifdef FEAT_WILDMENU
869 else if (xpc.xp_numfiles == -1)
870 xpc.xp_context = EXPAND_NOTHING;
871#endif
872 }
873 if (wim_index < 3)
874 ++wim_index;
875 if (c == ESC)
876 gotesc = TRUE;
877 if (res == OK)
878 goto cmdline_changed;
879 }
880
881 gotesc = FALSE;
882
883 /* <S-Tab> goes to last match, in a clumsy way */
884 if (c == K_S_TAB && KeyTyped)
885 {
886 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
887 && nextwild(&xpc, WILD_PREV, 0) == OK
888 && nextwild(&xpc, WILD_PREV, 0) == OK)
889 goto cmdline_changed;
890 }
891
892 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
893 c = NL;
894
895 do_abbr = TRUE; /* default: check for abbreviation */
896
897 /*
898 * Big switch for a typed command line character.
899 */
900 switch (c)
901 {
902 case K_BS:
903 case Ctrl_H:
904 case K_DEL:
905 case K_KDEL:
906 case Ctrl_W:
907#ifdef FEAT_FKMAP
908 if (cmd_fkmap && c == K_BS)
909 c = K_DEL;
910#endif
911 if (c == K_KDEL)
912 c = K_DEL;
913
914 /*
915 * delete current character is the same as backspace on next
916 * character, except at end of line
917 */
918 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
919 ++ccline.cmdpos;
920#ifdef FEAT_MBYTE
921 if (has_mbyte && c == K_DEL)
922 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
923 ccline.cmdbuff + ccline.cmdpos);
924#endif
925 if (ccline.cmdpos > 0)
926 {
927 char_u *p;
928
929 j = ccline.cmdpos;
930 p = ccline.cmdbuff + j;
931#ifdef FEAT_MBYTE
932 if (has_mbyte)
933 {
934 p = mb_prevptr(ccline.cmdbuff, p);
935 if (c == Ctrl_W)
936 {
937 while (p > ccline.cmdbuff && vim_isspace(*p))
938 p = mb_prevptr(ccline.cmdbuff, p);
939 i = mb_get_class(p);
940 while (p > ccline.cmdbuff && mb_get_class(p) == i)
941 p = mb_prevptr(ccline.cmdbuff, p);
942 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000943 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
945 }
946 else
947#endif
948 if (c == Ctrl_W)
949 {
950 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
951 --p;
952 i = vim_iswordc(p[-1]);
953 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
954 && vim_iswordc(p[-1]) == i)
955 --p;
956 }
957 else
958 --p;
959 ccline.cmdpos = (int)(p - ccline.cmdbuff);
960 ccline.cmdlen -= j - ccline.cmdpos;
961 i = ccline.cmdpos;
962 while (i < ccline.cmdlen)
963 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
964
965 /* Truncate at the end, required for multi-byte chars. */
966 ccline.cmdbuff[ccline.cmdlen] = NUL;
967 redrawcmd();
968 }
969 else if (ccline.cmdlen == 0 && c != Ctrl_W
970 && ccline.cmdprompt == NULL && indent == 0)
971 {
972 /* In ex and debug mode it doesn't make sense to return. */
973 if (exmode_active
974#ifdef FEAT_EVAL
975 || ccline.cmdfirstc == '>'
976#endif
977 )
978 goto cmdline_not_changed;
979
980 vim_free(ccline.cmdbuff); /* no commandline to return */
981 ccline.cmdbuff = NULL;
982 if (!cmd_silent)
983 {
984#ifdef FEAT_RIGHTLEFT
985 if (cmdmsg_rl)
986 msg_col = Columns;
987 else
988#endif
989 msg_col = 0;
990 msg_putchar(' '); /* delete ':' */
991 }
992 redraw_cmdline = TRUE;
993 goto returncmd; /* back to cmd mode */
994 }
995 goto cmdline_changed;
996
997 case K_INS:
998 case K_KINS:
999#ifdef FEAT_FKMAP
1000 /* if Farsi mode set, we are in reverse insert mode -
1001 Do not change the mode */
1002 if (cmd_fkmap)
1003 beep_flush();
1004 else
1005#endif
1006 ccline.overstrike = !ccline.overstrike;
1007#ifdef CURSOR_SHAPE
1008 ui_cursor_shape(); /* may show different cursor shape */
1009#endif
1010 goto cmdline_not_changed;
1011
1012 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001013 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 {
1015 /* ":lmap" mappings exists, toggle use of mappings. */
1016 State ^= LANGMAP;
1017#ifdef USE_IM_CONTROL
1018 im_set_active(FALSE); /* Disable input method */
1019#endif
1020 if (b_im_ptr != NULL)
1021 {
1022 if (State & LANGMAP)
1023 *b_im_ptr = B_IMODE_LMAP;
1024 else
1025 *b_im_ptr = B_IMODE_NONE;
1026 }
1027 }
1028#ifdef USE_IM_CONTROL
1029 else
1030 {
1031 /* There are no ":lmap" mappings, toggle IM. When
1032 * 'imdisable' is set don't try getting the status, it's
1033 * always off. */
1034 if ((p_imdisable && b_im_ptr != NULL)
1035 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1036 {
1037 im_set_active(FALSE); /* Disable input method */
1038 if (b_im_ptr != NULL)
1039 *b_im_ptr = B_IMODE_NONE;
1040 }
1041 else
1042 {
1043 im_set_active(TRUE); /* Enable input method */
1044 if (b_im_ptr != NULL)
1045 *b_im_ptr = B_IMODE_IM;
1046 }
1047 }
1048#endif
1049 if (b_im_ptr != NULL)
1050 {
1051 if (b_im_ptr == &curbuf->b_p_iminsert)
1052 set_iminsert_global();
1053 else
1054 set_imsearch_global();
1055 }
1056#ifdef CURSOR_SHAPE
1057 ui_cursor_shape(); /* may show different cursor shape */
1058#endif
1059#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1060 /* Show/unshow value of 'keymap' in status lines later. */
1061 status_redraw_curbuf();
1062#endif
1063 goto cmdline_not_changed;
1064
1065/* case '@': only in very old vi */
1066 case Ctrl_U:
1067 /* delete all characters left of the cursor */
1068 j = ccline.cmdpos;
1069 ccline.cmdlen -= j;
1070 i = ccline.cmdpos = 0;
1071 while (i < ccline.cmdlen)
1072 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1073 /* Truncate at the end, required for multi-byte chars. */
1074 ccline.cmdbuff[ccline.cmdlen] = NUL;
1075 redrawcmd();
1076 goto cmdline_changed;
1077
1078#ifdef FEAT_CLIPBOARD
1079 case Ctrl_Y:
1080 /* Copy the modeless selection, if there is one. */
1081 if (clip_star.state != SELECT_CLEARED)
1082 {
1083 if (clip_star.state == SELECT_DONE)
1084 clip_copy_modeless_selection(TRUE);
1085 goto cmdline_not_changed;
1086 }
1087 break;
1088#endif
1089
1090 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1091 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001092 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001093 * ":normal" runs out of characters. */
1094 if (exmode_active
1095#ifdef FEAT_EX_EXTRA
1096 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1097#endif
1098 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 goto cmdline_not_changed;
1100
1101 gotesc = TRUE; /* will free ccline.cmdbuff after
1102 putting it in history */
1103 goto returncmd; /* back to cmd mode */
1104
1105 case Ctrl_R: /* insert register */
1106#ifdef USE_ON_FLY_SCROLL
1107 dont_scroll = TRUE; /* disallow scrolling here */
1108#endif
1109 putcmdline('"', TRUE);
1110 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001111 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 if (i == Ctrl_O)
1113 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1114 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001115 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 --no_mapping;
1117#ifdef FEAT_EVAL
1118 /*
1119 * Insert the result of an expression.
1120 * Need to save the current command line, to be able to enter
1121 * a new one...
1122 */
1123 new_cmdpos = -1;
1124 if (c == '=')
1125 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1127 {
1128 beep_flush();
1129 c = ESC;
1130 }
1131 else
1132 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001133 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001135 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 }
1137 }
1138#endif
1139 if (c != ESC) /* use ESC to cancel inserting register */
1140 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001141 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001142
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001143#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001144 /* When there was a serious error abort getting the
1145 * command line. */
1146 if (aborting())
1147 {
1148 gotesc = TRUE; /* will free ccline.cmdbuff after
1149 putting it in history */
1150 goto returncmd; /* back to cmd mode */
1151 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 KeyTyped = FALSE; /* Don't do p_wc completion. */
1154#ifdef FEAT_EVAL
1155 if (new_cmdpos >= 0)
1156 {
1157 /* set_cmdline_pos() was used */
1158 if (new_cmdpos > ccline.cmdlen)
1159 ccline.cmdpos = ccline.cmdlen;
1160 else
1161 ccline.cmdpos = new_cmdpos;
1162 }
1163#endif
1164 }
1165 redrawcmd();
1166 goto cmdline_changed;
1167
1168 case Ctrl_D:
1169 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1170 break; /* Use ^D as normal char instead */
1171
1172 redrawcmd();
1173 continue; /* don't do incremental search now */
1174
1175 case K_RIGHT:
1176 case K_S_RIGHT:
1177 case K_C_RIGHT:
1178 do
1179 {
1180 if (ccline.cmdpos >= ccline.cmdlen)
1181 break;
1182 i = cmdline_charsize(ccline.cmdpos);
1183 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1184 break;
1185 ccline.cmdspos += i;
1186#ifdef FEAT_MBYTE
1187 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001188 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 + ccline.cmdpos);
1190 else
1191#endif
1192 ++ccline.cmdpos;
1193 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001194 while ((c == K_S_RIGHT || c == K_C_RIGHT
1195 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1197#ifdef FEAT_MBYTE
1198 if (has_mbyte)
1199 set_cmdspos_cursor();
1200#endif
1201 goto cmdline_not_changed;
1202
1203 case K_LEFT:
1204 case K_S_LEFT:
1205 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001206 if (ccline.cmdpos == 0)
1207 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 do
1209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 --ccline.cmdpos;
1211#ifdef FEAT_MBYTE
1212 if (has_mbyte) /* move to first byte of char */
1213 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1214 ccline.cmdbuff + ccline.cmdpos);
1215#endif
1216 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1217 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001218 while (ccline.cmdpos > 0
1219 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001220 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1222#ifdef FEAT_MBYTE
1223 if (has_mbyte)
1224 set_cmdspos_cursor();
1225#endif
1226 goto cmdline_not_changed;
1227
1228 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001229 /* Ignore mouse event or ex_window() result. */
1230 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
Bram Moolenaar4770d092006-01-12 23:22:24 +00001232#ifdef FEAT_GUI_W32
1233 /* On Win32 ignore <M-F4>, we get it when closing the window was
1234 * cancelled. */
1235 case K_F4:
1236 if (mod_mask == MOD_MASK_ALT)
1237 {
1238 redrawcmd(); /* somehow the cmdline is cleared */
1239 goto cmdline_not_changed;
1240 }
1241 break;
1242#endif
1243
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244#ifdef FEAT_MOUSE
1245 case K_MIDDLEDRAG:
1246 case K_MIDDLERELEASE:
1247 goto cmdline_not_changed; /* Ignore mouse */
1248
1249 case K_MIDDLEMOUSE:
1250# ifdef FEAT_GUI
1251 /* When GUI is active, also paste when 'mouse' is empty */
1252 if (!gui.in_use)
1253# endif
1254 if (!mouse_has(MOUSE_COMMAND))
1255 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001256# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001258 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001260# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001261 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 redrawcmd();
1263 goto cmdline_changed;
1264
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001265# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001267 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 redrawcmd();
1269 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271
1272 case K_LEFTDRAG:
1273 case K_LEFTRELEASE:
1274 case K_RIGHTDRAG:
1275 case K_RIGHTRELEASE:
1276 /* Ignore drag and release events when the button-down wasn't
1277 * seen before. */
1278 if (ignore_drag_release)
1279 goto cmdline_not_changed;
1280 /* FALLTHROUGH */
1281 case K_LEFTMOUSE:
1282 case K_RIGHTMOUSE:
1283 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1284 ignore_drag_release = TRUE;
1285 else
1286 ignore_drag_release = FALSE;
1287# ifdef FEAT_GUI
1288 /* When GUI is active, also move when 'mouse' is empty */
1289 if (!gui.in_use)
1290# endif
1291 if (!mouse_has(MOUSE_COMMAND))
1292 goto cmdline_not_changed; /* Ignore mouse */
1293# ifdef FEAT_CLIPBOARD
1294 if (mouse_row < cmdline_row && clip_star.available)
1295 {
1296 int button, is_click, is_drag;
1297
1298 /*
1299 * Handle modeless selection.
1300 */
1301 button = get_mouse_button(KEY2TERMCAP1(c),
1302 &is_click, &is_drag);
1303 if (mouse_model_popup() && button == MOUSE_LEFT
1304 && (mod_mask & MOD_MASK_SHIFT))
1305 {
1306 /* Translate shift-left to right button. */
1307 button = MOUSE_RIGHT;
1308 mod_mask &= ~MOD_MASK_SHIFT;
1309 }
1310 clip_modeless(button, is_click, is_drag);
1311 goto cmdline_not_changed;
1312 }
1313# endif
1314
1315 set_cmdspos();
1316 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1317 ++ccline.cmdpos)
1318 {
1319 i = cmdline_charsize(ccline.cmdpos);
1320 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1321 && mouse_col < ccline.cmdspos % Columns + i)
1322 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001323# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 if (has_mbyte)
1325 {
1326 /* Count ">" for double-wide char that doesn't fit. */
1327 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001328 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 + ccline.cmdpos) - 1;
1330 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001331# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 ccline.cmdspos += i;
1333 }
1334 goto cmdline_not_changed;
1335
1336 /* Mouse scroll wheel: ignored here */
1337 case K_MOUSEDOWN:
1338 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001339 case K_MOUSELEFT:
1340 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 /* Alternate buttons ignored here */
1342 case K_X1MOUSE:
1343 case K_X1DRAG:
1344 case K_X1RELEASE:
1345 case K_X2MOUSE:
1346 case K_X2DRAG:
1347 case K_X2RELEASE:
1348 goto cmdline_not_changed;
1349
1350#endif /* FEAT_MOUSE */
1351
1352#ifdef FEAT_GUI
1353 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1354 case K_LEFTRELEASE_NM:
1355 goto cmdline_not_changed;
1356
1357 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001358 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 {
1360 gui_do_scroll();
1361 redrawcmd();
1362 }
1363 goto cmdline_not_changed;
1364
1365 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001366 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001368 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 redrawcmd();
1370 }
1371 goto cmdline_not_changed;
1372#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001373#ifdef FEAT_GUI_TABLINE
1374 case K_TABLINE:
1375 case K_TABMENU:
1376 /* Don't want to change any tabs here. Make sure the same tab
1377 * is still selected. */
1378 if (gui_use_tabline())
1379 gui_mch_set_curtab(tabpage_index(curtab));
1380 goto cmdline_not_changed;
1381#endif
1382
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 case K_SELECT: /* end of Select mode mapping - ignore */
1384 goto cmdline_not_changed;
1385
1386 case Ctrl_B: /* begin of command line */
1387 case K_HOME:
1388 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 case K_S_HOME:
1390 case K_C_HOME:
1391 ccline.cmdpos = 0;
1392 set_cmdspos();
1393 goto cmdline_not_changed;
1394
1395 case Ctrl_E: /* end of command line */
1396 case K_END:
1397 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 case K_S_END:
1399 case K_C_END:
1400 ccline.cmdpos = ccline.cmdlen;
1401 set_cmdspos_cursor();
1402 goto cmdline_not_changed;
1403
1404 case Ctrl_A: /* all matches */
1405 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1406 break;
1407 goto cmdline_changed;
1408
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001409 case Ctrl_L:
1410#ifdef FEAT_SEARCH_EXTRA
1411 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1412 {
1413 /* Add a character from under the cursor for 'incsearch' */
1414 if (did_incsearch
1415 && !equalpos(curwin->w_cursor, old_cursor))
1416 {
1417 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001418 /* If 'ignorecase' and 'smartcase' are set and the
1419 * command line has no uppercase characters, convert
1420 * the character to lowercase */
1421 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1422 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001423 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001424 {
1425 if (c == firstc || vim_strchr((char_u *)(
1426 p_magic ? "\\^$.*[" : "\\^$"), c)
1427 != NULL)
1428 {
1429 /* put a backslash before special characters */
1430 stuffcharReadbuff(c);
1431 c = '\\';
1432 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001433 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001434 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001435 }
1436 goto cmdline_not_changed;
1437 }
1438#endif
1439
1440 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1442 break;
1443 goto cmdline_changed;
1444
1445 case Ctrl_N: /* next match */
1446 case Ctrl_P: /* previous match */
1447 if (xpc.xp_numfiles > 0)
1448 {
1449 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1450 == FAIL)
1451 break;
1452 goto cmdline_changed;
1453 }
1454
1455#ifdef FEAT_CMDHIST
1456 case K_UP:
1457 case K_DOWN:
1458 case K_S_UP:
1459 case K_S_DOWN:
1460 case K_PAGEUP:
1461 case K_KPAGEUP:
1462 case K_PAGEDOWN:
1463 case K_KPAGEDOWN:
1464 if (hislen == 0 || firstc == NUL) /* no history */
1465 goto cmdline_not_changed;
1466
1467 i = hiscnt;
1468
1469 /* save current command string so it can be restored later */
1470 if (lookfor == NULL)
1471 {
1472 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1473 goto cmdline_not_changed;
1474 lookfor[ccline.cmdpos] = NUL;
1475 }
1476
1477 j = (int)STRLEN(lookfor);
1478 for (;;)
1479 {
1480 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001481 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001482 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 if (hiscnt == hislen) /* first time */
1485 hiscnt = hisidx[histype];
1486 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1487 hiscnt = hislen - 1;
1488 else if (hiscnt != hisidx[histype] + 1)
1489 --hiscnt;
1490 else /* at top of list */
1491 {
1492 hiscnt = i;
1493 break;
1494 }
1495 }
1496 else /* one step forwards */
1497 {
1498 /* on last entry, clear the line */
1499 if (hiscnt == hisidx[histype])
1500 {
1501 hiscnt = hislen;
1502 break;
1503 }
1504
1505 /* not on a history line, nothing to do */
1506 if (hiscnt == hislen)
1507 break;
1508 if (hiscnt == hislen - 1) /* wrap around */
1509 hiscnt = 0;
1510 else
1511 ++hiscnt;
1512 }
1513 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1514 {
1515 hiscnt = i;
1516 break;
1517 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001518 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001519 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 || STRNCMP(history[histype][hiscnt].hisstr,
1521 lookfor, (size_t)j) == 0)
1522 break;
1523 }
1524
1525 if (hiscnt != i) /* jumped to other entry */
1526 {
1527 char_u *p;
1528 int len;
1529 int old_firstc;
1530
1531 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001532 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 if (hiscnt == hislen)
1534 p = lookfor; /* back to the old one */
1535 else
1536 p = history[histype][hiscnt].hisstr;
1537
1538 if (histype == HIST_SEARCH
1539 && p != lookfor
1540 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1541 {
1542 /* Correct for the separator character used when
1543 * adding the history entry vs the one used now.
1544 * First loop: count length.
1545 * Second loop: copy the characters. */
1546 for (i = 0; i <= 1; ++i)
1547 {
1548 len = 0;
1549 for (j = 0; p[j] != NUL; ++j)
1550 {
1551 /* Replace old sep with new sep, unless it is
1552 * escaped. */
1553 if (p[j] == old_firstc
1554 && (j == 0 || p[j - 1] != '\\'))
1555 {
1556 if (i > 0)
1557 ccline.cmdbuff[len] = firstc;
1558 }
1559 else
1560 {
1561 /* Escape new sep, unless it is already
1562 * escaped. */
1563 if (p[j] == firstc
1564 && (j == 0 || p[j - 1] != '\\'))
1565 {
1566 if (i > 0)
1567 ccline.cmdbuff[len] = '\\';
1568 ++len;
1569 }
1570 if (i > 0)
1571 ccline.cmdbuff[len] = p[j];
1572 }
1573 ++len;
1574 }
1575 if (i == 0)
1576 {
1577 alloc_cmdbuff(len);
1578 if (ccline.cmdbuff == NULL)
1579 goto returncmd;
1580 }
1581 }
1582 ccline.cmdbuff[len] = NUL;
1583 }
1584 else
1585 {
1586 alloc_cmdbuff((int)STRLEN(p));
1587 if (ccline.cmdbuff == NULL)
1588 goto returncmd;
1589 STRCPY(ccline.cmdbuff, p);
1590 }
1591
1592 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1593 redrawcmd();
1594 goto cmdline_changed;
1595 }
1596 beep_flush();
1597 goto cmdline_not_changed;
1598#endif
1599
1600 case Ctrl_V:
1601 case Ctrl_Q:
1602#ifdef FEAT_MOUSE
1603 ignore_drag_release = TRUE;
1604#endif
1605 putcmdline('^', TRUE);
1606 c = get_literal(); /* get next (two) character(s) */
1607 do_abbr = FALSE; /* don't do abbreviation now */
1608#ifdef FEAT_MBYTE
1609 /* may need to remove ^ when composing char was typed */
1610 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1611 {
1612 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1613 msg_putchar(' ');
1614 cursorcmd();
1615 }
1616#endif
1617 break;
1618
1619#ifdef FEAT_DIGRAPHS
1620 case Ctrl_K:
1621#ifdef FEAT_MOUSE
1622 ignore_drag_release = TRUE;
1623#endif
1624 putcmdline('?', TRUE);
1625#ifdef USE_ON_FLY_SCROLL
1626 dont_scroll = TRUE; /* disallow scrolling here */
1627#endif
1628 c = get_digraph(TRUE);
1629 if (c != NUL)
1630 break;
1631
1632 redrawcmd();
1633 goto cmdline_not_changed;
1634#endif /* FEAT_DIGRAPHS */
1635
1636#ifdef FEAT_RIGHTLEFT
1637 case Ctrl__: /* CTRL-_: switch language mode */
1638 if (!p_ari)
1639 break;
1640#ifdef FEAT_FKMAP
1641 if (p_altkeymap)
1642 {
1643 cmd_fkmap = !cmd_fkmap;
1644 if (cmd_fkmap) /* in Farsi always in Insert mode */
1645 ccline.overstrike = FALSE;
1646 }
1647 else /* Hebrew is default */
1648#endif
1649 cmd_hkmap = !cmd_hkmap;
1650 goto cmdline_not_changed;
1651#endif
1652
1653 default:
1654#ifdef UNIX
1655 if (c == intr_char)
1656 {
1657 gotesc = TRUE; /* will free ccline.cmdbuff after
1658 putting it in history */
1659 goto returncmd; /* back to Normal mode */
1660 }
1661#endif
1662 /*
1663 * Normal character with no special meaning. Just set mod_mask
1664 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1665 * the string <S-Space>. This should only happen after ^V.
1666 */
1667 if (!IS_SPECIAL(c))
1668 mod_mask = 0x0;
1669 break;
1670 }
1671 /*
1672 * End of switch on command line character.
1673 * We come here if we have a normal character.
1674 */
1675
1676 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1677#ifdef FEAT_MBYTE
1678 /* Add ABBR_OFF for characters above 0x100, this is
1679 * what check_abbr() expects. */
1680 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1681#endif
1682 c))
1683 goto cmdline_changed;
1684
1685 /*
1686 * put the character in the command line
1687 */
1688 if (IS_SPECIAL(c) || mod_mask != 0)
1689 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1690 else
1691 {
1692#ifdef FEAT_MBYTE
1693 if (has_mbyte)
1694 {
1695 j = (*mb_char2bytes)(c, IObuff);
1696 IObuff[j] = NUL; /* exclude composing chars */
1697 put_on_cmdline(IObuff, j, TRUE);
1698 }
1699 else
1700#endif
1701 {
1702 IObuff[0] = c;
1703 put_on_cmdline(IObuff, 1, TRUE);
1704 }
1705 }
1706 goto cmdline_changed;
1707
1708/*
1709 * This part implements incremental searches for "/" and "?"
1710 * Jump to cmdline_not_changed when a character has been read but the command
1711 * line did not change. Then we only search and redraw if something changed in
1712 * the past.
1713 * Jump to cmdline_changed when the command line did change.
1714 * (Sorry for the goto's, I know it is ugly).
1715 */
1716cmdline_not_changed:
1717#ifdef FEAT_SEARCH_EXTRA
1718 if (!incsearch_postponed)
1719 continue;
1720#endif
1721
1722cmdline_changed:
1723#ifdef FEAT_SEARCH_EXTRA
1724 /*
1725 * 'incsearch' highlighting.
1726 */
1727 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1728 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001729 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001730#ifdef FEAT_RELTIME
1731 proftime_T tm;
1732#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 /* if there is a character waiting, search and redraw later */
1735 if (char_avail())
1736 {
1737 incsearch_postponed = TRUE;
1738 continue;
1739 }
1740 incsearch_postponed = FALSE;
1741 curwin->w_cursor = old_cursor; /* start at old position */
1742
1743 /* If there is no command line, don't do anything */
1744 if (ccline.cmdlen == 0)
1745 i = 0;
1746 else
1747 {
1748 cursor_off(); /* so the user knows we're busy */
1749 out_flush();
1750 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001751#ifdef FEAT_RELTIME
1752 /* Set the time limit to half a second. */
1753 profile_setlimit(500L, &tm);
1754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001756 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1757#ifdef FEAT_RELTIME
1758 &tm
1759#else
1760 NULL
1761#endif
1762 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 --emsg_off;
1764 /* if interrupted while searching, behave like it failed */
1765 if (got_int)
1766 {
1767 (void)vpeekc(); /* remove <C-C> from input stream */
1768 got_int = FALSE; /* don't abandon the command line */
1769 i = 0;
1770 }
1771 else if (char_avail())
1772 /* cancelled searching because a char was typed */
1773 incsearch_postponed = TRUE;
1774 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001775 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 highlight_match = TRUE; /* highlight position */
1777 else
1778 highlight_match = FALSE; /* remove highlight */
1779
1780 /* first restore the old curwin values, so the screen is
1781 * positioned in the same way as the actual search command */
1782 curwin->w_leftcol = old_leftcol;
1783 curwin->w_topline = old_topline;
1784# ifdef FEAT_DIFF
1785 curwin->w_topfill = old_topfill;
1786# endif
1787 curwin->w_botline = old_botline;
1788 changed_cline_bef_curs();
1789 update_topline();
1790
1791 if (i != 0)
1792 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001793 pos_T save_pos = curwin->w_cursor;
1794
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001796 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 * moves the whole match onto the screen when 'nowrap' is set.
1798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 curwin->w_cursor.lnum += search_match_lines;
1800 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001801 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1802 {
1803 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1804 coladvance((colnr_T)MAXCOL);
1805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001807 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001808 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001810 else
1811 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001812
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001814# ifdef FEAT_WINDOWS
1815 /* May redraw the status line to show the cursor position. */
1816 if (p_ru && curwin->w_status_height > 0)
1817 curwin->w_redr_status = TRUE;
1818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001820 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001821 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001822 restore_cmdline(&save_ccline);
1823
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001824 /* Leave it at the end to make CTRL-R CTRL-W work. */
1825 if (i != 0)
1826 curwin->w_cursor = end_pos;
1827
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 msg_starthere();
1829 redrawcmdline();
1830 did_incsearch = TRUE;
1831 }
1832#else /* FEAT_SEARCH_EXTRA */
1833 ;
1834#endif
1835
1836#ifdef FEAT_RIGHTLEFT
1837 if (cmdmsg_rl
1838# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001839 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840# endif
1841 )
1842 /* Always redraw the whole command line to fix shaping and
1843 * right-left typing. Not efficient, but it works. */
1844 redrawcmd();
1845#endif
1846 }
1847
1848returncmd:
1849
1850#ifdef FEAT_RIGHTLEFT
1851 cmdmsg_rl = FALSE;
1852#endif
1853
1854#ifdef FEAT_FKMAP
1855 cmd_fkmap = 0;
1856#endif
1857
1858 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001859 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860
1861#ifdef FEAT_SEARCH_EXTRA
1862 if (did_incsearch)
1863 {
1864 curwin->w_cursor = old_cursor;
1865 curwin->w_curswant = old_curswant;
1866 curwin->w_leftcol = old_leftcol;
1867 curwin->w_topline = old_topline;
1868# ifdef FEAT_DIFF
1869 curwin->w_topfill = old_topfill;
1870# endif
1871 curwin->w_botline = old_botline;
1872 highlight_match = FALSE;
1873 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001874 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876#endif
1877
1878 if (ccline.cmdbuff != NULL)
1879 {
1880 /*
1881 * Put line in history buffer (":" and "=" only when it was typed).
1882 */
1883#ifdef FEAT_CMDHIST
1884 if (ccline.cmdlen && firstc != NUL
1885 && (some_key_typed || histype == HIST_SEARCH))
1886 {
1887 add_to_history(histype, ccline.cmdbuff, TRUE,
1888 histype == HIST_SEARCH ? firstc : NUL);
1889 if (firstc == ':')
1890 {
1891 vim_free(new_last_cmdline);
1892 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1893 }
1894 }
1895#endif
1896
1897 if (gotesc) /* abandon command line */
1898 {
1899 vim_free(ccline.cmdbuff);
1900 ccline.cmdbuff = NULL;
1901 if (msg_scrolled == 0)
1902 compute_cmdrow();
1903 MSG("");
1904 redraw_cmdline = TRUE;
1905 }
1906 }
1907
1908 /*
1909 * If the screen was shifted up, redraw the whole screen (later).
1910 * If the line is too long, clear it, so ruler and shown command do
1911 * not get printed in the middle of it.
1912 */
1913 msg_check();
1914 msg_scroll = save_msg_scroll;
1915 redir_off = FALSE;
1916
1917 /* When the command line was typed, no need for a wait-return prompt. */
1918 if (some_key_typed)
1919 need_wait_return = FALSE;
1920
1921 State = save_State;
1922#ifdef USE_IM_CONTROL
1923 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1924 im_save_status(b_im_ptr);
1925 im_set_active(FALSE);
1926#endif
1927#ifdef FEAT_MOUSE
1928 setmouse();
1929#endif
1930#ifdef CURSOR_SHAPE
1931 ui_cursor_shape(); /* may show different cursor shape */
1932#endif
1933
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001934 {
1935 char_u *p = ccline.cmdbuff;
1936
1937 /* Make ccline empty, getcmdline() may try to use it. */
1938 ccline.cmdbuff = NULL;
1939 return p;
1940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941}
1942
1943#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1944/*
1945 * Get a command line with a prompt.
1946 * This is prepared to be called recursively from getcmdline() (e.g. by
1947 * f_input() when evaluating an expression from CTRL-R =).
1948 * Returns the command line in allocated memory, or NULL.
1949 */
1950 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001951getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 int firstc;
1953 char_u *prompt; /* command line prompt */
1954 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001955 int xp_context; /* type of expansion */
1956 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
1958 char_u *s;
1959 struct cmdline_info save_ccline;
1960 int msg_col_save = msg_col;
1961
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001962 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 ccline.cmdprompt = prompt;
1964 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001965# ifdef FEAT_EVAL
1966 ccline.xp_context = xp_context;
1967 ccline.xp_arg = xp_arg;
1968 ccline.input_fn = (firstc == '@');
1969# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001971 restore_cmdline(&save_ccline);
Bram Moolenaar1db1f772011-08-17 16:25:48 +02001972 /* Restore msg_col, the prompt from input() may have changed it.
1973 * But only if called recursively and the commandline is therefore being
1974 * restored to an old one; if not, the input() prompt stays on the screen,
1975 * so we need its modified msg_col left intact. */
1976 if (ccline.cmdbuff != NULL)
1977 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
1979 return s;
1980}
1981#endif
1982
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001983/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001984 * Return TRUE when the text must not be changed and we can't switch to
1985 * another window or buffer. Used when editing the command line, evaluating
1986 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001987 */
1988 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001989text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001990{
1991#ifdef FEAT_CMDWIN
1992 if (cmdwin_type != 0)
1993 return TRUE;
1994#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001995 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001996}
1997
1998/*
1999 * Give an error message for a command that isn't allowed while the cmdline
2000 * window is open or editing the cmdline in another way.
2001 */
2002 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002003text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002004{
2005#ifdef FEAT_CMDWIN
2006 if (cmdwin_type != 0)
2007 EMSG(_(e_cmdwin));
2008 else
2009#endif
2010 EMSG(_(e_secure));
2011}
2012
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002013#if defined(FEAT_AUTOCMD) || defined(PROTO)
2014/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002015 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2016 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002017 */
2018 int
2019curbuf_locked()
2020{
2021 if (curbuf_lock > 0)
2022 {
2023 EMSG(_("E788: Not allowed to edit another buffer now"));
2024 return TRUE;
2025 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002026 return allbuf_locked();
2027}
2028
2029/*
2030 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2031 * message.
2032 */
2033 int
2034allbuf_locked()
2035{
2036 if (allbuf_lock > 0)
2037 {
2038 EMSG(_("E811: Not allowed to change buffer information now"));
2039 return TRUE;
2040 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002041 return FALSE;
2042}
2043#endif
2044
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 static int
2046cmdline_charsize(idx)
2047 int idx;
2048{
2049#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2050 if (cmdline_star > 0) /* showing '*', always 1 position */
2051 return 1;
2052#endif
2053 return ptr2cells(ccline.cmdbuff + idx);
2054}
2055
2056/*
2057 * Compute the offset of the cursor on the command line for the prompt and
2058 * indent.
2059 */
2060 static void
2061set_cmdspos()
2062{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002063 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 ccline.cmdspos = 1 + ccline.cmdindent;
2065 else
2066 ccline.cmdspos = 0 + ccline.cmdindent;
2067}
2068
2069/*
2070 * Compute the screen position for the cursor on the command line.
2071 */
2072 static void
2073set_cmdspos_cursor()
2074{
2075 int i, m, c;
2076
2077 set_cmdspos();
2078 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002079 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002081 if (m < 0) /* overflow, Columns or Rows at weird value */
2082 m = MAXCOL;
2083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 else
2085 m = MAXCOL;
2086 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2087 {
2088 c = cmdline_charsize(i);
2089#ifdef FEAT_MBYTE
2090 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2091 if (has_mbyte)
2092 correct_cmdspos(i, c);
2093#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002094 /* If the cmdline doesn't fit, show cursor on last visible char.
2095 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 if ((ccline.cmdspos += c) >= m)
2097 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 ccline.cmdspos -= c;
2099 break;
2100 }
2101#ifdef FEAT_MBYTE
2102 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002103 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104#endif
2105 }
2106}
2107
2108#ifdef FEAT_MBYTE
2109/*
2110 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2111 * character that doesn't fit, so that a ">" must be displayed.
2112 */
2113 static void
2114correct_cmdspos(idx, cells)
2115 int idx;
2116 int cells;
2117{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002118 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2120 && ccline.cmdspos % Columns + cells > Columns)
2121 ccline.cmdspos++;
2122}
2123#endif
2124
2125/*
2126 * Get an Ex command line for the ":" command.
2127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002129getexline(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 int c; /* normally ':', NUL for ":append" */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002131 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 int indent; /* indent for inside conditionals */
2133{
2134 /* When executing a register, remove ':' that's in front of each line. */
2135 if (exec_from_reg && vpeekc() == ':')
2136 (void)vgetc();
2137 return getcmdline(c, 1L, indent);
2138}
2139
2140/*
2141 * Get an Ex command line for Ex mode.
2142 * In Ex mode we only use the OS supplied line editing features and no
2143 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002144 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002147getexmodeline(promptc, cookie, indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002148 int promptc; /* normally ':', NUL for ":append" and '?' for
2149 :s prompt */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002150 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 int indent; /* indent for inside conditionals */
2152{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002153 garray_T line_ga;
2154 char_u *pend;
2155 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002156 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002157 int escaped = FALSE; /* CTRL-V typed */
2158 int vcol = 0;
2159 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002160 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161
2162 /* Switch cursor on now. This avoids that it happens after the "\n", which
2163 * confuses the system function that computes tabstops. */
2164 cursor_on();
2165
2166 /* always start in column 0; write a newline if necessary */
2167 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002168 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002170 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002172 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002173 if (p_prompt)
2174 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 while (indent-- > 0)
2176 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
2179
2180 ga_init2(&line_ga, 1, 30);
2181
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002182 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002183 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002184 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002185 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002186 while (indent >= 8)
2187 {
2188 ga_append(&line_ga, TAB);
2189 msg_puts((char_u *)" ");
2190 indent -= 8;
2191 }
2192 while (indent-- > 0)
2193 {
2194 ga_append(&line_ga, ' ');
2195 msg_putchar(' ');
2196 }
2197 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002198 ++no_mapping;
2199 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002200
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 /*
2202 * Get the line, one character at a time.
2203 */
2204 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002205 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 {
2207 if (ga_grow(&line_ga, 40) == FAIL)
2208 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002210 /* Get one character at a time. Don't use inchar(), it can't handle
2211 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002212 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002213 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214
2215 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002216 * Handle line editing.
2217 * Previously this was left to the system, putting the terminal in
2218 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002220 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002222 msg_putchar('\n');
2223 break;
2224 }
2225
2226 if (!escaped)
2227 {
2228 /* CR typed means "enter", which is NL */
2229 if (c1 == '\r')
2230 c1 = '\n';
2231
2232 if (c1 == BS || c1 == K_BS
2233 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002235 if (line_ga.ga_len > 0)
2236 {
2237 --line_ga.ga_len;
2238 goto redraw;
2239 }
2240 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 }
2242
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002243 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002245 msg_col = startcol;
2246 msg_clr_eos();
2247 line_ga.ga_len = 0;
2248 continue;
2249 }
2250
2251 if (c1 == Ctrl_T)
2252 {
2253 p = (char_u *)line_ga.ga_data;
2254 p[line_ga.ga_len] = NUL;
2255 indent = get_indent_str(p, 8);
2256 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2257add_indent:
2258 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002260 char_u *s = skipwhite(p);
2261
2262 ga_grow(&line_ga, 1);
2263 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2264 *s = ' ';
2265 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002267redraw:
2268 /* redraw the line */
2269 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002270 vcol = 0;
2271 for (p = (char_u *)line_ga.ga_data;
2272 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002274 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002276 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002278 msg_putchar(' ');
2279 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002281 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002283 msg_outtrans_len(p, 1);
2284 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 }
2286 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002287 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002288 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002289 continue;
2290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002292 if (c1 == Ctrl_D)
2293 {
2294 /* Delete one shiftwidth. */
2295 p = (char_u *)line_ga.ga_data;
2296 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002298 if (prev_char == '^')
2299 ex_keep_indent = TRUE;
2300 indent = 0;
2301 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303 else
2304 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002305 p[line_ga.ga_len] = NUL;
2306 indent = get_indent_str(p, 8);
2307 --indent;
2308 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002310 while (get_indent_str(p, 8) > indent)
2311 {
2312 char_u *s = skipwhite(p);
2313
2314 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2315 --line_ga.ga_len;
2316 }
2317 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002319
2320 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2321 {
2322 escaped = TRUE;
2323 continue;
2324 }
2325
2326 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2327 if (IS_SPECIAL(c1))
2328 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002330
2331 if (IS_SPECIAL(c1))
2332 c1 = '?';
2333 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002334 if (c1 == '\n')
2335 msg_putchar('\n');
2336 else if (c1 == TAB)
2337 {
2338 /* Don't use chartabsize(), 'ts' can be different */
2339 do
2340 {
2341 msg_putchar(' ');
2342 } while (++vcol % 8);
2343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002346 msg_outtrans_len(
2347 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2348 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002350 ++line_ga.ga_len;
2351 escaped = FALSE;
2352
2353 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002354 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002355
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002356 /* We are done when a NL is entered, but not when it comes after an
2357 * odd number of backslashes, that results in a NUL. */
2358 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002360 int bcount = 0;
2361
2362 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2363 ++bcount;
2364
2365 if (bcount > 0)
2366 {
2367 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2368 * "\NL", etc. */
2369 line_ga.ga_len -= (bcount + 1) / 2;
2370 pend -= (bcount + 1) / 2;
2371 pend[-1] = '\n';
2372 }
2373
2374 if ((bcount & 1) == 0)
2375 {
2376 --line_ga.ga_len;
2377 --pend;
2378 *pend = NUL;
2379 break;
2380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 }
2382 }
2383
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002384 --no_mapping;
2385 --allow_keys;
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 /* make following messages go to the next line */
2388 msg_didout = FALSE;
2389 msg_col = 0;
2390 if (msg_row < Rows - 1)
2391 ++msg_row;
2392 emsg_on_display = FALSE; /* don't want ui_delay() */
2393
2394 if (got_int)
2395 ga_clear(&line_ga);
2396
2397 return (char_u *)line_ga.ga_data;
2398}
2399
Bram Moolenaare344bea2005-09-01 20:46:49 +00002400# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2401 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402/*
2403 * Return TRUE if ccline.overstrike is on.
2404 */
2405 int
2406cmdline_overstrike()
2407{
2408 return ccline.overstrike;
2409}
2410
2411/*
2412 * Return TRUE if the cursor is at the end of the cmdline.
2413 */
2414 int
2415cmdline_at_end()
2416{
2417 return (ccline.cmdpos >= ccline.cmdlen);
2418}
2419#endif
2420
Bram Moolenaar9372a112005-12-06 19:59:18 +00002421#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422/*
2423 * Return the virtual column number at the current cursor position.
2424 * This is used by the IM code to obtain the start of the preedit string.
2425 */
2426 colnr_T
2427cmdline_getvcol_cursor()
2428{
2429 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2430 return MAXCOL;
2431
2432# ifdef FEAT_MBYTE
2433 if (has_mbyte)
2434 {
2435 colnr_T col;
2436 int i = 0;
2437
2438 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002439 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
2441 return col;
2442 }
2443 else
2444# endif
2445 return ccline.cmdpos;
2446}
2447#endif
2448
2449#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2450/*
2451 * If part of the command line is an IM preedit string, redraw it with
2452 * IM feedback attributes. The cursor position is restored after drawing.
2453 */
2454 static void
2455redrawcmd_preedit()
2456{
2457 if ((State & CMDLINE)
2458 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002459 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 && !p_imdisable
2461 && im_is_preediting())
2462 {
2463 int cmdpos = 0;
2464 int cmdspos;
2465 int old_row;
2466 int old_col;
2467 colnr_T col;
2468
2469 old_row = msg_row;
2470 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002471 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
2473# ifdef FEAT_MBYTE
2474 if (has_mbyte)
2475 {
2476 for (col = 0; col < preedit_start_col
2477 && cmdpos < ccline.cmdlen; ++col)
2478 {
2479 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002480 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
2482 }
2483 else
2484# endif
2485 {
2486 cmdspos += preedit_start_col;
2487 cmdpos += preedit_start_col;
2488 }
2489
2490 msg_row = cmdline_row + (cmdspos / (int)Columns);
2491 msg_col = cmdspos % (int)Columns;
2492 if (msg_row >= Rows)
2493 msg_row = Rows - 1;
2494
2495 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2496 {
2497 int char_len;
2498 int char_attr;
2499
2500 char_attr = im_get_feedback_attr(col);
2501 if (char_attr < 0)
2502 break; /* end of preedit string */
2503
2504# ifdef FEAT_MBYTE
2505 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002506 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 else
2508# endif
2509 char_len = 1;
2510
2511 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2512 cmdpos += char_len;
2513 }
2514
2515 msg_row = old_row;
2516 msg_col = old_col;
2517 }
2518}
2519#endif /* FEAT_XIM && FEAT_GUI_GTK */
2520
2521/*
2522 * Allocate a new command line buffer.
2523 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2524 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2525 */
2526 static void
2527alloc_cmdbuff(len)
2528 int len;
2529{
2530 /*
2531 * give some extra space to avoid having to allocate all the time
2532 */
2533 if (len < 80)
2534 len = 100;
2535 else
2536 len += 20;
2537
2538 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2539 ccline.cmdbufflen = len;
2540}
2541
2542/*
2543 * Re-allocate the command line to length len + something extra.
2544 * return FAIL for failure, OK otherwise
2545 */
2546 static int
2547realloc_cmdbuff(len)
2548 int len;
2549{
2550 char_u *p;
2551
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002552 if (len < ccline.cmdbufflen)
2553 return OK; /* no need to resize */
2554
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 p = ccline.cmdbuff;
2556 alloc_cmdbuff(len); /* will get some more */
2557 if (ccline.cmdbuff == NULL) /* out of memory */
2558 {
2559 ccline.cmdbuff = p; /* keep the old one */
2560 return FAIL;
2561 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002562 /* There isn't always a NUL after the command, but it may need to be
2563 * there, thus copy up to the NUL and add a NUL. */
2564 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2565 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002567
2568 if (ccline.xpc != NULL
2569 && ccline.xpc->xp_pattern != NULL
2570 && ccline.xpc->xp_context != EXPAND_NOTHING
2571 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2572 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002573 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002574
2575 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2576 * to point into the newly allocated memory. */
2577 if (i >= 0 && i <= ccline.cmdlen)
2578 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2579 }
2580
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 return OK;
2582}
2583
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002584#if defined(FEAT_ARABIC) || defined(PROTO)
2585static char_u *arshape_buf = NULL;
2586
2587# if defined(EXITFREE) || defined(PROTO)
2588 void
2589free_cmdline_buf()
2590{
2591 vim_free(arshape_buf);
2592}
2593# endif
2594#endif
2595
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596/*
2597 * Draw part of the cmdline at the current cursor position. But draw stars
2598 * when cmdline_star is TRUE.
2599 */
2600 static void
2601draw_cmdline(start, len)
2602 int start;
2603 int len;
2604{
2605#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2606 int i;
2607
2608 if (cmdline_star > 0)
2609 for (i = 0; i < len; ++i)
2610 {
2611 msg_putchar('*');
2612# ifdef FEAT_MBYTE
2613 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002614 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615# endif
2616 }
2617 else
2618#endif
2619#ifdef FEAT_ARABIC
2620 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 static int buflen = 0;
2623 char_u *p;
2624 int j;
2625 int newlen = 0;
2626 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002627 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 int prev_c = 0;
2629 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002630 int u8c;
2631 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
2634 /*
2635 * Do arabic shaping into a temporary buffer. This is very
2636 * inefficient!
2637 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002638 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 {
2640 /* Re-allocate the buffer. We keep it around to avoid a lot of
2641 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002642 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002643 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002644 arshape_buf = alloc(buflen);
2645 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 return; /* out of memory */
2647 }
2648
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002649 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2650 {
2651 /* Prepend a space to draw the leading composing char on. */
2652 arshape_buf[0] = ' ';
2653 newlen = 1;
2654 }
2655
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 for (j = start; j < start + len; j += mb_l)
2657 {
2658 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002659 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002660 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (ARABIC_CHAR(u8c))
2662 {
2663 /* Do Arabic shaping. */
2664 if (cmdmsg_rl)
2665 {
2666 /* displaying from right to left */
2667 pc = prev_c;
2668 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002669 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 if (j + mb_l >= start + len)
2671 nc = NUL;
2672 else
2673 nc = utf_ptr2char(p + mb_l);
2674 }
2675 else
2676 {
2677 /* displaying from left to right */
2678 if (j + mb_l >= start + len)
2679 pc = NUL;
2680 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002681 {
2682 int pcc[MAX_MCO];
2683
2684 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002686 pc1 = pcc[0];
2687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 nc = prev_c;
2689 }
2690 prev_c = u8c;
2691
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002692 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002694 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002695 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002697 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2698 if (u8cc[1] != 0)
2699 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002700 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
2702 }
2703 else
2704 {
2705 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002706 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 newlen += mb_l;
2708 }
2709 }
2710
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002711 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713 else
2714#endif
2715 msg_outtrans_len(ccline.cmdbuff + start, len);
2716}
2717
2718/*
2719 * Put a character on the command line. Shifts the following text to the
2720 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2721 * "c" must be printable (fit in one display cell)!
2722 */
2723 void
2724putcmdline(c, shift)
2725 int c;
2726 int shift;
2727{
2728 if (cmd_silent)
2729 return;
2730 msg_no_more = TRUE;
2731 msg_putchar(c);
2732 if (shift)
2733 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2734 msg_no_more = FALSE;
2735 cursorcmd();
2736}
2737
2738/*
2739 * Undo a putcmdline(c, FALSE).
2740 */
2741 void
2742unputcmdline()
2743{
2744 if (cmd_silent)
2745 return;
2746 msg_no_more = TRUE;
2747 if (ccline.cmdlen == ccline.cmdpos)
2748 msg_putchar(' ');
2749 else
2750 draw_cmdline(ccline.cmdpos, 1);
2751 msg_no_more = FALSE;
2752 cursorcmd();
2753}
2754
2755/*
2756 * Put the given string, of the given length, onto the command line.
2757 * If len is -1, then STRLEN() is used to calculate the length.
2758 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2759 * part will be redrawn, otherwise it will not. If this function is called
2760 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2761 * called afterwards.
2762 */
2763 int
2764put_on_cmdline(str, len, redraw)
2765 char_u *str;
2766 int len;
2767 int redraw;
2768{
2769 int retval;
2770 int i;
2771 int m;
2772 int c;
2773
2774 if (len < 0)
2775 len = (int)STRLEN(str);
2776
2777 /* Check if ccline.cmdbuff needs to be longer */
2778 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002779 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 else
2781 retval = OK;
2782 if (retval == OK)
2783 {
2784 if (!ccline.overstrike)
2785 {
2786 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2787 ccline.cmdbuff + ccline.cmdpos,
2788 (size_t)(ccline.cmdlen - ccline.cmdpos));
2789 ccline.cmdlen += len;
2790 }
2791 else
2792 {
2793#ifdef FEAT_MBYTE
2794 if (has_mbyte)
2795 {
2796 /* Count nr of characters in the new string. */
2797 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002798 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 ++m;
2800 /* Count nr of bytes in cmdline that are overwritten by these
2801 * characters. */
2802 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002803 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 --m;
2805 if (i < ccline.cmdlen)
2806 {
2807 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2808 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2809 ccline.cmdlen += ccline.cmdpos + len - i;
2810 }
2811 else
2812 ccline.cmdlen = ccline.cmdpos + len;
2813 }
2814 else
2815#endif
2816 if (ccline.cmdpos + len > ccline.cmdlen)
2817 ccline.cmdlen = ccline.cmdpos + len;
2818 }
2819 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2820 ccline.cmdbuff[ccline.cmdlen] = NUL;
2821
2822#ifdef FEAT_MBYTE
2823 if (enc_utf8)
2824 {
2825 /* When the inserted text starts with a composing character,
2826 * backup to the character before it. There could be two of them.
2827 */
2828 i = 0;
2829 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2830 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2831 {
2832 i = (*mb_head_off)(ccline.cmdbuff,
2833 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2834 ccline.cmdpos -= i;
2835 len += i;
2836 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2837 }
2838# ifdef FEAT_ARABIC
2839 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2840 {
2841 /* Check the previous character for Arabic combining pair. */
2842 i = (*mb_head_off)(ccline.cmdbuff,
2843 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2844 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2845 + ccline.cmdpos - i), c))
2846 {
2847 ccline.cmdpos -= i;
2848 len += i;
2849 }
2850 else
2851 i = 0;
2852 }
2853# endif
2854 if (i != 0)
2855 {
2856 /* Also backup the cursor position. */
2857 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2858 ccline.cmdspos -= i;
2859 msg_col -= i;
2860 if (msg_col < 0)
2861 {
2862 msg_col += Columns;
2863 --msg_row;
2864 }
2865 }
2866 }
2867#endif
2868
2869 if (redraw && !cmd_silent)
2870 {
2871 msg_no_more = TRUE;
2872 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002873 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2875 /* Avoid clearing the rest of the line too often. */
2876 if (cmdline_row != i || ccline.overstrike)
2877 msg_clr_eos();
2878 msg_no_more = FALSE;
2879 }
2880#ifdef FEAT_FKMAP
2881 /*
2882 * If we are in Farsi command mode, the character input must be in
2883 * Insert mode. So do not advance the cmdpos.
2884 */
2885 if (!cmd_fkmap)
2886#endif
2887 {
2888 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002891 if (m < 0) /* overflow, Columns or Rows at weird value */
2892 m = MAXCOL;
2893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 else
2895 m = MAXCOL;
2896 for (i = 0; i < len; ++i)
2897 {
2898 c = cmdline_charsize(ccline.cmdpos);
2899#ifdef FEAT_MBYTE
2900 /* count ">" for a double-wide char that doesn't fit. */
2901 if (has_mbyte)
2902 correct_cmdspos(ccline.cmdpos, c);
2903#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002904 /* Stop cursor at the end of the screen, but do increment the
2905 * insert position, so that entering a very long command
2906 * works, even though you can't see it. */
2907 if (ccline.cmdspos + c < m)
2908 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909#ifdef FEAT_MBYTE
2910 if (has_mbyte)
2911 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002912 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 if (c > len - i - 1)
2914 c = len - i - 1;
2915 ccline.cmdpos += c;
2916 i += c;
2917 }
2918#endif
2919 ++ccline.cmdpos;
2920 }
2921 }
2922 }
2923 if (redraw)
2924 msg_check();
2925 return retval;
2926}
2927
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002928static struct cmdline_info prev_ccline;
2929static int prev_ccline_used = FALSE;
2930
2931/*
2932 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2933 * and overwrite it. But get_cmdline_str() may need it, thus make it
2934 * available globally in prev_ccline.
2935 */
2936 static void
2937save_cmdline(ccp)
2938 struct cmdline_info *ccp;
2939{
2940 if (!prev_ccline_used)
2941 {
2942 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2943 prev_ccline_used = TRUE;
2944 }
2945 *ccp = prev_ccline;
2946 prev_ccline = ccline;
2947 ccline.cmdbuff = NULL;
2948 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002949 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002950}
2951
2952/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002953 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002954 */
2955 static void
2956restore_cmdline(ccp)
2957 struct cmdline_info *ccp;
2958{
2959 ccline = prev_ccline;
2960 prev_ccline = *ccp;
2961}
2962
Bram Moolenaar5a305422006-04-28 22:38:25 +00002963#if defined(FEAT_EVAL) || defined(PROTO)
2964/*
2965 * Save the command line into allocated memory. Returns a pointer to be
2966 * passed to restore_cmdline_alloc() later.
2967 * Returns NULL when failed.
2968 */
2969 char_u *
2970save_cmdline_alloc()
2971{
2972 struct cmdline_info *p;
2973
2974 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2975 if (p != NULL)
2976 save_cmdline(p);
2977 return (char_u *)p;
2978}
2979
2980/*
2981 * Restore the command line from the return value of save_cmdline_alloc().
2982 */
2983 void
2984restore_cmdline_alloc(p)
2985 char_u *p;
2986{
2987 if (p != NULL)
2988 {
2989 restore_cmdline((struct cmdline_info *)p);
2990 vim_free(p);
2991 }
2992}
2993#endif
2994
Bram Moolenaar8299df92004-07-10 09:47:34 +00002995/*
2996 * paste a yank register into the command line.
2997 * used by CTRL-R command in command-line mode
2998 * insert_reg() can't be used here, because special characters from the
2999 * register contents will be interpreted as commands.
3000 *
3001 * return FAIL for failure, OK otherwise
3002 */
3003 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003004cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003005 int regname;
3006 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003007 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003008{
3009 long i;
3010 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003011 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003012 int allocated;
3013 struct cmdline_info save_ccline;
3014
3015 /* check for valid regname; also accept special characters for CTRL-R in
3016 * the command line */
3017 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3018 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3019 return FAIL;
3020
3021 /* A register containing CTRL-R can cause an endless loop. Allow using
3022 * CTRL-C to break the loop. */
3023 line_breakcheck();
3024 if (got_int)
3025 return FAIL;
3026
3027#ifdef FEAT_CLIPBOARD
3028 regname = may_get_selection(regname);
3029#endif
3030
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003031 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003032 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003033 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003034 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003035 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003036 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003037 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003038
3039 if (i)
3040 {
3041 /* Got the value of a special register in "arg". */
3042 if (arg == NULL)
3043 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003044
3045 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3046 * part of the word. */
3047 p = arg;
3048 if (p_is && regname == Ctrl_W)
3049 {
3050 char_u *w;
3051 int len;
3052
3053 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003054 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003055 {
3056#ifdef FEAT_MBYTE
3057 if (has_mbyte)
3058 {
3059 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3060 if (!vim_iswordc(mb_ptr2char(w - len)))
3061 break;
3062 w -= len;
3063 }
3064 else
3065#endif
3066 {
3067 if (!vim_iswordc(w[-1]))
3068 break;
3069 --w;
3070 }
3071 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003072 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003073 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3074 p += len;
3075 }
3076
3077 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003078 if (allocated)
3079 vim_free(arg);
3080 return OK;
3081 }
3082
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003083 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003084}
3085
3086/*
3087 * Put a string on the command line.
3088 * When "literally" is TRUE, insert literally.
3089 * When "literally" is FALSE, insert as typed, but don't leave the command
3090 * line.
3091 */
3092 void
3093cmdline_paste_str(s, literally)
3094 char_u *s;
3095 int literally;
3096{
3097 int c, cv;
3098
3099 if (literally)
3100 put_on_cmdline(s, -1, TRUE);
3101 else
3102 while (*s != NUL)
3103 {
3104 cv = *s;
3105 if (cv == Ctrl_V && s[1])
3106 ++s;
3107#ifdef FEAT_MBYTE
3108 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003109 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003110 else
3111#endif
3112 c = *s++;
3113 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3114#ifdef UNIX
3115 || c == intr_char
3116#endif
3117 || (c == Ctrl_BSL && *s == Ctrl_N))
3118 stuffcharReadbuff(Ctrl_V);
3119 stuffcharReadbuff(c);
3120 }
3121}
3122
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#ifdef FEAT_WILDMENU
3124/*
3125 * Delete characters on the command line, from "from" to the current
3126 * position.
3127 */
3128 static void
3129cmdline_del(from)
3130 int from;
3131{
3132 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3133 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3134 ccline.cmdlen -= ccline.cmdpos - from;
3135 ccline.cmdpos = from;
3136}
3137#endif
3138
3139/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003140 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 * search
3142 */
3143 void
3144redrawcmdline()
3145{
3146 if (cmd_silent)
3147 return;
3148 need_wait_return = FALSE;
3149 compute_cmdrow();
3150 redrawcmd();
3151 cursorcmd();
3152}
3153
3154 static void
3155redrawcmdprompt()
3156{
3157 int i;
3158
3159 if (cmd_silent)
3160 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003161 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 msg_putchar(ccline.cmdfirstc);
3163 if (ccline.cmdprompt != NULL)
3164 {
3165 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3166 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3167 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003168 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 --ccline.cmdindent;
3170 }
3171 else
3172 for (i = ccline.cmdindent; i > 0; --i)
3173 msg_putchar(' ');
3174}
3175
3176/*
3177 * Redraw what is currently on the command line.
3178 */
3179 void
3180redrawcmd()
3181{
3182 if (cmd_silent)
3183 return;
3184
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003185 /* when 'incsearch' is set there may be no command line while redrawing */
3186 if (ccline.cmdbuff == NULL)
3187 {
3188 windgoto(cmdline_row, 0);
3189 msg_clr_eos();
3190 return;
3191 }
3192
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 msg_start();
3194 redrawcmdprompt();
3195
3196 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3197 msg_no_more = TRUE;
3198 draw_cmdline(0, ccline.cmdlen);
3199 msg_clr_eos();
3200 msg_no_more = FALSE;
3201
3202 set_cmdspos_cursor();
3203
3204 /*
3205 * An emsg() before may have set msg_scroll. This is used in normal mode,
3206 * in cmdline mode we can reset them now.
3207 */
3208 msg_scroll = FALSE; /* next message overwrites cmdline */
3209
3210 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3211 * in cmdline mode */
3212 skip_redraw = FALSE;
3213}
3214
3215 void
3216compute_cmdrow()
3217{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003218 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 cmdline_row = Rows - 1;
3220 else
3221 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3222 + W_STATUS_HEIGHT(lastwin);
3223}
3224
3225 static void
3226cursorcmd()
3227{
3228 if (cmd_silent)
3229 return;
3230
3231#ifdef FEAT_RIGHTLEFT
3232 if (cmdmsg_rl)
3233 {
3234 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3235 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3236 if (msg_row <= 0)
3237 msg_row = Rows - 1;
3238 }
3239 else
3240#endif
3241 {
3242 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3243 msg_col = ccline.cmdspos % (int)Columns;
3244 if (msg_row >= Rows)
3245 msg_row = Rows - 1;
3246 }
3247
3248 windgoto(msg_row, msg_col);
3249#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3250 redrawcmd_preedit();
3251#endif
3252#ifdef MCH_CURSOR_SHAPE
3253 mch_update_cursor();
3254#endif
3255}
3256
3257 void
3258gotocmdline(clr)
3259 int clr;
3260{
3261 msg_start();
3262#ifdef FEAT_RIGHTLEFT
3263 if (cmdmsg_rl)
3264 msg_col = Columns - 1;
3265 else
3266#endif
3267 msg_col = 0; /* always start in column 0 */
3268 if (clr) /* clear the bottom line(s) */
3269 msg_clr_eos(); /* will reset clear_cmdline */
3270 windgoto(cmdline_row, 0);
3271}
3272
3273/*
3274 * Check the word in front of the cursor for an abbreviation.
3275 * Called when the non-id character "c" has been entered.
3276 * When an abbreviation is recognized it is removed from the text with
3277 * backspaces and the replacement string is inserted, followed by "c".
3278 */
3279 static int
3280ccheck_abbr(c)
3281 int c;
3282{
3283 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3284 return FALSE;
3285
3286 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3287}
3288
3289/*
3290 * Return FAIL if this is not an appropriate context in which to do
3291 * completion of anything, return OK if it is (even if there are no matches).
3292 * For the caller, this means that the character is just passed through like a
3293 * normal character (instead of being expanded). This allows :s/^I^D etc.
3294 */
3295 static int
3296nextwild(xp, type, options)
3297 expand_T *xp;
3298 int type;
3299 int options; /* extra options for ExpandOne() */
3300{
3301 int i, j;
3302 char_u *p1;
3303 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 int difflen;
3305 int v;
3306
3307 if (xp->xp_numfiles == -1)
3308 {
3309 set_expand_context(xp);
3310 cmd_showtail = expand_showtail(xp);
3311 }
3312
3313 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3314 {
3315 beep_flush();
3316 return OK; /* Something illegal on command line */
3317 }
3318 if (xp->xp_context == EXPAND_NOTHING)
3319 {
3320 /* Caller can use the character as a normal char instead */
3321 return FAIL;
3322 }
3323
3324 MSG_PUTS("..."); /* show that we are busy */
3325 out_flush();
3326
3327 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003328 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
3330 if (type == WILD_NEXT || type == WILD_PREV)
3331 {
3332 /*
3333 * Get next/previous match for a previous expanded pattern.
3334 */
3335 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3336 }
3337 else
3338 {
3339 /*
3340 * Translate string into pattern and expand it.
3341 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003342 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3343 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 p2 = NULL;
3345 else
3346 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003347 int use_options = options |
3348 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE;
3349
3350 if (p_wic)
3351 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003352 p2 = ExpandOne(xp, p1,
3353 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003354 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003356 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 if (p2 != NULL && type == WILD_LONGEST)
3358 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003359 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 if (ccline.cmdbuff[i + j] == '*'
3361 || ccline.cmdbuff[i + j] == '?')
3362 break;
3363 if ((int)STRLEN(p2) < j)
3364 {
3365 vim_free(p2);
3366 p2 = NULL;
3367 }
3368 }
3369 }
3370 }
3371
3372 if (p2 != NULL && !got_int)
3373 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003374 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003375 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003377 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 xp->xp_pattern = ccline.cmdbuff + i;
3379 }
3380 else
3381 v = OK;
3382 if (v == OK)
3383 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003384 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3385 &ccline.cmdbuff[ccline.cmdpos],
3386 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3387 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 ccline.cmdlen += difflen;
3389 ccline.cmdpos += difflen;
3390 }
3391 }
3392 vim_free(p2);
3393
3394 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003395 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396
3397 /* When expanding a ":map" command and no matches are found, assume that
3398 * the key is supposed to be inserted literally */
3399 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3400 return FAIL;
3401
3402 if (xp->xp_numfiles <= 0 && p2 == NULL)
3403 beep_flush();
3404 else if (xp->xp_numfiles == 1)
3405 /* free expanded pattern */
3406 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3407
3408 return OK;
3409}
3410
3411/*
3412 * Do wildcard expansion on the string 'str'.
3413 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003414 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 * Return NULL for failure.
3416 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003417 * "orig" is the originally expanded string, copied to allocated memory. It
3418 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3419 * WILD_PREV "orig" should be NULL.
3420 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003421 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3422 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 *
3424 * mode = WILD_FREE: just free previously expanded matches
3425 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3426 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3427 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3428 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3429 * mode = WILD_ALL: return all matches concatenated
3430 * mode = WILD_LONGEST: return longest matched part
3431 *
3432 * options = WILD_LIST_NOTFOUND: list entries without a match
3433 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3434 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3435 * options = WILD_NO_BEEP: Don't beep for multiple matches
3436 * options = WILD_ADD_SLASH: add a slash after directory names
3437 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3438 * options = WILD_SILENT: don't print warning messages
3439 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003440 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 *
3442 * The variables xp->xp_context and xp->xp_backslash must have been set!
3443 */
3444 char_u *
3445ExpandOne(xp, str, orig, options, mode)
3446 expand_T *xp;
3447 char_u *str;
3448 char_u *orig; /* allocated copy of original of expanded string */
3449 int options;
3450 int mode;
3451{
3452 char_u *ss = NULL;
3453 static int findex;
3454 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003455 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 int i;
3457 long_u len;
3458 int non_suf_match; /* number without matching suffix */
3459
3460 /*
3461 * first handle the case of using an old match
3462 */
3463 if (mode == WILD_NEXT || mode == WILD_PREV)
3464 {
3465 if (xp->xp_numfiles > 0)
3466 {
3467 if (mode == WILD_PREV)
3468 {
3469 if (findex == -1)
3470 findex = xp->xp_numfiles;
3471 --findex;
3472 }
3473 else /* mode == WILD_NEXT */
3474 ++findex;
3475
3476 /*
3477 * When wrapping around, return the original string, set findex to
3478 * -1.
3479 */
3480 if (findex < 0)
3481 {
3482 if (orig_save == NULL)
3483 findex = xp->xp_numfiles - 1;
3484 else
3485 findex = -1;
3486 }
3487 if (findex >= xp->xp_numfiles)
3488 {
3489 if (orig_save == NULL)
3490 findex = 0;
3491 else
3492 findex = -1;
3493 }
3494#ifdef FEAT_WILDMENU
3495 if (p_wmnu)
3496 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3497 findex, cmd_showtail);
3498#endif
3499 if (findex == -1)
3500 return vim_strsave(orig_save);
3501 return vim_strsave(xp->xp_files[findex]);
3502 }
3503 else
3504 return NULL;
3505 }
3506
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003507 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3509 {
3510 FreeWild(xp->xp_numfiles, xp->xp_files);
3511 xp->xp_numfiles = -1;
3512 vim_free(orig_save);
3513 orig_save = NULL;
3514 }
3515 findex = 0;
3516
3517 if (mode == WILD_FREE) /* only release file name */
3518 return NULL;
3519
3520 if (xp->xp_numfiles == -1)
3521 {
3522 vim_free(orig_save);
3523 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003524 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
3526 /*
3527 * Do the expansion.
3528 */
3529 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3530 options) == FAIL)
3531 {
3532#ifdef FNAME_ILLEGAL
3533 /* Illegal file name has been silently skipped. But when there
3534 * are wildcards, the real problem is that there was no match,
3535 * causing the pattern to be added, which has illegal characters.
3536 */
3537 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3538 EMSG2(_(e_nomatch2), str);
3539#endif
3540 }
3541 else if (xp->xp_numfiles == 0)
3542 {
3543 if (!(options & WILD_SILENT))
3544 EMSG2(_(e_nomatch2), str);
3545 }
3546 else
3547 {
3548 /* Escape the matches for use on the command line. */
3549 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3550
3551 /*
3552 * Check for matching suffixes in file names.
3553 */
3554 if (mode != WILD_ALL && mode != WILD_LONGEST)
3555 {
3556 if (xp->xp_numfiles)
3557 non_suf_match = xp->xp_numfiles;
3558 else
3559 non_suf_match = 1;
3560 if ((xp->xp_context == EXPAND_FILES
3561 || xp->xp_context == EXPAND_DIRECTORIES)
3562 && xp->xp_numfiles > 1)
3563 {
3564 /*
3565 * More than one match; check suffix.
3566 * The files will have been sorted on matching suffix in
3567 * expand_wildcards, only need to check the first two.
3568 */
3569 non_suf_match = 0;
3570 for (i = 0; i < 2; ++i)
3571 if (match_suffix(xp->xp_files[i]))
3572 ++non_suf_match;
3573 }
3574 if (non_suf_match != 1)
3575 {
3576 /* Can we ever get here unless it's while expanding
3577 * interactively? If not, we can get rid of this all
3578 * together. Don't really want to wait for this message
3579 * (and possibly have to hit return to continue!).
3580 */
3581 if (!(options & WILD_SILENT))
3582 EMSG(_(e_toomany));
3583 else if (!(options & WILD_NO_BEEP))
3584 beep_flush();
3585 }
3586 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3587 ss = vim_strsave(xp->xp_files[0]);
3588 }
3589 }
3590 }
3591
3592 /* Find longest common part */
3593 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3594 {
3595 for (len = 0; xp->xp_files[0][len]; ++len)
3596 {
3597 for (i = 0; i < xp->xp_numfiles; ++i)
3598 {
3599#ifdef CASE_INSENSITIVE_FILENAME
3600 if (xp->xp_context == EXPAND_DIRECTORIES
3601 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003602 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 || xp->xp_context == EXPAND_BUFFERS)
3604 {
3605 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3606 TOLOWER_LOC(xp->xp_files[0][len]))
3607 break;
3608 }
3609 else
3610#endif
3611 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3612 break;
3613 }
3614 if (i < xp->xp_numfiles)
3615 {
3616 if (!(options & WILD_NO_BEEP))
3617 vim_beep();
3618 break;
3619 }
3620 }
3621 ss = alloc((unsigned)len + 1);
3622 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003623 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 findex = -1; /* next p_wc gets first one */
3625 }
3626
3627 /* Concatenate all matching names */
3628 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3629 {
3630 len = 0;
3631 for (i = 0; i < xp->xp_numfiles; ++i)
3632 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3633 ss = lalloc(len, TRUE);
3634 if (ss != NULL)
3635 {
3636 *ss = NUL;
3637 for (i = 0; i < xp->xp_numfiles; ++i)
3638 {
3639 STRCAT(ss, xp->xp_files[i]);
3640 if (i != xp->xp_numfiles - 1)
3641 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3642 }
3643 }
3644 }
3645
3646 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3647 ExpandCleanup(xp);
3648
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003649 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003650 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003651 vim_free(orig);
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 return ss;
3654}
3655
3656/*
3657 * Prepare an expand structure for use.
3658 */
3659 void
3660ExpandInit(xp)
3661 expand_T *xp;
3662{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003663 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003664 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003666#ifndef BACKSLASH_IN_FILENAME
3667 xp->xp_shell = FALSE;
3668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 xp->xp_numfiles = -1;
3670 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003671#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3672 xp->xp_arg = NULL;
3673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674}
3675
3676/*
3677 * Cleanup an expand structure after use.
3678 */
3679 void
3680ExpandCleanup(xp)
3681 expand_T *xp;
3682{
3683 if (xp->xp_numfiles >= 0)
3684 {
3685 FreeWild(xp->xp_numfiles, xp->xp_files);
3686 xp->xp_numfiles = -1;
3687 }
3688}
3689
3690 void
3691ExpandEscape(xp, str, numfiles, files, options)
3692 expand_T *xp;
3693 char_u *str;
3694 int numfiles;
3695 char_u **files;
3696 int options;
3697{
3698 int i;
3699 char_u *p;
3700
3701 /*
3702 * May change home directory back to "~"
3703 */
3704 if (options & WILD_HOME_REPLACE)
3705 tilde_replace(str, numfiles, files);
3706
3707 if (options & WILD_ESCAPE)
3708 {
3709 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003710 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003711 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 || xp->xp_context == EXPAND_BUFFERS
3713 || xp->xp_context == EXPAND_DIRECTORIES)
3714 {
3715 /*
3716 * Insert a backslash into a file name before a space, \, %, #
3717 * and wildmatch characters, except '~'.
3718 */
3719 for (i = 0; i < numfiles; ++i)
3720 {
3721 /* for ":set path=" we need to escape spaces twice */
3722 if (xp->xp_backslash == XP_BS_THREE)
3723 {
3724 p = vim_strsave_escaped(files[i], (char_u *)" ");
3725 if (p != NULL)
3726 {
3727 vim_free(files[i]);
3728 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003729#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 p = vim_strsave_escaped(files[i], (char_u *)" ");
3731 if (p != NULL)
3732 {
3733 vim_free(files[i]);
3734 files[i] = p;
3735 }
3736#endif
3737 }
3738 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003739#ifdef BACKSLASH_IN_FILENAME
3740 p = vim_strsave_fnameescape(files[i], FALSE);
3741#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003742 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 if (p != NULL)
3745 {
3746 vim_free(files[i]);
3747 files[i] = p;
3748 }
3749
3750 /* If 'str' starts with "\~", replace "~" at start of
3751 * files[i] with "\~". */
3752 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003753 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
3755 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003756
3757 /* If the first file starts with a '+' escape it. Otherwise it
3758 * could be seen as "+cmd". */
3759 if (*files[0] == '+')
3760 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762 else if (xp->xp_context == EXPAND_TAGS)
3763 {
3764 /*
3765 * Insert a backslash before characters in a tag name that
3766 * would terminate the ":tag" command.
3767 */
3768 for (i = 0; i < numfiles; ++i)
3769 {
3770 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3771 if (p != NULL)
3772 {
3773 vim_free(files[i]);
3774 files[i] = p;
3775 }
3776 }
3777 }
3778 }
3779}
3780
3781/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003782 * Escape special characters in "fname" for when used as a file name argument
3783 * after a Vim command, or, when "shell" is non-zero, a shell command.
3784 * Returns the result in allocated memory.
3785 */
3786 char_u *
3787vim_strsave_fnameescape(fname, shell)
3788 char_u *fname;
3789 int shell;
3790{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003791 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003792#ifdef BACKSLASH_IN_FILENAME
3793 char_u buf[20];
3794 int j = 0;
3795
3796 /* Don't escape '[' and '{' if they are in 'isfname'. */
3797 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3798 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3799 buf[j++] = *p;
3800 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003801 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003802#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003803 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3804 if (shell && csh_like_shell() && p != NULL)
3805 {
3806 char_u *s;
3807
3808 /* For csh and similar shells need to put two backslashes before '!'.
3809 * One is taken by Vim, one by the shell. */
3810 s = vim_strsave_escaped(p, (char_u *)"!");
3811 vim_free(p);
3812 p = s;
3813 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003814#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003815
3816 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3817 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003818 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003819 escape_fname(&p);
3820
3821 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003822}
3823
3824/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003825 * Put a backslash before the file name in "pp", which is in allocated memory.
3826 */
3827 static void
3828escape_fname(pp)
3829 char_u **pp;
3830{
3831 char_u *p;
3832
3833 p = alloc((unsigned)(STRLEN(*pp) + 2));
3834 if (p != NULL)
3835 {
3836 p[0] = '\\';
3837 STRCPY(p + 1, *pp);
3838 vim_free(*pp);
3839 *pp = p;
3840 }
3841}
3842
3843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 * For each file name in files[num_files]:
3845 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3846 */
3847 void
3848tilde_replace(orig_pat, num_files, files)
3849 char_u *orig_pat;
3850 int num_files;
3851 char_u **files;
3852{
3853 int i;
3854 char_u *p;
3855
3856 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3857 {
3858 for (i = 0; i < num_files; ++i)
3859 {
3860 p = home_replace_save(NULL, files[i]);
3861 if (p != NULL)
3862 {
3863 vim_free(files[i]);
3864 files[i] = p;
3865 }
3866 }
3867 }
3868}
3869
3870/*
3871 * Show all matches for completion on the command line.
3872 * Returns EXPAND_NOTHING when the character that triggered expansion should
3873 * be inserted like a normal character.
3874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 static int
3876showmatches(xp, wildmenu)
3877 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003878 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879{
3880#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3881 int num_files;
3882 char_u **files_found;
3883 int i, j, k;
3884 int maxlen;
3885 int lines;
3886 int columns;
3887 char_u *p;
3888 int lastlen;
3889 int attr;
3890 int showtail;
3891
3892 if (xp->xp_numfiles == -1)
3893 {
3894 set_expand_context(xp);
3895 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3896 &num_files, &files_found);
3897 showtail = expand_showtail(xp);
3898 if (i != EXPAND_OK)
3899 return i;
3900
3901 }
3902 else
3903 {
3904 num_files = xp->xp_numfiles;
3905 files_found = xp->xp_files;
3906 showtail = cmd_showtail;
3907 }
3908
3909#ifdef FEAT_WILDMENU
3910 if (!wildmenu)
3911 {
3912#endif
3913 msg_didany = FALSE; /* lines_left will be set */
3914 msg_start(); /* prepare for paging */
3915 msg_putchar('\n');
3916 out_flush();
3917 cmdline_row = msg_row;
3918 msg_didany = FALSE; /* lines_left will be set again */
3919 msg_start(); /* prepare for paging */
3920#ifdef FEAT_WILDMENU
3921 }
3922#endif
3923
3924 if (got_int)
3925 got_int = FALSE; /* only int. the completion, not the cmd line */
3926#ifdef FEAT_WILDMENU
3927 else if (wildmenu)
3928 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3929#endif
3930 else
3931 {
3932 /* find the length of the longest file name */
3933 maxlen = 0;
3934 for (i = 0; i < num_files; ++i)
3935 {
3936 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003937 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 || xp->xp_context == EXPAND_BUFFERS))
3939 {
3940 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3941 j = vim_strsize(NameBuff);
3942 }
3943 else
3944 j = vim_strsize(L_SHOWFILE(i));
3945 if (j > maxlen)
3946 maxlen = j;
3947 }
3948
3949 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3950 lines = num_files;
3951 else
3952 {
3953 /* compute the number of columns and lines for the listing */
3954 maxlen += 2; /* two spaces between file names */
3955 columns = ((int)Columns + 2) / maxlen;
3956 if (columns < 1)
3957 columns = 1;
3958 lines = (num_files + columns - 1) / columns;
3959 }
3960
3961 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3962
3963 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3964 {
3965 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3966 msg_clr_eos();
3967 msg_advance(maxlen - 3);
3968 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3969 }
3970
3971 /* list the files line by line */
3972 for (i = 0; i < lines; ++i)
3973 {
3974 lastlen = 999;
3975 for (k = i; k < num_files; k += lines)
3976 {
3977 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3978 {
3979 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3980 p = files_found[k] + STRLEN(files_found[k]) + 1;
3981 msg_advance(maxlen + 1);
3982 msg_puts(p);
3983 msg_advance(maxlen + 3);
3984 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3985 break;
3986 }
3987 for (j = maxlen - lastlen; --j >= 0; )
3988 msg_putchar(' ');
3989 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003990 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 || xp->xp_context == EXPAND_BUFFERS)
3992 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01003993 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01003994 if (xp->xp_numfiles != -1)
3995 {
3996 char_u *halved_slash;
3997 char_u *exp_path;
3998
3999 /* Expansion was done before and special characters
4000 * were escaped, need to halve backslashes. Also
4001 * $HOME has been replaced with ~/. */
4002 exp_path = expand_env_save_opt(files_found[k], TRUE);
4003 halved_slash = backslash_halve_save(
4004 exp_path != NULL ? exp_path : files_found[k]);
4005 j = mch_isdir(halved_slash != NULL ? halved_slash
4006 : files_found[k]);
4007 vim_free(exp_path);
4008 vim_free(halved_slash);
4009 }
4010 else
4011 /* Expansion was done here, file names are literal. */
4012 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 if (showtail)
4014 p = L_SHOWFILE(k);
4015 else
4016 {
4017 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4018 TRUE);
4019 p = NameBuff;
4020 }
4021 }
4022 else
4023 {
4024 j = FALSE;
4025 p = L_SHOWFILE(k);
4026 }
4027 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4028 }
4029 if (msg_col > 0) /* when not wrapped around */
4030 {
4031 msg_clr_eos();
4032 msg_putchar('\n');
4033 }
4034 out_flush(); /* show one line at a time */
4035 if (got_int)
4036 {
4037 got_int = FALSE;
4038 break;
4039 }
4040 }
4041
4042 /*
4043 * we redraw the command below the lines that we have just listed
4044 * This is a bit tricky, but it saves a lot of screen updating.
4045 */
4046 cmdline_row = msg_row; /* will put it back later */
4047 }
4048
4049 if (xp->xp_numfiles == -1)
4050 FreeWild(num_files, files_found);
4051
4052 return EXPAND_OK;
4053}
4054
4055/*
4056 * Private gettail for showmatches() (and win_redr_status_matches()):
4057 * Find tail of file name path, but ignore trailing "/".
4058 */
4059 char_u *
4060sm_gettail(s)
4061 char_u *s;
4062{
4063 char_u *p;
4064 char_u *t = s;
4065 int had_sep = FALSE;
4066
4067 for (p = s; *p != NUL; )
4068 {
4069 if (vim_ispathsep(*p)
4070#ifdef BACKSLASH_IN_FILENAME
4071 && !rem_backslash(p)
4072#endif
4073 )
4074 had_sep = TRUE;
4075 else if (had_sep)
4076 {
4077 t = p;
4078 had_sep = FALSE;
4079 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004080 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082 return t;
4083}
4084
4085/*
4086 * Return TRUE if we only need to show the tail of completion matches.
4087 * When not completing file names or there is a wildcard in the path FALSE is
4088 * returned.
4089 */
4090 static int
4091expand_showtail(xp)
4092 expand_T *xp;
4093{
4094 char_u *s;
4095 char_u *end;
4096
4097 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004098 if (xp->xp_context != EXPAND_FILES
4099 && xp->xp_context != EXPAND_SHELLCMD
4100 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 return FALSE;
4102
4103 end = gettail(xp->xp_pattern);
4104 if (end == xp->xp_pattern) /* there is no path separator */
4105 return FALSE;
4106
4107 for (s = xp->xp_pattern; s < end; s++)
4108 {
4109 /* Skip escaped wildcards. Only when the backslash is not a path
4110 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4111 if (rem_backslash(s))
4112 ++s;
4113 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4114 return FALSE;
4115 }
4116 return TRUE;
4117}
4118
4119/*
4120 * Prepare a string for expansion.
4121 * When expanding file names: The string will be used with expand_wildcards().
4122 * Copy the file name into allocated memory and add a '*' at the end.
4123 * When expanding other names: The string will be used with regcomp(). Copy
4124 * the name into allocated memory and prepend "^".
4125 */
4126 char_u *
4127addstar(fname, len, context)
4128 char_u *fname;
4129 int len;
4130 int context; /* EXPAND_FILES etc. */
4131{
4132 char_u *retval;
4133 int i, j;
4134 int new_len;
4135 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004136 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004138 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004139 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004140 && context != EXPAND_SHELLCMD
4141 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 {
4143 /*
4144 * Matching will be done internally (on something other than files).
4145 * So we convert the file-matching-type wildcards into our kind for
4146 * use with vim_regcomp(). First work out how long it will be:
4147 */
4148
4149 /* For help tags the translation is done in find_help_tags().
4150 * For a tag pattern starting with "/" no translation is needed. */
4151 if (context == EXPAND_HELP
4152 || context == EXPAND_COLORS
4153 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004154 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004155 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 || (context == EXPAND_TAGS && fname[0] == '/'))
4157 retval = vim_strnsave(fname, len);
4158 else
4159 {
4160 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4161 for (i = 0; i < len; i++)
4162 {
4163 if (fname[i] == '*' || fname[i] == '~')
4164 new_len++; /* '*' needs to be replaced by ".*"
4165 '~' needs to be replaced by "\~" */
4166
4167 /* Buffer names are like file names. "." should be literal */
4168 if (context == EXPAND_BUFFERS && fname[i] == '.')
4169 new_len++; /* "." becomes "\." */
4170
4171 /* Custom expansion takes care of special things, match
4172 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004173 if ((context == EXPAND_USER_DEFINED
4174 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 new_len++; /* '\' becomes "\\" */
4176 }
4177 retval = alloc(new_len);
4178 if (retval != NULL)
4179 {
4180 retval[0] = '^';
4181 j = 1;
4182 for (i = 0; i < len; i++, j++)
4183 {
4184 /* Skip backslash. But why? At least keep it for custom
4185 * expansion. */
4186 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004187 && context != EXPAND_USER_LIST
4188 && fname[i] == '\\'
4189 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 break;
4191
4192 switch (fname[i])
4193 {
4194 case '*': retval[j++] = '.';
4195 break;
4196 case '~': retval[j++] = '\\';
4197 break;
4198 case '?': retval[j] = '.';
4199 continue;
4200 case '.': if (context == EXPAND_BUFFERS)
4201 retval[j++] = '\\';
4202 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004203 case '\\': if (context == EXPAND_USER_DEFINED
4204 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 retval[j++] = '\\';
4206 break;
4207 }
4208 retval[j] = fname[i];
4209 }
4210 retval[j] = NUL;
4211 }
4212 }
4213 }
4214 else
4215 {
4216 retval = alloc(len + 4);
4217 if (retval != NULL)
4218 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004219 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
4221 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004222 * Don't add a star to *, ~, ~user, $var or `cmd`.
4223 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 * ~ would be at the start of the file name, but not the tail.
4225 * $ could be anywhere in the tail.
4226 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004227 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 */
4229 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004230 ends_in_star = (len > 0 && retval[len - 1] == '*');
4231#ifndef BACKSLASH_IN_FILENAME
4232 for (i = len - 2; i >= 0; --i)
4233 {
4234 if (retval[i] != '\\')
4235 break;
4236 ends_in_star = !ends_in_star;
4237 }
4238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004240 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 && vim_strchr(tail, '$') == NULL
4242 && vim_strchr(retval, '`') == NULL)
4243 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004244 else if (len > 0 && retval[len - 1] == '$')
4245 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 retval[len] = NUL;
4247 }
4248 }
4249 return retval;
4250}
4251
4252/*
4253 * Must parse the command line so far to work out what context we are in.
4254 * Completion can then be done based on that context.
4255 * This routine sets the variables:
4256 * xp->xp_pattern The start of the pattern to be expanded within
4257 * the command line (ends at the cursor).
4258 * xp->xp_context The type of thing to expand. Will be one of:
4259 *
4260 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4261 * the command line, like an unknown command. Caller
4262 * should beep.
4263 * EXPAND_NOTHING Unrecognised context for completion, use char like
4264 * a normal char, rather than for completion. eg
4265 * :s/^I/
4266 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4267 * it.
4268 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4269 * EXPAND_FILES After command with XFILE set, or after setting
4270 * with P_EXPAND set. eg :e ^I, :w>>^I
4271 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4272 * when we know only directories are of interest. eg
4273 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004274 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4276 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4277 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4278 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4279 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4280 * EXPAND_EVENTS Complete event names
4281 * EXPAND_SYNTAX Complete :syntax command arguments
4282 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4283 * EXPAND_AUGROUP Complete autocommand group names
4284 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4285 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4286 * eg :unmap a^I , :cunab x^I
4287 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4288 * eg :call sub^I
4289 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4290 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4291 * names in expressions, eg :while s^I
4292 * EXPAND_ENV_VARS Complete environment variable names
4293 */
4294 static void
4295set_expand_context(xp)
4296 expand_T *xp;
4297{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004298 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 if (ccline.cmdfirstc != ':'
4300#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004301 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004302 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303#endif
4304 )
4305 {
4306 xp->xp_context = EXPAND_NOTHING;
4307 return;
4308 }
4309 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4310}
4311
4312 void
4313set_cmd_context(xp, str, len, col)
4314 expand_T *xp;
4315 char_u *str; /* start of command line */
4316 int len; /* length of command line (excl. NUL) */
4317 int col; /* position of cursor */
4318{
4319 int old_char = NUL;
4320 char_u *nextcomm;
4321
4322 /*
4323 * Avoid a UMR warning from Purify, only save the character if it has been
4324 * written before.
4325 */
4326 if (col < len)
4327 old_char = str[col];
4328 str[col] = NUL;
4329 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004330
4331#ifdef FEAT_EVAL
4332 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004333 {
4334# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004335 /* pass CMD_SIZE because there is no real command */
4336 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004337# endif
4338 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004339 else if (ccline.input_fn)
4340 {
4341 xp->xp_context = ccline.xp_context;
4342 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004343# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004344 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004345# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004346 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004347 else
4348#endif
4349 while (nextcomm != NULL)
4350 nextcomm = set_one_cmd_context(xp, nextcomm);
4351
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 str[col] = old_char;
4353}
4354
4355/*
4356 * Expand the command line "str" from context "xp".
4357 * "xp" must have been set by set_cmd_context().
4358 * xp->xp_pattern points into "str", to where the text that is to be expanded
4359 * starts.
4360 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4361 * cursor.
4362 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4363 * key that triggered expansion literally.
4364 * Returns EXPAND_OK otherwise.
4365 */
4366 int
4367expand_cmdline(xp, str, col, matchcount, matches)
4368 expand_T *xp;
4369 char_u *str; /* start of command line */
4370 int col; /* position of cursor */
4371 int *matchcount; /* return: nr of matches */
4372 char_u ***matches; /* return: array of pointers to matches */
4373{
4374 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004375 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
4377 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4378 {
4379 beep_flush();
4380 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4381 }
4382 if (xp->xp_context == EXPAND_NOTHING)
4383 {
4384 /* Caller can use the character as a normal char instead */
4385 return EXPAND_NOTHING;
4386 }
4387
4388 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004389 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4390 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 if (file_str == NULL)
4392 return EXPAND_UNSUCCESSFUL;
4393
Bram Moolenaar94950a92010-12-02 16:01:29 +01004394 if (p_wic)
4395 options += WILD_ICASE;
4396
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004398 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
4400 *matchcount = 0;
4401 *matches = NULL;
4402 }
4403 vim_free(file_str);
4404
4405 return EXPAND_OK;
4406}
4407
4408#ifdef FEAT_MULTI_LANG
4409/*
4410 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4411 */
4412static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4413
4414 static void
4415cleanup_help_tags(num_file, file)
4416 int num_file;
4417 char_u **file;
4418{
4419 int i, j;
4420 int len;
4421
4422 for (i = 0; i < num_file; ++i)
4423 {
4424 len = (int)STRLEN(file[i]) - 3;
4425 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4426 {
4427 /* Sorting on priority means the same item in another language may
4428 * be anywhere. Search all items for a match up to the "@en". */
4429 for (j = 0; j < num_file; ++j)
4430 if (j != i
4431 && (int)STRLEN(file[j]) == len + 3
4432 && STRNCMP(file[i], file[j], len + 1) == 0)
4433 break;
4434 if (j == num_file)
4435 file[i][len] = NUL;
4436 }
4437 }
4438}
4439#endif
4440
4441/*
4442 * Do the expansion based on xp->xp_context and "pat".
4443 */
4444 static int
4445ExpandFromContext(xp, pat, num_file, file, options)
4446 expand_T *xp;
4447 char_u *pat;
4448 int *num_file;
4449 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004450 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
4452#ifdef FEAT_CMDL_COMPL
4453 regmatch_T regmatch;
4454#endif
4455 int ret;
4456 int flags;
4457
4458 flags = EW_DIR; /* include directories */
4459 if (options & WILD_LIST_NOTFOUND)
4460 flags |= EW_NOTFOUND;
4461 if (options & WILD_ADD_SLASH)
4462 flags |= EW_ADDSLASH;
4463 if (options & WILD_KEEP_ALL)
4464 flags |= EW_KEEPALL;
4465 if (options & WILD_SILENT)
4466 flags |= EW_SILENT;
4467
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004468 if (xp->xp_context == EXPAND_FILES
4469 || xp->xp_context == EXPAND_DIRECTORIES
4470 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
4472 /*
4473 * Expand file or directory names.
4474 */
4475 int free_pat = FALSE;
4476 int i;
4477
4478 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4479 * space */
4480 if (xp->xp_backslash != XP_BS_NONE)
4481 {
4482 free_pat = TRUE;
4483 pat = vim_strsave(pat);
4484 for (i = 0; pat[i]; ++i)
4485 if (pat[i] == '\\')
4486 {
4487 if (xp->xp_backslash == XP_BS_THREE
4488 && pat[i + 1] == '\\'
4489 && pat[i + 2] == '\\'
4490 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004491 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 if (xp->xp_backslash == XP_BS_ONE
4493 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004494 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
4496 }
4497
4498 if (xp->xp_context == EXPAND_FILES)
4499 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004500 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4501 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 else
4503 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004504 if (options & WILD_ICASE)
4505 flags |= EW_ICASE;
4506
Bram Moolenaard7834d32009-12-02 16:14:36 +00004507 /* Expand wildcards, supporting %:h and the like. */
4508 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 if (free_pat)
4510 vim_free(pat);
4511 return ret;
4512 }
4513
4514 *file = (char_u **)"";
4515 *num_file = 0;
4516 if (xp->xp_context == EXPAND_HELP)
4517 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004518 /* With an empty argument we would get all the help tags, which is
4519 * very slow. Get matches for "help" instead. */
4520 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4521 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 {
4523#ifdef FEAT_MULTI_LANG
4524 cleanup_help_tags(*num_file, *file);
4525#endif
4526 return OK;
4527 }
4528 return FAIL;
4529 }
4530
4531#ifndef FEAT_CMDL_COMPL
4532 return FAIL;
4533#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004534 if (xp->xp_context == EXPAND_SHELLCMD)
4535 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 if (xp->xp_context == EXPAND_OLD_SETTING)
4537 return ExpandOldSetting(num_file, file);
4538 if (xp->xp_context == EXPAND_BUFFERS)
4539 return ExpandBufnames(pat, num_file, file, options);
4540 if (xp->xp_context == EXPAND_TAGS
4541 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4542 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4543 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004544 {
4545 char *directories[] = {"colors", NULL};
4546 return ExpandRTDir(pat, num_file, file, directories);
4547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004549 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004550 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004551 return ExpandRTDir(pat, num_file, file, directories);
4552 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004553 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004554 {
4555 char *directories[] = {"syntax", NULL};
4556 return ExpandRTDir(pat, num_file, file, directories);
4557 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004558 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004559 {
4560 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4561 return ExpandRTDir(pat, num_file, file, directories);
4562 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004563# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4564 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004565 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004566# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
4568 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4569 if (regmatch.regprog == NULL)
4570 return FAIL;
4571
4572 /* set ignore-case according to p_ic, p_scs and pat */
4573 regmatch.rm_ic = ignorecase(pat);
4574
4575 if (xp->xp_context == EXPAND_SETTINGS
4576 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4577 ret = ExpandSettings(xp, &regmatch, num_file, file);
4578 else if (xp->xp_context == EXPAND_MAPPINGS)
4579 ret = ExpandMappings(&regmatch, num_file, file);
4580# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4581 else if (xp->xp_context == EXPAND_USER_DEFINED)
4582 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4583# endif
4584 else
4585 {
4586 static struct expgen
4587 {
4588 int context;
4589 char_u *((*func)__ARGS((expand_T *, int)));
4590 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004591 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 } tab[] =
4593 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004594 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4595 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004597 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4598 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4599 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4600 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601#endif
4602#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004603 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4604 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4605 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4606 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607#endif
4608#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004609 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4610 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611#endif
4612#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004613 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004615 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004617 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4618 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004620#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004621 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004622#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004623#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004624 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004625#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004626#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004627 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4630 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004631 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4632 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004634 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 };
4636 int i;
4637
4638 /*
4639 * Find a context in the table and call the ExpandGeneric() with the
4640 * right function to do the expansion.
4641 */
4642 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004643 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 if (xp->xp_context == tab[i].context)
4645 {
4646 if (tab[i].ic)
4647 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004648 ret = ExpandGeneric(xp, &regmatch, num_file, file,
4649 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 break;
4651 }
4652 }
4653
4654 vim_free(regmatch.regprog);
4655
4656 return ret;
4657#endif /* FEAT_CMDL_COMPL */
4658}
4659
4660#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4661/*
4662 * Expand a list of names.
4663 *
4664 * Generic function for command line completion. It calls a function to
4665 * obtain strings, one by one. The strings are matched against a regexp
4666 * program. Matching strings are copied into an array, which is returned.
4667 *
4668 * Returns OK when no problems encountered, FAIL for error (out of memory).
4669 */
4670 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004671ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 expand_T *xp;
4673 regmatch_T *regmatch;
4674 int *num_file;
4675 char_u ***file;
4676 char_u *((*func)__ARGS((expand_T *, int)));
4677 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004678 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
4680 int i;
4681 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004682 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 char_u *str;
4684
4685 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004686 * round == 0: count the number of matching names
4687 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004689 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
4691 for (i = 0; ; ++i)
4692 {
4693 str = (*func)(xp, i);
4694 if (str == NULL) /* end of list */
4695 break;
4696 if (*str == NUL) /* skip empty strings */
4697 continue;
4698
4699 if (vim_regexec(regmatch, str, (colnr_T)0))
4700 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004701 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004703 if (escaped)
4704 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4705 else
4706 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 (*file)[count] = str;
4708#ifdef FEAT_MENU
4709 if (func == get_menu_names && str != NULL)
4710 {
4711 /* test for separator added by get_menu_names() */
4712 str += STRLEN(str) - 1;
4713 if (*str == '\001')
4714 *str = '.';
4715 }
4716#endif
4717 }
4718 ++count;
4719 }
4720 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004721 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
4723 if (count == 0)
4724 return OK;
4725 *num_file = count;
4726 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4727 if (*file == NULL)
4728 {
4729 *file = (char_u **)"";
4730 return FAIL;
4731 }
4732 count = 0;
4733 }
4734 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004735
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004736 /* Sort the results. Keep menu's in the specified order. */
4737 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4738 sort_strings(*file, *num_file);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004739
Bram Moolenaar4f688582007-07-24 12:34:30 +00004740#ifdef FEAT_CMDL_COMPL
4741 /* Reset the variables used for special highlight names expansion, so that
4742 * they don't show up when getting normal highlight names by ID. */
4743 reset_expand_highlight();
4744#endif
4745
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 return OK;
4747}
4748
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004749/*
4750 * Complete a shell command.
4751 * Returns FAIL or OK;
4752 */
4753 static int
4754expand_shellcmd(filepat, num_file, file, flagsarg)
4755 char_u *filepat; /* pattern to match with command names */
4756 int *num_file; /* return: number of matches */
4757 char_u ***file; /* return: array with matches */
4758 int flagsarg; /* EW_ flags */
4759{
4760 char_u *pat;
4761 int i;
4762 char_u *path;
4763 int mustfree = FALSE;
4764 garray_T ga;
4765 char_u *buf = alloc(MAXPATHL);
4766 size_t l;
4767 char_u *s, *e;
4768 int flags = flagsarg;
4769 int ret;
4770
4771 if (buf == NULL)
4772 return FAIL;
4773
4774 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4775 * space */
4776 pat = vim_strsave(filepat);
4777 for (i = 0; pat[i]; ++i)
4778 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004779 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004780
4781 flags |= EW_FILE | EW_EXEC;
4782
4783 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004784 if (mch_isFullName(pat))
4785 path = (char_u *)" ";
4786 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004787 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4788 path = (char_u *)".";
4789 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004790 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004791 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004792 if (path == NULL)
4793 path = (char_u *)"";
4794 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004795
4796 /*
4797 * Go over all directories in $PATH. Expand matches in that directory and
4798 * collect them in "ga".
4799 */
4800 ga_init2(&ga, (int)sizeof(char *), 10);
4801 for (s = path; *s != NUL; s = e)
4802 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004803 if (*s == ' ')
4804 ++s; /* Skip space used for absolute path name. */
4805
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004806#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4807 e = vim_strchr(s, ';');
4808#else
4809 e = vim_strchr(s, ':');
4810#endif
4811 if (e == NULL)
4812 e = s + STRLEN(s);
4813
4814 l = e - s;
4815 if (l > MAXPATHL - 5)
4816 break;
4817 vim_strncpy(buf, s, l);
4818 add_pathsep(buf);
4819 l = STRLEN(buf);
4820 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4821
4822 /* Expand matches in one directory of $PATH. */
4823 ret = expand_wildcards(1, &buf, num_file, file, flags);
4824 if (ret == OK)
4825 {
4826 if (ga_grow(&ga, *num_file) == FAIL)
4827 FreeWild(*num_file, *file);
4828 else
4829 {
4830 for (i = 0; i < *num_file; ++i)
4831 {
4832 s = (*file)[i];
4833 if (STRLEN(s) > l)
4834 {
4835 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004836 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004837 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4838 }
4839 else
4840 vim_free(s);
4841 }
4842 vim_free(*file);
4843 }
4844 }
4845 if (*e != NUL)
4846 ++e;
4847 }
4848 *file = ga.ga_data;
4849 *num_file = ga.ga_len;
4850
4851 vim_free(buf);
4852 vim_free(pat);
4853 if (mustfree)
4854 vim_free(path);
4855 return OK;
4856}
4857
4858
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004860static 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));
4861
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004863 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004864 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004866 static void *
4867call_user_expand_func(user_expand_func, xp, num_file, file)
4868 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 int *num_file;
4871 char_u ***file;
4872{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004873 char_u keep;
4874 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004877 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004878 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004881 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 *num_file = 0;
4883 *file = NULL;
4884
Bram Moolenaar21669c02008-01-18 12:16:16 +00004885 if (ccline.cmdbuff == NULL)
4886 {
4887 /* Completion from Insert mode, pass fake arguments. */
4888 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004889 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004890 args[1] = xp->xp_pattern;
4891 }
4892 else
4893 {
4894 /* Completion on the command line, pass real arguments. */
4895 keep = ccline.cmdbuff[ccline.cmdlen];
4896 ccline.cmdbuff[ccline.cmdlen] = 0;
4897 sprintf((char *)num, "%d", ccline.cmdpos);
4898 args[1] = ccline.cmdbuff;
4899 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004900 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 args[2] = num;
4902
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004903 /* Save the cmdline, we don't know what the function may do. */
4904 save_ccline = ccline;
4905 ccline.cmdbuff = NULL;
4906 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004908
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004909 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004910
4911 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004913 if (ccline.cmdbuff != NULL)
4914 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004915
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004916 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004917 return ret;
4918}
4919
4920/*
4921 * Expand names with a function defined by the user.
4922 */
4923 static int
4924ExpandUserDefined(xp, regmatch, num_file, file)
4925 expand_T *xp;
4926 regmatch_T *regmatch;
4927 int *num_file;
4928 char_u ***file;
4929{
4930 char_u *retstr;
4931 char_u *s;
4932 char_u *e;
4933 char_u keep;
4934 garray_T ga;
4935
4936 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4937 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 return FAIL;
4939
4940 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004941 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
4943 e = vim_strchr(s, '\n');
4944 if (e == NULL)
4945 e = s + STRLEN(s);
4946 keep = *e;
4947 *e = 0;
4948
4949 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4950 {
4951 *e = keep;
4952 if (*e != NUL)
4953 ++e;
4954 continue;
4955 }
4956
4957 if (ga_grow(&ga, 1) == FAIL)
4958 break;
4959
4960 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4961 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
4963 *e = keep;
4964 if (*e != NUL)
4965 ++e;
4966 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004967 vim_free(retstr);
4968 *file = ga.ga_data;
4969 *num_file = ga.ga_len;
4970 return OK;
4971}
4972
4973/*
4974 * Expand names with a list returned by a function defined by the user.
4975 */
4976 static int
4977ExpandUserList(xp, num_file, file)
4978 expand_T *xp;
4979 int *num_file;
4980 char_u ***file;
4981{
4982 list_T *retlist;
4983 listitem_T *li;
4984 garray_T ga;
4985
4986 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4987 if (retlist == NULL)
4988 return FAIL;
4989
4990 ga_init2(&ga, (int)sizeof(char *), 3);
4991 /* Loop over the items in the list. */
4992 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4993 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00004994 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
4995 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004996
4997 if (ga_grow(&ga, 1) == FAIL)
4998 break;
4999
5000 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005001 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005002 ++ga.ga_len;
5003 }
5004 list_unref(retlist);
5005
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 *file = ga.ga_data;
5007 *num_file = ga.ga_len;
5008 return OK;
5009}
5010#endif
5011
5012/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005013 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005014 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005015 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 */
5017 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005018ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 char_u *pat;
5020 int *num_file;
5021 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005022 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005024 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 char_u *s;
5026 char_u *e;
5027 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005028 int i;
5029 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030
5031 *num_file = 0;
5032 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005033 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005034 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005036 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005038 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5039 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005041 ga_clear_strings(&ga);
5042 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005044 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5045 matches = globpath(p_rtp, s, 0);
5046 vim_free(s);
5047 if (matches == NULL)
5048 continue;
5049
5050 for (s = matches; *s != NUL; s = e)
5051 {
5052 e = vim_strchr(s, '\n');
5053 if (e == NULL)
5054 e = s + STRLEN(s);
5055 if (ga_grow(&ga, 1) == FAIL)
5056 break;
5057 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5058 {
5059 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5060 if (*s == '\n' || vim_ispathsep(*s))
5061 break;
5062 ++s;
5063 ((char_u **)ga.ga_data)[ga.ga_len] =
5064 vim_strnsave(s, (int)(e - s - 4));
5065 ++ga.ga_len;
5066 }
5067 if (*e != NUL)
5068 ++e;
5069 }
5070 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005072 if (ga.ga_len == 0)
5073 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005074
5075 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005076 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005077 remove_duplicates(&ga);
5078
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 *file = ga.ga_data;
5080 *num_file = ga.ga_len;
5081 return OK;
5082}
5083
5084#endif
5085
5086#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5087/*
5088 * Expand "file" for all comma-separated directories in "path".
5089 * Returns an allocated string with all matches concatenated, separated by
5090 * newlines. Returns NULL for an error or no matches.
5091 */
5092 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005093globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 char_u *path;
5095 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005096 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097{
5098 expand_T xpc;
5099 char_u *buf;
5100 garray_T ga;
5101 int i;
5102 int len;
5103 int num_p;
5104 char_u **p;
5105 char_u *cur = NULL;
5106
5107 buf = alloc(MAXPATHL);
5108 if (buf == NULL)
5109 return NULL;
5110
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005111 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 ga_init2(&ga, 1, 100);
5115
5116 /* Loop over all entries in {path}. */
5117 while (*path != NUL)
5118 {
5119 /* Copy one item of the path to buf[] and concatenate the file name. */
5120 copy_option_part(&path, buf, MAXPATHL, ",");
5121 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5122 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005123# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005124 /* Using the platform's path separator (\) makes vim incorrectly
5125 * treat it as an escape character, use '/' instead. */
5126 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5127 STRCAT(buf, "/");
5128# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005130# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005132 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5133 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005135 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005137 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
5139 /* Concatenate new results to previous ones. */
5140 if (ga_grow(&ga, len) == OK)
5141 {
5142 cur = (char_u *)ga.ga_data + ga.ga_len;
5143 for (i = 0; i < num_p; ++i)
5144 {
5145 STRCPY(cur, p[i]);
5146 cur += STRLEN(p[i]);
5147 *cur++ = '\n';
5148 }
5149 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151 FreeWild(num_p, p);
5152 }
5153 }
5154 }
5155 if (cur != NULL)
5156 *--cur = 0; /* Replace trailing newline with NUL */
5157
5158 vim_free(buf);
5159 return (char_u *)ga.ga_data;
5160}
5161
5162#endif
5163
5164#if defined(FEAT_CMDHIST) || defined(PROTO)
5165
5166/*********************************
5167 * Command line history stuff *
5168 *********************************/
5169
5170/*
5171 * Translate a history character to the associated type number.
5172 */
5173 static int
5174hist_char2type(c)
5175 int c;
5176{
5177 if (c == ':')
5178 return HIST_CMD;
5179 if (c == '=')
5180 return HIST_EXPR;
5181 if (c == '@')
5182 return HIST_INPUT;
5183 if (c == '>')
5184 return HIST_DEBUG;
5185 return HIST_SEARCH; /* must be '?' or '/' */
5186}
5187
5188/*
5189 * Table of history names.
5190 * These names are used in :history and various hist...() functions.
5191 * It is sufficient to give the significant prefix of a history name.
5192 */
5193
5194static char *(history_names[]) =
5195{
5196 "cmd",
5197 "search",
5198 "expr",
5199 "input",
5200 "debug",
5201 NULL
5202};
5203
5204/*
5205 * init_history() - Initialize the command line history.
5206 * Also used to re-allocate the history when the size changes.
5207 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005208 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209init_history()
5210{
5211 int newlen; /* new length of history table */
5212 histentry_T *temp;
5213 int i;
5214 int j;
5215 int type;
5216
5217 /*
5218 * If size of history table changed, reallocate it
5219 */
5220 newlen = (int)p_hi;
5221 if (newlen != hislen) /* history length changed */
5222 {
5223 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5224 {
5225 if (newlen)
5226 {
5227 temp = (histentry_T *)lalloc(
5228 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5229 if (temp == NULL) /* out of memory! */
5230 {
5231 if (type == 0) /* first one: just keep the old length */
5232 {
5233 newlen = hislen;
5234 break;
5235 }
5236 /* Already changed one table, now we can only have zero
5237 * length for all tables. */
5238 newlen = 0;
5239 type = -1;
5240 continue;
5241 }
5242 }
5243 else
5244 temp = NULL;
5245 if (newlen == 0 || temp != NULL)
5246 {
5247 if (hisidx[type] < 0) /* there are no entries yet */
5248 {
5249 for (i = 0; i < newlen; ++i)
5250 {
5251 temp[i].hisnum = 0;
5252 temp[i].hisstr = NULL;
5253 }
5254 }
5255 else if (newlen > hislen) /* array becomes bigger */
5256 {
5257 for (i = 0; i <= hisidx[type]; ++i)
5258 temp[i] = history[type][i];
5259 j = i;
5260 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5261 {
5262 temp[i].hisnum = 0;
5263 temp[i].hisstr = NULL;
5264 }
5265 for ( ; j < hislen; ++i, ++j)
5266 temp[i] = history[type][j];
5267 }
5268 else /* array becomes smaller or 0 */
5269 {
5270 j = hisidx[type];
5271 for (i = newlen - 1; ; --i)
5272 {
5273 if (i >= 0) /* copy newest entries */
5274 temp[i] = history[type][j];
5275 else /* remove older entries */
5276 vim_free(history[type][j].hisstr);
5277 if (--j < 0)
5278 j = hislen - 1;
5279 if (j == hisidx[type])
5280 break;
5281 }
5282 hisidx[type] = newlen - 1;
5283 }
5284 vim_free(history[type]);
5285 history[type] = temp;
5286 }
5287 }
5288 hislen = newlen;
5289 }
5290}
5291
5292/*
5293 * Check if command line 'str' is already in history.
5294 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5295 */
5296 static int
Bram Moolenaar4c402232011-07-27 17:58:46 +02005297in_history(type, str, move_to_front, sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 int type;
5299 char_u *str;
5300 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005301 int sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302{
5303 int i;
5304 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005305 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306
5307 if (hisidx[type] < 0)
5308 return FALSE;
5309 i = hisidx[type];
5310 do
5311 {
5312 if (history[type][i].hisstr == NULL)
5313 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005314
5315 /* For search history, check that the separator character matches as
5316 * well. */
5317 p = history[type][i].hisstr;
5318 if (STRCMP(str, p) == 0
5319 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {
5321 if (!move_to_front)
5322 return TRUE;
5323 last_i = i;
5324 break;
5325 }
5326 if (--i < 0)
5327 i = hislen - 1;
5328 } while (i != hisidx[type]);
5329
5330 if (last_i >= 0)
5331 {
5332 str = history[type][i].hisstr;
5333 while (i != hisidx[type])
5334 {
5335 if (++i >= hislen)
5336 i = 0;
5337 history[type][last_i] = history[type][i];
5338 last_i = i;
5339 }
5340 history[type][i].hisstr = str;
5341 history[type][i].hisnum = ++hisnum[type];
5342 return TRUE;
5343 }
5344 return FALSE;
5345}
5346
5347/*
5348 * Convert history name (from table above) to its HIST_ equivalent.
5349 * When "name" is empty, return "cmd" history.
5350 * Returns -1 for unknown history name.
5351 */
5352 int
5353get_histtype(name)
5354 char_u *name;
5355{
5356 int i;
5357 int len = (int)STRLEN(name);
5358
5359 /* No argument: use current history. */
5360 if (len == 0)
5361 return hist_char2type(ccline.cmdfirstc);
5362
5363 for (i = 0; history_names[i] != NULL; ++i)
5364 if (STRNICMP(name, history_names[i], len) == 0)
5365 return i;
5366
5367 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5368 return hist_char2type(name[0]);
5369
5370 return -1;
5371}
5372
5373static int last_maptick = -1; /* last seen maptick */
5374
5375/*
5376 * Add the given string to the given history. If the string is already in the
5377 * history then it is moved to the front. "histype" may be one of he HIST_
5378 * values.
5379 */
5380 void
5381add_to_history(histype, new_entry, in_map, sep)
5382 int histype;
5383 char_u *new_entry;
5384 int in_map; /* consider maptick when inside a mapping */
5385 int sep; /* separator character used (search hist) */
5386{
5387 histentry_T *hisptr;
5388 int len;
5389
5390 if (hislen == 0) /* no history */
5391 return;
5392
5393 /*
5394 * Searches inside the same mapping overwrite each other, so that only
5395 * the last line is kept. Be careful not to remove a line that was moved
5396 * down, only lines that were added.
5397 */
5398 if (histype == HIST_SEARCH && in_map)
5399 {
5400 if (maptick == last_maptick)
5401 {
5402 /* Current line is from the same mapping, remove it */
5403 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5404 vim_free(hisptr->hisstr);
5405 hisptr->hisstr = NULL;
5406 hisptr->hisnum = 0;
5407 --hisnum[histype];
5408 if (--hisidx[HIST_SEARCH] < 0)
5409 hisidx[HIST_SEARCH] = hislen - 1;
5410 }
5411 last_maptick = -1;
5412 }
Bram Moolenaar4c402232011-07-27 17:58:46 +02005413 if (!in_history(histype, new_entry, TRUE, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {
5415 if (++hisidx[histype] == hislen)
5416 hisidx[histype] = 0;
5417 hisptr = &history[histype][hisidx[histype]];
5418 vim_free(hisptr->hisstr);
5419
5420 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005421 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5423 if (hisptr->hisstr != NULL)
5424 hisptr->hisstr[len + 1] = sep;
5425
5426 hisptr->hisnum = ++hisnum[histype];
5427 if (histype == HIST_SEARCH && in_map)
5428 last_maptick = maptick;
5429 }
5430}
5431
5432#if defined(FEAT_EVAL) || defined(PROTO)
5433
5434/*
5435 * Get identifier of newest history entry.
5436 * "histype" may be one of the HIST_ values.
5437 */
5438 int
5439get_history_idx(histype)
5440 int histype;
5441{
5442 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5443 || hisidx[histype] < 0)
5444 return -1;
5445
5446 return history[histype][hisidx[histype]].hisnum;
5447}
5448
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005449static struct cmdline_info *get_ccline_ptr __ARGS((void));
5450
5451/*
5452 * Get pointer to the command line info to use. cmdline_paste() may clear
5453 * ccline and put the previous value in prev_ccline.
5454 */
5455 static struct cmdline_info *
5456get_ccline_ptr()
5457{
5458 if ((State & CMDLINE) == 0)
5459 return NULL;
5460 if (ccline.cmdbuff != NULL)
5461 return &ccline;
5462 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5463 return &prev_ccline;
5464 return NULL;
5465}
5466
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467/*
5468 * Get the current command line in allocated memory.
5469 * Only works when the command line is being edited.
5470 * Returns NULL when something is wrong.
5471 */
5472 char_u *
5473get_cmdline_str()
5474{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005475 struct cmdline_info *p = get_ccline_ptr();
5476
5477 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005479 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480}
5481
5482/*
5483 * Get the current command line position, counted in bytes.
5484 * Zero is the first position.
5485 * Only works when the command line is being edited.
5486 * Returns -1 when something is wrong.
5487 */
5488 int
5489get_cmdline_pos()
5490{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005491 struct cmdline_info *p = get_ccline_ptr();
5492
5493 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005495 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496}
5497
5498/*
5499 * Set the command line byte position to "pos". Zero is the first position.
5500 * Only works when the command line is being edited.
5501 * Returns 1 when failed, 0 when OK.
5502 */
5503 int
5504set_cmdline_pos(pos)
5505 int pos;
5506{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005507 struct cmdline_info *p = get_ccline_ptr();
5508
5509 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 return 1;
5511
5512 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5513 * changed the command line. */
5514 if (pos < 0)
5515 new_cmdpos = 0;
5516 else
5517 new_cmdpos = pos;
5518 return 0;
5519}
5520
5521/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005522 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005523 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005524 * Only works when the command line is being edited.
5525 * Returns NUL when something is wrong.
5526 */
5527 int
5528get_cmdline_type()
5529{
5530 struct cmdline_info *p = get_ccline_ptr();
5531
5532 if (p == NULL)
5533 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005534 if (p->cmdfirstc == NUL)
5535 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005536 return p->cmdfirstc;
5537}
5538
5539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 * Calculate history index from a number:
5541 * num > 0: seen as identifying number of a history entry
5542 * num < 0: relative position in history wrt newest entry
5543 * "histype" may be one of the HIST_ values.
5544 */
5545 static int
5546calc_hist_idx(histype, num)
5547 int histype;
5548 int num;
5549{
5550 int i;
5551 histentry_T *hist;
5552 int wrapped = FALSE;
5553
5554 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5555 || (i = hisidx[histype]) < 0 || num == 0)
5556 return -1;
5557
5558 hist = history[histype];
5559 if (num > 0)
5560 {
5561 while (hist[i].hisnum > num)
5562 if (--i < 0)
5563 {
5564 if (wrapped)
5565 break;
5566 i += hislen;
5567 wrapped = TRUE;
5568 }
5569 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5570 return i;
5571 }
5572 else if (-num <= hislen)
5573 {
5574 i += num + 1;
5575 if (i < 0)
5576 i += hislen;
5577 if (hist[i].hisstr != NULL)
5578 return i;
5579 }
5580 return -1;
5581}
5582
5583/*
5584 * Get a history entry by its index.
5585 * "histype" may be one of the HIST_ values.
5586 */
5587 char_u *
5588get_history_entry(histype, idx)
5589 int histype;
5590 int idx;
5591{
5592 idx = calc_hist_idx(histype, idx);
5593 if (idx >= 0)
5594 return history[histype][idx].hisstr;
5595 else
5596 return (char_u *)"";
5597}
5598
5599/*
5600 * Clear all entries of a history.
5601 * "histype" may be one of the HIST_ values.
5602 */
5603 int
5604clr_history(histype)
5605 int histype;
5606{
5607 int i;
5608 histentry_T *hisptr;
5609
5610 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5611 {
5612 hisptr = history[histype];
5613 for (i = hislen; i--;)
5614 {
5615 vim_free(hisptr->hisstr);
5616 hisptr->hisnum = 0;
5617 hisptr++->hisstr = NULL;
5618 }
5619 hisidx[histype] = -1; /* mark history as cleared */
5620 hisnum[histype] = 0; /* reset identifier counter */
5621 return OK;
5622 }
5623 return FAIL;
5624}
5625
5626/*
5627 * Remove all entries matching {str} from a history.
5628 * "histype" may be one of the HIST_ values.
5629 */
5630 int
5631del_history_entry(histype, str)
5632 int histype;
5633 char_u *str;
5634{
5635 regmatch_T regmatch;
5636 histentry_T *hisptr;
5637 int idx;
5638 int i;
5639 int last;
5640 int found = FALSE;
5641
5642 regmatch.regprog = NULL;
5643 regmatch.rm_ic = FALSE; /* always match case */
5644 if (hislen != 0
5645 && histype >= 0
5646 && histype < HIST_COUNT
5647 && *str != NUL
5648 && (idx = hisidx[histype]) >= 0
5649 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5650 != NULL)
5651 {
5652 i = last = idx;
5653 do
5654 {
5655 hisptr = &history[histype][i];
5656 if (hisptr->hisstr == NULL)
5657 break;
5658 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5659 {
5660 found = TRUE;
5661 vim_free(hisptr->hisstr);
5662 hisptr->hisstr = NULL;
5663 hisptr->hisnum = 0;
5664 }
5665 else
5666 {
5667 if (i != last)
5668 {
5669 history[histype][last] = *hisptr;
5670 hisptr->hisstr = NULL;
5671 hisptr->hisnum = 0;
5672 }
5673 if (--last < 0)
5674 last += hislen;
5675 }
5676 if (--i < 0)
5677 i += hislen;
5678 } while (i != idx);
5679 if (history[histype][idx].hisstr == NULL)
5680 hisidx[histype] = -1;
5681 }
5682 vim_free(regmatch.regprog);
5683 return found;
5684}
5685
5686/*
5687 * Remove an indexed entry from a history.
5688 * "histype" may be one of the HIST_ values.
5689 */
5690 int
5691del_history_idx(histype, idx)
5692 int histype;
5693 int idx;
5694{
5695 int i, j;
5696
5697 i = calc_hist_idx(histype, idx);
5698 if (i < 0)
5699 return FALSE;
5700 idx = hisidx[histype];
5701 vim_free(history[histype][i].hisstr);
5702
5703 /* When deleting the last added search string in a mapping, reset
5704 * last_maptick, so that the last added search string isn't deleted again.
5705 */
5706 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5707 last_maptick = -1;
5708
5709 while (i != idx)
5710 {
5711 j = (i + 1) % hislen;
5712 history[histype][i] = history[histype][j];
5713 i = j;
5714 }
5715 history[histype][i].hisstr = NULL;
5716 history[histype][i].hisnum = 0;
5717 if (--i < 0)
5718 i += hislen;
5719 hisidx[histype] = i;
5720 return TRUE;
5721}
5722
5723#endif /* FEAT_EVAL */
5724
5725#if defined(FEAT_CRYPT) || defined(PROTO)
5726/*
5727 * Very specific function to remove the value in ":set key=val" from the
5728 * history.
5729 */
5730 void
5731remove_key_from_history()
5732{
5733 char_u *p;
5734 int i;
5735
5736 i = hisidx[HIST_CMD];
5737 if (i < 0)
5738 return;
5739 p = history[HIST_CMD][i].hisstr;
5740 if (p != NULL)
5741 for ( ; *p; ++p)
5742 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5743 {
5744 p = vim_strchr(p + 3, '=');
5745 if (p == NULL)
5746 break;
5747 ++p;
5748 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5749 if (p[i] == '\\' && p[i + 1])
5750 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005751 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 --p;
5753 }
5754}
5755#endif
5756
5757#endif /* FEAT_CMDHIST */
5758
5759#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5760/*
5761 * Get indices "num1,num2" that specify a range within a list (not a range of
5762 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5763 * Returns OK if parsed successfully, otherwise FAIL.
5764 */
5765 int
5766get_list_range(str, num1, num2)
5767 char_u **str;
5768 int *num1;
5769 int *num2;
5770{
5771 int len;
5772 int first = FALSE;
5773 long num;
5774
5775 *str = skipwhite(*str);
5776 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5777 {
5778 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5779 *str += len;
5780 *num1 = (int)num;
5781 first = TRUE;
5782 }
5783 *str = skipwhite(*str);
5784 if (**str == ',') /* parse "to" part of range */
5785 {
5786 *str = skipwhite(*str + 1);
5787 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5788 if (len > 0)
5789 {
5790 *num2 = (int)num;
5791 *str = skipwhite(*str + len);
5792 }
5793 else if (!first) /* no number given at all */
5794 return FAIL;
5795 }
5796 else if (first) /* only one number given */
5797 *num2 = *num1;
5798 return OK;
5799}
5800#endif
5801
5802#if defined(FEAT_CMDHIST) || defined(PROTO)
5803/*
5804 * :history command - print a history
5805 */
5806 void
5807ex_history(eap)
5808 exarg_T *eap;
5809{
5810 histentry_T *hist;
5811 int histype1 = HIST_CMD;
5812 int histype2 = HIST_CMD;
5813 int hisidx1 = 1;
5814 int hisidx2 = -1;
5815 int idx;
5816 int i, j, k;
5817 char_u *end;
5818 char_u *arg = eap->arg;
5819
5820 if (hislen == 0)
5821 {
5822 MSG(_("'history' option is zero"));
5823 return;
5824 }
5825
5826 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5827 {
5828 end = arg;
5829 while (ASCII_ISALPHA(*end)
5830 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5831 end++;
5832 i = *end;
5833 *end = NUL;
5834 histype1 = get_histtype(arg);
5835 if (histype1 == -1)
5836 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005837 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {
5839 histype1 = 0;
5840 histype2 = HIST_COUNT-1;
5841 }
5842 else
5843 {
5844 *end = i;
5845 EMSG(_(e_trailing));
5846 return;
5847 }
5848 }
5849 else
5850 histype2 = histype1;
5851 *end = i;
5852 }
5853 else
5854 end = arg;
5855 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5856 {
5857 EMSG(_(e_trailing));
5858 return;
5859 }
5860
5861 for (; !got_int && histype1 <= histype2; ++histype1)
5862 {
5863 STRCPY(IObuff, "\n # ");
5864 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5865 MSG_PUTS_TITLE(IObuff);
5866 idx = hisidx[histype1];
5867 hist = history[histype1];
5868 j = hisidx1;
5869 k = hisidx2;
5870 if (j < 0)
5871 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5872 if (k < 0)
5873 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5874 if (idx >= 0 && j <= k)
5875 for (i = idx + 1; !got_int; ++i)
5876 {
5877 if (i == hislen)
5878 i = 0;
5879 if (hist[i].hisstr != NULL
5880 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5881 {
5882 msg_putchar('\n');
5883 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5884 hist[i].hisnum);
5885 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5886 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5887 (int)Columns - 10);
5888 else
5889 STRCAT(IObuff, hist[i].hisstr);
5890 msg_outtrans(IObuff);
5891 out_flush();
5892 }
5893 if (i == idx)
5894 break;
5895 }
5896 }
5897}
5898#endif
5899
5900#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5901static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5902static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5903static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5904static int viminfo_add_at_front = FALSE;
5905
5906static int hist_type2char __ARGS((int type, int use_question));
5907
5908/*
5909 * Translate a history type number to the associated character.
5910 */
5911 static int
5912hist_type2char(type, use_question)
5913 int type;
5914 int use_question; /* use '?' instead of '/' */
5915{
5916 if (type == HIST_CMD)
5917 return ':';
5918 if (type == HIST_SEARCH)
5919 {
5920 if (use_question)
5921 return '?';
5922 else
5923 return '/';
5924 }
5925 if (type == HIST_EXPR)
5926 return '=';
5927 return '@';
5928}
5929
5930/*
5931 * Prepare for reading the history from the viminfo file.
5932 * This allocates history arrays to store the read history lines.
5933 */
5934 void
5935prepare_viminfo_history(asklen)
5936 int asklen;
5937{
5938 int i;
5939 int num;
5940 int type;
5941 int len;
5942
5943 init_history();
5944 viminfo_add_at_front = (asklen != 0);
5945 if (asklen > hislen)
5946 asklen = hislen;
5947
5948 for (type = 0; type < HIST_COUNT; ++type)
5949 {
5950 /*
5951 * Count the number of empty spaces in the history list. If there are
5952 * more spaces available than we request, then fill them up.
5953 */
5954 for (i = 0, num = 0; i < hislen; i++)
5955 if (history[type][i].hisstr == NULL)
5956 num++;
5957 len = asklen;
5958 if (num > len)
5959 len = num;
5960 if (len <= 0)
5961 viminfo_history[type] = NULL;
5962 else
5963 viminfo_history[type] =
5964 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5965 if (viminfo_history[type] == NULL)
5966 len = 0;
5967 viminfo_hislen[type] = len;
5968 viminfo_hisidx[type] = 0;
5969 }
5970}
5971
5972/*
5973 * Accept a line from the viminfo, store it in the history array when it's
5974 * new.
5975 */
5976 int
5977read_viminfo_history(virp)
5978 vir_T *virp;
5979{
5980 int type;
5981 long_u len;
5982 char_u *val;
5983 char_u *p;
5984
5985 type = hist_char2type(virp->vir_line[0]);
5986 if (viminfo_hisidx[type] < viminfo_hislen[type])
5987 {
5988 val = viminfo_readstring(virp, 1, TRUE);
5989 if (val != NULL && *val != NUL)
5990 {
5991 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar4c402232011-07-27 17:58:46 +02005992 viminfo_add_at_front, *val))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 {
5994 /* Need to re-allocate to append the separator byte. */
5995 len = STRLEN(val);
5996 p = lalloc(len + 2, TRUE);
5997 if (p != NULL)
5998 {
5999 if (type == HIST_SEARCH)
6000 {
6001 /* Search entry: Move the separator from the first
6002 * column to after the NUL. */
6003 mch_memmove(p, val + 1, (size_t)len);
6004 p[len] = (*val == ' ' ? NUL : *val);
6005 }
6006 else
6007 {
6008 /* Not a search entry: No separator in the viminfo
6009 * file, add a NUL separator. */
6010 mch_memmove(p, val, (size_t)len + 1);
6011 p[len + 1] = NUL;
6012 }
6013 viminfo_history[type][viminfo_hisidx[type]++] = p;
6014 }
6015 }
6016 }
6017 vim_free(val);
6018 }
6019 return viminfo_readline(virp);
6020}
6021
6022 void
6023finish_viminfo_history()
6024{
6025 int idx;
6026 int i;
6027 int type;
6028
6029 for (type = 0; type < HIST_COUNT; ++type)
6030 {
6031 if (history[type] == NULL)
6032 return;
6033 idx = hisidx[type] + viminfo_hisidx[type];
6034 if (idx >= hislen)
6035 idx -= hislen;
6036 else if (idx < 0)
6037 idx = hislen - 1;
6038 if (viminfo_add_at_front)
6039 hisidx[type] = idx;
6040 else
6041 {
6042 if (hisidx[type] == -1)
6043 hisidx[type] = hislen - 1;
6044 do
6045 {
6046 if (history[type][idx].hisstr != NULL)
6047 break;
6048 if (++idx == hislen)
6049 idx = 0;
6050 } while (idx != hisidx[type]);
6051 if (idx != hisidx[type] && --idx < 0)
6052 idx = hislen - 1;
6053 }
6054 for (i = 0; i < viminfo_hisidx[type]; i++)
6055 {
6056 vim_free(history[type][idx].hisstr);
6057 history[type][idx].hisstr = viminfo_history[type][i];
6058 if (--idx < 0)
6059 idx = hislen - 1;
6060 }
6061 idx += 1;
6062 idx %= hislen;
6063 for (i = 0; i < viminfo_hisidx[type]; i++)
6064 {
6065 history[type][idx++].hisnum = ++hisnum[type];
6066 idx %= hislen;
6067 }
6068 vim_free(viminfo_history[type]);
6069 viminfo_history[type] = NULL;
6070 }
6071}
6072
6073 void
6074write_viminfo_history(fp)
6075 FILE *fp;
6076{
6077 int i;
6078 int type;
6079 int num_saved;
6080 char_u *p;
6081 int c;
6082
6083 init_history();
6084 if (hislen == 0)
6085 return;
6086 for (type = 0; type < HIST_COUNT; ++type)
6087 {
6088 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6089 if (num_saved == 0)
6090 continue;
6091 if (num_saved < 0) /* Use default */
6092 num_saved = hislen;
6093 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6094 type == HIST_CMD ? _("Command Line") :
6095 type == HIST_SEARCH ? _("Search String") :
6096 type == HIST_EXPR ? _("Expression") :
6097 _("Input Line"));
6098 if (num_saved > hislen)
6099 num_saved = hislen;
6100 i = hisidx[type];
6101 if (i >= 0)
6102 while (num_saved--)
6103 {
6104 p = history[type][i].hisstr;
6105 if (p != NULL)
6106 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006107 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 /* For the search history: put the separator in the second
6109 * column; use a space if there isn't one. */
6110 if (type == HIST_SEARCH)
6111 {
6112 c = p[STRLEN(p) + 1];
6113 putc(c == NUL ? ' ' : c, fp);
6114 }
6115 viminfo_writestring(fp, p);
6116 }
6117 if (--i < 0)
6118 i = hislen - 1;
6119 }
6120 }
6121}
6122#endif /* FEAT_VIMINFO */
6123
6124#if defined(FEAT_FKMAP) || defined(PROTO)
6125/*
6126 * Write a character at the current cursor+offset position.
6127 * It is directly written into the command buffer block.
6128 */
6129 void
6130cmd_pchar(c, offset)
6131 int c, offset;
6132{
6133 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6134 {
6135 EMSG(_("E198: cmd_pchar beyond the command length"));
6136 return;
6137 }
6138 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6139 ccline.cmdbuff[ccline.cmdlen] = NUL;
6140}
6141
6142 int
6143cmd_gchar(offset)
6144 int offset;
6145{
6146 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6147 {
6148 /* EMSG(_("cmd_gchar beyond the command length")); */
6149 return NUL;
6150 }
6151 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6152}
6153#endif
6154
6155#if defined(FEAT_CMDWIN) || defined(PROTO)
6156/*
6157 * Open a window on the current command line and history. Allow editing in
6158 * the window. Returns when the window is closed.
6159 * Returns:
6160 * CR if the command is to be executed
6161 * Ctrl_C if it is to be abandoned
6162 * K_IGNORE if editing continues
6163 */
6164 static int
6165ex_window()
6166{
6167 struct cmdline_info save_ccline;
6168 buf_T *old_curbuf = curbuf;
6169 win_T *old_curwin = curwin;
6170 buf_T *bp;
6171 win_T *wp;
6172 int i;
6173 linenr_T lnum;
6174 int histtype;
6175 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006176#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 int save_restart_edit = restart_edit;
6180 int save_State = State;
6181 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006182#ifdef FEAT_RIGHTLEFT
6183 int save_cmdmsg_rl = cmdmsg_rl;
6184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
6186 /* Can't do this recursively. Can't do it when typing a password. */
6187 if (cmdwin_type != 0
6188# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6189 || cmdline_star > 0
6190# endif
6191 )
6192 {
6193 beep_flush();
6194 return K_IGNORE;
6195 }
6196
6197 /* Save current window sizes. */
6198 win_size_save(&winsizes);
6199
6200# ifdef FEAT_AUTOCMD
6201 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006202 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006204 /* don't use a new tab page */
6205 cmdmod.tab = 0;
6206
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 /* Create a window for the command-line buffer. */
6208 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6209 {
6210 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006211# ifdef FEAT_AUTOCMD
6212 unblock_autocmds();
6213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 return K_IGNORE;
6215 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006216 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
6218 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006219 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006220 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6222 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6223 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006224#ifdef FEAT_FOLDING
6225 curwin->w_p_fen = FALSE;
6226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006228 curwin->w_p_rl = cmdmsg_rl;
6229 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006231 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
6233# ifdef FEAT_AUTOCMD
6234 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006235 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236# endif
6237
Bram Moolenaar46152342005-09-07 21:18:43 +00006238 /* Showing the prompt may have set need_wait_return, reset it. */
6239 need_wait_return = FALSE;
6240
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006241 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6243 {
6244 if (p_wc == TAB)
6245 {
6246 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6247 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6248 }
6249 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6250 }
6251
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006252 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6253 * sets 'textwidth' to 78). */
6254 curbuf->b_p_tw = 0;
6255
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 /* Fill the buffer with the history. */
6257 init_history();
6258 if (hislen > 0)
6259 {
6260 i = hisidx[histtype];
6261 if (i >= 0)
6262 {
6263 lnum = 0;
6264 do
6265 {
6266 if (++i == hislen)
6267 i = 0;
6268 if (history[histtype][i].hisstr != NULL)
6269 ml_append(lnum++, history[histtype][i].hisstr,
6270 (colnr_T)0, FALSE);
6271 }
6272 while (i != hisidx[histtype]);
6273 }
6274 }
6275
6276 /* Replace the empty last line with the current command-line and put the
6277 * cursor there. */
6278 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6279 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6280 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006281 changed_line_abv_curs();
6282 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006283 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284
6285 /* Save the command line info, can be used recursively. */
6286 save_ccline = ccline;
6287 ccline.cmdbuff = NULL;
6288 ccline.cmdprompt = NULL;
6289
6290 /* No Ex mode here! */
6291 exmode_active = 0;
6292
6293 State = NORMAL;
6294# ifdef FEAT_MOUSE
6295 setmouse();
6296# endif
6297
6298# ifdef FEAT_AUTOCMD
6299 /* Trigger CmdwinEnter autocommands. */
6300 typestr[0] = cmdwin_type;
6301 typestr[1] = NUL;
6302 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006303 if (restart_edit != 0) /* autocmd with ":startinsert" */
6304 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305# endif
6306
6307 i = RedrawingDisabled;
6308 RedrawingDisabled = 0;
6309
6310 /*
6311 * Call the main loop until <CR> or CTRL-C is typed.
6312 */
6313 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006314 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315
6316 RedrawingDisabled = i;
6317
6318# ifdef FEAT_AUTOCMD
6319 /* Trigger CmdwinLeave autocommands. */
6320 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6321# endif
6322
Bram Moolenaarccc18222007-05-10 18:25:20 +00006323 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 ccline = save_ccline;
6325 cmdwin_type = 0;
6326
6327 exmode_active = save_exmode;
6328
Bram Moolenaarf9821062008-06-20 16:31:07 +00006329 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 * this happens! */
6331 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6332 {
6333 cmdwin_result = Ctrl_C;
6334 EMSG(_("E199: Active window or buffer deleted"));
6335 }
6336 else
6337 {
6338# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6339 /* autocmds may abort script processing */
6340 if (aborting() && cmdwin_result != K_IGNORE)
6341 cmdwin_result = Ctrl_C;
6342# endif
6343 /* Set the new command line from the cmdline buffer. */
6344 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006345 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006347 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6348
6349 if (histtype == HIST_CMD)
6350 {
6351 /* Execute the command directly. */
6352 ccline.cmdbuff = vim_strsave((char_u *)p);
6353 cmdwin_result = CAR;
6354 }
6355 else
6356 {
6357 /* First need to cancel what we were doing. */
6358 ccline.cmdbuff = NULL;
6359 stuffcharReadbuff(':');
6360 stuffReadbuff((char_u *)p);
6361 stuffcharReadbuff(CAR);
6362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 }
6364 else if (cmdwin_result == K_XF2) /* :qa typed */
6365 {
6366 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6367 cmdwin_result = CAR;
6368 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006369 else if (cmdwin_result == Ctrl_C)
6370 {
6371 /* :q or :close, don't execute any command
6372 * and don't modify the cmd window. */
6373 ccline.cmdbuff = NULL;
6374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 else
6376 ccline.cmdbuff = vim_strsave(ml_get_curline());
6377 if (ccline.cmdbuff == NULL)
6378 cmdwin_result = Ctrl_C;
6379 else
6380 {
6381 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6382 ccline.cmdbufflen = ccline.cmdlen + 1;
6383 ccline.cmdpos = curwin->w_cursor.col;
6384 if (ccline.cmdpos > ccline.cmdlen)
6385 ccline.cmdpos = ccline.cmdlen;
6386 if (cmdwin_result == K_IGNORE)
6387 {
6388 set_cmdspos_cursor();
6389 redrawcmd();
6390 }
6391 }
6392
6393# ifdef FEAT_AUTOCMD
6394 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006395 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396# endif
6397 wp = curwin;
6398 bp = curbuf;
6399 win_goto(old_curwin);
6400 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006401
6402 /* win_close() may have already wiped the buffer when 'bh' is
6403 * set to 'wipe' */
6404 if (buf_valid(bp))
6405 close_buffer(NULL, bp, DOBUF_WIPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
6407 /* Restore window sizes. */
6408 win_size_restore(&winsizes);
6409
6410# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006411 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412# endif
6413 }
6414
6415 ga_clear(&winsizes);
6416 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006417# ifdef FEAT_RIGHTLEFT
6418 cmdmsg_rl = save_cmdmsg_rl;
6419# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420
6421 State = save_State;
6422# ifdef FEAT_MOUSE
6423 setmouse();
6424# endif
6425
6426 return cmdwin_result;
6427}
6428#endif /* FEAT_CMDWIN */
6429
6430/*
6431 * Used for commands that either take a simple command string argument, or:
6432 * cmd << endmarker
6433 * {script}
6434 * endmarker
6435 * Returns a pointer to allocated memory with {script} or NULL.
6436 */
6437 char_u *
6438script_get(eap, cmd)
6439 exarg_T *eap;
6440 char_u *cmd;
6441{
6442 char_u *theline;
6443 char *end_pattern = NULL;
6444 char dot[] = ".";
6445 garray_T ga;
6446
6447 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6448 return NULL;
6449
6450 ga_init2(&ga, 1, 0x400);
6451
6452 if (cmd[2] != NUL)
6453 end_pattern = (char *)skipwhite(cmd + 2);
6454 else
6455 end_pattern = dot;
6456
6457 for (;;)
6458 {
6459 theline = eap->getline(
6460#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006461 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462#endif
6463 NUL, eap->cookie, 0);
6464
6465 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006466 {
6467 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470
6471 ga_concat(&ga, theline);
6472 ga_append(&ga, '\n');
6473 vim_free(theline);
6474 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006475 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476
6477 return (char_u *)ga.ga_data;
6478}