blob: 1f5c873dac8e7a0da5067e1784c78329146cea0f [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
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200124#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
125static int
126#ifdef __BORLANDC__
127_RTLENTRYF
128#endif
129sort_func_compare __ARGS((const void *s1, const void *s2));
130#endif
131
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132/*
133 * getcmdline() - accept a command line starting with firstc.
134 *
135 * firstc == ':' get ":" command line.
136 * firstc == '/' or '?' get search pattern
137 * firstc == '=' get expression
138 * firstc == '@' get text for input() function
139 * firstc == '>' get text for debug mode
140 * firstc == NUL get text for :insert command
141 * firstc == -1 like NUL, and break on CTRL-C
142 *
143 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
144 * command line.
145 *
146 * Careful: getcmdline() can be called recursively!
147 *
148 * Return pointer to allocated string if there is a commandline, NULL
149 * otherwise.
150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 char_u *
152getcmdline(firstc, count, indent)
153 int firstc;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000154 long count UNUSED; /* only used for incremental search */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 int indent; /* indent for inside conditionals */
156{
157 int c;
158 int i;
159 int j;
160 int gotesc = FALSE; /* TRUE when <ESC> just typed */
161 int do_abbr; /* when TRUE check for abbr. */
162#ifdef FEAT_CMDHIST
163 char_u *lookfor = NULL; /* string to match */
164 int hiscnt; /* current history line in use */
165 int histype; /* history type to be used */
166#endif
167#ifdef FEAT_SEARCH_EXTRA
168 pos_T old_cursor;
169 colnr_T old_curswant;
170 colnr_T old_leftcol;
171 linenr_T old_topline;
172# ifdef FEAT_DIFF
173 int old_topfill;
174# endif
175 linenr_T old_botline;
176 int did_incsearch = FALSE;
177 int incsearch_postponed = FALSE;
178#endif
179 int did_wild_list = FALSE; /* did wild_list() recently */
180 int wim_index = 0; /* index in wim_flags[] */
181 int res;
182 int save_msg_scroll = msg_scroll;
183 int save_State = State; /* remember State when called */
184 int some_key_typed = FALSE; /* one of the keys was typed */
185#ifdef FEAT_MOUSE
186 /* mouse drag and release events are ignored, unless they are
187 * preceded with a mouse down event */
188 int ignore_drag_release = TRUE;
189#endif
190#ifdef FEAT_EVAL
191 int break_ctrl_c = FALSE;
192#endif
193 expand_T xpc;
194 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000195#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
196 /* Everything that may work recursively should save and restore the
197 * current command line in save_ccline. That includes update_screen(), a
198 * custom status line may invoke ":normal". */
199 struct cmdline_info save_ccline;
200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
202#ifdef FEAT_SNIFF
203 want_sniff_request = 0;
204#endif
205#ifdef FEAT_EVAL
206 if (firstc == -1)
207 {
208 firstc = NUL;
209 break_ctrl_c = TRUE;
210 }
211#endif
212#ifdef FEAT_RIGHTLEFT
213 /* start without Hebrew mapping for a command line */
214 if (firstc == ':' || firstc == '=' || firstc == '>')
215 cmd_hkmap = 0;
216#endif
217
218 ccline.overstrike = FALSE; /* always start in insert mode */
219#ifdef FEAT_SEARCH_EXTRA
220 old_cursor = curwin->w_cursor; /* needs to be restored later */
221 old_curswant = curwin->w_curswant;
222 old_leftcol = curwin->w_leftcol;
223 old_topline = curwin->w_topline;
224# ifdef FEAT_DIFF
225 old_topfill = curwin->w_topfill;
226# endif
227 old_botline = curwin->w_botline;
228#endif
229
230 /*
231 * set some variables for redrawcmd()
232 */
233 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000234 ccline.cmdindent = (firstc > 0 ? indent : 0);
235
236 /* alloc initial ccline.cmdbuff */
237 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 if (ccline.cmdbuff == NULL)
239 return NULL; /* out of memory */
240 ccline.cmdlen = ccline.cmdpos = 0;
241 ccline.cmdbuff[0] = NUL;
242
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000243 /* autoindent for :insert and :append */
244 if (firstc <= 0)
245 {
246 copy_spaces(ccline.cmdbuff, indent);
247 ccline.cmdbuff[indent] = NUL;
248 ccline.cmdpos = indent;
249 ccline.cmdspos = indent;
250 ccline.cmdlen = indent;
251 }
252
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000254 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256#ifdef FEAT_RIGHTLEFT
257 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
258 && (firstc == '/' || firstc == '?'))
259 cmdmsg_rl = TRUE;
260 else
261 cmdmsg_rl = FALSE;
262#endif
263
264 redir_off = TRUE; /* don't redirect the typed command */
265 if (!cmd_silent)
266 {
267 i = msg_scrolled;
268 msg_scrolled = 0; /* avoid wait_return message */
269 gotocmdline(TRUE);
270 msg_scrolled += i;
271 redrawcmdprompt(); /* draw prompt or indent */
272 set_cmdspos();
273 }
274 xpc.xp_context = EXPAND_NOTHING;
275 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000276#ifndef BACKSLASH_IN_FILENAME
277 xpc.xp_shell = FALSE;
278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000280#if defined(FEAT_EVAL)
281 if (ccline.input_fn)
282 {
283 xpc.xp_context = ccline.xp_context;
284 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000285# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000286 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000287# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000288 }
289#endif
290
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 /*
292 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
293 * doing ":@0" when register 0 doesn't contain a CR.
294 */
295 msg_scroll = FALSE;
296
297 State = CMDLINE;
298
299 if (firstc == '/' || firstc == '?' || firstc == '@')
300 {
301 /* Use ":lmap" mappings for search pattern and input(). */
302 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
303 b_im_ptr = &curbuf->b_p_iminsert;
304 else
305 b_im_ptr = &curbuf->b_p_imsearch;
306 if (*b_im_ptr == B_IMODE_LMAP)
307 State |= LANGMAP;
308#ifdef USE_IM_CONTROL
309 im_set_active(*b_im_ptr == B_IMODE_IM);
310#endif
311 }
312#ifdef USE_IM_CONTROL
313 else if (p_imcmdline)
314 im_set_active(TRUE);
315#endif
316
317#ifdef FEAT_MOUSE
318 setmouse();
319#endif
320#ifdef CURSOR_SHAPE
321 ui_cursor_shape(); /* may show different cursor shape */
322#endif
323
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000324 /* When inside an autocommand for writing "exiting" may be set and
325 * terminal mode set to cooked. Need to set raw mode here then. */
326 settmode(TMODE_RAW);
327
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_CMDHIST
329 init_history();
330 hiscnt = hislen; /* set hiscnt to impossible history value */
331 histype = hist_char2type(firstc);
332#endif
333
334#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000335 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336#endif
337
338 /*
339 * Collect the command string, handling editing keys.
340 */
341 for (;;)
342 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000343 redir_off = TRUE; /* Don't redirect the typed command.
344 Repeated, because a ":redir" inside
345 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#ifdef USE_ON_FLY_SCROLL
347 dont_scroll = FALSE; /* allow scrolling here */
348#endif
349 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
350
351 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000352
353 /* Get a character. Ignore K_IGNORE, it should not do anything, such
354 * as stop completion. */
355 do
356 {
357 c = safe_vgetc();
358 } while (c == K_IGNORE);
359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 if (KeyTyped)
361 {
362 some_key_typed = TRUE;
363#ifdef FEAT_RIGHTLEFT
364 if (cmd_hkmap)
365 c = hkmap(c);
366# ifdef FEAT_FKMAP
367 if (cmd_fkmap)
368 c = cmdl_fkmap(c);
369# endif
370 if (cmdmsg_rl && !KeyStuffed)
371 {
372 /* Invert horizontal movements and operations. Only when
373 * typed by the user directly, not when the result of a
374 * mapping. */
375 switch (c)
376 {
377 case K_RIGHT: c = K_LEFT; break;
378 case K_S_RIGHT: c = K_S_LEFT; break;
379 case K_C_RIGHT: c = K_C_LEFT; break;
380 case K_LEFT: c = K_RIGHT; break;
381 case K_S_LEFT: c = K_S_RIGHT; break;
382 case K_C_LEFT: c = K_C_RIGHT; break;
383 }
384 }
385#endif
386 }
387
388 /*
389 * Ignore got_int when CTRL-C was typed here.
390 * Don't ignore it in :global, we really need to break then, e.g., for
391 * ":g/pat/normal /pat" (without the <CR>).
392 * Don't ignore it for the input() function.
393 */
394 if ((c == Ctrl_C
395#ifdef UNIX
396 || c == intr_char
397#endif
398 )
399#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
400 && firstc != '@'
401#endif
402#ifdef FEAT_EVAL
403 && !break_ctrl_c
404#endif
405 && !global_busy)
406 got_int = FALSE;
407
408#ifdef FEAT_CMDHIST
409 /* free old command line when finished moving around in the history
410 * list */
411 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000412 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000413 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 && c != K_PAGEDOWN && c != K_PAGEUP
415 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000416 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
418 {
419 vim_free(lookfor);
420 lookfor = NULL;
421 }
422#endif
423
424 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000425 * When there are matching completions to select <S-Tab> works like
426 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000428 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 c = Ctrl_P;
430
431#ifdef FEAT_WILDMENU
432 /* Special translations for 'wildmenu' */
433 if (did_wild_list && p_wmnu)
434 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000435 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000437 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 c = Ctrl_N;
439 }
440 /* Hitting CR after "emenu Name.": complete submenu */
441 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
442 && ccline.cmdpos > 1
443 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
444 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
445 && (c == '\n' || c == '\r' || c == K_KENTER))
446 c = K_DOWN;
447#endif
448
449 /* free expanded names when finished walking through matches */
450 if (xpc.xp_numfiles != -1
451 && !(c == p_wc && KeyTyped) && c != p_wcm
452 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
453 && c != Ctrl_L)
454 {
455 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
456 did_wild_list = FALSE;
457#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000458 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459#endif
460 xpc.xp_context = EXPAND_NOTHING;
461 wim_index = 0;
462#ifdef FEAT_WILDMENU
463 if (p_wmnu && wild_menu_showing != 0)
464 {
465 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000466 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000467
468 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000469 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
471 if (wild_menu_showing == WM_SCROLLED)
472 {
473 /* Entered command line, move it up */
474 cmdline_row--;
475 redrawcmd();
476 }
477 else if (save_p_ls != -1)
478 {
479 /* restore 'laststatus' and 'winminheight' */
480 p_ls = save_p_ls;
481 p_wmh = save_p_wmh;
482 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000483 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000485 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 redrawcmd();
487 save_p_ls = -1;
488 }
489 else
490 {
491# ifdef FEAT_VERTSPLIT
492 win_redraw_last_status(topframe);
493# else
494 lastwin->w_redr_status = TRUE;
495# endif
496 redraw_statuslines();
497 }
498 KeyTyped = skt;
499 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000500 if (ccline.input_fn)
501 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503#endif
504 }
505
506#ifdef FEAT_WILDMENU
507 /* Special translations for 'wildmenu' */
508 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
509 {
510 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000511 if (c == K_DOWN && ccline.cmdpos > 0
512 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000514 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {
516 /* Hitting <Up>: Remove one submenu name in front of the
517 * cursor */
518 int found = FALSE;
519
520 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
521 i = 0;
522 while (--j > 0)
523 {
524 /* check for start of menu name */
525 if (ccline.cmdbuff[j] == ' '
526 && ccline.cmdbuff[j - 1] != '\\')
527 {
528 i = j + 1;
529 break;
530 }
531 /* check for start of submenu name */
532 if (ccline.cmdbuff[j] == '.'
533 && ccline.cmdbuff[j - 1] != '\\')
534 {
535 if (found)
536 {
537 i = j + 1;
538 break;
539 }
540 else
541 found = TRUE;
542 }
543 }
544 if (i > 0)
545 cmdline_del(i);
546 c = p_wc;
547 xpc.xp_context = EXPAND_NOTHING;
548 }
549 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000550 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000551 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000552 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 {
554 char_u upseg[5];
555
556 upseg[0] = PATHSEP;
557 upseg[1] = '.';
558 upseg[2] = '.';
559 upseg[3] = PATHSEP;
560 upseg[4] = NUL;
561
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000562 if (c == K_DOWN
563 && ccline.cmdpos > 0
564 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
565 && (ccline.cmdpos < 3
566 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
568 {
569 /* go down a directory */
570 c = p_wc;
571 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000572 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
574 /* If in a direct ancestor, strip off one ../ to go down */
575 int found = FALSE;
576
577 j = ccline.cmdpos;
578 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
579 while (--j > i)
580 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000581#ifdef FEAT_MBYTE
582 if (has_mbyte)
583 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 if (vim_ispathsep(ccline.cmdbuff[j]))
586 {
587 found = TRUE;
588 break;
589 }
590 }
591 if (found
592 && ccline.cmdbuff[j - 1] == '.'
593 && ccline.cmdbuff[j - 2] == '.'
594 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
595 {
596 cmdline_del(j - 2);
597 c = p_wc;
598 }
599 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000600 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 {
602 /* go up a directory */
603 int found = FALSE;
604
605 j = ccline.cmdpos - 1;
606 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
607 while (--j > i)
608 {
609#ifdef FEAT_MBYTE
610 if (has_mbyte)
611 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
612#endif
613 if (vim_ispathsep(ccline.cmdbuff[j])
614#ifdef BACKSLASH_IN_FILENAME
615 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
616 == NULL
617#endif
618 )
619 {
620 if (found)
621 {
622 i = j + 1;
623 break;
624 }
625 else
626 found = TRUE;
627 }
628 }
629
630 if (!found)
631 j = i;
632 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
633 j += 4;
634 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
635 && j == i)
636 j += 3;
637 else
638 j = 0;
639 if (j > 0)
640 {
641 /* TODO this is only for DOS/UNIX systems - need to put in
642 * machine-specific stuff here and in upseg init */
643 cmdline_del(j);
644 put_on_cmdline(upseg + 1, 3, FALSE);
645 }
646 else if (ccline.cmdpos > i)
647 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100648
649 /* Now complete in the new directory. Set KeyTyped in case the
650 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100652 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
656#endif /* FEAT_WILDMENU */
657
658 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
659 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
660 if (c == Ctrl_BSL)
661 {
662 ++no_mapping;
663 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000664 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 --no_mapping;
666 --allow_keys;
667 /* CTRL-\ e doesn't work when obtaining an expression. */
668 if (c != Ctrl_N && c != Ctrl_G
669 && (c != 'e' || ccline.cmdfirstc == '='))
670 {
671 vungetc(c);
672 c = Ctrl_BSL;
673 }
674#ifdef FEAT_EVAL
675 else if (c == 'e')
676 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200677 char_u *p = NULL;
678 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 /*
681 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000682 * Need to save and restore the current command line, to be
683 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 */
685 if (ccline.cmdpos == ccline.cmdlen)
686 new_cmdpos = 99999; /* keep it at the end */
687 else
688 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000689
690 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000692 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 if (c == '=')
694 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000695 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000696 * to avoid nasty things like going to another buffer when
697 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000698 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000699 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000701 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000702 restore_cmdline(&save_ccline);
703
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200704 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200706 len = (int)STRLEN(p);
707 if (realloc_cmdbuff(len + 1) == OK)
708 {
709 ccline.cmdlen = len;
710 STRCPY(ccline.cmdbuff, p);
711 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200713 /* Restore the cursor or use the position set with
714 * set_cmdline_pos(). */
715 if (new_cmdpos > ccline.cmdlen)
716 ccline.cmdpos = ccline.cmdlen;
717 else
718 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200720 KeyTyped = FALSE; /* Don't do p_wc completion. */
721 redrawcmd();
722 goto cmdline_changed;
723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 }
725 }
726 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100727 got_int = FALSE; /* don't abandon the command line */
728 did_emsg = FALSE;
729 emsg_on_display = FALSE;
730 redrawcmd();
731 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 }
733#endif
734 else
735 {
736 if (c == Ctrl_G && p_im && restart_edit == 0)
737 restart_edit = 'a';
738 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
739 in history */
740 goto returncmd; /* back to Normal mode */
741 }
742 }
743
744#ifdef FEAT_CMDWIN
745 if (c == cedit_key || c == K_CMDWIN)
746 {
747 /*
748 * Open a window to edit the command line (and history).
749 */
750 c = ex_window();
751 some_key_typed = TRUE;
752 }
753# ifdef FEAT_DIGRAPHS
754 else
755# endif
756#endif
757#ifdef FEAT_DIGRAPHS
758 c = do_digraph(c);
759#endif
760
761 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
762 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
763 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000764 /* In Ex mode a backslash escapes a newline. */
765 if (exmode_active
766 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000767 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000768 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000769 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000771 if (c == K_KENTER)
772 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000774 else
775 {
776 gotesc = FALSE; /* Might have typed ESC previously, don't
777 truncate the cmdline now. */
778 if (ccheck_abbr(c + ABBR_OFF))
779 goto cmdline_changed;
780 if (!cmd_silent)
781 {
782 windgoto(msg_row, 0);
783 out_flush();
784 }
785 break;
786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
788
789 /*
790 * Completion for 'wildchar' or 'wildcharm' key.
791 * - hitting <ESC> twice means: abandon command line.
792 * - wildcard expansion is only done when the 'wildchar' key is really
793 * typed, not when it comes from a macro
794 */
795 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
796 {
797 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
798 {
799 /* if 'wildmode' contains "list" may still need to list */
800 if (xpc.xp_numfiles > 1
801 && !did_wild_list
802 && (wim_flags[wim_index] & WIM_LIST))
803 {
804 (void)showmatches(&xpc, FALSE);
805 redrawcmd();
806 did_wild_list = TRUE;
807 }
808 if (wim_flags[wim_index] & WIM_LONGEST)
809 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
810 else if (wim_flags[wim_index] & WIM_FULL)
811 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
812 else
813 res = OK; /* don't insert 'wildchar' now */
814 }
815 else /* typed p_wc first time */
816 {
817 wim_index = 0;
818 j = ccline.cmdpos;
819 /* if 'wildmode' first contains "longest", get longest
820 * common part */
821 if (wim_flags[0] & WIM_LONGEST)
822 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
823 else
824 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
825
826 /* if interrupted while completing, behave like it failed */
827 if (got_int)
828 {
829 (void)vpeekc(); /* remove <C-C> from input stream */
830 got_int = FALSE; /* don't abandon the command line */
831 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
832#ifdef FEAT_WILDMENU
833 xpc.xp_context = EXPAND_NOTHING;
834#endif
835 goto cmdline_changed;
836 }
837
838 /* when more than one match, and 'wildmode' first contains
839 * "list", or no change and 'wildmode' contains "longest,list",
840 * list all matches */
841 if (res == OK && xpc.xp_numfiles > 1)
842 {
843 /* a "longest" that didn't do anything is skipped (but not
844 * "list:longest") */
845 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
846 wim_index = 1;
847 if ((wim_flags[wim_index] & WIM_LIST)
848#ifdef FEAT_WILDMENU
849 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
850#endif
851 )
852 {
853 if (!(wim_flags[0] & WIM_LONGEST))
854 {
855#ifdef FEAT_WILDMENU
856 int p_wmnu_save = p_wmnu;
857 p_wmnu = 0;
858#endif
859 nextwild(&xpc, WILD_PREV, 0); /* remove match */
860#ifdef FEAT_WILDMENU
861 p_wmnu = p_wmnu_save;
862#endif
863 }
864#ifdef FEAT_WILDMENU
865 (void)showmatches(&xpc, p_wmnu
866 && ((wim_flags[wim_index] & WIM_LIST) == 0));
867#else
868 (void)showmatches(&xpc, FALSE);
869#endif
870 redrawcmd();
871 did_wild_list = TRUE;
872 if (wim_flags[wim_index] & WIM_LONGEST)
873 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
874 else if (wim_flags[wim_index] & WIM_FULL)
875 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
876 }
877 else
878 vim_beep();
879 }
880#ifdef FEAT_WILDMENU
881 else if (xpc.xp_numfiles == -1)
882 xpc.xp_context = EXPAND_NOTHING;
883#endif
884 }
885 if (wim_index < 3)
886 ++wim_index;
887 if (c == ESC)
888 gotesc = TRUE;
889 if (res == OK)
890 goto cmdline_changed;
891 }
892
893 gotesc = FALSE;
894
895 /* <S-Tab> goes to last match, in a clumsy way */
896 if (c == K_S_TAB && KeyTyped)
897 {
898 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
899 && nextwild(&xpc, WILD_PREV, 0) == OK
900 && nextwild(&xpc, WILD_PREV, 0) == OK)
901 goto cmdline_changed;
902 }
903
904 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
905 c = NL;
906
907 do_abbr = TRUE; /* default: check for abbreviation */
908
909 /*
910 * Big switch for a typed command line character.
911 */
912 switch (c)
913 {
914 case K_BS:
915 case Ctrl_H:
916 case K_DEL:
917 case K_KDEL:
918 case Ctrl_W:
919#ifdef FEAT_FKMAP
920 if (cmd_fkmap && c == K_BS)
921 c = K_DEL;
922#endif
923 if (c == K_KDEL)
924 c = K_DEL;
925
926 /*
927 * delete current character is the same as backspace on next
928 * character, except at end of line
929 */
930 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
931 ++ccline.cmdpos;
932#ifdef FEAT_MBYTE
933 if (has_mbyte && c == K_DEL)
934 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
935 ccline.cmdbuff + ccline.cmdpos);
936#endif
937 if (ccline.cmdpos > 0)
938 {
939 char_u *p;
940
941 j = ccline.cmdpos;
942 p = ccline.cmdbuff + j;
943#ifdef FEAT_MBYTE
944 if (has_mbyte)
945 {
946 p = mb_prevptr(ccline.cmdbuff, p);
947 if (c == Ctrl_W)
948 {
949 while (p > ccline.cmdbuff && vim_isspace(*p))
950 p = mb_prevptr(ccline.cmdbuff, p);
951 i = mb_get_class(p);
952 while (p > ccline.cmdbuff && mb_get_class(p) == i)
953 p = mb_prevptr(ccline.cmdbuff, p);
954 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000955 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957 }
958 else
959#endif
960 if (c == Ctrl_W)
961 {
962 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
963 --p;
964 i = vim_iswordc(p[-1]);
965 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
966 && vim_iswordc(p[-1]) == i)
967 --p;
968 }
969 else
970 --p;
971 ccline.cmdpos = (int)(p - ccline.cmdbuff);
972 ccline.cmdlen -= j - ccline.cmdpos;
973 i = ccline.cmdpos;
974 while (i < ccline.cmdlen)
975 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
976
977 /* Truncate at the end, required for multi-byte chars. */
978 ccline.cmdbuff[ccline.cmdlen] = NUL;
979 redrawcmd();
980 }
981 else if (ccline.cmdlen == 0 && c != Ctrl_W
982 && ccline.cmdprompt == NULL && indent == 0)
983 {
984 /* In ex and debug mode it doesn't make sense to return. */
985 if (exmode_active
986#ifdef FEAT_EVAL
987 || ccline.cmdfirstc == '>'
988#endif
989 )
990 goto cmdline_not_changed;
991
992 vim_free(ccline.cmdbuff); /* no commandline to return */
993 ccline.cmdbuff = NULL;
994 if (!cmd_silent)
995 {
996#ifdef FEAT_RIGHTLEFT
997 if (cmdmsg_rl)
998 msg_col = Columns;
999 else
1000#endif
1001 msg_col = 0;
1002 msg_putchar(' '); /* delete ':' */
1003 }
1004 redraw_cmdline = TRUE;
1005 goto returncmd; /* back to cmd mode */
1006 }
1007 goto cmdline_changed;
1008
1009 case K_INS:
1010 case K_KINS:
1011#ifdef FEAT_FKMAP
1012 /* if Farsi mode set, we are in reverse insert mode -
1013 Do not change the mode */
1014 if (cmd_fkmap)
1015 beep_flush();
1016 else
1017#endif
1018 ccline.overstrike = !ccline.overstrike;
1019#ifdef CURSOR_SHAPE
1020 ui_cursor_shape(); /* may show different cursor shape */
1021#endif
1022 goto cmdline_not_changed;
1023
1024 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001025 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 {
1027 /* ":lmap" mappings exists, toggle use of mappings. */
1028 State ^= LANGMAP;
1029#ifdef USE_IM_CONTROL
1030 im_set_active(FALSE); /* Disable input method */
1031#endif
1032 if (b_im_ptr != NULL)
1033 {
1034 if (State & LANGMAP)
1035 *b_im_ptr = B_IMODE_LMAP;
1036 else
1037 *b_im_ptr = B_IMODE_NONE;
1038 }
1039 }
1040#ifdef USE_IM_CONTROL
1041 else
1042 {
1043 /* There are no ":lmap" mappings, toggle IM. When
1044 * 'imdisable' is set don't try getting the status, it's
1045 * always off. */
1046 if ((p_imdisable && b_im_ptr != NULL)
1047 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1048 {
1049 im_set_active(FALSE); /* Disable input method */
1050 if (b_im_ptr != NULL)
1051 *b_im_ptr = B_IMODE_NONE;
1052 }
1053 else
1054 {
1055 im_set_active(TRUE); /* Enable input method */
1056 if (b_im_ptr != NULL)
1057 *b_im_ptr = B_IMODE_IM;
1058 }
1059 }
1060#endif
1061 if (b_im_ptr != NULL)
1062 {
1063 if (b_im_ptr == &curbuf->b_p_iminsert)
1064 set_iminsert_global();
1065 else
1066 set_imsearch_global();
1067 }
1068#ifdef CURSOR_SHAPE
1069 ui_cursor_shape(); /* may show different cursor shape */
1070#endif
1071#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1072 /* Show/unshow value of 'keymap' in status lines later. */
1073 status_redraw_curbuf();
1074#endif
1075 goto cmdline_not_changed;
1076
1077/* case '@': only in very old vi */
1078 case Ctrl_U:
1079 /* delete all characters left of the cursor */
1080 j = ccline.cmdpos;
1081 ccline.cmdlen -= j;
1082 i = ccline.cmdpos = 0;
1083 while (i < ccline.cmdlen)
1084 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1085 /* Truncate at the end, required for multi-byte chars. */
1086 ccline.cmdbuff[ccline.cmdlen] = NUL;
1087 redrawcmd();
1088 goto cmdline_changed;
1089
1090#ifdef FEAT_CLIPBOARD
1091 case Ctrl_Y:
1092 /* Copy the modeless selection, if there is one. */
1093 if (clip_star.state != SELECT_CLEARED)
1094 {
1095 if (clip_star.state == SELECT_DONE)
1096 clip_copy_modeless_selection(TRUE);
1097 goto cmdline_not_changed;
1098 }
1099 break;
1100#endif
1101
1102 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1103 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001104 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001105 * ":normal" runs out of characters. */
1106 if (exmode_active
1107#ifdef FEAT_EX_EXTRA
1108 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1109#endif
1110 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 goto cmdline_not_changed;
1112
1113 gotesc = TRUE; /* will free ccline.cmdbuff after
1114 putting it in history */
1115 goto returncmd; /* back to cmd mode */
1116
1117 case Ctrl_R: /* insert register */
1118#ifdef USE_ON_FLY_SCROLL
1119 dont_scroll = TRUE; /* disallow scrolling here */
1120#endif
1121 putcmdline('"', TRUE);
1122 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001123 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (i == Ctrl_O)
1125 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1126 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001127 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 --no_mapping;
1129#ifdef FEAT_EVAL
1130 /*
1131 * Insert the result of an expression.
1132 * Need to save the current command line, to be able to enter
1133 * a new one...
1134 */
1135 new_cmdpos = -1;
1136 if (c == '=')
1137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1139 {
1140 beep_flush();
1141 c = ESC;
1142 }
1143 else
1144 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001145 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001147 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 }
1149 }
1150#endif
1151 if (c != ESC) /* use ESC to cancel inserting register */
1152 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001153 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001154
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001155#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001156 /* When there was a serious error abort getting the
1157 * command line. */
1158 if (aborting())
1159 {
1160 gotesc = TRUE; /* will free ccline.cmdbuff after
1161 putting it in history */
1162 goto returncmd; /* back to cmd mode */
1163 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 KeyTyped = FALSE; /* Don't do p_wc completion. */
1166#ifdef FEAT_EVAL
1167 if (new_cmdpos >= 0)
1168 {
1169 /* set_cmdline_pos() was used */
1170 if (new_cmdpos > ccline.cmdlen)
1171 ccline.cmdpos = ccline.cmdlen;
1172 else
1173 ccline.cmdpos = new_cmdpos;
1174 }
1175#endif
1176 }
1177 redrawcmd();
1178 goto cmdline_changed;
1179
1180 case Ctrl_D:
1181 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1182 break; /* Use ^D as normal char instead */
1183
1184 redrawcmd();
1185 continue; /* don't do incremental search now */
1186
1187 case K_RIGHT:
1188 case K_S_RIGHT:
1189 case K_C_RIGHT:
1190 do
1191 {
1192 if (ccline.cmdpos >= ccline.cmdlen)
1193 break;
1194 i = cmdline_charsize(ccline.cmdpos);
1195 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1196 break;
1197 ccline.cmdspos += i;
1198#ifdef FEAT_MBYTE
1199 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001200 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 + ccline.cmdpos);
1202 else
1203#endif
1204 ++ccline.cmdpos;
1205 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001206 while ((c == K_S_RIGHT || c == K_C_RIGHT
1207 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1209#ifdef FEAT_MBYTE
1210 if (has_mbyte)
1211 set_cmdspos_cursor();
1212#endif
1213 goto cmdline_not_changed;
1214
1215 case K_LEFT:
1216 case K_S_LEFT:
1217 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001218 if (ccline.cmdpos == 0)
1219 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 do
1221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 --ccline.cmdpos;
1223#ifdef FEAT_MBYTE
1224 if (has_mbyte) /* move to first byte of char */
1225 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1226 ccline.cmdbuff + ccline.cmdpos);
1227#endif
1228 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1229 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001230 while (ccline.cmdpos > 0
1231 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001232 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1234#ifdef FEAT_MBYTE
1235 if (has_mbyte)
1236 set_cmdspos_cursor();
1237#endif
1238 goto cmdline_not_changed;
1239
1240 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001241 /* Ignore mouse event or ex_window() result. */
1242 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243
Bram Moolenaar4770d092006-01-12 23:22:24 +00001244#ifdef FEAT_GUI_W32
1245 /* On Win32 ignore <M-F4>, we get it when closing the window was
1246 * cancelled. */
1247 case K_F4:
1248 if (mod_mask == MOD_MASK_ALT)
1249 {
1250 redrawcmd(); /* somehow the cmdline is cleared */
1251 goto cmdline_not_changed;
1252 }
1253 break;
1254#endif
1255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256#ifdef FEAT_MOUSE
1257 case K_MIDDLEDRAG:
1258 case K_MIDDLERELEASE:
1259 goto cmdline_not_changed; /* Ignore mouse */
1260
1261 case K_MIDDLEMOUSE:
1262# ifdef FEAT_GUI
1263 /* When GUI is active, also paste when 'mouse' is empty */
1264 if (!gui.in_use)
1265# endif
1266 if (!mouse_has(MOUSE_COMMAND))
1267 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001268# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001270 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001272# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001273 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 redrawcmd();
1275 goto cmdline_changed;
1276
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001277# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001279 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 redrawcmd();
1281 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001282# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283
1284 case K_LEFTDRAG:
1285 case K_LEFTRELEASE:
1286 case K_RIGHTDRAG:
1287 case K_RIGHTRELEASE:
1288 /* Ignore drag and release events when the button-down wasn't
1289 * seen before. */
1290 if (ignore_drag_release)
1291 goto cmdline_not_changed;
1292 /* FALLTHROUGH */
1293 case K_LEFTMOUSE:
1294 case K_RIGHTMOUSE:
1295 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1296 ignore_drag_release = TRUE;
1297 else
1298 ignore_drag_release = FALSE;
1299# ifdef FEAT_GUI
1300 /* When GUI is active, also move when 'mouse' is empty */
1301 if (!gui.in_use)
1302# endif
1303 if (!mouse_has(MOUSE_COMMAND))
1304 goto cmdline_not_changed; /* Ignore mouse */
1305# ifdef FEAT_CLIPBOARD
1306 if (mouse_row < cmdline_row && clip_star.available)
1307 {
1308 int button, is_click, is_drag;
1309
1310 /*
1311 * Handle modeless selection.
1312 */
1313 button = get_mouse_button(KEY2TERMCAP1(c),
1314 &is_click, &is_drag);
1315 if (mouse_model_popup() && button == MOUSE_LEFT
1316 && (mod_mask & MOD_MASK_SHIFT))
1317 {
1318 /* Translate shift-left to right button. */
1319 button = MOUSE_RIGHT;
1320 mod_mask &= ~MOD_MASK_SHIFT;
1321 }
1322 clip_modeless(button, is_click, is_drag);
1323 goto cmdline_not_changed;
1324 }
1325# endif
1326
1327 set_cmdspos();
1328 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1329 ++ccline.cmdpos)
1330 {
1331 i = cmdline_charsize(ccline.cmdpos);
1332 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1333 && mouse_col < ccline.cmdspos % Columns + i)
1334 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001335# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 if (has_mbyte)
1337 {
1338 /* Count ">" for double-wide char that doesn't fit. */
1339 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001340 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 + ccline.cmdpos) - 1;
1342 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 ccline.cmdspos += i;
1345 }
1346 goto cmdline_not_changed;
1347
1348 /* Mouse scroll wheel: ignored here */
1349 case K_MOUSEDOWN:
1350 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001351 case K_MOUSELEFT:
1352 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 /* Alternate buttons ignored here */
1354 case K_X1MOUSE:
1355 case K_X1DRAG:
1356 case K_X1RELEASE:
1357 case K_X2MOUSE:
1358 case K_X2DRAG:
1359 case K_X2RELEASE:
1360 goto cmdline_not_changed;
1361
1362#endif /* FEAT_MOUSE */
1363
1364#ifdef FEAT_GUI
1365 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1366 case K_LEFTRELEASE_NM:
1367 goto cmdline_not_changed;
1368
1369 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001370 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 {
1372 gui_do_scroll();
1373 redrawcmd();
1374 }
1375 goto cmdline_not_changed;
1376
1377 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001378 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001380 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 redrawcmd();
1382 }
1383 goto cmdline_not_changed;
1384#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001385#ifdef FEAT_GUI_TABLINE
1386 case K_TABLINE:
1387 case K_TABMENU:
1388 /* Don't want to change any tabs here. Make sure the same tab
1389 * is still selected. */
1390 if (gui_use_tabline())
1391 gui_mch_set_curtab(tabpage_index(curtab));
1392 goto cmdline_not_changed;
1393#endif
1394
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 case K_SELECT: /* end of Select mode mapping - ignore */
1396 goto cmdline_not_changed;
1397
1398 case Ctrl_B: /* begin of command line */
1399 case K_HOME:
1400 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 case K_S_HOME:
1402 case K_C_HOME:
1403 ccline.cmdpos = 0;
1404 set_cmdspos();
1405 goto cmdline_not_changed;
1406
1407 case Ctrl_E: /* end of command line */
1408 case K_END:
1409 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 case K_S_END:
1411 case K_C_END:
1412 ccline.cmdpos = ccline.cmdlen;
1413 set_cmdspos_cursor();
1414 goto cmdline_not_changed;
1415
1416 case Ctrl_A: /* all matches */
1417 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1418 break;
1419 goto cmdline_changed;
1420
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001421 case Ctrl_L:
1422#ifdef FEAT_SEARCH_EXTRA
1423 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1424 {
1425 /* Add a character from under the cursor for 'incsearch' */
1426 if (did_incsearch
1427 && !equalpos(curwin->w_cursor, old_cursor))
1428 {
1429 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001430 /* If 'ignorecase' and 'smartcase' are set and the
1431 * command line has no uppercase characters, convert
1432 * the character to lowercase */
1433 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1434 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001435 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001436 {
1437 if (c == firstc || vim_strchr((char_u *)(
1438 p_magic ? "\\^$.*[" : "\\^$"), c)
1439 != NULL)
1440 {
1441 /* put a backslash before special characters */
1442 stuffcharReadbuff(c);
1443 c = '\\';
1444 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001445 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001446 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001447 }
1448 goto cmdline_not_changed;
1449 }
1450#endif
1451
1452 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1454 break;
1455 goto cmdline_changed;
1456
1457 case Ctrl_N: /* next match */
1458 case Ctrl_P: /* previous match */
1459 if (xpc.xp_numfiles > 0)
1460 {
1461 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1462 == FAIL)
1463 break;
1464 goto cmdline_changed;
1465 }
1466
1467#ifdef FEAT_CMDHIST
1468 case K_UP:
1469 case K_DOWN:
1470 case K_S_UP:
1471 case K_S_DOWN:
1472 case K_PAGEUP:
1473 case K_KPAGEUP:
1474 case K_PAGEDOWN:
1475 case K_KPAGEDOWN:
1476 if (hislen == 0 || firstc == NUL) /* no history */
1477 goto cmdline_not_changed;
1478
1479 i = hiscnt;
1480
1481 /* save current command string so it can be restored later */
1482 if (lookfor == NULL)
1483 {
1484 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1485 goto cmdline_not_changed;
1486 lookfor[ccline.cmdpos] = NUL;
1487 }
1488
1489 j = (int)STRLEN(lookfor);
1490 for (;;)
1491 {
1492 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001493 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001494 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
1496 if (hiscnt == hislen) /* first time */
1497 hiscnt = hisidx[histype];
1498 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1499 hiscnt = hislen - 1;
1500 else if (hiscnt != hisidx[histype] + 1)
1501 --hiscnt;
1502 else /* at top of list */
1503 {
1504 hiscnt = i;
1505 break;
1506 }
1507 }
1508 else /* one step forwards */
1509 {
1510 /* on last entry, clear the line */
1511 if (hiscnt == hisidx[histype])
1512 {
1513 hiscnt = hislen;
1514 break;
1515 }
1516
1517 /* not on a history line, nothing to do */
1518 if (hiscnt == hislen)
1519 break;
1520 if (hiscnt == hislen - 1) /* wrap around */
1521 hiscnt = 0;
1522 else
1523 ++hiscnt;
1524 }
1525 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1526 {
1527 hiscnt = i;
1528 break;
1529 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001530 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001531 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 || STRNCMP(history[histype][hiscnt].hisstr,
1533 lookfor, (size_t)j) == 0)
1534 break;
1535 }
1536
1537 if (hiscnt != i) /* jumped to other entry */
1538 {
1539 char_u *p;
1540 int len;
1541 int old_firstc;
1542
1543 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001544 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 if (hiscnt == hislen)
1546 p = lookfor; /* back to the old one */
1547 else
1548 p = history[histype][hiscnt].hisstr;
1549
1550 if (histype == HIST_SEARCH
1551 && p != lookfor
1552 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1553 {
1554 /* Correct for the separator character used when
1555 * adding the history entry vs the one used now.
1556 * First loop: count length.
1557 * Second loop: copy the characters. */
1558 for (i = 0; i <= 1; ++i)
1559 {
1560 len = 0;
1561 for (j = 0; p[j] != NUL; ++j)
1562 {
1563 /* Replace old sep with new sep, unless it is
1564 * escaped. */
1565 if (p[j] == old_firstc
1566 && (j == 0 || p[j - 1] != '\\'))
1567 {
1568 if (i > 0)
1569 ccline.cmdbuff[len] = firstc;
1570 }
1571 else
1572 {
1573 /* Escape new sep, unless it is already
1574 * escaped. */
1575 if (p[j] == firstc
1576 && (j == 0 || p[j - 1] != '\\'))
1577 {
1578 if (i > 0)
1579 ccline.cmdbuff[len] = '\\';
1580 ++len;
1581 }
1582 if (i > 0)
1583 ccline.cmdbuff[len] = p[j];
1584 }
1585 ++len;
1586 }
1587 if (i == 0)
1588 {
1589 alloc_cmdbuff(len);
1590 if (ccline.cmdbuff == NULL)
1591 goto returncmd;
1592 }
1593 }
1594 ccline.cmdbuff[len] = NUL;
1595 }
1596 else
1597 {
1598 alloc_cmdbuff((int)STRLEN(p));
1599 if (ccline.cmdbuff == NULL)
1600 goto returncmd;
1601 STRCPY(ccline.cmdbuff, p);
1602 }
1603
1604 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1605 redrawcmd();
1606 goto cmdline_changed;
1607 }
1608 beep_flush();
1609 goto cmdline_not_changed;
1610#endif
1611
1612 case Ctrl_V:
1613 case Ctrl_Q:
1614#ifdef FEAT_MOUSE
1615 ignore_drag_release = TRUE;
1616#endif
1617 putcmdline('^', TRUE);
1618 c = get_literal(); /* get next (two) character(s) */
1619 do_abbr = FALSE; /* don't do abbreviation now */
1620#ifdef FEAT_MBYTE
1621 /* may need to remove ^ when composing char was typed */
1622 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1623 {
1624 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1625 msg_putchar(' ');
1626 cursorcmd();
1627 }
1628#endif
1629 break;
1630
1631#ifdef FEAT_DIGRAPHS
1632 case Ctrl_K:
1633#ifdef FEAT_MOUSE
1634 ignore_drag_release = TRUE;
1635#endif
1636 putcmdline('?', TRUE);
1637#ifdef USE_ON_FLY_SCROLL
1638 dont_scroll = TRUE; /* disallow scrolling here */
1639#endif
1640 c = get_digraph(TRUE);
1641 if (c != NUL)
1642 break;
1643
1644 redrawcmd();
1645 goto cmdline_not_changed;
1646#endif /* FEAT_DIGRAPHS */
1647
1648#ifdef FEAT_RIGHTLEFT
1649 case Ctrl__: /* CTRL-_: switch language mode */
1650 if (!p_ari)
1651 break;
1652#ifdef FEAT_FKMAP
1653 if (p_altkeymap)
1654 {
1655 cmd_fkmap = !cmd_fkmap;
1656 if (cmd_fkmap) /* in Farsi always in Insert mode */
1657 ccline.overstrike = FALSE;
1658 }
1659 else /* Hebrew is default */
1660#endif
1661 cmd_hkmap = !cmd_hkmap;
1662 goto cmdline_not_changed;
1663#endif
1664
1665 default:
1666#ifdef UNIX
1667 if (c == intr_char)
1668 {
1669 gotesc = TRUE; /* will free ccline.cmdbuff after
1670 putting it in history */
1671 goto returncmd; /* back to Normal mode */
1672 }
1673#endif
1674 /*
1675 * Normal character with no special meaning. Just set mod_mask
1676 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1677 * the string <S-Space>. This should only happen after ^V.
1678 */
1679 if (!IS_SPECIAL(c))
1680 mod_mask = 0x0;
1681 break;
1682 }
1683 /*
1684 * End of switch on command line character.
1685 * We come here if we have a normal character.
1686 */
1687
1688 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1689#ifdef FEAT_MBYTE
1690 /* Add ABBR_OFF for characters above 0x100, this is
1691 * what check_abbr() expects. */
1692 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1693#endif
1694 c))
1695 goto cmdline_changed;
1696
1697 /*
1698 * put the character in the command line
1699 */
1700 if (IS_SPECIAL(c) || mod_mask != 0)
1701 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1702 else
1703 {
1704#ifdef FEAT_MBYTE
1705 if (has_mbyte)
1706 {
1707 j = (*mb_char2bytes)(c, IObuff);
1708 IObuff[j] = NUL; /* exclude composing chars */
1709 put_on_cmdline(IObuff, j, TRUE);
1710 }
1711 else
1712#endif
1713 {
1714 IObuff[0] = c;
1715 put_on_cmdline(IObuff, 1, TRUE);
1716 }
1717 }
1718 goto cmdline_changed;
1719
1720/*
1721 * This part implements incremental searches for "/" and "?"
1722 * Jump to cmdline_not_changed when a character has been read but the command
1723 * line did not change. Then we only search and redraw if something changed in
1724 * the past.
1725 * Jump to cmdline_changed when the command line did change.
1726 * (Sorry for the goto's, I know it is ugly).
1727 */
1728cmdline_not_changed:
1729#ifdef FEAT_SEARCH_EXTRA
1730 if (!incsearch_postponed)
1731 continue;
1732#endif
1733
1734cmdline_changed:
1735#ifdef FEAT_SEARCH_EXTRA
1736 /*
1737 * 'incsearch' highlighting.
1738 */
1739 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1740 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001741 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001742#ifdef FEAT_RELTIME
1743 proftime_T tm;
1744#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001745
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 /* if there is a character waiting, search and redraw later */
1747 if (char_avail())
1748 {
1749 incsearch_postponed = TRUE;
1750 continue;
1751 }
1752 incsearch_postponed = FALSE;
1753 curwin->w_cursor = old_cursor; /* start at old position */
1754
1755 /* If there is no command line, don't do anything */
1756 if (ccline.cmdlen == 0)
1757 i = 0;
1758 else
1759 {
1760 cursor_off(); /* so the user knows we're busy */
1761 out_flush();
1762 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001763#ifdef FEAT_RELTIME
1764 /* Set the time limit to half a second. */
1765 profile_setlimit(500L, &tm);
1766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001768 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1769#ifdef FEAT_RELTIME
1770 &tm
1771#else
1772 NULL
1773#endif
1774 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 --emsg_off;
1776 /* if interrupted while searching, behave like it failed */
1777 if (got_int)
1778 {
1779 (void)vpeekc(); /* remove <C-C> from input stream */
1780 got_int = FALSE; /* don't abandon the command line */
1781 i = 0;
1782 }
1783 else if (char_avail())
1784 /* cancelled searching because a char was typed */
1785 incsearch_postponed = TRUE;
1786 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001787 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 highlight_match = TRUE; /* highlight position */
1789 else
1790 highlight_match = FALSE; /* remove highlight */
1791
1792 /* first restore the old curwin values, so the screen is
1793 * positioned in the same way as the actual search command */
1794 curwin->w_leftcol = old_leftcol;
1795 curwin->w_topline = old_topline;
1796# ifdef FEAT_DIFF
1797 curwin->w_topfill = old_topfill;
1798# endif
1799 curwin->w_botline = old_botline;
1800 changed_cline_bef_curs();
1801 update_topline();
1802
1803 if (i != 0)
1804 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001805 pos_T save_pos = curwin->w_cursor;
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001808 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 * moves the whole match onto the screen when 'nowrap' is set.
1810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 curwin->w_cursor.lnum += search_match_lines;
1812 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001813 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1814 {
1815 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1816 coladvance((colnr_T)MAXCOL);
1817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001819 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001820 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001822 else
1823 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001824
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001826# ifdef FEAT_WINDOWS
1827 /* May redraw the status line to show the cursor position. */
1828 if (p_ru && curwin->w_status_height > 0)
1829 curwin->w_redr_status = TRUE;
1830# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001832 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001833 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001834 restore_cmdline(&save_ccline);
1835
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001836 /* Leave it at the end to make CTRL-R CTRL-W work. */
1837 if (i != 0)
1838 curwin->w_cursor = end_pos;
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 msg_starthere();
1841 redrawcmdline();
1842 did_incsearch = TRUE;
1843 }
1844#else /* FEAT_SEARCH_EXTRA */
1845 ;
1846#endif
1847
1848#ifdef FEAT_RIGHTLEFT
1849 if (cmdmsg_rl
1850# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001851 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852# endif
1853 )
1854 /* Always redraw the whole command line to fix shaping and
1855 * right-left typing. Not efficient, but it works. */
1856 redrawcmd();
1857#endif
1858 }
1859
1860returncmd:
1861
1862#ifdef FEAT_RIGHTLEFT
1863 cmdmsg_rl = FALSE;
1864#endif
1865
1866#ifdef FEAT_FKMAP
1867 cmd_fkmap = 0;
1868#endif
1869
1870 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001871 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
1873#ifdef FEAT_SEARCH_EXTRA
1874 if (did_incsearch)
1875 {
1876 curwin->w_cursor = old_cursor;
1877 curwin->w_curswant = old_curswant;
1878 curwin->w_leftcol = old_leftcol;
1879 curwin->w_topline = old_topline;
1880# ifdef FEAT_DIFF
1881 curwin->w_topfill = old_topfill;
1882# endif
1883 curwin->w_botline = old_botline;
1884 highlight_match = FALSE;
1885 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001886 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
1888#endif
1889
1890 if (ccline.cmdbuff != NULL)
1891 {
1892 /*
1893 * Put line in history buffer (":" and "=" only when it was typed).
1894 */
1895#ifdef FEAT_CMDHIST
1896 if (ccline.cmdlen && firstc != NUL
1897 && (some_key_typed || histype == HIST_SEARCH))
1898 {
1899 add_to_history(histype, ccline.cmdbuff, TRUE,
1900 histype == HIST_SEARCH ? firstc : NUL);
1901 if (firstc == ':')
1902 {
1903 vim_free(new_last_cmdline);
1904 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1905 }
1906 }
1907#endif
1908
1909 if (gotesc) /* abandon command line */
1910 {
1911 vim_free(ccline.cmdbuff);
1912 ccline.cmdbuff = NULL;
1913 if (msg_scrolled == 0)
1914 compute_cmdrow();
1915 MSG("");
1916 redraw_cmdline = TRUE;
1917 }
1918 }
1919
1920 /*
1921 * If the screen was shifted up, redraw the whole screen (later).
1922 * If the line is too long, clear it, so ruler and shown command do
1923 * not get printed in the middle of it.
1924 */
1925 msg_check();
1926 msg_scroll = save_msg_scroll;
1927 redir_off = FALSE;
1928
1929 /* When the command line was typed, no need for a wait-return prompt. */
1930 if (some_key_typed)
1931 need_wait_return = FALSE;
1932
1933 State = save_State;
1934#ifdef USE_IM_CONTROL
1935 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1936 im_save_status(b_im_ptr);
1937 im_set_active(FALSE);
1938#endif
1939#ifdef FEAT_MOUSE
1940 setmouse();
1941#endif
1942#ifdef CURSOR_SHAPE
1943 ui_cursor_shape(); /* may show different cursor shape */
1944#endif
1945
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001946 {
1947 char_u *p = ccline.cmdbuff;
1948
1949 /* Make ccline empty, getcmdline() may try to use it. */
1950 ccline.cmdbuff = NULL;
1951 return p;
1952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953}
1954
1955#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1956/*
1957 * Get a command line with a prompt.
1958 * This is prepared to be called recursively from getcmdline() (e.g. by
1959 * f_input() when evaluating an expression from CTRL-R =).
1960 * Returns the command line in allocated memory, or NULL.
1961 */
1962 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001963getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 int firstc;
1965 char_u *prompt; /* command line prompt */
1966 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001967 int xp_context; /* type of expansion */
1968 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969{
1970 char_u *s;
1971 struct cmdline_info save_ccline;
1972 int msg_col_save = msg_col;
1973
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001974 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 ccline.cmdprompt = prompt;
1976 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001977# ifdef FEAT_EVAL
1978 ccline.xp_context = xp_context;
1979 ccline.xp_arg = xp_arg;
1980 ccline.input_fn = (firstc == '@');
1981# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001983 restore_cmdline(&save_ccline);
Bram Moolenaar1db1f772011-08-17 16:25:48 +02001984 /* Restore msg_col, the prompt from input() may have changed it.
1985 * But only if called recursively and the commandline is therefore being
1986 * restored to an old one; if not, the input() prompt stays on the screen,
1987 * so we need its modified msg_col left intact. */
1988 if (ccline.cmdbuff != NULL)
1989 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990
1991 return s;
1992}
1993#endif
1994
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001995/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001996 * Return TRUE when the text must not be changed and we can't switch to
1997 * another window or buffer. Used when editing the command line, evaluating
1998 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001999 */
2000 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002001text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002002{
2003#ifdef FEAT_CMDWIN
2004 if (cmdwin_type != 0)
2005 return TRUE;
2006#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002007 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002008}
2009
2010/*
2011 * Give an error message for a command that isn't allowed while the cmdline
2012 * window is open or editing the cmdline in another way.
2013 */
2014 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002015text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002016{
2017#ifdef FEAT_CMDWIN
2018 if (cmdwin_type != 0)
2019 EMSG(_(e_cmdwin));
2020 else
2021#endif
2022 EMSG(_(e_secure));
2023}
2024
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002025#if defined(FEAT_AUTOCMD) || defined(PROTO)
2026/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002027 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2028 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002029 */
2030 int
2031curbuf_locked()
2032{
2033 if (curbuf_lock > 0)
2034 {
2035 EMSG(_("E788: Not allowed to edit another buffer now"));
2036 return TRUE;
2037 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002038 return allbuf_locked();
2039}
2040
2041/*
2042 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2043 * message.
2044 */
2045 int
2046allbuf_locked()
2047{
2048 if (allbuf_lock > 0)
2049 {
2050 EMSG(_("E811: Not allowed to change buffer information now"));
2051 return TRUE;
2052 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002053 return FALSE;
2054}
2055#endif
2056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 static int
2058cmdline_charsize(idx)
2059 int idx;
2060{
2061#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2062 if (cmdline_star > 0) /* showing '*', always 1 position */
2063 return 1;
2064#endif
2065 return ptr2cells(ccline.cmdbuff + idx);
2066}
2067
2068/*
2069 * Compute the offset of the cursor on the command line for the prompt and
2070 * indent.
2071 */
2072 static void
2073set_cmdspos()
2074{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002075 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 ccline.cmdspos = 1 + ccline.cmdindent;
2077 else
2078 ccline.cmdspos = 0 + ccline.cmdindent;
2079}
2080
2081/*
2082 * Compute the screen position for the cursor on the command line.
2083 */
2084 static void
2085set_cmdspos_cursor()
2086{
2087 int i, m, c;
2088
2089 set_cmdspos();
2090 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002093 if (m < 0) /* overflow, Columns or Rows at weird value */
2094 m = MAXCOL;
2095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 else
2097 m = MAXCOL;
2098 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2099 {
2100 c = cmdline_charsize(i);
2101#ifdef FEAT_MBYTE
2102 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2103 if (has_mbyte)
2104 correct_cmdspos(i, c);
2105#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002106 /* If the cmdline doesn't fit, show cursor on last visible char.
2107 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 if ((ccline.cmdspos += c) >= m)
2109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 ccline.cmdspos -= c;
2111 break;
2112 }
2113#ifdef FEAT_MBYTE
2114 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002115 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116#endif
2117 }
2118}
2119
2120#ifdef FEAT_MBYTE
2121/*
2122 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2123 * character that doesn't fit, so that a ">" must be displayed.
2124 */
2125 static void
2126correct_cmdspos(idx, cells)
2127 int idx;
2128 int cells;
2129{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002130 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2132 && ccline.cmdspos % Columns + cells > Columns)
2133 ccline.cmdspos++;
2134}
2135#endif
2136
2137/*
2138 * Get an Ex command line for the ":" command.
2139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002141getexline(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 int c; /* normally ':', NUL for ":append" */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002143 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 int indent; /* indent for inside conditionals */
2145{
2146 /* When executing a register, remove ':' that's in front of each line. */
2147 if (exec_from_reg && vpeekc() == ':')
2148 (void)vgetc();
2149 return getcmdline(c, 1L, indent);
2150}
2151
2152/*
2153 * Get an Ex command line for Ex mode.
2154 * In Ex mode we only use the OS supplied line editing features and no
2155 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002156 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002159getexmodeline(promptc, cookie, indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002160 int promptc; /* normally ':', NUL for ":append" and '?' for
2161 :s prompt */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002162 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 int indent; /* indent for inside conditionals */
2164{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002165 garray_T line_ga;
2166 char_u *pend;
2167 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002168 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002169 int escaped = FALSE; /* CTRL-V typed */
2170 int vcol = 0;
2171 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002172 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173
2174 /* Switch cursor on now. This avoids that it happens after the "\n", which
2175 * confuses the system function that computes tabstops. */
2176 cursor_on();
2177
2178 /* always start in column 0; write a newline if necessary */
2179 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002180 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002182 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002184 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002185 if (p_prompt)
2186 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 while (indent-- > 0)
2188 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 }
2191
2192 ga_init2(&line_ga, 1, 30);
2193
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002194 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002195 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002196 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002197 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002198 while (indent >= 8)
2199 {
2200 ga_append(&line_ga, TAB);
2201 msg_puts((char_u *)" ");
2202 indent -= 8;
2203 }
2204 while (indent-- > 0)
2205 {
2206 ga_append(&line_ga, ' ');
2207 msg_putchar(' ');
2208 }
2209 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002210 ++no_mapping;
2211 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002212
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 /*
2214 * Get the line, one character at a time.
2215 */
2216 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002217 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {
2219 if (ga_grow(&line_ga, 40) == FAIL)
2220 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002222 /* Get one character at a time. Don't use inchar(), it can't handle
2223 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002224 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002225 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002228 * Handle line editing.
2229 * Previously this was left to the system, putting the terminal in
2230 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002232 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002234 msg_putchar('\n');
2235 break;
2236 }
2237
2238 if (!escaped)
2239 {
2240 /* CR typed means "enter", which is NL */
2241 if (c1 == '\r')
2242 c1 = '\n';
2243
2244 if (c1 == BS || c1 == K_BS
2245 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002247 if (line_ga.ga_len > 0)
2248 {
2249 --line_ga.ga_len;
2250 goto redraw;
2251 }
2252 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
2254
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002255 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002257 msg_col = startcol;
2258 msg_clr_eos();
2259 line_ga.ga_len = 0;
2260 continue;
2261 }
2262
2263 if (c1 == Ctrl_T)
2264 {
2265 p = (char_u *)line_ga.ga_data;
2266 p[line_ga.ga_len] = NUL;
2267 indent = get_indent_str(p, 8);
2268 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2269add_indent:
2270 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002272 char_u *s = skipwhite(p);
2273
2274 ga_grow(&line_ga, 1);
2275 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2276 *s = ' ';
2277 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002279redraw:
2280 /* redraw the line */
2281 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002282 vcol = 0;
2283 for (p = (char_u *)line_ga.ga_data;
2284 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002286 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002288 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002290 msg_putchar(' ');
2291 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002293 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002295 msg_outtrans_len(p, 1);
2296 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 }
2298 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002299 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002300 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002301 continue;
2302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002304 if (c1 == Ctrl_D)
2305 {
2306 /* Delete one shiftwidth. */
2307 p = (char_u *)line_ga.ga_data;
2308 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002310 if (prev_char == '^')
2311 ex_keep_indent = TRUE;
2312 indent = 0;
2313 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
2315 else
2316 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002317 p[line_ga.ga_len] = NUL;
2318 indent = get_indent_str(p, 8);
2319 --indent;
2320 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002322 while (get_indent_str(p, 8) > indent)
2323 {
2324 char_u *s = skipwhite(p);
2325
2326 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2327 --line_ga.ga_len;
2328 }
2329 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002331
2332 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2333 {
2334 escaped = TRUE;
2335 continue;
2336 }
2337
2338 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2339 if (IS_SPECIAL(c1))
2340 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002342
2343 if (IS_SPECIAL(c1))
2344 c1 = '?';
2345 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002346 if (c1 == '\n')
2347 msg_putchar('\n');
2348 else if (c1 == TAB)
2349 {
2350 /* Don't use chartabsize(), 'ts' can be different */
2351 do
2352 {
2353 msg_putchar(' ');
2354 } while (++vcol % 8);
2355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002358 msg_outtrans_len(
2359 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2360 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002362 ++line_ga.ga_len;
2363 escaped = FALSE;
2364
2365 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002366 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002367
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002368 /* We are done when a NL is entered, but not when it comes after an
2369 * odd number of backslashes, that results in a NUL. */
2370 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002372 int bcount = 0;
2373
2374 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2375 ++bcount;
2376
2377 if (bcount > 0)
2378 {
2379 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2380 * "\NL", etc. */
2381 line_ga.ga_len -= (bcount + 1) / 2;
2382 pend -= (bcount + 1) / 2;
2383 pend[-1] = '\n';
2384 }
2385
2386 if ((bcount & 1) == 0)
2387 {
2388 --line_ga.ga_len;
2389 --pend;
2390 *pend = NUL;
2391 break;
2392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
2394 }
2395
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002396 --no_mapping;
2397 --allow_keys;
2398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 /* make following messages go to the next line */
2400 msg_didout = FALSE;
2401 msg_col = 0;
2402 if (msg_row < Rows - 1)
2403 ++msg_row;
2404 emsg_on_display = FALSE; /* don't want ui_delay() */
2405
2406 if (got_int)
2407 ga_clear(&line_ga);
2408
2409 return (char_u *)line_ga.ga_data;
2410}
2411
Bram Moolenaare344bea2005-09-01 20:46:49 +00002412# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2413 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414/*
2415 * Return TRUE if ccline.overstrike is on.
2416 */
2417 int
2418cmdline_overstrike()
2419{
2420 return ccline.overstrike;
2421}
2422
2423/*
2424 * Return TRUE if the cursor is at the end of the cmdline.
2425 */
2426 int
2427cmdline_at_end()
2428{
2429 return (ccline.cmdpos >= ccline.cmdlen);
2430}
2431#endif
2432
Bram Moolenaar9372a112005-12-06 19:59:18 +00002433#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434/*
2435 * Return the virtual column number at the current cursor position.
2436 * This is used by the IM code to obtain the start of the preedit string.
2437 */
2438 colnr_T
2439cmdline_getvcol_cursor()
2440{
2441 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2442 return MAXCOL;
2443
2444# ifdef FEAT_MBYTE
2445 if (has_mbyte)
2446 {
2447 colnr_T col;
2448 int i = 0;
2449
2450 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002451 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
2453 return col;
2454 }
2455 else
2456# endif
2457 return ccline.cmdpos;
2458}
2459#endif
2460
2461#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2462/*
2463 * If part of the command line is an IM preedit string, redraw it with
2464 * IM feedback attributes. The cursor position is restored after drawing.
2465 */
2466 static void
2467redrawcmd_preedit()
2468{
2469 if ((State & CMDLINE)
2470 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002471 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 && !p_imdisable
2473 && im_is_preediting())
2474 {
2475 int cmdpos = 0;
2476 int cmdspos;
2477 int old_row;
2478 int old_col;
2479 colnr_T col;
2480
2481 old_row = msg_row;
2482 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002483 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485# ifdef FEAT_MBYTE
2486 if (has_mbyte)
2487 {
2488 for (col = 0; col < preedit_start_col
2489 && cmdpos < ccline.cmdlen; ++col)
2490 {
2491 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002492 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 }
2494 }
2495 else
2496# endif
2497 {
2498 cmdspos += preedit_start_col;
2499 cmdpos += preedit_start_col;
2500 }
2501
2502 msg_row = cmdline_row + (cmdspos / (int)Columns);
2503 msg_col = cmdspos % (int)Columns;
2504 if (msg_row >= Rows)
2505 msg_row = Rows - 1;
2506
2507 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2508 {
2509 int char_len;
2510 int char_attr;
2511
2512 char_attr = im_get_feedback_attr(col);
2513 if (char_attr < 0)
2514 break; /* end of preedit string */
2515
2516# ifdef FEAT_MBYTE
2517 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002518 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 else
2520# endif
2521 char_len = 1;
2522
2523 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2524 cmdpos += char_len;
2525 }
2526
2527 msg_row = old_row;
2528 msg_col = old_col;
2529 }
2530}
2531#endif /* FEAT_XIM && FEAT_GUI_GTK */
2532
2533/*
2534 * Allocate a new command line buffer.
2535 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2536 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2537 */
2538 static void
2539alloc_cmdbuff(len)
2540 int len;
2541{
2542 /*
2543 * give some extra space to avoid having to allocate all the time
2544 */
2545 if (len < 80)
2546 len = 100;
2547 else
2548 len += 20;
2549
2550 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2551 ccline.cmdbufflen = len;
2552}
2553
2554/*
2555 * Re-allocate the command line to length len + something extra.
2556 * return FAIL for failure, OK otherwise
2557 */
2558 static int
2559realloc_cmdbuff(len)
2560 int len;
2561{
2562 char_u *p;
2563
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002564 if (len < ccline.cmdbufflen)
2565 return OK; /* no need to resize */
2566
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 p = ccline.cmdbuff;
2568 alloc_cmdbuff(len); /* will get some more */
2569 if (ccline.cmdbuff == NULL) /* out of memory */
2570 {
2571 ccline.cmdbuff = p; /* keep the old one */
2572 return FAIL;
2573 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002574 /* There isn't always a NUL after the command, but it may need to be
2575 * there, thus copy up to the NUL and add a NUL. */
2576 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2577 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002579
2580 if (ccline.xpc != NULL
2581 && ccline.xpc->xp_pattern != NULL
2582 && ccline.xpc->xp_context != EXPAND_NOTHING
2583 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2584 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002585 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002586
2587 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2588 * to point into the newly allocated memory. */
2589 if (i >= 0 && i <= ccline.cmdlen)
2590 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2591 }
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 return OK;
2594}
2595
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002596#if defined(FEAT_ARABIC) || defined(PROTO)
2597static char_u *arshape_buf = NULL;
2598
2599# if defined(EXITFREE) || defined(PROTO)
2600 void
2601free_cmdline_buf()
2602{
2603 vim_free(arshape_buf);
2604}
2605# endif
2606#endif
2607
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608/*
2609 * Draw part of the cmdline at the current cursor position. But draw stars
2610 * when cmdline_star is TRUE.
2611 */
2612 static void
2613draw_cmdline(start, len)
2614 int start;
2615 int len;
2616{
2617#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2618 int i;
2619
2620 if (cmdline_star > 0)
2621 for (i = 0; i < len; ++i)
2622 {
2623 msg_putchar('*');
2624# ifdef FEAT_MBYTE
2625 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002626 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627# endif
2628 }
2629 else
2630#endif
2631#ifdef FEAT_ARABIC
2632 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2633 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 static int buflen = 0;
2635 char_u *p;
2636 int j;
2637 int newlen = 0;
2638 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002639 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 int prev_c = 0;
2641 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002642 int u8c;
2643 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
2646 /*
2647 * Do arabic shaping into a temporary buffer. This is very
2648 * inefficient!
2649 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002650 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 {
2652 /* Re-allocate the buffer. We keep it around to avoid a lot of
2653 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002654 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002655 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002656 arshape_buf = alloc(buflen);
2657 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 return; /* out of memory */
2659 }
2660
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002661 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2662 {
2663 /* Prepend a space to draw the leading composing char on. */
2664 arshape_buf[0] = ' ';
2665 newlen = 1;
2666 }
2667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 for (j = start; j < start + len; j += mb_l)
2669 {
2670 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002671 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002672 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (ARABIC_CHAR(u8c))
2674 {
2675 /* Do Arabic shaping. */
2676 if (cmdmsg_rl)
2677 {
2678 /* displaying from right to left */
2679 pc = prev_c;
2680 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002681 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (j + mb_l >= start + len)
2683 nc = NUL;
2684 else
2685 nc = utf_ptr2char(p + mb_l);
2686 }
2687 else
2688 {
2689 /* displaying from left to right */
2690 if (j + mb_l >= start + len)
2691 pc = NUL;
2692 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002693 {
2694 int pcc[MAX_MCO];
2695
2696 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002698 pc1 = pcc[0];
2699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 nc = prev_c;
2701 }
2702 prev_c = u8c;
2703
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002704 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002706 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002707 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002709 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2710 if (u8cc[1] != 0)
2711 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002712 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 }
2715 else
2716 {
2717 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002718 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 newlen += mb_l;
2720 }
2721 }
2722
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002723 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 else
2726#endif
2727 msg_outtrans_len(ccline.cmdbuff + start, len);
2728}
2729
2730/*
2731 * Put a character on the command line. Shifts the following text to the
2732 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2733 * "c" must be printable (fit in one display cell)!
2734 */
2735 void
2736putcmdline(c, shift)
2737 int c;
2738 int shift;
2739{
2740 if (cmd_silent)
2741 return;
2742 msg_no_more = TRUE;
2743 msg_putchar(c);
2744 if (shift)
2745 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2746 msg_no_more = FALSE;
2747 cursorcmd();
2748}
2749
2750/*
2751 * Undo a putcmdline(c, FALSE).
2752 */
2753 void
2754unputcmdline()
2755{
2756 if (cmd_silent)
2757 return;
2758 msg_no_more = TRUE;
2759 if (ccline.cmdlen == ccline.cmdpos)
2760 msg_putchar(' ');
2761 else
2762 draw_cmdline(ccline.cmdpos, 1);
2763 msg_no_more = FALSE;
2764 cursorcmd();
2765}
2766
2767/*
2768 * Put the given string, of the given length, onto the command line.
2769 * If len is -1, then STRLEN() is used to calculate the length.
2770 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2771 * part will be redrawn, otherwise it will not. If this function is called
2772 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2773 * called afterwards.
2774 */
2775 int
2776put_on_cmdline(str, len, redraw)
2777 char_u *str;
2778 int len;
2779 int redraw;
2780{
2781 int retval;
2782 int i;
2783 int m;
2784 int c;
2785
2786 if (len < 0)
2787 len = (int)STRLEN(str);
2788
2789 /* Check if ccline.cmdbuff needs to be longer */
2790 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002791 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 else
2793 retval = OK;
2794 if (retval == OK)
2795 {
2796 if (!ccline.overstrike)
2797 {
2798 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2799 ccline.cmdbuff + ccline.cmdpos,
2800 (size_t)(ccline.cmdlen - ccline.cmdpos));
2801 ccline.cmdlen += len;
2802 }
2803 else
2804 {
2805#ifdef FEAT_MBYTE
2806 if (has_mbyte)
2807 {
2808 /* Count nr of characters in the new string. */
2809 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002810 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 ++m;
2812 /* Count nr of bytes in cmdline that are overwritten by these
2813 * characters. */
2814 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002815 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 --m;
2817 if (i < ccline.cmdlen)
2818 {
2819 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2820 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2821 ccline.cmdlen += ccline.cmdpos + len - i;
2822 }
2823 else
2824 ccline.cmdlen = ccline.cmdpos + len;
2825 }
2826 else
2827#endif
2828 if (ccline.cmdpos + len > ccline.cmdlen)
2829 ccline.cmdlen = ccline.cmdpos + len;
2830 }
2831 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2832 ccline.cmdbuff[ccline.cmdlen] = NUL;
2833
2834#ifdef FEAT_MBYTE
2835 if (enc_utf8)
2836 {
2837 /* When the inserted text starts with a composing character,
2838 * backup to the character before it. There could be two of them.
2839 */
2840 i = 0;
2841 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2842 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2843 {
2844 i = (*mb_head_off)(ccline.cmdbuff,
2845 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2846 ccline.cmdpos -= i;
2847 len += i;
2848 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2849 }
2850# ifdef FEAT_ARABIC
2851 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2852 {
2853 /* Check the previous character for Arabic combining pair. */
2854 i = (*mb_head_off)(ccline.cmdbuff,
2855 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2856 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2857 + ccline.cmdpos - i), c))
2858 {
2859 ccline.cmdpos -= i;
2860 len += i;
2861 }
2862 else
2863 i = 0;
2864 }
2865# endif
2866 if (i != 0)
2867 {
2868 /* Also backup the cursor position. */
2869 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2870 ccline.cmdspos -= i;
2871 msg_col -= i;
2872 if (msg_col < 0)
2873 {
2874 msg_col += Columns;
2875 --msg_row;
2876 }
2877 }
2878 }
2879#endif
2880
2881 if (redraw && !cmd_silent)
2882 {
2883 msg_no_more = TRUE;
2884 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002885 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2887 /* Avoid clearing the rest of the line too often. */
2888 if (cmdline_row != i || ccline.overstrike)
2889 msg_clr_eos();
2890 msg_no_more = FALSE;
2891 }
2892#ifdef FEAT_FKMAP
2893 /*
2894 * If we are in Farsi command mode, the character input must be in
2895 * Insert mode. So do not advance the cmdpos.
2896 */
2897 if (!cmd_fkmap)
2898#endif
2899 {
2900 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002903 if (m < 0) /* overflow, Columns or Rows at weird value */
2904 m = MAXCOL;
2905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 else
2907 m = MAXCOL;
2908 for (i = 0; i < len; ++i)
2909 {
2910 c = cmdline_charsize(ccline.cmdpos);
2911#ifdef FEAT_MBYTE
2912 /* count ">" for a double-wide char that doesn't fit. */
2913 if (has_mbyte)
2914 correct_cmdspos(ccline.cmdpos, c);
2915#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002916 /* Stop cursor at the end of the screen, but do increment the
2917 * insert position, so that entering a very long command
2918 * works, even though you can't see it. */
2919 if (ccline.cmdspos + c < m)
2920 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921#ifdef FEAT_MBYTE
2922 if (has_mbyte)
2923 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002924 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 if (c > len - i - 1)
2926 c = len - i - 1;
2927 ccline.cmdpos += c;
2928 i += c;
2929 }
2930#endif
2931 ++ccline.cmdpos;
2932 }
2933 }
2934 }
2935 if (redraw)
2936 msg_check();
2937 return retval;
2938}
2939
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002940static struct cmdline_info prev_ccline;
2941static int prev_ccline_used = FALSE;
2942
2943/*
2944 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2945 * and overwrite it. But get_cmdline_str() may need it, thus make it
2946 * available globally in prev_ccline.
2947 */
2948 static void
2949save_cmdline(ccp)
2950 struct cmdline_info *ccp;
2951{
2952 if (!prev_ccline_used)
2953 {
2954 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2955 prev_ccline_used = TRUE;
2956 }
2957 *ccp = prev_ccline;
2958 prev_ccline = ccline;
2959 ccline.cmdbuff = NULL;
2960 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002961 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002962}
2963
2964/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002965 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002966 */
2967 static void
2968restore_cmdline(ccp)
2969 struct cmdline_info *ccp;
2970{
2971 ccline = prev_ccline;
2972 prev_ccline = *ccp;
2973}
2974
Bram Moolenaar5a305422006-04-28 22:38:25 +00002975#if defined(FEAT_EVAL) || defined(PROTO)
2976/*
2977 * Save the command line into allocated memory. Returns a pointer to be
2978 * passed to restore_cmdline_alloc() later.
2979 * Returns NULL when failed.
2980 */
2981 char_u *
2982save_cmdline_alloc()
2983{
2984 struct cmdline_info *p;
2985
2986 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2987 if (p != NULL)
2988 save_cmdline(p);
2989 return (char_u *)p;
2990}
2991
2992/*
2993 * Restore the command line from the return value of save_cmdline_alloc().
2994 */
2995 void
2996restore_cmdline_alloc(p)
2997 char_u *p;
2998{
2999 if (p != NULL)
3000 {
3001 restore_cmdline((struct cmdline_info *)p);
3002 vim_free(p);
3003 }
3004}
3005#endif
3006
Bram Moolenaar8299df92004-07-10 09:47:34 +00003007/*
3008 * paste a yank register into the command line.
3009 * used by CTRL-R command in command-line mode
3010 * insert_reg() can't be used here, because special characters from the
3011 * register contents will be interpreted as commands.
3012 *
3013 * return FAIL for failure, OK otherwise
3014 */
3015 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003016cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003017 int regname;
3018 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003019 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003020{
3021 long i;
3022 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003023 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003024 int allocated;
3025 struct cmdline_info save_ccline;
3026
3027 /* check for valid regname; also accept special characters for CTRL-R in
3028 * the command line */
3029 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3030 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3031 return FAIL;
3032
3033 /* A register containing CTRL-R can cause an endless loop. Allow using
3034 * CTRL-C to break the loop. */
3035 line_breakcheck();
3036 if (got_int)
3037 return FAIL;
3038
3039#ifdef FEAT_CLIPBOARD
3040 regname = may_get_selection(regname);
3041#endif
3042
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003043 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003044 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003045 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003046 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003047 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003048 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003049 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003050
3051 if (i)
3052 {
3053 /* Got the value of a special register in "arg". */
3054 if (arg == NULL)
3055 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003056
3057 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3058 * part of the word. */
3059 p = arg;
3060 if (p_is && regname == Ctrl_W)
3061 {
3062 char_u *w;
3063 int len;
3064
3065 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003066 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003067 {
3068#ifdef FEAT_MBYTE
3069 if (has_mbyte)
3070 {
3071 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3072 if (!vim_iswordc(mb_ptr2char(w - len)))
3073 break;
3074 w -= len;
3075 }
3076 else
3077#endif
3078 {
3079 if (!vim_iswordc(w[-1]))
3080 break;
3081 --w;
3082 }
3083 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003084 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003085 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3086 p += len;
3087 }
3088
3089 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003090 if (allocated)
3091 vim_free(arg);
3092 return OK;
3093 }
3094
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003095 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003096}
3097
3098/*
3099 * Put a string on the command line.
3100 * When "literally" is TRUE, insert literally.
3101 * When "literally" is FALSE, insert as typed, but don't leave the command
3102 * line.
3103 */
3104 void
3105cmdline_paste_str(s, literally)
3106 char_u *s;
3107 int literally;
3108{
3109 int c, cv;
3110
3111 if (literally)
3112 put_on_cmdline(s, -1, TRUE);
3113 else
3114 while (*s != NUL)
3115 {
3116 cv = *s;
3117 if (cv == Ctrl_V && s[1])
3118 ++s;
3119#ifdef FEAT_MBYTE
3120 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003121 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003122 else
3123#endif
3124 c = *s++;
3125 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3126#ifdef UNIX
3127 || c == intr_char
3128#endif
3129 || (c == Ctrl_BSL && *s == Ctrl_N))
3130 stuffcharReadbuff(Ctrl_V);
3131 stuffcharReadbuff(c);
3132 }
3133}
3134
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135#ifdef FEAT_WILDMENU
3136/*
3137 * Delete characters on the command line, from "from" to the current
3138 * position.
3139 */
3140 static void
3141cmdline_del(from)
3142 int from;
3143{
3144 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3145 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3146 ccline.cmdlen -= ccline.cmdpos - from;
3147 ccline.cmdpos = from;
3148}
3149#endif
3150
3151/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003152 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 * search
3154 */
3155 void
3156redrawcmdline()
3157{
3158 if (cmd_silent)
3159 return;
3160 need_wait_return = FALSE;
3161 compute_cmdrow();
3162 redrawcmd();
3163 cursorcmd();
3164}
3165
3166 static void
3167redrawcmdprompt()
3168{
3169 int i;
3170
3171 if (cmd_silent)
3172 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003173 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 msg_putchar(ccline.cmdfirstc);
3175 if (ccline.cmdprompt != NULL)
3176 {
3177 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3178 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3179 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003180 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 --ccline.cmdindent;
3182 }
3183 else
3184 for (i = ccline.cmdindent; i > 0; --i)
3185 msg_putchar(' ');
3186}
3187
3188/*
3189 * Redraw what is currently on the command line.
3190 */
3191 void
3192redrawcmd()
3193{
3194 if (cmd_silent)
3195 return;
3196
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003197 /* when 'incsearch' is set there may be no command line while redrawing */
3198 if (ccline.cmdbuff == NULL)
3199 {
3200 windgoto(cmdline_row, 0);
3201 msg_clr_eos();
3202 return;
3203 }
3204
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 msg_start();
3206 redrawcmdprompt();
3207
3208 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3209 msg_no_more = TRUE;
3210 draw_cmdline(0, ccline.cmdlen);
3211 msg_clr_eos();
3212 msg_no_more = FALSE;
3213
3214 set_cmdspos_cursor();
3215
3216 /*
3217 * An emsg() before may have set msg_scroll. This is used in normal mode,
3218 * in cmdline mode we can reset them now.
3219 */
3220 msg_scroll = FALSE; /* next message overwrites cmdline */
3221
3222 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3223 * in cmdline mode */
3224 skip_redraw = FALSE;
3225}
3226
3227 void
3228compute_cmdrow()
3229{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003230 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 cmdline_row = Rows - 1;
3232 else
3233 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3234 + W_STATUS_HEIGHT(lastwin);
3235}
3236
3237 static void
3238cursorcmd()
3239{
3240 if (cmd_silent)
3241 return;
3242
3243#ifdef FEAT_RIGHTLEFT
3244 if (cmdmsg_rl)
3245 {
3246 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3247 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3248 if (msg_row <= 0)
3249 msg_row = Rows - 1;
3250 }
3251 else
3252#endif
3253 {
3254 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3255 msg_col = ccline.cmdspos % (int)Columns;
3256 if (msg_row >= Rows)
3257 msg_row = Rows - 1;
3258 }
3259
3260 windgoto(msg_row, msg_col);
3261#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3262 redrawcmd_preedit();
3263#endif
3264#ifdef MCH_CURSOR_SHAPE
3265 mch_update_cursor();
3266#endif
3267}
3268
3269 void
3270gotocmdline(clr)
3271 int clr;
3272{
3273 msg_start();
3274#ifdef FEAT_RIGHTLEFT
3275 if (cmdmsg_rl)
3276 msg_col = Columns - 1;
3277 else
3278#endif
3279 msg_col = 0; /* always start in column 0 */
3280 if (clr) /* clear the bottom line(s) */
3281 msg_clr_eos(); /* will reset clear_cmdline */
3282 windgoto(cmdline_row, 0);
3283}
3284
3285/*
3286 * Check the word in front of the cursor for an abbreviation.
3287 * Called when the non-id character "c" has been entered.
3288 * When an abbreviation is recognized it is removed from the text with
3289 * backspaces and the replacement string is inserted, followed by "c".
3290 */
3291 static int
3292ccheck_abbr(c)
3293 int c;
3294{
3295 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3296 return FALSE;
3297
3298 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3299}
3300
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003301#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3302 static int
3303#ifdef __BORLANDC__
3304_RTLENTRYF
3305#endif
3306sort_func_compare(s1, s2)
3307 const void *s1;
3308 const void *s2;
3309{
3310 char_u *p1 = *(char_u **)s1;
3311 char_u *p2 = *(char_u **)s2;
3312
3313 if (*p1 != '<' && *p2 == '<') return -1;
3314 if (*p1 == '<' && *p2 != '<') return 1;
3315 return STRCMP(p1, p2);
3316}
3317#endif
3318
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319/*
3320 * Return FAIL if this is not an appropriate context in which to do
3321 * completion of anything, return OK if it is (even if there are no matches).
3322 * For the caller, this means that the character is just passed through like a
3323 * normal character (instead of being expanded). This allows :s/^I^D etc.
3324 */
3325 static int
3326nextwild(xp, type, options)
3327 expand_T *xp;
3328 int type;
3329 int options; /* extra options for ExpandOne() */
3330{
3331 int i, j;
3332 char_u *p1;
3333 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 int difflen;
3335 int v;
3336
3337 if (xp->xp_numfiles == -1)
3338 {
3339 set_expand_context(xp);
3340 cmd_showtail = expand_showtail(xp);
3341 }
3342
3343 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3344 {
3345 beep_flush();
3346 return OK; /* Something illegal on command line */
3347 }
3348 if (xp->xp_context == EXPAND_NOTHING)
3349 {
3350 /* Caller can use the character as a normal char instead */
3351 return FAIL;
3352 }
3353
3354 MSG_PUTS("..."); /* show that we are busy */
3355 out_flush();
3356
3357 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003358 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
3360 if (type == WILD_NEXT || type == WILD_PREV)
3361 {
3362 /*
3363 * Get next/previous match for a previous expanded pattern.
3364 */
3365 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3366 }
3367 else
3368 {
3369 /*
3370 * Translate string into pattern and expand it.
3371 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003372 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3373 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 p2 = NULL;
3375 else
3376 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003377 int use_options = options |
3378 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE;
3379
3380 if (p_wic)
3381 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003382 p2 = ExpandOne(xp, p1,
3383 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003384 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003386 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 if (p2 != NULL && type == WILD_LONGEST)
3388 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003389 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 if (ccline.cmdbuff[i + j] == '*'
3391 || ccline.cmdbuff[i + j] == '?')
3392 break;
3393 if ((int)STRLEN(p2) < j)
3394 {
3395 vim_free(p2);
3396 p2 = NULL;
3397 }
3398 }
3399 }
3400 }
3401
3402 if (p2 != NULL && !got_int)
3403 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003404 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003405 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003407 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 xp->xp_pattern = ccline.cmdbuff + i;
3409 }
3410 else
3411 v = OK;
3412 if (v == OK)
3413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003414 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3415 &ccline.cmdbuff[ccline.cmdpos],
3416 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3417 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 ccline.cmdlen += difflen;
3419 ccline.cmdpos += difflen;
3420 }
3421 }
3422 vim_free(p2);
3423
3424 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003425 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
3427 /* When expanding a ":map" command and no matches are found, assume that
3428 * the key is supposed to be inserted literally */
3429 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3430 return FAIL;
3431
3432 if (xp->xp_numfiles <= 0 && p2 == NULL)
3433 beep_flush();
3434 else if (xp->xp_numfiles == 1)
3435 /* free expanded pattern */
3436 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3437
3438 return OK;
3439}
3440
3441/*
3442 * Do wildcard expansion on the string 'str'.
3443 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003444 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 * Return NULL for failure.
3446 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003447 * "orig" is the originally expanded string, copied to allocated memory. It
3448 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3449 * WILD_PREV "orig" should be NULL.
3450 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003451 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3452 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 *
3454 * mode = WILD_FREE: just free previously expanded matches
3455 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3456 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3457 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3458 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3459 * mode = WILD_ALL: return all matches concatenated
3460 * mode = WILD_LONGEST: return longest matched part
3461 *
3462 * options = WILD_LIST_NOTFOUND: list entries without a match
3463 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3464 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3465 * options = WILD_NO_BEEP: Don't beep for multiple matches
3466 * options = WILD_ADD_SLASH: add a slash after directory names
3467 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3468 * options = WILD_SILENT: don't print warning messages
3469 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003470 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 *
3472 * The variables xp->xp_context and xp->xp_backslash must have been set!
3473 */
3474 char_u *
3475ExpandOne(xp, str, orig, options, mode)
3476 expand_T *xp;
3477 char_u *str;
3478 char_u *orig; /* allocated copy of original of expanded string */
3479 int options;
3480 int mode;
3481{
3482 char_u *ss = NULL;
3483 static int findex;
3484 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003485 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 int i;
3487 long_u len;
3488 int non_suf_match; /* number without matching suffix */
3489
3490 /*
3491 * first handle the case of using an old match
3492 */
3493 if (mode == WILD_NEXT || mode == WILD_PREV)
3494 {
3495 if (xp->xp_numfiles > 0)
3496 {
3497 if (mode == WILD_PREV)
3498 {
3499 if (findex == -1)
3500 findex = xp->xp_numfiles;
3501 --findex;
3502 }
3503 else /* mode == WILD_NEXT */
3504 ++findex;
3505
3506 /*
3507 * When wrapping around, return the original string, set findex to
3508 * -1.
3509 */
3510 if (findex < 0)
3511 {
3512 if (orig_save == NULL)
3513 findex = xp->xp_numfiles - 1;
3514 else
3515 findex = -1;
3516 }
3517 if (findex >= xp->xp_numfiles)
3518 {
3519 if (orig_save == NULL)
3520 findex = 0;
3521 else
3522 findex = -1;
3523 }
3524#ifdef FEAT_WILDMENU
3525 if (p_wmnu)
3526 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3527 findex, cmd_showtail);
3528#endif
3529 if (findex == -1)
3530 return vim_strsave(orig_save);
3531 return vim_strsave(xp->xp_files[findex]);
3532 }
3533 else
3534 return NULL;
3535 }
3536
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003537 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3539 {
3540 FreeWild(xp->xp_numfiles, xp->xp_files);
3541 xp->xp_numfiles = -1;
3542 vim_free(orig_save);
3543 orig_save = NULL;
3544 }
3545 findex = 0;
3546
3547 if (mode == WILD_FREE) /* only release file name */
3548 return NULL;
3549
3550 if (xp->xp_numfiles == -1)
3551 {
3552 vim_free(orig_save);
3553 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003554 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
3556 /*
3557 * Do the expansion.
3558 */
3559 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3560 options) == FAIL)
3561 {
3562#ifdef FNAME_ILLEGAL
3563 /* Illegal file name has been silently skipped. But when there
3564 * are wildcards, the real problem is that there was no match,
3565 * causing the pattern to be added, which has illegal characters.
3566 */
3567 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3568 EMSG2(_(e_nomatch2), str);
3569#endif
3570 }
3571 else if (xp->xp_numfiles == 0)
3572 {
3573 if (!(options & WILD_SILENT))
3574 EMSG2(_(e_nomatch2), str);
3575 }
3576 else
3577 {
3578 /* Escape the matches for use on the command line. */
3579 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3580
3581 /*
3582 * Check for matching suffixes in file names.
3583 */
3584 if (mode != WILD_ALL && mode != WILD_LONGEST)
3585 {
3586 if (xp->xp_numfiles)
3587 non_suf_match = xp->xp_numfiles;
3588 else
3589 non_suf_match = 1;
3590 if ((xp->xp_context == EXPAND_FILES
3591 || xp->xp_context == EXPAND_DIRECTORIES)
3592 && xp->xp_numfiles > 1)
3593 {
3594 /*
3595 * More than one match; check suffix.
3596 * The files will have been sorted on matching suffix in
3597 * expand_wildcards, only need to check the first two.
3598 */
3599 non_suf_match = 0;
3600 for (i = 0; i < 2; ++i)
3601 if (match_suffix(xp->xp_files[i]))
3602 ++non_suf_match;
3603 }
3604 if (non_suf_match != 1)
3605 {
3606 /* Can we ever get here unless it's while expanding
3607 * interactively? If not, we can get rid of this all
3608 * together. Don't really want to wait for this message
3609 * (and possibly have to hit return to continue!).
3610 */
3611 if (!(options & WILD_SILENT))
3612 EMSG(_(e_toomany));
3613 else if (!(options & WILD_NO_BEEP))
3614 beep_flush();
3615 }
3616 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3617 ss = vim_strsave(xp->xp_files[0]);
3618 }
3619 }
3620 }
3621
3622 /* Find longest common part */
3623 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3624 {
3625 for (len = 0; xp->xp_files[0][len]; ++len)
3626 {
3627 for (i = 0; i < xp->xp_numfiles; ++i)
3628 {
3629#ifdef CASE_INSENSITIVE_FILENAME
3630 if (xp->xp_context == EXPAND_DIRECTORIES
3631 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003632 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 || xp->xp_context == EXPAND_BUFFERS)
3634 {
3635 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3636 TOLOWER_LOC(xp->xp_files[0][len]))
3637 break;
3638 }
3639 else
3640#endif
3641 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3642 break;
3643 }
3644 if (i < xp->xp_numfiles)
3645 {
3646 if (!(options & WILD_NO_BEEP))
3647 vim_beep();
3648 break;
3649 }
3650 }
3651 ss = alloc((unsigned)len + 1);
3652 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003653 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 findex = -1; /* next p_wc gets first one */
3655 }
3656
3657 /* Concatenate all matching names */
3658 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3659 {
3660 len = 0;
3661 for (i = 0; i < xp->xp_numfiles; ++i)
3662 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3663 ss = lalloc(len, TRUE);
3664 if (ss != NULL)
3665 {
3666 *ss = NUL;
3667 for (i = 0; i < xp->xp_numfiles; ++i)
3668 {
3669 STRCAT(ss, xp->xp_files[i]);
3670 if (i != xp->xp_numfiles - 1)
3671 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3672 }
3673 }
3674 }
3675
3676 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3677 ExpandCleanup(xp);
3678
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003679 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003680 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003681 vim_free(orig);
3682
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 return ss;
3684}
3685
3686/*
3687 * Prepare an expand structure for use.
3688 */
3689 void
3690ExpandInit(xp)
3691 expand_T *xp;
3692{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003693 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003694 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003696#ifndef BACKSLASH_IN_FILENAME
3697 xp->xp_shell = FALSE;
3698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 xp->xp_numfiles = -1;
3700 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003701#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3702 xp->xp_arg = NULL;
3703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704}
3705
3706/*
3707 * Cleanup an expand structure after use.
3708 */
3709 void
3710ExpandCleanup(xp)
3711 expand_T *xp;
3712{
3713 if (xp->xp_numfiles >= 0)
3714 {
3715 FreeWild(xp->xp_numfiles, xp->xp_files);
3716 xp->xp_numfiles = -1;
3717 }
3718}
3719
3720 void
3721ExpandEscape(xp, str, numfiles, files, options)
3722 expand_T *xp;
3723 char_u *str;
3724 int numfiles;
3725 char_u **files;
3726 int options;
3727{
3728 int i;
3729 char_u *p;
3730
3731 /*
3732 * May change home directory back to "~"
3733 */
3734 if (options & WILD_HOME_REPLACE)
3735 tilde_replace(str, numfiles, files);
3736
3737 if (options & WILD_ESCAPE)
3738 {
3739 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003740 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003741 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 || xp->xp_context == EXPAND_BUFFERS
3743 || xp->xp_context == EXPAND_DIRECTORIES)
3744 {
3745 /*
3746 * Insert a backslash into a file name before a space, \, %, #
3747 * and wildmatch characters, except '~'.
3748 */
3749 for (i = 0; i < numfiles; ++i)
3750 {
3751 /* for ":set path=" we need to escape spaces twice */
3752 if (xp->xp_backslash == XP_BS_THREE)
3753 {
3754 p = vim_strsave_escaped(files[i], (char_u *)" ");
3755 if (p != NULL)
3756 {
3757 vim_free(files[i]);
3758 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003759#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 p = vim_strsave_escaped(files[i], (char_u *)" ");
3761 if (p != NULL)
3762 {
3763 vim_free(files[i]);
3764 files[i] = p;
3765 }
3766#endif
3767 }
3768 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003769#ifdef BACKSLASH_IN_FILENAME
3770 p = vim_strsave_fnameescape(files[i], FALSE);
3771#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003772 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 if (p != NULL)
3775 {
3776 vim_free(files[i]);
3777 files[i] = p;
3778 }
3779
3780 /* If 'str' starts with "\~", replace "~" at start of
3781 * files[i] with "\~". */
3782 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003783 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 }
3785 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003786
3787 /* If the first file starts with a '+' escape it. Otherwise it
3788 * could be seen as "+cmd". */
3789 if (*files[0] == '+')
3790 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792 else if (xp->xp_context == EXPAND_TAGS)
3793 {
3794 /*
3795 * Insert a backslash before characters in a tag name that
3796 * would terminate the ":tag" command.
3797 */
3798 for (i = 0; i < numfiles; ++i)
3799 {
3800 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3801 if (p != NULL)
3802 {
3803 vim_free(files[i]);
3804 files[i] = p;
3805 }
3806 }
3807 }
3808 }
3809}
3810
3811/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003812 * Escape special characters in "fname" for when used as a file name argument
3813 * after a Vim command, or, when "shell" is non-zero, a shell command.
3814 * Returns the result in allocated memory.
3815 */
3816 char_u *
3817vim_strsave_fnameescape(fname, shell)
3818 char_u *fname;
3819 int shell;
3820{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003821 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003822#ifdef BACKSLASH_IN_FILENAME
3823 char_u buf[20];
3824 int j = 0;
3825
3826 /* Don't escape '[' and '{' if they are in 'isfname'. */
3827 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3828 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3829 buf[j++] = *p;
3830 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003831 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003832#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003833 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3834 if (shell && csh_like_shell() && p != NULL)
3835 {
3836 char_u *s;
3837
3838 /* For csh and similar shells need to put two backslashes before '!'.
3839 * One is taken by Vim, one by the shell. */
3840 s = vim_strsave_escaped(p, (char_u *)"!");
3841 vim_free(p);
3842 p = s;
3843 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003844#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003845
3846 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3847 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003848 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003849 escape_fname(&p);
3850
3851 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003852}
3853
3854/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003855 * Put a backslash before the file name in "pp", which is in allocated memory.
3856 */
3857 static void
3858escape_fname(pp)
3859 char_u **pp;
3860{
3861 char_u *p;
3862
3863 p = alloc((unsigned)(STRLEN(*pp) + 2));
3864 if (p != NULL)
3865 {
3866 p[0] = '\\';
3867 STRCPY(p + 1, *pp);
3868 vim_free(*pp);
3869 *pp = p;
3870 }
3871}
3872
3873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 * For each file name in files[num_files]:
3875 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3876 */
3877 void
3878tilde_replace(orig_pat, num_files, files)
3879 char_u *orig_pat;
3880 int num_files;
3881 char_u **files;
3882{
3883 int i;
3884 char_u *p;
3885
3886 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3887 {
3888 for (i = 0; i < num_files; ++i)
3889 {
3890 p = home_replace_save(NULL, files[i]);
3891 if (p != NULL)
3892 {
3893 vim_free(files[i]);
3894 files[i] = p;
3895 }
3896 }
3897 }
3898}
3899
3900/*
3901 * Show all matches for completion on the command line.
3902 * Returns EXPAND_NOTHING when the character that triggered expansion should
3903 * be inserted like a normal character.
3904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 static int
3906showmatches(xp, wildmenu)
3907 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003908 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
3910#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3911 int num_files;
3912 char_u **files_found;
3913 int i, j, k;
3914 int maxlen;
3915 int lines;
3916 int columns;
3917 char_u *p;
3918 int lastlen;
3919 int attr;
3920 int showtail;
3921
3922 if (xp->xp_numfiles == -1)
3923 {
3924 set_expand_context(xp);
3925 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3926 &num_files, &files_found);
3927 showtail = expand_showtail(xp);
3928 if (i != EXPAND_OK)
3929 return i;
3930
3931 }
3932 else
3933 {
3934 num_files = xp->xp_numfiles;
3935 files_found = xp->xp_files;
3936 showtail = cmd_showtail;
3937 }
3938
3939#ifdef FEAT_WILDMENU
3940 if (!wildmenu)
3941 {
3942#endif
3943 msg_didany = FALSE; /* lines_left will be set */
3944 msg_start(); /* prepare for paging */
3945 msg_putchar('\n');
3946 out_flush();
3947 cmdline_row = msg_row;
3948 msg_didany = FALSE; /* lines_left will be set again */
3949 msg_start(); /* prepare for paging */
3950#ifdef FEAT_WILDMENU
3951 }
3952#endif
3953
3954 if (got_int)
3955 got_int = FALSE; /* only int. the completion, not the cmd line */
3956#ifdef FEAT_WILDMENU
3957 else if (wildmenu)
3958 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3959#endif
3960 else
3961 {
3962 /* find the length of the longest file name */
3963 maxlen = 0;
3964 for (i = 0; i < num_files; ++i)
3965 {
3966 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003967 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 || xp->xp_context == EXPAND_BUFFERS))
3969 {
3970 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3971 j = vim_strsize(NameBuff);
3972 }
3973 else
3974 j = vim_strsize(L_SHOWFILE(i));
3975 if (j > maxlen)
3976 maxlen = j;
3977 }
3978
3979 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3980 lines = num_files;
3981 else
3982 {
3983 /* compute the number of columns and lines for the listing */
3984 maxlen += 2; /* two spaces between file names */
3985 columns = ((int)Columns + 2) / maxlen;
3986 if (columns < 1)
3987 columns = 1;
3988 lines = (num_files + columns - 1) / columns;
3989 }
3990
3991 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3992
3993 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3994 {
3995 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3996 msg_clr_eos();
3997 msg_advance(maxlen - 3);
3998 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3999 }
4000
4001 /* list the files line by line */
4002 for (i = 0; i < lines; ++i)
4003 {
4004 lastlen = 999;
4005 for (k = i; k < num_files; k += lines)
4006 {
4007 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4008 {
4009 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4010 p = files_found[k] + STRLEN(files_found[k]) + 1;
4011 msg_advance(maxlen + 1);
4012 msg_puts(p);
4013 msg_advance(maxlen + 3);
4014 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4015 break;
4016 }
4017 for (j = maxlen - lastlen; --j >= 0; )
4018 msg_putchar(' ');
4019 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004020 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 || xp->xp_context == EXPAND_BUFFERS)
4022 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004023 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004024 if (xp->xp_numfiles != -1)
4025 {
4026 char_u *halved_slash;
4027 char_u *exp_path;
4028
4029 /* Expansion was done before and special characters
4030 * were escaped, need to halve backslashes. Also
4031 * $HOME has been replaced with ~/. */
4032 exp_path = expand_env_save_opt(files_found[k], TRUE);
4033 halved_slash = backslash_halve_save(
4034 exp_path != NULL ? exp_path : files_found[k]);
4035 j = mch_isdir(halved_slash != NULL ? halved_slash
4036 : files_found[k]);
4037 vim_free(exp_path);
4038 vim_free(halved_slash);
4039 }
4040 else
4041 /* Expansion was done here, file names are literal. */
4042 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (showtail)
4044 p = L_SHOWFILE(k);
4045 else
4046 {
4047 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4048 TRUE);
4049 p = NameBuff;
4050 }
4051 }
4052 else
4053 {
4054 j = FALSE;
4055 p = L_SHOWFILE(k);
4056 }
4057 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4058 }
4059 if (msg_col > 0) /* when not wrapped around */
4060 {
4061 msg_clr_eos();
4062 msg_putchar('\n');
4063 }
4064 out_flush(); /* show one line at a time */
4065 if (got_int)
4066 {
4067 got_int = FALSE;
4068 break;
4069 }
4070 }
4071
4072 /*
4073 * we redraw the command below the lines that we have just listed
4074 * This is a bit tricky, but it saves a lot of screen updating.
4075 */
4076 cmdline_row = msg_row; /* will put it back later */
4077 }
4078
4079 if (xp->xp_numfiles == -1)
4080 FreeWild(num_files, files_found);
4081
4082 return EXPAND_OK;
4083}
4084
4085/*
4086 * Private gettail for showmatches() (and win_redr_status_matches()):
4087 * Find tail of file name path, but ignore trailing "/".
4088 */
4089 char_u *
4090sm_gettail(s)
4091 char_u *s;
4092{
4093 char_u *p;
4094 char_u *t = s;
4095 int had_sep = FALSE;
4096
4097 for (p = s; *p != NUL; )
4098 {
4099 if (vim_ispathsep(*p)
4100#ifdef BACKSLASH_IN_FILENAME
4101 && !rem_backslash(p)
4102#endif
4103 )
4104 had_sep = TRUE;
4105 else if (had_sep)
4106 {
4107 t = p;
4108 had_sep = FALSE;
4109 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004110 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
4112 return t;
4113}
4114
4115/*
4116 * Return TRUE if we only need to show the tail of completion matches.
4117 * When not completing file names or there is a wildcard in the path FALSE is
4118 * returned.
4119 */
4120 static int
4121expand_showtail(xp)
4122 expand_T *xp;
4123{
4124 char_u *s;
4125 char_u *end;
4126
4127 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004128 if (xp->xp_context != EXPAND_FILES
4129 && xp->xp_context != EXPAND_SHELLCMD
4130 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 return FALSE;
4132
4133 end = gettail(xp->xp_pattern);
4134 if (end == xp->xp_pattern) /* there is no path separator */
4135 return FALSE;
4136
4137 for (s = xp->xp_pattern; s < end; s++)
4138 {
4139 /* Skip escaped wildcards. Only when the backslash is not a path
4140 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4141 if (rem_backslash(s))
4142 ++s;
4143 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4144 return FALSE;
4145 }
4146 return TRUE;
4147}
4148
4149/*
4150 * Prepare a string for expansion.
4151 * When expanding file names: The string will be used with expand_wildcards().
4152 * Copy the file name into allocated memory and add a '*' at the end.
4153 * When expanding other names: The string will be used with regcomp(). Copy
4154 * the name into allocated memory and prepend "^".
4155 */
4156 char_u *
4157addstar(fname, len, context)
4158 char_u *fname;
4159 int len;
4160 int context; /* EXPAND_FILES etc. */
4161{
4162 char_u *retval;
4163 int i, j;
4164 int new_len;
4165 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004166 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004168 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004169 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004170 && context != EXPAND_SHELLCMD
4171 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 {
4173 /*
4174 * Matching will be done internally (on something other than files).
4175 * So we convert the file-matching-type wildcards into our kind for
4176 * use with vim_regcomp(). First work out how long it will be:
4177 */
4178
4179 /* For help tags the translation is done in find_help_tags().
4180 * For a tag pattern starting with "/" no translation is needed. */
4181 if (context == EXPAND_HELP
4182 || context == EXPAND_COLORS
4183 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004184 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004185 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 || (context == EXPAND_TAGS && fname[0] == '/'))
4187 retval = vim_strnsave(fname, len);
4188 else
4189 {
4190 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4191 for (i = 0; i < len; i++)
4192 {
4193 if (fname[i] == '*' || fname[i] == '~')
4194 new_len++; /* '*' needs to be replaced by ".*"
4195 '~' needs to be replaced by "\~" */
4196
4197 /* Buffer names are like file names. "." should be literal */
4198 if (context == EXPAND_BUFFERS && fname[i] == '.')
4199 new_len++; /* "." becomes "\." */
4200
4201 /* Custom expansion takes care of special things, match
4202 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004203 if ((context == EXPAND_USER_DEFINED
4204 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 new_len++; /* '\' becomes "\\" */
4206 }
4207 retval = alloc(new_len);
4208 if (retval != NULL)
4209 {
4210 retval[0] = '^';
4211 j = 1;
4212 for (i = 0; i < len; i++, j++)
4213 {
4214 /* Skip backslash. But why? At least keep it for custom
4215 * expansion. */
4216 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004217 && context != EXPAND_USER_LIST
4218 && fname[i] == '\\'
4219 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 break;
4221
4222 switch (fname[i])
4223 {
4224 case '*': retval[j++] = '.';
4225 break;
4226 case '~': retval[j++] = '\\';
4227 break;
4228 case '?': retval[j] = '.';
4229 continue;
4230 case '.': if (context == EXPAND_BUFFERS)
4231 retval[j++] = '\\';
4232 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004233 case '\\': if (context == EXPAND_USER_DEFINED
4234 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 retval[j++] = '\\';
4236 break;
4237 }
4238 retval[j] = fname[i];
4239 }
4240 retval[j] = NUL;
4241 }
4242 }
4243 }
4244 else
4245 {
4246 retval = alloc(len + 4);
4247 if (retval != NULL)
4248 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004249 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004252 * Don't add a star to *, ~, ~user, $var or `cmd`.
4253 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 * ~ would be at the start of the file name, but not the tail.
4255 * $ could be anywhere in the tail.
4256 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004257 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 */
4259 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004260 ends_in_star = (len > 0 && retval[len - 1] == '*');
4261#ifndef BACKSLASH_IN_FILENAME
4262 for (i = len - 2; i >= 0; --i)
4263 {
4264 if (retval[i] != '\\')
4265 break;
4266 ends_in_star = !ends_in_star;
4267 }
4268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004270 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 && vim_strchr(tail, '$') == NULL
4272 && vim_strchr(retval, '`') == NULL)
4273 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004274 else if (len > 0 && retval[len - 1] == '$')
4275 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 retval[len] = NUL;
4277 }
4278 }
4279 return retval;
4280}
4281
4282/*
4283 * Must parse the command line so far to work out what context we are in.
4284 * Completion can then be done based on that context.
4285 * This routine sets the variables:
4286 * xp->xp_pattern The start of the pattern to be expanded within
4287 * the command line (ends at the cursor).
4288 * xp->xp_context The type of thing to expand. Will be one of:
4289 *
4290 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4291 * the command line, like an unknown command. Caller
4292 * should beep.
4293 * EXPAND_NOTHING Unrecognised context for completion, use char like
4294 * a normal char, rather than for completion. eg
4295 * :s/^I/
4296 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4297 * it.
4298 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4299 * EXPAND_FILES After command with XFILE set, or after setting
4300 * with P_EXPAND set. eg :e ^I, :w>>^I
4301 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4302 * when we know only directories are of interest. eg
4303 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004304 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4306 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4307 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4308 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4309 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4310 * EXPAND_EVENTS Complete event names
4311 * EXPAND_SYNTAX Complete :syntax command arguments
4312 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4313 * EXPAND_AUGROUP Complete autocommand group names
4314 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4315 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4316 * eg :unmap a^I , :cunab x^I
4317 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4318 * eg :call sub^I
4319 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4320 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4321 * names in expressions, eg :while s^I
4322 * EXPAND_ENV_VARS Complete environment variable names
4323 */
4324 static void
4325set_expand_context(xp)
4326 expand_T *xp;
4327{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004328 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 if (ccline.cmdfirstc != ':'
4330#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004331 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004332 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333#endif
4334 )
4335 {
4336 xp->xp_context = EXPAND_NOTHING;
4337 return;
4338 }
4339 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4340}
4341
4342 void
4343set_cmd_context(xp, str, len, col)
4344 expand_T *xp;
4345 char_u *str; /* start of command line */
4346 int len; /* length of command line (excl. NUL) */
4347 int col; /* position of cursor */
4348{
4349 int old_char = NUL;
4350 char_u *nextcomm;
4351
4352 /*
4353 * Avoid a UMR warning from Purify, only save the character if it has been
4354 * written before.
4355 */
4356 if (col < len)
4357 old_char = str[col];
4358 str[col] = NUL;
4359 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004360
4361#ifdef FEAT_EVAL
4362 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004363 {
4364# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004365 /* pass CMD_SIZE because there is no real command */
4366 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004367# endif
4368 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004369 else if (ccline.input_fn)
4370 {
4371 xp->xp_context = ccline.xp_context;
4372 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004373# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004374 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004375# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004376 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004377 else
4378#endif
4379 while (nextcomm != NULL)
4380 nextcomm = set_one_cmd_context(xp, nextcomm);
4381
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 str[col] = old_char;
4383}
4384
4385/*
4386 * Expand the command line "str" from context "xp".
4387 * "xp" must have been set by set_cmd_context().
4388 * xp->xp_pattern points into "str", to where the text that is to be expanded
4389 * starts.
4390 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4391 * cursor.
4392 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4393 * key that triggered expansion literally.
4394 * Returns EXPAND_OK otherwise.
4395 */
4396 int
4397expand_cmdline(xp, str, col, matchcount, matches)
4398 expand_T *xp;
4399 char_u *str; /* start of command line */
4400 int col; /* position of cursor */
4401 int *matchcount; /* return: nr of matches */
4402 char_u ***matches; /* return: array of pointers to matches */
4403{
4404 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004405 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406
4407 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4408 {
4409 beep_flush();
4410 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4411 }
4412 if (xp->xp_context == EXPAND_NOTHING)
4413 {
4414 /* Caller can use the character as a normal char instead */
4415 return EXPAND_NOTHING;
4416 }
4417
4418 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004419 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4420 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 if (file_str == NULL)
4422 return EXPAND_UNSUCCESSFUL;
4423
Bram Moolenaar94950a92010-12-02 16:01:29 +01004424 if (p_wic)
4425 options += WILD_ICASE;
4426
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004428 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
4430 *matchcount = 0;
4431 *matches = NULL;
4432 }
4433 vim_free(file_str);
4434
4435 return EXPAND_OK;
4436}
4437
4438#ifdef FEAT_MULTI_LANG
4439/*
4440 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4441 */
4442static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4443
4444 static void
4445cleanup_help_tags(num_file, file)
4446 int num_file;
4447 char_u **file;
4448{
4449 int i, j;
4450 int len;
4451
4452 for (i = 0; i < num_file; ++i)
4453 {
4454 len = (int)STRLEN(file[i]) - 3;
4455 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4456 {
4457 /* Sorting on priority means the same item in another language may
4458 * be anywhere. Search all items for a match up to the "@en". */
4459 for (j = 0; j < num_file; ++j)
4460 if (j != i
4461 && (int)STRLEN(file[j]) == len + 3
4462 && STRNCMP(file[i], file[j], len + 1) == 0)
4463 break;
4464 if (j == num_file)
4465 file[i][len] = NUL;
4466 }
4467 }
4468}
4469#endif
4470
4471/*
4472 * Do the expansion based on xp->xp_context and "pat".
4473 */
4474 static int
4475ExpandFromContext(xp, pat, num_file, file, options)
4476 expand_T *xp;
4477 char_u *pat;
4478 int *num_file;
4479 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004480 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481{
4482#ifdef FEAT_CMDL_COMPL
4483 regmatch_T regmatch;
4484#endif
4485 int ret;
4486 int flags;
4487
4488 flags = EW_DIR; /* include directories */
4489 if (options & WILD_LIST_NOTFOUND)
4490 flags |= EW_NOTFOUND;
4491 if (options & WILD_ADD_SLASH)
4492 flags |= EW_ADDSLASH;
4493 if (options & WILD_KEEP_ALL)
4494 flags |= EW_KEEPALL;
4495 if (options & WILD_SILENT)
4496 flags |= EW_SILENT;
4497
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004498 if (xp->xp_context == EXPAND_FILES
4499 || xp->xp_context == EXPAND_DIRECTORIES
4500 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 {
4502 /*
4503 * Expand file or directory names.
4504 */
4505 int free_pat = FALSE;
4506 int i;
4507
4508 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4509 * space */
4510 if (xp->xp_backslash != XP_BS_NONE)
4511 {
4512 free_pat = TRUE;
4513 pat = vim_strsave(pat);
4514 for (i = 0; pat[i]; ++i)
4515 if (pat[i] == '\\')
4516 {
4517 if (xp->xp_backslash == XP_BS_THREE
4518 && pat[i + 1] == '\\'
4519 && pat[i + 2] == '\\'
4520 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004521 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 if (xp->xp_backslash == XP_BS_ONE
4523 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004524 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526 }
4527
4528 if (xp->xp_context == EXPAND_FILES)
4529 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004530 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4531 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 else
4533 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004534 if (options & WILD_ICASE)
4535 flags |= EW_ICASE;
4536
Bram Moolenaard7834d32009-12-02 16:14:36 +00004537 /* Expand wildcards, supporting %:h and the like. */
4538 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (free_pat)
4540 vim_free(pat);
4541 return ret;
4542 }
4543
4544 *file = (char_u **)"";
4545 *num_file = 0;
4546 if (xp->xp_context == EXPAND_HELP)
4547 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004548 /* With an empty argument we would get all the help tags, which is
4549 * very slow. Get matches for "help" instead. */
4550 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4551 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 {
4553#ifdef FEAT_MULTI_LANG
4554 cleanup_help_tags(*num_file, *file);
4555#endif
4556 return OK;
4557 }
4558 return FAIL;
4559 }
4560
4561#ifndef FEAT_CMDL_COMPL
4562 return FAIL;
4563#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004564 if (xp->xp_context == EXPAND_SHELLCMD)
4565 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (xp->xp_context == EXPAND_OLD_SETTING)
4567 return ExpandOldSetting(num_file, file);
4568 if (xp->xp_context == EXPAND_BUFFERS)
4569 return ExpandBufnames(pat, num_file, file, options);
4570 if (xp->xp_context == EXPAND_TAGS
4571 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4572 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4573 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004574 {
4575 char *directories[] = {"colors", NULL};
4576 return ExpandRTDir(pat, num_file, file, directories);
4577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004579 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004580 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004581 return ExpandRTDir(pat, num_file, file, directories);
4582 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004583 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004584 {
4585 char *directories[] = {"syntax", NULL};
4586 return ExpandRTDir(pat, num_file, file, directories);
4587 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004588 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004589 {
4590 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4591 return ExpandRTDir(pat, num_file, file, directories);
4592 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004593# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4594 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004595 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004596# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
4598 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4599 if (regmatch.regprog == NULL)
4600 return FAIL;
4601
4602 /* set ignore-case according to p_ic, p_scs and pat */
4603 regmatch.rm_ic = ignorecase(pat);
4604
4605 if (xp->xp_context == EXPAND_SETTINGS
4606 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4607 ret = ExpandSettings(xp, &regmatch, num_file, file);
4608 else if (xp->xp_context == EXPAND_MAPPINGS)
4609 ret = ExpandMappings(&regmatch, num_file, file);
4610# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4611 else if (xp->xp_context == EXPAND_USER_DEFINED)
4612 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4613# endif
4614 else
4615 {
4616 static struct expgen
4617 {
4618 int context;
4619 char_u *((*func)__ARGS((expand_T *, int)));
4620 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004621 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 } tab[] =
4623 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004624 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4625 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004627 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4628 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4629 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4630 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631#endif
4632#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004633 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4634 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4635 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4636 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#endif
4638#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004639 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4640 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641#endif
4642#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004643 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004645 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004647 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4648 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004650#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004651 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004652#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004653#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004654 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004655#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004656#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004657 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4660 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004661 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4662 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004664 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 };
4666 int i;
4667
4668 /*
4669 * Find a context in the table and call the ExpandGeneric() with the
4670 * right function to do the expansion.
4671 */
4672 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004673 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 if (xp->xp_context == tab[i].context)
4675 {
4676 if (tab[i].ic)
4677 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004678 ret = ExpandGeneric(xp, &regmatch, num_file, file,
4679 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 break;
4681 }
4682 }
4683
4684 vim_free(regmatch.regprog);
4685
4686 return ret;
4687#endif /* FEAT_CMDL_COMPL */
4688}
4689
4690#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4691/*
4692 * Expand a list of names.
4693 *
4694 * Generic function for command line completion. It calls a function to
4695 * obtain strings, one by one. The strings are matched against a regexp
4696 * program. Matching strings are copied into an array, which is returned.
4697 *
4698 * Returns OK when no problems encountered, FAIL for error (out of memory).
4699 */
4700 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004701ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 expand_T *xp;
4703 regmatch_T *regmatch;
4704 int *num_file;
4705 char_u ***file;
4706 char_u *((*func)__ARGS((expand_T *, int)));
4707 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004708 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
4710 int i;
4711 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004712 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 char_u *str;
4714
4715 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004716 * round == 0: count the number of matching names
4717 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004719 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
4721 for (i = 0; ; ++i)
4722 {
4723 str = (*func)(xp, i);
4724 if (str == NULL) /* end of list */
4725 break;
4726 if (*str == NUL) /* skip empty strings */
4727 continue;
4728
4729 if (vim_regexec(regmatch, str, (colnr_T)0))
4730 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004731 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004733 if (escaped)
4734 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4735 else
4736 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 (*file)[count] = str;
4738#ifdef FEAT_MENU
4739 if (func == get_menu_names && str != NULL)
4740 {
4741 /* test for separator added by get_menu_names() */
4742 str += STRLEN(str) - 1;
4743 if (*str == '\001')
4744 *str = '.';
4745 }
4746#endif
4747 }
4748 ++count;
4749 }
4750 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004751 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 {
4753 if (count == 0)
4754 return OK;
4755 *num_file = count;
4756 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4757 if (*file == NULL)
4758 {
4759 *file = (char_u **)"";
4760 return FAIL;
4761 }
4762 count = 0;
4763 }
4764 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004765
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004766 /* Sort the results. Keep menu's in the specified order. */
4767 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004768 {
4769 if (xp->xp_context == EXPAND_EXPRESSION
4770 || xp->xp_context == EXPAND_FUNCTIONS
4771 || xp->xp_context == EXPAND_USER_FUNC)
4772 /* <SNR> functions should be sorted to the end. */
4773 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4774 sort_func_compare);
4775 else
4776 sort_strings(*file, *num_file);
4777 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004778
Bram Moolenaar4f688582007-07-24 12:34:30 +00004779#ifdef FEAT_CMDL_COMPL
4780 /* Reset the variables used for special highlight names expansion, so that
4781 * they don't show up when getting normal highlight names by ID. */
4782 reset_expand_highlight();
4783#endif
4784
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 return OK;
4786}
4787
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004788/*
4789 * Complete a shell command.
4790 * Returns FAIL or OK;
4791 */
4792 static int
4793expand_shellcmd(filepat, num_file, file, flagsarg)
4794 char_u *filepat; /* pattern to match with command names */
4795 int *num_file; /* return: number of matches */
4796 char_u ***file; /* return: array with matches */
4797 int flagsarg; /* EW_ flags */
4798{
4799 char_u *pat;
4800 int i;
4801 char_u *path;
4802 int mustfree = FALSE;
4803 garray_T ga;
4804 char_u *buf = alloc(MAXPATHL);
4805 size_t l;
4806 char_u *s, *e;
4807 int flags = flagsarg;
4808 int ret;
4809
4810 if (buf == NULL)
4811 return FAIL;
4812
4813 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4814 * space */
4815 pat = vim_strsave(filepat);
4816 for (i = 0; pat[i]; ++i)
4817 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004818 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004819
4820 flags |= EW_FILE | EW_EXEC;
4821
4822 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004823 if (mch_isFullName(pat))
4824 path = (char_u *)" ";
4825 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004826 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4827 path = (char_u *)".";
4828 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004829 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004830 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004831 if (path == NULL)
4832 path = (char_u *)"";
4833 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004834
4835 /*
4836 * Go over all directories in $PATH. Expand matches in that directory and
4837 * collect them in "ga".
4838 */
4839 ga_init2(&ga, (int)sizeof(char *), 10);
4840 for (s = path; *s != NUL; s = e)
4841 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004842 if (*s == ' ')
4843 ++s; /* Skip space used for absolute path name. */
4844
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004845#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4846 e = vim_strchr(s, ';');
4847#else
4848 e = vim_strchr(s, ':');
4849#endif
4850 if (e == NULL)
4851 e = s + STRLEN(s);
4852
4853 l = e - s;
4854 if (l > MAXPATHL - 5)
4855 break;
4856 vim_strncpy(buf, s, l);
4857 add_pathsep(buf);
4858 l = STRLEN(buf);
4859 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4860
4861 /* Expand matches in one directory of $PATH. */
4862 ret = expand_wildcards(1, &buf, num_file, file, flags);
4863 if (ret == OK)
4864 {
4865 if (ga_grow(&ga, *num_file) == FAIL)
4866 FreeWild(*num_file, *file);
4867 else
4868 {
4869 for (i = 0; i < *num_file; ++i)
4870 {
4871 s = (*file)[i];
4872 if (STRLEN(s) > l)
4873 {
4874 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004875 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004876 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4877 }
4878 else
4879 vim_free(s);
4880 }
4881 vim_free(*file);
4882 }
4883 }
4884 if (*e != NUL)
4885 ++e;
4886 }
4887 *file = ga.ga_data;
4888 *num_file = ga.ga_len;
4889
4890 vim_free(buf);
4891 vim_free(pat);
4892 if (mustfree)
4893 vim_free(path);
4894 return OK;
4895}
4896
4897
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004899static 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));
4900
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004902 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004903 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004905 static void *
4906call_user_expand_func(user_expand_func, xp, num_file, file)
4907 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 int *num_file;
4910 char_u ***file;
4911{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004912 char_u keep;
4913 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004916 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004917 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
4919 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004920 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 *num_file = 0;
4922 *file = NULL;
4923
Bram Moolenaar21669c02008-01-18 12:16:16 +00004924 if (ccline.cmdbuff == NULL)
4925 {
4926 /* Completion from Insert mode, pass fake arguments. */
4927 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004928 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004929 args[1] = xp->xp_pattern;
4930 }
4931 else
4932 {
4933 /* Completion on the command line, pass real arguments. */
4934 keep = ccline.cmdbuff[ccline.cmdlen];
4935 ccline.cmdbuff[ccline.cmdlen] = 0;
4936 sprintf((char *)num, "%d", ccline.cmdpos);
4937 args[1] = ccline.cmdbuff;
4938 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004939 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 args[2] = num;
4941
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004942 /* Save the cmdline, we don't know what the function may do. */
4943 save_ccline = ccline;
4944 ccline.cmdbuff = NULL;
4945 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004947
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004948 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004949
4950 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004952 if (ccline.cmdbuff != NULL)
4953 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004954
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004955 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004956 return ret;
4957}
4958
4959/*
4960 * Expand names with a function defined by the user.
4961 */
4962 static int
4963ExpandUserDefined(xp, regmatch, num_file, file)
4964 expand_T *xp;
4965 regmatch_T *regmatch;
4966 int *num_file;
4967 char_u ***file;
4968{
4969 char_u *retstr;
4970 char_u *s;
4971 char_u *e;
4972 char_u keep;
4973 garray_T ga;
4974
4975 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4976 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 return FAIL;
4978
4979 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004980 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
4982 e = vim_strchr(s, '\n');
4983 if (e == NULL)
4984 e = s + STRLEN(s);
4985 keep = *e;
4986 *e = 0;
4987
4988 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4989 {
4990 *e = keep;
4991 if (*e != NUL)
4992 ++e;
4993 continue;
4994 }
4995
4996 if (ga_grow(&ga, 1) == FAIL)
4997 break;
4998
4999 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5000 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
5002 *e = keep;
5003 if (*e != NUL)
5004 ++e;
5005 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005006 vim_free(retstr);
5007 *file = ga.ga_data;
5008 *num_file = ga.ga_len;
5009 return OK;
5010}
5011
5012/*
5013 * Expand names with a list returned by a function defined by the user.
5014 */
5015 static int
5016ExpandUserList(xp, num_file, file)
5017 expand_T *xp;
5018 int *num_file;
5019 char_u ***file;
5020{
5021 list_T *retlist;
5022 listitem_T *li;
5023 garray_T ga;
5024
5025 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5026 if (retlist == NULL)
5027 return FAIL;
5028
5029 ga_init2(&ga, (int)sizeof(char *), 3);
5030 /* Loop over the items in the list. */
5031 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5032 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005033 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5034 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005035
5036 if (ga_grow(&ga, 1) == FAIL)
5037 break;
5038
5039 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005040 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005041 ++ga.ga_len;
5042 }
5043 list_unref(retlist);
5044
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 *file = ga.ga_data;
5046 *num_file = ga.ga_len;
5047 return OK;
5048}
5049#endif
5050
5051/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005052 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005053 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005054 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 */
5056 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005057ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 char_u *pat;
5059 int *num_file;
5060 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005061 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005063 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 char_u *s;
5065 char_u *e;
5066 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005067 int i;
5068 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 *num_file = 0;
5071 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005072 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005073 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005075 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005077 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5078 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005080 ga_clear_strings(&ga);
5081 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005083 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5084 matches = globpath(p_rtp, s, 0);
5085 vim_free(s);
5086 if (matches == NULL)
5087 continue;
5088
5089 for (s = matches; *s != NUL; s = e)
5090 {
5091 e = vim_strchr(s, '\n');
5092 if (e == NULL)
5093 e = s + STRLEN(s);
5094 if (ga_grow(&ga, 1) == FAIL)
5095 break;
5096 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5097 {
5098 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5099 if (*s == '\n' || vim_ispathsep(*s))
5100 break;
5101 ++s;
5102 ((char_u **)ga.ga_data)[ga.ga_len] =
5103 vim_strnsave(s, (int)(e - s - 4));
5104 ++ga.ga_len;
5105 }
5106 if (*e != NUL)
5107 ++e;
5108 }
5109 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005111 if (ga.ga_len == 0)
5112 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005113
5114 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005115 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005116 remove_duplicates(&ga);
5117
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 *file = ga.ga_data;
5119 *num_file = ga.ga_len;
5120 return OK;
5121}
5122
5123#endif
5124
5125#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5126/*
5127 * Expand "file" for all comma-separated directories in "path".
5128 * Returns an allocated string with all matches concatenated, separated by
5129 * newlines. Returns NULL for an error or no matches.
5130 */
5131 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005132globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 char_u *path;
5134 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005135 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136{
5137 expand_T xpc;
5138 char_u *buf;
5139 garray_T ga;
5140 int i;
5141 int len;
5142 int num_p;
5143 char_u **p;
5144 char_u *cur = NULL;
5145
5146 buf = alloc(MAXPATHL);
5147 if (buf == NULL)
5148 return NULL;
5149
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005150 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005152
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 ga_init2(&ga, 1, 100);
5154
5155 /* Loop over all entries in {path}. */
5156 while (*path != NUL)
5157 {
5158 /* Copy one item of the path to buf[] and concatenate the file name. */
5159 copy_option_part(&path, buf, MAXPATHL, ",");
5160 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5161 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005162# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005163 /* Using the platform's path separator (\) makes vim incorrectly
5164 * treat it as an escape character, use '/' instead. */
5165 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5166 STRCAT(buf, "/");
5167# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005169# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005171 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5172 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005174 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005176 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177
5178 /* Concatenate new results to previous ones. */
5179 if (ga_grow(&ga, len) == OK)
5180 {
5181 cur = (char_u *)ga.ga_data + ga.ga_len;
5182 for (i = 0; i < num_p; ++i)
5183 {
5184 STRCPY(cur, p[i]);
5185 cur += STRLEN(p[i]);
5186 *cur++ = '\n';
5187 }
5188 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
5190 FreeWild(num_p, p);
5191 }
5192 }
5193 }
5194 if (cur != NULL)
5195 *--cur = 0; /* Replace trailing newline with NUL */
5196
5197 vim_free(buf);
5198 return (char_u *)ga.ga_data;
5199}
5200
5201#endif
5202
5203#if defined(FEAT_CMDHIST) || defined(PROTO)
5204
5205/*********************************
5206 * Command line history stuff *
5207 *********************************/
5208
5209/*
5210 * Translate a history character to the associated type number.
5211 */
5212 static int
5213hist_char2type(c)
5214 int c;
5215{
5216 if (c == ':')
5217 return HIST_CMD;
5218 if (c == '=')
5219 return HIST_EXPR;
5220 if (c == '@')
5221 return HIST_INPUT;
5222 if (c == '>')
5223 return HIST_DEBUG;
5224 return HIST_SEARCH; /* must be '?' or '/' */
5225}
5226
5227/*
5228 * Table of history names.
5229 * These names are used in :history and various hist...() functions.
5230 * It is sufficient to give the significant prefix of a history name.
5231 */
5232
5233static char *(history_names[]) =
5234{
5235 "cmd",
5236 "search",
5237 "expr",
5238 "input",
5239 "debug",
5240 NULL
5241};
5242
5243/*
5244 * init_history() - Initialize the command line history.
5245 * Also used to re-allocate the history when the size changes.
5246 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005247 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248init_history()
5249{
5250 int newlen; /* new length of history table */
5251 histentry_T *temp;
5252 int i;
5253 int j;
5254 int type;
5255
5256 /*
5257 * If size of history table changed, reallocate it
5258 */
5259 newlen = (int)p_hi;
5260 if (newlen != hislen) /* history length changed */
5261 {
5262 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5263 {
5264 if (newlen)
5265 {
5266 temp = (histentry_T *)lalloc(
5267 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5268 if (temp == NULL) /* out of memory! */
5269 {
5270 if (type == 0) /* first one: just keep the old length */
5271 {
5272 newlen = hislen;
5273 break;
5274 }
5275 /* Already changed one table, now we can only have zero
5276 * length for all tables. */
5277 newlen = 0;
5278 type = -1;
5279 continue;
5280 }
5281 }
5282 else
5283 temp = NULL;
5284 if (newlen == 0 || temp != NULL)
5285 {
5286 if (hisidx[type] < 0) /* there are no entries yet */
5287 {
5288 for (i = 0; i < newlen; ++i)
5289 {
5290 temp[i].hisnum = 0;
5291 temp[i].hisstr = NULL;
5292 }
5293 }
5294 else if (newlen > hislen) /* array becomes bigger */
5295 {
5296 for (i = 0; i <= hisidx[type]; ++i)
5297 temp[i] = history[type][i];
5298 j = i;
5299 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5300 {
5301 temp[i].hisnum = 0;
5302 temp[i].hisstr = NULL;
5303 }
5304 for ( ; j < hislen; ++i, ++j)
5305 temp[i] = history[type][j];
5306 }
5307 else /* array becomes smaller or 0 */
5308 {
5309 j = hisidx[type];
5310 for (i = newlen - 1; ; --i)
5311 {
5312 if (i >= 0) /* copy newest entries */
5313 temp[i] = history[type][j];
5314 else /* remove older entries */
5315 vim_free(history[type][j].hisstr);
5316 if (--j < 0)
5317 j = hislen - 1;
5318 if (j == hisidx[type])
5319 break;
5320 }
5321 hisidx[type] = newlen - 1;
5322 }
5323 vim_free(history[type]);
5324 history[type] = temp;
5325 }
5326 }
5327 hislen = newlen;
5328 }
5329}
5330
5331/*
5332 * Check if command line 'str' is already in history.
5333 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5334 */
5335 static int
Bram Moolenaar4c402232011-07-27 17:58:46 +02005336in_history(type, str, move_to_front, sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 int type;
5338 char_u *str;
5339 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005340 int sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
5342 int i;
5343 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005344 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345
5346 if (hisidx[type] < 0)
5347 return FALSE;
5348 i = hisidx[type];
5349 do
5350 {
5351 if (history[type][i].hisstr == NULL)
5352 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005353
5354 /* For search history, check that the separator character matches as
5355 * well. */
5356 p = history[type][i].hisstr;
5357 if (STRCMP(str, p) == 0
5358 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 {
5360 if (!move_to_front)
5361 return TRUE;
5362 last_i = i;
5363 break;
5364 }
5365 if (--i < 0)
5366 i = hislen - 1;
5367 } while (i != hisidx[type]);
5368
5369 if (last_i >= 0)
5370 {
5371 str = history[type][i].hisstr;
5372 while (i != hisidx[type])
5373 {
5374 if (++i >= hislen)
5375 i = 0;
5376 history[type][last_i] = history[type][i];
5377 last_i = i;
5378 }
5379 history[type][i].hisstr = str;
5380 history[type][i].hisnum = ++hisnum[type];
5381 return TRUE;
5382 }
5383 return FALSE;
5384}
5385
5386/*
5387 * Convert history name (from table above) to its HIST_ equivalent.
5388 * When "name" is empty, return "cmd" history.
5389 * Returns -1 for unknown history name.
5390 */
5391 int
5392get_histtype(name)
5393 char_u *name;
5394{
5395 int i;
5396 int len = (int)STRLEN(name);
5397
5398 /* No argument: use current history. */
5399 if (len == 0)
5400 return hist_char2type(ccline.cmdfirstc);
5401
5402 for (i = 0; history_names[i] != NULL; ++i)
5403 if (STRNICMP(name, history_names[i], len) == 0)
5404 return i;
5405
5406 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5407 return hist_char2type(name[0]);
5408
5409 return -1;
5410}
5411
5412static int last_maptick = -1; /* last seen maptick */
5413
5414/*
5415 * Add the given string to the given history. If the string is already in the
5416 * history then it is moved to the front. "histype" may be one of he HIST_
5417 * values.
5418 */
5419 void
5420add_to_history(histype, new_entry, in_map, sep)
5421 int histype;
5422 char_u *new_entry;
5423 int in_map; /* consider maptick when inside a mapping */
5424 int sep; /* separator character used (search hist) */
5425{
5426 histentry_T *hisptr;
5427 int len;
5428
5429 if (hislen == 0) /* no history */
5430 return;
5431
5432 /*
5433 * Searches inside the same mapping overwrite each other, so that only
5434 * the last line is kept. Be careful not to remove a line that was moved
5435 * down, only lines that were added.
5436 */
5437 if (histype == HIST_SEARCH && in_map)
5438 {
5439 if (maptick == last_maptick)
5440 {
5441 /* Current line is from the same mapping, remove it */
5442 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5443 vim_free(hisptr->hisstr);
5444 hisptr->hisstr = NULL;
5445 hisptr->hisnum = 0;
5446 --hisnum[histype];
5447 if (--hisidx[HIST_SEARCH] < 0)
5448 hisidx[HIST_SEARCH] = hislen - 1;
5449 }
5450 last_maptick = -1;
5451 }
Bram Moolenaar4c402232011-07-27 17:58:46 +02005452 if (!in_history(histype, new_entry, TRUE, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 {
5454 if (++hisidx[histype] == hislen)
5455 hisidx[histype] = 0;
5456 hisptr = &history[histype][hisidx[histype]];
5457 vim_free(hisptr->hisstr);
5458
5459 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005460 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5462 if (hisptr->hisstr != NULL)
5463 hisptr->hisstr[len + 1] = sep;
5464
5465 hisptr->hisnum = ++hisnum[histype];
5466 if (histype == HIST_SEARCH && in_map)
5467 last_maptick = maptick;
5468 }
5469}
5470
5471#if defined(FEAT_EVAL) || defined(PROTO)
5472
5473/*
5474 * Get identifier of newest history entry.
5475 * "histype" may be one of the HIST_ values.
5476 */
5477 int
5478get_history_idx(histype)
5479 int histype;
5480{
5481 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5482 || hisidx[histype] < 0)
5483 return -1;
5484
5485 return history[histype][hisidx[histype]].hisnum;
5486}
5487
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005488static struct cmdline_info *get_ccline_ptr __ARGS((void));
5489
5490/*
5491 * Get pointer to the command line info to use. cmdline_paste() may clear
5492 * ccline and put the previous value in prev_ccline.
5493 */
5494 static struct cmdline_info *
5495get_ccline_ptr()
5496{
5497 if ((State & CMDLINE) == 0)
5498 return NULL;
5499 if (ccline.cmdbuff != NULL)
5500 return &ccline;
5501 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5502 return &prev_ccline;
5503 return NULL;
5504}
5505
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506/*
5507 * Get the current command line in allocated memory.
5508 * Only works when the command line is being edited.
5509 * Returns NULL when something is wrong.
5510 */
5511 char_u *
5512get_cmdline_str()
5513{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005514 struct cmdline_info *p = get_ccline_ptr();
5515
5516 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005518 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519}
5520
5521/*
5522 * Get the current command line position, counted in bytes.
5523 * Zero is the first position.
5524 * Only works when the command line is being edited.
5525 * Returns -1 when something is wrong.
5526 */
5527 int
5528get_cmdline_pos()
5529{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005530 struct cmdline_info *p = get_ccline_ptr();
5531
5532 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005534 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535}
5536
5537/*
5538 * Set the command line byte position to "pos". Zero is the first position.
5539 * Only works when the command line is being edited.
5540 * Returns 1 when failed, 0 when OK.
5541 */
5542 int
5543set_cmdline_pos(pos)
5544 int pos;
5545{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005546 struct cmdline_info *p = get_ccline_ptr();
5547
5548 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 return 1;
5550
5551 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5552 * changed the command line. */
5553 if (pos < 0)
5554 new_cmdpos = 0;
5555 else
5556 new_cmdpos = pos;
5557 return 0;
5558}
5559
5560/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005561 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005562 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005563 * Only works when the command line is being edited.
5564 * Returns NUL when something is wrong.
5565 */
5566 int
5567get_cmdline_type()
5568{
5569 struct cmdline_info *p = get_ccline_ptr();
5570
5571 if (p == NULL)
5572 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005573 if (p->cmdfirstc == NUL)
5574 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005575 return p->cmdfirstc;
5576}
5577
5578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 * Calculate history index from a number:
5580 * num > 0: seen as identifying number of a history entry
5581 * num < 0: relative position in history wrt newest entry
5582 * "histype" may be one of the HIST_ values.
5583 */
5584 static int
5585calc_hist_idx(histype, num)
5586 int histype;
5587 int num;
5588{
5589 int i;
5590 histentry_T *hist;
5591 int wrapped = FALSE;
5592
5593 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5594 || (i = hisidx[histype]) < 0 || num == 0)
5595 return -1;
5596
5597 hist = history[histype];
5598 if (num > 0)
5599 {
5600 while (hist[i].hisnum > num)
5601 if (--i < 0)
5602 {
5603 if (wrapped)
5604 break;
5605 i += hislen;
5606 wrapped = TRUE;
5607 }
5608 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5609 return i;
5610 }
5611 else if (-num <= hislen)
5612 {
5613 i += num + 1;
5614 if (i < 0)
5615 i += hislen;
5616 if (hist[i].hisstr != NULL)
5617 return i;
5618 }
5619 return -1;
5620}
5621
5622/*
5623 * Get a history entry by its index.
5624 * "histype" may be one of the HIST_ values.
5625 */
5626 char_u *
5627get_history_entry(histype, idx)
5628 int histype;
5629 int idx;
5630{
5631 idx = calc_hist_idx(histype, idx);
5632 if (idx >= 0)
5633 return history[histype][idx].hisstr;
5634 else
5635 return (char_u *)"";
5636}
5637
5638/*
5639 * Clear all entries of a history.
5640 * "histype" may be one of the HIST_ values.
5641 */
5642 int
5643clr_history(histype)
5644 int histype;
5645{
5646 int i;
5647 histentry_T *hisptr;
5648
5649 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5650 {
5651 hisptr = history[histype];
5652 for (i = hislen; i--;)
5653 {
5654 vim_free(hisptr->hisstr);
5655 hisptr->hisnum = 0;
5656 hisptr++->hisstr = NULL;
5657 }
5658 hisidx[histype] = -1; /* mark history as cleared */
5659 hisnum[histype] = 0; /* reset identifier counter */
5660 return OK;
5661 }
5662 return FAIL;
5663}
5664
5665/*
5666 * Remove all entries matching {str} from a history.
5667 * "histype" may be one of the HIST_ values.
5668 */
5669 int
5670del_history_entry(histype, str)
5671 int histype;
5672 char_u *str;
5673{
5674 regmatch_T regmatch;
5675 histentry_T *hisptr;
5676 int idx;
5677 int i;
5678 int last;
5679 int found = FALSE;
5680
5681 regmatch.regprog = NULL;
5682 regmatch.rm_ic = FALSE; /* always match case */
5683 if (hislen != 0
5684 && histype >= 0
5685 && histype < HIST_COUNT
5686 && *str != NUL
5687 && (idx = hisidx[histype]) >= 0
5688 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5689 != NULL)
5690 {
5691 i = last = idx;
5692 do
5693 {
5694 hisptr = &history[histype][i];
5695 if (hisptr->hisstr == NULL)
5696 break;
5697 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5698 {
5699 found = TRUE;
5700 vim_free(hisptr->hisstr);
5701 hisptr->hisstr = NULL;
5702 hisptr->hisnum = 0;
5703 }
5704 else
5705 {
5706 if (i != last)
5707 {
5708 history[histype][last] = *hisptr;
5709 hisptr->hisstr = NULL;
5710 hisptr->hisnum = 0;
5711 }
5712 if (--last < 0)
5713 last += hislen;
5714 }
5715 if (--i < 0)
5716 i += hislen;
5717 } while (i != idx);
5718 if (history[histype][idx].hisstr == NULL)
5719 hisidx[histype] = -1;
5720 }
5721 vim_free(regmatch.regprog);
5722 return found;
5723}
5724
5725/*
5726 * Remove an indexed entry from a history.
5727 * "histype" may be one of the HIST_ values.
5728 */
5729 int
5730del_history_idx(histype, idx)
5731 int histype;
5732 int idx;
5733{
5734 int i, j;
5735
5736 i = calc_hist_idx(histype, idx);
5737 if (i < 0)
5738 return FALSE;
5739 idx = hisidx[histype];
5740 vim_free(history[histype][i].hisstr);
5741
5742 /* When deleting the last added search string in a mapping, reset
5743 * last_maptick, so that the last added search string isn't deleted again.
5744 */
5745 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5746 last_maptick = -1;
5747
5748 while (i != idx)
5749 {
5750 j = (i + 1) % hislen;
5751 history[histype][i] = history[histype][j];
5752 i = j;
5753 }
5754 history[histype][i].hisstr = NULL;
5755 history[histype][i].hisnum = 0;
5756 if (--i < 0)
5757 i += hislen;
5758 hisidx[histype] = i;
5759 return TRUE;
5760}
5761
5762#endif /* FEAT_EVAL */
5763
5764#if defined(FEAT_CRYPT) || defined(PROTO)
5765/*
5766 * Very specific function to remove the value in ":set key=val" from the
5767 * history.
5768 */
5769 void
5770remove_key_from_history()
5771{
5772 char_u *p;
5773 int i;
5774
5775 i = hisidx[HIST_CMD];
5776 if (i < 0)
5777 return;
5778 p = history[HIST_CMD][i].hisstr;
5779 if (p != NULL)
5780 for ( ; *p; ++p)
5781 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5782 {
5783 p = vim_strchr(p + 3, '=');
5784 if (p == NULL)
5785 break;
5786 ++p;
5787 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5788 if (p[i] == '\\' && p[i + 1])
5789 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005790 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 --p;
5792 }
5793}
5794#endif
5795
5796#endif /* FEAT_CMDHIST */
5797
5798#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5799/*
5800 * Get indices "num1,num2" that specify a range within a list (not a range of
5801 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5802 * Returns OK if parsed successfully, otherwise FAIL.
5803 */
5804 int
5805get_list_range(str, num1, num2)
5806 char_u **str;
5807 int *num1;
5808 int *num2;
5809{
5810 int len;
5811 int first = FALSE;
5812 long num;
5813
5814 *str = skipwhite(*str);
5815 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5816 {
5817 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5818 *str += len;
5819 *num1 = (int)num;
5820 first = TRUE;
5821 }
5822 *str = skipwhite(*str);
5823 if (**str == ',') /* parse "to" part of range */
5824 {
5825 *str = skipwhite(*str + 1);
5826 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5827 if (len > 0)
5828 {
5829 *num2 = (int)num;
5830 *str = skipwhite(*str + len);
5831 }
5832 else if (!first) /* no number given at all */
5833 return FAIL;
5834 }
5835 else if (first) /* only one number given */
5836 *num2 = *num1;
5837 return OK;
5838}
5839#endif
5840
5841#if defined(FEAT_CMDHIST) || defined(PROTO)
5842/*
5843 * :history command - print a history
5844 */
5845 void
5846ex_history(eap)
5847 exarg_T *eap;
5848{
5849 histentry_T *hist;
5850 int histype1 = HIST_CMD;
5851 int histype2 = HIST_CMD;
5852 int hisidx1 = 1;
5853 int hisidx2 = -1;
5854 int idx;
5855 int i, j, k;
5856 char_u *end;
5857 char_u *arg = eap->arg;
5858
5859 if (hislen == 0)
5860 {
5861 MSG(_("'history' option is zero"));
5862 return;
5863 }
5864
5865 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5866 {
5867 end = arg;
5868 while (ASCII_ISALPHA(*end)
5869 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5870 end++;
5871 i = *end;
5872 *end = NUL;
5873 histype1 = get_histtype(arg);
5874 if (histype1 == -1)
5875 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005876 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 {
5878 histype1 = 0;
5879 histype2 = HIST_COUNT-1;
5880 }
5881 else
5882 {
5883 *end = i;
5884 EMSG(_(e_trailing));
5885 return;
5886 }
5887 }
5888 else
5889 histype2 = histype1;
5890 *end = i;
5891 }
5892 else
5893 end = arg;
5894 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5895 {
5896 EMSG(_(e_trailing));
5897 return;
5898 }
5899
5900 for (; !got_int && histype1 <= histype2; ++histype1)
5901 {
5902 STRCPY(IObuff, "\n # ");
5903 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5904 MSG_PUTS_TITLE(IObuff);
5905 idx = hisidx[histype1];
5906 hist = history[histype1];
5907 j = hisidx1;
5908 k = hisidx2;
5909 if (j < 0)
5910 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5911 if (k < 0)
5912 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5913 if (idx >= 0 && j <= k)
5914 for (i = idx + 1; !got_int; ++i)
5915 {
5916 if (i == hislen)
5917 i = 0;
5918 if (hist[i].hisstr != NULL
5919 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5920 {
5921 msg_putchar('\n');
5922 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5923 hist[i].hisnum);
5924 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5925 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005926 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 else
5928 STRCAT(IObuff, hist[i].hisstr);
5929 msg_outtrans(IObuff);
5930 out_flush();
5931 }
5932 if (i == idx)
5933 break;
5934 }
5935 }
5936}
5937#endif
5938
5939#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5940static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5941static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5942static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5943static int viminfo_add_at_front = FALSE;
5944
5945static int hist_type2char __ARGS((int type, int use_question));
5946
5947/*
5948 * Translate a history type number to the associated character.
5949 */
5950 static int
5951hist_type2char(type, use_question)
5952 int type;
5953 int use_question; /* use '?' instead of '/' */
5954{
5955 if (type == HIST_CMD)
5956 return ':';
5957 if (type == HIST_SEARCH)
5958 {
5959 if (use_question)
5960 return '?';
5961 else
5962 return '/';
5963 }
5964 if (type == HIST_EXPR)
5965 return '=';
5966 return '@';
5967}
5968
5969/*
5970 * Prepare for reading the history from the viminfo file.
5971 * This allocates history arrays to store the read history lines.
5972 */
5973 void
5974prepare_viminfo_history(asklen)
5975 int asklen;
5976{
5977 int i;
5978 int num;
5979 int type;
5980 int len;
5981
5982 init_history();
5983 viminfo_add_at_front = (asklen != 0);
5984 if (asklen > hislen)
5985 asklen = hislen;
5986
5987 for (type = 0; type < HIST_COUNT; ++type)
5988 {
5989 /*
5990 * Count the number of empty spaces in the history list. If there are
5991 * more spaces available than we request, then fill them up.
5992 */
5993 for (i = 0, num = 0; i < hislen; i++)
5994 if (history[type][i].hisstr == NULL)
5995 num++;
5996 len = asklen;
5997 if (num > len)
5998 len = num;
5999 if (len <= 0)
6000 viminfo_history[type] = NULL;
6001 else
6002 viminfo_history[type] =
6003 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6004 if (viminfo_history[type] == NULL)
6005 len = 0;
6006 viminfo_hislen[type] = len;
6007 viminfo_hisidx[type] = 0;
6008 }
6009}
6010
6011/*
6012 * Accept a line from the viminfo, store it in the history array when it's
6013 * new.
6014 */
6015 int
6016read_viminfo_history(virp)
6017 vir_T *virp;
6018{
6019 int type;
6020 long_u len;
6021 char_u *val;
6022 char_u *p;
6023
6024 type = hist_char2type(virp->vir_line[0]);
6025 if (viminfo_hisidx[type] < viminfo_hislen[type])
6026 {
6027 val = viminfo_readstring(virp, 1, TRUE);
6028 if (val != NULL && *val != NUL)
6029 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006030 int sep = (*val == ' ' ? NUL : *val);
6031
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006033 viminfo_add_at_front, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 {
6035 /* Need to re-allocate to append the separator byte. */
6036 len = STRLEN(val);
6037 p = lalloc(len + 2, TRUE);
6038 if (p != NULL)
6039 {
6040 if (type == HIST_SEARCH)
6041 {
6042 /* Search entry: Move the separator from the first
6043 * column to after the NUL. */
6044 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006045 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 }
6047 else
6048 {
6049 /* Not a search entry: No separator in the viminfo
6050 * file, add a NUL separator. */
6051 mch_memmove(p, val, (size_t)len + 1);
6052 p[len + 1] = NUL;
6053 }
6054 viminfo_history[type][viminfo_hisidx[type]++] = p;
6055 }
6056 }
6057 }
6058 vim_free(val);
6059 }
6060 return viminfo_readline(virp);
6061}
6062
6063 void
6064finish_viminfo_history()
6065{
6066 int idx;
6067 int i;
6068 int type;
6069
6070 for (type = 0; type < HIST_COUNT; ++type)
6071 {
6072 if (history[type] == NULL)
6073 return;
6074 idx = hisidx[type] + viminfo_hisidx[type];
6075 if (idx >= hislen)
6076 idx -= hislen;
6077 else if (idx < 0)
6078 idx = hislen - 1;
6079 if (viminfo_add_at_front)
6080 hisidx[type] = idx;
6081 else
6082 {
6083 if (hisidx[type] == -1)
6084 hisidx[type] = hislen - 1;
6085 do
6086 {
6087 if (history[type][idx].hisstr != NULL)
6088 break;
6089 if (++idx == hislen)
6090 idx = 0;
6091 } while (idx != hisidx[type]);
6092 if (idx != hisidx[type] && --idx < 0)
6093 idx = hislen - 1;
6094 }
6095 for (i = 0; i < viminfo_hisidx[type]; i++)
6096 {
6097 vim_free(history[type][idx].hisstr);
6098 history[type][idx].hisstr = viminfo_history[type][i];
6099 if (--idx < 0)
6100 idx = hislen - 1;
6101 }
6102 idx += 1;
6103 idx %= hislen;
6104 for (i = 0; i < viminfo_hisidx[type]; i++)
6105 {
6106 history[type][idx++].hisnum = ++hisnum[type];
6107 idx %= hislen;
6108 }
6109 vim_free(viminfo_history[type]);
6110 viminfo_history[type] = NULL;
6111 }
6112}
6113
6114 void
6115write_viminfo_history(fp)
6116 FILE *fp;
6117{
6118 int i;
6119 int type;
6120 int num_saved;
6121 char_u *p;
6122 int c;
6123
6124 init_history();
6125 if (hislen == 0)
6126 return;
6127 for (type = 0; type < HIST_COUNT; ++type)
6128 {
6129 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6130 if (num_saved == 0)
6131 continue;
6132 if (num_saved < 0) /* Use default */
6133 num_saved = hislen;
6134 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6135 type == HIST_CMD ? _("Command Line") :
6136 type == HIST_SEARCH ? _("Search String") :
6137 type == HIST_EXPR ? _("Expression") :
6138 _("Input Line"));
6139 if (num_saved > hislen)
6140 num_saved = hislen;
6141 i = hisidx[type];
6142 if (i >= 0)
6143 while (num_saved--)
6144 {
6145 p = history[type][i].hisstr;
6146 if (p != NULL)
6147 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006148 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 /* For the search history: put the separator in the second
6150 * column; use a space if there isn't one. */
6151 if (type == HIST_SEARCH)
6152 {
6153 c = p[STRLEN(p) + 1];
6154 putc(c == NUL ? ' ' : c, fp);
6155 }
6156 viminfo_writestring(fp, p);
6157 }
6158 if (--i < 0)
6159 i = hislen - 1;
6160 }
6161 }
6162}
6163#endif /* FEAT_VIMINFO */
6164
6165#if defined(FEAT_FKMAP) || defined(PROTO)
6166/*
6167 * Write a character at the current cursor+offset position.
6168 * It is directly written into the command buffer block.
6169 */
6170 void
6171cmd_pchar(c, offset)
6172 int c, offset;
6173{
6174 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6175 {
6176 EMSG(_("E198: cmd_pchar beyond the command length"));
6177 return;
6178 }
6179 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6180 ccline.cmdbuff[ccline.cmdlen] = NUL;
6181}
6182
6183 int
6184cmd_gchar(offset)
6185 int offset;
6186{
6187 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6188 {
6189 /* EMSG(_("cmd_gchar beyond the command length")); */
6190 return NUL;
6191 }
6192 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6193}
6194#endif
6195
6196#if defined(FEAT_CMDWIN) || defined(PROTO)
6197/*
6198 * Open a window on the current command line and history. Allow editing in
6199 * the window. Returns when the window is closed.
6200 * Returns:
6201 * CR if the command is to be executed
6202 * Ctrl_C if it is to be abandoned
6203 * K_IGNORE if editing continues
6204 */
6205 static int
6206ex_window()
6207{
6208 struct cmdline_info save_ccline;
6209 buf_T *old_curbuf = curbuf;
6210 win_T *old_curwin = curwin;
6211 buf_T *bp;
6212 win_T *wp;
6213 int i;
6214 linenr_T lnum;
6215 int histtype;
6216 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006217#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 int save_restart_edit = restart_edit;
6221 int save_State = State;
6222 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006223#ifdef FEAT_RIGHTLEFT
6224 int save_cmdmsg_rl = cmdmsg_rl;
6225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226
6227 /* Can't do this recursively. Can't do it when typing a password. */
6228 if (cmdwin_type != 0
6229# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6230 || cmdline_star > 0
6231# endif
6232 )
6233 {
6234 beep_flush();
6235 return K_IGNORE;
6236 }
6237
6238 /* Save current window sizes. */
6239 win_size_save(&winsizes);
6240
6241# ifdef FEAT_AUTOCMD
6242 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006243 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006245 /* don't use a new tab page */
6246 cmdmod.tab = 0;
6247
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 /* Create a window for the command-line buffer. */
6249 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6250 {
6251 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006252# ifdef FEAT_AUTOCMD
6253 unblock_autocmds();
6254# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 return K_IGNORE;
6256 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006257 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258
6259 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006260 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006261 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6263 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6264 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006265#ifdef FEAT_FOLDING
6266 curwin->w_p_fen = FALSE;
6267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006269 curwin->w_p_rl = cmdmsg_rl;
6270 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006272 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273
6274# ifdef FEAT_AUTOCMD
6275 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006276 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277# endif
6278
Bram Moolenaar46152342005-09-07 21:18:43 +00006279 /* Showing the prompt may have set need_wait_return, reset it. */
6280 need_wait_return = FALSE;
6281
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006282 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6284 {
6285 if (p_wc == TAB)
6286 {
6287 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6288 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6289 }
6290 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6291 }
6292
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006293 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6294 * sets 'textwidth' to 78). */
6295 curbuf->b_p_tw = 0;
6296
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 /* Fill the buffer with the history. */
6298 init_history();
6299 if (hislen > 0)
6300 {
6301 i = hisidx[histtype];
6302 if (i >= 0)
6303 {
6304 lnum = 0;
6305 do
6306 {
6307 if (++i == hislen)
6308 i = 0;
6309 if (history[histtype][i].hisstr != NULL)
6310 ml_append(lnum++, history[histtype][i].hisstr,
6311 (colnr_T)0, FALSE);
6312 }
6313 while (i != hisidx[histtype]);
6314 }
6315 }
6316
6317 /* Replace the empty last line with the current command-line and put the
6318 * cursor there. */
6319 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6320 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6321 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006322 changed_line_abv_curs();
6323 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006324 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
6326 /* Save the command line info, can be used recursively. */
6327 save_ccline = ccline;
6328 ccline.cmdbuff = NULL;
6329 ccline.cmdprompt = NULL;
6330
6331 /* No Ex mode here! */
6332 exmode_active = 0;
6333
6334 State = NORMAL;
6335# ifdef FEAT_MOUSE
6336 setmouse();
6337# endif
6338
6339# ifdef FEAT_AUTOCMD
6340 /* Trigger CmdwinEnter autocommands. */
6341 typestr[0] = cmdwin_type;
6342 typestr[1] = NUL;
6343 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006344 if (restart_edit != 0) /* autocmd with ":startinsert" */
6345 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346# endif
6347
6348 i = RedrawingDisabled;
6349 RedrawingDisabled = 0;
6350
6351 /*
6352 * Call the main loop until <CR> or CTRL-C is typed.
6353 */
6354 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006355 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356
6357 RedrawingDisabled = i;
6358
6359# ifdef FEAT_AUTOCMD
6360 /* Trigger CmdwinLeave autocommands. */
6361 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6362# endif
6363
Bram Moolenaarccc18222007-05-10 18:25:20 +00006364 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 ccline = save_ccline;
6366 cmdwin_type = 0;
6367
6368 exmode_active = save_exmode;
6369
Bram Moolenaarf9821062008-06-20 16:31:07 +00006370 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 * this happens! */
6372 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6373 {
6374 cmdwin_result = Ctrl_C;
6375 EMSG(_("E199: Active window or buffer deleted"));
6376 }
6377 else
6378 {
6379# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6380 /* autocmds may abort script processing */
6381 if (aborting() && cmdwin_result != K_IGNORE)
6382 cmdwin_result = Ctrl_C;
6383# endif
6384 /* Set the new command line from the cmdline buffer. */
6385 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006386 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006388 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6389
6390 if (histtype == HIST_CMD)
6391 {
6392 /* Execute the command directly. */
6393 ccline.cmdbuff = vim_strsave((char_u *)p);
6394 cmdwin_result = CAR;
6395 }
6396 else
6397 {
6398 /* First need to cancel what we were doing. */
6399 ccline.cmdbuff = NULL;
6400 stuffcharReadbuff(':');
6401 stuffReadbuff((char_u *)p);
6402 stuffcharReadbuff(CAR);
6403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 }
6405 else if (cmdwin_result == K_XF2) /* :qa typed */
6406 {
6407 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6408 cmdwin_result = CAR;
6409 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006410 else if (cmdwin_result == Ctrl_C)
6411 {
6412 /* :q or :close, don't execute any command
6413 * and don't modify the cmd window. */
6414 ccline.cmdbuff = NULL;
6415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 else
6417 ccline.cmdbuff = vim_strsave(ml_get_curline());
6418 if (ccline.cmdbuff == NULL)
6419 cmdwin_result = Ctrl_C;
6420 else
6421 {
6422 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6423 ccline.cmdbufflen = ccline.cmdlen + 1;
6424 ccline.cmdpos = curwin->w_cursor.col;
6425 if (ccline.cmdpos > ccline.cmdlen)
6426 ccline.cmdpos = ccline.cmdlen;
6427 if (cmdwin_result == K_IGNORE)
6428 {
6429 set_cmdspos_cursor();
6430 redrawcmd();
6431 }
6432 }
6433
6434# ifdef FEAT_AUTOCMD
6435 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006436 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437# endif
6438 wp = curwin;
6439 bp = curbuf;
6440 win_goto(old_curwin);
6441 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006442
6443 /* win_close() may have already wiped the buffer when 'bh' is
6444 * set to 'wipe' */
6445 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006446 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447
6448 /* Restore window sizes. */
6449 win_size_restore(&winsizes);
6450
6451# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006452 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453# endif
6454 }
6455
6456 ga_clear(&winsizes);
6457 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006458# ifdef FEAT_RIGHTLEFT
6459 cmdmsg_rl = save_cmdmsg_rl;
6460# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
6462 State = save_State;
6463# ifdef FEAT_MOUSE
6464 setmouse();
6465# endif
6466
6467 return cmdwin_result;
6468}
6469#endif /* FEAT_CMDWIN */
6470
6471/*
6472 * Used for commands that either take a simple command string argument, or:
6473 * cmd << endmarker
6474 * {script}
6475 * endmarker
6476 * Returns a pointer to allocated memory with {script} or NULL.
6477 */
6478 char_u *
6479script_get(eap, cmd)
6480 exarg_T *eap;
6481 char_u *cmd;
6482{
6483 char_u *theline;
6484 char *end_pattern = NULL;
6485 char dot[] = ".";
6486 garray_T ga;
6487
6488 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6489 return NULL;
6490
6491 ga_init2(&ga, 1, 0x400);
6492
6493 if (cmd[2] != NUL)
6494 end_pattern = (char *)skipwhite(cmd + 2);
6495 else
6496 end_pattern = dot;
6497
6498 for (;;)
6499 {
6500 theline = eap->getline(
6501#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006502 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503#endif
6504 NUL, eap->cookie, 0);
6505
6506 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006507 {
6508 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
6512 ga_concat(&ga, theline);
6513 ga_append(&ga, '\n');
6514 vim_free(theline);
6515 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006516 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517
6518 return (char_u *)ga.ga_data;
6519}