blob: e6ed44a41ee06c61fa4a4e16a81c2f9f9d00d332 [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
70static int in_history __ARGS((int, char_u *, int));
71# 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 Moolenaar071d4272004-06-13 20:20:40 +0000113static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname));
114# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
115static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000116static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117# endif
118#endif
119
120#ifdef FEAT_CMDWIN
121static int ex_window __ARGS((void));
122#endif
123
124/*
125 * getcmdline() - accept a command line starting with firstc.
126 *
127 * firstc == ':' get ":" command line.
128 * firstc == '/' or '?' get search pattern
129 * firstc == '=' get expression
130 * firstc == '@' get text for input() function
131 * firstc == '>' get text for debug mode
132 * firstc == NUL get text for :insert command
133 * firstc == -1 like NUL, and break on CTRL-C
134 *
135 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
136 * command line.
137 *
138 * Careful: getcmdline() can be called recursively!
139 *
140 * Return pointer to allocated string if there is a commandline, NULL
141 * otherwise.
142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 char_u *
144getcmdline(firstc, count, indent)
145 int firstc;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000146 long count UNUSED; /* only used for incremental search */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 int indent; /* indent for inside conditionals */
148{
149 int c;
150 int i;
151 int j;
152 int gotesc = FALSE; /* TRUE when <ESC> just typed */
153 int do_abbr; /* when TRUE check for abbr. */
154#ifdef FEAT_CMDHIST
155 char_u *lookfor = NULL; /* string to match */
156 int hiscnt; /* current history line in use */
157 int histype; /* history type to be used */
158#endif
159#ifdef FEAT_SEARCH_EXTRA
160 pos_T old_cursor;
161 colnr_T old_curswant;
162 colnr_T old_leftcol;
163 linenr_T old_topline;
164# ifdef FEAT_DIFF
165 int old_topfill;
166# endif
167 linenr_T old_botline;
168 int did_incsearch = FALSE;
169 int incsearch_postponed = FALSE;
170#endif
171 int did_wild_list = FALSE; /* did wild_list() recently */
172 int wim_index = 0; /* index in wim_flags[] */
173 int res;
174 int save_msg_scroll = msg_scroll;
175 int save_State = State; /* remember State when called */
176 int some_key_typed = FALSE; /* one of the keys was typed */
177#ifdef FEAT_MOUSE
178 /* mouse drag and release events are ignored, unless they are
179 * preceded with a mouse down event */
180 int ignore_drag_release = TRUE;
181#endif
182#ifdef FEAT_EVAL
183 int break_ctrl_c = FALSE;
184#endif
185 expand_T xpc;
186 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000187#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
188 /* Everything that may work recursively should save and restore the
189 * current command line in save_ccline. That includes update_screen(), a
190 * custom status line may invoke ":normal". */
191 struct cmdline_info save_ccline;
192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194#ifdef FEAT_SNIFF
195 want_sniff_request = 0;
196#endif
197#ifdef FEAT_EVAL
198 if (firstc == -1)
199 {
200 firstc = NUL;
201 break_ctrl_c = TRUE;
202 }
203#endif
204#ifdef FEAT_RIGHTLEFT
205 /* start without Hebrew mapping for a command line */
206 if (firstc == ':' || firstc == '=' || firstc == '>')
207 cmd_hkmap = 0;
208#endif
209
210 ccline.overstrike = FALSE; /* always start in insert mode */
211#ifdef FEAT_SEARCH_EXTRA
212 old_cursor = curwin->w_cursor; /* needs to be restored later */
213 old_curswant = curwin->w_curswant;
214 old_leftcol = curwin->w_leftcol;
215 old_topline = curwin->w_topline;
216# ifdef FEAT_DIFF
217 old_topfill = curwin->w_topfill;
218# endif
219 old_botline = curwin->w_botline;
220#endif
221
222 /*
223 * set some variables for redrawcmd()
224 */
225 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000226 ccline.cmdindent = (firstc > 0 ? indent : 0);
227
228 /* alloc initial ccline.cmdbuff */
229 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 if (ccline.cmdbuff == NULL)
231 return NULL; /* out of memory */
232 ccline.cmdlen = ccline.cmdpos = 0;
233 ccline.cmdbuff[0] = NUL;
234
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000235 /* autoindent for :insert and :append */
236 if (firstc <= 0)
237 {
238 copy_spaces(ccline.cmdbuff, indent);
239 ccline.cmdbuff[indent] = NUL;
240 ccline.cmdpos = indent;
241 ccline.cmdspos = indent;
242 ccline.cmdlen = indent;
243 }
244
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000246 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247
248#ifdef FEAT_RIGHTLEFT
249 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
250 && (firstc == '/' || firstc == '?'))
251 cmdmsg_rl = TRUE;
252 else
253 cmdmsg_rl = FALSE;
254#endif
255
256 redir_off = TRUE; /* don't redirect the typed command */
257 if (!cmd_silent)
258 {
259 i = msg_scrolled;
260 msg_scrolled = 0; /* avoid wait_return message */
261 gotocmdline(TRUE);
262 msg_scrolled += i;
263 redrawcmdprompt(); /* draw prompt or indent */
264 set_cmdspos();
265 }
266 xpc.xp_context = EXPAND_NOTHING;
267 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000268#ifndef BACKSLASH_IN_FILENAME
269 xpc.xp_shell = FALSE;
270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000272#if defined(FEAT_EVAL)
273 if (ccline.input_fn)
274 {
275 xpc.xp_context = ccline.xp_context;
276 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000277# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000278 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000279# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000280 }
281#endif
282
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 /*
284 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
285 * doing ":@0" when register 0 doesn't contain a CR.
286 */
287 msg_scroll = FALSE;
288
289 State = CMDLINE;
290
291 if (firstc == '/' || firstc == '?' || firstc == '@')
292 {
293 /* Use ":lmap" mappings for search pattern and input(). */
294 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
295 b_im_ptr = &curbuf->b_p_iminsert;
296 else
297 b_im_ptr = &curbuf->b_p_imsearch;
298 if (*b_im_ptr == B_IMODE_LMAP)
299 State |= LANGMAP;
300#ifdef USE_IM_CONTROL
301 im_set_active(*b_im_ptr == B_IMODE_IM);
302#endif
303 }
304#ifdef USE_IM_CONTROL
305 else if (p_imcmdline)
306 im_set_active(TRUE);
307#endif
308
309#ifdef FEAT_MOUSE
310 setmouse();
311#endif
312#ifdef CURSOR_SHAPE
313 ui_cursor_shape(); /* may show different cursor shape */
314#endif
315
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000316 /* When inside an autocommand for writing "exiting" may be set and
317 * terminal mode set to cooked. Need to set raw mode here then. */
318 settmode(TMODE_RAW);
319
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifdef FEAT_CMDHIST
321 init_history();
322 hiscnt = hislen; /* set hiscnt to impossible history value */
323 histype = hist_char2type(firstc);
324#endif
325
326#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000327 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#endif
329
330 /*
331 * Collect the command string, handling editing keys.
332 */
333 for (;;)
334 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000335 redir_off = TRUE; /* Don't redirect the typed command.
336 Repeated, because a ":redir" inside
337 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338#ifdef USE_ON_FLY_SCROLL
339 dont_scroll = FALSE; /* allow scrolling here */
340#endif
341 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
342
343 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000344
345 /* Get a character. Ignore K_IGNORE, it should not do anything, such
346 * as stop completion. */
347 do
348 {
349 c = safe_vgetc();
350 } while (c == K_IGNORE);
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 if (KeyTyped)
353 {
354 some_key_typed = TRUE;
355#ifdef FEAT_RIGHTLEFT
356 if (cmd_hkmap)
357 c = hkmap(c);
358# ifdef FEAT_FKMAP
359 if (cmd_fkmap)
360 c = cmdl_fkmap(c);
361# endif
362 if (cmdmsg_rl && !KeyStuffed)
363 {
364 /* Invert horizontal movements and operations. Only when
365 * typed by the user directly, not when the result of a
366 * mapping. */
367 switch (c)
368 {
369 case K_RIGHT: c = K_LEFT; break;
370 case K_S_RIGHT: c = K_S_LEFT; break;
371 case K_C_RIGHT: c = K_C_LEFT; break;
372 case K_LEFT: c = K_RIGHT; break;
373 case K_S_LEFT: c = K_S_RIGHT; break;
374 case K_C_LEFT: c = K_C_RIGHT; break;
375 }
376 }
377#endif
378 }
379
380 /*
381 * Ignore got_int when CTRL-C was typed here.
382 * Don't ignore it in :global, we really need to break then, e.g., for
383 * ":g/pat/normal /pat" (without the <CR>).
384 * Don't ignore it for the input() function.
385 */
386 if ((c == Ctrl_C
387#ifdef UNIX
388 || c == intr_char
389#endif
390 )
391#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
392 && firstc != '@'
393#endif
394#ifdef FEAT_EVAL
395 && !break_ctrl_c
396#endif
397 && !global_busy)
398 got_int = FALSE;
399
400#ifdef FEAT_CMDHIST
401 /* free old command line when finished moving around in the history
402 * list */
403 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000404 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000405 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 && c != K_PAGEDOWN && c != K_PAGEUP
407 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000408 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
410 {
411 vim_free(lookfor);
412 lookfor = NULL;
413 }
414#endif
415
416 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000417 * When there are matching completions to select <S-Tab> works like
418 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000420 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 c = Ctrl_P;
422
423#ifdef FEAT_WILDMENU
424 /* Special translations for 'wildmenu' */
425 if (did_wild_list && p_wmnu)
426 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000427 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000429 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 c = Ctrl_N;
431 }
432 /* Hitting CR after "emenu Name.": complete submenu */
433 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
434 && ccline.cmdpos > 1
435 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
436 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
437 && (c == '\n' || c == '\r' || c == K_KENTER))
438 c = K_DOWN;
439#endif
440
441 /* free expanded names when finished walking through matches */
442 if (xpc.xp_numfiles != -1
443 && !(c == p_wc && KeyTyped) && c != p_wcm
444 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
445 && c != Ctrl_L)
446 {
447 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
448 did_wild_list = FALSE;
449#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000450 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451#endif
452 xpc.xp_context = EXPAND_NOTHING;
453 wim_index = 0;
454#ifdef FEAT_WILDMENU
455 if (p_wmnu && wild_menu_showing != 0)
456 {
457 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000458 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000459
460 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000461 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
463 if (wild_menu_showing == WM_SCROLLED)
464 {
465 /* Entered command line, move it up */
466 cmdline_row--;
467 redrawcmd();
468 }
469 else if (save_p_ls != -1)
470 {
471 /* restore 'laststatus' and 'winminheight' */
472 p_ls = save_p_ls;
473 p_wmh = save_p_wmh;
474 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000475 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000477 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 redrawcmd();
479 save_p_ls = -1;
480 }
481 else
482 {
483# ifdef FEAT_VERTSPLIT
484 win_redraw_last_status(topframe);
485# else
486 lastwin->w_redr_status = TRUE;
487# endif
488 redraw_statuslines();
489 }
490 KeyTyped = skt;
491 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000492 if (ccline.input_fn)
493 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 }
495#endif
496 }
497
498#ifdef FEAT_WILDMENU
499 /* Special translations for 'wildmenu' */
500 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
501 {
502 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000503 if (c == K_DOWN && ccline.cmdpos > 0
504 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000506 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {
508 /* Hitting <Up>: Remove one submenu name in front of the
509 * cursor */
510 int found = FALSE;
511
512 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
513 i = 0;
514 while (--j > 0)
515 {
516 /* check for start of menu name */
517 if (ccline.cmdbuff[j] == ' '
518 && ccline.cmdbuff[j - 1] != '\\')
519 {
520 i = j + 1;
521 break;
522 }
523 /* check for start of submenu name */
524 if (ccline.cmdbuff[j] == '.'
525 && ccline.cmdbuff[j - 1] != '\\')
526 {
527 if (found)
528 {
529 i = j + 1;
530 break;
531 }
532 else
533 found = TRUE;
534 }
535 }
536 if (i > 0)
537 cmdline_del(i);
538 c = p_wc;
539 xpc.xp_context = EXPAND_NOTHING;
540 }
541 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000542 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000543 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000544 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 {
546 char_u upseg[5];
547
548 upseg[0] = PATHSEP;
549 upseg[1] = '.';
550 upseg[2] = '.';
551 upseg[3] = PATHSEP;
552 upseg[4] = NUL;
553
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000554 if (c == K_DOWN
555 && ccline.cmdpos > 0
556 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
557 && (ccline.cmdpos < 3
558 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
560 {
561 /* go down a directory */
562 c = p_wc;
563 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000564 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 {
566 /* If in a direct ancestor, strip off one ../ to go down */
567 int found = FALSE;
568
569 j = ccline.cmdpos;
570 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
571 while (--j > i)
572 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000573#ifdef FEAT_MBYTE
574 if (has_mbyte)
575 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (vim_ispathsep(ccline.cmdbuff[j]))
578 {
579 found = TRUE;
580 break;
581 }
582 }
583 if (found
584 && ccline.cmdbuff[j - 1] == '.'
585 && ccline.cmdbuff[j - 2] == '.'
586 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
587 {
588 cmdline_del(j - 2);
589 c = p_wc;
590 }
591 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000592 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 {
594 /* go up a directory */
595 int found = FALSE;
596
597 j = ccline.cmdpos - 1;
598 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
599 while (--j > i)
600 {
601#ifdef FEAT_MBYTE
602 if (has_mbyte)
603 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
604#endif
605 if (vim_ispathsep(ccline.cmdbuff[j])
606#ifdef BACKSLASH_IN_FILENAME
607 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
608 == NULL
609#endif
610 )
611 {
612 if (found)
613 {
614 i = j + 1;
615 break;
616 }
617 else
618 found = TRUE;
619 }
620 }
621
622 if (!found)
623 j = i;
624 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
625 j += 4;
626 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
627 && j == i)
628 j += 3;
629 else
630 j = 0;
631 if (j > 0)
632 {
633 /* TODO this is only for DOS/UNIX systems - need to put in
634 * machine-specific stuff here and in upseg init */
635 cmdline_del(j);
636 put_on_cmdline(upseg + 1, 3, FALSE);
637 }
638 else if (ccline.cmdpos > i)
639 cmdline_del(i);
640 c = p_wc;
641 }
642 }
643#if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
644 if (p_wmnu
645 && (xpc.xp_context == EXPAND_FILES
646 || xpc.xp_context == EXPAND_MENUNAMES)
647 && (c == K_UP || c == K_DOWN))
648 xpc.xp_context = EXPAND_NOTHING;
649#endif
650
651#endif /* FEAT_WILDMENU */
652
653 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
654 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
655 if (c == Ctrl_BSL)
656 {
657 ++no_mapping;
658 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000659 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 --no_mapping;
661 --allow_keys;
662 /* CTRL-\ e doesn't work when obtaining an expression. */
663 if (c != Ctrl_N && c != Ctrl_G
664 && (c != 'e' || ccline.cmdfirstc == '='))
665 {
666 vungetc(c);
667 c = Ctrl_BSL;
668 }
669#ifdef FEAT_EVAL
670 else if (c == 'e')
671 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000672 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673
674 /*
675 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000676 * Need to save and restore the current command line, to be
677 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 */
679 if (ccline.cmdpos == ccline.cmdlen)
680 new_cmdpos = 99999; /* keep it at the end */
681 else
682 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000683
684 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000686 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 if (c == '=')
688 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000689 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000690 * to avoid nasty things like going to another buffer when
691 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000692 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000693 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000695 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000696 restore_cmdline(&save_ccline);
697
698 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000700 ccline.cmdlen = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 STRCPY(ccline.cmdbuff, p);
702 vim_free(p);
703
704 /* Restore the cursor or use the position set with
705 * set_cmdline_pos(). */
706 if (new_cmdpos > ccline.cmdlen)
707 ccline.cmdpos = ccline.cmdlen;
708 else
709 ccline.cmdpos = new_cmdpos;
710
711 KeyTyped = FALSE; /* Don't do p_wc completion. */
712 redrawcmd();
713 goto cmdline_changed;
714 }
715 }
716 beep_flush();
717 c = ESC;
718 }
719#endif
720 else
721 {
722 if (c == Ctrl_G && p_im && restart_edit == 0)
723 restart_edit = 'a';
724 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
725 in history */
726 goto returncmd; /* back to Normal mode */
727 }
728 }
729
730#ifdef FEAT_CMDWIN
731 if (c == cedit_key || c == K_CMDWIN)
732 {
733 /*
734 * Open a window to edit the command line (and history).
735 */
736 c = ex_window();
737 some_key_typed = TRUE;
738 }
739# ifdef FEAT_DIGRAPHS
740 else
741# endif
742#endif
743#ifdef FEAT_DIGRAPHS
744 c = do_digraph(c);
745#endif
746
747 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
748 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
749 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000750 /* In Ex mode a backslash escapes a newline. */
751 if (exmode_active
752 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000753 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000754 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000755 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000757 if (c == K_KENTER)
758 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000760 else
761 {
762 gotesc = FALSE; /* Might have typed ESC previously, don't
763 truncate the cmdline now. */
764 if (ccheck_abbr(c + ABBR_OFF))
765 goto cmdline_changed;
766 if (!cmd_silent)
767 {
768 windgoto(msg_row, 0);
769 out_flush();
770 }
771 break;
772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 }
774
775 /*
776 * Completion for 'wildchar' or 'wildcharm' key.
777 * - hitting <ESC> twice means: abandon command line.
778 * - wildcard expansion is only done when the 'wildchar' key is really
779 * typed, not when it comes from a macro
780 */
781 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
782 {
783 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
784 {
785 /* if 'wildmode' contains "list" may still need to list */
786 if (xpc.xp_numfiles > 1
787 && !did_wild_list
788 && (wim_flags[wim_index] & WIM_LIST))
789 {
790 (void)showmatches(&xpc, FALSE);
791 redrawcmd();
792 did_wild_list = TRUE;
793 }
794 if (wim_flags[wim_index] & WIM_LONGEST)
795 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
796 else if (wim_flags[wim_index] & WIM_FULL)
797 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
798 else
799 res = OK; /* don't insert 'wildchar' now */
800 }
801 else /* typed p_wc first time */
802 {
803 wim_index = 0;
804 j = ccline.cmdpos;
805 /* if 'wildmode' first contains "longest", get longest
806 * common part */
807 if (wim_flags[0] & WIM_LONGEST)
808 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
809 else
810 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
811
812 /* if interrupted while completing, behave like it failed */
813 if (got_int)
814 {
815 (void)vpeekc(); /* remove <C-C> from input stream */
816 got_int = FALSE; /* don't abandon the command line */
817 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
818#ifdef FEAT_WILDMENU
819 xpc.xp_context = EXPAND_NOTHING;
820#endif
821 goto cmdline_changed;
822 }
823
824 /* when more than one match, and 'wildmode' first contains
825 * "list", or no change and 'wildmode' contains "longest,list",
826 * list all matches */
827 if (res == OK && xpc.xp_numfiles > 1)
828 {
829 /* a "longest" that didn't do anything is skipped (but not
830 * "list:longest") */
831 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
832 wim_index = 1;
833 if ((wim_flags[wim_index] & WIM_LIST)
834#ifdef FEAT_WILDMENU
835 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
836#endif
837 )
838 {
839 if (!(wim_flags[0] & WIM_LONGEST))
840 {
841#ifdef FEAT_WILDMENU
842 int p_wmnu_save = p_wmnu;
843 p_wmnu = 0;
844#endif
845 nextwild(&xpc, WILD_PREV, 0); /* remove match */
846#ifdef FEAT_WILDMENU
847 p_wmnu = p_wmnu_save;
848#endif
849 }
850#ifdef FEAT_WILDMENU
851 (void)showmatches(&xpc, p_wmnu
852 && ((wim_flags[wim_index] & WIM_LIST) == 0));
853#else
854 (void)showmatches(&xpc, FALSE);
855#endif
856 redrawcmd();
857 did_wild_list = TRUE;
858 if (wim_flags[wim_index] & WIM_LONGEST)
859 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
860 else if (wim_flags[wim_index] & WIM_FULL)
861 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
862 }
863 else
864 vim_beep();
865 }
866#ifdef FEAT_WILDMENU
867 else if (xpc.xp_numfiles == -1)
868 xpc.xp_context = EXPAND_NOTHING;
869#endif
870 }
871 if (wim_index < 3)
872 ++wim_index;
873 if (c == ESC)
874 gotesc = TRUE;
875 if (res == OK)
876 goto cmdline_changed;
877 }
878
879 gotesc = FALSE;
880
881 /* <S-Tab> goes to last match, in a clumsy way */
882 if (c == K_S_TAB && KeyTyped)
883 {
884 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
885 && nextwild(&xpc, WILD_PREV, 0) == OK
886 && nextwild(&xpc, WILD_PREV, 0) == OK)
887 goto cmdline_changed;
888 }
889
890 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
891 c = NL;
892
893 do_abbr = TRUE; /* default: check for abbreviation */
894
895 /*
896 * Big switch for a typed command line character.
897 */
898 switch (c)
899 {
900 case K_BS:
901 case Ctrl_H:
902 case K_DEL:
903 case K_KDEL:
904 case Ctrl_W:
905#ifdef FEAT_FKMAP
906 if (cmd_fkmap && c == K_BS)
907 c = K_DEL;
908#endif
909 if (c == K_KDEL)
910 c = K_DEL;
911
912 /*
913 * delete current character is the same as backspace on next
914 * character, except at end of line
915 */
916 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
917 ++ccline.cmdpos;
918#ifdef FEAT_MBYTE
919 if (has_mbyte && c == K_DEL)
920 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
921 ccline.cmdbuff + ccline.cmdpos);
922#endif
923 if (ccline.cmdpos > 0)
924 {
925 char_u *p;
926
927 j = ccline.cmdpos;
928 p = ccline.cmdbuff + j;
929#ifdef FEAT_MBYTE
930 if (has_mbyte)
931 {
932 p = mb_prevptr(ccline.cmdbuff, p);
933 if (c == Ctrl_W)
934 {
935 while (p > ccline.cmdbuff && vim_isspace(*p))
936 p = mb_prevptr(ccline.cmdbuff, p);
937 i = mb_get_class(p);
938 while (p > ccline.cmdbuff && mb_get_class(p) == i)
939 p = mb_prevptr(ccline.cmdbuff, p);
940 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000941 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
943 }
944 else
945#endif
946 if (c == Ctrl_W)
947 {
948 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
949 --p;
950 i = vim_iswordc(p[-1]);
951 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
952 && vim_iswordc(p[-1]) == i)
953 --p;
954 }
955 else
956 --p;
957 ccline.cmdpos = (int)(p - ccline.cmdbuff);
958 ccline.cmdlen -= j - ccline.cmdpos;
959 i = ccline.cmdpos;
960 while (i < ccline.cmdlen)
961 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
962
963 /* Truncate at the end, required for multi-byte chars. */
964 ccline.cmdbuff[ccline.cmdlen] = NUL;
965 redrawcmd();
966 }
967 else if (ccline.cmdlen == 0 && c != Ctrl_W
968 && ccline.cmdprompt == NULL && indent == 0)
969 {
970 /* In ex and debug mode it doesn't make sense to return. */
971 if (exmode_active
972#ifdef FEAT_EVAL
973 || ccline.cmdfirstc == '>'
974#endif
975 )
976 goto cmdline_not_changed;
977
978 vim_free(ccline.cmdbuff); /* no commandline to return */
979 ccline.cmdbuff = NULL;
980 if (!cmd_silent)
981 {
982#ifdef FEAT_RIGHTLEFT
983 if (cmdmsg_rl)
984 msg_col = Columns;
985 else
986#endif
987 msg_col = 0;
988 msg_putchar(' '); /* delete ':' */
989 }
990 redraw_cmdline = TRUE;
991 goto returncmd; /* back to cmd mode */
992 }
993 goto cmdline_changed;
994
995 case K_INS:
996 case K_KINS:
997#ifdef FEAT_FKMAP
998 /* if Farsi mode set, we are in reverse insert mode -
999 Do not change the mode */
1000 if (cmd_fkmap)
1001 beep_flush();
1002 else
1003#endif
1004 ccline.overstrike = !ccline.overstrike;
1005#ifdef CURSOR_SHAPE
1006 ui_cursor_shape(); /* may show different cursor shape */
1007#endif
1008 goto cmdline_not_changed;
1009
1010 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001011 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
1013 /* ":lmap" mappings exists, toggle use of mappings. */
1014 State ^= LANGMAP;
1015#ifdef USE_IM_CONTROL
1016 im_set_active(FALSE); /* Disable input method */
1017#endif
1018 if (b_im_ptr != NULL)
1019 {
1020 if (State & LANGMAP)
1021 *b_im_ptr = B_IMODE_LMAP;
1022 else
1023 *b_im_ptr = B_IMODE_NONE;
1024 }
1025 }
1026#ifdef USE_IM_CONTROL
1027 else
1028 {
1029 /* There are no ":lmap" mappings, toggle IM. When
1030 * 'imdisable' is set don't try getting the status, it's
1031 * always off. */
1032 if ((p_imdisable && b_im_ptr != NULL)
1033 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1034 {
1035 im_set_active(FALSE); /* Disable input method */
1036 if (b_im_ptr != NULL)
1037 *b_im_ptr = B_IMODE_NONE;
1038 }
1039 else
1040 {
1041 im_set_active(TRUE); /* Enable input method */
1042 if (b_im_ptr != NULL)
1043 *b_im_ptr = B_IMODE_IM;
1044 }
1045 }
1046#endif
1047 if (b_im_ptr != NULL)
1048 {
1049 if (b_im_ptr == &curbuf->b_p_iminsert)
1050 set_iminsert_global();
1051 else
1052 set_imsearch_global();
1053 }
1054#ifdef CURSOR_SHAPE
1055 ui_cursor_shape(); /* may show different cursor shape */
1056#endif
1057#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1058 /* Show/unshow value of 'keymap' in status lines later. */
1059 status_redraw_curbuf();
1060#endif
1061 goto cmdline_not_changed;
1062
1063/* case '@': only in very old vi */
1064 case Ctrl_U:
1065 /* delete all characters left of the cursor */
1066 j = ccline.cmdpos;
1067 ccline.cmdlen -= j;
1068 i = ccline.cmdpos = 0;
1069 while (i < ccline.cmdlen)
1070 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1071 /* Truncate at the end, required for multi-byte chars. */
1072 ccline.cmdbuff[ccline.cmdlen] = NUL;
1073 redrawcmd();
1074 goto cmdline_changed;
1075
1076#ifdef FEAT_CLIPBOARD
1077 case Ctrl_Y:
1078 /* Copy the modeless selection, if there is one. */
1079 if (clip_star.state != SELECT_CLEARED)
1080 {
1081 if (clip_star.state == SELECT_DONE)
1082 clip_copy_modeless_selection(TRUE);
1083 goto cmdline_not_changed;
1084 }
1085 break;
1086#endif
1087
1088 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1089 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001090 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001091 * ":normal" runs out of characters. */
1092 if (exmode_active
1093#ifdef FEAT_EX_EXTRA
1094 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1095#endif
1096 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 goto cmdline_not_changed;
1098
1099 gotesc = TRUE; /* will free ccline.cmdbuff after
1100 putting it in history */
1101 goto returncmd; /* back to cmd mode */
1102
1103 case Ctrl_R: /* insert register */
1104#ifdef USE_ON_FLY_SCROLL
1105 dont_scroll = TRUE; /* disallow scrolling here */
1106#endif
1107 putcmdline('"', TRUE);
1108 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001109 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 if (i == Ctrl_O)
1111 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1112 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001113 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 --no_mapping;
1115#ifdef FEAT_EVAL
1116 /*
1117 * Insert the result of an expression.
1118 * Need to save the current command line, to be able to enter
1119 * a new one...
1120 */
1121 new_cmdpos = -1;
1122 if (c == '=')
1123 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1125 {
1126 beep_flush();
1127 c = ESC;
1128 }
1129 else
1130 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001131 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001133 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 }
1136#endif
1137 if (c != ESC) /* use ESC to cancel inserting register */
1138 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001139 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001140
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001141#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001142 /* When there was a serious error abort getting the
1143 * command line. */
1144 if (aborting())
1145 {
1146 gotesc = TRUE; /* will free ccline.cmdbuff after
1147 putting it in history */
1148 goto returncmd; /* back to cmd mode */
1149 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 KeyTyped = FALSE; /* Don't do p_wc completion. */
1152#ifdef FEAT_EVAL
1153 if (new_cmdpos >= 0)
1154 {
1155 /* set_cmdline_pos() was used */
1156 if (new_cmdpos > ccline.cmdlen)
1157 ccline.cmdpos = ccline.cmdlen;
1158 else
1159 ccline.cmdpos = new_cmdpos;
1160 }
1161#endif
1162 }
1163 redrawcmd();
1164 goto cmdline_changed;
1165
1166 case Ctrl_D:
1167 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1168 break; /* Use ^D as normal char instead */
1169
1170 redrawcmd();
1171 continue; /* don't do incremental search now */
1172
1173 case K_RIGHT:
1174 case K_S_RIGHT:
1175 case K_C_RIGHT:
1176 do
1177 {
1178 if (ccline.cmdpos >= ccline.cmdlen)
1179 break;
1180 i = cmdline_charsize(ccline.cmdpos);
1181 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1182 break;
1183 ccline.cmdspos += i;
1184#ifdef FEAT_MBYTE
1185 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001186 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 + ccline.cmdpos);
1188 else
1189#endif
1190 ++ccline.cmdpos;
1191 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001192 while ((c == K_S_RIGHT || c == K_C_RIGHT
1193 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1195#ifdef FEAT_MBYTE
1196 if (has_mbyte)
1197 set_cmdspos_cursor();
1198#endif
1199 goto cmdline_not_changed;
1200
1201 case K_LEFT:
1202 case K_S_LEFT:
1203 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001204 if (ccline.cmdpos == 0)
1205 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 do
1207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 --ccline.cmdpos;
1209#ifdef FEAT_MBYTE
1210 if (has_mbyte) /* move to first byte of char */
1211 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1212 ccline.cmdbuff + ccline.cmdpos);
1213#endif
1214 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1215 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001216 while (ccline.cmdpos > 0
1217 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001218 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1220#ifdef FEAT_MBYTE
1221 if (has_mbyte)
1222 set_cmdspos_cursor();
1223#endif
1224 goto cmdline_not_changed;
1225
1226 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001227 /* Ignore mouse event or ex_window() result. */
1228 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229
Bram Moolenaar4770d092006-01-12 23:22:24 +00001230#ifdef FEAT_GUI_W32
1231 /* On Win32 ignore <M-F4>, we get it when closing the window was
1232 * cancelled. */
1233 case K_F4:
1234 if (mod_mask == MOD_MASK_ALT)
1235 {
1236 redrawcmd(); /* somehow the cmdline is cleared */
1237 goto cmdline_not_changed;
1238 }
1239 break;
1240#endif
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242#ifdef FEAT_MOUSE
1243 case K_MIDDLEDRAG:
1244 case K_MIDDLERELEASE:
1245 goto cmdline_not_changed; /* Ignore mouse */
1246
1247 case K_MIDDLEMOUSE:
1248# ifdef FEAT_GUI
1249 /* When GUI is active, also paste when 'mouse' is empty */
1250 if (!gui.in_use)
1251# endif
1252 if (!mouse_has(MOUSE_COMMAND))
1253 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001254# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001256 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001258# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001259 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 redrawcmd();
1261 goto cmdline_changed;
1262
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001263# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001265 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 redrawcmd();
1267 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
1270 case K_LEFTDRAG:
1271 case K_LEFTRELEASE:
1272 case K_RIGHTDRAG:
1273 case K_RIGHTRELEASE:
1274 /* Ignore drag and release events when the button-down wasn't
1275 * seen before. */
1276 if (ignore_drag_release)
1277 goto cmdline_not_changed;
1278 /* FALLTHROUGH */
1279 case K_LEFTMOUSE:
1280 case K_RIGHTMOUSE:
1281 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1282 ignore_drag_release = TRUE;
1283 else
1284 ignore_drag_release = FALSE;
1285# ifdef FEAT_GUI
1286 /* When GUI is active, also move when 'mouse' is empty */
1287 if (!gui.in_use)
1288# endif
1289 if (!mouse_has(MOUSE_COMMAND))
1290 goto cmdline_not_changed; /* Ignore mouse */
1291# ifdef FEAT_CLIPBOARD
1292 if (mouse_row < cmdline_row && clip_star.available)
1293 {
1294 int button, is_click, is_drag;
1295
1296 /*
1297 * Handle modeless selection.
1298 */
1299 button = get_mouse_button(KEY2TERMCAP1(c),
1300 &is_click, &is_drag);
1301 if (mouse_model_popup() && button == MOUSE_LEFT
1302 && (mod_mask & MOD_MASK_SHIFT))
1303 {
1304 /* Translate shift-left to right button. */
1305 button = MOUSE_RIGHT;
1306 mod_mask &= ~MOD_MASK_SHIFT;
1307 }
1308 clip_modeless(button, is_click, is_drag);
1309 goto cmdline_not_changed;
1310 }
1311# endif
1312
1313 set_cmdspos();
1314 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1315 ++ccline.cmdpos)
1316 {
1317 i = cmdline_charsize(ccline.cmdpos);
1318 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1319 && mouse_col < ccline.cmdspos % Columns + i)
1320 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001321# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (has_mbyte)
1323 {
1324 /* Count ">" for double-wide char that doesn't fit. */
1325 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001326 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 + ccline.cmdpos) - 1;
1328 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 ccline.cmdspos += i;
1331 }
1332 goto cmdline_not_changed;
1333
1334 /* Mouse scroll wheel: ignored here */
1335 case K_MOUSEDOWN:
1336 case K_MOUSEUP:
1337 /* Alternate buttons ignored here */
1338 case K_X1MOUSE:
1339 case K_X1DRAG:
1340 case K_X1RELEASE:
1341 case K_X2MOUSE:
1342 case K_X2DRAG:
1343 case K_X2RELEASE:
1344 goto cmdline_not_changed;
1345
1346#endif /* FEAT_MOUSE */
1347
1348#ifdef FEAT_GUI
1349 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1350 case K_LEFTRELEASE_NM:
1351 goto cmdline_not_changed;
1352
1353 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001354 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 {
1356 gui_do_scroll();
1357 redrawcmd();
1358 }
1359 goto cmdline_not_changed;
1360
1361 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001362 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 {
1364 gui_do_horiz_scroll();
1365 redrawcmd();
1366 }
1367 goto cmdline_not_changed;
1368#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001369#ifdef FEAT_GUI_TABLINE
1370 case K_TABLINE:
1371 case K_TABMENU:
1372 /* Don't want to change any tabs here. Make sure the same tab
1373 * is still selected. */
1374 if (gui_use_tabline())
1375 gui_mch_set_curtab(tabpage_index(curtab));
1376 goto cmdline_not_changed;
1377#endif
1378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 case K_SELECT: /* end of Select mode mapping - ignore */
1380 goto cmdline_not_changed;
1381
1382 case Ctrl_B: /* begin of command line */
1383 case K_HOME:
1384 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 case K_S_HOME:
1386 case K_C_HOME:
1387 ccline.cmdpos = 0;
1388 set_cmdspos();
1389 goto cmdline_not_changed;
1390
1391 case Ctrl_E: /* end of command line */
1392 case K_END:
1393 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 case K_S_END:
1395 case K_C_END:
1396 ccline.cmdpos = ccline.cmdlen;
1397 set_cmdspos_cursor();
1398 goto cmdline_not_changed;
1399
1400 case Ctrl_A: /* all matches */
1401 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1402 break;
1403 goto cmdline_changed;
1404
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001405 case Ctrl_L:
1406#ifdef FEAT_SEARCH_EXTRA
1407 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1408 {
1409 /* Add a character from under the cursor for 'incsearch' */
1410 if (did_incsearch
1411 && !equalpos(curwin->w_cursor, old_cursor))
1412 {
1413 c = gchar_cursor();
1414 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001415 {
1416 if (c == firstc || vim_strchr((char_u *)(
1417 p_magic ? "\\^$.*[" : "\\^$"), c)
1418 != NULL)
1419 {
1420 /* put a backslash before special characters */
1421 stuffcharReadbuff(c);
1422 c = '\\';
1423 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001424 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001425 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001426 }
1427 goto cmdline_not_changed;
1428 }
1429#endif
1430
1431 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1433 break;
1434 goto cmdline_changed;
1435
1436 case Ctrl_N: /* next match */
1437 case Ctrl_P: /* previous match */
1438 if (xpc.xp_numfiles > 0)
1439 {
1440 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1441 == FAIL)
1442 break;
1443 goto cmdline_changed;
1444 }
1445
1446#ifdef FEAT_CMDHIST
1447 case K_UP:
1448 case K_DOWN:
1449 case K_S_UP:
1450 case K_S_DOWN:
1451 case K_PAGEUP:
1452 case K_KPAGEUP:
1453 case K_PAGEDOWN:
1454 case K_KPAGEDOWN:
1455 if (hislen == 0 || firstc == NUL) /* no history */
1456 goto cmdline_not_changed;
1457
1458 i = hiscnt;
1459
1460 /* save current command string so it can be restored later */
1461 if (lookfor == NULL)
1462 {
1463 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1464 goto cmdline_not_changed;
1465 lookfor[ccline.cmdpos] = NUL;
1466 }
1467
1468 j = (int)STRLEN(lookfor);
1469 for (;;)
1470 {
1471 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001472 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001473 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 {
1475 if (hiscnt == hislen) /* first time */
1476 hiscnt = hisidx[histype];
1477 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1478 hiscnt = hislen - 1;
1479 else if (hiscnt != hisidx[histype] + 1)
1480 --hiscnt;
1481 else /* at top of list */
1482 {
1483 hiscnt = i;
1484 break;
1485 }
1486 }
1487 else /* one step forwards */
1488 {
1489 /* on last entry, clear the line */
1490 if (hiscnt == hisidx[histype])
1491 {
1492 hiscnt = hislen;
1493 break;
1494 }
1495
1496 /* not on a history line, nothing to do */
1497 if (hiscnt == hislen)
1498 break;
1499 if (hiscnt == hislen - 1) /* wrap around */
1500 hiscnt = 0;
1501 else
1502 ++hiscnt;
1503 }
1504 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1505 {
1506 hiscnt = i;
1507 break;
1508 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001509 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001510 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 || STRNCMP(history[histype][hiscnt].hisstr,
1512 lookfor, (size_t)j) == 0)
1513 break;
1514 }
1515
1516 if (hiscnt != i) /* jumped to other entry */
1517 {
1518 char_u *p;
1519 int len;
1520 int old_firstc;
1521
1522 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001523 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (hiscnt == hislen)
1525 p = lookfor; /* back to the old one */
1526 else
1527 p = history[histype][hiscnt].hisstr;
1528
1529 if (histype == HIST_SEARCH
1530 && p != lookfor
1531 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1532 {
1533 /* Correct for the separator character used when
1534 * adding the history entry vs the one used now.
1535 * First loop: count length.
1536 * Second loop: copy the characters. */
1537 for (i = 0; i <= 1; ++i)
1538 {
1539 len = 0;
1540 for (j = 0; p[j] != NUL; ++j)
1541 {
1542 /* Replace old sep with new sep, unless it is
1543 * escaped. */
1544 if (p[j] == old_firstc
1545 && (j == 0 || p[j - 1] != '\\'))
1546 {
1547 if (i > 0)
1548 ccline.cmdbuff[len] = firstc;
1549 }
1550 else
1551 {
1552 /* Escape new sep, unless it is already
1553 * escaped. */
1554 if (p[j] == firstc
1555 && (j == 0 || p[j - 1] != '\\'))
1556 {
1557 if (i > 0)
1558 ccline.cmdbuff[len] = '\\';
1559 ++len;
1560 }
1561 if (i > 0)
1562 ccline.cmdbuff[len] = p[j];
1563 }
1564 ++len;
1565 }
1566 if (i == 0)
1567 {
1568 alloc_cmdbuff(len);
1569 if (ccline.cmdbuff == NULL)
1570 goto returncmd;
1571 }
1572 }
1573 ccline.cmdbuff[len] = NUL;
1574 }
1575 else
1576 {
1577 alloc_cmdbuff((int)STRLEN(p));
1578 if (ccline.cmdbuff == NULL)
1579 goto returncmd;
1580 STRCPY(ccline.cmdbuff, p);
1581 }
1582
1583 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1584 redrawcmd();
1585 goto cmdline_changed;
1586 }
1587 beep_flush();
1588 goto cmdline_not_changed;
1589#endif
1590
1591 case Ctrl_V:
1592 case Ctrl_Q:
1593#ifdef FEAT_MOUSE
1594 ignore_drag_release = TRUE;
1595#endif
1596 putcmdline('^', TRUE);
1597 c = get_literal(); /* get next (two) character(s) */
1598 do_abbr = FALSE; /* don't do abbreviation now */
1599#ifdef FEAT_MBYTE
1600 /* may need to remove ^ when composing char was typed */
1601 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1602 {
1603 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1604 msg_putchar(' ');
1605 cursorcmd();
1606 }
1607#endif
1608 break;
1609
1610#ifdef FEAT_DIGRAPHS
1611 case Ctrl_K:
1612#ifdef FEAT_MOUSE
1613 ignore_drag_release = TRUE;
1614#endif
1615 putcmdline('?', TRUE);
1616#ifdef USE_ON_FLY_SCROLL
1617 dont_scroll = TRUE; /* disallow scrolling here */
1618#endif
1619 c = get_digraph(TRUE);
1620 if (c != NUL)
1621 break;
1622
1623 redrawcmd();
1624 goto cmdline_not_changed;
1625#endif /* FEAT_DIGRAPHS */
1626
1627#ifdef FEAT_RIGHTLEFT
1628 case Ctrl__: /* CTRL-_: switch language mode */
1629 if (!p_ari)
1630 break;
1631#ifdef FEAT_FKMAP
1632 if (p_altkeymap)
1633 {
1634 cmd_fkmap = !cmd_fkmap;
1635 if (cmd_fkmap) /* in Farsi always in Insert mode */
1636 ccline.overstrike = FALSE;
1637 }
1638 else /* Hebrew is default */
1639#endif
1640 cmd_hkmap = !cmd_hkmap;
1641 goto cmdline_not_changed;
1642#endif
1643
1644 default:
1645#ifdef UNIX
1646 if (c == intr_char)
1647 {
1648 gotesc = TRUE; /* will free ccline.cmdbuff after
1649 putting it in history */
1650 goto returncmd; /* back to Normal mode */
1651 }
1652#endif
1653 /*
1654 * Normal character with no special meaning. Just set mod_mask
1655 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1656 * the string <S-Space>. This should only happen after ^V.
1657 */
1658 if (!IS_SPECIAL(c))
1659 mod_mask = 0x0;
1660 break;
1661 }
1662 /*
1663 * End of switch on command line character.
1664 * We come here if we have a normal character.
1665 */
1666
1667 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1668#ifdef FEAT_MBYTE
1669 /* Add ABBR_OFF for characters above 0x100, this is
1670 * what check_abbr() expects. */
1671 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1672#endif
1673 c))
1674 goto cmdline_changed;
1675
1676 /*
1677 * put the character in the command line
1678 */
1679 if (IS_SPECIAL(c) || mod_mask != 0)
1680 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1681 else
1682 {
1683#ifdef FEAT_MBYTE
1684 if (has_mbyte)
1685 {
1686 j = (*mb_char2bytes)(c, IObuff);
1687 IObuff[j] = NUL; /* exclude composing chars */
1688 put_on_cmdline(IObuff, j, TRUE);
1689 }
1690 else
1691#endif
1692 {
1693 IObuff[0] = c;
1694 put_on_cmdline(IObuff, 1, TRUE);
1695 }
1696 }
1697 goto cmdline_changed;
1698
1699/*
1700 * This part implements incremental searches for "/" and "?"
1701 * Jump to cmdline_not_changed when a character has been read but the command
1702 * line did not change. Then we only search and redraw if something changed in
1703 * the past.
1704 * Jump to cmdline_changed when the command line did change.
1705 * (Sorry for the goto's, I know it is ugly).
1706 */
1707cmdline_not_changed:
1708#ifdef FEAT_SEARCH_EXTRA
1709 if (!incsearch_postponed)
1710 continue;
1711#endif
1712
1713cmdline_changed:
1714#ifdef FEAT_SEARCH_EXTRA
1715 /*
1716 * 'incsearch' highlighting.
1717 */
1718 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1719 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001720 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001721#ifdef FEAT_RELTIME
1722 proftime_T tm;
1723#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001724
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 /* if there is a character waiting, search and redraw later */
1726 if (char_avail())
1727 {
1728 incsearch_postponed = TRUE;
1729 continue;
1730 }
1731 incsearch_postponed = FALSE;
1732 curwin->w_cursor = old_cursor; /* start at old position */
1733
1734 /* If there is no command line, don't do anything */
1735 if (ccline.cmdlen == 0)
1736 i = 0;
1737 else
1738 {
1739 cursor_off(); /* so the user knows we're busy */
1740 out_flush();
1741 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001742#ifdef FEAT_RELTIME
1743 /* Set the time limit to half a second. */
1744 profile_setlimit(500L, &tm);
1745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001747 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1748#ifdef FEAT_RELTIME
1749 &tm
1750#else
1751 NULL
1752#endif
1753 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 --emsg_off;
1755 /* if interrupted while searching, behave like it failed */
1756 if (got_int)
1757 {
1758 (void)vpeekc(); /* remove <C-C> from input stream */
1759 got_int = FALSE; /* don't abandon the command line */
1760 i = 0;
1761 }
1762 else if (char_avail())
1763 /* cancelled searching because a char was typed */
1764 incsearch_postponed = TRUE;
1765 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001766 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 highlight_match = TRUE; /* highlight position */
1768 else
1769 highlight_match = FALSE; /* remove highlight */
1770
1771 /* first restore the old curwin values, so the screen is
1772 * positioned in the same way as the actual search command */
1773 curwin->w_leftcol = old_leftcol;
1774 curwin->w_topline = old_topline;
1775# ifdef FEAT_DIFF
1776 curwin->w_topfill = old_topfill;
1777# endif
1778 curwin->w_botline = old_botline;
1779 changed_cline_bef_curs();
1780 update_topline();
1781
1782 if (i != 0)
1783 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001784 pos_T save_pos = curwin->w_cursor;
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001787 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 * moves the whole match onto the screen when 'nowrap' is set.
1789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 curwin->w_cursor.lnum += search_match_lines;
1791 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001792 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1793 {
1794 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1795 coladvance((colnr_T)MAXCOL);
1796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001798 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001799 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001801 else
1802 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001805# ifdef FEAT_WINDOWS
1806 /* May redraw the status line to show the cursor position. */
1807 if (p_ru && curwin->w_status_height > 0)
1808 curwin->w_redr_status = TRUE;
1809# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001811 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001812 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001813 restore_cmdline(&save_ccline);
1814
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001815 /* Leave it at the end to make CTRL-R CTRL-W work. */
1816 if (i != 0)
1817 curwin->w_cursor = end_pos;
1818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 msg_starthere();
1820 redrawcmdline();
1821 did_incsearch = TRUE;
1822 }
1823#else /* FEAT_SEARCH_EXTRA */
1824 ;
1825#endif
1826
1827#ifdef FEAT_RIGHTLEFT
1828 if (cmdmsg_rl
1829# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001830 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831# endif
1832 )
1833 /* Always redraw the whole command line to fix shaping and
1834 * right-left typing. Not efficient, but it works. */
1835 redrawcmd();
1836#endif
1837 }
1838
1839returncmd:
1840
1841#ifdef FEAT_RIGHTLEFT
1842 cmdmsg_rl = FALSE;
1843#endif
1844
1845#ifdef FEAT_FKMAP
1846 cmd_fkmap = 0;
1847#endif
1848
1849 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001850 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
1852#ifdef FEAT_SEARCH_EXTRA
1853 if (did_incsearch)
1854 {
1855 curwin->w_cursor = old_cursor;
1856 curwin->w_curswant = old_curswant;
1857 curwin->w_leftcol = old_leftcol;
1858 curwin->w_topline = old_topline;
1859# ifdef FEAT_DIFF
1860 curwin->w_topfill = old_topfill;
1861# endif
1862 curwin->w_botline = old_botline;
1863 highlight_match = FALSE;
1864 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001865 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 }
1867#endif
1868
1869 if (ccline.cmdbuff != NULL)
1870 {
1871 /*
1872 * Put line in history buffer (":" and "=" only when it was typed).
1873 */
1874#ifdef FEAT_CMDHIST
1875 if (ccline.cmdlen && firstc != NUL
1876 && (some_key_typed || histype == HIST_SEARCH))
1877 {
1878 add_to_history(histype, ccline.cmdbuff, TRUE,
1879 histype == HIST_SEARCH ? firstc : NUL);
1880 if (firstc == ':')
1881 {
1882 vim_free(new_last_cmdline);
1883 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1884 }
1885 }
1886#endif
1887
1888 if (gotesc) /* abandon command line */
1889 {
1890 vim_free(ccline.cmdbuff);
1891 ccline.cmdbuff = NULL;
1892 if (msg_scrolled == 0)
1893 compute_cmdrow();
1894 MSG("");
1895 redraw_cmdline = TRUE;
1896 }
1897 }
1898
1899 /*
1900 * If the screen was shifted up, redraw the whole screen (later).
1901 * If the line is too long, clear it, so ruler and shown command do
1902 * not get printed in the middle of it.
1903 */
1904 msg_check();
1905 msg_scroll = save_msg_scroll;
1906 redir_off = FALSE;
1907
1908 /* When the command line was typed, no need for a wait-return prompt. */
1909 if (some_key_typed)
1910 need_wait_return = FALSE;
1911
1912 State = save_State;
1913#ifdef USE_IM_CONTROL
1914 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1915 im_save_status(b_im_ptr);
1916 im_set_active(FALSE);
1917#endif
1918#ifdef FEAT_MOUSE
1919 setmouse();
1920#endif
1921#ifdef CURSOR_SHAPE
1922 ui_cursor_shape(); /* may show different cursor shape */
1923#endif
1924
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001925 {
1926 char_u *p = ccline.cmdbuff;
1927
1928 /* Make ccline empty, getcmdline() may try to use it. */
1929 ccline.cmdbuff = NULL;
1930 return p;
1931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932}
1933
1934#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1935/*
1936 * Get a command line with a prompt.
1937 * This is prepared to be called recursively from getcmdline() (e.g. by
1938 * f_input() when evaluating an expression from CTRL-R =).
1939 * Returns the command line in allocated memory, or NULL.
1940 */
1941 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001942getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 int firstc;
1944 char_u *prompt; /* command line prompt */
1945 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001946 int xp_context; /* type of expansion */
1947 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
1949 char_u *s;
1950 struct cmdline_info save_ccline;
1951 int msg_col_save = msg_col;
1952
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001953 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 ccline.cmdprompt = prompt;
1955 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001956# ifdef FEAT_EVAL
1957 ccline.xp_context = xp_context;
1958 ccline.xp_arg = xp_arg;
1959 ccline.input_fn = (firstc == '@');
1960# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001962 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 /* Restore msg_col, the prompt from input() may have changed it. */
1964 msg_col = msg_col_save;
1965
1966 return s;
1967}
1968#endif
1969
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001970/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001971 * Return TRUE when the text must not be changed and we can't switch to
1972 * another window or buffer. Used when editing the command line, evaluating
1973 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001974 */
1975 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001976text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001977{
1978#ifdef FEAT_CMDWIN
1979 if (cmdwin_type != 0)
1980 return TRUE;
1981#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001982 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001983}
1984
1985/*
1986 * Give an error message for a command that isn't allowed while the cmdline
1987 * window is open or editing the cmdline in another way.
1988 */
1989 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001990text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001991{
1992#ifdef FEAT_CMDWIN
1993 if (cmdwin_type != 0)
1994 EMSG(_(e_cmdwin));
1995 else
1996#endif
1997 EMSG(_(e_secure));
1998}
1999
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002000#if defined(FEAT_AUTOCMD) || defined(PROTO)
2001/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002002 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2003 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002004 */
2005 int
2006curbuf_locked()
2007{
2008 if (curbuf_lock > 0)
2009 {
2010 EMSG(_("E788: Not allowed to edit another buffer now"));
2011 return TRUE;
2012 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002013 return allbuf_locked();
2014}
2015
2016/*
2017 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2018 * message.
2019 */
2020 int
2021allbuf_locked()
2022{
2023 if (allbuf_lock > 0)
2024 {
2025 EMSG(_("E811: Not allowed to change buffer information now"));
2026 return TRUE;
2027 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002028 return FALSE;
2029}
2030#endif
2031
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 static int
2033cmdline_charsize(idx)
2034 int idx;
2035{
2036#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2037 if (cmdline_star > 0) /* showing '*', always 1 position */
2038 return 1;
2039#endif
2040 return ptr2cells(ccline.cmdbuff + idx);
2041}
2042
2043/*
2044 * Compute the offset of the cursor on the command line for the prompt and
2045 * indent.
2046 */
2047 static void
2048set_cmdspos()
2049{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002050 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 ccline.cmdspos = 1 + ccline.cmdindent;
2052 else
2053 ccline.cmdspos = 0 + ccline.cmdindent;
2054}
2055
2056/*
2057 * Compute the screen position for the cursor on the command line.
2058 */
2059 static void
2060set_cmdspos_cursor()
2061{
2062 int i, m, c;
2063
2064 set_cmdspos();
2065 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002068 if (m < 0) /* overflow, Columns or Rows at weird value */
2069 m = MAXCOL;
2070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 else
2072 m = MAXCOL;
2073 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2074 {
2075 c = cmdline_charsize(i);
2076#ifdef FEAT_MBYTE
2077 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2078 if (has_mbyte)
2079 correct_cmdspos(i, c);
2080#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002081 /* If the cmdline doesn't fit, show cursor on last visible char.
2082 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 if ((ccline.cmdspos += c) >= m)
2084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 ccline.cmdspos -= c;
2086 break;
2087 }
2088#ifdef FEAT_MBYTE
2089 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002090 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091#endif
2092 }
2093}
2094
2095#ifdef FEAT_MBYTE
2096/*
2097 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2098 * character that doesn't fit, so that a ">" must be displayed.
2099 */
2100 static void
2101correct_cmdspos(idx, cells)
2102 int idx;
2103 int cells;
2104{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002105 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2107 && ccline.cmdspos % Columns + cells > Columns)
2108 ccline.cmdspos++;
2109}
2110#endif
2111
2112/*
2113 * Get an Ex command line for the ":" command.
2114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002116getexline(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 int c; /* normally ':', NUL for ":append" */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002118 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 int indent; /* indent for inside conditionals */
2120{
2121 /* When executing a register, remove ':' that's in front of each line. */
2122 if (exec_from_reg && vpeekc() == ':')
2123 (void)vgetc();
2124 return getcmdline(c, 1L, indent);
2125}
2126
2127/*
2128 * Get an Ex command line for Ex mode.
2129 * In Ex mode we only use the OS supplied line editing features and no
2130 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002131 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002134getexmodeline(promptc, cookie, indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002135 int promptc; /* normally ':', NUL for ":append" and '?' for
2136 :s prompt */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002137 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 int indent; /* indent for inside conditionals */
2139{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002140 garray_T line_ga;
2141 char_u *pend;
2142 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002143 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002144 int escaped = FALSE; /* CTRL-V typed */
2145 int vcol = 0;
2146 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002147 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148
2149 /* Switch cursor on now. This avoids that it happens after the "\n", which
2150 * confuses the system function that computes tabstops. */
2151 cursor_on();
2152
2153 /* always start in column 0; write a newline if necessary */
2154 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002155 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002157 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002159 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002160 if (p_prompt)
2161 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 while (indent-- > 0)
2163 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166
2167 ga_init2(&line_ga, 1, 30);
2168
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002169 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002170 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002171 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002172 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002173 while (indent >= 8)
2174 {
2175 ga_append(&line_ga, TAB);
2176 msg_puts((char_u *)" ");
2177 indent -= 8;
2178 }
2179 while (indent-- > 0)
2180 {
2181 ga_append(&line_ga, ' ');
2182 msg_putchar(' ');
2183 }
2184 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002185 ++no_mapping;
2186 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002187
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 /*
2189 * Get the line, one character at a time.
2190 */
2191 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002192 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
2194 if (ga_grow(&line_ga, 40) == FAIL)
2195 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002197 /* Get one character at a time. Don't use inchar(), it can't handle
2198 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002199 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002200 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
2202 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002203 * Handle line editing.
2204 * Previously this was left to the system, putting the terminal in
2205 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002207 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002209 msg_putchar('\n');
2210 break;
2211 }
2212
2213 if (!escaped)
2214 {
2215 /* CR typed means "enter", which is NL */
2216 if (c1 == '\r')
2217 c1 = '\n';
2218
2219 if (c1 == BS || c1 == K_BS
2220 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002222 if (line_ga.ga_len > 0)
2223 {
2224 --line_ga.ga_len;
2225 goto redraw;
2226 }
2227 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 }
2229
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002230 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002232 msg_col = startcol;
2233 msg_clr_eos();
2234 line_ga.ga_len = 0;
2235 continue;
2236 }
2237
2238 if (c1 == Ctrl_T)
2239 {
2240 p = (char_u *)line_ga.ga_data;
2241 p[line_ga.ga_len] = NUL;
2242 indent = get_indent_str(p, 8);
2243 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2244add_indent:
2245 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002247 char_u *s = skipwhite(p);
2248
2249 ga_grow(&line_ga, 1);
2250 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2251 *s = ' ';
2252 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002254redraw:
2255 /* redraw the line */
2256 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002257 vcol = 0;
2258 for (p = (char_u *)line_ga.ga_data;
2259 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002261 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002263 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002265 msg_putchar(' ');
2266 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002268 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002270 msg_outtrans_len(p, 1);
2271 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
2273 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002274 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002275 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002276 continue;
2277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002279 if (c1 == Ctrl_D)
2280 {
2281 /* Delete one shiftwidth. */
2282 p = (char_u *)line_ga.ga_data;
2283 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002285 if (prev_char == '^')
2286 ex_keep_indent = TRUE;
2287 indent = 0;
2288 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 }
2290 else
2291 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002292 p[line_ga.ga_len] = NUL;
2293 indent = get_indent_str(p, 8);
2294 --indent;
2295 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002297 while (get_indent_str(p, 8) > indent)
2298 {
2299 char_u *s = skipwhite(p);
2300
2301 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2302 --line_ga.ga_len;
2303 }
2304 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002306
2307 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2308 {
2309 escaped = TRUE;
2310 continue;
2311 }
2312
2313 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2314 if (IS_SPECIAL(c1))
2315 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002317
2318 if (IS_SPECIAL(c1))
2319 c1 = '?';
2320 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002321 if (c1 == '\n')
2322 msg_putchar('\n');
2323 else if (c1 == TAB)
2324 {
2325 /* Don't use chartabsize(), 'ts' can be different */
2326 do
2327 {
2328 msg_putchar(' ');
2329 } while (++vcol % 8);
2330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002333 msg_outtrans_len(
2334 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2335 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002337 ++line_ga.ga_len;
2338 escaped = FALSE;
2339
2340 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002341 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002342
2343 /* we are done when a NL is entered, but not when it comes after a
2344 * backslash */
2345 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2346 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002349 --pend;
2350 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002351 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353 }
2354
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002355 --no_mapping;
2356 --allow_keys;
2357
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 /* make following messages go to the next line */
2359 msg_didout = FALSE;
2360 msg_col = 0;
2361 if (msg_row < Rows - 1)
2362 ++msg_row;
2363 emsg_on_display = FALSE; /* don't want ui_delay() */
2364
2365 if (got_int)
2366 ga_clear(&line_ga);
2367
2368 return (char_u *)line_ga.ga_data;
2369}
2370
Bram Moolenaare344bea2005-09-01 20:46:49 +00002371# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2372 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373/*
2374 * Return TRUE if ccline.overstrike is on.
2375 */
2376 int
2377cmdline_overstrike()
2378{
2379 return ccline.overstrike;
2380}
2381
2382/*
2383 * Return TRUE if the cursor is at the end of the cmdline.
2384 */
2385 int
2386cmdline_at_end()
2387{
2388 return (ccline.cmdpos >= ccline.cmdlen);
2389}
2390#endif
2391
Bram Moolenaar9372a112005-12-06 19:59:18 +00002392#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393/*
2394 * Return the virtual column number at the current cursor position.
2395 * This is used by the IM code to obtain the start of the preedit string.
2396 */
2397 colnr_T
2398cmdline_getvcol_cursor()
2399{
2400 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2401 return MAXCOL;
2402
2403# ifdef FEAT_MBYTE
2404 if (has_mbyte)
2405 {
2406 colnr_T col;
2407 int i = 0;
2408
2409 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002410 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
2412 return col;
2413 }
2414 else
2415# endif
2416 return ccline.cmdpos;
2417}
2418#endif
2419
2420#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2421/*
2422 * If part of the command line is an IM preedit string, redraw it with
2423 * IM feedback attributes. The cursor position is restored after drawing.
2424 */
2425 static void
2426redrawcmd_preedit()
2427{
2428 if ((State & CMDLINE)
2429 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002430 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 && !p_imdisable
2432 && im_is_preediting())
2433 {
2434 int cmdpos = 0;
2435 int cmdspos;
2436 int old_row;
2437 int old_col;
2438 colnr_T col;
2439
2440 old_row = msg_row;
2441 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002442 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
2444# ifdef FEAT_MBYTE
2445 if (has_mbyte)
2446 {
2447 for (col = 0; col < preedit_start_col
2448 && cmdpos < ccline.cmdlen; ++col)
2449 {
2450 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002451 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 }
2453 }
2454 else
2455# endif
2456 {
2457 cmdspos += preedit_start_col;
2458 cmdpos += preedit_start_col;
2459 }
2460
2461 msg_row = cmdline_row + (cmdspos / (int)Columns);
2462 msg_col = cmdspos % (int)Columns;
2463 if (msg_row >= Rows)
2464 msg_row = Rows - 1;
2465
2466 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2467 {
2468 int char_len;
2469 int char_attr;
2470
2471 char_attr = im_get_feedback_attr(col);
2472 if (char_attr < 0)
2473 break; /* end of preedit string */
2474
2475# ifdef FEAT_MBYTE
2476 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002477 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 else
2479# endif
2480 char_len = 1;
2481
2482 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2483 cmdpos += char_len;
2484 }
2485
2486 msg_row = old_row;
2487 msg_col = old_col;
2488 }
2489}
2490#endif /* FEAT_XIM && FEAT_GUI_GTK */
2491
2492/*
2493 * Allocate a new command line buffer.
2494 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2495 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2496 */
2497 static void
2498alloc_cmdbuff(len)
2499 int len;
2500{
2501 /*
2502 * give some extra space to avoid having to allocate all the time
2503 */
2504 if (len < 80)
2505 len = 100;
2506 else
2507 len += 20;
2508
2509 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2510 ccline.cmdbufflen = len;
2511}
2512
2513/*
2514 * Re-allocate the command line to length len + something extra.
2515 * return FAIL for failure, OK otherwise
2516 */
2517 static int
2518realloc_cmdbuff(len)
2519 int len;
2520{
2521 char_u *p;
2522
2523 p = ccline.cmdbuff;
2524 alloc_cmdbuff(len); /* will get some more */
2525 if (ccline.cmdbuff == NULL) /* out of memory */
2526 {
2527 ccline.cmdbuff = p; /* keep the old one */
2528 return FAIL;
2529 }
2530 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2531 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002532
2533 if (ccline.xpc != NULL
2534 && ccline.xpc->xp_pattern != NULL
2535 && ccline.xpc->xp_context != EXPAND_NOTHING
2536 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2537 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002538 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002539
2540 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2541 * to point into the newly allocated memory. */
2542 if (i >= 0 && i <= ccline.cmdlen)
2543 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2544 }
2545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 return OK;
2547}
2548
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002549#if defined(FEAT_ARABIC) || defined(PROTO)
2550static char_u *arshape_buf = NULL;
2551
2552# if defined(EXITFREE) || defined(PROTO)
2553 void
2554free_cmdline_buf()
2555{
2556 vim_free(arshape_buf);
2557}
2558# endif
2559#endif
2560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561/*
2562 * Draw part of the cmdline at the current cursor position. But draw stars
2563 * when cmdline_star is TRUE.
2564 */
2565 static void
2566draw_cmdline(start, len)
2567 int start;
2568 int len;
2569{
2570#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2571 int i;
2572
2573 if (cmdline_star > 0)
2574 for (i = 0; i < len; ++i)
2575 {
2576 msg_putchar('*');
2577# ifdef FEAT_MBYTE
2578 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002579 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580# endif
2581 }
2582 else
2583#endif
2584#ifdef FEAT_ARABIC
2585 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2586 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 static int buflen = 0;
2588 char_u *p;
2589 int j;
2590 int newlen = 0;
2591 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002592 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 int prev_c = 0;
2594 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002595 int u8c;
2596 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
2599 /*
2600 * Do arabic shaping into a temporary buffer. This is very
2601 * inefficient!
2602 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002603 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
2605 /* Re-allocate the buffer. We keep it around to avoid a lot of
2606 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002607 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002608 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002609 arshape_buf = alloc(buflen);
2610 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 return; /* out of memory */
2612 }
2613
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002614 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2615 {
2616 /* Prepend a space to draw the leading composing char on. */
2617 arshape_buf[0] = ' ';
2618 newlen = 1;
2619 }
2620
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 for (j = start; j < start + len; j += mb_l)
2622 {
2623 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002624 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002625 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 if (ARABIC_CHAR(u8c))
2627 {
2628 /* Do Arabic shaping. */
2629 if (cmdmsg_rl)
2630 {
2631 /* displaying from right to left */
2632 pc = prev_c;
2633 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002634 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 if (j + mb_l >= start + len)
2636 nc = NUL;
2637 else
2638 nc = utf_ptr2char(p + mb_l);
2639 }
2640 else
2641 {
2642 /* displaying from left to right */
2643 if (j + mb_l >= start + len)
2644 pc = NUL;
2645 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002646 {
2647 int pcc[MAX_MCO];
2648
2649 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002651 pc1 = pcc[0];
2652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 nc = prev_c;
2654 }
2655 prev_c = u8c;
2656
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002657 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002659 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002660 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002662 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2663 if (u8cc[1] != 0)
2664 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002665 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667 }
2668 else
2669 {
2670 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002671 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 newlen += mb_l;
2673 }
2674 }
2675
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002676 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678 else
2679#endif
2680 msg_outtrans_len(ccline.cmdbuff + start, len);
2681}
2682
2683/*
2684 * Put a character on the command line. Shifts the following text to the
2685 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2686 * "c" must be printable (fit in one display cell)!
2687 */
2688 void
2689putcmdline(c, shift)
2690 int c;
2691 int shift;
2692{
2693 if (cmd_silent)
2694 return;
2695 msg_no_more = TRUE;
2696 msg_putchar(c);
2697 if (shift)
2698 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2699 msg_no_more = FALSE;
2700 cursorcmd();
2701}
2702
2703/*
2704 * Undo a putcmdline(c, FALSE).
2705 */
2706 void
2707unputcmdline()
2708{
2709 if (cmd_silent)
2710 return;
2711 msg_no_more = TRUE;
2712 if (ccline.cmdlen == ccline.cmdpos)
2713 msg_putchar(' ');
2714 else
2715 draw_cmdline(ccline.cmdpos, 1);
2716 msg_no_more = FALSE;
2717 cursorcmd();
2718}
2719
2720/*
2721 * Put the given string, of the given length, onto the command line.
2722 * If len is -1, then STRLEN() is used to calculate the length.
2723 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2724 * part will be redrawn, otherwise it will not. If this function is called
2725 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2726 * called afterwards.
2727 */
2728 int
2729put_on_cmdline(str, len, redraw)
2730 char_u *str;
2731 int len;
2732 int redraw;
2733{
2734 int retval;
2735 int i;
2736 int m;
2737 int c;
2738
2739 if (len < 0)
2740 len = (int)STRLEN(str);
2741
2742 /* Check if ccline.cmdbuff needs to be longer */
2743 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2744 retval = realloc_cmdbuff(ccline.cmdlen + len);
2745 else
2746 retval = OK;
2747 if (retval == OK)
2748 {
2749 if (!ccline.overstrike)
2750 {
2751 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2752 ccline.cmdbuff + ccline.cmdpos,
2753 (size_t)(ccline.cmdlen - ccline.cmdpos));
2754 ccline.cmdlen += len;
2755 }
2756 else
2757 {
2758#ifdef FEAT_MBYTE
2759 if (has_mbyte)
2760 {
2761 /* Count nr of characters in the new string. */
2762 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002763 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 ++m;
2765 /* Count nr of bytes in cmdline that are overwritten by these
2766 * characters. */
2767 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002768 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 --m;
2770 if (i < ccline.cmdlen)
2771 {
2772 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2773 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2774 ccline.cmdlen += ccline.cmdpos + len - i;
2775 }
2776 else
2777 ccline.cmdlen = ccline.cmdpos + len;
2778 }
2779 else
2780#endif
2781 if (ccline.cmdpos + len > ccline.cmdlen)
2782 ccline.cmdlen = ccline.cmdpos + len;
2783 }
2784 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2785 ccline.cmdbuff[ccline.cmdlen] = NUL;
2786
2787#ifdef FEAT_MBYTE
2788 if (enc_utf8)
2789 {
2790 /* When the inserted text starts with a composing character,
2791 * backup to the character before it. There could be two of them.
2792 */
2793 i = 0;
2794 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2795 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2796 {
2797 i = (*mb_head_off)(ccline.cmdbuff,
2798 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2799 ccline.cmdpos -= i;
2800 len += i;
2801 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2802 }
2803# ifdef FEAT_ARABIC
2804 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2805 {
2806 /* Check the previous character for Arabic combining pair. */
2807 i = (*mb_head_off)(ccline.cmdbuff,
2808 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2809 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2810 + ccline.cmdpos - i), c))
2811 {
2812 ccline.cmdpos -= i;
2813 len += i;
2814 }
2815 else
2816 i = 0;
2817 }
2818# endif
2819 if (i != 0)
2820 {
2821 /* Also backup the cursor position. */
2822 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2823 ccline.cmdspos -= i;
2824 msg_col -= i;
2825 if (msg_col < 0)
2826 {
2827 msg_col += Columns;
2828 --msg_row;
2829 }
2830 }
2831 }
2832#endif
2833
2834 if (redraw && !cmd_silent)
2835 {
2836 msg_no_more = TRUE;
2837 i = cmdline_row;
2838 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2839 /* Avoid clearing the rest of the line too often. */
2840 if (cmdline_row != i || ccline.overstrike)
2841 msg_clr_eos();
2842 msg_no_more = FALSE;
2843 }
2844#ifdef FEAT_FKMAP
2845 /*
2846 * If we are in Farsi command mode, the character input must be in
2847 * Insert mode. So do not advance the cmdpos.
2848 */
2849 if (!cmd_fkmap)
2850#endif
2851 {
2852 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002853 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002855 if (m < 0) /* overflow, Columns or Rows at weird value */
2856 m = MAXCOL;
2857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 else
2859 m = MAXCOL;
2860 for (i = 0; i < len; ++i)
2861 {
2862 c = cmdline_charsize(ccline.cmdpos);
2863#ifdef FEAT_MBYTE
2864 /* count ">" for a double-wide char that doesn't fit. */
2865 if (has_mbyte)
2866 correct_cmdspos(ccline.cmdpos, c);
2867#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002868 /* Stop cursor at the end of the screen, but do increment the
2869 * insert position, so that entering a very long command
2870 * works, even though you can't see it. */
2871 if (ccline.cmdspos + c < m)
2872 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873#ifdef FEAT_MBYTE
2874 if (has_mbyte)
2875 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002876 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 if (c > len - i - 1)
2878 c = len - i - 1;
2879 ccline.cmdpos += c;
2880 i += c;
2881 }
2882#endif
2883 ++ccline.cmdpos;
2884 }
2885 }
2886 }
2887 if (redraw)
2888 msg_check();
2889 return retval;
2890}
2891
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002892static struct cmdline_info prev_ccline;
2893static int prev_ccline_used = FALSE;
2894
2895/*
2896 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2897 * and overwrite it. But get_cmdline_str() may need it, thus make it
2898 * available globally in prev_ccline.
2899 */
2900 static void
2901save_cmdline(ccp)
2902 struct cmdline_info *ccp;
2903{
2904 if (!prev_ccline_used)
2905 {
2906 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2907 prev_ccline_used = TRUE;
2908 }
2909 *ccp = prev_ccline;
2910 prev_ccline = ccline;
2911 ccline.cmdbuff = NULL;
2912 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002913 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002914}
2915
2916/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002917 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002918 */
2919 static void
2920restore_cmdline(ccp)
2921 struct cmdline_info *ccp;
2922{
2923 ccline = prev_ccline;
2924 prev_ccline = *ccp;
2925}
2926
Bram Moolenaar5a305422006-04-28 22:38:25 +00002927#if defined(FEAT_EVAL) || defined(PROTO)
2928/*
2929 * Save the command line into allocated memory. Returns a pointer to be
2930 * passed to restore_cmdline_alloc() later.
2931 * Returns NULL when failed.
2932 */
2933 char_u *
2934save_cmdline_alloc()
2935{
2936 struct cmdline_info *p;
2937
2938 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2939 if (p != NULL)
2940 save_cmdline(p);
2941 return (char_u *)p;
2942}
2943
2944/*
2945 * Restore the command line from the return value of save_cmdline_alloc().
2946 */
2947 void
2948restore_cmdline_alloc(p)
2949 char_u *p;
2950{
2951 if (p != NULL)
2952 {
2953 restore_cmdline((struct cmdline_info *)p);
2954 vim_free(p);
2955 }
2956}
2957#endif
2958
Bram Moolenaar8299df92004-07-10 09:47:34 +00002959/*
2960 * paste a yank register into the command line.
2961 * used by CTRL-R command in command-line mode
2962 * insert_reg() can't be used here, because special characters from the
2963 * register contents will be interpreted as commands.
2964 *
2965 * return FAIL for failure, OK otherwise
2966 */
2967 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002968cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00002969 int regname;
2970 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002971 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00002972{
2973 long i;
2974 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002975 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002976 int allocated;
2977 struct cmdline_info save_ccline;
2978
2979 /* check for valid regname; also accept special characters for CTRL-R in
2980 * the command line */
2981 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2982 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2983 return FAIL;
2984
2985 /* A register containing CTRL-R can cause an endless loop. Allow using
2986 * CTRL-C to break the loop. */
2987 line_breakcheck();
2988 if (got_int)
2989 return FAIL;
2990
2991#ifdef FEAT_CLIPBOARD
2992 regname = may_get_selection(regname);
2993#endif
2994
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002995 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002996 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002997 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002998 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002999 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003000 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003001 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003002
3003 if (i)
3004 {
3005 /* Got the value of a special register in "arg". */
3006 if (arg == NULL)
3007 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003008
3009 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3010 * part of the word. */
3011 p = arg;
3012 if (p_is && regname == Ctrl_W)
3013 {
3014 char_u *w;
3015 int len;
3016
3017 /* Locate start of last word in the cmd buffer. */
3018 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
3019 {
3020#ifdef FEAT_MBYTE
3021 if (has_mbyte)
3022 {
3023 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3024 if (!vim_iswordc(mb_ptr2char(w - len)))
3025 break;
3026 w -= len;
3027 }
3028 else
3029#endif
3030 {
3031 if (!vim_iswordc(w[-1]))
3032 break;
3033 --w;
3034 }
3035 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003036 len = (int)((ccline.cmdbuff + ccline.cmdlen) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003037 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3038 p += len;
3039 }
3040
3041 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003042 if (allocated)
3043 vim_free(arg);
3044 return OK;
3045 }
3046
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003047 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003048}
3049
3050/*
3051 * Put a string on the command line.
3052 * When "literally" is TRUE, insert literally.
3053 * When "literally" is FALSE, insert as typed, but don't leave the command
3054 * line.
3055 */
3056 void
3057cmdline_paste_str(s, literally)
3058 char_u *s;
3059 int literally;
3060{
3061 int c, cv;
3062
3063 if (literally)
3064 put_on_cmdline(s, -1, TRUE);
3065 else
3066 while (*s != NUL)
3067 {
3068 cv = *s;
3069 if (cv == Ctrl_V && s[1])
3070 ++s;
3071#ifdef FEAT_MBYTE
3072 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003073 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003074 else
3075#endif
3076 c = *s++;
3077 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3078#ifdef UNIX
3079 || c == intr_char
3080#endif
3081 || (c == Ctrl_BSL && *s == Ctrl_N))
3082 stuffcharReadbuff(Ctrl_V);
3083 stuffcharReadbuff(c);
3084 }
3085}
3086
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087#ifdef FEAT_WILDMENU
3088/*
3089 * Delete characters on the command line, from "from" to the current
3090 * position.
3091 */
3092 static void
3093cmdline_del(from)
3094 int from;
3095{
3096 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3097 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3098 ccline.cmdlen -= ccline.cmdpos - from;
3099 ccline.cmdpos = from;
3100}
3101#endif
3102
3103/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003104 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 * search
3106 */
3107 void
3108redrawcmdline()
3109{
3110 if (cmd_silent)
3111 return;
3112 need_wait_return = FALSE;
3113 compute_cmdrow();
3114 redrawcmd();
3115 cursorcmd();
3116}
3117
3118 static void
3119redrawcmdprompt()
3120{
3121 int i;
3122
3123 if (cmd_silent)
3124 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003125 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 msg_putchar(ccline.cmdfirstc);
3127 if (ccline.cmdprompt != NULL)
3128 {
3129 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3130 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3131 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003132 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 --ccline.cmdindent;
3134 }
3135 else
3136 for (i = ccline.cmdindent; i > 0; --i)
3137 msg_putchar(' ');
3138}
3139
3140/*
3141 * Redraw what is currently on the command line.
3142 */
3143 void
3144redrawcmd()
3145{
3146 if (cmd_silent)
3147 return;
3148
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003149 /* when 'incsearch' is set there may be no command line while redrawing */
3150 if (ccline.cmdbuff == NULL)
3151 {
3152 windgoto(cmdline_row, 0);
3153 msg_clr_eos();
3154 return;
3155 }
3156
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 msg_start();
3158 redrawcmdprompt();
3159
3160 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3161 msg_no_more = TRUE;
3162 draw_cmdline(0, ccline.cmdlen);
3163 msg_clr_eos();
3164 msg_no_more = FALSE;
3165
3166 set_cmdspos_cursor();
3167
3168 /*
3169 * An emsg() before may have set msg_scroll. This is used in normal mode,
3170 * in cmdline mode we can reset them now.
3171 */
3172 msg_scroll = FALSE; /* next message overwrites cmdline */
3173
3174 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3175 * in cmdline mode */
3176 skip_redraw = FALSE;
3177}
3178
3179 void
3180compute_cmdrow()
3181{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003182 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 cmdline_row = Rows - 1;
3184 else
3185 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3186 + W_STATUS_HEIGHT(lastwin);
3187}
3188
3189 static void
3190cursorcmd()
3191{
3192 if (cmd_silent)
3193 return;
3194
3195#ifdef FEAT_RIGHTLEFT
3196 if (cmdmsg_rl)
3197 {
3198 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3199 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3200 if (msg_row <= 0)
3201 msg_row = Rows - 1;
3202 }
3203 else
3204#endif
3205 {
3206 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3207 msg_col = ccline.cmdspos % (int)Columns;
3208 if (msg_row >= Rows)
3209 msg_row = Rows - 1;
3210 }
3211
3212 windgoto(msg_row, msg_col);
3213#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3214 redrawcmd_preedit();
3215#endif
3216#ifdef MCH_CURSOR_SHAPE
3217 mch_update_cursor();
3218#endif
3219}
3220
3221 void
3222gotocmdline(clr)
3223 int clr;
3224{
3225 msg_start();
3226#ifdef FEAT_RIGHTLEFT
3227 if (cmdmsg_rl)
3228 msg_col = Columns - 1;
3229 else
3230#endif
3231 msg_col = 0; /* always start in column 0 */
3232 if (clr) /* clear the bottom line(s) */
3233 msg_clr_eos(); /* will reset clear_cmdline */
3234 windgoto(cmdline_row, 0);
3235}
3236
3237/*
3238 * Check the word in front of the cursor for an abbreviation.
3239 * Called when the non-id character "c" has been entered.
3240 * When an abbreviation is recognized it is removed from the text with
3241 * backspaces and the replacement string is inserted, followed by "c".
3242 */
3243 static int
3244ccheck_abbr(c)
3245 int c;
3246{
3247 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3248 return FALSE;
3249
3250 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3251}
3252
3253/*
3254 * Return FAIL if this is not an appropriate context in which to do
3255 * completion of anything, return OK if it is (even if there are no matches).
3256 * For the caller, this means that the character is just passed through like a
3257 * normal character (instead of being expanded). This allows :s/^I^D etc.
3258 */
3259 static int
3260nextwild(xp, type, options)
3261 expand_T *xp;
3262 int type;
3263 int options; /* extra options for ExpandOne() */
3264{
3265 int i, j;
3266 char_u *p1;
3267 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 int difflen;
3269 int v;
3270
3271 if (xp->xp_numfiles == -1)
3272 {
3273 set_expand_context(xp);
3274 cmd_showtail = expand_showtail(xp);
3275 }
3276
3277 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3278 {
3279 beep_flush();
3280 return OK; /* Something illegal on command line */
3281 }
3282 if (xp->xp_context == EXPAND_NOTHING)
3283 {
3284 /* Caller can use the character as a normal char instead */
3285 return FAIL;
3286 }
3287
3288 MSG_PUTS("..."); /* show that we are busy */
3289 out_flush();
3290
3291 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003292 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
3294 if (type == WILD_NEXT || type == WILD_PREV)
3295 {
3296 /*
3297 * Get next/previous match for a previous expanded pattern.
3298 */
3299 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3300 }
3301 else
3302 {
3303 /*
3304 * Translate string into pattern and expand it.
3305 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003306 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3307 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 p2 = NULL;
3309 else
3310 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003311 p2 = ExpandOne(xp, p1,
3312 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3314 |options, type);
3315 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003316 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (p2 != NULL && type == WILD_LONGEST)
3318 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003319 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 if (ccline.cmdbuff[i + j] == '*'
3321 || ccline.cmdbuff[i + j] == '?')
3322 break;
3323 if ((int)STRLEN(p2) < j)
3324 {
3325 vim_free(p2);
3326 p2 = NULL;
3327 }
3328 }
3329 }
3330 }
3331
3332 if (p2 != NULL && !got_int)
3333 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003334 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3336 {
3337 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3338 xp->xp_pattern = ccline.cmdbuff + i;
3339 }
3340 else
3341 v = OK;
3342 if (v == OK)
3343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003344 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3345 &ccline.cmdbuff[ccline.cmdpos],
3346 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3347 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 ccline.cmdlen += difflen;
3349 ccline.cmdpos += difflen;
3350 }
3351 }
3352 vim_free(p2);
3353
3354 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003355 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
3357 /* When expanding a ":map" command and no matches are found, assume that
3358 * the key is supposed to be inserted literally */
3359 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3360 return FAIL;
3361
3362 if (xp->xp_numfiles <= 0 && p2 == NULL)
3363 beep_flush();
3364 else if (xp->xp_numfiles == 1)
3365 /* free expanded pattern */
3366 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3367
3368 return OK;
3369}
3370
3371/*
3372 * Do wildcard expansion on the string 'str'.
3373 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003374 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 * Return NULL for failure.
3376 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003377 * "orig" is the originally expanded string, copied to allocated memory. It
3378 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3379 * WILD_PREV "orig" should be NULL.
3380 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003381 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3382 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 *
3384 * mode = WILD_FREE: just free previously expanded matches
3385 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3386 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3387 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3388 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3389 * mode = WILD_ALL: return all matches concatenated
3390 * mode = WILD_LONGEST: return longest matched part
3391 *
3392 * options = WILD_LIST_NOTFOUND: list entries without a match
3393 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3394 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3395 * options = WILD_NO_BEEP: Don't beep for multiple matches
3396 * options = WILD_ADD_SLASH: add a slash after directory names
3397 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3398 * options = WILD_SILENT: don't print warning messages
3399 * options = WILD_ESCAPE: put backslash before special chars
3400 *
3401 * The variables xp->xp_context and xp->xp_backslash must have been set!
3402 */
3403 char_u *
3404ExpandOne(xp, str, orig, options, mode)
3405 expand_T *xp;
3406 char_u *str;
3407 char_u *orig; /* allocated copy of original of expanded string */
3408 int options;
3409 int mode;
3410{
3411 char_u *ss = NULL;
3412 static int findex;
3413 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003414 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 int i;
3416 long_u len;
3417 int non_suf_match; /* number without matching suffix */
3418
3419 /*
3420 * first handle the case of using an old match
3421 */
3422 if (mode == WILD_NEXT || mode == WILD_PREV)
3423 {
3424 if (xp->xp_numfiles > 0)
3425 {
3426 if (mode == WILD_PREV)
3427 {
3428 if (findex == -1)
3429 findex = xp->xp_numfiles;
3430 --findex;
3431 }
3432 else /* mode == WILD_NEXT */
3433 ++findex;
3434
3435 /*
3436 * When wrapping around, return the original string, set findex to
3437 * -1.
3438 */
3439 if (findex < 0)
3440 {
3441 if (orig_save == NULL)
3442 findex = xp->xp_numfiles - 1;
3443 else
3444 findex = -1;
3445 }
3446 if (findex >= xp->xp_numfiles)
3447 {
3448 if (orig_save == NULL)
3449 findex = 0;
3450 else
3451 findex = -1;
3452 }
3453#ifdef FEAT_WILDMENU
3454 if (p_wmnu)
3455 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3456 findex, cmd_showtail);
3457#endif
3458 if (findex == -1)
3459 return vim_strsave(orig_save);
3460 return vim_strsave(xp->xp_files[findex]);
3461 }
3462 else
3463 return NULL;
3464 }
3465
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003466 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3468 {
3469 FreeWild(xp->xp_numfiles, xp->xp_files);
3470 xp->xp_numfiles = -1;
3471 vim_free(orig_save);
3472 orig_save = NULL;
3473 }
3474 findex = 0;
3475
3476 if (mode == WILD_FREE) /* only release file name */
3477 return NULL;
3478
3479 if (xp->xp_numfiles == -1)
3480 {
3481 vim_free(orig_save);
3482 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003483 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
3485 /*
3486 * Do the expansion.
3487 */
3488 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3489 options) == FAIL)
3490 {
3491#ifdef FNAME_ILLEGAL
3492 /* Illegal file name has been silently skipped. But when there
3493 * are wildcards, the real problem is that there was no match,
3494 * causing the pattern to be added, which has illegal characters.
3495 */
3496 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3497 EMSG2(_(e_nomatch2), str);
3498#endif
3499 }
3500 else if (xp->xp_numfiles == 0)
3501 {
3502 if (!(options & WILD_SILENT))
3503 EMSG2(_(e_nomatch2), str);
3504 }
3505 else
3506 {
3507 /* Escape the matches for use on the command line. */
3508 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3509
3510 /*
3511 * Check for matching suffixes in file names.
3512 */
3513 if (mode != WILD_ALL && mode != WILD_LONGEST)
3514 {
3515 if (xp->xp_numfiles)
3516 non_suf_match = xp->xp_numfiles;
3517 else
3518 non_suf_match = 1;
3519 if ((xp->xp_context == EXPAND_FILES
3520 || xp->xp_context == EXPAND_DIRECTORIES)
3521 && xp->xp_numfiles > 1)
3522 {
3523 /*
3524 * More than one match; check suffix.
3525 * The files will have been sorted on matching suffix in
3526 * expand_wildcards, only need to check the first two.
3527 */
3528 non_suf_match = 0;
3529 for (i = 0; i < 2; ++i)
3530 if (match_suffix(xp->xp_files[i]))
3531 ++non_suf_match;
3532 }
3533 if (non_suf_match != 1)
3534 {
3535 /* Can we ever get here unless it's while expanding
3536 * interactively? If not, we can get rid of this all
3537 * together. Don't really want to wait for this message
3538 * (and possibly have to hit return to continue!).
3539 */
3540 if (!(options & WILD_SILENT))
3541 EMSG(_(e_toomany));
3542 else if (!(options & WILD_NO_BEEP))
3543 beep_flush();
3544 }
3545 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3546 ss = vim_strsave(xp->xp_files[0]);
3547 }
3548 }
3549 }
3550
3551 /* Find longest common part */
3552 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3553 {
3554 for (len = 0; xp->xp_files[0][len]; ++len)
3555 {
3556 for (i = 0; i < xp->xp_numfiles; ++i)
3557 {
3558#ifdef CASE_INSENSITIVE_FILENAME
3559 if (xp->xp_context == EXPAND_DIRECTORIES
3560 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003561 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 || xp->xp_context == EXPAND_BUFFERS)
3563 {
3564 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3565 TOLOWER_LOC(xp->xp_files[0][len]))
3566 break;
3567 }
3568 else
3569#endif
3570 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3571 break;
3572 }
3573 if (i < xp->xp_numfiles)
3574 {
3575 if (!(options & WILD_NO_BEEP))
3576 vim_beep();
3577 break;
3578 }
3579 }
3580 ss = alloc((unsigned)len + 1);
3581 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003582 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 findex = -1; /* next p_wc gets first one */
3584 }
3585
3586 /* Concatenate all matching names */
3587 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3588 {
3589 len = 0;
3590 for (i = 0; i < xp->xp_numfiles; ++i)
3591 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3592 ss = lalloc(len, TRUE);
3593 if (ss != NULL)
3594 {
3595 *ss = NUL;
3596 for (i = 0; i < xp->xp_numfiles; ++i)
3597 {
3598 STRCAT(ss, xp->xp_files[i]);
3599 if (i != xp->xp_numfiles - 1)
3600 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3601 }
3602 }
3603 }
3604
3605 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3606 ExpandCleanup(xp);
3607
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003608 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003609 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003610 vim_free(orig);
3611
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 return ss;
3613}
3614
3615/*
3616 * Prepare an expand structure for use.
3617 */
3618 void
3619ExpandInit(xp)
3620 expand_T *xp;
3621{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003622 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003623 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003625#ifndef BACKSLASH_IN_FILENAME
3626 xp->xp_shell = FALSE;
3627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 xp->xp_numfiles = -1;
3629 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003630#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3631 xp->xp_arg = NULL;
3632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633}
3634
3635/*
3636 * Cleanup an expand structure after use.
3637 */
3638 void
3639ExpandCleanup(xp)
3640 expand_T *xp;
3641{
3642 if (xp->xp_numfiles >= 0)
3643 {
3644 FreeWild(xp->xp_numfiles, xp->xp_files);
3645 xp->xp_numfiles = -1;
3646 }
3647}
3648
3649 void
3650ExpandEscape(xp, str, numfiles, files, options)
3651 expand_T *xp;
3652 char_u *str;
3653 int numfiles;
3654 char_u **files;
3655 int options;
3656{
3657 int i;
3658 char_u *p;
3659
3660 /*
3661 * May change home directory back to "~"
3662 */
3663 if (options & WILD_HOME_REPLACE)
3664 tilde_replace(str, numfiles, files);
3665
3666 if (options & WILD_ESCAPE)
3667 {
3668 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003669 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 || xp->xp_context == EXPAND_BUFFERS
3671 || xp->xp_context == EXPAND_DIRECTORIES)
3672 {
3673 /*
3674 * Insert a backslash into a file name before a space, \, %, #
3675 * and wildmatch characters, except '~'.
3676 */
3677 for (i = 0; i < numfiles; ++i)
3678 {
3679 /* for ":set path=" we need to escape spaces twice */
3680 if (xp->xp_backslash == XP_BS_THREE)
3681 {
3682 p = vim_strsave_escaped(files[i], (char_u *)" ");
3683 if (p != NULL)
3684 {
3685 vim_free(files[i]);
3686 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003687#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 p = vim_strsave_escaped(files[i], (char_u *)" ");
3689 if (p != NULL)
3690 {
3691 vim_free(files[i]);
3692 files[i] = p;
3693 }
3694#endif
3695 }
3696 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003697#ifdef BACKSLASH_IN_FILENAME
3698 p = vim_strsave_fnameescape(files[i], FALSE);
3699#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003700 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 if (p != NULL)
3703 {
3704 vim_free(files[i]);
3705 files[i] = p;
3706 }
3707
3708 /* If 'str' starts with "\~", replace "~" at start of
3709 * files[i] with "\~". */
3710 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003711 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 }
3713 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003714
3715 /* If the first file starts with a '+' escape it. Otherwise it
3716 * could be seen as "+cmd". */
3717 if (*files[0] == '+')
3718 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720 else if (xp->xp_context == EXPAND_TAGS)
3721 {
3722 /*
3723 * Insert a backslash before characters in a tag name that
3724 * would terminate the ":tag" command.
3725 */
3726 for (i = 0; i < numfiles; ++i)
3727 {
3728 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3729 if (p != NULL)
3730 {
3731 vim_free(files[i]);
3732 files[i] = p;
3733 }
3734 }
3735 }
3736 }
3737}
3738
3739/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003740 * Escape special characters in "fname" for when used as a file name argument
3741 * after a Vim command, or, when "shell" is non-zero, a shell command.
3742 * Returns the result in allocated memory.
3743 */
3744 char_u *
3745vim_strsave_fnameescape(fname, shell)
3746 char_u *fname;
3747 int shell;
3748{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003749 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003750#ifdef BACKSLASH_IN_FILENAME
3751 char_u buf[20];
3752 int j = 0;
3753
3754 /* Don't escape '[' and '{' if they are in 'isfname'. */
3755 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3756 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3757 buf[j++] = *p;
3758 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003759 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003760#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003761 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3762 if (shell && csh_like_shell() && p != NULL)
3763 {
3764 char_u *s;
3765
3766 /* For csh and similar shells need to put two backslashes before '!'.
3767 * One is taken by Vim, one by the shell. */
3768 s = vim_strsave_escaped(p, (char_u *)"!");
3769 vim_free(p);
3770 p = s;
3771 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003772#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003773
3774 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3775 * ":write". "cd -" has a special meaning. */
3776 if (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))
3777 escape_fname(&p);
3778
3779 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003780}
3781
3782/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003783 * Put a backslash before the file name in "pp", which is in allocated memory.
3784 */
3785 static void
3786escape_fname(pp)
3787 char_u **pp;
3788{
3789 char_u *p;
3790
3791 p = alloc((unsigned)(STRLEN(*pp) + 2));
3792 if (p != NULL)
3793 {
3794 p[0] = '\\';
3795 STRCPY(p + 1, *pp);
3796 vim_free(*pp);
3797 *pp = p;
3798 }
3799}
3800
3801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 * For each file name in files[num_files]:
3803 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3804 */
3805 void
3806tilde_replace(orig_pat, num_files, files)
3807 char_u *orig_pat;
3808 int num_files;
3809 char_u **files;
3810{
3811 int i;
3812 char_u *p;
3813
3814 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3815 {
3816 for (i = 0; i < num_files; ++i)
3817 {
3818 p = home_replace_save(NULL, files[i]);
3819 if (p != NULL)
3820 {
3821 vim_free(files[i]);
3822 files[i] = p;
3823 }
3824 }
3825 }
3826}
3827
3828/*
3829 * Show all matches for completion on the command line.
3830 * Returns EXPAND_NOTHING when the character that triggered expansion should
3831 * be inserted like a normal character.
3832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 static int
3834showmatches(xp, wildmenu)
3835 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003836 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837{
3838#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3839 int num_files;
3840 char_u **files_found;
3841 int i, j, k;
3842 int maxlen;
3843 int lines;
3844 int columns;
3845 char_u *p;
3846 int lastlen;
3847 int attr;
3848 int showtail;
3849
3850 if (xp->xp_numfiles == -1)
3851 {
3852 set_expand_context(xp);
3853 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3854 &num_files, &files_found);
3855 showtail = expand_showtail(xp);
3856 if (i != EXPAND_OK)
3857 return i;
3858
3859 }
3860 else
3861 {
3862 num_files = xp->xp_numfiles;
3863 files_found = xp->xp_files;
3864 showtail = cmd_showtail;
3865 }
3866
3867#ifdef FEAT_WILDMENU
3868 if (!wildmenu)
3869 {
3870#endif
3871 msg_didany = FALSE; /* lines_left will be set */
3872 msg_start(); /* prepare for paging */
3873 msg_putchar('\n');
3874 out_flush();
3875 cmdline_row = msg_row;
3876 msg_didany = FALSE; /* lines_left will be set again */
3877 msg_start(); /* prepare for paging */
3878#ifdef FEAT_WILDMENU
3879 }
3880#endif
3881
3882 if (got_int)
3883 got_int = FALSE; /* only int. the completion, not the cmd line */
3884#ifdef FEAT_WILDMENU
3885 else if (wildmenu)
3886 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3887#endif
3888 else
3889 {
3890 /* find the length of the longest file name */
3891 maxlen = 0;
3892 for (i = 0; i < num_files; ++i)
3893 {
3894 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003895 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 || xp->xp_context == EXPAND_BUFFERS))
3897 {
3898 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3899 j = vim_strsize(NameBuff);
3900 }
3901 else
3902 j = vim_strsize(L_SHOWFILE(i));
3903 if (j > maxlen)
3904 maxlen = j;
3905 }
3906
3907 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3908 lines = num_files;
3909 else
3910 {
3911 /* compute the number of columns and lines for the listing */
3912 maxlen += 2; /* two spaces between file names */
3913 columns = ((int)Columns + 2) / maxlen;
3914 if (columns < 1)
3915 columns = 1;
3916 lines = (num_files + columns - 1) / columns;
3917 }
3918
3919 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3920
3921 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3922 {
3923 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3924 msg_clr_eos();
3925 msg_advance(maxlen - 3);
3926 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3927 }
3928
3929 /* list the files line by line */
3930 for (i = 0; i < lines; ++i)
3931 {
3932 lastlen = 999;
3933 for (k = i; k < num_files; k += lines)
3934 {
3935 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3936 {
3937 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3938 p = files_found[k] + STRLEN(files_found[k]) + 1;
3939 msg_advance(maxlen + 1);
3940 msg_puts(p);
3941 msg_advance(maxlen + 3);
3942 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3943 break;
3944 }
3945 for (j = maxlen - lastlen; --j >= 0; )
3946 msg_putchar(' ');
3947 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003948 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 || xp->xp_context == EXPAND_BUFFERS)
3950 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01003951 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01003952 if (xp->xp_numfiles != -1)
3953 {
3954 char_u *halved_slash;
3955 char_u *exp_path;
3956
3957 /* Expansion was done before and special characters
3958 * were escaped, need to halve backslashes. Also
3959 * $HOME has been replaced with ~/. */
3960 exp_path = expand_env_save_opt(files_found[k], TRUE);
3961 halved_slash = backslash_halve_save(
3962 exp_path != NULL ? exp_path : files_found[k]);
3963 j = mch_isdir(halved_slash != NULL ? halved_slash
3964 : files_found[k]);
3965 vim_free(exp_path);
3966 vim_free(halved_slash);
3967 }
3968 else
3969 /* Expansion was done here, file names are literal. */
3970 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 if (showtail)
3972 p = L_SHOWFILE(k);
3973 else
3974 {
3975 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3976 TRUE);
3977 p = NameBuff;
3978 }
3979 }
3980 else
3981 {
3982 j = FALSE;
3983 p = L_SHOWFILE(k);
3984 }
3985 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3986 }
3987 if (msg_col > 0) /* when not wrapped around */
3988 {
3989 msg_clr_eos();
3990 msg_putchar('\n');
3991 }
3992 out_flush(); /* show one line at a time */
3993 if (got_int)
3994 {
3995 got_int = FALSE;
3996 break;
3997 }
3998 }
3999
4000 /*
4001 * we redraw the command below the lines that we have just listed
4002 * This is a bit tricky, but it saves a lot of screen updating.
4003 */
4004 cmdline_row = msg_row; /* will put it back later */
4005 }
4006
4007 if (xp->xp_numfiles == -1)
4008 FreeWild(num_files, files_found);
4009
4010 return EXPAND_OK;
4011}
4012
4013/*
4014 * Private gettail for showmatches() (and win_redr_status_matches()):
4015 * Find tail of file name path, but ignore trailing "/".
4016 */
4017 char_u *
4018sm_gettail(s)
4019 char_u *s;
4020{
4021 char_u *p;
4022 char_u *t = s;
4023 int had_sep = FALSE;
4024
4025 for (p = s; *p != NUL; )
4026 {
4027 if (vim_ispathsep(*p)
4028#ifdef BACKSLASH_IN_FILENAME
4029 && !rem_backslash(p)
4030#endif
4031 )
4032 had_sep = TRUE;
4033 else if (had_sep)
4034 {
4035 t = p;
4036 had_sep = FALSE;
4037 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004038 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
4040 return t;
4041}
4042
4043/*
4044 * Return TRUE if we only need to show the tail of completion matches.
4045 * When not completing file names or there is a wildcard in the path FALSE is
4046 * returned.
4047 */
4048 static int
4049expand_showtail(xp)
4050 expand_T *xp;
4051{
4052 char_u *s;
4053 char_u *end;
4054
4055 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004056 if (xp->xp_context != EXPAND_FILES
4057 && xp->xp_context != EXPAND_SHELLCMD
4058 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 return FALSE;
4060
4061 end = gettail(xp->xp_pattern);
4062 if (end == xp->xp_pattern) /* there is no path separator */
4063 return FALSE;
4064
4065 for (s = xp->xp_pattern; s < end; s++)
4066 {
4067 /* Skip escaped wildcards. Only when the backslash is not a path
4068 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4069 if (rem_backslash(s))
4070 ++s;
4071 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4072 return FALSE;
4073 }
4074 return TRUE;
4075}
4076
4077/*
4078 * Prepare a string for expansion.
4079 * When expanding file names: The string will be used with expand_wildcards().
4080 * Copy the file name into allocated memory and add a '*' at the end.
4081 * When expanding other names: The string will be used with regcomp(). Copy
4082 * the name into allocated memory and prepend "^".
4083 */
4084 char_u *
4085addstar(fname, len, context)
4086 char_u *fname;
4087 int len;
4088 int context; /* EXPAND_FILES etc. */
4089{
4090 char_u *retval;
4091 int i, j;
4092 int new_len;
4093 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004094 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004096 if (context != EXPAND_FILES
4097 && context != EXPAND_SHELLCMD
4098 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 {
4100 /*
4101 * Matching will be done internally (on something other than files).
4102 * So we convert the file-matching-type wildcards into our kind for
4103 * use with vim_regcomp(). First work out how long it will be:
4104 */
4105
4106 /* For help tags the translation is done in find_help_tags().
4107 * For a tag pattern starting with "/" no translation is needed. */
4108 if (context == EXPAND_HELP
4109 || context == EXPAND_COLORS
4110 || context == EXPAND_COMPILER
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004111 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 || (context == EXPAND_TAGS && fname[0] == '/'))
4113 retval = vim_strnsave(fname, len);
4114 else
4115 {
4116 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4117 for (i = 0; i < len; i++)
4118 {
4119 if (fname[i] == '*' || fname[i] == '~')
4120 new_len++; /* '*' needs to be replaced by ".*"
4121 '~' needs to be replaced by "\~" */
4122
4123 /* Buffer names are like file names. "." should be literal */
4124 if (context == EXPAND_BUFFERS && fname[i] == '.')
4125 new_len++; /* "." becomes "\." */
4126
4127 /* Custom expansion takes care of special things, match
4128 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004129 if ((context == EXPAND_USER_DEFINED
4130 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 new_len++; /* '\' becomes "\\" */
4132 }
4133 retval = alloc(new_len);
4134 if (retval != NULL)
4135 {
4136 retval[0] = '^';
4137 j = 1;
4138 for (i = 0; i < len; i++, j++)
4139 {
4140 /* Skip backslash. But why? At least keep it for custom
4141 * expansion. */
4142 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004143 && context != EXPAND_USER_LIST
4144 && fname[i] == '\\'
4145 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 break;
4147
4148 switch (fname[i])
4149 {
4150 case '*': retval[j++] = '.';
4151 break;
4152 case '~': retval[j++] = '\\';
4153 break;
4154 case '?': retval[j] = '.';
4155 continue;
4156 case '.': if (context == EXPAND_BUFFERS)
4157 retval[j++] = '\\';
4158 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004159 case '\\': if (context == EXPAND_USER_DEFINED
4160 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 retval[j++] = '\\';
4162 break;
4163 }
4164 retval[j] = fname[i];
4165 }
4166 retval[j] = NUL;
4167 }
4168 }
4169 }
4170 else
4171 {
4172 retval = alloc(len + 4);
4173 if (retval != NULL)
4174 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004175 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
4177 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004178 * Don't add a star to *, ~, ~user, $var or `cmd`.
4179 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 * ~ would be at the start of the file name, but not the tail.
4181 * $ could be anywhere in the tail.
4182 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004183 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 */
4185 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004186 ends_in_star = (len > 0 && retval[len - 1] == '*');
4187#ifndef BACKSLASH_IN_FILENAME
4188 for (i = len - 2; i >= 0; --i)
4189 {
4190 if (retval[i] != '\\')
4191 break;
4192 ends_in_star = !ends_in_star;
4193 }
4194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004196 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 && vim_strchr(tail, '$') == NULL
4198 && vim_strchr(retval, '`') == NULL)
4199 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004200 else if (len > 0 && retval[len - 1] == '$')
4201 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 retval[len] = NUL;
4203 }
4204 }
4205 return retval;
4206}
4207
4208/*
4209 * Must parse the command line so far to work out what context we are in.
4210 * Completion can then be done based on that context.
4211 * This routine sets the variables:
4212 * xp->xp_pattern The start of the pattern to be expanded within
4213 * the command line (ends at the cursor).
4214 * xp->xp_context The type of thing to expand. Will be one of:
4215 *
4216 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4217 * the command line, like an unknown command. Caller
4218 * should beep.
4219 * EXPAND_NOTHING Unrecognised context for completion, use char like
4220 * a normal char, rather than for completion. eg
4221 * :s/^I/
4222 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4223 * it.
4224 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4225 * EXPAND_FILES After command with XFILE set, or after setting
4226 * with P_EXPAND set. eg :e ^I, :w>>^I
4227 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4228 * when we know only directories are of interest. eg
4229 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004230 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4232 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4233 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4234 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4235 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4236 * EXPAND_EVENTS Complete event names
4237 * EXPAND_SYNTAX Complete :syntax command arguments
4238 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4239 * EXPAND_AUGROUP Complete autocommand group names
4240 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4241 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4242 * eg :unmap a^I , :cunab x^I
4243 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4244 * eg :call sub^I
4245 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4246 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4247 * names in expressions, eg :while s^I
4248 * EXPAND_ENV_VARS Complete environment variable names
4249 */
4250 static void
4251set_expand_context(xp)
4252 expand_T *xp;
4253{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004254 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 if (ccline.cmdfirstc != ':'
4256#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004257 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004258 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259#endif
4260 )
4261 {
4262 xp->xp_context = EXPAND_NOTHING;
4263 return;
4264 }
4265 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4266}
4267
4268 void
4269set_cmd_context(xp, str, len, col)
4270 expand_T *xp;
4271 char_u *str; /* start of command line */
4272 int len; /* length of command line (excl. NUL) */
4273 int col; /* position of cursor */
4274{
4275 int old_char = NUL;
4276 char_u *nextcomm;
4277
4278 /*
4279 * Avoid a UMR warning from Purify, only save the character if it has been
4280 * written before.
4281 */
4282 if (col < len)
4283 old_char = str[col];
4284 str[col] = NUL;
4285 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004286
4287#ifdef FEAT_EVAL
4288 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004289 {
4290# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004291 /* pass CMD_SIZE because there is no real command */
4292 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004293# endif
4294 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004295 else if (ccline.input_fn)
4296 {
4297 xp->xp_context = ccline.xp_context;
4298 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004299# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004300 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004301# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004302 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004303 else
4304#endif
4305 while (nextcomm != NULL)
4306 nextcomm = set_one_cmd_context(xp, nextcomm);
4307
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 str[col] = old_char;
4309}
4310
4311/*
4312 * Expand the command line "str" from context "xp".
4313 * "xp" must have been set by set_cmd_context().
4314 * xp->xp_pattern points into "str", to where the text that is to be expanded
4315 * starts.
4316 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4317 * cursor.
4318 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4319 * key that triggered expansion literally.
4320 * Returns EXPAND_OK otherwise.
4321 */
4322 int
4323expand_cmdline(xp, str, col, matchcount, matches)
4324 expand_T *xp;
4325 char_u *str; /* start of command line */
4326 int col; /* position of cursor */
4327 int *matchcount; /* return: nr of matches */
4328 char_u ***matches; /* return: array of pointers to matches */
4329{
4330 char_u *file_str = NULL;
4331
4332 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4333 {
4334 beep_flush();
4335 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4336 }
4337 if (xp->xp_context == EXPAND_NOTHING)
4338 {
4339 /* Caller can use the character as a normal char instead */
4340 return EXPAND_NOTHING;
4341 }
4342
4343 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004344 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4345 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 if (file_str == NULL)
4347 return EXPAND_UNSUCCESSFUL;
4348
4349 /* find all files that match the description */
4350 if (ExpandFromContext(xp, file_str, matchcount, matches,
4351 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4352 {
4353 *matchcount = 0;
4354 *matches = NULL;
4355 }
4356 vim_free(file_str);
4357
4358 return EXPAND_OK;
4359}
4360
4361#ifdef FEAT_MULTI_LANG
4362/*
4363 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4364 */
4365static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4366
4367 static void
4368cleanup_help_tags(num_file, file)
4369 int num_file;
4370 char_u **file;
4371{
4372 int i, j;
4373 int len;
4374
4375 for (i = 0; i < num_file; ++i)
4376 {
4377 len = (int)STRLEN(file[i]) - 3;
4378 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4379 {
4380 /* Sorting on priority means the same item in another language may
4381 * be anywhere. Search all items for a match up to the "@en". */
4382 for (j = 0; j < num_file; ++j)
4383 if (j != i
4384 && (int)STRLEN(file[j]) == len + 3
4385 && STRNCMP(file[i], file[j], len + 1) == 0)
4386 break;
4387 if (j == num_file)
4388 file[i][len] = NUL;
4389 }
4390 }
4391}
4392#endif
4393
4394/*
4395 * Do the expansion based on xp->xp_context and "pat".
4396 */
4397 static int
4398ExpandFromContext(xp, pat, num_file, file, options)
4399 expand_T *xp;
4400 char_u *pat;
4401 int *num_file;
4402 char_u ***file;
4403 int options;
4404{
4405#ifdef FEAT_CMDL_COMPL
4406 regmatch_T regmatch;
4407#endif
4408 int ret;
4409 int flags;
4410
4411 flags = EW_DIR; /* include directories */
4412 if (options & WILD_LIST_NOTFOUND)
4413 flags |= EW_NOTFOUND;
4414 if (options & WILD_ADD_SLASH)
4415 flags |= EW_ADDSLASH;
4416 if (options & WILD_KEEP_ALL)
4417 flags |= EW_KEEPALL;
4418 if (options & WILD_SILENT)
4419 flags |= EW_SILENT;
4420
4421 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4422 {
4423 /*
4424 * Expand file or directory names.
4425 */
4426 int free_pat = FALSE;
4427 int i;
4428
4429 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4430 * space */
4431 if (xp->xp_backslash != XP_BS_NONE)
4432 {
4433 free_pat = TRUE;
4434 pat = vim_strsave(pat);
4435 for (i = 0; pat[i]; ++i)
4436 if (pat[i] == '\\')
4437 {
4438 if (xp->xp_backslash == XP_BS_THREE
4439 && pat[i + 1] == '\\'
4440 && pat[i + 2] == '\\'
4441 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004442 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 if (xp->xp_backslash == XP_BS_ONE
4444 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004445 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447 }
4448
4449 if (xp->xp_context == EXPAND_FILES)
4450 flags |= EW_FILE;
4451 else
4452 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaard7834d32009-12-02 16:14:36 +00004453 /* Expand wildcards, supporting %:h and the like. */
4454 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 if (free_pat)
4456 vim_free(pat);
4457 return ret;
4458 }
4459
4460 *file = (char_u **)"";
4461 *num_file = 0;
4462 if (xp->xp_context == EXPAND_HELP)
4463 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004464 /* With an empty argument we would get all the help tags, which is
4465 * very slow. Get matches for "help" instead. */
4466 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4467 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 {
4469#ifdef FEAT_MULTI_LANG
4470 cleanup_help_tags(*num_file, *file);
4471#endif
4472 return OK;
4473 }
4474 return FAIL;
4475 }
4476
4477#ifndef FEAT_CMDL_COMPL
4478 return FAIL;
4479#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004480 if (xp->xp_context == EXPAND_SHELLCMD)
4481 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 if (xp->xp_context == EXPAND_OLD_SETTING)
4483 return ExpandOldSetting(num_file, file);
4484 if (xp->xp_context == EXPAND_BUFFERS)
4485 return ExpandBufnames(pat, num_file, file, options);
4486 if (xp->xp_context == EXPAND_TAGS
4487 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4488 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4489 if (xp->xp_context == EXPAND_COLORS)
4490 return ExpandRTDir(pat, num_file, file, "colors");
4491 if (xp->xp_context == EXPAND_COMPILER)
4492 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004493 if (xp->xp_context == EXPAND_FILETYPE)
4494 return ExpandRTDir(pat, num_file, file, "syntax");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004495# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4496 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004497 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004498# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
4500 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4501 if (regmatch.regprog == NULL)
4502 return FAIL;
4503
4504 /* set ignore-case according to p_ic, p_scs and pat */
4505 regmatch.rm_ic = ignorecase(pat);
4506
4507 if (xp->xp_context == EXPAND_SETTINGS
4508 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4509 ret = ExpandSettings(xp, &regmatch, num_file, file);
4510 else if (xp->xp_context == EXPAND_MAPPINGS)
4511 ret = ExpandMappings(&regmatch, num_file, file);
4512# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4513 else if (xp->xp_context == EXPAND_USER_DEFINED)
4514 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4515# endif
4516 else
4517 {
4518 static struct expgen
4519 {
4520 int context;
4521 char_u *((*func)__ARGS((expand_T *, int)));
4522 int ic;
4523 } tab[] =
4524 {
4525 {EXPAND_COMMANDS, get_command_name, FALSE},
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004526 {EXPAND_BEHAVE, get_behave_arg, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527#ifdef FEAT_USR_CMDS
4528 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4529 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4530 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4531 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4532#endif
4533#ifdef FEAT_EVAL
4534 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4535 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4536 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4537 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4538#endif
4539#ifdef FEAT_MENU
4540 {EXPAND_MENUS, get_menu_name, FALSE},
4541 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4542#endif
4543#ifdef FEAT_SYN_HL
4544 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4545#endif
4546 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4547#ifdef FEAT_AUTOCMD
4548 {EXPAND_EVENTS, get_event_name, TRUE},
4549 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4550#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004551#ifdef FEAT_CSCOPE
4552 {EXPAND_CSCOPE, get_cscope_name, TRUE},
4553#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004554#ifdef FEAT_SIGNS
4555 {EXPAND_SIGN, get_sign_name, TRUE},
4556#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004557#ifdef FEAT_PROFILE
4558 {EXPAND_PROFILE, get_profile_name, TRUE},
4559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4561 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4562 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4563#endif
4564 {EXPAND_ENV_VARS, get_env_name, TRUE},
4565 };
4566 int i;
4567
4568 /*
4569 * Find a context in the table and call the ExpandGeneric() with the
4570 * right function to do the expansion.
4571 */
4572 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004573 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 if (xp->xp_context == tab[i].context)
4575 {
4576 if (tab[i].ic)
4577 regmatch.rm_ic = TRUE;
4578 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4579 break;
4580 }
4581 }
4582
4583 vim_free(regmatch.regprog);
4584
4585 return ret;
4586#endif /* FEAT_CMDL_COMPL */
4587}
4588
4589#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4590/*
4591 * Expand a list of names.
4592 *
4593 * Generic function for command line completion. It calls a function to
4594 * obtain strings, one by one. The strings are matched against a regexp
4595 * program. Matching strings are copied into an array, which is returned.
4596 *
4597 * Returns OK when no problems encountered, FAIL for error (out of memory).
4598 */
4599 int
4600ExpandGeneric(xp, regmatch, num_file, file, func)
4601 expand_T *xp;
4602 regmatch_T *regmatch;
4603 int *num_file;
4604 char_u ***file;
4605 char_u *((*func)__ARGS((expand_T *, int)));
4606 /* returns a string from the list */
4607{
4608 int i;
4609 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004610 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 char_u *str;
4612
4613 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004614 * round == 0: count the number of matching names
4615 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004617 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 {
4619 for (i = 0; ; ++i)
4620 {
4621 str = (*func)(xp, i);
4622 if (str == NULL) /* end of list */
4623 break;
4624 if (*str == NUL) /* skip empty strings */
4625 continue;
4626
4627 if (vim_regexec(regmatch, str, (colnr_T)0))
4628 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004629 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
4631 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4632 (*file)[count] = str;
4633#ifdef FEAT_MENU
4634 if (func == get_menu_names && str != NULL)
4635 {
4636 /* test for separator added by get_menu_names() */
4637 str += STRLEN(str) - 1;
4638 if (*str == '\001')
4639 *str = '.';
4640 }
4641#endif
4642 }
4643 ++count;
4644 }
4645 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004646 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
4648 if (count == 0)
4649 return OK;
4650 *num_file = count;
4651 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4652 if (*file == NULL)
4653 {
4654 *file = (char_u **)"";
4655 return FAIL;
4656 }
4657 count = 0;
4658 }
4659 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004660
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004661 /* Sort the results. Keep menu's in the specified order. */
4662 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4663 sort_strings(*file, *num_file);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004664
Bram Moolenaar4f688582007-07-24 12:34:30 +00004665#ifdef FEAT_CMDL_COMPL
4666 /* Reset the variables used for special highlight names expansion, so that
4667 * they don't show up when getting normal highlight names by ID. */
4668 reset_expand_highlight();
4669#endif
4670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 return OK;
4672}
4673
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004674/*
4675 * Complete a shell command.
4676 * Returns FAIL or OK;
4677 */
4678 static int
4679expand_shellcmd(filepat, num_file, file, flagsarg)
4680 char_u *filepat; /* pattern to match with command names */
4681 int *num_file; /* return: number of matches */
4682 char_u ***file; /* return: array with matches */
4683 int flagsarg; /* EW_ flags */
4684{
4685 char_u *pat;
4686 int i;
4687 char_u *path;
4688 int mustfree = FALSE;
4689 garray_T ga;
4690 char_u *buf = alloc(MAXPATHL);
4691 size_t l;
4692 char_u *s, *e;
4693 int flags = flagsarg;
4694 int ret;
4695
4696 if (buf == NULL)
4697 return FAIL;
4698
4699 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4700 * space */
4701 pat = vim_strsave(filepat);
4702 for (i = 0; pat[i]; ++i)
4703 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004704 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004705
4706 flags |= EW_FILE | EW_EXEC;
4707
4708 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004709 if (mch_isFullName(pat))
4710 path = (char_u *)" ";
4711 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004712 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4713 path = (char_u *)".";
4714 else
4715 path = vim_getenv((char_u *)"PATH", &mustfree);
4716
4717 /*
4718 * Go over all directories in $PATH. Expand matches in that directory and
4719 * collect them in "ga".
4720 */
4721 ga_init2(&ga, (int)sizeof(char *), 10);
4722 for (s = path; *s != NUL; s = e)
4723 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004724 if (*s == ' ')
4725 ++s; /* Skip space used for absolute path name. */
4726
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004727#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4728 e = vim_strchr(s, ';');
4729#else
4730 e = vim_strchr(s, ':');
4731#endif
4732 if (e == NULL)
4733 e = s + STRLEN(s);
4734
4735 l = e - s;
4736 if (l > MAXPATHL - 5)
4737 break;
4738 vim_strncpy(buf, s, l);
4739 add_pathsep(buf);
4740 l = STRLEN(buf);
4741 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4742
4743 /* Expand matches in one directory of $PATH. */
4744 ret = expand_wildcards(1, &buf, num_file, file, flags);
4745 if (ret == OK)
4746 {
4747 if (ga_grow(&ga, *num_file) == FAIL)
4748 FreeWild(*num_file, *file);
4749 else
4750 {
4751 for (i = 0; i < *num_file; ++i)
4752 {
4753 s = (*file)[i];
4754 if (STRLEN(s) > l)
4755 {
4756 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004757 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004758 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4759 }
4760 else
4761 vim_free(s);
4762 }
4763 vim_free(*file);
4764 }
4765 }
4766 if (*e != NUL)
4767 ++e;
4768 }
4769 *file = ga.ga_data;
4770 *num_file = ga.ga_len;
4771
4772 vim_free(buf);
4773 vim_free(pat);
4774 if (mustfree)
4775 vim_free(path);
4776 return OK;
4777}
4778
4779
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004781static 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));
4782
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004784 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004785 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004787 static void *
4788call_user_expand_func(user_expand_func, xp, num_file, file)
4789 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 int *num_file;
4792 char_u ***file;
4793{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004794 char_u keep;
4795 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004798 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004799 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
4801 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004802 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 *num_file = 0;
4804 *file = NULL;
4805
Bram Moolenaar21669c02008-01-18 12:16:16 +00004806 if (ccline.cmdbuff == NULL)
4807 {
4808 /* Completion from Insert mode, pass fake arguments. */
4809 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004810 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004811 args[1] = xp->xp_pattern;
4812 }
4813 else
4814 {
4815 /* Completion on the command line, pass real arguments. */
4816 keep = ccline.cmdbuff[ccline.cmdlen];
4817 ccline.cmdbuff[ccline.cmdlen] = 0;
4818 sprintf((char *)num, "%d", ccline.cmdpos);
4819 args[1] = ccline.cmdbuff;
4820 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004821 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 args[2] = num;
4823
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004824 /* Save the cmdline, we don't know what the function may do. */
4825 save_ccline = ccline;
4826 ccline.cmdbuff = NULL;
4827 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004829
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004830 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004831
4832 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004834 if (ccline.cmdbuff != NULL)
4835 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004836
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004837 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004838 return ret;
4839}
4840
4841/*
4842 * Expand names with a function defined by the user.
4843 */
4844 static int
4845ExpandUserDefined(xp, regmatch, num_file, file)
4846 expand_T *xp;
4847 regmatch_T *regmatch;
4848 int *num_file;
4849 char_u ***file;
4850{
4851 char_u *retstr;
4852 char_u *s;
4853 char_u *e;
4854 char_u keep;
4855 garray_T ga;
4856
4857 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4858 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 return FAIL;
4860
4861 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004862 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
4864 e = vim_strchr(s, '\n');
4865 if (e == NULL)
4866 e = s + STRLEN(s);
4867 keep = *e;
4868 *e = 0;
4869
4870 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4871 {
4872 *e = keep;
4873 if (*e != NUL)
4874 ++e;
4875 continue;
4876 }
4877
4878 if (ga_grow(&ga, 1) == FAIL)
4879 break;
4880
4881 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4882 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 *e = keep;
4885 if (*e != NUL)
4886 ++e;
4887 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004888 vim_free(retstr);
4889 *file = ga.ga_data;
4890 *num_file = ga.ga_len;
4891 return OK;
4892}
4893
4894/*
4895 * Expand names with a list returned by a function defined by the user.
4896 */
4897 static int
4898ExpandUserList(xp, num_file, file)
4899 expand_T *xp;
4900 int *num_file;
4901 char_u ***file;
4902{
4903 list_T *retlist;
4904 listitem_T *li;
4905 garray_T ga;
4906
4907 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4908 if (retlist == NULL)
4909 return FAIL;
4910
4911 ga_init2(&ga, (int)sizeof(char *), 3);
4912 /* Loop over the items in the list. */
4913 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4914 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00004915 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
4916 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004917
4918 if (ga_grow(&ga, 1) == FAIL)
4919 break;
4920
4921 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00004922 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004923 ++ga.ga_len;
4924 }
4925 list_unref(retlist);
4926
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 *file = ga.ga_data;
4928 *num_file = ga.ga_len;
4929 return OK;
4930}
4931#endif
4932
4933/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004934 * Expand color scheme, compiler or filetype names:
4935 * 'runtimepath'/{dirname}/{pat}.vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 */
4937 static int
4938ExpandRTDir(pat, num_file, file, dirname)
4939 char_u *pat;
4940 int *num_file;
4941 char_u ***file;
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004942 char *dirname; /* "colors", "compiler" or "syntax" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943{
4944 char_u *all;
4945 char_u *s;
4946 char_u *e;
4947 garray_T ga;
4948
4949 *num_file = 0;
4950 *file = NULL;
4951 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4952 if (s == NULL)
4953 return FAIL;
4954 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00004955 all = globpath(p_rtp, s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 vim_free(s);
4957 if (all == NULL)
4958 return FAIL;
4959
4960 ga_init2(&ga, (int)sizeof(char *), 3);
4961 for (s = all; *s != NUL; s = e)
4962 {
4963 e = vim_strchr(s, '\n');
4964 if (e == NULL)
4965 e = s + STRLEN(s);
4966 if (ga_grow(&ga, 1) == FAIL)
4967 break;
4968 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4969 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004970 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 if (*s == '\n' || vim_ispathsep(*s))
4972 break;
4973 ++s;
4974 ((char_u **)ga.ga_data)[ga.ga_len] =
4975 vim_strnsave(s, (int)(e - s - 4));
4976 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
4978 if (*e != NUL)
4979 ++e;
4980 }
4981 vim_free(all);
4982 *file = ga.ga_data;
4983 *num_file = ga.ga_len;
4984 return OK;
4985}
4986
4987#endif
4988
4989#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4990/*
4991 * Expand "file" for all comma-separated directories in "path".
4992 * Returns an allocated string with all matches concatenated, separated by
4993 * newlines. Returns NULL for an error or no matches.
4994 */
4995 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00004996globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 char_u *path;
4998 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00004999 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000{
5001 expand_T xpc;
5002 char_u *buf;
5003 garray_T ga;
5004 int i;
5005 int len;
5006 int num_p;
5007 char_u **p;
5008 char_u *cur = NULL;
5009
5010 buf = alloc(MAXPATHL);
5011 if (buf == NULL)
5012 return NULL;
5013
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005014 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005016
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 ga_init2(&ga, 1, 100);
5018
5019 /* Loop over all entries in {path}. */
5020 while (*path != NUL)
5021 {
5022 /* Copy one item of the path to buf[] and concatenate the file name. */
5023 copy_option_part(&path, buf, MAXPATHL, ",");
5024 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5025 {
5026 add_pathsep(buf);
5027 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005028 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5029 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005031 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005033 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
5035 /* Concatenate new results to previous ones. */
5036 if (ga_grow(&ga, len) == OK)
5037 {
5038 cur = (char_u *)ga.ga_data + ga.ga_len;
5039 for (i = 0; i < num_p; ++i)
5040 {
5041 STRCPY(cur, p[i]);
5042 cur += STRLEN(p[i]);
5043 *cur++ = '\n';
5044 }
5045 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
5047 FreeWild(num_p, p);
5048 }
5049 }
5050 }
5051 if (cur != NULL)
5052 *--cur = 0; /* Replace trailing newline with NUL */
5053
5054 vim_free(buf);
5055 return (char_u *)ga.ga_data;
5056}
5057
5058#endif
5059
5060#if defined(FEAT_CMDHIST) || defined(PROTO)
5061
5062/*********************************
5063 * Command line history stuff *
5064 *********************************/
5065
5066/*
5067 * Translate a history character to the associated type number.
5068 */
5069 static int
5070hist_char2type(c)
5071 int c;
5072{
5073 if (c == ':')
5074 return HIST_CMD;
5075 if (c == '=')
5076 return HIST_EXPR;
5077 if (c == '@')
5078 return HIST_INPUT;
5079 if (c == '>')
5080 return HIST_DEBUG;
5081 return HIST_SEARCH; /* must be '?' or '/' */
5082}
5083
5084/*
5085 * Table of history names.
5086 * These names are used in :history and various hist...() functions.
5087 * It is sufficient to give the significant prefix of a history name.
5088 */
5089
5090static char *(history_names[]) =
5091{
5092 "cmd",
5093 "search",
5094 "expr",
5095 "input",
5096 "debug",
5097 NULL
5098};
5099
5100/*
5101 * init_history() - Initialize the command line history.
5102 * Also used to re-allocate the history when the size changes.
5103 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005104 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105init_history()
5106{
5107 int newlen; /* new length of history table */
5108 histentry_T *temp;
5109 int i;
5110 int j;
5111 int type;
5112
5113 /*
5114 * If size of history table changed, reallocate it
5115 */
5116 newlen = (int)p_hi;
5117 if (newlen != hislen) /* history length changed */
5118 {
5119 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5120 {
5121 if (newlen)
5122 {
5123 temp = (histentry_T *)lalloc(
5124 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5125 if (temp == NULL) /* out of memory! */
5126 {
5127 if (type == 0) /* first one: just keep the old length */
5128 {
5129 newlen = hislen;
5130 break;
5131 }
5132 /* Already changed one table, now we can only have zero
5133 * length for all tables. */
5134 newlen = 0;
5135 type = -1;
5136 continue;
5137 }
5138 }
5139 else
5140 temp = NULL;
5141 if (newlen == 0 || temp != NULL)
5142 {
5143 if (hisidx[type] < 0) /* there are no entries yet */
5144 {
5145 for (i = 0; i < newlen; ++i)
5146 {
5147 temp[i].hisnum = 0;
5148 temp[i].hisstr = NULL;
5149 }
5150 }
5151 else if (newlen > hislen) /* array becomes bigger */
5152 {
5153 for (i = 0; i <= hisidx[type]; ++i)
5154 temp[i] = history[type][i];
5155 j = i;
5156 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5157 {
5158 temp[i].hisnum = 0;
5159 temp[i].hisstr = NULL;
5160 }
5161 for ( ; j < hislen; ++i, ++j)
5162 temp[i] = history[type][j];
5163 }
5164 else /* array becomes smaller or 0 */
5165 {
5166 j = hisidx[type];
5167 for (i = newlen - 1; ; --i)
5168 {
5169 if (i >= 0) /* copy newest entries */
5170 temp[i] = history[type][j];
5171 else /* remove older entries */
5172 vim_free(history[type][j].hisstr);
5173 if (--j < 0)
5174 j = hislen - 1;
5175 if (j == hisidx[type])
5176 break;
5177 }
5178 hisidx[type] = newlen - 1;
5179 }
5180 vim_free(history[type]);
5181 history[type] = temp;
5182 }
5183 }
5184 hislen = newlen;
5185 }
5186}
5187
5188/*
5189 * Check if command line 'str' is already in history.
5190 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5191 */
5192 static int
5193in_history(type, str, move_to_front)
5194 int type;
5195 char_u *str;
5196 int move_to_front; /* Move the entry to the front if it exists */
5197{
5198 int i;
5199 int last_i = -1;
5200
5201 if (hisidx[type] < 0)
5202 return FALSE;
5203 i = hisidx[type];
5204 do
5205 {
5206 if (history[type][i].hisstr == NULL)
5207 return FALSE;
5208 if (STRCMP(str, history[type][i].hisstr) == 0)
5209 {
5210 if (!move_to_front)
5211 return TRUE;
5212 last_i = i;
5213 break;
5214 }
5215 if (--i < 0)
5216 i = hislen - 1;
5217 } while (i != hisidx[type]);
5218
5219 if (last_i >= 0)
5220 {
5221 str = history[type][i].hisstr;
5222 while (i != hisidx[type])
5223 {
5224 if (++i >= hislen)
5225 i = 0;
5226 history[type][last_i] = history[type][i];
5227 last_i = i;
5228 }
5229 history[type][i].hisstr = str;
5230 history[type][i].hisnum = ++hisnum[type];
5231 return TRUE;
5232 }
5233 return FALSE;
5234}
5235
5236/*
5237 * Convert history name (from table above) to its HIST_ equivalent.
5238 * When "name" is empty, return "cmd" history.
5239 * Returns -1 for unknown history name.
5240 */
5241 int
5242get_histtype(name)
5243 char_u *name;
5244{
5245 int i;
5246 int len = (int)STRLEN(name);
5247
5248 /* No argument: use current history. */
5249 if (len == 0)
5250 return hist_char2type(ccline.cmdfirstc);
5251
5252 for (i = 0; history_names[i] != NULL; ++i)
5253 if (STRNICMP(name, history_names[i], len) == 0)
5254 return i;
5255
5256 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5257 return hist_char2type(name[0]);
5258
5259 return -1;
5260}
5261
5262static int last_maptick = -1; /* last seen maptick */
5263
5264/*
5265 * Add the given string to the given history. If the string is already in the
5266 * history then it is moved to the front. "histype" may be one of he HIST_
5267 * values.
5268 */
5269 void
5270add_to_history(histype, new_entry, in_map, sep)
5271 int histype;
5272 char_u *new_entry;
5273 int in_map; /* consider maptick when inside a mapping */
5274 int sep; /* separator character used (search hist) */
5275{
5276 histentry_T *hisptr;
5277 int len;
5278
5279 if (hislen == 0) /* no history */
5280 return;
5281
5282 /*
5283 * Searches inside the same mapping overwrite each other, so that only
5284 * the last line is kept. Be careful not to remove a line that was moved
5285 * down, only lines that were added.
5286 */
5287 if (histype == HIST_SEARCH && in_map)
5288 {
5289 if (maptick == last_maptick)
5290 {
5291 /* Current line is from the same mapping, remove it */
5292 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5293 vim_free(hisptr->hisstr);
5294 hisptr->hisstr = NULL;
5295 hisptr->hisnum = 0;
5296 --hisnum[histype];
5297 if (--hisidx[HIST_SEARCH] < 0)
5298 hisidx[HIST_SEARCH] = hislen - 1;
5299 }
5300 last_maptick = -1;
5301 }
5302 if (!in_history(histype, new_entry, TRUE))
5303 {
5304 if (++hisidx[histype] == hislen)
5305 hisidx[histype] = 0;
5306 hisptr = &history[histype][hisidx[histype]];
5307 vim_free(hisptr->hisstr);
5308
5309 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005310 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5312 if (hisptr->hisstr != NULL)
5313 hisptr->hisstr[len + 1] = sep;
5314
5315 hisptr->hisnum = ++hisnum[histype];
5316 if (histype == HIST_SEARCH && in_map)
5317 last_maptick = maptick;
5318 }
5319}
5320
5321#if defined(FEAT_EVAL) || defined(PROTO)
5322
5323/*
5324 * Get identifier of newest history entry.
5325 * "histype" may be one of the HIST_ values.
5326 */
5327 int
5328get_history_idx(histype)
5329 int histype;
5330{
5331 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5332 || hisidx[histype] < 0)
5333 return -1;
5334
5335 return history[histype][hisidx[histype]].hisnum;
5336}
5337
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005338static struct cmdline_info *get_ccline_ptr __ARGS((void));
5339
5340/*
5341 * Get pointer to the command line info to use. cmdline_paste() may clear
5342 * ccline and put the previous value in prev_ccline.
5343 */
5344 static struct cmdline_info *
5345get_ccline_ptr()
5346{
5347 if ((State & CMDLINE) == 0)
5348 return NULL;
5349 if (ccline.cmdbuff != NULL)
5350 return &ccline;
5351 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5352 return &prev_ccline;
5353 return NULL;
5354}
5355
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356/*
5357 * Get the current command line in allocated memory.
5358 * Only works when the command line is being edited.
5359 * Returns NULL when something is wrong.
5360 */
5361 char_u *
5362get_cmdline_str()
5363{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005364 struct cmdline_info *p = get_ccline_ptr();
5365
5366 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005368 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369}
5370
5371/*
5372 * Get the current command line position, counted in bytes.
5373 * Zero is the first position.
5374 * Only works when the command line is being edited.
5375 * Returns -1 when something is wrong.
5376 */
5377 int
5378get_cmdline_pos()
5379{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005380 struct cmdline_info *p = get_ccline_ptr();
5381
5382 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005384 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385}
5386
5387/*
5388 * Set the command line byte position to "pos". Zero is the first position.
5389 * Only works when the command line is being edited.
5390 * Returns 1 when failed, 0 when OK.
5391 */
5392 int
5393set_cmdline_pos(pos)
5394 int pos;
5395{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005396 struct cmdline_info *p = get_ccline_ptr();
5397
5398 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 return 1;
5400
5401 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5402 * changed the command line. */
5403 if (pos < 0)
5404 new_cmdpos = 0;
5405 else
5406 new_cmdpos = pos;
5407 return 0;
5408}
5409
5410/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005411 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005412 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005413 * Only works when the command line is being edited.
5414 * Returns NUL when something is wrong.
5415 */
5416 int
5417get_cmdline_type()
5418{
5419 struct cmdline_info *p = get_ccline_ptr();
5420
5421 if (p == NULL)
5422 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005423 if (p->cmdfirstc == NUL)
5424 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005425 return p->cmdfirstc;
5426}
5427
5428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 * Calculate history index from a number:
5430 * num > 0: seen as identifying number of a history entry
5431 * num < 0: relative position in history wrt newest entry
5432 * "histype" may be one of the HIST_ values.
5433 */
5434 static int
5435calc_hist_idx(histype, num)
5436 int histype;
5437 int num;
5438{
5439 int i;
5440 histentry_T *hist;
5441 int wrapped = FALSE;
5442
5443 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5444 || (i = hisidx[histype]) < 0 || num == 0)
5445 return -1;
5446
5447 hist = history[histype];
5448 if (num > 0)
5449 {
5450 while (hist[i].hisnum > num)
5451 if (--i < 0)
5452 {
5453 if (wrapped)
5454 break;
5455 i += hislen;
5456 wrapped = TRUE;
5457 }
5458 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5459 return i;
5460 }
5461 else if (-num <= hislen)
5462 {
5463 i += num + 1;
5464 if (i < 0)
5465 i += hislen;
5466 if (hist[i].hisstr != NULL)
5467 return i;
5468 }
5469 return -1;
5470}
5471
5472/*
5473 * Get a history entry by its index.
5474 * "histype" may be one of the HIST_ values.
5475 */
5476 char_u *
5477get_history_entry(histype, idx)
5478 int histype;
5479 int idx;
5480{
5481 idx = calc_hist_idx(histype, idx);
5482 if (idx >= 0)
5483 return history[histype][idx].hisstr;
5484 else
5485 return (char_u *)"";
5486}
5487
5488/*
5489 * Clear all entries of a history.
5490 * "histype" may be one of the HIST_ values.
5491 */
5492 int
5493clr_history(histype)
5494 int histype;
5495{
5496 int i;
5497 histentry_T *hisptr;
5498
5499 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5500 {
5501 hisptr = history[histype];
5502 for (i = hislen; i--;)
5503 {
5504 vim_free(hisptr->hisstr);
5505 hisptr->hisnum = 0;
5506 hisptr++->hisstr = NULL;
5507 }
5508 hisidx[histype] = -1; /* mark history as cleared */
5509 hisnum[histype] = 0; /* reset identifier counter */
5510 return OK;
5511 }
5512 return FAIL;
5513}
5514
5515/*
5516 * Remove all entries matching {str} from a history.
5517 * "histype" may be one of the HIST_ values.
5518 */
5519 int
5520del_history_entry(histype, str)
5521 int histype;
5522 char_u *str;
5523{
5524 regmatch_T regmatch;
5525 histentry_T *hisptr;
5526 int idx;
5527 int i;
5528 int last;
5529 int found = FALSE;
5530
5531 regmatch.regprog = NULL;
5532 regmatch.rm_ic = FALSE; /* always match case */
5533 if (hislen != 0
5534 && histype >= 0
5535 && histype < HIST_COUNT
5536 && *str != NUL
5537 && (idx = hisidx[histype]) >= 0
5538 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5539 != NULL)
5540 {
5541 i = last = idx;
5542 do
5543 {
5544 hisptr = &history[histype][i];
5545 if (hisptr->hisstr == NULL)
5546 break;
5547 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5548 {
5549 found = TRUE;
5550 vim_free(hisptr->hisstr);
5551 hisptr->hisstr = NULL;
5552 hisptr->hisnum = 0;
5553 }
5554 else
5555 {
5556 if (i != last)
5557 {
5558 history[histype][last] = *hisptr;
5559 hisptr->hisstr = NULL;
5560 hisptr->hisnum = 0;
5561 }
5562 if (--last < 0)
5563 last += hislen;
5564 }
5565 if (--i < 0)
5566 i += hislen;
5567 } while (i != idx);
5568 if (history[histype][idx].hisstr == NULL)
5569 hisidx[histype] = -1;
5570 }
5571 vim_free(regmatch.regprog);
5572 return found;
5573}
5574
5575/*
5576 * Remove an indexed entry from a history.
5577 * "histype" may be one of the HIST_ values.
5578 */
5579 int
5580del_history_idx(histype, idx)
5581 int histype;
5582 int idx;
5583{
5584 int i, j;
5585
5586 i = calc_hist_idx(histype, idx);
5587 if (i < 0)
5588 return FALSE;
5589 idx = hisidx[histype];
5590 vim_free(history[histype][i].hisstr);
5591
5592 /* When deleting the last added search string in a mapping, reset
5593 * last_maptick, so that the last added search string isn't deleted again.
5594 */
5595 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5596 last_maptick = -1;
5597
5598 while (i != idx)
5599 {
5600 j = (i + 1) % hislen;
5601 history[histype][i] = history[histype][j];
5602 i = j;
5603 }
5604 history[histype][i].hisstr = NULL;
5605 history[histype][i].hisnum = 0;
5606 if (--i < 0)
5607 i += hislen;
5608 hisidx[histype] = i;
5609 return TRUE;
5610}
5611
5612#endif /* FEAT_EVAL */
5613
5614#if defined(FEAT_CRYPT) || defined(PROTO)
5615/*
5616 * Very specific function to remove the value in ":set key=val" from the
5617 * history.
5618 */
5619 void
5620remove_key_from_history()
5621{
5622 char_u *p;
5623 int i;
5624
5625 i = hisidx[HIST_CMD];
5626 if (i < 0)
5627 return;
5628 p = history[HIST_CMD][i].hisstr;
5629 if (p != NULL)
5630 for ( ; *p; ++p)
5631 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5632 {
5633 p = vim_strchr(p + 3, '=');
5634 if (p == NULL)
5635 break;
5636 ++p;
5637 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5638 if (p[i] == '\\' && p[i + 1])
5639 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005640 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 --p;
5642 }
5643}
5644#endif
5645
5646#endif /* FEAT_CMDHIST */
5647
5648#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5649/*
5650 * Get indices "num1,num2" that specify a range within a list (not a range of
5651 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5652 * Returns OK if parsed successfully, otherwise FAIL.
5653 */
5654 int
5655get_list_range(str, num1, num2)
5656 char_u **str;
5657 int *num1;
5658 int *num2;
5659{
5660 int len;
5661 int first = FALSE;
5662 long num;
5663
5664 *str = skipwhite(*str);
5665 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5666 {
5667 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5668 *str += len;
5669 *num1 = (int)num;
5670 first = TRUE;
5671 }
5672 *str = skipwhite(*str);
5673 if (**str == ',') /* parse "to" part of range */
5674 {
5675 *str = skipwhite(*str + 1);
5676 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5677 if (len > 0)
5678 {
5679 *num2 = (int)num;
5680 *str = skipwhite(*str + len);
5681 }
5682 else if (!first) /* no number given at all */
5683 return FAIL;
5684 }
5685 else if (first) /* only one number given */
5686 *num2 = *num1;
5687 return OK;
5688}
5689#endif
5690
5691#if defined(FEAT_CMDHIST) || defined(PROTO)
5692/*
5693 * :history command - print a history
5694 */
5695 void
5696ex_history(eap)
5697 exarg_T *eap;
5698{
5699 histentry_T *hist;
5700 int histype1 = HIST_CMD;
5701 int histype2 = HIST_CMD;
5702 int hisidx1 = 1;
5703 int hisidx2 = -1;
5704 int idx;
5705 int i, j, k;
5706 char_u *end;
5707 char_u *arg = eap->arg;
5708
5709 if (hislen == 0)
5710 {
5711 MSG(_("'history' option is zero"));
5712 return;
5713 }
5714
5715 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5716 {
5717 end = arg;
5718 while (ASCII_ISALPHA(*end)
5719 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5720 end++;
5721 i = *end;
5722 *end = NUL;
5723 histype1 = get_histtype(arg);
5724 if (histype1 == -1)
5725 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005726 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
5728 histype1 = 0;
5729 histype2 = HIST_COUNT-1;
5730 }
5731 else
5732 {
5733 *end = i;
5734 EMSG(_(e_trailing));
5735 return;
5736 }
5737 }
5738 else
5739 histype2 = histype1;
5740 *end = i;
5741 }
5742 else
5743 end = arg;
5744 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5745 {
5746 EMSG(_(e_trailing));
5747 return;
5748 }
5749
5750 for (; !got_int && histype1 <= histype2; ++histype1)
5751 {
5752 STRCPY(IObuff, "\n # ");
5753 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5754 MSG_PUTS_TITLE(IObuff);
5755 idx = hisidx[histype1];
5756 hist = history[histype1];
5757 j = hisidx1;
5758 k = hisidx2;
5759 if (j < 0)
5760 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5761 if (k < 0)
5762 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5763 if (idx >= 0 && j <= k)
5764 for (i = idx + 1; !got_int; ++i)
5765 {
5766 if (i == hislen)
5767 i = 0;
5768 if (hist[i].hisstr != NULL
5769 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5770 {
5771 msg_putchar('\n');
5772 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5773 hist[i].hisnum);
5774 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5775 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5776 (int)Columns - 10);
5777 else
5778 STRCAT(IObuff, hist[i].hisstr);
5779 msg_outtrans(IObuff);
5780 out_flush();
5781 }
5782 if (i == idx)
5783 break;
5784 }
5785 }
5786}
5787#endif
5788
5789#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5790static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5791static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5792static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5793static int viminfo_add_at_front = FALSE;
5794
5795static int hist_type2char __ARGS((int type, int use_question));
5796
5797/*
5798 * Translate a history type number to the associated character.
5799 */
5800 static int
5801hist_type2char(type, use_question)
5802 int type;
5803 int use_question; /* use '?' instead of '/' */
5804{
5805 if (type == HIST_CMD)
5806 return ':';
5807 if (type == HIST_SEARCH)
5808 {
5809 if (use_question)
5810 return '?';
5811 else
5812 return '/';
5813 }
5814 if (type == HIST_EXPR)
5815 return '=';
5816 return '@';
5817}
5818
5819/*
5820 * Prepare for reading the history from the viminfo file.
5821 * This allocates history arrays to store the read history lines.
5822 */
5823 void
5824prepare_viminfo_history(asklen)
5825 int asklen;
5826{
5827 int i;
5828 int num;
5829 int type;
5830 int len;
5831
5832 init_history();
5833 viminfo_add_at_front = (asklen != 0);
5834 if (asklen > hislen)
5835 asklen = hislen;
5836
5837 for (type = 0; type < HIST_COUNT; ++type)
5838 {
5839 /*
5840 * Count the number of empty spaces in the history list. If there are
5841 * more spaces available than we request, then fill them up.
5842 */
5843 for (i = 0, num = 0; i < hislen; i++)
5844 if (history[type][i].hisstr == NULL)
5845 num++;
5846 len = asklen;
5847 if (num > len)
5848 len = num;
5849 if (len <= 0)
5850 viminfo_history[type] = NULL;
5851 else
5852 viminfo_history[type] =
5853 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5854 if (viminfo_history[type] == NULL)
5855 len = 0;
5856 viminfo_hislen[type] = len;
5857 viminfo_hisidx[type] = 0;
5858 }
5859}
5860
5861/*
5862 * Accept a line from the viminfo, store it in the history array when it's
5863 * new.
5864 */
5865 int
5866read_viminfo_history(virp)
5867 vir_T *virp;
5868{
5869 int type;
5870 long_u len;
5871 char_u *val;
5872 char_u *p;
5873
5874 type = hist_char2type(virp->vir_line[0]);
5875 if (viminfo_hisidx[type] < viminfo_hislen[type])
5876 {
5877 val = viminfo_readstring(virp, 1, TRUE);
5878 if (val != NULL && *val != NUL)
5879 {
5880 if (!in_history(type, val + (type == HIST_SEARCH),
5881 viminfo_add_at_front))
5882 {
5883 /* Need to re-allocate to append the separator byte. */
5884 len = STRLEN(val);
5885 p = lalloc(len + 2, TRUE);
5886 if (p != NULL)
5887 {
5888 if (type == HIST_SEARCH)
5889 {
5890 /* Search entry: Move the separator from the first
5891 * column to after the NUL. */
5892 mch_memmove(p, val + 1, (size_t)len);
5893 p[len] = (*val == ' ' ? NUL : *val);
5894 }
5895 else
5896 {
5897 /* Not a search entry: No separator in the viminfo
5898 * file, add a NUL separator. */
5899 mch_memmove(p, val, (size_t)len + 1);
5900 p[len + 1] = NUL;
5901 }
5902 viminfo_history[type][viminfo_hisidx[type]++] = p;
5903 }
5904 }
5905 }
5906 vim_free(val);
5907 }
5908 return viminfo_readline(virp);
5909}
5910
5911 void
5912finish_viminfo_history()
5913{
5914 int idx;
5915 int i;
5916 int type;
5917
5918 for (type = 0; type < HIST_COUNT; ++type)
5919 {
5920 if (history[type] == NULL)
5921 return;
5922 idx = hisidx[type] + viminfo_hisidx[type];
5923 if (idx >= hislen)
5924 idx -= hislen;
5925 else if (idx < 0)
5926 idx = hislen - 1;
5927 if (viminfo_add_at_front)
5928 hisidx[type] = idx;
5929 else
5930 {
5931 if (hisidx[type] == -1)
5932 hisidx[type] = hislen - 1;
5933 do
5934 {
5935 if (history[type][idx].hisstr != NULL)
5936 break;
5937 if (++idx == hislen)
5938 idx = 0;
5939 } while (idx != hisidx[type]);
5940 if (idx != hisidx[type] && --idx < 0)
5941 idx = hislen - 1;
5942 }
5943 for (i = 0; i < viminfo_hisidx[type]; i++)
5944 {
5945 vim_free(history[type][idx].hisstr);
5946 history[type][idx].hisstr = viminfo_history[type][i];
5947 if (--idx < 0)
5948 idx = hislen - 1;
5949 }
5950 idx += 1;
5951 idx %= hislen;
5952 for (i = 0; i < viminfo_hisidx[type]; i++)
5953 {
5954 history[type][idx++].hisnum = ++hisnum[type];
5955 idx %= hislen;
5956 }
5957 vim_free(viminfo_history[type]);
5958 viminfo_history[type] = NULL;
5959 }
5960}
5961
5962 void
5963write_viminfo_history(fp)
5964 FILE *fp;
5965{
5966 int i;
5967 int type;
5968 int num_saved;
5969 char_u *p;
5970 int c;
5971
5972 init_history();
5973 if (hislen == 0)
5974 return;
5975 for (type = 0; type < HIST_COUNT; ++type)
5976 {
5977 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5978 if (num_saved == 0)
5979 continue;
5980 if (num_saved < 0) /* Use default */
5981 num_saved = hislen;
5982 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5983 type == HIST_CMD ? _("Command Line") :
5984 type == HIST_SEARCH ? _("Search String") :
5985 type == HIST_EXPR ? _("Expression") :
5986 _("Input Line"));
5987 if (num_saved > hislen)
5988 num_saved = hislen;
5989 i = hisidx[type];
5990 if (i >= 0)
5991 while (num_saved--)
5992 {
5993 p = history[type][i].hisstr;
5994 if (p != NULL)
5995 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005996 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 /* For the search history: put the separator in the second
5998 * column; use a space if there isn't one. */
5999 if (type == HIST_SEARCH)
6000 {
6001 c = p[STRLEN(p) + 1];
6002 putc(c == NUL ? ' ' : c, fp);
6003 }
6004 viminfo_writestring(fp, p);
6005 }
6006 if (--i < 0)
6007 i = hislen - 1;
6008 }
6009 }
6010}
6011#endif /* FEAT_VIMINFO */
6012
6013#if defined(FEAT_FKMAP) || defined(PROTO)
6014/*
6015 * Write a character at the current cursor+offset position.
6016 * It is directly written into the command buffer block.
6017 */
6018 void
6019cmd_pchar(c, offset)
6020 int c, offset;
6021{
6022 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6023 {
6024 EMSG(_("E198: cmd_pchar beyond the command length"));
6025 return;
6026 }
6027 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6028 ccline.cmdbuff[ccline.cmdlen] = NUL;
6029}
6030
6031 int
6032cmd_gchar(offset)
6033 int offset;
6034{
6035 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6036 {
6037 /* EMSG(_("cmd_gchar beyond the command length")); */
6038 return NUL;
6039 }
6040 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6041}
6042#endif
6043
6044#if defined(FEAT_CMDWIN) || defined(PROTO)
6045/*
6046 * Open a window on the current command line and history. Allow editing in
6047 * the window. Returns when the window is closed.
6048 * Returns:
6049 * CR if the command is to be executed
6050 * Ctrl_C if it is to be abandoned
6051 * K_IGNORE if editing continues
6052 */
6053 static int
6054ex_window()
6055{
6056 struct cmdline_info save_ccline;
6057 buf_T *old_curbuf = curbuf;
6058 win_T *old_curwin = curwin;
6059 buf_T *bp;
6060 win_T *wp;
6061 int i;
6062 linenr_T lnum;
6063 int histtype;
6064 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006065#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 int save_restart_edit = restart_edit;
6069 int save_State = State;
6070 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006071#ifdef FEAT_RIGHTLEFT
6072 int save_cmdmsg_rl = cmdmsg_rl;
6073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074
6075 /* Can't do this recursively. Can't do it when typing a password. */
6076 if (cmdwin_type != 0
6077# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6078 || cmdline_star > 0
6079# endif
6080 )
6081 {
6082 beep_flush();
6083 return K_IGNORE;
6084 }
6085
6086 /* Save current window sizes. */
6087 win_size_save(&winsizes);
6088
6089# ifdef FEAT_AUTOCMD
6090 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006091 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006093 /* don't use a new tab page */
6094 cmdmod.tab = 0;
6095
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 /* Create a window for the command-line buffer. */
6097 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6098 {
6099 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006100# ifdef FEAT_AUTOCMD
6101 unblock_autocmds();
6102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 return K_IGNORE;
6104 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006105 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
6107 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006108 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006109 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6111 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6112 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006113#ifdef FEAT_FOLDING
6114 curwin->w_p_fen = FALSE;
6115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006117 curwin->w_p_rl = cmdmsg_rl;
6118 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119# endif
6120# ifdef FEAT_SCROLLBIND
6121 curwin->w_p_scb = FALSE;
6122# endif
6123
6124# ifdef FEAT_AUTOCMD
6125 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006126 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127# endif
6128
Bram Moolenaar46152342005-09-07 21:18:43 +00006129 /* Showing the prompt may have set need_wait_return, reset it. */
6130 need_wait_return = FALSE;
6131
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006132 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6134 {
6135 if (p_wc == TAB)
6136 {
6137 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6138 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6139 }
6140 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6141 }
6142
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006143 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6144 * sets 'textwidth' to 78). */
6145 curbuf->b_p_tw = 0;
6146
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 /* Fill the buffer with the history. */
6148 init_history();
6149 if (hislen > 0)
6150 {
6151 i = hisidx[histtype];
6152 if (i >= 0)
6153 {
6154 lnum = 0;
6155 do
6156 {
6157 if (++i == hislen)
6158 i = 0;
6159 if (history[histtype][i].hisstr != NULL)
6160 ml_append(lnum++, history[histtype][i].hisstr,
6161 (colnr_T)0, FALSE);
6162 }
6163 while (i != hisidx[histtype]);
6164 }
6165 }
6166
6167 /* Replace the empty last line with the current command-line and put the
6168 * cursor there. */
6169 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6170 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6171 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006172 changed_line_abv_curs();
6173 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006174 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175
6176 /* Save the command line info, can be used recursively. */
6177 save_ccline = ccline;
6178 ccline.cmdbuff = NULL;
6179 ccline.cmdprompt = NULL;
6180
6181 /* No Ex mode here! */
6182 exmode_active = 0;
6183
6184 State = NORMAL;
6185# ifdef FEAT_MOUSE
6186 setmouse();
6187# endif
6188
6189# ifdef FEAT_AUTOCMD
6190 /* Trigger CmdwinEnter autocommands. */
6191 typestr[0] = cmdwin_type;
6192 typestr[1] = NUL;
6193 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006194 if (restart_edit != 0) /* autocmd with ":startinsert" */
6195 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196# endif
6197
6198 i = RedrawingDisabled;
6199 RedrawingDisabled = 0;
6200
6201 /*
6202 * Call the main loop until <CR> or CTRL-C is typed.
6203 */
6204 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006205 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
6207 RedrawingDisabled = i;
6208
6209# ifdef FEAT_AUTOCMD
6210 /* Trigger CmdwinLeave autocommands. */
6211 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6212# endif
6213
Bram Moolenaarccc18222007-05-10 18:25:20 +00006214 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 ccline = save_ccline;
6216 cmdwin_type = 0;
6217
6218 exmode_active = save_exmode;
6219
Bram Moolenaarf9821062008-06-20 16:31:07 +00006220 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 * this happens! */
6222 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6223 {
6224 cmdwin_result = Ctrl_C;
6225 EMSG(_("E199: Active window or buffer deleted"));
6226 }
6227 else
6228 {
6229# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6230 /* autocmds may abort script processing */
6231 if (aborting() && cmdwin_result != K_IGNORE)
6232 cmdwin_result = Ctrl_C;
6233# endif
6234 /* Set the new command line from the cmdline buffer. */
6235 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006236 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006238 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6239
6240 if (histtype == HIST_CMD)
6241 {
6242 /* Execute the command directly. */
6243 ccline.cmdbuff = vim_strsave((char_u *)p);
6244 cmdwin_result = CAR;
6245 }
6246 else
6247 {
6248 /* First need to cancel what we were doing. */
6249 ccline.cmdbuff = NULL;
6250 stuffcharReadbuff(':');
6251 stuffReadbuff((char_u *)p);
6252 stuffcharReadbuff(CAR);
6253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
6255 else if (cmdwin_result == K_XF2) /* :qa typed */
6256 {
6257 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6258 cmdwin_result = CAR;
6259 }
6260 else
6261 ccline.cmdbuff = vim_strsave(ml_get_curline());
6262 if (ccline.cmdbuff == NULL)
6263 cmdwin_result = Ctrl_C;
6264 else
6265 {
6266 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6267 ccline.cmdbufflen = ccline.cmdlen + 1;
6268 ccline.cmdpos = curwin->w_cursor.col;
6269 if (ccline.cmdpos > ccline.cmdlen)
6270 ccline.cmdpos = ccline.cmdlen;
6271 if (cmdwin_result == K_IGNORE)
6272 {
6273 set_cmdspos_cursor();
6274 redrawcmd();
6275 }
6276 }
6277
6278# ifdef FEAT_AUTOCMD
6279 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006280 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281# endif
6282 wp = curwin;
6283 bp = curbuf;
6284 win_goto(old_curwin);
6285 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006286
6287 /* win_close() may have already wiped the buffer when 'bh' is
6288 * set to 'wipe' */
6289 if (buf_valid(bp))
6290 close_buffer(NULL, bp, DOBUF_WIPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291
6292 /* Restore window sizes. */
6293 win_size_restore(&winsizes);
6294
6295# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006296 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297# endif
6298 }
6299
6300 ga_clear(&winsizes);
6301 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006302# ifdef FEAT_RIGHTLEFT
6303 cmdmsg_rl = save_cmdmsg_rl;
6304# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305
6306 State = save_State;
6307# ifdef FEAT_MOUSE
6308 setmouse();
6309# endif
6310
6311 return cmdwin_result;
6312}
6313#endif /* FEAT_CMDWIN */
6314
6315/*
6316 * Used for commands that either take a simple command string argument, or:
6317 * cmd << endmarker
6318 * {script}
6319 * endmarker
6320 * Returns a pointer to allocated memory with {script} or NULL.
6321 */
6322 char_u *
6323script_get(eap, cmd)
6324 exarg_T *eap;
6325 char_u *cmd;
6326{
6327 char_u *theline;
6328 char *end_pattern = NULL;
6329 char dot[] = ".";
6330 garray_T ga;
6331
6332 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6333 return NULL;
6334
6335 ga_init2(&ga, 1, 0x400);
6336
6337 if (cmd[2] != NUL)
6338 end_pattern = (char *)skipwhite(cmd + 2);
6339 else
6340 end_pattern = dot;
6341
6342 for (;;)
6343 {
6344 theline = eap->getline(
6345#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006346 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347#endif
6348 NUL, eap->cookie, 0);
6349
6350 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006351 {
6352 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355
6356 ga_concat(&ga, theline);
6357 ga_append(&ga, '\n');
6358 vim_free(theline);
6359 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006360 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361
6362 return (char_u *)ga.ga_data;
6363}