blob: 48ee8312cad0a0d7604ff8e04f8d3113a2ce5965 [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 Moolenaarbfd8fc02005-09-20 23:22:24 +000034 int xp_context; /* type of expansion */
35# ifdef FEAT_EVAL
36 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000037 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000039};
40
41static struct cmdline_info ccline; /* current cmdline_info */
42
43static int cmd_showtail; /* Only show path tail in lists ? */
44
45#ifdef FEAT_EVAL
46static int new_cmdpos; /* position set by set_cmdline_pos() */
47#endif
48
49#ifdef FEAT_CMDHIST
50typedef struct hist_entry
51{
52 int hisnum; /* identifying number */
53 char_u *hisstr; /* actual entry, separator char after the NUL */
54} histentry_T;
55
56static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
57static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
58static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
59 /* identifying (unique) number of newest history entry */
60static int hislen = 0; /* actual length of history tables */
61
62static int hist_char2type __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
64static int in_history __ARGS((int, char_u *, int));
65# ifdef FEAT_EVAL
66static int calc_hist_idx __ARGS((int histype, int num));
67# endif
68#endif
69
70#ifdef FEAT_RIGHTLEFT
71static int cmd_hkmap = 0; /* Hebrew mapping during command line */
72#endif
73
74#ifdef FEAT_FKMAP
75static int cmd_fkmap = 0; /* Farsi mapping during command line */
76#endif
77
78static int cmdline_charsize __ARGS((int idx));
79static void set_cmdspos __ARGS((void));
80static void set_cmdspos_cursor __ARGS((void));
81#ifdef FEAT_MBYTE
82static void correct_cmdspos __ARGS((int idx, int cells));
83#endif
84static void alloc_cmdbuff __ARGS((int len));
85static int realloc_cmdbuff __ARGS((int len));
86static void draw_cmdline __ARGS((int start, int len));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +000087static void save_cmdline __ARGS((struct cmdline_info *ccp));
88static void restore_cmdline __ARGS((struct cmdline_info *ccp));
Bram Moolenaar1769d5a2006-10-17 14:25:24 +000089static int cmdline_paste __ARGS((int regname, int literally, int remcr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
91static void redrawcmd_preedit __ARGS((void));
92#endif
93#ifdef FEAT_WILDMENU
94static void cmdline_del __ARGS((int from));
95#endif
96static void redrawcmdprompt __ARGS((void));
97static void cursorcmd __ARGS((void));
98static int ccheck_abbr __ARGS((int));
99static int nextwild __ARGS((expand_T *xp, int type, int options));
Bram Moolenaar45360022005-07-21 21:08:21 +0000100static void escape_fname __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101static int showmatches __ARGS((expand_T *xp, int wildmenu));
102static void set_expand_context __ARGS((expand_T *xp));
103static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int));
104static int expand_showtail __ARGS((expand_T *xp));
105#ifdef FEAT_CMDL_COMPL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000106static int expand_shellcmd __ARGS((char_u *filepat, int *num_file, char_u ***file, int flagsarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname));
108# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
109static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000110static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111# endif
112#endif
113
114#ifdef FEAT_CMDWIN
115static int ex_window __ARGS((void));
116#endif
117
118/*
119 * getcmdline() - accept a command line starting with firstc.
120 *
121 * firstc == ':' get ":" command line.
122 * firstc == '/' or '?' get search pattern
123 * firstc == '=' get expression
124 * firstc == '@' get text for input() function
125 * firstc == '>' get text for debug mode
126 * firstc == NUL get text for :insert command
127 * firstc == -1 like NUL, and break on CTRL-C
128 *
129 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
130 * command line.
131 *
132 * Careful: getcmdline() can be called recursively!
133 *
134 * Return pointer to allocated string if there is a commandline, NULL
135 * otherwise.
136 */
137/*ARGSUSED*/
138 char_u *
139getcmdline(firstc, count, indent)
140 int firstc;
141 long count; /* only used for incremental search */
142 int indent; /* indent for inside conditionals */
143{
144 int c;
145 int i;
146 int j;
147 int gotesc = FALSE; /* TRUE when <ESC> just typed */
148 int do_abbr; /* when TRUE check for abbr. */
149#ifdef FEAT_CMDHIST
150 char_u *lookfor = NULL; /* string to match */
151 int hiscnt; /* current history line in use */
152 int histype; /* history type to be used */
153#endif
154#ifdef FEAT_SEARCH_EXTRA
155 pos_T old_cursor;
156 colnr_T old_curswant;
157 colnr_T old_leftcol;
158 linenr_T old_topline;
159# ifdef FEAT_DIFF
160 int old_topfill;
161# endif
162 linenr_T old_botline;
163 int did_incsearch = FALSE;
164 int incsearch_postponed = FALSE;
165#endif
166 int did_wild_list = FALSE; /* did wild_list() recently */
167 int wim_index = 0; /* index in wim_flags[] */
168 int res;
169 int save_msg_scroll = msg_scroll;
170 int save_State = State; /* remember State when called */
171 int some_key_typed = FALSE; /* one of the keys was typed */
172#ifdef FEAT_MOUSE
173 /* mouse drag and release events are ignored, unless they are
174 * preceded with a mouse down event */
175 int ignore_drag_release = TRUE;
176#endif
177#ifdef FEAT_EVAL
178 int break_ctrl_c = FALSE;
179#endif
180 expand_T xpc;
181 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000182#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
183 /* Everything that may work recursively should save and restore the
184 * current command line in save_ccline. That includes update_screen(), a
185 * custom status line may invoke ":normal". */
186 struct cmdline_info save_ccline;
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
189#ifdef FEAT_SNIFF
190 want_sniff_request = 0;
191#endif
192#ifdef FEAT_EVAL
193 if (firstc == -1)
194 {
195 firstc = NUL;
196 break_ctrl_c = TRUE;
197 }
198#endif
199#ifdef FEAT_RIGHTLEFT
200 /* start without Hebrew mapping for a command line */
201 if (firstc == ':' || firstc == '=' || firstc == '>')
202 cmd_hkmap = 0;
203#endif
204
205 ccline.overstrike = FALSE; /* always start in insert mode */
206#ifdef FEAT_SEARCH_EXTRA
207 old_cursor = curwin->w_cursor; /* needs to be restored later */
208 old_curswant = curwin->w_curswant;
209 old_leftcol = curwin->w_leftcol;
210 old_topline = curwin->w_topline;
211# ifdef FEAT_DIFF
212 old_topfill = curwin->w_topfill;
213# endif
214 old_botline = curwin->w_botline;
215#endif
216
217 /*
218 * set some variables for redrawcmd()
219 */
220 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000221 ccline.cmdindent = (firstc > 0 ? indent : 0);
222
223 /* alloc initial ccline.cmdbuff */
224 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 if (ccline.cmdbuff == NULL)
226 return NULL; /* out of memory */
227 ccline.cmdlen = ccline.cmdpos = 0;
228 ccline.cmdbuff[0] = NUL;
229
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000230 /* autoindent for :insert and :append */
231 if (firstc <= 0)
232 {
233 copy_spaces(ccline.cmdbuff, indent);
234 ccline.cmdbuff[indent] = NUL;
235 ccline.cmdpos = indent;
236 ccline.cmdspos = indent;
237 ccline.cmdlen = indent;
238 }
239
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 ExpandInit(&xpc);
241
242#ifdef FEAT_RIGHTLEFT
243 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
244 && (firstc == '/' || firstc == '?'))
245 cmdmsg_rl = TRUE;
246 else
247 cmdmsg_rl = FALSE;
248#endif
249
250 redir_off = TRUE; /* don't redirect the typed command */
251 if (!cmd_silent)
252 {
253 i = msg_scrolled;
254 msg_scrolled = 0; /* avoid wait_return message */
255 gotocmdline(TRUE);
256 msg_scrolled += i;
257 redrawcmdprompt(); /* draw prompt or indent */
258 set_cmdspos();
259 }
260 xpc.xp_context = EXPAND_NOTHING;
261 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000262#ifndef BACKSLASH_IN_FILENAME
263 xpc.xp_shell = FALSE;
264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000266#if defined(FEAT_EVAL)
267 if (ccline.input_fn)
268 {
269 xpc.xp_context = ccline.xp_context;
270 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000271# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000272 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000273# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000274 }
275#endif
276
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 /*
278 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
279 * doing ":@0" when register 0 doesn't contain a CR.
280 */
281 msg_scroll = FALSE;
282
283 State = CMDLINE;
284
285 if (firstc == '/' || firstc == '?' || firstc == '@')
286 {
287 /* Use ":lmap" mappings for search pattern and input(). */
288 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
289 b_im_ptr = &curbuf->b_p_iminsert;
290 else
291 b_im_ptr = &curbuf->b_p_imsearch;
292 if (*b_im_ptr == B_IMODE_LMAP)
293 State |= LANGMAP;
294#ifdef USE_IM_CONTROL
295 im_set_active(*b_im_ptr == B_IMODE_IM);
296#endif
297 }
298#ifdef USE_IM_CONTROL
299 else if (p_imcmdline)
300 im_set_active(TRUE);
301#endif
302
303#ifdef FEAT_MOUSE
304 setmouse();
305#endif
306#ifdef CURSOR_SHAPE
307 ui_cursor_shape(); /* may show different cursor shape */
308#endif
309
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000310 /* When inside an autocommand for writing "exiting" may be set and
311 * terminal mode set to cooked. Need to set raw mode here then. */
312 settmode(TMODE_RAW);
313
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#ifdef FEAT_CMDHIST
315 init_history();
316 hiscnt = hislen; /* set hiscnt to impossible history value */
317 histype = hist_char2type(firstc);
318#endif
319
320#ifdef FEAT_DIGRAPHS
321 do_digraph(-1); /* init digraph typahead */
322#endif
323
324 /*
325 * Collect the command string, handling editing keys.
326 */
327 for (;;)
328 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000329 redir_off = TRUE; /* Don't redirect the typed command.
330 Repeated, because a ":redir" inside
331 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#ifdef USE_ON_FLY_SCROLL
333 dont_scroll = FALSE; /* allow scrolling here */
334#endif
335 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
336
337 cursorcmd(); /* set the cursor on the right spot */
338 c = safe_vgetc();
339 if (KeyTyped)
340 {
341 some_key_typed = TRUE;
342#ifdef FEAT_RIGHTLEFT
343 if (cmd_hkmap)
344 c = hkmap(c);
345# ifdef FEAT_FKMAP
346 if (cmd_fkmap)
347 c = cmdl_fkmap(c);
348# endif
349 if (cmdmsg_rl && !KeyStuffed)
350 {
351 /* Invert horizontal movements and operations. Only when
352 * typed by the user directly, not when the result of a
353 * mapping. */
354 switch (c)
355 {
356 case K_RIGHT: c = K_LEFT; break;
357 case K_S_RIGHT: c = K_S_LEFT; break;
358 case K_C_RIGHT: c = K_C_LEFT; break;
359 case K_LEFT: c = K_RIGHT; break;
360 case K_S_LEFT: c = K_S_RIGHT; break;
361 case K_C_LEFT: c = K_C_RIGHT; break;
362 }
363 }
364#endif
365 }
366
367 /*
368 * Ignore got_int when CTRL-C was typed here.
369 * Don't ignore it in :global, we really need to break then, e.g., for
370 * ":g/pat/normal /pat" (without the <CR>).
371 * Don't ignore it for the input() function.
372 */
373 if ((c == Ctrl_C
374#ifdef UNIX
375 || c == intr_char
376#endif
377 )
378#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
379 && firstc != '@'
380#endif
381#ifdef FEAT_EVAL
382 && !break_ctrl_c
383#endif
384 && !global_busy)
385 got_int = FALSE;
386
387#ifdef FEAT_CMDHIST
388 /* free old command line when finished moving around in the history
389 * list */
390 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000391 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000392 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 && c != K_PAGEDOWN && c != K_PAGEUP
394 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000395 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
397 {
398 vim_free(lookfor);
399 lookfor = NULL;
400 }
401#endif
402
403 /*
404 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
405 */
406 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles != -1)
407 c = Ctrl_P;
408
409#ifdef FEAT_WILDMENU
410 /* Special translations for 'wildmenu' */
411 if (did_wild_list && p_wmnu)
412 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000413 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000415 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 c = Ctrl_N;
417 }
418 /* Hitting CR after "emenu Name.": complete submenu */
419 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
420 && ccline.cmdpos > 1
421 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
422 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
423 && (c == '\n' || c == '\r' || c == K_KENTER))
424 c = K_DOWN;
425#endif
426
427 /* free expanded names when finished walking through matches */
428 if (xpc.xp_numfiles != -1
429 && !(c == p_wc && KeyTyped) && c != p_wcm
430 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
431 && c != Ctrl_L)
432 {
433 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
434 did_wild_list = FALSE;
435#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000436 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437#endif
438 xpc.xp_context = EXPAND_NOTHING;
439 wim_index = 0;
440#ifdef FEAT_WILDMENU
441 if (p_wmnu && wild_menu_showing != 0)
442 {
443 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000444 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000445
446 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000447 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448
449 if (wild_menu_showing == WM_SCROLLED)
450 {
451 /* Entered command line, move it up */
452 cmdline_row--;
453 redrawcmd();
454 }
455 else if (save_p_ls != -1)
456 {
457 /* restore 'laststatus' and 'winminheight' */
458 p_ls = save_p_ls;
459 p_wmh = save_p_wmh;
460 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000461 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000463 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 redrawcmd();
465 save_p_ls = -1;
466 }
467 else
468 {
469# ifdef FEAT_VERTSPLIT
470 win_redraw_last_status(topframe);
471# else
472 lastwin->w_redr_status = TRUE;
473# endif
474 redraw_statuslines();
475 }
476 KeyTyped = skt;
477 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000478 if (ccline.input_fn)
479 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 }
481#endif
482 }
483
484#ifdef FEAT_WILDMENU
485 /* Special translations for 'wildmenu' */
486 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
487 {
488 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000489 if (c == K_DOWN && ccline.cmdpos > 0
490 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000492 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 {
494 /* Hitting <Up>: Remove one submenu name in front of the
495 * cursor */
496 int found = FALSE;
497
498 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
499 i = 0;
500 while (--j > 0)
501 {
502 /* check for start of menu name */
503 if (ccline.cmdbuff[j] == ' '
504 && ccline.cmdbuff[j - 1] != '\\')
505 {
506 i = j + 1;
507 break;
508 }
509 /* check for start of submenu name */
510 if (ccline.cmdbuff[j] == '.'
511 && ccline.cmdbuff[j - 1] != '\\')
512 {
513 if (found)
514 {
515 i = j + 1;
516 break;
517 }
518 else
519 found = TRUE;
520 }
521 }
522 if (i > 0)
523 cmdline_del(i);
524 c = p_wc;
525 xpc.xp_context = EXPAND_NOTHING;
526 }
527 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000528 if ((xpc.xp_context == EXPAND_FILES
529 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {
531 char_u upseg[5];
532
533 upseg[0] = PATHSEP;
534 upseg[1] = '.';
535 upseg[2] = '.';
536 upseg[3] = PATHSEP;
537 upseg[4] = NUL;
538
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000539 if (c == K_DOWN
540 && ccline.cmdpos > 0
541 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
542 && (ccline.cmdpos < 3
543 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
545 {
546 /* go down a directory */
547 c = p_wc;
548 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000549 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {
551 /* If in a direct ancestor, strip off one ../ to go down */
552 int found = FALSE;
553
554 j = ccline.cmdpos;
555 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
556 while (--j > i)
557 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000558#ifdef FEAT_MBYTE
559 if (has_mbyte)
560 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 if (vim_ispathsep(ccline.cmdbuff[j]))
563 {
564 found = TRUE;
565 break;
566 }
567 }
568 if (found
569 && ccline.cmdbuff[j - 1] == '.'
570 && ccline.cmdbuff[j - 2] == '.'
571 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
572 {
573 cmdline_del(j - 2);
574 c = p_wc;
575 }
576 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000577 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {
579 /* go up a directory */
580 int found = FALSE;
581
582 j = ccline.cmdpos - 1;
583 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
584 while (--j > i)
585 {
586#ifdef FEAT_MBYTE
587 if (has_mbyte)
588 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
589#endif
590 if (vim_ispathsep(ccline.cmdbuff[j])
591#ifdef BACKSLASH_IN_FILENAME
592 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
593 == NULL
594#endif
595 )
596 {
597 if (found)
598 {
599 i = j + 1;
600 break;
601 }
602 else
603 found = TRUE;
604 }
605 }
606
607 if (!found)
608 j = i;
609 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
610 j += 4;
611 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
612 && j == i)
613 j += 3;
614 else
615 j = 0;
616 if (j > 0)
617 {
618 /* TODO this is only for DOS/UNIX systems - need to put in
619 * machine-specific stuff here and in upseg init */
620 cmdline_del(j);
621 put_on_cmdline(upseg + 1, 3, FALSE);
622 }
623 else if (ccline.cmdpos > i)
624 cmdline_del(i);
625 c = p_wc;
626 }
627 }
628#if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
629 if (p_wmnu
630 && (xpc.xp_context == EXPAND_FILES
631 || xpc.xp_context == EXPAND_MENUNAMES)
632 && (c == K_UP || c == K_DOWN))
633 xpc.xp_context = EXPAND_NOTHING;
634#endif
635
636#endif /* FEAT_WILDMENU */
637
638 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
639 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
640 if (c == Ctrl_BSL)
641 {
642 ++no_mapping;
643 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000644 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 --no_mapping;
646 --allow_keys;
647 /* CTRL-\ e doesn't work when obtaining an expression. */
648 if (c != Ctrl_N && c != Ctrl_G
649 && (c != 'e' || ccline.cmdfirstc == '='))
650 {
651 vungetc(c);
652 c = Ctrl_BSL;
653 }
654#ifdef FEAT_EVAL
655 else if (c == 'e')
656 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000657 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658
659 /*
660 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000661 * Need to save and restore the current command line, to be
662 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 */
664 if (ccline.cmdpos == ccline.cmdlen)
665 new_cmdpos = 99999; /* keep it at the end */
666 else
667 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000668
669 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000671 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if (c == '=')
673 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000674 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000675 * to avoid nasty things like going to another buffer when
676 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000677 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000678 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000680 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000681 restore_cmdline(&save_ccline);
682
683 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000685 ccline.cmdlen = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 STRCPY(ccline.cmdbuff, p);
687 vim_free(p);
688
689 /* Restore the cursor or use the position set with
690 * set_cmdline_pos(). */
691 if (new_cmdpos > ccline.cmdlen)
692 ccline.cmdpos = ccline.cmdlen;
693 else
694 ccline.cmdpos = new_cmdpos;
695
696 KeyTyped = FALSE; /* Don't do p_wc completion. */
697 redrawcmd();
698 goto cmdline_changed;
699 }
700 }
701 beep_flush();
702 c = ESC;
703 }
704#endif
705 else
706 {
707 if (c == Ctrl_G && p_im && restart_edit == 0)
708 restart_edit = 'a';
709 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
710 in history */
711 goto returncmd; /* back to Normal mode */
712 }
713 }
714
715#ifdef FEAT_CMDWIN
716 if (c == cedit_key || c == K_CMDWIN)
717 {
718 /*
719 * Open a window to edit the command line (and history).
720 */
721 c = ex_window();
722 some_key_typed = TRUE;
723 }
724# ifdef FEAT_DIGRAPHS
725 else
726# endif
727#endif
728#ifdef FEAT_DIGRAPHS
729 c = do_digraph(c);
730#endif
731
732 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
733 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
734 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000735 /* In Ex mode a backslash escapes a newline. */
736 if (exmode_active
737 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000738 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000739 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000740 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000742 if (c == K_KENTER)
743 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000745 else
746 {
747 gotesc = FALSE; /* Might have typed ESC previously, don't
748 truncate the cmdline now. */
749 if (ccheck_abbr(c + ABBR_OFF))
750 goto cmdline_changed;
751 if (!cmd_silent)
752 {
753 windgoto(msg_row, 0);
754 out_flush();
755 }
756 break;
757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 }
759
760 /*
761 * Completion for 'wildchar' or 'wildcharm' key.
762 * - hitting <ESC> twice means: abandon command line.
763 * - wildcard expansion is only done when the 'wildchar' key is really
764 * typed, not when it comes from a macro
765 */
766 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
767 {
768 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
769 {
770 /* if 'wildmode' contains "list" may still need to list */
771 if (xpc.xp_numfiles > 1
772 && !did_wild_list
773 && (wim_flags[wim_index] & WIM_LIST))
774 {
775 (void)showmatches(&xpc, FALSE);
776 redrawcmd();
777 did_wild_list = TRUE;
778 }
779 if (wim_flags[wim_index] & WIM_LONGEST)
780 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
781 else if (wim_flags[wim_index] & WIM_FULL)
782 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
783 else
784 res = OK; /* don't insert 'wildchar' now */
785 }
786 else /* typed p_wc first time */
787 {
788 wim_index = 0;
789 j = ccline.cmdpos;
790 /* if 'wildmode' first contains "longest", get longest
791 * common part */
792 if (wim_flags[0] & WIM_LONGEST)
793 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
794 else
795 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
796
797 /* if interrupted while completing, behave like it failed */
798 if (got_int)
799 {
800 (void)vpeekc(); /* remove <C-C> from input stream */
801 got_int = FALSE; /* don't abandon the command line */
802 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
803#ifdef FEAT_WILDMENU
804 xpc.xp_context = EXPAND_NOTHING;
805#endif
806 goto cmdline_changed;
807 }
808
809 /* when more than one match, and 'wildmode' first contains
810 * "list", or no change and 'wildmode' contains "longest,list",
811 * list all matches */
812 if (res == OK && xpc.xp_numfiles > 1)
813 {
814 /* a "longest" that didn't do anything is skipped (but not
815 * "list:longest") */
816 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
817 wim_index = 1;
818 if ((wim_flags[wim_index] & WIM_LIST)
819#ifdef FEAT_WILDMENU
820 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
821#endif
822 )
823 {
824 if (!(wim_flags[0] & WIM_LONGEST))
825 {
826#ifdef FEAT_WILDMENU
827 int p_wmnu_save = p_wmnu;
828 p_wmnu = 0;
829#endif
830 nextwild(&xpc, WILD_PREV, 0); /* remove match */
831#ifdef FEAT_WILDMENU
832 p_wmnu = p_wmnu_save;
833#endif
834 }
835#ifdef FEAT_WILDMENU
836 (void)showmatches(&xpc, p_wmnu
837 && ((wim_flags[wim_index] & WIM_LIST) == 0));
838#else
839 (void)showmatches(&xpc, FALSE);
840#endif
841 redrawcmd();
842 did_wild_list = TRUE;
843 if (wim_flags[wim_index] & WIM_LONGEST)
844 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
845 else if (wim_flags[wim_index] & WIM_FULL)
846 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
847 }
848 else
849 vim_beep();
850 }
851#ifdef FEAT_WILDMENU
852 else if (xpc.xp_numfiles == -1)
853 xpc.xp_context = EXPAND_NOTHING;
854#endif
855 }
856 if (wim_index < 3)
857 ++wim_index;
858 if (c == ESC)
859 gotesc = TRUE;
860 if (res == OK)
861 goto cmdline_changed;
862 }
863
864 gotesc = FALSE;
865
866 /* <S-Tab> goes to last match, in a clumsy way */
867 if (c == K_S_TAB && KeyTyped)
868 {
869 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
870 && nextwild(&xpc, WILD_PREV, 0) == OK
871 && nextwild(&xpc, WILD_PREV, 0) == OK)
872 goto cmdline_changed;
873 }
874
875 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
876 c = NL;
877
878 do_abbr = TRUE; /* default: check for abbreviation */
879
880 /*
881 * Big switch for a typed command line character.
882 */
883 switch (c)
884 {
885 case K_BS:
886 case Ctrl_H:
887 case K_DEL:
888 case K_KDEL:
889 case Ctrl_W:
890#ifdef FEAT_FKMAP
891 if (cmd_fkmap && c == K_BS)
892 c = K_DEL;
893#endif
894 if (c == K_KDEL)
895 c = K_DEL;
896
897 /*
898 * delete current character is the same as backspace on next
899 * character, except at end of line
900 */
901 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
902 ++ccline.cmdpos;
903#ifdef FEAT_MBYTE
904 if (has_mbyte && c == K_DEL)
905 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
906 ccline.cmdbuff + ccline.cmdpos);
907#endif
908 if (ccline.cmdpos > 0)
909 {
910 char_u *p;
911
912 j = ccline.cmdpos;
913 p = ccline.cmdbuff + j;
914#ifdef FEAT_MBYTE
915 if (has_mbyte)
916 {
917 p = mb_prevptr(ccline.cmdbuff, p);
918 if (c == Ctrl_W)
919 {
920 while (p > ccline.cmdbuff && vim_isspace(*p))
921 p = mb_prevptr(ccline.cmdbuff, p);
922 i = mb_get_class(p);
923 while (p > ccline.cmdbuff && mb_get_class(p) == i)
924 p = mb_prevptr(ccline.cmdbuff, p);
925 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000926 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 }
928 }
929 else
930#endif
931 if (c == Ctrl_W)
932 {
933 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
934 --p;
935 i = vim_iswordc(p[-1]);
936 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
937 && vim_iswordc(p[-1]) == i)
938 --p;
939 }
940 else
941 --p;
942 ccline.cmdpos = (int)(p - ccline.cmdbuff);
943 ccline.cmdlen -= j - ccline.cmdpos;
944 i = ccline.cmdpos;
945 while (i < ccline.cmdlen)
946 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
947
948 /* Truncate at the end, required for multi-byte chars. */
949 ccline.cmdbuff[ccline.cmdlen] = NUL;
950 redrawcmd();
951 }
952 else if (ccline.cmdlen == 0 && c != Ctrl_W
953 && ccline.cmdprompt == NULL && indent == 0)
954 {
955 /* In ex and debug mode it doesn't make sense to return. */
956 if (exmode_active
957#ifdef FEAT_EVAL
958 || ccline.cmdfirstc == '>'
959#endif
960 )
961 goto cmdline_not_changed;
962
963 vim_free(ccline.cmdbuff); /* no commandline to return */
964 ccline.cmdbuff = NULL;
965 if (!cmd_silent)
966 {
967#ifdef FEAT_RIGHTLEFT
968 if (cmdmsg_rl)
969 msg_col = Columns;
970 else
971#endif
972 msg_col = 0;
973 msg_putchar(' '); /* delete ':' */
974 }
975 redraw_cmdline = TRUE;
976 goto returncmd; /* back to cmd mode */
977 }
978 goto cmdline_changed;
979
980 case K_INS:
981 case K_KINS:
982#ifdef FEAT_FKMAP
983 /* if Farsi mode set, we are in reverse insert mode -
984 Do not change the mode */
985 if (cmd_fkmap)
986 beep_flush();
987 else
988#endif
989 ccline.overstrike = !ccline.overstrike;
990#ifdef CURSOR_SHAPE
991 ui_cursor_shape(); /* may show different cursor shape */
992#endif
993 goto cmdline_not_changed;
994
995 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000996 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 {
998 /* ":lmap" mappings exists, toggle use of mappings. */
999 State ^= LANGMAP;
1000#ifdef USE_IM_CONTROL
1001 im_set_active(FALSE); /* Disable input method */
1002#endif
1003 if (b_im_ptr != NULL)
1004 {
1005 if (State & LANGMAP)
1006 *b_im_ptr = B_IMODE_LMAP;
1007 else
1008 *b_im_ptr = B_IMODE_NONE;
1009 }
1010 }
1011#ifdef USE_IM_CONTROL
1012 else
1013 {
1014 /* There are no ":lmap" mappings, toggle IM. When
1015 * 'imdisable' is set don't try getting the status, it's
1016 * always off. */
1017 if ((p_imdisable && b_im_ptr != NULL)
1018 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1019 {
1020 im_set_active(FALSE); /* Disable input method */
1021 if (b_im_ptr != NULL)
1022 *b_im_ptr = B_IMODE_NONE;
1023 }
1024 else
1025 {
1026 im_set_active(TRUE); /* Enable input method */
1027 if (b_im_ptr != NULL)
1028 *b_im_ptr = B_IMODE_IM;
1029 }
1030 }
1031#endif
1032 if (b_im_ptr != NULL)
1033 {
1034 if (b_im_ptr == &curbuf->b_p_iminsert)
1035 set_iminsert_global();
1036 else
1037 set_imsearch_global();
1038 }
1039#ifdef CURSOR_SHAPE
1040 ui_cursor_shape(); /* may show different cursor shape */
1041#endif
1042#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1043 /* Show/unshow value of 'keymap' in status lines later. */
1044 status_redraw_curbuf();
1045#endif
1046 goto cmdline_not_changed;
1047
1048/* case '@': only in very old vi */
1049 case Ctrl_U:
1050 /* delete all characters left of the cursor */
1051 j = ccline.cmdpos;
1052 ccline.cmdlen -= j;
1053 i = ccline.cmdpos = 0;
1054 while (i < ccline.cmdlen)
1055 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1056 /* Truncate at the end, required for multi-byte chars. */
1057 ccline.cmdbuff[ccline.cmdlen] = NUL;
1058 redrawcmd();
1059 goto cmdline_changed;
1060
1061#ifdef FEAT_CLIPBOARD
1062 case Ctrl_Y:
1063 /* Copy the modeless selection, if there is one. */
1064 if (clip_star.state != SELECT_CLEARED)
1065 {
1066 if (clip_star.state == SELECT_DONE)
1067 clip_copy_modeless_selection(TRUE);
1068 goto cmdline_not_changed;
1069 }
1070 break;
1071#endif
1072
1073 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1074 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001075 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001076 * ":normal" runs out of characters. */
1077 if (exmode_active
1078#ifdef FEAT_EX_EXTRA
1079 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1080#endif
1081 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 goto cmdline_not_changed;
1083
1084 gotesc = TRUE; /* will free ccline.cmdbuff after
1085 putting it in history */
1086 goto returncmd; /* back to cmd mode */
1087
1088 case Ctrl_R: /* insert register */
1089#ifdef USE_ON_FLY_SCROLL
1090 dont_scroll = TRUE; /* disallow scrolling here */
1091#endif
1092 putcmdline('"', TRUE);
1093 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001094 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 if (i == Ctrl_O)
1096 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1097 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001098 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 --no_mapping;
1100#ifdef FEAT_EVAL
1101 /*
1102 * Insert the result of an expression.
1103 * Need to save the current command line, to be able to enter
1104 * a new one...
1105 */
1106 new_cmdpos = -1;
1107 if (c == '=')
1108 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1110 {
1111 beep_flush();
1112 c = ESC;
1113 }
1114 else
1115 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001116 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001118 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120 }
1121#endif
1122 if (c != ESC) /* use ESC to cancel inserting register */
1123 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001124 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001125
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001126#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001127 /* When there was a serious error abort getting the
1128 * command line. */
1129 if (aborting())
1130 {
1131 gotesc = TRUE; /* will free ccline.cmdbuff after
1132 putting it in history */
1133 goto returncmd; /* back to cmd mode */
1134 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 KeyTyped = FALSE; /* Don't do p_wc completion. */
1137#ifdef FEAT_EVAL
1138 if (new_cmdpos >= 0)
1139 {
1140 /* set_cmdline_pos() was used */
1141 if (new_cmdpos > ccline.cmdlen)
1142 ccline.cmdpos = ccline.cmdlen;
1143 else
1144 ccline.cmdpos = new_cmdpos;
1145 }
1146#endif
1147 }
1148 redrawcmd();
1149 goto cmdline_changed;
1150
1151 case Ctrl_D:
1152 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1153 break; /* Use ^D as normal char instead */
1154
1155 redrawcmd();
1156 continue; /* don't do incremental search now */
1157
1158 case K_RIGHT:
1159 case K_S_RIGHT:
1160 case K_C_RIGHT:
1161 do
1162 {
1163 if (ccline.cmdpos >= ccline.cmdlen)
1164 break;
1165 i = cmdline_charsize(ccline.cmdpos);
1166 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1167 break;
1168 ccline.cmdspos += i;
1169#ifdef FEAT_MBYTE
1170 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001171 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 + ccline.cmdpos);
1173 else
1174#endif
1175 ++ccline.cmdpos;
1176 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001177 while ((c == K_S_RIGHT || c == K_C_RIGHT
1178 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1180#ifdef FEAT_MBYTE
1181 if (has_mbyte)
1182 set_cmdspos_cursor();
1183#endif
1184 goto cmdline_not_changed;
1185
1186 case K_LEFT:
1187 case K_S_LEFT:
1188 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001189 if (ccline.cmdpos == 0)
1190 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 do
1192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 --ccline.cmdpos;
1194#ifdef FEAT_MBYTE
1195 if (has_mbyte) /* move to first byte of char */
1196 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1197 ccline.cmdbuff + ccline.cmdpos);
1198#endif
1199 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1200 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001201 while (ccline.cmdpos > 0
1202 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001203 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1205#ifdef FEAT_MBYTE
1206 if (has_mbyte)
1207 set_cmdspos_cursor();
1208#endif
1209 goto cmdline_not_changed;
1210
1211 case K_IGNORE:
1212 goto cmdline_not_changed; /* Ignore mouse */
1213
Bram Moolenaar4770d092006-01-12 23:22:24 +00001214#ifdef FEAT_GUI_W32
1215 /* On Win32 ignore <M-F4>, we get it when closing the window was
1216 * cancelled. */
1217 case K_F4:
1218 if (mod_mask == MOD_MASK_ALT)
1219 {
1220 redrawcmd(); /* somehow the cmdline is cleared */
1221 goto cmdline_not_changed;
1222 }
1223 break;
1224#endif
1225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226#ifdef FEAT_MOUSE
1227 case K_MIDDLEDRAG:
1228 case K_MIDDLERELEASE:
1229 goto cmdline_not_changed; /* Ignore mouse */
1230
1231 case K_MIDDLEMOUSE:
1232# ifdef FEAT_GUI
1233 /* When GUI is active, also paste when 'mouse' is empty */
1234 if (!gui.in_use)
1235# endif
1236 if (!mouse_has(MOUSE_COMMAND))
1237 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001238# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001240 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001242# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001243 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 redrawcmd();
1245 goto cmdline_changed;
1246
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001247# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001249 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 redrawcmd();
1251 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
1254 case K_LEFTDRAG:
1255 case K_LEFTRELEASE:
1256 case K_RIGHTDRAG:
1257 case K_RIGHTRELEASE:
1258 /* Ignore drag and release events when the button-down wasn't
1259 * seen before. */
1260 if (ignore_drag_release)
1261 goto cmdline_not_changed;
1262 /* FALLTHROUGH */
1263 case K_LEFTMOUSE:
1264 case K_RIGHTMOUSE:
1265 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1266 ignore_drag_release = TRUE;
1267 else
1268 ignore_drag_release = FALSE;
1269# ifdef FEAT_GUI
1270 /* When GUI is active, also move when 'mouse' is empty */
1271 if (!gui.in_use)
1272# endif
1273 if (!mouse_has(MOUSE_COMMAND))
1274 goto cmdline_not_changed; /* Ignore mouse */
1275# ifdef FEAT_CLIPBOARD
1276 if (mouse_row < cmdline_row && clip_star.available)
1277 {
1278 int button, is_click, is_drag;
1279
1280 /*
1281 * Handle modeless selection.
1282 */
1283 button = get_mouse_button(KEY2TERMCAP1(c),
1284 &is_click, &is_drag);
1285 if (mouse_model_popup() && button == MOUSE_LEFT
1286 && (mod_mask & MOD_MASK_SHIFT))
1287 {
1288 /* Translate shift-left to right button. */
1289 button = MOUSE_RIGHT;
1290 mod_mask &= ~MOD_MASK_SHIFT;
1291 }
1292 clip_modeless(button, is_click, is_drag);
1293 goto cmdline_not_changed;
1294 }
1295# endif
1296
1297 set_cmdspos();
1298 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1299 ++ccline.cmdpos)
1300 {
1301 i = cmdline_charsize(ccline.cmdpos);
1302 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1303 && mouse_col < ccline.cmdspos % Columns + i)
1304 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001305# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 if (has_mbyte)
1307 {
1308 /* Count ">" for double-wide char that doesn't fit. */
1309 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001310 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 + ccline.cmdpos) - 1;
1312 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 ccline.cmdspos += i;
1315 }
1316 goto cmdline_not_changed;
1317
1318 /* Mouse scroll wheel: ignored here */
1319 case K_MOUSEDOWN:
1320 case K_MOUSEUP:
1321 /* Alternate buttons ignored here */
1322 case K_X1MOUSE:
1323 case K_X1DRAG:
1324 case K_X1RELEASE:
1325 case K_X2MOUSE:
1326 case K_X2DRAG:
1327 case K_X2RELEASE:
1328 goto cmdline_not_changed;
1329
1330#endif /* FEAT_MOUSE */
1331
1332#ifdef FEAT_GUI
1333 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1334 case K_LEFTRELEASE_NM:
1335 goto cmdline_not_changed;
1336
1337 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001338 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 {
1340 gui_do_scroll();
1341 redrawcmd();
1342 }
1343 goto cmdline_not_changed;
1344
1345 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001346 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 {
1348 gui_do_horiz_scroll();
1349 redrawcmd();
1350 }
1351 goto cmdline_not_changed;
1352#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001353#ifdef FEAT_GUI_TABLINE
1354 case K_TABLINE:
1355 case K_TABMENU:
1356 /* Don't want to change any tabs here. Make sure the same tab
1357 * is still selected. */
1358 if (gui_use_tabline())
1359 gui_mch_set_curtab(tabpage_index(curtab));
1360 goto cmdline_not_changed;
1361#endif
1362
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 case K_SELECT: /* end of Select mode mapping - ignore */
1364 goto cmdline_not_changed;
1365
1366 case Ctrl_B: /* begin of command line */
1367 case K_HOME:
1368 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 case K_S_HOME:
1370 case K_C_HOME:
1371 ccline.cmdpos = 0;
1372 set_cmdspos();
1373 goto cmdline_not_changed;
1374
1375 case Ctrl_E: /* end of command line */
1376 case K_END:
1377 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 case K_S_END:
1379 case K_C_END:
1380 ccline.cmdpos = ccline.cmdlen;
1381 set_cmdspos_cursor();
1382 goto cmdline_not_changed;
1383
1384 case Ctrl_A: /* all matches */
1385 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1386 break;
1387 goto cmdline_changed;
1388
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001389 case Ctrl_L:
1390#ifdef FEAT_SEARCH_EXTRA
1391 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1392 {
1393 /* Add a character from under the cursor for 'incsearch' */
1394 if (did_incsearch
1395 && !equalpos(curwin->w_cursor, old_cursor))
1396 {
1397 c = gchar_cursor();
1398 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001399 {
1400 if (c == firstc || vim_strchr((char_u *)(
1401 p_magic ? "\\^$.*[" : "\\^$"), c)
1402 != NULL)
1403 {
1404 /* put a backslash before special characters */
1405 stuffcharReadbuff(c);
1406 c = '\\';
1407 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001408 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001409 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001410 }
1411 goto cmdline_not_changed;
1412 }
1413#endif
1414
1415 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1417 break;
1418 goto cmdline_changed;
1419
1420 case Ctrl_N: /* next match */
1421 case Ctrl_P: /* previous match */
1422 if (xpc.xp_numfiles > 0)
1423 {
1424 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1425 == FAIL)
1426 break;
1427 goto cmdline_changed;
1428 }
1429
1430#ifdef FEAT_CMDHIST
1431 case K_UP:
1432 case K_DOWN:
1433 case K_S_UP:
1434 case K_S_DOWN:
1435 case K_PAGEUP:
1436 case K_KPAGEUP:
1437 case K_PAGEDOWN:
1438 case K_KPAGEDOWN:
1439 if (hislen == 0 || firstc == NUL) /* no history */
1440 goto cmdline_not_changed;
1441
1442 i = hiscnt;
1443
1444 /* save current command string so it can be restored later */
1445 if (lookfor == NULL)
1446 {
1447 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1448 goto cmdline_not_changed;
1449 lookfor[ccline.cmdpos] = NUL;
1450 }
1451
1452 j = (int)STRLEN(lookfor);
1453 for (;;)
1454 {
1455 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001456 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001457 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 {
1459 if (hiscnt == hislen) /* first time */
1460 hiscnt = hisidx[histype];
1461 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1462 hiscnt = hislen - 1;
1463 else if (hiscnt != hisidx[histype] + 1)
1464 --hiscnt;
1465 else /* at top of list */
1466 {
1467 hiscnt = i;
1468 break;
1469 }
1470 }
1471 else /* one step forwards */
1472 {
1473 /* on last entry, clear the line */
1474 if (hiscnt == hisidx[histype])
1475 {
1476 hiscnt = hislen;
1477 break;
1478 }
1479
1480 /* not on a history line, nothing to do */
1481 if (hiscnt == hislen)
1482 break;
1483 if (hiscnt == hislen - 1) /* wrap around */
1484 hiscnt = 0;
1485 else
1486 ++hiscnt;
1487 }
1488 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1489 {
1490 hiscnt = i;
1491 break;
1492 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001493 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001494 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 || STRNCMP(history[histype][hiscnt].hisstr,
1496 lookfor, (size_t)j) == 0)
1497 break;
1498 }
1499
1500 if (hiscnt != i) /* jumped to other entry */
1501 {
1502 char_u *p;
1503 int len;
1504 int old_firstc;
1505
1506 vim_free(ccline.cmdbuff);
1507 if (hiscnt == hislen)
1508 p = lookfor; /* back to the old one */
1509 else
1510 p = history[histype][hiscnt].hisstr;
1511
1512 if (histype == HIST_SEARCH
1513 && p != lookfor
1514 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1515 {
1516 /* Correct for the separator character used when
1517 * adding the history entry vs the one used now.
1518 * First loop: count length.
1519 * Second loop: copy the characters. */
1520 for (i = 0; i <= 1; ++i)
1521 {
1522 len = 0;
1523 for (j = 0; p[j] != NUL; ++j)
1524 {
1525 /* Replace old sep with new sep, unless it is
1526 * escaped. */
1527 if (p[j] == old_firstc
1528 && (j == 0 || p[j - 1] != '\\'))
1529 {
1530 if (i > 0)
1531 ccline.cmdbuff[len] = firstc;
1532 }
1533 else
1534 {
1535 /* Escape new sep, unless it is already
1536 * escaped. */
1537 if (p[j] == firstc
1538 && (j == 0 || p[j - 1] != '\\'))
1539 {
1540 if (i > 0)
1541 ccline.cmdbuff[len] = '\\';
1542 ++len;
1543 }
1544 if (i > 0)
1545 ccline.cmdbuff[len] = p[j];
1546 }
1547 ++len;
1548 }
1549 if (i == 0)
1550 {
1551 alloc_cmdbuff(len);
1552 if (ccline.cmdbuff == NULL)
1553 goto returncmd;
1554 }
1555 }
1556 ccline.cmdbuff[len] = NUL;
1557 }
1558 else
1559 {
1560 alloc_cmdbuff((int)STRLEN(p));
1561 if (ccline.cmdbuff == NULL)
1562 goto returncmd;
1563 STRCPY(ccline.cmdbuff, p);
1564 }
1565
1566 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1567 redrawcmd();
1568 goto cmdline_changed;
1569 }
1570 beep_flush();
1571 goto cmdline_not_changed;
1572#endif
1573
1574 case Ctrl_V:
1575 case Ctrl_Q:
1576#ifdef FEAT_MOUSE
1577 ignore_drag_release = TRUE;
1578#endif
1579 putcmdline('^', TRUE);
1580 c = get_literal(); /* get next (two) character(s) */
1581 do_abbr = FALSE; /* don't do abbreviation now */
1582#ifdef FEAT_MBYTE
1583 /* may need to remove ^ when composing char was typed */
1584 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1585 {
1586 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1587 msg_putchar(' ');
1588 cursorcmd();
1589 }
1590#endif
1591 break;
1592
1593#ifdef FEAT_DIGRAPHS
1594 case Ctrl_K:
1595#ifdef FEAT_MOUSE
1596 ignore_drag_release = TRUE;
1597#endif
1598 putcmdline('?', TRUE);
1599#ifdef USE_ON_FLY_SCROLL
1600 dont_scroll = TRUE; /* disallow scrolling here */
1601#endif
1602 c = get_digraph(TRUE);
1603 if (c != NUL)
1604 break;
1605
1606 redrawcmd();
1607 goto cmdline_not_changed;
1608#endif /* FEAT_DIGRAPHS */
1609
1610#ifdef FEAT_RIGHTLEFT
1611 case Ctrl__: /* CTRL-_: switch language mode */
1612 if (!p_ari)
1613 break;
1614#ifdef FEAT_FKMAP
1615 if (p_altkeymap)
1616 {
1617 cmd_fkmap = !cmd_fkmap;
1618 if (cmd_fkmap) /* in Farsi always in Insert mode */
1619 ccline.overstrike = FALSE;
1620 }
1621 else /* Hebrew is default */
1622#endif
1623 cmd_hkmap = !cmd_hkmap;
1624 goto cmdline_not_changed;
1625#endif
1626
1627 default:
1628#ifdef UNIX
1629 if (c == intr_char)
1630 {
1631 gotesc = TRUE; /* will free ccline.cmdbuff after
1632 putting it in history */
1633 goto returncmd; /* back to Normal mode */
1634 }
1635#endif
1636 /*
1637 * Normal character with no special meaning. Just set mod_mask
1638 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1639 * the string <S-Space>. This should only happen after ^V.
1640 */
1641 if (!IS_SPECIAL(c))
1642 mod_mask = 0x0;
1643 break;
1644 }
1645 /*
1646 * End of switch on command line character.
1647 * We come here if we have a normal character.
1648 */
1649
1650 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1651#ifdef FEAT_MBYTE
1652 /* Add ABBR_OFF for characters above 0x100, this is
1653 * what check_abbr() expects. */
1654 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1655#endif
1656 c))
1657 goto cmdline_changed;
1658
1659 /*
1660 * put the character in the command line
1661 */
1662 if (IS_SPECIAL(c) || mod_mask != 0)
1663 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1664 else
1665 {
1666#ifdef FEAT_MBYTE
1667 if (has_mbyte)
1668 {
1669 j = (*mb_char2bytes)(c, IObuff);
1670 IObuff[j] = NUL; /* exclude composing chars */
1671 put_on_cmdline(IObuff, j, TRUE);
1672 }
1673 else
1674#endif
1675 {
1676 IObuff[0] = c;
1677 put_on_cmdline(IObuff, 1, TRUE);
1678 }
1679 }
1680 goto cmdline_changed;
1681
1682/*
1683 * This part implements incremental searches for "/" and "?"
1684 * Jump to cmdline_not_changed when a character has been read but the command
1685 * line did not change. Then we only search and redraw if something changed in
1686 * the past.
1687 * Jump to cmdline_changed when the command line did change.
1688 * (Sorry for the goto's, I know it is ugly).
1689 */
1690cmdline_not_changed:
1691#ifdef FEAT_SEARCH_EXTRA
1692 if (!incsearch_postponed)
1693 continue;
1694#endif
1695
1696cmdline_changed:
1697#ifdef FEAT_SEARCH_EXTRA
1698 /*
1699 * 'incsearch' highlighting.
1700 */
1701 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1702 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001703 pos_T end_pos;
1704
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 /* if there is a character waiting, search and redraw later */
1706 if (char_avail())
1707 {
1708 incsearch_postponed = TRUE;
1709 continue;
1710 }
1711 incsearch_postponed = FALSE;
1712 curwin->w_cursor = old_cursor; /* start at old position */
1713
1714 /* If there is no command line, don't do anything */
1715 if (ccline.cmdlen == 0)
1716 i = 0;
1717 else
1718 {
1719 cursor_off(); /* so the user knows we're busy */
1720 out_flush();
1721 ++emsg_off; /* So it doesn't beep if bad expr */
1722 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1723 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
1724 --emsg_off;
1725 /* if interrupted while searching, behave like it failed */
1726 if (got_int)
1727 {
1728 (void)vpeekc(); /* remove <C-C> from input stream */
1729 got_int = FALSE; /* don't abandon the command line */
1730 i = 0;
1731 }
1732 else if (char_avail())
1733 /* cancelled searching because a char was typed */
1734 incsearch_postponed = TRUE;
1735 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001736 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 highlight_match = TRUE; /* highlight position */
1738 else
1739 highlight_match = FALSE; /* remove highlight */
1740
1741 /* first restore the old curwin values, so the screen is
1742 * positioned in the same way as the actual search command */
1743 curwin->w_leftcol = old_leftcol;
1744 curwin->w_topline = old_topline;
1745# ifdef FEAT_DIFF
1746 curwin->w_topfill = old_topfill;
1747# endif
1748 curwin->w_botline = old_botline;
1749 changed_cline_bef_curs();
1750 update_topline();
1751
1752 if (i != 0)
1753 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001754 pos_T save_pos = curwin->w_cursor;
1755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001757 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 * moves the whole match onto the screen when 'nowrap' is set.
1759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 curwin->w_cursor.lnum += search_match_lines;
1761 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001762 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1763 {
1764 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1765 coladvance((colnr_T)MAXCOL);
1766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001768 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001769 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001771 else
1772 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001773
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001775# ifdef FEAT_WINDOWS
1776 /* May redraw the status line to show the cursor position. */
1777 if (p_ru && curwin->w_status_height > 0)
1778 curwin->w_redr_status = TRUE;
1779# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001781 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001782 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001783 restore_cmdline(&save_ccline);
1784
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001785 /* Leave it at the end to make CTRL-R CTRL-W work. */
1786 if (i != 0)
1787 curwin->w_cursor = end_pos;
1788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 msg_starthere();
1790 redrawcmdline();
1791 did_incsearch = TRUE;
1792 }
1793#else /* FEAT_SEARCH_EXTRA */
1794 ;
1795#endif
1796
1797#ifdef FEAT_RIGHTLEFT
1798 if (cmdmsg_rl
1799# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001800 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801# endif
1802 )
1803 /* Always redraw the whole command line to fix shaping and
1804 * right-left typing. Not efficient, but it works. */
1805 redrawcmd();
1806#endif
1807 }
1808
1809returncmd:
1810
1811#ifdef FEAT_RIGHTLEFT
1812 cmdmsg_rl = FALSE;
1813#endif
1814
1815#ifdef FEAT_FKMAP
1816 cmd_fkmap = 0;
1817#endif
1818
1819 ExpandCleanup(&xpc);
1820
1821#ifdef FEAT_SEARCH_EXTRA
1822 if (did_incsearch)
1823 {
1824 curwin->w_cursor = old_cursor;
1825 curwin->w_curswant = old_curswant;
1826 curwin->w_leftcol = old_leftcol;
1827 curwin->w_topline = old_topline;
1828# ifdef FEAT_DIFF
1829 curwin->w_topfill = old_topfill;
1830# endif
1831 curwin->w_botline = old_botline;
1832 highlight_match = FALSE;
1833 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001834 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 }
1836#endif
1837
1838 if (ccline.cmdbuff != NULL)
1839 {
1840 /*
1841 * Put line in history buffer (":" and "=" only when it was typed).
1842 */
1843#ifdef FEAT_CMDHIST
1844 if (ccline.cmdlen && firstc != NUL
1845 && (some_key_typed || histype == HIST_SEARCH))
1846 {
1847 add_to_history(histype, ccline.cmdbuff, TRUE,
1848 histype == HIST_SEARCH ? firstc : NUL);
1849 if (firstc == ':')
1850 {
1851 vim_free(new_last_cmdline);
1852 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1853 }
1854 }
1855#endif
1856
1857 if (gotesc) /* abandon command line */
1858 {
1859 vim_free(ccline.cmdbuff);
1860 ccline.cmdbuff = NULL;
1861 if (msg_scrolled == 0)
1862 compute_cmdrow();
1863 MSG("");
1864 redraw_cmdline = TRUE;
1865 }
1866 }
1867
1868 /*
1869 * If the screen was shifted up, redraw the whole screen (later).
1870 * If the line is too long, clear it, so ruler and shown command do
1871 * not get printed in the middle of it.
1872 */
1873 msg_check();
1874 msg_scroll = save_msg_scroll;
1875 redir_off = FALSE;
1876
1877 /* When the command line was typed, no need for a wait-return prompt. */
1878 if (some_key_typed)
1879 need_wait_return = FALSE;
1880
1881 State = save_State;
1882#ifdef USE_IM_CONTROL
1883 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1884 im_save_status(b_im_ptr);
1885 im_set_active(FALSE);
1886#endif
1887#ifdef FEAT_MOUSE
1888 setmouse();
1889#endif
1890#ifdef CURSOR_SHAPE
1891 ui_cursor_shape(); /* may show different cursor shape */
1892#endif
1893
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001894 {
1895 char_u *p = ccline.cmdbuff;
1896
1897 /* Make ccline empty, getcmdline() may try to use it. */
1898 ccline.cmdbuff = NULL;
1899 return p;
1900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901}
1902
1903#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1904/*
1905 * Get a command line with a prompt.
1906 * This is prepared to be called recursively from getcmdline() (e.g. by
1907 * f_input() when evaluating an expression from CTRL-R =).
1908 * Returns the command line in allocated memory, or NULL.
1909 */
1910 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001911getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 int firstc;
1913 char_u *prompt; /* command line prompt */
1914 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001915 int xp_context; /* type of expansion */
1916 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917{
1918 char_u *s;
1919 struct cmdline_info save_ccline;
1920 int msg_col_save = msg_col;
1921
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001922 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 ccline.cmdprompt = prompt;
1924 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001925# ifdef FEAT_EVAL
1926 ccline.xp_context = xp_context;
1927 ccline.xp_arg = xp_arg;
1928 ccline.input_fn = (firstc == '@');
1929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001931 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 /* Restore msg_col, the prompt from input() may have changed it. */
1933 msg_col = msg_col_save;
1934
1935 return s;
1936}
1937#endif
1938
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001939/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001940 * Return TRUE when the text must not be changed and we can't switch to
1941 * another window or buffer. Used when editing the command line, evaluating
1942 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001943 */
1944 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001945text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001946{
1947#ifdef FEAT_CMDWIN
1948 if (cmdwin_type != 0)
1949 return TRUE;
1950#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001951 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001952}
1953
1954/*
1955 * Give an error message for a command that isn't allowed while the cmdline
1956 * window is open or editing the cmdline in another way.
1957 */
1958 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001959text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001960{
1961#ifdef FEAT_CMDWIN
1962 if (cmdwin_type != 0)
1963 EMSG(_(e_cmdwin));
1964 else
1965#endif
1966 EMSG(_(e_secure));
1967}
1968
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001969#if defined(FEAT_AUTOCMD) || defined(PROTO)
1970/*
1971 * Check if "curbuf_lock" is set and return TRUE when it is and give an error
1972 * message.
1973 */
1974 int
1975curbuf_locked()
1976{
1977 if (curbuf_lock > 0)
1978 {
1979 EMSG(_("E788: Not allowed to edit another buffer now"));
1980 return TRUE;
1981 }
1982 return FALSE;
1983}
1984#endif
1985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 static int
1987cmdline_charsize(idx)
1988 int idx;
1989{
1990#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1991 if (cmdline_star > 0) /* showing '*', always 1 position */
1992 return 1;
1993#endif
1994 return ptr2cells(ccline.cmdbuff + idx);
1995}
1996
1997/*
1998 * Compute the offset of the cursor on the command line for the prompt and
1999 * indent.
2000 */
2001 static void
2002set_cmdspos()
2003{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002004 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 ccline.cmdspos = 1 + ccline.cmdindent;
2006 else
2007 ccline.cmdspos = 0 + ccline.cmdindent;
2008}
2009
2010/*
2011 * Compute the screen position for the cursor on the command line.
2012 */
2013 static void
2014set_cmdspos_cursor()
2015{
2016 int i, m, c;
2017
2018 set_cmdspos();
2019 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002020 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002022 if (m < 0) /* overflow, Columns or Rows at weird value */
2023 m = MAXCOL;
2024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 else
2026 m = MAXCOL;
2027 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2028 {
2029 c = cmdline_charsize(i);
2030#ifdef FEAT_MBYTE
2031 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2032 if (has_mbyte)
2033 correct_cmdspos(i, c);
2034#endif
2035 /* If the cmdline doesn't fit, put cursor on last visible char. */
2036 if ((ccline.cmdspos += c) >= m)
2037 {
2038 ccline.cmdpos = i - 1;
2039 ccline.cmdspos -= c;
2040 break;
2041 }
2042#ifdef FEAT_MBYTE
2043 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002044 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045#endif
2046 }
2047}
2048
2049#ifdef FEAT_MBYTE
2050/*
2051 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2052 * character that doesn't fit, so that a ">" must be displayed.
2053 */
2054 static void
2055correct_cmdspos(idx, cells)
2056 int idx;
2057 int cells;
2058{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002059 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2061 && ccline.cmdspos % Columns + cells > Columns)
2062 ccline.cmdspos++;
2063}
2064#endif
2065
2066/*
2067 * Get an Ex command line for the ":" command.
2068 */
2069/* ARGSUSED */
2070 char_u *
2071getexline(c, dummy, indent)
2072 int c; /* normally ':', NUL for ":append" */
2073 void *dummy; /* cookie not used */
2074 int indent; /* indent for inside conditionals */
2075{
2076 /* When executing a register, remove ':' that's in front of each line. */
2077 if (exec_from_reg && vpeekc() == ':')
2078 (void)vgetc();
2079 return getcmdline(c, 1L, indent);
2080}
2081
2082/*
2083 * Get an Ex command line for Ex mode.
2084 * In Ex mode we only use the OS supplied line editing features and no
2085 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002086 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 */
2088/* ARGSUSED */
2089 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002090getexmodeline(promptc, dummy, indent)
2091 int promptc; /* normally ':', NUL for ":append" and '?' for
2092 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 void *dummy; /* cookie not used */
2094 int indent; /* indent for inside conditionals */
2095{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002096 garray_T line_ga;
2097 char_u *pend;
2098 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002099 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002100 int escaped = FALSE; /* CTRL-V typed */
2101 int vcol = 0;
2102 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002103 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105 /* Switch cursor on now. This avoids that it happens after the "\n", which
2106 * confuses the system function that computes tabstops. */
2107 cursor_on();
2108
2109 /* always start in column 0; write a newline if necessary */
2110 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002111 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002113 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002115 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002116 if (p_prompt)
2117 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 while (indent-- > 0)
2119 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
2122
2123 ga_init2(&line_ga, 1, 30);
2124
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002125 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002126 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002127 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002128 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002129 while (indent >= 8)
2130 {
2131 ga_append(&line_ga, TAB);
2132 msg_puts((char_u *)" ");
2133 indent -= 8;
2134 }
2135 while (indent-- > 0)
2136 {
2137 ga_append(&line_ga, ' ');
2138 msg_putchar(' ');
2139 }
2140 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002141 ++no_mapping;
2142 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002143
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 /*
2145 * Get the line, one character at a time.
2146 */
2147 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002148 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
2150 if (ga_grow(&line_ga, 40) == FAIL)
2151 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002152 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002154 /* Get one character at a time. Don't use inchar(), it can't handle
2155 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002156 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002157 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158
2159 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002160 * Handle line editing.
2161 * Previously this was left to the system, putting the terminal in
2162 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002164 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002166 msg_putchar('\n');
2167 break;
2168 }
2169
2170 if (!escaped)
2171 {
2172 /* CR typed means "enter", which is NL */
2173 if (c1 == '\r')
2174 c1 = '\n';
2175
2176 if (c1 == BS || c1 == K_BS
2177 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002179 if (line_ga.ga_len > 0)
2180 {
2181 --line_ga.ga_len;
2182 goto redraw;
2183 }
2184 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
2186
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002187 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002189 msg_col = startcol;
2190 msg_clr_eos();
2191 line_ga.ga_len = 0;
2192 continue;
2193 }
2194
2195 if (c1 == Ctrl_T)
2196 {
2197 p = (char_u *)line_ga.ga_data;
2198 p[line_ga.ga_len] = NUL;
2199 indent = get_indent_str(p, 8);
2200 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2201add_indent:
2202 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002204 char_u *s = skipwhite(p);
2205
2206 ga_grow(&line_ga, 1);
2207 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2208 *s = ' ';
2209 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002211redraw:
2212 /* redraw the line */
2213 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002214 vcol = 0;
2215 for (p = (char_u *)line_ga.ga_data;
2216 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002218 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002220 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002222 msg_putchar(' ');
2223 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002225 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002227 msg_outtrans_len(p, 1);
2228 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
2230 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002231 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002232 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002233 continue;
2234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002236 if (c1 == Ctrl_D)
2237 {
2238 /* Delete one shiftwidth. */
2239 p = (char_u *)line_ga.ga_data;
2240 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002242 if (prev_char == '^')
2243 ex_keep_indent = TRUE;
2244 indent = 0;
2245 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247 else
2248 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002249 p[line_ga.ga_len] = NUL;
2250 indent = get_indent_str(p, 8);
2251 --indent;
2252 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002254 while (get_indent_str(p, 8) > indent)
2255 {
2256 char_u *s = skipwhite(p);
2257
2258 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2259 --line_ga.ga_len;
2260 }
2261 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002263
2264 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2265 {
2266 escaped = TRUE;
2267 continue;
2268 }
2269
2270 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2271 if (IS_SPECIAL(c1))
2272 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002274
2275 if (IS_SPECIAL(c1))
2276 c1 = '?';
2277 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002278 if (c1 == '\n')
2279 msg_putchar('\n');
2280 else if (c1 == TAB)
2281 {
2282 /* Don't use chartabsize(), 'ts' can be different */
2283 do
2284 {
2285 msg_putchar(' ');
2286 } while (++vcol % 8);
2287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002290 msg_outtrans_len(
2291 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2292 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002294 ++line_ga.ga_len;
2295 escaped = FALSE;
2296
2297 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002298 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002299
2300 /* we are done when a NL is entered, but not when it comes after a
2301 * backslash */
2302 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2303 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002306 --pend;
2307 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002308 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
2310 }
2311
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002312 --no_mapping;
2313 --allow_keys;
2314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 /* make following messages go to the next line */
2316 msg_didout = FALSE;
2317 msg_col = 0;
2318 if (msg_row < Rows - 1)
2319 ++msg_row;
2320 emsg_on_display = FALSE; /* don't want ui_delay() */
2321
2322 if (got_int)
2323 ga_clear(&line_ga);
2324
2325 return (char_u *)line_ga.ga_data;
2326}
2327
Bram Moolenaare344bea2005-09-01 20:46:49 +00002328# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2329 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330/*
2331 * Return TRUE if ccline.overstrike is on.
2332 */
2333 int
2334cmdline_overstrike()
2335{
2336 return ccline.overstrike;
2337}
2338
2339/*
2340 * Return TRUE if the cursor is at the end of the cmdline.
2341 */
2342 int
2343cmdline_at_end()
2344{
2345 return (ccline.cmdpos >= ccline.cmdlen);
2346}
2347#endif
2348
Bram Moolenaar9372a112005-12-06 19:59:18 +00002349#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350/*
2351 * Return the virtual column number at the current cursor position.
2352 * This is used by the IM code to obtain the start of the preedit string.
2353 */
2354 colnr_T
2355cmdline_getvcol_cursor()
2356{
2357 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2358 return MAXCOL;
2359
2360# ifdef FEAT_MBYTE
2361 if (has_mbyte)
2362 {
2363 colnr_T col;
2364 int i = 0;
2365
2366 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002367 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
2369 return col;
2370 }
2371 else
2372# endif
2373 return ccline.cmdpos;
2374}
2375#endif
2376
2377#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2378/*
2379 * If part of the command line is an IM preedit string, redraw it with
2380 * IM feedback attributes. The cursor position is restored after drawing.
2381 */
2382 static void
2383redrawcmd_preedit()
2384{
2385 if ((State & CMDLINE)
2386 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002387 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 && !p_imdisable
2389 && im_is_preediting())
2390 {
2391 int cmdpos = 0;
2392 int cmdspos;
2393 int old_row;
2394 int old_col;
2395 colnr_T col;
2396
2397 old_row = msg_row;
2398 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002399 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
2401# ifdef FEAT_MBYTE
2402 if (has_mbyte)
2403 {
2404 for (col = 0; col < preedit_start_col
2405 && cmdpos < ccline.cmdlen; ++col)
2406 {
2407 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002408 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410 }
2411 else
2412# endif
2413 {
2414 cmdspos += preedit_start_col;
2415 cmdpos += preedit_start_col;
2416 }
2417
2418 msg_row = cmdline_row + (cmdspos / (int)Columns);
2419 msg_col = cmdspos % (int)Columns;
2420 if (msg_row >= Rows)
2421 msg_row = Rows - 1;
2422
2423 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2424 {
2425 int char_len;
2426 int char_attr;
2427
2428 char_attr = im_get_feedback_attr(col);
2429 if (char_attr < 0)
2430 break; /* end of preedit string */
2431
2432# ifdef FEAT_MBYTE
2433 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002434 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 else
2436# endif
2437 char_len = 1;
2438
2439 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2440 cmdpos += char_len;
2441 }
2442
2443 msg_row = old_row;
2444 msg_col = old_col;
2445 }
2446}
2447#endif /* FEAT_XIM && FEAT_GUI_GTK */
2448
2449/*
2450 * Allocate a new command line buffer.
2451 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2452 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2453 */
2454 static void
2455alloc_cmdbuff(len)
2456 int len;
2457{
2458 /*
2459 * give some extra space to avoid having to allocate all the time
2460 */
2461 if (len < 80)
2462 len = 100;
2463 else
2464 len += 20;
2465
2466 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2467 ccline.cmdbufflen = len;
2468}
2469
2470/*
2471 * Re-allocate the command line to length len + something extra.
2472 * return FAIL for failure, OK otherwise
2473 */
2474 static int
2475realloc_cmdbuff(len)
2476 int len;
2477{
2478 char_u *p;
2479
2480 p = ccline.cmdbuff;
2481 alloc_cmdbuff(len); /* will get some more */
2482 if (ccline.cmdbuff == NULL) /* out of memory */
2483 {
2484 ccline.cmdbuff = p; /* keep the old one */
2485 return FAIL;
2486 }
2487 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2488 vim_free(p);
2489 return OK;
2490}
2491
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002492#if defined(FEAT_ARABIC) || defined(PROTO)
2493static char_u *arshape_buf = NULL;
2494
2495# if defined(EXITFREE) || defined(PROTO)
2496 void
2497free_cmdline_buf()
2498{
2499 vim_free(arshape_buf);
2500}
2501# endif
2502#endif
2503
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504/*
2505 * Draw part of the cmdline at the current cursor position. But draw stars
2506 * when cmdline_star is TRUE.
2507 */
2508 static void
2509draw_cmdline(start, len)
2510 int start;
2511 int len;
2512{
2513#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2514 int i;
2515
2516 if (cmdline_star > 0)
2517 for (i = 0; i < len; ++i)
2518 {
2519 msg_putchar('*');
2520# ifdef FEAT_MBYTE
2521 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002522 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523# endif
2524 }
2525 else
2526#endif
2527#ifdef FEAT_ARABIC
2528 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2529 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 static int buflen = 0;
2531 char_u *p;
2532 int j;
2533 int newlen = 0;
2534 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002535 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 int prev_c = 0;
2537 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002538 int u8c;
2539 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
2542 /*
2543 * Do arabic shaping into a temporary buffer. This is very
2544 * inefficient!
2545 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002546 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
2548 /* Re-allocate the buffer. We keep it around to avoid a lot of
2549 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002550 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002551 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002552 arshape_buf = alloc(buflen);
2553 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 return; /* out of memory */
2555 }
2556
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002557 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2558 {
2559 /* Prepend a space to draw the leading composing char on. */
2560 arshape_buf[0] = ' ';
2561 newlen = 1;
2562 }
2563
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 for (j = start; j < start + len; j += mb_l)
2565 {
2566 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002567 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002568 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 if (ARABIC_CHAR(u8c))
2570 {
2571 /* Do Arabic shaping. */
2572 if (cmdmsg_rl)
2573 {
2574 /* displaying from right to left */
2575 pc = prev_c;
2576 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002577 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (j + mb_l >= start + len)
2579 nc = NUL;
2580 else
2581 nc = utf_ptr2char(p + mb_l);
2582 }
2583 else
2584 {
2585 /* displaying from left to right */
2586 if (j + mb_l >= start + len)
2587 pc = NUL;
2588 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002589 {
2590 int pcc[MAX_MCO];
2591
2592 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002594 pc1 = pcc[0];
2595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 nc = prev_c;
2597 }
2598 prev_c = u8c;
2599
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002600 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002602 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002603 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002605 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2606 if (u8cc[1] != 0)
2607 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002608 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 }
2610 }
2611 else
2612 {
2613 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002614 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 newlen += mb_l;
2616 }
2617 }
2618
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002619 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 }
2621 else
2622#endif
2623 msg_outtrans_len(ccline.cmdbuff + start, len);
2624}
2625
2626/*
2627 * Put a character on the command line. Shifts the following text to the
2628 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2629 * "c" must be printable (fit in one display cell)!
2630 */
2631 void
2632putcmdline(c, shift)
2633 int c;
2634 int shift;
2635{
2636 if (cmd_silent)
2637 return;
2638 msg_no_more = TRUE;
2639 msg_putchar(c);
2640 if (shift)
2641 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2642 msg_no_more = FALSE;
2643 cursorcmd();
2644}
2645
2646/*
2647 * Undo a putcmdline(c, FALSE).
2648 */
2649 void
2650unputcmdline()
2651{
2652 if (cmd_silent)
2653 return;
2654 msg_no_more = TRUE;
2655 if (ccline.cmdlen == ccline.cmdpos)
2656 msg_putchar(' ');
2657 else
2658 draw_cmdline(ccline.cmdpos, 1);
2659 msg_no_more = FALSE;
2660 cursorcmd();
2661}
2662
2663/*
2664 * Put the given string, of the given length, onto the command line.
2665 * If len is -1, then STRLEN() is used to calculate the length.
2666 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2667 * part will be redrawn, otherwise it will not. If this function is called
2668 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2669 * called afterwards.
2670 */
2671 int
2672put_on_cmdline(str, len, redraw)
2673 char_u *str;
2674 int len;
2675 int redraw;
2676{
2677 int retval;
2678 int i;
2679 int m;
2680 int c;
2681
2682 if (len < 0)
2683 len = (int)STRLEN(str);
2684
2685 /* Check if ccline.cmdbuff needs to be longer */
2686 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2687 retval = realloc_cmdbuff(ccline.cmdlen + len);
2688 else
2689 retval = OK;
2690 if (retval == OK)
2691 {
2692 if (!ccline.overstrike)
2693 {
2694 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2695 ccline.cmdbuff + ccline.cmdpos,
2696 (size_t)(ccline.cmdlen - ccline.cmdpos));
2697 ccline.cmdlen += len;
2698 }
2699 else
2700 {
2701#ifdef FEAT_MBYTE
2702 if (has_mbyte)
2703 {
2704 /* Count nr of characters in the new string. */
2705 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002706 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 ++m;
2708 /* Count nr of bytes in cmdline that are overwritten by these
2709 * characters. */
2710 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002711 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 --m;
2713 if (i < ccline.cmdlen)
2714 {
2715 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2716 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2717 ccline.cmdlen += ccline.cmdpos + len - i;
2718 }
2719 else
2720 ccline.cmdlen = ccline.cmdpos + len;
2721 }
2722 else
2723#endif
2724 if (ccline.cmdpos + len > ccline.cmdlen)
2725 ccline.cmdlen = ccline.cmdpos + len;
2726 }
2727 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2728 ccline.cmdbuff[ccline.cmdlen] = NUL;
2729
2730#ifdef FEAT_MBYTE
2731 if (enc_utf8)
2732 {
2733 /* When the inserted text starts with a composing character,
2734 * backup to the character before it. There could be two of them.
2735 */
2736 i = 0;
2737 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2738 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2739 {
2740 i = (*mb_head_off)(ccline.cmdbuff,
2741 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2742 ccline.cmdpos -= i;
2743 len += i;
2744 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2745 }
2746# ifdef FEAT_ARABIC
2747 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2748 {
2749 /* Check the previous character for Arabic combining pair. */
2750 i = (*mb_head_off)(ccline.cmdbuff,
2751 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2752 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2753 + ccline.cmdpos - i), c))
2754 {
2755 ccline.cmdpos -= i;
2756 len += i;
2757 }
2758 else
2759 i = 0;
2760 }
2761# endif
2762 if (i != 0)
2763 {
2764 /* Also backup the cursor position. */
2765 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2766 ccline.cmdspos -= i;
2767 msg_col -= i;
2768 if (msg_col < 0)
2769 {
2770 msg_col += Columns;
2771 --msg_row;
2772 }
2773 }
2774 }
2775#endif
2776
2777 if (redraw && !cmd_silent)
2778 {
2779 msg_no_more = TRUE;
2780 i = cmdline_row;
2781 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2782 /* Avoid clearing the rest of the line too often. */
2783 if (cmdline_row != i || ccline.overstrike)
2784 msg_clr_eos();
2785 msg_no_more = FALSE;
2786 }
2787#ifdef FEAT_FKMAP
2788 /*
2789 * If we are in Farsi command mode, the character input must be in
2790 * Insert mode. So do not advance the cmdpos.
2791 */
2792 if (!cmd_fkmap)
2793#endif
2794 {
2795 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002798 if (m < 0) /* overflow, Columns or Rows at weird value */
2799 m = MAXCOL;
2800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 else
2802 m = MAXCOL;
2803 for (i = 0; i < len; ++i)
2804 {
2805 c = cmdline_charsize(ccline.cmdpos);
2806#ifdef FEAT_MBYTE
2807 /* count ">" for a double-wide char that doesn't fit. */
2808 if (has_mbyte)
2809 correct_cmdspos(ccline.cmdpos, c);
2810#endif
2811 /* Stop cursor at the end of the screen */
2812 if (ccline.cmdspos + c >= m)
2813 break;
2814 ccline.cmdspos += c;
2815#ifdef FEAT_MBYTE
2816 if (has_mbyte)
2817 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002818 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 if (c > len - i - 1)
2820 c = len - i - 1;
2821 ccline.cmdpos += c;
2822 i += c;
2823 }
2824#endif
2825 ++ccline.cmdpos;
2826 }
2827 }
2828 }
2829 if (redraw)
2830 msg_check();
2831 return retval;
2832}
2833
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002834static struct cmdline_info prev_ccline;
2835static int prev_ccline_used = FALSE;
2836
2837/*
2838 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2839 * and overwrite it. But get_cmdline_str() may need it, thus make it
2840 * available globally in prev_ccline.
2841 */
2842 static void
2843save_cmdline(ccp)
2844 struct cmdline_info *ccp;
2845{
2846 if (!prev_ccline_used)
2847 {
2848 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2849 prev_ccline_used = TRUE;
2850 }
2851 *ccp = prev_ccline;
2852 prev_ccline = ccline;
2853 ccline.cmdbuff = NULL;
2854 ccline.cmdprompt = NULL;
2855}
2856
2857/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002858 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002859 */
2860 static void
2861restore_cmdline(ccp)
2862 struct cmdline_info *ccp;
2863{
2864 ccline = prev_ccline;
2865 prev_ccline = *ccp;
2866}
2867
Bram Moolenaar5a305422006-04-28 22:38:25 +00002868#if defined(FEAT_EVAL) || defined(PROTO)
2869/*
2870 * Save the command line into allocated memory. Returns a pointer to be
2871 * passed to restore_cmdline_alloc() later.
2872 * Returns NULL when failed.
2873 */
2874 char_u *
2875save_cmdline_alloc()
2876{
2877 struct cmdline_info *p;
2878
2879 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2880 if (p != NULL)
2881 save_cmdline(p);
2882 return (char_u *)p;
2883}
2884
2885/*
2886 * Restore the command line from the return value of save_cmdline_alloc().
2887 */
2888 void
2889restore_cmdline_alloc(p)
2890 char_u *p;
2891{
2892 if (p != NULL)
2893 {
2894 restore_cmdline((struct cmdline_info *)p);
2895 vim_free(p);
2896 }
2897}
2898#endif
2899
Bram Moolenaar8299df92004-07-10 09:47:34 +00002900/*
2901 * paste a yank register into the command line.
2902 * used by CTRL-R command in command-line mode
2903 * insert_reg() can't be used here, because special characters from the
2904 * register contents will be interpreted as commands.
2905 *
2906 * return FAIL for failure, OK otherwise
2907 */
2908 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002909cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00002910 int regname;
2911 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002912 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00002913{
2914 long i;
2915 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002916 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002917 int allocated;
2918 struct cmdline_info save_ccline;
2919
2920 /* check for valid regname; also accept special characters for CTRL-R in
2921 * the command line */
2922 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2923 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2924 return FAIL;
2925
2926 /* A register containing CTRL-R can cause an endless loop. Allow using
2927 * CTRL-C to break the loop. */
2928 line_breakcheck();
2929 if (got_int)
2930 return FAIL;
2931
2932#ifdef FEAT_CLIPBOARD
2933 regname = may_get_selection(regname);
2934#endif
2935
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002936 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002937 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002938 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002939 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002940 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002941 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002942 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002943
2944 if (i)
2945 {
2946 /* Got the value of a special register in "arg". */
2947 if (arg == NULL)
2948 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002949
2950 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
2951 * part of the word. */
2952 p = arg;
2953 if (p_is && regname == Ctrl_W)
2954 {
2955 char_u *w;
2956 int len;
2957
2958 /* Locate start of last word in the cmd buffer. */
2959 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
2960 {
2961#ifdef FEAT_MBYTE
2962 if (has_mbyte)
2963 {
2964 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
2965 if (!vim_iswordc(mb_ptr2char(w - len)))
2966 break;
2967 w -= len;
2968 }
2969 else
2970#endif
2971 {
2972 if (!vim_iswordc(w[-1]))
2973 break;
2974 --w;
2975 }
2976 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002977 len = (int)((ccline.cmdbuff + ccline.cmdlen) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002978 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
2979 p += len;
2980 }
2981
2982 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002983 if (allocated)
2984 vim_free(arg);
2985 return OK;
2986 }
2987
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002988 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002989}
2990
2991/*
2992 * Put a string on the command line.
2993 * When "literally" is TRUE, insert literally.
2994 * When "literally" is FALSE, insert as typed, but don't leave the command
2995 * line.
2996 */
2997 void
2998cmdline_paste_str(s, literally)
2999 char_u *s;
3000 int literally;
3001{
3002 int c, cv;
3003
3004 if (literally)
3005 put_on_cmdline(s, -1, TRUE);
3006 else
3007 while (*s != NUL)
3008 {
3009 cv = *s;
3010 if (cv == Ctrl_V && s[1])
3011 ++s;
3012#ifdef FEAT_MBYTE
3013 if (has_mbyte)
3014 {
3015 c = mb_ptr2char(s);
3016 s += mb_char2len(c);
3017 }
3018 else
3019#endif
3020 c = *s++;
3021 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3022#ifdef UNIX
3023 || c == intr_char
3024#endif
3025 || (c == Ctrl_BSL && *s == Ctrl_N))
3026 stuffcharReadbuff(Ctrl_V);
3027 stuffcharReadbuff(c);
3028 }
3029}
3030
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031#ifdef FEAT_WILDMENU
3032/*
3033 * Delete characters on the command line, from "from" to the current
3034 * position.
3035 */
3036 static void
3037cmdline_del(from)
3038 int from;
3039{
3040 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3041 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3042 ccline.cmdlen -= ccline.cmdpos - from;
3043 ccline.cmdpos = from;
3044}
3045#endif
3046
3047/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003048 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 * search
3050 */
3051 void
3052redrawcmdline()
3053{
3054 if (cmd_silent)
3055 return;
3056 need_wait_return = FALSE;
3057 compute_cmdrow();
3058 redrawcmd();
3059 cursorcmd();
3060}
3061
3062 static void
3063redrawcmdprompt()
3064{
3065 int i;
3066
3067 if (cmd_silent)
3068 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003069 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 msg_putchar(ccline.cmdfirstc);
3071 if (ccline.cmdprompt != NULL)
3072 {
3073 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3074 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3075 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003076 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 --ccline.cmdindent;
3078 }
3079 else
3080 for (i = ccline.cmdindent; i > 0; --i)
3081 msg_putchar(' ');
3082}
3083
3084/*
3085 * Redraw what is currently on the command line.
3086 */
3087 void
3088redrawcmd()
3089{
3090 if (cmd_silent)
3091 return;
3092
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003093 /* when 'incsearch' is set there may be no command line while redrawing */
3094 if (ccline.cmdbuff == NULL)
3095 {
3096 windgoto(cmdline_row, 0);
3097 msg_clr_eos();
3098 return;
3099 }
3100
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 msg_start();
3102 redrawcmdprompt();
3103
3104 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3105 msg_no_more = TRUE;
3106 draw_cmdline(0, ccline.cmdlen);
3107 msg_clr_eos();
3108 msg_no_more = FALSE;
3109
3110 set_cmdspos_cursor();
3111
3112 /*
3113 * An emsg() before may have set msg_scroll. This is used in normal mode,
3114 * in cmdline mode we can reset them now.
3115 */
3116 msg_scroll = FALSE; /* next message overwrites cmdline */
3117
3118 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3119 * in cmdline mode */
3120 skip_redraw = FALSE;
3121}
3122
3123 void
3124compute_cmdrow()
3125{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003126 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 cmdline_row = Rows - 1;
3128 else
3129 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3130 + W_STATUS_HEIGHT(lastwin);
3131}
3132
3133 static void
3134cursorcmd()
3135{
3136 if (cmd_silent)
3137 return;
3138
3139#ifdef FEAT_RIGHTLEFT
3140 if (cmdmsg_rl)
3141 {
3142 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3143 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3144 if (msg_row <= 0)
3145 msg_row = Rows - 1;
3146 }
3147 else
3148#endif
3149 {
3150 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3151 msg_col = ccline.cmdspos % (int)Columns;
3152 if (msg_row >= Rows)
3153 msg_row = Rows - 1;
3154 }
3155
3156 windgoto(msg_row, msg_col);
3157#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3158 redrawcmd_preedit();
3159#endif
3160#ifdef MCH_CURSOR_SHAPE
3161 mch_update_cursor();
3162#endif
3163}
3164
3165 void
3166gotocmdline(clr)
3167 int clr;
3168{
3169 msg_start();
3170#ifdef FEAT_RIGHTLEFT
3171 if (cmdmsg_rl)
3172 msg_col = Columns - 1;
3173 else
3174#endif
3175 msg_col = 0; /* always start in column 0 */
3176 if (clr) /* clear the bottom line(s) */
3177 msg_clr_eos(); /* will reset clear_cmdline */
3178 windgoto(cmdline_row, 0);
3179}
3180
3181/*
3182 * Check the word in front of the cursor for an abbreviation.
3183 * Called when the non-id character "c" has been entered.
3184 * When an abbreviation is recognized it is removed from the text with
3185 * backspaces and the replacement string is inserted, followed by "c".
3186 */
3187 static int
3188ccheck_abbr(c)
3189 int c;
3190{
3191 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3192 return FALSE;
3193
3194 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3195}
3196
3197/*
3198 * Return FAIL if this is not an appropriate context in which to do
3199 * completion of anything, return OK if it is (even if there are no matches).
3200 * For the caller, this means that the character is just passed through like a
3201 * normal character (instead of being expanded). This allows :s/^I^D etc.
3202 */
3203 static int
3204nextwild(xp, type, options)
3205 expand_T *xp;
3206 int type;
3207 int options; /* extra options for ExpandOne() */
3208{
3209 int i, j;
3210 char_u *p1;
3211 char_u *p2;
3212 int oldlen;
3213 int difflen;
3214 int v;
3215
3216 if (xp->xp_numfiles == -1)
3217 {
3218 set_expand_context(xp);
3219 cmd_showtail = expand_showtail(xp);
3220 }
3221
3222 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3223 {
3224 beep_flush();
3225 return OK; /* Something illegal on command line */
3226 }
3227 if (xp->xp_context == EXPAND_NOTHING)
3228 {
3229 /* Caller can use the character as a normal char instead */
3230 return FAIL;
3231 }
3232
3233 MSG_PUTS("..."); /* show that we are busy */
3234 out_flush();
3235
3236 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3237 oldlen = ccline.cmdpos - i;
3238
3239 if (type == WILD_NEXT || type == WILD_PREV)
3240 {
3241 /*
3242 * Get next/previous match for a previous expanded pattern.
3243 */
3244 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3245 }
3246 else
3247 {
3248 /*
3249 * Translate string into pattern and expand it.
3250 */
3251 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3252 p2 = NULL;
3253 else
3254 {
3255 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3256 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3257 |options, type);
3258 vim_free(p1);
3259 /* longest match: make sure it is not shorter (happens with :help */
3260 if (p2 != NULL && type == WILD_LONGEST)
3261 {
3262 for (j = 0; j < oldlen; ++j)
3263 if (ccline.cmdbuff[i + j] == '*'
3264 || ccline.cmdbuff[i + j] == '?')
3265 break;
3266 if ((int)STRLEN(p2) < j)
3267 {
3268 vim_free(p2);
3269 p2 = NULL;
3270 }
3271 }
3272 }
3273 }
3274
3275 if (p2 != NULL && !got_int)
3276 {
3277 difflen = (int)STRLEN(p2) - oldlen;
3278 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3279 {
3280 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3281 xp->xp_pattern = ccline.cmdbuff + i;
3282 }
3283 else
3284 v = OK;
3285 if (v == OK)
3286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003287 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3288 &ccline.cmdbuff[ccline.cmdpos],
3289 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3290 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 ccline.cmdlen += difflen;
3292 ccline.cmdpos += difflen;
3293 }
3294 }
3295 vim_free(p2);
3296
3297 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003298 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299
3300 /* When expanding a ":map" command and no matches are found, assume that
3301 * the key is supposed to be inserted literally */
3302 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3303 return FAIL;
3304
3305 if (xp->xp_numfiles <= 0 && p2 == NULL)
3306 beep_flush();
3307 else if (xp->xp_numfiles == 1)
3308 /* free expanded pattern */
3309 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3310
3311 return OK;
3312}
3313
3314/*
3315 * Do wildcard expansion on the string 'str'.
3316 * Chars that should not be expanded must be preceded with a backslash.
3317 * Return a pointer to alloced memory containing the new string.
3318 * Return NULL for failure.
3319 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003320 * "orig" is the originally expanded string, copied to allocated memory. It
3321 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3322 * WILD_PREV "orig" should be NULL.
3323 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003324 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3325 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 *
3327 * mode = WILD_FREE: just free previously expanded matches
3328 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3329 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3330 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3331 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3332 * mode = WILD_ALL: return all matches concatenated
3333 * mode = WILD_LONGEST: return longest matched part
3334 *
3335 * options = WILD_LIST_NOTFOUND: list entries without a match
3336 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3337 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3338 * options = WILD_NO_BEEP: Don't beep for multiple matches
3339 * options = WILD_ADD_SLASH: add a slash after directory names
3340 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3341 * options = WILD_SILENT: don't print warning messages
3342 * options = WILD_ESCAPE: put backslash before special chars
3343 *
3344 * The variables xp->xp_context and xp->xp_backslash must have been set!
3345 */
3346 char_u *
3347ExpandOne(xp, str, orig, options, mode)
3348 expand_T *xp;
3349 char_u *str;
3350 char_u *orig; /* allocated copy of original of expanded string */
3351 int options;
3352 int mode;
3353{
3354 char_u *ss = NULL;
3355 static int findex;
3356 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003357 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 int i;
3359 long_u len;
3360 int non_suf_match; /* number without matching suffix */
3361
3362 /*
3363 * first handle the case of using an old match
3364 */
3365 if (mode == WILD_NEXT || mode == WILD_PREV)
3366 {
3367 if (xp->xp_numfiles > 0)
3368 {
3369 if (mode == WILD_PREV)
3370 {
3371 if (findex == -1)
3372 findex = xp->xp_numfiles;
3373 --findex;
3374 }
3375 else /* mode == WILD_NEXT */
3376 ++findex;
3377
3378 /*
3379 * When wrapping around, return the original string, set findex to
3380 * -1.
3381 */
3382 if (findex < 0)
3383 {
3384 if (orig_save == NULL)
3385 findex = xp->xp_numfiles - 1;
3386 else
3387 findex = -1;
3388 }
3389 if (findex >= xp->xp_numfiles)
3390 {
3391 if (orig_save == NULL)
3392 findex = 0;
3393 else
3394 findex = -1;
3395 }
3396#ifdef FEAT_WILDMENU
3397 if (p_wmnu)
3398 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3399 findex, cmd_showtail);
3400#endif
3401 if (findex == -1)
3402 return vim_strsave(orig_save);
3403 return vim_strsave(xp->xp_files[findex]);
3404 }
3405 else
3406 return NULL;
3407 }
3408
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003409 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3411 {
3412 FreeWild(xp->xp_numfiles, xp->xp_files);
3413 xp->xp_numfiles = -1;
3414 vim_free(orig_save);
3415 orig_save = NULL;
3416 }
3417 findex = 0;
3418
3419 if (mode == WILD_FREE) /* only release file name */
3420 return NULL;
3421
3422 if (xp->xp_numfiles == -1)
3423 {
3424 vim_free(orig_save);
3425 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003426 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
3428 /*
3429 * Do the expansion.
3430 */
3431 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3432 options) == FAIL)
3433 {
3434#ifdef FNAME_ILLEGAL
3435 /* Illegal file name has been silently skipped. But when there
3436 * are wildcards, the real problem is that there was no match,
3437 * causing the pattern to be added, which has illegal characters.
3438 */
3439 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3440 EMSG2(_(e_nomatch2), str);
3441#endif
3442 }
3443 else if (xp->xp_numfiles == 0)
3444 {
3445 if (!(options & WILD_SILENT))
3446 EMSG2(_(e_nomatch2), str);
3447 }
3448 else
3449 {
3450 /* Escape the matches for use on the command line. */
3451 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3452
3453 /*
3454 * Check for matching suffixes in file names.
3455 */
3456 if (mode != WILD_ALL && mode != WILD_LONGEST)
3457 {
3458 if (xp->xp_numfiles)
3459 non_suf_match = xp->xp_numfiles;
3460 else
3461 non_suf_match = 1;
3462 if ((xp->xp_context == EXPAND_FILES
3463 || xp->xp_context == EXPAND_DIRECTORIES)
3464 && xp->xp_numfiles > 1)
3465 {
3466 /*
3467 * More than one match; check suffix.
3468 * The files will have been sorted on matching suffix in
3469 * expand_wildcards, only need to check the first two.
3470 */
3471 non_suf_match = 0;
3472 for (i = 0; i < 2; ++i)
3473 if (match_suffix(xp->xp_files[i]))
3474 ++non_suf_match;
3475 }
3476 if (non_suf_match != 1)
3477 {
3478 /* Can we ever get here unless it's while expanding
3479 * interactively? If not, we can get rid of this all
3480 * together. Don't really want to wait for this message
3481 * (and possibly have to hit return to continue!).
3482 */
3483 if (!(options & WILD_SILENT))
3484 EMSG(_(e_toomany));
3485 else if (!(options & WILD_NO_BEEP))
3486 beep_flush();
3487 }
3488 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3489 ss = vim_strsave(xp->xp_files[0]);
3490 }
3491 }
3492 }
3493
3494 /* Find longest common part */
3495 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3496 {
3497 for (len = 0; xp->xp_files[0][len]; ++len)
3498 {
3499 for (i = 0; i < xp->xp_numfiles; ++i)
3500 {
3501#ifdef CASE_INSENSITIVE_FILENAME
3502 if (xp->xp_context == EXPAND_DIRECTORIES
3503 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003504 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 || xp->xp_context == EXPAND_BUFFERS)
3506 {
3507 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3508 TOLOWER_LOC(xp->xp_files[0][len]))
3509 break;
3510 }
3511 else
3512#endif
3513 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3514 break;
3515 }
3516 if (i < xp->xp_numfiles)
3517 {
3518 if (!(options & WILD_NO_BEEP))
3519 vim_beep();
3520 break;
3521 }
3522 }
3523 ss = alloc((unsigned)len + 1);
3524 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003525 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 findex = -1; /* next p_wc gets first one */
3527 }
3528
3529 /* Concatenate all matching names */
3530 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3531 {
3532 len = 0;
3533 for (i = 0; i < xp->xp_numfiles; ++i)
3534 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3535 ss = lalloc(len, TRUE);
3536 if (ss != NULL)
3537 {
3538 *ss = NUL;
3539 for (i = 0; i < xp->xp_numfiles; ++i)
3540 {
3541 STRCAT(ss, xp->xp_files[i]);
3542 if (i != xp->xp_numfiles - 1)
3543 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3544 }
3545 }
3546 }
3547
3548 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3549 ExpandCleanup(xp);
3550
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003551 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003552 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003553 vim_free(orig);
3554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 return ss;
3556}
3557
3558/*
3559 * Prepare an expand structure for use.
3560 */
3561 void
3562ExpandInit(xp)
3563 expand_T *xp;
3564{
3565 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003566#ifndef BACKSLASH_IN_FILENAME
3567 xp->xp_shell = FALSE;
3568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 xp->xp_numfiles = -1;
3570 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003571#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3572 xp->xp_arg = NULL;
3573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574}
3575
3576/*
3577 * Cleanup an expand structure after use.
3578 */
3579 void
3580ExpandCleanup(xp)
3581 expand_T *xp;
3582{
3583 if (xp->xp_numfiles >= 0)
3584 {
3585 FreeWild(xp->xp_numfiles, xp->xp_files);
3586 xp->xp_numfiles = -1;
3587 }
3588}
3589
3590 void
3591ExpandEscape(xp, str, numfiles, files, options)
3592 expand_T *xp;
3593 char_u *str;
3594 int numfiles;
3595 char_u **files;
3596 int options;
3597{
3598 int i;
3599 char_u *p;
3600
3601 /*
3602 * May change home directory back to "~"
3603 */
3604 if (options & WILD_HOME_REPLACE)
3605 tilde_replace(str, numfiles, files);
3606
3607 if (options & WILD_ESCAPE)
3608 {
3609 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003610 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 || xp->xp_context == EXPAND_BUFFERS
3612 || xp->xp_context == EXPAND_DIRECTORIES)
3613 {
3614 /*
3615 * Insert a backslash into a file name before a space, \, %, #
3616 * and wildmatch characters, except '~'.
3617 */
3618 for (i = 0; i < numfiles; ++i)
3619 {
3620 /* for ":set path=" we need to escape spaces twice */
3621 if (xp->xp_backslash == XP_BS_THREE)
3622 {
3623 p = vim_strsave_escaped(files[i], (char_u *)" ");
3624 if (p != NULL)
3625 {
3626 vim_free(files[i]);
3627 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003628#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 p = vim_strsave_escaped(files[i], (char_u *)" ");
3630 if (p != NULL)
3631 {
3632 vim_free(files[i]);
3633 files[i] = p;
3634 }
3635#endif
3636 }
3637 }
3638#ifdef BACKSLASH_IN_FILENAME
3639 {
3640 char_u buf[20];
3641 int j = 0;
3642
3643 /* Don't escape '[' and '{' if they are in 'isfname'. */
3644 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3645 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3646 buf[j++] = *p;
3647 buf[j] = NUL;
3648 p = vim_strsave_escaped(files[i], buf);
3649 }
3650#else
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003651 p = vim_strsave_escaped(files[i],
3652 xp->xp_shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653#endif
3654 if (p != NULL)
3655 {
3656 vim_free(files[i]);
3657 files[i] = p;
3658 }
3659
3660 /* If 'str' starts with "\~", replace "~" at start of
3661 * files[i] with "\~". */
3662 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003663 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003666
3667 /* If the first file starts with a '+' escape it. Otherwise it
3668 * could be seen as "+cmd". */
3669 if (*files[0] == '+')
3670 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
3672 else if (xp->xp_context == EXPAND_TAGS)
3673 {
3674 /*
3675 * Insert a backslash before characters in a tag name that
3676 * would terminate the ":tag" command.
3677 */
3678 for (i = 0; i < numfiles; ++i)
3679 {
3680 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3681 if (p != NULL)
3682 {
3683 vim_free(files[i]);
3684 files[i] = p;
3685 }
3686 }
3687 }
3688 }
3689}
3690
3691/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003692 * Put a backslash before the file name in "pp", which is in allocated memory.
3693 */
3694 static void
3695escape_fname(pp)
3696 char_u **pp;
3697{
3698 char_u *p;
3699
3700 p = alloc((unsigned)(STRLEN(*pp) + 2));
3701 if (p != NULL)
3702 {
3703 p[0] = '\\';
3704 STRCPY(p + 1, *pp);
3705 vim_free(*pp);
3706 *pp = p;
3707 }
3708}
3709
3710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 * For each file name in files[num_files]:
3712 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3713 */
3714 void
3715tilde_replace(orig_pat, num_files, files)
3716 char_u *orig_pat;
3717 int num_files;
3718 char_u **files;
3719{
3720 int i;
3721 char_u *p;
3722
3723 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3724 {
3725 for (i = 0; i < num_files; ++i)
3726 {
3727 p = home_replace_save(NULL, files[i]);
3728 if (p != NULL)
3729 {
3730 vim_free(files[i]);
3731 files[i] = p;
3732 }
3733 }
3734 }
3735}
3736
3737/*
3738 * Show all matches for completion on the command line.
3739 * Returns EXPAND_NOTHING when the character that triggered expansion should
3740 * be inserted like a normal character.
3741 */
3742/*ARGSUSED*/
3743 static int
3744showmatches(xp, wildmenu)
3745 expand_T *xp;
3746 int wildmenu;
3747{
3748#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3749 int num_files;
3750 char_u **files_found;
3751 int i, j, k;
3752 int maxlen;
3753 int lines;
3754 int columns;
3755 char_u *p;
3756 int lastlen;
3757 int attr;
3758 int showtail;
3759
3760 if (xp->xp_numfiles == -1)
3761 {
3762 set_expand_context(xp);
3763 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3764 &num_files, &files_found);
3765 showtail = expand_showtail(xp);
3766 if (i != EXPAND_OK)
3767 return i;
3768
3769 }
3770 else
3771 {
3772 num_files = xp->xp_numfiles;
3773 files_found = xp->xp_files;
3774 showtail = cmd_showtail;
3775 }
3776
3777#ifdef FEAT_WILDMENU
3778 if (!wildmenu)
3779 {
3780#endif
3781 msg_didany = FALSE; /* lines_left will be set */
3782 msg_start(); /* prepare for paging */
3783 msg_putchar('\n');
3784 out_flush();
3785 cmdline_row = msg_row;
3786 msg_didany = FALSE; /* lines_left will be set again */
3787 msg_start(); /* prepare for paging */
3788#ifdef FEAT_WILDMENU
3789 }
3790#endif
3791
3792 if (got_int)
3793 got_int = FALSE; /* only int. the completion, not the cmd line */
3794#ifdef FEAT_WILDMENU
3795 else if (wildmenu)
3796 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3797#endif
3798 else
3799 {
3800 /* find the length of the longest file name */
3801 maxlen = 0;
3802 for (i = 0; i < num_files; ++i)
3803 {
3804 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003805 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 || xp->xp_context == EXPAND_BUFFERS))
3807 {
3808 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3809 j = vim_strsize(NameBuff);
3810 }
3811 else
3812 j = vim_strsize(L_SHOWFILE(i));
3813 if (j > maxlen)
3814 maxlen = j;
3815 }
3816
3817 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3818 lines = num_files;
3819 else
3820 {
3821 /* compute the number of columns and lines for the listing */
3822 maxlen += 2; /* two spaces between file names */
3823 columns = ((int)Columns + 2) / maxlen;
3824 if (columns < 1)
3825 columns = 1;
3826 lines = (num_files + columns - 1) / columns;
3827 }
3828
3829 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3830
3831 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3832 {
3833 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3834 msg_clr_eos();
3835 msg_advance(maxlen - 3);
3836 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3837 }
3838
3839 /* list the files line by line */
3840 for (i = 0; i < lines; ++i)
3841 {
3842 lastlen = 999;
3843 for (k = i; k < num_files; k += lines)
3844 {
3845 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3846 {
3847 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3848 p = files_found[k] + STRLEN(files_found[k]) + 1;
3849 msg_advance(maxlen + 1);
3850 msg_puts(p);
3851 msg_advance(maxlen + 3);
3852 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3853 break;
3854 }
3855 for (j = maxlen - lastlen; --j >= 0; )
3856 msg_putchar(' ');
3857 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003858 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 || xp->xp_context == EXPAND_BUFFERS)
3860 {
3861 /* highlight directories */
3862 j = (mch_isdir(files_found[k]));
3863 if (showtail)
3864 p = L_SHOWFILE(k);
3865 else
3866 {
3867 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3868 TRUE);
3869 p = NameBuff;
3870 }
3871 }
3872 else
3873 {
3874 j = FALSE;
3875 p = L_SHOWFILE(k);
3876 }
3877 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3878 }
3879 if (msg_col > 0) /* when not wrapped around */
3880 {
3881 msg_clr_eos();
3882 msg_putchar('\n');
3883 }
3884 out_flush(); /* show one line at a time */
3885 if (got_int)
3886 {
3887 got_int = FALSE;
3888 break;
3889 }
3890 }
3891
3892 /*
3893 * we redraw the command below the lines that we have just listed
3894 * This is a bit tricky, but it saves a lot of screen updating.
3895 */
3896 cmdline_row = msg_row; /* will put it back later */
3897 }
3898
3899 if (xp->xp_numfiles == -1)
3900 FreeWild(num_files, files_found);
3901
3902 return EXPAND_OK;
3903}
3904
3905/*
3906 * Private gettail for showmatches() (and win_redr_status_matches()):
3907 * Find tail of file name path, but ignore trailing "/".
3908 */
3909 char_u *
3910sm_gettail(s)
3911 char_u *s;
3912{
3913 char_u *p;
3914 char_u *t = s;
3915 int had_sep = FALSE;
3916
3917 for (p = s; *p != NUL; )
3918 {
3919 if (vim_ispathsep(*p)
3920#ifdef BACKSLASH_IN_FILENAME
3921 && !rem_backslash(p)
3922#endif
3923 )
3924 had_sep = TRUE;
3925 else if (had_sep)
3926 {
3927 t = p;
3928 had_sep = FALSE;
3929 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003930 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932 return t;
3933}
3934
3935/*
3936 * Return TRUE if we only need to show the tail of completion matches.
3937 * When not completing file names or there is a wildcard in the path FALSE is
3938 * returned.
3939 */
3940 static int
3941expand_showtail(xp)
3942 expand_T *xp;
3943{
3944 char_u *s;
3945 char_u *end;
3946
3947 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003948 if (xp->xp_context != EXPAND_FILES
3949 && xp->xp_context != EXPAND_SHELLCMD
3950 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 return FALSE;
3952
3953 end = gettail(xp->xp_pattern);
3954 if (end == xp->xp_pattern) /* there is no path separator */
3955 return FALSE;
3956
3957 for (s = xp->xp_pattern; s < end; s++)
3958 {
3959 /* Skip escaped wildcards. Only when the backslash is not a path
3960 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3961 if (rem_backslash(s))
3962 ++s;
3963 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3964 return FALSE;
3965 }
3966 return TRUE;
3967}
3968
3969/*
3970 * Prepare a string for expansion.
3971 * When expanding file names: The string will be used with expand_wildcards().
3972 * Copy the file name into allocated memory and add a '*' at the end.
3973 * When expanding other names: The string will be used with regcomp(). Copy
3974 * the name into allocated memory and prepend "^".
3975 */
3976 char_u *
3977addstar(fname, len, context)
3978 char_u *fname;
3979 int len;
3980 int context; /* EXPAND_FILES etc. */
3981{
3982 char_u *retval;
3983 int i, j;
3984 int new_len;
3985 char_u *tail;
3986
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003987 if (context != EXPAND_FILES
3988 && context != EXPAND_SHELLCMD
3989 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 {
3991 /*
3992 * Matching will be done internally (on something other than files).
3993 * So we convert the file-matching-type wildcards into our kind for
3994 * use with vim_regcomp(). First work out how long it will be:
3995 */
3996
3997 /* For help tags the translation is done in find_help_tags().
3998 * For a tag pattern starting with "/" no translation is needed. */
3999 if (context == EXPAND_HELP
4000 || context == EXPAND_COLORS
4001 || context == EXPAND_COMPILER
4002 || (context == EXPAND_TAGS && fname[0] == '/'))
4003 retval = vim_strnsave(fname, len);
4004 else
4005 {
4006 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4007 for (i = 0; i < len; i++)
4008 {
4009 if (fname[i] == '*' || fname[i] == '~')
4010 new_len++; /* '*' needs to be replaced by ".*"
4011 '~' needs to be replaced by "\~" */
4012
4013 /* Buffer names are like file names. "." should be literal */
4014 if (context == EXPAND_BUFFERS && fname[i] == '.')
4015 new_len++; /* "." becomes "\." */
4016
4017 /* Custom expansion takes care of special things, match
4018 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004019 if ((context == EXPAND_USER_DEFINED
4020 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 new_len++; /* '\' becomes "\\" */
4022 }
4023 retval = alloc(new_len);
4024 if (retval != NULL)
4025 {
4026 retval[0] = '^';
4027 j = 1;
4028 for (i = 0; i < len; i++, j++)
4029 {
4030 /* Skip backslash. But why? At least keep it for custom
4031 * expansion. */
4032 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004033 && context != EXPAND_USER_LIST
4034 && fname[i] == '\\'
4035 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 break;
4037
4038 switch (fname[i])
4039 {
4040 case '*': retval[j++] = '.';
4041 break;
4042 case '~': retval[j++] = '\\';
4043 break;
4044 case '?': retval[j] = '.';
4045 continue;
4046 case '.': if (context == EXPAND_BUFFERS)
4047 retval[j++] = '\\';
4048 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004049 case '\\': if (context == EXPAND_USER_DEFINED
4050 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 retval[j++] = '\\';
4052 break;
4053 }
4054 retval[j] = fname[i];
4055 }
4056 retval[j] = NUL;
4057 }
4058 }
4059 }
4060 else
4061 {
4062 retval = alloc(len + 4);
4063 if (retval != NULL)
4064 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004065 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
4067 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004068 * Don't add a star to *, ~, ~user, $var or `cmd`.
4069 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 * ~ would be at the start of the file name, but not the tail.
4071 * $ could be anywhere in the tail.
4072 * ` could be anywhere in the file name.
4073 */
4074 tail = gettail(retval);
4075 if ((*retval != '~' || tail != retval)
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004076 && (len == 0 || retval[len - 1] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 && vim_strchr(tail, '$') == NULL
4078 && vim_strchr(retval, '`') == NULL)
4079 retval[len++] = '*';
4080 retval[len] = NUL;
4081 }
4082 }
4083 return retval;
4084}
4085
4086/*
4087 * Must parse the command line so far to work out what context we are in.
4088 * Completion can then be done based on that context.
4089 * This routine sets the variables:
4090 * xp->xp_pattern The start of the pattern to be expanded within
4091 * the command line (ends at the cursor).
4092 * xp->xp_context The type of thing to expand. Will be one of:
4093 *
4094 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4095 * the command line, like an unknown command. Caller
4096 * should beep.
4097 * EXPAND_NOTHING Unrecognised context for completion, use char like
4098 * a normal char, rather than for completion. eg
4099 * :s/^I/
4100 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4101 * it.
4102 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4103 * EXPAND_FILES After command with XFILE set, or after setting
4104 * with P_EXPAND set. eg :e ^I, :w>>^I
4105 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4106 * when we know only directories are of interest. eg
4107 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004108 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4110 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4111 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4112 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4113 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4114 * EXPAND_EVENTS Complete event names
4115 * EXPAND_SYNTAX Complete :syntax command arguments
4116 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4117 * EXPAND_AUGROUP Complete autocommand group names
4118 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4119 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4120 * eg :unmap a^I , :cunab x^I
4121 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4122 * eg :call sub^I
4123 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4124 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4125 * names in expressions, eg :while s^I
4126 * EXPAND_ENV_VARS Complete environment variable names
4127 */
4128 static void
4129set_expand_context(xp)
4130 expand_T *xp;
4131{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004132 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 if (ccline.cmdfirstc != ':'
4134#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004135 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004136 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137#endif
4138 )
4139 {
4140 xp->xp_context = EXPAND_NOTHING;
4141 return;
4142 }
4143 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4144}
4145
4146 void
4147set_cmd_context(xp, str, len, col)
4148 expand_T *xp;
4149 char_u *str; /* start of command line */
4150 int len; /* length of command line (excl. NUL) */
4151 int col; /* position of cursor */
4152{
4153 int old_char = NUL;
4154 char_u *nextcomm;
4155
4156 /*
4157 * Avoid a UMR warning from Purify, only save the character if it has been
4158 * written before.
4159 */
4160 if (col < len)
4161 old_char = str[col];
4162 str[col] = NUL;
4163 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004164
4165#ifdef FEAT_EVAL
4166 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004167 {
4168# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004169 /* pass CMD_SIZE because there is no real command */
4170 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004171# endif
4172 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004173 else if (ccline.input_fn)
4174 {
4175 xp->xp_context = ccline.xp_context;
4176 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004177# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004178 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004179# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004180 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004181 else
4182#endif
4183 while (nextcomm != NULL)
4184 nextcomm = set_one_cmd_context(xp, nextcomm);
4185
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 str[col] = old_char;
4187}
4188
4189/*
4190 * Expand the command line "str" from context "xp".
4191 * "xp" must have been set by set_cmd_context().
4192 * xp->xp_pattern points into "str", to where the text that is to be expanded
4193 * starts.
4194 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4195 * cursor.
4196 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4197 * key that triggered expansion literally.
4198 * Returns EXPAND_OK otherwise.
4199 */
4200 int
4201expand_cmdline(xp, str, col, matchcount, matches)
4202 expand_T *xp;
4203 char_u *str; /* start of command line */
4204 int col; /* position of cursor */
4205 int *matchcount; /* return: nr of matches */
4206 char_u ***matches; /* return: array of pointers to matches */
4207{
4208 char_u *file_str = NULL;
4209
4210 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4211 {
4212 beep_flush();
4213 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4214 }
4215 if (xp->xp_context == EXPAND_NOTHING)
4216 {
4217 /* Caller can use the character as a normal char instead */
4218 return EXPAND_NOTHING;
4219 }
4220
4221 /* add star to file name, or convert to regexp if not exp. files. */
4222 file_str = addstar(xp->xp_pattern,
4223 (int)(str + col - xp->xp_pattern), xp->xp_context);
4224 if (file_str == NULL)
4225 return EXPAND_UNSUCCESSFUL;
4226
4227 /* find all files that match the description */
4228 if (ExpandFromContext(xp, file_str, matchcount, matches,
4229 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4230 {
4231 *matchcount = 0;
4232 *matches = NULL;
4233 }
4234 vim_free(file_str);
4235
4236 return EXPAND_OK;
4237}
4238
4239#ifdef FEAT_MULTI_LANG
4240/*
4241 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4242 */
4243static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4244
4245 static void
4246cleanup_help_tags(num_file, file)
4247 int num_file;
4248 char_u **file;
4249{
4250 int i, j;
4251 int len;
4252
4253 for (i = 0; i < num_file; ++i)
4254 {
4255 len = (int)STRLEN(file[i]) - 3;
4256 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4257 {
4258 /* Sorting on priority means the same item in another language may
4259 * be anywhere. Search all items for a match up to the "@en". */
4260 for (j = 0; j < num_file; ++j)
4261 if (j != i
4262 && (int)STRLEN(file[j]) == len + 3
4263 && STRNCMP(file[i], file[j], len + 1) == 0)
4264 break;
4265 if (j == num_file)
4266 file[i][len] = NUL;
4267 }
4268 }
4269}
4270#endif
4271
4272/*
4273 * Do the expansion based on xp->xp_context and "pat".
4274 */
4275 static int
4276ExpandFromContext(xp, pat, num_file, file, options)
4277 expand_T *xp;
4278 char_u *pat;
4279 int *num_file;
4280 char_u ***file;
4281 int options;
4282{
4283#ifdef FEAT_CMDL_COMPL
4284 regmatch_T regmatch;
4285#endif
4286 int ret;
4287 int flags;
4288
4289 flags = EW_DIR; /* include directories */
4290 if (options & WILD_LIST_NOTFOUND)
4291 flags |= EW_NOTFOUND;
4292 if (options & WILD_ADD_SLASH)
4293 flags |= EW_ADDSLASH;
4294 if (options & WILD_KEEP_ALL)
4295 flags |= EW_KEEPALL;
4296 if (options & WILD_SILENT)
4297 flags |= EW_SILENT;
4298
4299 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4300 {
4301 /*
4302 * Expand file or directory names.
4303 */
4304 int free_pat = FALSE;
4305 int i;
4306
4307 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4308 * space */
4309 if (xp->xp_backslash != XP_BS_NONE)
4310 {
4311 free_pat = TRUE;
4312 pat = vim_strsave(pat);
4313 for (i = 0; pat[i]; ++i)
4314 if (pat[i] == '\\')
4315 {
4316 if (xp->xp_backslash == XP_BS_THREE
4317 && pat[i + 1] == '\\'
4318 && pat[i + 2] == '\\'
4319 && pat[i + 3] == ' ')
Bram Moolenaar452a81b2007-08-06 20:28:43 +00004320 mch_memmove(pat + i, pat + i + 3,
4321 STRLEN(pat + i + 3) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 if (xp->xp_backslash == XP_BS_ONE
4323 && pat[i + 1] == ' ')
Bram Moolenaar452a81b2007-08-06 20:28:43 +00004324 mch_memmove(pat + i, pat + i + 1, STRLEN(pat + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
4326 }
4327
4328 if (xp->xp_context == EXPAND_FILES)
4329 flags |= EW_FILE;
4330 else
4331 flags = (flags | EW_DIR) & ~EW_FILE;
4332 ret = expand_wildcards(1, &pat, num_file, file, flags);
4333 if (free_pat)
4334 vim_free(pat);
4335 return ret;
4336 }
4337
4338 *file = (char_u **)"";
4339 *num_file = 0;
4340 if (xp->xp_context == EXPAND_HELP)
4341 {
4342 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4343 {
4344#ifdef FEAT_MULTI_LANG
4345 cleanup_help_tags(*num_file, *file);
4346#endif
4347 return OK;
4348 }
4349 return FAIL;
4350 }
4351
4352#ifndef FEAT_CMDL_COMPL
4353 return FAIL;
4354#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004355 if (xp->xp_context == EXPAND_SHELLCMD)
4356 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 if (xp->xp_context == EXPAND_OLD_SETTING)
4358 return ExpandOldSetting(num_file, file);
4359 if (xp->xp_context == EXPAND_BUFFERS)
4360 return ExpandBufnames(pat, num_file, file, options);
4361 if (xp->xp_context == EXPAND_TAGS
4362 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4363 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4364 if (xp->xp_context == EXPAND_COLORS)
4365 return ExpandRTDir(pat, num_file, file, "colors");
4366 if (xp->xp_context == EXPAND_COMPILER)
4367 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004368# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4369 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004370 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004371# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
4373 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4374 if (regmatch.regprog == NULL)
4375 return FAIL;
4376
4377 /* set ignore-case according to p_ic, p_scs and pat */
4378 regmatch.rm_ic = ignorecase(pat);
4379
4380 if (xp->xp_context == EXPAND_SETTINGS
4381 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4382 ret = ExpandSettings(xp, &regmatch, num_file, file);
4383 else if (xp->xp_context == EXPAND_MAPPINGS)
4384 ret = ExpandMappings(&regmatch, num_file, file);
4385# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4386 else if (xp->xp_context == EXPAND_USER_DEFINED)
4387 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4388# endif
4389 else
4390 {
4391 static struct expgen
4392 {
4393 int context;
4394 char_u *((*func)__ARGS((expand_T *, int)));
4395 int ic;
4396 } tab[] =
4397 {
4398 {EXPAND_COMMANDS, get_command_name, FALSE},
4399#ifdef FEAT_USR_CMDS
4400 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4401 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4402 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4403 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4404#endif
4405#ifdef FEAT_EVAL
4406 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4407 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4408 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4409 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4410#endif
4411#ifdef FEAT_MENU
4412 {EXPAND_MENUS, get_menu_name, FALSE},
4413 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4414#endif
4415#ifdef FEAT_SYN_HL
4416 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4417#endif
4418 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4419#ifdef FEAT_AUTOCMD
4420 {EXPAND_EVENTS, get_event_name, TRUE},
4421 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4422#endif
4423#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4424 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4425 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4426#endif
4427 {EXPAND_ENV_VARS, get_env_name, TRUE},
4428 };
4429 int i;
4430
4431 /*
4432 * Find a context in the table and call the ExpandGeneric() with the
4433 * right function to do the expansion.
4434 */
4435 ret = FAIL;
4436 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4437 if (xp->xp_context == tab[i].context)
4438 {
4439 if (tab[i].ic)
4440 regmatch.rm_ic = TRUE;
4441 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4442 break;
4443 }
4444 }
4445
4446 vim_free(regmatch.regprog);
4447
4448 return ret;
4449#endif /* FEAT_CMDL_COMPL */
4450}
4451
4452#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4453/*
4454 * Expand a list of names.
4455 *
4456 * Generic function for command line completion. It calls a function to
4457 * obtain strings, one by one. The strings are matched against a regexp
4458 * program. Matching strings are copied into an array, which is returned.
4459 *
4460 * Returns OK when no problems encountered, FAIL for error (out of memory).
4461 */
4462 int
4463ExpandGeneric(xp, regmatch, num_file, file, func)
4464 expand_T *xp;
4465 regmatch_T *regmatch;
4466 int *num_file;
4467 char_u ***file;
4468 char_u *((*func)__ARGS((expand_T *, int)));
4469 /* returns a string from the list */
4470{
4471 int i;
4472 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004473 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 char_u *str;
4475
4476 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004477 * round == 0: count the number of matching names
4478 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004480 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
4482 for (i = 0; ; ++i)
4483 {
4484 str = (*func)(xp, i);
4485 if (str == NULL) /* end of list */
4486 break;
4487 if (*str == NUL) /* skip empty strings */
4488 continue;
4489
4490 if (vim_regexec(regmatch, str, (colnr_T)0))
4491 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004492 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 {
4494 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4495 (*file)[count] = str;
4496#ifdef FEAT_MENU
4497 if (func == get_menu_names && str != NULL)
4498 {
4499 /* test for separator added by get_menu_names() */
4500 str += STRLEN(str) - 1;
4501 if (*str == '\001')
4502 *str = '.';
4503 }
4504#endif
4505 }
4506 ++count;
4507 }
4508 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004509 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 {
4511 if (count == 0)
4512 return OK;
4513 *num_file = count;
4514 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4515 if (*file == NULL)
4516 {
4517 *file = (char_u **)"";
4518 return FAIL;
4519 }
4520 count = 0;
4521 }
4522 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004523
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004524 /* Sort the results. Keep menu's in the specified order. */
4525 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4526 sort_strings(*file, *num_file);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004527
Bram Moolenaar4f688582007-07-24 12:34:30 +00004528#ifdef FEAT_CMDL_COMPL
4529 /* Reset the variables used for special highlight names expansion, so that
4530 * they don't show up when getting normal highlight names by ID. */
4531 reset_expand_highlight();
4532#endif
4533
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 return OK;
4535}
4536
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004537/*
4538 * Complete a shell command.
4539 * Returns FAIL or OK;
4540 */
4541 static int
4542expand_shellcmd(filepat, num_file, file, flagsarg)
4543 char_u *filepat; /* pattern to match with command names */
4544 int *num_file; /* return: number of matches */
4545 char_u ***file; /* return: array with matches */
4546 int flagsarg; /* EW_ flags */
4547{
4548 char_u *pat;
4549 int i;
4550 char_u *path;
4551 int mustfree = FALSE;
4552 garray_T ga;
4553 char_u *buf = alloc(MAXPATHL);
4554 size_t l;
4555 char_u *s, *e;
4556 int flags = flagsarg;
4557 int ret;
4558
4559 if (buf == NULL)
4560 return FAIL;
4561
4562 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4563 * space */
4564 pat = vim_strsave(filepat);
4565 for (i = 0; pat[i]; ++i)
4566 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar452a81b2007-08-06 20:28:43 +00004567 mch_memmove(pat + i, pat + i + 1, STRLEN(pat + i));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004568
4569 flags |= EW_FILE | EW_EXEC;
4570
4571 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004572 if (mch_isFullName(pat))
4573 path = (char_u *)" ";
4574 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004575 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4576 path = (char_u *)".";
4577 else
4578 path = vim_getenv((char_u *)"PATH", &mustfree);
4579
4580 /*
4581 * Go over all directories in $PATH. Expand matches in that directory and
4582 * collect them in "ga".
4583 */
4584 ga_init2(&ga, (int)sizeof(char *), 10);
4585 for (s = path; *s != NUL; s = e)
4586 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004587 if (*s == ' ')
4588 ++s; /* Skip space used for absolute path name. */
4589
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004590#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4591 e = vim_strchr(s, ';');
4592#else
4593 e = vim_strchr(s, ':');
4594#endif
4595 if (e == NULL)
4596 e = s + STRLEN(s);
4597
4598 l = e - s;
4599 if (l > MAXPATHL - 5)
4600 break;
4601 vim_strncpy(buf, s, l);
4602 add_pathsep(buf);
4603 l = STRLEN(buf);
4604 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4605
4606 /* Expand matches in one directory of $PATH. */
4607 ret = expand_wildcards(1, &buf, num_file, file, flags);
4608 if (ret == OK)
4609 {
4610 if (ga_grow(&ga, *num_file) == FAIL)
4611 FreeWild(*num_file, *file);
4612 else
4613 {
4614 for (i = 0; i < *num_file; ++i)
4615 {
4616 s = (*file)[i];
4617 if (STRLEN(s) > l)
4618 {
4619 /* Remove the path again. */
4620 mch_memmove(s, s + l, STRLEN(s + l) + 1);
4621 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4622 }
4623 else
4624 vim_free(s);
4625 }
4626 vim_free(*file);
4627 }
4628 }
4629 if (*e != NUL)
4630 ++e;
4631 }
4632 *file = ga.ga_data;
4633 *num_file = ga.ga_len;
4634
4635 vim_free(buf);
4636 vim_free(pat);
4637 if (mustfree)
4638 vim_free(path);
4639 return OK;
4640}
4641
4642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004644static 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));
4645
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004647 * call "user_expand_func()" to invoke a user defined VimL function and return
4648 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004650 static void *
4651call_user_expand_func(user_expand_func, xp, num_file, file)
4652 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 int *num_file;
4655 char_u ***file;
4656{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004657 char_u keep;
4658 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004661 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004662 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
4664 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004665 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 *num_file = 0;
4667 *file = NULL;
4668
4669 keep = ccline.cmdbuff[ccline.cmdlen];
4670 ccline.cmdbuff[ccline.cmdlen] = 0;
4671 sprintf((char *)num, "%d", ccline.cmdpos);
4672 args[0] = xp->xp_pattern;
4673 args[1] = ccline.cmdbuff;
4674 args[2] = num;
4675
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004676 /* Save the cmdline, we don't know what the function may do. */
4677 save_ccline = ccline;
4678 ccline.cmdbuff = NULL;
4679 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004681
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004682 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004683
4684 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004688
4689 return ret;
4690}
4691
4692/*
4693 * Expand names with a function defined by the user.
4694 */
4695 static int
4696ExpandUserDefined(xp, regmatch, num_file, file)
4697 expand_T *xp;
4698 regmatch_T *regmatch;
4699 int *num_file;
4700 char_u ***file;
4701{
4702 char_u *retstr;
4703 char_u *s;
4704 char_u *e;
4705 char_u keep;
4706 garray_T ga;
4707
4708 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4709 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 return FAIL;
4711
4712 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004713 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
4715 e = vim_strchr(s, '\n');
4716 if (e == NULL)
4717 e = s + STRLEN(s);
4718 keep = *e;
4719 *e = 0;
4720
4721 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4722 {
4723 *e = keep;
4724 if (*e != NUL)
4725 ++e;
4726 continue;
4727 }
4728
4729 if (ga_grow(&ga, 1) == FAIL)
4730 break;
4731
4732 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4733 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734
4735 *e = keep;
4736 if (*e != NUL)
4737 ++e;
4738 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004739 vim_free(retstr);
4740 *file = ga.ga_data;
4741 *num_file = ga.ga_len;
4742 return OK;
4743}
4744
4745/*
4746 * Expand names with a list returned by a function defined by the user.
4747 */
4748 static int
4749ExpandUserList(xp, num_file, file)
4750 expand_T *xp;
4751 int *num_file;
4752 char_u ***file;
4753{
4754 list_T *retlist;
4755 listitem_T *li;
4756 garray_T ga;
4757
4758 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4759 if (retlist == NULL)
4760 return FAIL;
4761
4762 ga_init2(&ga, (int)sizeof(char *), 3);
4763 /* Loop over the items in the list. */
4764 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4765 {
4766 if (li->li_tv.v_type != VAR_STRING)
4767 continue; /* Skip non-string items */
4768
4769 if (ga_grow(&ga, 1) == FAIL)
4770 break;
4771
4772 ((char_u **)ga.ga_data)[ga.ga_len] =
4773 vim_strsave(li->li_tv.vval.v_string);
4774 ++ga.ga_len;
4775 }
4776 list_unref(retlist);
4777
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 *file = ga.ga_data;
4779 *num_file = ga.ga_len;
4780 return OK;
4781}
4782#endif
4783
4784/*
4785 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4786 * or compiler names.
4787 */
4788 static int
4789ExpandRTDir(pat, num_file, file, dirname)
4790 char_u *pat;
4791 int *num_file;
4792 char_u ***file;
4793 char *dirname; /* "colors" or "compiler" */
4794{
4795 char_u *all;
4796 char_u *s;
4797 char_u *e;
4798 garray_T ga;
4799
4800 *num_file = 0;
4801 *file = NULL;
4802 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4803 if (s == NULL)
4804 return FAIL;
4805 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4806 all = globpath(p_rtp, s);
4807 vim_free(s);
4808 if (all == NULL)
4809 return FAIL;
4810
4811 ga_init2(&ga, (int)sizeof(char *), 3);
4812 for (s = all; *s != NUL; s = e)
4813 {
4814 e = vim_strchr(s, '\n');
4815 if (e == NULL)
4816 e = s + STRLEN(s);
4817 if (ga_grow(&ga, 1) == FAIL)
4818 break;
4819 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4820 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004821 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 if (*s == '\n' || vim_ispathsep(*s))
4823 break;
4824 ++s;
4825 ((char_u **)ga.ga_data)[ga.ga_len] =
4826 vim_strnsave(s, (int)(e - s - 4));
4827 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
4829 if (*e != NUL)
4830 ++e;
4831 }
4832 vim_free(all);
4833 *file = ga.ga_data;
4834 *num_file = ga.ga_len;
4835 return OK;
4836}
4837
4838#endif
4839
4840#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4841/*
4842 * Expand "file" for all comma-separated directories in "path".
4843 * Returns an allocated string with all matches concatenated, separated by
4844 * newlines. Returns NULL for an error or no matches.
4845 */
4846 char_u *
4847globpath(path, file)
4848 char_u *path;
4849 char_u *file;
4850{
4851 expand_T xpc;
4852 char_u *buf;
4853 garray_T ga;
4854 int i;
4855 int len;
4856 int num_p;
4857 char_u **p;
4858 char_u *cur = NULL;
4859
4860 buf = alloc(MAXPATHL);
4861 if (buf == NULL)
4862 return NULL;
4863
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004864 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004866
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 ga_init2(&ga, 1, 100);
4868
4869 /* Loop over all entries in {path}. */
4870 while (*path != NUL)
4871 {
4872 /* Copy one item of the path to buf[] and concatenate the file name. */
4873 copy_option_part(&path, buf, MAXPATHL, ",");
4874 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4875 {
4876 add_pathsep(buf);
4877 STRCAT(buf, file);
4878 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4879 && num_p > 0)
4880 {
4881 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4882 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004883 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
4885 /* Concatenate new results to previous ones. */
4886 if (ga_grow(&ga, len) == OK)
4887 {
4888 cur = (char_u *)ga.ga_data + ga.ga_len;
4889 for (i = 0; i < num_p; ++i)
4890 {
4891 STRCPY(cur, p[i]);
4892 cur += STRLEN(p[i]);
4893 *cur++ = '\n';
4894 }
4895 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 }
4897 FreeWild(num_p, p);
4898 }
4899 }
4900 }
4901 if (cur != NULL)
4902 *--cur = 0; /* Replace trailing newline with NUL */
4903
4904 vim_free(buf);
4905 return (char_u *)ga.ga_data;
4906}
4907
4908#endif
4909
4910#if defined(FEAT_CMDHIST) || defined(PROTO)
4911
4912/*********************************
4913 * Command line history stuff *
4914 *********************************/
4915
4916/*
4917 * Translate a history character to the associated type number.
4918 */
4919 static int
4920hist_char2type(c)
4921 int c;
4922{
4923 if (c == ':')
4924 return HIST_CMD;
4925 if (c == '=')
4926 return HIST_EXPR;
4927 if (c == '@')
4928 return HIST_INPUT;
4929 if (c == '>')
4930 return HIST_DEBUG;
4931 return HIST_SEARCH; /* must be '?' or '/' */
4932}
4933
4934/*
4935 * Table of history names.
4936 * These names are used in :history and various hist...() functions.
4937 * It is sufficient to give the significant prefix of a history name.
4938 */
4939
4940static char *(history_names[]) =
4941{
4942 "cmd",
4943 "search",
4944 "expr",
4945 "input",
4946 "debug",
4947 NULL
4948};
4949
4950/*
4951 * init_history() - Initialize the command line history.
4952 * Also used to re-allocate the history when the size changes.
4953 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004954 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955init_history()
4956{
4957 int newlen; /* new length of history table */
4958 histentry_T *temp;
4959 int i;
4960 int j;
4961 int type;
4962
4963 /*
4964 * If size of history table changed, reallocate it
4965 */
4966 newlen = (int)p_hi;
4967 if (newlen != hislen) /* history length changed */
4968 {
4969 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4970 {
4971 if (newlen)
4972 {
4973 temp = (histentry_T *)lalloc(
4974 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4975 if (temp == NULL) /* out of memory! */
4976 {
4977 if (type == 0) /* first one: just keep the old length */
4978 {
4979 newlen = hislen;
4980 break;
4981 }
4982 /* Already changed one table, now we can only have zero
4983 * length for all tables. */
4984 newlen = 0;
4985 type = -1;
4986 continue;
4987 }
4988 }
4989 else
4990 temp = NULL;
4991 if (newlen == 0 || temp != NULL)
4992 {
4993 if (hisidx[type] < 0) /* there are no entries yet */
4994 {
4995 for (i = 0; i < newlen; ++i)
4996 {
4997 temp[i].hisnum = 0;
4998 temp[i].hisstr = NULL;
4999 }
5000 }
5001 else if (newlen > hislen) /* array becomes bigger */
5002 {
5003 for (i = 0; i <= hisidx[type]; ++i)
5004 temp[i] = history[type][i];
5005 j = i;
5006 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5007 {
5008 temp[i].hisnum = 0;
5009 temp[i].hisstr = NULL;
5010 }
5011 for ( ; j < hislen; ++i, ++j)
5012 temp[i] = history[type][j];
5013 }
5014 else /* array becomes smaller or 0 */
5015 {
5016 j = hisidx[type];
5017 for (i = newlen - 1; ; --i)
5018 {
5019 if (i >= 0) /* copy newest entries */
5020 temp[i] = history[type][j];
5021 else /* remove older entries */
5022 vim_free(history[type][j].hisstr);
5023 if (--j < 0)
5024 j = hislen - 1;
5025 if (j == hisidx[type])
5026 break;
5027 }
5028 hisidx[type] = newlen - 1;
5029 }
5030 vim_free(history[type]);
5031 history[type] = temp;
5032 }
5033 }
5034 hislen = newlen;
5035 }
5036}
5037
5038/*
5039 * Check if command line 'str' is already in history.
5040 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5041 */
5042 static int
5043in_history(type, str, move_to_front)
5044 int type;
5045 char_u *str;
5046 int move_to_front; /* Move the entry to the front if it exists */
5047{
5048 int i;
5049 int last_i = -1;
5050
5051 if (hisidx[type] < 0)
5052 return FALSE;
5053 i = hisidx[type];
5054 do
5055 {
5056 if (history[type][i].hisstr == NULL)
5057 return FALSE;
5058 if (STRCMP(str, history[type][i].hisstr) == 0)
5059 {
5060 if (!move_to_front)
5061 return TRUE;
5062 last_i = i;
5063 break;
5064 }
5065 if (--i < 0)
5066 i = hislen - 1;
5067 } while (i != hisidx[type]);
5068
5069 if (last_i >= 0)
5070 {
5071 str = history[type][i].hisstr;
5072 while (i != hisidx[type])
5073 {
5074 if (++i >= hislen)
5075 i = 0;
5076 history[type][last_i] = history[type][i];
5077 last_i = i;
5078 }
5079 history[type][i].hisstr = str;
5080 history[type][i].hisnum = ++hisnum[type];
5081 return TRUE;
5082 }
5083 return FALSE;
5084}
5085
5086/*
5087 * Convert history name (from table above) to its HIST_ equivalent.
5088 * When "name" is empty, return "cmd" history.
5089 * Returns -1 for unknown history name.
5090 */
5091 int
5092get_histtype(name)
5093 char_u *name;
5094{
5095 int i;
5096 int len = (int)STRLEN(name);
5097
5098 /* No argument: use current history. */
5099 if (len == 0)
5100 return hist_char2type(ccline.cmdfirstc);
5101
5102 for (i = 0; history_names[i] != NULL; ++i)
5103 if (STRNICMP(name, history_names[i], len) == 0)
5104 return i;
5105
5106 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5107 return hist_char2type(name[0]);
5108
5109 return -1;
5110}
5111
5112static int last_maptick = -1; /* last seen maptick */
5113
5114/*
5115 * Add the given string to the given history. If the string is already in the
5116 * history then it is moved to the front. "histype" may be one of he HIST_
5117 * values.
5118 */
5119 void
5120add_to_history(histype, new_entry, in_map, sep)
5121 int histype;
5122 char_u *new_entry;
5123 int in_map; /* consider maptick when inside a mapping */
5124 int sep; /* separator character used (search hist) */
5125{
5126 histentry_T *hisptr;
5127 int len;
5128
5129 if (hislen == 0) /* no history */
5130 return;
5131
5132 /*
5133 * Searches inside the same mapping overwrite each other, so that only
5134 * the last line is kept. Be careful not to remove a line that was moved
5135 * down, only lines that were added.
5136 */
5137 if (histype == HIST_SEARCH && in_map)
5138 {
5139 if (maptick == last_maptick)
5140 {
5141 /* Current line is from the same mapping, remove it */
5142 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5143 vim_free(hisptr->hisstr);
5144 hisptr->hisstr = NULL;
5145 hisptr->hisnum = 0;
5146 --hisnum[histype];
5147 if (--hisidx[HIST_SEARCH] < 0)
5148 hisidx[HIST_SEARCH] = hislen - 1;
5149 }
5150 last_maptick = -1;
5151 }
5152 if (!in_history(histype, new_entry, TRUE))
5153 {
5154 if (++hisidx[histype] == hislen)
5155 hisidx[histype] = 0;
5156 hisptr = &history[histype][hisidx[histype]];
5157 vim_free(hisptr->hisstr);
5158
5159 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005160 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5162 if (hisptr->hisstr != NULL)
5163 hisptr->hisstr[len + 1] = sep;
5164
5165 hisptr->hisnum = ++hisnum[histype];
5166 if (histype == HIST_SEARCH && in_map)
5167 last_maptick = maptick;
5168 }
5169}
5170
5171#if defined(FEAT_EVAL) || defined(PROTO)
5172
5173/*
5174 * Get identifier of newest history entry.
5175 * "histype" may be one of the HIST_ values.
5176 */
5177 int
5178get_history_idx(histype)
5179 int histype;
5180{
5181 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5182 || hisidx[histype] < 0)
5183 return -1;
5184
5185 return history[histype][hisidx[histype]].hisnum;
5186}
5187
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005188static struct cmdline_info *get_ccline_ptr __ARGS((void));
5189
5190/*
5191 * Get pointer to the command line info to use. cmdline_paste() may clear
5192 * ccline and put the previous value in prev_ccline.
5193 */
5194 static struct cmdline_info *
5195get_ccline_ptr()
5196{
5197 if ((State & CMDLINE) == 0)
5198 return NULL;
5199 if (ccline.cmdbuff != NULL)
5200 return &ccline;
5201 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5202 return &prev_ccline;
5203 return NULL;
5204}
5205
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206/*
5207 * Get the current command line in allocated memory.
5208 * Only works when the command line is being edited.
5209 * Returns NULL when something is wrong.
5210 */
5211 char_u *
5212get_cmdline_str()
5213{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005214 struct cmdline_info *p = get_ccline_ptr();
5215
5216 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005218 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219}
5220
5221/*
5222 * Get the current command line position, counted in bytes.
5223 * Zero is the first position.
5224 * Only works when the command line is being edited.
5225 * Returns -1 when something is wrong.
5226 */
5227 int
5228get_cmdline_pos()
5229{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005230 struct cmdline_info *p = get_ccline_ptr();
5231
5232 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005234 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235}
5236
5237/*
5238 * Set the command line byte position to "pos". Zero is the first position.
5239 * Only works when the command line is being edited.
5240 * Returns 1 when failed, 0 when OK.
5241 */
5242 int
5243set_cmdline_pos(pos)
5244 int pos;
5245{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005246 struct cmdline_info *p = get_ccline_ptr();
5247
5248 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 return 1;
5250
5251 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5252 * changed the command line. */
5253 if (pos < 0)
5254 new_cmdpos = 0;
5255 else
5256 new_cmdpos = pos;
5257 return 0;
5258}
5259
5260/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005261 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005262 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005263 * Only works when the command line is being edited.
5264 * Returns NUL when something is wrong.
5265 */
5266 int
5267get_cmdline_type()
5268{
5269 struct cmdline_info *p = get_ccline_ptr();
5270
5271 if (p == NULL)
5272 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005273 if (p->cmdfirstc == NUL)
5274 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005275 return p->cmdfirstc;
5276}
5277
5278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 * Calculate history index from a number:
5280 * num > 0: seen as identifying number of a history entry
5281 * num < 0: relative position in history wrt newest entry
5282 * "histype" may be one of the HIST_ values.
5283 */
5284 static int
5285calc_hist_idx(histype, num)
5286 int histype;
5287 int num;
5288{
5289 int i;
5290 histentry_T *hist;
5291 int wrapped = FALSE;
5292
5293 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5294 || (i = hisidx[histype]) < 0 || num == 0)
5295 return -1;
5296
5297 hist = history[histype];
5298 if (num > 0)
5299 {
5300 while (hist[i].hisnum > num)
5301 if (--i < 0)
5302 {
5303 if (wrapped)
5304 break;
5305 i += hislen;
5306 wrapped = TRUE;
5307 }
5308 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5309 return i;
5310 }
5311 else if (-num <= hislen)
5312 {
5313 i += num + 1;
5314 if (i < 0)
5315 i += hislen;
5316 if (hist[i].hisstr != NULL)
5317 return i;
5318 }
5319 return -1;
5320}
5321
5322/*
5323 * Get a history entry by its index.
5324 * "histype" may be one of the HIST_ values.
5325 */
5326 char_u *
5327get_history_entry(histype, idx)
5328 int histype;
5329 int idx;
5330{
5331 idx = calc_hist_idx(histype, idx);
5332 if (idx >= 0)
5333 return history[histype][idx].hisstr;
5334 else
5335 return (char_u *)"";
5336}
5337
5338/*
5339 * Clear all entries of a history.
5340 * "histype" may be one of the HIST_ values.
5341 */
5342 int
5343clr_history(histype)
5344 int histype;
5345{
5346 int i;
5347 histentry_T *hisptr;
5348
5349 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5350 {
5351 hisptr = history[histype];
5352 for (i = hislen; i--;)
5353 {
5354 vim_free(hisptr->hisstr);
5355 hisptr->hisnum = 0;
5356 hisptr++->hisstr = NULL;
5357 }
5358 hisidx[histype] = -1; /* mark history as cleared */
5359 hisnum[histype] = 0; /* reset identifier counter */
5360 return OK;
5361 }
5362 return FAIL;
5363}
5364
5365/*
5366 * Remove all entries matching {str} from a history.
5367 * "histype" may be one of the HIST_ values.
5368 */
5369 int
5370del_history_entry(histype, str)
5371 int histype;
5372 char_u *str;
5373{
5374 regmatch_T regmatch;
5375 histentry_T *hisptr;
5376 int idx;
5377 int i;
5378 int last;
5379 int found = FALSE;
5380
5381 regmatch.regprog = NULL;
5382 regmatch.rm_ic = FALSE; /* always match case */
5383 if (hislen != 0
5384 && histype >= 0
5385 && histype < HIST_COUNT
5386 && *str != NUL
5387 && (idx = hisidx[histype]) >= 0
5388 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5389 != NULL)
5390 {
5391 i = last = idx;
5392 do
5393 {
5394 hisptr = &history[histype][i];
5395 if (hisptr->hisstr == NULL)
5396 break;
5397 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5398 {
5399 found = TRUE;
5400 vim_free(hisptr->hisstr);
5401 hisptr->hisstr = NULL;
5402 hisptr->hisnum = 0;
5403 }
5404 else
5405 {
5406 if (i != last)
5407 {
5408 history[histype][last] = *hisptr;
5409 hisptr->hisstr = NULL;
5410 hisptr->hisnum = 0;
5411 }
5412 if (--last < 0)
5413 last += hislen;
5414 }
5415 if (--i < 0)
5416 i += hislen;
5417 } while (i != idx);
5418 if (history[histype][idx].hisstr == NULL)
5419 hisidx[histype] = -1;
5420 }
5421 vim_free(regmatch.regprog);
5422 return found;
5423}
5424
5425/*
5426 * Remove an indexed entry from a history.
5427 * "histype" may be one of the HIST_ values.
5428 */
5429 int
5430del_history_idx(histype, idx)
5431 int histype;
5432 int idx;
5433{
5434 int i, j;
5435
5436 i = calc_hist_idx(histype, idx);
5437 if (i < 0)
5438 return FALSE;
5439 idx = hisidx[histype];
5440 vim_free(history[histype][i].hisstr);
5441
5442 /* When deleting the last added search string in a mapping, reset
5443 * last_maptick, so that the last added search string isn't deleted again.
5444 */
5445 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5446 last_maptick = -1;
5447
5448 while (i != idx)
5449 {
5450 j = (i + 1) % hislen;
5451 history[histype][i] = history[histype][j];
5452 i = j;
5453 }
5454 history[histype][i].hisstr = NULL;
5455 history[histype][i].hisnum = 0;
5456 if (--i < 0)
5457 i += hislen;
5458 hisidx[histype] = i;
5459 return TRUE;
5460}
5461
5462#endif /* FEAT_EVAL */
5463
5464#if defined(FEAT_CRYPT) || defined(PROTO)
5465/*
5466 * Very specific function to remove the value in ":set key=val" from the
5467 * history.
5468 */
5469 void
5470remove_key_from_history()
5471{
5472 char_u *p;
5473 int i;
5474
5475 i = hisidx[HIST_CMD];
5476 if (i < 0)
5477 return;
5478 p = history[HIST_CMD][i].hisstr;
5479 if (p != NULL)
5480 for ( ; *p; ++p)
5481 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5482 {
5483 p = vim_strchr(p + 3, '=');
5484 if (p == NULL)
5485 break;
5486 ++p;
5487 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5488 if (p[i] == '\\' && p[i + 1])
5489 ++i;
5490 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5491 --p;
5492 }
5493}
5494#endif
5495
5496#endif /* FEAT_CMDHIST */
5497
5498#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5499/*
5500 * Get indices "num1,num2" that specify a range within a list (not a range of
5501 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5502 * Returns OK if parsed successfully, otherwise FAIL.
5503 */
5504 int
5505get_list_range(str, num1, num2)
5506 char_u **str;
5507 int *num1;
5508 int *num2;
5509{
5510 int len;
5511 int first = FALSE;
5512 long num;
5513
5514 *str = skipwhite(*str);
5515 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5516 {
5517 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5518 *str += len;
5519 *num1 = (int)num;
5520 first = TRUE;
5521 }
5522 *str = skipwhite(*str);
5523 if (**str == ',') /* parse "to" part of range */
5524 {
5525 *str = skipwhite(*str + 1);
5526 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5527 if (len > 0)
5528 {
5529 *num2 = (int)num;
5530 *str = skipwhite(*str + len);
5531 }
5532 else if (!first) /* no number given at all */
5533 return FAIL;
5534 }
5535 else if (first) /* only one number given */
5536 *num2 = *num1;
5537 return OK;
5538}
5539#endif
5540
5541#if defined(FEAT_CMDHIST) || defined(PROTO)
5542/*
5543 * :history command - print a history
5544 */
5545 void
5546ex_history(eap)
5547 exarg_T *eap;
5548{
5549 histentry_T *hist;
5550 int histype1 = HIST_CMD;
5551 int histype2 = HIST_CMD;
5552 int hisidx1 = 1;
5553 int hisidx2 = -1;
5554 int idx;
5555 int i, j, k;
5556 char_u *end;
5557 char_u *arg = eap->arg;
5558
5559 if (hislen == 0)
5560 {
5561 MSG(_("'history' option is zero"));
5562 return;
5563 }
5564
5565 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5566 {
5567 end = arg;
5568 while (ASCII_ISALPHA(*end)
5569 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5570 end++;
5571 i = *end;
5572 *end = NUL;
5573 histype1 = get_histtype(arg);
5574 if (histype1 == -1)
5575 {
5576 if (STRICMP(arg, "all") == 0)
5577 {
5578 histype1 = 0;
5579 histype2 = HIST_COUNT-1;
5580 }
5581 else
5582 {
5583 *end = i;
5584 EMSG(_(e_trailing));
5585 return;
5586 }
5587 }
5588 else
5589 histype2 = histype1;
5590 *end = i;
5591 }
5592 else
5593 end = arg;
5594 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5595 {
5596 EMSG(_(e_trailing));
5597 return;
5598 }
5599
5600 for (; !got_int && histype1 <= histype2; ++histype1)
5601 {
5602 STRCPY(IObuff, "\n # ");
5603 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5604 MSG_PUTS_TITLE(IObuff);
5605 idx = hisidx[histype1];
5606 hist = history[histype1];
5607 j = hisidx1;
5608 k = hisidx2;
5609 if (j < 0)
5610 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5611 if (k < 0)
5612 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5613 if (idx >= 0 && j <= k)
5614 for (i = idx + 1; !got_int; ++i)
5615 {
5616 if (i == hislen)
5617 i = 0;
5618 if (hist[i].hisstr != NULL
5619 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5620 {
5621 msg_putchar('\n');
5622 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5623 hist[i].hisnum);
5624 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5625 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5626 (int)Columns - 10);
5627 else
5628 STRCAT(IObuff, hist[i].hisstr);
5629 msg_outtrans(IObuff);
5630 out_flush();
5631 }
5632 if (i == idx)
5633 break;
5634 }
5635 }
5636}
5637#endif
5638
5639#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5640static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5641static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5642static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5643static int viminfo_add_at_front = FALSE;
5644
5645static int hist_type2char __ARGS((int type, int use_question));
5646
5647/*
5648 * Translate a history type number to the associated character.
5649 */
5650 static int
5651hist_type2char(type, use_question)
5652 int type;
5653 int use_question; /* use '?' instead of '/' */
5654{
5655 if (type == HIST_CMD)
5656 return ':';
5657 if (type == HIST_SEARCH)
5658 {
5659 if (use_question)
5660 return '?';
5661 else
5662 return '/';
5663 }
5664 if (type == HIST_EXPR)
5665 return '=';
5666 return '@';
5667}
5668
5669/*
5670 * Prepare for reading the history from the viminfo file.
5671 * This allocates history arrays to store the read history lines.
5672 */
5673 void
5674prepare_viminfo_history(asklen)
5675 int asklen;
5676{
5677 int i;
5678 int num;
5679 int type;
5680 int len;
5681
5682 init_history();
5683 viminfo_add_at_front = (asklen != 0);
5684 if (asklen > hislen)
5685 asklen = hislen;
5686
5687 for (type = 0; type < HIST_COUNT; ++type)
5688 {
5689 /*
5690 * Count the number of empty spaces in the history list. If there are
5691 * more spaces available than we request, then fill them up.
5692 */
5693 for (i = 0, num = 0; i < hislen; i++)
5694 if (history[type][i].hisstr == NULL)
5695 num++;
5696 len = asklen;
5697 if (num > len)
5698 len = num;
5699 if (len <= 0)
5700 viminfo_history[type] = NULL;
5701 else
5702 viminfo_history[type] =
5703 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5704 if (viminfo_history[type] == NULL)
5705 len = 0;
5706 viminfo_hislen[type] = len;
5707 viminfo_hisidx[type] = 0;
5708 }
5709}
5710
5711/*
5712 * Accept a line from the viminfo, store it in the history array when it's
5713 * new.
5714 */
5715 int
5716read_viminfo_history(virp)
5717 vir_T *virp;
5718{
5719 int type;
5720 long_u len;
5721 char_u *val;
5722 char_u *p;
5723
5724 type = hist_char2type(virp->vir_line[0]);
5725 if (viminfo_hisidx[type] < viminfo_hislen[type])
5726 {
5727 val = viminfo_readstring(virp, 1, TRUE);
5728 if (val != NULL && *val != NUL)
5729 {
5730 if (!in_history(type, val + (type == HIST_SEARCH),
5731 viminfo_add_at_front))
5732 {
5733 /* Need to re-allocate to append the separator byte. */
5734 len = STRLEN(val);
5735 p = lalloc(len + 2, TRUE);
5736 if (p != NULL)
5737 {
5738 if (type == HIST_SEARCH)
5739 {
5740 /* Search entry: Move the separator from the first
5741 * column to after the NUL. */
5742 mch_memmove(p, val + 1, (size_t)len);
5743 p[len] = (*val == ' ' ? NUL : *val);
5744 }
5745 else
5746 {
5747 /* Not a search entry: No separator in the viminfo
5748 * file, add a NUL separator. */
5749 mch_memmove(p, val, (size_t)len + 1);
5750 p[len + 1] = NUL;
5751 }
5752 viminfo_history[type][viminfo_hisidx[type]++] = p;
5753 }
5754 }
5755 }
5756 vim_free(val);
5757 }
5758 return viminfo_readline(virp);
5759}
5760
5761 void
5762finish_viminfo_history()
5763{
5764 int idx;
5765 int i;
5766 int type;
5767
5768 for (type = 0; type < HIST_COUNT; ++type)
5769 {
5770 if (history[type] == NULL)
5771 return;
5772 idx = hisidx[type] + viminfo_hisidx[type];
5773 if (idx >= hislen)
5774 idx -= hislen;
5775 else if (idx < 0)
5776 idx = hislen - 1;
5777 if (viminfo_add_at_front)
5778 hisidx[type] = idx;
5779 else
5780 {
5781 if (hisidx[type] == -1)
5782 hisidx[type] = hislen - 1;
5783 do
5784 {
5785 if (history[type][idx].hisstr != NULL)
5786 break;
5787 if (++idx == hislen)
5788 idx = 0;
5789 } while (idx != hisidx[type]);
5790 if (idx != hisidx[type] && --idx < 0)
5791 idx = hislen - 1;
5792 }
5793 for (i = 0; i < viminfo_hisidx[type]; i++)
5794 {
5795 vim_free(history[type][idx].hisstr);
5796 history[type][idx].hisstr = viminfo_history[type][i];
5797 if (--idx < 0)
5798 idx = hislen - 1;
5799 }
5800 idx += 1;
5801 idx %= hislen;
5802 for (i = 0; i < viminfo_hisidx[type]; i++)
5803 {
5804 history[type][idx++].hisnum = ++hisnum[type];
5805 idx %= hislen;
5806 }
5807 vim_free(viminfo_history[type]);
5808 viminfo_history[type] = NULL;
5809 }
5810}
5811
5812 void
5813write_viminfo_history(fp)
5814 FILE *fp;
5815{
5816 int i;
5817 int type;
5818 int num_saved;
5819 char_u *p;
5820 int c;
5821
5822 init_history();
5823 if (hislen == 0)
5824 return;
5825 for (type = 0; type < HIST_COUNT; ++type)
5826 {
5827 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5828 if (num_saved == 0)
5829 continue;
5830 if (num_saved < 0) /* Use default */
5831 num_saved = hislen;
5832 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5833 type == HIST_CMD ? _("Command Line") :
5834 type == HIST_SEARCH ? _("Search String") :
5835 type == HIST_EXPR ? _("Expression") :
5836 _("Input Line"));
5837 if (num_saved > hislen)
5838 num_saved = hislen;
5839 i = hisidx[type];
5840 if (i >= 0)
5841 while (num_saved--)
5842 {
5843 p = history[type][i].hisstr;
5844 if (p != NULL)
5845 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005846 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 /* For the search history: put the separator in the second
5848 * column; use a space if there isn't one. */
5849 if (type == HIST_SEARCH)
5850 {
5851 c = p[STRLEN(p) + 1];
5852 putc(c == NUL ? ' ' : c, fp);
5853 }
5854 viminfo_writestring(fp, p);
5855 }
5856 if (--i < 0)
5857 i = hislen - 1;
5858 }
5859 }
5860}
5861#endif /* FEAT_VIMINFO */
5862
5863#if defined(FEAT_FKMAP) || defined(PROTO)
5864/*
5865 * Write a character at the current cursor+offset position.
5866 * It is directly written into the command buffer block.
5867 */
5868 void
5869cmd_pchar(c, offset)
5870 int c, offset;
5871{
5872 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5873 {
5874 EMSG(_("E198: cmd_pchar beyond the command length"));
5875 return;
5876 }
5877 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5878 ccline.cmdbuff[ccline.cmdlen] = NUL;
5879}
5880
5881 int
5882cmd_gchar(offset)
5883 int offset;
5884{
5885 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5886 {
5887 /* EMSG(_("cmd_gchar beyond the command length")); */
5888 return NUL;
5889 }
5890 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5891}
5892#endif
5893
5894#if defined(FEAT_CMDWIN) || defined(PROTO)
5895/*
5896 * Open a window on the current command line and history. Allow editing in
5897 * the window. Returns when the window is closed.
5898 * Returns:
5899 * CR if the command is to be executed
5900 * Ctrl_C if it is to be abandoned
5901 * K_IGNORE if editing continues
5902 */
5903 static int
5904ex_window()
5905{
5906 struct cmdline_info save_ccline;
5907 buf_T *old_curbuf = curbuf;
5908 win_T *old_curwin = curwin;
5909 buf_T *bp;
5910 win_T *wp;
5911 int i;
5912 linenr_T lnum;
5913 int histtype;
5914 garray_T winsizes;
5915 char_u typestr[2];
5916 int save_restart_edit = restart_edit;
5917 int save_State = State;
5918 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005919#ifdef FEAT_RIGHTLEFT
5920 int save_cmdmsg_rl = cmdmsg_rl;
5921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922
5923 /* Can't do this recursively. Can't do it when typing a password. */
5924 if (cmdwin_type != 0
5925# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5926 || cmdline_star > 0
5927# endif
5928 )
5929 {
5930 beep_flush();
5931 return K_IGNORE;
5932 }
5933
5934 /* Save current window sizes. */
5935 win_size_save(&winsizes);
5936
5937# ifdef FEAT_AUTOCMD
5938 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005939 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005941 /* don't use a new tab page */
5942 cmdmod.tab = 0;
5943
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 /* Create a window for the command-line buffer. */
5945 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5946 {
5947 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005948# ifdef FEAT_AUTOCMD
5949 unblock_autocmds();
5950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 return K_IGNORE;
5952 }
5953 cmdwin_type = ccline.cmdfirstc;
5954 if (cmdwin_type == NUL)
5955 cmdwin_type = '-';
5956
5957 /* Create the command-line buffer empty. */
5958 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5959 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5960 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5961 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5962 curbuf->b_p_ma = TRUE;
5963# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005964 curwin->w_p_rl = cmdmsg_rl;
5965 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966# endif
5967# ifdef FEAT_SCROLLBIND
5968 curwin->w_p_scb = FALSE;
5969# endif
5970
5971# ifdef FEAT_AUTOCMD
5972 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005973 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974# endif
5975
Bram Moolenaar46152342005-09-07 21:18:43 +00005976 /* Showing the prompt may have set need_wait_return, reset it. */
5977 need_wait_return = FALSE;
5978
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 histtype = hist_char2type(ccline.cmdfirstc);
5980 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5981 {
5982 if (p_wc == TAB)
5983 {
5984 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5985 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5986 }
5987 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5988 }
5989
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005990 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5991 * sets 'textwidth' to 78). */
5992 curbuf->b_p_tw = 0;
5993
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 /* Fill the buffer with the history. */
5995 init_history();
5996 if (hislen > 0)
5997 {
5998 i = hisidx[histtype];
5999 if (i >= 0)
6000 {
6001 lnum = 0;
6002 do
6003 {
6004 if (++i == hislen)
6005 i = 0;
6006 if (history[histtype][i].hisstr != NULL)
6007 ml_append(lnum++, history[histtype][i].hisstr,
6008 (colnr_T)0, FALSE);
6009 }
6010 while (i != hisidx[histtype]);
6011 }
6012 }
6013
6014 /* Replace the empty last line with the current command-line and put the
6015 * cursor there. */
6016 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6017 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6018 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006019 changed_line_abv_curs();
6020 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006021 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022
6023 /* Save the command line info, can be used recursively. */
6024 save_ccline = ccline;
6025 ccline.cmdbuff = NULL;
6026 ccline.cmdprompt = NULL;
6027
6028 /* No Ex mode here! */
6029 exmode_active = 0;
6030
6031 State = NORMAL;
6032# ifdef FEAT_MOUSE
6033 setmouse();
6034# endif
6035
6036# ifdef FEAT_AUTOCMD
6037 /* Trigger CmdwinEnter autocommands. */
6038 typestr[0] = cmdwin_type;
6039 typestr[1] = NUL;
6040 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006041 if (restart_edit != 0) /* autocmd with ":startinsert" */
6042 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043# endif
6044
6045 i = RedrawingDisabled;
6046 RedrawingDisabled = 0;
6047
6048 /*
6049 * Call the main loop until <CR> or CTRL-C is typed.
6050 */
6051 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006052 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053
6054 RedrawingDisabled = i;
6055
6056# ifdef FEAT_AUTOCMD
6057 /* Trigger CmdwinLeave autocommands. */
6058 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6059# endif
6060
Bram Moolenaarccc18222007-05-10 18:25:20 +00006061 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 ccline = save_ccline;
6063 cmdwin_type = 0;
6064
6065 exmode_active = save_exmode;
6066
6067 /* Safety check: The old window or buffer was deleted: It's a a bug when
6068 * this happens! */
6069 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6070 {
6071 cmdwin_result = Ctrl_C;
6072 EMSG(_("E199: Active window or buffer deleted"));
6073 }
6074 else
6075 {
6076# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6077 /* autocmds may abort script processing */
6078 if (aborting() && cmdwin_result != K_IGNORE)
6079 cmdwin_result = Ctrl_C;
6080# endif
6081 /* Set the new command line from the cmdline buffer. */
6082 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006083 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006085 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6086
6087 if (histtype == HIST_CMD)
6088 {
6089 /* Execute the command directly. */
6090 ccline.cmdbuff = vim_strsave((char_u *)p);
6091 cmdwin_result = CAR;
6092 }
6093 else
6094 {
6095 /* First need to cancel what we were doing. */
6096 ccline.cmdbuff = NULL;
6097 stuffcharReadbuff(':');
6098 stuffReadbuff((char_u *)p);
6099 stuffcharReadbuff(CAR);
6100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 }
6102 else if (cmdwin_result == K_XF2) /* :qa typed */
6103 {
6104 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6105 cmdwin_result = CAR;
6106 }
6107 else
6108 ccline.cmdbuff = vim_strsave(ml_get_curline());
6109 if (ccline.cmdbuff == NULL)
6110 cmdwin_result = Ctrl_C;
6111 else
6112 {
6113 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6114 ccline.cmdbufflen = ccline.cmdlen + 1;
6115 ccline.cmdpos = curwin->w_cursor.col;
6116 if (ccline.cmdpos > ccline.cmdlen)
6117 ccline.cmdpos = ccline.cmdlen;
6118 if (cmdwin_result == K_IGNORE)
6119 {
6120 set_cmdspos_cursor();
6121 redrawcmd();
6122 }
6123 }
6124
6125# ifdef FEAT_AUTOCMD
6126 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006127 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128# endif
6129 wp = curwin;
6130 bp = curbuf;
6131 win_goto(old_curwin);
6132 win_close(wp, TRUE);
6133 close_buffer(NULL, bp, DOBUF_WIPE);
6134
6135 /* Restore window sizes. */
6136 win_size_restore(&winsizes);
6137
6138# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006139 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140# endif
6141 }
6142
6143 ga_clear(&winsizes);
6144 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006145# ifdef FEAT_RIGHTLEFT
6146 cmdmsg_rl = save_cmdmsg_rl;
6147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148
6149 State = save_State;
6150# ifdef FEAT_MOUSE
6151 setmouse();
6152# endif
6153
6154 return cmdwin_result;
6155}
6156#endif /* FEAT_CMDWIN */
6157
6158/*
6159 * Used for commands that either take a simple command string argument, or:
6160 * cmd << endmarker
6161 * {script}
6162 * endmarker
6163 * Returns a pointer to allocated memory with {script} or NULL.
6164 */
6165 char_u *
6166script_get(eap, cmd)
6167 exarg_T *eap;
6168 char_u *cmd;
6169{
6170 char_u *theline;
6171 char *end_pattern = NULL;
6172 char dot[] = ".";
6173 garray_T ga;
6174
6175 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6176 return NULL;
6177
6178 ga_init2(&ga, 1, 0x400);
6179
6180 if (cmd[2] != NUL)
6181 end_pattern = (char *)skipwhite(cmd + 2);
6182 else
6183 end_pattern = dot;
6184
6185 for (;;)
6186 {
6187 theline = eap->getline(
6188#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006189 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190#endif
6191 NUL, eap->cookie, 0);
6192
6193 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
6194 break;
6195
6196 ga_concat(&ga, theline);
6197 ga_append(&ga, '\n');
6198 vim_free(theline);
6199 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006200 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201
6202 return (char_u *)ga.ga_data;
6203}