blob: ac801eb8b3fa3edc9dd907e1bbdc28e98f73013a [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 */
143/*ARGSUSED*/
144 char_u *
145getcmdline(firstc, count, indent)
146 int firstc;
147 long count; /* only used for incremental search */
148 int indent; /* indent for inside conditionals */
149{
150 int c;
151 int i;
152 int j;
153 int gotesc = FALSE; /* TRUE when <ESC> just typed */
154 int do_abbr; /* when TRUE check for abbr. */
155#ifdef FEAT_CMDHIST
156 char_u *lookfor = NULL; /* string to match */
157 int hiscnt; /* current history line in use */
158 int histype; /* history type to be used */
159#endif
160#ifdef FEAT_SEARCH_EXTRA
161 pos_T old_cursor;
162 colnr_T old_curswant;
163 colnr_T old_leftcol;
164 linenr_T old_topline;
165# ifdef FEAT_DIFF
166 int old_topfill;
167# endif
168 linenr_T old_botline;
169 int did_incsearch = FALSE;
170 int incsearch_postponed = FALSE;
171#endif
172 int did_wild_list = FALSE; /* did wild_list() recently */
173 int wim_index = 0; /* index in wim_flags[] */
174 int res;
175 int save_msg_scroll = msg_scroll;
176 int save_State = State; /* remember State when called */
177 int some_key_typed = FALSE; /* one of the keys was typed */
178#ifdef FEAT_MOUSE
179 /* mouse drag and release events are ignored, unless they are
180 * preceded with a mouse down event */
181 int ignore_drag_release = TRUE;
182#endif
183#ifdef FEAT_EVAL
184 int break_ctrl_c = FALSE;
185#endif
186 expand_T xpc;
187 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000188#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
189 /* Everything that may work recursively should save and restore the
190 * current command line in save_ccline. That includes update_screen(), a
191 * custom status line may invoke ":normal". */
192 struct cmdline_info save_ccline;
193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194
195#ifdef FEAT_SNIFF
196 want_sniff_request = 0;
197#endif
198#ifdef FEAT_EVAL
199 if (firstc == -1)
200 {
201 firstc = NUL;
202 break_ctrl_c = TRUE;
203 }
204#endif
205#ifdef FEAT_RIGHTLEFT
206 /* start without Hebrew mapping for a command line */
207 if (firstc == ':' || firstc == '=' || firstc == '>')
208 cmd_hkmap = 0;
209#endif
210
211 ccline.overstrike = FALSE; /* always start in insert mode */
212#ifdef FEAT_SEARCH_EXTRA
213 old_cursor = curwin->w_cursor; /* needs to be restored later */
214 old_curswant = curwin->w_curswant;
215 old_leftcol = curwin->w_leftcol;
216 old_topline = curwin->w_topline;
217# ifdef FEAT_DIFF
218 old_topfill = curwin->w_topfill;
219# endif
220 old_botline = curwin->w_botline;
221#endif
222
223 /*
224 * set some variables for redrawcmd()
225 */
226 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000227 ccline.cmdindent = (firstc > 0 ? indent : 0);
228
229 /* alloc initial ccline.cmdbuff */
230 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 if (ccline.cmdbuff == NULL)
232 return NULL; /* out of memory */
233 ccline.cmdlen = ccline.cmdpos = 0;
234 ccline.cmdbuff[0] = NUL;
235
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000236 /* autoindent for :insert and :append */
237 if (firstc <= 0)
238 {
239 copy_spaces(ccline.cmdbuff, indent);
240 ccline.cmdbuff[indent] = NUL;
241 ccline.cmdpos = indent;
242 ccline.cmdspos = indent;
243 ccline.cmdlen = indent;
244 }
245
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000247 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248
249#ifdef FEAT_RIGHTLEFT
250 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
251 && (firstc == '/' || firstc == '?'))
252 cmdmsg_rl = TRUE;
253 else
254 cmdmsg_rl = FALSE;
255#endif
256
257 redir_off = TRUE; /* don't redirect the typed command */
258 if (!cmd_silent)
259 {
260 i = msg_scrolled;
261 msg_scrolled = 0; /* avoid wait_return message */
262 gotocmdline(TRUE);
263 msg_scrolled += i;
264 redrawcmdprompt(); /* draw prompt or indent */
265 set_cmdspos();
266 }
267 xpc.xp_context = EXPAND_NOTHING;
268 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000269#ifndef BACKSLASH_IN_FILENAME
270 xpc.xp_shell = FALSE;
271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000273#if defined(FEAT_EVAL)
274 if (ccline.input_fn)
275 {
276 xpc.xp_context = ccline.xp_context;
277 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000278# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000279 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000280# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000281 }
282#endif
283
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 /*
285 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
286 * doing ":@0" when register 0 doesn't contain a CR.
287 */
288 msg_scroll = FALSE;
289
290 State = CMDLINE;
291
292 if (firstc == '/' || firstc == '?' || firstc == '@')
293 {
294 /* Use ":lmap" mappings for search pattern and input(). */
295 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
296 b_im_ptr = &curbuf->b_p_iminsert;
297 else
298 b_im_ptr = &curbuf->b_p_imsearch;
299 if (*b_im_ptr == B_IMODE_LMAP)
300 State |= LANGMAP;
301#ifdef USE_IM_CONTROL
302 im_set_active(*b_im_ptr == B_IMODE_IM);
303#endif
304 }
305#ifdef USE_IM_CONTROL
306 else if (p_imcmdline)
307 im_set_active(TRUE);
308#endif
309
310#ifdef FEAT_MOUSE
311 setmouse();
312#endif
313#ifdef CURSOR_SHAPE
314 ui_cursor_shape(); /* may show different cursor shape */
315#endif
316
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000317 /* When inside an autocommand for writing "exiting" may be set and
318 * terminal mode set to cooked. Need to set raw mode here then. */
319 settmode(TMODE_RAW);
320
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#ifdef FEAT_CMDHIST
322 init_history();
323 hiscnt = hislen; /* set hiscnt to impossible history value */
324 histype = hist_char2type(firstc);
325#endif
326
327#ifdef FEAT_DIGRAPHS
328 do_digraph(-1); /* init digraph typahead */
329#endif
330
331 /*
332 * Collect the command string, handling editing keys.
333 */
334 for (;;)
335 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000336 redir_off = TRUE; /* Don't redirect the typed command.
337 Repeated, because a ":redir" inside
338 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339#ifdef USE_ON_FLY_SCROLL
340 dont_scroll = FALSE; /* allow scrolling here */
341#endif
342 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
343
344 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000345
346 /* Get a character. Ignore K_IGNORE, it should not do anything, such
347 * as stop completion. */
348 do
349 {
350 c = safe_vgetc();
351 } while (c == K_IGNORE);
352
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 if (KeyTyped)
354 {
355 some_key_typed = TRUE;
356#ifdef FEAT_RIGHTLEFT
357 if (cmd_hkmap)
358 c = hkmap(c);
359# ifdef FEAT_FKMAP
360 if (cmd_fkmap)
361 c = cmdl_fkmap(c);
362# endif
363 if (cmdmsg_rl && !KeyStuffed)
364 {
365 /* Invert horizontal movements and operations. Only when
366 * typed by the user directly, not when the result of a
367 * mapping. */
368 switch (c)
369 {
370 case K_RIGHT: c = K_LEFT; break;
371 case K_S_RIGHT: c = K_S_LEFT; break;
372 case K_C_RIGHT: c = K_C_LEFT; break;
373 case K_LEFT: c = K_RIGHT; break;
374 case K_S_LEFT: c = K_S_RIGHT; break;
375 case K_C_LEFT: c = K_C_RIGHT; break;
376 }
377 }
378#endif
379 }
380
381 /*
382 * Ignore got_int when CTRL-C was typed here.
383 * Don't ignore it in :global, we really need to break then, e.g., for
384 * ":g/pat/normal /pat" (without the <CR>).
385 * Don't ignore it for the input() function.
386 */
387 if ((c == Ctrl_C
388#ifdef UNIX
389 || c == intr_char
390#endif
391 )
392#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
393 && firstc != '@'
394#endif
395#ifdef FEAT_EVAL
396 && !break_ctrl_c
397#endif
398 && !global_busy)
399 got_int = FALSE;
400
401#ifdef FEAT_CMDHIST
402 /* free old command line when finished moving around in the history
403 * list */
404 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000405 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000406 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 && c != K_PAGEDOWN && c != K_PAGEUP
408 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000409 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
411 {
412 vim_free(lookfor);
413 lookfor = NULL;
414 }
415#endif
416
417 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000418 * When there are matching completions to select <S-Tab> works like
419 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000421 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 c = Ctrl_P;
423
424#ifdef FEAT_WILDMENU
425 /* Special translations for 'wildmenu' */
426 if (did_wild_list && p_wmnu)
427 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000428 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000430 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 c = Ctrl_N;
432 }
433 /* Hitting CR after "emenu Name.": complete submenu */
434 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
435 && ccline.cmdpos > 1
436 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
437 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
438 && (c == '\n' || c == '\r' || c == K_KENTER))
439 c = K_DOWN;
440#endif
441
442 /* free expanded names when finished walking through matches */
443 if (xpc.xp_numfiles != -1
444 && !(c == p_wc && KeyTyped) && c != p_wcm
445 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
446 && c != Ctrl_L)
447 {
448 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
449 did_wild_list = FALSE;
450#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000451 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452#endif
453 xpc.xp_context = EXPAND_NOTHING;
454 wim_index = 0;
455#ifdef FEAT_WILDMENU
456 if (p_wmnu && wild_menu_showing != 0)
457 {
458 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000459 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000460
461 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000462 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463
464 if (wild_menu_showing == WM_SCROLLED)
465 {
466 /* Entered command line, move it up */
467 cmdline_row--;
468 redrawcmd();
469 }
470 else if (save_p_ls != -1)
471 {
472 /* restore 'laststatus' and 'winminheight' */
473 p_ls = save_p_ls;
474 p_wmh = save_p_wmh;
475 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000476 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000478 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 redrawcmd();
480 save_p_ls = -1;
481 }
482 else
483 {
484# ifdef FEAT_VERTSPLIT
485 win_redraw_last_status(topframe);
486# else
487 lastwin->w_redr_status = TRUE;
488# endif
489 redraw_statuslines();
490 }
491 KeyTyped = skt;
492 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000493 if (ccline.input_fn)
494 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 }
496#endif
497 }
498
499#ifdef FEAT_WILDMENU
500 /* Special translations for 'wildmenu' */
501 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
502 {
503 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000504 if (c == K_DOWN && ccline.cmdpos > 0
505 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000507 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 {
509 /* Hitting <Up>: Remove one submenu name in front of the
510 * cursor */
511 int found = FALSE;
512
513 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
514 i = 0;
515 while (--j > 0)
516 {
517 /* check for start of menu name */
518 if (ccline.cmdbuff[j] == ' '
519 && ccline.cmdbuff[j - 1] != '\\')
520 {
521 i = j + 1;
522 break;
523 }
524 /* check for start of submenu name */
525 if (ccline.cmdbuff[j] == '.'
526 && ccline.cmdbuff[j - 1] != '\\')
527 {
528 if (found)
529 {
530 i = j + 1;
531 break;
532 }
533 else
534 found = TRUE;
535 }
536 }
537 if (i > 0)
538 cmdline_del(i);
539 c = p_wc;
540 xpc.xp_context = EXPAND_NOTHING;
541 }
542 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000543 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000544 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000545 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 {
547 char_u upseg[5];
548
549 upseg[0] = PATHSEP;
550 upseg[1] = '.';
551 upseg[2] = '.';
552 upseg[3] = PATHSEP;
553 upseg[4] = NUL;
554
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000555 if (c == K_DOWN
556 && ccline.cmdpos > 0
557 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
558 && (ccline.cmdpos < 3
559 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
561 {
562 /* go down a directory */
563 c = p_wc;
564 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000565 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {
567 /* If in a direct ancestor, strip off one ../ to go down */
568 int found = FALSE;
569
570 j = ccline.cmdpos;
571 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
572 while (--j > i)
573 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000574#ifdef FEAT_MBYTE
575 if (has_mbyte)
576 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 if (vim_ispathsep(ccline.cmdbuff[j]))
579 {
580 found = TRUE;
581 break;
582 }
583 }
584 if (found
585 && ccline.cmdbuff[j - 1] == '.'
586 && ccline.cmdbuff[j - 2] == '.'
587 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
588 {
589 cmdline_del(j - 2);
590 c = p_wc;
591 }
592 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000593 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 {
595 /* go up a directory */
596 int found = FALSE;
597
598 j = ccline.cmdpos - 1;
599 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
600 while (--j > i)
601 {
602#ifdef FEAT_MBYTE
603 if (has_mbyte)
604 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
605#endif
606 if (vim_ispathsep(ccline.cmdbuff[j])
607#ifdef BACKSLASH_IN_FILENAME
608 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
609 == NULL
610#endif
611 )
612 {
613 if (found)
614 {
615 i = j + 1;
616 break;
617 }
618 else
619 found = TRUE;
620 }
621 }
622
623 if (!found)
624 j = i;
625 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
626 j += 4;
627 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
628 && j == i)
629 j += 3;
630 else
631 j = 0;
632 if (j > 0)
633 {
634 /* TODO this is only for DOS/UNIX systems - need to put in
635 * machine-specific stuff here and in upseg init */
636 cmdline_del(j);
637 put_on_cmdline(upseg + 1, 3, FALSE);
638 }
639 else if (ccline.cmdpos > i)
640 cmdline_del(i);
641 c = p_wc;
642 }
643 }
644#if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
645 if (p_wmnu
646 && (xpc.xp_context == EXPAND_FILES
647 || xpc.xp_context == EXPAND_MENUNAMES)
648 && (c == K_UP || c == K_DOWN))
649 xpc.xp_context = EXPAND_NOTHING;
650#endif
651
652#endif /* FEAT_WILDMENU */
653
654 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
655 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
656 if (c == Ctrl_BSL)
657 {
658 ++no_mapping;
659 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000660 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 --no_mapping;
662 --allow_keys;
663 /* CTRL-\ e doesn't work when obtaining an expression. */
664 if (c != Ctrl_N && c != Ctrl_G
665 && (c != 'e' || ccline.cmdfirstc == '='))
666 {
667 vungetc(c);
668 c = Ctrl_BSL;
669 }
670#ifdef FEAT_EVAL
671 else if (c == 'e')
672 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000673 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
675 /*
676 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000677 * Need to save and restore the current command line, to be
678 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 */
680 if (ccline.cmdpos == ccline.cmdlen)
681 new_cmdpos = 99999; /* keep it at the end */
682 else
683 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000684
685 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000687 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 if (c == '=')
689 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000690 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000691 * to avoid nasty things like going to another buffer when
692 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000693 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000694 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000696 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000697 restore_cmdline(&save_ccline);
698
699 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000701 ccline.cmdlen = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 STRCPY(ccline.cmdbuff, p);
703 vim_free(p);
704
705 /* Restore the cursor or use the position set with
706 * set_cmdline_pos(). */
707 if (new_cmdpos > ccline.cmdlen)
708 ccline.cmdpos = ccline.cmdlen;
709 else
710 ccline.cmdpos = new_cmdpos;
711
712 KeyTyped = FALSE; /* Don't do p_wc completion. */
713 redrawcmd();
714 goto cmdline_changed;
715 }
716 }
717 beep_flush();
718 c = ESC;
719 }
720#endif
721 else
722 {
723 if (c == Ctrl_G && p_im && restart_edit == 0)
724 restart_edit = 'a';
725 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
726 in history */
727 goto returncmd; /* back to Normal mode */
728 }
729 }
730
731#ifdef FEAT_CMDWIN
732 if (c == cedit_key || c == K_CMDWIN)
733 {
734 /*
735 * Open a window to edit the command line (and history).
736 */
737 c = ex_window();
738 some_key_typed = TRUE;
739 }
740# ifdef FEAT_DIGRAPHS
741 else
742# endif
743#endif
744#ifdef FEAT_DIGRAPHS
745 c = do_digraph(c);
746#endif
747
748 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
749 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
750 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000751 /* In Ex mode a backslash escapes a newline. */
752 if (exmode_active
753 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000754 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000755 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000756 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000758 if (c == K_KENTER)
759 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000761 else
762 {
763 gotesc = FALSE; /* Might have typed ESC previously, don't
764 truncate the cmdline now. */
765 if (ccheck_abbr(c + ABBR_OFF))
766 goto cmdline_changed;
767 if (!cmd_silent)
768 {
769 windgoto(msg_row, 0);
770 out_flush();
771 }
772 break;
773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 }
775
776 /*
777 * Completion for 'wildchar' or 'wildcharm' key.
778 * - hitting <ESC> twice means: abandon command line.
779 * - wildcard expansion is only done when the 'wildchar' key is really
780 * typed, not when it comes from a macro
781 */
782 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
783 {
784 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
785 {
786 /* if 'wildmode' contains "list" may still need to list */
787 if (xpc.xp_numfiles > 1
788 && !did_wild_list
789 && (wim_flags[wim_index] & WIM_LIST))
790 {
791 (void)showmatches(&xpc, FALSE);
792 redrawcmd();
793 did_wild_list = TRUE;
794 }
795 if (wim_flags[wim_index] & WIM_LONGEST)
796 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
797 else if (wim_flags[wim_index] & WIM_FULL)
798 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
799 else
800 res = OK; /* don't insert 'wildchar' now */
801 }
802 else /* typed p_wc first time */
803 {
804 wim_index = 0;
805 j = ccline.cmdpos;
806 /* if 'wildmode' first contains "longest", get longest
807 * common part */
808 if (wim_flags[0] & WIM_LONGEST)
809 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
810 else
811 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
812
813 /* if interrupted while completing, behave like it failed */
814 if (got_int)
815 {
816 (void)vpeekc(); /* remove <C-C> from input stream */
817 got_int = FALSE; /* don't abandon the command line */
818 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
819#ifdef FEAT_WILDMENU
820 xpc.xp_context = EXPAND_NOTHING;
821#endif
822 goto cmdline_changed;
823 }
824
825 /* when more than one match, and 'wildmode' first contains
826 * "list", or no change and 'wildmode' contains "longest,list",
827 * list all matches */
828 if (res == OK && xpc.xp_numfiles > 1)
829 {
830 /* a "longest" that didn't do anything is skipped (but not
831 * "list:longest") */
832 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
833 wim_index = 1;
834 if ((wim_flags[wim_index] & WIM_LIST)
835#ifdef FEAT_WILDMENU
836 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
837#endif
838 )
839 {
840 if (!(wim_flags[0] & WIM_LONGEST))
841 {
842#ifdef FEAT_WILDMENU
843 int p_wmnu_save = p_wmnu;
844 p_wmnu = 0;
845#endif
846 nextwild(&xpc, WILD_PREV, 0); /* remove match */
847#ifdef FEAT_WILDMENU
848 p_wmnu = p_wmnu_save;
849#endif
850 }
851#ifdef FEAT_WILDMENU
852 (void)showmatches(&xpc, p_wmnu
853 && ((wim_flags[wim_index] & WIM_LIST) == 0));
854#else
855 (void)showmatches(&xpc, FALSE);
856#endif
857 redrawcmd();
858 did_wild_list = TRUE;
859 if (wim_flags[wim_index] & WIM_LONGEST)
860 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
861 else if (wim_flags[wim_index] & WIM_FULL)
862 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
863 }
864 else
865 vim_beep();
866 }
867#ifdef FEAT_WILDMENU
868 else if (xpc.xp_numfiles == -1)
869 xpc.xp_context = EXPAND_NOTHING;
870#endif
871 }
872 if (wim_index < 3)
873 ++wim_index;
874 if (c == ESC)
875 gotesc = TRUE;
876 if (res == OK)
877 goto cmdline_changed;
878 }
879
880 gotesc = FALSE;
881
882 /* <S-Tab> goes to last match, in a clumsy way */
883 if (c == K_S_TAB && KeyTyped)
884 {
885 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
886 && nextwild(&xpc, WILD_PREV, 0) == OK
887 && nextwild(&xpc, WILD_PREV, 0) == OK)
888 goto cmdline_changed;
889 }
890
891 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
892 c = NL;
893
894 do_abbr = TRUE; /* default: check for abbreviation */
895
896 /*
897 * Big switch for a typed command line character.
898 */
899 switch (c)
900 {
901 case K_BS:
902 case Ctrl_H:
903 case K_DEL:
904 case K_KDEL:
905 case Ctrl_W:
906#ifdef FEAT_FKMAP
907 if (cmd_fkmap && c == K_BS)
908 c = K_DEL;
909#endif
910 if (c == K_KDEL)
911 c = K_DEL;
912
913 /*
914 * delete current character is the same as backspace on next
915 * character, except at end of line
916 */
917 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
918 ++ccline.cmdpos;
919#ifdef FEAT_MBYTE
920 if (has_mbyte && c == K_DEL)
921 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
922 ccline.cmdbuff + ccline.cmdpos);
923#endif
924 if (ccline.cmdpos > 0)
925 {
926 char_u *p;
927
928 j = ccline.cmdpos;
929 p = ccline.cmdbuff + j;
930#ifdef FEAT_MBYTE
931 if (has_mbyte)
932 {
933 p = mb_prevptr(ccline.cmdbuff, p);
934 if (c == Ctrl_W)
935 {
936 while (p > ccline.cmdbuff && vim_isspace(*p))
937 p = mb_prevptr(ccline.cmdbuff, p);
938 i = mb_get_class(p);
939 while (p > ccline.cmdbuff && mb_get_class(p) == i)
940 p = mb_prevptr(ccline.cmdbuff, p);
941 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000942 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
944 }
945 else
946#endif
947 if (c == Ctrl_W)
948 {
949 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
950 --p;
951 i = vim_iswordc(p[-1]);
952 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
953 && vim_iswordc(p[-1]) == i)
954 --p;
955 }
956 else
957 --p;
958 ccline.cmdpos = (int)(p - ccline.cmdbuff);
959 ccline.cmdlen -= j - ccline.cmdpos;
960 i = ccline.cmdpos;
961 while (i < ccline.cmdlen)
962 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
963
964 /* Truncate at the end, required for multi-byte chars. */
965 ccline.cmdbuff[ccline.cmdlen] = NUL;
966 redrawcmd();
967 }
968 else if (ccline.cmdlen == 0 && c != Ctrl_W
969 && ccline.cmdprompt == NULL && indent == 0)
970 {
971 /* In ex and debug mode it doesn't make sense to return. */
972 if (exmode_active
973#ifdef FEAT_EVAL
974 || ccline.cmdfirstc == '>'
975#endif
976 )
977 goto cmdline_not_changed;
978
979 vim_free(ccline.cmdbuff); /* no commandline to return */
980 ccline.cmdbuff = NULL;
981 if (!cmd_silent)
982 {
983#ifdef FEAT_RIGHTLEFT
984 if (cmdmsg_rl)
985 msg_col = Columns;
986 else
987#endif
988 msg_col = 0;
989 msg_putchar(' '); /* delete ':' */
990 }
991 redraw_cmdline = TRUE;
992 goto returncmd; /* back to cmd mode */
993 }
994 goto cmdline_changed;
995
996 case K_INS:
997 case K_KINS:
998#ifdef FEAT_FKMAP
999 /* if Farsi mode set, we are in reverse insert mode -
1000 Do not change the mode */
1001 if (cmd_fkmap)
1002 beep_flush();
1003 else
1004#endif
1005 ccline.overstrike = !ccline.overstrike;
1006#ifdef CURSOR_SHAPE
1007 ui_cursor_shape(); /* may show different cursor shape */
1008#endif
1009 goto cmdline_not_changed;
1010
1011 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001012 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 {
1014 /* ":lmap" mappings exists, toggle use of mappings. */
1015 State ^= LANGMAP;
1016#ifdef USE_IM_CONTROL
1017 im_set_active(FALSE); /* Disable input method */
1018#endif
1019 if (b_im_ptr != NULL)
1020 {
1021 if (State & LANGMAP)
1022 *b_im_ptr = B_IMODE_LMAP;
1023 else
1024 *b_im_ptr = B_IMODE_NONE;
1025 }
1026 }
1027#ifdef USE_IM_CONTROL
1028 else
1029 {
1030 /* There are no ":lmap" mappings, toggle IM. When
1031 * 'imdisable' is set don't try getting the status, it's
1032 * always off. */
1033 if ((p_imdisable && b_im_ptr != NULL)
1034 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1035 {
1036 im_set_active(FALSE); /* Disable input method */
1037 if (b_im_ptr != NULL)
1038 *b_im_ptr = B_IMODE_NONE;
1039 }
1040 else
1041 {
1042 im_set_active(TRUE); /* Enable input method */
1043 if (b_im_ptr != NULL)
1044 *b_im_ptr = B_IMODE_IM;
1045 }
1046 }
1047#endif
1048 if (b_im_ptr != NULL)
1049 {
1050 if (b_im_ptr == &curbuf->b_p_iminsert)
1051 set_iminsert_global();
1052 else
1053 set_imsearch_global();
1054 }
1055#ifdef CURSOR_SHAPE
1056 ui_cursor_shape(); /* may show different cursor shape */
1057#endif
1058#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1059 /* Show/unshow value of 'keymap' in status lines later. */
1060 status_redraw_curbuf();
1061#endif
1062 goto cmdline_not_changed;
1063
1064/* case '@': only in very old vi */
1065 case Ctrl_U:
1066 /* delete all characters left of the cursor */
1067 j = ccline.cmdpos;
1068 ccline.cmdlen -= j;
1069 i = ccline.cmdpos = 0;
1070 while (i < ccline.cmdlen)
1071 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1072 /* Truncate at the end, required for multi-byte chars. */
1073 ccline.cmdbuff[ccline.cmdlen] = NUL;
1074 redrawcmd();
1075 goto cmdline_changed;
1076
1077#ifdef FEAT_CLIPBOARD
1078 case Ctrl_Y:
1079 /* Copy the modeless selection, if there is one. */
1080 if (clip_star.state != SELECT_CLEARED)
1081 {
1082 if (clip_star.state == SELECT_DONE)
1083 clip_copy_modeless_selection(TRUE);
1084 goto cmdline_not_changed;
1085 }
1086 break;
1087#endif
1088
1089 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1090 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001091 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001092 * ":normal" runs out of characters. */
1093 if (exmode_active
1094#ifdef FEAT_EX_EXTRA
1095 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1096#endif
1097 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 goto cmdline_not_changed;
1099
1100 gotesc = TRUE; /* will free ccline.cmdbuff after
1101 putting it in history */
1102 goto returncmd; /* back to cmd mode */
1103
1104 case Ctrl_R: /* insert register */
1105#ifdef USE_ON_FLY_SCROLL
1106 dont_scroll = TRUE; /* disallow scrolling here */
1107#endif
1108 putcmdline('"', TRUE);
1109 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001110 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 if (i == Ctrl_O)
1112 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1113 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001114 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 --no_mapping;
1116#ifdef FEAT_EVAL
1117 /*
1118 * Insert the result of an expression.
1119 * Need to save the current command line, to be able to enter
1120 * a new one...
1121 */
1122 new_cmdpos = -1;
1123 if (c == '=')
1124 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1126 {
1127 beep_flush();
1128 c = ESC;
1129 }
1130 else
1131 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001132 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001134 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 }
1136 }
1137#endif
1138 if (c != ESC) /* use ESC to cancel inserting register */
1139 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001140 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001141
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001142#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001143 /* When there was a serious error abort getting the
1144 * command line. */
1145 if (aborting())
1146 {
1147 gotesc = TRUE; /* will free ccline.cmdbuff after
1148 putting it in history */
1149 goto returncmd; /* back to cmd mode */
1150 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 KeyTyped = FALSE; /* Don't do p_wc completion. */
1153#ifdef FEAT_EVAL
1154 if (new_cmdpos >= 0)
1155 {
1156 /* set_cmdline_pos() was used */
1157 if (new_cmdpos > ccline.cmdlen)
1158 ccline.cmdpos = ccline.cmdlen;
1159 else
1160 ccline.cmdpos = new_cmdpos;
1161 }
1162#endif
1163 }
1164 redrawcmd();
1165 goto cmdline_changed;
1166
1167 case Ctrl_D:
1168 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1169 break; /* Use ^D as normal char instead */
1170
1171 redrawcmd();
1172 continue; /* don't do incremental search now */
1173
1174 case K_RIGHT:
1175 case K_S_RIGHT:
1176 case K_C_RIGHT:
1177 do
1178 {
1179 if (ccline.cmdpos >= ccline.cmdlen)
1180 break;
1181 i = cmdline_charsize(ccline.cmdpos);
1182 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1183 break;
1184 ccline.cmdspos += i;
1185#ifdef FEAT_MBYTE
1186 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001187 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 + ccline.cmdpos);
1189 else
1190#endif
1191 ++ccline.cmdpos;
1192 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001193 while ((c == K_S_RIGHT || c == K_C_RIGHT
1194 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1196#ifdef FEAT_MBYTE
1197 if (has_mbyte)
1198 set_cmdspos_cursor();
1199#endif
1200 goto cmdline_not_changed;
1201
1202 case K_LEFT:
1203 case K_S_LEFT:
1204 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001205 if (ccline.cmdpos == 0)
1206 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 do
1208 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 --ccline.cmdpos;
1210#ifdef FEAT_MBYTE
1211 if (has_mbyte) /* move to first byte of char */
1212 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1213 ccline.cmdbuff + ccline.cmdpos);
1214#endif
1215 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1216 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001217 while (ccline.cmdpos > 0
1218 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001219 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1221#ifdef FEAT_MBYTE
1222 if (has_mbyte)
1223 set_cmdspos_cursor();
1224#endif
1225 goto cmdline_not_changed;
1226
1227 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001228 /* Ignore mouse event or ex_window() result. */
1229 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
Bram Moolenaar4770d092006-01-12 23:22:24 +00001231#ifdef FEAT_GUI_W32
1232 /* On Win32 ignore <M-F4>, we get it when closing the window was
1233 * cancelled. */
1234 case K_F4:
1235 if (mod_mask == MOD_MASK_ALT)
1236 {
1237 redrawcmd(); /* somehow the cmdline is cleared */
1238 goto cmdline_not_changed;
1239 }
1240 break;
1241#endif
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243#ifdef FEAT_MOUSE
1244 case K_MIDDLEDRAG:
1245 case K_MIDDLERELEASE:
1246 goto cmdline_not_changed; /* Ignore mouse */
1247
1248 case K_MIDDLEMOUSE:
1249# ifdef FEAT_GUI
1250 /* When GUI is active, also paste when 'mouse' is empty */
1251 if (!gui.in_use)
1252# endif
1253 if (!mouse_has(MOUSE_COMMAND))
1254 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001255# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001257 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001259# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001260 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 redrawcmd();
1262 goto cmdline_changed;
1263
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001264# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001266 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 redrawcmd();
1268 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001269# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
1271 case K_LEFTDRAG:
1272 case K_LEFTRELEASE:
1273 case K_RIGHTDRAG:
1274 case K_RIGHTRELEASE:
1275 /* Ignore drag and release events when the button-down wasn't
1276 * seen before. */
1277 if (ignore_drag_release)
1278 goto cmdline_not_changed;
1279 /* FALLTHROUGH */
1280 case K_LEFTMOUSE:
1281 case K_RIGHTMOUSE:
1282 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1283 ignore_drag_release = TRUE;
1284 else
1285 ignore_drag_release = FALSE;
1286# ifdef FEAT_GUI
1287 /* When GUI is active, also move when 'mouse' is empty */
1288 if (!gui.in_use)
1289# endif
1290 if (!mouse_has(MOUSE_COMMAND))
1291 goto cmdline_not_changed; /* Ignore mouse */
1292# ifdef FEAT_CLIPBOARD
1293 if (mouse_row < cmdline_row && clip_star.available)
1294 {
1295 int button, is_click, is_drag;
1296
1297 /*
1298 * Handle modeless selection.
1299 */
1300 button = get_mouse_button(KEY2TERMCAP1(c),
1301 &is_click, &is_drag);
1302 if (mouse_model_popup() && button == MOUSE_LEFT
1303 && (mod_mask & MOD_MASK_SHIFT))
1304 {
1305 /* Translate shift-left to right button. */
1306 button = MOUSE_RIGHT;
1307 mod_mask &= ~MOD_MASK_SHIFT;
1308 }
1309 clip_modeless(button, is_click, is_drag);
1310 goto cmdline_not_changed;
1311 }
1312# endif
1313
1314 set_cmdspos();
1315 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1316 ++ccline.cmdpos)
1317 {
1318 i = cmdline_charsize(ccline.cmdpos);
1319 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1320 && mouse_col < ccline.cmdspos % Columns + i)
1321 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001322# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 if (has_mbyte)
1324 {
1325 /* Count ">" for double-wide char that doesn't fit. */
1326 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001327 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 + ccline.cmdpos) - 1;
1329 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001330# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 ccline.cmdspos += i;
1332 }
1333 goto cmdline_not_changed;
1334
1335 /* Mouse scroll wheel: ignored here */
1336 case K_MOUSEDOWN:
1337 case K_MOUSEUP:
1338 /* Alternate buttons ignored here */
1339 case K_X1MOUSE:
1340 case K_X1DRAG:
1341 case K_X1RELEASE:
1342 case K_X2MOUSE:
1343 case K_X2DRAG:
1344 case K_X2RELEASE:
1345 goto cmdline_not_changed;
1346
1347#endif /* FEAT_MOUSE */
1348
1349#ifdef FEAT_GUI
1350 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1351 case K_LEFTRELEASE_NM:
1352 goto cmdline_not_changed;
1353
1354 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001355 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 {
1357 gui_do_scroll();
1358 redrawcmd();
1359 }
1360 goto cmdline_not_changed;
1361
1362 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001363 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 {
1365 gui_do_horiz_scroll();
1366 redrawcmd();
1367 }
1368 goto cmdline_not_changed;
1369#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001370#ifdef FEAT_GUI_TABLINE
1371 case K_TABLINE:
1372 case K_TABMENU:
1373 /* Don't want to change any tabs here. Make sure the same tab
1374 * is still selected. */
1375 if (gui_use_tabline())
1376 gui_mch_set_curtab(tabpage_index(curtab));
1377 goto cmdline_not_changed;
1378#endif
1379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 case K_SELECT: /* end of Select mode mapping - ignore */
1381 goto cmdline_not_changed;
1382
1383 case Ctrl_B: /* begin of command line */
1384 case K_HOME:
1385 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 case K_S_HOME:
1387 case K_C_HOME:
1388 ccline.cmdpos = 0;
1389 set_cmdspos();
1390 goto cmdline_not_changed;
1391
1392 case Ctrl_E: /* end of command line */
1393 case K_END:
1394 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 case K_S_END:
1396 case K_C_END:
1397 ccline.cmdpos = ccline.cmdlen;
1398 set_cmdspos_cursor();
1399 goto cmdline_not_changed;
1400
1401 case Ctrl_A: /* all matches */
1402 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1403 break;
1404 goto cmdline_changed;
1405
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001406 case Ctrl_L:
1407#ifdef FEAT_SEARCH_EXTRA
1408 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1409 {
1410 /* Add a character from under the cursor for 'incsearch' */
1411 if (did_incsearch
1412 && !equalpos(curwin->w_cursor, old_cursor))
1413 {
1414 c = gchar_cursor();
1415 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001416 {
1417 if (c == firstc || vim_strchr((char_u *)(
1418 p_magic ? "\\^$.*[" : "\\^$"), c)
1419 != NULL)
1420 {
1421 /* put a backslash before special characters */
1422 stuffcharReadbuff(c);
1423 c = '\\';
1424 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001425 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001426 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001427 }
1428 goto cmdline_not_changed;
1429 }
1430#endif
1431
1432 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1434 break;
1435 goto cmdline_changed;
1436
1437 case Ctrl_N: /* next match */
1438 case Ctrl_P: /* previous match */
1439 if (xpc.xp_numfiles > 0)
1440 {
1441 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1442 == FAIL)
1443 break;
1444 goto cmdline_changed;
1445 }
1446
1447#ifdef FEAT_CMDHIST
1448 case K_UP:
1449 case K_DOWN:
1450 case K_S_UP:
1451 case K_S_DOWN:
1452 case K_PAGEUP:
1453 case K_KPAGEUP:
1454 case K_PAGEDOWN:
1455 case K_KPAGEDOWN:
1456 if (hislen == 0 || firstc == NUL) /* no history */
1457 goto cmdline_not_changed;
1458
1459 i = hiscnt;
1460
1461 /* save current command string so it can be restored later */
1462 if (lookfor == NULL)
1463 {
1464 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1465 goto cmdline_not_changed;
1466 lookfor[ccline.cmdpos] = NUL;
1467 }
1468
1469 j = (int)STRLEN(lookfor);
1470 for (;;)
1471 {
1472 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001473 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001474 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 {
1476 if (hiscnt == hislen) /* first time */
1477 hiscnt = hisidx[histype];
1478 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1479 hiscnt = hislen - 1;
1480 else if (hiscnt != hisidx[histype] + 1)
1481 --hiscnt;
1482 else /* at top of list */
1483 {
1484 hiscnt = i;
1485 break;
1486 }
1487 }
1488 else /* one step forwards */
1489 {
1490 /* on last entry, clear the line */
1491 if (hiscnt == hisidx[histype])
1492 {
1493 hiscnt = hislen;
1494 break;
1495 }
1496
1497 /* not on a history line, nothing to do */
1498 if (hiscnt == hislen)
1499 break;
1500 if (hiscnt == hislen - 1) /* wrap around */
1501 hiscnt = 0;
1502 else
1503 ++hiscnt;
1504 }
1505 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1506 {
1507 hiscnt = i;
1508 break;
1509 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001510 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001511 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 || STRNCMP(history[histype][hiscnt].hisstr,
1513 lookfor, (size_t)j) == 0)
1514 break;
1515 }
1516
1517 if (hiscnt != i) /* jumped to other entry */
1518 {
1519 char_u *p;
1520 int len;
1521 int old_firstc;
1522
1523 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001524 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (hiscnt == hislen)
1526 p = lookfor; /* back to the old one */
1527 else
1528 p = history[histype][hiscnt].hisstr;
1529
1530 if (histype == HIST_SEARCH
1531 && p != lookfor
1532 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1533 {
1534 /* Correct for the separator character used when
1535 * adding the history entry vs the one used now.
1536 * First loop: count length.
1537 * Second loop: copy the characters. */
1538 for (i = 0; i <= 1; ++i)
1539 {
1540 len = 0;
1541 for (j = 0; p[j] != NUL; ++j)
1542 {
1543 /* Replace old sep with new sep, unless it is
1544 * escaped. */
1545 if (p[j] == old_firstc
1546 && (j == 0 || p[j - 1] != '\\'))
1547 {
1548 if (i > 0)
1549 ccline.cmdbuff[len] = firstc;
1550 }
1551 else
1552 {
1553 /* Escape new sep, unless it is already
1554 * escaped. */
1555 if (p[j] == firstc
1556 && (j == 0 || p[j - 1] != '\\'))
1557 {
1558 if (i > 0)
1559 ccline.cmdbuff[len] = '\\';
1560 ++len;
1561 }
1562 if (i > 0)
1563 ccline.cmdbuff[len] = p[j];
1564 }
1565 ++len;
1566 }
1567 if (i == 0)
1568 {
1569 alloc_cmdbuff(len);
1570 if (ccline.cmdbuff == NULL)
1571 goto returncmd;
1572 }
1573 }
1574 ccline.cmdbuff[len] = NUL;
1575 }
1576 else
1577 {
1578 alloc_cmdbuff((int)STRLEN(p));
1579 if (ccline.cmdbuff == NULL)
1580 goto returncmd;
1581 STRCPY(ccline.cmdbuff, p);
1582 }
1583
1584 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1585 redrawcmd();
1586 goto cmdline_changed;
1587 }
1588 beep_flush();
1589 goto cmdline_not_changed;
1590#endif
1591
1592 case Ctrl_V:
1593 case Ctrl_Q:
1594#ifdef FEAT_MOUSE
1595 ignore_drag_release = TRUE;
1596#endif
1597 putcmdline('^', TRUE);
1598 c = get_literal(); /* get next (two) character(s) */
1599 do_abbr = FALSE; /* don't do abbreviation now */
1600#ifdef FEAT_MBYTE
1601 /* may need to remove ^ when composing char was typed */
1602 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1603 {
1604 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1605 msg_putchar(' ');
1606 cursorcmd();
1607 }
1608#endif
1609 break;
1610
1611#ifdef FEAT_DIGRAPHS
1612 case Ctrl_K:
1613#ifdef FEAT_MOUSE
1614 ignore_drag_release = TRUE;
1615#endif
1616 putcmdline('?', TRUE);
1617#ifdef USE_ON_FLY_SCROLL
1618 dont_scroll = TRUE; /* disallow scrolling here */
1619#endif
1620 c = get_digraph(TRUE);
1621 if (c != NUL)
1622 break;
1623
1624 redrawcmd();
1625 goto cmdline_not_changed;
1626#endif /* FEAT_DIGRAPHS */
1627
1628#ifdef FEAT_RIGHTLEFT
1629 case Ctrl__: /* CTRL-_: switch language mode */
1630 if (!p_ari)
1631 break;
1632#ifdef FEAT_FKMAP
1633 if (p_altkeymap)
1634 {
1635 cmd_fkmap = !cmd_fkmap;
1636 if (cmd_fkmap) /* in Farsi always in Insert mode */
1637 ccline.overstrike = FALSE;
1638 }
1639 else /* Hebrew is default */
1640#endif
1641 cmd_hkmap = !cmd_hkmap;
1642 goto cmdline_not_changed;
1643#endif
1644
1645 default:
1646#ifdef UNIX
1647 if (c == intr_char)
1648 {
1649 gotesc = TRUE; /* will free ccline.cmdbuff after
1650 putting it in history */
1651 goto returncmd; /* back to Normal mode */
1652 }
1653#endif
1654 /*
1655 * Normal character with no special meaning. Just set mod_mask
1656 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1657 * the string <S-Space>. This should only happen after ^V.
1658 */
1659 if (!IS_SPECIAL(c))
1660 mod_mask = 0x0;
1661 break;
1662 }
1663 /*
1664 * End of switch on command line character.
1665 * We come here if we have a normal character.
1666 */
1667
1668 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1669#ifdef FEAT_MBYTE
1670 /* Add ABBR_OFF for characters above 0x100, this is
1671 * what check_abbr() expects. */
1672 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1673#endif
1674 c))
1675 goto cmdline_changed;
1676
1677 /*
1678 * put the character in the command line
1679 */
1680 if (IS_SPECIAL(c) || mod_mask != 0)
1681 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1682 else
1683 {
1684#ifdef FEAT_MBYTE
1685 if (has_mbyte)
1686 {
1687 j = (*mb_char2bytes)(c, IObuff);
1688 IObuff[j] = NUL; /* exclude composing chars */
1689 put_on_cmdline(IObuff, j, TRUE);
1690 }
1691 else
1692#endif
1693 {
1694 IObuff[0] = c;
1695 put_on_cmdline(IObuff, 1, TRUE);
1696 }
1697 }
1698 goto cmdline_changed;
1699
1700/*
1701 * This part implements incremental searches for "/" and "?"
1702 * Jump to cmdline_not_changed when a character has been read but the command
1703 * line did not change. Then we only search and redraw if something changed in
1704 * the past.
1705 * Jump to cmdline_changed when the command line did change.
1706 * (Sorry for the goto's, I know it is ugly).
1707 */
1708cmdline_not_changed:
1709#ifdef FEAT_SEARCH_EXTRA
1710 if (!incsearch_postponed)
1711 continue;
1712#endif
1713
1714cmdline_changed:
1715#ifdef FEAT_SEARCH_EXTRA
1716 /*
1717 * 'incsearch' highlighting.
1718 */
1719 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1720 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001721 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001722#ifdef FEAT_RELTIME
1723 proftime_T tm;
1724#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 /* if there is a character waiting, search and redraw later */
1727 if (char_avail())
1728 {
1729 incsearch_postponed = TRUE;
1730 continue;
1731 }
1732 incsearch_postponed = FALSE;
1733 curwin->w_cursor = old_cursor; /* start at old position */
1734
1735 /* If there is no command line, don't do anything */
1736 if (ccline.cmdlen == 0)
1737 i = 0;
1738 else
1739 {
1740 cursor_off(); /* so the user knows we're busy */
1741 out_flush();
1742 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001743#ifdef FEAT_RELTIME
1744 /* Set the time limit to half a second. */
1745 profile_setlimit(500L, &tm);
1746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001748 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1749#ifdef FEAT_RELTIME
1750 &tm
1751#else
1752 NULL
1753#endif
1754 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 --emsg_off;
1756 /* if interrupted while searching, behave like it failed */
1757 if (got_int)
1758 {
1759 (void)vpeekc(); /* remove <C-C> from input stream */
1760 got_int = FALSE; /* don't abandon the command line */
1761 i = 0;
1762 }
1763 else if (char_avail())
1764 /* cancelled searching because a char was typed */
1765 incsearch_postponed = TRUE;
1766 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001767 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 highlight_match = TRUE; /* highlight position */
1769 else
1770 highlight_match = FALSE; /* remove highlight */
1771
1772 /* first restore the old curwin values, so the screen is
1773 * positioned in the same way as the actual search command */
1774 curwin->w_leftcol = old_leftcol;
1775 curwin->w_topline = old_topline;
1776# ifdef FEAT_DIFF
1777 curwin->w_topfill = old_topfill;
1778# endif
1779 curwin->w_botline = old_botline;
1780 changed_cline_bef_curs();
1781 update_topline();
1782
1783 if (i != 0)
1784 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001785 pos_T save_pos = curwin->w_cursor;
1786
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001788 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 * moves the whole match onto the screen when 'nowrap' is set.
1790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 curwin->w_cursor.lnum += search_match_lines;
1792 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001793 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1794 {
1795 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1796 coladvance((colnr_T)MAXCOL);
1797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001799 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001800 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001802 else
1803 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001804
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001806# ifdef FEAT_WINDOWS
1807 /* May redraw the status line to show the cursor position. */
1808 if (p_ru && curwin->w_status_height > 0)
1809 curwin->w_redr_status = TRUE;
1810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001812 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001813 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001814 restore_cmdline(&save_ccline);
1815
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001816 /* Leave it at the end to make CTRL-R CTRL-W work. */
1817 if (i != 0)
1818 curwin->w_cursor = end_pos;
1819
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 msg_starthere();
1821 redrawcmdline();
1822 did_incsearch = TRUE;
1823 }
1824#else /* FEAT_SEARCH_EXTRA */
1825 ;
1826#endif
1827
1828#ifdef FEAT_RIGHTLEFT
1829 if (cmdmsg_rl
1830# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001831 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832# endif
1833 )
1834 /* Always redraw the whole command line to fix shaping and
1835 * right-left typing. Not efficient, but it works. */
1836 redrawcmd();
1837#endif
1838 }
1839
1840returncmd:
1841
1842#ifdef FEAT_RIGHTLEFT
1843 cmdmsg_rl = FALSE;
1844#endif
1845
1846#ifdef FEAT_FKMAP
1847 cmd_fkmap = 0;
1848#endif
1849
1850 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001851 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
1853#ifdef FEAT_SEARCH_EXTRA
1854 if (did_incsearch)
1855 {
1856 curwin->w_cursor = old_cursor;
1857 curwin->w_curswant = old_curswant;
1858 curwin->w_leftcol = old_leftcol;
1859 curwin->w_topline = old_topline;
1860# ifdef FEAT_DIFF
1861 curwin->w_topfill = old_topfill;
1862# endif
1863 curwin->w_botline = old_botline;
1864 highlight_match = FALSE;
1865 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001866 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868#endif
1869
1870 if (ccline.cmdbuff != NULL)
1871 {
1872 /*
1873 * Put line in history buffer (":" and "=" only when it was typed).
1874 */
1875#ifdef FEAT_CMDHIST
1876 if (ccline.cmdlen && firstc != NUL
1877 && (some_key_typed || histype == HIST_SEARCH))
1878 {
1879 add_to_history(histype, ccline.cmdbuff, TRUE,
1880 histype == HIST_SEARCH ? firstc : NUL);
1881 if (firstc == ':')
1882 {
1883 vim_free(new_last_cmdline);
1884 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1885 }
1886 }
1887#endif
1888
1889 if (gotesc) /* abandon command line */
1890 {
1891 vim_free(ccline.cmdbuff);
1892 ccline.cmdbuff = NULL;
1893 if (msg_scrolled == 0)
1894 compute_cmdrow();
1895 MSG("");
1896 redraw_cmdline = TRUE;
1897 }
1898 }
1899
1900 /*
1901 * If the screen was shifted up, redraw the whole screen (later).
1902 * If the line is too long, clear it, so ruler and shown command do
1903 * not get printed in the middle of it.
1904 */
1905 msg_check();
1906 msg_scroll = save_msg_scroll;
1907 redir_off = FALSE;
1908
1909 /* When the command line was typed, no need for a wait-return prompt. */
1910 if (some_key_typed)
1911 need_wait_return = FALSE;
1912
1913 State = save_State;
1914#ifdef USE_IM_CONTROL
1915 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1916 im_save_status(b_im_ptr);
1917 im_set_active(FALSE);
1918#endif
1919#ifdef FEAT_MOUSE
1920 setmouse();
1921#endif
1922#ifdef CURSOR_SHAPE
1923 ui_cursor_shape(); /* may show different cursor shape */
1924#endif
1925
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001926 {
1927 char_u *p = ccline.cmdbuff;
1928
1929 /* Make ccline empty, getcmdline() may try to use it. */
1930 ccline.cmdbuff = NULL;
1931 return p;
1932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933}
1934
1935#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1936/*
1937 * Get a command line with a prompt.
1938 * This is prepared to be called recursively from getcmdline() (e.g. by
1939 * f_input() when evaluating an expression from CTRL-R =).
1940 * Returns the command line in allocated memory, or NULL.
1941 */
1942 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001943getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 int firstc;
1945 char_u *prompt; /* command line prompt */
1946 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001947 int xp_context; /* type of expansion */
1948 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949{
1950 char_u *s;
1951 struct cmdline_info save_ccline;
1952 int msg_col_save = msg_col;
1953
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001954 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 ccline.cmdprompt = prompt;
1956 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001957# ifdef FEAT_EVAL
1958 ccline.xp_context = xp_context;
1959 ccline.xp_arg = xp_arg;
1960 ccline.input_fn = (firstc == '@');
1961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001963 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 /* Restore msg_col, the prompt from input() may have changed it. */
1965 msg_col = msg_col_save;
1966
1967 return s;
1968}
1969#endif
1970
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001971/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001972 * Return TRUE when the text must not be changed and we can't switch to
1973 * another window or buffer. Used when editing the command line, evaluating
1974 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001975 */
1976 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001977text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001978{
1979#ifdef FEAT_CMDWIN
1980 if (cmdwin_type != 0)
1981 return TRUE;
1982#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001983 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001984}
1985
1986/*
1987 * Give an error message for a command that isn't allowed while the cmdline
1988 * window is open or editing the cmdline in another way.
1989 */
1990 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001991text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001992{
1993#ifdef FEAT_CMDWIN
1994 if (cmdwin_type != 0)
1995 EMSG(_(e_cmdwin));
1996 else
1997#endif
1998 EMSG(_(e_secure));
1999}
2000
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002001#if defined(FEAT_AUTOCMD) || defined(PROTO)
2002/*
2003 * Check if "curbuf_lock" is set and return TRUE when it is and give an error
2004 * message.
2005 */
2006 int
2007curbuf_locked()
2008{
2009 if (curbuf_lock > 0)
2010 {
2011 EMSG(_("E788: Not allowed to edit another buffer now"));
2012 return TRUE;
2013 }
2014 return FALSE;
2015}
2016#endif
2017
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 static int
2019cmdline_charsize(idx)
2020 int idx;
2021{
2022#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2023 if (cmdline_star > 0) /* showing '*', always 1 position */
2024 return 1;
2025#endif
2026 return ptr2cells(ccline.cmdbuff + idx);
2027}
2028
2029/*
2030 * Compute the offset of the cursor on the command line for the prompt and
2031 * indent.
2032 */
2033 static void
2034set_cmdspos()
2035{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002036 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 ccline.cmdspos = 1 + ccline.cmdindent;
2038 else
2039 ccline.cmdspos = 0 + ccline.cmdindent;
2040}
2041
2042/*
2043 * Compute the screen position for the cursor on the command line.
2044 */
2045 static void
2046set_cmdspos_cursor()
2047{
2048 int i, m, c;
2049
2050 set_cmdspos();
2051 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002052 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002054 if (m < 0) /* overflow, Columns or Rows at weird value */
2055 m = MAXCOL;
2056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 else
2058 m = MAXCOL;
2059 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2060 {
2061 c = cmdline_charsize(i);
2062#ifdef FEAT_MBYTE
2063 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2064 if (has_mbyte)
2065 correct_cmdspos(i, c);
2066#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002067 /* If the cmdline doesn't fit, show cursor on last visible char.
2068 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 if ((ccline.cmdspos += c) >= m)
2070 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 ccline.cmdspos -= c;
2072 break;
2073 }
2074#ifdef FEAT_MBYTE
2075 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002076 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077#endif
2078 }
2079}
2080
2081#ifdef FEAT_MBYTE
2082/*
2083 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2084 * character that doesn't fit, so that a ">" must be displayed.
2085 */
2086 static void
2087correct_cmdspos(idx, cells)
2088 int idx;
2089 int cells;
2090{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002091 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2093 && ccline.cmdspos % Columns + cells > Columns)
2094 ccline.cmdspos++;
2095}
2096#endif
2097
2098/*
2099 * Get an Ex command line for the ":" command.
2100 */
2101/* ARGSUSED */
2102 char_u *
2103getexline(c, dummy, indent)
2104 int c; /* normally ':', NUL for ":append" */
2105 void *dummy; /* cookie not used */
2106 int indent; /* indent for inside conditionals */
2107{
2108 /* When executing a register, remove ':' that's in front of each line. */
2109 if (exec_from_reg && vpeekc() == ':')
2110 (void)vgetc();
2111 return getcmdline(c, 1L, indent);
2112}
2113
2114/*
2115 * Get an Ex command line for Ex mode.
2116 * In Ex mode we only use the OS supplied line editing features and no
2117 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002118 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 */
2120/* ARGSUSED */
2121 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002122getexmodeline(promptc, dummy, indent)
2123 int promptc; /* normally ':', NUL for ":append" and '?' for
2124 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 void *dummy; /* cookie not used */
2126 int indent; /* indent for inside conditionals */
2127{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002128 garray_T line_ga;
2129 char_u *pend;
2130 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002131 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002132 int escaped = FALSE; /* CTRL-V typed */
2133 int vcol = 0;
2134 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002135 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
2137 /* Switch cursor on now. This avoids that it happens after the "\n", which
2138 * confuses the system function that computes tabstops. */
2139 cursor_on();
2140
2141 /* always start in column 0; write a newline if necessary */
2142 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002143 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002145 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002147 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002148 if (p_prompt)
2149 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 while (indent-- > 0)
2151 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154
2155 ga_init2(&line_ga, 1, 30);
2156
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002157 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002158 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002159 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002160 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002161 while (indent >= 8)
2162 {
2163 ga_append(&line_ga, TAB);
2164 msg_puts((char_u *)" ");
2165 indent -= 8;
2166 }
2167 while (indent-- > 0)
2168 {
2169 ga_append(&line_ga, ' ');
2170 msg_putchar(' ');
2171 }
2172 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002173 ++no_mapping;
2174 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002175
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 /*
2177 * Get the line, one character at a time.
2178 */
2179 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002180 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
2182 if (ga_grow(&line_ga, 40) == FAIL)
2183 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002184 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002186 /* Get one character at a time. Don't use inchar(), it can't handle
2187 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002188 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002189 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
2191 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002192 * Handle line editing.
2193 * Previously this was left to the system, putting the terminal in
2194 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002196 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002198 msg_putchar('\n');
2199 break;
2200 }
2201
2202 if (!escaped)
2203 {
2204 /* CR typed means "enter", which is NL */
2205 if (c1 == '\r')
2206 c1 = '\n';
2207
2208 if (c1 == BS || c1 == K_BS
2209 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002211 if (line_ga.ga_len > 0)
2212 {
2213 --line_ga.ga_len;
2214 goto redraw;
2215 }
2216 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 }
2218
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002219 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002221 msg_col = startcol;
2222 msg_clr_eos();
2223 line_ga.ga_len = 0;
2224 continue;
2225 }
2226
2227 if (c1 == Ctrl_T)
2228 {
2229 p = (char_u *)line_ga.ga_data;
2230 p[line_ga.ga_len] = NUL;
2231 indent = get_indent_str(p, 8);
2232 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2233add_indent:
2234 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002236 char_u *s = skipwhite(p);
2237
2238 ga_grow(&line_ga, 1);
2239 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2240 *s = ' ';
2241 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002243redraw:
2244 /* redraw the line */
2245 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002246 vcol = 0;
2247 for (p = (char_u *)line_ga.ga_data;
2248 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002250 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002252 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002254 msg_putchar(' ');
2255 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002257 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002259 msg_outtrans_len(p, 1);
2260 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
2262 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002263 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002264 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002265 continue;
2266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002268 if (c1 == Ctrl_D)
2269 {
2270 /* Delete one shiftwidth. */
2271 p = (char_u *)line_ga.ga_data;
2272 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002274 if (prev_char == '^')
2275 ex_keep_indent = TRUE;
2276 indent = 0;
2277 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279 else
2280 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002281 p[line_ga.ga_len] = NUL;
2282 indent = get_indent_str(p, 8);
2283 --indent;
2284 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002286 while (get_indent_str(p, 8) > indent)
2287 {
2288 char_u *s = skipwhite(p);
2289
2290 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2291 --line_ga.ga_len;
2292 }
2293 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002295
2296 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2297 {
2298 escaped = TRUE;
2299 continue;
2300 }
2301
2302 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2303 if (IS_SPECIAL(c1))
2304 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002306
2307 if (IS_SPECIAL(c1))
2308 c1 = '?';
2309 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002310 if (c1 == '\n')
2311 msg_putchar('\n');
2312 else if (c1 == TAB)
2313 {
2314 /* Don't use chartabsize(), 'ts' can be different */
2315 do
2316 {
2317 msg_putchar(' ');
2318 } while (++vcol % 8);
2319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002322 msg_outtrans_len(
2323 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2324 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002326 ++line_ga.ga_len;
2327 escaped = FALSE;
2328
2329 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002330 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002331
2332 /* we are done when a NL is entered, but not when it comes after a
2333 * backslash */
2334 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2335 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002338 --pend;
2339 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002340 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
2342 }
2343
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002344 --no_mapping;
2345 --allow_keys;
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 /* make following messages go to the next line */
2348 msg_didout = FALSE;
2349 msg_col = 0;
2350 if (msg_row < Rows - 1)
2351 ++msg_row;
2352 emsg_on_display = FALSE; /* don't want ui_delay() */
2353
2354 if (got_int)
2355 ga_clear(&line_ga);
2356
2357 return (char_u *)line_ga.ga_data;
2358}
2359
Bram Moolenaare344bea2005-09-01 20:46:49 +00002360# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2361 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362/*
2363 * Return TRUE if ccline.overstrike is on.
2364 */
2365 int
2366cmdline_overstrike()
2367{
2368 return ccline.overstrike;
2369}
2370
2371/*
2372 * Return TRUE if the cursor is at the end of the cmdline.
2373 */
2374 int
2375cmdline_at_end()
2376{
2377 return (ccline.cmdpos >= ccline.cmdlen);
2378}
2379#endif
2380
Bram Moolenaar9372a112005-12-06 19:59:18 +00002381#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382/*
2383 * Return the virtual column number at the current cursor position.
2384 * This is used by the IM code to obtain the start of the preedit string.
2385 */
2386 colnr_T
2387cmdline_getvcol_cursor()
2388{
2389 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2390 return MAXCOL;
2391
2392# ifdef FEAT_MBYTE
2393 if (has_mbyte)
2394 {
2395 colnr_T col;
2396 int i = 0;
2397
2398 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002399 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
2401 return col;
2402 }
2403 else
2404# endif
2405 return ccline.cmdpos;
2406}
2407#endif
2408
2409#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2410/*
2411 * If part of the command line is an IM preedit string, redraw it with
2412 * IM feedback attributes. The cursor position is restored after drawing.
2413 */
2414 static void
2415redrawcmd_preedit()
2416{
2417 if ((State & CMDLINE)
2418 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002419 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 && !p_imdisable
2421 && im_is_preediting())
2422 {
2423 int cmdpos = 0;
2424 int cmdspos;
2425 int old_row;
2426 int old_col;
2427 colnr_T col;
2428
2429 old_row = msg_row;
2430 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002431 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432
2433# ifdef FEAT_MBYTE
2434 if (has_mbyte)
2435 {
2436 for (col = 0; col < preedit_start_col
2437 && cmdpos < ccline.cmdlen; ++col)
2438 {
2439 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002440 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 }
2442 }
2443 else
2444# endif
2445 {
2446 cmdspos += preedit_start_col;
2447 cmdpos += preedit_start_col;
2448 }
2449
2450 msg_row = cmdline_row + (cmdspos / (int)Columns);
2451 msg_col = cmdspos % (int)Columns;
2452 if (msg_row >= Rows)
2453 msg_row = Rows - 1;
2454
2455 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2456 {
2457 int char_len;
2458 int char_attr;
2459
2460 char_attr = im_get_feedback_attr(col);
2461 if (char_attr < 0)
2462 break; /* end of preedit string */
2463
2464# ifdef FEAT_MBYTE
2465 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002466 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 else
2468# endif
2469 char_len = 1;
2470
2471 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2472 cmdpos += char_len;
2473 }
2474
2475 msg_row = old_row;
2476 msg_col = old_col;
2477 }
2478}
2479#endif /* FEAT_XIM && FEAT_GUI_GTK */
2480
2481/*
2482 * Allocate a new command line buffer.
2483 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2484 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2485 */
2486 static void
2487alloc_cmdbuff(len)
2488 int len;
2489{
2490 /*
2491 * give some extra space to avoid having to allocate all the time
2492 */
2493 if (len < 80)
2494 len = 100;
2495 else
2496 len += 20;
2497
2498 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2499 ccline.cmdbufflen = len;
2500}
2501
2502/*
2503 * Re-allocate the command line to length len + something extra.
2504 * return FAIL for failure, OK otherwise
2505 */
2506 static int
2507realloc_cmdbuff(len)
2508 int len;
2509{
2510 char_u *p;
2511
2512 p = ccline.cmdbuff;
2513 alloc_cmdbuff(len); /* will get some more */
2514 if (ccline.cmdbuff == NULL) /* out of memory */
2515 {
2516 ccline.cmdbuff = p; /* keep the old one */
2517 return FAIL;
2518 }
2519 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2520 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002521
2522 if (ccline.xpc != NULL
2523 && ccline.xpc->xp_pattern != NULL
2524 && ccline.xpc->xp_context != EXPAND_NOTHING
2525 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2526 {
2527 int i = ccline.xpc->xp_pattern - p;
2528
2529 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2530 * to point into the newly allocated memory. */
2531 if (i >= 0 && i <= ccline.cmdlen)
2532 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2533 }
2534
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 return OK;
2536}
2537
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002538#if defined(FEAT_ARABIC) || defined(PROTO)
2539static char_u *arshape_buf = NULL;
2540
2541# if defined(EXITFREE) || defined(PROTO)
2542 void
2543free_cmdline_buf()
2544{
2545 vim_free(arshape_buf);
2546}
2547# endif
2548#endif
2549
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550/*
2551 * Draw part of the cmdline at the current cursor position. But draw stars
2552 * when cmdline_star is TRUE.
2553 */
2554 static void
2555draw_cmdline(start, len)
2556 int start;
2557 int len;
2558{
2559#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2560 int i;
2561
2562 if (cmdline_star > 0)
2563 for (i = 0; i < len; ++i)
2564 {
2565 msg_putchar('*');
2566# ifdef FEAT_MBYTE
2567 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002568 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569# endif
2570 }
2571 else
2572#endif
2573#ifdef FEAT_ARABIC
2574 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 static int buflen = 0;
2577 char_u *p;
2578 int j;
2579 int newlen = 0;
2580 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002581 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 int prev_c = 0;
2583 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002584 int u8c;
2585 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587
2588 /*
2589 * Do arabic shaping into a temporary buffer. This is very
2590 * inefficient!
2591 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002592 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
2594 /* Re-allocate the buffer. We keep it around to avoid a lot of
2595 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002596 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002597 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002598 arshape_buf = alloc(buflen);
2599 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 return; /* out of memory */
2601 }
2602
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002603 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2604 {
2605 /* Prepend a space to draw the leading composing char on. */
2606 arshape_buf[0] = ' ';
2607 newlen = 1;
2608 }
2609
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 for (j = start; j < start + len; j += mb_l)
2611 {
2612 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002613 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002614 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (ARABIC_CHAR(u8c))
2616 {
2617 /* Do Arabic shaping. */
2618 if (cmdmsg_rl)
2619 {
2620 /* displaying from right to left */
2621 pc = prev_c;
2622 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002623 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if (j + mb_l >= start + len)
2625 nc = NUL;
2626 else
2627 nc = utf_ptr2char(p + mb_l);
2628 }
2629 else
2630 {
2631 /* displaying from left to right */
2632 if (j + mb_l >= start + len)
2633 pc = NUL;
2634 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002635 {
2636 int pcc[MAX_MCO];
2637
2638 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002640 pc1 = pcc[0];
2641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 nc = prev_c;
2643 }
2644 prev_c = u8c;
2645
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002646 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002648 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002649 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002651 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2652 if (u8cc[1] != 0)
2653 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002654 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
2656 }
2657 else
2658 {
2659 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002660 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 newlen += mb_l;
2662 }
2663 }
2664
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002665 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667 else
2668#endif
2669 msg_outtrans_len(ccline.cmdbuff + start, len);
2670}
2671
2672/*
2673 * Put a character on the command line. Shifts the following text to the
2674 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2675 * "c" must be printable (fit in one display cell)!
2676 */
2677 void
2678putcmdline(c, shift)
2679 int c;
2680 int shift;
2681{
2682 if (cmd_silent)
2683 return;
2684 msg_no_more = TRUE;
2685 msg_putchar(c);
2686 if (shift)
2687 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2688 msg_no_more = FALSE;
2689 cursorcmd();
2690}
2691
2692/*
2693 * Undo a putcmdline(c, FALSE).
2694 */
2695 void
2696unputcmdline()
2697{
2698 if (cmd_silent)
2699 return;
2700 msg_no_more = TRUE;
2701 if (ccline.cmdlen == ccline.cmdpos)
2702 msg_putchar(' ');
2703 else
2704 draw_cmdline(ccline.cmdpos, 1);
2705 msg_no_more = FALSE;
2706 cursorcmd();
2707}
2708
2709/*
2710 * Put the given string, of the given length, onto the command line.
2711 * If len is -1, then STRLEN() is used to calculate the length.
2712 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2713 * part will be redrawn, otherwise it will not. If this function is called
2714 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2715 * called afterwards.
2716 */
2717 int
2718put_on_cmdline(str, len, redraw)
2719 char_u *str;
2720 int len;
2721 int redraw;
2722{
2723 int retval;
2724 int i;
2725 int m;
2726 int c;
2727
2728 if (len < 0)
2729 len = (int)STRLEN(str);
2730
2731 /* Check if ccline.cmdbuff needs to be longer */
2732 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2733 retval = realloc_cmdbuff(ccline.cmdlen + len);
2734 else
2735 retval = OK;
2736 if (retval == OK)
2737 {
2738 if (!ccline.overstrike)
2739 {
2740 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2741 ccline.cmdbuff + ccline.cmdpos,
2742 (size_t)(ccline.cmdlen - ccline.cmdpos));
2743 ccline.cmdlen += len;
2744 }
2745 else
2746 {
2747#ifdef FEAT_MBYTE
2748 if (has_mbyte)
2749 {
2750 /* Count nr of characters in the new string. */
2751 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002752 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 ++m;
2754 /* Count nr of bytes in cmdline that are overwritten by these
2755 * characters. */
2756 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002757 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 --m;
2759 if (i < ccline.cmdlen)
2760 {
2761 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2762 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2763 ccline.cmdlen += ccline.cmdpos + len - i;
2764 }
2765 else
2766 ccline.cmdlen = ccline.cmdpos + len;
2767 }
2768 else
2769#endif
2770 if (ccline.cmdpos + len > ccline.cmdlen)
2771 ccline.cmdlen = ccline.cmdpos + len;
2772 }
2773 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2774 ccline.cmdbuff[ccline.cmdlen] = NUL;
2775
2776#ifdef FEAT_MBYTE
2777 if (enc_utf8)
2778 {
2779 /* When the inserted text starts with a composing character,
2780 * backup to the character before it. There could be two of them.
2781 */
2782 i = 0;
2783 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2784 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2785 {
2786 i = (*mb_head_off)(ccline.cmdbuff,
2787 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2788 ccline.cmdpos -= i;
2789 len += i;
2790 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2791 }
2792# ifdef FEAT_ARABIC
2793 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2794 {
2795 /* Check the previous character for Arabic combining pair. */
2796 i = (*mb_head_off)(ccline.cmdbuff,
2797 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2798 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2799 + ccline.cmdpos - i), c))
2800 {
2801 ccline.cmdpos -= i;
2802 len += i;
2803 }
2804 else
2805 i = 0;
2806 }
2807# endif
2808 if (i != 0)
2809 {
2810 /* Also backup the cursor position. */
2811 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2812 ccline.cmdspos -= i;
2813 msg_col -= i;
2814 if (msg_col < 0)
2815 {
2816 msg_col += Columns;
2817 --msg_row;
2818 }
2819 }
2820 }
2821#endif
2822
2823 if (redraw && !cmd_silent)
2824 {
2825 msg_no_more = TRUE;
2826 i = cmdline_row;
2827 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2828 /* Avoid clearing the rest of the line too often. */
2829 if (cmdline_row != i || ccline.overstrike)
2830 msg_clr_eos();
2831 msg_no_more = FALSE;
2832 }
2833#ifdef FEAT_FKMAP
2834 /*
2835 * If we are in Farsi command mode, the character input must be in
2836 * Insert mode. So do not advance the cmdpos.
2837 */
2838 if (!cmd_fkmap)
2839#endif
2840 {
2841 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002844 if (m < 0) /* overflow, Columns or Rows at weird value */
2845 m = MAXCOL;
2846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 else
2848 m = MAXCOL;
2849 for (i = 0; i < len; ++i)
2850 {
2851 c = cmdline_charsize(ccline.cmdpos);
2852#ifdef FEAT_MBYTE
2853 /* count ">" for a double-wide char that doesn't fit. */
2854 if (has_mbyte)
2855 correct_cmdspos(ccline.cmdpos, c);
2856#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002857 /* Stop cursor at the end of the screen, but do increment the
2858 * insert position, so that entering a very long command
2859 * works, even though you can't see it. */
2860 if (ccline.cmdspos + c < m)
2861 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862#ifdef FEAT_MBYTE
2863 if (has_mbyte)
2864 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002865 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 if (c > len - i - 1)
2867 c = len - i - 1;
2868 ccline.cmdpos += c;
2869 i += c;
2870 }
2871#endif
2872 ++ccline.cmdpos;
2873 }
2874 }
2875 }
2876 if (redraw)
2877 msg_check();
2878 return retval;
2879}
2880
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002881static struct cmdline_info prev_ccline;
2882static int prev_ccline_used = FALSE;
2883
2884/*
2885 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2886 * and overwrite it. But get_cmdline_str() may need it, thus make it
2887 * available globally in prev_ccline.
2888 */
2889 static void
2890save_cmdline(ccp)
2891 struct cmdline_info *ccp;
2892{
2893 if (!prev_ccline_used)
2894 {
2895 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2896 prev_ccline_used = TRUE;
2897 }
2898 *ccp = prev_ccline;
2899 prev_ccline = ccline;
2900 ccline.cmdbuff = NULL;
2901 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002902 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002903}
2904
2905/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002906 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002907 */
2908 static void
2909restore_cmdline(ccp)
2910 struct cmdline_info *ccp;
2911{
2912 ccline = prev_ccline;
2913 prev_ccline = *ccp;
2914}
2915
Bram Moolenaar5a305422006-04-28 22:38:25 +00002916#if defined(FEAT_EVAL) || defined(PROTO)
2917/*
2918 * Save the command line into allocated memory. Returns a pointer to be
2919 * passed to restore_cmdline_alloc() later.
2920 * Returns NULL when failed.
2921 */
2922 char_u *
2923save_cmdline_alloc()
2924{
2925 struct cmdline_info *p;
2926
2927 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2928 if (p != NULL)
2929 save_cmdline(p);
2930 return (char_u *)p;
2931}
2932
2933/*
2934 * Restore the command line from the return value of save_cmdline_alloc().
2935 */
2936 void
2937restore_cmdline_alloc(p)
2938 char_u *p;
2939{
2940 if (p != NULL)
2941 {
2942 restore_cmdline((struct cmdline_info *)p);
2943 vim_free(p);
2944 }
2945}
2946#endif
2947
Bram Moolenaar8299df92004-07-10 09:47:34 +00002948/*
2949 * paste a yank register into the command line.
2950 * used by CTRL-R command in command-line mode
2951 * insert_reg() can't be used here, because special characters from the
2952 * register contents will be interpreted as commands.
2953 *
2954 * return FAIL for failure, OK otherwise
2955 */
2956 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002957cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00002958 int regname;
2959 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002960 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00002961{
2962 long i;
2963 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002964 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002965 int allocated;
2966 struct cmdline_info save_ccline;
2967
2968 /* check for valid regname; also accept special characters for CTRL-R in
2969 * the command line */
2970 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2971 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2972 return FAIL;
2973
2974 /* A register containing CTRL-R can cause an endless loop. Allow using
2975 * CTRL-C to break the loop. */
2976 line_breakcheck();
2977 if (got_int)
2978 return FAIL;
2979
2980#ifdef FEAT_CLIPBOARD
2981 regname = may_get_selection(regname);
2982#endif
2983
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002984 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002985 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002986 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002987 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002988 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002989 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002990 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002991
2992 if (i)
2993 {
2994 /* Got the value of a special register in "arg". */
2995 if (arg == NULL)
2996 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002997
2998 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
2999 * part of the word. */
3000 p = arg;
3001 if (p_is && regname == Ctrl_W)
3002 {
3003 char_u *w;
3004 int len;
3005
3006 /* Locate start of last word in the cmd buffer. */
3007 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
3008 {
3009#ifdef FEAT_MBYTE
3010 if (has_mbyte)
3011 {
3012 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3013 if (!vim_iswordc(mb_ptr2char(w - len)))
3014 break;
3015 w -= len;
3016 }
3017 else
3018#endif
3019 {
3020 if (!vim_iswordc(w[-1]))
3021 break;
3022 --w;
3023 }
3024 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003025 len = (int)((ccline.cmdbuff + ccline.cmdlen) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003026 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3027 p += len;
3028 }
3029
3030 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003031 if (allocated)
3032 vim_free(arg);
3033 return OK;
3034 }
3035
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003036 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003037}
3038
3039/*
3040 * Put a string on the command line.
3041 * When "literally" is TRUE, insert literally.
3042 * When "literally" is FALSE, insert as typed, but don't leave the command
3043 * line.
3044 */
3045 void
3046cmdline_paste_str(s, literally)
3047 char_u *s;
3048 int literally;
3049{
3050 int c, cv;
3051
3052 if (literally)
3053 put_on_cmdline(s, -1, TRUE);
3054 else
3055 while (*s != NUL)
3056 {
3057 cv = *s;
3058 if (cv == Ctrl_V && s[1])
3059 ++s;
3060#ifdef FEAT_MBYTE
3061 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003062 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003063 else
3064#endif
3065 c = *s++;
3066 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3067#ifdef UNIX
3068 || c == intr_char
3069#endif
3070 || (c == Ctrl_BSL && *s == Ctrl_N))
3071 stuffcharReadbuff(Ctrl_V);
3072 stuffcharReadbuff(c);
3073 }
3074}
3075
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076#ifdef FEAT_WILDMENU
3077/*
3078 * Delete characters on the command line, from "from" to the current
3079 * position.
3080 */
3081 static void
3082cmdline_del(from)
3083 int from;
3084{
3085 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3086 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3087 ccline.cmdlen -= ccline.cmdpos - from;
3088 ccline.cmdpos = from;
3089}
3090#endif
3091
3092/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003093 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 * search
3095 */
3096 void
3097redrawcmdline()
3098{
3099 if (cmd_silent)
3100 return;
3101 need_wait_return = FALSE;
3102 compute_cmdrow();
3103 redrawcmd();
3104 cursorcmd();
3105}
3106
3107 static void
3108redrawcmdprompt()
3109{
3110 int i;
3111
3112 if (cmd_silent)
3113 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003114 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 msg_putchar(ccline.cmdfirstc);
3116 if (ccline.cmdprompt != NULL)
3117 {
3118 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3119 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3120 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003121 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 --ccline.cmdindent;
3123 }
3124 else
3125 for (i = ccline.cmdindent; i > 0; --i)
3126 msg_putchar(' ');
3127}
3128
3129/*
3130 * Redraw what is currently on the command line.
3131 */
3132 void
3133redrawcmd()
3134{
3135 if (cmd_silent)
3136 return;
3137
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003138 /* when 'incsearch' is set there may be no command line while redrawing */
3139 if (ccline.cmdbuff == NULL)
3140 {
3141 windgoto(cmdline_row, 0);
3142 msg_clr_eos();
3143 return;
3144 }
3145
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 msg_start();
3147 redrawcmdprompt();
3148
3149 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3150 msg_no_more = TRUE;
3151 draw_cmdline(0, ccline.cmdlen);
3152 msg_clr_eos();
3153 msg_no_more = FALSE;
3154
3155 set_cmdspos_cursor();
3156
3157 /*
3158 * An emsg() before may have set msg_scroll. This is used in normal mode,
3159 * in cmdline mode we can reset them now.
3160 */
3161 msg_scroll = FALSE; /* next message overwrites cmdline */
3162
3163 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3164 * in cmdline mode */
3165 skip_redraw = FALSE;
3166}
3167
3168 void
3169compute_cmdrow()
3170{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003171 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 cmdline_row = Rows - 1;
3173 else
3174 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3175 + W_STATUS_HEIGHT(lastwin);
3176}
3177
3178 static void
3179cursorcmd()
3180{
3181 if (cmd_silent)
3182 return;
3183
3184#ifdef FEAT_RIGHTLEFT
3185 if (cmdmsg_rl)
3186 {
3187 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3188 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3189 if (msg_row <= 0)
3190 msg_row = Rows - 1;
3191 }
3192 else
3193#endif
3194 {
3195 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3196 msg_col = ccline.cmdspos % (int)Columns;
3197 if (msg_row >= Rows)
3198 msg_row = Rows - 1;
3199 }
3200
3201 windgoto(msg_row, msg_col);
3202#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3203 redrawcmd_preedit();
3204#endif
3205#ifdef MCH_CURSOR_SHAPE
3206 mch_update_cursor();
3207#endif
3208}
3209
3210 void
3211gotocmdline(clr)
3212 int clr;
3213{
3214 msg_start();
3215#ifdef FEAT_RIGHTLEFT
3216 if (cmdmsg_rl)
3217 msg_col = Columns - 1;
3218 else
3219#endif
3220 msg_col = 0; /* always start in column 0 */
3221 if (clr) /* clear the bottom line(s) */
3222 msg_clr_eos(); /* will reset clear_cmdline */
3223 windgoto(cmdline_row, 0);
3224}
3225
3226/*
3227 * Check the word in front of the cursor for an abbreviation.
3228 * Called when the non-id character "c" has been entered.
3229 * When an abbreviation is recognized it is removed from the text with
3230 * backspaces and the replacement string is inserted, followed by "c".
3231 */
3232 static int
3233ccheck_abbr(c)
3234 int c;
3235{
3236 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3237 return FALSE;
3238
3239 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3240}
3241
3242/*
3243 * Return FAIL if this is not an appropriate context in which to do
3244 * completion of anything, return OK if it is (even if there are no matches).
3245 * For the caller, this means that the character is just passed through like a
3246 * normal character (instead of being expanded). This allows :s/^I^D etc.
3247 */
3248 static int
3249nextwild(xp, type, options)
3250 expand_T *xp;
3251 int type;
3252 int options; /* extra options for ExpandOne() */
3253{
3254 int i, j;
3255 char_u *p1;
3256 char_u *p2;
3257 int oldlen;
3258 int difflen;
3259 int v;
3260
3261 if (xp->xp_numfiles == -1)
3262 {
3263 set_expand_context(xp);
3264 cmd_showtail = expand_showtail(xp);
3265 }
3266
3267 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3268 {
3269 beep_flush();
3270 return OK; /* Something illegal on command line */
3271 }
3272 if (xp->xp_context == EXPAND_NOTHING)
3273 {
3274 /* Caller can use the character as a normal char instead */
3275 return FAIL;
3276 }
3277
3278 MSG_PUTS("..."); /* show that we are busy */
3279 out_flush();
3280
3281 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3282 oldlen = ccline.cmdpos - i;
3283
3284 if (type == WILD_NEXT || type == WILD_PREV)
3285 {
3286 /*
3287 * Get next/previous match for a previous expanded pattern.
3288 */
3289 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3290 }
3291 else
3292 {
3293 /*
3294 * Translate string into pattern and expand it.
3295 */
3296 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3297 p2 = NULL;
3298 else
3299 {
3300 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3301 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3302 |options, type);
3303 vim_free(p1);
3304 /* longest match: make sure it is not shorter (happens with :help */
3305 if (p2 != NULL && type == WILD_LONGEST)
3306 {
3307 for (j = 0; j < oldlen; ++j)
3308 if (ccline.cmdbuff[i + j] == '*'
3309 || ccline.cmdbuff[i + j] == '?')
3310 break;
3311 if ((int)STRLEN(p2) < j)
3312 {
3313 vim_free(p2);
3314 p2 = NULL;
3315 }
3316 }
3317 }
3318 }
3319
3320 if (p2 != NULL && !got_int)
3321 {
3322 difflen = (int)STRLEN(p2) - oldlen;
3323 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3324 {
3325 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3326 xp->xp_pattern = ccline.cmdbuff + i;
3327 }
3328 else
3329 v = OK;
3330 if (v == OK)
3331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003332 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3333 &ccline.cmdbuff[ccline.cmdpos],
3334 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3335 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 ccline.cmdlen += difflen;
3337 ccline.cmdpos += difflen;
3338 }
3339 }
3340 vim_free(p2);
3341
3342 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003343 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
3345 /* When expanding a ":map" command and no matches are found, assume that
3346 * the key is supposed to be inserted literally */
3347 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3348 return FAIL;
3349
3350 if (xp->xp_numfiles <= 0 && p2 == NULL)
3351 beep_flush();
3352 else if (xp->xp_numfiles == 1)
3353 /* free expanded pattern */
3354 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3355
3356 return OK;
3357}
3358
3359/*
3360 * Do wildcard expansion on the string 'str'.
3361 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003362 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 * Return NULL for failure.
3364 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003365 * "orig" is the originally expanded string, copied to allocated memory. It
3366 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3367 * WILD_PREV "orig" should be NULL.
3368 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003369 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3370 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 *
3372 * mode = WILD_FREE: just free previously expanded matches
3373 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3374 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3375 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3376 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3377 * mode = WILD_ALL: return all matches concatenated
3378 * mode = WILD_LONGEST: return longest matched part
3379 *
3380 * options = WILD_LIST_NOTFOUND: list entries without a match
3381 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3382 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3383 * options = WILD_NO_BEEP: Don't beep for multiple matches
3384 * options = WILD_ADD_SLASH: add a slash after directory names
3385 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3386 * options = WILD_SILENT: don't print warning messages
3387 * options = WILD_ESCAPE: put backslash before special chars
3388 *
3389 * The variables xp->xp_context and xp->xp_backslash must have been set!
3390 */
3391 char_u *
3392ExpandOne(xp, str, orig, options, mode)
3393 expand_T *xp;
3394 char_u *str;
3395 char_u *orig; /* allocated copy of original of expanded string */
3396 int options;
3397 int mode;
3398{
3399 char_u *ss = NULL;
3400 static int findex;
3401 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003402 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 int i;
3404 long_u len;
3405 int non_suf_match; /* number without matching suffix */
3406
3407 /*
3408 * first handle the case of using an old match
3409 */
3410 if (mode == WILD_NEXT || mode == WILD_PREV)
3411 {
3412 if (xp->xp_numfiles > 0)
3413 {
3414 if (mode == WILD_PREV)
3415 {
3416 if (findex == -1)
3417 findex = xp->xp_numfiles;
3418 --findex;
3419 }
3420 else /* mode == WILD_NEXT */
3421 ++findex;
3422
3423 /*
3424 * When wrapping around, return the original string, set findex to
3425 * -1.
3426 */
3427 if (findex < 0)
3428 {
3429 if (orig_save == NULL)
3430 findex = xp->xp_numfiles - 1;
3431 else
3432 findex = -1;
3433 }
3434 if (findex >= xp->xp_numfiles)
3435 {
3436 if (orig_save == NULL)
3437 findex = 0;
3438 else
3439 findex = -1;
3440 }
3441#ifdef FEAT_WILDMENU
3442 if (p_wmnu)
3443 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3444 findex, cmd_showtail);
3445#endif
3446 if (findex == -1)
3447 return vim_strsave(orig_save);
3448 return vim_strsave(xp->xp_files[findex]);
3449 }
3450 else
3451 return NULL;
3452 }
3453
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003454 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3456 {
3457 FreeWild(xp->xp_numfiles, xp->xp_files);
3458 xp->xp_numfiles = -1;
3459 vim_free(orig_save);
3460 orig_save = NULL;
3461 }
3462 findex = 0;
3463
3464 if (mode == WILD_FREE) /* only release file name */
3465 return NULL;
3466
3467 if (xp->xp_numfiles == -1)
3468 {
3469 vim_free(orig_save);
3470 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003471 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
3473 /*
3474 * Do the expansion.
3475 */
3476 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3477 options) == FAIL)
3478 {
3479#ifdef FNAME_ILLEGAL
3480 /* Illegal file name has been silently skipped. But when there
3481 * are wildcards, the real problem is that there was no match,
3482 * causing the pattern to be added, which has illegal characters.
3483 */
3484 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3485 EMSG2(_(e_nomatch2), str);
3486#endif
3487 }
3488 else if (xp->xp_numfiles == 0)
3489 {
3490 if (!(options & WILD_SILENT))
3491 EMSG2(_(e_nomatch2), str);
3492 }
3493 else
3494 {
3495 /* Escape the matches for use on the command line. */
3496 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3497
3498 /*
3499 * Check for matching suffixes in file names.
3500 */
3501 if (mode != WILD_ALL && mode != WILD_LONGEST)
3502 {
3503 if (xp->xp_numfiles)
3504 non_suf_match = xp->xp_numfiles;
3505 else
3506 non_suf_match = 1;
3507 if ((xp->xp_context == EXPAND_FILES
3508 || xp->xp_context == EXPAND_DIRECTORIES)
3509 && xp->xp_numfiles > 1)
3510 {
3511 /*
3512 * More than one match; check suffix.
3513 * The files will have been sorted on matching suffix in
3514 * expand_wildcards, only need to check the first two.
3515 */
3516 non_suf_match = 0;
3517 for (i = 0; i < 2; ++i)
3518 if (match_suffix(xp->xp_files[i]))
3519 ++non_suf_match;
3520 }
3521 if (non_suf_match != 1)
3522 {
3523 /* Can we ever get here unless it's while expanding
3524 * interactively? If not, we can get rid of this all
3525 * together. Don't really want to wait for this message
3526 * (and possibly have to hit return to continue!).
3527 */
3528 if (!(options & WILD_SILENT))
3529 EMSG(_(e_toomany));
3530 else if (!(options & WILD_NO_BEEP))
3531 beep_flush();
3532 }
3533 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3534 ss = vim_strsave(xp->xp_files[0]);
3535 }
3536 }
3537 }
3538
3539 /* Find longest common part */
3540 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3541 {
3542 for (len = 0; xp->xp_files[0][len]; ++len)
3543 {
3544 for (i = 0; i < xp->xp_numfiles; ++i)
3545 {
3546#ifdef CASE_INSENSITIVE_FILENAME
3547 if (xp->xp_context == EXPAND_DIRECTORIES
3548 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003549 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 || xp->xp_context == EXPAND_BUFFERS)
3551 {
3552 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3553 TOLOWER_LOC(xp->xp_files[0][len]))
3554 break;
3555 }
3556 else
3557#endif
3558 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3559 break;
3560 }
3561 if (i < xp->xp_numfiles)
3562 {
3563 if (!(options & WILD_NO_BEEP))
3564 vim_beep();
3565 break;
3566 }
3567 }
3568 ss = alloc((unsigned)len + 1);
3569 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003570 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 findex = -1; /* next p_wc gets first one */
3572 }
3573
3574 /* Concatenate all matching names */
3575 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3576 {
3577 len = 0;
3578 for (i = 0; i < xp->xp_numfiles; ++i)
3579 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3580 ss = lalloc(len, TRUE);
3581 if (ss != NULL)
3582 {
3583 *ss = NUL;
3584 for (i = 0; i < xp->xp_numfiles; ++i)
3585 {
3586 STRCAT(ss, xp->xp_files[i]);
3587 if (i != xp->xp_numfiles - 1)
3588 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3589 }
3590 }
3591 }
3592
3593 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3594 ExpandCleanup(xp);
3595
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003596 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003597 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003598 vim_free(orig);
3599
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 return ss;
3601}
3602
3603/*
3604 * Prepare an expand structure for use.
3605 */
3606 void
3607ExpandInit(xp)
3608 expand_T *xp;
3609{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003610 xp->xp_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003612#ifndef BACKSLASH_IN_FILENAME
3613 xp->xp_shell = FALSE;
3614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 xp->xp_numfiles = -1;
3616 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003617#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3618 xp->xp_arg = NULL;
3619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620}
3621
3622/*
3623 * Cleanup an expand structure after use.
3624 */
3625 void
3626ExpandCleanup(xp)
3627 expand_T *xp;
3628{
3629 if (xp->xp_numfiles >= 0)
3630 {
3631 FreeWild(xp->xp_numfiles, xp->xp_files);
3632 xp->xp_numfiles = -1;
3633 }
3634}
3635
3636 void
3637ExpandEscape(xp, str, numfiles, files, options)
3638 expand_T *xp;
3639 char_u *str;
3640 int numfiles;
3641 char_u **files;
3642 int options;
3643{
3644 int i;
3645 char_u *p;
3646
3647 /*
3648 * May change home directory back to "~"
3649 */
3650 if (options & WILD_HOME_REPLACE)
3651 tilde_replace(str, numfiles, files);
3652
3653 if (options & WILD_ESCAPE)
3654 {
3655 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003656 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 || xp->xp_context == EXPAND_BUFFERS
3658 || xp->xp_context == EXPAND_DIRECTORIES)
3659 {
3660 /*
3661 * Insert a backslash into a file name before a space, \, %, #
3662 * and wildmatch characters, except '~'.
3663 */
3664 for (i = 0; i < numfiles; ++i)
3665 {
3666 /* for ":set path=" we need to escape spaces twice */
3667 if (xp->xp_backslash == XP_BS_THREE)
3668 {
3669 p = vim_strsave_escaped(files[i], (char_u *)" ");
3670 if (p != NULL)
3671 {
3672 vim_free(files[i]);
3673 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003674#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 p = vim_strsave_escaped(files[i], (char_u *)" ");
3676 if (p != NULL)
3677 {
3678 vim_free(files[i]);
3679 files[i] = p;
3680 }
3681#endif
3682 }
3683 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003684#ifdef BACKSLASH_IN_FILENAME
3685 p = vim_strsave_fnameescape(files[i], FALSE);
3686#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003687 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 if (p != NULL)
3690 {
3691 vim_free(files[i]);
3692 files[i] = p;
3693 }
3694
3695 /* If 'str' starts with "\~", replace "~" at start of
3696 * files[i] with "\~". */
3697 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003698 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003701
3702 /* If the first file starts with a '+' escape it. Otherwise it
3703 * could be seen as "+cmd". */
3704 if (*files[0] == '+')
3705 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 }
3707 else if (xp->xp_context == EXPAND_TAGS)
3708 {
3709 /*
3710 * Insert a backslash before characters in a tag name that
3711 * would terminate the ":tag" command.
3712 */
3713 for (i = 0; i < numfiles; ++i)
3714 {
3715 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3716 if (p != NULL)
3717 {
3718 vim_free(files[i]);
3719 files[i] = p;
3720 }
3721 }
3722 }
3723 }
3724}
3725
3726/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003727 * Escape special characters in "fname" for when used as a file name argument
3728 * after a Vim command, or, when "shell" is non-zero, a shell command.
3729 * Returns the result in allocated memory.
3730 */
3731 char_u *
3732vim_strsave_fnameescape(fname, shell)
3733 char_u *fname;
3734 int shell;
3735{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003736 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003737#ifdef BACKSLASH_IN_FILENAME
3738 char_u buf[20];
3739 int j = 0;
3740
3741 /* Don't escape '[' and '{' if they are in 'isfname'. */
3742 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3743 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3744 buf[j++] = *p;
3745 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003746 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003747#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003748 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3749 if (shell && csh_like_shell() && p != NULL)
3750 {
3751 char_u *s;
3752
3753 /* For csh and similar shells need to put two backslashes before '!'.
3754 * One is taken by Vim, one by the shell. */
3755 s = vim_strsave_escaped(p, (char_u *)"!");
3756 vim_free(p);
3757 p = s;
3758 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003759#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003760
3761 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3762 * ":write". "cd -" has a special meaning. */
3763 if (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))
3764 escape_fname(&p);
3765
3766 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003767}
3768
3769/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003770 * Put a backslash before the file name in "pp", which is in allocated memory.
3771 */
3772 static void
3773escape_fname(pp)
3774 char_u **pp;
3775{
3776 char_u *p;
3777
3778 p = alloc((unsigned)(STRLEN(*pp) + 2));
3779 if (p != NULL)
3780 {
3781 p[0] = '\\';
3782 STRCPY(p + 1, *pp);
3783 vim_free(*pp);
3784 *pp = p;
3785 }
3786}
3787
3788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 * For each file name in files[num_files]:
3790 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3791 */
3792 void
3793tilde_replace(orig_pat, num_files, files)
3794 char_u *orig_pat;
3795 int num_files;
3796 char_u **files;
3797{
3798 int i;
3799 char_u *p;
3800
3801 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3802 {
3803 for (i = 0; i < num_files; ++i)
3804 {
3805 p = home_replace_save(NULL, files[i]);
3806 if (p != NULL)
3807 {
3808 vim_free(files[i]);
3809 files[i] = p;
3810 }
3811 }
3812 }
3813}
3814
3815/*
3816 * Show all matches for completion on the command line.
3817 * Returns EXPAND_NOTHING when the character that triggered expansion should
3818 * be inserted like a normal character.
3819 */
3820/*ARGSUSED*/
3821 static int
3822showmatches(xp, wildmenu)
3823 expand_T *xp;
3824 int wildmenu;
3825{
3826#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3827 int num_files;
3828 char_u **files_found;
3829 int i, j, k;
3830 int maxlen;
3831 int lines;
3832 int columns;
3833 char_u *p;
3834 int lastlen;
3835 int attr;
3836 int showtail;
3837
3838 if (xp->xp_numfiles == -1)
3839 {
3840 set_expand_context(xp);
3841 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3842 &num_files, &files_found);
3843 showtail = expand_showtail(xp);
3844 if (i != EXPAND_OK)
3845 return i;
3846
3847 }
3848 else
3849 {
3850 num_files = xp->xp_numfiles;
3851 files_found = xp->xp_files;
3852 showtail = cmd_showtail;
3853 }
3854
3855#ifdef FEAT_WILDMENU
3856 if (!wildmenu)
3857 {
3858#endif
3859 msg_didany = FALSE; /* lines_left will be set */
3860 msg_start(); /* prepare for paging */
3861 msg_putchar('\n');
3862 out_flush();
3863 cmdline_row = msg_row;
3864 msg_didany = FALSE; /* lines_left will be set again */
3865 msg_start(); /* prepare for paging */
3866#ifdef FEAT_WILDMENU
3867 }
3868#endif
3869
3870 if (got_int)
3871 got_int = FALSE; /* only int. the completion, not the cmd line */
3872#ifdef FEAT_WILDMENU
3873 else if (wildmenu)
3874 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3875#endif
3876 else
3877 {
3878 /* find the length of the longest file name */
3879 maxlen = 0;
3880 for (i = 0; i < num_files; ++i)
3881 {
3882 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003883 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 || xp->xp_context == EXPAND_BUFFERS))
3885 {
3886 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3887 j = vim_strsize(NameBuff);
3888 }
3889 else
3890 j = vim_strsize(L_SHOWFILE(i));
3891 if (j > maxlen)
3892 maxlen = j;
3893 }
3894
3895 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3896 lines = num_files;
3897 else
3898 {
3899 /* compute the number of columns and lines for the listing */
3900 maxlen += 2; /* two spaces between file names */
3901 columns = ((int)Columns + 2) / maxlen;
3902 if (columns < 1)
3903 columns = 1;
3904 lines = (num_files + columns - 1) / columns;
3905 }
3906
3907 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3908
3909 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3910 {
3911 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3912 msg_clr_eos();
3913 msg_advance(maxlen - 3);
3914 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3915 }
3916
3917 /* list the files line by line */
3918 for (i = 0; i < lines; ++i)
3919 {
3920 lastlen = 999;
3921 for (k = i; k < num_files; k += lines)
3922 {
3923 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3924 {
3925 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3926 p = files_found[k] + STRLEN(files_found[k]) + 1;
3927 msg_advance(maxlen + 1);
3928 msg_puts(p);
3929 msg_advance(maxlen + 3);
3930 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3931 break;
3932 }
3933 for (j = maxlen - lastlen; --j >= 0; )
3934 msg_putchar(' ');
3935 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003936 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 || xp->xp_context == EXPAND_BUFFERS)
3938 {
3939 /* highlight directories */
3940 j = (mch_isdir(files_found[k]));
3941 if (showtail)
3942 p = L_SHOWFILE(k);
3943 else
3944 {
3945 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3946 TRUE);
3947 p = NameBuff;
3948 }
3949 }
3950 else
3951 {
3952 j = FALSE;
3953 p = L_SHOWFILE(k);
3954 }
3955 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3956 }
3957 if (msg_col > 0) /* when not wrapped around */
3958 {
3959 msg_clr_eos();
3960 msg_putchar('\n');
3961 }
3962 out_flush(); /* show one line at a time */
3963 if (got_int)
3964 {
3965 got_int = FALSE;
3966 break;
3967 }
3968 }
3969
3970 /*
3971 * we redraw the command below the lines that we have just listed
3972 * This is a bit tricky, but it saves a lot of screen updating.
3973 */
3974 cmdline_row = msg_row; /* will put it back later */
3975 }
3976
3977 if (xp->xp_numfiles == -1)
3978 FreeWild(num_files, files_found);
3979
3980 return EXPAND_OK;
3981}
3982
3983/*
3984 * Private gettail for showmatches() (and win_redr_status_matches()):
3985 * Find tail of file name path, but ignore trailing "/".
3986 */
3987 char_u *
3988sm_gettail(s)
3989 char_u *s;
3990{
3991 char_u *p;
3992 char_u *t = s;
3993 int had_sep = FALSE;
3994
3995 for (p = s; *p != NUL; )
3996 {
3997 if (vim_ispathsep(*p)
3998#ifdef BACKSLASH_IN_FILENAME
3999 && !rem_backslash(p)
4000#endif
4001 )
4002 had_sep = TRUE;
4003 else if (had_sep)
4004 {
4005 t = p;
4006 had_sep = FALSE;
4007 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004008 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
4010 return t;
4011}
4012
4013/*
4014 * Return TRUE if we only need to show the tail of completion matches.
4015 * When not completing file names or there is a wildcard in the path FALSE is
4016 * returned.
4017 */
4018 static int
4019expand_showtail(xp)
4020 expand_T *xp;
4021{
4022 char_u *s;
4023 char_u *end;
4024
4025 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004026 if (xp->xp_context != EXPAND_FILES
4027 && xp->xp_context != EXPAND_SHELLCMD
4028 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 return FALSE;
4030
4031 end = gettail(xp->xp_pattern);
4032 if (end == xp->xp_pattern) /* there is no path separator */
4033 return FALSE;
4034
4035 for (s = xp->xp_pattern; s < end; s++)
4036 {
4037 /* Skip escaped wildcards. Only when the backslash is not a path
4038 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4039 if (rem_backslash(s))
4040 ++s;
4041 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4042 return FALSE;
4043 }
4044 return TRUE;
4045}
4046
4047/*
4048 * Prepare a string for expansion.
4049 * When expanding file names: The string will be used with expand_wildcards().
4050 * Copy the file name into allocated memory and add a '*' at the end.
4051 * When expanding other names: The string will be used with regcomp(). Copy
4052 * the name into allocated memory and prepend "^".
4053 */
4054 char_u *
4055addstar(fname, len, context)
4056 char_u *fname;
4057 int len;
4058 int context; /* EXPAND_FILES etc. */
4059{
4060 char_u *retval;
4061 int i, j;
4062 int new_len;
4063 char_u *tail;
4064
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004065 if (context != EXPAND_FILES
4066 && context != EXPAND_SHELLCMD
4067 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
4069 /*
4070 * Matching will be done internally (on something other than files).
4071 * So we convert the file-matching-type wildcards into our kind for
4072 * use with vim_regcomp(). First work out how long it will be:
4073 */
4074
4075 /* For help tags the translation is done in find_help_tags().
4076 * For a tag pattern starting with "/" no translation is needed. */
4077 if (context == EXPAND_HELP
4078 || context == EXPAND_COLORS
4079 || context == EXPAND_COMPILER
4080 || (context == EXPAND_TAGS && fname[0] == '/'))
4081 retval = vim_strnsave(fname, len);
4082 else
4083 {
4084 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4085 for (i = 0; i < len; i++)
4086 {
4087 if (fname[i] == '*' || fname[i] == '~')
4088 new_len++; /* '*' needs to be replaced by ".*"
4089 '~' needs to be replaced by "\~" */
4090
4091 /* Buffer names are like file names. "." should be literal */
4092 if (context == EXPAND_BUFFERS && fname[i] == '.')
4093 new_len++; /* "." becomes "\." */
4094
4095 /* Custom expansion takes care of special things, match
4096 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004097 if ((context == EXPAND_USER_DEFINED
4098 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 new_len++; /* '\' becomes "\\" */
4100 }
4101 retval = alloc(new_len);
4102 if (retval != NULL)
4103 {
4104 retval[0] = '^';
4105 j = 1;
4106 for (i = 0; i < len; i++, j++)
4107 {
4108 /* Skip backslash. But why? At least keep it for custom
4109 * expansion. */
4110 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004111 && context != EXPAND_USER_LIST
4112 && fname[i] == '\\'
4113 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 break;
4115
4116 switch (fname[i])
4117 {
4118 case '*': retval[j++] = '.';
4119 break;
4120 case '~': retval[j++] = '\\';
4121 break;
4122 case '?': retval[j] = '.';
4123 continue;
4124 case '.': if (context == EXPAND_BUFFERS)
4125 retval[j++] = '\\';
4126 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004127 case '\\': if (context == EXPAND_USER_DEFINED
4128 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 retval[j++] = '\\';
4130 break;
4131 }
4132 retval[j] = fname[i];
4133 }
4134 retval[j] = NUL;
4135 }
4136 }
4137 }
4138 else
4139 {
4140 retval = alloc(len + 4);
4141 if (retval != NULL)
4142 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004143 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004146 * Don't add a star to *, ~, ~user, $var or `cmd`.
4147 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 * ~ would be at the start of the file name, but not the tail.
4149 * $ could be anywhere in the tail.
4150 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004151 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 */
4153 tail = gettail(retval);
4154 if ((*retval != '~' || tail != retval)
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004155 && (len == 0 || retval[len - 1] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 && vim_strchr(tail, '$') == NULL
4157 && vim_strchr(retval, '`') == NULL)
4158 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004159 else if (len > 0 && retval[len - 1] == '$')
4160 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 retval[len] = NUL;
4162 }
4163 }
4164 return retval;
4165}
4166
4167/*
4168 * Must parse the command line so far to work out what context we are in.
4169 * Completion can then be done based on that context.
4170 * This routine sets the variables:
4171 * xp->xp_pattern The start of the pattern to be expanded within
4172 * the command line (ends at the cursor).
4173 * xp->xp_context The type of thing to expand. Will be one of:
4174 *
4175 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4176 * the command line, like an unknown command. Caller
4177 * should beep.
4178 * EXPAND_NOTHING Unrecognised context for completion, use char like
4179 * a normal char, rather than for completion. eg
4180 * :s/^I/
4181 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4182 * it.
4183 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4184 * EXPAND_FILES After command with XFILE set, or after setting
4185 * with P_EXPAND set. eg :e ^I, :w>>^I
4186 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4187 * when we know only directories are of interest. eg
4188 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004189 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4191 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4192 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4193 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4194 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4195 * EXPAND_EVENTS Complete event names
4196 * EXPAND_SYNTAX Complete :syntax command arguments
4197 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4198 * EXPAND_AUGROUP Complete autocommand group names
4199 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4200 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4201 * eg :unmap a^I , :cunab x^I
4202 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4203 * eg :call sub^I
4204 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4205 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4206 * names in expressions, eg :while s^I
4207 * EXPAND_ENV_VARS Complete environment variable names
4208 */
4209 static void
4210set_expand_context(xp)
4211 expand_T *xp;
4212{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004213 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 if (ccline.cmdfirstc != ':'
4215#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004216 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004217 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218#endif
4219 )
4220 {
4221 xp->xp_context = EXPAND_NOTHING;
4222 return;
4223 }
4224 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4225}
4226
4227 void
4228set_cmd_context(xp, str, len, col)
4229 expand_T *xp;
4230 char_u *str; /* start of command line */
4231 int len; /* length of command line (excl. NUL) */
4232 int col; /* position of cursor */
4233{
4234 int old_char = NUL;
4235 char_u *nextcomm;
4236
4237 /*
4238 * Avoid a UMR warning from Purify, only save the character if it has been
4239 * written before.
4240 */
4241 if (col < len)
4242 old_char = str[col];
4243 str[col] = NUL;
4244 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004245
4246#ifdef FEAT_EVAL
4247 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004248 {
4249# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004250 /* pass CMD_SIZE because there is no real command */
4251 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004252# endif
4253 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004254 else if (ccline.input_fn)
4255 {
4256 xp->xp_context = ccline.xp_context;
4257 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004258# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004259 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004260# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004261 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004262 else
4263#endif
4264 while (nextcomm != NULL)
4265 nextcomm = set_one_cmd_context(xp, nextcomm);
4266
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 str[col] = old_char;
4268}
4269
4270/*
4271 * Expand the command line "str" from context "xp".
4272 * "xp" must have been set by set_cmd_context().
4273 * xp->xp_pattern points into "str", to where the text that is to be expanded
4274 * starts.
4275 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4276 * cursor.
4277 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4278 * key that triggered expansion literally.
4279 * Returns EXPAND_OK otherwise.
4280 */
4281 int
4282expand_cmdline(xp, str, col, matchcount, matches)
4283 expand_T *xp;
4284 char_u *str; /* start of command line */
4285 int col; /* position of cursor */
4286 int *matchcount; /* return: nr of matches */
4287 char_u ***matches; /* return: array of pointers to matches */
4288{
4289 char_u *file_str = NULL;
4290
4291 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4292 {
4293 beep_flush();
4294 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4295 }
4296 if (xp->xp_context == EXPAND_NOTHING)
4297 {
4298 /* Caller can use the character as a normal char instead */
4299 return EXPAND_NOTHING;
4300 }
4301
4302 /* add star to file name, or convert to regexp if not exp. files. */
4303 file_str = addstar(xp->xp_pattern,
4304 (int)(str + col - xp->xp_pattern), xp->xp_context);
4305 if (file_str == NULL)
4306 return EXPAND_UNSUCCESSFUL;
4307
4308 /* find all files that match the description */
4309 if (ExpandFromContext(xp, file_str, matchcount, matches,
4310 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4311 {
4312 *matchcount = 0;
4313 *matches = NULL;
4314 }
4315 vim_free(file_str);
4316
4317 return EXPAND_OK;
4318}
4319
4320#ifdef FEAT_MULTI_LANG
4321/*
4322 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4323 */
4324static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4325
4326 static void
4327cleanup_help_tags(num_file, file)
4328 int num_file;
4329 char_u **file;
4330{
4331 int i, j;
4332 int len;
4333
4334 for (i = 0; i < num_file; ++i)
4335 {
4336 len = (int)STRLEN(file[i]) - 3;
4337 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4338 {
4339 /* Sorting on priority means the same item in another language may
4340 * be anywhere. Search all items for a match up to the "@en". */
4341 for (j = 0; j < num_file; ++j)
4342 if (j != i
4343 && (int)STRLEN(file[j]) == len + 3
4344 && STRNCMP(file[i], file[j], len + 1) == 0)
4345 break;
4346 if (j == num_file)
4347 file[i][len] = NUL;
4348 }
4349 }
4350}
4351#endif
4352
4353/*
4354 * Do the expansion based on xp->xp_context and "pat".
4355 */
4356 static int
4357ExpandFromContext(xp, pat, num_file, file, options)
4358 expand_T *xp;
4359 char_u *pat;
4360 int *num_file;
4361 char_u ***file;
4362 int options;
4363{
4364#ifdef FEAT_CMDL_COMPL
4365 regmatch_T regmatch;
4366#endif
4367 int ret;
4368 int flags;
4369
4370 flags = EW_DIR; /* include directories */
4371 if (options & WILD_LIST_NOTFOUND)
4372 flags |= EW_NOTFOUND;
4373 if (options & WILD_ADD_SLASH)
4374 flags |= EW_ADDSLASH;
4375 if (options & WILD_KEEP_ALL)
4376 flags |= EW_KEEPALL;
4377 if (options & WILD_SILENT)
4378 flags |= EW_SILENT;
4379
4380 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4381 {
4382 /*
4383 * Expand file or directory names.
4384 */
4385 int free_pat = FALSE;
4386 int i;
4387
4388 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4389 * space */
4390 if (xp->xp_backslash != XP_BS_NONE)
4391 {
4392 free_pat = TRUE;
4393 pat = vim_strsave(pat);
4394 for (i = 0; pat[i]; ++i)
4395 if (pat[i] == '\\')
4396 {
4397 if (xp->xp_backslash == XP_BS_THREE
4398 && pat[i + 1] == '\\'
4399 && pat[i + 2] == '\\'
4400 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004401 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 if (xp->xp_backslash == XP_BS_ONE
4403 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004404 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
4406 }
4407
4408 if (xp->xp_context == EXPAND_FILES)
4409 flags |= EW_FILE;
4410 else
4411 flags = (flags | EW_DIR) & ~EW_FILE;
4412 ret = expand_wildcards(1, &pat, num_file, file, flags);
4413 if (free_pat)
4414 vim_free(pat);
4415 return ret;
4416 }
4417
4418 *file = (char_u **)"";
4419 *num_file = 0;
4420 if (xp->xp_context == EXPAND_HELP)
4421 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004422 /* With an empty argument we would get all the help tags, which is
4423 * very slow. Get matches for "help" instead. */
4424 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4425 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 {
4427#ifdef FEAT_MULTI_LANG
4428 cleanup_help_tags(*num_file, *file);
4429#endif
4430 return OK;
4431 }
4432 return FAIL;
4433 }
4434
4435#ifndef FEAT_CMDL_COMPL
4436 return FAIL;
4437#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004438 if (xp->xp_context == EXPAND_SHELLCMD)
4439 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 if (xp->xp_context == EXPAND_OLD_SETTING)
4441 return ExpandOldSetting(num_file, file);
4442 if (xp->xp_context == EXPAND_BUFFERS)
4443 return ExpandBufnames(pat, num_file, file, options);
4444 if (xp->xp_context == EXPAND_TAGS
4445 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4446 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4447 if (xp->xp_context == EXPAND_COLORS)
4448 return ExpandRTDir(pat, num_file, file, "colors");
4449 if (xp->xp_context == EXPAND_COMPILER)
4450 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004451# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4452 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004453 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
4456 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4457 if (regmatch.regprog == NULL)
4458 return FAIL;
4459
4460 /* set ignore-case according to p_ic, p_scs and pat */
4461 regmatch.rm_ic = ignorecase(pat);
4462
4463 if (xp->xp_context == EXPAND_SETTINGS
4464 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4465 ret = ExpandSettings(xp, &regmatch, num_file, file);
4466 else if (xp->xp_context == EXPAND_MAPPINGS)
4467 ret = ExpandMappings(&regmatch, num_file, file);
4468# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4469 else if (xp->xp_context == EXPAND_USER_DEFINED)
4470 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4471# endif
4472 else
4473 {
4474 static struct expgen
4475 {
4476 int context;
4477 char_u *((*func)__ARGS((expand_T *, int)));
4478 int ic;
4479 } tab[] =
4480 {
4481 {EXPAND_COMMANDS, get_command_name, FALSE},
4482#ifdef FEAT_USR_CMDS
4483 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4484 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4485 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4486 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4487#endif
4488#ifdef FEAT_EVAL
4489 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4490 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4491 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4492 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4493#endif
4494#ifdef FEAT_MENU
4495 {EXPAND_MENUS, get_menu_name, FALSE},
4496 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4497#endif
4498#ifdef FEAT_SYN_HL
4499 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4500#endif
4501 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4502#ifdef FEAT_AUTOCMD
4503 {EXPAND_EVENTS, get_event_name, TRUE},
4504 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4505#endif
4506#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4507 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4508 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4509#endif
4510 {EXPAND_ENV_VARS, get_env_name, TRUE},
4511 };
4512 int i;
4513
4514 /*
4515 * Find a context in the table and call the ExpandGeneric() with the
4516 * right function to do the expansion.
4517 */
4518 ret = FAIL;
4519 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4520 if (xp->xp_context == tab[i].context)
4521 {
4522 if (tab[i].ic)
4523 regmatch.rm_ic = TRUE;
4524 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4525 break;
4526 }
4527 }
4528
4529 vim_free(regmatch.regprog);
4530
4531 return ret;
4532#endif /* FEAT_CMDL_COMPL */
4533}
4534
4535#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4536/*
4537 * Expand a list of names.
4538 *
4539 * Generic function for command line completion. It calls a function to
4540 * obtain strings, one by one. The strings are matched against a regexp
4541 * program. Matching strings are copied into an array, which is returned.
4542 *
4543 * Returns OK when no problems encountered, FAIL for error (out of memory).
4544 */
4545 int
4546ExpandGeneric(xp, regmatch, num_file, file, func)
4547 expand_T *xp;
4548 regmatch_T *regmatch;
4549 int *num_file;
4550 char_u ***file;
4551 char_u *((*func)__ARGS((expand_T *, int)));
4552 /* returns a string from the list */
4553{
4554 int i;
4555 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004556 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 char_u *str;
4558
4559 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004560 * round == 0: count the number of matching names
4561 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004563 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 {
4565 for (i = 0; ; ++i)
4566 {
4567 str = (*func)(xp, i);
4568 if (str == NULL) /* end of list */
4569 break;
4570 if (*str == NUL) /* skip empty strings */
4571 continue;
4572
4573 if (vim_regexec(regmatch, str, (colnr_T)0))
4574 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004575 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 {
4577 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4578 (*file)[count] = str;
4579#ifdef FEAT_MENU
4580 if (func == get_menu_names && str != NULL)
4581 {
4582 /* test for separator added by get_menu_names() */
4583 str += STRLEN(str) - 1;
4584 if (*str == '\001')
4585 *str = '.';
4586 }
4587#endif
4588 }
4589 ++count;
4590 }
4591 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004592 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 {
4594 if (count == 0)
4595 return OK;
4596 *num_file = count;
4597 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4598 if (*file == NULL)
4599 {
4600 *file = (char_u **)"";
4601 return FAIL;
4602 }
4603 count = 0;
4604 }
4605 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004606
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004607 /* Sort the results. Keep menu's in the specified order. */
4608 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4609 sort_strings(*file, *num_file);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004610
Bram Moolenaar4f688582007-07-24 12:34:30 +00004611#ifdef FEAT_CMDL_COMPL
4612 /* Reset the variables used for special highlight names expansion, so that
4613 * they don't show up when getting normal highlight names by ID. */
4614 reset_expand_highlight();
4615#endif
4616
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 return OK;
4618}
4619
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004620/*
4621 * Complete a shell command.
4622 * Returns FAIL or OK;
4623 */
4624 static int
4625expand_shellcmd(filepat, num_file, file, flagsarg)
4626 char_u *filepat; /* pattern to match with command names */
4627 int *num_file; /* return: number of matches */
4628 char_u ***file; /* return: array with matches */
4629 int flagsarg; /* EW_ flags */
4630{
4631 char_u *pat;
4632 int i;
4633 char_u *path;
4634 int mustfree = FALSE;
4635 garray_T ga;
4636 char_u *buf = alloc(MAXPATHL);
4637 size_t l;
4638 char_u *s, *e;
4639 int flags = flagsarg;
4640 int ret;
4641
4642 if (buf == NULL)
4643 return FAIL;
4644
4645 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4646 * space */
4647 pat = vim_strsave(filepat);
4648 for (i = 0; pat[i]; ++i)
4649 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004650 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004651
4652 flags |= EW_FILE | EW_EXEC;
4653
4654 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004655 if (mch_isFullName(pat))
4656 path = (char_u *)" ";
4657 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004658 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4659 path = (char_u *)".";
4660 else
4661 path = vim_getenv((char_u *)"PATH", &mustfree);
4662
4663 /*
4664 * Go over all directories in $PATH. Expand matches in that directory and
4665 * collect them in "ga".
4666 */
4667 ga_init2(&ga, (int)sizeof(char *), 10);
4668 for (s = path; *s != NUL; s = e)
4669 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004670 if (*s == ' ')
4671 ++s; /* Skip space used for absolute path name. */
4672
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004673#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4674 e = vim_strchr(s, ';');
4675#else
4676 e = vim_strchr(s, ':');
4677#endif
4678 if (e == NULL)
4679 e = s + STRLEN(s);
4680
4681 l = e - s;
4682 if (l > MAXPATHL - 5)
4683 break;
4684 vim_strncpy(buf, s, l);
4685 add_pathsep(buf);
4686 l = STRLEN(buf);
4687 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4688
4689 /* Expand matches in one directory of $PATH. */
4690 ret = expand_wildcards(1, &buf, num_file, file, flags);
4691 if (ret == OK)
4692 {
4693 if (ga_grow(&ga, *num_file) == FAIL)
4694 FreeWild(*num_file, *file);
4695 else
4696 {
4697 for (i = 0; i < *num_file; ++i)
4698 {
4699 s = (*file)[i];
4700 if (STRLEN(s) > l)
4701 {
4702 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004703 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004704 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4705 }
4706 else
4707 vim_free(s);
4708 }
4709 vim_free(*file);
4710 }
4711 }
4712 if (*e != NUL)
4713 ++e;
4714 }
4715 *file = ga.ga_data;
4716 *num_file = ga.ga_len;
4717
4718 vim_free(buf);
4719 vim_free(pat);
4720 if (mustfree)
4721 vim_free(path);
4722 return OK;
4723}
4724
4725
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004727static 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));
4728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004730 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004731 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004733 static void *
4734call_user_expand_func(user_expand_func, xp, num_file, file)
4735 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 int *num_file;
4738 char_u ***file;
4739{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004740 char_u keep;
4741 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004744 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004745 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746
4747 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004748 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 *num_file = 0;
4750 *file = NULL;
4751
Bram Moolenaar21669c02008-01-18 12:16:16 +00004752 if (ccline.cmdbuff == NULL)
4753 {
4754 /* Completion from Insert mode, pass fake arguments. */
4755 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004756 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004757 args[1] = xp->xp_pattern;
4758 }
4759 else
4760 {
4761 /* Completion on the command line, pass real arguments. */
4762 keep = ccline.cmdbuff[ccline.cmdlen];
4763 ccline.cmdbuff[ccline.cmdlen] = 0;
4764 sprintf((char *)num, "%d", ccline.cmdpos);
4765 args[1] = ccline.cmdbuff;
4766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 args[0] = xp->xp_pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 args[2] = num;
4769
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004770 /* Save the cmdline, we don't know what the function may do. */
4771 save_ccline = ccline;
4772 ccline.cmdbuff = NULL;
4773 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004775
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004776 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004777
4778 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004780 if (ccline.cmdbuff != NULL)
4781 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004782
4783 return ret;
4784}
4785
4786/*
4787 * Expand names with a function defined by the user.
4788 */
4789 static int
4790ExpandUserDefined(xp, regmatch, num_file, file)
4791 expand_T *xp;
4792 regmatch_T *regmatch;
4793 int *num_file;
4794 char_u ***file;
4795{
4796 char_u *retstr;
4797 char_u *s;
4798 char_u *e;
4799 char_u keep;
4800 garray_T ga;
4801
4802 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4803 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 return FAIL;
4805
4806 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004807 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 {
4809 e = vim_strchr(s, '\n');
4810 if (e == NULL)
4811 e = s + STRLEN(s);
4812 keep = *e;
4813 *e = 0;
4814
4815 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4816 {
4817 *e = keep;
4818 if (*e != NUL)
4819 ++e;
4820 continue;
4821 }
4822
4823 if (ga_grow(&ga, 1) == FAIL)
4824 break;
4825
4826 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4827 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828
4829 *e = keep;
4830 if (*e != NUL)
4831 ++e;
4832 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004833 vim_free(retstr);
4834 *file = ga.ga_data;
4835 *num_file = ga.ga_len;
4836 return OK;
4837}
4838
4839/*
4840 * Expand names with a list returned by a function defined by the user.
4841 */
4842 static int
4843ExpandUserList(xp, num_file, file)
4844 expand_T *xp;
4845 int *num_file;
4846 char_u ***file;
4847{
4848 list_T *retlist;
4849 listitem_T *li;
4850 garray_T ga;
4851
4852 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4853 if (retlist == NULL)
4854 return FAIL;
4855
4856 ga_init2(&ga, (int)sizeof(char *), 3);
4857 /* Loop over the items in the list. */
4858 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4859 {
4860 if (li->li_tv.v_type != VAR_STRING)
4861 continue; /* Skip non-string items */
4862
4863 if (ga_grow(&ga, 1) == FAIL)
4864 break;
4865
4866 ((char_u **)ga.ga_data)[ga.ga_len] =
4867 vim_strsave(li->li_tv.vval.v_string);
4868 ++ga.ga_len;
4869 }
4870 list_unref(retlist);
4871
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 *file = ga.ga_data;
4873 *num_file = ga.ga_len;
4874 return OK;
4875}
4876#endif
4877
4878/*
4879 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4880 * or compiler names.
4881 */
4882 static int
4883ExpandRTDir(pat, num_file, file, dirname)
4884 char_u *pat;
4885 int *num_file;
4886 char_u ***file;
4887 char *dirname; /* "colors" or "compiler" */
4888{
4889 char_u *all;
4890 char_u *s;
4891 char_u *e;
4892 garray_T ga;
4893
4894 *num_file = 0;
4895 *file = NULL;
4896 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4897 if (s == NULL)
4898 return FAIL;
4899 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4900 all = globpath(p_rtp, s);
4901 vim_free(s);
4902 if (all == NULL)
4903 return FAIL;
4904
4905 ga_init2(&ga, (int)sizeof(char *), 3);
4906 for (s = all; *s != NUL; s = e)
4907 {
4908 e = vim_strchr(s, '\n');
4909 if (e == NULL)
4910 e = s + STRLEN(s);
4911 if (ga_grow(&ga, 1) == FAIL)
4912 break;
4913 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4914 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004915 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (*s == '\n' || vim_ispathsep(*s))
4917 break;
4918 ++s;
4919 ((char_u **)ga.ga_data)[ga.ga_len] =
4920 vim_strnsave(s, (int)(e - s - 4));
4921 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 if (*e != NUL)
4924 ++e;
4925 }
4926 vim_free(all);
4927 *file = ga.ga_data;
4928 *num_file = ga.ga_len;
4929 return OK;
4930}
4931
4932#endif
4933
4934#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4935/*
4936 * Expand "file" for all comma-separated directories in "path".
4937 * Returns an allocated string with all matches concatenated, separated by
4938 * newlines. Returns NULL for an error or no matches.
4939 */
4940 char_u *
4941globpath(path, file)
4942 char_u *path;
4943 char_u *file;
4944{
4945 expand_T xpc;
4946 char_u *buf;
4947 garray_T ga;
4948 int i;
4949 int len;
4950 int num_p;
4951 char_u **p;
4952 char_u *cur = NULL;
4953
4954 buf = alloc(MAXPATHL);
4955 if (buf == NULL)
4956 return NULL;
4957
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004958 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004960
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 ga_init2(&ga, 1, 100);
4962
4963 /* Loop over all entries in {path}. */
4964 while (*path != NUL)
4965 {
4966 /* Copy one item of the path to buf[] and concatenate the file name. */
4967 copy_option_part(&path, buf, MAXPATHL, ",");
4968 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4969 {
4970 add_pathsep(buf);
4971 STRCAT(buf, file);
4972 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4973 && num_p > 0)
4974 {
4975 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4976 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004977 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978
4979 /* Concatenate new results to previous ones. */
4980 if (ga_grow(&ga, len) == OK)
4981 {
4982 cur = (char_u *)ga.ga_data + ga.ga_len;
4983 for (i = 0; i < num_p; ++i)
4984 {
4985 STRCPY(cur, p[i]);
4986 cur += STRLEN(p[i]);
4987 *cur++ = '\n';
4988 }
4989 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991 FreeWild(num_p, p);
4992 }
4993 }
4994 }
4995 if (cur != NULL)
4996 *--cur = 0; /* Replace trailing newline with NUL */
4997
4998 vim_free(buf);
4999 return (char_u *)ga.ga_data;
5000}
5001
5002#endif
5003
5004#if defined(FEAT_CMDHIST) || defined(PROTO)
5005
5006/*********************************
5007 * Command line history stuff *
5008 *********************************/
5009
5010/*
5011 * Translate a history character to the associated type number.
5012 */
5013 static int
5014hist_char2type(c)
5015 int c;
5016{
5017 if (c == ':')
5018 return HIST_CMD;
5019 if (c == '=')
5020 return HIST_EXPR;
5021 if (c == '@')
5022 return HIST_INPUT;
5023 if (c == '>')
5024 return HIST_DEBUG;
5025 return HIST_SEARCH; /* must be '?' or '/' */
5026}
5027
5028/*
5029 * Table of history names.
5030 * These names are used in :history and various hist...() functions.
5031 * It is sufficient to give the significant prefix of a history name.
5032 */
5033
5034static char *(history_names[]) =
5035{
5036 "cmd",
5037 "search",
5038 "expr",
5039 "input",
5040 "debug",
5041 NULL
5042};
5043
5044/*
5045 * init_history() - Initialize the command line history.
5046 * Also used to re-allocate the history when the size changes.
5047 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005048 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049init_history()
5050{
5051 int newlen; /* new length of history table */
5052 histentry_T *temp;
5053 int i;
5054 int j;
5055 int type;
5056
5057 /*
5058 * If size of history table changed, reallocate it
5059 */
5060 newlen = (int)p_hi;
5061 if (newlen != hislen) /* history length changed */
5062 {
5063 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5064 {
5065 if (newlen)
5066 {
5067 temp = (histentry_T *)lalloc(
5068 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5069 if (temp == NULL) /* out of memory! */
5070 {
5071 if (type == 0) /* first one: just keep the old length */
5072 {
5073 newlen = hislen;
5074 break;
5075 }
5076 /* Already changed one table, now we can only have zero
5077 * length for all tables. */
5078 newlen = 0;
5079 type = -1;
5080 continue;
5081 }
5082 }
5083 else
5084 temp = NULL;
5085 if (newlen == 0 || temp != NULL)
5086 {
5087 if (hisidx[type] < 0) /* there are no entries yet */
5088 {
5089 for (i = 0; i < newlen; ++i)
5090 {
5091 temp[i].hisnum = 0;
5092 temp[i].hisstr = NULL;
5093 }
5094 }
5095 else if (newlen > hislen) /* array becomes bigger */
5096 {
5097 for (i = 0; i <= hisidx[type]; ++i)
5098 temp[i] = history[type][i];
5099 j = i;
5100 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5101 {
5102 temp[i].hisnum = 0;
5103 temp[i].hisstr = NULL;
5104 }
5105 for ( ; j < hislen; ++i, ++j)
5106 temp[i] = history[type][j];
5107 }
5108 else /* array becomes smaller or 0 */
5109 {
5110 j = hisidx[type];
5111 for (i = newlen - 1; ; --i)
5112 {
5113 if (i >= 0) /* copy newest entries */
5114 temp[i] = history[type][j];
5115 else /* remove older entries */
5116 vim_free(history[type][j].hisstr);
5117 if (--j < 0)
5118 j = hislen - 1;
5119 if (j == hisidx[type])
5120 break;
5121 }
5122 hisidx[type] = newlen - 1;
5123 }
5124 vim_free(history[type]);
5125 history[type] = temp;
5126 }
5127 }
5128 hislen = newlen;
5129 }
5130}
5131
5132/*
5133 * Check if command line 'str' is already in history.
5134 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5135 */
5136 static int
5137in_history(type, str, move_to_front)
5138 int type;
5139 char_u *str;
5140 int move_to_front; /* Move the entry to the front if it exists */
5141{
5142 int i;
5143 int last_i = -1;
5144
5145 if (hisidx[type] < 0)
5146 return FALSE;
5147 i = hisidx[type];
5148 do
5149 {
5150 if (history[type][i].hisstr == NULL)
5151 return FALSE;
5152 if (STRCMP(str, history[type][i].hisstr) == 0)
5153 {
5154 if (!move_to_front)
5155 return TRUE;
5156 last_i = i;
5157 break;
5158 }
5159 if (--i < 0)
5160 i = hislen - 1;
5161 } while (i != hisidx[type]);
5162
5163 if (last_i >= 0)
5164 {
5165 str = history[type][i].hisstr;
5166 while (i != hisidx[type])
5167 {
5168 if (++i >= hislen)
5169 i = 0;
5170 history[type][last_i] = history[type][i];
5171 last_i = i;
5172 }
5173 history[type][i].hisstr = str;
5174 history[type][i].hisnum = ++hisnum[type];
5175 return TRUE;
5176 }
5177 return FALSE;
5178}
5179
5180/*
5181 * Convert history name (from table above) to its HIST_ equivalent.
5182 * When "name" is empty, return "cmd" history.
5183 * Returns -1 for unknown history name.
5184 */
5185 int
5186get_histtype(name)
5187 char_u *name;
5188{
5189 int i;
5190 int len = (int)STRLEN(name);
5191
5192 /* No argument: use current history. */
5193 if (len == 0)
5194 return hist_char2type(ccline.cmdfirstc);
5195
5196 for (i = 0; history_names[i] != NULL; ++i)
5197 if (STRNICMP(name, history_names[i], len) == 0)
5198 return i;
5199
5200 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5201 return hist_char2type(name[0]);
5202
5203 return -1;
5204}
5205
5206static int last_maptick = -1; /* last seen maptick */
5207
5208/*
5209 * Add the given string to the given history. If the string is already in the
5210 * history then it is moved to the front. "histype" may be one of he HIST_
5211 * values.
5212 */
5213 void
5214add_to_history(histype, new_entry, in_map, sep)
5215 int histype;
5216 char_u *new_entry;
5217 int in_map; /* consider maptick when inside a mapping */
5218 int sep; /* separator character used (search hist) */
5219{
5220 histentry_T *hisptr;
5221 int len;
5222
5223 if (hislen == 0) /* no history */
5224 return;
5225
5226 /*
5227 * Searches inside the same mapping overwrite each other, so that only
5228 * the last line is kept. Be careful not to remove a line that was moved
5229 * down, only lines that were added.
5230 */
5231 if (histype == HIST_SEARCH && in_map)
5232 {
5233 if (maptick == last_maptick)
5234 {
5235 /* Current line is from the same mapping, remove it */
5236 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5237 vim_free(hisptr->hisstr);
5238 hisptr->hisstr = NULL;
5239 hisptr->hisnum = 0;
5240 --hisnum[histype];
5241 if (--hisidx[HIST_SEARCH] < 0)
5242 hisidx[HIST_SEARCH] = hislen - 1;
5243 }
5244 last_maptick = -1;
5245 }
5246 if (!in_history(histype, new_entry, TRUE))
5247 {
5248 if (++hisidx[histype] == hislen)
5249 hisidx[histype] = 0;
5250 hisptr = &history[histype][hisidx[histype]];
5251 vim_free(hisptr->hisstr);
5252
5253 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005254 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5256 if (hisptr->hisstr != NULL)
5257 hisptr->hisstr[len + 1] = sep;
5258
5259 hisptr->hisnum = ++hisnum[histype];
5260 if (histype == HIST_SEARCH && in_map)
5261 last_maptick = maptick;
5262 }
5263}
5264
5265#if defined(FEAT_EVAL) || defined(PROTO)
5266
5267/*
5268 * Get identifier of newest history entry.
5269 * "histype" may be one of the HIST_ values.
5270 */
5271 int
5272get_history_idx(histype)
5273 int histype;
5274{
5275 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5276 || hisidx[histype] < 0)
5277 return -1;
5278
5279 return history[histype][hisidx[histype]].hisnum;
5280}
5281
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005282static struct cmdline_info *get_ccline_ptr __ARGS((void));
5283
5284/*
5285 * Get pointer to the command line info to use. cmdline_paste() may clear
5286 * ccline and put the previous value in prev_ccline.
5287 */
5288 static struct cmdline_info *
5289get_ccline_ptr()
5290{
5291 if ((State & CMDLINE) == 0)
5292 return NULL;
5293 if (ccline.cmdbuff != NULL)
5294 return &ccline;
5295 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5296 return &prev_ccline;
5297 return NULL;
5298}
5299
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300/*
5301 * Get the current command line in allocated memory.
5302 * Only works when the command line is being edited.
5303 * Returns NULL when something is wrong.
5304 */
5305 char_u *
5306get_cmdline_str()
5307{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005308 struct cmdline_info *p = get_ccline_ptr();
5309
5310 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005312 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313}
5314
5315/*
5316 * Get the current command line position, counted in bytes.
5317 * Zero is the first position.
5318 * Only works when the command line is being edited.
5319 * Returns -1 when something is wrong.
5320 */
5321 int
5322get_cmdline_pos()
5323{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005324 struct cmdline_info *p = get_ccline_ptr();
5325
5326 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005328 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329}
5330
5331/*
5332 * Set the command line byte position to "pos". Zero is the first position.
5333 * Only works when the command line is being edited.
5334 * Returns 1 when failed, 0 when OK.
5335 */
5336 int
5337set_cmdline_pos(pos)
5338 int pos;
5339{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005340 struct cmdline_info *p = get_ccline_ptr();
5341
5342 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 return 1;
5344
5345 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5346 * changed the command line. */
5347 if (pos < 0)
5348 new_cmdpos = 0;
5349 else
5350 new_cmdpos = pos;
5351 return 0;
5352}
5353
5354/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005355 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005356 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005357 * Only works when the command line is being edited.
5358 * Returns NUL when something is wrong.
5359 */
5360 int
5361get_cmdline_type()
5362{
5363 struct cmdline_info *p = get_ccline_ptr();
5364
5365 if (p == NULL)
5366 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005367 if (p->cmdfirstc == NUL)
5368 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005369 return p->cmdfirstc;
5370}
5371
5372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 * Calculate history index from a number:
5374 * num > 0: seen as identifying number of a history entry
5375 * num < 0: relative position in history wrt newest entry
5376 * "histype" may be one of the HIST_ values.
5377 */
5378 static int
5379calc_hist_idx(histype, num)
5380 int histype;
5381 int num;
5382{
5383 int i;
5384 histentry_T *hist;
5385 int wrapped = FALSE;
5386
5387 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5388 || (i = hisidx[histype]) < 0 || num == 0)
5389 return -1;
5390
5391 hist = history[histype];
5392 if (num > 0)
5393 {
5394 while (hist[i].hisnum > num)
5395 if (--i < 0)
5396 {
5397 if (wrapped)
5398 break;
5399 i += hislen;
5400 wrapped = TRUE;
5401 }
5402 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5403 return i;
5404 }
5405 else if (-num <= hislen)
5406 {
5407 i += num + 1;
5408 if (i < 0)
5409 i += hislen;
5410 if (hist[i].hisstr != NULL)
5411 return i;
5412 }
5413 return -1;
5414}
5415
5416/*
5417 * Get a history entry by its index.
5418 * "histype" may be one of the HIST_ values.
5419 */
5420 char_u *
5421get_history_entry(histype, idx)
5422 int histype;
5423 int idx;
5424{
5425 idx = calc_hist_idx(histype, idx);
5426 if (idx >= 0)
5427 return history[histype][idx].hisstr;
5428 else
5429 return (char_u *)"";
5430}
5431
5432/*
5433 * Clear all entries of a history.
5434 * "histype" may be one of the HIST_ values.
5435 */
5436 int
5437clr_history(histype)
5438 int histype;
5439{
5440 int i;
5441 histentry_T *hisptr;
5442
5443 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5444 {
5445 hisptr = history[histype];
5446 for (i = hislen; i--;)
5447 {
5448 vim_free(hisptr->hisstr);
5449 hisptr->hisnum = 0;
5450 hisptr++->hisstr = NULL;
5451 }
5452 hisidx[histype] = -1; /* mark history as cleared */
5453 hisnum[histype] = 0; /* reset identifier counter */
5454 return OK;
5455 }
5456 return FAIL;
5457}
5458
5459/*
5460 * Remove all entries matching {str} from a history.
5461 * "histype" may be one of the HIST_ values.
5462 */
5463 int
5464del_history_entry(histype, str)
5465 int histype;
5466 char_u *str;
5467{
5468 regmatch_T regmatch;
5469 histentry_T *hisptr;
5470 int idx;
5471 int i;
5472 int last;
5473 int found = FALSE;
5474
5475 regmatch.regprog = NULL;
5476 regmatch.rm_ic = FALSE; /* always match case */
5477 if (hislen != 0
5478 && histype >= 0
5479 && histype < HIST_COUNT
5480 && *str != NUL
5481 && (idx = hisidx[histype]) >= 0
5482 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5483 != NULL)
5484 {
5485 i = last = idx;
5486 do
5487 {
5488 hisptr = &history[histype][i];
5489 if (hisptr->hisstr == NULL)
5490 break;
5491 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5492 {
5493 found = TRUE;
5494 vim_free(hisptr->hisstr);
5495 hisptr->hisstr = NULL;
5496 hisptr->hisnum = 0;
5497 }
5498 else
5499 {
5500 if (i != last)
5501 {
5502 history[histype][last] = *hisptr;
5503 hisptr->hisstr = NULL;
5504 hisptr->hisnum = 0;
5505 }
5506 if (--last < 0)
5507 last += hislen;
5508 }
5509 if (--i < 0)
5510 i += hislen;
5511 } while (i != idx);
5512 if (history[histype][idx].hisstr == NULL)
5513 hisidx[histype] = -1;
5514 }
5515 vim_free(regmatch.regprog);
5516 return found;
5517}
5518
5519/*
5520 * Remove an indexed entry from a history.
5521 * "histype" may be one of the HIST_ values.
5522 */
5523 int
5524del_history_idx(histype, idx)
5525 int histype;
5526 int idx;
5527{
5528 int i, j;
5529
5530 i = calc_hist_idx(histype, idx);
5531 if (i < 0)
5532 return FALSE;
5533 idx = hisidx[histype];
5534 vim_free(history[histype][i].hisstr);
5535
5536 /* When deleting the last added search string in a mapping, reset
5537 * last_maptick, so that the last added search string isn't deleted again.
5538 */
5539 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5540 last_maptick = -1;
5541
5542 while (i != idx)
5543 {
5544 j = (i + 1) % hislen;
5545 history[histype][i] = history[histype][j];
5546 i = j;
5547 }
5548 history[histype][i].hisstr = NULL;
5549 history[histype][i].hisnum = 0;
5550 if (--i < 0)
5551 i += hislen;
5552 hisidx[histype] = i;
5553 return TRUE;
5554}
5555
5556#endif /* FEAT_EVAL */
5557
5558#if defined(FEAT_CRYPT) || defined(PROTO)
5559/*
5560 * Very specific function to remove the value in ":set key=val" from the
5561 * history.
5562 */
5563 void
5564remove_key_from_history()
5565{
5566 char_u *p;
5567 int i;
5568
5569 i = hisidx[HIST_CMD];
5570 if (i < 0)
5571 return;
5572 p = history[HIST_CMD][i].hisstr;
5573 if (p != NULL)
5574 for ( ; *p; ++p)
5575 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5576 {
5577 p = vim_strchr(p + 3, '=');
5578 if (p == NULL)
5579 break;
5580 ++p;
5581 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5582 if (p[i] == '\\' && p[i + 1])
5583 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005584 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 --p;
5586 }
5587}
5588#endif
5589
5590#endif /* FEAT_CMDHIST */
5591
5592#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5593/*
5594 * Get indices "num1,num2" that specify a range within a list (not a range of
5595 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5596 * Returns OK if parsed successfully, otherwise FAIL.
5597 */
5598 int
5599get_list_range(str, num1, num2)
5600 char_u **str;
5601 int *num1;
5602 int *num2;
5603{
5604 int len;
5605 int first = FALSE;
5606 long num;
5607
5608 *str = skipwhite(*str);
5609 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5610 {
5611 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5612 *str += len;
5613 *num1 = (int)num;
5614 first = TRUE;
5615 }
5616 *str = skipwhite(*str);
5617 if (**str == ',') /* parse "to" part of range */
5618 {
5619 *str = skipwhite(*str + 1);
5620 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5621 if (len > 0)
5622 {
5623 *num2 = (int)num;
5624 *str = skipwhite(*str + len);
5625 }
5626 else if (!first) /* no number given at all */
5627 return FAIL;
5628 }
5629 else if (first) /* only one number given */
5630 *num2 = *num1;
5631 return OK;
5632}
5633#endif
5634
5635#if defined(FEAT_CMDHIST) || defined(PROTO)
5636/*
5637 * :history command - print a history
5638 */
5639 void
5640ex_history(eap)
5641 exarg_T *eap;
5642{
5643 histentry_T *hist;
5644 int histype1 = HIST_CMD;
5645 int histype2 = HIST_CMD;
5646 int hisidx1 = 1;
5647 int hisidx2 = -1;
5648 int idx;
5649 int i, j, k;
5650 char_u *end;
5651 char_u *arg = eap->arg;
5652
5653 if (hislen == 0)
5654 {
5655 MSG(_("'history' option is zero"));
5656 return;
5657 }
5658
5659 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5660 {
5661 end = arg;
5662 while (ASCII_ISALPHA(*end)
5663 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5664 end++;
5665 i = *end;
5666 *end = NUL;
5667 histype1 = get_histtype(arg);
5668 if (histype1 == -1)
5669 {
5670 if (STRICMP(arg, "all") == 0)
5671 {
5672 histype1 = 0;
5673 histype2 = HIST_COUNT-1;
5674 }
5675 else
5676 {
5677 *end = i;
5678 EMSG(_(e_trailing));
5679 return;
5680 }
5681 }
5682 else
5683 histype2 = histype1;
5684 *end = i;
5685 }
5686 else
5687 end = arg;
5688 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5689 {
5690 EMSG(_(e_trailing));
5691 return;
5692 }
5693
5694 for (; !got_int && histype1 <= histype2; ++histype1)
5695 {
5696 STRCPY(IObuff, "\n # ");
5697 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5698 MSG_PUTS_TITLE(IObuff);
5699 idx = hisidx[histype1];
5700 hist = history[histype1];
5701 j = hisidx1;
5702 k = hisidx2;
5703 if (j < 0)
5704 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5705 if (k < 0)
5706 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5707 if (idx >= 0 && j <= k)
5708 for (i = idx + 1; !got_int; ++i)
5709 {
5710 if (i == hislen)
5711 i = 0;
5712 if (hist[i].hisstr != NULL
5713 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5714 {
5715 msg_putchar('\n');
5716 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5717 hist[i].hisnum);
5718 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5719 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5720 (int)Columns - 10);
5721 else
5722 STRCAT(IObuff, hist[i].hisstr);
5723 msg_outtrans(IObuff);
5724 out_flush();
5725 }
5726 if (i == idx)
5727 break;
5728 }
5729 }
5730}
5731#endif
5732
5733#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5734static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5735static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5736static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5737static int viminfo_add_at_front = FALSE;
5738
5739static int hist_type2char __ARGS((int type, int use_question));
5740
5741/*
5742 * Translate a history type number to the associated character.
5743 */
5744 static int
5745hist_type2char(type, use_question)
5746 int type;
5747 int use_question; /* use '?' instead of '/' */
5748{
5749 if (type == HIST_CMD)
5750 return ':';
5751 if (type == HIST_SEARCH)
5752 {
5753 if (use_question)
5754 return '?';
5755 else
5756 return '/';
5757 }
5758 if (type == HIST_EXPR)
5759 return '=';
5760 return '@';
5761}
5762
5763/*
5764 * Prepare for reading the history from the viminfo file.
5765 * This allocates history arrays to store the read history lines.
5766 */
5767 void
5768prepare_viminfo_history(asklen)
5769 int asklen;
5770{
5771 int i;
5772 int num;
5773 int type;
5774 int len;
5775
5776 init_history();
5777 viminfo_add_at_front = (asklen != 0);
5778 if (asklen > hislen)
5779 asklen = hislen;
5780
5781 for (type = 0; type < HIST_COUNT; ++type)
5782 {
5783 /*
5784 * Count the number of empty spaces in the history list. If there are
5785 * more spaces available than we request, then fill them up.
5786 */
5787 for (i = 0, num = 0; i < hislen; i++)
5788 if (history[type][i].hisstr == NULL)
5789 num++;
5790 len = asklen;
5791 if (num > len)
5792 len = num;
5793 if (len <= 0)
5794 viminfo_history[type] = NULL;
5795 else
5796 viminfo_history[type] =
5797 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5798 if (viminfo_history[type] == NULL)
5799 len = 0;
5800 viminfo_hislen[type] = len;
5801 viminfo_hisidx[type] = 0;
5802 }
5803}
5804
5805/*
5806 * Accept a line from the viminfo, store it in the history array when it's
5807 * new.
5808 */
5809 int
5810read_viminfo_history(virp)
5811 vir_T *virp;
5812{
5813 int type;
5814 long_u len;
5815 char_u *val;
5816 char_u *p;
5817
5818 type = hist_char2type(virp->vir_line[0]);
5819 if (viminfo_hisidx[type] < viminfo_hislen[type])
5820 {
5821 val = viminfo_readstring(virp, 1, TRUE);
5822 if (val != NULL && *val != NUL)
5823 {
5824 if (!in_history(type, val + (type == HIST_SEARCH),
5825 viminfo_add_at_front))
5826 {
5827 /* Need to re-allocate to append the separator byte. */
5828 len = STRLEN(val);
5829 p = lalloc(len + 2, TRUE);
5830 if (p != NULL)
5831 {
5832 if (type == HIST_SEARCH)
5833 {
5834 /* Search entry: Move the separator from the first
5835 * column to after the NUL. */
5836 mch_memmove(p, val + 1, (size_t)len);
5837 p[len] = (*val == ' ' ? NUL : *val);
5838 }
5839 else
5840 {
5841 /* Not a search entry: No separator in the viminfo
5842 * file, add a NUL separator. */
5843 mch_memmove(p, val, (size_t)len + 1);
5844 p[len + 1] = NUL;
5845 }
5846 viminfo_history[type][viminfo_hisidx[type]++] = p;
5847 }
5848 }
5849 }
5850 vim_free(val);
5851 }
5852 return viminfo_readline(virp);
5853}
5854
5855 void
5856finish_viminfo_history()
5857{
5858 int idx;
5859 int i;
5860 int type;
5861
5862 for (type = 0; type < HIST_COUNT; ++type)
5863 {
5864 if (history[type] == NULL)
5865 return;
5866 idx = hisidx[type] + viminfo_hisidx[type];
5867 if (idx >= hislen)
5868 idx -= hislen;
5869 else if (idx < 0)
5870 idx = hislen - 1;
5871 if (viminfo_add_at_front)
5872 hisidx[type] = idx;
5873 else
5874 {
5875 if (hisidx[type] == -1)
5876 hisidx[type] = hislen - 1;
5877 do
5878 {
5879 if (history[type][idx].hisstr != NULL)
5880 break;
5881 if (++idx == hislen)
5882 idx = 0;
5883 } while (idx != hisidx[type]);
5884 if (idx != hisidx[type] && --idx < 0)
5885 idx = hislen - 1;
5886 }
5887 for (i = 0; i < viminfo_hisidx[type]; i++)
5888 {
5889 vim_free(history[type][idx].hisstr);
5890 history[type][idx].hisstr = viminfo_history[type][i];
5891 if (--idx < 0)
5892 idx = hislen - 1;
5893 }
5894 idx += 1;
5895 idx %= hislen;
5896 for (i = 0; i < viminfo_hisidx[type]; i++)
5897 {
5898 history[type][idx++].hisnum = ++hisnum[type];
5899 idx %= hislen;
5900 }
5901 vim_free(viminfo_history[type]);
5902 viminfo_history[type] = NULL;
5903 }
5904}
5905
5906 void
5907write_viminfo_history(fp)
5908 FILE *fp;
5909{
5910 int i;
5911 int type;
5912 int num_saved;
5913 char_u *p;
5914 int c;
5915
5916 init_history();
5917 if (hislen == 0)
5918 return;
5919 for (type = 0; type < HIST_COUNT; ++type)
5920 {
5921 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5922 if (num_saved == 0)
5923 continue;
5924 if (num_saved < 0) /* Use default */
5925 num_saved = hislen;
5926 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5927 type == HIST_CMD ? _("Command Line") :
5928 type == HIST_SEARCH ? _("Search String") :
5929 type == HIST_EXPR ? _("Expression") :
5930 _("Input Line"));
5931 if (num_saved > hislen)
5932 num_saved = hislen;
5933 i = hisidx[type];
5934 if (i >= 0)
5935 while (num_saved--)
5936 {
5937 p = history[type][i].hisstr;
5938 if (p != NULL)
5939 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005940 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 /* For the search history: put the separator in the second
5942 * column; use a space if there isn't one. */
5943 if (type == HIST_SEARCH)
5944 {
5945 c = p[STRLEN(p) + 1];
5946 putc(c == NUL ? ' ' : c, fp);
5947 }
5948 viminfo_writestring(fp, p);
5949 }
5950 if (--i < 0)
5951 i = hislen - 1;
5952 }
5953 }
5954}
5955#endif /* FEAT_VIMINFO */
5956
5957#if defined(FEAT_FKMAP) || defined(PROTO)
5958/*
5959 * Write a character at the current cursor+offset position.
5960 * It is directly written into the command buffer block.
5961 */
5962 void
5963cmd_pchar(c, offset)
5964 int c, offset;
5965{
5966 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5967 {
5968 EMSG(_("E198: cmd_pchar beyond the command length"));
5969 return;
5970 }
5971 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5972 ccline.cmdbuff[ccline.cmdlen] = NUL;
5973}
5974
5975 int
5976cmd_gchar(offset)
5977 int offset;
5978{
5979 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5980 {
5981 /* EMSG(_("cmd_gchar beyond the command length")); */
5982 return NUL;
5983 }
5984 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5985}
5986#endif
5987
5988#if defined(FEAT_CMDWIN) || defined(PROTO)
5989/*
5990 * Open a window on the current command line and history. Allow editing in
5991 * the window. Returns when the window is closed.
5992 * Returns:
5993 * CR if the command is to be executed
5994 * Ctrl_C if it is to be abandoned
5995 * K_IGNORE if editing continues
5996 */
5997 static int
5998ex_window()
5999{
6000 struct cmdline_info save_ccline;
6001 buf_T *old_curbuf = curbuf;
6002 win_T *old_curwin = curwin;
6003 buf_T *bp;
6004 win_T *wp;
6005 int i;
6006 linenr_T lnum;
6007 int histtype;
6008 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006009#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 int save_restart_edit = restart_edit;
6013 int save_State = State;
6014 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006015#ifdef FEAT_RIGHTLEFT
6016 int save_cmdmsg_rl = cmdmsg_rl;
6017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018
6019 /* Can't do this recursively. Can't do it when typing a password. */
6020 if (cmdwin_type != 0
6021# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6022 || cmdline_star > 0
6023# endif
6024 )
6025 {
6026 beep_flush();
6027 return K_IGNORE;
6028 }
6029
6030 /* Save current window sizes. */
6031 win_size_save(&winsizes);
6032
6033# ifdef FEAT_AUTOCMD
6034 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006035 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006037 /* don't use a new tab page */
6038 cmdmod.tab = 0;
6039
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 /* Create a window for the command-line buffer. */
6041 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6042 {
6043 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006044# ifdef FEAT_AUTOCMD
6045 unblock_autocmds();
6046# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 return K_IGNORE;
6048 }
6049 cmdwin_type = ccline.cmdfirstc;
6050 if (cmdwin_type == NUL)
6051 cmdwin_type = '-';
6052
6053 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006054 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006055 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6057 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6058 curbuf->b_p_ma = TRUE;
6059# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006060 curwin->w_p_rl = cmdmsg_rl;
6061 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062# endif
6063# ifdef FEAT_SCROLLBIND
6064 curwin->w_p_scb = FALSE;
6065# endif
6066
6067# ifdef FEAT_AUTOCMD
6068 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006069 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070# endif
6071
Bram Moolenaar46152342005-09-07 21:18:43 +00006072 /* Showing the prompt may have set need_wait_return, reset it. */
6073 need_wait_return = FALSE;
6074
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 histtype = hist_char2type(ccline.cmdfirstc);
6076 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6077 {
6078 if (p_wc == TAB)
6079 {
6080 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6081 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6082 }
6083 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6084 }
6085
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006086 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6087 * sets 'textwidth' to 78). */
6088 curbuf->b_p_tw = 0;
6089
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 /* Fill the buffer with the history. */
6091 init_history();
6092 if (hislen > 0)
6093 {
6094 i = hisidx[histtype];
6095 if (i >= 0)
6096 {
6097 lnum = 0;
6098 do
6099 {
6100 if (++i == hislen)
6101 i = 0;
6102 if (history[histtype][i].hisstr != NULL)
6103 ml_append(lnum++, history[histtype][i].hisstr,
6104 (colnr_T)0, FALSE);
6105 }
6106 while (i != hisidx[histtype]);
6107 }
6108 }
6109
6110 /* Replace the empty last line with the current command-line and put the
6111 * cursor there. */
6112 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6113 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6114 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006115 changed_line_abv_curs();
6116 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006117 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118
6119 /* Save the command line info, can be used recursively. */
6120 save_ccline = ccline;
6121 ccline.cmdbuff = NULL;
6122 ccline.cmdprompt = NULL;
6123
6124 /* No Ex mode here! */
6125 exmode_active = 0;
6126
6127 State = NORMAL;
6128# ifdef FEAT_MOUSE
6129 setmouse();
6130# endif
6131
6132# ifdef FEAT_AUTOCMD
6133 /* Trigger CmdwinEnter autocommands. */
6134 typestr[0] = cmdwin_type;
6135 typestr[1] = NUL;
6136 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006137 if (restart_edit != 0) /* autocmd with ":startinsert" */
6138 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139# endif
6140
6141 i = RedrawingDisabled;
6142 RedrawingDisabled = 0;
6143
6144 /*
6145 * Call the main loop until <CR> or CTRL-C is typed.
6146 */
6147 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006148 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149
6150 RedrawingDisabled = i;
6151
6152# ifdef FEAT_AUTOCMD
6153 /* Trigger CmdwinLeave autocommands. */
6154 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6155# endif
6156
Bram Moolenaarccc18222007-05-10 18:25:20 +00006157 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 ccline = save_ccline;
6159 cmdwin_type = 0;
6160
6161 exmode_active = save_exmode;
6162
Bram Moolenaarf9821062008-06-20 16:31:07 +00006163 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 * this happens! */
6165 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6166 {
6167 cmdwin_result = Ctrl_C;
6168 EMSG(_("E199: Active window or buffer deleted"));
6169 }
6170 else
6171 {
6172# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6173 /* autocmds may abort script processing */
6174 if (aborting() && cmdwin_result != K_IGNORE)
6175 cmdwin_result = Ctrl_C;
6176# endif
6177 /* Set the new command line from the cmdline buffer. */
6178 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006179 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006181 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6182
6183 if (histtype == HIST_CMD)
6184 {
6185 /* Execute the command directly. */
6186 ccline.cmdbuff = vim_strsave((char_u *)p);
6187 cmdwin_result = CAR;
6188 }
6189 else
6190 {
6191 /* First need to cancel what we were doing. */
6192 ccline.cmdbuff = NULL;
6193 stuffcharReadbuff(':');
6194 stuffReadbuff((char_u *)p);
6195 stuffcharReadbuff(CAR);
6196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 }
6198 else if (cmdwin_result == K_XF2) /* :qa typed */
6199 {
6200 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6201 cmdwin_result = CAR;
6202 }
6203 else
6204 ccline.cmdbuff = vim_strsave(ml_get_curline());
6205 if (ccline.cmdbuff == NULL)
6206 cmdwin_result = Ctrl_C;
6207 else
6208 {
6209 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6210 ccline.cmdbufflen = ccline.cmdlen + 1;
6211 ccline.cmdpos = curwin->w_cursor.col;
6212 if (ccline.cmdpos > ccline.cmdlen)
6213 ccline.cmdpos = ccline.cmdlen;
6214 if (cmdwin_result == K_IGNORE)
6215 {
6216 set_cmdspos_cursor();
6217 redrawcmd();
6218 }
6219 }
6220
6221# ifdef FEAT_AUTOCMD
6222 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006223 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224# endif
6225 wp = curwin;
6226 bp = curbuf;
6227 win_goto(old_curwin);
6228 win_close(wp, TRUE);
6229 close_buffer(NULL, bp, DOBUF_WIPE);
6230
6231 /* Restore window sizes. */
6232 win_size_restore(&winsizes);
6233
6234# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006235 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236# endif
6237 }
6238
6239 ga_clear(&winsizes);
6240 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006241# ifdef FEAT_RIGHTLEFT
6242 cmdmsg_rl = save_cmdmsg_rl;
6243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244
6245 State = save_State;
6246# ifdef FEAT_MOUSE
6247 setmouse();
6248# endif
6249
6250 return cmdwin_result;
6251}
6252#endif /* FEAT_CMDWIN */
6253
6254/*
6255 * Used for commands that either take a simple command string argument, or:
6256 * cmd << endmarker
6257 * {script}
6258 * endmarker
6259 * Returns a pointer to allocated memory with {script} or NULL.
6260 */
6261 char_u *
6262script_get(eap, cmd)
6263 exarg_T *eap;
6264 char_u *cmd;
6265{
6266 char_u *theline;
6267 char *end_pattern = NULL;
6268 char dot[] = ".";
6269 garray_T ga;
6270
6271 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6272 return NULL;
6273
6274 ga_init2(&ga, 1, 0x400);
6275
6276 if (cmd[2] != NUL)
6277 end_pattern = (char *)skipwhite(cmd + 2);
6278 else
6279 end_pattern = dot;
6280
6281 for (;;)
6282 {
6283 theline = eap->getline(
6284#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006285 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286#endif
6287 NUL, eap->cookie, 0);
6288
6289 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006290 {
6291 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
6295 ga_concat(&ga, theline);
6296 ga_append(&ga, '\n');
6297 vim_free(theline);
6298 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006299 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300
6301 return (char_u *)ga.ga_data;
6302}