blob: 66715893590c2083701aa7509bf005b252d7a3ee [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 */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000338
339 /* Get a character. Ignore K_IGNORE, it should not do anything, such
340 * as stop completion. */
341 do
342 {
343 c = safe_vgetc();
344 } while (c == K_IGNORE);
345
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 if (KeyTyped)
347 {
348 some_key_typed = TRUE;
349#ifdef FEAT_RIGHTLEFT
350 if (cmd_hkmap)
351 c = hkmap(c);
352# ifdef FEAT_FKMAP
353 if (cmd_fkmap)
354 c = cmdl_fkmap(c);
355# endif
356 if (cmdmsg_rl && !KeyStuffed)
357 {
358 /* Invert horizontal movements and operations. Only when
359 * typed by the user directly, not when the result of a
360 * mapping. */
361 switch (c)
362 {
363 case K_RIGHT: c = K_LEFT; break;
364 case K_S_RIGHT: c = K_S_LEFT; break;
365 case K_C_RIGHT: c = K_C_LEFT; break;
366 case K_LEFT: c = K_RIGHT; break;
367 case K_S_LEFT: c = K_S_RIGHT; break;
368 case K_C_LEFT: c = K_C_RIGHT; break;
369 }
370 }
371#endif
372 }
373
374 /*
375 * Ignore got_int when CTRL-C was typed here.
376 * Don't ignore it in :global, we really need to break then, e.g., for
377 * ":g/pat/normal /pat" (without the <CR>).
378 * Don't ignore it for the input() function.
379 */
380 if ((c == Ctrl_C
381#ifdef UNIX
382 || c == intr_char
383#endif
384 )
385#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
386 && firstc != '@'
387#endif
388#ifdef FEAT_EVAL
389 && !break_ctrl_c
390#endif
391 && !global_busy)
392 got_int = FALSE;
393
394#ifdef FEAT_CMDHIST
395 /* free old command line when finished moving around in the history
396 * list */
397 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000398 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000399 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 && c != K_PAGEDOWN && c != K_PAGEUP
401 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000402 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
404 {
405 vim_free(lookfor);
406 lookfor = NULL;
407 }
408#endif
409
410 /*
411 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
412 */
413 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles != -1)
414 c = Ctrl_P;
415
416#ifdef FEAT_WILDMENU
417 /* Special translations for 'wildmenu' */
418 if (did_wild_list && p_wmnu)
419 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000420 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000422 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 c = Ctrl_N;
424 }
425 /* Hitting CR after "emenu Name.": complete submenu */
426 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
427 && ccline.cmdpos > 1
428 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
429 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
430 && (c == '\n' || c == '\r' || c == K_KENTER))
431 c = K_DOWN;
432#endif
433
434 /* free expanded names when finished walking through matches */
435 if (xpc.xp_numfiles != -1
436 && !(c == p_wc && KeyTyped) && c != p_wcm
437 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
438 && c != Ctrl_L)
439 {
440 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
441 did_wild_list = FALSE;
442#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000443 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444#endif
445 xpc.xp_context = EXPAND_NOTHING;
446 wim_index = 0;
447#ifdef FEAT_WILDMENU
448 if (p_wmnu && wild_menu_showing != 0)
449 {
450 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000451 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000452
453 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000454 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455
456 if (wild_menu_showing == WM_SCROLLED)
457 {
458 /* Entered command line, move it up */
459 cmdline_row--;
460 redrawcmd();
461 }
462 else if (save_p_ls != -1)
463 {
464 /* restore 'laststatus' and 'winminheight' */
465 p_ls = save_p_ls;
466 p_wmh = save_p_wmh;
467 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000468 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000470 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 redrawcmd();
472 save_p_ls = -1;
473 }
474 else
475 {
476# ifdef FEAT_VERTSPLIT
477 win_redraw_last_status(topframe);
478# else
479 lastwin->w_redr_status = TRUE;
480# endif
481 redraw_statuslines();
482 }
483 KeyTyped = skt;
484 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000485 if (ccline.input_fn)
486 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 }
488#endif
489 }
490
491#ifdef FEAT_WILDMENU
492 /* Special translations for 'wildmenu' */
493 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
494 {
495 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000496 if (c == K_DOWN && ccline.cmdpos > 0
497 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000499 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 {
501 /* Hitting <Up>: Remove one submenu name in front of the
502 * cursor */
503 int found = FALSE;
504
505 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
506 i = 0;
507 while (--j > 0)
508 {
509 /* check for start of menu name */
510 if (ccline.cmdbuff[j] == ' '
511 && ccline.cmdbuff[j - 1] != '\\')
512 {
513 i = j + 1;
514 break;
515 }
516 /* check for start of submenu name */
517 if (ccline.cmdbuff[j] == '.'
518 && ccline.cmdbuff[j - 1] != '\\')
519 {
520 if (found)
521 {
522 i = j + 1;
523 break;
524 }
525 else
526 found = TRUE;
527 }
528 }
529 if (i > 0)
530 cmdline_del(i);
531 c = p_wc;
532 xpc.xp_context = EXPAND_NOTHING;
533 }
534 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000535 if ((xpc.xp_context == EXPAND_FILES
536 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 {
538 char_u upseg[5];
539
540 upseg[0] = PATHSEP;
541 upseg[1] = '.';
542 upseg[2] = '.';
543 upseg[3] = PATHSEP;
544 upseg[4] = NUL;
545
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000546 if (c == K_DOWN
547 && ccline.cmdpos > 0
548 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
549 && (ccline.cmdpos < 3
550 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
552 {
553 /* go down a directory */
554 c = p_wc;
555 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000556 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 {
558 /* If in a direct ancestor, strip off one ../ to go down */
559 int found = FALSE;
560
561 j = ccline.cmdpos;
562 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
563 while (--j > i)
564 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000565#ifdef FEAT_MBYTE
566 if (has_mbyte)
567 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if (vim_ispathsep(ccline.cmdbuff[j]))
570 {
571 found = TRUE;
572 break;
573 }
574 }
575 if (found
576 && ccline.cmdbuff[j - 1] == '.'
577 && ccline.cmdbuff[j - 2] == '.'
578 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
579 {
580 cmdline_del(j - 2);
581 c = p_wc;
582 }
583 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000584 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 {
586 /* go up a directory */
587 int found = FALSE;
588
589 j = ccline.cmdpos - 1;
590 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
591 while (--j > i)
592 {
593#ifdef FEAT_MBYTE
594 if (has_mbyte)
595 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
596#endif
597 if (vim_ispathsep(ccline.cmdbuff[j])
598#ifdef BACKSLASH_IN_FILENAME
599 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
600 == NULL
601#endif
602 )
603 {
604 if (found)
605 {
606 i = j + 1;
607 break;
608 }
609 else
610 found = TRUE;
611 }
612 }
613
614 if (!found)
615 j = i;
616 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
617 j += 4;
618 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
619 && j == i)
620 j += 3;
621 else
622 j = 0;
623 if (j > 0)
624 {
625 /* TODO this is only for DOS/UNIX systems - need to put in
626 * machine-specific stuff here and in upseg init */
627 cmdline_del(j);
628 put_on_cmdline(upseg + 1, 3, FALSE);
629 }
630 else if (ccline.cmdpos > i)
631 cmdline_del(i);
632 c = p_wc;
633 }
634 }
635#if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
636 if (p_wmnu
637 && (xpc.xp_context == EXPAND_FILES
638 || xpc.xp_context == EXPAND_MENUNAMES)
639 && (c == K_UP || c == K_DOWN))
640 xpc.xp_context = EXPAND_NOTHING;
641#endif
642
643#endif /* FEAT_WILDMENU */
644
645 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
646 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
647 if (c == Ctrl_BSL)
648 {
649 ++no_mapping;
650 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000651 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 --no_mapping;
653 --allow_keys;
654 /* CTRL-\ e doesn't work when obtaining an expression. */
655 if (c != Ctrl_N && c != Ctrl_G
656 && (c != 'e' || ccline.cmdfirstc == '='))
657 {
658 vungetc(c);
659 c = Ctrl_BSL;
660 }
661#ifdef FEAT_EVAL
662 else if (c == 'e')
663 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000664 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
666 /*
667 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000668 * Need to save and restore the current command line, to be
669 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 */
671 if (ccline.cmdpos == ccline.cmdlen)
672 new_cmdpos = 99999; /* keep it at the end */
673 else
674 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000675
676 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000678 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 if (c == '=')
680 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000681 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000682 * to avoid nasty things like going to another buffer when
683 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000684 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000685 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000687 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000688 restore_cmdline(&save_ccline);
689
690 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000692 ccline.cmdlen = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 STRCPY(ccline.cmdbuff, p);
694 vim_free(p);
695
696 /* Restore the cursor or use the position set with
697 * set_cmdline_pos(). */
698 if (new_cmdpos > ccline.cmdlen)
699 ccline.cmdpos = ccline.cmdlen;
700 else
701 ccline.cmdpos = new_cmdpos;
702
703 KeyTyped = FALSE; /* Don't do p_wc completion. */
704 redrawcmd();
705 goto cmdline_changed;
706 }
707 }
708 beep_flush();
709 c = ESC;
710 }
711#endif
712 else
713 {
714 if (c == Ctrl_G && p_im && restart_edit == 0)
715 restart_edit = 'a';
716 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
717 in history */
718 goto returncmd; /* back to Normal mode */
719 }
720 }
721
722#ifdef FEAT_CMDWIN
723 if (c == cedit_key || c == K_CMDWIN)
724 {
725 /*
726 * Open a window to edit the command line (and history).
727 */
728 c = ex_window();
729 some_key_typed = TRUE;
730 }
731# ifdef FEAT_DIGRAPHS
732 else
733# endif
734#endif
735#ifdef FEAT_DIGRAPHS
736 c = do_digraph(c);
737#endif
738
739 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
740 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
741 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000742 /* In Ex mode a backslash escapes a newline. */
743 if (exmode_active
744 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000745 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000746 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000747 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000749 if (c == K_KENTER)
750 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000752 else
753 {
754 gotesc = FALSE; /* Might have typed ESC previously, don't
755 truncate the cmdline now. */
756 if (ccheck_abbr(c + ABBR_OFF))
757 goto cmdline_changed;
758 if (!cmd_silent)
759 {
760 windgoto(msg_row, 0);
761 out_flush();
762 }
763 break;
764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 }
766
767 /*
768 * Completion for 'wildchar' or 'wildcharm' key.
769 * - hitting <ESC> twice means: abandon command line.
770 * - wildcard expansion is only done when the 'wildchar' key is really
771 * typed, not when it comes from a macro
772 */
773 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
774 {
775 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
776 {
777 /* if 'wildmode' contains "list" may still need to list */
778 if (xpc.xp_numfiles > 1
779 && !did_wild_list
780 && (wim_flags[wim_index] & WIM_LIST))
781 {
782 (void)showmatches(&xpc, FALSE);
783 redrawcmd();
784 did_wild_list = TRUE;
785 }
786 if (wim_flags[wim_index] & WIM_LONGEST)
787 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
788 else if (wim_flags[wim_index] & WIM_FULL)
789 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
790 else
791 res = OK; /* don't insert 'wildchar' now */
792 }
793 else /* typed p_wc first time */
794 {
795 wim_index = 0;
796 j = ccline.cmdpos;
797 /* if 'wildmode' first contains "longest", get longest
798 * common part */
799 if (wim_flags[0] & WIM_LONGEST)
800 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
801 else
802 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
803
804 /* if interrupted while completing, behave like it failed */
805 if (got_int)
806 {
807 (void)vpeekc(); /* remove <C-C> from input stream */
808 got_int = FALSE; /* don't abandon the command line */
809 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
810#ifdef FEAT_WILDMENU
811 xpc.xp_context = EXPAND_NOTHING;
812#endif
813 goto cmdline_changed;
814 }
815
816 /* when more than one match, and 'wildmode' first contains
817 * "list", or no change and 'wildmode' contains "longest,list",
818 * list all matches */
819 if (res == OK && xpc.xp_numfiles > 1)
820 {
821 /* a "longest" that didn't do anything is skipped (but not
822 * "list:longest") */
823 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
824 wim_index = 1;
825 if ((wim_flags[wim_index] & WIM_LIST)
826#ifdef FEAT_WILDMENU
827 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
828#endif
829 )
830 {
831 if (!(wim_flags[0] & WIM_LONGEST))
832 {
833#ifdef FEAT_WILDMENU
834 int p_wmnu_save = p_wmnu;
835 p_wmnu = 0;
836#endif
837 nextwild(&xpc, WILD_PREV, 0); /* remove match */
838#ifdef FEAT_WILDMENU
839 p_wmnu = p_wmnu_save;
840#endif
841 }
842#ifdef FEAT_WILDMENU
843 (void)showmatches(&xpc, p_wmnu
844 && ((wim_flags[wim_index] & WIM_LIST) == 0));
845#else
846 (void)showmatches(&xpc, FALSE);
847#endif
848 redrawcmd();
849 did_wild_list = TRUE;
850 if (wim_flags[wim_index] & WIM_LONGEST)
851 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
852 else if (wim_flags[wim_index] & WIM_FULL)
853 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
854 }
855 else
856 vim_beep();
857 }
858#ifdef FEAT_WILDMENU
859 else if (xpc.xp_numfiles == -1)
860 xpc.xp_context = EXPAND_NOTHING;
861#endif
862 }
863 if (wim_index < 3)
864 ++wim_index;
865 if (c == ESC)
866 gotesc = TRUE;
867 if (res == OK)
868 goto cmdline_changed;
869 }
870
871 gotesc = FALSE;
872
873 /* <S-Tab> goes to last match, in a clumsy way */
874 if (c == K_S_TAB && KeyTyped)
875 {
876 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
877 && nextwild(&xpc, WILD_PREV, 0) == OK
878 && nextwild(&xpc, WILD_PREV, 0) == OK)
879 goto cmdline_changed;
880 }
881
882 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
883 c = NL;
884
885 do_abbr = TRUE; /* default: check for abbreviation */
886
887 /*
888 * Big switch for a typed command line character.
889 */
890 switch (c)
891 {
892 case K_BS:
893 case Ctrl_H:
894 case K_DEL:
895 case K_KDEL:
896 case Ctrl_W:
897#ifdef FEAT_FKMAP
898 if (cmd_fkmap && c == K_BS)
899 c = K_DEL;
900#endif
901 if (c == K_KDEL)
902 c = K_DEL;
903
904 /*
905 * delete current character is the same as backspace on next
906 * character, except at end of line
907 */
908 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
909 ++ccline.cmdpos;
910#ifdef FEAT_MBYTE
911 if (has_mbyte && c == K_DEL)
912 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
913 ccline.cmdbuff + ccline.cmdpos);
914#endif
915 if (ccline.cmdpos > 0)
916 {
917 char_u *p;
918
919 j = ccline.cmdpos;
920 p = ccline.cmdbuff + j;
921#ifdef FEAT_MBYTE
922 if (has_mbyte)
923 {
924 p = mb_prevptr(ccline.cmdbuff, p);
925 if (c == Ctrl_W)
926 {
927 while (p > ccline.cmdbuff && vim_isspace(*p))
928 p = mb_prevptr(ccline.cmdbuff, p);
929 i = mb_get_class(p);
930 while (p > ccline.cmdbuff && mb_get_class(p) == i)
931 p = mb_prevptr(ccline.cmdbuff, p);
932 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000933 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 }
935 }
936 else
937#endif
938 if (c == Ctrl_W)
939 {
940 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
941 --p;
942 i = vim_iswordc(p[-1]);
943 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
944 && vim_iswordc(p[-1]) == i)
945 --p;
946 }
947 else
948 --p;
949 ccline.cmdpos = (int)(p - ccline.cmdbuff);
950 ccline.cmdlen -= j - ccline.cmdpos;
951 i = ccline.cmdpos;
952 while (i < ccline.cmdlen)
953 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
954
955 /* Truncate at the end, required for multi-byte chars. */
956 ccline.cmdbuff[ccline.cmdlen] = NUL;
957 redrawcmd();
958 }
959 else if (ccline.cmdlen == 0 && c != Ctrl_W
960 && ccline.cmdprompt == NULL && indent == 0)
961 {
962 /* In ex and debug mode it doesn't make sense to return. */
963 if (exmode_active
964#ifdef FEAT_EVAL
965 || ccline.cmdfirstc == '>'
966#endif
967 )
968 goto cmdline_not_changed;
969
970 vim_free(ccline.cmdbuff); /* no commandline to return */
971 ccline.cmdbuff = NULL;
972 if (!cmd_silent)
973 {
974#ifdef FEAT_RIGHTLEFT
975 if (cmdmsg_rl)
976 msg_col = Columns;
977 else
978#endif
979 msg_col = 0;
980 msg_putchar(' '); /* delete ':' */
981 }
982 redraw_cmdline = TRUE;
983 goto returncmd; /* back to cmd mode */
984 }
985 goto cmdline_changed;
986
987 case K_INS:
988 case K_KINS:
989#ifdef FEAT_FKMAP
990 /* if Farsi mode set, we are in reverse insert mode -
991 Do not change the mode */
992 if (cmd_fkmap)
993 beep_flush();
994 else
995#endif
996 ccline.overstrike = !ccline.overstrike;
997#ifdef CURSOR_SHAPE
998 ui_cursor_shape(); /* may show different cursor shape */
999#endif
1000 goto cmdline_not_changed;
1001
1002 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001003 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 {
1005 /* ":lmap" mappings exists, toggle use of mappings. */
1006 State ^= LANGMAP;
1007#ifdef USE_IM_CONTROL
1008 im_set_active(FALSE); /* Disable input method */
1009#endif
1010 if (b_im_ptr != NULL)
1011 {
1012 if (State & LANGMAP)
1013 *b_im_ptr = B_IMODE_LMAP;
1014 else
1015 *b_im_ptr = B_IMODE_NONE;
1016 }
1017 }
1018#ifdef USE_IM_CONTROL
1019 else
1020 {
1021 /* There are no ":lmap" mappings, toggle IM. When
1022 * 'imdisable' is set don't try getting the status, it's
1023 * always off. */
1024 if ((p_imdisable && b_im_ptr != NULL)
1025 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1026 {
1027 im_set_active(FALSE); /* Disable input method */
1028 if (b_im_ptr != NULL)
1029 *b_im_ptr = B_IMODE_NONE;
1030 }
1031 else
1032 {
1033 im_set_active(TRUE); /* Enable input method */
1034 if (b_im_ptr != NULL)
1035 *b_im_ptr = B_IMODE_IM;
1036 }
1037 }
1038#endif
1039 if (b_im_ptr != NULL)
1040 {
1041 if (b_im_ptr == &curbuf->b_p_iminsert)
1042 set_iminsert_global();
1043 else
1044 set_imsearch_global();
1045 }
1046#ifdef CURSOR_SHAPE
1047 ui_cursor_shape(); /* may show different cursor shape */
1048#endif
1049#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1050 /* Show/unshow value of 'keymap' in status lines later. */
1051 status_redraw_curbuf();
1052#endif
1053 goto cmdline_not_changed;
1054
1055/* case '@': only in very old vi */
1056 case Ctrl_U:
1057 /* delete all characters left of the cursor */
1058 j = ccline.cmdpos;
1059 ccline.cmdlen -= j;
1060 i = ccline.cmdpos = 0;
1061 while (i < ccline.cmdlen)
1062 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1063 /* Truncate at the end, required for multi-byte chars. */
1064 ccline.cmdbuff[ccline.cmdlen] = NUL;
1065 redrawcmd();
1066 goto cmdline_changed;
1067
1068#ifdef FEAT_CLIPBOARD
1069 case Ctrl_Y:
1070 /* Copy the modeless selection, if there is one. */
1071 if (clip_star.state != SELECT_CLEARED)
1072 {
1073 if (clip_star.state == SELECT_DONE)
1074 clip_copy_modeless_selection(TRUE);
1075 goto cmdline_not_changed;
1076 }
1077 break;
1078#endif
1079
1080 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1081 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001082 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001083 * ":normal" runs out of characters. */
1084 if (exmode_active
1085#ifdef FEAT_EX_EXTRA
1086 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1087#endif
1088 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 goto cmdline_not_changed;
1090
1091 gotesc = TRUE; /* will free ccline.cmdbuff after
1092 putting it in history */
1093 goto returncmd; /* back to cmd mode */
1094
1095 case Ctrl_R: /* insert register */
1096#ifdef USE_ON_FLY_SCROLL
1097 dont_scroll = TRUE; /* disallow scrolling here */
1098#endif
1099 putcmdline('"', TRUE);
1100 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001101 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (i == Ctrl_O)
1103 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1104 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001105 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 --no_mapping;
1107#ifdef FEAT_EVAL
1108 /*
1109 * Insert the result of an expression.
1110 * Need to save the current command line, to be able to enter
1111 * a new one...
1112 */
1113 new_cmdpos = -1;
1114 if (c == '=')
1115 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1117 {
1118 beep_flush();
1119 c = ESC;
1120 }
1121 else
1122 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001123 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001125 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 }
1127 }
1128#endif
1129 if (c != ESC) /* use ESC to cancel inserting register */
1130 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001131 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001132
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001133#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001134 /* When there was a serious error abort getting the
1135 * command line. */
1136 if (aborting())
1137 {
1138 gotesc = TRUE; /* will free ccline.cmdbuff after
1139 putting it in history */
1140 goto returncmd; /* back to cmd mode */
1141 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 KeyTyped = FALSE; /* Don't do p_wc completion. */
1144#ifdef FEAT_EVAL
1145 if (new_cmdpos >= 0)
1146 {
1147 /* set_cmdline_pos() was used */
1148 if (new_cmdpos > ccline.cmdlen)
1149 ccline.cmdpos = ccline.cmdlen;
1150 else
1151 ccline.cmdpos = new_cmdpos;
1152 }
1153#endif
1154 }
1155 redrawcmd();
1156 goto cmdline_changed;
1157
1158 case Ctrl_D:
1159 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1160 break; /* Use ^D as normal char instead */
1161
1162 redrawcmd();
1163 continue; /* don't do incremental search now */
1164
1165 case K_RIGHT:
1166 case K_S_RIGHT:
1167 case K_C_RIGHT:
1168 do
1169 {
1170 if (ccline.cmdpos >= ccline.cmdlen)
1171 break;
1172 i = cmdline_charsize(ccline.cmdpos);
1173 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1174 break;
1175 ccline.cmdspos += i;
1176#ifdef FEAT_MBYTE
1177 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001178 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 + ccline.cmdpos);
1180 else
1181#endif
1182 ++ccline.cmdpos;
1183 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001184 while ((c == K_S_RIGHT || c == K_C_RIGHT
1185 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1187#ifdef FEAT_MBYTE
1188 if (has_mbyte)
1189 set_cmdspos_cursor();
1190#endif
1191 goto cmdline_not_changed;
1192
1193 case K_LEFT:
1194 case K_S_LEFT:
1195 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001196 if (ccline.cmdpos == 0)
1197 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 do
1199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 --ccline.cmdpos;
1201#ifdef FEAT_MBYTE
1202 if (has_mbyte) /* move to first byte of char */
1203 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1204 ccline.cmdbuff + ccline.cmdpos);
1205#endif
1206 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1207 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001208 while (ccline.cmdpos > 0
1209 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001210 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1212#ifdef FEAT_MBYTE
1213 if (has_mbyte)
1214 set_cmdspos_cursor();
1215#endif
1216 goto cmdline_not_changed;
1217
1218 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001219 /* Ignore mouse event or ex_window() result. */
1220 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
Bram Moolenaar4770d092006-01-12 23:22:24 +00001222#ifdef FEAT_GUI_W32
1223 /* On Win32 ignore <M-F4>, we get it when closing the window was
1224 * cancelled. */
1225 case K_F4:
1226 if (mod_mask == MOD_MASK_ALT)
1227 {
1228 redrawcmd(); /* somehow the cmdline is cleared */
1229 goto cmdline_not_changed;
1230 }
1231 break;
1232#endif
1233
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234#ifdef FEAT_MOUSE
1235 case K_MIDDLEDRAG:
1236 case K_MIDDLERELEASE:
1237 goto cmdline_not_changed; /* Ignore mouse */
1238
1239 case K_MIDDLEMOUSE:
1240# ifdef FEAT_GUI
1241 /* When GUI is active, also paste when 'mouse' is empty */
1242 if (!gui.in_use)
1243# endif
1244 if (!mouse_has(MOUSE_COMMAND))
1245 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001246# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001248 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001250# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001251 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 redrawcmd();
1253 goto cmdline_changed;
1254
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001255# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001257 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 redrawcmd();
1259 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
1262 case K_LEFTDRAG:
1263 case K_LEFTRELEASE:
1264 case K_RIGHTDRAG:
1265 case K_RIGHTRELEASE:
1266 /* Ignore drag and release events when the button-down wasn't
1267 * seen before. */
1268 if (ignore_drag_release)
1269 goto cmdline_not_changed;
1270 /* FALLTHROUGH */
1271 case K_LEFTMOUSE:
1272 case K_RIGHTMOUSE:
1273 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1274 ignore_drag_release = TRUE;
1275 else
1276 ignore_drag_release = FALSE;
1277# ifdef FEAT_GUI
1278 /* When GUI is active, also move when 'mouse' is empty */
1279 if (!gui.in_use)
1280# endif
1281 if (!mouse_has(MOUSE_COMMAND))
1282 goto cmdline_not_changed; /* Ignore mouse */
1283# ifdef FEAT_CLIPBOARD
1284 if (mouse_row < cmdline_row && clip_star.available)
1285 {
1286 int button, is_click, is_drag;
1287
1288 /*
1289 * Handle modeless selection.
1290 */
1291 button = get_mouse_button(KEY2TERMCAP1(c),
1292 &is_click, &is_drag);
1293 if (mouse_model_popup() && button == MOUSE_LEFT
1294 && (mod_mask & MOD_MASK_SHIFT))
1295 {
1296 /* Translate shift-left to right button. */
1297 button = MOUSE_RIGHT;
1298 mod_mask &= ~MOD_MASK_SHIFT;
1299 }
1300 clip_modeless(button, is_click, is_drag);
1301 goto cmdline_not_changed;
1302 }
1303# endif
1304
1305 set_cmdspos();
1306 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1307 ++ccline.cmdpos)
1308 {
1309 i = cmdline_charsize(ccline.cmdpos);
1310 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1311 && mouse_col < ccline.cmdspos % Columns + i)
1312 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001313# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 if (has_mbyte)
1315 {
1316 /* Count ">" for double-wide char that doesn't fit. */
1317 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001318 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 + ccline.cmdpos) - 1;
1320 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 ccline.cmdspos += i;
1323 }
1324 goto cmdline_not_changed;
1325
1326 /* Mouse scroll wheel: ignored here */
1327 case K_MOUSEDOWN:
1328 case K_MOUSEUP:
1329 /* Alternate buttons ignored here */
1330 case K_X1MOUSE:
1331 case K_X1DRAG:
1332 case K_X1RELEASE:
1333 case K_X2MOUSE:
1334 case K_X2DRAG:
1335 case K_X2RELEASE:
1336 goto cmdline_not_changed;
1337
1338#endif /* FEAT_MOUSE */
1339
1340#ifdef FEAT_GUI
1341 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1342 case K_LEFTRELEASE_NM:
1343 goto cmdline_not_changed;
1344
1345 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001346 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 {
1348 gui_do_scroll();
1349 redrawcmd();
1350 }
1351 goto cmdline_not_changed;
1352
1353 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001354 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 {
1356 gui_do_horiz_scroll();
1357 redrawcmd();
1358 }
1359 goto cmdline_not_changed;
1360#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001361#ifdef FEAT_GUI_TABLINE
1362 case K_TABLINE:
1363 case K_TABMENU:
1364 /* Don't want to change any tabs here. Make sure the same tab
1365 * is still selected. */
1366 if (gui_use_tabline())
1367 gui_mch_set_curtab(tabpage_index(curtab));
1368 goto cmdline_not_changed;
1369#endif
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 case K_SELECT: /* end of Select mode mapping - ignore */
1372 goto cmdline_not_changed;
1373
1374 case Ctrl_B: /* begin of command line */
1375 case K_HOME:
1376 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 case K_S_HOME:
1378 case K_C_HOME:
1379 ccline.cmdpos = 0;
1380 set_cmdspos();
1381 goto cmdline_not_changed;
1382
1383 case Ctrl_E: /* end of command line */
1384 case K_END:
1385 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 case K_S_END:
1387 case K_C_END:
1388 ccline.cmdpos = ccline.cmdlen;
1389 set_cmdspos_cursor();
1390 goto cmdline_not_changed;
1391
1392 case Ctrl_A: /* all matches */
1393 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1394 break;
1395 goto cmdline_changed;
1396
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001397 case Ctrl_L:
1398#ifdef FEAT_SEARCH_EXTRA
1399 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1400 {
1401 /* Add a character from under the cursor for 'incsearch' */
1402 if (did_incsearch
1403 && !equalpos(curwin->w_cursor, old_cursor))
1404 {
1405 c = gchar_cursor();
1406 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001407 {
1408 if (c == firstc || vim_strchr((char_u *)(
1409 p_magic ? "\\^$.*[" : "\\^$"), c)
1410 != NULL)
1411 {
1412 /* put a backslash before special characters */
1413 stuffcharReadbuff(c);
1414 c = '\\';
1415 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001416 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001417 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001418 }
1419 goto cmdline_not_changed;
1420 }
1421#endif
1422
1423 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1425 break;
1426 goto cmdline_changed;
1427
1428 case Ctrl_N: /* next match */
1429 case Ctrl_P: /* previous match */
1430 if (xpc.xp_numfiles > 0)
1431 {
1432 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1433 == FAIL)
1434 break;
1435 goto cmdline_changed;
1436 }
1437
1438#ifdef FEAT_CMDHIST
1439 case K_UP:
1440 case K_DOWN:
1441 case K_S_UP:
1442 case K_S_DOWN:
1443 case K_PAGEUP:
1444 case K_KPAGEUP:
1445 case K_PAGEDOWN:
1446 case K_KPAGEDOWN:
1447 if (hislen == 0 || firstc == NUL) /* no history */
1448 goto cmdline_not_changed;
1449
1450 i = hiscnt;
1451
1452 /* save current command string so it can be restored later */
1453 if (lookfor == NULL)
1454 {
1455 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1456 goto cmdline_not_changed;
1457 lookfor[ccline.cmdpos] = NUL;
1458 }
1459
1460 j = (int)STRLEN(lookfor);
1461 for (;;)
1462 {
1463 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001464 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001465 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 {
1467 if (hiscnt == hislen) /* first time */
1468 hiscnt = hisidx[histype];
1469 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1470 hiscnt = hislen - 1;
1471 else if (hiscnt != hisidx[histype] + 1)
1472 --hiscnt;
1473 else /* at top of list */
1474 {
1475 hiscnt = i;
1476 break;
1477 }
1478 }
1479 else /* one step forwards */
1480 {
1481 /* on last entry, clear the line */
1482 if (hiscnt == hisidx[histype])
1483 {
1484 hiscnt = hislen;
1485 break;
1486 }
1487
1488 /* not on a history line, nothing to do */
1489 if (hiscnt == hislen)
1490 break;
1491 if (hiscnt == hislen - 1) /* wrap around */
1492 hiscnt = 0;
1493 else
1494 ++hiscnt;
1495 }
1496 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1497 {
1498 hiscnt = i;
1499 break;
1500 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001501 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001502 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 || STRNCMP(history[histype][hiscnt].hisstr,
1504 lookfor, (size_t)j) == 0)
1505 break;
1506 }
1507
1508 if (hiscnt != i) /* jumped to other entry */
1509 {
1510 char_u *p;
1511 int len;
1512 int old_firstc;
1513
1514 vim_free(ccline.cmdbuff);
1515 if (hiscnt == hislen)
1516 p = lookfor; /* back to the old one */
1517 else
1518 p = history[histype][hiscnt].hisstr;
1519
1520 if (histype == HIST_SEARCH
1521 && p != lookfor
1522 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1523 {
1524 /* Correct for the separator character used when
1525 * adding the history entry vs the one used now.
1526 * First loop: count length.
1527 * Second loop: copy the characters. */
1528 for (i = 0; i <= 1; ++i)
1529 {
1530 len = 0;
1531 for (j = 0; p[j] != NUL; ++j)
1532 {
1533 /* Replace old sep with new sep, unless it is
1534 * escaped. */
1535 if (p[j] == old_firstc
1536 && (j == 0 || p[j - 1] != '\\'))
1537 {
1538 if (i > 0)
1539 ccline.cmdbuff[len] = firstc;
1540 }
1541 else
1542 {
1543 /* Escape new sep, unless it is already
1544 * escaped. */
1545 if (p[j] == firstc
1546 && (j == 0 || p[j - 1] != '\\'))
1547 {
1548 if (i > 0)
1549 ccline.cmdbuff[len] = '\\';
1550 ++len;
1551 }
1552 if (i > 0)
1553 ccline.cmdbuff[len] = p[j];
1554 }
1555 ++len;
1556 }
1557 if (i == 0)
1558 {
1559 alloc_cmdbuff(len);
1560 if (ccline.cmdbuff == NULL)
1561 goto returncmd;
1562 }
1563 }
1564 ccline.cmdbuff[len] = NUL;
1565 }
1566 else
1567 {
1568 alloc_cmdbuff((int)STRLEN(p));
1569 if (ccline.cmdbuff == NULL)
1570 goto returncmd;
1571 STRCPY(ccline.cmdbuff, p);
1572 }
1573
1574 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1575 redrawcmd();
1576 goto cmdline_changed;
1577 }
1578 beep_flush();
1579 goto cmdline_not_changed;
1580#endif
1581
1582 case Ctrl_V:
1583 case Ctrl_Q:
1584#ifdef FEAT_MOUSE
1585 ignore_drag_release = TRUE;
1586#endif
1587 putcmdline('^', TRUE);
1588 c = get_literal(); /* get next (two) character(s) */
1589 do_abbr = FALSE; /* don't do abbreviation now */
1590#ifdef FEAT_MBYTE
1591 /* may need to remove ^ when composing char was typed */
1592 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1593 {
1594 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1595 msg_putchar(' ');
1596 cursorcmd();
1597 }
1598#endif
1599 break;
1600
1601#ifdef FEAT_DIGRAPHS
1602 case Ctrl_K:
1603#ifdef FEAT_MOUSE
1604 ignore_drag_release = TRUE;
1605#endif
1606 putcmdline('?', TRUE);
1607#ifdef USE_ON_FLY_SCROLL
1608 dont_scroll = TRUE; /* disallow scrolling here */
1609#endif
1610 c = get_digraph(TRUE);
1611 if (c != NUL)
1612 break;
1613
1614 redrawcmd();
1615 goto cmdline_not_changed;
1616#endif /* FEAT_DIGRAPHS */
1617
1618#ifdef FEAT_RIGHTLEFT
1619 case Ctrl__: /* CTRL-_: switch language mode */
1620 if (!p_ari)
1621 break;
1622#ifdef FEAT_FKMAP
1623 if (p_altkeymap)
1624 {
1625 cmd_fkmap = !cmd_fkmap;
1626 if (cmd_fkmap) /* in Farsi always in Insert mode */
1627 ccline.overstrike = FALSE;
1628 }
1629 else /* Hebrew is default */
1630#endif
1631 cmd_hkmap = !cmd_hkmap;
1632 goto cmdline_not_changed;
1633#endif
1634
1635 default:
1636#ifdef UNIX
1637 if (c == intr_char)
1638 {
1639 gotesc = TRUE; /* will free ccline.cmdbuff after
1640 putting it in history */
1641 goto returncmd; /* back to Normal mode */
1642 }
1643#endif
1644 /*
1645 * Normal character with no special meaning. Just set mod_mask
1646 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1647 * the string <S-Space>. This should only happen after ^V.
1648 */
1649 if (!IS_SPECIAL(c))
1650 mod_mask = 0x0;
1651 break;
1652 }
1653 /*
1654 * End of switch on command line character.
1655 * We come here if we have a normal character.
1656 */
1657
1658 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1659#ifdef FEAT_MBYTE
1660 /* Add ABBR_OFF for characters above 0x100, this is
1661 * what check_abbr() expects. */
1662 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1663#endif
1664 c))
1665 goto cmdline_changed;
1666
1667 /*
1668 * put the character in the command line
1669 */
1670 if (IS_SPECIAL(c) || mod_mask != 0)
1671 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1672 else
1673 {
1674#ifdef FEAT_MBYTE
1675 if (has_mbyte)
1676 {
1677 j = (*mb_char2bytes)(c, IObuff);
1678 IObuff[j] = NUL; /* exclude composing chars */
1679 put_on_cmdline(IObuff, j, TRUE);
1680 }
1681 else
1682#endif
1683 {
1684 IObuff[0] = c;
1685 put_on_cmdline(IObuff, 1, TRUE);
1686 }
1687 }
1688 goto cmdline_changed;
1689
1690/*
1691 * This part implements incremental searches for "/" and "?"
1692 * Jump to cmdline_not_changed when a character has been read but the command
1693 * line did not change. Then we only search and redraw if something changed in
1694 * the past.
1695 * Jump to cmdline_changed when the command line did change.
1696 * (Sorry for the goto's, I know it is ugly).
1697 */
1698cmdline_not_changed:
1699#ifdef FEAT_SEARCH_EXTRA
1700 if (!incsearch_postponed)
1701 continue;
1702#endif
1703
1704cmdline_changed:
1705#ifdef FEAT_SEARCH_EXTRA
1706 /*
1707 * 'incsearch' highlighting.
1708 */
1709 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1710 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001711 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001712#ifdef FEAT_RELTIME
1713 proftime_T tm;
1714#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001715
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 /* if there is a character waiting, search and redraw later */
1717 if (char_avail())
1718 {
1719 incsearch_postponed = TRUE;
1720 continue;
1721 }
1722 incsearch_postponed = FALSE;
1723 curwin->w_cursor = old_cursor; /* start at old position */
1724
1725 /* If there is no command line, don't do anything */
1726 if (ccline.cmdlen == 0)
1727 i = 0;
1728 else
1729 {
1730 cursor_off(); /* so the user knows we're busy */
1731 out_flush();
1732 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001733#ifdef FEAT_RELTIME
1734 /* Set the time limit to half a second. */
1735 profile_setlimit(500L, &tm);
1736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001738 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1739#ifdef FEAT_RELTIME
1740 &tm
1741#else
1742 NULL
1743#endif
1744 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 --emsg_off;
1746 /* if interrupted while searching, behave like it failed */
1747 if (got_int)
1748 {
1749 (void)vpeekc(); /* remove <C-C> from input stream */
1750 got_int = FALSE; /* don't abandon the command line */
1751 i = 0;
1752 }
1753 else if (char_avail())
1754 /* cancelled searching because a char was typed */
1755 incsearch_postponed = TRUE;
1756 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001757 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 highlight_match = TRUE; /* highlight position */
1759 else
1760 highlight_match = FALSE; /* remove highlight */
1761
1762 /* first restore the old curwin values, so the screen is
1763 * positioned in the same way as the actual search command */
1764 curwin->w_leftcol = old_leftcol;
1765 curwin->w_topline = old_topline;
1766# ifdef FEAT_DIFF
1767 curwin->w_topfill = old_topfill;
1768# endif
1769 curwin->w_botline = old_botline;
1770 changed_cline_bef_curs();
1771 update_topline();
1772
1773 if (i != 0)
1774 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001775 pos_T save_pos = curwin->w_cursor;
1776
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001778 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 * moves the whole match onto the screen when 'nowrap' is set.
1780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 curwin->w_cursor.lnum += search_match_lines;
1782 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001783 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1784 {
1785 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1786 coladvance((colnr_T)MAXCOL);
1787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001789 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001790 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001792 else
1793 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001794
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001796# ifdef FEAT_WINDOWS
1797 /* May redraw the status line to show the cursor position. */
1798 if (p_ru && curwin->w_status_height > 0)
1799 curwin->w_redr_status = TRUE;
1800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001802 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001803 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001804 restore_cmdline(&save_ccline);
1805
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001806 /* Leave it at the end to make CTRL-R CTRL-W work. */
1807 if (i != 0)
1808 curwin->w_cursor = end_pos;
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 msg_starthere();
1811 redrawcmdline();
1812 did_incsearch = TRUE;
1813 }
1814#else /* FEAT_SEARCH_EXTRA */
1815 ;
1816#endif
1817
1818#ifdef FEAT_RIGHTLEFT
1819 if (cmdmsg_rl
1820# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001821 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822# endif
1823 )
1824 /* Always redraw the whole command line to fix shaping and
1825 * right-left typing. Not efficient, but it works. */
1826 redrawcmd();
1827#endif
1828 }
1829
1830returncmd:
1831
1832#ifdef FEAT_RIGHTLEFT
1833 cmdmsg_rl = FALSE;
1834#endif
1835
1836#ifdef FEAT_FKMAP
1837 cmd_fkmap = 0;
1838#endif
1839
1840 ExpandCleanup(&xpc);
1841
1842#ifdef FEAT_SEARCH_EXTRA
1843 if (did_incsearch)
1844 {
1845 curwin->w_cursor = old_cursor;
1846 curwin->w_curswant = old_curswant;
1847 curwin->w_leftcol = old_leftcol;
1848 curwin->w_topline = old_topline;
1849# ifdef FEAT_DIFF
1850 curwin->w_topfill = old_topfill;
1851# endif
1852 curwin->w_botline = old_botline;
1853 highlight_match = FALSE;
1854 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001855 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 }
1857#endif
1858
1859 if (ccline.cmdbuff != NULL)
1860 {
1861 /*
1862 * Put line in history buffer (":" and "=" only when it was typed).
1863 */
1864#ifdef FEAT_CMDHIST
1865 if (ccline.cmdlen && firstc != NUL
1866 && (some_key_typed || histype == HIST_SEARCH))
1867 {
1868 add_to_history(histype, ccline.cmdbuff, TRUE,
1869 histype == HIST_SEARCH ? firstc : NUL);
1870 if (firstc == ':')
1871 {
1872 vim_free(new_last_cmdline);
1873 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1874 }
1875 }
1876#endif
1877
1878 if (gotesc) /* abandon command line */
1879 {
1880 vim_free(ccline.cmdbuff);
1881 ccline.cmdbuff = NULL;
1882 if (msg_scrolled == 0)
1883 compute_cmdrow();
1884 MSG("");
1885 redraw_cmdline = TRUE;
1886 }
1887 }
1888
1889 /*
1890 * If the screen was shifted up, redraw the whole screen (later).
1891 * If the line is too long, clear it, so ruler and shown command do
1892 * not get printed in the middle of it.
1893 */
1894 msg_check();
1895 msg_scroll = save_msg_scroll;
1896 redir_off = FALSE;
1897
1898 /* When the command line was typed, no need for a wait-return prompt. */
1899 if (some_key_typed)
1900 need_wait_return = FALSE;
1901
1902 State = save_State;
1903#ifdef USE_IM_CONTROL
1904 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1905 im_save_status(b_im_ptr);
1906 im_set_active(FALSE);
1907#endif
1908#ifdef FEAT_MOUSE
1909 setmouse();
1910#endif
1911#ifdef CURSOR_SHAPE
1912 ui_cursor_shape(); /* may show different cursor shape */
1913#endif
1914
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001915 {
1916 char_u *p = ccline.cmdbuff;
1917
1918 /* Make ccline empty, getcmdline() may try to use it. */
1919 ccline.cmdbuff = NULL;
1920 return p;
1921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922}
1923
1924#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1925/*
1926 * Get a command line with a prompt.
1927 * This is prepared to be called recursively from getcmdline() (e.g. by
1928 * f_input() when evaluating an expression from CTRL-R =).
1929 * Returns the command line in allocated memory, or NULL.
1930 */
1931 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001932getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 int firstc;
1934 char_u *prompt; /* command line prompt */
1935 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001936 int xp_context; /* type of expansion */
1937 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938{
1939 char_u *s;
1940 struct cmdline_info save_ccline;
1941 int msg_col_save = msg_col;
1942
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001943 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 ccline.cmdprompt = prompt;
1945 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001946# ifdef FEAT_EVAL
1947 ccline.xp_context = xp_context;
1948 ccline.xp_arg = xp_arg;
1949 ccline.input_fn = (firstc == '@');
1950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001952 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 /* Restore msg_col, the prompt from input() may have changed it. */
1954 msg_col = msg_col_save;
1955
1956 return s;
1957}
1958#endif
1959
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001960/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001961 * Return TRUE when the text must not be changed and we can't switch to
1962 * another window or buffer. Used when editing the command line, evaluating
1963 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001964 */
1965 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001966text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001967{
1968#ifdef FEAT_CMDWIN
1969 if (cmdwin_type != 0)
1970 return TRUE;
1971#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001972 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001973}
1974
1975/*
1976 * Give an error message for a command that isn't allowed while the cmdline
1977 * window is open or editing the cmdline in another way.
1978 */
1979 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001980text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001981{
1982#ifdef FEAT_CMDWIN
1983 if (cmdwin_type != 0)
1984 EMSG(_(e_cmdwin));
1985 else
1986#endif
1987 EMSG(_(e_secure));
1988}
1989
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001990#if defined(FEAT_AUTOCMD) || defined(PROTO)
1991/*
1992 * Check if "curbuf_lock" is set and return TRUE when it is and give an error
1993 * message.
1994 */
1995 int
1996curbuf_locked()
1997{
1998 if (curbuf_lock > 0)
1999 {
2000 EMSG(_("E788: Not allowed to edit another buffer now"));
2001 return TRUE;
2002 }
2003 return FALSE;
2004}
2005#endif
2006
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 static int
2008cmdline_charsize(idx)
2009 int idx;
2010{
2011#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2012 if (cmdline_star > 0) /* showing '*', always 1 position */
2013 return 1;
2014#endif
2015 return ptr2cells(ccline.cmdbuff + idx);
2016}
2017
2018/*
2019 * Compute the offset of the cursor on the command line for the prompt and
2020 * indent.
2021 */
2022 static void
2023set_cmdspos()
2024{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002025 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 ccline.cmdspos = 1 + ccline.cmdindent;
2027 else
2028 ccline.cmdspos = 0 + ccline.cmdindent;
2029}
2030
2031/*
2032 * Compute the screen position for the cursor on the command line.
2033 */
2034 static void
2035set_cmdspos_cursor()
2036{
2037 int i, m, c;
2038
2039 set_cmdspos();
2040 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002041 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002043 if (m < 0) /* overflow, Columns or Rows at weird value */
2044 m = MAXCOL;
2045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 else
2047 m = MAXCOL;
2048 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2049 {
2050 c = cmdline_charsize(i);
2051#ifdef FEAT_MBYTE
2052 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2053 if (has_mbyte)
2054 correct_cmdspos(i, c);
2055#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002056 /* If the cmdline doesn't fit, show cursor on last visible char.
2057 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 if ((ccline.cmdspos += c) >= m)
2059 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 ccline.cmdspos -= c;
2061 break;
2062 }
2063#ifdef FEAT_MBYTE
2064 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002065 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066#endif
2067 }
2068}
2069
2070#ifdef FEAT_MBYTE
2071/*
2072 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2073 * character that doesn't fit, so that a ">" must be displayed.
2074 */
2075 static void
2076correct_cmdspos(idx, cells)
2077 int idx;
2078 int cells;
2079{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002080 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2082 && ccline.cmdspos % Columns + cells > Columns)
2083 ccline.cmdspos++;
2084}
2085#endif
2086
2087/*
2088 * Get an Ex command line for the ":" command.
2089 */
2090/* ARGSUSED */
2091 char_u *
2092getexline(c, dummy, indent)
2093 int c; /* normally ':', NUL for ":append" */
2094 void *dummy; /* cookie not used */
2095 int indent; /* indent for inside conditionals */
2096{
2097 /* When executing a register, remove ':' that's in front of each line. */
2098 if (exec_from_reg && vpeekc() == ':')
2099 (void)vgetc();
2100 return getcmdline(c, 1L, indent);
2101}
2102
2103/*
2104 * Get an Ex command line for Ex mode.
2105 * In Ex mode we only use the OS supplied line editing features and no
2106 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002107 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 */
2109/* ARGSUSED */
2110 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002111getexmodeline(promptc, dummy, indent)
2112 int promptc; /* normally ':', NUL for ":append" and '?' for
2113 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 void *dummy; /* cookie not used */
2115 int indent; /* indent for inside conditionals */
2116{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002117 garray_T line_ga;
2118 char_u *pend;
2119 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002120 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002121 int escaped = FALSE; /* CTRL-V typed */
2122 int vcol = 0;
2123 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002124 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125
2126 /* Switch cursor on now. This avoids that it happens after the "\n", which
2127 * confuses the system function that computes tabstops. */
2128 cursor_on();
2129
2130 /* always start in column 0; write a newline if necessary */
2131 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002132 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002134 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002136 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002137 if (p_prompt)
2138 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 while (indent-- > 0)
2140 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143
2144 ga_init2(&line_ga, 1, 30);
2145
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002146 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002147 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002148 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002149 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002150 while (indent >= 8)
2151 {
2152 ga_append(&line_ga, TAB);
2153 msg_puts((char_u *)" ");
2154 indent -= 8;
2155 }
2156 while (indent-- > 0)
2157 {
2158 ga_append(&line_ga, ' ');
2159 msg_putchar(' ');
2160 }
2161 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002162 ++no_mapping;
2163 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002164
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 /*
2166 * Get the line, one character at a time.
2167 */
2168 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002169 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
2171 if (ga_grow(&line_ga, 40) == FAIL)
2172 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002173 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002175 /* Get one character at a time. Don't use inchar(), it can't handle
2176 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002177 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002178 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
2180 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002181 * Handle line editing.
2182 * Previously this was left to the system, putting the terminal in
2183 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002185 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002187 msg_putchar('\n');
2188 break;
2189 }
2190
2191 if (!escaped)
2192 {
2193 /* CR typed means "enter", which is NL */
2194 if (c1 == '\r')
2195 c1 = '\n';
2196
2197 if (c1 == BS || c1 == K_BS
2198 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002200 if (line_ga.ga_len > 0)
2201 {
2202 --line_ga.ga_len;
2203 goto redraw;
2204 }
2205 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002208 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002210 msg_col = startcol;
2211 msg_clr_eos();
2212 line_ga.ga_len = 0;
2213 continue;
2214 }
2215
2216 if (c1 == Ctrl_T)
2217 {
2218 p = (char_u *)line_ga.ga_data;
2219 p[line_ga.ga_len] = NUL;
2220 indent = get_indent_str(p, 8);
2221 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2222add_indent:
2223 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002225 char_u *s = skipwhite(p);
2226
2227 ga_grow(&line_ga, 1);
2228 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2229 *s = ' ';
2230 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002232redraw:
2233 /* redraw the line */
2234 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002235 vcol = 0;
2236 for (p = (char_u *)line_ga.ga_data;
2237 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002239 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002241 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002243 msg_putchar(' ');
2244 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002246 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002248 msg_outtrans_len(p, 1);
2249 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
2251 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002252 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002253 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002254 continue;
2255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002257 if (c1 == Ctrl_D)
2258 {
2259 /* Delete one shiftwidth. */
2260 p = (char_u *)line_ga.ga_data;
2261 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002263 if (prev_char == '^')
2264 ex_keep_indent = TRUE;
2265 indent = 0;
2266 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
2268 else
2269 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002270 p[line_ga.ga_len] = NUL;
2271 indent = get_indent_str(p, 8);
2272 --indent;
2273 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002275 while (get_indent_str(p, 8) > indent)
2276 {
2277 char_u *s = skipwhite(p);
2278
2279 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2280 --line_ga.ga_len;
2281 }
2282 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002284
2285 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2286 {
2287 escaped = TRUE;
2288 continue;
2289 }
2290
2291 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2292 if (IS_SPECIAL(c1))
2293 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002295
2296 if (IS_SPECIAL(c1))
2297 c1 = '?';
2298 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002299 if (c1 == '\n')
2300 msg_putchar('\n');
2301 else if (c1 == TAB)
2302 {
2303 /* Don't use chartabsize(), 'ts' can be different */
2304 do
2305 {
2306 msg_putchar(' ');
2307 } while (++vcol % 8);
2308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002311 msg_outtrans_len(
2312 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2313 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002315 ++line_ga.ga_len;
2316 escaped = FALSE;
2317
2318 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002319 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002320
2321 /* we are done when a NL is entered, but not when it comes after a
2322 * backslash */
2323 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2324 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002327 --pend;
2328 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002329 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331 }
2332
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002333 --no_mapping;
2334 --allow_keys;
2335
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 /* make following messages go to the next line */
2337 msg_didout = FALSE;
2338 msg_col = 0;
2339 if (msg_row < Rows - 1)
2340 ++msg_row;
2341 emsg_on_display = FALSE; /* don't want ui_delay() */
2342
2343 if (got_int)
2344 ga_clear(&line_ga);
2345
2346 return (char_u *)line_ga.ga_data;
2347}
2348
Bram Moolenaare344bea2005-09-01 20:46:49 +00002349# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2350 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351/*
2352 * Return TRUE if ccline.overstrike is on.
2353 */
2354 int
2355cmdline_overstrike()
2356{
2357 return ccline.overstrike;
2358}
2359
2360/*
2361 * Return TRUE if the cursor is at the end of the cmdline.
2362 */
2363 int
2364cmdline_at_end()
2365{
2366 return (ccline.cmdpos >= ccline.cmdlen);
2367}
2368#endif
2369
Bram Moolenaar9372a112005-12-06 19:59:18 +00002370#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371/*
2372 * Return the virtual column number at the current cursor position.
2373 * This is used by the IM code to obtain the start of the preedit string.
2374 */
2375 colnr_T
2376cmdline_getvcol_cursor()
2377{
2378 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2379 return MAXCOL;
2380
2381# ifdef FEAT_MBYTE
2382 if (has_mbyte)
2383 {
2384 colnr_T col;
2385 int i = 0;
2386
2387 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002388 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
2390 return col;
2391 }
2392 else
2393# endif
2394 return ccline.cmdpos;
2395}
2396#endif
2397
2398#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2399/*
2400 * If part of the command line is an IM preedit string, redraw it with
2401 * IM feedback attributes. The cursor position is restored after drawing.
2402 */
2403 static void
2404redrawcmd_preedit()
2405{
2406 if ((State & CMDLINE)
2407 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002408 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 && !p_imdisable
2410 && im_is_preediting())
2411 {
2412 int cmdpos = 0;
2413 int cmdspos;
2414 int old_row;
2415 int old_col;
2416 colnr_T col;
2417
2418 old_row = msg_row;
2419 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002420 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422# ifdef FEAT_MBYTE
2423 if (has_mbyte)
2424 {
2425 for (col = 0; col < preedit_start_col
2426 && cmdpos < ccline.cmdlen; ++col)
2427 {
2428 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002429 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 }
2431 }
2432 else
2433# endif
2434 {
2435 cmdspos += preedit_start_col;
2436 cmdpos += preedit_start_col;
2437 }
2438
2439 msg_row = cmdline_row + (cmdspos / (int)Columns);
2440 msg_col = cmdspos % (int)Columns;
2441 if (msg_row >= Rows)
2442 msg_row = Rows - 1;
2443
2444 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2445 {
2446 int char_len;
2447 int char_attr;
2448
2449 char_attr = im_get_feedback_attr(col);
2450 if (char_attr < 0)
2451 break; /* end of preedit string */
2452
2453# ifdef FEAT_MBYTE
2454 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002455 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 else
2457# endif
2458 char_len = 1;
2459
2460 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2461 cmdpos += char_len;
2462 }
2463
2464 msg_row = old_row;
2465 msg_col = old_col;
2466 }
2467}
2468#endif /* FEAT_XIM && FEAT_GUI_GTK */
2469
2470/*
2471 * Allocate a new command line buffer.
2472 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2473 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2474 */
2475 static void
2476alloc_cmdbuff(len)
2477 int len;
2478{
2479 /*
2480 * give some extra space to avoid having to allocate all the time
2481 */
2482 if (len < 80)
2483 len = 100;
2484 else
2485 len += 20;
2486
2487 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2488 ccline.cmdbufflen = len;
2489}
2490
2491/*
2492 * Re-allocate the command line to length len + something extra.
2493 * return FAIL for failure, OK otherwise
2494 */
2495 static int
2496realloc_cmdbuff(len)
2497 int len;
2498{
2499 char_u *p;
2500
2501 p = ccline.cmdbuff;
2502 alloc_cmdbuff(len); /* will get some more */
2503 if (ccline.cmdbuff == NULL) /* out of memory */
2504 {
2505 ccline.cmdbuff = p; /* keep the old one */
2506 return FAIL;
2507 }
2508 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2509 vim_free(p);
2510 return OK;
2511}
2512
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002513#if defined(FEAT_ARABIC) || defined(PROTO)
2514static char_u *arshape_buf = NULL;
2515
2516# if defined(EXITFREE) || defined(PROTO)
2517 void
2518free_cmdline_buf()
2519{
2520 vim_free(arshape_buf);
2521}
2522# endif
2523#endif
2524
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525/*
2526 * Draw part of the cmdline at the current cursor position. But draw stars
2527 * when cmdline_star is TRUE.
2528 */
2529 static void
2530draw_cmdline(start, len)
2531 int start;
2532 int len;
2533{
2534#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2535 int i;
2536
2537 if (cmdline_star > 0)
2538 for (i = 0; i < len; ++i)
2539 {
2540 msg_putchar('*');
2541# ifdef FEAT_MBYTE
2542 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002543 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544# endif
2545 }
2546 else
2547#endif
2548#ifdef FEAT_ARABIC
2549 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2550 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 static int buflen = 0;
2552 char_u *p;
2553 int j;
2554 int newlen = 0;
2555 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002556 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 int prev_c = 0;
2558 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002559 int u8c;
2560 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562
2563 /*
2564 * Do arabic shaping into a temporary buffer. This is very
2565 * inefficient!
2566 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002567 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 {
2569 /* Re-allocate the buffer. We keep it around to avoid a lot of
2570 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002571 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002572 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002573 arshape_buf = alloc(buflen);
2574 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 return; /* out of memory */
2576 }
2577
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002578 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2579 {
2580 /* Prepend a space to draw the leading composing char on. */
2581 arshape_buf[0] = ' ';
2582 newlen = 1;
2583 }
2584
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 for (j = start; j < start + len; j += mb_l)
2586 {
2587 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002588 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002589 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (ARABIC_CHAR(u8c))
2591 {
2592 /* Do Arabic shaping. */
2593 if (cmdmsg_rl)
2594 {
2595 /* displaying from right to left */
2596 pc = prev_c;
2597 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002598 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 if (j + mb_l >= start + len)
2600 nc = NUL;
2601 else
2602 nc = utf_ptr2char(p + mb_l);
2603 }
2604 else
2605 {
2606 /* displaying from left to right */
2607 if (j + mb_l >= start + len)
2608 pc = NUL;
2609 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002610 {
2611 int pcc[MAX_MCO];
2612
2613 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002615 pc1 = pcc[0];
2616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 nc = prev_c;
2618 }
2619 prev_c = u8c;
2620
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002621 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002623 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002624 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002626 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2627 if (u8cc[1] != 0)
2628 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002629 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
2631 }
2632 else
2633 {
2634 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002635 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 newlen += mb_l;
2637 }
2638 }
2639
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002640 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 }
2642 else
2643#endif
2644 msg_outtrans_len(ccline.cmdbuff + start, len);
2645}
2646
2647/*
2648 * Put a character on the command line. Shifts the following text to the
2649 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2650 * "c" must be printable (fit in one display cell)!
2651 */
2652 void
2653putcmdline(c, shift)
2654 int c;
2655 int shift;
2656{
2657 if (cmd_silent)
2658 return;
2659 msg_no_more = TRUE;
2660 msg_putchar(c);
2661 if (shift)
2662 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2663 msg_no_more = FALSE;
2664 cursorcmd();
2665}
2666
2667/*
2668 * Undo a putcmdline(c, FALSE).
2669 */
2670 void
2671unputcmdline()
2672{
2673 if (cmd_silent)
2674 return;
2675 msg_no_more = TRUE;
2676 if (ccline.cmdlen == ccline.cmdpos)
2677 msg_putchar(' ');
2678 else
2679 draw_cmdline(ccline.cmdpos, 1);
2680 msg_no_more = FALSE;
2681 cursorcmd();
2682}
2683
2684/*
2685 * Put the given string, of the given length, onto the command line.
2686 * If len is -1, then STRLEN() is used to calculate the length.
2687 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2688 * part will be redrawn, otherwise it will not. If this function is called
2689 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2690 * called afterwards.
2691 */
2692 int
2693put_on_cmdline(str, len, redraw)
2694 char_u *str;
2695 int len;
2696 int redraw;
2697{
2698 int retval;
2699 int i;
2700 int m;
2701 int c;
2702
2703 if (len < 0)
2704 len = (int)STRLEN(str);
2705
2706 /* Check if ccline.cmdbuff needs to be longer */
2707 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2708 retval = realloc_cmdbuff(ccline.cmdlen + len);
2709 else
2710 retval = OK;
2711 if (retval == OK)
2712 {
2713 if (!ccline.overstrike)
2714 {
2715 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2716 ccline.cmdbuff + ccline.cmdpos,
2717 (size_t)(ccline.cmdlen - ccline.cmdpos));
2718 ccline.cmdlen += len;
2719 }
2720 else
2721 {
2722#ifdef FEAT_MBYTE
2723 if (has_mbyte)
2724 {
2725 /* Count nr of characters in the new string. */
2726 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002727 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 ++m;
2729 /* Count nr of bytes in cmdline that are overwritten by these
2730 * characters. */
2731 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002732 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 --m;
2734 if (i < ccline.cmdlen)
2735 {
2736 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2737 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2738 ccline.cmdlen += ccline.cmdpos + len - i;
2739 }
2740 else
2741 ccline.cmdlen = ccline.cmdpos + len;
2742 }
2743 else
2744#endif
2745 if (ccline.cmdpos + len > ccline.cmdlen)
2746 ccline.cmdlen = ccline.cmdpos + len;
2747 }
2748 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2749 ccline.cmdbuff[ccline.cmdlen] = NUL;
2750
2751#ifdef FEAT_MBYTE
2752 if (enc_utf8)
2753 {
2754 /* When the inserted text starts with a composing character,
2755 * backup to the character before it. There could be two of them.
2756 */
2757 i = 0;
2758 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2759 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2760 {
2761 i = (*mb_head_off)(ccline.cmdbuff,
2762 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2763 ccline.cmdpos -= i;
2764 len += i;
2765 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2766 }
2767# ifdef FEAT_ARABIC
2768 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2769 {
2770 /* Check the previous character for Arabic combining pair. */
2771 i = (*mb_head_off)(ccline.cmdbuff,
2772 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2773 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2774 + ccline.cmdpos - i), c))
2775 {
2776 ccline.cmdpos -= i;
2777 len += i;
2778 }
2779 else
2780 i = 0;
2781 }
2782# endif
2783 if (i != 0)
2784 {
2785 /* Also backup the cursor position. */
2786 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2787 ccline.cmdspos -= i;
2788 msg_col -= i;
2789 if (msg_col < 0)
2790 {
2791 msg_col += Columns;
2792 --msg_row;
2793 }
2794 }
2795 }
2796#endif
2797
2798 if (redraw && !cmd_silent)
2799 {
2800 msg_no_more = TRUE;
2801 i = cmdline_row;
2802 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2803 /* Avoid clearing the rest of the line too often. */
2804 if (cmdline_row != i || ccline.overstrike)
2805 msg_clr_eos();
2806 msg_no_more = FALSE;
2807 }
2808#ifdef FEAT_FKMAP
2809 /*
2810 * If we are in Farsi command mode, the character input must be in
2811 * Insert mode. So do not advance the cmdpos.
2812 */
2813 if (!cmd_fkmap)
2814#endif
2815 {
2816 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002819 if (m < 0) /* overflow, Columns or Rows at weird value */
2820 m = MAXCOL;
2821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 else
2823 m = MAXCOL;
2824 for (i = 0; i < len; ++i)
2825 {
2826 c = cmdline_charsize(ccline.cmdpos);
2827#ifdef FEAT_MBYTE
2828 /* count ">" for a double-wide char that doesn't fit. */
2829 if (has_mbyte)
2830 correct_cmdspos(ccline.cmdpos, c);
2831#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002832 /* Stop cursor at the end of the screen, but do increment the
2833 * insert position, so that entering a very long command
2834 * works, even though you can't see it. */
2835 if (ccline.cmdspos + c < m)
2836 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837#ifdef FEAT_MBYTE
2838 if (has_mbyte)
2839 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002840 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 if (c > len - i - 1)
2842 c = len - i - 1;
2843 ccline.cmdpos += c;
2844 i += c;
2845 }
2846#endif
2847 ++ccline.cmdpos;
2848 }
2849 }
2850 }
2851 if (redraw)
2852 msg_check();
2853 return retval;
2854}
2855
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002856static struct cmdline_info prev_ccline;
2857static int prev_ccline_used = FALSE;
2858
2859/*
2860 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2861 * and overwrite it. But get_cmdline_str() may need it, thus make it
2862 * available globally in prev_ccline.
2863 */
2864 static void
2865save_cmdline(ccp)
2866 struct cmdline_info *ccp;
2867{
2868 if (!prev_ccline_used)
2869 {
2870 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2871 prev_ccline_used = TRUE;
2872 }
2873 *ccp = prev_ccline;
2874 prev_ccline = ccline;
2875 ccline.cmdbuff = NULL;
2876 ccline.cmdprompt = NULL;
2877}
2878
2879/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002880 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002881 */
2882 static void
2883restore_cmdline(ccp)
2884 struct cmdline_info *ccp;
2885{
2886 ccline = prev_ccline;
2887 prev_ccline = *ccp;
2888}
2889
Bram Moolenaar5a305422006-04-28 22:38:25 +00002890#if defined(FEAT_EVAL) || defined(PROTO)
2891/*
2892 * Save the command line into allocated memory. Returns a pointer to be
2893 * passed to restore_cmdline_alloc() later.
2894 * Returns NULL when failed.
2895 */
2896 char_u *
2897save_cmdline_alloc()
2898{
2899 struct cmdline_info *p;
2900
2901 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2902 if (p != NULL)
2903 save_cmdline(p);
2904 return (char_u *)p;
2905}
2906
2907/*
2908 * Restore the command line from the return value of save_cmdline_alloc().
2909 */
2910 void
2911restore_cmdline_alloc(p)
2912 char_u *p;
2913{
2914 if (p != NULL)
2915 {
2916 restore_cmdline((struct cmdline_info *)p);
2917 vim_free(p);
2918 }
2919}
2920#endif
2921
Bram Moolenaar8299df92004-07-10 09:47:34 +00002922/*
2923 * paste a yank register into the command line.
2924 * used by CTRL-R command in command-line mode
2925 * insert_reg() can't be used here, because special characters from the
2926 * register contents will be interpreted as commands.
2927 *
2928 * return FAIL for failure, OK otherwise
2929 */
2930 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002931cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00002932 int regname;
2933 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002934 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00002935{
2936 long i;
2937 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002938 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002939 int allocated;
2940 struct cmdline_info save_ccline;
2941
2942 /* check for valid regname; also accept special characters for CTRL-R in
2943 * the command line */
2944 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2945 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2946 return FAIL;
2947
2948 /* A register containing CTRL-R can cause an endless loop. Allow using
2949 * CTRL-C to break the loop. */
2950 line_breakcheck();
2951 if (got_int)
2952 return FAIL;
2953
2954#ifdef FEAT_CLIPBOARD
2955 regname = may_get_selection(regname);
2956#endif
2957
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002958 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002959 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002960 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002961 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002962 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002963 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002964 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002965
2966 if (i)
2967 {
2968 /* Got the value of a special register in "arg". */
2969 if (arg == NULL)
2970 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002971
2972 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
2973 * part of the word. */
2974 p = arg;
2975 if (p_is && regname == Ctrl_W)
2976 {
2977 char_u *w;
2978 int len;
2979
2980 /* Locate start of last word in the cmd buffer. */
2981 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
2982 {
2983#ifdef FEAT_MBYTE
2984 if (has_mbyte)
2985 {
2986 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
2987 if (!vim_iswordc(mb_ptr2char(w - len)))
2988 break;
2989 w -= len;
2990 }
2991 else
2992#endif
2993 {
2994 if (!vim_iswordc(w[-1]))
2995 break;
2996 --w;
2997 }
2998 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002999 len = (int)((ccline.cmdbuff + ccline.cmdlen) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003000 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3001 p += len;
3002 }
3003
3004 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003005 if (allocated)
3006 vim_free(arg);
3007 return OK;
3008 }
3009
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003010 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003011}
3012
3013/*
3014 * Put a string on the command line.
3015 * When "literally" is TRUE, insert literally.
3016 * When "literally" is FALSE, insert as typed, but don't leave the command
3017 * line.
3018 */
3019 void
3020cmdline_paste_str(s, literally)
3021 char_u *s;
3022 int literally;
3023{
3024 int c, cv;
3025
3026 if (literally)
3027 put_on_cmdline(s, -1, TRUE);
3028 else
3029 while (*s != NUL)
3030 {
3031 cv = *s;
3032 if (cv == Ctrl_V && s[1])
3033 ++s;
3034#ifdef FEAT_MBYTE
3035 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003036 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003037 else
3038#endif
3039 c = *s++;
3040 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3041#ifdef UNIX
3042 || c == intr_char
3043#endif
3044 || (c == Ctrl_BSL && *s == Ctrl_N))
3045 stuffcharReadbuff(Ctrl_V);
3046 stuffcharReadbuff(c);
3047 }
3048}
3049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050#ifdef FEAT_WILDMENU
3051/*
3052 * Delete characters on the command line, from "from" to the current
3053 * position.
3054 */
3055 static void
3056cmdline_del(from)
3057 int from;
3058{
3059 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3060 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3061 ccline.cmdlen -= ccline.cmdpos - from;
3062 ccline.cmdpos = from;
3063}
3064#endif
3065
3066/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003067 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 * search
3069 */
3070 void
3071redrawcmdline()
3072{
3073 if (cmd_silent)
3074 return;
3075 need_wait_return = FALSE;
3076 compute_cmdrow();
3077 redrawcmd();
3078 cursorcmd();
3079}
3080
3081 static void
3082redrawcmdprompt()
3083{
3084 int i;
3085
3086 if (cmd_silent)
3087 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003088 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 msg_putchar(ccline.cmdfirstc);
3090 if (ccline.cmdprompt != NULL)
3091 {
3092 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3093 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3094 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003095 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 --ccline.cmdindent;
3097 }
3098 else
3099 for (i = ccline.cmdindent; i > 0; --i)
3100 msg_putchar(' ');
3101}
3102
3103/*
3104 * Redraw what is currently on the command line.
3105 */
3106 void
3107redrawcmd()
3108{
3109 if (cmd_silent)
3110 return;
3111
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003112 /* when 'incsearch' is set there may be no command line while redrawing */
3113 if (ccline.cmdbuff == NULL)
3114 {
3115 windgoto(cmdline_row, 0);
3116 msg_clr_eos();
3117 return;
3118 }
3119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 msg_start();
3121 redrawcmdprompt();
3122
3123 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3124 msg_no_more = TRUE;
3125 draw_cmdline(0, ccline.cmdlen);
3126 msg_clr_eos();
3127 msg_no_more = FALSE;
3128
3129 set_cmdspos_cursor();
3130
3131 /*
3132 * An emsg() before may have set msg_scroll. This is used in normal mode,
3133 * in cmdline mode we can reset them now.
3134 */
3135 msg_scroll = FALSE; /* next message overwrites cmdline */
3136
3137 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3138 * in cmdline mode */
3139 skip_redraw = FALSE;
3140}
3141
3142 void
3143compute_cmdrow()
3144{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003145 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 cmdline_row = Rows - 1;
3147 else
3148 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3149 + W_STATUS_HEIGHT(lastwin);
3150}
3151
3152 static void
3153cursorcmd()
3154{
3155 if (cmd_silent)
3156 return;
3157
3158#ifdef FEAT_RIGHTLEFT
3159 if (cmdmsg_rl)
3160 {
3161 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3162 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3163 if (msg_row <= 0)
3164 msg_row = Rows - 1;
3165 }
3166 else
3167#endif
3168 {
3169 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3170 msg_col = ccline.cmdspos % (int)Columns;
3171 if (msg_row >= Rows)
3172 msg_row = Rows - 1;
3173 }
3174
3175 windgoto(msg_row, msg_col);
3176#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3177 redrawcmd_preedit();
3178#endif
3179#ifdef MCH_CURSOR_SHAPE
3180 mch_update_cursor();
3181#endif
3182}
3183
3184 void
3185gotocmdline(clr)
3186 int clr;
3187{
3188 msg_start();
3189#ifdef FEAT_RIGHTLEFT
3190 if (cmdmsg_rl)
3191 msg_col = Columns - 1;
3192 else
3193#endif
3194 msg_col = 0; /* always start in column 0 */
3195 if (clr) /* clear the bottom line(s) */
3196 msg_clr_eos(); /* will reset clear_cmdline */
3197 windgoto(cmdline_row, 0);
3198}
3199
3200/*
3201 * Check the word in front of the cursor for an abbreviation.
3202 * Called when the non-id character "c" has been entered.
3203 * When an abbreviation is recognized it is removed from the text with
3204 * backspaces and the replacement string is inserted, followed by "c".
3205 */
3206 static int
3207ccheck_abbr(c)
3208 int c;
3209{
3210 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3211 return FALSE;
3212
3213 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3214}
3215
3216/*
3217 * Return FAIL if this is not an appropriate context in which to do
3218 * completion of anything, return OK if it is (even if there are no matches).
3219 * For the caller, this means that the character is just passed through like a
3220 * normal character (instead of being expanded). This allows :s/^I^D etc.
3221 */
3222 static int
3223nextwild(xp, type, options)
3224 expand_T *xp;
3225 int type;
3226 int options; /* extra options for ExpandOne() */
3227{
3228 int i, j;
3229 char_u *p1;
3230 char_u *p2;
3231 int oldlen;
3232 int difflen;
3233 int v;
3234
3235 if (xp->xp_numfiles == -1)
3236 {
3237 set_expand_context(xp);
3238 cmd_showtail = expand_showtail(xp);
3239 }
3240
3241 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3242 {
3243 beep_flush();
3244 return OK; /* Something illegal on command line */
3245 }
3246 if (xp->xp_context == EXPAND_NOTHING)
3247 {
3248 /* Caller can use the character as a normal char instead */
3249 return FAIL;
3250 }
3251
3252 MSG_PUTS("..."); /* show that we are busy */
3253 out_flush();
3254
3255 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3256 oldlen = ccline.cmdpos - i;
3257
3258 if (type == WILD_NEXT || type == WILD_PREV)
3259 {
3260 /*
3261 * Get next/previous match for a previous expanded pattern.
3262 */
3263 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3264 }
3265 else
3266 {
3267 /*
3268 * Translate string into pattern and expand it.
3269 */
3270 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3271 p2 = NULL;
3272 else
3273 {
3274 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3275 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3276 |options, type);
3277 vim_free(p1);
3278 /* longest match: make sure it is not shorter (happens with :help */
3279 if (p2 != NULL && type == WILD_LONGEST)
3280 {
3281 for (j = 0; j < oldlen; ++j)
3282 if (ccline.cmdbuff[i + j] == '*'
3283 || ccline.cmdbuff[i + j] == '?')
3284 break;
3285 if ((int)STRLEN(p2) < j)
3286 {
3287 vim_free(p2);
3288 p2 = NULL;
3289 }
3290 }
3291 }
3292 }
3293
3294 if (p2 != NULL && !got_int)
3295 {
3296 difflen = (int)STRLEN(p2) - oldlen;
3297 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3298 {
3299 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3300 xp->xp_pattern = ccline.cmdbuff + i;
3301 }
3302 else
3303 v = OK;
3304 if (v == OK)
3305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003306 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3307 &ccline.cmdbuff[ccline.cmdpos],
3308 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3309 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 ccline.cmdlen += difflen;
3311 ccline.cmdpos += difflen;
3312 }
3313 }
3314 vim_free(p2);
3315
3316 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003317 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318
3319 /* When expanding a ":map" command and no matches are found, assume that
3320 * the key is supposed to be inserted literally */
3321 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3322 return FAIL;
3323
3324 if (xp->xp_numfiles <= 0 && p2 == NULL)
3325 beep_flush();
3326 else if (xp->xp_numfiles == 1)
3327 /* free expanded pattern */
3328 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3329
3330 return OK;
3331}
3332
3333/*
3334 * Do wildcard expansion on the string 'str'.
3335 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003336 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 * Return NULL for failure.
3338 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003339 * "orig" is the originally expanded string, copied to allocated memory. It
3340 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3341 * WILD_PREV "orig" should be NULL.
3342 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003343 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3344 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 *
3346 * mode = WILD_FREE: just free previously expanded matches
3347 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3348 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3349 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3350 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3351 * mode = WILD_ALL: return all matches concatenated
3352 * mode = WILD_LONGEST: return longest matched part
3353 *
3354 * options = WILD_LIST_NOTFOUND: list entries without a match
3355 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3356 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3357 * options = WILD_NO_BEEP: Don't beep for multiple matches
3358 * options = WILD_ADD_SLASH: add a slash after directory names
3359 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3360 * options = WILD_SILENT: don't print warning messages
3361 * options = WILD_ESCAPE: put backslash before special chars
3362 *
3363 * The variables xp->xp_context and xp->xp_backslash must have been set!
3364 */
3365 char_u *
3366ExpandOne(xp, str, orig, options, mode)
3367 expand_T *xp;
3368 char_u *str;
3369 char_u *orig; /* allocated copy of original of expanded string */
3370 int options;
3371 int mode;
3372{
3373 char_u *ss = NULL;
3374 static int findex;
3375 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003376 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 int i;
3378 long_u len;
3379 int non_suf_match; /* number without matching suffix */
3380
3381 /*
3382 * first handle the case of using an old match
3383 */
3384 if (mode == WILD_NEXT || mode == WILD_PREV)
3385 {
3386 if (xp->xp_numfiles > 0)
3387 {
3388 if (mode == WILD_PREV)
3389 {
3390 if (findex == -1)
3391 findex = xp->xp_numfiles;
3392 --findex;
3393 }
3394 else /* mode == WILD_NEXT */
3395 ++findex;
3396
3397 /*
3398 * When wrapping around, return the original string, set findex to
3399 * -1.
3400 */
3401 if (findex < 0)
3402 {
3403 if (orig_save == NULL)
3404 findex = xp->xp_numfiles - 1;
3405 else
3406 findex = -1;
3407 }
3408 if (findex >= xp->xp_numfiles)
3409 {
3410 if (orig_save == NULL)
3411 findex = 0;
3412 else
3413 findex = -1;
3414 }
3415#ifdef FEAT_WILDMENU
3416 if (p_wmnu)
3417 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3418 findex, cmd_showtail);
3419#endif
3420 if (findex == -1)
3421 return vim_strsave(orig_save);
3422 return vim_strsave(xp->xp_files[findex]);
3423 }
3424 else
3425 return NULL;
3426 }
3427
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003428 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3430 {
3431 FreeWild(xp->xp_numfiles, xp->xp_files);
3432 xp->xp_numfiles = -1;
3433 vim_free(orig_save);
3434 orig_save = NULL;
3435 }
3436 findex = 0;
3437
3438 if (mode == WILD_FREE) /* only release file name */
3439 return NULL;
3440
3441 if (xp->xp_numfiles == -1)
3442 {
3443 vim_free(orig_save);
3444 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003445 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 /*
3448 * Do the expansion.
3449 */
3450 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3451 options) == FAIL)
3452 {
3453#ifdef FNAME_ILLEGAL
3454 /* Illegal file name has been silently skipped. But when there
3455 * are wildcards, the real problem is that there was no match,
3456 * causing the pattern to be added, which has illegal characters.
3457 */
3458 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3459 EMSG2(_(e_nomatch2), str);
3460#endif
3461 }
3462 else if (xp->xp_numfiles == 0)
3463 {
3464 if (!(options & WILD_SILENT))
3465 EMSG2(_(e_nomatch2), str);
3466 }
3467 else
3468 {
3469 /* Escape the matches for use on the command line. */
3470 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3471
3472 /*
3473 * Check for matching suffixes in file names.
3474 */
3475 if (mode != WILD_ALL && mode != WILD_LONGEST)
3476 {
3477 if (xp->xp_numfiles)
3478 non_suf_match = xp->xp_numfiles;
3479 else
3480 non_suf_match = 1;
3481 if ((xp->xp_context == EXPAND_FILES
3482 || xp->xp_context == EXPAND_DIRECTORIES)
3483 && xp->xp_numfiles > 1)
3484 {
3485 /*
3486 * More than one match; check suffix.
3487 * The files will have been sorted on matching suffix in
3488 * expand_wildcards, only need to check the first two.
3489 */
3490 non_suf_match = 0;
3491 for (i = 0; i < 2; ++i)
3492 if (match_suffix(xp->xp_files[i]))
3493 ++non_suf_match;
3494 }
3495 if (non_suf_match != 1)
3496 {
3497 /* Can we ever get here unless it's while expanding
3498 * interactively? If not, we can get rid of this all
3499 * together. Don't really want to wait for this message
3500 * (and possibly have to hit return to continue!).
3501 */
3502 if (!(options & WILD_SILENT))
3503 EMSG(_(e_toomany));
3504 else if (!(options & WILD_NO_BEEP))
3505 beep_flush();
3506 }
3507 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3508 ss = vim_strsave(xp->xp_files[0]);
3509 }
3510 }
3511 }
3512
3513 /* Find longest common part */
3514 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3515 {
3516 for (len = 0; xp->xp_files[0][len]; ++len)
3517 {
3518 for (i = 0; i < xp->xp_numfiles; ++i)
3519 {
3520#ifdef CASE_INSENSITIVE_FILENAME
3521 if (xp->xp_context == EXPAND_DIRECTORIES
3522 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003523 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 || xp->xp_context == EXPAND_BUFFERS)
3525 {
3526 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3527 TOLOWER_LOC(xp->xp_files[0][len]))
3528 break;
3529 }
3530 else
3531#endif
3532 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3533 break;
3534 }
3535 if (i < xp->xp_numfiles)
3536 {
3537 if (!(options & WILD_NO_BEEP))
3538 vim_beep();
3539 break;
3540 }
3541 }
3542 ss = alloc((unsigned)len + 1);
3543 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003544 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 findex = -1; /* next p_wc gets first one */
3546 }
3547
3548 /* Concatenate all matching names */
3549 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3550 {
3551 len = 0;
3552 for (i = 0; i < xp->xp_numfiles; ++i)
3553 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3554 ss = lalloc(len, TRUE);
3555 if (ss != NULL)
3556 {
3557 *ss = NUL;
3558 for (i = 0; i < xp->xp_numfiles; ++i)
3559 {
3560 STRCAT(ss, xp->xp_files[i]);
3561 if (i != xp->xp_numfiles - 1)
3562 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3563 }
3564 }
3565 }
3566
3567 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3568 ExpandCleanup(xp);
3569
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003570 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003571 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003572 vim_free(orig);
3573
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 return ss;
3575}
3576
3577/*
3578 * Prepare an expand structure for use.
3579 */
3580 void
3581ExpandInit(xp)
3582 expand_T *xp;
3583{
3584 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003585#ifndef BACKSLASH_IN_FILENAME
3586 xp->xp_shell = FALSE;
3587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 xp->xp_numfiles = -1;
3589 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003590#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3591 xp->xp_arg = NULL;
3592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593}
3594
3595/*
3596 * Cleanup an expand structure after use.
3597 */
3598 void
3599ExpandCleanup(xp)
3600 expand_T *xp;
3601{
3602 if (xp->xp_numfiles >= 0)
3603 {
3604 FreeWild(xp->xp_numfiles, xp->xp_files);
3605 xp->xp_numfiles = -1;
3606 }
3607}
3608
3609 void
3610ExpandEscape(xp, str, numfiles, files, options)
3611 expand_T *xp;
3612 char_u *str;
3613 int numfiles;
3614 char_u **files;
3615 int options;
3616{
3617 int i;
3618 char_u *p;
3619
3620 /*
3621 * May change home directory back to "~"
3622 */
3623 if (options & WILD_HOME_REPLACE)
3624 tilde_replace(str, numfiles, files);
3625
3626 if (options & WILD_ESCAPE)
3627 {
3628 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003629 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 || xp->xp_context == EXPAND_BUFFERS
3631 || xp->xp_context == EXPAND_DIRECTORIES)
3632 {
3633 /*
3634 * Insert a backslash into a file name before a space, \, %, #
3635 * and wildmatch characters, except '~'.
3636 */
3637 for (i = 0; i < numfiles; ++i)
3638 {
3639 /* for ":set path=" we need to escape spaces twice */
3640 if (xp->xp_backslash == XP_BS_THREE)
3641 {
3642 p = vim_strsave_escaped(files[i], (char_u *)" ");
3643 if (p != NULL)
3644 {
3645 vim_free(files[i]);
3646 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003647#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 p = vim_strsave_escaped(files[i], (char_u *)" ");
3649 if (p != NULL)
3650 {
3651 vim_free(files[i]);
3652 files[i] = p;
3653 }
3654#endif
3655 }
3656 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003657#ifdef BACKSLASH_IN_FILENAME
3658 p = vim_strsave_fnameescape(files[i], FALSE);
3659#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003660 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 if (p != NULL)
3663 {
3664 vim_free(files[i]);
3665 files[i] = p;
3666 }
3667
3668 /* If 'str' starts with "\~", replace "~" at start of
3669 * files[i] with "\~". */
3670 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003671 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003674
3675 /* If the first file starts with a '+' escape it. Otherwise it
3676 * could be seen as "+cmd". */
3677 if (*files[0] == '+')
3678 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680 else if (xp->xp_context == EXPAND_TAGS)
3681 {
3682 /*
3683 * Insert a backslash before characters in a tag name that
3684 * would terminate the ":tag" command.
3685 */
3686 for (i = 0; i < numfiles; ++i)
3687 {
3688 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3689 if (p != NULL)
3690 {
3691 vim_free(files[i]);
3692 files[i] = p;
3693 }
3694 }
3695 }
3696 }
3697}
3698
3699/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003700 * Escape special characters in "fname" for when used as a file name argument
3701 * after a Vim command, or, when "shell" is non-zero, a shell command.
3702 * Returns the result in allocated memory.
3703 */
3704 char_u *
3705vim_strsave_fnameescape(fname, shell)
3706 char_u *fname;
3707 int shell;
3708{
3709#ifdef BACKSLASH_IN_FILENAME
3710 char_u buf[20];
3711 int j = 0;
Bram Moolenaar59fb5aa2008-05-29 13:36:10 +00003712 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003713
3714 /* Don't escape '[' and '{' if they are in 'isfname'. */
3715 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3716 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3717 buf[j++] = *p;
3718 buf[j] = NUL;
3719 return vim_strsave_escaped(fname, buf);
3720#else
3721 return vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3722#endif
3723}
3724
3725/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003726 * Put a backslash before the file name in "pp", which is in allocated memory.
3727 */
3728 static void
3729escape_fname(pp)
3730 char_u **pp;
3731{
3732 char_u *p;
3733
3734 p = alloc((unsigned)(STRLEN(*pp) + 2));
3735 if (p != NULL)
3736 {
3737 p[0] = '\\';
3738 STRCPY(p + 1, *pp);
3739 vim_free(*pp);
3740 *pp = p;
3741 }
3742}
3743
3744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 * For each file name in files[num_files]:
3746 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3747 */
3748 void
3749tilde_replace(orig_pat, num_files, files)
3750 char_u *orig_pat;
3751 int num_files;
3752 char_u **files;
3753{
3754 int i;
3755 char_u *p;
3756
3757 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3758 {
3759 for (i = 0; i < num_files; ++i)
3760 {
3761 p = home_replace_save(NULL, files[i]);
3762 if (p != NULL)
3763 {
3764 vim_free(files[i]);
3765 files[i] = p;
3766 }
3767 }
3768 }
3769}
3770
3771/*
3772 * Show all matches for completion on the command line.
3773 * Returns EXPAND_NOTHING when the character that triggered expansion should
3774 * be inserted like a normal character.
3775 */
3776/*ARGSUSED*/
3777 static int
3778showmatches(xp, wildmenu)
3779 expand_T *xp;
3780 int wildmenu;
3781{
3782#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3783 int num_files;
3784 char_u **files_found;
3785 int i, j, k;
3786 int maxlen;
3787 int lines;
3788 int columns;
3789 char_u *p;
3790 int lastlen;
3791 int attr;
3792 int showtail;
3793
3794 if (xp->xp_numfiles == -1)
3795 {
3796 set_expand_context(xp);
3797 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3798 &num_files, &files_found);
3799 showtail = expand_showtail(xp);
3800 if (i != EXPAND_OK)
3801 return i;
3802
3803 }
3804 else
3805 {
3806 num_files = xp->xp_numfiles;
3807 files_found = xp->xp_files;
3808 showtail = cmd_showtail;
3809 }
3810
3811#ifdef FEAT_WILDMENU
3812 if (!wildmenu)
3813 {
3814#endif
3815 msg_didany = FALSE; /* lines_left will be set */
3816 msg_start(); /* prepare for paging */
3817 msg_putchar('\n');
3818 out_flush();
3819 cmdline_row = msg_row;
3820 msg_didany = FALSE; /* lines_left will be set again */
3821 msg_start(); /* prepare for paging */
3822#ifdef FEAT_WILDMENU
3823 }
3824#endif
3825
3826 if (got_int)
3827 got_int = FALSE; /* only int. the completion, not the cmd line */
3828#ifdef FEAT_WILDMENU
3829 else if (wildmenu)
3830 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3831#endif
3832 else
3833 {
3834 /* find the length of the longest file name */
3835 maxlen = 0;
3836 for (i = 0; i < num_files; ++i)
3837 {
3838 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003839 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 || xp->xp_context == EXPAND_BUFFERS))
3841 {
3842 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3843 j = vim_strsize(NameBuff);
3844 }
3845 else
3846 j = vim_strsize(L_SHOWFILE(i));
3847 if (j > maxlen)
3848 maxlen = j;
3849 }
3850
3851 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3852 lines = num_files;
3853 else
3854 {
3855 /* compute the number of columns and lines for the listing */
3856 maxlen += 2; /* two spaces between file names */
3857 columns = ((int)Columns + 2) / maxlen;
3858 if (columns < 1)
3859 columns = 1;
3860 lines = (num_files + columns - 1) / columns;
3861 }
3862
3863 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3864
3865 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3866 {
3867 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3868 msg_clr_eos();
3869 msg_advance(maxlen - 3);
3870 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3871 }
3872
3873 /* list the files line by line */
3874 for (i = 0; i < lines; ++i)
3875 {
3876 lastlen = 999;
3877 for (k = i; k < num_files; k += lines)
3878 {
3879 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3880 {
3881 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3882 p = files_found[k] + STRLEN(files_found[k]) + 1;
3883 msg_advance(maxlen + 1);
3884 msg_puts(p);
3885 msg_advance(maxlen + 3);
3886 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3887 break;
3888 }
3889 for (j = maxlen - lastlen; --j >= 0; )
3890 msg_putchar(' ');
3891 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003892 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 || xp->xp_context == EXPAND_BUFFERS)
3894 {
3895 /* highlight directories */
3896 j = (mch_isdir(files_found[k]));
3897 if (showtail)
3898 p = L_SHOWFILE(k);
3899 else
3900 {
3901 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3902 TRUE);
3903 p = NameBuff;
3904 }
3905 }
3906 else
3907 {
3908 j = FALSE;
3909 p = L_SHOWFILE(k);
3910 }
3911 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3912 }
3913 if (msg_col > 0) /* when not wrapped around */
3914 {
3915 msg_clr_eos();
3916 msg_putchar('\n');
3917 }
3918 out_flush(); /* show one line at a time */
3919 if (got_int)
3920 {
3921 got_int = FALSE;
3922 break;
3923 }
3924 }
3925
3926 /*
3927 * we redraw the command below the lines that we have just listed
3928 * This is a bit tricky, but it saves a lot of screen updating.
3929 */
3930 cmdline_row = msg_row; /* will put it back later */
3931 }
3932
3933 if (xp->xp_numfiles == -1)
3934 FreeWild(num_files, files_found);
3935
3936 return EXPAND_OK;
3937}
3938
3939/*
3940 * Private gettail for showmatches() (and win_redr_status_matches()):
3941 * Find tail of file name path, but ignore trailing "/".
3942 */
3943 char_u *
3944sm_gettail(s)
3945 char_u *s;
3946{
3947 char_u *p;
3948 char_u *t = s;
3949 int had_sep = FALSE;
3950
3951 for (p = s; *p != NUL; )
3952 {
3953 if (vim_ispathsep(*p)
3954#ifdef BACKSLASH_IN_FILENAME
3955 && !rem_backslash(p)
3956#endif
3957 )
3958 had_sep = TRUE;
3959 else if (had_sep)
3960 {
3961 t = p;
3962 had_sep = FALSE;
3963 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003964 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
3966 return t;
3967}
3968
3969/*
3970 * Return TRUE if we only need to show the tail of completion matches.
3971 * When not completing file names or there is a wildcard in the path FALSE is
3972 * returned.
3973 */
3974 static int
3975expand_showtail(xp)
3976 expand_T *xp;
3977{
3978 char_u *s;
3979 char_u *end;
3980
3981 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003982 if (xp->xp_context != EXPAND_FILES
3983 && xp->xp_context != EXPAND_SHELLCMD
3984 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 return FALSE;
3986
3987 end = gettail(xp->xp_pattern);
3988 if (end == xp->xp_pattern) /* there is no path separator */
3989 return FALSE;
3990
3991 for (s = xp->xp_pattern; s < end; s++)
3992 {
3993 /* Skip escaped wildcards. Only when the backslash is not a path
3994 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3995 if (rem_backslash(s))
3996 ++s;
3997 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3998 return FALSE;
3999 }
4000 return TRUE;
4001}
4002
4003/*
4004 * Prepare a string for expansion.
4005 * When expanding file names: The string will be used with expand_wildcards().
4006 * Copy the file name into allocated memory and add a '*' at the end.
4007 * When expanding other names: The string will be used with regcomp(). Copy
4008 * the name into allocated memory and prepend "^".
4009 */
4010 char_u *
4011addstar(fname, len, context)
4012 char_u *fname;
4013 int len;
4014 int context; /* EXPAND_FILES etc. */
4015{
4016 char_u *retval;
4017 int i, j;
4018 int new_len;
4019 char_u *tail;
4020
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004021 if (context != EXPAND_FILES
4022 && context != EXPAND_SHELLCMD
4023 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
4025 /*
4026 * Matching will be done internally (on something other than files).
4027 * So we convert the file-matching-type wildcards into our kind for
4028 * use with vim_regcomp(). First work out how long it will be:
4029 */
4030
4031 /* For help tags the translation is done in find_help_tags().
4032 * For a tag pattern starting with "/" no translation is needed. */
4033 if (context == EXPAND_HELP
4034 || context == EXPAND_COLORS
4035 || context == EXPAND_COMPILER
4036 || (context == EXPAND_TAGS && fname[0] == '/'))
4037 retval = vim_strnsave(fname, len);
4038 else
4039 {
4040 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4041 for (i = 0; i < len; i++)
4042 {
4043 if (fname[i] == '*' || fname[i] == '~')
4044 new_len++; /* '*' needs to be replaced by ".*"
4045 '~' needs to be replaced by "\~" */
4046
4047 /* Buffer names are like file names. "." should be literal */
4048 if (context == EXPAND_BUFFERS && fname[i] == '.')
4049 new_len++; /* "." becomes "\." */
4050
4051 /* Custom expansion takes care of special things, match
4052 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004053 if ((context == EXPAND_USER_DEFINED
4054 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 new_len++; /* '\' becomes "\\" */
4056 }
4057 retval = alloc(new_len);
4058 if (retval != NULL)
4059 {
4060 retval[0] = '^';
4061 j = 1;
4062 for (i = 0; i < len; i++, j++)
4063 {
4064 /* Skip backslash. But why? At least keep it for custom
4065 * expansion. */
4066 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004067 && context != EXPAND_USER_LIST
4068 && fname[i] == '\\'
4069 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 break;
4071
4072 switch (fname[i])
4073 {
4074 case '*': retval[j++] = '.';
4075 break;
4076 case '~': retval[j++] = '\\';
4077 break;
4078 case '?': retval[j] = '.';
4079 continue;
4080 case '.': if (context == EXPAND_BUFFERS)
4081 retval[j++] = '\\';
4082 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004083 case '\\': if (context == EXPAND_USER_DEFINED
4084 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 retval[j++] = '\\';
4086 break;
4087 }
4088 retval[j] = fname[i];
4089 }
4090 retval[j] = NUL;
4091 }
4092 }
4093 }
4094 else
4095 {
4096 retval = alloc(len + 4);
4097 if (retval != NULL)
4098 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004099 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
4101 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004102 * Don't add a star to *, ~, ~user, $var or `cmd`.
4103 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 * ~ would be at the start of the file name, but not the tail.
4105 * $ could be anywhere in the tail.
4106 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004107 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 */
4109 tail = gettail(retval);
4110 if ((*retval != '~' || tail != retval)
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004111 && (len == 0 || retval[len - 1] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 && vim_strchr(tail, '$') == NULL
4113 && vim_strchr(retval, '`') == NULL)
4114 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004115 else if (len > 0 && retval[len - 1] == '$')
4116 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 retval[len] = NUL;
4118 }
4119 }
4120 return retval;
4121}
4122
4123/*
4124 * Must parse the command line so far to work out what context we are in.
4125 * Completion can then be done based on that context.
4126 * This routine sets the variables:
4127 * xp->xp_pattern The start of the pattern to be expanded within
4128 * the command line (ends at the cursor).
4129 * xp->xp_context The type of thing to expand. Will be one of:
4130 *
4131 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4132 * the command line, like an unknown command. Caller
4133 * should beep.
4134 * EXPAND_NOTHING Unrecognised context for completion, use char like
4135 * a normal char, rather than for completion. eg
4136 * :s/^I/
4137 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4138 * it.
4139 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4140 * EXPAND_FILES After command with XFILE set, or after setting
4141 * with P_EXPAND set. eg :e ^I, :w>>^I
4142 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4143 * when we know only directories are of interest. eg
4144 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004145 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4147 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4148 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4149 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4150 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4151 * EXPAND_EVENTS Complete event names
4152 * EXPAND_SYNTAX Complete :syntax command arguments
4153 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4154 * EXPAND_AUGROUP Complete autocommand group names
4155 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4156 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4157 * eg :unmap a^I , :cunab x^I
4158 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4159 * eg :call sub^I
4160 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4161 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4162 * names in expressions, eg :while s^I
4163 * EXPAND_ENV_VARS Complete environment variable names
4164 */
4165 static void
4166set_expand_context(xp)
4167 expand_T *xp;
4168{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004169 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 if (ccline.cmdfirstc != ':'
4171#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004172 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004173 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174#endif
4175 )
4176 {
4177 xp->xp_context = EXPAND_NOTHING;
4178 return;
4179 }
4180 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4181}
4182
4183 void
4184set_cmd_context(xp, str, len, col)
4185 expand_T *xp;
4186 char_u *str; /* start of command line */
4187 int len; /* length of command line (excl. NUL) */
4188 int col; /* position of cursor */
4189{
4190 int old_char = NUL;
4191 char_u *nextcomm;
4192
4193 /*
4194 * Avoid a UMR warning from Purify, only save the character if it has been
4195 * written before.
4196 */
4197 if (col < len)
4198 old_char = str[col];
4199 str[col] = NUL;
4200 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004201
4202#ifdef FEAT_EVAL
4203 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004204 {
4205# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004206 /* pass CMD_SIZE because there is no real command */
4207 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004208# endif
4209 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004210 else if (ccline.input_fn)
4211 {
4212 xp->xp_context = ccline.xp_context;
4213 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004214# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004215 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004216# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004217 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004218 else
4219#endif
4220 while (nextcomm != NULL)
4221 nextcomm = set_one_cmd_context(xp, nextcomm);
4222
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 str[col] = old_char;
4224}
4225
4226/*
4227 * Expand the command line "str" from context "xp".
4228 * "xp" must have been set by set_cmd_context().
4229 * xp->xp_pattern points into "str", to where the text that is to be expanded
4230 * starts.
4231 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4232 * cursor.
4233 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4234 * key that triggered expansion literally.
4235 * Returns EXPAND_OK otherwise.
4236 */
4237 int
4238expand_cmdline(xp, str, col, matchcount, matches)
4239 expand_T *xp;
4240 char_u *str; /* start of command line */
4241 int col; /* position of cursor */
4242 int *matchcount; /* return: nr of matches */
4243 char_u ***matches; /* return: array of pointers to matches */
4244{
4245 char_u *file_str = NULL;
4246
4247 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4248 {
4249 beep_flush();
4250 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4251 }
4252 if (xp->xp_context == EXPAND_NOTHING)
4253 {
4254 /* Caller can use the character as a normal char instead */
4255 return EXPAND_NOTHING;
4256 }
4257
4258 /* add star to file name, or convert to regexp if not exp. files. */
4259 file_str = addstar(xp->xp_pattern,
4260 (int)(str + col - xp->xp_pattern), xp->xp_context);
4261 if (file_str == NULL)
4262 return EXPAND_UNSUCCESSFUL;
4263
4264 /* find all files that match the description */
4265 if (ExpandFromContext(xp, file_str, matchcount, matches,
4266 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4267 {
4268 *matchcount = 0;
4269 *matches = NULL;
4270 }
4271 vim_free(file_str);
4272
4273 return EXPAND_OK;
4274}
4275
4276#ifdef FEAT_MULTI_LANG
4277/*
4278 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4279 */
4280static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4281
4282 static void
4283cleanup_help_tags(num_file, file)
4284 int num_file;
4285 char_u **file;
4286{
4287 int i, j;
4288 int len;
4289
4290 for (i = 0; i < num_file; ++i)
4291 {
4292 len = (int)STRLEN(file[i]) - 3;
4293 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4294 {
4295 /* Sorting on priority means the same item in another language may
4296 * be anywhere. Search all items for a match up to the "@en". */
4297 for (j = 0; j < num_file; ++j)
4298 if (j != i
4299 && (int)STRLEN(file[j]) == len + 3
4300 && STRNCMP(file[i], file[j], len + 1) == 0)
4301 break;
4302 if (j == num_file)
4303 file[i][len] = NUL;
4304 }
4305 }
4306}
4307#endif
4308
4309/*
4310 * Do the expansion based on xp->xp_context and "pat".
4311 */
4312 static int
4313ExpandFromContext(xp, pat, num_file, file, options)
4314 expand_T *xp;
4315 char_u *pat;
4316 int *num_file;
4317 char_u ***file;
4318 int options;
4319{
4320#ifdef FEAT_CMDL_COMPL
4321 regmatch_T regmatch;
4322#endif
4323 int ret;
4324 int flags;
4325
4326 flags = EW_DIR; /* include directories */
4327 if (options & WILD_LIST_NOTFOUND)
4328 flags |= EW_NOTFOUND;
4329 if (options & WILD_ADD_SLASH)
4330 flags |= EW_ADDSLASH;
4331 if (options & WILD_KEEP_ALL)
4332 flags |= EW_KEEPALL;
4333 if (options & WILD_SILENT)
4334 flags |= EW_SILENT;
4335
4336 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4337 {
4338 /*
4339 * Expand file or directory names.
4340 */
4341 int free_pat = FALSE;
4342 int i;
4343
4344 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4345 * space */
4346 if (xp->xp_backslash != XP_BS_NONE)
4347 {
4348 free_pat = TRUE;
4349 pat = vim_strsave(pat);
4350 for (i = 0; pat[i]; ++i)
4351 if (pat[i] == '\\')
4352 {
4353 if (xp->xp_backslash == XP_BS_THREE
4354 && pat[i + 1] == '\\'
4355 && pat[i + 2] == '\\'
4356 && pat[i + 3] == ' ')
Bram Moolenaar452a81b2007-08-06 20:28:43 +00004357 mch_memmove(pat + i, pat + i + 3,
4358 STRLEN(pat + i + 3) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 if (xp->xp_backslash == XP_BS_ONE
4360 && pat[i + 1] == ' ')
Bram Moolenaar452a81b2007-08-06 20:28:43 +00004361 mch_memmove(pat + i, pat + i + 1, STRLEN(pat + i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363 }
4364
4365 if (xp->xp_context == EXPAND_FILES)
4366 flags |= EW_FILE;
4367 else
4368 flags = (flags | EW_DIR) & ~EW_FILE;
4369 ret = expand_wildcards(1, &pat, num_file, file, flags);
4370 if (free_pat)
4371 vim_free(pat);
4372 return ret;
4373 }
4374
4375 *file = (char_u **)"";
4376 *num_file = 0;
4377 if (xp->xp_context == EXPAND_HELP)
4378 {
4379 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4380 {
4381#ifdef FEAT_MULTI_LANG
4382 cleanup_help_tags(*num_file, *file);
4383#endif
4384 return OK;
4385 }
4386 return FAIL;
4387 }
4388
4389#ifndef FEAT_CMDL_COMPL
4390 return FAIL;
4391#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004392 if (xp->xp_context == EXPAND_SHELLCMD)
4393 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 if (xp->xp_context == EXPAND_OLD_SETTING)
4395 return ExpandOldSetting(num_file, file);
4396 if (xp->xp_context == EXPAND_BUFFERS)
4397 return ExpandBufnames(pat, num_file, file, options);
4398 if (xp->xp_context == EXPAND_TAGS
4399 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4400 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4401 if (xp->xp_context == EXPAND_COLORS)
4402 return ExpandRTDir(pat, num_file, file, "colors");
4403 if (xp->xp_context == EXPAND_COMPILER)
4404 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004405# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4406 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004407 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
4410 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4411 if (regmatch.regprog == NULL)
4412 return FAIL;
4413
4414 /* set ignore-case according to p_ic, p_scs and pat */
4415 regmatch.rm_ic = ignorecase(pat);
4416
4417 if (xp->xp_context == EXPAND_SETTINGS
4418 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4419 ret = ExpandSettings(xp, &regmatch, num_file, file);
4420 else if (xp->xp_context == EXPAND_MAPPINGS)
4421 ret = ExpandMappings(&regmatch, num_file, file);
4422# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4423 else if (xp->xp_context == EXPAND_USER_DEFINED)
4424 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4425# endif
4426 else
4427 {
4428 static struct expgen
4429 {
4430 int context;
4431 char_u *((*func)__ARGS((expand_T *, int)));
4432 int ic;
4433 } tab[] =
4434 {
4435 {EXPAND_COMMANDS, get_command_name, FALSE},
4436#ifdef FEAT_USR_CMDS
4437 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4438 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4439 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4440 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4441#endif
4442#ifdef FEAT_EVAL
4443 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4444 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4445 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4446 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4447#endif
4448#ifdef FEAT_MENU
4449 {EXPAND_MENUS, get_menu_name, FALSE},
4450 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4451#endif
4452#ifdef FEAT_SYN_HL
4453 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4454#endif
4455 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4456#ifdef FEAT_AUTOCMD
4457 {EXPAND_EVENTS, get_event_name, TRUE},
4458 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4459#endif
4460#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4461 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4462 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4463#endif
4464 {EXPAND_ENV_VARS, get_env_name, TRUE},
4465 };
4466 int i;
4467
4468 /*
4469 * Find a context in the table and call the ExpandGeneric() with the
4470 * right function to do the expansion.
4471 */
4472 ret = FAIL;
4473 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4474 if (xp->xp_context == tab[i].context)
4475 {
4476 if (tab[i].ic)
4477 regmatch.rm_ic = TRUE;
4478 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4479 break;
4480 }
4481 }
4482
4483 vim_free(regmatch.regprog);
4484
4485 return ret;
4486#endif /* FEAT_CMDL_COMPL */
4487}
4488
4489#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4490/*
4491 * Expand a list of names.
4492 *
4493 * Generic function for command line completion. It calls a function to
4494 * obtain strings, one by one. The strings are matched against a regexp
4495 * program. Matching strings are copied into an array, which is returned.
4496 *
4497 * Returns OK when no problems encountered, FAIL for error (out of memory).
4498 */
4499 int
4500ExpandGeneric(xp, regmatch, num_file, file, func)
4501 expand_T *xp;
4502 regmatch_T *regmatch;
4503 int *num_file;
4504 char_u ***file;
4505 char_u *((*func)__ARGS((expand_T *, int)));
4506 /* returns a string from the list */
4507{
4508 int i;
4509 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004510 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 char_u *str;
4512
4513 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004514 * round == 0: count the number of matching names
4515 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004517 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 {
4519 for (i = 0; ; ++i)
4520 {
4521 str = (*func)(xp, i);
4522 if (str == NULL) /* end of list */
4523 break;
4524 if (*str == NUL) /* skip empty strings */
4525 continue;
4526
4527 if (vim_regexec(regmatch, str, (colnr_T)0))
4528 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004529 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
4531 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4532 (*file)[count] = str;
4533#ifdef FEAT_MENU
4534 if (func == get_menu_names && str != NULL)
4535 {
4536 /* test for separator added by get_menu_names() */
4537 str += STRLEN(str) - 1;
4538 if (*str == '\001')
4539 *str = '.';
4540 }
4541#endif
4542 }
4543 ++count;
4544 }
4545 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004546 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 {
4548 if (count == 0)
4549 return OK;
4550 *num_file = count;
4551 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4552 if (*file == NULL)
4553 {
4554 *file = (char_u **)"";
4555 return FAIL;
4556 }
4557 count = 0;
4558 }
4559 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004560
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004561 /* Sort the results. Keep menu's in the specified order. */
4562 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4563 sort_strings(*file, *num_file);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004564
Bram Moolenaar4f688582007-07-24 12:34:30 +00004565#ifdef FEAT_CMDL_COMPL
4566 /* Reset the variables used for special highlight names expansion, so that
4567 * they don't show up when getting normal highlight names by ID. */
4568 reset_expand_highlight();
4569#endif
4570
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 return OK;
4572}
4573
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004574/*
4575 * Complete a shell command.
4576 * Returns FAIL or OK;
4577 */
4578 static int
4579expand_shellcmd(filepat, num_file, file, flagsarg)
4580 char_u *filepat; /* pattern to match with command names */
4581 int *num_file; /* return: number of matches */
4582 char_u ***file; /* return: array with matches */
4583 int flagsarg; /* EW_ flags */
4584{
4585 char_u *pat;
4586 int i;
4587 char_u *path;
4588 int mustfree = FALSE;
4589 garray_T ga;
4590 char_u *buf = alloc(MAXPATHL);
4591 size_t l;
4592 char_u *s, *e;
4593 int flags = flagsarg;
4594 int ret;
4595
4596 if (buf == NULL)
4597 return FAIL;
4598
4599 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4600 * space */
4601 pat = vim_strsave(filepat);
4602 for (i = 0; pat[i]; ++i)
4603 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar452a81b2007-08-06 20:28:43 +00004604 mch_memmove(pat + i, pat + i + 1, STRLEN(pat + i));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004605
4606 flags |= EW_FILE | EW_EXEC;
4607
4608 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004609 if (mch_isFullName(pat))
4610 path = (char_u *)" ";
4611 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004612 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4613 path = (char_u *)".";
4614 else
4615 path = vim_getenv((char_u *)"PATH", &mustfree);
4616
4617 /*
4618 * Go over all directories in $PATH. Expand matches in that directory and
4619 * collect them in "ga".
4620 */
4621 ga_init2(&ga, (int)sizeof(char *), 10);
4622 for (s = path; *s != NUL; s = e)
4623 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004624 if (*s == ' ')
4625 ++s; /* Skip space used for absolute path name. */
4626
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004627#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4628 e = vim_strchr(s, ';');
4629#else
4630 e = vim_strchr(s, ':');
4631#endif
4632 if (e == NULL)
4633 e = s + STRLEN(s);
4634
4635 l = e - s;
4636 if (l > MAXPATHL - 5)
4637 break;
4638 vim_strncpy(buf, s, l);
4639 add_pathsep(buf);
4640 l = STRLEN(buf);
4641 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4642
4643 /* Expand matches in one directory of $PATH. */
4644 ret = expand_wildcards(1, &buf, num_file, file, flags);
4645 if (ret == OK)
4646 {
4647 if (ga_grow(&ga, *num_file) == FAIL)
4648 FreeWild(*num_file, *file);
4649 else
4650 {
4651 for (i = 0; i < *num_file; ++i)
4652 {
4653 s = (*file)[i];
4654 if (STRLEN(s) > l)
4655 {
4656 /* Remove the path again. */
4657 mch_memmove(s, s + l, STRLEN(s + l) + 1);
4658 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4659 }
4660 else
4661 vim_free(s);
4662 }
4663 vim_free(*file);
4664 }
4665 }
4666 if (*e != NUL)
4667 ++e;
4668 }
4669 *file = ga.ga_data;
4670 *num_file = ga.ga_len;
4671
4672 vim_free(buf);
4673 vim_free(pat);
4674 if (mustfree)
4675 vim_free(path);
4676 return OK;
4677}
4678
4679
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004681static 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));
4682
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004684 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004685 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004687 static void *
4688call_user_expand_func(user_expand_func, xp, num_file, file)
4689 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 int *num_file;
4692 char_u ***file;
4693{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004694 char_u keep;
4695 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004698 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004699 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
4701 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004702 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 *num_file = 0;
4704 *file = NULL;
4705
Bram Moolenaar21669c02008-01-18 12:16:16 +00004706 if (ccline.cmdbuff == NULL)
4707 {
4708 /* Completion from Insert mode, pass fake arguments. */
4709 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004710 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004711 args[1] = xp->xp_pattern;
4712 }
4713 else
4714 {
4715 /* Completion on the command line, pass real arguments. */
4716 keep = ccline.cmdbuff[ccline.cmdlen];
4717 ccline.cmdbuff[ccline.cmdlen] = 0;
4718 sprintf((char *)num, "%d", ccline.cmdpos);
4719 args[1] = ccline.cmdbuff;
4720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 args[0] = xp->xp_pattern;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 args[2] = num;
4723
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004724 /* Save the cmdline, we don't know what the function may do. */
4725 save_ccline = ccline;
4726 ccline.cmdbuff = NULL;
4727 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004729
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004730 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004731
4732 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004734 if (ccline.cmdbuff != NULL)
4735 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004736
4737 return ret;
4738}
4739
4740/*
4741 * Expand names with a function defined by the user.
4742 */
4743 static int
4744ExpandUserDefined(xp, regmatch, num_file, file)
4745 expand_T *xp;
4746 regmatch_T *regmatch;
4747 int *num_file;
4748 char_u ***file;
4749{
4750 char_u *retstr;
4751 char_u *s;
4752 char_u *e;
4753 char_u keep;
4754 garray_T ga;
4755
4756 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4757 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 return FAIL;
4759
4760 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004761 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
4763 e = vim_strchr(s, '\n');
4764 if (e == NULL)
4765 e = s + STRLEN(s);
4766 keep = *e;
4767 *e = 0;
4768
4769 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4770 {
4771 *e = keep;
4772 if (*e != NUL)
4773 ++e;
4774 continue;
4775 }
4776
4777 if (ga_grow(&ga, 1) == FAIL)
4778 break;
4779
4780 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4781 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782
4783 *e = keep;
4784 if (*e != NUL)
4785 ++e;
4786 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004787 vim_free(retstr);
4788 *file = ga.ga_data;
4789 *num_file = ga.ga_len;
4790 return OK;
4791}
4792
4793/*
4794 * Expand names with a list returned by a function defined by the user.
4795 */
4796 static int
4797ExpandUserList(xp, num_file, file)
4798 expand_T *xp;
4799 int *num_file;
4800 char_u ***file;
4801{
4802 list_T *retlist;
4803 listitem_T *li;
4804 garray_T ga;
4805
4806 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4807 if (retlist == NULL)
4808 return FAIL;
4809
4810 ga_init2(&ga, (int)sizeof(char *), 3);
4811 /* Loop over the items in the list. */
4812 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4813 {
4814 if (li->li_tv.v_type != VAR_STRING)
4815 continue; /* Skip non-string items */
4816
4817 if (ga_grow(&ga, 1) == FAIL)
4818 break;
4819
4820 ((char_u **)ga.ga_data)[ga.ga_len] =
4821 vim_strsave(li->li_tv.vval.v_string);
4822 ++ga.ga_len;
4823 }
4824 list_unref(retlist);
4825
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 *file = ga.ga_data;
4827 *num_file = ga.ga_len;
4828 return OK;
4829}
4830#endif
4831
4832/*
4833 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4834 * or compiler names.
4835 */
4836 static int
4837ExpandRTDir(pat, num_file, file, dirname)
4838 char_u *pat;
4839 int *num_file;
4840 char_u ***file;
4841 char *dirname; /* "colors" or "compiler" */
4842{
4843 char_u *all;
4844 char_u *s;
4845 char_u *e;
4846 garray_T ga;
4847
4848 *num_file = 0;
4849 *file = NULL;
4850 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4851 if (s == NULL)
4852 return FAIL;
4853 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4854 all = globpath(p_rtp, s);
4855 vim_free(s);
4856 if (all == NULL)
4857 return FAIL;
4858
4859 ga_init2(&ga, (int)sizeof(char *), 3);
4860 for (s = all; *s != NUL; s = e)
4861 {
4862 e = vim_strchr(s, '\n');
4863 if (e == NULL)
4864 e = s + STRLEN(s);
4865 if (ga_grow(&ga, 1) == FAIL)
4866 break;
4867 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4868 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004869 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 if (*s == '\n' || vim_ispathsep(*s))
4871 break;
4872 ++s;
4873 ((char_u **)ga.ga_data)[ga.ga_len] =
4874 vim_strnsave(s, (int)(e - s - 4));
4875 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 }
4877 if (*e != NUL)
4878 ++e;
4879 }
4880 vim_free(all);
4881 *file = ga.ga_data;
4882 *num_file = ga.ga_len;
4883 return OK;
4884}
4885
4886#endif
4887
4888#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4889/*
4890 * Expand "file" for all comma-separated directories in "path".
4891 * Returns an allocated string with all matches concatenated, separated by
4892 * newlines. Returns NULL for an error or no matches.
4893 */
4894 char_u *
4895globpath(path, file)
4896 char_u *path;
4897 char_u *file;
4898{
4899 expand_T xpc;
4900 char_u *buf;
4901 garray_T ga;
4902 int i;
4903 int len;
4904 int num_p;
4905 char_u **p;
4906 char_u *cur = NULL;
4907
4908 buf = alloc(MAXPATHL);
4909 if (buf == NULL)
4910 return NULL;
4911
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004912 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004914
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 ga_init2(&ga, 1, 100);
4916
4917 /* Loop over all entries in {path}. */
4918 while (*path != NUL)
4919 {
4920 /* Copy one item of the path to buf[] and concatenate the file name. */
4921 copy_option_part(&path, buf, MAXPATHL, ",");
4922 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4923 {
4924 add_pathsep(buf);
4925 STRCAT(buf, file);
4926 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4927 && num_p > 0)
4928 {
4929 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4930 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004931 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
4933 /* Concatenate new results to previous ones. */
4934 if (ga_grow(&ga, len) == OK)
4935 {
4936 cur = (char_u *)ga.ga_data + ga.ga_len;
4937 for (i = 0; i < num_p; ++i)
4938 {
4939 STRCPY(cur, p[i]);
4940 cur += STRLEN(p[i]);
4941 *cur++ = '\n';
4942 }
4943 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
4945 FreeWild(num_p, p);
4946 }
4947 }
4948 }
4949 if (cur != NULL)
4950 *--cur = 0; /* Replace trailing newline with NUL */
4951
4952 vim_free(buf);
4953 return (char_u *)ga.ga_data;
4954}
4955
4956#endif
4957
4958#if defined(FEAT_CMDHIST) || defined(PROTO)
4959
4960/*********************************
4961 * Command line history stuff *
4962 *********************************/
4963
4964/*
4965 * Translate a history character to the associated type number.
4966 */
4967 static int
4968hist_char2type(c)
4969 int c;
4970{
4971 if (c == ':')
4972 return HIST_CMD;
4973 if (c == '=')
4974 return HIST_EXPR;
4975 if (c == '@')
4976 return HIST_INPUT;
4977 if (c == '>')
4978 return HIST_DEBUG;
4979 return HIST_SEARCH; /* must be '?' or '/' */
4980}
4981
4982/*
4983 * Table of history names.
4984 * These names are used in :history and various hist...() functions.
4985 * It is sufficient to give the significant prefix of a history name.
4986 */
4987
4988static char *(history_names[]) =
4989{
4990 "cmd",
4991 "search",
4992 "expr",
4993 "input",
4994 "debug",
4995 NULL
4996};
4997
4998/*
4999 * init_history() - Initialize the command line history.
5000 * Also used to re-allocate the history when the size changes.
5001 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005002 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003init_history()
5004{
5005 int newlen; /* new length of history table */
5006 histentry_T *temp;
5007 int i;
5008 int j;
5009 int type;
5010
5011 /*
5012 * If size of history table changed, reallocate it
5013 */
5014 newlen = (int)p_hi;
5015 if (newlen != hislen) /* history length changed */
5016 {
5017 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5018 {
5019 if (newlen)
5020 {
5021 temp = (histentry_T *)lalloc(
5022 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5023 if (temp == NULL) /* out of memory! */
5024 {
5025 if (type == 0) /* first one: just keep the old length */
5026 {
5027 newlen = hislen;
5028 break;
5029 }
5030 /* Already changed one table, now we can only have zero
5031 * length for all tables. */
5032 newlen = 0;
5033 type = -1;
5034 continue;
5035 }
5036 }
5037 else
5038 temp = NULL;
5039 if (newlen == 0 || temp != NULL)
5040 {
5041 if (hisidx[type] < 0) /* there are no entries yet */
5042 {
5043 for (i = 0; i < newlen; ++i)
5044 {
5045 temp[i].hisnum = 0;
5046 temp[i].hisstr = NULL;
5047 }
5048 }
5049 else if (newlen > hislen) /* array becomes bigger */
5050 {
5051 for (i = 0; i <= hisidx[type]; ++i)
5052 temp[i] = history[type][i];
5053 j = i;
5054 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5055 {
5056 temp[i].hisnum = 0;
5057 temp[i].hisstr = NULL;
5058 }
5059 for ( ; j < hislen; ++i, ++j)
5060 temp[i] = history[type][j];
5061 }
5062 else /* array becomes smaller or 0 */
5063 {
5064 j = hisidx[type];
5065 for (i = newlen - 1; ; --i)
5066 {
5067 if (i >= 0) /* copy newest entries */
5068 temp[i] = history[type][j];
5069 else /* remove older entries */
5070 vim_free(history[type][j].hisstr);
5071 if (--j < 0)
5072 j = hislen - 1;
5073 if (j == hisidx[type])
5074 break;
5075 }
5076 hisidx[type] = newlen - 1;
5077 }
5078 vim_free(history[type]);
5079 history[type] = temp;
5080 }
5081 }
5082 hislen = newlen;
5083 }
5084}
5085
5086/*
5087 * Check if command line 'str' is already in history.
5088 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5089 */
5090 static int
5091in_history(type, str, move_to_front)
5092 int type;
5093 char_u *str;
5094 int move_to_front; /* Move the entry to the front if it exists */
5095{
5096 int i;
5097 int last_i = -1;
5098
5099 if (hisidx[type] < 0)
5100 return FALSE;
5101 i = hisidx[type];
5102 do
5103 {
5104 if (history[type][i].hisstr == NULL)
5105 return FALSE;
5106 if (STRCMP(str, history[type][i].hisstr) == 0)
5107 {
5108 if (!move_to_front)
5109 return TRUE;
5110 last_i = i;
5111 break;
5112 }
5113 if (--i < 0)
5114 i = hislen - 1;
5115 } while (i != hisidx[type]);
5116
5117 if (last_i >= 0)
5118 {
5119 str = history[type][i].hisstr;
5120 while (i != hisidx[type])
5121 {
5122 if (++i >= hislen)
5123 i = 0;
5124 history[type][last_i] = history[type][i];
5125 last_i = i;
5126 }
5127 history[type][i].hisstr = str;
5128 history[type][i].hisnum = ++hisnum[type];
5129 return TRUE;
5130 }
5131 return FALSE;
5132}
5133
5134/*
5135 * Convert history name (from table above) to its HIST_ equivalent.
5136 * When "name" is empty, return "cmd" history.
5137 * Returns -1 for unknown history name.
5138 */
5139 int
5140get_histtype(name)
5141 char_u *name;
5142{
5143 int i;
5144 int len = (int)STRLEN(name);
5145
5146 /* No argument: use current history. */
5147 if (len == 0)
5148 return hist_char2type(ccline.cmdfirstc);
5149
5150 for (i = 0; history_names[i] != NULL; ++i)
5151 if (STRNICMP(name, history_names[i], len) == 0)
5152 return i;
5153
5154 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5155 return hist_char2type(name[0]);
5156
5157 return -1;
5158}
5159
5160static int last_maptick = -1; /* last seen maptick */
5161
5162/*
5163 * Add the given string to the given history. If the string is already in the
5164 * history then it is moved to the front. "histype" may be one of he HIST_
5165 * values.
5166 */
5167 void
5168add_to_history(histype, new_entry, in_map, sep)
5169 int histype;
5170 char_u *new_entry;
5171 int in_map; /* consider maptick when inside a mapping */
5172 int sep; /* separator character used (search hist) */
5173{
5174 histentry_T *hisptr;
5175 int len;
5176
5177 if (hislen == 0) /* no history */
5178 return;
5179
5180 /*
5181 * Searches inside the same mapping overwrite each other, so that only
5182 * the last line is kept. Be careful not to remove a line that was moved
5183 * down, only lines that were added.
5184 */
5185 if (histype == HIST_SEARCH && in_map)
5186 {
5187 if (maptick == last_maptick)
5188 {
5189 /* Current line is from the same mapping, remove it */
5190 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5191 vim_free(hisptr->hisstr);
5192 hisptr->hisstr = NULL;
5193 hisptr->hisnum = 0;
5194 --hisnum[histype];
5195 if (--hisidx[HIST_SEARCH] < 0)
5196 hisidx[HIST_SEARCH] = hislen - 1;
5197 }
5198 last_maptick = -1;
5199 }
5200 if (!in_history(histype, new_entry, TRUE))
5201 {
5202 if (++hisidx[histype] == hislen)
5203 hisidx[histype] = 0;
5204 hisptr = &history[histype][hisidx[histype]];
5205 vim_free(hisptr->hisstr);
5206
5207 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005208 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5210 if (hisptr->hisstr != NULL)
5211 hisptr->hisstr[len + 1] = sep;
5212
5213 hisptr->hisnum = ++hisnum[histype];
5214 if (histype == HIST_SEARCH && in_map)
5215 last_maptick = maptick;
5216 }
5217}
5218
5219#if defined(FEAT_EVAL) || defined(PROTO)
5220
5221/*
5222 * Get identifier of newest history entry.
5223 * "histype" may be one of the HIST_ values.
5224 */
5225 int
5226get_history_idx(histype)
5227 int histype;
5228{
5229 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5230 || hisidx[histype] < 0)
5231 return -1;
5232
5233 return history[histype][hisidx[histype]].hisnum;
5234}
5235
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005236static struct cmdline_info *get_ccline_ptr __ARGS((void));
5237
5238/*
5239 * Get pointer to the command line info to use. cmdline_paste() may clear
5240 * ccline and put the previous value in prev_ccline.
5241 */
5242 static struct cmdline_info *
5243get_ccline_ptr()
5244{
5245 if ((State & CMDLINE) == 0)
5246 return NULL;
5247 if (ccline.cmdbuff != NULL)
5248 return &ccline;
5249 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5250 return &prev_ccline;
5251 return NULL;
5252}
5253
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254/*
5255 * Get the current command line in allocated memory.
5256 * Only works when the command line is being edited.
5257 * Returns NULL when something is wrong.
5258 */
5259 char_u *
5260get_cmdline_str()
5261{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005262 struct cmdline_info *p = get_ccline_ptr();
5263
5264 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005266 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267}
5268
5269/*
5270 * Get the current command line position, counted in bytes.
5271 * Zero is the first position.
5272 * Only works when the command line is being edited.
5273 * Returns -1 when something is wrong.
5274 */
5275 int
5276get_cmdline_pos()
5277{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005278 struct cmdline_info *p = get_ccline_ptr();
5279
5280 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005282 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283}
5284
5285/*
5286 * Set the command line byte position to "pos". Zero is the first position.
5287 * Only works when the command line is being edited.
5288 * Returns 1 when failed, 0 when OK.
5289 */
5290 int
5291set_cmdline_pos(pos)
5292 int pos;
5293{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005294 struct cmdline_info *p = get_ccline_ptr();
5295
5296 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 return 1;
5298
5299 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5300 * changed the command line. */
5301 if (pos < 0)
5302 new_cmdpos = 0;
5303 else
5304 new_cmdpos = pos;
5305 return 0;
5306}
5307
5308/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005309 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005310 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005311 * Only works when the command line is being edited.
5312 * Returns NUL when something is wrong.
5313 */
5314 int
5315get_cmdline_type()
5316{
5317 struct cmdline_info *p = get_ccline_ptr();
5318
5319 if (p == NULL)
5320 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005321 if (p->cmdfirstc == NUL)
5322 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005323 return p->cmdfirstc;
5324}
5325
5326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 * Calculate history index from a number:
5328 * num > 0: seen as identifying number of a history entry
5329 * num < 0: relative position in history wrt newest entry
5330 * "histype" may be one of the HIST_ values.
5331 */
5332 static int
5333calc_hist_idx(histype, num)
5334 int histype;
5335 int num;
5336{
5337 int i;
5338 histentry_T *hist;
5339 int wrapped = FALSE;
5340
5341 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5342 || (i = hisidx[histype]) < 0 || num == 0)
5343 return -1;
5344
5345 hist = history[histype];
5346 if (num > 0)
5347 {
5348 while (hist[i].hisnum > num)
5349 if (--i < 0)
5350 {
5351 if (wrapped)
5352 break;
5353 i += hislen;
5354 wrapped = TRUE;
5355 }
5356 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5357 return i;
5358 }
5359 else if (-num <= hislen)
5360 {
5361 i += num + 1;
5362 if (i < 0)
5363 i += hislen;
5364 if (hist[i].hisstr != NULL)
5365 return i;
5366 }
5367 return -1;
5368}
5369
5370/*
5371 * Get a history entry by its index.
5372 * "histype" may be one of the HIST_ values.
5373 */
5374 char_u *
5375get_history_entry(histype, idx)
5376 int histype;
5377 int idx;
5378{
5379 idx = calc_hist_idx(histype, idx);
5380 if (idx >= 0)
5381 return history[histype][idx].hisstr;
5382 else
5383 return (char_u *)"";
5384}
5385
5386/*
5387 * Clear all entries of a history.
5388 * "histype" may be one of the HIST_ values.
5389 */
5390 int
5391clr_history(histype)
5392 int histype;
5393{
5394 int i;
5395 histentry_T *hisptr;
5396
5397 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5398 {
5399 hisptr = history[histype];
5400 for (i = hislen; i--;)
5401 {
5402 vim_free(hisptr->hisstr);
5403 hisptr->hisnum = 0;
5404 hisptr++->hisstr = NULL;
5405 }
5406 hisidx[histype] = -1; /* mark history as cleared */
5407 hisnum[histype] = 0; /* reset identifier counter */
5408 return OK;
5409 }
5410 return FAIL;
5411}
5412
5413/*
5414 * Remove all entries matching {str} from a history.
5415 * "histype" may be one of the HIST_ values.
5416 */
5417 int
5418del_history_entry(histype, str)
5419 int histype;
5420 char_u *str;
5421{
5422 regmatch_T regmatch;
5423 histentry_T *hisptr;
5424 int idx;
5425 int i;
5426 int last;
5427 int found = FALSE;
5428
5429 regmatch.regprog = NULL;
5430 regmatch.rm_ic = FALSE; /* always match case */
5431 if (hislen != 0
5432 && histype >= 0
5433 && histype < HIST_COUNT
5434 && *str != NUL
5435 && (idx = hisidx[histype]) >= 0
5436 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5437 != NULL)
5438 {
5439 i = last = idx;
5440 do
5441 {
5442 hisptr = &history[histype][i];
5443 if (hisptr->hisstr == NULL)
5444 break;
5445 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5446 {
5447 found = TRUE;
5448 vim_free(hisptr->hisstr);
5449 hisptr->hisstr = NULL;
5450 hisptr->hisnum = 0;
5451 }
5452 else
5453 {
5454 if (i != last)
5455 {
5456 history[histype][last] = *hisptr;
5457 hisptr->hisstr = NULL;
5458 hisptr->hisnum = 0;
5459 }
5460 if (--last < 0)
5461 last += hislen;
5462 }
5463 if (--i < 0)
5464 i += hislen;
5465 } while (i != idx);
5466 if (history[histype][idx].hisstr == NULL)
5467 hisidx[histype] = -1;
5468 }
5469 vim_free(regmatch.regprog);
5470 return found;
5471}
5472
5473/*
5474 * Remove an indexed entry from a history.
5475 * "histype" may be one of the HIST_ values.
5476 */
5477 int
5478del_history_idx(histype, idx)
5479 int histype;
5480 int idx;
5481{
5482 int i, j;
5483
5484 i = calc_hist_idx(histype, idx);
5485 if (i < 0)
5486 return FALSE;
5487 idx = hisidx[histype];
5488 vim_free(history[histype][i].hisstr);
5489
5490 /* When deleting the last added search string in a mapping, reset
5491 * last_maptick, so that the last added search string isn't deleted again.
5492 */
5493 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5494 last_maptick = -1;
5495
5496 while (i != idx)
5497 {
5498 j = (i + 1) % hislen;
5499 history[histype][i] = history[histype][j];
5500 i = j;
5501 }
5502 history[histype][i].hisstr = NULL;
5503 history[histype][i].hisnum = 0;
5504 if (--i < 0)
5505 i += hislen;
5506 hisidx[histype] = i;
5507 return TRUE;
5508}
5509
5510#endif /* FEAT_EVAL */
5511
5512#if defined(FEAT_CRYPT) || defined(PROTO)
5513/*
5514 * Very specific function to remove the value in ":set key=val" from the
5515 * history.
5516 */
5517 void
5518remove_key_from_history()
5519{
5520 char_u *p;
5521 int i;
5522
5523 i = hisidx[HIST_CMD];
5524 if (i < 0)
5525 return;
5526 p = history[HIST_CMD][i].hisstr;
5527 if (p != NULL)
5528 for ( ; *p; ++p)
5529 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5530 {
5531 p = vim_strchr(p + 3, '=');
5532 if (p == NULL)
5533 break;
5534 ++p;
5535 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5536 if (p[i] == '\\' && p[i + 1])
5537 ++i;
5538 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5539 --p;
5540 }
5541}
5542#endif
5543
5544#endif /* FEAT_CMDHIST */
5545
5546#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5547/*
5548 * Get indices "num1,num2" that specify a range within a list (not a range of
5549 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5550 * Returns OK if parsed successfully, otherwise FAIL.
5551 */
5552 int
5553get_list_range(str, num1, num2)
5554 char_u **str;
5555 int *num1;
5556 int *num2;
5557{
5558 int len;
5559 int first = FALSE;
5560 long num;
5561
5562 *str = skipwhite(*str);
5563 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5564 {
5565 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5566 *str += len;
5567 *num1 = (int)num;
5568 first = TRUE;
5569 }
5570 *str = skipwhite(*str);
5571 if (**str == ',') /* parse "to" part of range */
5572 {
5573 *str = skipwhite(*str + 1);
5574 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5575 if (len > 0)
5576 {
5577 *num2 = (int)num;
5578 *str = skipwhite(*str + len);
5579 }
5580 else if (!first) /* no number given at all */
5581 return FAIL;
5582 }
5583 else if (first) /* only one number given */
5584 *num2 = *num1;
5585 return OK;
5586}
5587#endif
5588
5589#if defined(FEAT_CMDHIST) || defined(PROTO)
5590/*
5591 * :history command - print a history
5592 */
5593 void
5594ex_history(eap)
5595 exarg_T *eap;
5596{
5597 histentry_T *hist;
5598 int histype1 = HIST_CMD;
5599 int histype2 = HIST_CMD;
5600 int hisidx1 = 1;
5601 int hisidx2 = -1;
5602 int idx;
5603 int i, j, k;
5604 char_u *end;
5605 char_u *arg = eap->arg;
5606
5607 if (hislen == 0)
5608 {
5609 MSG(_("'history' option is zero"));
5610 return;
5611 }
5612
5613 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5614 {
5615 end = arg;
5616 while (ASCII_ISALPHA(*end)
5617 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5618 end++;
5619 i = *end;
5620 *end = NUL;
5621 histype1 = get_histtype(arg);
5622 if (histype1 == -1)
5623 {
5624 if (STRICMP(arg, "all") == 0)
5625 {
5626 histype1 = 0;
5627 histype2 = HIST_COUNT-1;
5628 }
5629 else
5630 {
5631 *end = i;
5632 EMSG(_(e_trailing));
5633 return;
5634 }
5635 }
5636 else
5637 histype2 = histype1;
5638 *end = i;
5639 }
5640 else
5641 end = arg;
5642 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5643 {
5644 EMSG(_(e_trailing));
5645 return;
5646 }
5647
5648 for (; !got_int && histype1 <= histype2; ++histype1)
5649 {
5650 STRCPY(IObuff, "\n # ");
5651 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5652 MSG_PUTS_TITLE(IObuff);
5653 idx = hisidx[histype1];
5654 hist = history[histype1];
5655 j = hisidx1;
5656 k = hisidx2;
5657 if (j < 0)
5658 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5659 if (k < 0)
5660 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5661 if (idx >= 0 && j <= k)
5662 for (i = idx + 1; !got_int; ++i)
5663 {
5664 if (i == hislen)
5665 i = 0;
5666 if (hist[i].hisstr != NULL
5667 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5668 {
5669 msg_putchar('\n');
5670 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5671 hist[i].hisnum);
5672 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5673 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5674 (int)Columns - 10);
5675 else
5676 STRCAT(IObuff, hist[i].hisstr);
5677 msg_outtrans(IObuff);
5678 out_flush();
5679 }
5680 if (i == idx)
5681 break;
5682 }
5683 }
5684}
5685#endif
5686
5687#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5688static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5689static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5690static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5691static int viminfo_add_at_front = FALSE;
5692
5693static int hist_type2char __ARGS((int type, int use_question));
5694
5695/*
5696 * Translate a history type number to the associated character.
5697 */
5698 static int
5699hist_type2char(type, use_question)
5700 int type;
5701 int use_question; /* use '?' instead of '/' */
5702{
5703 if (type == HIST_CMD)
5704 return ':';
5705 if (type == HIST_SEARCH)
5706 {
5707 if (use_question)
5708 return '?';
5709 else
5710 return '/';
5711 }
5712 if (type == HIST_EXPR)
5713 return '=';
5714 return '@';
5715}
5716
5717/*
5718 * Prepare for reading the history from the viminfo file.
5719 * This allocates history arrays to store the read history lines.
5720 */
5721 void
5722prepare_viminfo_history(asklen)
5723 int asklen;
5724{
5725 int i;
5726 int num;
5727 int type;
5728 int len;
5729
5730 init_history();
5731 viminfo_add_at_front = (asklen != 0);
5732 if (asklen > hislen)
5733 asklen = hislen;
5734
5735 for (type = 0; type < HIST_COUNT; ++type)
5736 {
5737 /*
5738 * Count the number of empty spaces in the history list. If there are
5739 * more spaces available than we request, then fill them up.
5740 */
5741 for (i = 0, num = 0; i < hislen; i++)
5742 if (history[type][i].hisstr == NULL)
5743 num++;
5744 len = asklen;
5745 if (num > len)
5746 len = num;
5747 if (len <= 0)
5748 viminfo_history[type] = NULL;
5749 else
5750 viminfo_history[type] =
5751 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5752 if (viminfo_history[type] == NULL)
5753 len = 0;
5754 viminfo_hislen[type] = len;
5755 viminfo_hisidx[type] = 0;
5756 }
5757}
5758
5759/*
5760 * Accept a line from the viminfo, store it in the history array when it's
5761 * new.
5762 */
5763 int
5764read_viminfo_history(virp)
5765 vir_T *virp;
5766{
5767 int type;
5768 long_u len;
5769 char_u *val;
5770 char_u *p;
5771
5772 type = hist_char2type(virp->vir_line[0]);
5773 if (viminfo_hisidx[type] < viminfo_hislen[type])
5774 {
5775 val = viminfo_readstring(virp, 1, TRUE);
5776 if (val != NULL && *val != NUL)
5777 {
5778 if (!in_history(type, val + (type == HIST_SEARCH),
5779 viminfo_add_at_front))
5780 {
5781 /* Need to re-allocate to append the separator byte. */
5782 len = STRLEN(val);
5783 p = lalloc(len + 2, TRUE);
5784 if (p != NULL)
5785 {
5786 if (type == HIST_SEARCH)
5787 {
5788 /* Search entry: Move the separator from the first
5789 * column to after the NUL. */
5790 mch_memmove(p, val + 1, (size_t)len);
5791 p[len] = (*val == ' ' ? NUL : *val);
5792 }
5793 else
5794 {
5795 /* Not a search entry: No separator in the viminfo
5796 * file, add a NUL separator. */
5797 mch_memmove(p, val, (size_t)len + 1);
5798 p[len + 1] = NUL;
5799 }
5800 viminfo_history[type][viminfo_hisidx[type]++] = p;
5801 }
5802 }
5803 }
5804 vim_free(val);
5805 }
5806 return viminfo_readline(virp);
5807}
5808
5809 void
5810finish_viminfo_history()
5811{
5812 int idx;
5813 int i;
5814 int type;
5815
5816 for (type = 0; type < HIST_COUNT; ++type)
5817 {
5818 if (history[type] == NULL)
5819 return;
5820 idx = hisidx[type] + viminfo_hisidx[type];
5821 if (idx >= hislen)
5822 idx -= hislen;
5823 else if (idx < 0)
5824 idx = hislen - 1;
5825 if (viminfo_add_at_front)
5826 hisidx[type] = idx;
5827 else
5828 {
5829 if (hisidx[type] == -1)
5830 hisidx[type] = hislen - 1;
5831 do
5832 {
5833 if (history[type][idx].hisstr != NULL)
5834 break;
5835 if (++idx == hislen)
5836 idx = 0;
5837 } while (idx != hisidx[type]);
5838 if (idx != hisidx[type] && --idx < 0)
5839 idx = hislen - 1;
5840 }
5841 for (i = 0; i < viminfo_hisidx[type]; i++)
5842 {
5843 vim_free(history[type][idx].hisstr);
5844 history[type][idx].hisstr = viminfo_history[type][i];
5845 if (--idx < 0)
5846 idx = hislen - 1;
5847 }
5848 idx += 1;
5849 idx %= hislen;
5850 for (i = 0; i < viminfo_hisidx[type]; i++)
5851 {
5852 history[type][idx++].hisnum = ++hisnum[type];
5853 idx %= hislen;
5854 }
5855 vim_free(viminfo_history[type]);
5856 viminfo_history[type] = NULL;
5857 }
5858}
5859
5860 void
5861write_viminfo_history(fp)
5862 FILE *fp;
5863{
5864 int i;
5865 int type;
5866 int num_saved;
5867 char_u *p;
5868 int c;
5869
5870 init_history();
5871 if (hislen == 0)
5872 return;
5873 for (type = 0; type < HIST_COUNT; ++type)
5874 {
5875 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5876 if (num_saved == 0)
5877 continue;
5878 if (num_saved < 0) /* Use default */
5879 num_saved = hislen;
5880 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5881 type == HIST_CMD ? _("Command Line") :
5882 type == HIST_SEARCH ? _("Search String") :
5883 type == HIST_EXPR ? _("Expression") :
5884 _("Input Line"));
5885 if (num_saved > hislen)
5886 num_saved = hislen;
5887 i = hisidx[type];
5888 if (i >= 0)
5889 while (num_saved--)
5890 {
5891 p = history[type][i].hisstr;
5892 if (p != NULL)
5893 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005894 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 /* For the search history: put the separator in the second
5896 * column; use a space if there isn't one. */
5897 if (type == HIST_SEARCH)
5898 {
5899 c = p[STRLEN(p) + 1];
5900 putc(c == NUL ? ' ' : c, fp);
5901 }
5902 viminfo_writestring(fp, p);
5903 }
5904 if (--i < 0)
5905 i = hislen - 1;
5906 }
5907 }
5908}
5909#endif /* FEAT_VIMINFO */
5910
5911#if defined(FEAT_FKMAP) || defined(PROTO)
5912/*
5913 * Write a character at the current cursor+offset position.
5914 * It is directly written into the command buffer block.
5915 */
5916 void
5917cmd_pchar(c, offset)
5918 int c, offset;
5919{
5920 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5921 {
5922 EMSG(_("E198: cmd_pchar beyond the command length"));
5923 return;
5924 }
5925 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5926 ccline.cmdbuff[ccline.cmdlen] = NUL;
5927}
5928
5929 int
5930cmd_gchar(offset)
5931 int offset;
5932{
5933 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5934 {
5935 /* EMSG(_("cmd_gchar beyond the command length")); */
5936 return NUL;
5937 }
5938 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5939}
5940#endif
5941
5942#if defined(FEAT_CMDWIN) || defined(PROTO)
5943/*
5944 * Open a window on the current command line and history. Allow editing in
5945 * the window. Returns when the window is closed.
5946 * Returns:
5947 * CR if the command is to be executed
5948 * Ctrl_C if it is to be abandoned
5949 * K_IGNORE if editing continues
5950 */
5951 static int
5952ex_window()
5953{
5954 struct cmdline_info save_ccline;
5955 buf_T *old_curbuf = curbuf;
5956 win_T *old_curwin = curwin;
5957 buf_T *bp;
5958 win_T *wp;
5959 int i;
5960 linenr_T lnum;
5961 int histtype;
5962 garray_T winsizes;
5963 char_u typestr[2];
5964 int save_restart_edit = restart_edit;
5965 int save_State = State;
5966 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005967#ifdef FEAT_RIGHTLEFT
5968 int save_cmdmsg_rl = cmdmsg_rl;
5969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
5971 /* Can't do this recursively. Can't do it when typing a password. */
5972 if (cmdwin_type != 0
5973# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5974 || cmdline_star > 0
5975# endif
5976 )
5977 {
5978 beep_flush();
5979 return K_IGNORE;
5980 }
5981
5982 /* Save current window sizes. */
5983 win_size_save(&winsizes);
5984
5985# ifdef FEAT_AUTOCMD
5986 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005987 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005989 /* don't use a new tab page */
5990 cmdmod.tab = 0;
5991
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 /* Create a window for the command-line buffer. */
5993 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5994 {
5995 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005996# ifdef FEAT_AUTOCMD
5997 unblock_autocmds();
5998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 return K_IGNORE;
6000 }
6001 cmdwin_type = ccline.cmdfirstc;
6002 if (cmdwin_type == NUL)
6003 cmdwin_type = '-';
6004
6005 /* Create the command-line buffer empty. */
6006 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
6007 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
6008 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6009 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6010 curbuf->b_p_ma = TRUE;
6011# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006012 curwin->w_p_rl = cmdmsg_rl;
6013 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014# endif
6015# ifdef FEAT_SCROLLBIND
6016 curwin->w_p_scb = FALSE;
6017# endif
6018
6019# ifdef FEAT_AUTOCMD
6020 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006021 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022# endif
6023
Bram Moolenaar46152342005-09-07 21:18:43 +00006024 /* Showing the prompt may have set need_wait_return, reset it. */
6025 need_wait_return = FALSE;
6026
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 histtype = hist_char2type(ccline.cmdfirstc);
6028 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6029 {
6030 if (p_wc == TAB)
6031 {
6032 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6033 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6034 }
6035 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6036 }
6037
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006038 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6039 * sets 'textwidth' to 78). */
6040 curbuf->b_p_tw = 0;
6041
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 /* Fill the buffer with the history. */
6043 init_history();
6044 if (hislen > 0)
6045 {
6046 i = hisidx[histtype];
6047 if (i >= 0)
6048 {
6049 lnum = 0;
6050 do
6051 {
6052 if (++i == hislen)
6053 i = 0;
6054 if (history[histtype][i].hisstr != NULL)
6055 ml_append(lnum++, history[histtype][i].hisstr,
6056 (colnr_T)0, FALSE);
6057 }
6058 while (i != hisidx[histtype]);
6059 }
6060 }
6061
6062 /* Replace the empty last line with the current command-line and put the
6063 * cursor there. */
6064 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6065 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6066 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006067 changed_line_abv_curs();
6068 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006069 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070
6071 /* Save the command line info, can be used recursively. */
6072 save_ccline = ccline;
6073 ccline.cmdbuff = NULL;
6074 ccline.cmdprompt = NULL;
6075
6076 /* No Ex mode here! */
6077 exmode_active = 0;
6078
6079 State = NORMAL;
6080# ifdef FEAT_MOUSE
6081 setmouse();
6082# endif
6083
6084# ifdef FEAT_AUTOCMD
6085 /* Trigger CmdwinEnter autocommands. */
6086 typestr[0] = cmdwin_type;
6087 typestr[1] = NUL;
6088 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006089 if (restart_edit != 0) /* autocmd with ":startinsert" */
6090 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091# endif
6092
6093 i = RedrawingDisabled;
6094 RedrawingDisabled = 0;
6095
6096 /*
6097 * Call the main loop until <CR> or CTRL-C is typed.
6098 */
6099 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006100 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101
6102 RedrawingDisabled = i;
6103
6104# ifdef FEAT_AUTOCMD
6105 /* Trigger CmdwinLeave autocommands. */
6106 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6107# endif
6108
Bram Moolenaarccc18222007-05-10 18:25:20 +00006109 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 ccline = save_ccline;
6111 cmdwin_type = 0;
6112
6113 exmode_active = save_exmode;
6114
Bram Moolenaarf9821062008-06-20 16:31:07 +00006115 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 * this happens! */
6117 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6118 {
6119 cmdwin_result = Ctrl_C;
6120 EMSG(_("E199: Active window or buffer deleted"));
6121 }
6122 else
6123 {
6124# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6125 /* autocmds may abort script processing */
6126 if (aborting() && cmdwin_result != K_IGNORE)
6127 cmdwin_result = Ctrl_C;
6128# endif
6129 /* Set the new command line from the cmdline buffer. */
6130 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006131 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006133 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6134
6135 if (histtype == HIST_CMD)
6136 {
6137 /* Execute the command directly. */
6138 ccline.cmdbuff = vim_strsave((char_u *)p);
6139 cmdwin_result = CAR;
6140 }
6141 else
6142 {
6143 /* First need to cancel what we were doing. */
6144 ccline.cmdbuff = NULL;
6145 stuffcharReadbuff(':');
6146 stuffReadbuff((char_u *)p);
6147 stuffcharReadbuff(CAR);
6148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 }
6150 else if (cmdwin_result == K_XF2) /* :qa typed */
6151 {
6152 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6153 cmdwin_result = CAR;
6154 }
6155 else
6156 ccline.cmdbuff = vim_strsave(ml_get_curline());
6157 if (ccline.cmdbuff == NULL)
6158 cmdwin_result = Ctrl_C;
6159 else
6160 {
6161 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6162 ccline.cmdbufflen = ccline.cmdlen + 1;
6163 ccline.cmdpos = curwin->w_cursor.col;
6164 if (ccline.cmdpos > ccline.cmdlen)
6165 ccline.cmdpos = ccline.cmdlen;
6166 if (cmdwin_result == K_IGNORE)
6167 {
6168 set_cmdspos_cursor();
6169 redrawcmd();
6170 }
6171 }
6172
6173# ifdef FEAT_AUTOCMD
6174 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006175 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176# endif
6177 wp = curwin;
6178 bp = curbuf;
6179 win_goto(old_curwin);
6180 win_close(wp, TRUE);
6181 close_buffer(NULL, bp, DOBUF_WIPE);
6182
6183 /* Restore window sizes. */
6184 win_size_restore(&winsizes);
6185
6186# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006187 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188# endif
6189 }
6190
6191 ga_clear(&winsizes);
6192 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006193# ifdef FEAT_RIGHTLEFT
6194 cmdmsg_rl = save_cmdmsg_rl;
6195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
6197 State = save_State;
6198# ifdef FEAT_MOUSE
6199 setmouse();
6200# endif
6201
6202 return cmdwin_result;
6203}
6204#endif /* FEAT_CMDWIN */
6205
6206/*
6207 * Used for commands that either take a simple command string argument, or:
6208 * cmd << endmarker
6209 * {script}
6210 * endmarker
6211 * Returns a pointer to allocated memory with {script} or NULL.
6212 */
6213 char_u *
6214script_get(eap, cmd)
6215 exarg_T *eap;
6216 char_u *cmd;
6217{
6218 char_u *theline;
6219 char *end_pattern = NULL;
6220 char dot[] = ".";
6221 garray_T ga;
6222
6223 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6224 return NULL;
6225
6226 ga_init2(&ga, 1, 0x400);
6227
6228 if (cmd[2] != NUL)
6229 end_pattern = (char *)skipwhite(cmd + 2);
6230 else
6231 end_pattern = dot;
6232
6233 for (;;)
6234 {
6235 theline = eap->getline(
6236#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006237 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238#endif
6239 NUL, eap->cookie, 0);
6240
6241 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
6242 break;
6243
6244 ga_concat(&ga, theline);
6245 ga_append(&ga, '\n');
6246 vim_free(theline);
6247 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006248 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
6250 return (char_u *)ga.ga_data;
6251}