blob: 68966def057630ce5a1ab2f0e3a31e6f1d3679e2 [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:
1189 do
1190 {
1191 if (ccline.cmdpos == 0)
1192 break;
1193 --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 Moolenaarbc7aa852005-03-06 23:38:09 +00001201 while ((c == K_S_LEFT || c == K_C_LEFT
1202 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1204#ifdef FEAT_MBYTE
1205 if (has_mbyte)
1206 set_cmdspos_cursor();
1207#endif
1208 goto cmdline_not_changed;
1209
1210 case K_IGNORE:
1211 goto cmdline_not_changed; /* Ignore mouse */
1212
Bram Moolenaar4770d092006-01-12 23:22:24 +00001213#ifdef FEAT_GUI_W32
1214 /* On Win32 ignore <M-F4>, we get it when closing the window was
1215 * cancelled. */
1216 case K_F4:
1217 if (mod_mask == MOD_MASK_ALT)
1218 {
1219 redrawcmd(); /* somehow the cmdline is cleared */
1220 goto cmdline_not_changed;
1221 }
1222 break;
1223#endif
1224
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225#ifdef FEAT_MOUSE
1226 case K_MIDDLEDRAG:
1227 case K_MIDDLERELEASE:
1228 goto cmdline_not_changed; /* Ignore mouse */
1229
1230 case K_MIDDLEMOUSE:
1231# ifdef FEAT_GUI
1232 /* When GUI is active, also paste when 'mouse' is empty */
1233 if (!gui.in_use)
1234# endif
1235 if (!mouse_has(MOUSE_COMMAND))
1236 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001237# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001239 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001241# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001242 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 redrawcmd();
1244 goto cmdline_changed;
1245
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001246# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001248 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 redrawcmd();
1250 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001251# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
1253 case K_LEFTDRAG:
1254 case K_LEFTRELEASE:
1255 case K_RIGHTDRAG:
1256 case K_RIGHTRELEASE:
1257 /* Ignore drag and release events when the button-down wasn't
1258 * seen before. */
1259 if (ignore_drag_release)
1260 goto cmdline_not_changed;
1261 /* FALLTHROUGH */
1262 case K_LEFTMOUSE:
1263 case K_RIGHTMOUSE:
1264 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1265 ignore_drag_release = TRUE;
1266 else
1267 ignore_drag_release = FALSE;
1268# ifdef FEAT_GUI
1269 /* When GUI is active, also move when 'mouse' is empty */
1270 if (!gui.in_use)
1271# endif
1272 if (!mouse_has(MOUSE_COMMAND))
1273 goto cmdline_not_changed; /* Ignore mouse */
1274# ifdef FEAT_CLIPBOARD
1275 if (mouse_row < cmdline_row && clip_star.available)
1276 {
1277 int button, is_click, is_drag;
1278
1279 /*
1280 * Handle modeless selection.
1281 */
1282 button = get_mouse_button(KEY2TERMCAP1(c),
1283 &is_click, &is_drag);
1284 if (mouse_model_popup() && button == MOUSE_LEFT
1285 && (mod_mask & MOD_MASK_SHIFT))
1286 {
1287 /* Translate shift-left to right button. */
1288 button = MOUSE_RIGHT;
1289 mod_mask &= ~MOD_MASK_SHIFT;
1290 }
1291 clip_modeless(button, is_click, is_drag);
1292 goto cmdline_not_changed;
1293 }
1294# endif
1295
1296 set_cmdspos();
1297 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1298 ++ccline.cmdpos)
1299 {
1300 i = cmdline_charsize(ccline.cmdpos);
1301 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1302 && mouse_col < ccline.cmdspos % Columns + i)
1303 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001304# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if (has_mbyte)
1306 {
1307 /* Count ">" for double-wide char that doesn't fit. */
1308 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001309 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 + ccline.cmdpos) - 1;
1311 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 ccline.cmdspos += i;
1314 }
1315 goto cmdline_not_changed;
1316
1317 /* Mouse scroll wheel: ignored here */
1318 case K_MOUSEDOWN:
1319 case K_MOUSEUP:
1320 /* Alternate buttons ignored here */
1321 case K_X1MOUSE:
1322 case K_X1DRAG:
1323 case K_X1RELEASE:
1324 case K_X2MOUSE:
1325 case K_X2DRAG:
1326 case K_X2RELEASE:
1327 goto cmdline_not_changed;
1328
1329#endif /* FEAT_MOUSE */
1330
1331#ifdef FEAT_GUI
1332 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1333 case K_LEFTRELEASE_NM:
1334 goto cmdline_not_changed;
1335
1336 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001337 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 {
1339 gui_do_scroll();
1340 redrawcmd();
1341 }
1342 goto cmdline_not_changed;
1343
1344 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001345 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 {
1347 gui_do_horiz_scroll();
1348 redrawcmd();
1349 }
1350 goto cmdline_not_changed;
1351#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001352#ifdef FEAT_GUI_TABLINE
1353 case K_TABLINE:
1354 case K_TABMENU:
1355 /* Don't want to change any tabs here. Make sure the same tab
1356 * is still selected. */
1357 if (gui_use_tabline())
1358 gui_mch_set_curtab(tabpage_index(curtab));
1359 goto cmdline_not_changed;
1360#endif
1361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 case K_SELECT: /* end of Select mode mapping - ignore */
1363 goto cmdline_not_changed;
1364
1365 case Ctrl_B: /* begin of command line */
1366 case K_HOME:
1367 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 case K_S_HOME:
1369 case K_C_HOME:
1370 ccline.cmdpos = 0;
1371 set_cmdspos();
1372 goto cmdline_not_changed;
1373
1374 case Ctrl_E: /* end of command line */
1375 case K_END:
1376 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 case K_S_END:
1378 case K_C_END:
1379 ccline.cmdpos = ccline.cmdlen;
1380 set_cmdspos_cursor();
1381 goto cmdline_not_changed;
1382
1383 case Ctrl_A: /* all matches */
1384 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1385 break;
1386 goto cmdline_changed;
1387
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001388 case Ctrl_L:
1389#ifdef FEAT_SEARCH_EXTRA
1390 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1391 {
1392 /* Add a character from under the cursor for 'incsearch' */
1393 if (did_incsearch
1394 && !equalpos(curwin->w_cursor, old_cursor))
1395 {
1396 c = gchar_cursor();
1397 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001398 {
1399 if (c == firstc || vim_strchr((char_u *)(
1400 p_magic ? "\\^$.*[" : "\\^$"), c)
1401 != NULL)
1402 {
1403 /* put a backslash before special characters */
1404 stuffcharReadbuff(c);
1405 c = '\\';
1406 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001407 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001408 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001409 }
1410 goto cmdline_not_changed;
1411 }
1412#endif
1413
1414 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1416 break;
1417 goto cmdline_changed;
1418
1419 case Ctrl_N: /* next match */
1420 case Ctrl_P: /* previous match */
1421 if (xpc.xp_numfiles > 0)
1422 {
1423 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1424 == FAIL)
1425 break;
1426 goto cmdline_changed;
1427 }
1428
1429#ifdef FEAT_CMDHIST
1430 case K_UP:
1431 case K_DOWN:
1432 case K_S_UP:
1433 case K_S_DOWN:
1434 case K_PAGEUP:
1435 case K_KPAGEUP:
1436 case K_PAGEDOWN:
1437 case K_KPAGEDOWN:
1438 if (hislen == 0 || firstc == NUL) /* no history */
1439 goto cmdline_not_changed;
1440
1441 i = hiscnt;
1442
1443 /* save current command string so it can be restored later */
1444 if (lookfor == NULL)
1445 {
1446 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1447 goto cmdline_not_changed;
1448 lookfor[ccline.cmdpos] = NUL;
1449 }
1450
1451 j = (int)STRLEN(lookfor);
1452 for (;;)
1453 {
1454 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001455 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001456 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 {
1458 if (hiscnt == hislen) /* first time */
1459 hiscnt = hisidx[histype];
1460 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1461 hiscnt = hislen - 1;
1462 else if (hiscnt != hisidx[histype] + 1)
1463 --hiscnt;
1464 else /* at top of list */
1465 {
1466 hiscnt = i;
1467 break;
1468 }
1469 }
1470 else /* one step forwards */
1471 {
1472 /* on last entry, clear the line */
1473 if (hiscnt == hisidx[histype])
1474 {
1475 hiscnt = hislen;
1476 break;
1477 }
1478
1479 /* not on a history line, nothing to do */
1480 if (hiscnt == hislen)
1481 break;
1482 if (hiscnt == hislen - 1) /* wrap around */
1483 hiscnt = 0;
1484 else
1485 ++hiscnt;
1486 }
1487 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1488 {
1489 hiscnt = i;
1490 break;
1491 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001492 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001493 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 || STRNCMP(history[histype][hiscnt].hisstr,
1495 lookfor, (size_t)j) == 0)
1496 break;
1497 }
1498
1499 if (hiscnt != i) /* jumped to other entry */
1500 {
1501 char_u *p;
1502 int len;
1503 int old_firstc;
1504
1505 vim_free(ccline.cmdbuff);
1506 if (hiscnt == hislen)
1507 p = lookfor; /* back to the old one */
1508 else
1509 p = history[histype][hiscnt].hisstr;
1510
1511 if (histype == HIST_SEARCH
1512 && p != lookfor
1513 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1514 {
1515 /* Correct for the separator character used when
1516 * adding the history entry vs the one used now.
1517 * First loop: count length.
1518 * Second loop: copy the characters. */
1519 for (i = 0; i <= 1; ++i)
1520 {
1521 len = 0;
1522 for (j = 0; p[j] != NUL; ++j)
1523 {
1524 /* Replace old sep with new sep, unless it is
1525 * escaped. */
1526 if (p[j] == old_firstc
1527 && (j == 0 || p[j - 1] != '\\'))
1528 {
1529 if (i > 0)
1530 ccline.cmdbuff[len] = firstc;
1531 }
1532 else
1533 {
1534 /* Escape new sep, unless it is already
1535 * escaped. */
1536 if (p[j] == firstc
1537 && (j == 0 || p[j - 1] != '\\'))
1538 {
1539 if (i > 0)
1540 ccline.cmdbuff[len] = '\\';
1541 ++len;
1542 }
1543 if (i > 0)
1544 ccline.cmdbuff[len] = p[j];
1545 }
1546 ++len;
1547 }
1548 if (i == 0)
1549 {
1550 alloc_cmdbuff(len);
1551 if (ccline.cmdbuff == NULL)
1552 goto returncmd;
1553 }
1554 }
1555 ccline.cmdbuff[len] = NUL;
1556 }
1557 else
1558 {
1559 alloc_cmdbuff((int)STRLEN(p));
1560 if (ccline.cmdbuff == NULL)
1561 goto returncmd;
1562 STRCPY(ccline.cmdbuff, p);
1563 }
1564
1565 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1566 redrawcmd();
1567 goto cmdline_changed;
1568 }
1569 beep_flush();
1570 goto cmdline_not_changed;
1571#endif
1572
1573 case Ctrl_V:
1574 case Ctrl_Q:
1575#ifdef FEAT_MOUSE
1576 ignore_drag_release = TRUE;
1577#endif
1578 putcmdline('^', TRUE);
1579 c = get_literal(); /* get next (two) character(s) */
1580 do_abbr = FALSE; /* don't do abbreviation now */
1581#ifdef FEAT_MBYTE
1582 /* may need to remove ^ when composing char was typed */
1583 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1584 {
1585 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1586 msg_putchar(' ');
1587 cursorcmd();
1588 }
1589#endif
1590 break;
1591
1592#ifdef FEAT_DIGRAPHS
1593 case Ctrl_K:
1594#ifdef FEAT_MOUSE
1595 ignore_drag_release = TRUE;
1596#endif
1597 putcmdline('?', TRUE);
1598#ifdef USE_ON_FLY_SCROLL
1599 dont_scroll = TRUE; /* disallow scrolling here */
1600#endif
1601 c = get_digraph(TRUE);
1602 if (c != NUL)
1603 break;
1604
1605 redrawcmd();
1606 goto cmdline_not_changed;
1607#endif /* FEAT_DIGRAPHS */
1608
1609#ifdef FEAT_RIGHTLEFT
1610 case Ctrl__: /* CTRL-_: switch language mode */
1611 if (!p_ari)
1612 break;
1613#ifdef FEAT_FKMAP
1614 if (p_altkeymap)
1615 {
1616 cmd_fkmap = !cmd_fkmap;
1617 if (cmd_fkmap) /* in Farsi always in Insert mode */
1618 ccline.overstrike = FALSE;
1619 }
1620 else /* Hebrew is default */
1621#endif
1622 cmd_hkmap = !cmd_hkmap;
1623 goto cmdline_not_changed;
1624#endif
1625
1626 default:
1627#ifdef UNIX
1628 if (c == intr_char)
1629 {
1630 gotesc = TRUE; /* will free ccline.cmdbuff after
1631 putting it in history */
1632 goto returncmd; /* back to Normal mode */
1633 }
1634#endif
1635 /*
1636 * Normal character with no special meaning. Just set mod_mask
1637 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1638 * the string <S-Space>. This should only happen after ^V.
1639 */
1640 if (!IS_SPECIAL(c))
1641 mod_mask = 0x0;
1642 break;
1643 }
1644 /*
1645 * End of switch on command line character.
1646 * We come here if we have a normal character.
1647 */
1648
1649 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1650#ifdef FEAT_MBYTE
1651 /* Add ABBR_OFF for characters above 0x100, this is
1652 * what check_abbr() expects. */
1653 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1654#endif
1655 c))
1656 goto cmdline_changed;
1657
1658 /*
1659 * put the character in the command line
1660 */
1661 if (IS_SPECIAL(c) || mod_mask != 0)
1662 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1663 else
1664 {
1665#ifdef FEAT_MBYTE
1666 if (has_mbyte)
1667 {
1668 j = (*mb_char2bytes)(c, IObuff);
1669 IObuff[j] = NUL; /* exclude composing chars */
1670 put_on_cmdline(IObuff, j, TRUE);
1671 }
1672 else
1673#endif
1674 {
1675 IObuff[0] = c;
1676 put_on_cmdline(IObuff, 1, TRUE);
1677 }
1678 }
1679 goto cmdline_changed;
1680
1681/*
1682 * This part implements incremental searches for "/" and "?"
1683 * Jump to cmdline_not_changed when a character has been read but the command
1684 * line did not change. Then we only search and redraw if something changed in
1685 * the past.
1686 * Jump to cmdline_changed when the command line did change.
1687 * (Sorry for the goto's, I know it is ugly).
1688 */
1689cmdline_not_changed:
1690#ifdef FEAT_SEARCH_EXTRA
1691 if (!incsearch_postponed)
1692 continue;
1693#endif
1694
1695cmdline_changed:
1696#ifdef FEAT_SEARCH_EXTRA
1697 /*
1698 * 'incsearch' highlighting.
1699 */
1700 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1701 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001702 pos_T end_pos;
1703
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 /* if there is a character waiting, search and redraw later */
1705 if (char_avail())
1706 {
1707 incsearch_postponed = TRUE;
1708 continue;
1709 }
1710 incsearch_postponed = FALSE;
1711 curwin->w_cursor = old_cursor; /* start at old position */
1712
1713 /* If there is no command line, don't do anything */
1714 if (ccline.cmdlen == 0)
1715 i = 0;
1716 else
1717 {
1718 cursor_off(); /* so the user knows we're busy */
1719 out_flush();
1720 ++emsg_off; /* So it doesn't beep if bad expr */
1721 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1722 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
1723 --emsg_off;
1724 /* if interrupted while searching, behave like it failed */
1725 if (got_int)
1726 {
1727 (void)vpeekc(); /* remove <C-C> from input stream */
1728 got_int = FALSE; /* don't abandon the command line */
1729 i = 0;
1730 }
1731 else if (char_avail())
1732 /* cancelled searching because a char was typed */
1733 incsearch_postponed = TRUE;
1734 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001735 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 highlight_match = TRUE; /* highlight position */
1737 else
1738 highlight_match = FALSE; /* remove highlight */
1739
1740 /* first restore the old curwin values, so the screen is
1741 * positioned in the same way as the actual search command */
1742 curwin->w_leftcol = old_leftcol;
1743 curwin->w_topline = old_topline;
1744# ifdef FEAT_DIFF
1745 curwin->w_topfill = old_topfill;
1746# endif
1747 curwin->w_botline = old_botline;
1748 changed_cline_bef_curs();
1749 update_topline();
1750
1751 if (i != 0)
1752 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001753 pos_T save_pos = curwin->w_cursor;
1754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001756 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 * moves the whole match onto the screen when 'nowrap' is set.
1758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 curwin->w_cursor.lnum += search_match_lines;
1760 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001761 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1762 {
1763 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1764 coladvance((colnr_T)MAXCOL);
1765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001767 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001768 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001770 else
1771 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001774# ifdef FEAT_WINDOWS
1775 /* May redraw the status line to show the cursor position. */
1776 if (p_ru && curwin->w_status_height > 0)
1777 curwin->w_redr_status = TRUE;
1778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001780 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001781 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001782 restore_cmdline(&save_ccline);
1783
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001784 /* Leave it at the end to make CTRL-R CTRL-W work. */
1785 if (i != 0)
1786 curwin->w_cursor = end_pos;
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 msg_starthere();
1789 redrawcmdline();
1790 did_incsearch = TRUE;
1791 }
1792#else /* FEAT_SEARCH_EXTRA */
1793 ;
1794#endif
1795
1796#ifdef FEAT_RIGHTLEFT
1797 if (cmdmsg_rl
1798# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001799 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800# endif
1801 )
1802 /* Always redraw the whole command line to fix shaping and
1803 * right-left typing. Not efficient, but it works. */
1804 redrawcmd();
1805#endif
1806 }
1807
1808returncmd:
1809
1810#ifdef FEAT_RIGHTLEFT
1811 cmdmsg_rl = FALSE;
1812#endif
1813
1814#ifdef FEAT_FKMAP
1815 cmd_fkmap = 0;
1816#endif
1817
1818 ExpandCleanup(&xpc);
1819
1820#ifdef FEAT_SEARCH_EXTRA
1821 if (did_incsearch)
1822 {
1823 curwin->w_cursor = old_cursor;
1824 curwin->w_curswant = old_curswant;
1825 curwin->w_leftcol = old_leftcol;
1826 curwin->w_topline = old_topline;
1827# ifdef FEAT_DIFF
1828 curwin->w_topfill = old_topfill;
1829# endif
1830 curwin->w_botline = old_botline;
1831 highlight_match = FALSE;
1832 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001833 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 }
1835#endif
1836
1837 if (ccline.cmdbuff != NULL)
1838 {
1839 /*
1840 * Put line in history buffer (":" and "=" only when it was typed).
1841 */
1842#ifdef FEAT_CMDHIST
1843 if (ccline.cmdlen && firstc != NUL
1844 && (some_key_typed || histype == HIST_SEARCH))
1845 {
1846 add_to_history(histype, ccline.cmdbuff, TRUE,
1847 histype == HIST_SEARCH ? firstc : NUL);
1848 if (firstc == ':')
1849 {
1850 vim_free(new_last_cmdline);
1851 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1852 }
1853 }
1854#endif
1855
1856 if (gotesc) /* abandon command line */
1857 {
1858 vim_free(ccline.cmdbuff);
1859 ccline.cmdbuff = NULL;
1860 if (msg_scrolled == 0)
1861 compute_cmdrow();
1862 MSG("");
1863 redraw_cmdline = TRUE;
1864 }
1865 }
1866
1867 /*
1868 * If the screen was shifted up, redraw the whole screen (later).
1869 * If the line is too long, clear it, so ruler and shown command do
1870 * not get printed in the middle of it.
1871 */
1872 msg_check();
1873 msg_scroll = save_msg_scroll;
1874 redir_off = FALSE;
1875
1876 /* When the command line was typed, no need for a wait-return prompt. */
1877 if (some_key_typed)
1878 need_wait_return = FALSE;
1879
1880 State = save_State;
1881#ifdef USE_IM_CONTROL
1882 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1883 im_save_status(b_im_ptr);
1884 im_set_active(FALSE);
1885#endif
1886#ifdef FEAT_MOUSE
1887 setmouse();
1888#endif
1889#ifdef CURSOR_SHAPE
1890 ui_cursor_shape(); /* may show different cursor shape */
1891#endif
1892
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001893 {
1894 char_u *p = ccline.cmdbuff;
1895
1896 /* Make ccline empty, getcmdline() may try to use it. */
1897 ccline.cmdbuff = NULL;
1898 return p;
1899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900}
1901
1902#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1903/*
1904 * Get a command line with a prompt.
1905 * This is prepared to be called recursively from getcmdline() (e.g. by
1906 * f_input() when evaluating an expression from CTRL-R =).
1907 * Returns the command line in allocated memory, or NULL.
1908 */
1909 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001910getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 int firstc;
1912 char_u *prompt; /* command line prompt */
1913 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001914 int xp_context; /* type of expansion */
1915 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916{
1917 char_u *s;
1918 struct cmdline_info save_ccline;
1919 int msg_col_save = msg_col;
1920
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001921 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 ccline.cmdprompt = prompt;
1923 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001924# ifdef FEAT_EVAL
1925 ccline.xp_context = xp_context;
1926 ccline.xp_arg = xp_arg;
1927 ccline.input_fn = (firstc == '@');
1928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001930 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 /* Restore msg_col, the prompt from input() may have changed it. */
1932 msg_col = msg_col_save;
1933
1934 return s;
1935}
1936#endif
1937
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001938/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001939 * Return TRUE when the text must not be changed and we can't switch to
1940 * another window or buffer. Used when editing the command line, evaluating
1941 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001942 */
1943 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001944text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001945{
1946#ifdef FEAT_CMDWIN
1947 if (cmdwin_type != 0)
1948 return TRUE;
1949#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001950 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001951}
1952
1953/*
1954 * Give an error message for a command that isn't allowed while the cmdline
1955 * window is open or editing the cmdline in another way.
1956 */
1957 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001958text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001959{
1960#ifdef FEAT_CMDWIN
1961 if (cmdwin_type != 0)
1962 EMSG(_(e_cmdwin));
1963 else
1964#endif
1965 EMSG(_(e_secure));
1966}
1967
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001968#if defined(FEAT_AUTOCMD) || defined(PROTO)
1969/*
1970 * Check if "curbuf_lock" is set and return TRUE when it is and give an error
1971 * message.
1972 */
1973 int
1974curbuf_locked()
1975{
1976 if (curbuf_lock > 0)
1977 {
1978 EMSG(_("E788: Not allowed to edit another buffer now"));
1979 return TRUE;
1980 }
1981 return FALSE;
1982}
1983#endif
1984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 static int
1986cmdline_charsize(idx)
1987 int idx;
1988{
1989#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1990 if (cmdline_star > 0) /* showing '*', always 1 position */
1991 return 1;
1992#endif
1993 return ptr2cells(ccline.cmdbuff + idx);
1994}
1995
1996/*
1997 * Compute the offset of the cursor on the command line for the prompt and
1998 * indent.
1999 */
2000 static void
2001set_cmdspos()
2002{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002003 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 ccline.cmdspos = 1 + ccline.cmdindent;
2005 else
2006 ccline.cmdspos = 0 + ccline.cmdindent;
2007}
2008
2009/*
2010 * Compute the screen position for the cursor on the command line.
2011 */
2012 static void
2013set_cmdspos_cursor()
2014{
2015 int i, m, c;
2016
2017 set_cmdspos();
2018 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002019 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002021 if (m < 0) /* overflow, Columns or Rows at weird value */
2022 m = MAXCOL;
2023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 else
2025 m = MAXCOL;
2026 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2027 {
2028 c = cmdline_charsize(i);
2029#ifdef FEAT_MBYTE
2030 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2031 if (has_mbyte)
2032 correct_cmdspos(i, c);
2033#endif
2034 /* If the cmdline doesn't fit, put cursor on last visible char. */
2035 if ((ccline.cmdspos += c) >= m)
2036 {
2037 ccline.cmdpos = i - 1;
2038 ccline.cmdspos -= c;
2039 break;
2040 }
2041#ifdef FEAT_MBYTE
2042 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002043 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044#endif
2045 }
2046}
2047
2048#ifdef FEAT_MBYTE
2049/*
2050 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2051 * character that doesn't fit, so that a ">" must be displayed.
2052 */
2053 static void
2054correct_cmdspos(idx, cells)
2055 int idx;
2056 int cells;
2057{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002058 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2060 && ccline.cmdspos % Columns + cells > Columns)
2061 ccline.cmdspos++;
2062}
2063#endif
2064
2065/*
2066 * Get an Ex command line for the ":" command.
2067 */
2068/* ARGSUSED */
2069 char_u *
2070getexline(c, dummy, indent)
2071 int c; /* normally ':', NUL for ":append" */
2072 void *dummy; /* cookie not used */
2073 int indent; /* indent for inside conditionals */
2074{
2075 /* When executing a register, remove ':' that's in front of each line. */
2076 if (exec_from_reg && vpeekc() == ':')
2077 (void)vgetc();
2078 return getcmdline(c, 1L, indent);
2079}
2080
2081/*
2082 * Get an Ex command line for Ex mode.
2083 * In Ex mode we only use the OS supplied line editing features and no
2084 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002085 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 */
2087/* ARGSUSED */
2088 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002089getexmodeline(promptc, dummy, indent)
2090 int promptc; /* normally ':', NUL for ":append" and '?' for
2091 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 void *dummy; /* cookie not used */
2093 int indent; /* indent for inside conditionals */
2094{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002095 garray_T line_ga;
2096 char_u *pend;
2097 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002098 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002099 int escaped = FALSE; /* CTRL-V typed */
2100 int vcol = 0;
2101 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002102 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103
2104 /* Switch cursor on now. This avoids that it happens after the "\n", which
2105 * confuses the system function that computes tabstops. */
2106 cursor_on();
2107
2108 /* always start in column 0; write a newline if necessary */
2109 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002110 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002112 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002114 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002115 if (p_prompt)
2116 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 while (indent-- > 0)
2118 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
2121
2122 ga_init2(&line_ga, 1, 30);
2123
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002124 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002125 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002126 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002127 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002128 while (indent >= 8)
2129 {
2130 ga_append(&line_ga, TAB);
2131 msg_puts((char_u *)" ");
2132 indent -= 8;
2133 }
2134 while (indent-- > 0)
2135 {
2136 ga_append(&line_ga, ' ');
2137 msg_putchar(' ');
2138 }
2139 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002140 ++no_mapping;
2141 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002142
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 /*
2144 * Get the line, one character at a time.
2145 */
2146 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002147 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
2149 if (ga_grow(&line_ga, 40) == FAIL)
2150 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002151 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002153 /* Get one character at a time. Don't use inchar(), it can't handle
2154 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002155 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002156 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157
2158 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002159 * Handle line editing.
2160 * Previously this was left to the system, putting the terminal in
2161 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002163 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002165 msg_putchar('\n');
2166 break;
2167 }
2168
2169 if (!escaped)
2170 {
2171 /* CR typed means "enter", which is NL */
2172 if (c1 == '\r')
2173 c1 = '\n';
2174
2175 if (c1 == BS || c1 == K_BS
2176 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002178 if (line_ga.ga_len > 0)
2179 {
2180 --line_ga.ga_len;
2181 goto redraw;
2182 }
2183 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 }
2185
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002186 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002188 msg_col = startcol;
2189 msg_clr_eos();
2190 line_ga.ga_len = 0;
2191 continue;
2192 }
2193
2194 if (c1 == Ctrl_T)
2195 {
2196 p = (char_u *)line_ga.ga_data;
2197 p[line_ga.ga_len] = NUL;
2198 indent = get_indent_str(p, 8);
2199 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2200add_indent:
2201 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002203 char_u *s = skipwhite(p);
2204
2205 ga_grow(&line_ga, 1);
2206 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2207 *s = ' ';
2208 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002210redraw:
2211 /* redraw the line */
2212 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002213 vcol = 0;
2214 for (p = (char_u *)line_ga.ga_data;
2215 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002217 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002219 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002221 msg_putchar(' ');
2222 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002224 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002226 msg_outtrans_len(p, 1);
2227 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 }
2229 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002230 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002231 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002232 continue;
2233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002235 if (c1 == Ctrl_D)
2236 {
2237 /* Delete one shiftwidth. */
2238 p = (char_u *)line_ga.ga_data;
2239 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002241 if (prev_char == '^')
2242 ex_keep_indent = TRUE;
2243 indent = 0;
2244 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 }
2246 else
2247 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002248 p[line_ga.ga_len] = NUL;
2249 indent = get_indent_str(p, 8);
2250 --indent;
2251 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002253 while (get_indent_str(p, 8) > indent)
2254 {
2255 char_u *s = skipwhite(p);
2256
2257 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2258 --line_ga.ga_len;
2259 }
2260 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002262
2263 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2264 {
2265 escaped = TRUE;
2266 continue;
2267 }
2268
2269 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2270 if (IS_SPECIAL(c1))
2271 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002273
2274 if (IS_SPECIAL(c1))
2275 c1 = '?';
2276 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002277 if (c1 == '\n')
2278 msg_putchar('\n');
2279 else if (c1 == TAB)
2280 {
2281 /* Don't use chartabsize(), 'ts' can be different */
2282 do
2283 {
2284 msg_putchar(' ');
2285 } while (++vcol % 8);
2286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002289 msg_outtrans_len(
2290 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2291 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002293 ++line_ga.ga_len;
2294 escaped = FALSE;
2295
2296 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002297 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002298
2299 /* we are done when a NL is entered, but not when it comes after a
2300 * backslash */
2301 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2302 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002305 --pend;
2306 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002307 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
2309 }
2310
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002311 --no_mapping;
2312 --allow_keys;
2313
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 /* make following messages go to the next line */
2315 msg_didout = FALSE;
2316 msg_col = 0;
2317 if (msg_row < Rows - 1)
2318 ++msg_row;
2319 emsg_on_display = FALSE; /* don't want ui_delay() */
2320
2321 if (got_int)
2322 ga_clear(&line_ga);
2323
2324 return (char_u *)line_ga.ga_data;
2325}
2326
Bram Moolenaare344bea2005-09-01 20:46:49 +00002327# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2328 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329/*
2330 * Return TRUE if ccline.overstrike is on.
2331 */
2332 int
2333cmdline_overstrike()
2334{
2335 return ccline.overstrike;
2336}
2337
2338/*
2339 * Return TRUE if the cursor is at the end of the cmdline.
2340 */
2341 int
2342cmdline_at_end()
2343{
2344 return (ccline.cmdpos >= ccline.cmdlen);
2345}
2346#endif
2347
Bram Moolenaar9372a112005-12-06 19:59:18 +00002348#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349/*
2350 * Return the virtual column number at the current cursor position.
2351 * This is used by the IM code to obtain the start of the preedit string.
2352 */
2353 colnr_T
2354cmdline_getvcol_cursor()
2355{
2356 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2357 return MAXCOL;
2358
2359# ifdef FEAT_MBYTE
2360 if (has_mbyte)
2361 {
2362 colnr_T col;
2363 int i = 0;
2364
2365 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002366 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
2368 return col;
2369 }
2370 else
2371# endif
2372 return ccline.cmdpos;
2373}
2374#endif
2375
2376#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2377/*
2378 * If part of the command line is an IM preedit string, redraw it with
2379 * IM feedback attributes. The cursor position is restored after drawing.
2380 */
2381 static void
2382redrawcmd_preedit()
2383{
2384 if ((State & CMDLINE)
2385 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002386 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 && !p_imdisable
2388 && im_is_preediting())
2389 {
2390 int cmdpos = 0;
2391 int cmdspos;
2392 int old_row;
2393 int old_col;
2394 colnr_T col;
2395
2396 old_row = msg_row;
2397 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002398 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
2400# ifdef FEAT_MBYTE
2401 if (has_mbyte)
2402 {
2403 for (col = 0; col < preedit_start_col
2404 && cmdpos < ccline.cmdlen; ++col)
2405 {
2406 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002407 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409 }
2410 else
2411# endif
2412 {
2413 cmdspos += preedit_start_col;
2414 cmdpos += preedit_start_col;
2415 }
2416
2417 msg_row = cmdline_row + (cmdspos / (int)Columns);
2418 msg_col = cmdspos % (int)Columns;
2419 if (msg_row >= Rows)
2420 msg_row = Rows - 1;
2421
2422 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2423 {
2424 int char_len;
2425 int char_attr;
2426
2427 char_attr = im_get_feedback_attr(col);
2428 if (char_attr < 0)
2429 break; /* end of preedit string */
2430
2431# ifdef FEAT_MBYTE
2432 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002433 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 else
2435# endif
2436 char_len = 1;
2437
2438 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2439 cmdpos += char_len;
2440 }
2441
2442 msg_row = old_row;
2443 msg_col = old_col;
2444 }
2445}
2446#endif /* FEAT_XIM && FEAT_GUI_GTK */
2447
2448/*
2449 * Allocate a new command line buffer.
2450 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2451 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2452 */
2453 static void
2454alloc_cmdbuff(len)
2455 int len;
2456{
2457 /*
2458 * give some extra space to avoid having to allocate all the time
2459 */
2460 if (len < 80)
2461 len = 100;
2462 else
2463 len += 20;
2464
2465 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2466 ccline.cmdbufflen = len;
2467}
2468
2469/*
2470 * Re-allocate the command line to length len + something extra.
2471 * return FAIL for failure, OK otherwise
2472 */
2473 static int
2474realloc_cmdbuff(len)
2475 int len;
2476{
2477 char_u *p;
2478
2479 p = ccline.cmdbuff;
2480 alloc_cmdbuff(len); /* will get some more */
2481 if (ccline.cmdbuff == NULL) /* out of memory */
2482 {
2483 ccline.cmdbuff = p; /* keep the old one */
2484 return FAIL;
2485 }
2486 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2487 vim_free(p);
2488 return OK;
2489}
2490
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002491#if defined(FEAT_ARABIC) || defined(PROTO)
2492static char_u *arshape_buf = NULL;
2493
2494# if defined(EXITFREE) || defined(PROTO)
2495 void
2496free_cmdline_buf()
2497{
2498 vim_free(arshape_buf);
2499}
2500# endif
2501#endif
2502
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503/*
2504 * Draw part of the cmdline at the current cursor position. But draw stars
2505 * when cmdline_star is TRUE.
2506 */
2507 static void
2508draw_cmdline(start, len)
2509 int start;
2510 int len;
2511{
2512#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2513 int i;
2514
2515 if (cmdline_star > 0)
2516 for (i = 0; i < len; ++i)
2517 {
2518 msg_putchar('*');
2519# ifdef FEAT_MBYTE
2520 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002521 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522# endif
2523 }
2524 else
2525#endif
2526#ifdef FEAT_ARABIC
2527 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 static int buflen = 0;
2530 char_u *p;
2531 int j;
2532 int newlen = 0;
2533 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002534 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 int prev_c = 0;
2536 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002537 int u8c;
2538 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
2541 /*
2542 * Do arabic shaping into a temporary buffer. This is very
2543 * inefficient!
2544 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002545 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
2547 /* Re-allocate the buffer. We keep it around to avoid a lot of
2548 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002549 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002550 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002551 arshape_buf = alloc(buflen);
2552 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 return; /* out of memory */
2554 }
2555
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002556 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2557 {
2558 /* Prepend a space to draw the leading composing char on. */
2559 arshape_buf[0] = ' ';
2560 newlen = 1;
2561 }
2562
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 for (j = start; j < start + len; j += mb_l)
2564 {
2565 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002566 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002567 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 if (ARABIC_CHAR(u8c))
2569 {
2570 /* Do Arabic shaping. */
2571 if (cmdmsg_rl)
2572 {
2573 /* displaying from right to left */
2574 pc = prev_c;
2575 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002576 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 if (j + mb_l >= start + len)
2578 nc = NUL;
2579 else
2580 nc = utf_ptr2char(p + mb_l);
2581 }
2582 else
2583 {
2584 /* displaying from left to right */
2585 if (j + mb_l >= start + len)
2586 pc = NUL;
2587 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002588 {
2589 int pcc[MAX_MCO];
2590
2591 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002593 pc1 = pcc[0];
2594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 nc = prev_c;
2596 }
2597 prev_c = u8c;
2598
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002599 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002601 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002602 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002604 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2605 if (u8cc[1] != 0)
2606 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002607 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 }
2609 }
2610 else
2611 {
2612 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002613 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 newlen += mb_l;
2615 }
2616 }
2617
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002618 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
2620 else
2621#endif
2622 msg_outtrans_len(ccline.cmdbuff + start, len);
2623}
2624
2625/*
2626 * Put a character on the command line. Shifts the following text to the
2627 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2628 * "c" must be printable (fit in one display cell)!
2629 */
2630 void
2631putcmdline(c, shift)
2632 int c;
2633 int shift;
2634{
2635 if (cmd_silent)
2636 return;
2637 msg_no_more = TRUE;
2638 msg_putchar(c);
2639 if (shift)
2640 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2641 msg_no_more = FALSE;
2642 cursorcmd();
2643}
2644
2645/*
2646 * Undo a putcmdline(c, FALSE).
2647 */
2648 void
2649unputcmdline()
2650{
2651 if (cmd_silent)
2652 return;
2653 msg_no_more = TRUE;
2654 if (ccline.cmdlen == ccline.cmdpos)
2655 msg_putchar(' ');
2656 else
2657 draw_cmdline(ccline.cmdpos, 1);
2658 msg_no_more = FALSE;
2659 cursorcmd();
2660}
2661
2662/*
2663 * Put the given string, of the given length, onto the command line.
2664 * If len is -1, then STRLEN() is used to calculate the length.
2665 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2666 * part will be redrawn, otherwise it will not. If this function is called
2667 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2668 * called afterwards.
2669 */
2670 int
2671put_on_cmdline(str, len, redraw)
2672 char_u *str;
2673 int len;
2674 int redraw;
2675{
2676 int retval;
2677 int i;
2678 int m;
2679 int c;
2680
2681 if (len < 0)
2682 len = (int)STRLEN(str);
2683
2684 /* Check if ccline.cmdbuff needs to be longer */
2685 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2686 retval = realloc_cmdbuff(ccline.cmdlen + len);
2687 else
2688 retval = OK;
2689 if (retval == OK)
2690 {
2691 if (!ccline.overstrike)
2692 {
2693 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2694 ccline.cmdbuff + ccline.cmdpos,
2695 (size_t)(ccline.cmdlen - ccline.cmdpos));
2696 ccline.cmdlen += len;
2697 }
2698 else
2699 {
2700#ifdef FEAT_MBYTE
2701 if (has_mbyte)
2702 {
2703 /* Count nr of characters in the new string. */
2704 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002705 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 ++m;
2707 /* Count nr of bytes in cmdline that are overwritten by these
2708 * characters. */
2709 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002710 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 --m;
2712 if (i < ccline.cmdlen)
2713 {
2714 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2715 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2716 ccline.cmdlen += ccline.cmdpos + len - i;
2717 }
2718 else
2719 ccline.cmdlen = ccline.cmdpos + len;
2720 }
2721 else
2722#endif
2723 if (ccline.cmdpos + len > ccline.cmdlen)
2724 ccline.cmdlen = ccline.cmdpos + len;
2725 }
2726 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2727 ccline.cmdbuff[ccline.cmdlen] = NUL;
2728
2729#ifdef FEAT_MBYTE
2730 if (enc_utf8)
2731 {
2732 /* When the inserted text starts with a composing character,
2733 * backup to the character before it. There could be two of them.
2734 */
2735 i = 0;
2736 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2737 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2738 {
2739 i = (*mb_head_off)(ccline.cmdbuff,
2740 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2741 ccline.cmdpos -= i;
2742 len += i;
2743 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2744 }
2745# ifdef FEAT_ARABIC
2746 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2747 {
2748 /* Check the previous character for Arabic combining pair. */
2749 i = (*mb_head_off)(ccline.cmdbuff,
2750 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2751 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2752 + ccline.cmdpos - i), c))
2753 {
2754 ccline.cmdpos -= i;
2755 len += i;
2756 }
2757 else
2758 i = 0;
2759 }
2760# endif
2761 if (i != 0)
2762 {
2763 /* Also backup the cursor position. */
2764 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2765 ccline.cmdspos -= i;
2766 msg_col -= i;
2767 if (msg_col < 0)
2768 {
2769 msg_col += Columns;
2770 --msg_row;
2771 }
2772 }
2773 }
2774#endif
2775
2776 if (redraw && !cmd_silent)
2777 {
2778 msg_no_more = TRUE;
2779 i = cmdline_row;
2780 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2781 /* Avoid clearing the rest of the line too often. */
2782 if (cmdline_row != i || ccline.overstrike)
2783 msg_clr_eos();
2784 msg_no_more = FALSE;
2785 }
2786#ifdef FEAT_FKMAP
2787 /*
2788 * If we are in Farsi command mode, the character input must be in
2789 * Insert mode. So do not advance the cmdpos.
2790 */
2791 if (!cmd_fkmap)
2792#endif
2793 {
2794 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002797 if (m < 0) /* overflow, Columns or Rows at weird value */
2798 m = MAXCOL;
2799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 else
2801 m = MAXCOL;
2802 for (i = 0; i < len; ++i)
2803 {
2804 c = cmdline_charsize(ccline.cmdpos);
2805#ifdef FEAT_MBYTE
2806 /* count ">" for a double-wide char that doesn't fit. */
2807 if (has_mbyte)
2808 correct_cmdspos(ccline.cmdpos, c);
2809#endif
2810 /* Stop cursor at the end of the screen */
2811 if (ccline.cmdspos + c >= m)
2812 break;
2813 ccline.cmdspos += c;
2814#ifdef FEAT_MBYTE
2815 if (has_mbyte)
2816 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002817 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 if (c > len - i - 1)
2819 c = len - i - 1;
2820 ccline.cmdpos += c;
2821 i += c;
2822 }
2823#endif
2824 ++ccline.cmdpos;
2825 }
2826 }
2827 }
2828 if (redraw)
2829 msg_check();
2830 return retval;
2831}
2832
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002833static struct cmdline_info prev_ccline;
2834static int prev_ccline_used = FALSE;
2835
2836/*
2837 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2838 * and overwrite it. But get_cmdline_str() may need it, thus make it
2839 * available globally in prev_ccline.
2840 */
2841 static void
2842save_cmdline(ccp)
2843 struct cmdline_info *ccp;
2844{
2845 if (!prev_ccline_used)
2846 {
2847 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2848 prev_ccline_used = TRUE;
2849 }
2850 *ccp = prev_ccline;
2851 prev_ccline = ccline;
2852 ccline.cmdbuff = NULL;
2853 ccline.cmdprompt = NULL;
2854}
2855
2856/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002857 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002858 */
2859 static void
2860restore_cmdline(ccp)
2861 struct cmdline_info *ccp;
2862{
2863 ccline = prev_ccline;
2864 prev_ccline = *ccp;
2865}
2866
Bram Moolenaar5a305422006-04-28 22:38:25 +00002867#if defined(FEAT_EVAL) || defined(PROTO)
2868/*
2869 * Save the command line into allocated memory. Returns a pointer to be
2870 * passed to restore_cmdline_alloc() later.
2871 * Returns NULL when failed.
2872 */
2873 char_u *
2874save_cmdline_alloc()
2875{
2876 struct cmdline_info *p;
2877
2878 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2879 if (p != NULL)
2880 save_cmdline(p);
2881 return (char_u *)p;
2882}
2883
2884/*
2885 * Restore the command line from the return value of save_cmdline_alloc().
2886 */
2887 void
2888restore_cmdline_alloc(p)
2889 char_u *p;
2890{
2891 if (p != NULL)
2892 {
2893 restore_cmdline((struct cmdline_info *)p);
2894 vim_free(p);
2895 }
2896}
2897#endif
2898
Bram Moolenaar8299df92004-07-10 09:47:34 +00002899/*
2900 * paste a yank register into the command line.
2901 * used by CTRL-R command in command-line mode
2902 * insert_reg() can't be used here, because special characters from the
2903 * register contents will be interpreted as commands.
2904 *
2905 * return FAIL for failure, OK otherwise
2906 */
2907 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002908cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00002909 int regname;
2910 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002911 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00002912{
2913 long i;
2914 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002915 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002916 int allocated;
2917 struct cmdline_info save_ccline;
2918
2919 /* check for valid regname; also accept special characters for CTRL-R in
2920 * the command line */
2921 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2922 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2923 return FAIL;
2924
2925 /* A register containing CTRL-R can cause an endless loop. Allow using
2926 * CTRL-C to break the loop. */
2927 line_breakcheck();
2928 if (got_int)
2929 return FAIL;
2930
2931#ifdef FEAT_CLIPBOARD
2932 regname = may_get_selection(regname);
2933#endif
2934
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002935 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002936 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002937 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002938 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002939 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002940 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002941 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002942
2943 if (i)
2944 {
2945 /* Got the value of a special register in "arg". */
2946 if (arg == NULL)
2947 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002948
2949 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
2950 * part of the word. */
2951 p = arg;
2952 if (p_is && regname == Ctrl_W)
2953 {
2954 char_u *w;
2955 int len;
2956
2957 /* Locate start of last word in the cmd buffer. */
2958 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
2959 {
2960#ifdef FEAT_MBYTE
2961 if (has_mbyte)
2962 {
2963 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
2964 if (!vim_iswordc(mb_ptr2char(w - len)))
2965 break;
2966 w -= len;
2967 }
2968 else
2969#endif
2970 {
2971 if (!vim_iswordc(w[-1]))
2972 break;
2973 --w;
2974 }
2975 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002976 len = (int)((ccline.cmdbuff + ccline.cmdlen) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002977 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
2978 p += len;
2979 }
2980
2981 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002982 if (allocated)
2983 vim_free(arg);
2984 return OK;
2985 }
2986
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002987 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002988}
2989
2990/*
2991 * Put a string on the command line.
2992 * When "literally" is TRUE, insert literally.
2993 * When "literally" is FALSE, insert as typed, but don't leave the command
2994 * line.
2995 */
2996 void
2997cmdline_paste_str(s, literally)
2998 char_u *s;
2999 int literally;
3000{
3001 int c, cv;
3002
3003 if (literally)
3004 put_on_cmdline(s, -1, TRUE);
3005 else
3006 while (*s != NUL)
3007 {
3008 cv = *s;
3009 if (cv == Ctrl_V && s[1])
3010 ++s;
3011#ifdef FEAT_MBYTE
3012 if (has_mbyte)
3013 {
3014 c = mb_ptr2char(s);
3015 s += mb_char2len(c);
3016 }
3017 else
3018#endif
3019 c = *s++;
3020 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3021#ifdef UNIX
3022 || c == intr_char
3023#endif
3024 || (c == Ctrl_BSL && *s == Ctrl_N))
3025 stuffcharReadbuff(Ctrl_V);
3026 stuffcharReadbuff(c);
3027 }
3028}
3029
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030#ifdef FEAT_WILDMENU
3031/*
3032 * Delete characters on the command line, from "from" to the current
3033 * position.
3034 */
3035 static void
3036cmdline_del(from)
3037 int from;
3038{
3039 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3040 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3041 ccline.cmdlen -= ccline.cmdpos - from;
3042 ccline.cmdpos = from;
3043}
3044#endif
3045
3046/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003047 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 * search
3049 */
3050 void
3051redrawcmdline()
3052{
3053 if (cmd_silent)
3054 return;
3055 need_wait_return = FALSE;
3056 compute_cmdrow();
3057 redrawcmd();
3058 cursorcmd();
3059}
3060
3061 static void
3062redrawcmdprompt()
3063{
3064 int i;
3065
3066 if (cmd_silent)
3067 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003068 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 msg_putchar(ccline.cmdfirstc);
3070 if (ccline.cmdprompt != NULL)
3071 {
3072 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3073 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3074 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003075 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 --ccline.cmdindent;
3077 }
3078 else
3079 for (i = ccline.cmdindent; i > 0; --i)
3080 msg_putchar(' ');
3081}
3082
3083/*
3084 * Redraw what is currently on the command line.
3085 */
3086 void
3087redrawcmd()
3088{
3089 if (cmd_silent)
3090 return;
3091
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003092 /* when 'incsearch' is set there may be no command line while redrawing */
3093 if (ccline.cmdbuff == NULL)
3094 {
3095 windgoto(cmdline_row, 0);
3096 msg_clr_eos();
3097 return;
3098 }
3099
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 msg_start();
3101 redrawcmdprompt();
3102
3103 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3104 msg_no_more = TRUE;
3105 draw_cmdline(0, ccline.cmdlen);
3106 msg_clr_eos();
3107 msg_no_more = FALSE;
3108
3109 set_cmdspos_cursor();
3110
3111 /*
3112 * An emsg() before may have set msg_scroll. This is used in normal mode,
3113 * in cmdline mode we can reset them now.
3114 */
3115 msg_scroll = FALSE; /* next message overwrites cmdline */
3116
3117 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3118 * in cmdline mode */
3119 skip_redraw = FALSE;
3120}
3121
3122 void
3123compute_cmdrow()
3124{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003125 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 cmdline_row = Rows - 1;
3127 else
3128 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3129 + W_STATUS_HEIGHT(lastwin);
3130}
3131
3132 static void
3133cursorcmd()
3134{
3135 if (cmd_silent)
3136 return;
3137
3138#ifdef FEAT_RIGHTLEFT
3139 if (cmdmsg_rl)
3140 {
3141 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3142 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3143 if (msg_row <= 0)
3144 msg_row = Rows - 1;
3145 }
3146 else
3147#endif
3148 {
3149 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3150 msg_col = ccline.cmdspos % (int)Columns;
3151 if (msg_row >= Rows)
3152 msg_row = Rows - 1;
3153 }
3154
3155 windgoto(msg_row, msg_col);
3156#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3157 redrawcmd_preedit();
3158#endif
3159#ifdef MCH_CURSOR_SHAPE
3160 mch_update_cursor();
3161#endif
3162}
3163
3164 void
3165gotocmdline(clr)
3166 int clr;
3167{
3168 msg_start();
3169#ifdef FEAT_RIGHTLEFT
3170 if (cmdmsg_rl)
3171 msg_col = Columns - 1;
3172 else
3173#endif
3174 msg_col = 0; /* always start in column 0 */
3175 if (clr) /* clear the bottom line(s) */
3176 msg_clr_eos(); /* will reset clear_cmdline */
3177 windgoto(cmdline_row, 0);
3178}
3179
3180/*
3181 * Check the word in front of the cursor for an abbreviation.
3182 * Called when the non-id character "c" has been entered.
3183 * When an abbreviation is recognized it is removed from the text with
3184 * backspaces and the replacement string is inserted, followed by "c".
3185 */
3186 static int
3187ccheck_abbr(c)
3188 int c;
3189{
3190 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3191 return FALSE;
3192
3193 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3194}
3195
3196/*
3197 * Return FAIL if this is not an appropriate context in which to do
3198 * completion of anything, return OK if it is (even if there are no matches).
3199 * For the caller, this means that the character is just passed through like a
3200 * normal character (instead of being expanded). This allows :s/^I^D etc.
3201 */
3202 static int
3203nextwild(xp, type, options)
3204 expand_T *xp;
3205 int type;
3206 int options; /* extra options for ExpandOne() */
3207{
3208 int i, j;
3209 char_u *p1;
3210 char_u *p2;
3211 int oldlen;
3212 int difflen;
3213 int v;
3214
3215 if (xp->xp_numfiles == -1)
3216 {
3217 set_expand_context(xp);
3218 cmd_showtail = expand_showtail(xp);
3219 }
3220
3221 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3222 {
3223 beep_flush();
3224 return OK; /* Something illegal on command line */
3225 }
3226 if (xp->xp_context == EXPAND_NOTHING)
3227 {
3228 /* Caller can use the character as a normal char instead */
3229 return FAIL;
3230 }
3231
3232 MSG_PUTS("..."); /* show that we are busy */
3233 out_flush();
3234
3235 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3236 oldlen = ccline.cmdpos - i;
3237
3238 if (type == WILD_NEXT || type == WILD_PREV)
3239 {
3240 /*
3241 * Get next/previous match for a previous expanded pattern.
3242 */
3243 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3244 }
3245 else
3246 {
3247 /*
3248 * Translate string into pattern and expand it.
3249 */
3250 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3251 p2 = NULL;
3252 else
3253 {
3254 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3255 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3256 |options, type);
3257 vim_free(p1);
3258 /* longest match: make sure it is not shorter (happens with :help */
3259 if (p2 != NULL && type == WILD_LONGEST)
3260 {
3261 for (j = 0; j < oldlen; ++j)
3262 if (ccline.cmdbuff[i + j] == '*'
3263 || ccline.cmdbuff[i + j] == '?')
3264 break;
3265 if ((int)STRLEN(p2) < j)
3266 {
3267 vim_free(p2);
3268 p2 = NULL;
3269 }
3270 }
3271 }
3272 }
3273
3274 if (p2 != NULL && !got_int)
3275 {
3276 difflen = (int)STRLEN(p2) - oldlen;
3277 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3278 {
3279 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3280 xp->xp_pattern = ccline.cmdbuff + i;
3281 }
3282 else
3283 v = OK;
3284 if (v == OK)
3285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003286 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3287 &ccline.cmdbuff[ccline.cmdpos],
3288 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3289 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 ccline.cmdlen += difflen;
3291 ccline.cmdpos += difflen;
3292 }
3293 }
3294 vim_free(p2);
3295
3296 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003297 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298
3299 /* When expanding a ":map" command and no matches are found, assume that
3300 * the key is supposed to be inserted literally */
3301 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3302 return FAIL;
3303
3304 if (xp->xp_numfiles <= 0 && p2 == NULL)
3305 beep_flush();
3306 else if (xp->xp_numfiles == 1)
3307 /* free expanded pattern */
3308 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3309
3310 return OK;
3311}
3312
3313/*
3314 * Do wildcard expansion on the string 'str'.
3315 * Chars that should not be expanded must be preceded with a backslash.
3316 * Return a pointer to alloced memory containing the new string.
3317 * Return NULL for failure.
3318 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003319 * "orig" is the originally expanded string, copied to allocated memory. It
3320 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3321 * WILD_PREV "orig" should be NULL.
3322 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003323 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3324 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 *
3326 * mode = WILD_FREE: just free previously expanded matches
3327 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3328 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3329 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3330 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3331 * mode = WILD_ALL: return all matches concatenated
3332 * mode = WILD_LONGEST: return longest matched part
3333 *
3334 * options = WILD_LIST_NOTFOUND: list entries without a match
3335 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3336 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3337 * options = WILD_NO_BEEP: Don't beep for multiple matches
3338 * options = WILD_ADD_SLASH: add a slash after directory names
3339 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3340 * options = WILD_SILENT: don't print warning messages
3341 * options = WILD_ESCAPE: put backslash before special chars
3342 *
3343 * The variables xp->xp_context and xp->xp_backslash must have been set!
3344 */
3345 char_u *
3346ExpandOne(xp, str, orig, options, mode)
3347 expand_T *xp;
3348 char_u *str;
3349 char_u *orig; /* allocated copy of original of expanded string */
3350 int options;
3351 int mode;
3352{
3353 char_u *ss = NULL;
3354 static int findex;
3355 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003356 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 int i;
3358 long_u len;
3359 int non_suf_match; /* number without matching suffix */
3360
3361 /*
3362 * first handle the case of using an old match
3363 */
3364 if (mode == WILD_NEXT || mode == WILD_PREV)
3365 {
3366 if (xp->xp_numfiles > 0)
3367 {
3368 if (mode == WILD_PREV)
3369 {
3370 if (findex == -1)
3371 findex = xp->xp_numfiles;
3372 --findex;
3373 }
3374 else /* mode == WILD_NEXT */
3375 ++findex;
3376
3377 /*
3378 * When wrapping around, return the original string, set findex to
3379 * -1.
3380 */
3381 if (findex < 0)
3382 {
3383 if (orig_save == NULL)
3384 findex = xp->xp_numfiles - 1;
3385 else
3386 findex = -1;
3387 }
3388 if (findex >= xp->xp_numfiles)
3389 {
3390 if (orig_save == NULL)
3391 findex = 0;
3392 else
3393 findex = -1;
3394 }
3395#ifdef FEAT_WILDMENU
3396 if (p_wmnu)
3397 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3398 findex, cmd_showtail);
3399#endif
3400 if (findex == -1)
3401 return vim_strsave(orig_save);
3402 return vim_strsave(xp->xp_files[findex]);
3403 }
3404 else
3405 return NULL;
3406 }
3407
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003408 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3410 {
3411 FreeWild(xp->xp_numfiles, xp->xp_files);
3412 xp->xp_numfiles = -1;
3413 vim_free(orig_save);
3414 orig_save = NULL;
3415 }
3416 findex = 0;
3417
3418 if (mode == WILD_FREE) /* only release file name */
3419 return NULL;
3420
3421 if (xp->xp_numfiles == -1)
3422 {
3423 vim_free(orig_save);
3424 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003425 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
3427 /*
3428 * Do the expansion.
3429 */
3430 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3431 options) == FAIL)
3432 {
3433#ifdef FNAME_ILLEGAL
3434 /* Illegal file name has been silently skipped. But when there
3435 * are wildcards, the real problem is that there was no match,
3436 * causing the pattern to be added, which has illegal characters.
3437 */
3438 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3439 EMSG2(_(e_nomatch2), str);
3440#endif
3441 }
3442 else if (xp->xp_numfiles == 0)
3443 {
3444 if (!(options & WILD_SILENT))
3445 EMSG2(_(e_nomatch2), str);
3446 }
3447 else
3448 {
3449 /* Escape the matches for use on the command line. */
3450 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3451
3452 /*
3453 * Check for matching suffixes in file names.
3454 */
3455 if (mode != WILD_ALL && mode != WILD_LONGEST)
3456 {
3457 if (xp->xp_numfiles)
3458 non_suf_match = xp->xp_numfiles;
3459 else
3460 non_suf_match = 1;
3461 if ((xp->xp_context == EXPAND_FILES
3462 || xp->xp_context == EXPAND_DIRECTORIES)
3463 && xp->xp_numfiles > 1)
3464 {
3465 /*
3466 * More than one match; check suffix.
3467 * The files will have been sorted on matching suffix in
3468 * expand_wildcards, only need to check the first two.
3469 */
3470 non_suf_match = 0;
3471 for (i = 0; i < 2; ++i)
3472 if (match_suffix(xp->xp_files[i]))
3473 ++non_suf_match;
3474 }
3475 if (non_suf_match != 1)
3476 {
3477 /* Can we ever get here unless it's while expanding
3478 * interactively? If not, we can get rid of this all
3479 * together. Don't really want to wait for this message
3480 * (and possibly have to hit return to continue!).
3481 */
3482 if (!(options & WILD_SILENT))
3483 EMSG(_(e_toomany));
3484 else if (!(options & WILD_NO_BEEP))
3485 beep_flush();
3486 }
3487 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3488 ss = vim_strsave(xp->xp_files[0]);
3489 }
3490 }
3491 }
3492
3493 /* Find longest common part */
3494 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3495 {
3496 for (len = 0; xp->xp_files[0][len]; ++len)
3497 {
3498 for (i = 0; i < xp->xp_numfiles; ++i)
3499 {
3500#ifdef CASE_INSENSITIVE_FILENAME
3501 if (xp->xp_context == EXPAND_DIRECTORIES
3502 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003503 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 || xp->xp_context == EXPAND_BUFFERS)
3505 {
3506 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3507 TOLOWER_LOC(xp->xp_files[0][len]))
3508 break;
3509 }
3510 else
3511#endif
3512 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3513 break;
3514 }
3515 if (i < xp->xp_numfiles)
3516 {
3517 if (!(options & WILD_NO_BEEP))
3518 vim_beep();
3519 break;
3520 }
3521 }
3522 ss = alloc((unsigned)len + 1);
3523 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003524 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 findex = -1; /* next p_wc gets first one */
3526 }
3527
3528 /* Concatenate all matching names */
3529 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3530 {
3531 len = 0;
3532 for (i = 0; i < xp->xp_numfiles; ++i)
3533 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3534 ss = lalloc(len, TRUE);
3535 if (ss != NULL)
3536 {
3537 *ss = NUL;
3538 for (i = 0; i < xp->xp_numfiles; ++i)
3539 {
3540 STRCAT(ss, xp->xp_files[i]);
3541 if (i != xp->xp_numfiles - 1)
3542 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3543 }
3544 }
3545 }
3546
3547 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3548 ExpandCleanup(xp);
3549
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003550 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003551 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003552 vim_free(orig);
3553
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 return ss;
3555}
3556
3557/*
3558 * Prepare an expand structure for use.
3559 */
3560 void
3561ExpandInit(xp)
3562 expand_T *xp;
3563{
3564 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003565#ifndef BACKSLASH_IN_FILENAME
3566 xp->xp_shell = FALSE;
3567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 xp->xp_numfiles = -1;
3569 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003570#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3571 xp->xp_arg = NULL;
3572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573}
3574
3575/*
3576 * Cleanup an expand structure after use.
3577 */
3578 void
3579ExpandCleanup(xp)
3580 expand_T *xp;
3581{
3582 if (xp->xp_numfiles >= 0)
3583 {
3584 FreeWild(xp->xp_numfiles, xp->xp_files);
3585 xp->xp_numfiles = -1;
3586 }
3587}
3588
3589 void
3590ExpandEscape(xp, str, numfiles, files, options)
3591 expand_T *xp;
3592 char_u *str;
3593 int numfiles;
3594 char_u **files;
3595 int options;
3596{
3597 int i;
3598 char_u *p;
3599
3600 /*
3601 * May change home directory back to "~"
3602 */
3603 if (options & WILD_HOME_REPLACE)
3604 tilde_replace(str, numfiles, files);
3605
3606 if (options & WILD_ESCAPE)
3607 {
3608 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003609 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 || xp->xp_context == EXPAND_BUFFERS
3611 || xp->xp_context == EXPAND_DIRECTORIES)
3612 {
3613 /*
3614 * Insert a backslash into a file name before a space, \, %, #
3615 * and wildmatch characters, except '~'.
3616 */
3617 for (i = 0; i < numfiles; ++i)
3618 {
3619 /* for ":set path=" we need to escape spaces twice */
3620 if (xp->xp_backslash == XP_BS_THREE)
3621 {
3622 p = vim_strsave_escaped(files[i], (char_u *)" ");
3623 if (p != NULL)
3624 {
3625 vim_free(files[i]);
3626 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003627#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 p = vim_strsave_escaped(files[i], (char_u *)" ");
3629 if (p != NULL)
3630 {
3631 vim_free(files[i]);
3632 files[i] = p;
3633 }
3634#endif
3635 }
3636 }
3637#ifdef BACKSLASH_IN_FILENAME
3638 {
3639 char_u buf[20];
3640 int j = 0;
3641
3642 /* Don't escape '[' and '{' if they are in 'isfname'. */
3643 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3644 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3645 buf[j++] = *p;
3646 buf[j] = NUL;
3647 p = vim_strsave_escaped(files[i], buf);
3648 }
3649#else
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003650 p = vim_strsave_escaped(files[i],
3651 xp->xp_shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652#endif
3653 if (p != NULL)
3654 {
3655 vim_free(files[i]);
3656 files[i] = p;
3657 }
3658
3659 /* If 'str' starts with "\~", replace "~" at start of
3660 * files[i] with "\~". */
3661 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003662 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
3664 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003665
3666 /* If the first file starts with a '+' escape it. Otherwise it
3667 * could be seen as "+cmd". */
3668 if (*files[0] == '+')
3669 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
3671 else if (xp->xp_context == EXPAND_TAGS)
3672 {
3673 /*
3674 * Insert a backslash before characters in a tag name that
3675 * would terminate the ":tag" command.
3676 */
3677 for (i = 0; i < numfiles; ++i)
3678 {
3679 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3680 if (p != NULL)
3681 {
3682 vim_free(files[i]);
3683 files[i] = p;
3684 }
3685 }
3686 }
3687 }
3688}
3689
3690/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003691 * Put a backslash before the file name in "pp", which is in allocated memory.
3692 */
3693 static void
3694escape_fname(pp)
3695 char_u **pp;
3696{
3697 char_u *p;
3698
3699 p = alloc((unsigned)(STRLEN(*pp) + 2));
3700 if (p != NULL)
3701 {
3702 p[0] = '\\';
3703 STRCPY(p + 1, *pp);
3704 vim_free(*pp);
3705 *pp = p;
3706 }
3707}
3708
3709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 * For each file name in files[num_files]:
3711 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3712 */
3713 void
3714tilde_replace(orig_pat, num_files, files)
3715 char_u *orig_pat;
3716 int num_files;
3717 char_u **files;
3718{
3719 int i;
3720 char_u *p;
3721
3722 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3723 {
3724 for (i = 0; i < num_files; ++i)
3725 {
3726 p = home_replace_save(NULL, files[i]);
3727 if (p != NULL)
3728 {
3729 vim_free(files[i]);
3730 files[i] = p;
3731 }
3732 }
3733 }
3734}
3735
3736/*
3737 * Show all matches for completion on the command line.
3738 * Returns EXPAND_NOTHING when the character that triggered expansion should
3739 * be inserted like a normal character.
3740 */
3741/*ARGSUSED*/
3742 static int
3743showmatches(xp, wildmenu)
3744 expand_T *xp;
3745 int wildmenu;
3746{
3747#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3748 int num_files;
3749 char_u **files_found;
3750 int i, j, k;
3751 int maxlen;
3752 int lines;
3753 int columns;
3754 char_u *p;
3755 int lastlen;
3756 int attr;
3757 int showtail;
3758
3759 if (xp->xp_numfiles == -1)
3760 {
3761 set_expand_context(xp);
3762 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3763 &num_files, &files_found);
3764 showtail = expand_showtail(xp);
3765 if (i != EXPAND_OK)
3766 return i;
3767
3768 }
3769 else
3770 {
3771 num_files = xp->xp_numfiles;
3772 files_found = xp->xp_files;
3773 showtail = cmd_showtail;
3774 }
3775
3776#ifdef FEAT_WILDMENU
3777 if (!wildmenu)
3778 {
3779#endif
3780 msg_didany = FALSE; /* lines_left will be set */
3781 msg_start(); /* prepare for paging */
3782 msg_putchar('\n');
3783 out_flush();
3784 cmdline_row = msg_row;
3785 msg_didany = FALSE; /* lines_left will be set again */
3786 msg_start(); /* prepare for paging */
3787#ifdef FEAT_WILDMENU
3788 }
3789#endif
3790
3791 if (got_int)
3792 got_int = FALSE; /* only int. the completion, not the cmd line */
3793#ifdef FEAT_WILDMENU
3794 else if (wildmenu)
3795 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3796#endif
3797 else
3798 {
3799 /* find the length of the longest file name */
3800 maxlen = 0;
3801 for (i = 0; i < num_files; ++i)
3802 {
3803 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003804 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 || xp->xp_context == EXPAND_BUFFERS))
3806 {
3807 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3808 j = vim_strsize(NameBuff);
3809 }
3810 else
3811 j = vim_strsize(L_SHOWFILE(i));
3812 if (j > maxlen)
3813 maxlen = j;
3814 }
3815
3816 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3817 lines = num_files;
3818 else
3819 {
3820 /* compute the number of columns and lines for the listing */
3821 maxlen += 2; /* two spaces between file names */
3822 columns = ((int)Columns + 2) / maxlen;
3823 if (columns < 1)
3824 columns = 1;
3825 lines = (num_files + columns - 1) / columns;
3826 }
3827
3828 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3829
3830 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3831 {
3832 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3833 msg_clr_eos();
3834 msg_advance(maxlen - 3);
3835 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3836 }
3837
3838 /* list the files line by line */
3839 for (i = 0; i < lines; ++i)
3840 {
3841 lastlen = 999;
3842 for (k = i; k < num_files; k += lines)
3843 {
3844 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3845 {
3846 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3847 p = files_found[k] + STRLEN(files_found[k]) + 1;
3848 msg_advance(maxlen + 1);
3849 msg_puts(p);
3850 msg_advance(maxlen + 3);
3851 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3852 break;
3853 }
3854 for (j = maxlen - lastlen; --j >= 0; )
3855 msg_putchar(' ');
3856 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003857 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 || xp->xp_context == EXPAND_BUFFERS)
3859 {
3860 /* highlight directories */
3861 j = (mch_isdir(files_found[k]));
3862 if (showtail)
3863 p = L_SHOWFILE(k);
3864 else
3865 {
3866 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3867 TRUE);
3868 p = NameBuff;
3869 }
3870 }
3871 else
3872 {
3873 j = FALSE;
3874 p = L_SHOWFILE(k);
3875 }
3876 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3877 }
3878 if (msg_col > 0) /* when not wrapped around */
3879 {
3880 msg_clr_eos();
3881 msg_putchar('\n');
3882 }
3883 out_flush(); /* show one line at a time */
3884 if (got_int)
3885 {
3886 got_int = FALSE;
3887 break;
3888 }
3889 }
3890
3891 /*
3892 * we redraw the command below the lines that we have just listed
3893 * This is a bit tricky, but it saves a lot of screen updating.
3894 */
3895 cmdline_row = msg_row; /* will put it back later */
3896 }
3897
3898 if (xp->xp_numfiles == -1)
3899 FreeWild(num_files, files_found);
3900
3901 return EXPAND_OK;
3902}
3903
3904/*
3905 * Private gettail for showmatches() (and win_redr_status_matches()):
3906 * Find tail of file name path, but ignore trailing "/".
3907 */
3908 char_u *
3909sm_gettail(s)
3910 char_u *s;
3911{
3912 char_u *p;
3913 char_u *t = s;
3914 int had_sep = FALSE;
3915
3916 for (p = s; *p != NUL; )
3917 {
3918 if (vim_ispathsep(*p)
3919#ifdef BACKSLASH_IN_FILENAME
3920 && !rem_backslash(p)
3921#endif
3922 )
3923 had_sep = TRUE;
3924 else if (had_sep)
3925 {
3926 t = p;
3927 had_sep = FALSE;
3928 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003929 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931 return t;
3932}
3933
3934/*
3935 * Return TRUE if we only need to show the tail of completion matches.
3936 * When not completing file names or there is a wildcard in the path FALSE is
3937 * returned.
3938 */
3939 static int
3940expand_showtail(xp)
3941 expand_T *xp;
3942{
3943 char_u *s;
3944 char_u *end;
3945
3946 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003947 if (xp->xp_context != EXPAND_FILES
3948 && xp->xp_context != EXPAND_SHELLCMD
3949 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 return FALSE;
3951
3952 end = gettail(xp->xp_pattern);
3953 if (end == xp->xp_pattern) /* there is no path separator */
3954 return FALSE;
3955
3956 for (s = xp->xp_pattern; s < end; s++)
3957 {
3958 /* Skip escaped wildcards. Only when the backslash is not a path
3959 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3960 if (rem_backslash(s))
3961 ++s;
3962 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3963 return FALSE;
3964 }
3965 return TRUE;
3966}
3967
3968/*
3969 * Prepare a string for expansion.
3970 * When expanding file names: The string will be used with expand_wildcards().
3971 * Copy the file name into allocated memory and add a '*' at the end.
3972 * When expanding other names: The string will be used with regcomp(). Copy
3973 * the name into allocated memory and prepend "^".
3974 */
3975 char_u *
3976addstar(fname, len, context)
3977 char_u *fname;
3978 int len;
3979 int context; /* EXPAND_FILES etc. */
3980{
3981 char_u *retval;
3982 int i, j;
3983 int new_len;
3984 char_u *tail;
3985
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003986 if (context != EXPAND_FILES
3987 && context != EXPAND_SHELLCMD
3988 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 {
3990 /*
3991 * Matching will be done internally (on something other than files).
3992 * So we convert the file-matching-type wildcards into our kind for
3993 * use with vim_regcomp(). First work out how long it will be:
3994 */
3995
3996 /* For help tags the translation is done in find_help_tags().
3997 * For a tag pattern starting with "/" no translation is needed. */
3998 if (context == EXPAND_HELP
3999 || context == EXPAND_COLORS
4000 || context == EXPAND_COMPILER
4001 || (context == EXPAND_TAGS && fname[0] == '/'))
4002 retval = vim_strnsave(fname, len);
4003 else
4004 {
4005 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4006 for (i = 0; i < len; i++)
4007 {
4008 if (fname[i] == '*' || fname[i] == '~')
4009 new_len++; /* '*' needs to be replaced by ".*"
4010 '~' needs to be replaced by "\~" */
4011
4012 /* Buffer names are like file names. "." should be literal */
4013 if (context == EXPAND_BUFFERS && fname[i] == '.')
4014 new_len++; /* "." becomes "\." */
4015
4016 /* Custom expansion takes care of special things, match
4017 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004018 if ((context == EXPAND_USER_DEFINED
4019 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 new_len++; /* '\' becomes "\\" */
4021 }
4022 retval = alloc(new_len);
4023 if (retval != NULL)
4024 {
4025 retval[0] = '^';
4026 j = 1;
4027 for (i = 0; i < len; i++, j++)
4028 {
4029 /* Skip backslash. But why? At least keep it for custom
4030 * expansion. */
4031 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004032 && context != EXPAND_USER_LIST
4033 && fname[i] == '\\'
4034 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 break;
4036
4037 switch (fname[i])
4038 {
4039 case '*': retval[j++] = '.';
4040 break;
4041 case '~': retval[j++] = '\\';
4042 break;
4043 case '?': retval[j] = '.';
4044 continue;
4045 case '.': if (context == EXPAND_BUFFERS)
4046 retval[j++] = '\\';
4047 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004048 case '\\': if (context == EXPAND_USER_DEFINED
4049 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 retval[j++] = '\\';
4051 break;
4052 }
4053 retval[j] = fname[i];
4054 }
4055 retval[j] = NUL;
4056 }
4057 }
4058 }
4059 else
4060 {
4061 retval = alloc(len + 4);
4062 if (retval != NULL)
4063 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004064 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004067 * Don't add a star to *, ~, ~user, $var or `cmd`.
4068 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 * ~ would be at the start of the file name, but not the tail.
4070 * $ could be anywhere in the tail.
4071 * ` could be anywhere in the file name.
4072 */
4073 tail = gettail(retval);
4074 if ((*retval != '~' || tail != retval)
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004075 && (len == 0 || retval[len - 1] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 && vim_strchr(tail, '$') == NULL
4077 && vim_strchr(retval, '`') == NULL)
4078 retval[len++] = '*';
4079 retval[len] = NUL;
4080 }
4081 }
4082 return retval;
4083}
4084
4085/*
4086 * Must parse the command line so far to work out what context we are in.
4087 * Completion can then be done based on that context.
4088 * This routine sets the variables:
4089 * xp->xp_pattern The start of the pattern to be expanded within
4090 * the command line (ends at the cursor).
4091 * xp->xp_context The type of thing to expand. Will be one of:
4092 *
4093 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4094 * the command line, like an unknown command. Caller
4095 * should beep.
4096 * EXPAND_NOTHING Unrecognised context for completion, use char like
4097 * a normal char, rather than for completion. eg
4098 * :s/^I/
4099 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4100 * it.
4101 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4102 * EXPAND_FILES After command with XFILE set, or after setting
4103 * with P_EXPAND set. eg :e ^I, :w>>^I
4104 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4105 * when we know only directories are of interest. eg
4106 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004107 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4109 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4110 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4111 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4112 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4113 * EXPAND_EVENTS Complete event names
4114 * EXPAND_SYNTAX Complete :syntax command arguments
4115 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4116 * EXPAND_AUGROUP Complete autocommand group names
4117 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4118 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4119 * eg :unmap a^I , :cunab x^I
4120 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4121 * eg :call sub^I
4122 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4123 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4124 * names in expressions, eg :while s^I
4125 * EXPAND_ENV_VARS Complete environment variable names
4126 */
4127 static void
4128set_expand_context(xp)
4129 expand_T *xp;
4130{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004131 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (ccline.cmdfirstc != ':'
4133#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004134 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004135 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136#endif
4137 )
4138 {
4139 xp->xp_context = EXPAND_NOTHING;
4140 return;
4141 }
4142 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4143}
4144
4145 void
4146set_cmd_context(xp, str, len, col)
4147 expand_T *xp;
4148 char_u *str; /* start of command line */
4149 int len; /* length of command line (excl. NUL) */
4150 int col; /* position of cursor */
4151{
4152 int old_char = NUL;
4153 char_u *nextcomm;
4154
4155 /*
4156 * Avoid a UMR warning from Purify, only save the character if it has been
4157 * written before.
4158 */
4159 if (col < len)
4160 old_char = str[col];
4161 str[col] = NUL;
4162 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004163
4164#ifdef FEAT_EVAL
4165 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004166 {
4167# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004168 /* pass CMD_SIZE because there is no real command */
4169 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004170# endif
4171 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004172 else if (ccline.input_fn)
4173 {
4174 xp->xp_context = ccline.xp_context;
4175 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004176# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004177 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004178# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004179 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004180 else
4181#endif
4182 while (nextcomm != NULL)
4183 nextcomm = set_one_cmd_context(xp, nextcomm);
4184
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 str[col] = old_char;
4186}
4187
4188/*
4189 * Expand the command line "str" from context "xp".
4190 * "xp" must have been set by set_cmd_context().
4191 * xp->xp_pattern points into "str", to where the text that is to be expanded
4192 * starts.
4193 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4194 * cursor.
4195 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4196 * key that triggered expansion literally.
4197 * Returns EXPAND_OK otherwise.
4198 */
4199 int
4200expand_cmdline(xp, str, col, matchcount, matches)
4201 expand_T *xp;
4202 char_u *str; /* start of command line */
4203 int col; /* position of cursor */
4204 int *matchcount; /* return: nr of matches */
4205 char_u ***matches; /* return: array of pointers to matches */
4206{
4207 char_u *file_str = NULL;
4208
4209 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4210 {
4211 beep_flush();
4212 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4213 }
4214 if (xp->xp_context == EXPAND_NOTHING)
4215 {
4216 /* Caller can use the character as a normal char instead */
4217 return EXPAND_NOTHING;
4218 }
4219
4220 /* add star to file name, or convert to regexp if not exp. files. */
4221 file_str = addstar(xp->xp_pattern,
4222 (int)(str + col - xp->xp_pattern), xp->xp_context);
4223 if (file_str == NULL)
4224 return EXPAND_UNSUCCESSFUL;
4225
4226 /* find all files that match the description */
4227 if (ExpandFromContext(xp, file_str, matchcount, matches,
4228 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4229 {
4230 *matchcount = 0;
4231 *matches = NULL;
4232 }
4233 vim_free(file_str);
4234
4235 return EXPAND_OK;
4236}
4237
4238#ifdef FEAT_MULTI_LANG
4239/*
4240 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4241 */
4242static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4243
4244 static void
4245cleanup_help_tags(num_file, file)
4246 int num_file;
4247 char_u **file;
4248{
4249 int i, j;
4250 int len;
4251
4252 for (i = 0; i < num_file; ++i)
4253 {
4254 len = (int)STRLEN(file[i]) - 3;
4255 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4256 {
4257 /* Sorting on priority means the same item in another language may
4258 * be anywhere. Search all items for a match up to the "@en". */
4259 for (j = 0; j < num_file; ++j)
4260 if (j != i
4261 && (int)STRLEN(file[j]) == len + 3
4262 && STRNCMP(file[i], file[j], len + 1) == 0)
4263 break;
4264 if (j == num_file)
4265 file[i][len] = NUL;
4266 }
4267 }
4268}
4269#endif
4270
4271/*
4272 * Do the expansion based on xp->xp_context and "pat".
4273 */
4274 static int
4275ExpandFromContext(xp, pat, num_file, file, options)
4276 expand_T *xp;
4277 char_u *pat;
4278 int *num_file;
4279 char_u ***file;
4280 int options;
4281{
4282#ifdef FEAT_CMDL_COMPL
4283 regmatch_T regmatch;
4284#endif
4285 int ret;
4286 int flags;
4287
4288 flags = EW_DIR; /* include directories */
4289 if (options & WILD_LIST_NOTFOUND)
4290 flags |= EW_NOTFOUND;
4291 if (options & WILD_ADD_SLASH)
4292 flags |= EW_ADDSLASH;
4293 if (options & WILD_KEEP_ALL)
4294 flags |= EW_KEEPALL;
4295 if (options & WILD_SILENT)
4296 flags |= EW_SILENT;
4297
4298 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4299 {
4300 /*
4301 * Expand file or directory names.
4302 */
4303 int free_pat = FALSE;
4304 int i;
4305
4306 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4307 * space */
4308 if (xp->xp_backslash != XP_BS_NONE)
4309 {
4310 free_pat = TRUE;
4311 pat = vim_strsave(pat);
4312 for (i = 0; pat[i]; ++i)
4313 if (pat[i] == '\\')
4314 {
4315 if (xp->xp_backslash == XP_BS_THREE
4316 && pat[i + 1] == '\\'
4317 && pat[i + 2] == '\\'
4318 && pat[i + 3] == ' ')
Bram Moolenaar452a81b2007-08-06 20:28:43 +00004319 mch_memmove(pat + i, pat + i + 3,
4320 STRLEN(pat + i + 3) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 if (xp->xp_backslash == XP_BS_ONE
4322 && pat[i + 1] == ' ')
Bram Moolenaar452a81b2007-08-06 20:28:43 +00004323 mch_memmove(pat + i, pat + i + 1, STRLEN(pat + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 }
4325 }
4326
4327 if (xp->xp_context == EXPAND_FILES)
4328 flags |= EW_FILE;
4329 else
4330 flags = (flags | EW_DIR) & ~EW_FILE;
4331 ret = expand_wildcards(1, &pat, num_file, file, flags);
4332 if (free_pat)
4333 vim_free(pat);
4334 return ret;
4335 }
4336
4337 *file = (char_u **)"";
4338 *num_file = 0;
4339 if (xp->xp_context == EXPAND_HELP)
4340 {
4341 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4342 {
4343#ifdef FEAT_MULTI_LANG
4344 cleanup_help_tags(*num_file, *file);
4345#endif
4346 return OK;
4347 }
4348 return FAIL;
4349 }
4350
4351#ifndef FEAT_CMDL_COMPL
4352 return FAIL;
4353#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004354 if (xp->xp_context == EXPAND_SHELLCMD)
4355 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 if (xp->xp_context == EXPAND_OLD_SETTING)
4357 return ExpandOldSetting(num_file, file);
4358 if (xp->xp_context == EXPAND_BUFFERS)
4359 return ExpandBufnames(pat, num_file, file, options);
4360 if (xp->xp_context == EXPAND_TAGS
4361 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4362 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4363 if (xp->xp_context == EXPAND_COLORS)
4364 return ExpandRTDir(pat, num_file, file, "colors");
4365 if (xp->xp_context == EXPAND_COMPILER)
4366 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004367# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4368 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004369 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371
4372 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4373 if (regmatch.regprog == NULL)
4374 return FAIL;
4375
4376 /* set ignore-case according to p_ic, p_scs and pat */
4377 regmatch.rm_ic = ignorecase(pat);
4378
4379 if (xp->xp_context == EXPAND_SETTINGS
4380 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4381 ret = ExpandSettings(xp, &regmatch, num_file, file);
4382 else if (xp->xp_context == EXPAND_MAPPINGS)
4383 ret = ExpandMappings(&regmatch, num_file, file);
4384# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4385 else if (xp->xp_context == EXPAND_USER_DEFINED)
4386 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4387# endif
4388 else
4389 {
4390 static struct expgen
4391 {
4392 int context;
4393 char_u *((*func)__ARGS((expand_T *, int)));
4394 int ic;
4395 } tab[] =
4396 {
4397 {EXPAND_COMMANDS, get_command_name, FALSE},
4398#ifdef FEAT_USR_CMDS
4399 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4400 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4401 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4402 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4403#endif
4404#ifdef FEAT_EVAL
4405 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4406 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4407 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4408 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4409#endif
4410#ifdef FEAT_MENU
4411 {EXPAND_MENUS, get_menu_name, FALSE},
4412 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4413#endif
4414#ifdef FEAT_SYN_HL
4415 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4416#endif
4417 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4418#ifdef FEAT_AUTOCMD
4419 {EXPAND_EVENTS, get_event_name, TRUE},
4420 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4421#endif
4422#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4423 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4424 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4425#endif
4426 {EXPAND_ENV_VARS, get_env_name, TRUE},
4427 };
4428 int i;
4429
4430 /*
4431 * Find a context in the table and call the ExpandGeneric() with the
4432 * right function to do the expansion.
4433 */
4434 ret = FAIL;
4435 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4436 if (xp->xp_context == tab[i].context)
4437 {
4438 if (tab[i].ic)
4439 regmatch.rm_ic = TRUE;
4440 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4441 break;
4442 }
4443 }
4444
4445 vim_free(regmatch.regprog);
4446
4447 return ret;
4448#endif /* FEAT_CMDL_COMPL */
4449}
4450
4451#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4452/*
4453 * Expand a list of names.
4454 *
4455 * Generic function for command line completion. It calls a function to
4456 * obtain strings, one by one. The strings are matched against a regexp
4457 * program. Matching strings are copied into an array, which is returned.
4458 *
4459 * Returns OK when no problems encountered, FAIL for error (out of memory).
4460 */
4461 int
4462ExpandGeneric(xp, regmatch, num_file, file, func)
4463 expand_T *xp;
4464 regmatch_T *regmatch;
4465 int *num_file;
4466 char_u ***file;
4467 char_u *((*func)__ARGS((expand_T *, int)));
4468 /* returns a string from the list */
4469{
4470 int i;
4471 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004472 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 char_u *str;
4474
4475 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004476 * round == 0: count the number of matching names
4477 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004479 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
4481 for (i = 0; ; ++i)
4482 {
4483 str = (*func)(xp, i);
4484 if (str == NULL) /* end of list */
4485 break;
4486 if (*str == NUL) /* skip empty strings */
4487 continue;
4488
4489 if (vim_regexec(regmatch, str, (colnr_T)0))
4490 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004491 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
4493 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4494 (*file)[count] = str;
4495#ifdef FEAT_MENU
4496 if (func == get_menu_names && str != NULL)
4497 {
4498 /* test for separator added by get_menu_names() */
4499 str += STRLEN(str) - 1;
4500 if (*str == '\001')
4501 *str = '.';
4502 }
4503#endif
4504 }
4505 ++count;
4506 }
4507 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004508 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
4510 if (count == 0)
4511 return OK;
4512 *num_file = count;
4513 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4514 if (*file == NULL)
4515 {
4516 *file = (char_u **)"";
4517 return FAIL;
4518 }
4519 count = 0;
4520 }
4521 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004522
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004523 /* Sort the results. Keep menu's in the specified order. */
4524 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4525 sort_strings(*file, *num_file);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004526
Bram Moolenaar4f688582007-07-24 12:34:30 +00004527#ifdef FEAT_CMDL_COMPL
4528 /* Reset the variables used for special highlight names expansion, so that
4529 * they don't show up when getting normal highlight names by ID. */
4530 reset_expand_highlight();
4531#endif
4532
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 return OK;
4534}
4535
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004536/*
4537 * Complete a shell command.
4538 * Returns FAIL or OK;
4539 */
4540 static int
4541expand_shellcmd(filepat, num_file, file, flagsarg)
4542 char_u *filepat; /* pattern to match with command names */
4543 int *num_file; /* return: number of matches */
4544 char_u ***file; /* return: array with matches */
4545 int flagsarg; /* EW_ flags */
4546{
4547 char_u *pat;
4548 int i;
4549 char_u *path;
4550 int mustfree = FALSE;
4551 garray_T ga;
4552 char_u *buf = alloc(MAXPATHL);
4553 size_t l;
4554 char_u *s, *e;
4555 int flags = flagsarg;
4556 int ret;
4557
4558 if (buf == NULL)
4559 return FAIL;
4560
4561 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4562 * space */
4563 pat = vim_strsave(filepat);
4564 for (i = 0; pat[i]; ++i)
4565 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar452a81b2007-08-06 20:28:43 +00004566 mch_memmove(pat + i, pat + i + 1, STRLEN(pat + i));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004567
4568 flags |= EW_FILE | EW_EXEC;
4569
4570 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004571 if (mch_isFullName(pat))
4572 path = (char_u *)" ";
4573 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004574 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4575 path = (char_u *)".";
4576 else
4577 path = vim_getenv((char_u *)"PATH", &mustfree);
4578
4579 /*
4580 * Go over all directories in $PATH. Expand matches in that directory and
4581 * collect them in "ga".
4582 */
4583 ga_init2(&ga, (int)sizeof(char *), 10);
4584 for (s = path; *s != NUL; s = e)
4585 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004586 if (*s == ' ')
4587 ++s; /* Skip space used for absolute path name. */
4588
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004589#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4590 e = vim_strchr(s, ';');
4591#else
4592 e = vim_strchr(s, ':');
4593#endif
4594 if (e == NULL)
4595 e = s + STRLEN(s);
4596
4597 l = e - s;
4598 if (l > MAXPATHL - 5)
4599 break;
4600 vim_strncpy(buf, s, l);
4601 add_pathsep(buf);
4602 l = STRLEN(buf);
4603 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4604
4605 /* Expand matches in one directory of $PATH. */
4606 ret = expand_wildcards(1, &buf, num_file, file, flags);
4607 if (ret == OK)
4608 {
4609 if (ga_grow(&ga, *num_file) == FAIL)
4610 FreeWild(*num_file, *file);
4611 else
4612 {
4613 for (i = 0; i < *num_file; ++i)
4614 {
4615 s = (*file)[i];
4616 if (STRLEN(s) > l)
4617 {
4618 /* Remove the path again. */
4619 mch_memmove(s, s + l, STRLEN(s + l) + 1);
4620 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4621 }
4622 else
4623 vim_free(s);
4624 }
4625 vim_free(*file);
4626 }
4627 }
4628 if (*e != NUL)
4629 ++e;
4630 }
4631 *file = ga.ga_data;
4632 *num_file = ga.ga_len;
4633
4634 vim_free(buf);
4635 vim_free(pat);
4636 if (mustfree)
4637 vim_free(path);
4638 return OK;
4639}
4640
4641
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004643static 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));
4644
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004646 * call "user_expand_func()" to invoke a user defined VimL function and return
4647 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004649 static void *
4650call_user_expand_func(user_expand_func, xp, num_file, file)
4651 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 int *num_file;
4654 char_u ***file;
4655{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004656 char_u keep;
4657 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004660 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004661 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662
4663 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004664 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 *num_file = 0;
4666 *file = NULL;
4667
4668 keep = ccline.cmdbuff[ccline.cmdlen];
4669 ccline.cmdbuff[ccline.cmdlen] = 0;
4670 sprintf((char *)num, "%d", ccline.cmdpos);
4671 args[0] = xp->xp_pattern;
4672 args[1] = ccline.cmdbuff;
4673 args[2] = num;
4674
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004675 /* Save the cmdline, we don't know what the function may do. */
4676 save_ccline = ccline;
4677 ccline.cmdbuff = NULL;
4678 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004680
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004681 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004682
4683 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004685
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004687
4688 return ret;
4689}
4690
4691/*
4692 * Expand names with a function defined by the user.
4693 */
4694 static int
4695ExpandUserDefined(xp, regmatch, num_file, file)
4696 expand_T *xp;
4697 regmatch_T *regmatch;
4698 int *num_file;
4699 char_u ***file;
4700{
4701 char_u *retstr;
4702 char_u *s;
4703 char_u *e;
4704 char_u keep;
4705 garray_T ga;
4706
4707 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4708 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 return FAIL;
4710
4711 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004712 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
4714 e = vim_strchr(s, '\n');
4715 if (e == NULL)
4716 e = s + STRLEN(s);
4717 keep = *e;
4718 *e = 0;
4719
4720 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4721 {
4722 *e = keep;
4723 if (*e != NUL)
4724 ++e;
4725 continue;
4726 }
4727
4728 if (ga_grow(&ga, 1) == FAIL)
4729 break;
4730
4731 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4732 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733
4734 *e = keep;
4735 if (*e != NUL)
4736 ++e;
4737 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004738 vim_free(retstr);
4739 *file = ga.ga_data;
4740 *num_file = ga.ga_len;
4741 return OK;
4742}
4743
4744/*
4745 * Expand names with a list returned by a function defined by the user.
4746 */
4747 static int
4748ExpandUserList(xp, num_file, file)
4749 expand_T *xp;
4750 int *num_file;
4751 char_u ***file;
4752{
4753 list_T *retlist;
4754 listitem_T *li;
4755 garray_T ga;
4756
4757 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4758 if (retlist == NULL)
4759 return FAIL;
4760
4761 ga_init2(&ga, (int)sizeof(char *), 3);
4762 /* Loop over the items in the list. */
4763 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4764 {
4765 if (li->li_tv.v_type != VAR_STRING)
4766 continue; /* Skip non-string items */
4767
4768 if (ga_grow(&ga, 1) == FAIL)
4769 break;
4770
4771 ((char_u **)ga.ga_data)[ga.ga_len] =
4772 vim_strsave(li->li_tv.vval.v_string);
4773 ++ga.ga_len;
4774 }
4775 list_unref(retlist);
4776
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 *file = ga.ga_data;
4778 *num_file = ga.ga_len;
4779 return OK;
4780}
4781#endif
4782
4783/*
4784 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4785 * or compiler names.
4786 */
4787 static int
4788ExpandRTDir(pat, num_file, file, dirname)
4789 char_u *pat;
4790 int *num_file;
4791 char_u ***file;
4792 char *dirname; /* "colors" or "compiler" */
4793{
4794 char_u *all;
4795 char_u *s;
4796 char_u *e;
4797 garray_T ga;
4798
4799 *num_file = 0;
4800 *file = NULL;
4801 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4802 if (s == NULL)
4803 return FAIL;
4804 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4805 all = globpath(p_rtp, s);
4806 vim_free(s);
4807 if (all == NULL)
4808 return FAIL;
4809
4810 ga_init2(&ga, (int)sizeof(char *), 3);
4811 for (s = all; *s != NUL; s = e)
4812 {
4813 e = vim_strchr(s, '\n');
4814 if (e == NULL)
4815 e = s + STRLEN(s);
4816 if (ga_grow(&ga, 1) == FAIL)
4817 break;
4818 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4819 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004820 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 if (*s == '\n' || vim_ispathsep(*s))
4822 break;
4823 ++s;
4824 ((char_u **)ga.ga_data)[ga.ga_len] =
4825 vim_strnsave(s, (int)(e - s - 4));
4826 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 }
4828 if (*e != NUL)
4829 ++e;
4830 }
4831 vim_free(all);
4832 *file = ga.ga_data;
4833 *num_file = ga.ga_len;
4834 return OK;
4835}
4836
4837#endif
4838
4839#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4840/*
4841 * Expand "file" for all comma-separated directories in "path".
4842 * Returns an allocated string with all matches concatenated, separated by
4843 * newlines. Returns NULL for an error or no matches.
4844 */
4845 char_u *
4846globpath(path, file)
4847 char_u *path;
4848 char_u *file;
4849{
4850 expand_T xpc;
4851 char_u *buf;
4852 garray_T ga;
4853 int i;
4854 int len;
4855 int num_p;
4856 char_u **p;
4857 char_u *cur = NULL;
4858
4859 buf = alloc(MAXPATHL);
4860 if (buf == NULL)
4861 return NULL;
4862
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004863 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004865
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 ga_init2(&ga, 1, 100);
4867
4868 /* Loop over all entries in {path}. */
4869 while (*path != NUL)
4870 {
4871 /* Copy one item of the path to buf[] and concatenate the file name. */
4872 copy_option_part(&path, buf, MAXPATHL, ",");
4873 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4874 {
4875 add_pathsep(buf);
4876 STRCAT(buf, file);
4877 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4878 && num_p > 0)
4879 {
4880 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4881 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004882 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 /* Concatenate new results to previous ones. */
4885 if (ga_grow(&ga, len) == OK)
4886 {
4887 cur = (char_u *)ga.ga_data + ga.ga_len;
4888 for (i = 0; i < num_p; ++i)
4889 {
4890 STRCPY(cur, p[i]);
4891 cur += STRLEN(p[i]);
4892 *cur++ = '\n';
4893 }
4894 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 }
4896 FreeWild(num_p, p);
4897 }
4898 }
4899 }
4900 if (cur != NULL)
4901 *--cur = 0; /* Replace trailing newline with NUL */
4902
4903 vim_free(buf);
4904 return (char_u *)ga.ga_data;
4905}
4906
4907#endif
4908
4909#if defined(FEAT_CMDHIST) || defined(PROTO)
4910
4911/*********************************
4912 * Command line history stuff *
4913 *********************************/
4914
4915/*
4916 * Translate a history character to the associated type number.
4917 */
4918 static int
4919hist_char2type(c)
4920 int c;
4921{
4922 if (c == ':')
4923 return HIST_CMD;
4924 if (c == '=')
4925 return HIST_EXPR;
4926 if (c == '@')
4927 return HIST_INPUT;
4928 if (c == '>')
4929 return HIST_DEBUG;
4930 return HIST_SEARCH; /* must be '?' or '/' */
4931}
4932
4933/*
4934 * Table of history names.
4935 * These names are used in :history and various hist...() functions.
4936 * It is sufficient to give the significant prefix of a history name.
4937 */
4938
4939static char *(history_names[]) =
4940{
4941 "cmd",
4942 "search",
4943 "expr",
4944 "input",
4945 "debug",
4946 NULL
4947};
4948
4949/*
4950 * init_history() - Initialize the command line history.
4951 * Also used to re-allocate the history when the size changes.
4952 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004953 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954init_history()
4955{
4956 int newlen; /* new length of history table */
4957 histentry_T *temp;
4958 int i;
4959 int j;
4960 int type;
4961
4962 /*
4963 * If size of history table changed, reallocate it
4964 */
4965 newlen = (int)p_hi;
4966 if (newlen != hislen) /* history length changed */
4967 {
4968 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4969 {
4970 if (newlen)
4971 {
4972 temp = (histentry_T *)lalloc(
4973 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4974 if (temp == NULL) /* out of memory! */
4975 {
4976 if (type == 0) /* first one: just keep the old length */
4977 {
4978 newlen = hislen;
4979 break;
4980 }
4981 /* Already changed one table, now we can only have zero
4982 * length for all tables. */
4983 newlen = 0;
4984 type = -1;
4985 continue;
4986 }
4987 }
4988 else
4989 temp = NULL;
4990 if (newlen == 0 || temp != NULL)
4991 {
4992 if (hisidx[type] < 0) /* there are no entries yet */
4993 {
4994 for (i = 0; i < newlen; ++i)
4995 {
4996 temp[i].hisnum = 0;
4997 temp[i].hisstr = NULL;
4998 }
4999 }
5000 else if (newlen > hislen) /* array becomes bigger */
5001 {
5002 for (i = 0; i <= hisidx[type]; ++i)
5003 temp[i] = history[type][i];
5004 j = i;
5005 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5006 {
5007 temp[i].hisnum = 0;
5008 temp[i].hisstr = NULL;
5009 }
5010 for ( ; j < hislen; ++i, ++j)
5011 temp[i] = history[type][j];
5012 }
5013 else /* array becomes smaller or 0 */
5014 {
5015 j = hisidx[type];
5016 for (i = newlen - 1; ; --i)
5017 {
5018 if (i >= 0) /* copy newest entries */
5019 temp[i] = history[type][j];
5020 else /* remove older entries */
5021 vim_free(history[type][j].hisstr);
5022 if (--j < 0)
5023 j = hislen - 1;
5024 if (j == hisidx[type])
5025 break;
5026 }
5027 hisidx[type] = newlen - 1;
5028 }
5029 vim_free(history[type]);
5030 history[type] = temp;
5031 }
5032 }
5033 hislen = newlen;
5034 }
5035}
5036
5037/*
5038 * Check if command line 'str' is already in history.
5039 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5040 */
5041 static int
5042in_history(type, str, move_to_front)
5043 int type;
5044 char_u *str;
5045 int move_to_front; /* Move the entry to the front if it exists */
5046{
5047 int i;
5048 int last_i = -1;
5049
5050 if (hisidx[type] < 0)
5051 return FALSE;
5052 i = hisidx[type];
5053 do
5054 {
5055 if (history[type][i].hisstr == NULL)
5056 return FALSE;
5057 if (STRCMP(str, history[type][i].hisstr) == 0)
5058 {
5059 if (!move_to_front)
5060 return TRUE;
5061 last_i = i;
5062 break;
5063 }
5064 if (--i < 0)
5065 i = hislen - 1;
5066 } while (i != hisidx[type]);
5067
5068 if (last_i >= 0)
5069 {
5070 str = history[type][i].hisstr;
5071 while (i != hisidx[type])
5072 {
5073 if (++i >= hislen)
5074 i = 0;
5075 history[type][last_i] = history[type][i];
5076 last_i = i;
5077 }
5078 history[type][i].hisstr = str;
5079 history[type][i].hisnum = ++hisnum[type];
5080 return TRUE;
5081 }
5082 return FALSE;
5083}
5084
5085/*
5086 * Convert history name (from table above) to its HIST_ equivalent.
5087 * When "name" is empty, return "cmd" history.
5088 * Returns -1 for unknown history name.
5089 */
5090 int
5091get_histtype(name)
5092 char_u *name;
5093{
5094 int i;
5095 int len = (int)STRLEN(name);
5096
5097 /* No argument: use current history. */
5098 if (len == 0)
5099 return hist_char2type(ccline.cmdfirstc);
5100
5101 for (i = 0; history_names[i] != NULL; ++i)
5102 if (STRNICMP(name, history_names[i], len) == 0)
5103 return i;
5104
5105 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5106 return hist_char2type(name[0]);
5107
5108 return -1;
5109}
5110
5111static int last_maptick = -1; /* last seen maptick */
5112
5113/*
5114 * Add the given string to the given history. If the string is already in the
5115 * history then it is moved to the front. "histype" may be one of he HIST_
5116 * values.
5117 */
5118 void
5119add_to_history(histype, new_entry, in_map, sep)
5120 int histype;
5121 char_u *new_entry;
5122 int in_map; /* consider maptick when inside a mapping */
5123 int sep; /* separator character used (search hist) */
5124{
5125 histentry_T *hisptr;
5126 int len;
5127
5128 if (hislen == 0) /* no history */
5129 return;
5130
5131 /*
5132 * Searches inside the same mapping overwrite each other, so that only
5133 * the last line is kept. Be careful not to remove a line that was moved
5134 * down, only lines that were added.
5135 */
5136 if (histype == HIST_SEARCH && in_map)
5137 {
5138 if (maptick == last_maptick)
5139 {
5140 /* Current line is from the same mapping, remove it */
5141 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5142 vim_free(hisptr->hisstr);
5143 hisptr->hisstr = NULL;
5144 hisptr->hisnum = 0;
5145 --hisnum[histype];
5146 if (--hisidx[HIST_SEARCH] < 0)
5147 hisidx[HIST_SEARCH] = hislen - 1;
5148 }
5149 last_maptick = -1;
5150 }
5151 if (!in_history(histype, new_entry, TRUE))
5152 {
5153 if (++hisidx[histype] == hislen)
5154 hisidx[histype] = 0;
5155 hisptr = &history[histype][hisidx[histype]];
5156 vim_free(hisptr->hisstr);
5157
5158 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005159 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5161 if (hisptr->hisstr != NULL)
5162 hisptr->hisstr[len + 1] = sep;
5163
5164 hisptr->hisnum = ++hisnum[histype];
5165 if (histype == HIST_SEARCH && in_map)
5166 last_maptick = maptick;
5167 }
5168}
5169
5170#if defined(FEAT_EVAL) || defined(PROTO)
5171
5172/*
5173 * Get identifier of newest history entry.
5174 * "histype" may be one of the HIST_ values.
5175 */
5176 int
5177get_history_idx(histype)
5178 int histype;
5179{
5180 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5181 || hisidx[histype] < 0)
5182 return -1;
5183
5184 return history[histype][hisidx[histype]].hisnum;
5185}
5186
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005187static struct cmdline_info *get_ccline_ptr __ARGS((void));
5188
5189/*
5190 * Get pointer to the command line info to use. cmdline_paste() may clear
5191 * ccline and put the previous value in prev_ccline.
5192 */
5193 static struct cmdline_info *
5194get_ccline_ptr()
5195{
5196 if ((State & CMDLINE) == 0)
5197 return NULL;
5198 if (ccline.cmdbuff != NULL)
5199 return &ccline;
5200 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5201 return &prev_ccline;
5202 return NULL;
5203}
5204
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205/*
5206 * Get the current command line in allocated memory.
5207 * Only works when the command line is being edited.
5208 * Returns NULL when something is wrong.
5209 */
5210 char_u *
5211get_cmdline_str()
5212{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005213 struct cmdline_info *p = get_ccline_ptr();
5214
5215 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005217 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218}
5219
5220/*
5221 * Get the current command line position, counted in bytes.
5222 * Zero is the first position.
5223 * Only works when the command line is being edited.
5224 * Returns -1 when something is wrong.
5225 */
5226 int
5227get_cmdline_pos()
5228{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005229 struct cmdline_info *p = get_ccline_ptr();
5230
5231 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005233 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234}
5235
5236/*
5237 * Set the command line byte position to "pos". Zero is the first position.
5238 * Only works when the command line is being edited.
5239 * Returns 1 when failed, 0 when OK.
5240 */
5241 int
5242set_cmdline_pos(pos)
5243 int pos;
5244{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005245 struct cmdline_info *p = get_ccline_ptr();
5246
5247 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 return 1;
5249
5250 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5251 * changed the command line. */
5252 if (pos < 0)
5253 new_cmdpos = 0;
5254 else
5255 new_cmdpos = pos;
5256 return 0;
5257}
5258
5259/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005260 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005261 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005262 * Only works when the command line is being edited.
5263 * Returns NUL when something is wrong.
5264 */
5265 int
5266get_cmdline_type()
5267{
5268 struct cmdline_info *p = get_ccline_ptr();
5269
5270 if (p == NULL)
5271 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005272 if (p->cmdfirstc == NUL)
5273 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005274 return p->cmdfirstc;
5275}
5276
5277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 * Calculate history index from a number:
5279 * num > 0: seen as identifying number of a history entry
5280 * num < 0: relative position in history wrt newest entry
5281 * "histype" may be one of the HIST_ values.
5282 */
5283 static int
5284calc_hist_idx(histype, num)
5285 int histype;
5286 int num;
5287{
5288 int i;
5289 histentry_T *hist;
5290 int wrapped = FALSE;
5291
5292 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5293 || (i = hisidx[histype]) < 0 || num == 0)
5294 return -1;
5295
5296 hist = history[histype];
5297 if (num > 0)
5298 {
5299 while (hist[i].hisnum > num)
5300 if (--i < 0)
5301 {
5302 if (wrapped)
5303 break;
5304 i += hislen;
5305 wrapped = TRUE;
5306 }
5307 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5308 return i;
5309 }
5310 else if (-num <= hislen)
5311 {
5312 i += num + 1;
5313 if (i < 0)
5314 i += hislen;
5315 if (hist[i].hisstr != NULL)
5316 return i;
5317 }
5318 return -1;
5319}
5320
5321/*
5322 * Get a history entry by its index.
5323 * "histype" may be one of the HIST_ values.
5324 */
5325 char_u *
5326get_history_entry(histype, idx)
5327 int histype;
5328 int idx;
5329{
5330 idx = calc_hist_idx(histype, idx);
5331 if (idx >= 0)
5332 return history[histype][idx].hisstr;
5333 else
5334 return (char_u *)"";
5335}
5336
5337/*
5338 * Clear all entries of a history.
5339 * "histype" may be one of the HIST_ values.
5340 */
5341 int
5342clr_history(histype)
5343 int histype;
5344{
5345 int i;
5346 histentry_T *hisptr;
5347
5348 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5349 {
5350 hisptr = history[histype];
5351 for (i = hislen; i--;)
5352 {
5353 vim_free(hisptr->hisstr);
5354 hisptr->hisnum = 0;
5355 hisptr++->hisstr = NULL;
5356 }
5357 hisidx[histype] = -1; /* mark history as cleared */
5358 hisnum[histype] = 0; /* reset identifier counter */
5359 return OK;
5360 }
5361 return FAIL;
5362}
5363
5364/*
5365 * Remove all entries matching {str} from a history.
5366 * "histype" may be one of the HIST_ values.
5367 */
5368 int
5369del_history_entry(histype, str)
5370 int histype;
5371 char_u *str;
5372{
5373 regmatch_T regmatch;
5374 histentry_T *hisptr;
5375 int idx;
5376 int i;
5377 int last;
5378 int found = FALSE;
5379
5380 regmatch.regprog = NULL;
5381 regmatch.rm_ic = FALSE; /* always match case */
5382 if (hislen != 0
5383 && histype >= 0
5384 && histype < HIST_COUNT
5385 && *str != NUL
5386 && (idx = hisidx[histype]) >= 0
5387 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5388 != NULL)
5389 {
5390 i = last = idx;
5391 do
5392 {
5393 hisptr = &history[histype][i];
5394 if (hisptr->hisstr == NULL)
5395 break;
5396 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5397 {
5398 found = TRUE;
5399 vim_free(hisptr->hisstr);
5400 hisptr->hisstr = NULL;
5401 hisptr->hisnum = 0;
5402 }
5403 else
5404 {
5405 if (i != last)
5406 {
5407 history[histype][last] = *hisptr;
5408 hisptr->hisstr = NULL;
5409 hisptr->hisnum = 0;
5410 }
5411 if (--last < 0)
5412 last += hislen;
5413 }
5414 if (--i < 0)
5415 i += hislen;
5416 } while (i != idx);
5417 if (history[histype][idx].hisstr == NULL)
5418 hisidx[histype] = -1;
5419 }
5420 vim_free(regmatch.regprog);
5421 return found;
5422}
5423
5424/*
5425 * Remove an indexed entry from a history.
5426 * "histype" may be one of the HIST_ values.
5427 */
5428 int
5429del_history_idx(histype, idx)
5430 int histype;
5431 int idx;
5432{
5433 int i, j;
5434
5435 i = calc_hist_idx(histype, idx);
5436 if (i < 0)
5437 return FALSE;
5438 idx = hisidx[histype];
5439 vim_free(history[histype][i].hisstr);
5440
5441 /* When deleting the last added search string in a mapping, reset
5442 * last_maptick, so that the last added search string isn't deleted again.
5443 */
5444 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5445 last_maptick = -1;
5446
5447 while (i != idx)
5448 {
5449 j = (i + 1) % hislen;
5450 history[histype][i] = history[histype][j];
5451 i = j;
5452 }
5453 history[histype][i].hisstr = NULL;
5454 history[histype][i].hisnum = 0;
5455 if (--i < 0)
5456 i += hislen;
5457 hisidx[histype] = i;
5458 return TRUE;
5459}
5460
5461#endif /* FEAT_EVAL */
5462
5463#if defined(FEAT_CRYPT) || defined(PROTO)
5464/*
5465 * Very specific function to remove the value in ":set key=val" from the
5466 * history.
5467 */
5468 void
5469remove_key_from_history()
5470{
5471 char_u *p;
5472 int i;
5473
5474 i = hisidx[HIST_CMD];
5475 if (i < 0)
5476 return;
5477 p = history[HIST_CMD][i].hisstr;
5478 if (p != NULL)
5479 for ( ; *p; ++p)
5480 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5481 {
5482 p = vim_strchr(p + 3, '=');
5483 if (p == NULL)
5484 break;
5485 ++p;
5486 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5487 if (p[i] == '\\' && p[i + 1])
5488 ++i;
5489 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5490 --p;
5491 }
5492}
5493#endif
5494
5495#endif /* FEAT_CMDHIST */
5496
5497#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5498/*
5499 * Get indices "num1,num2" that specify a range within a list (not a range of
5500 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5501 * Returns OK if parsed successfully, otherwise FAIL.
5502 */
5503 int
5504get_list_range(str, num1, num2)
5505 char_u **str;
5506 int *num1;
5507 int *num2;
5508{
5509 int len;
5510 int first = FALSE;
5511 long num;
5512
5513 *str = skipwhite(*str);
5514 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5515 {
5516 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5517 *str += len;
5518 *num1 = (int)num;
5519 first = TRUE;
5520 }
5521 *str = skipwhite(*str);
5522 if (**str == ',') /* parse "to" part of range */
5523 {
5524 *str = skipwhite(*str + 1);
5525 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5526 if (len > 0)
5527 {
5528 *num2 = (int)num;
5529 *str = skipwhite(*str + len);
5530 }
5531 else if (!first) /* no number given at all */
5532 return FAIL;
5533 }
5534 else if (first) /* only one number given */
5535 *num2 = *num1;
5536 return OK;
5537}
5538#endif
5539
5540#if defined(FEAT_CMDHIST) || defined(PROTO)
5541/*
5542 * :history command - print a history
5543 */
5544 void
5545ex_history(eap)
5546 exarg_T *eap;
5547{
5548 histentry_T *hist;
5549 int histype1 = HIST_CMD;
5550 int histype2 = HIST_CMD;
5551 int hisidx1 = 1;
5552 int hisidx2 = -1;
5553 int idx;
5554 int i, j, k;
5555 char_u *end;
5556 char_u *arg = eap->arg;
5557
5558 if (hislen == 0)
5559 {
5560 MSG(_("'history' option is zero"));
5561 return;
5562 }
5563
5564 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5565 {
5566 end = arg;
5567 while (ASCII_ISALPHA(*end)
5568 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5569 end++;
5570 i = *end;
5571 *end = NUL;
5572 histype1 = get_histtype(arg);
5573 if (histype1 == -1)
5574 {
5575 if (STRICMP(arg, "all") == 0)
5576 {
5577 histype1 = 0;
5578 histype2 = HIST_COUNT-1;
5579 }
5580 else
5581 {
5582 *end = i;
5583 EMSG(_(e_trailing));
5584 return;
5585 }
5586 }
5587 else
5588 histype2 = histype1;
5589 *end = i;
5590 }
5591 else
5592 end = arg;
5593 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5594 {
5595 EMSG(_(e_trailing));
5596 return;
5597 }
5598
5599 for (; !got_int && histype1 <= histype2; ++histype1)
5600 {
5601 STRCPY(IObuff, "\n # ");
5602 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5603 MSG_PUTS_TITLE(IObuff);
5604 idx = hisidx[histype1];
5605 hist = history[histype1];
5606 j = hisidx1;
5607 k = hisidx2;
5608 if (j < 0)
5609 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5610 if (k < 0)
5611 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5612 if (idx >= 0 && j <= k)
5613 for (i = idx + 1; !got_int; ++i)
5614 {
5615 if (i == hislen)
5616 i = 0;
5617 if (hist[i].hisstr != NULL
5618 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5619 {
5620 msg_putchar('\n');
5621 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5622 hist[i].hisnum);
5623 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5624 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5625 (int)Columns - 10);
5626 else
5627 STRCAT(IObuff, hist[i].hisstr);
5628 msg_outtrans(IObuff);
5629 out_flush();
5630 }
5631 if (i == idx)
5632 break;
5633 }
5634 }
5635}
5636#endif
5637
5638#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5639static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5640static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5641static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5642static int viminfo_add_at_front = FALSE;
5643
5644static int hist_type2char __ARGS((int type, int use_question));
5645
5646/*
5647 * Translate a history type number to the associated character.
5648 */
5649 static int
5650hist_type2char(type, use_question)
5651 int type;
5652 int use_question; /* use '?' instead of '/' */
5653{
5654 if (type == HIST_CMD)
5655 return ':';
5656 if (type == HIST_SEARCH)
5657 {
5658 if (use_question)
5659 return '?';
5660 else
5661 return '/';
5662 }
5663 if (type == HIST_EXPR)
5664 return '=';
5665 return '@';
5666}
5667
5668/*
5669 * Prepare for reading the history from the viminfo file.
5670 * This allocates history arrays to store the read history lines.
5671 */
5672 void
5673prepare_viminfo_history(asklen)
5674 int asklen;
5675{
5676 int i;
5677 int num;
5678 int type;
5679 int len;
5680
5681 init_history();
5682 viminfo_add_at_front = (asklen != 0);
5683 if (asklen > hislen)
5684 asklen = hislen;
5685
5686 for (type = 0; type < HIST_COUNT; ++type)
5687 {
5688 /*
5689 * Count the number of empty spaces in the history list. If there are
5690 * more spaces available than we request, then fill them up.
5691 */
5692 for (i = 0, num = 0; i < hislen; i++)
5693 if (history[type][i].hisstr == NULL)
5694 num++;
5695 len = asklen;
5696 if (num > len)
5697 len = num;
5698 if (len <= 0)
5699 viminfo_history[type] = NULL;
5700 else
5701 viminfo_history[type] =
5702 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5703 if (viminfo_history[type] == NULL)
5704 len = 0;
5705 viminfo_hislen[type] = len;
5706 viminfo_hisidx[type] = 0;
5707 }
5708}
5709
5710/*
5711 * Accept a line from the viminfo, store it in the history array when it's
5712 * new.
5713 */
5714 int
5715read_viminfo_history(virp)
5716 vir_T *virp;
5717{
5718 int type;
5719 long_u len;
5720 char_u *val;
5721 char_u *p;
5722
5723 type = hist_char2type(virp->vir_line[0]);
5724 if (viminfo_hisidx[type] < viminfo_hislen[type])
5725 {
5726 val = viminfo_readstring(virp, 1, TRUE);
5727 if (val != NULL && *val != NUL)
5728 {
5729 if (!in_history(type, val + (type == HIST_SEARCH),
5730 viminfo_add_at_front))
5731 {
5732 /* Need to re-allocate to append the separator byte. */
5733 len = STRLEN(val);
5734 p = lalloc(len + 2, TRUE);
5735 if (p != NULL)
5736 {
5737 if (type == HIST_SEARCH)
5738 {
5739 /* Search entry: Move the separator from the first
5740 * column to after the NUL. */
5741 mch_memmove(p, val + 1, (size_t)len);
5742 p[len] = (*val == ' ' ? NUL : *val);
5743 }
5744 else
5745 {
5746 /* Not a search entry: No separator in the viminfo
5747 * file, add a NUL separator. */
5748 mch_memmove(p, val, (size_t)len + 1);
5749 p[len + 1] = NUL;
5750 }
5751 viminfo_history[type][viminfo_hisidx[type]++] = p;
5752 }
5753 }
5754 }
5755 vim_free(val);
5756 }
5757 return viminfo_readline(virp);
5758}
5759
5760 void
5761finish_viminfo_history()
5762{
5763 int idx;
5764 int i;
5765 int type;
5766
5767 for (type = 0; type < HIST_COUNT; ++type)
5768 {
5769 if (history[type] == NULL)
5770 return;
5771 idx = hisidx[type] + viminfo_hisidx[type];
5772 if (idx >= hislen)
5773 idx -= hislen;
5774 else if (idx < 0)
5775 idx = hislen - 1;
5776 if (viminfo_add_at_front)
5777 hisidx[type] = idx;
5778 else
5779 {
5780 if (hisidx[type] == -1)
5781 hisidx[type] = hislen - 1;
5782 do
5783 {
5784 if (history[type][idx].hisstr != NULL)
5785 break;
5786 if (++idx == hislen)
5787 idx = 0;
5788 } while (idx != hisidx[type]);
5789 if (idx != hisidx[type] && --idx < 0)
5790 idx = hislen - 1;
5791 }
5792 for (i = 0; i < viminfo_hisidx[type]; i++)
5793 {
5794 vim_free(history[type][idx].hisstr);
5795 history[type][idx].hisstr = viminfo_history[type][i];
5796 if (--idx < 0)
5797 idx = hislen - 1;
5798 }
5799 idx += 1;
5800 idx %= hislen;
5801 for (i = 0; i < viminfo_hisidx[type]; i++)
5802 {
5803 history[type][idx++].hisnum = ++hisnum[type];
5804 idx %= hislen;
5805 }
5806 vim_free(viminfo_history[type]);
5807 viminfo_history[type] = NULL;
5808 }
5809}
5810
5811 void
5812write_viminfo_history(fp)
5813 FILE *fp;
5814{
5815 int i;
5816 int type;
5817 int num_saved;
5818 char_u *p;
5819 int c;
5820
5821 init_history();
5822 if (hislen == 0)
5823 return;
5824 for (type = 0; type < HIST_COUNT; ++type)
5825 {
5826 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5827 if (num_saved == 0)
5828 continue;
5829 if (num_saved < 0) /* Use default */
5830 num_saved = hislen;
5831 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5832 type == HIST_CMD ? _("Command Line") :
5833 type == HIST_SEARCH ? _("Search String") :
5834 type == HIST_EXPR ? _("Expression") :
5835 _("Input Line"));
5836 if (num_saved > hislen)
5837 num_saved = hislen;
5838 i = hisidx[type];
5839 if (i >= 0)
5840 while (num_saved--)
5841 {
5842 p = history[type][i].hisstr;
5843 if (p != NULL)
5844 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005845 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 /* For the search history: put the separator in the second
5847 * column; use a space if there isn't one. */
5848 if (type == HIST_SEARCH)
5849 {
5850 c = p[STRLEN(p) + 1];
5851 putc(c == NUL ? ' ' : c, fp);
5852 }
5853 viminfo_writestring(fp, p);
5854 }
5855 if (--i < 0)
5856 i = hislen - 1;
5857 }
5858 }
5859}
5860#endif /* FEAT_VIMINFO */
5861
5862#if defined(FEAT_FKMAP) || defined(PROTO)
5863/*
5864 * Write a character at the current cursor+offset position.
5865 * It is directly written into the command buffer block.
5866 */
5867 void
5868cmd_pchar(c, offset)
5869 int c, offset;
5870{
5871 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5872 {
5873 EMSG(_("E198: cmd_pchar beyond the command length"));
5874 return;
5875 }
5876 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5877 ccline.cmdbuff[ccline.cmdlen] = NUL;
5878}
5879
5880 int
5881cmd_gchar(offset)
5882 int offset;
5883{
5884 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5885 {
5886 /* EMSG(_("cmd_gchar beyond the command length")); */
5887 return NUL;
5888 }
5889 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5890}
5891#endif
5892
5893#if defined(FEAT_CMDWIN) || defined(PROTO)
5894/*
5895 * Open a window on the current command line and history. Allow editing in
5896 * the window. Returns when the window is closed.
5897 * Returns:
5898 * CR if the command is to be executed
5899 * Ctrl_C if it is to be abandoned
5900 * K_IGNORE if editing continues
5901 */
5902 static int
5903ex_window()
5904{
5905 struct cmdline_info save_ccline;
5906 buf_T *old_curbuf = curbuf;
5907 win_T *old_curwin = curwin;
5908 buf_T *bp;
5909 win_T *wp;
5910 int i;
5911 linenr_T lnum;
5912 int histtype;
5913 garray_T winsizes;
5914 char_u typestr[2];
5915 int save_restart_edit = restart_edit;
5916 int save_State = State;
5917 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005918#ifdef FEAT_RIGHTLEFT
5919 int save_cmdmsg_rl = cmdmsg_rl;
5920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921
5922 /* Can't do this recursively. Can't do it when typing a password. */
5923 if (cmdwin_type != 0
5924# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5925 || cmdline_star > 0
5926# endif
5927 )
5928 {
5929 beep_flush();
5930 return K_IGNORE;
5931 }
5932
5933 /* Save current window sizes. */
5934 win_size_save(&winsizes);
5935
5936# ifdef FEAT_AUTOCMD
5937 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005938 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005940 /* don't use a new tab page */
5941 cmdmod.tab = 0;
5942
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 /* Create a window for the command-line buffer. */
5944 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5945 {
5946 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005947# ifdef FEAT_AUTOCMD
5948 unblock_autocmds();
5949# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 return K_IGNORE;
5951 }
5952 cmdwin_type = ccline.cmdfirstc;
5953 if (cmdwin_type == NUL)
5954 cmdwin_type = '-';
5955
5956 /* Create the command-line buffer empty. */
5957 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5958 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5959 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5960 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5961 curbuf->b_p_ma = TRUE;
5962# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005963 curwin->w_p_rl = cmdmsg_rl;
5964 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965# endif
5966# ifdef FEAT_SCROLLBIND
5967 curwin->w_p_scb = FALSE;
5968# endif
5969
5970# ifdef FEAT_AUTOCMD
5971 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005972 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973# endif
5974
Bram Moolenaar46152342005-09-07 21:18:43 +00005975 /* Showing the prompt may have set need_wait_return, reset it. */
5976 need_wait_return = FALSE;
5977
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 histtype = hist_char2type(ccline.cmdfirstc);
5979 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5980 {
5981 if (p_wc == TAB)
5982 {
5983 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5984 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5985 }
5986 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5987 }
5988
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005989 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5990 * sets 'textwidth' to 78). */
5991 curbuf->b_p_tw = 0;
5992
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 /* Fill the buffer with the history. */
5994 init_history();
5995 if (hislen > 0)
5996 {
5997 i = hisidx[histtype];
5998 if (i >= 0)
5999 {
6000 lnum = 0;
6001 do
6002 {
6003 if (++i == hislen)
6004 i = 0;
6005 if (history[histtype][i].hisstr != NULL)
6006 ml_append(lnum++, history[histtype][i].hisstr,
6007 (colnr_T)0, FALSE);
6008 }
6009 while (i != hisidx[histtype]);
6010 }
6011 }
6012
6013 /* Replace the empty last line with the current command-line and put the
6014 * cursor there. */
6015 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6016 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6017 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006018 changed_line_abv_curs();
6019 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006020 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021
6022 /* Save the command line info, can be used recursively. */
6023 save_ccline = ccline;
6024 ccline.cmdbuff = NULL;
6025 ccline.cmdprompt = NULL;
6026
6027 /* No Ex mode here! */
6028 exmode_active = 0;
6029
6030 State = NORMAL;
6031# ifdef FEAT_MOUSE
6032 setmouse();
6033# endif
6034
6035# ifdef FEAT_AUTOCMD
6036 /* Trigger CmdwinEnter autocommands. */
6037 typestr[0] = cmdwin_type;
6038 typestr[1] = NUL;
6039 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006040 if (restart_edit != 0) /* autocmd with ":startinsert" */
6041 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042# endif
6043
6044 i = RedrawingDisabled;
6045 RedrawingDisabled = 0;
6046
6047 /*
6048 * Call the main loop until <CR> or CTRL-C is typed.
6049 */
6050 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006051 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052
6053 RedrawingDisabled = i;
6054
6055# ifdef FEAT_AUTOCMD
6056 /* Trigger CmdwinLeave autocommands. */
6057 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6058# endif
6059
Bram Moolenaarccc18222007-05-10 18:25:20 +00006060 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 ccline = save_ccline;
6062 cmdwin_type = 0;
6063
6064 exmode_active = save_exmode;
6065
6066 /* Safety check: The old window or buffer was deleted: It's a a bug when
6067 * this happens! */
6068 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6069 {
6070 cmdwin_result = Ctrl_C;
6071 EMSG(_("E199: Active window or buffer deleted"));
6072 }
6073 else
6074 {
6075# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6076 /* autocmds may abort script processing */
6077 if (aborting() && cmdwin_result != K_IGNORE)
6078 cmdwin_result = Ctrl_C;
6079# endif
6080 /* Set the new command line from the cmdline buffer. */
6081 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006082 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006084 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6085
6086 if (histtype == HIST_CMD)
6087 {
6088 /* Execute the command directly. */
6089 ccline.cmdbuff = vim_strsave((char_u *)p);
6090 cmdwin_result = CAR;
6091 }
6092 else
6093 {
6094 /* First need to cancel what we were doing. */
6095 ccline.cmdbuff = NULL;
6096 stuffcharReadbuff(':');
6097 stuffReadbuff((char_u *)p);
6098 stuffcharReadbuff(CAR);
6099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 }
6101 else if (cmdwin_result == K_XF2) /* :qa typed */
6102 {
6103 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6104 cmdwin_result = CAR;
6105 }
6106 else
6107 ccline.cmdbuff = vim_strsave(ml_get_curline());
6108 if (ccline.cmdbuff == NULL)
6109 cmdwin_result = Ctrl_C;
6110 else
6111 {
6112 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6113 ccline.cmdbufflen = ccline.cmdlen + 1;
6114 ccline.cmdpos = curwin->w_cursor.col;
6115 if (ccline.cmdpos > ccline.cmdlen)
6116 ccline.cmdpos = ccline.cmdlen;
6117 if (cmdwin_result == K_IGNORE)
6118 {
6119 set_cmdspos_cursor();
6120 redrawcmd();
6121 }
6122 }
6123
6124# ifdef FEAT_AUTOCMD
6125 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006126 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127# endif
6128 wp = curwin;
6129 bp = curbuf;
6130 win_goto(old_curwin);
6131 win_close(wp, TRUE);
6132 close_buffer(NULL, bp, DOBUF_WIPE);
6133
6134 /* Restore window sizes. */
6135 win_size_restore(&winsizes);
6136
6137# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006138 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139# endif
6140 }
6141
6142 ga_clear(&winsizes);
6143 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006144# ifdef FEAT_RIGHTLEFT
6145 cmdmsg_rl = save_cmdmsg_rl;
6146# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147
6148 State = save_State;
6149# ifdef FEAT_MOUSE
6150 setmouse();
6151# endif
6152
6153 return cmdwin_result;
6154}
6155#endif /* FEAT_CMDWIN */
6156
6157/*
6158 * Used for commands that either take a simple command string argument, or:
6159 * cmd << endmarker
6160 * {script}
6161 * endmarker
6162 * Returns a pointer to allocated memory with {script} or NULL.
6163 */
6164 char_u *
6165script_get(eap, cmd)
6166 exarg_T *eap;
6167 char_u *cmd;
6168{
6169 char_u *theline;
6170 char *end_pattern = NULL;
6171 char dot[] = ".";
6172 garray_T ga;
6173
6174 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6175 return NULL;
6176
6177 ga_init2(&ga, 1, 0x400);
6178
6179 if (cmd[2] != NUL)
6180 end_pattern = (char *)skipwhite(cmd + 2);
6181 else
6182 end_pattern = dot;
6183
6184 for (;;)
6185 {
6186 theline = eap->getline(
6187#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006188 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189#endif
6190 NUL, eap->cookie, 0);
6191
6192 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
6193 break;
6194
6195 ga_concat(&ga, theline);
6196 ga_append(&ga, '\n');
6197 vim_free(theline);
6198 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006199 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200
6201 return (char_u *)ga.ga_data;
6202}