blob: da03a0494f0adaec4dcbd757f78519c5665bbc41 [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;
2873 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2874 /* Avoid clearing the rest of the line too often. */
2875 if (cmdline_row != i || ccline.overstrike)
2876 msg_clr_eos();
2877 msg_no_more = FALSE;
2878 }
2879#ifdef FEAT_FKMAP
2880 /*
2881 * If we are in Farsi command mode, the character input must be in
2882 * Insert mode. So do not advance the cmdpos.
2883 */
2884 if (!cmd_fkmap)
2885#endif
2886 {
2887 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002890 if (m < 0) /* overflow, Columns or Rows at weird value */
2891 m = MAXCOL;
2892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 else
2894 m = MAXCOL;
2895 for (i = 0; i < len; ++i)
2896 {
2897 c = cmdline_charsize(ccline.cmdpos);
2898#ifdef FEAT_MBYTE
2899 /* count ">" for a double-wide char that doesn't fit. */
2900 if (has_mbyte)
2901 correct_cmdspos(ccline.cmdpos, c);
2902#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002903 /* Stop cursor at the end of the screen, but do increment the
2904 * insert position, so that entering a very long command
2905 * works, even though you can't see it. */
2906 if (ccline.cmdspos + c < m)
2907 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908#ifdef FEAT_MBYTE
2909 if (has_mbyte)
2910 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002911 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 if (c > len - i - 1)
2913 c = len - i - 1;
2914 ccline.cmdpos += c;
2915 i += c;
2916 }
2917#endif
2918 ++ccline.cmdpos;
2919 }
2920 }
2921 }
2922 if (redraw)
2923 msg_check();
2924 return retval;
2925}
2926
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002927static struct cmdline_info prev_ccline;
2928static int prev_ccline_used = FALSE;
2929
2930/*
2931 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2932 * and overwrite it. But get_cmdline_str() may need it, thus make it
2933 * available globally in prev_ccline.
2934 */
2935 static void
2936save_cmdline(ccp)
2937 struct cmdline_info *ccp;
2938{
2939 if (!prev_ccline_used)
2940 {
2941 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2942 prev_ccline_used = TRUE;
2943 }
2944 *ccp = prev_ccline;
2945 prev_ccline = ccline;
2946 ccline.cmdbuff = NULL;
2947 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002948 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002949}
2950
2951/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002952 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002953 */
2954 static void
2955restore_cmdline(ccp)
2956 struct cmdline_info *ccp;
2957{
2958 ccline = prev_ccline;
2959 prev_ccline = *ccp;
2960}
2961
Bram Moolenaar5a305422006-04-28 22:38:25 +00002962#if defined(FEAT_EVAL) || defined(PROTO)
2963/*
2964 * Save the command line into allocated memory. Returns a pointer to be
2965 * passed to restore_cmdline_alloc() later.
2966 * Returns NULL when failed.
2967 */
2968 char_u *
2969save_cmdline_alloc()
2970{
2971 struct cmdline_info *p;
2972
2973 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2974 if (p != NULL)
2975 save_cmdline(p);
2976 return (char_u *)p;
2977}
2978
2979/*
2980 * Restore the command line from the return value of save_cmdline_alloc().
2981 */
2982 void
2983restore_cmdline_alloc(p)
2984 char_u *p;
2985{
2986 if (p != NULL)
2987 {
2988 restore_cmdline((struct cmdline_info *)p);
2989 vim_free(p);
2990 }
2991}
2992#endif
2993
Bram Moolenaar8299df92004-07-10 09:47:34 +00002994/*
2995 * paste a yank register into the command line.
2996 * used by CTRL-R command in command-line mode
2997 * insert_reg() can't be used here, because special characters from the
2998 * register contents will be interpreted as commands.
2999 *
3000 * return FAIL for failure, OK otherwise
3001 */
3002 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003003cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003004 int regname;
3005 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003006 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003007{
3008 long i;
3009 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003010 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003011 int allocated;
3012 struct cmdline_info save_ccline;
3013
3014 /* check for valid regname; also accept special characters for CTRL-R in
3015 * the command line */
3016 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3017 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3018 return FAIL;
3019
3020 /* A register containing CTRL-R can cause an endless loop. Allow using
3021 * CTRL-C to break the loop. */
3022 line_breakcheck();
3023 if (got_int)
3024 return FAIL;
3025
3026#ifdef FEAT_CLIPBOARD
3027 regname = may_get_selection(regname);
3028#endif
3029
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003030 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003031 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003032 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003033 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003034 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003035 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003036 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003037
3038 if (i)
3039 {
3040 /* Got the value of a special register in "arg". */
3041 if (arg == NULL)
3042 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003043
3044 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3045 * part of the word. */
3046 p = arg;
3047 if (p_is && regname == Ctrl_W)
3048 {
3049 char_u *w;
3050 int len;
3051
3052 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003053 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003054 {
3055#ifdef FEAT_MBYTE
3056 if (has_mbyte)
3057 {
3058 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3059 if (!vim_iswordc(mb_ptr2char(w - len)))
3060 break;
3061 w -= len;
3062 }
3063 else
3064#endif
3065 {
3066 if (!vim_iswordc(w[-1]))
3067 break;
3068 --w;
3069 }
3070 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003071 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003072 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3073 p += len;
3074 }
3075
3076 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003077 if (allocated)
3078 vim_free(arg);
3079 return OK;
3080 }
3081
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003082 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003083}
3084
3085/*
3086 * Put a string on the command line.
3087 * When "literally" is TRUE, insert literally.
3088 * When "literally" is FALSE, insert as typed, but don't leave the command
3089 * line.
3090 */
3091 void
3092cmdline_paste_str(s, literally)
3093 char_u *s;
3094 int literally;
3095{
3096 int c, cv;
3097
3098 if (literally)
3099 put_on_cmdline(s, -1, TRUE);
3100 else
3101 while (*s != NUL)
3102 {
3103 cv = *s;
3104 if (cv == Ctrl_V && s[1])
3105 ++s;
3106#ifdef FEAT_MBYTE
3107 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003108 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003109 else
3110#endif
3111 c = *s++;
3112 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3113#ifdef UNIX
3114 || c == intr_char
3115#endif
3116 || (c == Ctrl_BSL && *s == Ctrl_N))
3117 stuffcharReadbuff(Ctrl_V);
3118 stuffcharReadbuff(c);
3119 }
3120}
3121
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122#ifdef FEAT_WILDMENU
3123/*
3124 * Delete characters on the command line, from "from" to the current
3125 * position.
3126 */
3127 static void
3128cmdline_del(from)
3129 int from;
3130{
3131 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3132 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3133 ccline.cmdlen -= ccline.cmdpos - from;
3134 ccline.cmdpos = from;
3135}
3136#endif
3137
3138/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003139 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 * search
3141 */
3142 void
3143redrawcmdline()
3144{
3145 if (cmd_silent)
3146 return;
3147 need_wait_return = FALSE;
3148 compute_cmdrow();
3149 redrawcmd();
3150 cursorcmd();
3151}
3152
3153 static void
3154redrawcmdprompt()
3155{
3156 int i;
3157
3158 if (cmd_silent)
3159 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003160 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 msg_putchar(ccline.cmdfirstc);
3162 if (ccline.cmdprompt != NULL)
3163 {
3164 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3165 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3166 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003167 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 --ccline.cmdindent;
3169 }
3170 else
3171 for (i = ccline.cmdindent; i > 0; --i)
3172 msg_putchar(' ');
3173}
3174
3175/*
3176 * Redraw what is currently on the command line.
3177 */
3178 void
3179redrawcmd()
3180{
3181 if (cmd_silent)
3182 return;
3183
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003184 /* when 'incsearch' is set there may be no command line while redrawing */
3185 if (ccline.cmdbuff == NULL)
3186 {
3187 windgoto(cmdline_row, 0);
3188 msg_clr_eos();
3189 return;
3190 }
3191
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 msg_start();
3193 redrawcmdprompt();
3194
3195 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3196 msg_no_more = TRUE;
3197 draw_cmdline(0, ccline.cmdlen);
3198 msg_clr_eos();
3199 msg_no_more = FALSE;
3200
3201 set_cmdspos_cursor();
3202
3203 /*
3204 * An emsg() before may have set msg_scroll. This is used in normal mode,
3205 * in cmdline mode we can reset them now.
3206 */
3207 msg_scroll = FALSE; /* next message overwrites cmdline */
3208
3209 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3210 * in cmdline mode */
3211 skip_redraw = FALSE;
3212}
3213
3214 void
3215compute_cmdrow()
3216{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003217 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 cmdline_row = Rows - 1;
3219 else
3220 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3221 + W_STATUS_HEIGHT(lastwin);
3222}
3223
3224 static void
3225cursorcmd()
3226{
3227 if (cmd_silent)
3228 return;
3229
3230#ifdef FEAT_RIGHTLEFT
3231 if (cmdmsg_rl)
3232 {
3233 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3234 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3235 if (msg_row <= 0)
3236 msg_row = Rows - 1;
3237 }
3238 else
3239#endif
3240 {
3241 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3242 msg_col = ccline.cmdspos % (int)Columns;
3243 if (msg_row >= Rows)
3244 msg_row = Rows - 1;
3245 }
3246
3247 windgoto(msg_row, msg_col);
3248#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3249 redrawcmd_preedit();
3250#endif
3251#ifdef MCH_CURSOR_SHAPE
3252 mch_update_cursor();
3253#endif
3254}
3255
3256 void
3257gotocmdline(clr)
3258 int clr;
3259{
3260 msg_start();
3261#ifdef FEAT_RIGHTLEFT
3262 if (cmdmsg_rl)
3263 msg_col = Columns - 1;
3264 else
3265#endif
3266 msg_col = 0; /* always start in column 0 */
3267 if (clr) /* clear the bottom line(s) */
3268 msg_clr_eos(); /* will reset clear_cmdline */
3269 windgoto(cmdline_row, 0);
3270}
3271
3272/*
3273 * Check the word in front of the cursor for an abbreviation.
3274 * Called when the non-id character "c" has been entered.
3275 * When an abbreviation is recognized it is removed from the text with
3276 * backspaces and the replacement string is inserted, followed by "c".
3277 */
3278 static int
3279ccheck_abbr(c)
3280 int c;
3281{
3282 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3283 return FALSE;
3284
3285 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3286}
3287
3288/*
3289 * Return FAIL if this is not an appropriate context in which to do
3290 * completion of anything, return OK if it is (even if there are no matches).
3291 * For the caller, this means that the character is just passed through like a
3292 * normal character (instead of being expanded). This allows :s/^I^D etc.
3293 */
3294 static int
3295nextwild(xp, type, options)
3296 expand_T *xp;
3297 int type;
3298 int options; /* extra options for ExpandOne() */
3299{
3300 int i, j;
3301 char_u *p1;
3302 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 int difflen;
3304 int v;
3305
3306 if (xp->xp_numfiles == -1)
3307 {
3308 set_expand_context(xp);
3309 cmd_showtail = expand_showtail(xp);
3310 }
3311
3312 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3313 {
3314 beep_flush();
3315 return OK; /* Something illegal on command line */
3316 }
3317 if (xp->xp_context == EXPAND_NOTHING)
3318 {
3319 /* Caller can use the character as a normal char instead */
3320 return FAIL;
3321 }
3322
3323 MSG_PUTS("..."); /* show that we are busy */
3324 out_flush();
3325
3326 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003327 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
3329 if (type == WILD_NEXT || type == WILD_PREV)
3330 {
3331 /*
3332 * Get next/previous match for a previous expanded pattern.
3333 */
3334 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3335 }
3336 else
3337 {
3338 /*
3339 * Translate string into pattern and expand it.
3340 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003341 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3342 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 p2 = NULL;
3344 else
3345 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003346 int use_options = options |
3347 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE;
3348
3349 if (p_wic)
3350 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003351 p2 = ExpandOne(xp, p1,
3352 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003353 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003355 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 if (p2 != NULL && type == WILD_LONGEST)
3357 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003358 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (ccline.cmdbuff[i + j] == '*'
3360 || ccline.cmdbuff[i + j] == '?')
3361 break;
3362 if ((int)STRLEN(p2) < j)
3363 {
3364 vim_free(p2);
3365 p2 = NULL;
3366 }
3367 }
3368 }
3369 }
3370
3371 if (p2 != NULL && !got_int)
3372 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003373 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003374 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003376 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 xp->xp_pattern = ccline.cmdbuff + i;
3378 }
3379 else
3380 v = OK;
3381 if (v == OK)
3382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003383 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3384 &ccline.cmdbuff[ccline.cmdpos],
3385 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3386 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 ccline.cmdlen += difflen;
3388 ccline.cmdpos += difflen;
3389 }
3390 }
3391 vim_free(p2);
3392
3393 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003394 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 /* When expanding a ":map" command and no matches are found, assume that
3397 * the key is supposed to be inserted literally */
3398 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3399 return FAIL;
3400
3401 if (xp->xp_numfiles <= 0 && p2 == NULL)
3402 beep_flush();
3403 else if (xp->xp_numfiles == 1)
3404 /* free expanded pattern */
3405 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3406
3407 return OK;
3408}
3409
3410/*
3411 * Do wildcard expansion on the string 'str'.
3412 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003413 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 * Return NULL for failure.
3415 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003416 * "orig" is the originally expanded string, copied to allocated memory. It
3417 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3418 * WILD_PREV "orig" should be NULL.
3419 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003420 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3421 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 *
3423 * mode = WILD_FREE: just free previously expanded matches
3424 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3425 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3426 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3427 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3428 * mode = WILD_ALL: return all matches concatenated
3429 * mode = WILD_LONGEST: return longest matched part
3430 *
3431 * options = WILD_LIST_NOTFOUND: list entries without a match
3432 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3433 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3434 * options = WILD_NO_BEEP: Don't beep for multiple matches
3435 * options = WILD_ADD_SLASH: add a slash after directory names
3436 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3437 * options = WILD_SILENT: don't print warning messages
3438 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003439 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 *
3441 * The variables xp->xp_context and xp->xp_backslash must have been set!
3442 */
3443 char_u *
3444ExpandOne(xp, str, orig, options, mode)
3445 expand_T *xp;
3446 char_u *str;
3447 char_u *orig; /* allocated copy of original of expanded string */
3448 int options;
3449 int mode;
3450{
3451 char_u *ss = NULL;
3452 static int findex;
3453 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003454 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 int i;
3456 long_u len;
3457 int non_suf_match; /* number without matching suffix */
3458
3459 /*
3460 * first handle the case of using an old match
3461 */
3462 if (mode == WILD_NEXT || mode == WILD_PREV)
3463 {
3464 if (xp->xp_numfiles > 0)
3465 {
3466 if (mode == WILD_PREV)
3467 {
3468 if (findex == -1)
3469 findex = xp->xp_numfiles;
3470 --findex;
3471 }
3472 else /* mode == WILD_NEXT */
3473 ++findex;
3474
3475 /*
3476 * When wrapping around, return the original string, set findex to
3477 * -1.
3478 */
3479 if (findex < 0)
3480 {
3481 if (orig_save == NULL)
3482 findex = xp->xp_numfiles - 1;
3483 else
3484 findex = -1;
3485 }
3486 if (findex >= xp->xp_numfiles)
3487 {
3488 if (orig_save == NULL)
3489 findex = 0;
3490 else
3491 findex = -1;
3492 }
3493#ifdef FEAT_WILDMENU
3494 if (p_wmnu)
3495 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3496 findex, cmd_showtail);
3497#endif
3498 if (findex == -1)
3499 return vim_strsave(orig_save);
3500 return vim_strsave(xp->xp_files[findex]);
3501 }
3502 else
3503 return NULL;
3504 }
3505
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003506 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3508 {
3509 FreeWild(xp->xp_numfiles, xp->xp_files);
3510 xp->xp_numfiles = -1;
3511 vim_free(orig_save);
3512 orig_save = NULL;
3513 }
3514 findex = 0;
3515
3516 if (mode == WILD_FREE) /* only release file name */
3517 return NULL;
3518
3519 if (xp->xp_numfiles == -1)
3520 {
3521 vim_free(orig_save);
3522 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003523 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
3525 /*
3526 * Do the expansion.
3527 */
3528 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3529 options) == FAIL)
3530 {
3531#ifdef FNAME_ILLEGAL
3532 /* Illegal file name has been silently skipped. But when there
3533 * are wildcards, the real problem is that there was no match,
3534 * causing the pattern to be added, which has illegal characters.
3535 */
3536 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3537 EMSG2(_(e_nomatch2), str);
3538#endif
3539 }
3540 else if (xp->xp_numfiles == 0)
3541 {
3542 if (!(options & WILD_SILENT))
3543 EMSG2(_(e_nomatch2), str);
3544 }
3545 else
3546 {
3547 /* Escape the matches for use on the command line. */
3548 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3549
3550 /*
3551 * Check for matching suffixes in file names.
3552 */
3553 if (mode != WILD_ALL && mode != WILD_LONGEST)
3554 {
3555 if (xp->xp_numfiles)
3556 non_suf_match = xp->xp_numfiles;
3557 else
3558 non_suf_match = 1;
3559 if ((xp->xp_context == EXPAND_FILES
3560 || xp->xp_context == EXPAND_DIRECTORIES)
3561 && xp->xp_numfiles > 1)
3562 {
3563 /*
3564 * More than one match; check suffix.
3565 * The files will have been sorted on matching suffix in
3566 * expand_wildcards, only need to check the first two.
3567 */
3568 non_suf_match = 0;
3569 for (i = 0; i < 2; ++i)
3570 if (match_suffix(xp->xp_files[i]))
3571 ++non_suf_match;
3572 }
3573 if (non_suf_match != 1)
3574 {
3575 /* Can we ever get here unless it's while expanding
3576 * interactively? If not, we can get rid of this all
3577 * together. Don't really want to wait for this message
3578 * (and possibly have to hit return to continue!).
3579 */
3580 if (!(options & WILD_SILENT))
3581 EMSG(_(e_toomany));
3582 else if (!(options & WILD_NO_BEEP))
3583 beep_flush();
3584 }
3585 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3586 ss = vim_strsave(xp->xp_files[0]);
3587 }
3588 }
3589 }
3590
3591 /* Find longest common part */
3592 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3593 {
3594 for (len = 0; xp->xp_files[0][len]; ++len)
3595 {
3596 for (i = 0; i < xp->xp_numfiles; ++i)
3597 {
3598#ifdef CASE_INSENSITIVE_FILENAME
3599 if (xp->xp_context == EXPAND_DIRECTORIES
3600 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003601 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 || xp->xp_context == EXPAND_BUFFERS)
3603 {
3604 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3605 TOLOWER_LOC(xp->xp_files[0][len]))
3606 break;
3607 }
3608 else
3609#endif
3610 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3611 break;
3612 }
3613 if (i < xp->xp_numfiles)
3614 {
3615 if (!(options & WILD_NO_BEEP))
3616 vim_beep();
3617 break;
3618 }
3619 }
3620 ss = alloc((unsigned)len + 1);
3621 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003622 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 findex = -1; /* next p_wc gets first one */
3624 }
3625
3626 /* Concatenate all matching names */
3627 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3628 {
3629 len = 0;
3630 for (i = 0; i < xp->xp_numfiles; ++i)
3631 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3632 ss = lalloc(len, TRUE);
3633 if (ss != NULL)
3634 {
3635 *ss = NUL;
3636 for (i = 0; i < xp->xp_numfiles; ++i)
3637 {
3638 STRCAT(ss, xp->xp_files[i]);
3639 if (i != xp->xp_numfiles - 1)
3640 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3641 }
3642 }
3643 }
3644
3645 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3646 ExpandCleanup(xp);
3647
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003648 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003649 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003650 vim_free(orig);
3651
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 return ss;
3653}
3654
3655/*
3656 * Prepare an expand structure for use.
3657 */
3658 void
3659ExpandInit(xp)
3660 expand_T *xp;
3661{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003662 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003663 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003665#ifndef BACKSLASH_IN_FILENAME
3666 xp->xp_shell = FALSE;
3667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 xp->xp_numfiles = -1;
3669 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003670#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3671 xp->xp_arg = NULL;
3672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673}
3674
3675/*
3676 * Cleanup an expand structure after use.
3677 */
3678 void
3679ExpandCleanup(xp)
3680 expand_T *xp;
3681{
3682 if (xp->xp_numfiles >= 0)
3683 {
3684 FreeWild(xp->xp_numfiles, xp->xp_files);
3685 xp->xp_numfiles = -1;
3686 }
3687}
3688
3689 void
3690ExpandEscape(xp, str, numfiles, files, options)
3691 expand_T *xp;
3692 char_u *str;
3693 int numfiles;
3694 char_u **files;
3695 int options;
3696{
3697 int i;
3698 char_u *p;
3699
3700 /*
3701 * May change home directory back to "~"
3702 */
3703 if (options & WILD_HOME_REPLACE)
3704 tilde_replace(str, numfiles, files);
3705
3706 if (options & WILD_ESCAPE)
3707 {
3708 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003709 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003710 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 || xp->xp_context == EXPAND_BUFFERS
3712 || xp->xp_context == EXPAND_DIRECTORIES)
3713 {
3714 /*
3715 * Insert a backslash into a file name before a space, \, %, #
3716 * and wildmatch characters, except '~'.
3717 */
3718 for (i = 0; i < numfiles; ++i)
3719 {
3720 /* for ":set path=" we need to escape spaces twice */
3721 if (xp->xp_backslash == XP_BS_THREE)
3722 {
3723 p = vim_strsave_escaped(files[i], (char_u *)" ");
3724 if (p != NULL)
3725 {
3726 vim_free(files[i]);
3727 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003728#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 p = vim_strsave_escaped(files[i], (char_u *)" ");
3730 if (p != NULL)
3731 {
3732 vim_free(files[i]);
3733 files[i] = p;
3734 }
3735#endif
3736 }
3737 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003738#ifdef BACKSLASH_IN_FILENAME
3739 p = vim_strsave_fnameescape(files[i], FALSE);
3740#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003741 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (p != NULL)
3744 {
3745 vim_free(files[i]);
3746 files[i] = p;
3747 }
3748
3749 /* If 'str' starts with "\~", replace "~" at start of
3750 * files[i] with "\~". */
3751 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003752 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003755
3756 /* If the first file starts with a '+' escape it. Otherwise it
3757 * could be seen as "+cmd". */
3758 if (*files[0] == '+')
3759 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761 else if (xp->xp_context == EXPAND_TAGS)
3762 {
3763 /*
3764 * Insert a backslash before characters in a tag name that
3765 * would terminate the ":tag" command.
3766 */
3767 for (i = 0; i < numfiles; ++i)
3768 {
3769 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3770 if (p != NULL)
3771 {
3772 vim_free(files[i]);
3773 files[i] = p;
3774 }
3775 }
3776 }
3777 }
3778}
3779
3780/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003781 * Escape special characters in "fname" for when used as a file name argument
3782 * after a Vim command, or, when "shell" is non-zero, a shell command.
3783 * Returns the result in allocated memory.
3784 */
3785 char_u *
3786vim_strsave_fnameescape(fname, shell)
3787 char_u *fname;
3788 int shell;
3789{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003790 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003791#ifdef BACKSLASH_IN_FILENAME
3792 char_u buf[20];
3793 int j = 0;
3794
3795 /* Don't escape '[' and '{' if they are in 'isfname'. */
3796 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3797 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3798 buf[j++] = *p;
3799 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003800 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003801#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003802 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3803 if (shell && csh_like_shell() && p != NULL)
3804 {
3805 char_u *s;
3806
3807 /* For csh and similar shells need to put two backslashes before '!'.
3808 * One is taken by Vim, one by the shell. */
3809 s = vim_strsave_escaped(p, (char_u *)"!");
3810 vim_free(p);
3811 p = s;
3812 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003813#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003814
3815 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3816 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003817 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003818 escape_fname(&p);
3819
3820 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003821}
3822
3823/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003824 * Put a backslash before the file name in "pp", which is in allocated memory.
3825 */
3826 static void
3827escape_fname(pp)
3828 char_u **pp;
3829{
3830 char_u *p;
3831
3832 p = alloc((unsigned)(STRLEN(*pp) + 2));
3833 if (p != NULL)
3834 {
3835 p[0] = '\\';
3836 STRCPY(p + 1, *pp);
3837 vim_free(*pp);
3838 *pp = p;
3839 }
3840}
3841
3842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 * For each file name in files[num_files]:
3844 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3845 */
3846 void
3847tilde_replace(orig_pat, num_files, files)
3848 char_u *orig_pat;
3849 int num_files;
3850 char_u **files;
3851{
3852 int i;
3853 char_u *p;
3854
3855 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3856 {
3857 for (i = 0; i < num_files; ++i)
3858 {
3859 p = home_replace_save(NULL, files[i]);
3860 if (p != NULL)
3861 {
3862 vim_free(files[i]);
3863 files[i] = p;
3864 }
3865 }
3866 }
3867}
3868
3869/*
3870 * Show all matches for completion on the command line.
3871 * Returns EXPAND_NOTHING when the character that triggered expansion should
3872 * be inserted like a normal character.
3873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 static int
3875showmatches(xp, wildmenu)
3876 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003877 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878{
3879#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3880 int num_files;
3881 char_u **files_found;
3882 int i, j, k;
3883 int maxlen;
3884 int lines;
3885 int columns;
3886 char_u *p;
3887 int lastlen;
3888 int attr;
3889 int showtail;
3890
3891 if (xp->xp_numfiles == -1)
3892 {
3893 set_expand_context(xp);
3894 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3895 &num_files, &files_found);
3896 showtail = expand_showtail(xp);
3897 if (i != EXPAND_OK)
3898 return i;
3899
3900 }
3901 else
3902 {
3903 num_files = xp->xp_numfiles;
3904 files_found = xp->xp_files;
3905 showtail = cmd_showtail;
3906 }
3907
3908#ifdef FEAT_WILDMENU
3909 if (!wildmenu)
3910 {
3911#endif
3912 msg_didany = FALSE; /* lines_left will be set */
3913 msg_start(); /* prepare for paging */
3914 msg_putchar('\n');
3915 out_flush();
3916 cmdline_row = msg_row;
3917 msg_didany = FALSE; /* lines_left will be set again */
3918 msg_start(); /* prepare for paging */
3919#ifdef FEAT_WILDMENU
3920 }
3921#endif
3922
3923 if (got_int)
3924 got_int = FALSE; /* only int. the completion, not the cmd line */
3925#ifdef FEAT_WILDMENU
3926 else if (wildmenu)
3927 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3928#endif
3929 else
3930 {
3931 /* find the length of the longest file name */
3932 maxlen = 0;
3933 for (i = 0; i < num_files; ++i)
3934 {
3935 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003936 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 || xp->xp_context == EXPAND_BUFFERS))
3938 {
3939 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3940 j = vim_strsize(NameBuff);
3941 }
3942 else
3943 j = vim_strsize(L_SHOWFILE(i));
3944 if (j > maxlen)
3945 maxlen = j;
3946 }
3947
3948 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3949 lines = num_files;
3950 else
3951 {
3952 /* compute the number of columns and lines for the listing */
3953 maxlen += 2; /* two spaces between file names */
3954 columns = ((int)Columns + 2) / maxlen;
3955 if (columns < 1)
3956 columns = 1;
3957 lines = (num_files + columns - 1) / columns;
3958 }
3959
3960 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3961
3962 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3963 {
3964 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3965 msg_clr_eos();
3966 msg_advance(maxlen - 3);
3967 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3968 }
3969
3970 /* list the files line by line */
3971 for (i = 0; i < lines; ++i)
3972 {
3973 lastlen = 999;
3974 for (k = i; k < num_files; k += lines)
3975 {
3976 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3977 {
3978 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3979 p = files_found[k] + STRLEN(files_found[k]) + 1;
3980 msg_advance(maxlen + 1);
3981 msg_puts(p);
3982 msg_advance(maxlen + 3);
3983 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3984 break;
3985 }
3986 for (j = maxlen - lastlen; --j >= 0; )
3987 msg_putchar(' ');
3988 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003989 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 || xp->xp_context == EXPAND_BUFFERS)
3991 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01003992 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01003993 if (xp->xp_numfiles != -1)
3994 {
3995 char_u *halved_slash;
3996 char_u *exp_path;
3997
3998 /* Expansion was done before and special characters
3999 * were escaped, need to halve backslashes. Also
4000 * $HOME has been replaced with ~/. */
4001 exp_path = expand_env_save_opt(files_found[k], TRUE);
4002 halved_slash = backslash_halve_save(
4003 exp_path != NULL ? exp_path : files_found[k]);
4004 j = mch_isdir(halved_slash != NULL ? halved_slash
4005 : files_found[k]);
4006 vim_free(exp_path);
4007 vim_free(halved_slash);
4008 }
4009 else
4010 /* Expansion was done here, file names are literal. */
4011 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 if (showtail)
4013 p = L_SHOWFILE(k);
4014 else
4015 {
4016 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4017 TRUE);
4018 p = NameBuff;
4019 }
4020 }
4021 else
4022 {
4023 j = FALSE;
4024 p = L_SHOWFILE(k);
4025 }
4026 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4027 }
4028 if (msg_col > 0) /* when not wrapped around */
4029 {
4030 msg_clr_eos();
4031 msg_putchar('\n');
4032 }
4033 out_flush(); /* show one line at a time */
4034 if (got_int)
4035 {
4036 got_int = FALSE;
4037 break;
4038 }
4039 }
4040
4041 /*
4042 * we redraw the command below the lines that we have just listed
4043 * This is a bit tricky, but it saves a lot of screen updating.
4044 */
4045 cmdline_row = msg_row; /* will put it back later */
4046 }
4047
4048 if (xp->xp_numfiles == -1)
4049 FreeWild(num_files, files_found);
4050
4051 return EXPAND_OK;
4052}
4053
4054/*
4055 * Private gettail for showmatches() (and win_redr_status_matches()):
4056 * Find tail of file name path, but ignore trailing "/".
4057 */
4058 char_u *
4059sm_gettail(s)
4060 char_u *s;
4061{
4062 char_u *p;
4063 char_u *t = s;
4064 int had_sep = FALSE;
4065
4066 for (p = s; *p != NUL; )
4067 {
4068 if (vim_ispathsep(*p)
4069#ifdef BACKSLASH_IN_FILENAME
4070 && !rem_backslash(p)
4071#endif
4072 )
4073 had_sep = TRUE;
4074 else if (had_sep)
4075 {
4076 t = p;
4077 had_sep = FALSE;
4078 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004079 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 }
4081 return t;
4082}
4083
4084/*
4085 * Return TRUE if we only need to show the tail of completion matches.
4086 * When not completing file names or there is a wildcard in the path FALSE is
4087 * returned.
4088 */
4089 static int
4090expand_showtail(xp)
4091 expand_T *xp;
4092{
4093 char_u *s;
4094 char_u *end;
4095
4096 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004097 if (xp->xp_context != EXPAND_FILES
4098 && xp->xp_context != EXPAND_SHELLCMD
4099 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 return FALSE;
4101
4102 end = gettail(xp->xp_pattern);
4103 if (end == xp->xp_pattern) /* there is no path separator */
4104 return FALSE;
4105
4106 for (s = xp->xp_pattern; s < end; s++)
4107 {
4108 /* Skip escaped wildcards. Only when the backslash is not a path
4109 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4110 if (rem_backslash(s))
4111 ++s;
4112 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4113 return FALSE;
4114 }
4115 return TRUE;
4116}
4117
4118/*
4119 * Prepare a string for expansion.
4120 * When expanding file names: The string will be used with expand_wildcards().
4121 * Copy the file name into allocated memory and add a '*' at the end.
4122 * When expanding other names: The string will be used with regcomp(). Copy
4123 * the name into allocated memory and prepend "^".
4124 */
4125 char_u *
4126addstar(fname, len, context)
4127 char_u *fname;
4128 int len;
4129 int context; /* EXPAND_FILES etc. */
4130{
4131 char_u *retval;
4132 int i, j;
4133 int new_len;
4134 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004135 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004137 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004138 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004139 && context != EXPAND_SHELLCMD
4140 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
4142 /*
4143 * Matching will be done internally (on something other than files).
4144 * So we convert the file-matching-type wildcards into our kind for
4145 * use with vim_regcomp(). First work out how long it will be:
4146 */
4147
4148 /* For help tags the translation is done in find_help_tags().
4149 * For a tag pattern starting with "/" no translation is needed. */
4150 if (context == EXPAND_HELP
4151 || context == EXPAND_COLORS
4152 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004153 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004154 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 || (context == EXPAND_TAGS && fname[0] == '/'))
4156 retval = vim_strnsave(fname, len);
4157 else
4158 {
4159 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4160 for (i = 0; i < len; i++)
4161 {
4162 if (fname[i] == '*' || fname[i] == '~')
4163 new_len++; /* '*' needs to be replaced by ".*"
4164 '~' needs to be replaced by "\~" */
4165
4166 /* Buffer names are like file names. "." should be literal */
4167 if (context == EXPAND_BUFFERS && fname[i] == '.')
4168 new_len++; /* "." becomes "\." */
4169
4170 /* Custom expansion takes care of special things, match
4171 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004172 if ((context == EXPAND_USER_DEFINED
4173 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 new_len++; /* '\' becomes "\\" */
4175 }
4176 retval = alloc(new_len);
4177 if (retval != NULL)
4178 {
4179 retval[0] = '^';
4180 j = 1;
4181 for (i = 0; i < len; i++, j++)
4182 {
4183 /* Skip backslash. But why? At least keep it for custom
4184 * expansion. */
4185 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004186 && context != EXPAND_USER_LIST
4187 && fname[i] == '\\'
4188 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 break;
4190
4191 switch (fname[i])
4192 {
4193 case '*': retval[j++] = '.';
4194 break;
4195 case '~': retval[j++] = '\\';
4196 break;
4197 case '?': retval[j] = '.';
4198 continue;
4199 case '.': if (context == EXPAND_BUFFERS)
4200 retval[j++] = '\\';
4201 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004202 case '\\': if (context == EXPAND_USER_DEFINED
4203 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 retval[j++] = '\\';
4205 break;
4206 }
4207 retval[j] = fname[i];
4208 }
4209 retval[j] = NUL;
4210 }
4211 }
4212 }
4213 else
4214 {
4215 retval = alloc(len + 4);
4216 if (retval != NULL)
4217 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004218 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
4220 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004221 * Don't add a star to *, ~, ~user, $var or `cmd`.
4222 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 * ~ would be at the start of the file name, but not the tail.
4224 * $ could be anywhere in the tail.
4225 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004226 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 */
4228 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004229 ends_in_star = (len > 0 && retval[len - 1] == '*');
4230#ifndef BACKSLASH_IN_FILENAME
4231 for (i = len - 2; i >= 0; --i)
4232 {
4233 if (retval[i] != '\\')
4234 break;
4235 ends_in_star = !ends_in_star;
4236 }
4237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004239 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 && vim_strchr(tail, '$') == NULL
4241 && vim_strchr(retval, '`') == NULL)
4242 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004243 else if (len > 0 && retval[len - 1] == '$')
4244 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 retval[len] = NUL;
4246 }
4247 }
4248 return retval;
4249}
4250
4251/*
4252 * Must parse the command line so far to work out what context we are in.
4253 * Completion can then be done based on that context.
4254 * This routine sets the variables:
4255 * xp->xp_pattern The start of the pattern to be expanded within
4256 * the command line (ends at the cursor).
4257 * xp->xp_context The type of thing to expand. Will be one of:
4258 *
4259 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4260 * the command line, like an unknown command. Caller
4261 * should beep.
4262 * EXPAND_NOTHING Unrecognised context for completion, use char like
4263 * a normal char, rather than for completion. eg
4264 * :s/^I/
4265 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4266 * it.
4267 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4268 * EXPAND_FILES After command with XFILE set, or after setting
4269 * with P_EXPAND set. eg :e ^I, :w>>^I
4270 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4271 * when we know only directories are of interest. eg
4272 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004273 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4275 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4276 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4277 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4278 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4279 * EXPAND_EVENTS Complete event names
4280 * EXPAND_SYNTAX Complete :syntax command arguments
4281 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4282 * EXPAND_AUGROUP Complete autocommand group names
4283 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4284 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4285 * eg :unmap a^I , :cunab x^I
4286 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4287 * eg :call sub^I
4288 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4289 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4290 * names in expressions, eg :while s^I
4291 * EXPAND_ENV_VARS Complete environment variable names
4292 */
4293 static void
4294set_expand_context(xp)
4295 expand_T *xp;
4296{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004297 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 if (ccline.cmdfirstc != ':'
4299#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004300 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004301 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302#endif
4303 )
4304 {
4305 xp->xp_context = EXPAND_NOTHING;
4306 return;
4307 }
4308 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4309}
4310
4311 void
4312set_cmd_context(xp, str, len, col)
4313 expand_T *xp;
4314 char_u *str; /* start of command line */
4315 int len; /* length of command line (excl. NUL) */
4316 int col; /* position of cursor */
4317{
4318 int old_char = NUL;
4319 char_u *nextcomm;
4320
4321 /*
4322 * Avoid a UMR warning from Purify, only save the character if it has been
4323 * written before.
4324 */
4325 if (col < len)
4326 old_char = str[col];
4327 str[col] = NUL;
4328 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004329
4330#ifdef FEAT_EVAL
4331 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004332 {
4333# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004334 /* pass CMD_SIZE because there is no real command */
4335 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004336# endif
4337 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004338 else if (ccline.input_fn)
4339 {
4340 xp->xp_context = ccline.xp_context;
4341 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004342# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004343 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004344# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004345 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004346 else
4347#endif
4348 while (nextcomm != NULL)
4349 nextcomm = set_one_cmd_context(xp, nextcomm);
4350
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 str[col] = old_char;
4352}
4353
4354/*
4355 * Expand the command line "str" from context "xp".
4356 * "xp" must have been set by set_cmd_context().
4357 * xp->xp_pattern points into "str", to where the text that is to be expanded
4358 * starts.
4359 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4360 * cursor.
4361 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4362 * key that triggered expansion literally.
4363 * Returns EXPAND_OK otherwise.
4364 */
4365 int
4366expand_cmdline(xp, str, col, matchcount, matches)
4367 expand_T *xp;
4368 char_u *str; /* start of command line */
4369 int col; /* position of cursor */
4370 int *matchcount; /* return: nr of matches */
4371 char_u ***matches; /* return: array of pointers to matches */
4372{
4373 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004374 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375
4376 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4377 {
4378 beep_flush();
4379 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4380 }
4381 if (xp->xp_context == EXPAND_NOTHING)
4382 {
4383 /* Caller can use the character as a normal char instead */
4384 return EXPAND_NOTHING;
4385 }
4386
4387 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004388 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4389 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 if (file_str == NULL)
4391 return EXPAND_UNSUCCESSFUL;
4392
Bram Moolenaar94950a92010-12-02 16:01:29 +01004393 if (p_wic)
4394 options += WILD_ICASE;
4395
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004397 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 {
4399 *matchcount = 0;
4400 *matches = NULL;
4401 }
4402 vim_free(file_str);
4403
4404 return EXPAND_OK;
4405}
4406
4407#ifdef FEAT_MULTI_LANG
4408/*
4409 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4410 */
4411static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4412
4413 static void
4414cleanup_help_tags(num_file, file)
4415 int num_file;
4416 char_u **file;
4417{
4418 int i, j;
4419 int len;
4420
4421 for (i = 0; i < num_file; ++i)
4422 {
4423 len = (int)STRLEN(file[i]) - 3;
4424 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4425 {
4426 /* Sorting on priority means the same item in another language may
4427 * be anywhere. Search all items for a match up to the "@en". */
4428 for (j = 0; j < num_file; ++j)
4429 if (j != i
4430 && (int)STRLEN(file[j]) == len + 3
4431 && STRNCMP(file[i], file[j], len + 1) == 0)
4432 break;
4433 if (j == num_file)
4434 file[i][len] = NUL;
4435 }
4436 }
4437}
4438#endif
4439
4440/*
4441 * Do the expansion based on xp->xp_context and "pat".
4442 */
4443 static int
4444ExpandFromContext(xp, pat, num_file, file, options)
4445 expand_T *xp;
4446 char_u *pat;
4447 int *num_file;
4448 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004449 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450{
4451#ifdef FEAT_CMDL_COMPL
4452 regmatch_T regmatch;
4453#endif
4454 int ret;
4455 int flags;
4456
4457 flags = EW_DIR; /* include directories */
4458 if (options & WILD_LIST_NOTFOUND)
4459 flags |= EW_NOTFOUND;
4460 if (options & WILD_ADD_SLASH)
4461 flags |= EW_ADDSLASH;
4462 if (options & WILD_KEEP_ALL)
4463 flags |= EW_KEEPALL;
4464 if (options & WILD_SILENT)
4465 flags |= EW_SILENT;
4466
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004467 if (xp->xp_context == EXPAND_FILES
4468 || xp->xp_context == EXPAND_DIRECTORIES
4469 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 {
4471 /*
4472 * Expand file or directory names.
4473 */
4474 int free_pat = FALSE;
4475 int i;
4476
4477 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4478 * space */
4479 if (xp->xp_backslash != XP_BS_NONE)
4480 {
4481 free_pat = TRUE;
4482 pat = vim_strsave(pat);
4483 for (i = 0; pat[i]; ++i)
4484 if (pat[i] == '\\')
4485 {
4486 if (xp->xp_backslash == XP_BS_THREE
4487 && pat[i + 1] == '\\'
4488 && pat[i + 2] == '\\'
4489 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004490 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 if (xp->xp_backslash == XP_BS_ONE
4492 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004493 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 }
4495 }
4496
4497 if (xp->xp_context == EXPAND_FILES)
4498 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004499 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4500 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 else
4502 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004503 if (options & WILD_ICASE)
4504 flags |= EW_ICASE;
4505
Bram Moolenaard7834d32009-12-02 16:14:36 +00004506 /* Expand wildcards, supporting %:h and the like. */
4507 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 if (free_pat)
4509 vim_free(pat);
4510 return ret;
4511 }
4512
4513 *file = (char_u **)"";
4514 *num_file = 0;
4515 if (xp->xp_context == EXPAND_HELP)
4516 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004517 /* With an empty argument we would get all the help tags, which is
4518 * very slow. Get matches for "help" instead. */
4519 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4520 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 {
4522#ifdef FEAT_MULTI_LANG
4523 cleanup_help_tags(*num_file, *file);
4524#endif
4525 return OK;
4526 }
4527 return FAIL;
4528 }
4529
4530#ifndef FEAT_CMDL_COMPL
4531 return FAIL;
4532#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004533 if (xp->xp_context == EXPAND_SHELLCMD)
4534 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (xp->xp_context == EXPAND_OLD_SETTING)
4536 return ExpandOldSetting(num_file, file);
4537 if (xp->xp_context == EXPAND_BUFFERS)
4538 return ExpandBufnames(pat, num_file, file, options);
4539 if (xp->xp_context == EXPAND_TAGS
4540 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4541 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4542 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004543 {
4544 char *directories[] = {"colors", NULL};
4545 return ExpandRTDir(pat, num_file, file, directories);
4546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004548 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004549 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004550 return ExpandRTDir(pat, num_file, file, directories);
4551 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004552 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004553 {
4554 char *directories[] = {"syntax", NULL};
4555 return ExpandRTDir(pat, num_file, file, directories);
4556 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004557 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004558 {
4559 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4560 return ExpandRTDir(pat, num_file, file, directories);
4561 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004562# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4563 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004564 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004565# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
4567 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4568 if (regmatch.regprog == NULL)
4569 return FAIL;
4570
4571 /* set ignore-case according to p_ic, p_scs and pat */
4572 regmatch.rm_ic = ignorecase(pat);
4573
4574 if (xp->xp_context == EXPAND_SETTINGS
4575 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4576 ret = ExpandSettings(xp, &regmatch, num_file, file);
4577 else if (xp->xp_context == EXPAND_MAPPINGS)
4578 ret = ExpandMappings(&regmatch, num_file, file);
4579# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4580 else if (xp->xp_context == EXPAND_USER_DEFINED)
4581 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4582# endif
4583 else
4584 {
4585 static struct expgen
4586 {
4587 int context;
4588 char_u *((*func)__ARGS((expand_T *, int)));
4589 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004590 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 } tab[] =
4592 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004593 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4594 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004596 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4597 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4598 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4599 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600#endif
4601#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004602 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4603 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4604 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4605 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606#endif
4607#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004608 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4609 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610#endif
4611#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004612 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004614 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004616 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4617 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004619#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004620 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004621#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004622#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004623 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004624#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004625#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004626 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4629 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004630 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4631 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004633 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 };
4635 int i;
4636
4637 /*
4638 * Find a context in the table and call the ExpandGeneric() with the
4639 * right function to do the expansion.
4640 */
4641 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004642 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if (xp->xp_context == tab[i].context)
4644 {
4645 if (tab[i].ic)
4646 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004647 ret = ExpandGeneric(xp, &regmatch, num_file, file,
4648 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 break;
4650 }
4651 }
4652
4653 vim_free(regmatch.regprog);
4654
4655 return ret;
4656#endif /* FEAT_CMDL_COMPL */
4657}
4658
4659#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4660/*
4661 * Expand a list of names.
4662 *
4663 * Generic function for command line completion. It calls a function to
4664 * obtain strings, one by one. The strings are matched against a regexp
4665 * program. Matching strings are copied into an array, which is returned.
4666 *
4667 * Returns OK when no problems encountered, FAIL for error (out of memory).
4668 */
4669 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004670ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 expand_T *xp;
4672 regmatch_T *regmatch;
4673 int *num_file;
4674 char_u ***file;
4675 char_u *((*func)__ARGS((expand_T *, int)));
4676 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004677 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678{
4679 int i;
4680 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004681 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 char_u *str;
4683
4684 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004685 * round == 0: count the number of matching names
4686 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004688 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 {
4690 for (i = 0; ; ++i)
4691 {
4692 str = (*func)(xp, i);
4693 if (str == NULL) /* end of list */
4694 break;
4695 if (*str == NUL) /* skip empty strings */
4696 continue;
4697
4698 if (vim_regexec(regmatch, str, (colnr_T)0))
4699 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004700 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004702 if (escaped)
4703 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4704 else
4705 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 (*file)[count] = str;
4707#ifdef FEAT_MENU
4708 if (func == get_menu_names && str != NULL)
4709 {
4710 /* test for separator added by get_menu_names() */
4711 str += STRLEN(str) - 1;
4712 if (*str == '\001')
4713 *str = '.';
4714 }
4715#endif
4716 }
4717 ++count;
4718 }
4719 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004720 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
4722 if (count == 0)
4723 return OK;
4724 *num_file = count;
4725 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4726 if (*file == NULL)
4727 {
4728 *file = (char_u **)"";
4729 return FAIL;
4730 }
4731 count = 0;
4732 }
4733 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004734
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004735 /* Sort the results. Keep menu's in the specified order. */
4736 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4737 sort_strings(*file, *num_file);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004738
Bram Moolenaar4f688582007-07-24 12:34:30 +00004739#ifdef FEAT_CMDL_COMPL
4740 /* Reset the variables used for special highlight names expansion, so that
4741 * they don't show up when getting normal highlight names by ID. */
4742 reset_expand_highlight();
4743#endif
4744
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 return OK;
4746}
4747
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004748/*
4749 * Complete a shell command.
4750 * Returns FAIL or OK;
4751 */
4752 static int
4753expand_shellcmd(filepat, num_file, file, flagsarg)
4754 char_u *filepat; /* pattern to match with command names */
4755 int *num_file; /* return: number of matches */
4756 char_u ***file; /* return: array with matches */
4757 int flagsarg; /* EW_ flags */
4758{
4759 char_u *pat;
4760 int i;
4761 char_u *path;
4762 int mustfree = FALSE;
4763 garray_T ga;
4764 char_u *buf = alloc(MAXPATHL);
4765 size_t l;
4766 char_u *s, *e;
4767 int flags = flagsarg;
4768 int ret;
4769
4770 if (buf == NULL)
4771 return FAIL;
4772
4773 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4774 * space */
4775 pat = vim_strsave(filepat);
4776 for (i = 0; pat[i]; ++i)
4777 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004778 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004779
4780 flags |= EW_FILE | EW_EXEC;
4781
4782 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004783 if (mch_isFullName(pat))
4784 path = (char_u *)" ";
4785 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004786 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4787 path = (char_u *)".";
4788 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004789 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004790 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004791 if (path == NULL)
4792 path = (char_u *)"";
4793 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004794
4795 /*
4796 * Go over all directories in $PATH. Expand matches in that directory and
4797 * collect them in "ga".
4798 */
4799 ga_init2(&ga, (int)sizeof(char *), 10);
4800 for (s = path; *s != NUL; s = e)
4801 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004802 if (*s == ' ')
4803 ++s; /* Skip space used for absolute path name. */
4804
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004805#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4806 e = vim_strchr(s, ';');
4807#else
4808 e = vim_strchr(s, ':');
4809#endif
4810 if (e == NULL)
4811 e = s + STRLEN(s);
4812
4813 l = e - s;
4814 if (l > MAXPATHL - 5)
4815 break;
4816 vim_strncpy(buf, s, l);
4817 add_pathsep(buf);
4818 l = STRLEN(buf);
4819 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4820
4821 /* Expand matches in one directory of $PATH. */
4822 ret = expand_wildcards(1, &buf, num_file, file, flags);
4823 if (ret == OK)
4824 {
4825 if (ga_grow(&ga, *num_file) == FAIL)
4826 FreeWild(*num_file, *file);
4827 else
4828 {
4829 for (i = 0; i < *num_file; ++i)
4830 {
4831 s = (*file)[i];
4832 if (STRLEN(s) > l)
4833 {
4834 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004835 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004836 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4837 }
4838 else
4839 vim_free(s);
4840 }
4841 vim_free(*file);
4842 }
4843 }
4844 if (*e != NUL)
4845 ++e;
4846 }
4847 *file = ga.ga_data;
4848 *num_file = ga.ga_len;
4849
4850 vim_free(buf);
4851 vim_free(pat);
4852 if (mustfree)
4853 vim_free(path);
4854 return OK;
4855}
4856
4857
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004859static 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));
4860
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004862 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004863 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004865 static void *
4866call_user_expand_func(user_expand_func, xp, num_file, file)
4867 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 int *num_file;
4870 char_u ***file;
4871{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004872 char_u keep;
4873 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004876 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004877 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
4879 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004880 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 *num_file = 0;
4882 *file = NULL;
4883
Bram Moolenaar21669c02008-01-18 12:16:16 +00004884 if (ccline.cmdbuff == NULL)
4885 {
4886 /* Completion from Insert mode, pass fake arguments. */
4887 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004888 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004889 args[1] = xp->xp_pattern;
4890 }
4891 else
4892 {
4893 /* Completion on the command line, pass real arguments. */
4894 keep = ccline.cmdbuff[ccline.cmdlen];
4895 ccline.cmdbuff[ccline.cmdlen] = 0;
4896 sprintf((char *)num, "%d", ccline.cmdpos);
4897 args[1] = ccline.cmdbuff;
4898 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004899 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 args[2] = num;
4901
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004902 /* Save the cmdline, we don't know what the function may do. */
4903 save_ccline = ccline;
4904 ccline.cmdbuff = NULL;
4905 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004907
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004908 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004909
4910 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004912 if (ccline.cmdbuff != NULL)
4913 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004914
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004915 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004916 return ret;
4917}
4918
4919/*
4920 * Expand names with a function defined by the user.
4921 */
4922 static int
4923ExpandUserDefined(xp, regmatch, num_file, file)
4924 expand_T *xp;
4925 regmatch_T *regmatch;
4926 int *num_file;
4927 char_u ***file;
4928{
4929 char_u *retstr;
4930 char_u *s;
4931 char_u *e;
4932 char_u keep;
4933 garray_T ga;
4934
4935 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4936 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 return FAIL;
4938
4939 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004940 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 {
4942 e = vim_strchr(s, '\n');
4943 if (e == NULL)
4944 e = s + STRLEN(s);
4945 keep = *e;
4946 *e = 0;
4947
4948 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4949 {
4950 *e = keep;
4951 if (*e != NUL)
4952 ++e;
4953 continue;
4954 }
4955
4956 if (ga_grow(&ga, 1) == FAIL)
4957 break;
4958
4959 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4960 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
4962 *e = keep;
4963 if (*e != NUL)
4964 ++e;
4965 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004966 vim_free(retstr);
4967 *file = ga.ga_data;
4968 *num_file = ga.ga_len;
4969 return OK;
4970}
4971
4972/*
4973 * Expand names with a list returned by a function defined by the user.
4974 */
4975 static int
4976ExpandUserList(xp, num_file, file)
4977 expand_T *xp;
4978 int *num_file;
4979 char_u ***file;
4980{
4981 list_T *retlist;
4982 listitem_T *li;
4983 garray_T ga;
4984
4985 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4986 if (retlist == NULL)
4987 return FAIL;
4988
4989 ga_init2(&ga, (int)sizeof(char *), 3);
4990 /* Loop over the items in the list. */
4991 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4992 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00004993 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
4994 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004995
4996 if (ga_grow(&ga, 1) == FAIL)
4997 break;
4998
4999 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005000 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005001 ++ga.ga_len;
5002 }
5003 list_unref(retlist);
5004
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 *file = ga.ga_data;
5006 *num_file = ga.ga_len;
5007 return OK;
5008}
5009#endif
5010
5011/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005012 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005013 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005014 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 */
5016 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005017ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 char_u *pat;
5019 int *num_file;
5020 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005021 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005023 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 char_u *s;
5025 char_u *e;
5026 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005027 int i;
5028 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029
5030 *num_file = 0;
5031 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005032 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005033 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005035 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005037 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5038 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005040 ga_clear_strings(&ga);
5041 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005043 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5044 matches = globpath(p_rtp, s, 0);
5045 vim_free(s);
5046 if (matches == NULL)
5047 continue;
5048
5049 for (s = matches; *s != NUL; s = e)
5050 {
5051 e = vim_strchr(s, '\n');
5052 if (e == NULL)
5053 e = s + STRLEN(s);
5054 if (ga_grow(&ga, 1) == FAIL)
5055 break;
5056 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5057 {
5058 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5059 if (*s == '\n' || vim_ispathsep(*s))
5060 break;
5061 ++s;
5062 ((char_u **)ga.ga_data)[ga.ga_len] =
5063 vim_strnsave(s, (int)(e - s - 4));
5064 ++ga.ga_len;
5065 }
5066 if (*e != NUL)
5067 ++e;
5068 }
5069 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005071 if (ga.ga_len == 0)
5072 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005073
5074 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005075 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005076 remove_duplicates(&ga);
5077
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 *file = ga.ga_data;
5079 *num_file = ga.ga_len;
5080 return OK;
5081}
5082
5083#endif
5084
5085#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5086/*
5087 * Expand "file" for all comma-separated directories in "path".
5088 * Returns an allocated string with all matches concatenated, separated by
5089 * newlines. Returns NULL for an error or no matches.
5090 */
5091 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005092globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 char_u *path;
5094 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005095 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096{
5097 expand_T xpc;
5098 char_u *buf;
5099 garray_T ga;
5100 int i;
5101 int len;
5102 int num_p;
5103 char_u **p;
5104 char_u *cur = NULL;
5105
5106 buf = alloc(MAXPATHL);
5107 if (buf == NULL)
5108 return NULL;
5109
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005110 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005112
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 ga_init2(&ga, 1, 100);
5114
5115 /* Loop over all entries in {path}. */
5116 while (*path != NUL)
5117 {
5118 /* Copy one item of the path to buf[] and concatenate the file name. */
5119 copy_option_part(&path, buf, MAXPATHL, ",");
5120 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5121 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005122# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005123 /* Using the platform's path separator (\) makes vim incorrectly
5124 * treat it as an escape character, use '/' instead. */
5125 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5126 STRCAT(buf, "/");
5127# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005131 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5132 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005134 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005136 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137
5138 /* Concatenate new results to previous ones. */
5139 if (ga_grow(&ga, len) == OK)
5140 {
5141 cur = (char_u *)ga.ga_data + ga.ga_len;
5142 for (i = 0; i < num_p; ++i)
5143 {
5144 STRCPY(cur, p[i]);
5145 cur += STRLEN(p[i]);
5146 *cur++ = '\n';
5147 }
5148 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150 FreeWild(num_p, p);
5151 }
5152 }
5153 }
5154 if (cur != NULL)
5155 *--cur = 0; /* Replace trailing newline with NUL */
5156
5157 vim_free(buf);
5158 return (char_u *)ga.ga_data;
5159}
5160
5161#endif
5162
5163#if defined(FEAT_CMDHIST) || defined(PROTO)
5164
5165/*********************************
5166 * Command line history stuff *
5167 *********************************/
5168
5169/*
5170 * Translate a history character to the associated type number.
5171 */
5172 static int
5173hist_char2type(c)
5174 int c;
5175{
5176 if (c == ':')
5177 return HIST_CMD;
5178 if (c == '=')
5179 return HIST_EXPR;
5180 if (c == '@')
5181 return HIST_INPUT;
5182 if (c == '>')
5183 return HIST_DEBUG;
5184 return HIST_SEARCH; /* must be '?' or '/' */
5185}
5186
5187/*
5188 * Table of history names.
5189 * These names are used in :history and various hist...() functions.
5190 * It is sufficient to give the significant prefix of a history name.
5191 */
5192
5193static char *(history_names[]) =
5194{
5195 "cmd",
5196 "search",
5197 "expr",
5198 "input",
5199 "debug",
5200 NULL
5201};
5202
5203/*
5204 * init_history() - Initialize the command line history.
5205 * Also used to re-allocate the history when the size changes.
5206 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005207 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208init_history()
5209{
5210 int newlen; /* new length of history table */
5211 histentry_T *temp;
5212 int i;
5213 int j;
5214 int type;
5215
5216 /*
5217 * If size of history table changed, reallocate it
5218 */
5219 newlen = (int)p_hi;
5220 if (newlen != hislen) /* history length changed */
5221 {
5222 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5223 {
5224 if (newlen)
5225 {
5226 temp = (histentry_T *)lalloc(
5227 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5228 if (temp == NULL) /* out of memory! */
5229 {
5230 if (type == 0) /* first one: just keep the old length */
5231 {
5232 newlen = hislen;
5233 break;
5234 }
5235 /* Already changed one table, now we can only have zero
5236 * length for all tables. */
5237 newlen = 0;
5238 type = -1;
5239 continue;
5240 }
5241 }
5242 else
5243 temp = NULL;
5244 if (newlen == 0 || temp != NULL)
5245 {
5246 if (hisidx[type] < 0) /* there are no entries yet */
5247 {
5248 for (i = 0; i < newlen; ++i)
5249 {
5250 temp[i].hisnum = 0;
5251 temp[i].hisstr = NULL;
5252 }
5253 }
5254 else if (newlen > hislen) /* array becomes bigger */
5255 {
5256 for (i = 0; i <= hisidx[type]; ++i)
5257 temp[i] = history[type][i];
5258 j = i;
5259 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5260 {
5261 temp[i].hisnum = 0;
5262 temp[i].hisstr = NULL;
5263 }
5264 for ( ; j < hislen; ++i, ++j)
5265 temp[i] = history[type][j];
5266 }
5267 else /* array becomes smaller or 0 */
5268 {
5269 j = hisidx[type];
5270 for (i = newlen - 1; ; --i)
5271 {
5272 if (i >= 0) /* copy newest entries */
5273 temp[i] = history[type][j];
5274 else /* remove older entries */
5275 vim_free(history[type][j].hisstr);
5276 if (--j < 0)
5277 j = hislen - 1;
5278 if (j == hisidx[type])
5279 break;
5280 }
5281 hisidx[type] = newlen - 1;
5282 }
5283 vim_free(history[type]);
5284 history[type] = temp;
5285 }
5286 }
5287 hislen = newlen;
5288 }
5289}
5290
5291/*
5292 * Check if command line 'str' is already in history.
5293 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5294 */
5295 static int
Bram Moolenaar4c402232011-07-27 17:58:46 +02005296in_history(type, str, move_to_front, sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 int type;
5298 char_u *str;
5299 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005300 int sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301{
5302 int i;
5303 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005304 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
5306 if (hisidx[type] < 0)
5307 return FALSE;
5308 i = hisidx[type];
5309 do
5310 {
5311 if (history[type][i].hisstr == NULL)
5312 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005313
5314 /* For search history, check that the separator character matches as
5315 * well. */
5316 p = history[type][i].hisstr;
5317 if (STRCMP(str, p) == 0
5318 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
5320 if (!move_to_front)
5321 return TRUE;
5322 last_i = i;
5323 break;
5324 }
5325 if (--i < 0)
5326 i = hislen - 1;
5327 } while (i != hisidx[type]);
5328
5329 if (last_i >= 0)
5330 {
5331 str = history[type][i].hisstr;
5332 while (i != hisidx[type])
5333 {
5334 if (++i >= hislen)
5335 i = 0;
5336 history[type][last_i] = history[type][i];
5337 last_i = i;
5338 }
5339 history[type][i].hisstr = str;
5340 history[type][i].hisnum = ++hisnum[type];
5341 return TRUE;
5342 }
5343 return FALSE;
5344}
5345
5346/*
5347 * Convert history name (from table above) to its HIST_ equivalent.
5348 * When "name" is empty, return "cmd" history.
5349 * Returns -1 for unknown history name.
5350 */
5351 int
5352get_histtype(name)
5353 char_u *name;
5354{
5355 int i;
5356 int len = (int)STRLEN(name);
5357
5358 /* No argument: use current history. */
5359 if (len == 0)
5360 return hist_char2type(ccline.cmdfirstc);
5361
5362 for (i = 0; history_names[i] != NULL; ++i)
5363 if (STRNICMP(name, history_names[i], len) == 0)
5364 return i;
5365
5366 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5367 return hist_char2type(name[0]);
5368
5369 return -1;
5370}
5371
5372static int last_maptick = -1; /* last seen maptick */
5373
5374/*
5375 * Add the given string to the given history. If the string is already in the
5376 * history then it is moved to the front. "histype" may be one of he HIST_
5377 * values.
5378 */
5379 void
5380add_to_history(histype, new_entry, in_map, sep)
5381 int histype;
5382 char_u *new_entry;
5383 int in_map; /* consider maptick when inside a mapping */
5384 int sep; /* separator character used (search hist) */
5385{
5386 histentry_T *hisptr;
5387 int len;
5388
5389 if (hislen == 0) /* no history */
5390 return;
5391
5392 /*
5393 * Searches inside the same mapping overwrite each other, so that only
5394 * the last line is kept. Be careful not to remove a line that was moved
5395 * down, only lines that were added.
5396 */
5397 if (histype == HIST_SEARCH && in_map)
5398 {
5399 if (maptick == last_maptick)
5400 {
5401 /* Current line is from the same mapping, remove it */
5402 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5403 vim_free(hisptr->hisstr);
5404 hisptr->hisstr = NULL;
5405 hisptr->hisnum = 0;
5406 --hisnum[histype];
5407 if (--hisidx[HIST_SEARCH] < 0)
5408 hisidx[HIST_SEARCH] = hislen - 1;
5409 }
5410 last_maptick = -1;
5411 }
Bram Moolenaar4c402232011-07-27 17:58:46 +02005412 if (!in_history(histype, new_entry, TRUE, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 {
5414 if (++hisidx[histype] == hislen)
5415 hisidx[histype] = 0;
5416 hisptr = &history[histype][hisidx[histype]];
5417 vim_free(hisptr->hisstr);
5418
5419 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005420 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5422 if (hisptr->hisstr != NULL)
5423 hisptr->hisstr[len + 1] = sep;
5424
5425 hisptr->hisnum = ++hisnum[histype];
5426 if (histype == HIST_SEARCH && in_map)
5427 last_maptick = maptick;
5428 }
5429}
5430
5431#if defined(FEAT_EVAL) || defined(PROTO)
5432
5433/*
5434 * Get identifier of newest history entry.
5435 * "histype" may be one of the HIST_ values.
5436 */
5437 int
5438get_history_idx(histype)
5439 int histype;
5440{
5441 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5442 || hisidx[histype] < 0)
5443 return -1;
5444
5445 return history[histype][hisidx[histype]].hisnum;
5446}
5447
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005448static struct cmdline_info *get_ccline_ptr __ARGS((void));
5449
5450/*
5451 * Get pointer to the command line info to use. cmdline_paste() may clear
5452 * ccline and put the previous value in prev_ccline.
5453 */
5454 static struct cmdline_info *
5455get_ccline_ptr()
5456{
5457 if ((State & CMDLINE) == 0)
5458 return NULL;
5459 if (ccline.cmdbuff != NULL)
5460 return &ccline;
5461 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5462 return &prev_ccline;
5463 return NULL;
5464}
5465
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466/*
5467 * Get the current command line in allocated memory.
5468 * Only works when the command line is being edited.
5469 * Returns NULL when something is wrong.
5470 */
5471 char_u *
5472get_cmdline_str()
5473{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005474 struct cmdline_info *p = get_ccline_ptr();
5475
5476 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005478 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479}
5480
5481/*
5482 * Get the current command line position, counted in bytes.
5483 * Zero is the first position.
5484 * Only works when the command line is being edited.
5485 * Returns -1 when something is wrong.
5486 */
5487 int
5488get_cmdline_pos()
5489{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005490 struct cmdline_info *p = get_ccline_ptr();
5491
5492 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005494 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495}
5496
5497/*
5498 * Set the command line byte position to "pos". Zero is the first position.
5499 * Only works when the command line is being edited.
5500 * Returns 1 when failed, 0 when OK.
5501 */
5502 int
5503set_cmdline_pos(pos)
5504 int pos;
5505{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005506 struct cmdline_info *p = get_ccline_ptr();
5507
5508 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 return 1;
5510
5511 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5512 * changed the command line. */
5513 if (pos < 0)
5514 new_cmdpos = 0;
5515 else
5516 new_cmdpos = pos;
5517 return 0;
5518}
5519
5520/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005521 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005522 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005523 * Only works when the command line is being edited.
5524 * Returns NUL when something is wrong.
5525 */
5526 int
5527get_cmdline_type()
5528{
5529 struct cmdline_info *p = get_ccline_ptr();
5530
5531 if (p == NULL)
5532 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005533 if (p->cmdfirstc == NUL)
5534 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005535 return p->cmdfirstc;
5536}
5537
5538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 * Calculate history index from a number:
5540 * num > 0: seen as identifying number of a history entry
5541 * num < 0: relative position in history wrt newest entry
5542 * "histype" may be one of the HIST_ values.
5543 */
5544 static int
5545calc_hist_idx(histype, num)
5546 int histype;
5547 int num;
5548{
5549 int i;
5550 histentry_T *hist;
5551 int wrapped = FALSE;
5552
5553 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5554 || (i = hisidx[histype]) < 0 || num == 0)
5555 return -1;
5556
5557 hist = history[histype];
5558 if (num > 0)
5559 {
5560 while (hist[i].hisnum > num)
5561 if (--i < 0)
5562 {
5563 if (wrapped)
5564 break;
5565 i += hislen;
5566 wrapped = TRUE;
5567 }
5568 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5569 return i;
5570 }
5571 else if (-num <= hislen)
5572 {
5573 i += num + 1;
5574 if (i < 0)
5575 i += hislen;
5576 if (hist[i].hisstr != NULL)
5577 return i;
5578 }
5579 return -1;
5580}
5581
5582/*
5583 * Get a history entry by its index.
5584 * "histype" may be one of the HIST_ values.
5585 */
5586 char_u *
5587get_history_entry(histype, idx)
5588 int histype;
5589 int idx;
5590{
5591 idx = calc_hist_idx(histype, idx);
5592 if (idx >= 0)
5593 return history[histype][idx].hisstr;
5594 else
5595 return (char_u *)"";
5596}
5597
5598/*
5599 * Clear all entries of a history.
5600 * "histype" may be one of the HIST_ values.
5601 */
5602 int
5603clr_history(histype)
5604 int histype;
5605{
5606 int i;
5607 histentry_T *hisptr;
5608
5609 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5610 {
5611 hisptr = history[histype];
5612 for (i = hislen; i--;)
5613 {
5614 vim_free(hisptr->hisstr);
5615 hisptr->hisnum = 0;
5616 hisptr++->hisstr = NULL;
5617 }
5618 hisidx[histype] = -1; /* mark history as cleared */
5619 hisnum[histype] = 0; /* reset identifier counter */
5620 return OK;
5621 }
5622 return FAIL;
5623}
5624
5625/*
5626 * Remove all entries matching {str} from a history.
5627 * "histype" may be one of the HIST_ values.
5628 */
5629 int
5630del_history_entry(histype, str)
5631 int histype;
5632 char_u *str;
5633{
5634 regmatch_T regmatch;
5635 histentry_T *hisptr;
5636 int idx;
5637 int i;
5638 int last;
5639 int found = FALSE;
5640
5641 regmatch.regprog = NULL;
5642 regmatch.rm_ic = FALSE; /* always match case */
5643 if (hislen != 0
5644 && histype >= 0
5645 && histype < HIST_COUNT
5646 && *str != NUL
5647 && (idx = hisidx[histype]) >= 0
5648 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5649 != NULL)
5650 {
5651 i = last = idx;
5652 do
5653 {
5654 hisptr = &history[histype][i];
5655 if (hisptr->hisstr == NULL)
5656 break;
5657 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5658 {
5659 found = TRUE;
5660 vim_free(hisptr->hisstr);
5661 hisptr->hisstr = NULL;
5662 hisptr->hisnum = 0;
5663 }
5664 else
5665 {
5666 if (i != last)
5667 {
5668 history[histype][last] = *hisptr;
5669 hisptr->hisstr = NULL;
5670 hisptr->hisnum = 0;
5671 }
5672 if (--last < 0)
5673 last += hislen;
5674 }
5675 if (--i < 0)
5676 i += hislen;
5677 } while (i != idx);
5678 if (history[histype][idx].hisstr == NULL)
5679 hisidx[histype] = -1;
5680 }
5681 vim_free(regmatch.regprog);
5682 return found;
5683}
5684
5685/*
5686 * Remove an indexed entry from a history.
5687 * "histype" may be one of the HIST_ values.
5688 */
5689 int
5690del_history_idx(histype, idx)
5691 int histype;
5692 int idx;
5693{
5694 int i, j;
5695
5696 i = calc_hist_idx(histype, idx);
5697 if (i < 0)
5698 return FALSE;
5699 idx = hisidx[histype];
5700 vim_free(history[histype][i].hisstr);
5701
5702 /* When deleting the last added search string in a mapping, reset
5703 * last_maptick, so that the last added search string isn't deleted again.
5704 */
5705 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5706 last_maptick = -1;
5707
5708 while (i != idx)
5709 {
5710 j = (i + 1) % hislen;
5711 history[histype][i] = history[histype][j];
5712 i = j;
5713 }
5714 history[histype][i].hisstr = NULL;
5715 history[histype][i].hisnum = 0;
5716 if (--i < 0)
5717 i += hislen;
5718 hisidx[histype] = i;
5719 return TRUE;
5720}
5721
5722#endif /* FEAT_EVAL */
5723
5724#if defined(FEAT_CRYPT) || defined(PROTO)
5725/*
5726 * Very specific function to remove the value in ":set key=val" from the
5727 * history.
5728 */
5729 void
5730remove_key_from_history()
5731{
5732 char_u *p;
5733 int i;
5734
5735 i = hisidx[HIST_CMD];
5736 if (i < 0)
5737 return;
5738 p = history[HIST_CMD][i].hisstr;
5739 if (p != NULL)
5740 for ( ; *p; ++p)
5741 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5742 {
5743 p = vim_strchr(p + 3, '=');
5744 if (p == NULL)
5745 break;
5746 ++p;
5747 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5748 if (p[i] == '\\' && p[i + 1])
5749 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005750 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 --p;
5752 }
5753}
5754#endif
5755
5756#endif /* FEAT_CMDHIST */
5757
5758#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5759/*
5760 * Get indices "num1,num2" that specify a range within a list (not a range of
5761 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5762 * Returns OK if parsed successfully, otherwise FAIL.
5763 */
5764 int
5765get_list_range(str, num1, num2)
5766 char_u **str;
5767 int *num1;
5768 int *num2;
5769{
5770 int len;
5771 int first = FALSE;
5772 long num;
5773
5774 *str = skipwhite(*str);
5775 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5776 {
5777 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5778 *str += len;
5779 *num1 = (int)num;
5780 first = TRUE;
5781 }
5782 *str = skipwhite(*str);
5783 if (**str == ',') /* parse "to" part of range */
5784 {
5785 *str = skipwhite(*str + 1);
5786 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5787 if (len > 0)
5788 {
5789 *num2 = (int)num;
5790 *str = skipwhite(*str + len);
5791 }
5792 else if (!first) /* no number given at all */
5793 return FAIL;
5794 }
5795 else if (first) /* only one number given */
5796 *num2 = *num1;
5797 return OK;
5798}
5799#endif
5800
5801#if defined(FEAT_CMDHIST) || defined(PROTO)
5802/*
5803 * :history command - print a history
5804 */
5805 void
5806ex_history(eap)
5807 exarg_T *eap;
5808{
5809 histentry_T *hist;
5810 int histype1 = HIST_CMD;
5811 int histype2 = HIST_CMD;
5812 int hisidx1 = 1;
5813 int hisidx2 = -1;
5814 int idx;
5815 int i, j, k;
5816 char_u *end;
5817 char_u *arg = eap->arg;
5818
5819 if (hislen == 0)
5820 {
5821 MSG(_("'history' option is zero"));
5822 return;
5823 }
5824
5825 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5826 {
5827 end = arg;
5828 while (ASCII_ISALPHA(*end)
5829 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5830 end++;
5831 i = *end;
5832 *end = NUL;
5833 histype1 = get_histtype(arg);
5834 if (histype1 == -1)
5835 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005836 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 {
5838 histype1 = 0;
5839 histype2 = HIST_COUNT-1;
5840 }
5841 else
5842 {
5843 *end = i;
5844 EMSG(_(e_trailing));
5845 return;
5846 }
5847 }
5848 else
5849 histype2 = histype1;
5850 *end = i;
5851 }
5852 else
5853 end = arg;
5854 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5855 {
5856 EMSG(_(e_trailing));
5857 return;
5858 }
5859
5860 for (; !got_int && histype1 <= histype2; ++histype1)
5861 {
5862 STRCPY(IObuff, "\n # ");
5863 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5864 MSG_PUTS_TITLE(IObuff);
5865 idx = hisidx[histype1];
5866 hist = history[histype1];
5867 j = hisidx1;
5868 k = hisidx2;
5869 if (j < 0)
5870 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5871 if (k < 0)
5872 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5873 if (idx >= 0 && j <= k)
5874 for (i = idx + 1; !got_int; ++i)
5875 {
5876 if (i == hislen)
5877 i = 0;
5878 if (hist[i].hisstr != NULL
5879 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5880 {
5881 msg_putchar('\n');
5882 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5883 hist[i].hisnum);
5884 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5885 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5886 (int)Columns - 10);
5887 else
5888 STRCAT(IObuff, hist[i].hisstr);
5889 msg_outtrans(IObuff);
5890 out_flush();
5891 }
5892 if (i == idx)
5893 break;
5894 }
5895 }
5896}
5897#endif
5898
5899#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5900static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5901static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5902static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5903static int viminfo_add_at_front = FALSE;
5904
5905static int hist_type2char __ARGS((int type, int use_question));
5906
5907/*
5908 * Translate a history type number to the associated character.
5909 */
5910 static int
5911hist_type2char(type, use_question)
5912 int type;
5913 int use_question; /* use '?' instead of '/' */
5914{
5915 if (type == HIST_CMD)
5916 return ':';
5917 if (type == HIST_SEARCH)
5918 {
5919 if (use_question)
5920 return '?';
5921 else
5922 return '/';
5923 }
5924 if (type == HIST_EXPR)
5925 return '=';
5926 return '@';
5927}
5928
5929/*
5930 * Prepare for reading the history from the viminfo file.
5931 * This allocates history arrays to store the read history lines.
5932 */
5933 void
5934prepare_viminfo_history(asklen)
5935 int asklen;
5936{
5937 int i;
5938 int num;
5939 int type;
5940 int len;
5941
5942 init_history();
5943 viminfo_add_at_front = (asklen != 0);
5944 if (asklen > hislen)
5945 asklen = hislen;
5946
5947 for (type = 0; type < HIST_COUNT; ++type)
5948 {
5949 /*
5950 * Count the number of empty spaces in the history list. If there are
5951 * more spaces available than we request, then fill them up.
5952 */
5953 for (i = 0, num = 0; i < hislen; i++)
5954 if (history[type][i].hisstr == NULL)
5955 num++;
5956 len = asklen;
5957 if (num > len)
5958 len = num;
5959 if (len <= 0)
5960 viminfo_history[type] = NULL;
5961 else
5962 viminfo_history[type] =
5963 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5964 if (viminfo_history[type] == NULL)
5965 len = 0;
5966 viminfo_hislen[type] = len;
5967 viminfo_hisidx[type] = 0;
5968 }
5969}
5970
5971/*
5972 * Accept a line from the viminfo, store it in the history array when it's
5973 * new.
5974 */
5975 int
5976read_viminfo_history(virp)
5977 vir_T *virp;
5978{
5979 int type;
5980 long_u len;
5981 char_u *val;
5982 char_u *p;
5983
5984 type = hist_char2type(virp->vir_line[0]);
5985 if (viminfo_hisidx[type] < viminfo_hislen[type])
5986 {
5987 val = viminfo_readstring(virp, 1, TRUE);
5988 if (val != NULL && *val != NUL)
5989 {
5990 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar4c402232011-07-27 17:58:46 +02005991 viminfo_add_at_front, *val))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 {
5993 /* Need to re-allocate to append the separator byte. */
5994 len = STRLEN(val);
5995 p = lalloc(len + 2, TRUE);
5996 if (p != NULL)
5997 {
5998 if (type == HIST_SEARCH)
5999 {
6000 /* Search entry: Move the separator from the first
6001 * column to after the NUL. */
6002 mch_memmove(p, val + 1, (size_t)len);
6003 p[len] = (*val == ' ' ? NUL : *val);
6004 }
6005 else
6006 {
6007 /* Not a search entry: No separator in the viminfo
6008 * file, add a NUL separator. */
6009 mch_memmove(p, val, (size_t)len + 1);
6010 p[len + 1] = NUL;
6011 }
6012 viminfo_history[type][viminfo_hisidx[type]++] = p;
6013 }
6014 }
6015 }
6016 vim_free(val);
6017 }
6018 return viminfo_readline(virp);
6019}
6020
6021 void
6022finish_viminfo_history()
6023{
6024 int idx;
6025 int i;
6026 int type;
6027
6028 for (type = 0; type < HIST_COUNT; ++type)
6029 {
6030 if (history[type] == NULL)
6031 return;
6032 idx = hisidx[type] + viminfo_hisidx[type];
6033 if (idx >= hislen)
6034 idx -= hislen;
6035 else if (idx < 0)
6036 idx = hislen - 1;
6037 if (viminfo_add_at_front)
6038 hisidx[type] = idx;
6039 else
6040 {
6041 if (hisidx[type] == -1)
6042 hisidx[type] = hislen - 1;
6043 do
6044 {
6045 if (history[type][idx].hisstr != NULL)
6046 break;
6047 if (++idx == hislen)
6048 idx = 0;
6049 } while (idx != hisidx[type]);
6050 if (idx != hisidx[type] && --idx < 0)
6051 idx = hislen - 1;
6052 }
6053 for (i = 0; i < viminfo_hisidx[type]; i++)
6054 {
6055 vim_free(history[type][idx].hisstr);
6056 history[type][idx].hisstr = viminfo_history[type][i];
6057 if (--idx < 0)
6058 idx = hislen - 1;
6059 }
6060 idx += 1;
6061 idx %= hislen;
6062 for (i = 0; i < viminfo_hisidx[type]; i++)
6063 {
6064 history[type][idx++].hisnum = ++hisnum[type];
6065 idx %= hislen;
6066 }
6067 vim_free(viminfo_history[type]);
6068 viminfo_history[type] = NULL;
6069 }
6070}
6071
6072 void
6073write_viminfo_history(fp)
6074 FILE *fp;
6075{
6076 int i;
6077 int type;
6078 int num_saved;
6079 char_u *p;
6080 int c;
6081
6082 init_history();
6083 if (hislen == 0)
6084 return;
6085 for (type = 0; type < HIST_COUNT; ++type)
6086 {
6087 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6088 if (num_saved == 0)
6089 continue;
6090 if (num_saved < 0) /* Use default */
6091 num_saved = hislen;
6092 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6093 type == HIST_CMD ? _("Command Line") :
6094 type == HIST_SEARCH ? _("Search String") :
6095 type == HIST_EXPR ? _("Expression") :
6096 _("Input Line"));
6097 if (num_saved > hislen)
6098 num_saved = hislen;
6099 i = hisidx[type];
6100 if (i >= 0)
6101 while (num_saved--)
6102 {
6103 p = history[type][i].hisstr;
6104 if (p != NULL)
6105 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006106 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 /* For the search history: put the separator in the second
6108 * column; use a space if there isn't one. */
6109 if (type == HIST_SEARCH)
6110 {
6111 c = p[STRLEN(p) + 1];
6112 putc(c == NUL ? ' ' : c, fp);
6113 }
6114 viminfo_writestring(fp, p);
6115 }
6116 if (--i < 0)
6117 i = hislen - 1;
6118 }
6119 }
6120}
6121#endif /* FEAT_VIMINFO */
6122
6123#if defined(FEAT_FKMAP) || defined(PROTO)
6124/*
6125 * Write a character at the current cursor+offset position.
6126 * It is directly written into the command buffer block.
6127 */
6128 void
6129cmd_pchar(c, offset)
6130 int c, offset;
6131{
6132 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6133 {
6134 EMSG(_("E198: cmd_pchar beyond the command length"));
6135 return;
6136 }
6137 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6138 ccline.cmdbuff[ccline.cmdlen] = NUL;
6139}
6140
6141 int
6142cmd_gchar(offset)
6143 int offset;
6144{
6145 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6146 {
6147 /* EMSG(_("cmd_gchar beyond the command length")); */
6148 return NUL;
6149 }
6150 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6151}
6152#endif
6153
6154#if defined(FEAT_CMDWIN) || defined(PROTO)
6155/*
6156 * Open a window on the current command line and history. Allow editing in
6157 * the window. Returns when the window is closed.
6158 * Returns:
6159 * CR if the command is to be executed
6160 * Ctrl_C if it is to be abandoned
6161 * K_IGNORE if editing continues
6162 */
6163 static int
6164ex_window()
6165{
6166 struct cmdline_info save_ccline;
6167 buf_T *old_curbuf = curbuf;
6168 win_T *old_curwin = curwin;
6169 buf_T *bp;
6170 win_T *wp;
6171 int i;
6172 linenr_T lnum;
6173 int histtype;
6174 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006175#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 int save_restart_edit = restart_edit;
6179 int save_State = State;
6180 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006181#ifdef FEAT_RIGHTLEFT
6182 int save_cmdmsg_rl = cmdmsg_rl;
6183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184
6185 /* Can't do this recursively. Can't do it when typing a password. */
6186 if (cmdwin_type != 0
6187# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6188 || cmdline_star > 0
6189# endif
6190 )
6191 {
6192 beep_flush();
6193 return K_IGNORE;
6194 }
6195
6196 /* Save current window sizes. */
6197 win_size_save(&winsizes);
6198
6199# ifdef FEAT_AUTOCMD
6200 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006201 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006203 /* don't use a new tab page */
6204 cmdmod.tab = 0;
6205
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 /* Create a window for the command-line buffer. */
6207 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6208 {
6209 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006210# ifdef FEAT_AUTOCMD
6211 unblock_autocmds();
6212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 return K_IGNORE;
6214 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006215 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216
6217 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006218 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006219 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6221 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6222 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006223#ifdef FEAT_FOLDING
6224 curwin->w_p_fen = FALSE;
6225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006227 curwin->w_p_rl = cmdmsg_rl;
6228 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006230 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
6232# ifdef FEAT_AUTOCMD
6233 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006234 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235# endif
6236
Bram Moolenaar46152342005-09-07 21:18:43 +00006237 /* Showing the prompt may have set need_wait_return, reset it. */
6238 need_wait_return = FALSE;
6239
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006240 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6242 {
6243 if (p_wc == TAB)
6244 {
6245 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6246 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6247 }
6248 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6249 }
6250
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006251 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6252 * sets 'textwidth' to 78). */
6253 curbuf->b_p_tw = 0;
6254
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 /* Fill the buffer with the history. */
6256 init_history();
6257 if (hislen > 0)
6258 {
6259 i = hisidx[histtype];
6260 if (i >= 0)
6261 {
6262 lnum = 0;
6263 do
6264 {
6265 if (++i == hislen)
6266 i = 0;
6267 if (history[histtype][i].hisstr != NULL)
6268 ml_append(lnum++, history[histtype][i].hisstr,
6269 (colnr_T)0, FALSE);
6270 }
6271 while (i != hisidx[histtype]);
6272 }
6273 }
6274
6275 /* Replace the empty last line with the current command-line and put the
6276 * cursor there. */
6277 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6278 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6279 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006280 changed_line_abv_curs();
6281 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006282 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283
6284 /* Save the command line info, can be used recursively. */
6285 save_ccline = ccline;
6286 ccline.cmdbuff = NULL;
6287 ccline.cmdprompt = NULL;
6288
6289 /* No Ex mode here! */
6290 exmode_active = 0;
6291
6292 State = NORMAL;
6293# ifdef FEAT_MOUSE
6294 setmouse();
6295# endif
6296
6297# ifdef FEAT_AUTOCMD
6298 /* Trigger CmdwinEnter autocommands. */
6299 typestr[0] = cmdwin_type;
6300 typestr[1] = NUL;
6301 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006302 if (restart_edit != 0) /* autocmd with ":startinsert" */
6303 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304# endif
6305
6306 i = RedrawingDisabled;
6307 RedrawingDisabled = 0;
6308
6309 /*
6310 * Call the main loop until <CR> or CTRL-C is typed.
6311 */
6312 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006313 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
6315 RedrawingDisabled = i;
6316
6317# ifdef FEAT_AUTOCMD
6318 /* Trigger CmdwinLeave autocommands. */
6319 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6320# endif
6321
Bram Moolenaarccc18222007-05-10 18:25:20 +00006322 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 ccline = save_ccline;
6324 cmdwin_type = 0;
6325
6326 exmode_active = save_exmode;
6327
Bram Moolenaarf9821062008-06-20 16:31:07 +00006328 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 * this happens! */
6330 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6331 {
6332 cmdwin_result = Ctrl_C;
6333 EMSG(_("E199: Active window or buffer deleted"));
6334 }
6335 else
6336 {
6337# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6338 /* autocmds may abort script processing */
6339 if (aborting() && cmdwin_result != K_IGNORE)
6340 cmdwin_result = Ctrl_C;
6341# endif
6342 /* Set the new command line from the cmdline buffer. */
6343 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006344 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006346 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6347
6348 if (histtype == HIST_CMD)
6349 {
6350 /* Execute the command directly. */
6351 ccline.cmdbuff = vim_strsave((char_u *)p);
6352 cmdwin_result = CAR;
6353 }
6354 else
6355 {
6356 /* First need to cancel what we were doing. */
6357 ccline.cmdbuff = NULL;
6358 stuffcharReadbuff(':');
6359 stuffReadbuff((char_u *)p);
6360 stuffcharReadbuff(CAR);
6361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 }
6363 else if (cmdwin_result == K_XF2) /* :qa typed */
6364 {
6365 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6366 cmdwin_result = CAR;
6367 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006368 else if (cmdwin_result == Ctrl_C)
6369 {
6370 /* :q or :close, don't execute any command
6371 * and don't modify the cmd window. */
6372 ccline.cmdbuff = NULL;
6373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 else
6375 ccline.cmdbuff = vim_strsave(ml_get_curline());
6376 if (ccline.cmdbuff == NULL)
6377 cmdwin_result = Ctrl_C;
6378 else
6379 {
6380 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6381 ccline.cmdbufflen = ccline.cmdlen + 1;
6382 ccline.cmdpos = curwin->w_cursor.col;
6383 if (ccline.cmdpos > ccline.cmdlen)
6384 ccline.cmdpos = ccline.cmdlen;
6385 if (cmdwin_result == K_IGNORE)
6386 {
6387 set_cmdspos_cursor();
6388 redrawcmd();
6389 }
6390 }
6391
6392# ifdef FEAT_AUTOCMD
6393 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006394 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395# endif
6396 wp = curwin;
6397 bp = curbuf;
6398 win_goto(old_curwin);
6399 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006400
6401 /* win_close() may have already wiped the buffer when 'bh' is
6402 * set to 'wipe' */
6403 if (buf_valid(bp))
6404 close_buffer(NULL, bp, DOBUF_WIPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405
6406 /* Restore window sizes. */
6407 win_size_restore(&winsizes);
6408
6409# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006410 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411# endif
6412 }
6413
6414 ga_clear(&winsizes);
6415 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006416# ifdef FEAT_RIGHTLEFT
6417 cmdmsg_rl = save_cmdmsg_rl;
6418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
6420 State = save_State;
6421# ifdef FEAT_MOUSE
6422 setmouse();
6423# endif
6424
6425 return cmdwin_result;
6426}
6427#endif /* FEAT_CMDWIN */
6428
6429/*
6430 * Used for commands that either take a simple command string argument, or:
6431 * cmd << endmarker
6432 * {script}
6433 * endmarker
6434 * Returns a pointer to allocated memory with {script} or NULL.
6435 */
6436 char_u *
6437script_get(eap, cmd)
6438 exarg_T *eap;
6439 char_u *cmd;
6440{
6441 char_u *theline;
6442 char *end_pattern = NULL;
6443 char dot[] = ".";
6444 garray_T ga;
6445
6446 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6447 return NULL;
6448
6449 ga_init2(&ga, 1, 0x400);
6450
6451 if (cmd[2] != NUL)
6452 end_pattern = (char *)skipwhite(cmd + 2);
6453 else
6454 end_pattern = dot;
6455
6456 for (;;)
6457 {
6458 theline = eap->getline(
6459#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006460 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461#endif
6462 NUL, eap->cookie, 0);
6463
6464 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006465 {
6466 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469
6470 ga_concat(&ga, theline);
6471 ga_append(&ga, '\n');
6472 vim_free(theline);
6473 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006474 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475
6476 return (char_u *)ga.ga_data;
6477}