blob: 38775af605b1361d63962f10d3f53eb650b7f56c [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
16/*
17 * Variables shared between getcmdline(), redrawcmdline() and others.
18 * These need to be saved when using CTRL-R |, that's why they are in a
19 * structure.
20 */
21struct cmdline_info
22{
23 char_u *cmdbuff; /* pointer to command line buffer */
24 int cmdbufflen; /* length of cmdbuff */
25 int cmdlen; /* number of chars in command line */
26 int cmdpos; /* current cursor position */
27 int cmdspos; /* cursor column on screen */
28 int cmdfirstc; /* ':', '/', '?', '=' or NUL */
29 int cmdindent; /* number of spaces before cmdline */
30 char_u *cmdprompt; /* message in front of cmdline */
31 int cmdattr; /* attributes for prompt */
32 int overstrike; /* Typing mode on the command line. Shared by
33 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000034 expand_T *xpc; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000036 int xp_context; /* type of expansion */
37# ifdef FEAT_EVAL
38 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000039 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041};
42
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000043/* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49static int cmd_showtail; /* Only show path tail in lists ? */
50
51#ifdef FEAT_EVAL
52static int new_cmdpos; /* position set by set_cmdline_pos() */
53#endif
54
55#ifdef FEAT_CMDHIST
56typedef struct hist_entry
57{
58 int hisnum; /* identifying number */
59 char_u *hisstr; /* actual entry, separator char after the NUL */
60} histentry_T;
61
62static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
63static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
64static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
65 /* identifying (unique) number of newest history entry */
66static int hislen = 0; /* actual length of history tables */
67
68static int hist_char2type __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
Bram Moolenaar4c402232011-07-27 17:58:46 +020070static int in_history __ARGS((int, char_u *, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000071# ifdef FEAT_EVAL
72static int calc_hist_idx __ARGS((int histype, int num));
73# endif
74#endif
75
76#ifdef FEAT_RIGHTLEFT
77static int cmd_hkmap = 0; /* Hebrew mapping during command line */
78#endif
79
80#ifdef FEAT_FKMAP
81static int cmd_fkmap = 0; /* Farsi mapping during command line */
82#endif
83
84static int cmdline_charsize __ARGS((int idx));
85static void set_cmdspos __ARGS((void));
86static void set_cmdspos_cursor __ARGS((void));
87#ifdef FEAT_MBYTE
88static void correct_cmdspos __ARGS((int idx, int cells));
89#endif
90static void alloc_cmdbuff __ARGS((int len));
91static int realloc_cmdbuff __ARGS((int len));
92static void draw_cmdline __ARGS((int start, int len));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +000093static void save_cmdline __ARGS((struct cmdline_info *ccp));
94static void restore_cmdline __ARGS((struct cmdline_info *ccp));
Bram Moolenaar1769d5a2006-10-17 14:25:24 +000095static int cmdline_paste __ARGS((int regname, int literally, int remcr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
97static void redrawcmd_preedit __ARGS((void));
98#endif
99#ifdef FEAT_WILDMENU
100static void cmdline_del __ARGS((int from));
101#endif
102static void redrawcmdprompt __ARGS((void));
103static void cursorcmd __ARGS((void));
104static int ccheck_abbr __ARGS((int));
105static int nextwild __ARGS((expand_T *xp, int type, int options));
Bram Moolenaar45360022005-07-21 21:08:21 +0000106static void escape_fname __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static int showmatches __ARGS((expand_T *xp, int wildmenu));
108static void set_expand_context __ARGS((expand_T *xp));
109static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int));
110static int expand_showtail __ARGS((expand_T *xp));
111#ifdef FEAT_CMDL_COMPL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000112static int expand_shellcmd __ARGS((char_u *filepat, int *num_file, char_u ***file, int flagsarg));
Bram Moolenaar0c7437a2011-06-26 19:40:23 +0200113static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname[]));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
115static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000116static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117# endif
118#endif
119
120#ifdef FEAT_CMDWIN
121static int ex_window __ARGS((void));
122#endif
123
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200124#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
125static int
126#ifdef __BORLANDC__
127_RTLENTRYF
128#endif
129sort_func_compare __ARGS((const void *s1, const void *s2));
130#endif
131
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132/*
133 * getcmdline() - accept a command line starting with firstc.
134 *
135 * firstc == ':' get ":" command line.
136 * firstc == '/' or '?' get search pattern
137 * firstc == '=' get expression
138 * firstc == '@' get text for input() function
139 * firstc == '>' get text for debug mode
140 * firstc == NUL get text for :insert command
141 * firstc == -1 like NUL, and break on CTRL-C
142 *
143 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
144 * command line.
145 *
146 * Careful: getcmdline() can be called recursively!
147 *
148 * Return pointer to allocated string if there is a commandline, NULL
149 * otherwise.
150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 char_u *
152getcmdline(firstc, count, indent)
153 int firstc;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000154 long count UNUSED; /* only used for incremental search */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 int indent; /* indent for inside conditionals */
156{
157 int c;
158 int i;
159 int j;
160 int gotesc = FALSE; /* TRUE when <ESC> just typed */
161 int do_abbr; /* when TRUE check for abbr. */
162#ifdef FEAT_CMDHIST
163 char_u *lookfor = NULL; /* string to match */
164 int hiscnt; /* current history line in use */
165 int histype; /* history type to be used */
166#endif
167#ifdef FEAT_SEARCH_EXTRA
168 pos_T old_cursor;
169 colnr_T old_curswant;
170 colnr_T old_leftcol;
171 linenr_T old_topline;
172# ifdef FEAT_DIFF
173 int old_topfill;
174# endif
175 linenr_T old_botline;
176 int did_incsearch = FALSE;
177 int incsearch_postponed = FALSE;
178#endif
179 int did_wild_list = FALSE; /* did wild_list() recently */
180 int wim_index = 0; /* index in wim_flags[] */
181 int res;
182 int save_msg_scroll = msg_scroll;
183 int save_State = State; /* remember State when called */
184 int some_key_typed = FALSE; /* one of the keys was typed */
185#ifdef FEAT_MOUSE
186 /* mouse drag and release events are ignored, unless they are
187 * preceded with a mouse down event */
188 int ignore_drag_release = TRUE;
189#endif
190#ifdef FEAT_EVAL
191 int break_ctrl_c = FALSE;
192#endif
193 expand_T xpc;
194 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000195#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
196 /* Everything that may work recursively should save and restore the
197 * current command line in save_ccline. That includes update_screen(), a
198 * custom status line may invoke ":normal". */
199 struct cmdline_info save_ccline;
200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
202#ifdef FEAT_SNIFF
203 want_sniff_request = 0;
204#endif
205#ifdef FEAT_EVAL
206 if (firstc == -1)
207 {
208 firstc = NUL;
209 break_ctrl_c = TRUE;
210 }
211#endif
212#ifdef FEAT_RIGHTLEFT
213 /* start without Hebrew mapping for a command line */
214 if (firstc == ':' || firstc == '=' || firstc == '>')
215 cmd_hkmap = 0;
216#endif
217
218 ccline.overstrike = FALSE; /* always start in insert mode */
219#ifdef FEAT_SEARCH_EXTRA
220 old_cursor = curwin->w_cursor; /* needs to be restored later */
221 old_curswant = curwin->w_curswant;
222 old_leftcol = curwin->w_leftcol;
223 old_topline = curwin->w_topline;
224# ifdef FEAT_DIFF
225 old_topfill = curwin->w_topfill;
226# endif
227 old_botline = curwin->w_botline;
228#endif
229
230 /*
231 * set some variables for redrawcmd()
232 */
233 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000234 ccline.cmdindent = (firstc > 0 ? indent : 0);
235
236 /* alloc initial ccline.cmdbuff */
237 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 if (ccline.cmdbuff == NULL)
239 return NULL; /* out of memory */
240 ccline.cmdlen = ccline.cmdpos = 0;
241 ccline.cmdbuff[0] = NUL;
242
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000243 /* autoindent for :insert and :append */
244 if (firstc <= 0)
245 {
246 copy_spaces(ccline.cmdbuff, indent);
247 ccline.cmdbuff[indent] = NUL;
248 ccline.cmdpos = indent;
249 ccline.cmdspos = indent;
250 ccline.cmdlen = indent;
251 }
252
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000254 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256#ifdef FEAT_RIGHTLEFT
257 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
258 && (firstc == '/' || firstc == '?'))
259 cmdmsg_rl = TRUE;
260 else
261 cmdmsg_rl = FALSE;
262#endif
263
264 redir_off = TRUE; /* don't redirect the typed command */
265 if (!cmd_silent)
266 {
267 i = msg_scrolled;
268 msg_scrolled = 0; /* avoid wait_return message */
269 gotocmdline(TRUE);
270 msg_scrolled += i;
271 redrawcmdprompt(); /* draw prompt or indent */
272 set_cmdspos();
273 }
274 xpc.xp_context = EXPAND_NOTHING;
275 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000276#ifndef BACKSLASH_IN_FILENAME
277 xpc.xp_shell = FALSE;
278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000280#if defined(FEAT_EVAL)
281 if (ccline.input_fn)
282 {
283 xpc.xp_context = ccline.xp_context;
284 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000285# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000286 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000287# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000288 }
289#endif
290
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 /*
292 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
293 * doing ":@0" when register 0 doesn't contain a CR.
294 */
295 msg_scroll = FALSE;
296
297 State = CMDLINE;
298
299 if (firstc == '/' || firstc == '?' || firstc == '@')
300 {
301 /* Use ":lmap" mappings for search pattern and input(). */
302 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
303 b_im_ptr = &curbuf->b_p_iminsert;
304 else
305 b_im_ptr = &curbuf->b_p_imsearch;
306 if (*b_im_ptr == B_IMODE_LMAP)
307 State |= LANGMAP;
308#ifdef USE_IM_CONTROL
309 im_set_active(*b_im_ptr == B_IMODE_IM);
310#endif
311 }
312#ifdef USE_IM_CONTROL
313 else if (p_imcmdline)
314 im_set_active(TRUE);
315#endif
316
317#ifdef FEAT_MOUSE
318 setmouse();
319#endif
320#ifdef CURSOR_SHAPE
321 ui_cursor_shape(); /* may show different cursor shape */
322#endif
323
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000324 /* When inside an autocommand for writing "exiting" may be set and
325 * terminal mode set to cooked. Need to set raw mode here then. */
326 settmode(TMODE_RAW);
327
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_CMDHIST
329 init_history();
330 hiscnt = hislen; /* set hiscnt to impossible history value */
331 histype = hist_char2type(firstc);
332#endif
333
334#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000335 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336#endif
337
338 /*
339 * Collect the command string, handling editing keys.
340 */
341 for (;;)
342 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000343 redir_off = TRUE; /* Don't redirect the typed command.
344 Repeated, because a ":redir" inside
345 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#ifdef USE_ON_FLY_SCROLL
347 dont_scroll = FALSE; /* allow scrolling here */
348#endif
349 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
350
351 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000352
353 /* Get a character. Ignore K_IGNORE, it should not do anything, such
354 * as stop completion. */
355 do
356 {
357 c = safe_vgetc();
358 } while (c == K_IGNORE);
359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 if (KeyTyped)
361 {
362 some_key_typed = TRUE;
363#ifdef FEAT_RIGHTLEFT
364 if (cmd_hkmap)
365 c = hkmap(c);
366# ifdef FEAT_FKMAP
367 if (cmd_fkmap)
368 c = cmdl_fkmap(c);
369# endif
370 if (cmdmsg_rl && !KeyStuffed)
371 {
372 /* Invert horizontal movements and operations. Only when
373 * typed by the user directly, not when the result of a
374 * mapping. */
375 switch (c)
376 {
377 case K_RIGHT: c = K_LEFT; break;
378 case K_S_RIGHT: c = K_S_LEFT; break;
379 case K_C_RIGHT: c = K_C_LEFT; break;
380 case K_LEFT: c = K_RIGHT; break;
381 case K_S_LEFT: c = K_S_RIGHT; break;
382 case K_C_LEFT: c = K_C_RIGHT; break;
383 }
384 }
385#endif
386 }
387
388 /*
389 * Ignore got_int when CTRL-C was typed here.
390 * Don't ignore it in :global, we really need to break then, e.g., for
391 * ":g/pat/normal /pat" (without the <CR>).
392 * Don't ignore it for the input() function.
393 */
394 if ((c == Ctrl_C
395#ifdef UNIX
396 || c == intr_char
397#endif
398 )
399#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
400 && firstc != '@'
401#endif
402#ifdef FEAT_EVAL
403 && !break_ctrl_c
404#endif
405 && !global_busy)
406 got_int = FALSE;
407
408#ifdef FEAT_CMDHIST
409 /* free old command line when finished moving around in the history
410 * list */
411 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000412 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000413 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 && c != K_PAGEDOWN && c != K_PAGEUP
415 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000416 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
418 {
419 vim_free(lookfor);
420 lookfor = NULL;
421 }
422#endif
423
424 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000425 * When there are matching completions to select <S-Tab> works like
426 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000428 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 c = Ctrl_P;
430
431#ifdef FEAT_WILDMENU
432 /* Special translations for 'wildmenu' */
433 if (did_wild_list && p_wmnu)
434 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000435 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000437 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 c = Ctrl_N;
439 }
440 /* Hitting CR after "emenu Name.": complete submenu */
441 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
442 && ccline.cmdpos > 1
443 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
444 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
445 && (c == '\n' || c == '\r' || c == K_KENTER))
446 c = K_DOWN;
447#endif
448
449 /* free expanded names when finished walking through matches */
450 if (xpc.xp_numfiles != -1
451 && !(c == p_wc && KeyTyped) && c != p_wcm
452 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
453 && c != Ctrl_L)
454 {
455 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
456 did_wild_list = FALSE;
457#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000458 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459#endif
460 xpc.xp_context = EXPAND_NOTHING;
461 wim_index = 0;
462#ifdef FEAT_WILDMENU
463 if (p_wmnu && wild_menu_showing != 0)
464 {
465 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000466 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000467
468 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000469 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
471 if (wild_menu_showing == WM_SCROLLED)
472 {
473 /* Entered command line, move it up */
474 cmdline_row--;
475 redrawcmd();
476 }
477 else if (save_p_ls != -1)
478 {
479 /* restore 'laststatus' and 'winminheight' */
480 p_ls = save_p_ls;
481 p_wmh = save_p_wmh;
482 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000483 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000485 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 redrawcmd();
487 save_p_ls = -1;
488 }
489 else
490 {
491# ifdef FEAT_VERTSPLIT
492 win_redraw_last_status(topframe);
493# else
494 lastwin->w_redr_status = TRUE;
495# endif
496 redraw_statuslines();
497 }
498 KeyTyped = skt;
499 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000500 if (ccline.input_fn)
501 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503#endif
504 }
505
506#ifdef FEAT_WILDMENU
507 /* Special translations for 'wildmenu' */
508 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
509 {
510 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000511 if (c == K_DOWN && ccline.cmdpos > 0
512 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000514 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {
516 /* Hitting <Up>: Remove one submenu name in front of the
517 * cursor */
518 int found = FALSE;
519
520 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
521 i = 0;
522 while (--j > 0)
523 {
524 /* check for start of menu name */
525 if (ccline.cmdbuff[j] == ' '
526 && ccline.cmdbuff[j - 1] != '\\')
527 {
528 i = j + 1;
529 break;
530 }
531 /* check for start of submenu name */
532 if (ccline.cmdbuff[j] == '.'
533 && ccline.cmdbuff[j - 1] != '\\')
534 {
535 if (found)
536 {
537 i = j + 1;
538 break;
539 }
540 else
541 found = TRUE;
542 }
543 }
544 if (i > 0)
545 cmdline_del(i);
546 c = p_wc;
547 xpc.xp_context = EXPAND_NOTHING;
548 }
549 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000550 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000551 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000552 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 {
554 char_u upseg[5];
555
556 upseg[0] = PATHSEP;
557 upseg[1] = '.';
558 upseg[2] = '.';
559 upseg[3] = PATHSEP;
560 upseg[4] = NUL;
561
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000562 if (c == K_DOWN
563 && ccline.cmdpos > 0
564 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
565 && (ccline.cmdpos < 3
566 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
568 {
569 /* go down a directory */
570 c = p_wc;
571 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000572 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
574 /* If in a direct ancestor, strip off one ../ to go down */
575 int found = FALSE;
576
577 j = ccline.cmdpos;
578 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
579 while (--j > i)
580 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000581#ifdef FEAT_MBYTE
582 if (has_mbyte)
583 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 if (vim_ispathsep(ccline.cmdbuff[j]))
586 {
587 found = TRUE;
588 break;
589 }
590 }
591 if (found
592 && ccline.cmdbuff[j - 1] == '.'
593 && ccline.cmdbuff[j - 2] == '.'
594 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
595 {
596 cmdline_del(j - 2);
597 c = p_wc;
598 }
599 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000600 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 {
602 /* go up a directory */
603 int found = FALSE;
604
605 j = ccline.cmdpos - 1;
606 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
607 while (--j > i)
608 {
609#ifdef FEAT_MBYTE
610 if (has_mbyte)
611 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
612#endif
613 if (vim_ispathsep(ccline.cmdbuff[j])
614#ifdef BACKSLASH_IN_FILENAME
615 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
616 == NULL
617#endif
618 )
619 {
620 if (found)
621 {
622 i = j + 1;
623 break;
624 }
625 else
626 found = TRUE;
627 }
628 }
629
630 if (!found)
631 j = i;
632 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
633 j += 4;
634 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
635 && j == i)
636 j += 3;
637 else
638 j = 0;
639 if (j > 0)
640 {
641 /* TODO this is only for DOS/UNIX systems - need to put in
642 * machine-specific stuff here and in upseg init */
643 cmdline_del(j);
644 put_on_cmdline(upseg + 1, 3, FALSE);
645 }
646 else if (ccline.cmdpos > i)
647 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100648
649 /* Now complete in the new directory. Set KeyTyped in case the
650 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100652 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
656#endif /* FEAT_WILDMENU */
657
658 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
659 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
660 if (c == Ctrl_BSL)
661 {
662 ++no_mapping;
663 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000664 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 --no_mapping;
666 --allow_keys;
667 /* CTRL-\ e doesn't work when obtaining an expression. */
668 if (c != Ctrl_N && c != Ctrl_G
669 && (c != 'e' || ccline.cmdfirstc == '='))
670 {
671 vungetc(c);
672 c = Ctrl_BSL;
673 }
674#ifdef FEAT_EVAL
675 else if (c == 'e')
676 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200677 char_u *p = NULL;
678 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 /*
681 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000682 * Need to save and restore the current command line, to be
683 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 */
685 if (ccline.cmdpos == ccline.cmdlen)
686 new_cmdpos = 99999; /* keep it at the end */
687 else
688 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000689
690 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000692 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 if (c == '=')
694 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000695 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000696 * to avoid nasty things like going to another buffer when
697 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000698 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000699 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000701 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000702 restore_cmdline(&save_ccline);
703
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200704 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200706 len = (int)STRLEN(p);
707 if (realloc_cmdbuff(len + 1) == OK)
708 {
709 ccline.cmdlen = len;
710 STRCPY(ccline.cmdbuff, p);
711 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200713 /* Restore the cursor or use the position set with
714 * set_cmdline_pos(). */
715 if (new_cmdpos > ccline.cmdlen)
716 ccline.cmdpos = ccline.cmdlen;
717 else
718 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200720 KeyTyped = FALSE; /* Don't do p_wc completion. */
721 redrawcmd();
722 goto cmdline_changed;
723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 }
725 }
726 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100727 got_int = FALSE; /* don't abandon the command line */
728 did_emsg = FALSE;
729 emsg_on_display = FALSE;
730 redrawcmd();
731 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 }
733#endif
734 else
735 {
736 if (c == Ctrl_G && p_im && restart_edit == 0)
737 restart_edit = 'a';
738 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
739 in history */
740 goto returncmd; /* back to Normal mode */
741 }
742 }
743
744#ifdef FEAT_CMDWIN
745 if (c == cedit_key || c == K_CMDWIN)
746 {
747 /*
748 * Open a window to edit the command line (and history).
749 */
750 c = ex_window();
751 some_key_typed = TRUE;
752 }
753# ifdef FEAT_DIGRAPHS
754 else
755# endif
756#endif
757#ifdef FEAT_DIGRAPHS
758 c = do_digraph(c);
759#endif
760
761 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
762 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
763 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000764 /* In Ex mode a backslash escapes a newline. */
765 if (exmode_active
766 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000767 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000768 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000769 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000771 if (c == K_KENTER)
772 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000774 else
775 {
776 gotesc = FALSE; /* Might have typed ESC previously, don't
777 truncate the cmdline now. */
778 if (ccheck_abbr(c + ABBR_OFF))
779 goto cmdline_changed;
780 if (!cmd_silent)
781 {
782 windgoto(msg_row, 0);
783 out_flush();
784 }
785 break;
786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
788
789 /*
790 * Completion for 'wildchar' or 'wildcharm' key.
791 * - hitting <ESC> twice means: abandon command line.
792 * - wildcard expansion is only done when the 'wildchar' key is really
793 * typed, not when it comes from a macro
794 */
795 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
796 {
797 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
798 {
799 /* if 'wildmode' contains "list" may still need to list */
800 if (xpc.xp_numfiles > 1
801 && !did_wild_list
802 && (wim_flags[wim_index] & WIM_LIST))
803 {
804 (void)showmatches(&xpc, FALSE);
805 redrawcmd();
806 did_wild_list = TRUE;
807 }
808 if (wim_flags[wim_index] & WIM_LONGEST)
809 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
810 else if (wim_flags[wim_index] & WIM_FULL)
811 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
812 else
813 res = OK; /* don't insert 'wildchar' now */
814 }
815 else /* typed p_wc first time */
816 {
817 wim_index = 0;
818 j = ccline.cmdpos;
819 /* if 'wildmode' first contains "longest", get longest
820 * common part */
821 if (wim_flags[0] & WIM_LONGEST)
822 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
823 else
824 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
825
826 /* if interrupted while completing, behave like it failed */
827 if (got_int)
828 {
829 (void)vpeekc(); /* remove <C-C> from input stream */
830 got_int = FALSE; /* don't abandon the command line */
831 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
832#ifdef FEAT_WILDMENU
833 xpc.xp_context = EXPAND_NOTHING;
834#endif
835 goto cmdline_changed;
836 }
837
838 /* when more than one match, and 'wildmode' first contains
839 * "list", or no change and 'wildmode' contains "longest,list",
840 * list all matches */
841 if (res == OK && xpc.xp_numfiles > 1)
842 {
843 /* a "longest" that didn't do anything is skipped (but not
844 * "list:longest") */
845 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
846 wim_index = 1;
847 if ((wim_flags[wim_index] & WIM_LIST)
848#ifdef FEAT_WILDMENU
849 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
850#endif
851 )
852 {
853 if (!(wim_flags[0] & WIM_LONGEST))
854 {
855#ifdef FEAT_WILDMENU
856 int p_wmnu_save = p_wmnu;
857 p_wmnu = 0;
858#endif
859 nextwild(&xpc, WILD_PREV, 0); /* remove match */
860#ifdef FEAT_WILDMENU
861 p_wmnu = p_wmnu_save;
862#endif
863 }
864#ifdef FEAT_WILDMENU
865 (void)showmatches(&xpc, p_wmnu
866 && ((wim_flags[wim_index] & WIM_LIST) == 0));
867#else
868 (void)showmatches(&xpc, FALSE);
869#endif
870 redrawcmd();
871 did_wild_list = TRUE;
872 if (wim_flags[wim_index] & WIM_LONGEST)
873 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
874 else if (wim_flags[wim_index] & WIM_FULL)
875 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
876 }
877 else
878 vim_beep();
879 }
880#ifdef FEAT_WILDMENU
881 else if (xpc.xp_numfiles == -1)
882 xpc.xp_context = EXPAND_NOTHING;
883#endif
884 }
885 if (wim_index < 3)
886 ++wim_index;
887 if (c == ESC)
888 gotesc = TRUE;
889 if (res == OK)
890 goto cmdline_changed;
891 }
892
893 gotesc = FALSE;
894
895 /* <S-Tab> goes to last match, in a clumsy way */
896 if (c == K_S_TAB && KeyTyped)
897 {
898 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
899 && nextwild(&xpc, WILD_PREV, 0) == OK
900 && nextwild(&xpc, WILD_PREV, 0) == OK)
901 goto cmdline_changed;
902 }
903
904 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
905 c = NL;
906
907 do_abbr = TRUE; /* default: check for abbreviation */
908
909 /*
910 * Big switch for a typed command line character.
911 */
912 switch (c)
913 {
914 case K_BS:
915 case Ctrl_H:
916 case K_DEL:
917 case K_KDEL:
918 case Ctrl_W:
919#ifdef FEAT_FKMAP
920 if (cmd_fkmap && c == K_BS)
921 c = K_DEL;
922#endif
923 if (c == K_KDEL)
924 c = K_DEL;
925
926 /*
927 * delete current character is the same as backspace on next
928 * character, except at end of line
929 */
930 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
931 ++ccline.cmdpos;
932#ifdef FEAT_MBYTE
933 if (has_mbyte && c == K_DEL)
934 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
935 ccline.cmdbuff + ccline.cmdpos);
936#endif
937 if (ccline.cmdpos > 0)
938 {
939 char_u *p;
940
941 j = ccline.cmdpos;
942 p = ccline.cmdbuff + j;
943#ifdef FEAT_MBYTE
944 if (has_mbyte)
945 {
946 p = mb_prevptr(ccline.cmdbuff, p);
947 if (c == Ctrl_W)
948 {
949 while (p > ccline.cmdbuff && vim_isspace(*p))
950 p = mb_prevptr(ccline.cmdbuff, p);
951 i = mb_get_class(p);
952 while (p > ccline.cmdbuff && mb_get_class(p) == i)
953 p = mb_prevptr(ccline.cmdbuff, p);
954 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000955 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957 }
958 else
959#endif
960 if (c == Ctrl_W)
961 {
962 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
963 --p;
964 i = vim_iswordc(p[-1]);
965 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
966 && vim_iswordc(p[-1]) == i)
967 --p;
968 }
969 else
970 --p;
971 ccline.cmdpos = (int)(p - ccline.cmdbuff);
972 ccline.cmdlen -= j - ccline.cmdpos;
973 i = ccline.cmdpos;
974 while (i < ccline.cmdlen)
975 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
976
977 /* Truncate at the end, required for multi-byte chars. */
978 ccline.cmdbuff[ccline.cmdlen] = NUL;
979 redrawcmd();
980 }
981 else if (ccline.cmdlen == 0 && c != Ctrl_W
982 && ccline.cmdprompt == NULL && indent == 0)
983 {
984 /* In ex and debug mode it doesn't make sense to return. */
985 if (exmode_active
986#ifdef FEAT_EVAL
987 || ccline.cmdfirstc == '>'
988#endif
989 )
990 goto cmdline_not_changed;
991
992 vim_free(ccline.cmdbuff); /* no commandline to return */
993 ccline.cmdbuff = NULL;
994 if (!cmd_silent)
995 {
996#ifdef FEAT_RIGHTLEFT
997 if (cmdmsg_rl)
998 msg_col = Columns;
999 else
1000#endif
1001 msg_col = 0;
1002 msg_putchar(' '); /* delete ':' */
1003 }
1004 redraw_cmdline = TRUE;
1005 goto returncmd; /* back to cmd mode */
1006 }
1007 goto cmdline_changed;
1008
1009 case K_INS:
1010 case K_KINS:
1011#ifdef FEAT_FKMAP
1012 /* if Farsi mode set, we are in reverse insert mode -
1013 Do not change the mode */
1014 if (cmd_fkmap)
1015 beep_flush();
1016 else
1017#endif
1018 ccline.overstrike = !ccline.overstrike;
1019#ifdef CURSOR_SHAPE
1020 ui_cursor_shape(); /* may show different cursor shape */
1021#endif
1022 goto cmdline_not_changed;
1023
1024 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001025 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 {
1027 /* ":lmap" mappings exists, toggle use of mappings. */
1028 State ^= LANGMAP;
1029#ifdef USE_IM_CONTROL
1030 im_set_active(FALSE); /* Disable input method */
1031#endif
1032 if (b_im_ptr != NULL)
1033 {
1034 if (State & LANGMAP)
1035 *b_im_ptr = B_IMODE_LMAP;
1036 else
1037 *b_im_ptr = B_IMODE_NONE;
1038 }
1039 }
1040#ifdef USE_IM_CONTROL
1041 else
1042 {
1043 /* There are no ":lmap" mappings, toggle IM. When
1044 * 'imdisable' is set don't try getting the status, it's
1045 * always off. */
1046 if ((p_imdisable && b_im_ptr != NULL)
1047 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1048 {
1049 im_set_active(FALSE); /* Disable input method */
1050 if (b_im_ptr != NULL)
1051 *b_im_ptr = B_IMODE_NONE;
1052 }
1053 else
1054 {
1055 im_set_active(TRUE); /* Enable input method */
1056 if (b_im_ptr != NULL)
1057 *b_im_ptr = B_IMODE_IM;
1058 }
1059 }
1060#endif
1061 if (b_im_ptr != NULL)
1062 {
1063 if (b_im_ptr == &curbuf->b_p_iminsert)
1064 set_iminsert_global();
1065 else
1066 set_imsearch_global();
1067 }
1068#ifdef CURSOR_SHAPE
1069 ui_cursor_shape(); /* may show different cursor shape */
1070#endif
1071#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1072 /* Show/unshow value of 'keymap' in status lines later. */
1073 status_redraw_curbuf();
1074#endif
1075 goto cmdline_not_changed;
1076
1077/* case '@': only in very old vi */
1078 case Ctrl_U:
1079 /* delete all characters left of the cursor */
1080 j = ccline.cmdpos;
1081 ccline.cmdlen -= j;
1082 i = ccline.cmdpos = 0;
1083 while (i < ccline.cmdlen)
1084 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1085 /* Truncate at the end, required for multi-byte chars. */
1086 ccline.cmdbuff[ccline.cmdlen] = NUL;
1087 redrawcmd();
1088 goto cmdline_changed;
1089
1090#ifdef FEAT_CLIPBOARD
1091 case Ctrl_Y:
1092 /* Copy the modeless selection, if there is one. */
1093 if (clip_star.state != SELECT_CLEARED)
1094 {
1095 if (clip_star.state == SELECT_DONE)
1096 clip_copy_modeless_selection(TRUE);
1097 goto cmdline_not_changed;
1098 }
1099 break;
1100#endif
1101
1102 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1103 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001104 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001105 * ":normal" runs out of characters. */
1106 if (exmode_active
1107#ifdef FEAT_EX_EXTRA
1108 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1109#endif
1110 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 goto cmdline_not_changed;
1112
1113 gotesc = TRUE; /* will free ccline.cmdbuff after
1114 putting it in history */
1115 goto returncmd; /* back to cmd mode */
1116
1117 case Ctrl_R: /* insert register */
1118#ifdef USE_ON_FLY_SCROLL
1119 dont_scroll = TRUE; /* disallow scrolling here */
1120#endif
1121 putcmdline('"', TRUE);
1122 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001123 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (i == Ctrl_O)
1125 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1126 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001127 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 --no_mapping;
1129#ifdef FEAT_EVAL
1130 /*
1131 * Insert the result of an expression.
1132 * Need to save the current command line, to be able to enter
1133 * a new one...
1134 */
1135 new_cmdpos = -1;
1136 if (c == '=')
1137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1139 {
1140 beep_flush();
1141 c = ESC;
1142 }
1143 else
1144 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001145 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001147 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 }
1149 }
1150#endif
1151 if (c != ESC) /* use ESC to cancel inserting register */
1152 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001153 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001154
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001155#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001156 /* When there was a serious error abort getting the
1157 * command line. */
1158 if (aborting())
1159 {
1160 gotesc = TRUE; /* will free ccline.cmdbuff after
1161 putting it in history */
1162 goto returncmd; /* back to cmd mode */
1163 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 KeyTyped = FALSE; /* Don't do p_wc completion. */
1166#ifdef FEAT_EVAL
1167 if (new_cmdpos >= 0)
1168 {
1169 /* set_cmdline_pos() was used */
1170 if (new_cmdpos > ccline.cmdlen)
1171 ccline.cmdpos = ccline.cmdlen;
1172 else
1173 ccline.cmdpos = new_cmdpos;
1174 }
1175#endif
1176 }
1177 redrawcmd();
1178 goto cmdline_changed;
1179
1180 case Ctrl_D:
1181 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1182 break; /* Use ^D as normal char instead */
1183
1184 redrawcmd();
1185 continue; /* don't do incremental search now */
1186
1187 case K_RIGHT:
1188 case K_S_RIGHT:
1189 case K_C_RIGHT:
1190 do
1191 {
1192 if (ccline.cmdpos >= ccline.cmdlen)
1193 break;
1194 i = cmdline_charsize(ccline.cmdpos);
1195 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1196 break;
1197 ccline.cmdspos += i;
1198#ifdef FEAT_MBYTE
1199 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001200 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 + ccline.cmdpos);
1202 else
1203#endif
1204 ++ccline.cmdpos;
1205 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001206 while ((c == K_S_RIGHT || c == K_C_RIGHT
1207 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1209#ifdef FEAT_MBYTE
1210 if (has_mbyte)
1211 set_cmdspos_cursor();
1212#endif
1213 goto cmdline_not_changed;
1214
1215 case K_LEFT:
1216 case K_S_LEFT:
1217 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001218 if (ccline.cmdpos == 0)
1219 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 do
1221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 --ccline.cmdpos;
1223#ifdef FEAT_MBYTE
1224 if (has_mbyte) /* move to first byte of char */
1225 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1226 ccline.cmdbuff + ccline.cmdpos);
1227#endif
1228 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1229 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001230 while (ccline.cmdpos > 0
1231 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001232 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1234#ifdef FEAT_MBYTE
1235 if (has_mbyte)
1236 set_cmdspos_cursor();
1237#endif
1238 goto cmdline_not_changed;
1239
1240 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001241 /* Ignore mouse event or ex_window() result. */
1242 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243
Bram Moolenaar4770d092006-01-12 23:22:24 +00001244#ifdef FEAT_GUI_W32
1245 /* On Win32 ignore <M-F4>, we get it when closing the window was
1246 * cancelled. */
1247 case K_F4:
1248 if (mod_mask == MOD_MASK_ALT)
1249 {
1250 redrawcmd(); /* somehow the cmdline is cleared */
1251 goto cmdline_not_changed;
1252 }
1253 break;
1254#endif
1255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256#ifdef FEAT_MOUSE
1257 case K_MIDDLEDRAG:
1258 case K_MIDDLERELEASE:
1259 goto cmdline_not_changed; /* Ignore mouse */
1260
1261 case K_MIDDLEMOUSE:
1262# ifdef FEAT_GUI
1263 /* When GUI is active, also paste when 'mouse' is empty */
1264 if (!gui.in_use)
1265# endif
1266 if (!mouse_has(MOUSE_COMMAND))
1267 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001268# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001270 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001272# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001273 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 redrawcmd();
1275 goto cmdline_changed;
1276
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001277# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001279 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 redrawcmd();
1281 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001282# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283
1284 case K_LEFTDRAG:
1285 case K_LEFTRELEASE:
1286 case K_RIGHTDRAG:
1287 case K_RIGHTRELEASE:
1288 /* Ignore drag and release events when the button-down wasn't
1289 * seen before. */
1290 if (ignore_drag_release)
1291 goto cmdline_not_changed;
1292 /* FALLTHROUGH */
1293 case K_LEFTMOUSE:
1294 case K_RIGHTMOUSE:
1295 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1296 ignore_drag_release = TRUE;
1297 else
1298 ignore_drag_release = FALSE;
1299# ifdef FEAT_GUI
1300 /* When GUI is active, also move when 'mouse' is empty */
1301 if (!gui.in_use)
1302# endif
1303 if (!mouse_has(MOUSE_COMMAND))
1304 goto cmdline_not_changed; /* Ignore mouse */
1305# ifdef FEAT_CLIPBOARD
1306 if (mouse_row < cmdline_row && clip_star.available)
1307 {
1308 int button, is_click, is_drag;
1309
1310 /*
1311 * Handle modeless selection.
1312 */
1313 button = get_mouse_button(KEY2TERMCAP1(c),
1314 &is_click, &is_drag);
1315 if (mouse_model_popup() && button == MOUSE_LEFT
1316 && (mod_mask & MOD_MASK_SHIFT))
1317 {
1318 /* Translate shift-left to right button. */
1319 button = MOUSE_RIGHT;
1320 mod_mask &= ~MOD_MASK_SHIFT;
1321 }
1322 clip_modeless(button, is_click, is_drag);
1323 goto cmdline_not_changed;
1324 }
1325# endif
1326
1327 set_cmdspos();
1328 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1329 ++ccline.cmdpos)
1330 {
1331 i = cmdline_charsize(ccline.cmdpos);
1332 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1333 && mouse_col < ccline.cmdspos % Columns + i)
1334 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001335# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 if (has_mbyte)
1337 {
1338 /* Count ">" for double-wide char that doesn't fit. */
1339 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001340 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 + ccline.cmdpos) - 1;
1342 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 ccline.cmdspos += i;
1345 }
1346 goto cmdline_not_changed;
1347
1348 /* Mouse scroll wheel: ignored here */
1349 case K_MOUSEDOWN:
1350 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001351 case K_MOUSELEFT:
1352 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 /* Alternate buttons ignored here */
1354 case K_X1MOUSE:
1355 case K_X1DRAG:
1356 case K_X1RELEASE:
1357 case K_X2MOUSE:
1358 case K_X2DRAG:
1359 case K_X2RELEASE:
1360 goto cmdline_not_changed;
1361
1362#endif /* FEAT_MOUSE */
1363
1364#ifdef FEAT_GUI
1365 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1366 case K_LEFTRELEASE_NM:
1367 goto cmdline_not_changed;
1368
1369 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001370 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 {
1372 gui_do_scroll();
1373 redrawcmd();
1374 }
1375 goto cmdline_not_changed;
1376
1377 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001378 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001380 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 redrawcmd();
1382 }
1383 goto cmdline_not_changed;
1384#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001385#ifdef FEAT_GUI_TABLINE
1386 case K_TABLINE:
1387 case K_TABMENU:
1388 /* Don't want to change any tabs here. Make sure the same tab
1389 * is still selected. */
1390 if (gui_use_tabline())
1391 gui_mch_set_curtab(tabpage_index(curtab));
1392 goto cmdline_not_changed;
1393#endif
1394
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 case K_SELECT: /* end of Select mode mapping - ignore */
1396 goto cmdline_not_changed;
1397
1398 case Ctrl_B: /* begin of command line */
1399 case K_HOME:
1400 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 case K_S_HOME:
1402 case K_C_HOME:
1403 ccline.cmdpos = 0;
1404 set_cmdspos();
1405 goto cmdline_not_changed;
1406
1407 case Ctrl_E: /* end of command line */
1408 case K_END:
1409 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 case K_S_END:
1411 case K_C_END:
1412 ccline.cmdpos = ccline.cmdlen;
1413 set_cmdspos_cursor();
1414 goto cmdline_not_changed;
1415
1416 case Ctrl_A: /* all matches */
1417 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1418 break;
1419 goto cmdline_changed;
1420
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001421 case Ctrl_L:
1422#ifdef FEAT_SEARCH_EXTRA
1423 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1424 {
1425 /* Add a character from under the cursor for 'incsearch' */
1426 if (did_incsearch
1427 && !equalpos(curwin->w_cursor, old_cursor))
1428 {
1429 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001430 /* If 'ignorecase' and 'smartcase' are set and the
1431 * command line has no uppercase characters, convert
1432 * the character to lowercase */
1433 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1434 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001435 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001436 {
1437 if (c == firstc || vim_strchr((char_u *)(
1438 p_magic ? "\\^$.*[" : "\\^$"), c)
1439 != NULL)
1440 {
1441 /* put a backslash before special characters */
1442 stuffcharReadbuff(c);
1443 c = '\\';
1444 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001445 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001446 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001447 }
1448 goto cmdline_not_changed;
1449 }
1450#endif
1451
1452 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1454 break;
1455 goto cmdline_changed;
1456
1457 case Ctrl_N: /* next match */
1458 case Ctrl_P: /* previous match */
1459 if (xpc.xp_numfiles > 0)
1460 {
1461 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1462 == FAIL)
1463 break;
1464 goto cmdline_changed;
1465 }
1466
1467#ifdef FEAT_CMDHIST
1468 case K_UP:
1469 case K_DOWN:
1470 case K_S_UP:
1471 case K_S_DOWN:
1472 case K_PAGEUP:
1473 case K_KPAGEUP:
1474 case K_PAGEDOWN:
1475 case K_KPAGEDOWN:
1476 if (hislen == 0 || firstc == NUL) /* no history */
1477 goto cmdline_not_changed;
1478
1479 i = hiscnt;
1480
1481 /* save current command string so it can be restored later */
1482 if (lookfor == NULL)
1483 {
1484 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1485 goto cmdline_not_changed;
1486 lookfor[ccline.cmdpos] = NUL;
1487 }
1488
1489 j = (int)STRLEN(lookfor);
1490 for (;;)
1491 {
1492 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001493 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001494 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
1496 if (hiscnt == hislen) /* first time */
1497 hiscnt = hisidx[histype];
1498 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1499 hiscnt = hislen - 1;
1500 else if (hiscnt != hisidx[histype] + 1)
1501 --hiscnt;
1502 else /* at top of list */
1503 {
1504 hiscnt = i;
1505 break;
1506 }
1507 }
1508 else /* one step forwards */
1509 {
1510 /* on last entry, clear the line */
1511 if (hiscnt == hisidx[histype])
1512 {
1513 hiscnt = hislen;
1514 break;
1515 }
1516
1517 /* not on a history line, nothing to do */
1518 if (hiscnt == hislen)
1519 break;
1520 if (hiscnt == hislen - 1) /* wrap around */
1521 hiscnt = 0;
1522 else
1523 ++hiscnt;
1524 }
1525 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1526 {
1527 hiscnt = i;
1528 break;
1529 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001530 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001531 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 || STRNCMP(history[histype][hiscnt].hisstr,
1533 lookfor, (size_t)j) == 0)
1534 break;
1535 }
1536
1537 if (hiscnt != i) /* jumped to other entry */
1538 {
1539 char_u *p;
1540 int len;
1541 int old_firstc;
1542
1543 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001544 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 if (hiscnt == hislen)
1546 p = lookfor; /* back to the old one */
1547 else
1548 p = history[histype][hiscnt].hisstr;
1549
1550 if (histype == HIST_SEARCH
1551 && p != lookfor
1552 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1553 {
1554 /* Correct for the separator character used when
1555 * adding the history entry vs the one used now.
1556 * First loop: count length.
1557 * Second loop: copy the characters. */
1558 for (i = 0; i <= 1; ++i)
1559 {
1560 len = 0;
1561 for (j = 0; p[j] != NUL; ++j)
1562 {
1563 /* Replace old sep with new sep, unless it is
1564 * escaped. */
1565 if (p[j] == old_firstc
1566 && (j == 0 || p[j - 1] != '\\'))
1567 {
1568 if (i > 0)
1569 ccline.cmdbuff[len] = firstc;
1570 }
1571 else
1572 {
1573 /* Escape new sep, unless it is already
1574 * escaped. */
1575 if (p[j] == firstc
1576 && (j == 0 || p[j - 1] != '\\'))
1577 {
1578 if (i > 0)
1579 ccline.cmdbuff[len] = '\\';
1580 ++len;
1581 }
1582 if (i > 0)
1583 ccline.cmdbuff[len] = p[j];
1584 }
1585 ++len;
1586 }
1587 if (i == 0)
1588 {
1589 alloc_cmdbuff(len);
1590 if (ccline.cmdbuff == NULL)
1591 goto returncmd;
1592 }
1593 }
1594 ccline.cmdbuff[len] = NUL;
1595 }
1596 else
1597 {
1598 alloc_cmdbuff((int)STRLEN(p));
1599 if (ccline.cmdbuff == NULL)
1600 goto returncmd;
1601 STRCPY(ccline.cmdbuff, p);
1602 }
1603
1604 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1605 redrawcmd();
1606 goto cmdline_changed;
1607 }
1608 beep_flush();
1609 goto cmdline_not_changed;
1610#endif
1611
1612 case Ctrl_V:
1613 case Ctrl_Q:
1614#ifdef FEAT_MOUSE
1615 ignore_drag_release = TRUE;
1616#endif
1617 putcmdline('^', TRUE);
1618 c = get_literal(); /* get next (two) character(s) */
1619 do_abbr = FALSE; /* don't do abbreviation now */
1620#ifdef FEAT_MBYTE
1621 /* may need to remove ^ when composing char was typed */
1622 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1623 {
1624 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1625 msg_putchar(' ');
1626 cursorcmd();
1627 }
1628#endif
1629 break;
1630
1631#ifdef FEAT_DIGRAPHS
1632 case Ctrl_K:
1633#ifdef FEAT_MOUSE
1634 ignore_drag_release = TRUE;
1635#endif
1636 putcmdline('?', TRUE);
1637#ifdef USE_ON_FLY_SCROLL
1638 dont_scroll = TRUE; /* disallow scrolling here */
1639#endif
1640 c = get_digraph(TRUE);
1641 if (c != NUL)
1642 break;
1643
1644 redrawcmd();
1645 goto cmdline_not_changed;
1646#endif /* FEAT_DIGRAPHS */
1647
1648#ifdef FEAT_RIGHTLEFT
1649 case Ctrl__: /* CTRL-_: switch language mode */
1650 if (!p_ari)
1651 break;
1652#ifdef FEAT_FKMAP
1653 if (p_altkeymap)
1654 {
1655 cmd_fkmap = !cmd_fkmap;
1656 if (cmd_fkmap) /* in Farsi always in Insert mode */
1657 ccline.overstrike = FALSE;
1658 }
1659 else /* Hebrew is default */
1660#endif
1661 cmd_hkmap = !cmd_hkmap;
1662 goto cmdline_not_changed;
1663#endif
1664
1665 default:
1666#ifdef UNIX
1667 if (c == intr_char)
1668 {
1669 gotesc = TRUE; /* will free ccline.cmdbuff after
1670 putting it in history */
1671 goto returncmd; /* back to Normal mode */
1672 }
1673#endif
1674 /*
1675 * Normal character with no special meaning. Just set mod_mask
1676 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1677 * the string <S-Space>. This should only happen after ^V.
1678 */
1679 if (!IS_SPECIAL(c))
1680 mod_mask = 0x0;
1681 break;
1682 }
1683 /*
1684 * End of switch on command line character.
1685 * We come here if we have a normal character.
1686 */
1687
1688 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1689#ifdef FEAT_MBYTE
1690 /* Add ABBR_OFF for characters above 0x100, this is
1691 * what check_abbr() expects. */
1692 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1693#endif
1694 c))
1695 goto cmdline_changed;
1696
1697 /*
1698 * put the character in the command line
1699 */
1700 if (IS_SPECIAL(c) || mod_mask != 0)
1701 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1702 else
1703 {
1704#ifdef FEAT_MBYTE
1705 if (has_mbyte)
1706 {
1707 j = (*mb_char2bytes)(c, IObuff);
1708 IObuff[j] = NUL; /* exclude composing chars */
1709 put_on_cmdline(IObuff, j, TRUE);
1710 }
1711 else
1712#endif
1713 {
1714 IObuff[0] = c;
1715 put_on_cmdline(IObuff, 1, TRUE);
1716 }
1717 }
1718 goto cmdline_changed;
1719
1720/*
1721 * This part implements incremental searches for "/" and "?"
1722 * Jump to cmdline_not_changed when a character has been read but the command
1723 * line did not change. Then we only search and redraw if something changed in
1724 * the past.
1725 * Jump to cmdline_changed when the command line did change.
1726 * (Sorry for the goto's, I know it is ugly).
1727 */
1728cmdline_not_changed:
1729#ifdef FEAT_SEARCH_EXTRA
1730 if (!incsearch_postponed)
1731 continue;
1732#endif
1733
1734cmdline_changed:
1735#ifdef FEAT_SEARCH_EXTRA
1736 /*
1737 * 'incsearch' highlighting.
1738 */
1739 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1740 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001741 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001742#ifdef FEAT_RELTIME
1743 proftime_T tm;
1744#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001745
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 /* if there is a character waiting, search and redraw later */
1747 if (char_avail())
1748 {
1749 incsearch_postponed = TRUE;
1750 continue;
1751 }
1752 incsearch_postponed = FALSE;
1753 curwin->w_cursor = old_cursor; /* start at old position */
1754
1755 /* If there is no command line, don't do anything */
1756 if (ccline.cmdlen == 0)
1757 i = 0;
1758 else
1759 {
1760 cursor_off(); /* so the user knows we're busy */
1761 out_flush();
1762 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001763#ifdef FEAT_RELTIME
1764 /* Set the time limit to half a second. */
1765 profile_setlimit(500L, &tm);
1766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001768 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1769#ifdef FEAT_RELTIME
1770 &tm
1771#else
1772 NULL
1773#endif
1774 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 --emsg_off;
1776 /* if interrupted while searching, behave like it failed */
1777 if (got_int)
1778 {
1779 (void)vpeekc(); /* remove <C-C> from input stream */
1780 got_int = FALSE; /* don't abandon the command line */
1781 i = 0;
1782 }
1783 else if (char_avail())
1784 /* cancelled searching because a char was typed */
1785 incsearch_postponed = TRUE;
1786 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001787 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 highlight_match = TRUE; /* highlight position */
1789 else
1790 highlight_match = FALSE; /* remove highlight */
1791
1792 /* first restore the old curwin values, so the screen is
1793 * positioned in the same way as the actual search command */
1794 curwin->w_leftcol = old_leftcol;
1795 curwin->w_topline = old_topline;
1796# ifdef FEAT_DIFF
1797 curwin->w_topfill = old_topfill;
1798# endif
1799 curwin->w_botline = old_botline;
1800 changed_cline_bef_curs();
1801 update_topline();
1802
1803 if (i != 0)
1804 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001805 pos_T save_pos = curwin->w_cursor;
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001808 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 * moves the whole match onto the screen when 'nowrap' is set.
1810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 curwin->w_cursor.lnum += search_match_lines;
1812 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001813 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1814 {
1815 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1816 coladvance((colnr_T)MAXCOL);
1817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001819 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001820 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001822 else
1823 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001824
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001826# ifdef FEAT_WINDOWS
1827 /* May redraw the status line to show the cursor position. */
1828 if (p_ru && curwin->w_status_height > 0)
1829 curwin->w_redr_status = TRUE;
1830# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001832 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001833 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001834 restore_cmdline(&save_ccline);
1835
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001836 /* Leave it at the end to make CTRL-R CTRL-W work. */
1837 if (i != 0)
1838 curwin->w_cursor = end_pos;
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 msg_starthere();
1841 redrawcmdline();
1842 did_incsearch = TRUE;
1843 }
1844#else /* FEAT_SEARCH_EXTRA */
1845 ;
1846#endif
1847
1848#ifdef FEAT_RIGHTLEFT
1849 if (cmdmsg_rl
1850# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001851 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852# endif
1853 )
1854 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001855 * right-left typing. Not efficient, but it works.
1856 * Do it only when there are no characters left to read
1857 * to avoid useless intermediate redraws. */
1858 if (vpeekc() == NUL)
1859 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860#endif
1861 }
1862
1863returncmd:
1864
1865#ifdef FEAT_RIGHTLEFT
1866 cmdmsg_rl = FALSE;
1867#endif
1868
1869#ifdef FEAT_FKMAP
1870 cmd_fkmap = 0;
1871#endif
1872
1873 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001874 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875
1876#ifdef FEAT_SEARCH_EXTRA
1877 if (did_incsearch)
1878 {
1879 curwin->w_cursor = old_cursor;
1880 curwin->w_curswant = old_curswant;
1881 curwin->w_leftcol = old_leftcol;
1882 curwin->w_topline = old_topline;
1883# ifdef FEAT_DIFF
1884 curwin->w_topfill = old_topfill;
1885# endif
1886 curwin->w_botline = old_botline;
1887 highlight_match = FALSE;
1888 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001889 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 }
1891#endif
1892
1893 if (ccline.cmdbuff != NULL)
1894 {
1895 /*
1896 * Put line in history buffer (":" and "=" only when it was typed).
1897 */
1898#ifdef FEAT_CMDHIST
1899 if (ccline.cmdlen && firstc != NUL
1900 && (some_key_typed || histype == HIST_SEARCH))
1901 {
1902 add_to_history(histype, ccline.cmdbuff, TRUE,
1903 histype == HIST_SEARCH ? firstc : NUL);
1904 if (firstc == ':')
1905 {
1906 vim_free(new_last_cmdline);
1907 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1908 }
1909 }
1910#endif
1911
1912 if (gotesc) /* abandon command line */
1913 {
1914 vim_free(ccline.cmdbuff);
1915 ccline.cmdbuff = NULL;
1916 if (msg_scrolled == 0)
1917 compute_cmdrow();
1918 MSG("");
1919 redraw_cmdline = TRUE;
1920 }
1921 }
1922
1923 /*
1924 * If the screen was shifted up, redraw the whole screen (later).
1925 * If the line is too long, clear it, so ruler and shown command do
1926 * not get printed in the middle of it.
1927 */
1928 msg_check();
1929 msg_scroll = save_msg_scroll;
1930 redir_off = FALSE;
1931
1932 /* When the command line was typed, no need for a wait-return prompt. */
1933 if (some_key_typed)
1934 need_wait_return = FALSE;
1935
1936 State = save_State;
1937#ifdef USE_IM_CONTROL
1938 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1939 im_save_status(b_im_ptr);
1940 im_set_active(FALSE);
1941#endif
1942#ifdef FEAT_MOUSE
1943 setmouse();
1944#endif
1945#ifdef CURSOR_SHAPE
1946 ui_cursor_shape(); /* may show different cursor shape */
1947#endif
1948
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001949 {
1950 char_u *p = ccline.cmdbuff;
1951
1952 /* Make ccline empty, getcmdline() may try to use it. */
1953 ccline.cmdbuff = NULL;
1954 return p;
1955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956}
1957
1958#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1959/*
1960 * Get a command line with a prompt.
1961 * This is prepared to be called recursively from getcmdline() (e.g. by
1962 * f_input() when evaluating an expression from CTRL-R =).
1963 * Returns the command line in allocated memory, or NULL.
1964 */
1965 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001966getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 int firstc;
1968 char_u *prompt; /* command line prompt */
1969 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001970 int xp_context; /* type of expansion */
1971 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972{
1973 char_u *s;
1974 struct cmdline_info save_ccline;
1975 int msg_col_save = msg_col;
1976
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001977 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 ccline.cmdprompt = prompt;
1979 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001980# ifdef FEAT_EVAL
1981 ccline.xp_context = xp_context;
1982 ccline.xp_arg = xp_arg;
1983 ccline.input_fn = (firstc == '@');
1984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001986 restore_cmdline(&save_ccline);
Bram Moolenaar1db1f772011-08-17 16:25:48 +02001987 /* Restore msg_col, the prompt from input() may have changed it.
1988 * But only if called recursively and the commandline is therefore being
1989 * restored to an old one; if not, the input() prompt stays on the screen,
1990 * so we need its modified msg_col left intact. */
1991 if (ccline.cmdbuff != NULL)
1992 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
1994 return s;
1995}
1996#endif
1997
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001998/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001999 * Return TRUE when the text must not be changed and we can't switch to
2000 * another window or buffer. Used when editing the command line, evaluating
2001 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002002 */
2003 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002004text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002005{
2006#ifdef FEAT_CMDWIN
2007 if (cmdwin_type != 0)
2008 return TRUE;
2009#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002010 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002011}
2012
2013/*
2014 * Give an error message for a command that isn't allowed while the cmdline
2015 * window is open or editing the cmdline in another way.
2016 */
2017 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002018text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002019{
2020#ifdef FEAT_CMDWIN
2021 if (cmdwin_type != 0)
2022 EMSG(_(e_cmdwin));
2023 else
2024#endif
2025 EMSG(_(e_secure));
2026}
2027
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002028#if defined(FEAT_AUTOCMD) || defined(PROTO)
2029/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002030 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2031 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002032 */
2033 int
2034curbuf_locked()
2035{
2036 if (curbuf_lock > 0)
2037 {
2038 EMSG(_("E788: Not allowed to edit another buffer now"));
2039 return TRUE;
2040 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002041 return allbuf_locked();
2042}
2043
2044/*
2045 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2046 * message.
2047 */
2048 int
2049allbuf_locked()
2050{
2051 if (allbuf_lock > 0)
2052 {
2053 EMSG(_("E811: Not allowed to change buffer information now"));
2054 return TRUE;
2055 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002056 return FALSE;
2057}
2058#endif
2059
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 static int
2061cmdline_charsize(idx)
2062 int idx;
2063{
2064#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2065 if (cmdline_star > 0) /* showing '*', always 1 position */
2066 return 1;
2067#endif
2068 return ptr2cells(ccline.cmdbuff + idx);
2069}
2070
2071/*
2072 * Compute the offset of the cursor on the command line for the prompt and
2073 * indent.
2074 */
2075 static void
2076set_cmdspos()
2077{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002078 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 ccline.cmdspos = 1 + ccline.cmdindent;
2080 else
2081 ccline.cmdspos = 0 + ccline.cmdindent;
2082}
2083
2084/*
2085 * Compute the screen position for the cursor on the command line.
2086 */
2087 static void
2088set_cmdspos_cursor()
2089{
2090 int i, m, c;
2091
2092 set_cmdspos();
2093 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002096 if (m < 0) /* overflow, Columns or Rows at weird value */
2097 m = MAXCOL;
2098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 else
2100 m = MAXCOL;
2101 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2102 {
2103 c = cmdline_charsize(i);
2104#ifdef FEAT_MBYTE
2105 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2106 if (has_mbyte)
2107 correct_cmdspos(i, c);
2108#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002109 /* If the cmdline doesn't fit, show cursor on last visible char.
2110 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 if ((ccline.cmdspos += c) >= m)
2112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 ccline.cmdspos -= c;
2114 break;
2115 }
2116#ifdef FEAT_MBYTE
2117 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002118 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119#endif
2120 }
2121}
2122
2123#ifdef FEAT_MBYTE
2124/*
2125 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2126 * character that doesn't fit, so that a ">" must be displayed.
2127 */
2128 static void
2129correct_cmdspos(idx, cells)
2130 int idx;
2131 int cells;
2132{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002133 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2135 && ccline.cmdspos % Columns + cells > Columns)
2136 ccline.cmdspos++;
2137}
2138#endif
2139
2140/*
2141 * Get an Ex command line for the ":" command.
2142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002144getexline(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 int c; /* normally ':', NUL for ":append" */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002146 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 int indent; /* indent for inside conditionals */
2148{
2149 /* When executing a register, remove ':' that's in front of each line. */
2150 if (exec_from_reg && vpeekc() == ':')
2151 (void)vgetc();
2152 return getcmdline(c, 1L, indent);
2153}
2154
2155/*
2156 * Get an Ex command line for Ex mode.
2157 * In Ex mode we only use the OS supplied line editing features and no
2158 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002159 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002162getexmodeline(promptc, cookie, indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002163 int promptc; /* normally ':', NUL for ":append" and '?' for
2164 :s prompt */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002165 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 int indent; /* indent for inside conditionals */
2167{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002168 garray_T line_ga;
2169 char_u *pend;
2170 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002171 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002172 int escaped = FALSE; /* CTRL-V typed */
2173 int vcol = 0;
2174 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002175 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
2177 /* Switch cursor on now. This avoids that it happens after the "\n", which
2178 * confuses the system function that computes tabstops. */
2179 cursor_on();
2180
2181 /* always start in column 0; write a newline if necessary */
2182 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002183 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002185 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002187 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002188 if (p_prompt)
2189 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 while (indent-- > 0)
2191 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
2194
2195 ga_init2(&line_ga, 1, 30);
2196
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002197 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002198 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002199 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002200 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002201 while (indent >= 8)
2202 {
2203 ga_append(&line_ga, TAB);
2204 msg_puts((char_u *)" ");
2205 indent -= 8;
2206 }
2207 while (indent-- > 0)
2208 {
2209 ga_append(&line_ga, ' ');
2210 msg_putchar(' ');
2211 }
2212 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002213 ++no_mapping;
2214 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002215
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 /*
2217 * Get the line, one character at a time.
2218 */
2219 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002220 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
2222 if (ga_grow(&line_ga, 40) == FAIL)
2223 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002225 /* Get one character at a time. Don't use inchar(), it can't handle
2226 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002227 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002228 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002231 * Handle line editing.
2232 * Previously this was left to the system, putting the terminal in
2233 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002235 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002237 msg_putchar('\n');
2238 break;
2239 }
2240
2241 if (!escaped)
2242 {
2243 /* CR typed means "enter", which is NL */
2244 if (c1 == '\r')
2245 c1 = '\n';
2246
2247 if (c1 == BS || c1 == K_BS
2248 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002250 if (line_ga.ga_len > 0)
2251 {
2252 --line_ga.ga_len;
2253 goto redraw;
2254 }
2255 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 }
2257
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002258 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002260 msg_col = startcol;
2261 msg_clr_eos();
2262 line_ga.ga_len = 0;
2263 continue;
2264 }
2265
2266 if (c1 == Ctrl_T)
2267 {
2268 p = (char_u *)line_ga.ga_data;
2269 p[line_ga.ga_len] = NUL;
2270 indent = get_indent_str(p, 8);
2271 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2272add_indent:
2273 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002275 char_u *s = skipwhite(p);
2276
2277 ga_grow(&line_ga, 1);
2278 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2279 *s = ' ';
2280 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002282redraw:
2283 /* redraw the line */
2284 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002285 vcol = 0;
2286 for (p = (char_u *)line_ga.ga_data;
2287 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002289 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002291 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002293 msg_putchar(' ');
2294 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002296 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002298 msg_outtrans_len(p, 1);
2299 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002302 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002303 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002304 continue;
2305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002307 if (c1 == Ctrl_D)
2308 {
2309 /* Delete one shiftwidth. */
2310 p = (char_u *)line_ga.ga_data;
2311 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002313 if (prev_char == '^')
2314 ex_keep_indent = TRUE;
2315 indent = 0;
2316 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
2318 else
2319 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002320 p[line_ga.ga_len] = NUL;
2321 indent = get_indent_str(p, 8);
2322 --indent;
2323 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002325 while (get_indent_str(p, 8) > indent)
2326 {
2327 char_u *s = skipwhite(p);
2328
2329 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2330 --line_ga.ga_len;
2331 }
2332 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002334
2335 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2336 {
2337 escaped = TRUE;
2338 continue;
2339 }
2340
2341 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2342 if (IS_SPECIAL(c1))
2343 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002345
2346 if (IS_SPECIAL(c1))
2347 c1 = '?';
2348 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002349 if (c1 == '\n')
2350 msg_putchar('\n');
2351 else if (c1 == TAB)
2352 {
2353 /* Don't use chartabsize(), 'ts' can be different */
2354 do
2355 {
2356 msg_putchar(' ');
2357 } while (++vcol % 8);
2358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002361 msg_outtrans_len(
2362 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2363 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002365 ++line_ga.ga_len;
2366 escaped = FALSE;
2367
2368 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002369 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002370
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002371 /* We are done when a NL is entered, but not when it comes after an
2372 * odd number of backslashes, that results in a NUL. */
2373 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002375 int bcount = 0;
2376
2377 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2378 ++bcount;
2379
2380 if (bcount > 0)
2381 {
2382 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2383 * "\NL", etc. */
2384 line_ga.ga_len -= (bcount + 1) / 2;
2385 pend -= (bcount + 1) / 2;
2386 pend[-1] = '\n';
2387 }
2388
2389 if ((bcount & 1) == 0)
2390 {
2391 --line_ga.ga_len;
2392 --pend;
2393 *pend = NUL;
2394 break;
2395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397 }
2398
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002399 --no_mapping;
2400 --allow_keys;
2401
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 /* make following messages go to the next line */
2403 msg_didout = FALSE;
2404 msg_col = 0;
2405 if (msg_row < Rows - 1)
2406 ++msg_row;
2407 emsg_on_display = FALSE; /* don't want ui_delay() */
2408
2409 if (got_int)
2410 ga_clear(&line_ga);
2411
2412 return (char_u *)line_ga.ga_data;
2413}
2414
Bram Moolenaare344bea2005-09-01 20:46:49 +00002415# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2416 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417/*
2418 * Return TRUE if ccline.overstrike is on.
2419 */
2420 int
2421cmdline_overstrike()
2422{
2423 return ccline.overstrike;
2424}
2425
2426/*
2427 * Return TRUE if the cursor is at the end of the cmdline.
2428 */
2429 int
2430cmdline_at_end()
2431{
2432 return (ccline.cmdpos >= ccline.cmdlen);
2433}
2434#endif
2435
Bram Moolenaar9372a112005-12-06 19:59:18 +00002436#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437/*
2438 * Return the virtual column number at the current cursor position.
2439 * This is used by the IM code to obtain the start of the preedit string.
2440 */
2441 colnr_T
2442cmdline_getvcol_cursor()
2443{
2444 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2445 return MAXCOL;
2446
2447# ifdef FEAT_MBYTE
2448 if (has_mbyte)
2449 {
2450 colnr_T col;
2451 int i = 0;
2452
2453 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002454 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455
2456 return col;
2457 }
2458 else
2459# endif
2460 return ccline.cmdpos;
2461}
2462#endif
2463
2464#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2465/*
2466 * If part of the command line is an IM preedit string, redraw it with
2467 * IM feedback attributes. The cursor position is restored after drawing.
2468 */
2469 static void
2470redrawcmd_preedit()
2471{
2472 if ((State & CMDLINE)
2473 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002474 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 && !p_imdisable
2476 && im_is_preediting())
2477 {
2478 int cmdpos = 0;
2479 int cmdspos;
2480 int old_row;
2481 int old_col;
2482 colnr_T col;
2483
2484 old_row = msg_row;
2485 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002486 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
2488# ifdef FEAT_MBYTE
2489 if (has_mbyte)
2490 {
2491 for (col = 0; col < preedit_start_col
2492 && cmdpos < ccline.cmdlen; ++col)
2493 {
2494 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002495 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 }
2497 }
2498 else
2499# endif
2500 {
2501 cmdspos += preedit_start_col;
2502 cmdpos += preedit_start_col;
2503 }
2504
2505 msg_row = cmdline_row + (cmdspos / (int)Columns);
2506 msg_col = cmdspos % (int)Columns;
2507 if (msg_row >= Rows)
2508 msg_row = Rows - 1;
2509
2510 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2511 {
2512 int char_len;
2513 int char_attr;
2514
2515 char_attr = im_get_feedback_attr(col);
2516 if (char_attr < 0)
2517 break; /* end of preedit string */
2518
2519# ifdef FEAT_MBYTE
2520 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002521 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 else
2523# endif
2524 char_len = 1;
2525
2526 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2527 cmdpos += char_len;
2528 }
2529
2530 msg_row = old_row;
2531 msg_col = old_col;
2532 }
2533}
2534#endif /* FEAT_XIM && FEAT_GUI_GTK */
2535
2536/*
2537 * Allocate a new command line buffer.
2538 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2539 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2540 */
2541 static void
2542alloc_cmdbuff(len)
2543 int len;
2544{
2545 /*
2546 * give some extra space to avoid having to allocate all the time
2547 */
2548 if (len < 80)
2549 len = 100;
2550 else
2551 len += 20;
2552
2553 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2554 ccline.cmdbufflen = len;
2555}
2556
2557/*
2558 * Re-allocate the command line to length len + something extra.
2559 * return FAIL for failure, OK otherwise
2560 */
2561 static int
2562realloc_cmdbuff(len)
2563 int len;
2564{
2565 char_u *p;
2566
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002567 if (len < ccline.cmdbufflen)
2568 return OK; /* no need to resize */
2569
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 p = ccline.cmdbuff;
2571 alloc_cmdbuff(len); /* will get some more */
2572 if (ccline.cmdbuff == NULL) /* out of memory */
2573 {
2574 ccline.cmdbuff = p; /* keep the old one */
2575 return FAIL;
2576 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002577 /* There isn't always a NUL after the command, but it may need to be
2578 * there, thus copy up to the NUL and add a NUL. */
2579 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2580 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002582
2583 if (ccline.xpc != NULL
2584 && ccline.xpc->xp_pattern != NULL
2585 && ccline.xpc->xp_context != EXPAND_NOTHING
2586 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2587 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002588 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002589
2590 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2591 * to point into the newly allocated memory. */
2592 if (i >= 0 && i <= ccline.cmdlen)
2593 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2594 }
2595
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 return OK;
2597}
2598
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002599#if defined(FEAT_ARABIC) || defined(PROTO)
2600static char_u *arshape_buf = NULL;
2601
2602# if defined(EXITFREE) || defined(PROTO)
2603 void
2604free_cmdline_buf()
2605{
2606 vim_free(arshape_buf);
2607}
2608# endif
2609#endif
2610
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611/*
2612 * Draw part of the cmdline at the current cursor position. But draw stars
2613 * when cmdline_star is TRUE.
2614 */
2615 static void
2616draw_cmdline(start, len)
2617 int start;
2618 int len;
2619{
2620#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2621 int i;
2622
2623 if (cmdline_star > 0)
2624 for (i = 0; i < len; ++i)
2625 {
2626 msg_putchar('*');
2627# ifdef FEAT_MBYTE
2628 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002629 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630# endif
2631 }
2632 else
2633#endif
2634#ifdef FEAT_ARABIC
2635 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2636 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 static int buflen = 0;
2638 char_u *p;
2639 int j;
2640 int newlen = 0;
2641 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002642 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 int prev_c = 0;
2644 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002645 int u8c;
2646 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
2649 /*
2650 * Do arabic shaping into a temporary buffer. This is very
2651 * inefficient!
2652 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002653 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
2655 /* Re-allocate the buffer. We keep it around to avoid a lot of
2656 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002657 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002658 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002659 arshape_buf = alloc(buflen);
2660 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 return; /* out of memory */
2662 }
2663
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002664 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2665 {
2666 /* Prepend a space to draw the leading composing char on. */
2667 arshape_buf[0] = ' ';
2668 newlen = 1;
2669 }
2670
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 for (j = start; j < start + len; j += mb_l)
2672 {
2673 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002674 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002675 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (ARABIC_CHAR(u8c))
2677 {
2678 /* Do Arabic shaping. */
2679 if (cmdmsg_rl)
2680 {
2681 /* displaying from right to left */
2682 pc = prev_c;
2683 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002684 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 if (j + mb_l >= start + len)
2686 nc = NUL;
2687 else
2688 nc = utf_ptr2char(p + mb_l);
2689 }
2690 else
2691 {
2692 /* displaying from left to right */
2693 if (j + mb_l >= start + len)
2694 pc = NUL;
2695 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002696 {
2697 int pcc[MAX_MCO];
2698
2699 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002701 pc1 = pcc[0];
2702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 nc = prev_c;
2704 }
2705 prev_c = u8c;
2706
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002707 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002709 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002710 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002712 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2713 if (u8cc[1] != 0)
2714 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002715 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
2717 }
2718 else
2719 {
2720 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002721 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 newlen += mb_l;
2723 }
2724 }
2725
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002726 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728 else
2729#endif
2730 msg_outtrans_len(ccline.cmdbuff + start, len);
2731}
2732
2733/*
2734 * Put a character on the command line. Shifts the following text to the
2735 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2736 * "c" must be printable (fit in one display cell)!
2737 */
2738 void
2739putcmdline(c, shift)
2740 int c;
2741 int shift;
2742{
2743 if (cmd_silent)
2744 return;
2745 msg_no_more = TRUE;
2746 msg_putchar(c);
2747 if (shift)
2748 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2749 msg_no_more = FALSE;
2750 cursorcmd();
2751}
2752
2753/*
2754 * Undo a putcmdline(c, FALSE).
2755 */
2756 void
2757unputcmdline()
2758{
2759 if (cmd_silent)
2760 return;
2761 msg_no_more = TRUE;
2762 if (ccline.cmdlen == ccline.cmdpos)
2763 msg_putchar(' ');
2764 else
2765 draw_cmdline(ccline.cmdpos, 1);
2766 msg_no_more = FALSE;
2767 cursorcmd();
2768}
2769
2770/*
2771 * Put the given string, of the given length, onto the command line.
2772 * If len is -1, then STRLEN() is used to calculate the length.
2773 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2774 * part will be redrawn, otherwise it will not. If this function is called
2775 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2776 * called afterwards.
2777 */
2778 int
2779put_on_cmdline(str, len, redraw)
2780 char_u *str;
2781 int len;
2782 int redraw;
2783{
2784 int retval;
2785 int i;
2786 int m;
2787 int c;
2788
2789 if (len < 0)
2790 len = (int)STRLEN(str);
2791
2792 /* Check if ccline.cmdbuff needs to be longer */
2793 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002794 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 else
2796 retval = OK;
2797 if (retval == OK)
2798 {
2799 if (!ccline.overstrike)
2800 {
2801 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2802 ccline.cmdbuff + ccline.cmdpos,
2803 (size_t)(ccline.cmdlen - ccline.cmdpos));
2804 ccline.cmdlen += len;
2805 }
2806 else
2807 {
2808#ifdef FEAT_MBYTE
2809 if (has_mbyte)
2810 {
2811 /* Count nr of characters in the new string. */
2812 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002813 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 ++m;
2815 /* Count nr of bytes in cmdline that are overwritten by these
2816 * characters. */
2817 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002818 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 --m;
2820 if (i < ccline.cmdlen)
2821 {
2822 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2823 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2824 ccline.cmdlen += ccline.cmdpos + len - i;
2825 }
2826 else
2827 ccline.cmdlen = ccline.cmdpos + len;
2828 }
2829 else
2830#endif
2831 if (ccline.cmdpos + len > ccline.cmdlen)
2832 ccline.cmdlen = ccline.cmdpos + len;
2833 }
2834 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2835 ccline.cmdbuff[ccline.cmdlen] = NUL;
2836
2837#ifdef FEAT_MBYTE
2838 if (enc_utf8)
2839 {
2840 /* When the inserted text starts with a composing character,
2841 * backup to the character before it. There could be two of them.
2842 */
2843 i = 0;
2844 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2845 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2846 {
2847 i = (*mb_head_off)(ccline.cmdbuff,
2848 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2849 ccline.cmdpos -= i;
2850 len += i;
2851 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2852 }
2853# ifdef FEAT_ARABIC
2854 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2855 {
2856 /* Check the previous character for Arabic combining pair. */
2857 i = (*mb_head_off)(ccline.cmdbuff,
2858 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2859 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2860 + ccline.cmdpos - i), c))
2861 {
2862 ccline.cmdpos -= i;
2863 len += i;
2864 }
2865 else
2866 i = 0;
2867 }
2868# endif
2869 if (i != 0)
2870 {
2871 /* Also backup the cursor position. */
2872 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2873 ccline.cmdspos -= i;
2874 msg_col -= i;
2875 if (msg_col < 0)
2876 {
2877 msg_col += Columns;
2878 --msg_row;
2879 }
2880 }
2881 }
2882#endif
2883
2884 if (redraw && !cmd_silent)
2885 {
2886 msg_no_more = TRUE;
2887 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002888 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2890 /* Avoid clearing the rest of the line too often. */
2891 if (cmdline_row != i || ccline.overstrike)
2892 msg_clr_eos();
2893 msg_no_more = FALSE;
2894 }
2895#ifdef FEAT_FKMAP
2896 /*
2897 * If we are in Farsi command mode, the character input must be in
2898 * Insert mode. So do not advance the cmdpos.
2899 */
2900 if (!cmd_fkmap)
2901#endif
2902 {
2903 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002906 if (m < 0) /* overflow, Columns or Rows at weird value */
2907 m = MAXCOL;
2908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 else
2910 m = MAXCOL;
2911 for (i = 0; i < len; ++i)
2912 {
2913 c = cmdline_charsize(ccline.cmdpos);
2914#ifdef FEAT_MBYTE
2915 /* count ">" for a double-wide char that doesn't fit. */
2916 if (has_mbyte)
2917 correct_cmdspos(ccline.cmdpos, c);
2918#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002919 /* Stop cursor at the end of the screen, but do increment the
2920 * insert position, so that entering a very long command
2921 * works, even though you can't see it. */
2922 if (ccline.cmdspos + c < m)
2923 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924#ifdef FEAT_MBYTE
2925 if (has_mbyte)
2926 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002927 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 if (c > len - i - 1)
2929 c = len - i - 1;
2930 ccline.cmdpos += c;
2931 i += c;
2932 }
2933#endif
2934 ++ccline.cmdpos;
2935 }
2936 }
2937 }
2938 if (redraw)
2939 msg_check();
2940 return retval;
2941}
2942
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002943static struct cmdline_info prev_ccline;
2944static int prev_ccline_used = FALSE;
2945
2946/*
2947 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2948 * and overwrite it. But get_cmdline_str() may need it, thus make it
2949 * available globally in prev_ccline.
2950 */
2951 static void
2952save_cmdline(ccp)
2953 struct cmdline_info *ccp;
2954{
2955 if (!prev_ccline_used)
2956 {
2957 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2958 prev_ccline_used = TRUE;
2959 }
2960 *ccp = prev_ccline;
2961 prev_ccline = ccline;
2962 ccline.cmdbuff = NULL;
2963 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002964 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002965}
2966
2967/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002968 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002969 */
2970 static void
2971restore_cmdline(ccp)
2972 struct cmdline_info *ccp;
2973{
2974 ccline = prev_ccline;
2975 prev_ccline = *ccp;
2976}
2977
Bram Moolenaar5a305422006-04-28 22:38:25 +00002978#if defined(FEAT_EVAL) || defined(PROTO)
2979/*
2980 * Save the command line into allocated memory. Returns a pointer to be
2981 * passed to restore_cmdline_alloc() later.
2982 * Returns NULL when failed.
2983 */
2984 char_u *
2985save_cmdline_alloc()
2986{
2987 struct cmdline_info *p;
2988
2989 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2990 if (p != NULL)
2991 save_cmdline(p);
2992 return (char_u *)p;
2993}
2994
2995/*
2996 * Restore the command line from the return value of save_cmdline_alloc().
2997 */
2998 void
2999restore_cmdline_alloc(p)
3000 char_u *p;
3001{
3002 if (p != NULL)
3003 {
3004 restore_cmdline((struct cmdline_info *)p);
3005 vim_free(p);
3006 }
3007}
3008#endif
3009
Bram Moolenaar8299df92004-07-10 09:47:34 +00003010/*
3011 * paste a yank register into the command line.
3012 * used by CTRL-R command in command-line mode
3013 * insert_reg() can't be used here, because special characters from the
3014 * register contents will be interpreted as commands.
3015 *
3016 * return FAIL for failure, OK otherwise
3017 */
3018 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003019cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003020 int regname;
3021 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003022 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003023{
3024 long i;
3025 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003026 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003027 int allocated;
3028 struct cmdline_info save_ccline;
3029
3030 /* check for valid regname; also accept special characters for CTRL-R in
3031 * the command line */
3032 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3033 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3034 return FAIL;
3035
3036 /* A register containing CTRL-R can cause an endless loop. Allow using
3037 * CTRL-C to break the loop. */
3038 line_breakcheck();
3039 if (got_int)
3040 return FAIL;
3041
3042#ifdef FEAT_CLIPBOARD
3043 regname = may_get_selection(regname);
3044#endif
3045
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003046 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003047 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003048 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003049 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003050 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003051 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003052 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003053
3054 if (i)
3055 {
3056 /* Got the value of a special register in "arg". */
3057 if (arg == NULL)
3058 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003059
3060 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3061 * part of the word. */
3062 p = arg;
3063 if (p_is && regname == Ctrl_W)
3064 {
3065 char_u *w;
3066 int len;
3067
3068 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003069 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003070 {
3071#ifdef FEAT_MBYTE
3072 if (has_mbyte)
3073 {
3074 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3075 if (!vim_iswordc(mb_ptr2char(w - len)))
3076 break;
3077 w -= len;
3078 }
3079 else
3080#endif
3081 {
3082 if (!vim_iswordc(w[-1]))
3083 break;
3084 --w;
3085 }
3086 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003087 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003088 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3089 p += len;
3090 }
3091
3092 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003093 if (allocated)
3094 vim_free(arg);
3095 return OK;
3096 }
3097
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003098 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003099}
3100
3101/*
3102 * Put a string on the command line.
3103 * When "literally" is TRUE, insert literally.
3104 * When "literally" is FALSE, insert as typed, but don't leave the command
3105 * line.
3106 */
3107 void
3108cmdline_paste_str(s, literally)
3109 char_u *s;
3110 int literally;
3111{
3112 int c, cv;
3113
3114 if (literally)
3115 put_on_cmdline(s, -1, TRUE);
3116 else
3117 while (*s != NUL)
3118 {
3119 cv = *s;
3120 if (cv == Ctrl_V && s[1])
3121 ++s;
3122#ifdef FEAT_MBYTE
3123 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003124 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003125 else
3126#endif
3127 c = *s++;
3128 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3129#ifdef UNIX
3130 || c == intr_char
3131#endif
3132 || (c == Ctrl_BSL && *s == Ctrl_N))
3133 stuffcharReadbuff(Ctrl_V);
3134 stuffcharReadbuff(c);
3135 }
3136}
3137
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#ifdef FEAT_WILDMENU
3139/*
3140 * Delete characters on the command line, from "from" to the current
3141 * position.
3142 */
3143 static void
3144cmdline_del(from)
3145 int from;
3146{
3147 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3148 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3149 ccline.cmdlen -= ccline.cmdpos - from;
3150 ccline.cmdpos = from;
3151}
3152#endif
3153
3154/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003155 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 * search
3157 */
3158 void
3159redrawcmdline()
3160{
3161 if (cmd_silent)
3162 return;
3163 need_wait_return = FALSE;
3164 compute_cmdrow();
3165 redrawcmd();
3166 cursorcmd();
3167}
3168
3169 static void
3170redrawcmdprompt()
3171{
3172 int i;
3173
3174 if (cmd_silent)
3175 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003176 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 msg_putchar(ccline.cmdfirstc);
3178 if (ccline.cmdprompt != NULL)
3179 {
3180 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3181 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3182 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003183 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 --ccline.cmdindent;
3185 }
3186 else
3187 for (i = ccline.cmdindent; i > 0; --i)
3188 msg_putchar(' ');
3189}
3190
3191/*
3192 * Redraw what is currently on the command line.
3193 */
3194 void
3195redrawcmd()
3196{
3197 if (cmd_silent)
3198 return;
3199
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003200 /* when 'incsearch' is set there may be no command line while redrawing */
3201 if (ccline.cmdbuff == NULL)
3202 {
3203 windgoto(cmdline_row, 0);
3204 msg_clr_eos();
3205 return;
3206 }
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 msg_start();
3209 redrawcmdprompt();
3210
3211 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3212 msg_no_more = TRUE;
3213 draw_cmdline(0, ccline.cmdlen);
3214 msg_clr_eos();
3215 msg_no_more = FALSE;
3216
3217 set_cmdspos_cursor();
3218
3219 /*
3220 * An emsg() before may have set msg_scroll. This is used in normal mode,
3221 * in cmdline mode we can reset them now.
3222 */
3223 msg_scroll = FALSE; /* next message overwrites cmdline */
3224
3225 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3226 * in cmdline mode */
3227 skip_redraw = FALSE;
3228}
3229
3230 void
3231compute_cmdrow()
3232{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003233 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 cmdline_row = Rows - 1;
3235 else
3236 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3237 + W_STATUS_HEIGHT(lastwin);
3238}
3239
3240 static void
3241cursorcmd()
3242{
3243 if (cmd_silent)
3244 return;
3245
3246#ifdef FEAT_RIGHTLEFT
3247 if (cmdmsg_rl)
3248 {
3249 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3250 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3251 if (msg_row <= 0)
3252 msg_row = Rows - 1;
3253 }
3254 else
3255#endif
3256 {
3257 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3258 msg_col = ccline.cmdspos % (int)Columns;
3259 if (msg_row >= Rows)
3260 msg_row = Rows - 1;
3261 }
3262
3263 windgoto(msg_row, msg_col);
3264#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3265 redrawcmd_preedit();
3266#endif
3267#ifdef MCH_CURSOR_SHAPE
3268 mch_update_cursor();
3269#endif
3270}
3271
3272 void
3273gotocmdline(clr)
3274 int clr;
3275{
3276 msg_start();
3277#ifdef FEAT_RIGHTLEFT
3278 if (cmdmsg_rl)
3279 msg_col = Columns - 1;
3280 else
3281#endif
3282 msg_col = 0; /* always start in column 0 */
3283 if (clr) /* clear the bottom line(s) */
3284 msg_clr_eos(); /* will reset clear_cmdline */
3285 windgoto(cmdline_row, 0);
3286}
3287
3288/*
3289 * Check the word in front of the cursor for an abbreviation.
3290 * Called when the non-id character "c" has been entered.
3291 * When an abbreviation is recognized it is removed from the text with
3292 * backspaces and the replacement string is inserted, followed by "c".
3293 */
3294 static int
3295ccheck_abbr(c)
3296 int c;
3297{
3298 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3299 return FALSE;
3300
3301 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3302}
3303
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003304#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3305 static int
3306#ifdef __BORLANDC__
3307_RTLENTRYF
3308#endif
3309sort_func_compare(s1, s2)
3310 const void *s1;
3311 const void *s2;
3312{
3313 char_u *p1 = *(char_u **)s1;
3314 char_u *p2 = *(char_u **)s2;
3315
3316 if (*p1 != '<' && *p2 == '<') return -1;
3317 if (*p1 == '<' && *p2 != '<') return 1;
3318 return STRCMP(p1, p2);
3319}
3320#endif
3321
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322/*
3323 * Return FAIL if this is not an appropriate context in which to do
3324 * completion of anything, return OK if it is (even if there are no matches).
3325 * For the caller, this means that the character is just passed through like a
3326 * normal character (instead of being expanded). This allows :s/^I^D etc.
3327 */
3328 static int
3329nextwild(xp, type, options)
3330 expand_T *xp;
3331 int type;
3332 int options; /* extra options for ExpandOne() */
3333{
3334 int i, j;
3335 char_u *p1;
3336 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 int difflen;
3338 int v;
3339
3340 if (xp->xp_numfiles == -1)
3341 {
3342 set_expand_context(xp);
3343 cmd_showtail = expand_showtail(xp);
3344 }
3345
3346 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3347 {
3348 beep_flush();
3349 return OK; /* Something illegal on command line */
3350 }
3351 if (xp->xp_context == EXPAND_NOTHING)
3352 {
3353 /* Caller can use the character as a normal char instead */
3354 return FAIL;
3355 }
3356
3357 MSG_PUTS("..."); /* show that we are busy */
3358 out_flush();
3359
3360 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003361 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
3363 if (type == WILD_NEXT || type == WILD_PREV)
3364 {
3365 /*
3366 * Get next/previous match for a previous expanded pattern.
3367 */
3368 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3369 }
3370 else
3371 {
3372 /*
3373 * Translate string into pattern and expand it.
3374 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003375 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3376 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 p2 = NULL;
3378 else
3379 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003380 int use_options = options |
3381 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE;
3382
3383 if (p_wic)
3384 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003385 p2 = ExpandOne(xp, p1,
3386 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003387 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003389 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 if (p2 != NULL && type == WILD_LONGEST)
3391 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003392 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 if (ccline.cmdbuff[i + j] == '*'
3394 || ccline.cmdbuff[i + j] == '?')
3395 break;
3396 if ((int)STRLEN(p2) < j)
3397 {
3398 vim_free(p2);
3399 p2 = NULL;
3400 }
3401 }
3402 }
3403 }
3404
3405 if (p2 != NULL && !got_int)
3406 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003407 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003408 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003410 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 xp->xp_pattern = ccline.cmdbuff + i;
3412 }
3413 else
3414 v = OK;
3415 if (v == OK)
3416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003417 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3418 &ccline.cmdbuff[ccline.cmdpos],
3419 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3420 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 ccline.cmdlen += difflen;
3422 ccline.cmdpos += difflen;
3423 }
3424 }
3425 vim_free(p2);
3426
3427 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003428 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430 /* When expanding a ":map" command and no matches are found, assume that
3431 * the key is supposed to be inserted literally */
3432 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3433 return FAIL;
3434
3435 if (xp->xp_numfiles <= 0 && p2 == NULL)
3436 beep_flush();
3437 else if (xp->xp_numfiles == 1)
3438 /* free expanded pattern */
3439 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3440
3441 return OK;
3442}
3443
3444/*
3445 * Do wildcard expansion on the string 'str'.
3446 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003447 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 * Return NULL for failure.
3449 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003450 * "orig" is the originally expanded string, copied to allocated memory. It
3451 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3452 * WILD_PREV "orig" should be NULL.
3453 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003454 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3455 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 *
3457 * mode = WILD_FREE: just free previously expanded matches
3458 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3459 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3460 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3461 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3462 * mode = WILD_ALL: return all matches concatenated
3463 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003464 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 *
3466 * options = WILD_LIST_NOTFOUND: list entries without a match
3467 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3468 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3469 * options = WILD_NO_BEEP: Don't beep for multiple matches
3470 * options = WILD_ADD_SLASH: add a slash after directory names
3471 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3472 * options = WILD_SILENT: don't print warning messages
3473 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003474 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 *
3476 * The variables xp->xp_context and xp->xp_backslash must have been set!
3477 */
3478 char_u *
3479ExpandOne(xp, str, orig, options, mode)
3480 expand_T *xp;
3481 char_u *str;
3482 char_u *orig; /* allocated copy of original of expanded string */
3483 int options;
3484 int mode;
3485{
3486 char_u *ss = NULL;
3487 static int findex;
3488 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003489 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 int i;
3491 long_u len;
3492 int non_suf_match; /* number without matching suffix */
3493
3494 /*
3495 * first handle the case of using an old match
3496 */
3497 if (mode == WILD_NEXT || mode == WILD_PREV)
3498 {
3499 if (xp->xp_numfiles > 0)
3500 {
3501 if (mode == WILD_PREV)
3502 {
3503 if (findex == -1)
3504 findex = xp->xp_numfiles;
3505 --findex;
3506 }
3507 else /* mode == WILD_NEXT */
3508 ++findex;
3509
3510 /*
3511 * When wrapping around, return the original string, set findex to
3512 * -1.
3513 */
3514 if (findex < 0)
3515 {
3516 if (orig_save == NULL)
3517 findex = xp->xp_numfiles - 1;
3518 else
3519 findex = -1;
3520 }
3521 if (findex >= xp->xp_numfiles)
3522 {
3523 if (orig_save == NULL)
3524 findex = 0;
3525 else
3526 findex = -1;
3527 }
3528#ifdef FEAT_WILDMENU
3529 if (p_wmnu)
3530 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3531 findex, cmd_showtail);
3532#endif
3533 if (findex == -1)
3534 return vim_strsave(orig_save);
3535 return vim_strsave(xp->xp_files[findex]);
3536 }
3537 else
3538 return NULL;
3539 }
3540
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003541 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3543 {
3544 FreeWild(xp->xp_numfiles, xp->xp_files);
3545 xp->xp_numfiles = -1;
3546 vim_free(orig_save);
3547 orig_save = NULL;
3548 }
3549 findex = 0;
3550
3551 if (mode == WILD_FREE) /* only release file name */
3552 return NULL;
3553
3554 if (xp->xp_numfiles == -1)
3555 {
3556 vim_free(orig_save);
3557 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003558 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559
3560 /*
3561 * Do the expansion.
3562 */
3563 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3564 options) == FAIL)
3565 {
3566#ifdef FNAME_ILLEGAL
3567 /* Illegal file name has been silently skipped. But when there
3568 * are wildcards, the real problem is that there was no match,
3569 * causing the pattern to be added, which has illegal characters.
3570 */
3571 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3572 EMSG2(_(e_nomatch2), str);
3573#endif
3574 }
3575 else if (xp->xp_numfiles == 0)
3576 {
3577 if (!(options & WILD_SILENT))
3578 EMSG2(_(e_nomatch2), str);
3579 }
3580 else
3581 {
3582 /* Escape the matches for use on the command line. */
3583 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3584
3585 /*
3586 * Check for matching suffixes in file names.
3587 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003588 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3589 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
3591 if (xp->xp_numfiles)
3592 non_suf_match = xp->xp_numfiles;
3593 else
3594 non_suf_match = 1;
3595 if ((xp->xp_context == EXPAND_FILES
3596 || xp->xp_context == EXPAND_DIRECTORIES)
3597 && xp->xp_numfiles > 1)
3598 {
3599 /*
3600 * More than one match; check suffix.
3601 * The files will have been sorted on matching suffix in
3602 * expand_wildcards, only need to check the first two.
3603 */
3604 non_suf_match = 0;
3605 for (i = 0; i < 2; ++i)
3606 if (match_suffix(xp->xp_files[i]))
3607 ++non_suf_match;
3608 }
3609 if (non_suf_match != 1)
3610 {
3611 /* Can we ever get here unless it's while expanding
3612 * interactively? If not, we can get rid of this all
3613 * together. Don't really want to wait for this message
3614 * (and possibly have to hit return to continue!).
3615 */
3616 if (!(options & WILD_SILENT))
3617 EMSG(_(e_toomany));
3618 else if (!(options & WILD_NO_BEEP))
3619 beep_flush();
3620 }
3621 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3622 ss = vim_strsave(xp->xp_files[0]);
3623 }
3624 }
3625 }
3626
3627 /* Find longest common part */
3628 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3629 {
3630 for (len = 0; xp->xp_files[0][len]; ++len)
3631 {
3632 for (i = 0; i < xp->xp_numfiles; ++i)
3633 {
3634#ifdef CASE_INSENSITIVE_FILENAME
3635 if (xp->xp_context == EXPAND_DIRECTORIES
3636 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003637 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 || xp->xp_context == EXPAND_BUFFERS)
3639 {
3640 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3641 TOLOWER_LOC(xp->xp_files[0][len]))
3642 break;
3643 }
3644 else
3645#endif
3646 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3647 break;
3648 }
3649 if (i < xp->xp_numfiles)
3650 {
3651 if (!(options & WILD_NO_BEEP))
3652 vim_beep();
3653 break;
3654 }
3655 }
3656 ss = alloc((unsigned)len + 1);
3657 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003658 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 findex = -1; /* next p_wc gets first one */
3660 }
3661
3662 /* Concatenate all matching names */
3663 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3664 {
3665 len = 0;
3666 for (i = 0; i < xp->xp_numfiles; ++i)
3667 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3668 ss = lalloc(len, TRUE);
3669 if (ss != NULL)
3670 {
3671 *ss = NUL;
3672 for (i = 0; i < xp->xp_numfiles; ++i)
3673 {
3674 STRCAT(ss, xp->xp_files[i]);
3675 if (i != xp->xp_numfiles - 1)
3676 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3677 }
3678 }
3679 }
3680
3681 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3682 ExpandCleanup(xp);
3683
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003684 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003685 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003686 vim_free(orig);
3687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 return ss;
3689}
3690
3691/*
3692 * Prepare an expand structure for use.
3693 */
3694 void
3695ExpandInit(xp)
3696 expand_T *xp;
3697{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003698 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003699 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003701#ifndef BACKSLASH_IN_FILENAME
3702 xp->xp_shell = FALSE;
3703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 xp->xp_numfiles = -1;
3705 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003706#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3707 xp->xp_arg = NULL;
3708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709}
3710
3711/*
3712 * Cleanup an expand structure after use.
3713 */
3714 void
3715ExpandCleanup(xp)
3716 expand_T *xp;
3717{
3718 if (xp->xp_numfiles >= 0)
3719 {
3720 FreeWild(xp->xp_numfiles, xp->xp_files);
3721 xp->xp_numfiles = -1;
3722 }
3723}
3724
3725 void
3726ExpandEscape(xp, str, numfiles, files, options)
3727 expand_T *xp;
3728 char_u *str;
3729 int numfiles;
3730 char_u **files;
3731 int options;
3732{
3733 int i;
3734 char_u *p;
3735
3736 /*
3737 * May change home directory back to "~"
3738 */
3739 if (options & WILD_HOME_REPLACE)
3740 tilde_replace(str, numfiles, files);
3741
3742 if (options & WILD_ESCAPE)
3743 {
3744 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003745 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003746 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 || xp->xp_context == EXPAND_BUFFERS
3748 || xp->xp_context == EXPAND_DIRECTORIES)
3749 {
3750 /*
3751 * Insert a backslash into a file name before a space, \, %, #
3752 * and wildmatch characters, except '~'.
3753 */
3754 for (i = 0; i < numfiles; ++i)
3755 {
3756 /* for ":set path=" we need to escape spaces twice */
3757 if (xp->xp_backslash == XP_BS_THREE)
3758 {
3759 p = vim_strsave_escaped(files[i], (char_u *)" ");
3760 if (p != NULL)
3761 {
3762 vim_free(files[i]);
3763 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003764#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 p = vim_strsave_escaped(files[i], (char_u *)" ");
3766 if (p != NULL)
3767 {
3768 vim_free(files[i]);
3769 files[i] = p;
3770 }
3771#endif
3772 }
3773 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003774#ifdef BACKSLASH_IN_FILENAME
3775 p = vim_strsave_fnameescape(files[i], FALSE);
3776#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003777 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 if (p != NULL)
3780 {
3781 vim_free(files[i]);
3782 files[i] = p;
3783 }
3784
3785 /* If 'str' starts with "\~", replace "~" at start of
3786 * files[i] with "\~". */
3787 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003788 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003791
3792 /* If the first file starts with a '+' escape it. Otherwise it
3793 * could be seen as "+cmd". */
3794 if (*files[0] == '+')
3795 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
3797 else if (xp->xp_context == EXPAND_TAGS)
3798 {
3799 /*
3800 * Insert a backslash before characters in a tag name that
3801 * would terminate the ":tag" command.
3802 */
3803 for (i = 0; i < numfiles; ++i)
3804 {
3805 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3806 if (p != NULL)
3807 {
3808 vim_free(files[i]);
3809 files[i] = p;
3810 }
3811 }
3812 }
3813 }
3814}
3815
3816/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003817 * Escape special characters in "fname" for when used as a file name argument
3818 * after a Vim command, or, when "shell" is non-zero, a shell command.
3819 * Returns the result in allocated memory.
3820 */
3821 char_u *
3822vim_strsave_fnameescape(fname, shell)
3823 char_u *fname;
3824 int shell;
3825{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003826 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003827#ifdef BACKSLASH_IN_FILENAME
3828 char_u buf[20];
3829 int j = 0;
3830
3831 /* Don't escape '[' and '{' if they are in 'isfname'. */
3832 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3833 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3834 buf[j++] = *p;
3835 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003836 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003837#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003838 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3839 if (shell && csh_like_shell() && p != NULL)
3840 {
3841 char_u *s;
3842
3843 /* For csh and similar shells need to put two backslashes before '!'.
3844 * One is taken by Vim, one by the shell. */
3845 s = vim_strsave_escaped(p, (char_u *)"!");
3846 vim_free(p);
3847 p = s;
3848 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003849#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003850
3851 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3852 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003853 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003854 escape_fname(&p);
3855
3856 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003857}
3858
3859/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003860 * Put a backslash before the file name in "pp", which is in allocated memory.
3861 */
3862 static void
3863escape_fname(pp)
3864 char_u **pp;
3865{
3866 char_u *p;
3867
3868 p = alloc((unsigned)(STRLEN(*pp) + 2));
3869 if (p != NULL)
3870 {
3871 p[0] = '\\';
3872 STRCPY(p + 1, *pp);
3873 vim_free(*pp);
3874 *pp = p;
3875 }
3876}
3877
3878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 * For each file name in files[num_files]:
3880 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3881 */
3882 void
3883tilde_replace(orig_pat, num_files, files)
3884 char_u *orig_pat;
3885 int num_files;
3886 char_u **files;
3887{
3888 int i;
3889 char_u *p;
3890
3891 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3892 {
3893 for (i = 0; i < num_files; ++i)
3894 {
3895 p = home_replace_save(NULL, files[i]);
3896 if (p != NULL)
3897 {
3898 vim_free(files[i]);
3899 files[i] = p;
3900 }
3901 }
3902 }
3903}
3904
3905/*
3906 * Show all matches for completion on the command line.
3907 * Returns EXPAND_NOTHING when the character that triggered expansion should
3908 * be inserted like a normal character.
3909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 static int
3911showmatches(xp, wildmenu)
3912 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003913 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914{
3915#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3916 int num_files;
3917 char_u **files_found;
3918 int i, j, k;
3919 int maxlen;
3920 int lines;
3921 int columns;
3922 char_u *p;
3923 int lastlen;
3924 int attr;
3925 int showtail;
3926
3927 if (xp->xp_numfiles == -1)
3928 {
3929 set_expand_context(xp);
3930 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3931 &num_files, &files_found);
3932 showtail = expand_showtail(xp);
3933 if (i != EXPAND_OK)
3934 return i;
3935
3936 }
3937 else
3938 {
3939 num_files = xp->xp_numfiles;
3940 files_found = xp->xp_files;
3941 showtail = cmd_showtail;
3942 }
3943
3944#ifdef FEAT_WILDMENU
3945 if (!wildmenu)
3946 {
3947#endif
3948 msg_didany = FALSE; /* lines_left will be set */
3949 msg_start(); /* prepare for paging */
3950 msg_putchar('\n');
3951 out_flush();
3952 cmdline_row = msg_row;
3953 msg_didany = FALSE; /* lines_left will be set again */
3954 msg_start(); /* prepare for paging */
3955#ifdef FEAT_WILDMENU
3956 }
3957#endif
3958
3959 if (got_int)
3960 got_int = FALSE; /* only int. the completion, not the cmd line */
3961#ifdef FEAT_WILDMENU
3962 else if (wildmenu)
3963 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3964#endif
3965 else
3966 {
3967 /* find the length of the longest file name */
3968 maxlen = 0;
3969 for (i = 0; i < num_files; ++i)
3970 {
3971 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003972 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 || xp->xp_context == EXPAND_BUFFERS))
3974 {
3975 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3976 j = vim_strsize(NameBuff);
3977 }
3978 else
3979 j = vim_strsize(L_SHOWFILE(i));
3980 if (j > maxlen)
3981 maxlen = j;
3982 }
3983
3984 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3985 lines = num_files;
3986 else
3987 {
3988 /* compute the number of columns and lines for the listing */
3989 maxlen += 2; /* two spaces between file names */
3990 columns = ((int)Columns + 2) / maxlen;
3991 if (columns < 1)
3992 columns = 1;
3993 lines = (num_files + columns - 1) / columns;
3994 }
3995
3996 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3997
3998 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3999 {
4000 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4001 msg_clr_eos();
4002 msg_advance(maxlen - 3);
4003 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4004 }
4005
4006 /* list the files line by line */
4007 for (i = 0; i < lines; ++i)
4008 {
4009 lastlen = 999;
4010 for (k = i; k < num_files; k += lines)
4011 {
4012 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4013 {
4014 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4015 p = files_found[k] + STRLEN(files_found[k]) + 1;
4016 msg_advance(maxlen + 1);
4017 msg_puts(p);
4018 msg_advance(maxlen + 3);
4019 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4020 break;
4021 }
4022 for (j = maxlen - lastlen; --j >= 0; )
4023 msg_putchar(' ');
4024 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004025 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 || xp->xp_context == EXPAND_BUFFERS)
4027 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004028 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004029 if (xp->xp_numfiles != -1)
4030 {
4031 char_u *halved_slash;
4032 char_u *exp_path;
4033
4034 /* Expansion was done before and special characters
4035 * were escaped, need to halve backslashes. Also
4036 * $HOME has been replaced with ~/. */
4037 exp_path = expand_env_save_opt(files_found[k], TRUE);
4038 halved_slash = backslash_halve_save(
4039 exp_path != NULL ? exp_path : files_found[k]);
4040 j = mch_isdir(halved_slash != NULL ? halved_slash
4041 : files_found[k]);
4042 vim_free(exp_path);
4043 vim_free(halved_slash);
4044 }
4045 else
4046 /* Expansion was done here, file names are literal. */
4047 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 if (showtail)
4049 p = L_SHOWFILE(k);
4050 else
4051 {
4052 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4053 TRUE);
4054 p = NameBuff;
4055 }
4056 }
4057 else
4058 {
4059 j = FALSE;
4060 p = L_SHOWFILE(k);
4061 }
4062 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4063 }
4064 if (msg_col > 0) /* when not wrapped around */
4065 {
4066 msg_clr_eos();
4067 msg_putchar('\n');
4068 }
4069 out_flush(); /* show one line at a time */
4070 if (got_int)
4071 {
4072 got_int = FALSE;
4073 break;
4074 }
4075 }
4076
4077 /*
4078 * we redraw the command below the lines that we have just listed
4079 * This is a bit tricky, but it saves a lot of screen updating.
4080 */
4081 cmdline_row = msg_row; /* will put it back later */
4082 }
4083
4084 if (xp->xp_numfiles == -1)
4085 FreeWild(num_files, files_found);
4086
4087 return EXPAND_OK;
4088}
4089
4090/*
4091 * Private gettail for showmatches() (and win_redr_status_matches()):
4092 * Find tail of file name path, but ignore trailing "/".
4093 */
4094 char_u *
4095sm_gettail(s)
4096 char_u *s;
4097{
4098 char_u *p;
4099 char_u *t = s;
4100 int had_sep = FALSE;
4101
4102 for (p = s; *p != NUL; )
4103 {
4104 if (vim_ispathsep(*p)
4105#ifdef BACKSLASH_IN_FILENAME
4106 && !rem_backslash(p)
4107#endif
4108 )
4109 had_sep = TRUE;
4110 else if (had_sep)
4111 {
4112 t = p;
4113 had_sep = FALSE;
4114 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004115 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117 return t;
4118}
4119
4120/*
4121 * Return TRUE if we only need to show the tail of completion matches.
4122 * When not completing file names or there is a wildcard in the path FALSE is
4123 * returned.
4124 */
4125 static int
4126expand_showtail(xp)
4127 expand_T *xp;
4128{
4129 char_u *s;
4130 char_u *end;
4131
4132 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004133 if (xp->xp_context != EXPAND_FILES
4134 && xp->xp_context != EXPAND_SHELLCMD
4135 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 return FALSE;
4137
4138 end = gettail(xp->xp_pattern);
4139 if (end == xp->xp_pattern) /* there is no path separator */
4140 return FALSE;
4141
4142 for (s = xp->xp_pattern; s < end; s++)
4143 {
4144 /* Skip escaped wildcards. Only when the backslash is not a path
4145 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4146 if (rem_backslash(s))
4147 ++s;
4148 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4149 return FALSE;
4150 }
4151 return TRUE;
4152}
4153
4154/*
4155 * Prepare a string for expansion.
4156 * When expanding file names: The string will be used with expand_wildcards().
4157 * Copy the file name into allocated memory and add a '*' at the end.
4158 * When expanding other names: The string will be used with regcomp(). Copy
4159 * the name into allocated memory and prepend "^".
4160 */
4161 char_u *
4162addstar(fname, len, context)
4163 char_u *fname;
4164 int len;
4165 int context; /* EXPAND_FILES etc. */
4166{
4167 char_u *retval;
4168 int i, j;
4169 int new_len;
4170 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004171 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004173 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004174 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004175 && context != EXPAND_SHELLCMD
4176 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
4178 /*
4179 * Matching will be done internally (on something other than files).
4180 * So we convert the file-matching-type wildcards into our kind for
4181 * use with vim_regcomp(). First work out how long it will be:
4182 */
4183
4184 /* For help tags the translation is done in find_help_tags().
4185 * For a tag pattern starting with "/" no translation is needed. */
4186 if (context == EXPAND_HELP
4187 || context == EXPAND_COLORS
4188 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004189 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004190 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 || (context == EXPAND_TAGS && fname[0] == '/'))
4192 retval = vim_strnsave(fname, len);
4193 else
4194 {
4195 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4196 for (i = 0; i < len; i++)
4197 {
4198 if (fname[i] == '*' || fname[i] == '~')
4199 new_len++; /* '*' needs to be replaced by ".*"
4200 '~' needs to be replaced by "\~" */
4201
4202 /* Buffer names are like file names. "." should be literal */
4203 if (context == EXPAND_BUFFERS && fname[i] == '.')
4204 new_len++; /* "." becomes "\." */
4205
4206 /* Custom expansion takes care of special things, match
4207 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004208 if ((context == EXPAND_USER_DEFINED
4209 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 new_len++; /* '\' becomes "\\" */
4211 }
4212 retval = alloc(new_len);
4213 if (retval != NULL)
4214 {
4215 retval[0] = '^';
4216 j = 1;
4217 for (i = 0; i < len; i++, j++)
4218 {
4219 /* Skip backslash. But why? At least keep it for custom
4220 * expansion. */
4221 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004222 && context != EXPAND_USER_LIST
4223 && fname[i] == '\\'
4224 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 break;
4226
4227 switch (fname[i])
4228 {
4229 case '*': retval[j++] = '.';
4230 break;
4231 case '~': retval[j++] = '\\';
4232 break;
4233 case '?': retval[j] = '.';
4234 continue;
4235 case '.': if (context == EXPAND_BUFFERS)
4236 retval[j++] = '\\';
4237 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004238 case '\\': if (context == EXPAND_USER_DEFINED
4239 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 retval[j++] = '\\';
4241 break;
4242 }
4243 retval[j] = fname[i];
4244 }
4245 retval[j] = NUL;
4246 }
4247 }
4248 }
4249 else
4250 {
4251 retval = alloc(len + 4);
4252 if (retval != NULL)
4253 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004254 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
4256 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004257 * Don't add a star to *, ~, ~user, $var or `cmd`.
4258 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 * ~ would be at the start of the file name, but not the tail.
4260 * $ could be anywhere in the tail.
4261 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004262 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 */
4264 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004265 ends_in_star = (len > 0 && retval[len - 1] == '*');
4266#ifndef BACKSLASH_IN_FILENAME
4267 for (i = len - 2; i >= 0; --i)
4268 {
4269 if (retval[i] != '\\')
4270 break;
4271 ends_in_star = !ends_in_star;
4272 }
4273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004275 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 && vim_strchr(tail, '$') == NULL
4277 && vim_strchr(retval, '`') == NULL)
4278 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004279 else if (len > 0 && retval[len - 1] == '$')
4280 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 retval[len] = NUL;
4282 }
4283 }
4284 return retval;
4285}
4286
4287/*
4288 * Must parse the command line so far to work out what context we are in.
4289 * Completion can then be done based on that context.
4290 * This routine sets the variables:
4291 * xp->xp_pattern The start of the pattern to be expanded within
4292 * the command line (ends at the cursor).
4293 * xp->xp_context The type of thing to expand. Will be one of:
4294 *
4295 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4296 * the command line, like an unknown command. Caller
4297 * should beep.
4298 * EXPAND_NOTHING Unrecognised context for completion, use char like
4299 * a normal char, rather than for completion. eg
4300 * :s/^I/
4301 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4302 * it.
4303 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4304 * EXPAND_FILES After command with XFILE set, or after setting
4305 * with P_EXPAND set. eg :e ^I, :w>>^I
4306 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4307 * when we know only directories are of interest. eg
4308 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004309 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4311 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4312 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4313 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4314 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4315 * EXPAND_EVENTS Complete event names
4316 * EXPAND_SYNTAX Complete :syntax command arguments
4317 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4318 * EXPAND_AUGROUP Complete autocommand group names
4319 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4320 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4321 * eg :unmap a^I , :cunab x^I
4322 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4323 * eg :call sub^I
4324 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4325 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4326 * names in expressions, eg :while s^I
4327 * EXPAND_ENV_VARS Complete environment variable names
4328 */
4329 static void
4330set_expand_context(xp)
4331 expand_T *xp;
4332{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004333 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 if (ccline.cmdfirstc != ':'
4335#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004336 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004337 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338#endif
4339 )
4340 {
4341 xp->xp_context = EXPAND_NOTHING;
4342 return;
4343 }
4344 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4345}
4346
4347 void
4348set_cmd_context(xp, str, len, col)
4349 expand_T *xp;
4350 char_u *str; /* start of command line */
4351 int len; /* length of command line (excl. NUL) */
4352 int col; /* position of cursor */
4353{
4354 int old_char = NUL;
4355 char_u *nextcomm;
4356
4357 /*
4358 * Avoid a UMR warning from Purify, only save the character if it has been
4359 * written before.
4360 */
4361 if (col < len)
4362 old_char = str[col];
4363 str[col] = NUL;
4364 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004365
4366#ifdef FEAT_EVAL
4367 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004368 {
4369# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004370 /* pass CMD_SIZE because there is no real command */
4371 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004372# endif
4373 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004374 else if (ccline.input_fn)
4375 {
4376 xp->xp_context = ccline.xp_context;
4377 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004378# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004379 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004380# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004381 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004382 else
4383#endif
4384 while (nextcomm != NULL)
4385 nextcomm = set_one_cmd_context(xp, nextcomm);
4386
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 str[col] = old_char;
4388}
4389
4390/*
4391 * Expand the command line "str" from context "xp".
4392 * "xp" must have been set by set_cmd_context().
4393 * xp->xp_pattern points into "str", to where the text that is to be expanded
4394 * starts.
4395 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4396 * cursor.
4397 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4398 * key that triggered expansion literally.
4399 * Returns EXPAND_OK otherwise.
4400 */
4401 int
4402expand_cmdline(xp, str, col, matchcount, matches)
4403 expand_T *xp;
4404 char_u *str; /* start of command line */
4405 int col; /* position of cursor */
4406 int *matchcount; /* return: nr of matches */
4407 char_u ***matches; /* return: array of pointers to matches */
4408{
4409 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004410 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
4412 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4413 {
4414 beep_flush();
4415 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4416 }
4417 if (xp->xp_context == EXPAND_NOTHING)
4418 {
4419 /* Caller can use the character as a normal char instead */
4420 return EXPAND_NOTHING;
4421 }
4422
4423 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004424 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4425 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 if (file_str == NULL)
4427 return EXPAND_UNSUCCESSFUL;
4428
Bram Moolenaar94950a92010-12-02 16:01:29 +01004429 if (p_wic)
4430 options += WILD_ICASE;
4431
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004433 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 {
4435 *matchcount = 0;
4436 *matches = NULL;
4437 }
4438 vim_free(file_str);
4439
4440 return EXPAND_OK;
4441}
4442
4443#ifdef FEAT_MULTI_LANG
4444/*
4445 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4446 */
4447static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4448
4449 static void
4450cleanup_help_tags(num_file, file)
4451 int num_file;
4452 char_u **file;
4453{
4454 int i, j;
4455 int len;
4456
4457 for (i = 0; i < num_file; ++i)
4458 {
4459 len = (int)STRLEN(file[i]) - 3;
4460 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4461 {
4462 /* Sorting on priority means the same item in another language may
4463 * be anywhere. Search all items for a match up to the "@en". */
4464 for (j = 0; j < num_file; ++j)
4465 if (j != i
4466 && (int)STRLEN(file[j]) == len + 3
4467 && STRNCMP(file[i], file[j], len + 1) == 0)
4468 break;
4469 if (j == num_file)
4470 file[i][len] = NUL;
4471 }
4472 }
4473}
4474#endif
4475
4476/*
4477 * Do the expansion based on xp->xp_context and "pat".
4478 */
4479 static int
4480ExpandFromContext(xp, pat, num_file, file, options)
4481 expand_T *xp;
4482 char_u *pat;
4483 int *num_file;
4484 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004485 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486{
4487#ifdef FEAT_CMDL_COMPL
4488 regmatch_T regmatch;
4489#endif
4490 int ret;
4491 int flags;
4492
4493 flags = EW_DIR; /* include directories */
4494 if (options & WILD_LIST_NOTFOUND)
4495 flags |= EW_NOTFOUND;
4496 if (options & WILD_ADD_SLASH)
4497 flags |= EW_ADDSLASH;
4498 if (options & WILD_KEEP_ALL)
4499 flags |= EW_KEEPALL;
4500 if (options & WILD_SILENT)
4501 flags |= EW_SILENT;
4502
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004503 if (xp->xp_context == EXPAND_FILES
4504 || xp->xp_context == EXPAND_DIRECTORIES
4505 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
4507 /*
4508 * Expand file or directory names.
4509 */
4510 int free_pat = FALSE;
4511 int i;
4512
4513 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4514 * space */
4515 if (xp->xp_backslash != XP_BS_NONE)
4516 {
4517 free_pat = TRUE;
4518 pat = vim_strsave(pat);
4519 for (i = 0; pat[i]; ++i)
4520 if (pat[i] == '\\')
4521 {
4522 if (xp->xp_backslash == XP_BS_THREE
4523 && pat[i + 1] == '\\'
4524 && pat[i + 2] == '\\'
4525 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004526 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 if (xp->xp_backslash == XP_BS_ONE
4528 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004529 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 }
4531 }
4532
4533 if (xp->xp_context == EXPAND_FILES)
4534 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004535 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4536 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 else
4538 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004539 if (options & WILD_ICASE)
4540 flags |= EW_ICASE;
4541
Bram Moolenaard7834d32009-12-02 16:14:36 +00004542 /* Expand wildcards, supporting %:h and the like. */
4543 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 if (free_pat)
4545 vim_free(pat);
4546 return ret;
4547 }
4548
4549 *file = (char_u **)"";
4550 *num_file = 0;
4551 if (xp->xp_context == EXPAND_HELP)
4552 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004553 /* With an empty argument we would get all the help tags, which is
4554 * very slow. Get matches for "help" instead. */
4555 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4556 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
4558#ifdef FEAT_MULTI_LANG
4559 cleanup_help_tags(*num_file, *file);
4560#endif
4561 return OK;
4562 }
4563 return FAIL;
4564 }
4565
4566#ifndef FEAT_CMDL_COMPL
4567 return FAIL;
4568#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004569 if (xp->xp_context == EXPAND_SHELLCMD)
4570 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 if (xp->xp_context == EXPAND_OLD_SETTING)
4572 return ExpandOldSetting(num_file, file);
4573 if (xp->xp_context == EXPAND_BUFFERS)
4574 return ExpandBufnames(pat, num_file, file, options);
4575 if (xp->xp_context == EXPAND_TAGS
4576 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4577 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4578 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004579 {
4580 char *directories[] = {"colors", NULL};
4581 return ExpandRTDir(pat, num_file, file, directories);
4582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004584 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004585 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004586 return ExpandRTDir(pat, num_file, file, directories);
4587 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004588 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004589 {
4590 char *directories[] = {"syntax", NULL};
4591 return ExpandRTDir(pat, num_file, file, directories);
4592 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004593 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004594 {
4595 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4596 return ExpandRTDir(pat, num_file, file, directories);
4597 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004598# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4599 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004600 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004601# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
4603 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4604 if (regmatch.regprog == NULL)
4605 return FAIL;
4606
4607 /* set ignore-case according to p_ic, p_scs and pat */
4608 regmatch.rm_ic = ignorecase(pat);
4609
4610 if (xp->xp_context == EXPAND_SETTINGS
4611 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4612 ret = ExpandSettings(xp, &regmatch, num_file, file);
4613 else if (xp->xp_context == EXPAND_MAPPINGS)
4614 ret = ExpandMappings(&regmatch, num_file, file);
4615# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4616 else if (xp->xp_context == EXPAND_USER_DEFINED)
4617 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4618# endif
4619 else
4620 {
4621 static struct expgen
4622 {
4623 int context;
4624 char_u *((*func)__ARGS((expand_T *, int)));
4625 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004626 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 } tab[] =
4628 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004629 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4630 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004632 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4633 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4634 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4635 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636#endif
4637#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004638 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4639 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4640 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4641 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642#endif
4643#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004644 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4645 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646#endif
4647#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004648 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004650 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004652 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4653 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004655#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004656 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004657#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004658#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004659 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004660#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004661#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004662 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4665 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004666 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4667 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004669 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 };
4671 int i;
4672
4673 /*
4674 * Find a context in the table and call the ExpandGeneric() with the
4675 * right function to do the expansion.
4676 */
4677 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004678 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if (xp->xp_context == tab[i].context)
4680 {
4681 if (tab[i].ic)
4682 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004683 ret = ExpandGeneric(xp, &regmatch, num_file, file,
4684 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 break;
4686 }
4687 }
4688
4689 vim_free(regmatch.regprog);
4690
4691 return ret;
4692#endif /* FEAT_CMDL_COMPL */
4693}
4694
4695#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4696/*
4697 * Expand a list of names.
4698 *
4699 * Generic function for command line completion. It calls a function to
4700 * obtain strings, one by one. The strings are matched against a regexp
4701 * program. Matching strings are copied into an array, which is returned.
4702 *
4703 * Returns OK when no problems encountered, FAIL for error (out of memory).
4704 */
4705 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004706ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 expand_T *xp;
4708 regmatch_T *regmatch;
4709 int *num_file;
4710 char_u ***file;
4711 char_u *((*func)__ARGS((expand_T *, int)));
4712 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004713 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 int i;
4716 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004717 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 char_u *str;
4719
4720 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004721 * round == 0: count the number of matching names
4722 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004724 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
4726 for (i = 0; ; ++i)
4727 {
4728 str = (*func)(xp, i);
4729 if (str == NULL) /* end of list */
4730 break;
4731 if (*str == NUL) /* skip empty strings */
4732 continue;
4733
4734 if (vim_regexec(regmatch, str, (colnr_T)0))
4735 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004736 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004738 if (escaped)
4739 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4740 else
4741 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 (*file)[count] = str;
4743#ifdef FEAT_MENU
4744 if (func == get_menu_names && str != NULL)
4745 {
4746 /* test for separator added by get_menu_names() */
4747 str += STRLEN(str) - 1;
4748 if (*str == '\001')
4749 *str = '.';
4750 }
4751#endif
4752 }
4753 ++count;
4754 }
4755 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004756 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
4758 if (count == 0)
4759 return OK;
4760 *num_file = count;
4761 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4762 if (*file == NULL)
4763 {
4764 *file = (char_u **)"";
4765 return FAIL;
4766 }
4767 count = 0;
4768 }
4769 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004770
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004771 /* Sort the results. Keep menu's in the specified order. */
4772 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004773 {
4774 if (xp->xp_context == EXPAND_EXPRESSION
4775 || xp->xp_context == EXPAND_FUNCTIONS
4776 || xp->xp_context == EXPAND_USER_FUNC)
4777 /* <SNR> functions should be sorted to the end. */
4778 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4779 sort_func_compare);
4780 else
4781 sort_strings(*file, *num_file);
4782 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004783
Bram Moolenaar4f688582007-07-24 12:34:30 +00004784#ifdef FEAT_CMDL_COMPL
4785 /* Reset the variables used for special highlight names expansion, so that
4786 * they don't show up when getting normal highlight names by ID. */
4787 reset_expand_highlight();
4788#endif
4789
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 return OK;
4791}
4792
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004793/*
4794 * Complete a shell command.
4795 * Returns FAIL or OK;
4796 */
4797 static int
4798expand_shellcmd(filepat, num_file, file, flagsarg)
4799 char_u *filepat; /* pattern to match with command names */
4800 int *num_file; /* return: number of matches */
4801 char_u ***file; /* return: array with matches */
4802 int flagsarg; /* EW_ flags */
4803{
4804 char_u *pat;
4805 int i;
4806 char_u *path;
4807 int mustfree = FALSE;
4808 garray_T ga;
4809 char_u *buf = alloc(MAXPATHL);
4810 size_t l;
4811 char_u *s, *e;
4812 int flags = flagsarg;
4813 int ret;
4814
4815 if (buf == NULL)
4816 return FAIL;
4817
4818 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4819 * space */
4820 pat = vim_strsave(filepat);
4821 for (i = 0; pat[i]; ++i)
4822 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004823 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004824
4825 flags |= EW_FILE | EW_EXEC;
4826
4827 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004828 if (mch_isFullName(pat))
4829 path = (char_u *)" ";
4830 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004831 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4832 path = (char_u *)".";
4833 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004834 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004835 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004836 if (path == NULL)
4837 path = (char_u *)"";
4838 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004839
4840 /*
4841 * Go over all directories in $PATH. Expand matches in that directory and
4842 * collect them in "ga".
4843 */
4844 ga_init2(&ga, (int)sizeof(char *), 10);
4845 for (s = path; *s != NUL; s = e)
4846 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004847 if (*s == ' ')
4848 ++s; /* Skip space used for absolute path name. */
4849
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004850#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4851 e = vim_strchr(s, ';');
4852#else
4853 e = vim_strchr(s, ':');
4854#endif
4855 if (e == NULL)
4856 e = s + STRLEN(s);
4857
4858 l = e - s;
4859 if (l > MAXPATHL - 5)
4860 break;
4861 vim_strncpy(buf, s, l);
4862 add_pathsep(buf);
4863 l = STRLEN(buf);
4864 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4865
4866 /* Expand matches in one directory of $PATH. */
4867 ret = expand_wildcards(1, &buf, num_file, file, flags);
4868 if (ret == OK)
4869 {
4870 if (ga_grow(&ga, *num_file) == FAIL)
4871 FreeWild(*num_file, *file);
4872 else
4873 {
4874 for (i = 0; i < *num_file; ++i)
4875 {
4876 s = (*file)[i];
4877 if (STRLEN(s) > l)
4878 {
4879 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004880 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004881 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4882 }
4883 else
4884 vim_free(s);
4885 }
4886 vim_free(*file);
4887 }
4888 }
4889 if (*e != NUL)
4890 ++e;
4891 }
4892 *file = ga.ga_data;
4893 *num_file = ga.ga_len;
4894
4895 vim_free(buf);
4896 vim_free(pat);
4897 if (mustfree)
4898 vim_free(path);
4899 return OK;
4900}
4901
4902
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004904static 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));
4905
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004907 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004908 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004910 static void *
4911call_user_expand_func(user_expand_func, xp, num_file, file)
4912 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 int *num_file;
4915 char_u ***file;
4916{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004917 char_u keep;
4918 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004921 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004922 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923
4924 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004925 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 *num_file = 0;
4927 *file = NULL;
4928
Bram Moolenaar21669c02008-01-18 12:16:16 +00004929 if (ccline.cmdbuff == NULL)
4930 {
4931 /* Completion from Insert mode, pass fake arguments. */
4932 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004933 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004934 args[1] = xp->xp_pattern;
4935 }
4936 else
4937 {
4938 /* Completion on the command line, pass real arguments. */
4939 keep = ccline.cmdbuff[ccline.cmdlen];
4940 ccline.cmdbuff[ccline.cmdlen] = 0;
4941 sprintf((char *)num, "%d", ccline.cmdpos);
4942 args[1] = ccline.cmdbuff;
4943 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004944 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 args[2] = num;
4946
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004947 /* Save the cmdline, we don't know what the function may do. */
4948 save_ccline = ccline;
4949 ccline.cmdbuff = NULL;
4950 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004952
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004953 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004954
4955 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004957 if (ccline.cmdbuff != NULL)
4958 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004959
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004960 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004961 return ret;
4962}
4963
4964/*
4965 * Expand names with a function defined by the user.
4966 */
4967 static int
4968ExpandUserDefined(xp, regmatch, num_file, file)
4969 expand_T *xp;
4970 regmatch_T *regmatch;
4971 int *num_file;
4972 char_u ***file;
4973{
4974 char_u *retstr;
4975 char_u *s;
4976 char_u *e;
4977 char_u keep;
4978 garray_T ga;
4979
4980 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4981 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 return FAIL;
4983
4984 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004985 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 {
4987 e = vim_strchr(s, '\n');
4988 if (e == NULL)
4989 e = s + STRLEN(s);
4990 keep = *e;
4991 *e = 0;
4992
4993 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4994 {
4995 *e = keep;
4996 if (*e != NUL)
4997 ++e;
4998 continue;
4999 }
5000
5001 if (ga_grow(&ga, 1) == FAIL)
5002 break;
5003
5004 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5005 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006
5007 *e = keep;
5008 if (*e != NUL)
5009 ++e;
5010 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005011 vim_free(retstr);
5012 *file = ga.ga_data;
5013 *num_file = ga.ga_len;
5014 return OK;
5015}
5016
5017/*
5018 * Expand names with a list returned by a function defined by the user.
5019 */
5020 static int
5021ExpandUserList(xp, num_file, file)
5022 expand_T *xp;
5023 int *num_file;
5024 char_u ***file;
5025{
5026 list_T *retlist;
5027 listitem_T *li;
5028 garray_T ga;
5029
5030 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5031 if (retlist == NULL)
5032 return FAIL;
5033
5034 ga_init2(&ga, (int)sizeof(char *), 3);
5035 /* Loop over the items in the list. */
5036 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5037 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005038 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5039 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005040
5041 if (ga_grow(&ga, 1) == FAIL)
5042 break;
5043
5044 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005045 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005046 ++ga.ga_len;
5047 }
5048 list_unref(retlist);
5049
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 *file = ga.ga_data;
5051 *num_file = ga.ga_len;
5052 return OK;
5053}
5054#endif
5055
5056/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005057 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005058 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005059 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 */
5061 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005062ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 char_u *pat;
5064 int *num_file;
5065 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005066 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005068 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 char_u *s;
5070 char_u *e;
5071 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005072 int i;
5073 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
5075 *num_file = 0;
5076 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005077 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005078 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005080 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005082 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5083 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005085 ga_clear_strings(&ga);
5086 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005088 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5089 matches = globpath(p_rtp, s, 0);
5090 vim_free(s);
5091 if (matches == NULL)
5092 continue;
5093
5094 for (s = matches; *s != NUL; s = e)
5095 {
5096 e = vim_strchr(s, '\n');
5097 if (e == NULL)
5098 e = s + STRLEN(s);
5099 if (ga_grow(&ga, 1) == FAIL)
5100 break;
5101 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5102 {
5103 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5104 if (*s == '\n' || vim_ispathsep(*s))
5105 break;
5106 ++s;
5107 ((char_u **)ga.ga_data)[ga.ga_len] =
5108 vim_strnsave(s, (int)(e - s - 4));
5109 ++ga.ga_len;
5110 }
5111 if (*e != NUL)
5112 ++e;
5113 }
5114 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005116 if (ga.ga_len == 0)
5117 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005118
5119 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005120 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005121 remove_duplicates(&ga);
5122
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 *file = ga.ga_data;
5124 *num_file = ga.ga_len;
5125 return OK;
5126}
5127
5128#endif
5129
5130#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5131/*
5132 * Expand "file" for all comma-separated directories in "path".
5133 * Returns an allocated string with all matches concatenated, separated by
5134 * newlines. Returns NULL for an error or no matches.
5135 */
5136 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005137globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 char_u *path;
5139 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005140 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
5142 expand_T xpc;
5143 char_u *buf;
5144 garray_T ga;
5145 int i;
5146 int len;
5147 int num_p;
5148 char_u **p;
5149 char_u *cur = NULL;
5150
5151 buf = alloc(MAXPATHL);
5152 if (buf == NULL)
5153 return NULL;
5154
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005155 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005157
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 ga_init2(&ga, 1, 100);
5159
5160 /* Loop over all entries in {path}. */
5161 while (*path != NUL)
5162 {
5163 /* Copy one item of the path to buf[] and concatenate the file name. */
5164 copy_option_part(&path, buf, MAXPATHL, ",");
5165 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5166 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005167# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005168 /* Using the platform's path separator (\) makes vim incorrectly
5169 * treat it as an escape character, use '/' instead. */
5170 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5171 STRCAT(buf, "/");
5172# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005176 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5177 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005179 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005181 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182
5183 /* Concatenate new results to previous ones. */
5184 if (ga_grow(&ga, len) == OK)
5185 {
5186 cur = (char_u *)ga.ga_data + ga.ga_len;
5187 for (i = 0; i < num_p; ++i)
5188 {
5189 STRCPY(cur, p[i]);
5190 cur += STRLEN(p[i]);
5191 *cur++ = '\n';
5192 }
5193 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 FreeWild(num_p, p);
5196 }
5197 }
5198 }
5199 if (cur != NULL)
5200 *--cur = 0; /* Replace trailing newline with NUL */
5201
5202 vim_free(buf);
5203 return (char_u *)ga.ga_data;
5204}
5205
5206#endif
5207
5208#if defined(FEAT_CMDHIST) || defined(PROTO)
5209
5210/*********************************
5211 * Command line history stuff *
5212 *********************************/
5213
5214/*
5215 * Translate a history character to the associated type number.
5216 */
5217 static int
5218hist_char2type(c)
5219 int c;
5220{
5221 if (c == ':')
5222 return HIST_CMD;
5223 if (c == '=')
5224 return HIST_EXPR;
5225 if (c == '@')
5226 return HIST_INPUT;
5227 if (c == '>')
5228 return HIST_DEBUG;
5229 return HIST_SEARCH; /* must be '?' or '/' */
5230}
5231
5232/*
5233 * Table of history names.
5234 * These names are used in :history and various hist...() functions.
5235 * It is sufficient to give the significant prefix of a history name.
5236 */
5237
5238static char *(history_names[]) =
5239{
5240 "cmd",
5241 "search",
5242 "expr",
5243 "input",
5244 "debug",
5245 NULL
5246};
5247
5248/*
5249 * init_history() - Initialize the command line history.
5250 * Also used to re-allocate the history when the size changes.
5251 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005252 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253init_history()
5254{
5255 int newlen; /* new length of history table */
5256 histentry_T *temp;
5257 int i;
5258 int j;
5259 int type;
5260
5261 /*
5262 * If size of history table changed, reallocate it
5263 */
5264 newlen = (int)p_hi;
5265 if (newlen != hislen) /* history length changed */
5266 {
5267 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5268 {
5269 if (newlen)
5270 {
5271 temp = (histentry_T *)lalloc(
5272 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5273 if (temp == NULL) /* out of memory! */
5274 {
5275 if (type == 0) /* first one: just keep the old length */
5276 {
5277 newlen = hislen;
5278 break;
5279 }
5280 /* Already changed one table, now we can only have zero
5281 * length for all tables. */
5282 newlen = 0;
5283 type = -1;
5284 continue;
5285 }
5286 }
5287 else
5288 temp = NULL;
5289 if (newlen == 0 || temp != NULL)
5290 {
5291 if (hisidx[type] < 0) /* there are no entries yet */
5292 {
5293 for (i = 0; i < newlen; ++i)
5294 {
5295 temp[i].hisnum = 0;
5296 temp[i].hisstr = NULL;
5297 }
5298 }
5299 else if (newlen > hislen) /* array becomes bigger */
5300 {
5301 for (i = 0; i <= hisidx[type]; ++i)
5302 temp[i] = history[type][i];
5303 j = i;
5304 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5305 {
5306 temp[i].hisnum = 0;
5307 temp[i].hisstr = NULL;
5308 }
5309 for ( ; j < hislen; ++i, ++j)
5310 temp[i] = history[type][j];
5311 }
5312 else /* array becomes smaller or 0 */
5313 {
5314 j = hisidx[type];
5315 for (i = newlen - 1; ; --i)
5316 {
5317 if (i >= 0) /* copy newest entries */
5318 temp[i] = history[type][j];
5319 else /* remove older entries */
5320 vim_free(history[type][j].hisstr);
5321 if (--j < 0)
5322 j = hislen - 1;
5323 if (j == hisidx[type])
5324 break;
5325 }
5326 hisidx[type] = newlen - 1;
5327 }
5328 vim_free(history[type]);
5329 history[type] = temp;
5330 }
5331 }
5332 hislen = newlen;
5333 }
5334}
5335
5336/*
5337 * Check if command line 'str' is already in history.
5338 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5339 */
5340 static int
Bram Moolenaar4c402232011-07-27 17:58:46 +02005341in_history(type, str, move_to_front, sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 int type;
5343 char_u *str;
5344 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005345 int sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 int i;
5348 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005349 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
5351 if (hisidx[type] < 0)
5352 return FALSE;
5353 i = hisidx[type];
5354 do
5355 {
5356 if (history[type][i].hisstr == NULL)
5357 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005358
5359 /* For search history, check that the separator character matches as
5360 * well. */
5361 p = history[type][i].hisstr;
5362 if (STRCMP(str, p) == 0
5363 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 {
5365 if (!move_to_front)
5366 return TRUE;
5367 last_i = i;
5368 break;
5369 }
5370 if (--i < 0)
5371 i = hislen - 1;
5372 } while (i != hisidx[type]);
5373
5374 if (last_i >= 0)
5375 {
5376 str = history[type][i].hisstr;
5377 while (i != hisidx[type])
5378 {
5379 if (++i >= hislen)
5380 i = 0;
5381 history[type][last_i] = history[type][i];
5382 last_i = i;
5383 }
5384 history[type][i].hisstr = str;
5385 history[type][i].hisnum = ++hisnum[type];
5386 return TRUE;
5387 }
5388 return FALSE;
5389}
5390
5391/*
5392 * Convert history name (from table above) to its HIST_ equivalent.
5393 * When "name" is empty, return "cmd" history.
5394 * Returns -1 for unknown history name.
5395 */
5396 int
5397get_histtype(name)
5398 char_u *name;
5399{
5400 int i;
5401 int len = (int)STRLEN(name);
5402
5403 /* No argument: use current history. */
5404 if (len == 0)
5405 return hist_char2type(ccline.cmdfirstc);
5406
5407 for (i = 0; history_names[i] != NULL; ++i)
5408 if (STRNICMP(name, history_names[i], len) == 0)
5409 return i;
5410
5411 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5412 return hist_char2type(name[0]);
5413
5414 return -1;
5415}
5416
5417static int last_maptick = -1; /* last seen maptick */
5418
5419/*
5420 * Add the given string to the given history. If the string is already in the
5421 * history then it is moved to the front. "histype" may be one of he HIST_
5422 * values.
5423 */
5424 void
5425add_to_history(histype, new_entry, in_map, sep)
5426 int histype;
5427 char_u *new_entry;
5428 int in_map; /* consider maptick when inside a mapping */
5429 int sep; /* separator character used (search hist) */
5430{
5431 histentry_T *hisptr;
5432 int len;
5433
5434 if (hislen == 0) /* no history */
5435 return;
5436
5437 /*
5438 * Searches inside the same mapping overwrite each other, so that only
5439 * the last line is kept. Be careful not to remove a line that was moved
5440 * down, only lines that were added.
5441 */
5442 if (histype == HIST_SEARCH && in_map)
5443 {
5444 if (maptick == last_maptick)
5445 {
5446 /* Current line is from the same mapping, remove it */
5447 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5448 vim_free(hisptr->hisstr);
5449 hisptr->hisstr = NULL;
5450 hisptr->hisnum = 0;
5451 --hisnum[histype];
5452 if (--hisidx[HIST_SEARCH] < 0)
5453 hisidx[HIST_SEARCH] = hislen - 1;
5454 }
5455 last_maptick = -1;
5456 }
Bram Moolenaar4c402232011-07-27 17:58:46 +02005457 if (!in_history(histype, new_entry, TRUE, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 {
5459 if (++hisidx[histype] == hislen)
5460 hisidx[histype] = 0;
5461 hisptr = &history[histype][hisidx[histype]];
5462 vim_free(hisptr->hisstr);
5463
5464 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005465 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5467 if (hisptr->hisstr != NULL)
5468 hisptr->hisstr[len + 1] = sep;
5469
5470 hisptr->hisnum = ++hisnum[histype];
5471 if (histype == HIST_SEARCH && in_map)
5472 last_maptick = maptick;
5473 }
5474}
5475
5476#if defined(FEAT_EVAL) || defined(PROTO)
5477
5478/*
5479 * Get identifier of newest history entry.
5480 * "histype" may be one of the HIST_ values.
5481 */
5482 int
5483get_history_idx(histype)
5484 int histype;
5485{
5486 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5487 || hisidx[histype] < 0)
5488 return -1;
5489
5490 return history[histype][hisidx[histype]].hisnum;
5491}
5492
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005493static struct cmdline_info *get_ccline_ptr __ARGS((void));
5494
5495/*
5496 * Get pointer to the command line info to use. cmdline_paste() may clear
5497 * ccline and put the previous value in prev_ccline.
5498 */
5499 static struct cmdline_info *
5500get_ccline_ptr()
5501{
5502 if ((State & CMDLINE) == 0)
5503 return NULL;
5504 if (ccline.cmdbuff != NULL)
5505 return &ccline;
5506 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5507 return &prev_ccline;
5508 return NULL;
5509}
5510
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511/*
5512 * Get the current command line in allocated memory.
5513 * Only works when the command line is being edited.
5514 * Returns NULL when something is wrong.
5515 */
5516 char_u *
5517get_cmdline_str()
5518{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005519 struct cmdline_info *p = get_ccline_ptr();
5520
5521 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005523 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524}
5525
5526/*
5527 * Get the current command line position, counted in bytes.
5528 * Zero is the first position.
5529 * Only works when the command line is being edited.
5530 * Returns -1 when something is wrong.
5531 */
5532 int
5533get_cmdline_pos()
5534{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005535 struct cmdline_info *p = get_ccline_ptr();
5536
5537 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005539 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540}
5541
5542/*
5543 * Set the command line byte position to "pos". Zero is the first position.
5544 * Only works when the command line is being edited.
5545 * Returns 1 when failed, 0 when OK.
5546 */
5547 int
5548set_cmdline_pos(pos)
5549 int pos;
5550{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005551 struct cmdline_info *p = get_ccline_ptr();
5552
5553 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 return 1;
5555
5556 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5557 * changed the command line. */
5558 if (pos < 0)
5559 new_cmdpos = 0;
5560 else
5561 new_cmdpos = pos;
5562 return 0;
5563}
5564
5565/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005566 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005567 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005568 * Only works when the command line is being edited.
5569 * Returns NUL when something is wrong.
5570 */
5571 int
5572get_cmdline_type()
5573{
5574 struct cmdline_info *p = get_ccline_ptr();
5575
5576 if (p == NULL)
5577 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005578 if (p->cmdfirstc == NUL)
5579 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005580 return p->cmdfirstc;
5581}
5582
5583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 * Calculate history index from a number:
5585 * num > 0: seen as identifying number of a history entry
5586 * num < 0: relative position in history wrt newest entry
5587 * "histype" may be one of the HIST_ values.
5588 */
5589 static int
5590calc_hist_idx(histype, num)
5591 int histype;
5592 int num;
5593{
5594 int i;
5595 histentry_T *hist;
5596 int wrapped = FALSE;
5597
5598 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5599 || (i = hisidx[histype]) < 0 || num == 0)
5600 return -1;
5601
5602 hist = history[histype];
5603 if (num > 0)
5604 {
5605 while (hist[i].hisnum > num)
5606 if (--i < 0)
5607 {
5608 if (wrapped)
5609 break;
5610 i += hislen;
5611 wrapped = TRUE;
5612 }
5613 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5614 return i;
5615 }
5616 else if (-num <= hislen)
5617 {
5618 i += num + 1;
5619 if (i < 0)
5620 i += hislen;
5621 if (hist[i].hisstr != NULL)
5622 return i;
5623 }
5624 return -1;
5625}
5626
5627/*
5628 * Get a history entry by its index.
5629 * "histype" may be one of the HIST_ values.
5630 */
5631 char_u *
5632get_history_entry(histype, idx)
5633 int histype;
5634 int idx;
5635{
5636 idx = calc_hist_idx(histype, idx);
5637 if (idx >= 0)
5638 return history[histype][idx].hisstr;
5639 else
5640 return (char_u *)"";
5641}
5642
5643/*
5644 * Clear all entries of a history.
5645 * "histype" may be one of the HIST_ values.
5646 */
5647 int
5648clr_history(histype)
5649 int histype;
5650{
5651 int i;
5652 histentry_T *hisptr;
5653
5654 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5655 {
5656 hisptr = history[histype];
5657 for (i = hislen; i--;)
5658 {
5659 vim_free(hisptr->hisstr);
5660 hisptr->hisnum = 0;
5661 hisptr++->hisstr = NULL;
5662 }
5663 hisidx[histype] = -1; /* mark history as cleared */
5664 hisnum[histype] = 0; /* reset identifier counter */
5665 return OK;
5666 }
5667 return FAIL;
5668}
5669
5670/*
5671 * Remove all entries matching {str} from a history.
5672 * "histype" may be one of the HIST_ values.
5673 */
5674 int
5675del_history_entry(histype, str)
5676 int histype;
5677 char_u *str;
5678{
5679 regmatch_T regmatch;
5680 histentry_T *hisptr;
5681 int idx;
5682 int i;
5683 int last;
5684 int found = FALSE;
5685
5686 regmatch.regprog = NULL;
5687 regmatch.rm_ic = FALSE; /* always match case */
5688 if (hislen != 0
5689 && histype >= 0
5690 && histype < HIST_COUNT
5691 && *str != NUL
5692 && (idx = hisidx[histype]) >= 0
5693 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5694 != NULL)
5695 {
5696 i = last = idx;
5697 do
5698 {
5699 hisptr = &history[histype][i];
5700 if (hisptr->hisstr == NULL)
5701 break;
5702 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5703 {
5704 found = TRUE;
5705 vim_free(hisptr->hisstr);
5706 hisptr->hisstr = NULL;
5707 hisptr->hisnum = 0;
5708 }
5709 else
5710 {
5711 if (i != last)
5712 {
5713 history[histype][last] = *hisptr;
5714 hisptr->hisstr = NULL;
5715 hisptr->hisnum = 0;
5716 }
5717 if (--last < 0)
5718 last += hislen;
5719 }
5720 if (--i < 0)
5721 i += hislen;
5722 } while (i != idx);
5723 if (history[histype][idx].hisstr == NULL)
5724 hisidx[histype] = -1;
5725 }
5726 vim_free(regmatch.regprog);
5727 return found;
5728}
5729
5730/*
5731 * Remove an indexed entry from a history.
5732 * "histype" may be one of the HIST_ values.
5733 */
5734 int
5735del_history_idx(histype, idx)
5736 int histype;
5737 int idx;
5738{
5739 int i, j;
5740
5741 i = calc_hist_idx(histype, idx);
5742 if (i < 0)
5743 return FALSE;
5744 idx = hisidx[histype];
5745 vim_free(history[histype][i].hisstr);
5746
5747 /* When deleting the last added search string in a mapping, reset
5748 * last_maptick, so that the last added search string isn't deleted again.
5749 */
5750 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5751 last_maptick = -1;
5752
5753 while (i != idx)
5754 {
5755 j = (i + 1) % hislen;
5756 history[histype][i] = history[histype][j];
5757 i = j;
5758 }
5759 history[histype][i].hisstr = NULL;
5760 history[histype][i].hisnum = 0;
5761 if (--i < 0)
5762 i += hislen;
5763 hisidx[histype] = i;
5764 return TRUE;
5765}
5766
5767#endif /* FEAT_EVAL */
5768
5769#if defined(FEAT_CRYPT) || defined(PROTO)
5770/*
5771 * Very specific function to remove the value in ":set key=val" from the
5772 * history.
5773 */
5774 void
5775remove_key_from_history()
5776{
5777 char_u *p;
5778 int i;
5779
5780 i = hisidx[HIST_CMD];
5781 if (i < 0)
5782 return;
5783 p = history[HIST_CMD][i].hisstr;
5784 if (p != NULL)
5785 for ( ; *p; ++p)
5786 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5787 {
5788 p = vim_strchr(p + 3, '=');
5789 if (p == NULL)
5790 break;
5791 ++p;
5792 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5793 if (p[i] == '\\' && p[i + 1])
5794 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005795 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 --p;
5797 }
5798}
5799#endif
5800
5801#endif /* FEAT_CMDHIST */
5802
5803#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5804/*
5805 * Get indices "num1,num2" that specify a range within a list (not a range of
5806 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5807 * Returns OK if parsed successfully, otherwise FAIL.
5808 */
5809 int
5810get_list_range(str, num1, num2)
5811 char_u **str;
5812 int *num1;
5813 int *num2;
5814{
5815 int len;
5816 int first = FALSE;
5817 long num;
5818
5819 *str = skipwhite(*str);
5820 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5821 {
5822 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5823 *str += len;
5824 *num1 = (int)num;
5825 first = TRUE;
5826 }
5827 *str = skipwhite(*str);
5828 if (**str == ',') /* parse "to" part of range */
5829 {
5830 *str = skipwhite(*str + 1);
5831 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5832 if (len > 0)
5833 {
5834 *num2 = (int)num;
5835 *str = skipwhite(*str + len);
5836 }
5837 else if (!first) /* no number given at all */
5838 return FAIL;
5839 }
5840 else if (first) /* only one number given */
5841 *num2 = *num1;
5842 return OK;
5843}
5844#endif
5845
5846#if defined(FEAT_CMDHIST) || defined(PROTO)
5847/*
5848 * :history command - print a history
5849 */
5850 void
5851ex_history(eap)
5852 exarg_T *eap;
5853{
5854 histentry_T *hist;
5855 int histype1 = HIST_CMD;
5856 int histype2 = HIST_CMD;
5857 int hisidx1 = 1;
5858 int hisidx2 = -1;
5859 int idx;
5860 int i, j, k;
5861 char_u *end;
5862 char_u *arg = eap->arg;
5863
5864 if (hislen == 0)
5865 {
5866 MSG(_("'history' option is zero"));
5867 return;
5868 }
5869
5870 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5871 {
5872 end = arg;
5873 while (ASCII_ISALPHA(*end)
5874 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5875 end++;
5876 i = *end;
5877 *end = NUL;
5878 histype1 = get_histtype(arg);
5879 if (histype1 == -1)
5880 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005881 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 {
5883 histype1 = 0;
5884 histype2 = HIST_COUNT-1;
5885 }
5886 else
5887 {
5888 *end = i;
5889 EMSG(_(e_trailing));
5890 return;
5891 }
5892 }
5893 else
5894 histype2 = histype1;
5895 *end = i;
5896 }
5897 else
5898 end = arg;
5899 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5900 {
5901 EMSG(_(e_trailing));
5902 return;
5903 }
5904
5905 for (; !got_int && histype1 <= histype2; ++histype1)
5906 {
5907 STRCPY(IObuff, "\n # ");
5908 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5909 MSG_PUTS_TITLE(IObuff);
5910 idx = hisidx[histype1];
5911 hist = history[histype1];
5912 j = hisidx1;
5913 k = hisidx2;
5914 if (j < 0)
5915 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5916 if (k < 0)
5917 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5918 if (idx >= 0 && j <= k)
5919 for (i = idx + 1; !got_int; ++i)
5920 {
5921 if (i == hislen)
5922 i = 0;
5923 if (hist[i].hisstr != NULL
5924 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5925 {
5926 msg_putchar('\n');
5927 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5928 hist[i].hisnum);
5929 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5930 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005931 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 else
5933 STRCAT(IObuff, hist[i].hisstr);
5934 msg_outtrans(IObuff);
5935 out_flush();
5936 }
5937 if (i == idx)
5938 break;
5939 }
5940 }
5941}
5942#endif
5943
5944#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5945static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5946static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5947static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5948static int viminfo_add_at_front = FALSE;
5949
5950static int hist_type2char __ARGS((int type, int use_question));
5951
5952/*
5953 * Translate a history type number to the associated character.
5954 */
5955 static int
5956hist_type2char(type, use_question)
5957 int type;
5958 int use_question; /* use '?' instead of '/' */
5959{
5960 if (type == HIST_CMD)
5961 return ':';
5962 if (type == HIST_SEARCH)
5963 {
5964 if (use_question)
5965 return '?';
5966 else
5967 return '/';
5968 }
5969 if (type == HIST_EXPR)
5970 return '=';
5971 return '@';
5972}
5973
5974/*
5975 * Prepare for reading the history from the viminfo file.
5976 * This allocates history arrays to store the read history lines.
5977 */
5978 void
5979prepare_viminfo_history(asklen)
5980 int asklen;
5981{
5982 int i;
5983 int num;
5984 int type;
5985 int len;
5986
5987 init_history();
5988 viminfo_add_at_front = (asklen != 0);
5989 if (asklen > hislen)
5990 asklen = hislen;
5991
5992 for (type = 0; type < HIST_COUNT; ++type)
5993 {
5994 /*
5995 * Count the number of empty spaces in the history list. If there are
5996 * more spaces available than we request, then fill them up.
5997 */
5998 for (i = 0, num = 0; i < hislen; i++)
5999 if (history[type][i].hisstr == NULL)
6000 num++;
6001 len = asklen;
6002 if (num > len)
6003 len = num;
6004 if (len <= 0)
6005 viminfo_history[type] = NULL;
6006 else
6007 viminfo_history[type] =
6008 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6009 if (viminfo_history[type] == NULL)
6010 len = 0;
6011 viminfo_hislen[type] = len;
6012 viminfo_hisidx[type] = 0;
6013 }
6014}
6015
6016/*
6017 * Accept a line from the viminfo, store it in the history array when it's
6018 * new.
6019 */
6020 int
6021read_viminfo_history(virp)
6022 vir_T *virp;
6023{
6024 int type;
6025 long_u len;
6026 char_u *val;
6027 char_u *p;
6028
6029 type = hist_char2type(virp->vir_line[0]);
6030 if (viminfo_hisidx[type] < viminfo_hislen[type])
6031 {
6032 val = viminfo_readstring(virp, 1, TRUE);
6033 if (val != NULL && *val != NUL)
6034 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006035 int sep = (*val == ' ' ? NUL : *val);
6036
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006038 viminfo_add_at_front, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 {
6040 /* Need to re-allocate to append the separator byte. */
6041 len = STRLEN(val);
6042 p = lalloc(len + 2, TRUE);
6043 if (p != NULL)
6044 {
6045 if (type == HIST_SEARCH)
6046 {
6047 /* Search entry: Move the separator from the first
6048 * column to after the NUL. */
6049 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006050 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 }
6052 else
6053 {
6054 /* Not a search entry: No separator in the viminfo
6055 * file, add a NUL separator. */
6056 mch_memmove(p, val, (size_t)len + 1);
6057 p[len + 1] = NUL;
6058 }
6059 viminfo_history[type][viminfo_hisidx[type]++] = p;
6060 }
6061 }
6062 }
6063 vim_free(val);
6064 }
6065 return viminfo_readline(virp);
6066}
6067
6068 void
6069finish_viminfo_history()
6070{
6071 int idx;
6072 int i;
6073 int type;
6074
6075 for (type = 0; type < HIST_COUNT; ++type)
6076 {
6077 if (history[type] == NULL)
6078 return;
6079 idx = hisidx[type] + viminfo_hisidx[type];
6080 if (idx >= hislen)
6081 idx -= hislen;
6082 else if (idx < 0)
6083 idx = hislen - 1;
6084 if (viminfo_add_at_front)
6085 hisidx[type] = idx;
6086 else
6087 {
6088 if (hisidx[type] == -1)
6089 hisidx[type] = hislen - 1;
6090 do
6091 {
6092 if (history[type][idx].hisstr != NULL)
6093 break;
6094 if (++idx == hislen)
6095 idx = 0;
6096 } while (idx != hisidx[type]);
6097 if (idx != hisidx[type] && --idx < 0)
6098 idx = hislen - 1;
6099 }
6100 for (i = 0; i < viminfo_hisidx[type]; i++)
6101 {
6102 vim_free(history[type][idx].hisstr);
6103 history[type][idx].hisstr = viminfo_history[type][i];
6104 if (--idx < 0)
6105 idx = hislen - 1;
6106 }
6107 idx += 1;
6108 idx %= hislen;
6109 for (i = 0; i < viminfo_hisidx[type]; i++)
6110 {
6111 history[type][idx++].hisnum = ++hisnum[type];
6112 idx %= hislen;
6113 }
6114 vim_free(viminfo_history[type]);
6115 viminfo_history[type] = NULL;
6116 }
6117}
6118
6119 void
6120write_viminfo_history(fp)
6121 FILE *fp;
6122{
6123 int i;
6124 int type;
6125 int num_saved;
6126 char_u *p;
6127 int c;
6128
6129 init_history();
6130 if (hislen == 0)
6131 return;
6132 for (type = 0; type < HIST_COUNT; ++type)
6133 {
6134 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6135 if (num_saved == 0)
6136 continue;
6137 if (num_saved < 0) /* Use default */
6138 num_saved = hislen;
6139 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6140 type == HIST_CMD ? _("Command Line") :
6141 type == HIST_SEARCH ? _("Search String") :
6142 type == HIST_EXPR ? _("Expression") :
6143 _("Input Line"));
6144 if (num_saved > hislen)
6145 num_saved = hislen;
6146 i = hisidx[type];
6147 if (i >= 0)
6148 while (num_saved--)
6149 {
6150 p = history[type][i].hisstr;
6151 if (p != NULL)
6152 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006153 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 /* For the search history: put the separator in the second
6155 * column; use a space if there isn't one. */
6156 if (type == HIST_SEARCH)
6157 {
6158 c = p[STRLEN(p) + 1];
6159 putc(c == NUL ? ' ' : c, fp);
6160 }
6161 viminfo_writestring(fp, p);
6162 }
6163 if (--i < 0)
6164 i = hislen - 1;
6165 }
6166 }
6167}
6168#endif /* FEAT_VIMINFO */
6169
6170#if defined(FEAT_FKMAP) || defined(PROTO)
6171/*
6172 * Write a character at the current cursor+offset position.
6173 * It is directly written into the command buffer block.
6174 */
6175 void
6176cmd_pchar(c, offset)
6177 int c, offset;
6178{
6179 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6180 {
6181 EMSG(_("E198: cmd_pchar beyond the command length"));
6182 return;
6183 }
6184 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6185 ccline.cmdbuff[ccline.cmdlen] = NUL;
6186}
6187
6188 int
6189cmd_gchar(offset)
6190 int offset;
6191{
6192 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6193 {
6194 /* EMSG(_("cmd_gchar beyond the command length")); */
6195 return NUL;
6196 }
6197 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6198}
6199#endif
6200
6201#if defined(FEAT_CMDWIN) || defined(PROTO)
6202/*
6203 * Open a window on the current command line and history. Allow editing in
6204 * the window. Returns when the window is closed.
6205 * Returns:
6206 * CR if the command is to be executed
6207 * Ctrl_C if it is to be abandoned
6208 * K_IGNORE if editing continues
6209 */
6210 static int
6211ex_window()
6212{
6213 struct cmdline_info save_ccline;
6214 buf_T *old_curbuf = curbuf;
6215 win_T *old_curwin = curwin;
6216 buf_T *bp;
6217 win_T *wp;
6218 int i;
6219 linenr_T lnum;
6220 int histtype;
6221 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006222#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 int save_restart_edit = restart_edit;
6226 int save_State = State;
6227 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006228#ifdef FEAT_RIGHTLEFT
6229 int save_cmdmsg_rl = cmdmsg_rl;
6230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
6232 /* Can't do this recursively. Can't do it when typing a password. */
6233 if (cmdwin_type != 0
6234# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6235 || cmdline_star > 0
6236# endif
6237 )
6238 {
6239 beep_flush();
6240 return K_IGNORE;
6241 }
6242
6243 /* Save current window sizes. */
6244 win_size_save(&winsizes);
6245
6246# ifdef FEAT_AUTOCMD
6247 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006248 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006250 /* don't use a new tab page */
6251 cmdmod.tab = 0;
6252
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 /* Create a window for the command-line buffer. */
6254 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6255 {
6256 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006257# ifdef FEAT_AUTOCMD
6258 unblock_autocmds();
6259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 return K_IGNORE;
6261 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006262 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263
6264 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006265 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006266 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6268 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6269 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006270#ifdef FEAT_FOLDING
6271 curwin->w_p_fen = FALSE;
6272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006274 curwin->w_p_rl = cmdmsg_rl;
6275 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006277 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278
6279# ifdef FEAT_AUTOCMD
6280 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006281 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282# endif
6283
Bram Moolenaar46152342005-09-07 21:18:43 +00006284 /* Showing the prompt may have set need_wait_return, reset it. */
6285 need_wait_return = FALSE;
6286
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006287 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6289 {
6290 if (p_wc == TAB)
6291 {
6292 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6293 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6294 }
6295 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6296 }
6297
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006298 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6299 * sets 'textwidth' to 78). */
6300 curbuf->b_p_tw = 0;
6301
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 /* Fill the buffer with the history. */
6303 init_history();
6304 if (hislen > 0)
6305 {
6306 i = hisidx[histtype];
6307 if (i >= 0)
6308 {
6309 lnum = 0;
6310 do
6311 {
6312 if (++i == hislen)
6313 i = 0;
6314 if (history[histtype][i].hisstr != NULL)
6315 ml_append(lnum++, history[histtype][i].hisstr,
6316 (colnr_T)0, FALSE);
6317 }
6318 while (i != hisidx[histtype]);
6319 }
6320 }
6321
6322 /* Replace the empty last line with the current command-line and put the
6323 * cursor there. */
6324 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6325 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6326 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006327 changed_line_abv_curs();
6328 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006329 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331 /* Save the command line info, can be used recursively. */
6332 save_ccline = ccline;
6333 ccline.cmdbuff = NULL;
6334 ccline.cmdprompt = NULL;
6335
6336 /* No Ex mode here! */
6337 exmode_active = 0;
6338
6339 State = NORMAL;
6340# ifdef FEAT_MOUSE
6341 setmouse();
6342# endif
6343
6344# ifdef FEAT_AUTOCMD
6345 /* Trigger CmdwinEnter autocommands. */
6346 typestr[0] = cmdwin_type;
6347 typestr[1] = NUL;
6348 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006349 if (restart_edit != 0) /* autocmd with ":startinsert" */
6350 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351# endif
6352
6353 i = RedrawingDisabled;
6354 RedrawingDisabled = 0;
6355
6356 /*
6357 * Call the main loop until <CR> or CTRL-C is typed.
6358 */
6359 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006360 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361
6362 RedrawingDisabled = i;
6363
6364# ifdef FEAT_AUTOCMD
6365 /* Trigger CmdwinLeave autocommands. */
6366 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6367# endif
6368
Bram Moolenaarccc18222007-05-10 18:25:20 +00006369 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 ccline = save_ccline;
6371 cmdwin_type = 0;
6372
6373 exmode_active = save_exmode;
6374
Bram Moolenaarf9821062008-06-20 16:31:07 +00006375 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 * this happens! */
6377 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6378 {
6379 cmdwin_result = Ctrl_C;
6380 EMSG(_("E199: Active window or buffer deleted"));
6381 }
6382 else
6383 {
6384# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6385 /* autocmds may abort script processing */
6386 if (aborting() && cmdwin_result != K_IGNORE)
6387 cmdwin_result = Ctrl_C;
6388# endif
6389 /* Set the new command line from the cmdline buffer. */
6390 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006391 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006393 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6394
6395 if (histtype == HIST_CMD)
6396 {
6397 /* Execute the command directly. */
6398 ccline.cmdbuff = vim_strsave((char_u *)p);
6399 cmdwin_result = CAR;
6400 }
6401 else
6402 {
6403 /* First need to cancel what we were doing. */
6404 ccline.cmdbuff = NULL;
6405 stuffcharReadbuff(':');
6406 stuffReadbuff((char_u *)p);
6407 stuffcharReadbuff(CAR);
6408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 }
6410 else if (cmdwin_result == K_XF2) /* :qa typed */
6411 {
6412 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6413 cmdwin_result = CAR;
6414 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006415 else if (cmdwin_result == Ctrl_C)
6416 {
6417 /* :q or :close, don't execute any command
6418 * and don't modify the cmd window. */
6419 ccline.cmdbuff = NULL;
6420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 else
6422 ccline.cmdbuff = vim_strsave(ml_get_curline());
6423 if (ccline.cmdbuff == NULL)
6424 cmdwin_result = Ctrl_C;
6425 else
6426 {
6427 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6428 ccline.cmdbufflen = ccline.cmdlen + 1;
6429 ccline.cmdpos = curwin->w_cursor.col;
6430 if (ccline.cmdpos > ccline.cmdlen)
6431 ccline.cmdpos = ccline.cmdlen;
6432 if (cmdwin_result == K_IGNORE)
6433 {
6434 set_cmdspos_cursor();
6435 redrawcmd();
6436 }
6437 }
6438
6439# ifdef FEAT_AUTOCMD
6440 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006441 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442# endif
6443 wp = curwin;
6444 bp = curbuf;
6445 win_goto(old_curwin);
6446 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006447
6448 /* win_close() may have already wiped the buffer when 'bh' is
6449 * set to 'wipe' */
6450 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006451 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
6453 /* Restore window sizes. */
6454 win_size_restore(&winsizes);
6455
6456# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006457 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458# endif
6459 }
6460
6461 ga_clear(&winsizes);
6462 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006463# ifdef FEAT_RIGHTLEFT
6464 cmdmsg_rl = save_cmdmsg_rl;
6465# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
6467 State = save_State;
6468# ifdef FEAT_MOUSE
6469 setmouse();
6470# endif
6471
6472 return cmdwin_result;
6473}
6474#endif /* FEAT_CMDWIN */
6475
6476/*
6477 * Used for commands that either take a simple command string argument, or:
6478 * cmd << endmarker
6479 * {script}
6480 * endmarker
6481 * Returns a pointer to allocated memory with {script} or NULL.
6482 */
6483 char_u *
6484script_get(eap, cmd)
6485 exarg_T *eap;
6486 char_u *cmd;
6487{
6488 char_u *theline;
6489 char *end_pattern = NULL;
6490 char dot[] = ".";
6491 garray_T ga;
6492
6493 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6494 return NULL;
6495
6496 ga_init2(&ga, 1, 0x400);
6497
6498 if (cmd[2] != NUL)
6499 end_pattern = (char *)skipwhite(cmd + 2);
6500 else
6501 end_pattern = dot;
6502
6503 for (;;)
6504 {
6505 theline = eap->getline(
6506#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006507 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508#endif
6509 NUL, eap->cookie, 0);
6510
6511 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006512 {
6513 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516
6517 ga_concat(&ga, theline);
6518 ga_append(&ga, '\n');
6519 vim_free(theline);
6520 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006521 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522
6523 return (char_u *)ga.ga_data;
6524}