blob: bf4999b47eef6b59ebef3551e498ffc108c91f47 [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);
648 c = p_wc;
649 }
650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651
652#endif /* FEAT_WILDMENU */
653
654 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
655 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
656 if (c == Ctrl_BSL)
657 {
658 ++no_mapping;
659 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000660 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 --no_mapping;
662 --allow_keys;
663 /* CTRL-\ e doesn't work when obtaining an expression. */
664 if (c != Ctrl_N && c != Ctrl_G
665 && (c != 'e' || ccline.cmdfirstc == '='))
666 {
667 vungetc(c);
668 c = Ctrl_BSL;
669 }
670#ifdef FEAT_EVAL
671 else if (c == 'e')
672 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200673 char_u *p = NULL;
674 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
676 /*
677 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000678 * Need to save and restore the current command line, to be
679 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 */
681 if (ccline.cmdpos == ccline.cmdlen)
682 new_cmdpos = 99999; /* keep it at the end */
683 else
684 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000685
686 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000688 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 if (c == '=')
690 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000691 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000692 * to avoid nasty things like going to another buffer when
693 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000694 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000695 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000697 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000698 restore_cmdline(&save_ccline);
699
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200700 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200702 len = (int)STRLEN(p);
703 if (realloc_cmdbuff(len + 1) == OK)
704 {
705 ccline.cmdlen = len;
706 STRCPY(ccline.cmdbuff, p);
707 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200709 /* Restore the cursor or use the position set with
710 * set_cmdline_pos(). */
711 if (new_cmdpos > ccline.cmdlen)
712 ccline.cmdpos = ccline.cmdlen;
713 else
714 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200716 KeyTyped = FALSE; /* Don't do p_wc completion. */
717 redrawcmd();
718 goto cmdline_changed;
719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 }
721 }
722 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100723 got_int = FALSE; /* don't abandon the command line */
724 did_emsg = FALSE;
725 emsg_on_display = FALSE;
726 redrawcmd();
727 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 }
729#endif
730 else
731 {
732 if (c == Ctrl_G && p_im && restart_edit == 0)
733 restart_edit = 'a';
734 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
735 in history */
736 goto returncmd; /* back to Normal mode */
737 }
738 }
739
740#ifdef FEAT_CMDWIN
741 if (c == cedit_key || c == K_CMDWIN)
742 {
743 /*
744 * Open a window to edit the command line (and history).
745 */
746 c = ex_window();
747 some_key_typed = TRUE;
748 }
749# ifdef FEAT_DIGRAPHS
750 else
751# endif
752#endif
753#ifdef FEAT_DIGRAPHS
754 c = do_digraph(c);
755#endif
756
757 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
758 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
759 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000760 /* In Ex mode a backslash escapes a newline. */
761 if (exmode_active
762 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000763 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000764 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000765 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000767 if (c == K_KENTER)
768 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000770 else
771 {
772 gotesc = FALSE; /* Might have typed ESC previously, don't
773 truncate the cmdline now. */
774 if (ccheck_abbr(c + ABBR_OFF))
775 goto cmdline_changed;
776 if (!cmd_silent)
777 {
778 windgoto(msg_row, 0);
779 out_flush();
780 }
781 break;
782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784
785 /*
786 * Completion for 'wildchar' or 'wildcharm' key.
787 * - hitting <ESC> twice means: abandon command line.
788 * - wildcard expansion is only done when the 'wildchar' key is really
789 * typed, not when it comes from a macro
790 */
791 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
792 {
793 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
794 {
795 /* if 'wildmode' contains "list" may still need to list */
796 if (xpc.xp_numfiles > 1
797 && !did_wild_list
798 && (wim_flags[wim_index] & WIM_LIST))
799 {
800 (void)showmatches(&xpc, FALSE);
801 redrawcmd();
802 did_wild_list = TRUE;
803 }
804 if (wim_flags[wim_index] & WIM_LONGEST)
805 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
806 else if (wim_flags[wim_index] & WIM_FULL)
807 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
808 else
809 res = OK; /* don't insert 'wildchar' now */
810 }
811 else /* typed p_wc first time */
812 {
813 wim_index = 0;
814 j = ccline.cmdpos;
815 /* if 'wildmode' first contains "longest", get longest
816 * common part */
817 if (wim_flags[0] & WIM_LONGEST)
818 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
819 else
820 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
821
822 /* if interrupted while completing, behave like it failed */
823 if (got_int)
824 {
825 (void)vpeekc(); /* remove <C-C> from input stream */
826 got_int = FALSE; /* don't abandon the command line */
827 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
828#ifdef FEAT_WILDMENU
829 xpc.xp_context = EXPAND_NOTHING;
830#endif
831 goto cmdline_changed;
832 }
833
834 /* when more than one match, and 'wildmode' first contains
835 * "list", or no change and 'wildmode' contains "longest,list",
836 * list all matches */
837 if (res == OK && xpc.xp_numfiles > 1)
838 {
839 /* a "longest" that didn't do anything is skipped (but not
840 * "list:longest") */
841 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
842 wim_index = 1;
843 if ((wim_flags[wim_index] & WIM_LIST)
844#ifdef FEAT_WILDMENU
845 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
846#endif
847 )
848 {
849 if (!(wim_flags[0] & WIM_LONGEST))
850 {
851#ifdef FEAT_WILDMENU
852 int p_wmnu_save = p_wmnu;
853 p_wmnu = 0;
854#endif
855 nextwild(&xpc, WILD_PREV, 0); /* remove match */
856#ifdef FEAT_WILDMENU
857 p_wmnu = p_wmnu_save;
858#endif
859 }
860#ifdef FEAT_WILDMENU
861 (void)showmatches(&xpc, p_wmnu
862 && ((wim_flags[wim_index] & WIM_LIST) == 0));
863#else
864 (void)showmatches(&xpc, FALSE);
865#endif
866 redrawcmd();
867 did_wild_list = TRUE;
868 if (wim_flags[wim_index] & WIM_LONGEST)
869 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
870 else if (wim_flags[wim_index] & WIM_FULL)
871 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
872 }
873 else
874 vim_beep();
875 }
876#ifdef FEAT_WILDMENU
877 else if (xpc.xp_numfiles == -1)
878 xpc.xp_context = EXPAND_NOTHING;
879#endif
880 }
881 if (wim_index < 3)
882 ++wim_index;
883 if (c == ESC)
884 gotesc = TRUE;
885 if (res == OK)
886 goto cmdline_changed;
887 }
888
889 gotesc = FALSE;
890
891 /* <S-Tab> goes to last match, in a clumsy way */
892 if (c == K_S_TAB && KeyTyped)
893 {
894 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
895 && nextwild(&xpc, WILD_PREV, 0) == OK
896 && nextwild(&xpc, WILD_PREV, 0) == OK)
897 goto cmdline_changed;
898 }
899
900 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
901 c = NL;
902
903 do_abbr = TRUE; /* default: check for abbreviation */
904
905 /*
906 * Big switch for a typed command line character.
907 */
908 switch (c)
909 {
910 case K_BS:
911 case Ctrl_H:
912 case K_DEL:
913 case K_KDEL:
914 case Ctrl_W:
915#ifdef FEAT_FKMAP
916 if (cmd_fkmap && c == K_BS)
917 c = K_DEL;
918#endif
919 if (c == K_KDEL)
920 c = K_DEL;
921
922 /*
923 * delete current character is the same as backspace on next
924 * character, except at end of line
925 */
926 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
927 ++ccline.cmdpos;
928#ifdef FEAT_MBYTE
929 if (has_mbyte && c == K_DEL)
930 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
931 ccline.cmdbuff + ccline.cmdpos);
932#endif
933 if (ccline.cmdpos > 0)
934 {
935 char_u *p;
936
937 j = ccline.cmdpos;
938 p = ccline.cmdbuff + j;
939#ifdef FEAT_MBYTE
940 if (has_mbyte)
941 {
942 p = mb_prevptr(ccline.cmdbuff, p);
943 if (c == Ctrl_W)
944 {
945 while (p > ccline.cmdbuff && vim_isspace(*p))
946 p = mb_prevptr(ccline.cmdbuff, p);
947 i = mb_get_class(p);
948 while (p > ccline.cmdbuff && mb_get_class(p) == i)
949 p = mb_prevptr(ccline.cmdbuff, p);
950 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000951 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 }
953 }
954 else
955#endif
956 if (c == Ctrl_W)
957 {
958 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
959 --p;
960 i = vim_iswordc(p[-1]);
961 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
962 && vim_iswordc(p[-1]) == i)
963 --p;
964 }
965 else
966 --p;
967 ccline.cmdpos = (int)(p - ccline.cmdbuff);
968 ccline.cmdlen -= j - ccline.cmdpos;
969 i = ccline.cmdpos;
970 while (i < ccline.cmdlen)
971 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
972
973 /* Truncate at the end, required for multi-byte chars. */
974 ccline.cmdbuff[ccline.cmdlen] = NUL;
975 redrawcmd();
976 }
977 else if (ccline.cmdlen == 0 && c != Ctrl_W
978 && ccline.cmdprompt == NULL && indent == 0)
979 {
980 /* In ex and debug mode it doesn't make sense to return. */
981 if (exmode_active
982#ifdef FEAT_EVAL
983 || ccline.cmdfirstc == '>'
984#endif
985 )
986 goto cmdline_not_changed;
987
988 vim_free(ccline.cmdbuff); /* no commandline to return */
989 ccline.cmdbuff = NULL;
990 if (!cmd_silent)
991 {
992#ifdef FEAT_RIGHTLEFT
993 if (cmdmsg_rl)
994 msg_col = Columns;
995 else
996#endif
997 msg_col = 0;
998 msg_putchar(' '); /* delete ':' */
999 }
1000 redraw_cmdline = TRUE;
1001 goto returncmd; /* back to cmd mode */
1002 }
1003 goto cmdline_changed;
1004
1005 case K_INS:
1006 case K_KINS:
1007#ifdef FEAT_FKMAP
1008 /* if Farsi mode set, we are in reverse insert mode -
1009 Do not change the mode */
1010 if (cmd_fkmap)
1011 beep_flush();
1012 else
1013#endif
1014 ccline.overstrike = !ccline.overstrike;
1015#ifdef CURSOR_SHAPE
1016 ui_cursor_shape(); /* may show different cursor shape */
1017#endif
1018 goto cmdline_not_changed;
1019
1020 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001021 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
1023 /* ":lmap" mappings exists, toggle use of mappings. */
1024 State ^= LANGMAP;
1025#ifdef USE_IM_CONTROL
1026 im_set_active(FALSE); /* Disable input method */
1027#endif
1028 if (b_im_ptr != NULL)
1029 {
1030 if (State & LANGMAP)
1031 *b_im_ptr = B_IMODE_LMAP;
1032 else
1033 *b_im_ptr = B_IMODE_NONE;
1034 }
1035 }
1036#ifdef USE_IM_CONTROL
1037 else
1038 {
1039 /* There are no ":lmap" mappings, toggle IM. When
1040 * 'imdisable' is set don't try getting the status, it's
1041 * always off. */
1042 if ((p_imdisable && b_im_ptr != NULL)
1043 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1044 {
1045 im_set_active(FALSE); /* Disable input method */
1046 if (b_im_ptr != NULL)
1047 *b_im_ptr = B_IMODE_NONE;
1048 }
1049 else
1050 {
1051 im_set_active(TRUE); /* Enable input method */
1052 if (b_im_ptr != NULL)
1053 *b_im_ptr = B_IMODE_IM;
1054 }
1055 }
1056#endif
1057 if (b_im_ptr != NULL)
1058 {
1059 if (b_im_ptr == &curbuf->b_p_iminsert)
1060 set_iminsert_global();
1061 else
1062 set_imsearch_global();
1063 }
1064#ifdef CURSOR_SHAPE
1065 ui_cursor_shape(); /* may show different cursor shape */
1066#endif
1067#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1068 /* Show/unshow value of 'keymap' in status lines later. */
1069 status_redraw_curbuf();
1070#endif
1071 goto cmdline_not_changed;
1072
1073/* case '@': only in very old vi */
1074 case Ctrl_U:
1075 /* delete all characters left of the cursor */
1076 j = ccline.cmdpos;
1077 ccline.cmdlen -= j;
1078 i = ccline.cmdpos = 0;
1079 while (i < ccline.cmdlen)
1080 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1081 /* Truncate at the end, required for multi-byte chars. */
1082 ccline.cmdbuff[ccline.cmdlen] = NUL;
1083 redrawcmd();
1084 goto cmdline_changed;
1085
1086#ifdef FEAT_CLIPBOARD
1087 case Ctrl_Y:
1088 /* Copy the modeless selection, if there is one. */
1089 if (clip_star.state != SELECT_CLEARED)
1090 {
1091 if (clip_star.state == SELECT_DONE)
1092 clip_copy_modeless_selection(TRUE);
1093 goto cmdline_not_changed;
1094 }
1095 break;
1096#endif
1097
1098 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1099 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001100 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001101 * ":normal" runs out of characters. */
1102 if (exmode_active
1103#ifdef FEAT_EX_EXTRA
1104 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1105#endif
1106 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 goto cmdline_not_changed;
1108
1109 gotesc = TRUE; /* will free ccline.cmdbuff after
1110 putting it in history */
1111 goto returncmd; /* back to cmd mode */
1112
1113 case Ctrl_R: /* insert register */
1114#ifdef USE_ON_FLY_SCROLL
1115 dont_scroll = TRUE; /* disallow scrolling here */
1116#endif
1117 putcmdline('"', TRUE);
1118 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001119 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 if (i == Ctrl_O)
1121 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1122 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001123 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 --no_mapping;
1125#ifdef FEAT_EVAL
1126 /*
1127 * Insert the result of an expression.
1128 * Need to save the current command line, to be able to enter
1129 * a new one...
1130 */
1131 new_cmdpos = -1;
1132 if (c == '=')
1133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1135 {
1136 beep_flush();
1137 c = ESC;
1138 }
1139 else
1140 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001141 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001143 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
1145 }
1146#endif
1147 if (c != ESC) /* use ESC to cancel inserting register */
1148 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001149 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001150
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001151#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001152 /* When there was a serious error abort getting the
1153 * command line. */
1154 if (aborting())
1155 {
1156 gotesc = TRUE; /* will free ccline.cmdbuff after
1157 putting it in history */
1158 goto returncmd; /* back to cmd mode */
1159 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 KeyTyped = FALSE; /* Don't do p_wc completion. */
1162#ifdef FEAT_EVAL
1163 if (new_cmdpos >= 0)
1164 {
1165 /* set_cmdline_pos() was used */
1166 if (new_cmdpos > ccline.cmdlen)
1167 ccline.cmdpos = ccline.cmdlen;
1168 else
1169 ccline.cmdpos = new_cmdpos;
1170 }
1171#endif
1172 }
1173 redrawcmd();
1174 goto cmdline_changed;
1175
1176 case Ctrl_D:
1177 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1178 break; /* Use ^D as normal char instead */
1179
1180 redrawcmd();
1181 continue; /* don't do incremental search now */
1182
1183 case K_RIGHT:
1184 case K_S_RIGHT:
1185 case K_C_RIGHT:
1186 do
1187 {
1188 if (ccline.cmdpos >= ccline.cmdlen)
1189 break;
1190 i = cmdline_charsize(ccline.cmdpos);
1191 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1192 break;
1193 ccline.cmdspos += i;
1194#ifdef FEAT_MBYTE
1195 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001196 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 + ccline.cmdpos);
1198 else
1199#endif
1200 ++ccline.cmdpos;
1201 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001202 while ((c == K_S_RIGHT || c == K_C_RIGHT
1203 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1205#ifdef FEAT_MBYTE
1206 if (has_mbyte)
1207 set_cmdspos_cursor();
1208#endif
1209 goto cmdline_not_changed;
1210
1211 case K_LEFT:
1212 case K_S_LEFT:
1213 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001214 if (ccline.cmdpos == 0)
1215 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 do
1217 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 --ccline.cmdpos;
1219#ifdef FEAT_MBYTE
1220 if (has_mbyte) /* move to first byte of char */
1221 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1222 ccline.cmdbuff + ccline.cmdpos);
1223#endif
1224 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1225 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001226 while (ccline.cmdpos > 0
1227 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001228 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1230#ifdef FEAT_MBYTE
1231 if (has_mbyte)
1232 set_cmdspos_cursor();
1233#endif
1234 goto cmdline_not_changed;
1235
1236 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001237 /* Ignore mouse event or ex_window() result. */
1238 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
Bram Moolenaar4770d092006-01-12 23:22:24 +00001240#ifdef FEAT_GUI_W32
1241 /* On Win32 ignore <M-F4>, we get it when closing the window was
1242 * cancelled. */
1243 case K_F4:
1244 if (mod_mask == MOD_MASK_ALT)
1245 {
1246 redrawcmd(); /* somehow the cmdline is cleared */
1247 goto cmdline_not_changed;
1248 }
1249 break;
1250#endif
1251
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252#ifdef FEAT_MOUSE
1253 case K_MIDDLEDRAG:
1254 case K_MIDDLERELEASE:
1255 goto cmdline_not_changed; /* Ignore mouse */
1256
1257 case K_MIDDLEMOUSE:
1258# ifdef FEAT_GUI
1259 /* When GUI is active, also paste when 'mouse' is empty */
1260 if (!gui.in_use)
1261# endif
1262 if (!mouse_has(MOUSE_COMMAND))
1263 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001264# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001266 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001268# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001269 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 redrawcmd();
1271 goto cmdline_changed;
1272
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001273# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001275 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 redrawcmd();
1277 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001278# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
1280 case K_LEFTDRAG:
1281 case K_LEFTRELEASE:
1282 case K_RIGHTDRAG:
1283 case K_RIGHTRELEASE:
1284 /* Ignore drag and release events when the button-down wasn't
1285 * seen before. */
1286 if (ignore_drag_release)
1287 goto cmdline_not_changed;
1288 /* FALLTHROUGH */
1289 case K_LEFTMOUSE:
1290 case K_RIGHTMOUSE:
1291 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1292 ignore_drag_release = TRUE;
1293 else
1294 ignore_drag_release = FALSE;
1295# ifdef FEAT_GUI
1296 /* When GUI is active, also move when 'mouse' is empty */
1297 if (!gui.in_use)
1298# endif
1299 if (!mouse_has(MOUSE_COMMAND))
1300 goto cmdline_not_changed; /* Ignore mouse */
1301# ifdef FEAT_CLIPBOARD
1302 if (mouse_row < cmdline_row && clip_star.available)
1303 {
1304 int button, is_click, is_drag;
1305
1306 /*
1307 * Handle modeless selection.
1308 */
1309 button = get_mouse_button(KEY2TERMCAP1(c),
1310 &is_click, &is_drag);
1311 if (mouse_model_popup() && button == MOUSE_LEFT
1312 && (mod_mask & MOD_MASK_SHIFT))
1313 {
1314 /* Translate shift-left to right button. */
1315 button = MOUSE_RIGHT;
1316 mod_mask &= ~MOD_MASK_SHIFT;
1317 }
1318 clip_modeless(button, is_click, is_drag);
1319 goto cmdline_not_changed;
1320 }
1321# endif
1322
1323 set_cmdspos();
1324 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1325 ++ccline.cmdpos)
1326 {
1327 i = cmdline_charsize(ccline.cmdpos);
1328 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1329 && mouse_col < ccline.cmdspos % Columns + i)
1330 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001331# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 if (has_mbyte)
1333 {
1334 /* Count ">" for double-wide char that doesn't fit. */
1335 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001336 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 + ccline.cmdpos) - 1;
1338 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001339# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 ccline.cmdspos += i;
1341 }
1342 goto cmdline_not_changed;
1343
1344 /* Mouse scroll wheel: ignored here */
1345 case K_MOUSEDOWN:
1346 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001347 case K_MOUSELEFT:
1348 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 /* Alternate buttons ignored here */
1350 case K_X1MOUSE:
1351 case K_X1DRAG:
1352 case K_X1RELEASE:
1353 case K_X2MOUSE:
1354 case K_X2DRAG:
1355 case K_X2RELEASE:
1356 goto cmdline_not_changed;
1357
1358#endif /* FEAT_MOUSE */
1359
1360#ifdef FEAT_GUI
1361 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1362 case K_LEFTRELEASE_NM:
1363 goto cmdline_not_changed;
1364
1365 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001366 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
1368 gui_do_scroll();
1369 redrawcmd();
1370 }
1371 goto cmdline_not_changed;
1372
1373 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001374 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001376 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 redrawcmd();
1378 }
1379 goto cmdline_not_changed;
1380#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001381#ifdef FEAT_GUI_TABLINE
1382 case K_TABLINE:
1383 case K_TABMENU:
1384 /* Don't want to change any tabs here. Make sure the same tab
1385 * is still selected. */
1386 if (gui_use_tabline())
1387 gui_mch_set_curtab(tabpage_index(curtab));
1388 goto cmdline_not_changed;
1389#endif
1390
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 case K_SELECT: /* end of Select mode mapping - ignore */
1392 goto cmdline_not_changed;
1393
1394 case Ctrl_B: /* begin of command line */
1395 case K_HOME:
1396 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 case K_S_HOME:
1398 case K_C_HOME:
1399 ccline.cmdpos = 0;
1400 set_cmdspos();
1401 goto cmdline_not_changed;
1402
1403 case Ctrl_E: /* end of command line */
1404 case K_END:
1405 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 case K_S_END:
1407 case K_C_END:
1408 ccline.cmdpos = ccline.cmdlen;
1409 set_cmdspos_cursor();
1410 goto cmdline_not_changed;
1411
1412 case Ctrl_A: /* all matches */
1413 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1414 break;
1415 goto cmdline_changed;
1416
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001417 case Ctrl_L:
1418#ifdef FEAT_SEARCH_EXTRA
1419 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1420 {
1421 /* Add a character from under the cursor for 'incsearch' */
1422 if (did_incsearch
1423 && !equalpos(curwin->w_cursor, old_cursor))
1424 {
1425 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001426 /* If 'ignorecase' and 'smartcase' are set and the
1427 * command line has no uppercase characters, convert
1428 * the character to lowercase */
1429 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1430 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001431 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001432 {
1433 if (c == firstc || vim_strchr((char_u *)(
1434 p_magic ? "\\^$.*[" : "\\^$"), c)
1435 != NULL)
1436 {
1437 /* put a backslash before special characters */
1438 stuffcharReadbuff(c);
1439 c = '\\';
1440 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001441 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001442 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001443 }
1444 goto cmdline_not_changed;
1445 }
1446#endif
1447
1448 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1450 break;
1451 goto cmdline_changed;
1452
1453 case Ctrl_N: /* next match */
1454 case Ctrl_P: /* previous match */
1455 if (xpc.xp_numfiles > 0)
1456 {
1457 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1458 == FAIL)
1459 break;
1460 goto cmdline_changed;
1461 }
1462
1463#ifdef FEAT_CMDHIST
1464 case K_UP:
1465 case K_DOWN:
1466 case K_S_UP:
1467 case K_S_DOWN:
1468 case K_PAGEUP:
1469 case K_KPAGEUP:
1470 case K_PAGEDOWN:
1471 case K_KPAGEDOWN:
1472 if (hislen == 0 || firstc == NUL) /* no history */
1473 goto cmdline_not_changed;
1474
1475 i = hiscnt;
1476
1477 /* save current command string so it can be restored later */
1478 if (lookfor == NULL)
1479 {
1480 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1481 goto cmdline_not_changed;
1482 lookfor[ccline.cmdpos] = NUL;
1483 }
1484
1485 j = (int)STRLEN(lookfor);
1486 for (;;)
1487 {
1488 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001489 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001490 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 {
1492 if (hiscnt == hislen) /* first time */
1493 hiscnt = hisidx[histype];
1494 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1495 hiscnt = hislen - 1;
1496 else if (hiscnt != hisidx[histype] + 1)
1497 --hiscnt;
1498 else /* at top of list */
1499 {
1500 hiscnt = i;
1501 break;
1502 }
1503 }
1504 else /* one step forwards */
1505 {
1506 /* on last entry, clear the line */
1507 if (hiscnt == hisidx[histype])
1508 {
1509 hiscnt = hislen;
1510 break;
1511 }
1512
1513 /* not on a history line, nothing to do */
1514 if (hiscnt == hislen)
1515 break;
1516 if (hiscnt == hislen - 1) /* wrap around */
1517 hiscnt = 0;
1518 else
1519 ++hiscnt;
1520 }
1521 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1522 {
1523 hiscnt = i;
1524 break;
1525 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001526 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001527 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 || STRNCMP(history[histype][hiscnt].hisstr,
1529 lookfor, (size_t)j) == 0)
1530 break;
1531 }
1532
1533 if (hiscnt != i) /* jumped to other entry */
1534 {
1535 char_u *p;
1536 int len;
1537 int old_firstc;
1538
1539 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001540 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 if (hiscnt == hislen)
1542 p = lookfor; /* back to the old one */
1543 else
1544 p = history[histype][hiscnt].hisstr;
1545
1546 if (histype == HIST_SEARCH
1547 && p != lookfor
1548 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1549 {
1550 /* Correct for the separator character used when
1551 * adding the history entry vs the one used now.
1552 * First loop: count length.
1553 * Second loop: copy the characters. */
1554 for (i = 0; i <= 1; ++i)
1555 {
1556 len = 0;
1557 for (j = 0; p[j] != NUL; ++j)
1558 {
1559 /* Replace old sep with new sep, unless it is
1560 * escaped. */
1561 if (p[j] == old_firstc
1562 && (j == 0 || p[j - 1] != '\\'))
1563 {
1564 if (i > 0)
1565 ccline.cmdbuff[len] = firstc;
1566 }
1567 else
1568 {
1569 /* Escape new sep, unless it is already
1570 * escaped. */
1571 if (p[j] == firstc
1572 && (j == 0 || p[j - 1] != '\\'))
1573 {
1574 if (i > 0)
1575 ccline.cmdbuff[len] = '\\';
1576 ++len;
1577 }
1578 if (i > 0)
1579 ccline.cmdbuff[len] = p[j];
1580 }
1581 ++len;
1582 }
1583 if (i == 0)
1584 {
1585 alloc_cmdbuff(len);
1586 if (ccline.cmdbuff == NULL)
1587 goto returncmd;
1588 }
1589 }
1590 ccline.cmdbuff[len] = NUL;
1591 }
1592 else
1593 {
1594 alloc_cmdbuff((int)STRLEN(p));
1595 if (ccline.cmdbuff == NULL)
1596 goto returncmd;
1597 STRCPY(ccline.cmdbuff, p);
1598 }
1599
1600 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1601 redrawcmd();
1602 goto cmdline_changed;
1603 }
1604 beep_flush();
1605 goto cmdline_not_changed;
1606#endif
1607
1608 case Ctrl_V:
1609 case Ctrl_Q:
1610#ifdef FEAT_MOUSE
1611 ignore_drag_release = TRUE;
1612#endif
1613 putcmdline('^', TRUE);
1614 c = get_literal(); /* get next (two) character(s) */
1615 do_abbr = FALSE; /* don't do abbreviation now */
1616#ifdef FEAT_MBYTE
1617 /* may need to remove ^ when composing char was typed */
1618 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1619 {
1620 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1621 msg_putchar(' ');
1622 cursorcmd();
1623 }
1624#endif
1625 break;
1626
1627#ifdef FEAT_DIGRAPHS
1628 case Ctrl_K:
1629#ifdef FEAT_MOUSE
1630 ignore_drag_release = TRUE;
1631#endif
1632 putcmdline('?', TRUE);
1633#ifdef USE_ON_FLY_SCROLL
1634 dont_scroll = TRUE; /* disallow scrolling here */
1635#endif
1636 c = get_digraph(TRUE);
1637 if (c != NUL)
1638 break;
1639
1640 redrawcmd();
1641 goto cmdline_not_changed;
1642#endif /* FEAT_DIGRAPHS */
1643
1644#ifdef FEAT_RIGHTLEFT
1645 case Ctrl__: /* CTRL-_: switch language mode */
1646 if (!p_ari)
1647 break;
1648#ifdef FEAT_FKMAP
1649 if (p_altkeymap)
1650 {
1651 cmd_fkmap = !cmd_fkmap;
1652 if (cmd_fkmap) /* in Farsi always in Insert mode */
1653 ccline.overstrike = FALSE;
1654 }
1655 else /* Hebrew is default */
1656#endif
1657 cmd_hkmap = !cmd_hkmap;
1658 goto cmdline_not_changed;
1659#endif
1660
1661 default:
1662#ifdef UNIX
1663 if (c == intr_char)
1664 {
1665 gotesc = TRUE; /* will free ccline.cmdbuff after
1666 putting it in history */
1667 goto returncmd; /* back to Normal mode */
1668 }
1669#endif
1670 /*
1671 * Normal character with no special meaning. Just set mod_mask
1672 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1673 * the string <S-Space>. This should only happen after ^V.
1674 */
1675 if (!IS_SPECIAL(c))
1676 mod_mask = 0x0;
1677 break;
1678 }
1679 /*
1680 * End of switch on command line character.
1681 * We come here if we have a normal character.
1682 */
1683
1684 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1685#ifdef FEAT_MBYTE
1686 /* Add ABBR_OFF for characters above 0x100, this is
1687 * what check_abbr() expects. */
1688 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1689#endif
1690 c))
1691 goto cmdline_changed;
1692
1693 /*
1694 * put the character in the command line
1695 */
1696 if (IS_SPECIAL(c) || mod_mask != 0)
1697 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1698 else
1699 {
1700#ifdef FEAT_MBYTE
1701 if (has_mbyte)
1702 {
1703 j = (*mb_char2bytes)(c, IObuff);
1704 IObuff[j] = NUL; /* exclude composing chars */
1705 put_on_cmdline(IObuff, j, TRUE);
1706 }
1707 else
1708#endif
1709 {
1710 IObuff[0] = c;
1711 put_on_cmdline(IObuff, 1, TRUE);
1712 }
1713 }
1714 goto cmdline_changed;
1715
1716/*
1717 * This part implements incremental searches for "/" and "?"
1718 * Jump to cmdline_not_changed when a character has been read but the command
1719 * line did not change. Then we only search and redraw if something changed in
1720 * the past.
1721 * Jump to cmdline_changed when the command line did change.
1722 * (Sorry for the goto's, I know it is ugly).
1723 */
1724cmdline_not_changed:
1725#ifdef FEAT_SEARCH_EXTRA
1726 if (!incsearch_postponed)
1727 continue;
1728#endif
1729
1730cmdline_changed:
1731#ifdef FEAT_SEARCH_EXTRA
1732 /*
1733 * 'incsearch' highlighting.
1734 */
1735 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1736 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001737 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001738#ifdef FEAT_RELTIME
1739 proftime_T tm;
1740#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001741
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 /* if there is a character waiting, search and redraw later */
1743 if (char_avail())
1744 {
1745 incsearch_postponed = TRUE;
1746 continue;
1747 }
1748 incsearch_postponed = FALSE;
1749 curwin->w_cursor = old_cursor; /* start at old position */
1750
1751 /* If there is no command line, don't do anything */
1752 if (ccline.cmdlen == 0)
1753 i = 0;
1754 else
1755 {
1756 cursor_off(); /* so the user knows we're busy */
1757 out_flush();
1758 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001759#ifdef FEAT_RELTIME
1760 /* Set the time limit to half a second. */
1761 profile_setlimit(500L, &tm);
1762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001764 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1765#ifdef FEAT_RELTIME
1766 &tm
1767#else
1768 NULL
1769#endif
1770 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 --emsg_off;
1772 /* if interrupted while searching, behave like it failed */
1773 if (got_int)
1774 {
1775 (void)vpeekc(); /* remove <C-C> from input stream */
1776 got_int = FALSE; /* don't abandon the command line */
1777 i = 0;
1778 }
1779 else if (char_avail())
1780 /* cancelled searching because a char was typed */
1781 incsearch_postponed = TRUE;
1782 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001783 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 highlight_match = TRUE; /* highlight position */
1785 else
1786 highlight_match = FALSE; /* remove highlight */
1787
1788 /* first restore the old curwin values, so the screen is
1789 * positioned in the same way as the actual search command */
1790 curwin->w_leftcol = old_leftcol;
1791 curwin->w_topline = old_topline;
1792# ifdef FEAT_DIFF
1793 curwin->w_topfill = old_topfill;
1794# endif
1795 curwin->w_botline = old_botline;
1796 changed_cline_bef_curs();
1797 update_topline();
1798
1799 if (i != 0)
1800 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001801 pos_T save_pos = curwin->w_cursor;
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001804 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 * moves the whole match onto the screen when 'nowrap' is set.
1806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 curwin->w_cursor.lnum += search_match_lines;
1808 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001809 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1810 {
1811 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1812 coladvance((colnr_T)MAXCOL);
1813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001815 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001816 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001818 else
1819 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001822# ifdef FEAT_WINDOWS
1823 /* May redraw the status line to show the cursor position. */
1824 if (p_ru && curwin->w_status_height > 0)
1825 curwin->w_redr_status = TRUE;
1826# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001828 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001829 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001830 restore_cmdline(&save_ccline);
1831
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001832 /* Leave it at the end to make CTRL-R CTRL-W work. */
1833 if (i != 0)
1834 curwin->w_cursor = end_pos;
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 msg_starthere();
1837 redrawcmdline();
1838 did_incsearch = TRUE;
1839 }
1840#else /* FEAT_SEARCH_EXTRA */
1841 ;
1842#endif
1843
1844#ifdef FEAT_RIGHTLEFT
1845 if (cmdmsg_rl
1846# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001847 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848# endif
1849 )
1850 /* Always redraw the whole command line to fix shaping and
1851 * right-left typing. Not efficient, but it works. */
1852 redrawcmd();
1853#endif
1854 }
1855
1856returncmd:
1857
1858#ifdef FEAT_RIGHTLEFT
1859 cmdmsg_rl = FALSE;
1860#endif
1861
1862#ifdef FEAT_FKMAP
1863 cmd_fkmap = 0;
1864#endif
1865
1866 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001867 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
1869#ifdef FEAT_SEARCH_EXTRA
1870 if (did_incsearch)
1871 {
1872 curwin->w_cursor = old_cursor;
1873 curwin->w_curswant = old_curswant;
1874 curwin->w_leftcol = old_leftcol;
1875 curwin->w_topline = old_topline;
1876# ifdef FEAT_DIFF
1877 curwin->w_topfill = old_topfill;
1878# endif
1879 curwin->w_botline = old_botline;
1880 highlight_match = FALSE;
1881 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001882 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 }
1884#endif
1885
1886 if (ccline.cmdbuff != NULL)
1887 {
1888 /*
1889 * Put line in history buffer (":" and "=" only when it was typed).
1890 */
1891#ifdef FEAT_CMDHIST
1892 if (ccline.cmdlen && firstc != NUL
1893 && (some_key_typed || histype == HIST_SEARCH))
1894 {
1895 add_to_history(histype, ccline.cmdbuff, TRUE,
1896 histype == HIST_SEARCH ? firstc : NUL);
1897 if (firstc == ':')
1898 {
1899 vim_free(new_last_cmdline);
1900 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1901 }
1902 }
1903#endif
1904
1905 if (gotesc) /* abandon command line */
1906 {
1907 vim_free(ccline.cmdbuff);
1908 ccline.cmdbuff = NULL;
1909 if (msg_scrolled == 0)
1910 compute_cmdrow();
1911 MSG("");
1912 redraw_cmdline = TRUE;
1913 }
1914 }
1915
1916 /*
1917 * If the screen was shifted up, redraw the whole screen (later).
1918 * If the line is too long, clear it, so ruler and shown command do
1919 * not get printed in the middle of it.
1920 */
1921 msg_check();
1922 msg_scroll = save_msg_scroll;
1923 redir_off = FALSE;
1924
1925 /* When the command line was typed, no need for a wait-return prompt. */
1926 if (some_key_typed)
1927 need_wait_return = FALSE;
1928
1929 State = save_State;
1930#ifdef USE_IM_CONTROL
1931 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1932 im_save_status(b_im_ptr);
1933 im_set_active(FALSE);
1934#endif
1935#ifdef FEAT_MOUSE
1936 setmouse();
1937#endif
1938#ifdef CURSOR_SHAPE
1939 ui_cursor_shape(); /* may show different cursor shape */
1940#endif
1941
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001942 {
1943 char_u *p = ccline.cmdbuff;
1944
1945 /* Make ccline empty, getcmdline() may try to use it. */
1946 ccline.cmdbuff = NULL;
1947 return p;
1948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949}
1950
1951#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1952/*
1953 * Get a command line with a prompt.
1954 * This is prepared to be called recursively from getcmdline() (e.g. by
1955 * f_input() when evaluating an expression from CTRL-R =).
1956 * Returns the command line in allocated memory, or NULL.
1957 */
1958 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001959getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 int firstc;
1961 char_u *prompt; /* command line prompt */
1962 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001963 int xp_context; /* type of expansion */
1964 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965{
1966 char_u *s;
1967 struct cmdline_info save_ccline;
1968 int msg_col_save = msg_col;
1969
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001970 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 ccline.cmdprompt = prompt;
1972 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001973# ifdef FEAT_EVAL
1974 ccline.xp_context = xp_context;
1975 ccline.xp_arg = xp_arg;
1976 ccline.input_fn = (firstc == '@');
1977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001979 restore_cmdline(&save_ccline);
Bram Moolenaar1db1f772011-08-17 16:25:48 +02001980 /* Restore msg_col, the prompt from input() may have changed it.
1981 * But only if called recursively and the commandline is therefore being
1982 * restored to an old one; if not, the input() prompt stays on the screen,
1983 * so we need its modified msg_col left intact. */
1984 if (ccline.cmdbuff != NULL)
1985 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986
1987 return s;
1988}
1989#endif
1990
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001991/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001992 * Return TRUE when the text must not be changed and we can't switch to
1993 * another window or buffer. Used when editing the command line, evaluating
1994 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001995 */
1996 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001997text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001998{
1999#ifdef FEAT_CMDWIN
2000 if (cmdwin_type != 0)
2001 return TRUE;
2002#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002003 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002004}
2005
2006/*
2007 * Give an error message for a command that isn't allowed while the cmdline
2008 * window is open or editing the cmdline in another way.
2009 */
2010 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002011text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002012{
2013#ifdef FEAT_CMDWIN
2014 if (cmdwin_type != 0)
2015 EMSG(_(e_cmdwin));
2016 else
2017#endif
2018 EMSG(_(e_secure));
2019}
2020
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002021#if defined(FEAT_AUTOCMD) || defined(PROTO)
2022/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002023 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2024 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002025 */
2026 int
2027curbuf_locked()
2028{
2029 if (curbuf_lock > 0)
2030 {
2031 EMSG(_("E788: Not allowed to edit another buffer now"));
2032 return TRUE;
2033 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002034 return allbuf_locked();
2035}
2036
2037/*
2038 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2039 * message.
2040 */
2041 int
2042allbuf_locked()
2043{
2044 if (allbuf_lock > 0)
2045 {
2046 EMSG(_("E811: Not allowed to change buffer information now"));
2047 return TRUE;
2048 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002049 return FALSE;
2050}
2051#endif
2052
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 static int
2054cmdline_charsize(idx)
2055 int idx;
2056{
2057#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2058 if (cmdline_star > 0) /* showing '*', always 1 position */
2059 return 1;
2060#endif
2061 return ptr2cells(ccline.cmdbuff + idx);
2062}
2063
2064/*
2065 * Compute the offset of the cursor on the command line for the prompt and
2066 * indent.
2067 */
2068 static void
2069set_cmdspos()
2070{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002071 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 ccline.cmdspos = 1 + ccline.cmdindent;
2073 else
2074 ccline.cmdspos = 0 + ccline.cmdindent;
2075}
2076
2077/*
2078 * Compute the screen position for the cursor on the command line.
2079 */
2080 static void
2081set_cmdspos_cursor()
2082{
2083 int i, m, c;
2084
2085 set_cmdspos();
2086 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002089 if (m < 0) /* overflow, Columns or Rows at weird value */
2090 m = MAXCOL;
2091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 else
2093 m = MAXCOL;
2094 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2095 {
2096 c = cmdline_charsize(i);
2097#ifdef FEAT_MBYTE
2098 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2099 if (has_mbyte)
2100 correct_cmdspos(i, c);
2101#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002102 /* If the cmdline doesn't fit, show cursor on last visible char.
2103 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 if ((ccline.cmdspos += c) >= m)
2105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 ccline.cmdspos -= c;
2107 break;
2108 }
2109#ifdef FEAT_MBYTE
2110 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002111 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112#endif
2113 }
2114}
2115
2116#ifdef FEAT_MBYTE
2117/*
2118 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2119 * character that doesn't fit, so that a ">" must be displayed.
2120 */
2121 static void
2122correct_cmdspos(idx, cells)
2123 int idx;
2124 int cells;
2125{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002126 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2128 && ccline.cmdspos % Columns + cells > Columns)
2129 ccline.cmdspos++;
2130}
2131#endif
2132
2133/*
2134 * Get an Ex command line for the ":" command.
2135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002137getexline(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 int c; /* normally ':', NUL for ":append" */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002139 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 int indent; /* indent for inside conditionals */
2141{
2142 /* When executing a register, remove ':' that's in front of each line. */
2143 if (exec_from_reg && vpeekc() == ':')
2144 (void)vgetc();
2145 return getcmdline(c, 1L, indent);
2146}
2147
2148/*
2149 * Get an Ex command line for Ex mode.
2150 * In Ex mode we only use the OS supplied line editing features and no
2151 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002152 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002155getexmodeline(promptc, cookie, indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002156 int promptc; /* normally ':', NUL for ":append" and '?' for
2157 :s prompt */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002158 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 int indent; /* indent for inside conditionals */
2160{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002161 garray_T line_ga;
2162 char_u *pend;
2163 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002164 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002165 int escaped = FALSE; /* CTRL-V typed */
2166 int vcol = 0;
2167 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002168 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169
2170 /* Switch cursor on now. This avoids that it happens after the "\n", which
2171 * confuses the system function that computes tabstops. */
2172 cursor_on();
2173
2174 /* always start in column 0; write a newline if necessary */
2175 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002176 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002178 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002180 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002181 if (p_prompt)
2182 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 while (indent-- > 0)
2184 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
2187
2188 ga_init2(&line_ga, 1, 30);
2189
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002190 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002191 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002192 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002193 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002194 while (indent >= 8)
2195 {
2196 ga_append(&line_ga, TAB);
2197 msg_puts((char_u *)" ");
2198 indent -= 8;
2199 }
2200 while (indent-- > 0)
2201 {
2202 ga_append(&line_ga, ' ');
2203 msg_putchar(' ');
2204 }
2205 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002206 ++no_mapping;
2207 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002208
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 /*
2210 * Get the line, one character at a time.
2211 */
2212 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002213 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 {
2215 if (ga_grow(&line_ga, 40) == FAIL)
2216 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002218 /* Get one character at a time. Don't use inchar(), it can't handle
2219 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002220 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002221 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002224 * Handle line editing.
2225 * Previously this was left to the system, putting the terminal in
2226 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002228 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002230 msg_putchar('\n');
2231 break;
2232 }
2233
2234 if (!escaped)
2235 {
2236 /* CR typed means "enter", which is NL */
2237 if (c1 == '\r')
2238 c1 = '\n';
2239
2240 if (c1 == BS || c1 == K_BS
2241 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002243 if (line_ga.ga_len > 0)
2244 {
2245 --line_ga.ga_len;
2246 goto redraw;
2247 }
2248 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 }
2250
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002251 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002253 msg_col = startcol;
2254 msg_clr_eos();
2255 line_ga.ga_len = 0;
2256 continue;
2257 }
2258
2259 if (c1 == Ctrl_T)
2260 {
2261 p = (char_u *)line_ga.ga_data;
2262 p[line_ga.ga_len] = NUL;
2263 indent = get_indent_str(p, 8);
2264 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2265add_indent:
2266 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002268 char_u *s = skipwhite(p);
2269
2270 ga_grow(&line_ga, 1);
2271 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2272 *s = ' ';
2273 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002275redraw:
2276 /* redraw the line */
2277 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002278 vcol = 0;
2279 for (p = (char_u *)line_ga.ga_data;
2280 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002282 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002284 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002286 msg_putchar(' ');
2287 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002289 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002291 msg_outtrans_len(p, 1);
2292 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
2294 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002295 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002296 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002297 continue;
2298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002300 if (c1 == Ctrl_D)
2301 {
2302 /* Delete one shiftwidth. */
2303 p = (char_u *)line_ga.ga_data;
2304 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002306 if (prev_char == '^')
2307 ex_keep_indent = TRUE;
2308 indent = 0;
2309 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 }
2311 else
2312 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002313 p[line_ga.ga_len] = NUL;
2314 indent = get_indent_str(p, 8);
2315 --indent;
2316 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002318 while (get_indent_str(p, 8) > indent)
2319 {
2320 char_u *s = skipwhite(p);
2321
2322 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2323 --line_ga.ga_len;
2324 }
2325 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002327
2328 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2329 {
2330 escaped = TRUE;
2331 continue;
2332 }
2333
2334 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2335 if (IS_SPECIAL(c1))
2336 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002338
2339 if (IS_SPECIAL(c1))
2340 c1 = '?';
2341 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002342 if (c1 == '\n')
2343 msg_putchar('\n');
2344 else if (c1 == TAB)
2345 {
2346 /* Don't use chartabsize(), 'ts' can be different */
2347 do
2348 {
2349 msg_putchar(' ');
2350 } while (++vcol % 8);
2351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002354 msg_outtrans_len(
2355 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2356 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002358 ++line_ga.ga_len;
2359 escaped = FALSE;
2360
2361 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002362 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002363
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002364 /* We are done when a NL is entered, but not when it comes after an
2365 * odd number of backslashes, that results in a NUL. */
2366 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002368 int bcount = 0;
2369
2370 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2371 ++bcount;
2372
2373 if (bcount > 0)
2374 {
2375 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2376 * "\NL", etc. */
2377 line_ga.ga_len -= (bcount + 1) / 2;
2378 pend -= (bcount + 1) / 2;
2379 pend[-1] = '\n';
2380 }
2381
2382 if ((bcount & 1) == 0)
2383 {
2384 --line_ga.ga_len;
2385 --pend;
2386 *pend = NUL;
2387 break;
2388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 }
2390 }
2391
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002392 --no_mapping;
2393 --allow_keys;
2394
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 /* make following messages go to the next line */
2396 msg_didout = FALSE;
2397 msg_col = 0;
2398 if (msg_row < Rows - 1)
2399 ++msg_row;
2400 emsg_on_display = FALSE; /* don't want ui_delay() */
2401
2402 if (got_int)
2403 ga_clear(&line_ga);
2404
2405 return (char_u *)line_ga.ga_data;
2406}
2407
Bram Moolenaare344bea2005-09-01 20:46:49 +00002408# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2409 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410/*
2411 * Return TRUE if ccline.overstrike is on.
2412 */
2413 int
2414cmdline_overstrike()
2415{
2416 return ccline.overstrike;
2417}
2418
2419/*
2420 * Return TRUE if the cursor is at the end of the cmdline.
2421 */
2422 int
2423cmdline_at_end()
2424{
2425 return (ccline.cmdpos >= ccline.cmdlen);
2426}
2427#endif
2428
Bram Moolenaar9372a112005-12-06 19:59:18 +00002429#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430/*
2431 * Return the virtual column number at the current cursor position.
2432 * This is used by the IM code to obtain the start of the preedit string.
2433 */
2434 colnr_T
2435cmdline_getvcol_cursor()
2436{
2437 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2438 return MAXCOL;
2439
2440# ifdef FEAT_MBYTE
2441 if (has_mbyte)
2442 {
2443 colnr_T col;
2444 int i = 0;
2445
2446 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002447 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 return col;
2450 }
2451 else
2452# endif
2453 return ccline.cmdpos;
2454}
2455#endif
2456
2457#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2458/*
2459 * If part of the command line is an IM preedit string, redraw it with
2460 * IM feedback attributes. The cursor position is restored after drawing.
2461 */
2462 static void
2463redrawcmd_preedit()
2464{
2465 if ((State & CMDLINE)
2466 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002467 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 && !p_imdisable
2469 && im_is_preediting())
2470 {
2471 int cmdpos = 0;
2472 int cmdspos;
2473 int old_row;
2474 int old_col;
2475 colnr_T col;
2476
2477 old_row = msg_row;
2478 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002479 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481# ifdef FEAT_MBYTE
2482 if (has_mbyte)
2483 {
2484 for (col = 0; col < preedit_start_col
2485 && cmdpos < ccline.cmdlen; ++col)
2486 {
2487 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002488 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 }
2490 }
2491 else
2492# endif
2493 {
2494 cmdspos += preedit_start_col;
2495 cmdpos += preedit_start_col;
2496 }
2497
2498 msg_row = cmdline_row + (cmdspos / (int)Columns);
2499 msg_col = cmdspos % (int)Columns;
2500 if (msg_row >= Rows)
2501 msg_row = Rows - 1;
2502
2503 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2504 {
2505 int char_len;
2506 int char_attr;
2507
2508 char_attr = im_get_feedback_attr(col);
2509 if (char_attr < 0)
2510 break; /* end of preedit string */
2511
2512# ifdef FEAT_MBYTE
2513 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002514 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 else
2516# endif
2517 char_len = 1;
2518
2519 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2520 cmdpos += char_len;
2521 }
2522
2523 msg_row = old_row;
2524 msg_col = old_col;
2525 }
2526}
2527#endif /* FEAT_XIM && FEAT_GUI_GTK */
2528
2529/*
2530 * Allocate a new command line buffer.
2531 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2532 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2533 */
2534 static void
2535alloc_cmdbuff(len)
2536 int len;
2537{
2538 /*
2539 * give some extra space to avoid having to allocate all the time
2540 */
2541 if (len < 80)
2542 len = 100;
2543 else
2544 len += 20;
2545
2546 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2547 ccline.cmdbufflen = len;
2548}
2549
2550/*
2551 * Re-allocate the command line to length len + something extra.
2552 * return FAIL for failure, OK otherwise
2553 */
2554 static int
2555realloc_cmdbuff(len)
2556 int len;
2557{
2558 char_u *p;
2559
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002560 if (len < ccline.cmdbufflen)
2561 return OK; /* no need to resize */
2562
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 p = ccline.cmdbuff;
2564 alloc_cmdbuff(len); /* will get some more */
2565 if (ccline.cmdbuff == NULL) /* out of memory */
2566 {
2567 ccline.cmdbuff = p; /* keep the old one */
2568 return FAIL;
2569 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002570 /* There isn't always a NUL after the command, but it may need to be
2571 * there, thus copy up to the NUL and add a NUL. */
2572 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2573 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002575
2576 if (ccline.xpc != NULL
2577 && ccline.xpc->xp_pattern != NULL
2578 && ccline.xpc->xp_context != EXPAND_NOTHING
2579 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2580 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002581 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002582
2583 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2584 * to point into the newly allocated memory. */
2585 if (i >= 0 && i <= ccline.cmdlen)
2586 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2587 }
2588
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 return OK;
2590}
2591
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002592#if defined(FEAT_ARABIC) || defined(PROTO)
2593static char_u *arshape_buf = NULL;
2594
2595# if defined(EXITFREE) || defined(PROTO)
2596 void
2597free_cmdline_buf()
2598{
2599 vim_free(arshape_buf);
2600}
2601# endif
2602#endif
2603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604/*
2605 * Draw part of the cmdline at the current cursor position. But draw stars
2606 * when cmdline_star is TRUE.
2607 */
2608 static void
2609draw_cmdline(start, len)
2610 int start;
2611 int len;
2612{
2613#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2614 int i;
2615
2616 if (cmdline_star > 0)
2617 for (i = 0; i < len; ++i)
2618 {
2619 msg_putchar('*');
2620# ifdef FEAT_MBYTE
2621 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002622 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623# endif
2624 }
2625 else
2626#endif
2627#ifdef FEAT_ARABIC
2628 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2629 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 static int buflen = 0;
2631 char_u *p;
2632 int j;
2633 int newlen = 0;
2634 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002635 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 int prev_c = 0;
2637 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002638 int u8c;
2639 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
2642 /*
2643 * Do arabic shaping into a temporary buffer. This is very
2644 * inefficient!
2645 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002646 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 {
2648 /* Re-allocate the buffer. We keep it around to avoid a lot of
2649 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002650 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002651 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002652 arshape_buf = alloc(buflen);
2653 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 return; /* out of memory */
2655 }
2656
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002657 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2658 {
2659 /* Prepend a space to draw the leading composing char on. */
2660 arshape_buf[0] = ' ';
2661 newlen = 1;
2662 }
2663
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 for (j = start; j < start + len; j += mb_l)
2665 {
2666 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002667 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002668 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 if (ARABIC_CHAR(u8c))
2670 {
2671 /* Do Arabic shaping. */
2672 if (cmdmsg_rl)
2673 {
2674 /* displaying from right to left */
2675 pc = prev_c;
2676 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002677 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (j + mb_l >= start + len)
2679 nc = NUL;
2680 else
2681 nc = utf_ptr2char(p + mb_l);
2682 }
2683 else
2684 {
2685 /* displaying from left to right */
2686 if (j + mb_l >= start + len)
2687 pc = NUL;
2688 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002689 {
2690 int pcc[MAX_MCO];
2691
2692 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002694 pc1 = pcc[0];
2695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 nc = prev_c;
2697 }
2698 prev_c = u8c;
2699
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002700 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002702 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002703 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002705 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2706 if (u8cc[1] != 0)
2707 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002708 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 }
2710 }
2711 else
2712 {
2713 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002714 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 newlen += mb_l;
2716 }
2717 }
2718
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002719 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721 else
2722#endif
2723 msg_outtrans_len(ccline.cmdbuff + start, len);
2724}
2725
2726/*
2727 * Put a character on the command line. Shifts the following text to the
2728 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2729 * "c" must be printable (fit in one display cell)!
2730 */
2731 void
2732putcmdline(c, shift)
2733 int c;
2734 int shift;
2735{
2736 if (cmd_silent)
2737 return;
2738 msg_no_more = TRUE;
2739 msg_putchar(c);
2740 if (shift)
2741 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2742 msg_no_more = FALSE;
2743 cursorcmd();
2744}
2745
2746/*
2747 * Undo a putcmdline(c, FALSE).
2748 */
2749 void
2750unputcmdline()
2751{
2752 if (cmd_silent)
2753 return;
2754 msg_no_more = TRUE;
2755 if (ccline.cmdlen == ccline.cmdpos)
2756 msg_putchar(' ');
2757 else
2758 draw_cmdline(ccline.cmdpos, 1);
2759 msg_no_more = FALSE;
2760 cursorcmd();
2761}
2762
2763/*
2764 * Put the given string, of the given length, onto the command line.
2765 * If len is -1, then STRLEN() is used to calculate the length.
2766 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2767 * part will be redrawn, otherwise it will not. If this function is called
2768 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2769 * called afterwards.
2770 */
2771 int
2772put_on_cmdline(str, len, redraw)
2773 char_u *str;
2774 int len;
2775 int redraw;
2776{
2777 int retval;
2778 int i;
2779 int m;
2780 int c;
2781
2782 if (len < 0)
2783 len = (int)STRLEN(str);
2784
2785 /* Check if ccline.cmdbuff needs to be longer */
2786 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002787 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 else
2789 retval = OK;
2790 if (retval == OK)
2791 {
2792 if (!ccline.overstrike)
2793 {
2794 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2795 ccline.cmdbuff + ccline.cmdpos,
2796 (size_t)(ccline.cmdlen - ccline.cmdpos));
2797 ccline.cmdlen += len;
2798 }
2799 else
2800 {
2801#ifdef FEAT_MBYTE
2802 if (has_mbyte)
2803 {
2804 /* Count nr of characters in the new string. */
2805 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002806 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 ++m;
2808 /* Count nr of bytes in cmdline that are overwritten by these
2809 * characters. */
2810 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002811 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 --m;
2813 if (i < ccline.cmdlen)
2814 {
2815 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2816 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2817 ccline.cmdlen += ccline.cmdpos + len - i;
2818 }
2819 else
2820 ccline.cmdlen = ccline.cmdpos + len;
2821 }
2822 else
2823#endif
2824 if (ccline.cmdpos + len > ccline.cmdlen)
2825 ccline.cmdlen = ccline.cmdpos + len;
2826 }
2827 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2828 ccline.cmdbuff[ccline.cmdlen] = NUL;
2829
2830#ifdef FEAT_MBYTE
2831 if (enc_utf8)
2832 {
2833 /* When the inserted text starts with a composing character,
2834 * backup to the character before it. There could be two of them.
2835 */
2836 i = 0;
2837 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2838 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2839 {
2840 i = (*mb_head_off)(ccline.cmdbuff,
2841 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2842 ccline.cmdpos -= i;
2843 len += i;
2844 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2845 }
2846# ifdef FEAT_ARABIC
2847 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2848 {
2849 /* Check the previous character for Arabic combining pair. */
2850 i = (*mb_head_off)(ccline.cmdbuff,
2851 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2852 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2853 + ccline.cmdpos - i), c))
2854 {
2855 ccline.cmdpos -= i;
2856 len += i;
2857 }
2858 else
2859 i = 0;
2860 }
2861# endif
2862 if (i != 0)
2863 {
2864 /* Also backup the cursor position. */
2865 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2866 ccline.cmdspos -= i;
2867 msg_col -= i;
2868 if (msg_col < 0)
2869 {
2870 msg_col += Columns;
2871 --msg_row;
2872 }
2873 }
2874 }
2875#endif
2876
2877 if (redraw && !cmd_silent)
2878 {
2879 msg_no_more = TRUE;
2880 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002881 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2883 /* Avoid clearing the rest of the line too often. */
2884 if (cmdline_row != i || ccline.overstrike)
2885 msg_clr_eos();
2886 msg_no_more = FALSE;
2887 }
2888#ifdef FEAT_FKMAP
2889 /*
2890 * If we are in Farsi command mode, the character input must be in
2891 * Insert mode. So do not advance the cmdpos.
2892 */
2893 if (!cmd_fkmap)
2894#endif
2895 {
2896 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002899 if (m < 0) /* overflow, Columns or Rows at weird value */
2900 m = MAXCOL;
2901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 else
2903 m = MAXCOL;
2904 for (i = 0; i < len; ++i)
2905 {
2906 c = cmdline_charsize(ccline.cmdpos);
2907#ifdef FEAT_MBYTE
2908 /* count ">" for a double-wide char that doesn't fit. */
2909 if (has_mbyte)
2910 correct_cmdspos(ccline.cmdpos, c);
2911#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002912 /* Stop cursor at the end of the screen, but do increment the
2913 * insert position, so that entering a very long command
2914 * works, even though you can't see it. */
2915 if (ccline.cmdspos + c < m)
2916 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917#ifdef FEAT_MBYTE
2918 if (has_mbyte)
2919 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002920 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 if (c > len - i - 1)
2922 c = len - i - 1;
2923 ccline.cmdpos += c;
2924 i += c;
2925 }
2926#endif
2927 ++ccline.cmdpos;
2928 }
2929 }
2930 }
2931 if (redraw)
2932 msg_check();
2933 return retval;
2934}
2935
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002936static struct cmdline_info prev_ccline;
2937static int prev_ccline_used = FALSE;
2938
2939/*
2940 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2941 * and overwrite it. But get_cmdline_str() may need it, thus make it
2942 * available globally in prev_ccline.
2943 */
2944 static void
2945save_cmdline(ccp)
2946 struct cmdline_info *ccp;
2947{
2948 if (!prev_ccline_used)
2949 {
2950 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2951 prev_ccline_used = TRUE;
2952 }
2953 *ccp = prev_ccline;
2954 prev_ccline = ccline;
2955 ccline.cmdbuff = NULL;
2956 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002957 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002958}
2959
2960/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002961 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002962 */
2963 static void
2964restore_cmdline(ccp)
2965 struct cmdline_info *ccp;
2966{
2967 ccline = prev_ccline;
2968 prev_ccline = *ccp;
2969}
2970
Bram Moolenaar5a305422006-04-28 22:38:25 +00002971#if defined(FEAT_EVAL) || defined(PROTO)
2972/*
2973 * Save the command line into allocated memory. Returns a pointer to be
2974 * passed to restore_cmdline_alloc() later.
2975 * Returns NULL when failed.
2976 */
2977 char_u *
2978save_cmdline_alloc()
2979{
2980 struct cmdline_info *p;
2981
2982 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2983 if (p != NULL)
2984 save_cmdline(p);
2985 return (char_u *)p;
2986}
2987
2988/*
2989 * Restore the command line from the return value of save_cmdline_alloc().
2990 */
2991 void
2992restore_cmdline_alloc(p)
2993 char_u *p;
2994{
2995 if (p != NULL)
2996 {
2997 restore_cmdline((struct cmdline_info *)p);
2998 vim_free(p);
2999 }
3000}
3001#endif
3002
Bram Moolenaar8299df92004-07-10 09:47:34 +00003003/*
3004 * paste a yank register into the command line.
3005 * used by CTRL-R command in command-line mode
3006 * insert_reg() can't be used here, because special characters from the
3007 * register contents will be interpreted as commands.
3008 *
3009 * return FAIL for failure, OK otherwise
3010 */
3011 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003012cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003013 int regname;
3014 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003015 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003016{
3017 long i;
3018 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003019 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003020 int allocated;
3021 struct cmdline_info save_ccline;
3022
3023 /* check for valid regname; also accept special characters for CTRL-R in
3024 * the command line */
3025 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3026 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3027 return FAIL;
3028
3029 /* A register containing CTRL-R can cause an endless loop. Allow using
3030 * CTRL-C to break the loop. */
3031 line_breakcheck();
3032 if (got_int)
3033 return FAIL;
3034
3035#ifdef FEAT_CLIPBOARD
3036 regname = may_get_selection(regname);
3037#endif
3038
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003039 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003040 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003041 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003042 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003043 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003044 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003045 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003046
3047 if (i)
3048 {
3049 /* Got the value of a special register in "arg". */
3050 if (arg == NULL)
3051 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003052
3053 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3054 * part of the word. */
3055 p = arg;
3056 if (p_is && regname == Ctrl_W)
3057 {
3058 char_u *w;
3059 int len;
3060
3061 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003062 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003063 {
3064#ifdef FEAT_MBYTE
3065 if (has_mbyte)
3066 {
3067 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3068 if (!vim_iswordc(mb_ptr2char(w - len)))
3069 break;
3070 w -= len;
3071 }
3072 else
3073#endif
3074 {
3075 if (!vim_iswordc(w[-1]))
3076 break;
3077 --w;
3078 }
3079 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003080 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003081 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3082 p += len;
3083 }
3084
3085 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003086 if (allocated)
3087 vim_free(arg);
3088 return OK;
3089 }
3090
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003091 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003092}
3093
3094/*
3095 * Put a string on the command line.
3096 * When "literally" is TRUE, insert literally.
3097 * When "literally" is FALSE, insert as typed, but don't leave the command
3098 * line.
3099 */
3100 void
3101cmdline_paste_str(s, literally)
3102 char_u *s;
3103 int literally;
3104{
3105 int c, cv;
3106
3107 if (literally)
3108 put_on_cmdline(s, -1, TRUE);
3109 else
3110 while (*s != NUL)
3111 {
3112 cv = *s;
3113 if (cv == Ctrl_V && s[1])
3114 ++s;
3115#ifdef FEAT_MBYTE
3116 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003117 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003118 else
3119#endif
3120 c = *s++;
3121 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3122#ifdef UNIX
3123 || c == intr_char
3124#endif
3125 || (c == Ctrl_BSL && *s == Ctrl_N))
3126 stuffcharReadbuff(Ctrl_V);
3127 stuffcharReadbuff(c);
3128 }
3129}
3130
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131#ifdef FEAT_WILDMENU
3132/*
3133 * Delete characters on the command line, from "from" to the current
3134 * position.
3135 */
3136 static void
3137cmdline_del(from)
3138 int from;
3139{
3140 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3141 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3142 ccline.cmdlen -= ccline.cmdpos - from;
3143 ccline.cmdpos = from;
3144}
3145#endif
3146
3147/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003148 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 * search
3150 */
3151 void
3152redrawcmdline()
3153{
3154 if (cmd_silent)
3155 return;
3156 need_wait_return = FALSE;
3157 compute_cmdrow();
3158 redrawcmd();
3159 cursorcmd();
3160}
3161
3162 static void
3163redrawcmdprompt()
3164{
3165 int i;
3166
3167 if (cmd_silent)
3168 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003169 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 msg_putchar(ccline.cmdfirstc);
3171 if (ccline.cmdprompt != NULL)
3172 {
3173 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3174 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3175 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003176 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 --ccline.cmdindent;
3178 }
3179 else
3180 for (i = ccline.cmdindent; i > 0; --i)
3181 msg_putchar(' ');
3182}
3183
3184/*
3185 * Redraw what is currently on the command line.
3186 */
3187 void
3188redrawcmd()
3189{
3190 if (cmd_silent)
3191 return;
3192
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003193 /* when 'incsearch' is set there may be no command line while redrawing */
3194 if (ccline.cmdbuff == NULL)
3195 {
3196 windgoto(cmdline_row, 0);
3197 msg_clr_eos();
3198 return;
3199 }
3200
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 msg_start();
3202 redrawcmdprompt();
3203
3204 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3205 msg_no_more = TRUE;
3206 draw_cmdline(0, ccline.cmdlen);
3207 msg_clr_eos();
3208 msg_no_more = FALSE;
3209
3210 set_cmdspos_cursor();
3211
3212 /*
3213 * An emsg() before may have set msg_scroll. This is used in normal mode,
3214 * in cmdline mode we can reset them now.
3215 */
3216 msg_scroll = FALSE; /* next message overwrites cmdline */
3217
3218 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3219 * in cmdline mode */
3220 skip_redraw = FALSE;
3221}
3222
3223 void
3224compute_cmdrow()
3225{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003226 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 cmdline_row = Rows - 1;
3228 else
3229 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3230 + W_STATUS_HEIGHT(lastwin);
3231}
3232
3233 static void
3234cursorcmd()
3235{
3236 if (cmd_silent)
3237 return;
3238
3239#ifdef FEAT_RIGHTLEFT
3240 if (cmdmsg_rl)
3241 {
3242 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3243 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3244 if (msg_row <= 0)
3245 msg_row = Rows - 1;
3246 }
3247 else
3248#endif
3249 {
3250 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3251 msg_col = ccline.cmdspos % (int)Columns;
3252 if (msg_row >= Rows)
3253 msg_row = Rows - 1;
3254 }
3255
3256 windgoto(msg_row, msg_col);
3257#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3258 redrawcmd_preedit();
3259#endif
3260#ifdef MCH_CURSOR_SHAPE
3261 mch_update_cursor();
3262#endif
3263}
3264
3265 void
3266gotocmdline(clr)
3267 int clr;
3268{
3269 msg_start();
3270#ifdef FEAT_RIGHTLEFT
3271 if (cmdmsg_rl)
3272 msg_col = Columns - 1;
3273 else
3274#endif
3275 msg_col = 0; /* always start in column 0 */
3276 if (clr) /* clear the bottom line(s) */
3277 msg_clr_eos(); /* will reset clear_cmdline */
3278 windgoto(cmdline_row, 0);
3279}
3280
3281/*
3282 * Check the word in front of the cursor for an abbreviation.
3283 * Called when the non-id character "c" has been entered.
3284 * When an abbreviation is recognized it is removed from the text with
3285 * backspaces and the replacement string is inserted, followed by "c".
3286 */
3287 static int
3288ccheck_abbr(c)
3289 int c;
3290{
3291 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3292 return FALSE;
3293
3294 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3295}
3296
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003297#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3298 static int
3299#ifdef __BORLANDC__
3300_RTLENTRYF
3301#endif
3302sort_func_compare(s1, s2)
3303 const void *s1;
3304 const void *s2;
3305{
3306 char_u *p1 = *(char_u **)s1;
3307 char_u *p2 = *(char_u **)s2;
3308
3309 if (*p1 != '<' && *p2 == '<') return -1;
3310 if (*p1 == '<' && *p2 != '<') return 1;
3311 return STRCMP(p1, p2);
3312}
3313#endif
3314
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315/*
3316 * Return FAIL if this is not an appropriate context in which to do
3317 * completion of anything, return OK if it is (even if there are no matches).
3318 * For the caller, this means that the character is just passed through like a
3319 * normal character (instead of being expanded). This allows :s/^I^D etc.
3320 */
3321 static int
3322nextwild(xp, type, options)
3323 expand_T *xp;
3324 int type;
3325 int options; /* extra options for ExpandOne() */
3326{
3327 int i, j;
3328 char_u *p1;
3329 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 int difflen;
3331 int v;
3332
3333 if (xp->xp_numfiles == -1)
3334 {
3335 set_expand_context(xp);
3336 cmd_showtail = expand_showtail(xp);
3337 }
3338
3339 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3340 {
3341 beep_flush();
3342 return OK; /* Something illegal on command line */
3343 }
3344 if (xp->xp_context == EXPAND_NOTHING)
3345 {
3346 /* Caller can use the character as a normal char instead */
3347 return FAIL;
3348 }
3349
3350 MSG_PUTS("..."); /* show that we are busy */
3351 out_flush();
3352
3353 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003354 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
3356 if (type == WILD_NEXT || type == WILD_PREV)
3357 {
3358 /*
3359 * Get next/previous match for a previous expanded pattern.
3360 */
3361 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3362 }
3363 else
3364 {
3365 /*
3366 * Translate string into pattern and expand it.
3367 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003368 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3369 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 p2 = NULL;
3371 else
3372 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003373 int use_options = options |
3374 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE;
3375
3376 if (p_wic)
3377 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003378 p2 = ExpandOne(xp, p1,
3379 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003380 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003382 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 if (p2 != NULL && type == WILD_LONGEST)
3384 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003385 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 if (ccline.cmdbuff[i + j] == '*'
3387 || ccline.cmdbuff[i + j] == '?')
3388 break;
3389 if ((int)STRLEN(p2) < j)
3390 {
3391 vim_free(p2);
3392 p2 = NULL;
3393 }
3394 }
3395 }
3396 }
3397
3398 if (p2 != NULL && !got_int)
3399 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003400 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003401 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003403 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 xp->xp_pattern = ccline.cmdbuff + i;
3405 }
3406 else
3407 v = OK;
3408 if (v == OK)
3409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003410 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3411 &ccline.cmdbuff[ccline.cmdpos],
3412 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3413 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 ccline.cmdlen += difflen;
3415 ccline.cmdpos += difflen;
3416 }
3417 }
3418 vim_free(p2);
3419
3420 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003421 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
3423 /* When expanding a ":map" command and no matches are found, assume that
3424 * the key is supposed to be inserted literally */
3425 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3426 return FAIL;
3427
3428 if (xp->xp_numfiles <= 0 && p2 == NULL)
3429 beep_flush();
3430 else if (xp->xp_numfiles == 1)
3431 /* free expanded pattern */
3432 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3433
3434 return OK;
3435}
3436
3437/*
3438 * Do wildcard expansion on the string 'str'.
3439 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003440 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 * Return NULL for failure.
3442 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003443 * "orig" is the originally expanded string, copied to allocated memory. It
3444 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3445 * WILD_PREV "orig" should be NULL.
3446 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003447 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3448 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 *
3450 * mode = WILD_FREE: just free previously expanded matches
3451 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3452 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3453 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3454 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3455 * mode = WILD_ALL: return all matches concatenated
3456 * mode = WILD_LONGEST: return longest matched part
3457 *
3458 * options = WILD_LIST_NOTFOUND: list entries without a match
3459 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3460 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3461 * options = WILD_NO_BEEP: Don't beep for multiple matches
3462 * options = WILD_ADD_SLASH: add a slash after directory names
3463 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3464 * options = WILD_SILENT: don't print warning messages
3465 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003466 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 *
3468 * The variables xp->xp_context and xp->xp_backslash must have been set!
3469 */
3470 char_u *
3471ExpandOne(xp, str, orig, options, mode)
3472 expand_T *xp;
3473 char_u *str;
3474 char_u *orig; /* allocated copy of original of expanded string */
3475 int options;
3476 int mode;
3477{
3478 char_u *ss = NULL;
3479 static int findex;
3480 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003481 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 int i;
3483 long_u len;
3484 int non_suf_match; /* number without matching suffix */
3485
3486 /*
3487 * first handle the case of using an old match
3488 */
3489 if (mode == WILD_NEXT || mode == WILD_PREV)
3490 {
3491 if (xp->xp_numfiles > 0)
3492 {
3493 if (mode == WILD_PREV)
3494 {
3495 if (findex == -1)
3496 findex = xp->xp_numfiles;
3497 --findex;
3498 }
3499 else /* mode == WILD_NEXT */
3500 ++findex;
3501
3502 /*
3503 * When wrapping around, return the original string, set findex to
3504 * -1.
3505 */
3506 if (findex < 0)
3507 {
3508 if (orig_save == NULL)
3509 findex = xp->xp_numfiles - 1;
3510 else
3511 findex = -1;
3512 }
3513 if (findex >= xp->xp_numfiles)
3514 {
3515 if (orig_save == NULL)
3516 findex = 0;
3517 else
3518 findex = -1;
3519 }
3520#ifdef FEAT_WILDMENU
3521 if (p_wmnu)
3522 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3523 findex, cmd_showtail);
3524#endif
3525 if (findex == -1)
3526 return vim_strsave(orig_save);
3527 return vim_strsave(xp->xp_files[findex]);
3528 }
3529 else
3530 return NULL;
3531 }
3532
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003533 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3535 {
3536 FreeWild(xp->xp_numfiles, xp->xp_files);
3537 xp->xp_numfiles = -1;
3538 vim_free(orig_save);
3539 orig_save = NULL;
3540 }
3541 findex = 0;
3542
3543 if (mode == WILD_FREE) /* only release file name */
3544 return NULL;
3545
3546 if (xp->xp_numfiles == -1)
3547 {
3548 vim_free(orig_save);
3549 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003550 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
3552 /*
3553 * Do the expansion.
3554 */
3555 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3556 options) == FAIL)
3557 {
3558#ifdef FNAME_ILLEGAL
3559 /* Illegal file name has been silently skipped. But when there
3560 * are wildcards, the real problem is that there was no match,
3561 * causing the pattern to be added, which has illegal characters.
3562 */
3563 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3564 EMSG2(_(e_nomatch2), str);
3565#endif
3566 }
3567 else if (xp->xp_numfiles == 0)
3568 {
3569 if (!(options & WILD_SILENT))
3570 EMSG2(_(e_nomatch2), str);
3571 }
3572 else
3573 {
3574 /* Escape the matches for use on the command line. */
3575 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3576
3577 /*
3578 * Check for matching suffixes in file names.
3579 */
3580 if (mode != WILD_ALL && mode != WILD_LONGEST)
3581 {
3582 if (xp->xp_numfiles)
3583 non_suf_match = xp->xp_numfiles;
3584 else
3585 non_suf_match = 1;
3586 if ((xp->xp_context == EXPAND_FILES
3587 || xp->xp_context == EXPAND_DIRECTORIES)
3588 && xp->xp_numfiles > 1)
3589 {
3590 /*
3591 * More than one match; check suffix.
3592 * The files will have been sorted on matching suffix in
3593 * expand_wildcards, only need to check the first two.
3594 */
3595 non_suf_match = 0;
3596 for (i = 0; i < 2; ++i)
3597 if (match_suffix(xp->xp_files[i]))
3598 ++non_suf_match;
3599 }
3600 if (non_suf_match != 1)
3601 {
3602 /* Can we ever get here unless it's while expanding
3603 * interactively? If not, we can get rid of this all
3604 * together. Don't really want to wait for this message
3605 * (and possibly have to hit return to continue!).
3606 */
3607 if (!(options & WILD_SILENT))
3608 EMSG(_(e_toomany));
3609 else if (!(options & WILD_NO_BEEP))
3610 beep_flush();
3611 }
3612 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3613 ss = vim_strsave(xp->xp_files[0]);
3614 }
3615 }
3616 }
3617
3618 /* Find longest common part */
3619 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3620 {
3621 for (len = 0; xp->xp_files[0][len]; ++len)
3622 {
3623 for (i = 0; i < xp->xp_numfiles; ++i)
3624 {
3625#ifdef CASE_INSENSITIVE_FILENAME
3626 if (xp->xp_context == EXPAND_DIRECTORIES
3627 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003628 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 || xp->xp_context == EXPAND_BUFFERS)
3630 {
3631 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3632 TOLOWER_LOC(xp->xp_files[0][len]))
3633 break;
3634 }
3635 else
3636#endif
3637 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3638 break;
3639 }
3640 if (i < xp->xp_numfiles)
3641 {
3642 if (!(options & WILD_NO_BEEP))
3643 vim_beep();
3644 break;
3645 }
3646 }
3647 ss = alloc((unsigned)len + 1);
3648 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003649 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 findex = -1; /* next p_wc gets first one */
3651 }
3652
3653 /* Concatenate all matching names */
3654 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3655 {
3656 len = 0;
3657 for (i = 0; i < xp->xp_numfiles; ++i)
3658 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3659 ss = lalloc(len, TRUE);
3660 if (ss != NULL)
3661 {
3662 *ss = NUL;
3663 for (i = 0; i < xp->xp_numfiles; ++i)
3664 {
3665 STRCAT(ss, xp->xp_files[i]);
3666 if (i != xp->xp_numfiles - 1)
3667 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3668 }
3669 }
3670 }
3671
3672 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3673 ExpandCleanup(xp);
3674
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003675 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003676 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003677 vim_free(orig);
3678
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 return ss;
3680}
3681
3682/*
3683 * Prepare an expand structure for use.
3684 */
3685 void
3686ExpandInit(xp)
3687 expand_T *xp;
3688{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003689 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003690 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003692#ifndef BACKSLASH_IN_FILENAME
3693 xp->xp_shell = FALSE;
3694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 xp->xp_numfiles = -1;
3696 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003697#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3698 xp->xp_arg = NULL;
3699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700}
3701
3702/*
3703 * Cleanup an expand structure after use.
3704 */
3705 void
3706ExpandCleanup(xp)
3707 expand_T *xp;
3708{
3709 if (xp->xp_numfiles >= 0)
3710 {
3711 FreeWild(xp->xp_numfiles, xp->xp_files);
3712 xp->xp_numfiles = -1;
3713 }
3714}
3715
3716 void
3717ExpandEscape(xp, str, numfiles, files, options)
3718 expand_T *xp;
3719 char_u *str;
3720 int numfiles;
3721 char_u **files;
3722 int options;
3723{
3724 int i;
3725 char_u *p;
3726
3727 /*
3728 * May change home directory back to "~"
3729 */
3730 if (options & WILD_HOME_REPLACE)
3731 tilde_replace(str, numfiles, files);
3732
3733 if (options & WILD_ESCAPE)
3734 {
3735 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003736 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003737 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 || xp->xp_context == EXPAND_BUFFERS
3739 || xp->xp_context == EXPAND_DIRECTORIES)
3740 {
3741 /*
3742 * Insert a backslash into a file name before a space, \, %, #
3743 * and wildmatch characters, except '~'.
3744 */
3745 for (i = 0; i < numfiles; ++i)
3746 {
3747 /* for ":set path=" we need to escape spaces twice */
3748 if (xp->xp_backslash == XP_BS_THREE)
3749 {
3750 p = vim_strsave_escaped(files[i], (char_u *)" ");
3751 if (p != NULL)
3752 {
3753 vim_free(files[i]);
3754 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003755#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 p = vim_strsave_escaped(files[i], (char_u *)" ");
3757 if (p != NULL)
3758 {
3759 vim_free(files[i]);
3760 files[i] = p;
3761 }
3762#endif
3763 }
3764 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003765#ifdef BACKSLASH_IN_FILENAME
3766 p = vim_strsave_fnameescape(files[i], FALSE);
3767#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003768 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 if (p != NULL)
3771 {
3772 vim_free(files[i]);
3773 files[i] = p;
3774 }
3775
3776 /* If 'str' starts with "\~", replace "~" at start of
3777 * files[i] with "\~". */
3778 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003779 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 }
3781 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003782
3783 /* If the first file starts with a '+' escape it. Otherwise it
3784 * could be seen as "+cmd". */
3785 if (*files[0] == '+')
3786 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
3788 else if (xp->xp_context == EXPAND_TAGS)
3789 {
3790 /*
3791 * Insert a backslash before characters in a tag name that
3792 * would terminate the ":tag" command.
3793 */
3794 for (i = 0; i < numfiles; ++i)
3795 {
3796 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3797 if (p != NULL)
3798 {
3799 vim_free(files[i]);
3800 files[i] = p;
3801 }
3802 }
3803 }
3804 }
3805}
3806
3807/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003808 * Escape special characters in "fname" for when used as a file name argument
3809 * after a Vim command, or, when "shell" is non-zero, a shell command.
3810 * Returns the result in allocated memory.
3811 */
3812 char_u *
3813vim_strsave_fnameescape(fname, shell)
3814 char_u *fname;
3815 int shell;
3816{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003817 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003818#ifdef BACKSLASH_IN_FILENAME
3819 char_u buf[20];
3820 int j = 0;
3821
3822 /* Don't escape '[' and '{' if they are in 'isfname'. */
3823 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3824 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3825 buf[j++] = *p;
3826 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003827 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003828#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003829 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3830 if (shell && csh_like_shell() && p != NULL)
3831 {
3832 char_u *s;
3833
3834 /* For csh and similar shells need to put two backslashes before '!'.
3835 * One is taken by Vim, one by the shell. */
3836 s = vim_strsave_escaped(p, (char_u *)"!");
3837 vim_free(p);
3838 p = s;
3839 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003840#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003841
3842 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3843 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003844 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003845 escape_fname(&p);
3846
3847 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003848}
3849
3850/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003851 * Put a backslash before the file name in "pp", which is in allocated memory.
3852 */
3853 static void
3854escape_fname(pp)
3855 char_u **pp;
3856{
3857 char_u *p;
3858
3859 p = alloc((unsigned)(STRLEN(*pp) + 2));
3860 if (p != NULL)
3861 {
3862 p[0] = '\\';
3863 STRCPY(p + 1, *pp);
3864 vim_free(*pp);
3865 *pp = p;
3866 }
3867}
3868
3869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 * For each file name in files[num_files]:
3871 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3872 */
3873 void
3874tilde_replace(orig_pat, num_files, files)
3875 char_u *orig_pat;
3876 int num_files;
3877 char_u **files;
3878{
3879 int i;
3880 char_u *p;
3881
3882 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3883 {
3884 for (i = 0; i < num_files; ++i)
3885 {
3886 p = home_replace_save(NULL, files[i]);
3887 if (p != NULL)
3888 {
3889 vim_free(files[i]);
3890 files[i] = p;
3891 }
3892 }
3893 }
3894}
3895
3896/*
3897 * Show all matches for completion on the command line.
3898 * Returns EXPAND_NOTHING when the character that triggered expansion should
3899 * be inserted like a normal character.
3900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 static int
3902showmatches(xp, wildmenu)
3903 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003904 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905{
3906#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3907 int num_files;
3908 char_u **files_found;
3909 int i, j, k;
3910 int maxlen;
3911 int lines;
3912 int columns;
3913 char_u *p;
3914 int lastlen;
3915 int attr;
3916 int showtail;
3917
3918 if (xp->xp_numfiles == -1)
3919 {
3920 set_expand_context(xp);
3921 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3922 &num_files, &files_found);
3923 showtail = expand_showtail(xp);
3924 if (i != EXPAND_OK)
3925 return i;
3926
3927 }
3928 else
3929 {
3930 num_files = xp->xp_numfiles;
3931 files_found = xp->xp_files;
3932 showtail = cmd_showtail;
3933 }
3934
3935#ifdef FEAT_WILDMENU
3936 if (!wildmenu)
3937 {
3938#endif
3939 msg_didany = FALSE; /* lines_left will be set */
3940 msg_start(); /* prepare for paging */
3941 msg_putchar('\n');
3942 out_flush();
3943 cmdline_row = msg_row;
3944 msg_didany = FALSE; /* lines_left will be set again */
3945 msg_start(); /* prepare for paging */
3946#ifdef FEAT_WILDMENU
3947 }
3948#endif
3949
3950 if (got_int)
3951 got_int = FALSE; /* only int. the completion, not the cmd line */
3952#ifdef FEAT_WILDMENU
3953 else if (wildmenu)
3954 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3955#endif
3956 else
3957 {
3958 /* find the length of the longest file name */
3959 maxlen = 0;
3960 for (i = 0; i < num_files; ++i)
3961 {
3962 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003963 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 || xp->xp_context == EXPAND_BUFFERS))
3965 {
3966 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3967 j = vim_strsize(NameBuff);
3968 }
3969 else
3970 j = vim_strsize(L_SHOWFILE(i));
3971 if (j > maxlen)
3972 maxlen = j;
3973 }
3974
3975 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3976 lines = num_files;
3977 else
3978 {
3979 /* compute the number of columns and lines for the listing */
3980 maxlen += 2; /* two spaces between file names */
3981 columns = ((int)Columns + 2) / maxlen;
3982 if (columns < 1)
3983 columns = 1;
3984 lines = (num_files + columns - 1) / columns;
3985 }
3986
3987 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3988
3989 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3990 {
3991 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3992 msg_clr_eos();
3993 msg_advance(maxlen - 3);
3994 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3995 }
3996
3997 /* list the files line by line */
3998 for (i = 0; i < lines; ++i)
3999 {
4000 lastlen = 999;
4001 for (k = i; k < num_files; k += lines)
4002 {
4003 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4004 {
4005 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4006 p = files_found[k] + STRLEN(files_found[k]) + 1;
4007 msg_advance(maxlen + 1);
4008 msg_puts(p);
4009 msg_advance(maxlen + 3);
4010 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4011 break;
4012 }
4013 for (j = maxlen - lastlen; --j >= 0; )
4014 msg_putchar(' ');
4015 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004016 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 || xp->xp_context == EXPAND_BUFFERS)
4018 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004019 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004020 if (xp->xp_numfiles != -1)
4021 {
4022 char_u *halved_slash;
4023 char_u *exp_path;
4024
4025 /* Expansion was done before and special characters
4026 * were escaped, need to halve backslashes. Also
4027 * $HOME has been replaced with ~/. */
4028 exp_path = expand_env_save_opt(files_found[k], TRUE);
4029 halved_slash = backslash_halve_save(
4030 exp_path != NULL ? exp_path : files_found[k]);
4031 j = mch_isdir(halved_slash != NULL ? halved_slash
4032 : files_found[k]);
4033 vim_free(exp_path);
4034 vim_free(halved_slash);
4035 }
4036 else
4037 /* Expansion was done here, file names are literal. */
4038 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if (showtail)
4040 p = L_SHOWFILE(k);
4041 else
4042 {
4043 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4044 TRUE);
4045 p = NameBuff;
4046 }
4047 }
4048 else
4049 {
4050 j = FALSE;
4051 p = L_SHOWFILE(k);
4052 }
4053 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4054 }
4055 if (msg_col > 0) /* when not wrapped around */
4056 {
4057 msg_clr_eos();
4058 msg_putchar('\n');
4059 }
4060 out_flush(); /* show one line at a time */
4061 if (got_int)
4062 {
4063 got_int = FALSE;
4064 break;
4065 }
4066 }
4067
4068 /*
4069 * we redraw the command below the lines that we have just listed
4070 * This is a bit tricky, but it saves a lot of screen updating.
4071 */
4072 cmdline_row = msg_row; /* will put it back later */
4073 }
4074
4075 if (xp->xp_numfiles == -1)
4076 FreeWild(num_files, files_found);
4077
4078 return EXPAND_OK;
4079}
4080
4081/*
4082 * Private gettail for showmatches() (and win_redr_status_matches()):
4083 * Find tail of file name path, but ignore trailing "/".
4084 */
4085 char_u *
4086sm_gettail(s)
4087 char_u *s;
4088{
4089 char_u *p;
4090 char_u *t = s;
4091 int had_sep = FALSE;
4092
4093 for (p = s; *p != NUL; )
4094 {
4095 if (vim_ispathsep(*p)
4096#ifdef BACKSLASH_IN_FILENAME
4097 && !rem_backslash(p)
4098#endif
4099 )
4100 had_sep = TRUE;
4101 else if (had_sep)
4102 {
4103 t = p;
4104 had_sep = FALSE;
4105 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004106 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108 return t;
4109}
4110
4111/*
4112 * Return TRUE if we only need to show the tail of completion matches.
4113 * When not completing file names or there is a wildcard in the path FALSE is
4114 * returned.
4115 */
4116 static int
4117expand_showtail(xp)
4118 expand_T *xp;
4119{
4120 char_u *s;
4121 char_u *end;
4122
4123 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004124 if (xp->xp_context != EXPAND_FILES
4125 && xp->xp_context != EXPAND_SHELLCMD
4126 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 return FALSE;
4128
4129 end = gettail(xp->xp_pattern);
4130 if (end == xp->xp_pattern) /* there is no path separator */
4131 return FALSE;
4132
4133 for (s = xp->xp_pattern; s < end; s++)
4134 {
4135 /* Skip escaped wildcards. Only when the backslash is not a path
4136 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4137 if (rem_backslash(s))
4138 ++s;
4139 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4140 return FALSE;
4141 }
4142 return TRUE;
4143}
4144
4145/*
4146 * Prepare a string for expansion.
4147 * When expanding file names: The string will be used with expand_wildcards().
4148 * Copy the file name into allocated memory and add a '*' at the end.
4149 * When expanding other names: The string will be used with regcomp(). Copy
4150 * the name into allocated memory and prepend "^".
4151 */
4152 char_u *
4153addstar(fname, len, context)
4154 char_u *fname;
4155 int len;
4156 int context; /* EXPAND_FILES etc. */
4157{
4158 char_u *retval;
4159 int i, j;
4160 int new_len;
4161 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004162 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004164 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004165 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004166 && context != EXPAND_SHELLCMD
4167 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
4169 /*
4170 * Matching will be done internally (on something other than files).
4171 * So we convert the file-matching-type wildcards into our kind for
4172 * use with vim_regcomp(). First work out how long it will be:
4173 */
4174
4175 /* For help tags the translation is done in find_help_tags().
4176 * For a tag pattern starting with "/" no translation is needed. */
4177 if (context == EXPAND_HELP
4178 || context == EXPAND_COLORS
4179 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004180 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004181 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 || (context == EXPAND_TAGS && fname[0] == '/'))
4183 retval = vim_strnsave(fname, len);
4184 else
4185 {
4186 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4187 for (i = 0; i < len; i++)
4188 {
4189 if (fname[i] == '*' || fname[i] == '~')
4190 new_len++; /* '*' needs to be replaced by ".*"
4191 '~' needs to be replaced by "\~" */
4192
4193 /* Buffer names are like file names. "." should be literal */
4194 if (context == EXPAND_BUFFERS && fname[i] == '.')
4195 new_len++; /* "." becomes "\." */
4196
4197 /* Custom expansion takes care of special things, match
4198 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004199 if ((context == EXPAND_USER_DEFINED
4200 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 new_len++; /* '\' becomes "\\" */
4202 }
4203 retval = alloc(new_len);
4204 if (retval != NULL)
4205 {
4206 retval[0] = '^';
4207 j = 1;
4208 for (i = 0; i < len; i++, j++)
4209 {
4210 /* Skip backslash. But why? At least keep it for custom
4211 * expansion. */
4212 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004213 && context != EXPAND_USER_LIST
4214 && fname[i] == '\\'
4215 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 break;
4217
4218 switch (fname[i])
4219 {
4220 case '*': retval[j++] = '.';
4221 break;
4222 case '~': retval[j++] = '\\';
4223 break;
4224 case '?': retval[j] = '.';
4225 continue;
4226 case '.': if (context == EXPAND_BUFFERS)
4227 retval[j++] = '\\';
4228 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004229 case '\\': if (context == EXPAND_USER_DEFINED
4230 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 retval[j++] = '\\';
4232 break;
4233 }
4234 retval[j] = fname[i];
4235 }
4236 retval[j] = NUL;
4237 }
4238 }
4239 }
4240 else
4241 {
4242 retval = alloc(len + 4);
4243 if (retval != NULL)
4244 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004245 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246
4247 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004248 * Don't add a star to *, ~, ~user, $var or `cmd`.
4249 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 * ~ would be at the start of the file name, but not the tail.
4251 * $ could be anywhere in the tail.
4252 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004253 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 */
4255 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004256 ends_in_star = (len > 0 && retval[len - 1] == '*');
4257#ifndef BACKSLASH_IN_FILENAME
4258 for (i = len - 2; i >= 0; --i)
4259 {
4260 if (retval[i] != '\\')
4261 break;
4262 ends_in_star = !ends_in_star;
4263 }
4264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004266 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 && vim_strchr(tail, '$') == NULL
4268 && vim_strchr(retval, '`') == NULL)
4269 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004270 else if (len > 0 && retval[len - 1] == '$')
4271 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 retval[len] = NUL;
4273 }
4274 }
4275 return retval;
4276}
4277
4278/*
4279 * Must parse the command line so far to work out what context we are in.
4280 * Completion can then be done based on that context.
4281 * This routine sets the variables:
4282 * xp->xp_pattern The start of the pattern to be expanded within
4283 * the command line (ends at the cursor).
4284 * xp->xp_context The type of thing to expand. Will be one of:
4285 *
4286 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4287 * the command line, like an unknown command. Caller
4288 * should beep.
4289 * EXPAND_NOTHING Unrecognised context for completion, use char like
4290 * a normal char, rather than for completion. eg
4291 * :s/^I/
4292 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4293 * it.
4294 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4295 * EXPAND_FILES After command with XFILE set, or after setting
4296 * with P_EXPAND set. eg :e ^I, :w>>^I
4297 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4298 * when we know only directories are of interest. eg
4299 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004300 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4302 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4303 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4304 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4305 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4306 * EXPAND_EVENTS Complete event names
4307 * EXPAND_SYNTAX Complete :syntax command arguments
4308 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4309 * EXPAND_AUGROUP Complete autocommand group names
4310 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4311 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4312 * eg :unmap a^I , :cunab x^I
4313 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4314 * eg :call sub^I
4315 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4316 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4317 * names in expressions, eg :while s^I
4318 * EXPAND_ENV_VARS Complete environment variable names
4319 */
4320 static void
4321set_expand_context(xp)
4322 expand_T *xp;
4323{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004324 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 if (ccline.cmdfirstc != ':'
4326#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004327 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004328 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329#endif
4330 )
4331 {
4332 xp->xp_context = EXPAND_NOTHING;
4333 return;
4334 }
4335 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4336}
4337
4338 void
4339set_cmd_context(xp, str, len, col)
4340 expand_T *xp;
4341 char_u *str; /* start of command line */
4342 int len; /* length of command line (excl. NUL) */
4343 int col; /* position of cursor */
4344{
4345 int old_char = NUL;
4346 char_u *nextcomm;
4347
4348 /*
4349 * Avoid a UMR warning from Purify, only save the character if it has been
4350 * written before.
4351 */
4352 if (col < len)
4353 old_char = str[col];
4354 str[col] = NUL;
4355 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004356
4357#ifdef FEAT_EVAL
4358 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004359 {
4360# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004361 /* pass CMD_SIZE because there is no real command */
4362 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004363# endif
4364 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004365 else if (ccline.input_fn)
4366 {
4367 xp->xp_context = ccline.xp_context;
4368 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004369# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004370 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004371# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004372 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004373 else
4374#endif
4375 while (nextcomm != NULL)
4376 nextcomm = set_one_cmd_context(xp, nextcomm);
4377
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 str[col] = old_char;
4379}
4380
4381/*
4382 * Expand the command line "str" from context "xp".
4383 * "xp" must have been set by set_cmd_context().
4384 * xp->xp_pattern points into "str", to where the text that is to be expanded
4385 * starts.
4386 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4387 * cursor.
4388 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4389 * key that triggered expansion literally.
4390 * Returns EXPAND_OK otherwise.
4391 */
4392 int
4393expand_cmdline(xp, str, col, matchcount, matches)
4394 expand_T *xp;
4395 char_u *str; /* start of command line */
4396 int col; /* position of cursor */
4397 int *matchcount; /* return: nr of matches */
4398 char_u ***matches; /* return: array of pointers to matches */
4399{
4400 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004401 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4404 {
4405 beep_flush();
4406 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4407 }
4408 if (xp->xp_context == EXPAND_NOTHING)
4409 {
4410 /* Caller can use the character as a normal char instead */
4411 return EXPAND_NOTHING;
4412 }
4413
4414 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004415 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4416 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 if (file_str == NULL)
4418 return EXPAND_UNSUCCESSFUL;
4419
Bram Moolenaar94950a92010-12-02 16:01:29 +01004420 if (p_wic)
4421 options += WILD_ICASE;
4422
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004424 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
4426 *matchcount = 0;
4427 *matches = NULL;
4428 }
4429 vim_free(file_str);
4430
4431 return EXPAND_OK;
4432}
4433
4434#ifdef FEAT_MULTI_LANG
4435/*
4436 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4437 */
4438static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4439
4440 static void
4441cleanup_help_tags(num_file, file)
4442 int num_file;
4443 char_u **file;
4444{
4445 int i, j;
4446 int len;
4447
4448 for (i = 0; i < num_file; ++i)
4449 {
4450 len = (int)STRLEN(file[i]) - 3;
4451 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4452 {
4453 /* Sorting on priority means the same item in another language may
4454 * be anywhere. Search all items for a match up to the "@en". */
4455 for (j = 0; j < num_file; ++j)
4456 if (j != i
4457 && (int)STRLEN(file[j]) == len + 3
4458 && STRNCMP(file[i], file[j], len + 1) == 0)
4459 break;
4460 if (j == num_file)
4461 file[i][len] = NUL;
4462 }
4463 }
4464}
4465#endif
4466
4467/*
4468 * Do the expansion based on xp->xp_context and "pat".
4469 */
4470 static int
4471ExpandFromContext(xp, pat, num_file, file, options)
4472 expand_T *xp;
4473 char_u *pat;
4474 int *num_file;
4475 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004476 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477{
4478#ifdef FEAT_CMDL_COMPL
4479 regmatch_T regmatch;
4480#endif
4481 int ret;
4482 int flags;
4483
4484 flags = EW_DIR; /* include directories */
4485 if (options & WILD_LIST_NOTFOUND)
4486 flags |= EW_NOTFOUND;
4487 if (options & WILD_ADD_SLASH)
4488 flags |= EW_ADDSLASH;
4489 if (options & WILD_KEEP_ALL)
4490 flags |= EW_KEEPALL;
4491 if (options & WILD_SILENT)
4492 flags |= EW_SILENT;
4493
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004494 if (xp->xp_context == EXPAND_FILES
4495 || xp->xp_context == EXPAND_DIRECTORIES
4496 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 {
4498 /*
4499 * Expand file or directory names.
4500 */
4501 int free_pat = FALSE;
4502 int i;
4503
4504 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4505 * space */
4506 if (xp->xp_backslash != XP_BS_NONE)
4507 {
4508 free_pat = TRUE;
4509 pat = vim_strsave(pat);
4510 for (i = 0; pat[i]; ++i)
4511 if (pat[i] == '\\')
4512 {
4513 if (xp->xp_backslash == XP_BS_THREE
4514 && pat[i + 1] == '\\'
4515 && pat[i + 2] == '\\'
4516 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004517 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 if (xp->xp_backslash == XP_BS_ONE
4519 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004520 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 }
4522 }
4523
4524 if (xp->xp_context == EXPAND_FILES)
4525 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004526 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4527 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 else
4529 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004530 if (options & WILD_ICASE)
4531 flags |= EW_ICASE;
4532
Bram Moolenaard7834d32009-12-02 16:14:36 +00004533 /* Expand wildcards, supporting %:h and the like. */
4534 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (free_pat)
4536 vim_free(pat);
4537 return ret;
4538 }
4539
4540 *file = (char_u **)"";
4541 *num_file = 0;
4542 if (xp->xp_context == EXPAND_HELP)
4543 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004544 /* With an empty argument we would get all the help tags, which is
4545 * very slow. Get matches for "help" instead. */
4546 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4547 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
4549#ifdef FEAT_MULTI_LANG
4550 cleanup_help_tags(*num_file, *file);
4551#endif
4552 return OK;
4553 }
4554 return FAIL;
4555 }
4556
4557#ifndef FEAT_CMDL_COMPL
4558 return FAIL;
4559#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004560 if (xp->xp_context == EXPAND_SHELLCMD)
4561 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 if (xp->xp_context == EXPAND_OLD_SETTING)
4563 return ExpandOldSetting(num_file, file);
4564 if (xp->xp_context == EXPAND_BUFFERS)
4565 return ExpandBufnames(pat, num_file, file, options);
4566 if (xp->xp_context == EXPAND_TAGS
4567 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4568 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4569 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004570 {
4571 char *directories[] = {"colors", NULL};
4572 return ExpandRTDir(pat, num_file, file, directories);
4573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004575 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004576 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004577 return ExpandRTDir(pat, num_file, file, directories);
4578 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004579 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004580 {
4581 char *directories[] = {"syntax", NULL};
4582 return ExpandRTDir(pat, num_file, file, directories);
4583 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004584 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004585 {
4586 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4587 return ExpandRTDir(pat, num_file, file, directories);
4588 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004589# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4590 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004591 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
4594 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4595 if (regmatch.regprog == NULL)
4596 return FAIL;
4597
4598 /* set ignore-case according to p_ic, p_scs and pat */
4599 regmatch.rm_ic = ignorecase(pat);
4600
4601 if (xp->xp_context == EXPAND_SETTINGS
4602 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4603 ret = ExpandSettings(xp, &regmatch, num_file, file);
4604 else if (xp->xp_context == EXPAND_MAPPINGS)
4605 ret = ExpandMappings(&regmatch, num_file, file);
4606# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4607 else if (xp->xp_context == EXPAND_USER_DEFINED)
4608 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4609# endif
4610 else
4611 {
4612 static struct expgen
4613 {
4614 int context;
4615 char_u *((*func)__ARGS((expand_T *, int)));
4616 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004617 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 } tab[] =
4619 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004620 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4621 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004623 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4624 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4625 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4626 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627#endif
4628#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004629 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4630 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4631 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4632 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633#endif
4634#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004635 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4636 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#endif
4638#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004639 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004641 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004643 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4644 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004646#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004647 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004648#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004649#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004650 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004651#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004652#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004653 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4656 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004657 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4658 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004660 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 };
4662 int i;
4663
4664 /*
4665 * Find a context in the table and call the ExpandGeneric() with the
4666 * right function to do the expansion.
4667 */
4668 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004669 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 if (xp->xp_context == tab[i].context)
4671 {
4672 if (tab[i].ic)
4673 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004674 ret = ExpandGeneric(xp, &regmatch, num_file, file,
4675 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 break;
4677 }
4678 }
4679
4680 vim_free(regmatch.regprog);
4681
4682 return ret;
4683#endif /* FEAT_CMDL_COMPL */
4684}
4685
4686#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4687/*
4688 * Expand a list of names.
4689 *
4690 * Generic function for command line completion. It calls a function to
4691 * obtain strings, one by one. The strings are matched against a regexp
4692 * program. Matching strings are copied into an array, which is returned.
4693 *
4694 * Returns OK when no problems encountered, FAIL for error (out of memory).
4695 */
4696 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004697ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 expand_T *xp;
4699 regmatch_T *regmatch;
4700 int *num_file;
4701 char_u ***file;
4702 char_u *((*func)__ARGS((expand_T *, int)));
4703 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004704 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705{
4706 int i;
4707 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004708 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 char_u *str;
4710
4711 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004712 * round == 0: count the number of matching names
4713 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004715 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
4717 for (i = 0; ; ++i)
4718 {
4719 str = (*func)(xp, i);
4720 if (str == NULL) /* end of list */
4721 break;
4722 if (*str == NUL) /* skip empty strings */
4723 continue;
4724
4725 if (vim_regexec(regmatch, str, (colnr_T)0))
4726 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004727 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004729 if (escaped)
4730 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4731 else
4732 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 (*file)[count] = str;
4734#ifdef FEAT_MENU
4735 if (func == get_menu_names && str != NULL)
4736 {
4737 /* test for separator added by get_menu_names() */
4738 str += STRLEN(str) - 1;
4739 if (*str == '\001')
4740 *str = '.';
4741 }
4742#endif
4743 }
4744 ++count;
4745 }
4746 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004747 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
4749 if (count == 0)
4750 return OK;
4751 *num_file = count;
4752 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4753 if (*file == NULL)
4754 {
4755 *file = (char_u **)"";
4756 return FAIL;
4757 }
4758 count = 0;
4759 }
4760 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004761
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004762 /* Sort the results. Keep menu's in the specified order. */
4763 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004764 {
4765 if (xp->xp_context == EXPAND_EXPRESSION
4766 || xp->xp_context == EXPAND_FUNCTIONS
4767 || xp->xp_context == EXPAND_USER_FUNC)
4768 /* <SNR> functions should be sorted to the end. */
4769 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4770 sort_func_compare);
4771 else
4772 sort_strings(*file, *num_file);
4773 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004774
Bram Moolenaar4f688582007-07-24 12:34:30 +00004775#ifdef FEAT_CMDL_COMPL
4776 /* Reset the variables used for special highlight names expansion, so that
4777 * they don't show up when getting normal highlight names by ID. */
4778 reset_expand_highlight();
4779#endif
4780
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 return OK;
4782}
4783
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004784/*
4785 * Complete a shell command.
4786 * Returns FAIL or OK;
4787 */
4788 static int
4789expand_shellcmd(filepat, num_file, file, flagsarg)
4790 char_u *filepat; /* pattern to match with command names */
4791 int *num_file; /* return: number of matches */
4792 char_u ***file; /* return: array with matches */
4793 int flagsarg; /* EW_ flags */
4794{
4795 char_u *pat;
4796 int i;
4797 char_u *path;
4798 int mustfree = FALSE;
4799 garray_T ga;
4800 char_u *buf = alloc(MAXPATHL);
4801 size_t l;
4802 char_u *s, *e;
4803 int flags = flagsarg;
4804 int ret;
4805
4806 if (buf == NULL)
4807 return FAIL;
4808
4809 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4810 * space */
4811 pat = vim_strsave(filepat);
4812 for (i = 0; pat[i]; ++i)
4813 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004814 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004815
4816 flags |= EW_FILE | EW_EXEC;
4817
4818 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004819 if (mch_isFullName(pat))
4820 path = (char_u *)" ";
4821 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004822 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4823 path = (char_u *)".";
4824 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004825 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004826 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004827 if (path == NULL)
4828 path = (char_u *)"";
4829 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004830
4831 /*
4832 * Go over all directories in $PATH. Expand matches in that directory and
4833 * collect them in "ga".
4834 */
4835 ga_init2(&ga, (int)sizeof(char *), 10);
4836 for (s = path; *s != NUL; s = e)
4837 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004838 if (*s == ' ')
4839 ++s; /* Skip space used for absolute path name. */
4840
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004841#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4842 e = vim_strchr(s, ';');
4843#else
4844 e = vim_strchr(s, ':');
4845#endif
4846 if (e == NULL)
4847 e = s + STRLEN(s);
4848
4849 l = e - s;
4850 if (l > MAXPATHL - 5)
4851 break;
4852 vim_strncpy(buf, s, l);
4853 add_pathsep(buf);
4854 l = STRLEN(buf);
4855 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4856
4857 /* Expand matches in one directory of $PATH. */
4858 ret = expand_wildcards(1, &buf, num_file, file, flags);
4859 if (ret == OK)
4860 {
4861 if (ga_grow(&ga, *num_file) == FAIL)
4862 FreeWild(*num_file, *file);
4863 else
4864 {
4865 for (i = 0; i < *num_file; ++i)
4866 {
4867 s = (*file)[i];
4868 if (STRLEN(s) > l)
4869 {
4870 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004871 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004872 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4873 }
4874 else
4875 vim_free(s);
4876 }
4877 vim_free(*file);
4878 }
4879 }
4880 if (*e != NUL)
4881 ++e;
4882 }
4883 *file = ga.ga_data;
4884 *num_file = ga.ga_len;
4885
4886 vim_free(buf);
4887 vim_free(pat);
4888 if (mustfree)
4889 vim_free(path);
4890 return OK;
4891}
4892
4893
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004895static 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));
4896
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004898 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004899 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004901 static void *
4902call_user_expand_func(user_expand_func, xp, num_file, file)
4903 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 int *num_file;
4906 char_u ***file;
4907{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004908 char_u keep;
4909 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004912 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004913 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914
4915 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004916 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 *num_file = 0;
4918 *file = NULL;
4919
Bram Moolenaar21669c02008-01-18 12:16:16 +00004920 if (ccline.cmdbuff == NULL)
4921 {
4922 /* Completion from Insert mode, pass fake arguments. */
4923 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004924 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004925 args[1] = xp->xp_pattern;
4926 }
4927 else
4928 {
4929 /* Completion on the command line, pass real arguments. */
4930 keep = ccline.cmdbuff[ccline.cmdlen];
4931 ccline.cmdbuff[ccline.cmdlen] = 0;
4932 sprintf((char *)num, "%d", ccline.cmdpos);
4933 args[1] = ccline.cmdbuff;
4934 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004935 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 args[2] = num;
4937
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004938 /* Save the cmdline, we don't know what the function may do. */
4939 save_ccline = ccline;
4940 ccline.cmdbuff = NULL;
4941 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004943
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004944 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004945
4946 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004948 if (ccline.cmdbuff != NULL)
4949 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004950
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004951 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004952 return ret;
4953}
4954
4955/*
4956 * Expand names with a function defined by the user.
4957 */
4958 static int
4959ExpandUserDefined(xp, regmatch, num_file, file)
4960 expand_T *xp;
4961 regmatch_T *regmatch;
4962 int *num_file;
4963 char_u ***file;
4964{
4965 char_u *retstr;
4966 char_u *s;
4967 char_u *e;
4968 char_u keep;
4969 garray_T ga;
4970
4971 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4972 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 return FAIL;
4974
4975 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004976 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 {
4978 e = vim_strchr(s, '\n');
4979 if (e == NULL)
4980 e = s + STRLEN(s);
4981 keep = *e;
4982 *e = 0;
4983
4984 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4985 {
4986 *e = keep;
4987 if (*e != NUL)
4988 ++e;
4989 continue;
4990 }
4991
4992 if (ga_grow(&ga, 1) == FAIL)
4993 break;
4994
4995 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4996 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997
4998 *e = keep;
4999 if (*e != NUL)
5000 ++e;
5001 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005002 vim_free(retstr);
5003 *file = ga.ga_data;
5004 *num_file = ga.ga_len;
5005 return OK;
5006}
5007
5008/*
5009 * Expand names with a list returned by a function defined by the user.
5010 */
5011 static int
5012ExpandUserList(xp, num_file, file)
5013 expand_T *xp;
5014 int *num_file;
5015 char_u ***file;
5016{
5017 list_T *retlist;
5018 listitem_T *li;
5019 garray_T ga;
5020
5021 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5022 if (retlist == NULL)
5023 return FAIL;
5024
5025 ga_init2(&ga, (int)sizeof(char *), 3);
5026 /* Loop over the items in the list. */
5027 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5028 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005029 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5030 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005031
5032 if (ga_grow(&ga, 1) == FAIL)
5033 break;
5034
5035 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005036 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005037 ++ga.ga_len;
5038 }
5039 list_unref(retlist);
5040
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 *file = ga.ga_data;
5042 *num_file = ga.ga_len;
5043 return OK;
5044}
5045#endif
5046
5047/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005048 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005049 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005050 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 */
5052 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005053ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 char_u *pat;
5055 int *num_file;
5056 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005057 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005059 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 char_u *s;
5061 char_u *e;
5062 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005063 int i;
5064 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065
5066 *num_file = 0;
5067 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005068 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005069 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005071 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005073 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5074 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005076 ga_clear_strings(&ga);
5077 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005079 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5080 matches = globpath(p_rtp, s, 0);
5081 vim_free(s);
5082 if (matches == NULL)
5083 continue;
5084
5085 for (s = matches; *s != NUL; s = e)
5086 {
5087 e = vim_strchr(s, '\n');
5088 if (e == NULL)
5089 e = s + STRLEN(s);
5090 if (ga_grow(&ga, 1) == FAIL)
5091 break;
5092 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5093 {
5094 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5095 if (*s == '\n' || vim_ispathsep(*s))
5096 break;
5097 ++s;
5098 ((char_u **)ga.ga_data)[ga.ga_len] =
5099 vim_strnsave(s, (int)(e - s - 4));
5100 ++ga.ga_len;
5101 }
5102 if (*e != NUL)
5103 ++e;
5104 }
5105 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005107 if (ga.ga_len == 0)
5108 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005109
5110 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005111 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005112 remove_duplicates(&ga);
5113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 *file = ga.ga_data;
5115 *num_file = ga.ga_len;
5116 return OK;
5117}
5118
5119#endif
5120
5121#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5122/*
5123 * Expand "file" for all comma-separated directories in "path".
5124 * Returns an allocated string with all matches concatenated, separated by
5125 * newlines. Returns NULL for an error or no matches.
5126 */
5127 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005128globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 char_u *path;
5130 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005131 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
5133 expand_T xpc;
5134 char_u *buf;
5135 garray_T ga;
5136 int i;
5137 int len;
5138 int num_p;
5139 char_u **p;
5140 char_u *cur = NULL;
5141
5142 buf = alloc(MAXPATHL);
5143 if (buf == NULL)
5144 return NULL;
5145
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005146 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 ga_init2(&ga, 1, 100);
5150
5151 /* Loop over all entries in {path}. */
5152 while (*path != NUL)
5153 {
5154 /* Copy one item of the path to buf[] and concatenate the file name. */
5155 copy_option_part(&path, buf, MAXPATHL, ",");
5156 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5157 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005158# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005159 /* Using the platform's path separator (\) makes vim incorrectly
5160 * treat it as an escape character, use '/' instead. */
5161 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5162 STRCAT(buf, "/");
5163# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005167 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5168 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005170 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005172 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
5174 /* Concatenate new results to previous ones. */
5175 if (ga_grow(&ga, len) == OK)
5176 {
5177 cur = (char_u *)ga.ga_data + ga.ga_len;
5178 for (i = 0; i < num_p; ++i)
5179 {
5180 STRCPY(cur, p[i]);
5181 cur += STRLEN(p[i]);
5182 *cur++ = '\n';
5183 }
5184 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186 FreeWild(num_p, p);
5187 }
5188 }
5189 }
5190 if (cur != NULL)
5191 *--cur = 0; /* Replace trailing newline with NUL */
5192
5193 vim_free(buf);
5194 return (char_u *)ga.ga_data;
5195}
5196
5197#endif
5198
5199#if defined(FEAT_CMDHIST) || defined(PROTO)
5200
5201/*********************************
5202 * Command line history stuff *
5203 *********************************/
5204
5205/*
5206 * Translate a history character to the associated type number.
5207 */
5208 static int
5209hist_char2type(c)
5210 int c;
5211{
5212 if (c == ':')
5213 return HIST_CMD;
5214 if (c == '=')
5215 return HIST_EXPR;
5216 if (c == '@')
5217 return HIST_INPUT;
5218 if (c == '>')
5219 return HIST_DEBUG;
5220 return HIST_SEARCH; /* must be '?' or '/' */
5221}
5222
5223/*
5224 * Table of history names.
5225 * These names are used in :history and various hist...() functions.
5226 * It is sufficient to give the significant prefix of a history name.
5227 */
5228
5229static char *(history_names[]) =
5230{
5231 "cmd",
5232 "search",
5233 "expr",
5234 "input",
5235 "debug",
5236 NULL
5237};
5238
5239/*
5240 * init_history() - Initialize the command line history.
5241 * Also used to re-allocate the history when the size changes.
5242 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005243 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244init_history()
5245{
5246 int newlen; /* new length of history table */
5247 histentry_T *temp;
5248 int i;
5249 int j;
5250 int type;
5251
5252 /*
5253 * If size of history table changed, reallocate it
5254 */
5255 newlen = (int)p_hi;
5256 if (newlen != hislen) /* history length changed */
5257 {
5258 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5259 {
5260 if (newlen)
5261 {
5262 temp = (histentry_T *)lalloc(
5263 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5264 if (temp == NULL) /* out of memory! */
5265 {
5266 if (type == 0) /* first one: just keep the old length */
5267 {
5268 newlen = hislen;
5269 break;
5270 }
5271 /* Already changed one table, now we can only have zero
5272 * length for all tables. */
5273 newlen = 0;
5274 type = -1;
5275 continue;
5276 }
5277 }
5278 else
5279 temp = NULL;
5280 if (newlen == 0 || temp != NULL)
5281 {
5282 if (hisidx[type] < 0) /* there are no entries yet */
5283 {
5284 for (i = 0; i < newlen; ++i)
5285 {
5286 temp[i].hisnum = 0;
5287 temp[i].hisstr = NULL;
5288 }
5289 }
5290 else if (newlen > hislen) /* array becomes bigger */
5291 {
5292 for (i = 0; i <= hisidx[type]; ++i)
5293 temp[i] = history[type][i];
5294 j = i;
5295 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5296 {
5297 temp[i].hisnum = 0;
5298 temp[i].hisstr = NULL;
5299 }
5300 for ( ; j < hislen; ++i, ++j)
5301 temp[i] = history[type][j];
5302 }
5303 else /* array becomes smaller or 0 */
5304 {
5305 j = hisidx[type];
5306 for (i = newlen - 1; ; --i)
5307 {
5308 if (i >= 0) /* copy newest entries */
5309 temp[i] = history[type][j];
5310 else /* remove older entries */
5311 vim_free(history[type][j].hisstr);
5312 if (--j < 0)
5313 j = hislen - 1;
5314 if (j == hisidx[type])
5315 break;
5316 }
5317 hisidx[type] = newlen - 1;
5318 }
5319 vim_free(history[type]);
5320 history[type] = temp;
5321 }
5322 }
5323 hislen = newlen;
5324 }
5325}
5326
5327/*
5328 * Check if command line 'str' is already in history.
5329 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5330 */
5331 static int
Bram Moolenaar4c402232011-07-27 17:58:46 +02005332in_history(type, str, move_to_front, sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 int type;
5334 char_u *str;
5335 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005336 int sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337{
5338 int i;
5339 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005340 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341
5342 if (hisidx[type] < 0)
5343 return FALSE;
5344 i = hisidx[type];
5345 do
5346 {
5347 if (history[type][i].hisstr == NULL)
5348 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005349
5350 /* For search history, check that the separator character matches as
5351 * well. */
5352 p = history[type][i].hisstr;
5353 if (STRCMP(str, p) == 0
5354 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 {
5356 if (!move_to_front)
5357 return TRUE;
5358 last_i = i;
5359 break;
5360 }
5361 if (--i < 0)
5362 i = hislen - 1;
5363 } while (i != hisidx[type]);
5364
5365 if (last_i >= 0)
5366 {
5367 str = history[type][i].hisstr;
5368 while (i != hisidx[type])
5369 {
5370 if (++i >= hislen)
5371 i = 0;
5372 history[type][last_i] = history[type][i];
5373 last_i = i;
5374 }
5375 history[type][i].hisstr = str;
5376 history[type][i].hisnum = ++hisnum[type];
5377 return TRUE;
5378 }
5379 return FALSE;
5380}
5381
5382/*
5383 * Convert history name (from table above) to its HIST_ equivalent.
5384 * When "name" is empty, return "cmd" history.
5385 * Returns -1 for unknown history name.
5386 */
5387 int
5388get_histtype(name)
5389 char_u *name;
5390{
5391 int i;
5392 int len = (int)STRLEN(name);
5393
5394 /* No argument: use current history. */
5395 if (len == 0)
5396 return hist_char2type(ccline.cmdfirstc);
5397
5398 for (i = 0; history_names[i] != NULL; ++i)
5399 if (STRNICMP(name, history_names[i], len) == 0)
5400 return i;
5401
5402 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5403 return hist_char2type(name[0]);
5404
5405 return -1;
5406}
5407
5408static int last_maptick = -1; /* last seen maptick */
5409
5410/*
5411 * Add the given string to the given history. If the string is already in the
5412 * history then it is moved to the front. "histype" may be one of he HIST_
5413 * values.
5414 */
5415 void
5416add_to_history(histype, new_entry, in_map, sep)
5417 int histype;
5418 char_u *new_entry;
5419 int in_map; /* consider maptick when inside a mapping */
5420 int sep; /* separator character used (search hist) */
5421{
5422 histentry_T *hisptr;
5423 int len;
5424
5425 if (hislen == 0) /* no history */
5426 return;
5427
5428 /*
5429 * Searches inside the same mapping overwrite each other, so that only
5430 * the last line is kept. Be careful not to remove a line that was moved
5431 * down, only lines that were added.
5432 */
5433 if (histype == HIST_SEARCH && in_map)
5434 {
5435 if (maptick == last_maptick)
5436 {
5437 /* Current line is from the same mapping, remove it */
5438 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5439 vim_free(hisptr->hisstr);
5440 hisptr->hisstr = NULL;
5441 hisptr->hisnum = 0;
5442 --hisnum[histype];
5443 if (--hisidx[HIST_SEARCH] < 0)
5444 hisidx[HIST_SEARCH] = hislen - 1;
5445 }
5446 last_maptick = -1;
5447 }
Bram Moolenaar4c402232011-07-27 17:58:46 +02005448 if (!in_history(histype, new_entry, TRUE, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 {
5450 if (++hisidx[histype] == hislen)
5451 hisidx[histype] = 0;
5452 hisptr = &history[histype][hisidx[histype]];
5453 vim_free(hisptr->hisstr);
5454
5455 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005456 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5458 if (hisptr->hisstr != NULL)
5459 hisptr->hisstr[len + 1] = sep;
5460
5461 hisptr->hisnum = ++hisnum[histype];
5462 if (histype == HIST_SEARCH && in_map)
5463 last_maptick = maptick;
5464 }
5465}
5466
5467#if defined(FEAT_EVAL) || defined(PROTO)
5468
5469/*
5470 * Get identifier of newest history entry.
5471 * "histype" may be one of the HIST_ values.
5472 */
5473 int
5474get_history_idx(histype)
5475 int histype;
5476{
5477 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5478 || hisidx[histype] < 0)
5479 return -1;
5480
5481 return history[histype][hisidx[histype]].hisnum;
5482}
5483
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005484static struct cmdline_info *get_ccline_ptr __ARGS((void));
5485
5486/*
5487 * Get pointer to the command line info to use. cmdline_paste() may clear
5488 * ccline and put the previous value in prev_ccline.
5489 */
5490 static struct cmdline_info *
5491get_ccline_ptr()
5492{
5493 if ((State & CMDLINE) == 0)
5494 return NULL;
5495 if (ccline.cmdbuff != NULL)
5496 return &ccline;
5497 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5498 return &prev_ccline;
5499 return NULL;
5500}
5501
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502/*
5503 * Get the current command line in allocated memory.
5504 * Only works when the command line is being edited.
5505 * Returns NULL when something is wrong.
5506 */
5507 char_u *
5508get_cmdline_str()
5509{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005510 struct cmdline_info *p = get_ccline_ptr();
5511
5512 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005514 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515}
5516
5517/*
5518 * Get the current command line position, counted in bytes.
5519 * Zero is the first position.
5520 * Only works when the command line is being edited.
5521 * Returns -1 when something is wrong.
5522 */
5523 int
5524get_cmdline_pos()
5525{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005526 struct cmdline_info *p = get_ccline_ptr();
5527
5528 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005530 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531}
5532
5533/*
5534 * Set the command line byte position to "pos". Zero is the first position.
5535 * Only works when the command line is being edited.
5536 * Returns 1 when failed, 0 when OK.
5537 */
5538 int
5539set_cmdline_pos(pos)
5540 int pos;
5541{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005542 struct cmdline_info *p = get_ccline_ptr();
5543
5544 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 return 1;
5546
5547 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5548 * changed the command line. */
5549 if (pos < 0)
5550 new_cmdpos = 0;
5551 else
5552 new_cmdpos = pos;
5553 return 0;
5554}
5555
5556/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005557 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005558 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005559 * Only works when the command line is being edited.
5560 * Returns NUL when something is wrong.
5561 */
5562 int
5563get_cmdline_type()
5564{
5565 struct cmdline_info *p = get_ccline_ptr();
5566
5567 if (p == NULL)
5568 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005569 if (p->cmdfirstc == NUL)
5570 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005571 return p->cmdfirstc;
5572}
5573
5574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 * Calculate history index from a number:
5576 * num > 0: seen as identifying number of a history entry
5577 * num < 0: relative position in history wrt newest entry
5578 * "histype" may be one of the HIST_ values.
5579 */
5580 static int
5581calc_hist_idx(histype, num)
5582 int histype;
5583 int num;
5584{
5585 int i;
5586 histentry_T *hist;
5587 int wrapped = FALSE;
5588
5589 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5590 || (i = hisidx[histype]) < 0 || num == 0)
5591 return -1;
5592
5593 hist = history[histype];
5594 if (num > 0)
5595 {
5596 while (hist[i].hisnum > num)
5597 if (--i < 0)
5598 {
5599 if (wrapped)
5600 break;
5601 i += hislen;
5602 wrapped = TRUE;
5603 }
5604 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5605 return i;
5606 }
5607 else if (-num <= hislen)
5608 {
5609 i += num + 1;
5610 if (i < 0)
5611 i += hislen;
5612 if (hist[i].hisstr != NULL)
5613 return i;
5614 }
5615 return -1;
5616}
5617
5618/*
5619 * Get a history entry by its index.
5620 * "histype" may be one of the HIST_ values.
5621 */
5622 char_u *
5623get_history_entry(histype, idx)
5624 int histype;
5625 int idx;
5626{
5627 idx = calc_hist_idx(histype, idx);
5628 if (idx >= 0)
5629 return history[histype][idx].hisstr;
5630 else
5631 return (char_u *)"";
5632}
5633
5634/*
5635 * Clear all entries of a history.
5636 * "histype" may be one of the HIST_ values.
5637 */
5638 int
5639clr_history(histype)
5640 int histype;
5641{
5642 int i;
5643 histentry_T *hisptr;
5644
5645 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5646 {
5647 hisptr = history[histype];
5648 for (i = hislen; i--;)
5649 {
5650 vim_free(hisptr->hisstr);
5651 hisptr->hisnum = 0;
5652 hisptr++->hisstr = NULL;
5653 }
5654 hisidx[histype] = -1; /* mark history as cleared */
5655 hisnum[histype] = 0; /* reset identifier counter */
5656 return OK;
5657 }
5658 return FAIL;
5659}
5660
5661/*
5662 * Remove all entries matching {str} from a history.
5663 * "histype" may be one of the HIST_ values.
5664 */
5665 int
5666del_history_entry(histype, str)
5667 int histype;
5668 char_u *str;
5669{
5670 regmatch_T regmatch;
5671 histentry_T *hisptr;
5672 int idx;
5673 int i;
5674 int last;
5675 int found = FALSE;
5676
5677 regmatch.regprog = NULL;
5678 regmatch.rm_ic = FALSE; /* always match case */
5679 if (hislen != 0
5680 && histype >= 0
5681 && histype < HIST_COUNT
5682 && *str != NUL
5683 && (idx = hisidx[histype]) >= 0
5684 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5685 != NULL)
5686 {
5687 i = last = idx;
5688 do
5689 {
5690 hisptr = &history[histype][i];
5691 if (hisptr->hisstr == NULL)
5692 break;
5693 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5694 {
5695 found = TRUE;
5696 vim_free(hisptr->hisstr);
5697 hisptr->hisstr = NULL;
5698 hisptr->hisnum = 0;
5699 }
5700 else
5701 {
5702 if (i != last)
5703 {
5704 history[histype][last] = *hisptr;
5705 hisptr->hisstr = NULL;
5706 hisptr->hisnum = 0;
5707 }
5708 if (--last < 0)
5709 last += hislen;
5710 }
5711 if (--i < 0)
5712 i += hislen;
5713 } while (i != idx);
5714 if (history[histype][idx].hisstr == NULL)
5715 hisidx[histype] = -1;
5716 }
5717 vim_free(regmatch.regprog);
5718 return found;
5719}
5720
5721/*
5722 * Remove an indexed entry from a history.
5723 * "histype" may be one of the HIST_ values.
5724 */
5725 int
5726del_history_idx(histype, idx)
5727 int histype;
5728 int idx;
5729{
5730 int i, j;
5731
5732 i = calc_hist_idx(histype, idx);
5733 if (i < 0)
5734 return FALSE;
5735 idx = hisidx[histype];
5736 vim_free(history[histype][i].hisstr);
5737
5738 /* When deleting the last added search string in a mapping, reset
5739 * last_maptick, so that the last added search string isn't deleted again.
5740 */
5741 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5742 last_maptick = -1;
5743
5744 while (i != idx)
5745 {
5746 j = (i + 1) % hislen;
5747 history[histype][i] = history[histype][j];
5748 i = j;
5749 }
5750 history[histype][i].hisstr = NULL;
5751 history[histype][i].hisnum = 0;
5752 if (--i < 0)
5753 i += hislen;
5754 hisidx[histype] = i;
5755 return TRUE;
5756}
5757
5758#endif /* FEAT_EVAL */
5759
5760#if defined(FEAT_CRYPT) || defined(PROTO)
5761/*
5762 * Very specific function to remove the value in ":set key=val" from the
5763 * history.
5764 */
5765 void
5766remove_key_from_history()
5767{
5768 char_u *p;
5769 int i;
5770
5771 i = hisidx[HIST_CMD];
5772 if (i < 0)
5773 return;
5774 p = history[HIST_CMD][i].hisstr;
5775 if (p != NULL)
5776 for ( ; *p; ++p)
5777 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5778 {
5779 p = vim_strchr(p + 3, '=');
5780 if (p == NULL)
5781 break;
5782 ++p;
5783 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5784 if (p[i] == '\\' && p[i + 1])
5785 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005786 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 --p;
5788 }
5789}
5790#endif
5791
5792#endif /* FEAT_CMDHIST */
5793
5794#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5795/*
5796 * Get indices "num1,num2" that specify a range within a list (not a range of
5797 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5798 * Returns OK if parsed successfully, otherwise FAIL.
5799 */
5800 int
5801get_list_range(str, num1, num2)
5802 char_u **str;
5803 int *num1;
5804 int *num2;
5805{
5806 int len;
5807 int first = FALSE;
5808 long num;
5809
5810 *str = skipwhite(*str);
5811 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5812 {
5813 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5814 *str += len;
5815 *num1 = (int)num;
5816 first = TRUE;
5817 }
5818 *str = skipwhite(*str);
5819 if (**str == ',') /* parse "to" part of range */
5820 {
5821 *str = skipwhite(*str + 1);
5822 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5823 if (len > 0)
5824 {
5825 *num2 = (int)num;
5826 *str = skipwhite(*str + len);
5827 }
5828 else if (!first) /* no number given at all */
5829 return FAIL;
5830 }
5831 else if (first) /* only one number given */
5832 *num2 = *num1;
5833 return OK;
5834}
5835#endif
5836
5837#if defined(FEAT_CMDHIST) || defined(PROTO)
5838/*
5839 * :history command - print a history
5840 */
5841 void
5842ex_history(eap)
5843 exarg_T *eap;
5844{
5845 histentry_T *hist;
5846 int histype1 = HIST_CMD;
5847 int histype2 = HIST_CMD;
5848 int hisidx1 = 1;
5849 int hisidx2 = -1;
5850 int idx;
5851 int i, j, k;
5852 char_u *end;
5853 char_u *arg = eap->arg;
5854
5855 if (hislen == 0)
5856 {
5857 MSG(_("'history' option is zero"));
5858 return;
5859 }
5860
5861 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5862 {
5863 end = arg;
5864 while (ASCII_ISALPHA(*end)
5865 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5866 end++;
5867 i = *end;
5868 *end = NUL;
5869 histype1 = get_histtype(arg);
5870 if (histype1 == -1)
5871 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005872 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 {
5874 histype1 = 0;
5875 histype2 = HIST_COUNT-1;
5876 }
5877 else
5878 {
5879 *end = i;
5880 EMSG(_(e_trailing));
5881 return;
5882 }
5883 }
5884 else
5885 histype2 = histype1;
5886 *end = i;
5887 }
5888 else
5889 end = arg;
5890 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5891 {
5892 EMSG(_(e_trailing));
5893 return;
5894 }
5895
5896 for (; !got_int && histype1 <= histype2; ++histype1)
5897 {
5898 STRCPY(IObuff, "\n # ");
5899 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5900 MSG_PUTS_TITLE(IObuff);
5901 idx = hisidx[histype1];
5902 hist = history[histype1];
5903 j = hisidx1;
5904 k = hisidx2;
5905 if (j < 0)
5906 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5907 if (k < 0)
5908 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5909 if (idx >= 0 && j <= k)
5910 for (i = idx + 1; !got_int; ++i)
5911 {
5912 if (i == hislen)
5913 i = 0;
5914 if (hist[i].hisstr != NULL
5915 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5916 {
5917 msg_putchar('\n');
5918 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5919 hist[i].hisnum);
5920 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5921 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5922 (int)Columns - 10);
5923 else
5924 STRCAT(IObuff, hist[i].hisstr);
5925 msg_outtrans(IObuff);
5926 out_flush();
5927 }
5928 if (i == idx)
5929 break;
5930 }
5931 }
5932}
5933#endif
5934
5935#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5936static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5937static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5938static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5939static int viminfo_add_at_front = FALSE;
5940
5941static int hist_type2char __ARGS((int type, int use_question));
5942
5943/*
5944 * Translate a history type number to the associated character.
5945 */
5946 static int
5947hist_type2char(type, use_question)
5948 int type;
5949 int use_question; /* use '?' instead of '/' */
5950{
5951 if (type == HIST_CMD)
5952 return ':';
5953 if (type == HIST_SEARCH)
5954 {
5955 if (use_question)
5956 return '?';
5957 else
5958 return '/';
5959 }
5960 if (type == HIST_EXPR)
5961 return '=';
5962 return '@';
5963}
5964
5965/*
5966 * Prepare for reading the history from the viminfo file.
5967 * This allocates history arrays to store the read history lines.
5968 */
5969 void
5970prepare_viminfo_history(asklen)
5971 int asklen;
5972{
5973 int i;
5974 int num;
5975 int type;
5976 int len;
5977
5978 init_history();
5979 viminfo_add_at_front = (asklen != 0);
5980 if (asklen > hislen)
5981 asklen = hislen;
5982
5983 for (type = 0; type < HIST_COUNT; ++type)
5984 {
5985 /*
5986 * Count the number of empty spaces in the history list. If there are
5987 * more spaces available than we request, then fill them up.
5988 */
5989 for (i = 0, num = 0; i < hislen; i++)
5990 if (history[type][i].hisstr == NULL)
5991 num++;
5992 len = asklen;
5993 if (num > len)
5994 len = num;
5995 if (len <= 0)
5996 viminfo_history[type] = NULL;
5997 else
5998 viminfo_history[type] =
5999 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6000 if (viminfo_history[type] == NULL)
6001 len = 0;
6002 viminfo_hislen[type] = len;
6003 viminfo_hisidx[type] = 0;
6004 }
6005}
6006
6007/*
6008 * Accept a line from the viminfo, store it in the history array when it's
6009 * new.
6010 */
6011 int
6012read_viminfo_history(virp)
6013 vir_T *virp;
6014{
6015 int type;
6016 long_u len;
6017 char_u *val;
6018 char_u *p;
6019
6020 type = hist_char2type(virp->vir_line[0]);
6021 if (viminfo_hisidx[type] < viminfo_hislen[type])
6022 {
6023 val = viminfo_readstring(virp, 1, TRUE);
6024 if (val != NULL && *val != NUL)
6025 {
6026 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar4c402232011-07-27 17:58:46 +02006027 viminfo_add_at_front, *val))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 {
6029 /* Need to re-allocate to append the separator byte. */
6030 len = STRLEN(val);
6031 p = lalloc(len + 2, TRUE);
6032 if (p != NULL)
6033 {
6034 if (type == HIST_SEARCH)
6035 {
6036 /* Search entry: Move the separator from the first
6037 * column to after the NUL. */
6038 mch_memmove(p, val + 1, (size_t)len);
6039 p[len] = (*val == ' ' ? NUL : *val);
6040 }
6041 else
6042 {
6043 /* Not a search entry: No separator in the viminfo
6044 * file, add a NUL separator. */
6045 mch_memmove(p, val, (size_t)len + 1);
6046 p[len + 1] = NUL;
6047 }
6048 viminfo_history[type][viminfo_hisidx[type]++] = p;
6049 }
6050 }
6051 }
6052 vim_free(val);
6053 }
6054 return viminfo_readline(virp);
6055}
6056
6057 void
6058finish_viminfo_history()
6059{
6060 int idx;
6061 int i;
6062 int type;
6063
6064 for (type = 0; type < HIST_COUNT; ++type)
6065 {
6066 if (history[type] == NULL)
6067 return;
6068 idx = hisidx[type] + viminfo_hisidx[type];
6069 if (idx >= hislen)
6070 idx -= hislen;
6071 else if (idx < 0)
6072 idx = hislen - 1;
6073 if (viminfo_add_at_front)
6074 hisidx[type] = idx;
6075 else
6076 {
6077 if (hisidx[type] == -1)
6078 hisidx[type] = hislen - 1;
6079 do
6080 {
6081 if (history[type][idx].hisstr != NULL)
6082 break;
6083 if (++idx == hislen)
6084 idx = 0;
6085 } while (idx != hisidx[type]);
6086 if (idx != hisidx[type] && --idx < 0)
6087 idx = hislen - 1;
6088 }
6089 for (i = 0; i < viminfo_hisidx[type]; i++)
6090 {
6091 vim_free(history[type][idx].hisstr);
6092 history[type][idx].hisstr = viminfo_history[type][i];
6093 if (--idx < 0)
6094 idx = hislen - 1;
6095 }
6096 idx += 1;
6097 idx %= hislen;
6098 for (i = 0; i < viminfo_hisidx[type]; i++)
6099 {
6100 history[type][idx++].hisnum = ++hisnum[type];
6101 idx %= hislen;
6102 }
6103 vim_free(viminfo_history[type]);
6104 viminfo_history[type] = NULL;
6105 }
6106}
6107
6108 void
6109write_viminfo_history(fp)
6110 FILE *fp;
6111{
6112 int i;
6113 int type;
6114 int num_saved;
6115 char_u *p;
6116 int c;
6117
6118 init_history();
6119 if (hislen == 0)
6120 return;
6121 for (type = 0; type < HIST_COUNT; ++type)
6122 {
6123 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6124 if (num_saved == 0)
6125 continue;
6126 if (num_saved < 0) /* Use default */
6127 num_saved = hislen;
6128 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6129 type == HIST_CMD ? _("Command Line") :
6130 type == HIST_SEARCH ? _("Search String") :
6131 type == HIST_EXPR ? _("Expression") :
6132 _("Input Line"));
6133 if (num_saved > hislen)
6134 num_saved = hislen;
6135 i = hisidx[type];
6136 if (i >= 0)
6137 while (num_saved--)
6138 {
6139 p = history[type][i].hisstr;
6140 if (p != NULL)
6141 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006142 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 /* For the search history: put the separator in the second
6144 * column; use a space if there isn't one. */
6145 if (type == HIST_SEARCH)
6146 {
6147 c = p[STRLEN(p) + 1];
6148 putc(c == NUL ? ' ' : c, fp);
6149 }
6150 viminfo_writestring(fp, p);
6151 }
6152 if (--i < 0)
6153 i = hislen - 1;
6154 }
6155 }
6156}
6157#endif /* FEAT_VIMINFO */
6158
6159#if defined(FEAT_FKMAP) || defined(PROTO)
6160/*
6161 * Write a character at the current cursor+offset position.
6162 * It is directly written into the command buffer block.
6163 */
6164 void
6165cmd_pchar(c, offset)
6166 int c, offset;
6167{
6168 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6169 {
6170 EMSG(_("E198: cmd_pchar beyond the command length"));
6171 return;
6172 }
6173 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6174 ccline.cmdbuff[ccline.cmdlen] = NUL;
6175}
6176
6177 int
6178cmd_gchar(offset)
6179 int offset;
6180{
6181 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6182 {
6183 /* EMSG(_("cmd_gchar beyond the command length")); */
6184 return NUL;
6185 }
6186 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6187}
6188#endif
6189
6190#if defined(FEAT_CMDWIN) || defined(PROTO)
6191/*
6192 * Open a window on the current command line and history. Allow editing in
6193 * the window. Returns when the window is closed.
6194 * Returns:
6195 * CR if the command is to be executed
6196 * Ctrl_C if it is to be abandoned
6197 * K_IGNORE if editing continues
6198 */
6199 static int
6200ex_window()
6201{
6202 struct cmdline_info save_ccline;
6203 buf_T *old_curbuf = curbuf;
6204 win_T *old_curwin = curwin;
6205 buf_T *bp;
6206 win_T *wp;
6207 int i;
6208 linenr_T lnum;
6209 int histtype;
6210 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006211#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 int save_restart_edit = restart_edit;
6215 int save_State = State;
6216 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006217#ifdef FEAT_RIGHTLEFT
6218 int save_cmdmsg_rl = cmdmsg_rl;
6219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220
6221 /* Can't do this recursively. Can't do it when typing a password. */
6222 if (cmdwin_type != 0
6223# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6224 || cmdline_star > 0
6225# endif
6226 )
6227 {
6228 beep_flush();
6229 return K_IGNORE;
6230 }
6231
6232 /* Save current window sizes. */
6233 win_size_save(&winsizes);
6234
6235# ifdef FEAT_AUTOCMD
6236 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006237 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006239 /* don't use a new tab page */
6240 cmdmod.tab = 0;
6241
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 /* Create a window for the command-line buffer. */
6243 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6244 {
6245 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006246# ifdef FEAT_AUTOCMD
6247 unblock_autocmds();
6248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 return K_IGNORE;
6250 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006251 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
6253 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006254 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006255 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6257 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6258 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006259#ifdef FEAT_FOLDING
6260 curwin->w_p_fen = FALSE;
6261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006263 curwin->w_p_rl = cmdmsg_rl;
6264 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006266 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267
6268# ifdef FEAT_AUTOCMD
6269 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006270 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271# endif
6272
Bram Moolenaar46152342005-09-07 21:18:43 +00006273 /* Showing the prompt may have set need_wait_return, reset it. */
6274 need_wait_return = FALSE;
6275
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006276 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6278 {
6279 if (p_wc == TAB)
6280 {
6281 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6282 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6283 }
6284 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6285 }
6286
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006287 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6288 * sets 'textwidth' to 78). */
6289 curbuf->b_p_tw = 0;
6290
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 /* Fill the buffer with the history. */
6292 init_history();
6293 if (hislen > 0)
6294 {
6295 i = hisidx[histtype];
6296 if (i >= 0)
6297 {
6298 lnum = 0;
6299 do
6300 {
6301 if (++i == hislen)
6302 i = 0;
6303 if (history[histtype][i].hisstr != NULL)
6304 ml_append(lnum++, history[histtype][i].hisstr,
6305 (colnr_T)0, FALSE);
6306 }
6307 while (i != hisidx[histtype]);
6308 }
6309 }
6310
6311 /* Replace the empty last line with the current command-line and put the
6312 * cursor there. */
6313 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6314 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6315 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006316 changed_line_abv_curs();
6317 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006318 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319
6320 /* Save the command line info, can be used recursively. */
6321 save_ccline = ccline;
6322 ccline.cmdbuff = NULL;
6323 ccline.cmdprompt = NULL;
6324
6325 /* No Ex mode here! */
6326 exmode_active = 0;
6327
6328 State = NORMAL;
6329# ifdef FEAT_MOUSE
6330 setmouse();
6331# endif
6332
6333# ifdef FEAT_AUTOCMD
6334 /* Trigger CmdwinEnter autocommands. */
6335 typestr[0] = cmdwin_type;
6336 typestr[1] = NUL;
6337 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006338 if (restart_edit != 0) /* autocmd with ":startinsert" */
6339 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340# endif
6341
6342 i = RedrawingDisabled;
6343 RedrawingDisabled = 0;
6344
6345 /*
6346 * Call the main loop until <CR> or CTRL-C is typed.
6347 */
6348 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006349 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350
6351 RedrawingDisabled = i;
6352
6353# ifdef FEAT_AUTOCMD
6354 /* Trigger CmdwinLeave autocommands. */
6355 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6356# endif
6357
Bram Moolenaarccc18222007-05-10 18:25:20 +00006358 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 ccline = save_ccline;
6360 cmdwin_type = 0;
6361
6362 exmode_active = save_exmode;
6363
Bram Moolenaarf9821062008-06-20 16:31:07 +00006364 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 * this happens! */
6366 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6367 {
6368 cmdwin_result = Ctrl_C;
6369 EMSG(_("E199: Active window or buffer deleted"));
6370 }
6371 else
6372 {
6373# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6374 /* autocmds may abort script processing */
6375 if (aborting() && cmdwin_result != K_IGNORE)
6376 cmdwin_result = Ctrl_C;
6377# endif
6378 /* Set the new command line from the cmdline buffer. */
6379 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006380 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006382 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6383
6384 if (histtype == HIST_CMD)
6385 {
6386 /* Execute the command directly. */
6387 ccline.cmdbuff = vim_strsave((char_u *)p);
6388 cmdwin_result = CAR;
6389 }
6390 else
6391 {
6392 /* First need to cancel what we were doing. */
6393 ccline.cmdbuff = NULL;
6394 stuffcharReadbuff(':');
6395 stuffReadbuff((char_u *)p);
6396 stuffcharReadbuff(CAR);
6397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399 else if (cmdwin_result == K_XF2) /* :qa typed */
6400 {
6401 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6402 cmdwin_result = CAR;
6403 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006404 else if (cmdwin_result == Ctrl_C)
6405 {
6406 /* :q or :close, don't execute any command
6407 * and don't modify the cmd window. */
6408 ccline.cmdbuff = NULL;
6409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 else
6411 ccline.cmdbuff = vim_strsave(ml_get_curline());
6412 if (ccline.cmdbuff == NULL)
6413 cmdwin_result = Ctrl_C;
6414 else
6415 {
6416 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6417 ccline.cmdbufflen = ccline.cmdlen + 1;
6418 ccline.cmdpos = curwin->w_cursor.col;
6419 if (ccline.cmdpos > ccline.cmdlen)
6420 ccline.cmdpos = ccline.cmdlen;
6421 if (cmdwin_result == K_IGNORE)
6422 {
6423 set_cmdspos_cursor();
6424 redrawcmd();
6425 }
6426 }
6427
6428# ifdef FEAT_AUTOCMD
6429 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006430 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431# endif
6432 wp = curwin;
6433 bp = curbuf;
6434 win_goto(old_curwin);
6435 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006436
6437 /* win_close() may have already wiped the buffer when 'bh' is
6438 * set to 'wipe' */
6439 if (buf_valid(bp))
6440 close_buffer(NULL, bp, DOBUF_WIPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441
6442 /* Restore window sizes. */
6443 win_size_restore(&winsizes);
6444
6445# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006446 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447# endif
6448 }
6449
6450 ga_clear(&winsizes);
6451 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006452# ifdef FEAT_RIGHTLEFT
6453 cmdmsg_rl = save_cmdmsg_rl;
6454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455
6456 State = save_State;
6457# ifdef FEAT_MOUSE
6458 setmouse();
6459# endif
6460
6461 return cmdwin_result;
6462}
6463#endif /* FEAT_CMDWIN */
6464
6465/*
6466 * Used for commands that either take a simple command string argument, or:
6467 * cmd << endmarker
6468 * {script}
6469 * endmarker
6470 * Returns a pointer to allocated memory with {script} or NULL.
6471 */
6472 char_u *
6473script_get(eap, cmd)
6474 exarg_T *eap;
6475 char_u *cmd;
6476{
6477 char_u *theline;
6478 char *end_pattern = NULL;
6479 char dot[] = ".";
6480 garray_T ga;
6481
6482 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6483 return NULL;
6484
6485 ga_init2(&ga, 1, 0x400);
6486
6487 if (cmd[2] != NUL)
6488 end_pattern = (char *)skipwhite(cmd + 2);
6489 else
6490 end_pattern = dot;
6491
6492 for (;;)
6493 {
6494 theline = eap->getline(
6495#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006496 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497#endif
6498 NUL, eap->cookie, 0);
6499
6500 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006501 {
6502 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
6506 ga_concat(&ga, theline);
6507 ga_append(&ga, '\n');
6508 vim_free(theline);
6509 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006510 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
6512 return (char_u *)ga.ga_data;
6513}