blob: f811889d31c34df2f675664c0cc113c726dc73bb [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
3464 *
3465 * options = WILD_LIST_NOTFOUND: list entries without a match
3466 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3467 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3468 * options = WILD_NO_BEEP: Don't beep for multiple matches
3469 * options = WILD_ADD_SLASH: add a slash after directory names
3470 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3471 * options = WILD_SILENT: don't print warning messages
3472 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003473 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 *
3475 * The variables xp->xp_context and xp->xp_backslash must have been set!
3476 */
3477 char_u *
3478ExpandOne(xp, str, orig, options, mode)
3479 expand_T *xp;
3480 char_u *str;
3481 char_u *orig; /* allocated copy of original of expanded string */
3482 int options;
3483 int mode;
3484{
3485 char_u *ss = NULL;
3486 static int findex;
3487 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003488 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 int i;
3490 long_u len;
3491 int non_suf_match; /* number without matching suffix */
3492
3493 /*
3494 * first handle the case of using an old match
3495 */
3496 if (mode == WILD_NEXT || mode == WILD_PREV)
3497 {
3498 if (xp->xp_numfiles > 0)
3499 {
3500 if (mode == WILD_PREV)
3501 {
3502 if (findex == -1)
3503 findex = xp->xp_numfiles;
3504 --findex;
3505 }
3506 else /* mode == WILD_NEXT */
3507 ++findex;
3508
3509 /*
3510 * When wrapping around, return the original string, set findex to
3511 * -1.
3512 */
3513 if (findex < 0)
3514 {
3515 if (orig_save == NULL)
3516 findex = xp->xp_numfiles - 1;
3517 else
3518 findex = -1;
3519 }
3520 if (findex >= xp->xp_numfiles)
3521 {
3522 if (orig_save == NULL)
3523 findex = 0;
3524 else
3525 findex = -1;
3526 }
3527#ifdef FEAT_WILDMENU
3528 if (p_wmnu)
3529 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3530 findex, cmd_showtail);
3531#endif
3532 if (findex == -1)
3533 return vim_strsave(orig_save);
3534 return vim_strsave(xp->xp_files[findex]);
3535 }
3536 else
3537 return NULL;
3538 }
3539
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003540 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3542 {
3543 FreeWild(xp->xp_numfiles, xp->xp_files);
3544 xp->xp_numfiles = -1;
3545 vim_free(orig_save);
3546 orig_save = NULL;
3547 }
3548 findex = 0;
3549
3550 if (mode == WILD_FREE) /* only release file name */
3551 return NULL;
3552
3553 if (xp->xp_numfiles == -1)
3554 {
3555 vim_free(orig_save);
3556 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003557 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
3559 /*
3560 * Do the expansion.
3561 */
3562 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3563 options) == FAIL)
3564 {
3565#ifdef FNAME_ILLEGAL
3566 /* Illegal file name has been silently skipped. But when there
3567 * are wildcards, the real problem is that there was no match,
3568 * causing the pattern to be added, which has illegal characters.
3569 */
3570 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3571 EMSG2(_(e_nomatch2), str);
3572#endif
3573 }
3574 else if (xp->xp_numfiles == 0)
3575 {
3576 if (!(options & WILD_SILENT))
3577 EMSG2(_(e_nomatch2), str);
3578 }
3579 else
3580 {
3581 /* Escape the matches for use on the command line. */
3582 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3583
3584 /*
3585 * Check for matching suffixes in file names.
3586 */
3587 if (mode != WILD_ALL && mode != WILD_LONGEST)
3588 {
3589 if (xp->xp_numfiles)
3590 non_suf_match = xp->xp_numfiles;
3591 else
3592 non_suf_match = 1;
3593 if ((xp->xp_context == EXPAND_FILES
3594 || xp->xp_context == EXPAND_DIRECTORIES)
3595 && xp->xp_numfiles > 1)
3596 {
3597 /*
3598 * More than one match; check suffix.
3599 * The files will have been sorted on matching suffix in
3600 * expand_wildcards, only need to check the first two.
3601 */
3602 non_suf_match = 0;
3603 for (i = 0; i < 2; ++i)
3604 if (match_suffix(xp->xp_files[i]))
3605 ++non_suf_match;
3606 }
3607 if (non_suf_match != 1)
3608 {
3609 /* Can we ever get here unless it's while expanding
3610 * interactively? If not, we can get rid of this all
3611 * together. Don't really want to wait for this message
3612 * (and possibly have to hit return to continue!).
3613 */
3614 if (!(options & WILD_SILENT))
3615 EMSG(_(e_toomany));
3616 else if (!(options & WILD_NO_BEEP))
3617 beep_flush();
3618 }
3619 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3620 ss = vim_strsave(xp->xp_files[0]);
3621 }
3622 }
3623 }
3624
3625 /* Find longest common part */
3626 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3627 {
3628 for (len = 0; xp->xp_files[0][len]; ++len)
3629 {
3630 for (i = 0; i < xp->xp_numfiles; ++i)
3631 {
3632#ifdef CASE_INSENSITIVE_FILENAME
3633 if (xp->xp_context == EXPAND_DIRECTORIES
3634 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003635 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 || xp->xp_context == EXPAND_BUFFERS)
3637 {
3638 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3639 TOLOWER_LOC(xp->xp_files[0][len]))
3640 break;
3641 }
3642 else
3643#endif
3644 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3645 break;
3646 }
3647 if (i < xp->xp_numfiles)
3648 {
3649 if (!(options & WILD_NO_BEEP))
3650 vim_beep();
3651 break;
3652 }
3653 }
3654 ss = alloc((unsigned)len + 1);
3655 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003656 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 findex = -1; /* next p_wc gets first one */
3658 }
3659
3660 /* Concatenate all matching names */
3661 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3662 {
3663 len = 0;
3664 for (i = 0; i < xp->xp_numfiles; ++i)
3665 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3666 ss = lalloc(len, TRUE);
3667 if (ss != NULL)
3668 {
3669 *ss = NUL;
3670 for (i = 0; i < xp->xp_numfiles; ++i)
3671 {
3672 STRCAT(ss, xp->xp_files[i]);
3673 if (i != xp->xp_numfiles - 1)
3674 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3675 }
3676 }
3677 }
3678
3679 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3680 ExpandCleanup(xp);
3681
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003682 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003683 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003684 vim_free(orig);
3685
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 return ss;
3687}
3688
3689/*
3690 * Prepare an expand structure for use.
3691 */
3692 void
3693ExpandInit(xp)
3694 expand_T *xp;
3695{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003696 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003697 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003699#ifndef BACKSLASH_IN_FILENAME
3700 xp->xp_shell = FALSE;
3701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 xp->xp_numfiles = -1;
3703 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003704#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3705 xp->xp_arg = NULL;
3706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707}
3708
3709/*
3710 * Cleanup an expand structure after use.
3711 */
3712 void
3713ExpandCleanup(xp)
3714 expand_T *xp;
3715{
3716 if (xp->xp_numfiles >= 0)
3717 {
3718 FreeWild(xp->xp_numfiles, xp->xp_files);
3719 xp->xp_numfiles = -1;
3720 }
3721}
3722
3723 void
3724ExpandEscape(xp, str, numfiles, files, options)
3725 expand_T *xp;
3726 char_u *str;
3727 int numfiles;
3728 char_u **files;
3729 int options;
3730{
3731 int i;
3732 char_u *p;
3733
3734 /*
3735 * May change home directory back to "~"
3736 */
3737 if (options & WILD_HOME_REPLACE)
3738 tilde_replace(str, numfiles, files);
3739
3740 if (options & WILD_ESCAPE)
3741 {
3742 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003743 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003744 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 || xp->xp_context == EXPAND_BUFFERS
3746 || xp->xp_context == EXPAND_DIRECTORIES)
3747 {
3748 /*
3749 * Insert a backslash into a file name before a space, \, %, #
3750 * and wildmatch characters, except '~'.
3751 */
3752 for (i = 0; i < numfiles; ++i)
3753 {
3754 /* for ":set path=" we need to escape spaces twice */
3755 if (xp->xp_backslash == XP_BS_THREE)
3756 {
3757 p = vim_strsave_escaped(files[i], (char_u *)" ");
3758 if (p != NULL)
3759 {
3760 vim_free(files[i]);
3761 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003762#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 p = vim_strsave_escaped(files[i], (char_u *)" ");
3764 if (p != NULL)
3765 {
3766 vim_free(files[i]);
3767 files[i] = p;
3768 }
3769#endif
3770 }
3771 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003772#ifdef BACKSLASH_IN_FILENAME
3773 p = vim_strsave_fnameescape(files[i], FALSE);
3774#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003775 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 if (p != NULL)
3778 {
3779 vim_free(files[i]);
3780 files[i] = p;
3781 }
3782
3783 /* If 'str' starts with "\~", replace "~" at start of
3784 * files[i] with "\~". */
3785 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003786 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
3788 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003789
3790 /* If the first file starts with a '+' escape it. Otherwise it
3791 * could be seen as "+cmd". */
3792 if (*files[0] == '+')
3793 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795 else if (xp->xp_context == EXPAND_TAGS)
3796 {
3797 /*
3798 * Insert a backslash before characters in a tag name that
3799 * would terminate the ":tag" command.
3800 */
3801 for (i = 0; i < numfiles; ++i)
3802 {
3803 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3804 if (p != NULL)
3805 {
3806 vim_free(files[i]);
3807 files[i] = p;
3808 }
3809 }
3810 }
3811 }
3812}
3813
3814/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003815 * Escape special characters in "fname" for when used as a file name argument
3816 * after a Vim command, or, when "shell" is non-zero, a shell command.
3817 * Returns the result in allocated memory.
3818 */
3819 char_u *
3820vim_strsave_fnameescape(fname, shell)
3821 char_u *fname;
3822 int shell;
3823{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003824 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003825#ifdef BACKSLASH_IN_FILENAME
3826 char_u buf[20];
3827 int j = 0;
3828
3829 /* Don't escape '[' and '{' if they are in 'isfname'. */
3830 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3831 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3832 buf[j++] = *p;
3833 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003834 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003835#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003836 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3837 if (shell && csh_like_shell() && p != NULL)
3838 {
3839 char_u *s;
3840
3841 /* For csh and similar shells need to put two backslashes before '!'.
3842 * One is taken by Vim, one by the shell. */
3843 s = vim_strsave_escaped(p, (char_u *)"!");
3844 vim_free(p);
3845 p = s;
3846 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003847#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003848
3849 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3850 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003851 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003852 escape_fname(&p);
3853
3854 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003855}
3856
3857/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003858 * Put a backslash before the file name in "pp", which is in allocated memory.
3859 */
3860 static void
3861escape_fname(pp)
3862 char_u **pp;
3863{
3864 char_u *p;
3865
3866 p = alloc((unsigned)(STRLEN(*pp) + 2));
3867 if (p != NULL)
3868 {
3869 p[0] = '\\';
3870 STRCPY(p + 1, *pp);
3871 vim_free(*pp);
3872 *pp = p;
3873 }
3874}
3875
3876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 * For each file name in files[num_files]:
3878 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3879 */
3880 void
3881tilde_replace(orig_pat, num_files, files)
3882 char_u *orig_pat;
3883 int num_files;
3884 char_u **files;
3885{
3886 int i;
3887 char_u *p;
3888
3889 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3890 {
3891 for (i = 0; i < num_files; ++i)
3892 {
3893 p = home_replace_save(NULL, files[i]);
3894 if (p != NULL)
3895 {
3896 vim_free(files[i]);
3897 files[i] = p;
3898 }
3899 }
3900 }
3901}
3902
3903/*
3904 * Show all matches for completion on the command line.
3905 * Returns EXPAND_NOTHING when the character that triggered expansion should
3906 * be inserted like a normal character.
3907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 static int
3909showmatches(xp, wildmenu)
3910 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003911 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912{
3913#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3914 int num_files;
3915 char_u **files_found;
3916 int i, j, k;
3917 int maxlen;
3918 int lines;
3919 int columns;
3920 char_u *p;
3921 int lastlen;
3922 int attr;
3923 int showtail;
3924
3925 if (xp->xp_numfiles == -1)
3926 {
3927 set_expand_context(xp);
3928 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3929 &num_files, &files_found);
3930 showtail = expand_showtail(xp);
3931 if (i != EXPAND_OK)
3932 return i;
3933
3934 }
3935 else
3936 {
3937 num_files = xp->xp_numfiles;
3938 files_found = xp->xp_files;
3939 showtail = cmd_showtail;
3940 }
3941
3942#ifdef FEAT_WILDMENU
3943 if (!wildmenu)
3944 {
3945#endif
3946 msg_didany = FALSE; /* lines_left will be set */
3947 msg_start(); /* prepare for paging */
3948 msg_putchar('\n');
3949 out_flush();
3950 cmdline_row = msg_row;
3951 msg_didany = FALSE; /* lines_left will be set again */
3952 msg_start(); /* prepare for paging */
3953#ifdef FEAT_WILDMENU
3954 }
3955#endif
3956
3957 if (got_int)
3958 got_int = FALSE; /* only int. the completion, not the cmd line */
3959#ifdef FEAT_WILDMENU
3960 else if (wildmenu)
3961 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3962#endif
3963 else
3964 {
3965 /* find the length of the longest file name */
3966 maxlen = 0;
3967 for (i = 0; i < num_files; ++i)
3968 {
3969 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003970 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 || xp->xp_context == EXPAND_BUFFERS))
3972 {
3973 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3974 j = vim_strsize(NameBuff);
3975 }
3976 else
3977 j = vim_strsize(L_SHOWFILE(i));
3978 if (j > maxlen)
3979 maxlen = j;
3980 }
3981
3982 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3983 lines = num_files;
3984 else
3985 {
3986 /* compute the number of columns and lines for the listing */
3987 maxlen += 2; /* two spaces between file names */
3988 columns = ((int)Columns + 2) / maxlen;
3989 if (columns < 1)
3990 columns = 1;
3991 lines = (num_files + columns - 1) / columns;
3992 }
3993
3994 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3995
3996 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3997 {
3998 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3999 msg_clr_eos();
4000 msg_advance(maxlen - 3);
4001 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4002 }
4003
4004 /* list the files line by line */
4005 for (i = 0; i < lines; ++i)
4006 {
4007 lastlen = 999;
4008 for (k = i; k < num_files; k += lines)
4009 {
4010 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4011 {
4012 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4013 p = files_found[k] + STRLEN(files_found[k]) + 1;
4014 msg_advance(maxlen + 1);
4015 msg_puts(p);
4016 msg_advance(maxlen + 3);
4017 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4018 break;
4019 }
4020 for (j = maxlen - lastlen; --j >= 0; )
4021 msg_putchar(' ');
4022 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004023 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 || xp->xp_context == EXPAND_BUFFERS)
4025 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004026 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004027 if (xp->xp_numfiles != -1)
4028 {
4029 char_u *halved_slash;
4030 char_u *exp_path;
4031
4032 /* Expansion was done before and special characters
4033 * were escaped, need to halve backslashes. Also
4034 * $HOME has been replaced with ~/. */
4035 exp_path = expand_env_save_opt(files_found[k], TRUE);
4036 halved_slash = backslash_halve_save(
4037 exp_path != NULL ? exp_path : files_found[k]);
4038 j = mch_isdir(halved_slash != NULL ? halved_slash
4039 : files_found[k]);
4040 vim_free(exp_path);
4041 vim_free(halved_slash);
4042 }
4043 else
4044 /* Expansion was done here, file names are literal. */
4045 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 if (showtail)
4047 p = L_SHOWFILE(k);
4048 else
4049 {
4050 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4051 TRUE);
4052 p = NameBuff;
4053 }
4054 }
4055 else
4056 {
4057 j = FALSE;
4058 p = L_SHOWFILE(k);
4059 }
4060 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4061 }
4062 if (msg_col > 0) /* when not wrapped around */
4063 {
4064 msg_clr_eos();
4065 msg_putchar('\n');
4066 }
4067 out_flush(); /* show one line at a time */
4068 if (got_int)
4069 {
4070 got_int = FALSE;
4071 break;
4072 }
4073 }
4074
4075 /*
4076 * we redraw the command below the lines that we have just listed
4077 * This is a bit tricky, but it saves a lot of screen updating.
4078 */
4079 cmdline_row = msg_row; /* will put it back later */
4080 }
4081
4082 if (xp->xp_numfiles == -1)
4083 FreeWild(num_files, files_found);
4084
4085 return EXPAND_OK;
4086}
4087
4088/*
4089 * Private gettail for showmatches() (and win_redr_status_matches()):
4090 * Find tail of file name path, but ignore trailing "/".
4091 */
4092 char_u *
4093sm_gettail(s)
4094 char_u *s;
4095{
4096 char_u *p;
4097 char_u *t = s;
4098 int had_sep = FALSE;
4099
4100 for (p = s; *p != NUL; )
4101 {
4102 if (vim_ispathsep(*p)
4103#ifdef BACKSLASH_IN_FILENAME
4104 && !rem_backslash(p)
4105#endif
4106 )
4107 had_sep = TRUE;
4108 else if (had_sep)
4109 {
4110 t = p;
4111 had_sep = FALSE;
4112 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004113 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115 return t;
4116}
4117
4118/*
4119 * Return TRUE if we only need to show the tail of completion matches.
4120 * When not completing file names or there is a wildcard in the path FALSE is
4121 * returned.
4122 */
4123 static int
4124expand_showtail(xp)
4125 expand_T *xp;
4126{
4127 char_u *s;
4128 char_u *end;
4129
4130 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004131 if (xp->xp_context != EXPAND_FILES
4132 && xp->xp_context != EXPAND_SHELLCMD
4133 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 return FALSE;
4135
4136 end = gettail(xp->xp_pattern);
4137 if (end == xp->xp_pattern) /* there is no path separator */
4138 return FALSE;
4139
4140 for (s = xp->xp_pattern; s < end; s++)
4141 {
4142 /* Skip escaped wildcards. Only when the backslash is not a path
4143 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4144 if (rem_backslash(s))
4145 ++s;
4146 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4147 return FALSE;
4148 }
4149 return TRUE;
4150}
4151
4152/*
4153 * Prepare a string for expansion.
4154 * When expanding file names: The string will be used with expand_wildcards().
4155 * Copy the file name into allocated memory and add a '*' at the end.
4156 * When expanding other names: The string will be used with regcomp(). Copy
4157 * the name into allocated memory and prepend "^".
4158 */
4159 char_u *
4160addstar(fname, len, context)
4161 char_u *fname;
4162 int len;
4163 int context; /* EXPAND_FILES etc. */
4164{
4165 char_u *retval;
4166 int i, j;
4167 int new_len;
4168 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004169 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004171 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004172 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004173 && context != EXPAND_SHELLCMD
4174 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 {
4176 /*
4177 * Matching will be done internally (on something other than files).
4178 * So we convert the file-matching-type wildcards into our kind for
4179 * use with vim_regcomp(). First work out how long it will be:
4180 */
4181
4182 /* For help tags the translation is done in find_help_tags().
4183 * For a tag pattern starting with "/" no translation is needed. */
4184 if (context == EXPAND_HELP
4185 || context == EXPAND_COLORS
4186 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004187 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004188 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 || (context == EXPAND_TAGS && fname[0] == '/'))
4190 retval = vim_strnsave(fname, len);
4191 else
4192 {
4193 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4194 for (i = 0; i < len; i++)
4195 {
4196 if (fname[i] == '*' || fname[i] == '~')
4197 new_len++; /* '*' needs to be replaced by ".*"
4198 '~' needs to be replaced by "\~" */
4199
4200 /* Buffer names are like file names. "." should be literal */
4201 if (context == EXPAND_BUFFERS && fname[i] == '.')
4202 new_len++; /* "." becomes "\." */
4203
4204 /* Custom expansion takes care of special things, match
4205 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004206 if ((context == EXPAND_USER_DEFINED
4207 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 new_len++; /* '\' becomes "\\" */
4209 }
4210 retval = alloc(new_len);
4211 if (retval != NULL)
4212 {
4213 retval[0] = '^';
4214 j = 1;
4215 for (i = 0; i < len; i++, j++)
4216 {
4217 /* Skip backslash. But why? At least keep it for custom
4218 * expansion. */
4219 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004220 && context != EXPAND_USER_LIST
4221 && fname[i] == '\\'
4222 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 break;
4224
4225 switch (fname[i])
4226 {
4227 case '*': retval[j++] = '.';
4228 break;
4229 case '~': retval[j++] = '\\';
4230 break;
4231 case '?': retval[j] = '.';
4232 continue;
4233 case '.': if (context == EXPAND_BUFFERS)
4234 retval[j++] = '\\';
4235 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004236 case '\\': if (context == EXPAND_USER_DEFINED
4237 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 retval[j++] = '\\';
4239 break;
4240 }
4241 retval[j] = fname[i];
4242 }
4243 retval[j] = NUL;
4244 }
4245 }
4246 }
4247 else
4248 {
4249 retval = alloc(len + 4);
4250 if (retval != NULL)
4251 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004252 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
4254 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004255 * Don't add a star to *, ~, ~user, $var or `cmd`.
4256 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 * ~ would be at the start of the file name, but not the tail.
4258 * $ could be anywhere in the tail.
4259 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004260 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 */
4262 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004263 ends_in_star = (len > 0 && retval[len - 1] == '*');
4264#ifndef BACKSLASH_IN_FILENAME
4265 for (i = len - 2; i >= 0; --i)
4266 {
4267 if (retval[i] != '\\')
4268 break;
4269 ends_in_star = !ends_in_star;
4270 }
4271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004273 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 && vim_strchr(tail, '$') == NULL
4275 && vim_strchr(retval, '`') == NULL)
4276 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004277 else if (len > 0 && retval[len - 1] == '$')
4278 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 retval[len] = NUL;
4280 }
4281 }
4282 return retval;
4283}
4284
4285/*
4286 * Must parse the command line so far to work out what context we are in.
4287 * Completion can then be done based on that context.
4288 * This routine sets the variables:
4289 * xp->xp_pattern The start of the pattern to be expanded within
4290 * the command line (ends at the cursor).
4291 * xp->xp_context The type of thing to expand. Will be one of:
4292 *
4293 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4294 * the command line, like an unknown command. Caller
4295 * should beep.
4296 * EXPAND_NOTHING Unrecognised context for completion, use char like
4297 * a normal char, rather than for completion. eg
4298 * :s/^I/
4299 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4300 * it.
4301 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4302 * EXPAND_FILES After command with XFILE set, or after setting
4303 * with P_EXPAND set. eg :e ^I, :w>>^I
4304 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4305 * when we know only directories are of interest. eg
4306 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004307 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4309 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4310 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4311 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4312 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4313 * EXPAND_EVENTS Complete event names
4314 * EXPAND_SYNTAX Complete :syntax command arguments
4315 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4316 * EXPAND_AUGROUP Complete autocommand group names
4317 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4318 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4319 * eg :unmap a^I , :cunab x^I
4320 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4321 * eg :call sub^I
4322 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4323 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4324 * names in expressions, eg :while s^I
4325 * EXPAND_ENV_VARS Complete environment variable names
4326 */
4327 static void
4328set_expand_context(xp)
4329 expand_T *xp;
4330{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004331 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 if (ccline.cmdfirstc != ':'
4333#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004334 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004335 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336#endif
4337 )
4338 {
4339 xp->xp_context = EXPAND_NOTHING;
4340 return;
4341 }
4342 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4343}
4344
4345 void
4346set_cmd_context(xp, str, len, col)
4347 expand_T *xp;
4348 char_u *str; /* start of command line */
4349 int len; /* length of command line (excl. NUL) */
4350 int col; /* position of cursor */
4351{
4352 int old_char = NUL;
4353 char_u *nextcomm;
4354
4355 /*
4356 * Avoid a UMR warning from Purify, only save the character if it has been
4357 * written before.
4358 */
4359 if (col < len)
4360 old_char = str[col];
4361 str[col] = NUL;
4362 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004363
4364#ifdef FEAT_EVAL
4365 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004366 {
4367# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004368 /* pass CMD_SIZE because there is no real command */
4369 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004370# endif
4371 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004372 else if (ccline.input_fn)
4373 {
4374 xp->xp_context = ccline.xp_context;
4375 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004376# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004377 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004378# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004379 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004380 else
4381#endif
4382 while (nextcomm != NULL)
4383 nextcomm = set_one_cmd_context(xp, nextcomm);
4384
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 str[col] = old_char;
4386}
4387
4388/*
4389 * Expand the command line "str" from context "xp".
4390 * "xp" must have been set by set_cmd_context().
4391 * xp->xp_pattern points into "str", to where the text that is to be expanded
4392 * starts.
4393 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4394 * cursor.
4395 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4396 * key that triggered expansion literally.
4397 * Returns EXPAND_OK otherwise.
4398 */
4399 int
4400expand_cmdline(xp, str, col, matchcount, matches)
4401 expand_T *xp;
4402 char_u *str; /* start of command line */
4403 int col; /* position of cursor */
4404 int *matchcount; /* return: nr of matches */
4405 char_u ***matches; /* return: array of pointers to matches */
4406{
4407 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004408 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
4410 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4411 {
4412 beep_flush();
4413 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4414 }
4415 if (xp->xp_context == EXPAND_NOTHING)
4416 {
4417 /* Caller can use the character as a normal char instead */
4418 return EXPAND_NOTHING;
4419 }
4420
4421 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004422 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4423 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 if (file_str == NULL)
4425 return EXPAND_UNSUCCESSFUL;
4426
Bram Moolenaar94950a92010-12-02 16:01:29 +01004427 if (p_wic)
4428 options += WILD_ICASE;
4429
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004431 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 {
4433 *matchcount = 0;
4434 *matches = NULL;
4435 }
4436 vim_free(file_str);
4437
4438 return EXPAND_OK;
4439}
4440
4441#ifdef FEAT_MULTI_LANG
4442/*
4443 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4444 */
4445static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4446
4447 static void
4448cleanup_help_tags(num_file, file)
4449 int num_file;
4450 char_u **file;
4451{
4452 int i, j;
4453 int len;
4454
4455 for (i = 0; i < num_file; ++i)
4456 {
4457 len = (int)STRLEN(file[i]) - 3;
4458 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4459 {
4460 /* Sorting on priority means the same item in another language may
4461 * be anywhere. Search all items for a match up to the "@en". */
4462 for (j = 0; j < num_file; ++j)
4463 if (j != i
4464 && (int)STRLEN(file[j]) == len + 3
4465 && STRNCMP(file[i], file[j], len + 1) == 0)
4466 break;
4467 if (j == num_file)
4468 file[i][len] = NUL;
4469 }
4470 }
4471}
4472#endif
4473
4474/*
4475 * Do the expansion based on xp->xp_context and "pat".
4476 */
4477 static int
4478ExpandFromContext(xp, pat, num_file, file, options)
4479 expand_T *xp;
4480 char_u *pat;
4481 int *num_file;
4482 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004483 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484{
4485#ifdef FEAT_CMDL_COMPL
4486 regmatch_T regmatch;
4487#endif
4488 int ret;
4489 int flags;
4490
4491 flags = EW_DIR; /* include directories */
4492 if (options & WILD_LIST_NOTFOUND)
4493 flags |= EW_NOTFOUND;
4494 if (options & WILD_ADD_SLASH)
4495 flags |= EW_ADDSLASH;
4496 if (options & WILD_KEEP_ALL)
4497 flags |= EW_KEEPALL;
4498 if (options & WILD_SILENT)
4499 flags |= EW_SILENT;
4500
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004501 if (xp->xp_context == EXPAND_FILES
4502 || xp->xp_context == EXPAND_DIRECTORIES
4503 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
4505 /*
4506 * Expand file or directory names.
4507 */
4508 int free_pat = FALSE;
4509 int i;
4510
4511 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4512 * space */
4513 if (xp->xp_backslash != XP_BS_NONE)
4514 {
4515 free_pat = TRUE;
4516 pat = vim_strsave(pat);
4517 for (i = 0; pat[i]; ++i)
4518 if (pat[i] == '\\')
4519 {
4520 if (xp->xp_backslash == XP_BS_THREE
4521 && pat[i + 1] == '\\'
4522 && pat[i + 2] == '\\'
4523 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004524 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 if (xp->xp_backslash == XP_BS_ONE
4526 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004527 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 }
4529 }
4530
4531 if (xp->xp_context == EXPAND_FILES)
4532 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004533 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4534 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 else
4536 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004537 if (options & WILD_ICASE)
4538 flags |= EW_ICASE;
4539
Bram Moolenaard7834d32009-12-02 16:14:36 +00004540 /* Expand wildcards, supporting %:h and the like. */
4541 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 if (free_pat)
4543 vim_free(pat);
4544 return ret;
4545 }
4546
4547 *file = (char_u **)"";
4548 *num_file = 0;
4549 if (xp->xp_context == EXPAND_HELP)
4550 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004551 /* With an empty argument we would get all the help tags, which is
4552 * very slow. Get matches for "help" instead. */
4553 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4554 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 {
4556#ifdef FEAT_MULTI_LANG
4557 cleanup_help_tags(*num_file, *file);
4558#endif
4559 return OK;
4560 }
4561 return FAIL;
4562 }
4563
4564#ifndef FEAT_CMDL_COMPL
4565 return FAIL;
4566#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004567 if (xp->xp_context == EXPAND_SHELLCMD)
4568 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 if (xp->xp_context == EXPAND_OLD_SETTING)
4570 return ExpandOldSetting(num_file, file);
4571 if (xp->xp_context == EXPAND_BUFFERS)
4572 return ExpandBufnames(pat, num_file, file, options);
4573 if (xp->xp_context == EXPAND_TAGS
4574 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4575 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4576 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004577 {
4578 char *directories[] = {"colors", NULL};
4579 return ExpandRTDir(pat, num_file, file, directories);
4580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004582 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004583 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004584 return ExpandRTDir(pat, num_file, file, directories);
4585 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004586 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004587 {
4588 char *directories[] = {"syntax", NULL};
4589 return ExpandRTDir(pat, num_file, file, directories);
4590 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004591 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004592 {
4593 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4594 return ExpandRTDir(pat, num_file, file, directories);
4595 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004596# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4597 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004598 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004599# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
4601 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4602 if (regmatch.regprog == NULL)
4603 return FAIL;
4604
4605 /* set ignore-case according to p_ic, p_scs and pat */
4606 regmatch.rm_ic = ignorecase(pat);
4607
4608 if (xp->xp_context == EXPAND_SETTINGS
4609 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4610 ret = ExpandSettings(xp, &regmatch, num_file, file);
4611 else if (xp->xp_context == EXPAND_MAPPINGS)
4612 ret = ExpandMappings(&regmatch, num_file, file);
4613# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4614 else if (xp->xp_context == EXPAND_USER_DEFINED)
4615 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4616# endif
4617 else
4618 {
4619 static struct expgen
4620 {
4621 int context;
4622 char_u *((*func)__ARGS((expand_T *, int)));
4623 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004624 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 } tab[] =
4626 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004627 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4628 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004630 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4631 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4632 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4633 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634#endif
4635#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004636 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4637 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4638 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4639 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640#endif
4641#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004642 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4643 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644#endif
4645#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004646 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004648 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004650 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4651 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004653#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004654 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004655#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004656#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004657 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004658#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004659#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004660 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4663 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004664 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4665 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004667 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 };
4669 int i;
4670
4671 /*
4672 * Find a context in the table and call the ExpandGeneric() with the
4673 * right function to do the expansion.
4674 */
4675 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004676 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 if (xp->xp_context == tab[i].context)
4678 {
4679 if (tab[i].ic)
4680 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004681 ret = ExpandGeneric(xp, &regmatch, num_file, file,
4682 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 break;
4684 }
4685 }
4686
4687 vim_free(regmatch.regprog);
4688
4689 return ret;
4690#endif /* FEAT_CMDL_COMPL */
4691}
4692
4693#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4694/*
4695 * Expand a list of names.
4696 *
4697 * Generic function for command line completion. It calls a function to
4698 * obtain strings, one by one. The strings are matched against a regexp
4699 * program. Matching strings are copied into an array, which is returned.
4700 *
4701 * Returns OK when no problems encountered, FAIL for error (out of memory).
4702 */
4703 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004704ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 expand_T *xp;
4706 regmatch_T *regmatch;
4707 int *num_file;
4708 char_u ***file;
4709 char_u *((*func)__ARGS((expand_T *, int)));
4710 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004711 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712{
4713 int i;
4714 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004715 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 char_u *str;
4717
4718 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004719 * round == 0: count the number of matching names
4720 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004722 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 {
4724 for (i = 0; ; ++i)
4725 {
4726 str = (*func)(xp, i);
4727 if (str == NULL) /* end of list */
4728 break;
4729 if (*str == NUL) /* skip empty strings */
4730 continue;
4731
4732 if (vim_regexec(regmatch, str, (colnr_T)0))
4733 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004734 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004736 if (escaped)
4737 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4738 else
4739 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 (*file)[count] = str;
4741#ifdef FEAT_MENU
4742 if (func == get_menu_names && str != NULL)
4743 {
4744 /* test for separator added by get_menu_names() */
4745 str += STRLEN(str) - 1;
4746 if (*str == '\001')
4747 *str = '.';
4748 }
4749#endif
4750 }
4751 ++count;
4752 }
4753 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004754 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
4756 if (count == 0)
4757 return OK;
4758 *num_file = count;
4759 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4760 if (*file == NULL)
4761 {
4762 *file = (char_u **)"";
4763 return FAIL;
4764 }
4765 count = 0;
4766 }
4767 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004768
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004769 /* Sort the results. Keep menu's in the specified order. */
4770 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004771 {
4772 if (xp->xp_context == EXPAND_EXPRESSION
4773 || xp->xp_context == EXPAND_FUNCTIONS
4774 || xp->xp_context == EXPAND_USER_FUNC)
4775 /* <SNR> functions should be sorted to the end. */
4776 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4777 sort_func_compare);
4778 else
4779 sort_strings(*file, *num_file);
4780 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004781
Bram Moolenaar4f688582007-07-24 12:34:30 +00004782#ifdef FEAT_CMDL_COMPL
4783 /* Reset the variables used for special highlight names expansion, so that
4784 * they don't show up when getting normal highlight names by ID. */
4785 reset_expand_highlight();
4786#endif
4787
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 return OK;
4789}
4790
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004791/*
4792 * Complete a shell command.
4793 * Returns FAIL or OK;
4794 */
4795 static int
4796expand_shellcmd(filepat, num_file, file, flagsarg)
4797 char_u *filepat; /* pattern to match with command names */
4798 int *num_file; /* return: number of matches */
4799 char_u ***file; /* return: array with matches */
4800 int flagsarg; /* EW_ flags */
4801{
4802 char_u *pat;
4803 int i;
4804 char_u *path;
4805 int mustfree = FALSE;
4806 garray_T ga;
4807 char_u *buf = alloc(MAXPATHL);
4808 size_t l;
4809 char_u *s, *e;
4810 int flags = flagsarg;
4811 int ret;
4812
4813 if (buf == NULL)
4814 return FAIL;
4815
4816 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4817 * space */
4818 pat = vim_strsave(filepat);
4819 for (i = 0; pat[i]; ++i)
4820 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004821 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004822
4823 flags |= EW_FILE | EW_EXEC;
4824
4825 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004826 if (mch_isFullName(pat))
4827 path = (char_u *)" ";
4828 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004829 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4830 path = (char_u *)".";
4831 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004832 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004833 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004834 if (path == NULL)
4835 path = (char_u *)"";
4836 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004837
4838 /*
4839 * Go over all directories in $PATH. Expand matches in that directory and
4840 * collect them in "ga".
4841 */
4842 ga_init2(&ga, (int)sizeof(char *), 10);
4843 for (s = path; *s != NUL; s = e)
4844 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004845 if (*s == ' ')
4846 ++s; /* Skip space used for absolute path name. */
4847
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004848#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4849 e = vim_strchr(s, ';');
4850#else
4851 e = vim_strchr(s, ':');
4852#endif
4853 if (e == NULL)
4854 e = s + STRLEN(s);
4855
4856 l = e - s;
4857 if (l > MAXPATHL - 5)
4858 break;
4859 vim_strncpy(buf, s, l);
4860 add_pathsep(buf);
4861 l = STRLEN(buf);
4862 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4863
4864 /* Expand matches in one directory of $PATH. */
4865 ret = expand_wildcards(1, &buf, num_file, file, flags);
4866 if (ret == OK)
4867 {
4868 if (ga_grow(&ga, *num_file) == FAIL)
4869 FreeWild(*num_file, *file);
4870 else
4871 {
4872 for (i = 0; i < *num_file; ++i)
4873 {
4874 s = (*file)[i];
4875 if (STRLEN(s) > l)
4876 {
4877 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004878 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004879 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4880 }
4881 else
4882 vim_free(s);
4883 }
4884 vim_free(*file);
4885 }
4886 }
4887 if (*e != NUL)
4888 ++e;
4889 }
4890 *file = ga.ga_data;
4891 *num_file = ga.ga_len;
4892
4893 vim_free(buf);
4894 vim_free(pat);
4895 if (mustfree)
4896 vim_free(path);
4897 return OK;
4898}
4899
4900
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004902static 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));
4903
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004905 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004906 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004908 static void *
4909call_user_expand_func(user_expand_func, xp, num_file, file)
4910 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 int *num_file;
4913 char_u ***file;
4914{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004915 char_u keep;
4916 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004919 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004920 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
4922 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004923 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 *num_file = 0;
4925 *file = NULL;
4926
Bram Moolenaar21669c02008-01-18 12:16:16 +00004927 if (ccline.cmdbuff == NULL)
4928 {
4929 /* Completion from Insert mode, pass fake arguments. */
4930 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004931 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004932 args[1] = xp->xp_pattern;
4933 }
4934 else
4935 {
4936 /* Completion on the command line, pass real arguments. */
4937 keep = ccline.cmdbuff[ccline.cmdlen];
4938 ccline.cmdbuff[ccline.cmdlen] = 0;
4939 sprintf((char *)num, "%d", ccline.cmdpos);
4940 args[1] = ccline.cmdbuff;
4941 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004942 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 args[2] = num;
4944
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004945 /* Save the cmdline, we don't know what the function may do. */
4946 save_ccline = ccline;
4947 ccline.cmdbuff = NULL;
4948 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004950
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004951 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004952
4953 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004955 if (ccline.cmdbuff != NULL)
4956 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004957
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004958 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004959 return ret;
4960}
4961
4962/*
4963 * Expand names with a function defined by the user.
4964 */
4965 static int
4966ExpandUserDefined(xp, regmatch, num_file, file)
4967 expand_T *xp;
4968 regmatch_T *regmatch;
4969 int *num_file;
4970 char_u ***file;
4971{
4972 char_u *retstr;
4973 char_u *s;
4974 char_u *e;
4975 char_u keep;
4976 garray_T ga;
4977
4978 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4979 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 return FAIL;
4981
4982 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004983 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 {
4985 e = vim_strchr(s, '\n');
4986 if (e == NULL)
4987 e = s + STRLEN(s);
4988 keep = *e;
4989 *e = 0;
4990
4991 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4992 {
4993 *e = keep;
4994 if (*e != NUL)
4995 ++e;
4996 continue;
4997 }
4998
4999 if (ga_grow(&ga, 1) == FAIL)
5000 break;
5001
5002 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5003 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
5005 *e = keep;
5006 if (*e != NUL)
5007 ++e;
5008 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005009 vim_free(retstr);
5010 *file = ga.ga_data;
5011 *num_file = ga.ga_len;
5012 return OK;
5013}
5014
5015/*
5016 * Expand names with a list returned by a function defined by the user.
5017 */
5018 static int
5019ExpandUserList(xp, num_file, file)
5020 expand_T *xp;
5021 int *num_file;
5022 char_u ***file;
5023{
5024 list_T *retlist;
5025 listitem_T *li;
5026 garray_T ga;
5027
5028 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5029 if (retlist == NULL)
5030 return FAIL;
5031
5032 ga_init2(&ga, (int)sizeof(char *), 3);
5033 /* Loop over the items in the list. */
5034 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5035 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005036 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5037 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005038
5039 if (ga_grow(&ga, 1) == FAIL)
5040 break;
5041
5042 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005043 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005044 ++ga.ga_len;
5045 }
5046 list_unref(retlist);
5047
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 *file = ga.ga_data;
5049 *num_file = ga.ga_len;
5050 return OK;
5051}
5052#endif
5053
5054/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005055 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005056 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005057 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 */
5059 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005060ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 char_u *pat;
5062 int *num_file;
5063 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005064 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005066 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 char_u *s;
5068 char_u *e;
5069 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005070 int i;
5071 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 *num_file = 0;
5074 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005075 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005076 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005078 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005080 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5081 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005083 ga_clear_strings(&ga);
5084 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005086 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5087 matches = globpath(p_rtp, s, 0);
5088 vim_free(s);
5089 if (matches == NULL)
5090 continue;
5091
5092 for (s = matches; *s != NUL; s = e)
5093 {
5094 e = vim_strchr(s, '\n');
5095 if (e == NULL)
5096 e = s + STRLEN(s);
5097 if (ga_grow(&ga, 1) == FAIL)
5098 break;
5099 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5100 {
5101 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5102 if (*s == '\n' || vim_ispathsep(*s))
5103 break;
5104 ++s;
5105 ((char_u **)ga.ga_data)[ga.ga_len] =
5106 vim_strnsave(s, (int)(e - s - 4));
5107 ++ga.ga_len;
5108 }
5109 if (*e != NUL)
5110 ++e;
5111 }
5112 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005114 if (ga.ga_len == 0)
5115 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005116
5117 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005118 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005119 remove_duplicates(&ga);
5120
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 *file = ga.ga_data;
5122 *num_file = ga.ga_len;
5123 return OK;
5124}
5125
5126#endif
5127
5128#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5129/*
5130 * Expand "file" for all comma-separated directories in "path".
5131 * Returns an allocated string with all matches concatenated, separated by
5132 * newlines. Returns NULL for an error or no matches.
5133 */
5134 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005135globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 char_u *path;
5137 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005138 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139{
5140 expand_T xpc;
5141 char_u *buf;
5142 garray_T ga;
5143 int i;
5144 int len;
5145 int num_p;
5146 char_u **p;
5147 char_u *cur = NULL;
5148
5149 buf = alloc(MAXPATHL);
5150 if (buf == NULL)
5151 return NULL;
5152
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005153 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005155
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 ga_init2(&ga, 1, 100);
5157
5158 /* Loop over all entries in {path}. */
5159 while (*path != NUL)
5160 {
5161 /* Copy one item of the path to buf[] and concatenate the file name. */
5162 copy_option_part(&path, buf, MAXPATHL, ",");
5163 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5164 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005165# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005166 /* Using the platform's path separator (\) makes vim incorrectly
5167 * treat it as an escape character, use '/' instead. */
5168 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5169 STRCAT(buf, "/");
5170# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005172# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005174 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5175 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005177 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005179 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180
5181 /* Concatenate new results to previous ones. */
5182 if (ga_grow(&ga, len) == OK)
5183 {
5184 cur = (char_u *)ga.ga_data + ga.ga_len;
5185 for (i = 0; i < num_p; ++i)
5186 {
5187 STRCPY(cur, p[i]);
5188 cur += STRLEN(p[i]);
5189 *cur++ = '\n';
5190 }
5191 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193 FreeWild(num_p, p);
5194 }
5195 }
5196 }
5197 if (cur != NULL)
5198 *--cur = 0; /* Replace trailing newline with NUL */
5199
5200 vim_free(buf);
5201 return (char_u *)ga.ga_data;
5202}
5203
5204#endif
5205
5206#if defined(FEAT_CMDHIST) || defined(PROTO)
5207
5208/*********************************
5209 * Command line history stuff *
5210 *********************************/
5211
5212/*
5213 * Translate a history character to the associated type number.
5214 */
5215 static int
5216hist_char2type(c)
5217 int c;
5218{
5219 if (c == ':')
5220 return HIST_CMD;
5221 if (c == '=')
5222 return HIST_EXPR;
5223 if (c == '@')
5224 return HIST_INPUT;
5225 if (c == '>')
5226 return HIST_DEBUG;
5227 return HIST_SEARCH; /* must be '?' or '/' */
5228}
5229
5230/*
5231 * Table of history names.
5232 * These names are used in :history and various hist...() functions.
5233 * It is sufficient to give the significant prefix of a history name.
5234 */
5235
5236static char *(history_names[]) =
5237{
5238 "cmd",
5239 "search",
5240 "expr",
5241 "input",
5242 "debug",
5243 NULL
5244};
5245
5246/*
5247 * init_history() - Initialize the command line history.
5248 * Also used to re-allocate the history when the size changes.
5249 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005250 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251init_history()
5252{
5253 int newlen; /* new length of history table */
5254 histentry_T *temp;
5255 int i;
5256 int j;
5257 int type;
5258
5259 /*
5260 * If size of history table changed, reallocate it
5261 */
5262 newlen = (int)p_hi;
5263 if (newlen != hislen) /* history length changed */
5264 {
5265 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5266 {
5267 if (newlen)
5268 {
5269 temp = (histentry_T *)lalloc(
5270 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5271 if (temp == NULL) /* out of memory! */
5272 {
5273 if (type == 0) /* first one: just keep the old length */
5274 {
5275 newlen = hislen;
5276 break;
5277 }
5278 /* Already changed one table, now we can only have zero
5279 * length for all tables. */
5280 newlen = 0;
5281 type = -1;
5282 continue;
5283 }
5284 }
5285 else
5286 temp = NULL;
5287 if (newlen == 0 || temp != NULL)
5288 {
5289 if (hisidx[type] < 0) /* there are no entries yet */
5290 {
5291 for (i = 0; i < newlen; ++i)
5292 {
5293 temp[i].hisnum = 0;
5294 temp[i].hisstr = NULL;
5295 }
5296 }
5297 else if (newlen > hislen) /* array becomes bigger */
5298 {
5299 for (i = 0; i <= hisidx[type]; ++i)
5300 temp[i] = history[type][i];
5301 j = i;
5302 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5303 {
5304 temp[i].hisnum = 0;
5305 temp[i].hisstr = NULL;
5306 }
5307 for ( ; j < hislen; ++i, ++j)
5308 temp[i] = history[type][j];
5309 }
5310 else /* array becomes smaller or 0 */
5311 {
5312 j = hisidx[type];
5313 for (i = newlen - 1; ; --i)
5314 {
5315 if (i >= 0) /* copy newest entries */
5316 temp[i] = history[type][j];
5317 else /* remove older entries */
5318 vim_free(history[type][j].hisstr);
5319 if (--j < 0)
5320 j = hislen - 1;
5321 if (j == hisidx[type])
5322 break;
5323 }
5324 hisidx[type] = newlen - 1;
5325 }
5326 vim_free(history[type]);
5327 history[type] = temp;
5328 }
5329 }
5330 hislen = newlen;
5331 }
5332}
5333
5334/*
5335 * Check if command line 'str' is already in history.
5336 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5337 */
5338 static int
Bram Moolenaar4c402232011-07-27 17:58:46 +02005339in_history(type, str, move_to_front, sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 int type;
5341 char_u *str;
5342 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005343 int sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344{
5345 int i;
5346 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005347 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348
5349 if (hisidx[type] < 0)
5350 return FALSE;
5351 i = hisidx[type];
5352 do
5353 {
5354 if (history[type][i].hisstr == NULL)
5355 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005356
5357 /* For search history, check that the separator character matches as
5358 * well. */
5359 p = history[type][i].hisstr;
5360 if (STRCMP(str, p) == 0
5361 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 {
5363 if (!move_to_front)
5364 return TRUE;
5365 last_i = i;
5366 break;
5367 }
5368 if (--i < 0)
5369 i = hislen - 1;
5370 } while (i != hisidx[type]);
5371
5372 if (last_i >= 0)
5373 {
5374 str = history[type][i].hisstr;
5375 while (i != hisidx[type])
5376 {
5377 if (++i >= hislen)
5378 i = 0;
5379 history[type][last_i] = history[type][i];
5380 last_i = i;
5381 }
5382 history[type][i].hisstr = str;
5383 history[type][i].hisnum = ++hisnum[type];
5384 return TRUE;
5385 }
5386 return FALSE;
5387}
5388
5389/*
5390 * Convert history name (from table above) to its HIST_ equivalent.
5391 * When "name" is empty, return "cmd" history.
5392 * Returns -1 for unknown history name.
5393 */
5394 int
5395get_histtype(name)
5396 char_u *name;
5397{
5398 int i;
5399 int len = (int)STRLEN(name);
5400
5401 /* No argument: use current history. */
5402 if (len == 0)
5403 return hist_char2type(ccline.cmdfirstc);
5404
5405 for (i = 0; history_names[i] != NULL; ++i)
5406 if (STRNICMP(name, history_names[i], len) == 0)
5407 return i;
5408
5409 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5410 return hist_char2type(name[0]);
5411
5412 return -1;
5413}
5414
5415static int last_maptick = -1; /* last seen maptick */
5416
5417/*
5418 * Add the given string to the given history. If the string is already in the
5419 * history then it is moved to the front. "histype" may be one of he HIST_
5420 * values.
5421 */
5422 void
5423add_to_history(histype, new_entry, in_map, sep)
5424 int histype;
5425 char_u *new_entry;
5426 int in_map; /* consider maptick when inside a mapping */
5427 int sep; /* separator character used (search hist) */
5428{
5429 histentry_T *hisptr;
5430 int len;
5431
5432 if (hislen == 0) /* no history */
5433 return;
5434
5435 /*
5436 * Searches inside the same mapping overwrite each other, so that only
5437 * the last line is kept. Be careful not to remove a line that was moved
5438 * down, only lines that were added.
5439 */
5440 if (histype == HIST_SEARCH && in_map)
5441 {
5442 if (maptick == last_maptick)
5443 {
5444 /* Current line is from the same mapping, remove it */
5445 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5446 vim_free(hisptr->hisstr);
5447 hisptr->hisstr = NULL;
5448 hisptr->hisnum = 0;
5449 --hisnum[histype];
5450 if (--hisidx[HIST_SEARCH] < 0)
5451 hisidx[HIST_SEARCH] = hislen - 1;
5452 }
5453 last_maptick = -1;
5454 }
Bram Moolenaar4c402232011-07-27 17:58:46 +02005455 if (!in_history(histype, new_entry, TRUE, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
5457 if (++hisidx[histype] == hislen)
5458 hisidx[histype] = 0;
5459 hisptr = &history[histype][hisidx[histype]];
5460 vim_free(hisptr->hisstr);
5461
5462 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005463 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5465 if (hisptr->hisstr != NULL)
5466 hisptr->hisstr[len + 1] = sep;
5467
5468 hisptr->hisnum = ++hisnum[histype];
5469 if (histype == HIST_SEARCH && in_map)
5470 last_maptick = maptick;
5471 }
5472}
5473
5474#if defined(FEAT_EVAL) || defined(PROTO)
5475
5476/*
5477 * Get identifier of newest history entry.
5478 * "histype" may be one of the HIST_ values.
5479 */
5480 int
5481get_history_idx(histype)
5482 int histype;
5483{
5484 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5485 || hisidx[histype] < 0)
5486 return -1;
5487
5488 return history[histype][hisidx[histype]].hisnum;
5489}
5490
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005491static struct cmdline_info *get_ccline_ptr __ARGS((void));
5492
5493/*
5494 * Get pointer to the command line info to use. cmdline_paste() may clear
5495 * ccline and put the previous value in prev_ccline.
5496 */
5497 static struct cmdline_info *
5498get_ccline_ptr()
5499{
5500 if ((State & CMDLINE) == 0)
5501 return NULL;
5502 if (ccline.cmdbuff != NULL)
5503 return &ccline;
5504 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5505 return &prev_ccline;
5506 return NULL;
5507}
5508
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509/*
5510 * Get the current command line in allocated memory.
5511 * Only works when the command line is being edited.
5512 * Returns NULL when something is wrong.
5513 */
5514 char_u *
5515get_cmdline_str()
5516{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005517 struct cmdline_info *p = get_ccline_ptr();
5518
5519 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005521 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522}
5523
5524/*
5525 * Get the current command line position, counted in bytes.
5526 * Zero is the first position.
5527 * Only works when the command line is being edited.
5528 * Returns -1 when something is wrong.
5529 */
5530 int
5531get_cmdline_pos()
5532{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005533 struct cmdline_info *p = get_ccline_ptr();
5534
5535 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005537 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538}
5539
5540/*
5541 * Set the command line byte position to "pos". Zero is the first position.
5542 * Only works when the command line is being edited.
5543 * Returns 1 when failed, 0 when OK.
5544 */
5545 int
5546set_cmdline_pos(pos)
5547 int pos;
5548{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005549 struct cmdline_info *p = get_ccline_ptr();
5550
5551 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 return 1;
5553
5554 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5555 * changed the command line. */
5556 if (pos < 0)
5557 new_cmdpos = 0;
5558 else
5559 new_cmdpos = pos;
5560 return 0;
5561}
5562
5563/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005564 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005565 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005566 * Only works when the command line is being edited.
5567 * Returns NUL when something is wrong.
5568 */
5569 int
5570get_cmdline_type()
5571{
5572 struct cmdline_info *p = get_ccline_ptr();
5573
5574 if (p == NULL)
5575 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005576 if (p->cmdfirstc == NUL)
5577 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005578 return p->cmdfirstc;
5579}
5580
5581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 * Calculate history index from a number:
5583 * num > 0: seen as identifying number of a history entry
5584 * num < 0: relative position in history wrt newest entry
5585 * "histype" may be one of the HIST_ values.
5586 */
5587 static int
5588calc_hist_idx(histype, num)
5589 int histype;
5590 int num;
5591{
5592 int i;
5593 histentry_T *hist;
5594 int wrapped = FALSE;
5595
5596 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5597 || (i = hisidx[histype]) < 0 || num == 0)
5598 return -1;
5599
5600 hist = history[histype];
5601 if (num > 0)
5602 {
5603 while (hist[i].hisnum > num)
5604 if (--i < 0)
5605 {
5606 if (wrapped)
5607 break;
5608 i += hislen;
5609 wrapped = TRUE;
5610 }
5611 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5612 return i;
5613 }
5614 else if (-num <= hislen)
5615 {
5616 i += num + 1;
5617 if (i < 0)
5618 i += hislen;
5619 if (hist[i].hisstr != NULL)
5620 return i;
5621 }
5622 return -1;
5623}
5624
5625/*
5626 * Get a history entry by its index.
5627 * "histype" may be one of the HIST_ values.
5628 */
5629 char_u *
5630get_history_entry(histype, idx)
5631 int histype;
5632 int idx;
5633{
5634 idx = calc_hist_idx(histype, idx);
5635 if (idx >= 0)
5636 return history[histype][idx].hisstr;
5637 else
5638 return (char_u *)"";
5639}
5640
5641/*
5642 * Clear all entries of a history.
5643 * "histype" may be one of the HIST_ values.
5644 */
5645 int
5646clr_history(histype)
5647 int histype;
5648{
5649 int i;
5650 histentry_T *hisptr;
5651
5652 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5653 {
5654 hisptr = history[histype];
5655 for (i = hislen; i--;)
5656 {
5657 vim_free(hisptr->hisstr);
5658 hisptr->hisnum = 0;
5659 hisptr++->hisstr = NULL;
5660 }
5661 hisidx[histype] = -1; /* mark history as cleared */
5662 hisnum[histype] = 0; /* reset identifier counter */
5663 return OK;
5664 }
5665 return FAIL;
5666}
5667
5668/*
5669 * Remove all entries matching {str} from a history.
5670 * "histype" may be one of the HIST_ values.
5671 */
5672 int
5673del_history_entry(histype, str)
5674 int histype;
5675 char_u *str;
5676{
5677 regmatch_T regmatch;
5678 histentry_T *hisptr;
5679 int idx;
5680 int i;
5681 int last;
5682 int found = FALSE;
5683
5684 regmatch.regprog = NULL;
5685 regmatch.rm_ic = FALSE; /* always match case */
5686 if (hislen != 0
5687 && histype >= 0
5688 && histype < HIST_COUNT
5689 && *str != NUL
5690 && (idx = hisidx[histype]) >= 0
5691 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5692 != NULL)
5693 {
5694 i = last = idx;
5695 do
5696 {
5697 hisptr = &history[histype][i];
5698 if (hisptr->hisstr == NULL)
5699 break;
5700 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5701 {
5702 found = TRUE;
5703 vim_free(hisptr->hisstr);
5704 hisptr->hisstr = NULL;
5705 hisptr->hisnum = 0;
5706 }
5707 else
5708 {
5709 if (i != last)
5710 {
5711 history[histype][last] = *hisptr;
5712 hisptr->hisstr = NULL;
5713 hisptr->hisnum = 0;
5714 }
5715 if (--last < 0)
5716 last += hislen;
5717 }
5718 if (--i < 0)
5719 i += hislen;
5720 } while (i != idx);
5721 if (history[histype][idx].hisstr == NULL)
5722 hisidx[histype] = -1;
5723 }
5724 vim_free(regmatch.regprog);
5725 return found;
5726}
5727
5728/*
5729 * Remove an indexed entry from a history.
5730 * "histype" may be one of the HIST_ values.
5731 */
5732 int
5733del_history_idx(histype, idx)
5734 int histype;
5735 int idx;
5736{
5737 int i, j;
5738
5739 i = calc_hist_idx(histype, idx);
5740 if (i < 0)
5741 return FALSE;
5742 idx = hisidx[histype];
5743 vim_free(history[histype][i].hisstr);
5744
5745 /* When deleting the last added search string in a mapping, reset
5746 * last_maptick, so that the last added search string isn't deleted again.
5747 */
5748 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5749 last_maptick = -1;
5750
5751 while (i != idx)
5752 {
5753 j = (i + 1) % hislen;
5754 history[histype][i] = history[histype][j];
5755 i = j;
5756 }
5757 history[histype][i].hisstr = NULL;
5758 history[histype][i].hisnum = 0;
5759 if (--i < 0)
5760 i += hislen;
5761 hisidx[histype] = i;
5762 return TRUE;
5763}
5764
5765#endif /* FEAT_EVAL */
5766
5767#if defined(FEAT_CRYPT) || defined(PROTO)
5768/*
5769 * Very specific function to remove the value in ":set key=val" from the
5770 * history.
5771 */
5772 void
5773remove_key_from_history()
5774{
5775 char_u *p;
5776 int i;
5777
5778 i = hisidx[HIST_CMD];
5779 if (i < 0)
5780 return;
5781 p = history[HIST_CMD][i].hisstr;
5782 if (p != NULL)
5783 for ( ; *p; ++p)
5784 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5785 {
5786 p = vim_strchr(p + 3, '=');
5787 if (p == NULL)
5788 break;
5789 ++p;
5790 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5791 if (p[i] == '\\' && p[i + 1])
5792 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005793 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 --p;
5795 }
5796}
5797#endif
5798
5799#endif /* FEAT_CMDHIST */
5800
5801#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5802/*
5803 * Get indices "num1,num2" that specify a range within a list (not a range of
5804 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5805 * Returns OK if parsed successfully, otherwise FAIL.
5806 */
5807 int
5808get_list_range(str, num1, num2)
5809 char_u **str;
5810 int *num1;
5811 int *num2;
5812{
5813 int len;
5814 int first = FALSE;
5815 long num;
5816
5817 *str = skipwhite(*str);
5818 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5819 {
5820 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5821 *str += len;
5822 *num1 = (int)num;
5823 first = TRUE;
5824 }
5825 *str = skipwhite(*str);
5826 if (**str == ',') /* parse "to" part of range */
5827 {
5828 *str = skipwhite(*str + 1);
5829 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5830 if (len > 0)
5831 {
5832 *num2 = (int)num;
5833 *str = skipwhite(*str + len);
5834 }
5835 else if (!first) /* no number given at all */
5836 return FAIL;
5837 }
5838 else if (first) /* only one number given */
5839 *num2 = *num1;
5840 return OK;
5841}
5842#endif
5843
5844#if defined(FEAT_CMDHIST) || defined(PROTO)
5845/*
5846 * :history command - print a history
5847 */
5848 void
5849ex_history(eap)
5850 exarg_T *eap;
5851{
5852 histentry_T *hist;
5853 int histype1 = HIST_CMD;
5854 int histype2 = HIST_CMD;
5855 int hisidx1 = 1;
5856 int hisidx2 = -1;
5857 int idx;
5858 int i, j, k;
5859 char_u *end;
5860 char_u *arg = eap->arg;
5861
5862 if (hislen == 0)
5863 {
5864 MSG(_("'history' option is zero"));
5865 return;
5866 }
5867
5868 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5869 {
5870 end = arg;
5871 while (ASCII_ISALPHA(*end)
5872 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5873 end++;
5874 i = *end;
5875 *end = NUL;
5876 histype1 = get_histtype(arg);
5877 if (histype1 == -1)
5878 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005879 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 {
5881 histype1 = 0;
5882 histype2 = HIST_COUNT-1;
5883 }
5884 else
5885 {
5886 *end = i;
5887 EMSG(_(e_trailing));
5888 return;
5889 }
5890 }
5891 else
5892 histype2 = histype1;
5893 *end = i;
5894 }
5895 else
5896 end = arg;
5897 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5898 {
5899 EMSG(_(e_trailing));
5900 return;
5901 }
5902
5903 for (; !got_int && histype1 <= histype2; ++histype1)
5904 {
5905 STRCPY(IObuff, "\n # ");
5906 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5907 MSG_PUTS_TITLE(IObuff);
5908 idx = hisidx[histype1];
5909 hist = history[histype1];
5910 j = hisidx1;
5911 k = hisidx2;
5912 if (j < 0)
5913 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5914 if (k < 0)
5915 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5916 if (idx >= 0 && j <= k)
5917 for (i = idx + 1; !got_int; ++i)
5918 {
5919 if (i == hislen)
5920 i = 0;
5921 if (hist[i].hisstr != NULL
5922 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5923 {
5924 msg_putchar('\n');
5925 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5926 hist[i].hisnum);
5927 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5928 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005929 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 else
5931 STRCAT(IObuff, hist[i].hisstr);
5932 msg_outtrans(IObuff);
5933 out_flush();
5934 }
5935 if (i == idx)
5936 break;
5937 }
5938 }
5939}
5940#endif
5941
5942#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5943static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5944static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5945static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5946static int viminfo_add_at_front = FALSE;
5947
5948static int hist_type2char __ARGS((int type, int use_question));
5949
5950/*
5951 * Translate a history type number to the associated character.
5952 */
5953 static int
5954hist_type2char(type, use_question)
5955 int type;
5956 int use_question; /* use '?' instead of '/' */
5957{
5958 if (type == HIST_CMD)
5959 return ':';
5960 if (type == HIST_SEARCH)
5961 {
5962 if (use_question)
5963 return '?';
5964 else
5965 return '/';
5966 }
5967 if (type == HIST_EXPR)
5968 return '=';
5969 return '@';
5970}
5971
5972/*
5973 * Prepare for reading the history from the viminfo file.
5974 * This allocates history arrays to store the read history lines.
5975 */
5976 void
5977prepare_viminfo_history(asklen)
5978 int asklen;
5979{
5980 int i;
5981 int num;
5982 int type;
5983 int len;
5984
5985 init_history();
5986 viminfo_add_at_front = (asklen != 0);
5987 if (asklen > hislen)
5988 asklen = hislen;
5989
5990 for (type = 0; type < HIST_COUNT; ++type)
5991 {
5992 /*
5993 * Count the number of empty spaces in the history list. If there are
5994 * more spaces available than we request, then fill them up.
5995 */
5996 for (i = 0, num = 0; i < hislen; i++)
5997 if (history[type][i].hisstr == NULL)
5998 num++;
5999 len = asklen;
6000 if (num > len)
6001 len = num;
6002 if (len <= 0)
6003 viminfo_history[type] = NULL;
6004 else
6005 viminfo_history[type] =
6006 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6007 if (viminfo_history[type] == NULL)
6008 len = 0;
6009 viminfo_hislen[type] = len;
6010 viminfo_hisidx[type] = 0;
6011 }
6012}
6013
6014/*
6015 * Accept a line from the viminfo, store it in the history array when it's
6016 * new.
6017 */
6018 int
6019read_viminfo_history(virp)
6020 vir_T *virp;
6021{
6022 int type;
6023 long_u len;
6024 char_u *val;
6025 char_u *p;
6026
6027 type = hist_char2type(virp->vir_line[0]);
6028 if (viminfo_hisidx[type] < viminfo_hislen[type])
6029 {
6030 val = viminfo_readstring(virp, 1, TRUE);
6031 if (val != NULL && *val != NUL)
6032 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006033 int sep = (*val == ' ' ? NUL : *val);
6034
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006036 viminfo_add_at_front, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 {
6038 /* Need to re-allocate to append the separator byte. */
6039 len = STRLEN(val);
6040 p = lalloc(len + 2, TRUE);
6041 if (p != NULL)
6042 {
6043 if (type == HIST_SEARCH)
6044 {
6045 /* Search entry: Move the separator from the first
6046 * column to after the NUL. */
6047 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006048 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 }
6050 else
6051 {
6052 /* Not a search entry: No separator in the viminfo
6053 * file, add a NUL separator. */
6054 mch_memmove(p, val, (size_t)len + 1);
6055 p[len + 1] = NUL;
6056 }
6057 viminfo_history[type][viminfo_hisidx[type]++] = p;
6058 }
6059 }
6060 }
6061 vim_free(val);
6062 }
6063 return viminfo_readline(virp);
6064}
6065
6066 void
6067finish_viminfo_history()
6068{
6069 int idx;
6070 int i;
6071 int type;
6072
6073 for (type = 0; type < HIST_COUNT; ++type)
6074 {
6075 if (history[type] == NULL)
6076 return;
6077 idx = hisidx[type] + viminfo_hisidx[type];
6078 if (idx >= hislen)
6079 idx -= hislen;
6080 else if (idx < 0)
6081 idx = hislen - 1;
6082 if (viminfo_add_at_front)
6083 hisidx[type] = idx;
6084 else
6085 {
6086 if (hisidx[type] == -1)
6087 hisidx[type] = hislen - 1;
6088 do
6089 {
6090 if (history[type][idx].hisstr != NULL)
6091 break;
6092 if (++idx == hislen)
6093 idx = 0;
6094 } while (idx != hisidx[type]);
6095 if (idx != hisidx[type] && --idx < 0)
6096 idx = hislen - 1;
6097 }
6098 for (i = 0; i < viminfo_hisidx[type]; i++)
6099 {
6100 vim_free(history[type][idx].hisstr);
6101 history[type][idx].hisstr = viminfo_history[type][i];
6102 if (--idx < 0)
6103 idx = hislen - 1;
6104 }
6105 idx += 1;
6106 idx %= hislen;
6107 for (i = 0; i < viminfo_hisidx[type]; i++)
6108 {
6109 history[type][idx++].hisnum = ++hisnum[type];
6110 idx %= hislen;
6111 }
6112 vim_free(viminfo_history[type]);
6113 viminfo_history[type] = NULL;
6114 }
6115}
6116
6117 void
6118write_viminfo_history(fp)
6119 FILE *fp;
6120{
6121 int i;
6122 int type;
6123 int num_saved;
6124 char_u *p;
6125 int c;
6126
6127 init_history();
6128 if (hislen == 0)
6129 return;
6130 for (type = 0; type < HIST_COUNT; ++type)
6131 {
6132 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6133 if (num_saved == 0)
6134 continue;
6135 if (num_saved < 0) /* Use default */
6136 num_saved = hislen;
6137 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6138 type == HIST_CMD ? _("Command Line") :
6139 type == HIST_SEARCH ? _("Search String") :
6140 type == HIST_EXPR ? _("Expression") :
6141 _("Input Line"));
6142 if (num_saved > hislen)
6143 num_saved = hislen;
6144 i = hisidx[type];
6145 if (i >= 0)
6146 while (num_saved--)
6147 {
6148 p = history[type][i].hisstr;
6149 if (p != NULL)
6150 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006151 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 /* For the search history: put the separator in the second
6153 * column; use a space if there isn't one. */
6154 if (type == HIST_SEARCH)
6155 {
6156 c = p[STRLEN(p) + 1];
6157 putc(c == NUL ? ' ' : c, fp);
6158 }
6159 viminfo_writestring(fp, p);
6160 }
6161 if (--i < 0)
6162 i = hislen - 1;
6163 }
6164 }
6165}
6166#endif /* FEAT_VIMINFO */
6167
6168#if defined(FEAT_FKMAP) || defined(PROTO)
6169/*
6170 * Write a character at the current cursor+offset position.
6171 * It is directly written into the command buffer block.
6172 */
6173 void
6174cmd_pchar(c, offset)
6175 int c, offset;
6176{
6177 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6178 {
6179 EMSG(_("E198: cmd_pchar beyond the command length"));
6180 return;
6181 }
6182 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6183 ccline.cmdbuff[ccline.cmdlen] = NUL;
6184}
6185
6186 int
6187cmd_gchar(offset)
6188 int offset;
6189{
6190 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6191 {
6192 /* EMSG(_("cmd_gchar beyond the command length")); */
6193 return NUL;
6194 }
6195 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6196}
6197#endif
6198
6199#if defined(FEAT_CMDWIN) || defined(PROTO)
6200/*
6201 * Open a window on the current command line and history. Allow editing in
6202 * the window. Returns when the window is closed.
6203 * Returns:
6204 * CR if the command is to be executed
6205 * Ctrl_C if it is to be abandoned
6206 * K_IGNORE if editing continues
6207 */
6208 static int
6209ex_window()
6210{
6211 struct cmdline_info save_ccline;
6212 buf_T *old_curbuf = curbuf;
6213 win_T *old_curwin = curwin;
6214 buf_T *bp;
6215 win_T *wp;
6216 int i;
6217 linenr_T lnum;
6218 int histtype;
6219 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006220#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 int save_restart_edit = restart_edit;
6224 int save_State = State;
6225 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006226#ifdef FEAT_RIGHTLEFT
6227 int save_cmdmsg_rl = cmdmsg_rl;
6228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229
6230 /* Can't do this recursively. Can't do it when typing a password. */
6231 if (cmdwin_type != 0
6232# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6233 || cmdline_star > 0
6234# endif
6235 )
6236 {
6237 beep_flush();
6238 return K_IGNORE;
6239 }
6240
6241 /* Save current window sizes. */
6242 win_size_save(&winsizes);
6243
6244# ifdef FEAT_AUTOCMD
6245 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006246 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006248 /* don't use a new tab page */
6249 cmdmod.tab = 0;
6250
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 /* Create a window for the command-line buffer. */
6252 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6253 {
6254 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006255# ifdef FEAT_AUTOCMD
6256 unblock_autocmds();
6257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 return K_IGNORE;
6259 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006260 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261
6262 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006263 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006264 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6266 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6267 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006268#ifdef FEAT_FOLDING
6269 curwin->w_p_fen = FALSE;
6270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006272 curwin->w_p_rl = cmdmsg_rl;
6273 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006275 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
6277# ifdef FEAT_AUTOCMD
6278 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006279 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280# endif
6281
Bram Moolenaar46152342005-09-07 21:18:43 +00006282 /* Showing the prompt may have set need_wait_return, reset it. */
6283 need_wait_return = FALSE;
6284
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006285 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6287 {
6288 if (p_wc == TAB)
6289 {
6290 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6291 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6292 }
6293 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6294 }
6295
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006296 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6297 * sets 'textwidth' to 78). */
6298 curbuf->b_p_tw = 0;
6299
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 /* Fill the buffer with the history. */
6301 init_history();
6302 if (hislen > 0)
6303 {
6304 i = hisidx[histtype];
6305 if (i >= 0)
6306 {
6307 lnum = 0;
6308 do
6309 {
6310 if (++i == hislen)
6311 i = 0;
6312 if (history[histtype][i].hisstr != NULL)
6313 ml_append(lnum++, history[histtype][i].hisstr,
6314 (colnr_T)0, FALSE);
6315 }
6316 while (i != hisidx[histtype]);
6317 }
6318 }
6319
6320 /* Replace the empty last line with the current command-line and put the
6321 * cursor there. */
6322 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6323 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6324 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006325 changed_line_abv_curs();
6326 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006327 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328
6329 /* Save the command line info, can be used recursively. */
6330 save_ccline = ccline;
6331 ccline.cmdbuff = NULL;
6332 ccline.cmdprompt = NULL;
6333
6334 /* No Ex mode here! */
6335 exmode_active = 0;
6336
6337 State = NORMAL;
6338# ifdef FEAT_MOUSE
6339 setmouse();
6340# endif
6341
6342# ifdef FEAT_AUTOCMD
6343 /* Trigger CmdwinEnter autocommands. */
6344 typestr[0] = cmdwin_type;
6345 typestr[1] = NUL;
6346 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006347 if (restart_edit != 0) /* autocmd with ":startinsert" */
6348 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349# endif
6350
6351 i = RedrawingDisabled;
6352 RedrawingDisabled = 0;
6353
6354 /*
6355 * Call the main loop until <CR> or CTRL-C is typed.
6356 */
6357 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006358 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359
6360 RedrawingDisabled = i;
6361
6362# ifdef FEAT_AUTOCMD
6363 /* Trigger CmdwinLeave autocommands. */
6364 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6365# endif
6366
Bram Moolenaarccc18222007-05-10 18:25:20 +00006367 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 ccline = save_ccline;
6369 cmdwin_type = 0;
6370
6371 exmode_active = save_exmode;
6372
Bram Moolenaarf9821062008-06-20 16:31:07 +00006373 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 * this happens! */
6375 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6376 {
6377 cmdwin_result = Ctrl_C;
6378 EMSG(_("E199: Active window or buffer deleted"));
6379 }
6380 else
6381 {
6382# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6383 /* autocmds may abort script processing */
6384 if (aborting() && cmdwin_result != K_IGNORE)
6385 cmdwin_result = Ctrl_C;
6386# endif
6387 /* Set the new command line from the cmdline buffer. */
6388 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006389 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006391 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6392
6393 if (histtype == HIST_CMD)
6394 {
6395 /* Execute the command directly. */
6396 ccline.cmdbuff = vim_strsave((char_u *)p);
6397 cmdwin_result = CAR;
6398 }
6399 else
6400 {
6401 /* First need to cancel what we were doing. */
6402 ccline.cmdbuff = NULL;
6403 stuffcharReadbuff(':');
6404 stuffReadbuff((char_u *)p);
6405 stuffcharReadbuff(CAR);
6406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 }
6408 else if (cmdwin_result == K_XF2) /* :qa typed */
6409 {
6410 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6411 cmdwin_result = CAR;
6412 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006413 else if (cmdwin_result == Ctrl_C)
6414 {
6415 /* :q or :close, don't execute any command
6416 * and don't modify the cmd window. */
6417 ccline.cmdbuff = NULL;
6418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 else
6420 ccline.cmdbuff = vim_strsave(ml_get_curline());
6421 if (ccline.cmdbuff == NULL)
6422 cmdwin_result = Ctrl_C;
6423 else
6424 {
6425 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6426 ccline.cmdbufflen = ccline.cmdlen + 1;
6427 ccline.cmdpos = curwin->w_cursor.col;
6428 if (ccline.cmdpos > ccline.cmdlen)
6429 ccline.cmdpos = ccline.cmdlen;
6430 if (cmdwin_result == K_IGNORE)
6431 {
6432 set_cmdspos_cursor();
6433 redrawcmd();
6434 }
6435 }
6436
6437# ifdef FEAT_AUTOCMD
6438 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006439 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440# endif
6441 wp = curwin;
6442 bp = curbuf;
6443 win_goto(old_curwin);
6444 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006445
6446 /* win_close() may have already wiped the buffer when 'bh' is
6447 * set to 'wipe' */
6448 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006449 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450
6451 /* Restore window sizes. */
6452 win_size_restore(&winsizes);
6453
6454# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006455 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456# endif
6457 }
6458
6459 ga_clear(&winsizes);
6460 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006461# ifdef FEAT_RIGHTLEFT
6462 cmdmsg_rl = save_cmdmsg_rl;
6463# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464
6465 State = save_State;
6466# ifdef FEAT_MOUSE
6467 setmouse();
6468# endif
6469
6470 return cmdwin_result;
6471}
6472#endif /* FEAT_CMDWIN */
6473
6474/*
6475 * Used for commands that either take a simple command string argument, or:
6476 * cmd << endmarker
6477 * {script}
6478 * endmarker
6479 * Returns a pointer to allocated memory with {script} or NULL.
6480 */
6481 char_u *
6482script_get(eap, cmd)
6483 exarg_T *eap;
6484 char_u *cmd;
6485{
6486 char_u *theline;
6487 char *end_pattern = NULL;
6488 char dot[] = ".";
6489 garray_T ga;
6490
6491 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6492 return NULL;
6493
6494 ga_init2(&ga, 1, 0x400);
6495
6496 if (cmd[2] != NUL)
6497 end_pattern = (char *)skipwhite(cmd + 2);
6498 else
6499 end_pattern = dot;
6500
6501 for (;;)
6502 {
6503 theline = eap->getline(
6504#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006505 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506#endif
6507 NUL, eap->cookie, 0);
6508
6509 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006510 {
6511 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514
6515 ga_concat(&ga, theline);
6516 ga_append(&ga, '\n');
6517 vim_free(theline);
6518 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006519 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
6521 return (char_u *)ga.ga_data;
6522}